From 6265a600b1779639b40b80ba1a8fd426d1fec7a5 Mon Sep 17 00:00:00 2001
From: Ondrej Jirman <megi@xff.cz>
Date: Sat, 2 Apr 2022 03:19:36 +0200
Subject: [PATCH 417/484] power: supply: ip5xxx: Add ip5xxx-usb supply

This supply represents presnece of the USB power supply on VIN.

Signed-off-by: Ondrej Jirman <megi@xff.cz>
---
 drivers/power/supply/ip5xxx_power.c | 49 +++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)

diff --git a/drivers/power/supply/ip5xxx_power.c b/drivers/power/supply/ip5xxx_power.c
index 1424fe4a0d15..78b5782f6bb6 100644
--- a/drivers/power/supply/ip5xxx_power.c
+++ b/drivers/power/supply/ip5xxx_power.c
@@ -40,6 +40,8 @@ struct ip5xxx {
 			struct regmap_field *timeout;
 			/* Overvoltage limit */
 			struct regmap_field *vin_overvolt;
+			/* VIN present */
+			struct regmap_field *vin_present;
 		} charger;
 		struct {
 			/* Boost converter status */
@@ -127,6 +129,7 @@ struct ip5xxx_regfield_config {
 	const struct reg_field charger_chg_end;
 	const struct reg_field charger_timeout;
 	const struct reg_field charger_vin_overvolt;
+	const struct reg_field charger_vin_present;
 	const struct reg_field boost_status;
 	const struct reg_field boost_enable;
 	const struct reg_field boost_llshdn_enable;
@@ -739,6 +742,45 @@ static const struct power_supply_desc ip5xxx_boost_desc = {
 	.property_is_writeable	= ip5xxx_boost_property_is_writeable,
 };
 
+static const enum power_supply_property ip5xxx_usb_properties[] = {
+	POWER_SUPPLY_PROP_PRESENT,
+};
+
+static int ip5xxx_usb_get_property(struct power_supply *psy,
+				     enum power_supply_property psp,
+				     union power_supply_propval *val)
+{
+	struct ip5xxx *ip5xxx = power_supply_get_drvdata(psy);
+	unsigned int rval;
+	int ret;
+
+	ret = ip5xxx_initialize(psy);
+	if (ret)
+		return ret;
+
+	switch (psp) {
+	case POWER_SUPPLY_PROP_PRESENT:
+		ret = ip5xxx_read(ip5xxx, ip5xxx->regs.charger.vin_present, &rval);
+		if (ret)
+			return ret;
+
+		val->intval = !!rval;
+		return 0;
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static const struct power_supply_desc ip5xxx_usb_desc = {
+	.name			= "ip5xxx-usb",
+	.type			= POWER_SUPPLY_TYPE_USB,
+	.properties		= ip5xxx_usb_properties,
+	.num_properties		= ARRAY_SIZE(ip5xxx_usb_properties),
+	.get_property		= ip5xxx_usb_get_property,
+	.property_is_writeable	= ip5xxx_boost_property_is_writeable,
+};
+
 static const struct regmap_config ip5xxx_regmap_config = {
 	.reg_bits		= 8,
 	.val_bits		= 8,
@@ -753,6 +795,7 @@ static struct ip5xxx_regfield_config ip51xx_fields = {
 	.charger_chg_end = REG_FIELD(0x71, 3, 3),
 	.charger_timeout = REG_FIELD(0x71, 0, 2),
 	.charger_vin_overvolt = REG_FIELD(0x72, 5, 5),
+	.charger_vin_present = REG_FIELD(0x70, 4, 4),
 	.boost_status = REG_FIELD(0x70, 2, 2),
 	.boost_enable = REG_FIELD(0x01, 2, 2),
 	.boost_llshdn_enable = REG_FIELD(0x02, 1, 1),
@@ -794,6 +837,7 @@ static struct ip5xxx_regfield_config ip5306_fields = {
 	.charger_chg_end = REG_FIELD(0x71, 3, 3),
 	.charger_timeout = REG_FIELD_UNSUPPORTED,
 	.charger_vin_overvolt = REG_FIELD_UNSUPPORTED,
+	.charger_vin_present = REG_FIELD_UNSUPPORTED,
 	.boost_status = REG_FIELD_UNSUPPORTED,
 	.boost_enable = REG_FIELD(0x00, 5, 5),
 	.boost_llshdn_enable = REG_FIELD_UNSUPPORTED,
@@ -850,6 +894,7 @@ static void ip5xxx_setup_regs(struct device *dev, struct ip5xxx *ip5xxx,
 	ip5xxx_setup_reg(charger_chg_end, charger.chg_end);
 	ip5xxx_setup_reg(charger_timeout, charger.timeout);
 	ip5xxx_setup_reg(charger_vin_overvolt, charger.vin_overvolt);
+	ip5xxx_setup_reg(charger_vin_present, charger.vin_present);
 	ip5xxx_setup_reg(boost_enable, boost.enable);
 	ip5xxx_setup_reg(boost_status, boost.status);
 	ip5xxx_setup_reg(boost_llshdn_enable, boost.light_load_shutdown.enable);
@@ -916,6 +961,10 @@ static int ip5xxx_power_probe(struct i2c_client *client)
 	if (IS_ERR(psy))
 		return PTR_ERR(psy);
 
+	psy = devm_power_supply_register(dev, &ip5xxx_usb_desc, &psy_cfg);
+	if (IS_ERR(psy))
+		return PTR_ERR(psy);
+
 	ret = power_supply_get_battery_info(psy, &ip5xxx->bat);
 	if (ret)
 		return dev_err_probe(dev, ret, "Failed to get battery info\n");
-- 
2.49.0

