From 6cff5e96b4ce3816bfd0361c8425038a372b0832 Mon Sep 17 00:00:00 2001
From: Ondrej Jirman <megi@xff.cz>
Date: Sat, 2 Apr 2022 03:19:36 +0200
Subject: [PATCH 394/463] 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 | 44 +++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)

diff --git a/drivers/power/supply/ip5xxx_power.c b/drivers/power/supply/ip5xxx_power.c
index b1f820efa7414..c567244679977 100644
--- a/drivers/power/supply/ip5xxx_power.c
+++ b/drivers/power/supply/ip5xxx_power.c
@@ -46,6 +46,7 @@
 #define IP5XXX_GPIO_CTL3		0x55
 #define IP5XXX_STATUS			0x70
 #define IP5XXX_STATUS_BOOST_ON			BIT(2)
+#define IP5XXX_STATUS_VIN_PRESENT		BIT(4)
 #define IP5XXX_READ0			0x71
 #define IP5XXX_READ0_CHG_STAT			GENMASK(7, 5)
 #define IP5XXX_READ0_CHG_STAT_IDLE		(0x0 << 5)
@@ -632,6 +633,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_STATUS, &rval);
+		if (ret)
+			return ret;
+
+		val->intval = !!(rval & IP5XXX_STATUS_VIN_PRESENT);
+		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,
@@ -665,6 +705,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.46.0

