diff mbox series

[RFC,3/6] power: supply: core: implement extension API

Message ID 20240606-power-supply-extensions-v1-3-b45669290bdc@weissschuh.net (mailing list archive)
State Handled Elsewhere, archived
Headers show
Series power: supply: extension API | expand

Commit Message

Thomas Weißschuh June 6, 2024, 2:50 p.m. UTC
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 drivers/power/supply/power_supply.h       |  6 +-
 drivers/power/supply/power_supply_core.c  | 93 ++++++++++++++++++++++++++++---
 drivers/power/supply/power_supply_hwmon.c |  2 +-
 drivers/power/supply/power_supply_sysfs.c | 22 +++++++-
 include/linux/power_supply.h              | 23 ++++++++
 5 files changed, 132 insertions(+), 14 deletions(-)
diff mbox series

Patch

diff --git a/drivers/power/supply/power_supply.h b/drivers/power/supply/power_supply.h
index 622be1f0a180..7f9e139064cc 100644
--- a/drivers/power/supply/power_supply.h
+++ b/drivers/power/supply/power_supply.h
@@ -13,8 +13,10 @@  struct device;
 struct device_type;
 struct power_supply;
 
-extern bool power_supply_has_property(const struct power_supply_desc *psy_desc,
-				      enum power_supply_property psp);
+bool power_supply_has_property(const struct power_supply *psy,
+			       enum power_supply_property psp);
+bool power_supply_ext_has_property(const struct power_supply_ext *psy_ext,
+				   enum power_supply_property psp);
 
 #ifdef CONFIG_SYSFS
 
diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c
index d57ecdd966e0..b32bbca9a848 100644
--- a/drivers/power/supply/power_supply_core.c
+++ b/drivers/power/supply/power_supply_core.c
@@ -1183,8 +1183,8 @@  bool power_supply_battery_bti_in_range(struct power_supply_battery_info *info,
 }
 EXPORT_SYMBOL_GPL(power_supply_battery_bti_in_range);
 
-bool power_supply_has_property(const struct power_supply_desc *psy_desc,
-			       enum power_supply_property psp)
+static bool psy_desc_has_property(const struct power_supply_desc *psy_desc,
+				  enum power_supply_property psp)
 {
 	bool found = false;
 	int i;
@@ -1199,6 +1199,33 @@  bool power_supply_has_property(const struct power_supply_desc *psy_desc,
 	return found;
 }
 
+bool power_supply_ext_has_property(const struct power_supply_ext *psy_ext,
+				   enum power_supply_property psp)
+{
+	bool found = false;
+	int i;
+
+	if (!psy_ext)
+		return false;
+
+	for (i = 0; i < psy_ext->num_properties; i++) {
+		if (psy_ext->properties[i] == psp) {
+			found = true;
+			break;
+		}
+	}
+
+	return found;
+}
+
+bool power_supply_has_property(const struct power_supply *psy,
+			       enum power_supply_property psp)
+{
+	if (power_supply_ext_has_property(psy->ext, psp))
+		return true;
+	return psy_desc_has_property(psy->desc, psp);
+}
+
 int power_supply_get_property(struct power_supply *psy,
 			    enum power_supply_property psp,
 			    union power_supply_propval *val)
@@ -1209,7 +1236,9 @@  int power_supply_get_property(struct power_supply *psy,
 		return -ENODEV;
 	}
 
-	if (power_supply_has_property(psy->desc, psp))
+	if (power_supply_ext_has_property(psy->ext, psp))
+		return psy->ext->get_property(psy, psp, val);
+	else if (psy_desc_has_property(psy->desc, psp))
 		return psy->desc->get_property(psy, psp, val);
 	else if (power_supply_battery_info_has_prop(psy->battery_info, psp))
 		return power_supply_battery_info_get_prop(psy->battery_info, psp, val);
@@ -1222,7 +1251,17 @@  int power_supply_set_property(struct power_supply *psy,
 			    enum power_supply_property psp,
 			    const union power_supply_propval *val)
 {
-	if (atomic_read(&psy->use_cnt) <= 0 || !psy->desc->set_property)
+	if (atomic_read(&psy->use_cnt) <= 0)
+		return -ENODEV;
+
+	if (power_supply_ext_has_property(psy->ext, psp)) {
+		if (psy->ext->set_property)
+			return psy->ext->set_property(psy, psp, val);
+		else
+			return -ENODEV;
+	}
+
+	if (!psy->desc->set_property)
 		return -ENODEV;
 
 	return psy->desc->set_property(psy, psp, val);
@@ -1232,8 +1271,17 @@  EXPORT_SYMBOL_GPL(power_supply_set_property);
 int power_supply_property_is_writeable(struct power_supply *psy,
 					enum power_supply_property psp)
 {
-	if (atomic_read(&psy->use_cnt) <= 0 ||
-			!psy->desc->property_is_writeable)
+	if (atomic_read(&psy->use_cnt) <= 0)
+		return -ENODEV;
+
+	if (power_supply_ext_has_property(psy->ext, psp)) {
+		if (psy->ext->property_is_writeable)
+			return psy->ext->property_is_writeable(psy, psp);
+		else
+			return -ENODEV;
+	}
+
+	if (!psy->desc->property_is_writeable)
 		return -ENODEV;
 
 	return psy->desc->property_is_writeable(psy, psp);
@@ -1256,6 +1304,35 @@  int power_supply_powers(struct power_supply *psy, struct device *dev)
 }
 EXPORT_SYMBOL_GPL(power_supply_powers);
 
+static int power_supply_update_groups(struct power_supply *psy)
+{
+	int ret;
+
+	ret = sysfs_update_groups(&psy->dev.kobj, power_supply_dev_type.groups);
+	power_supply_changed(psy);
+	return ret;
+}
+
+int power_supply_register_extension(struct power_supply *psy, const struct power_supply_ext *ext)
+{
+	if (psy->ext)
+		return -EEXIST;
+
+	psy->ext = ext;
+	return power_supply_update_groups(psy);
+}
+EXPORT_SYMBOL_GPL(power_supply_register_extension);
+
+void power_supply_unregister_extension(struct power_supply *psy, const struct power_supply_ext *ext)
+{
+	if (psy->ext != ext)
+		dev_warn(&psy->dev, "Trying to unregister invalid extension");
+
+	psy->ext = NULL;
+	power_supply_update_groups(psy);
+}
+EXPORT_SYMBOL_GPL(power_supply_unregister_extension);
+
 static void power_supply_dev_release(struct device *dev)
 {
 	struct power_supply *psy = to_power_supply(dev);
@@ -1308,7 +1385,7 @@  static int psy_register_thermal(struct power_supply *psy)
 		return 0;
 
 	/* Register battery zone device psy reports temperature */
-	if (power_supply_has_property(psy->desc, POWER_SUPPLY_PROP_TEMP)) {
+	if (power_supply_has_property(psy, POWER_SUPPLY_PROP_TEMP)) {
 		/* Prefer our hwmon device and avoid duplicates */
 		struct thermal_zone_params tzp = {
 			.no_hwmon = IS_ENABLED(CONFIG_POWER_SUPPLY_HWMON)
@@ -1361,7 +1438,7 @@  __power_supply_register(struct device *parent,
 		pr_warn("%s: Expected proper parent device for '%s'\n",
 			__func__, desc->name);
 
-	if (power_supply_has_property(desc, POWER_SUPPLY_PROP_USB_TYPE) &&
+	if (psy_desc_has_property(desc, POWER_SUPPLY_PROP_USB_TYPE) &&
 	    (!desc->usb_types || !desc->num_usb_types))
 		return ERR_PTR(-EINVAL);
 
diff --git a/drivers/power/supply/power_supply_hwmon.c b/drivers/power/supply/power_supply_hwmon.c
index 2ecbe4a74c25..8cb852a734b1 100644
--- a/drivers/power/supply/power_supply_hwmon.c
+++ b/drivers/power/supply/power_supply_hwmon.c
@@ -374,7 +374,7 @@  int power_supply_add_hwmon_sysfs(struct power_supply *psy)
 	for (i = 0; i < ARRAY_SIZE(power_supply_hwmon_props); i++) {
 		const enum power_supply_property prop = power_supply_hwmon_props[i];
 
-		if (power_supply_has_property(psy->desc, prop))
+		if (power_supply_has_property(psy, prop))
 			set_bit(prop, psyhw->props);
 	}
 
diff --git a/drivers/power/supply/power_supply_sysfs.c b/drivers/power/supply/power_supply_sysfs.c
index abd44ebfe6fe..3487f161e9bf 100644
--- a/drivers/power/supply/power_supply_sysfs.c
+++ b/drivers/power/supply/power_supply_sysfs.c
@@ -304,8 +304,12 @@  static ssize_t power_supply_show_property(struct device *dev,
 						&value, buf);
 		break;
 	case POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR:
-		ret = power_supply_charge_behaviour_show(dev, psy->desc->charge_behaviours,
-							 value.intval, buf);
+		if (power_supply_ext_has_property(psy->ext, POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR))
+			ret = power_supply_charge_behaviour_show(dev, psy->ext->charge_behaviours,
+								 value.intval, buf);
+		else
+			ret = power_supply_charge_behaviour_show(dev, psy->desc->charge_behaviours,
+								 value.intval, buf);
 		break;
 	case POWER_SUPPLY_PROP_MODEL_NAME ... POWER_SUPPLY_PROP_SERIAL_NUMBER:
 		ret = sysfs_emit(buf, "%s\n", value.strval);
@@ -374,7 +378,7 @@  static umode_t power_supply_attr_is_visible(struct kobject *kobj,
 	if (attrno == POWER_SUPPLY_PROP_TYPE)
 		return mode;
 
-	if (power_supply_has_property(psy->desc, attrno)) {
+	if (power_supply_has_property(psy, attrno)) {
 		if (power_supply_property_is_writeable(psy, attrno) > 0)
 			mode |= S_IWUSR;
 		return mode;
@@ -486,7 +490,19 @@  int power_supply_uevent(const struct device *dev, struct kobj_uevent_env *env)
 	if (ret)
 		goto out;
 
+	if (psy->ext) {
+		for (j = 0; j < psy->ext->num_properties; j++) {
+			set_bit(psy->ext->properties[j], psy_drv_properties);
+			ret = add_prop_uevent(dev, env, psy->ext->properties[j],
+					      prop_buf);
+			if (ret)
+				goto out;
+		}
+	}
+
 	for (j = 0; j < psy->desc->num_properties; j++) {
+		if (test_bit(psy->desc->properties[j], psy_drv_properties))
+			continue;
 		set_bit(psy->desc->properties[j], psy_drv_properties);
 		ret = add_prop_uevent(dev, env, psy->desc->properties[j],
 				      prop_buf);
diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h
index 8e5705a56b85..0c1e23081d2a 100644
--- a/include/linux/power_supply.h
+++ b/include/linux/power_supply.h
@@ -280,6 +280,22 @@  struct power_supply_desc {
 	int use_for_apm;
 };
 
+struct power_supply_ext {
+	u8 charge_behaviours;
+	const enum power_supply_property *properties;
+	size_t num_properties;
+
+	int (*get_property)(struct power_supply *psy,
+			    enum power_supply_property psp,
+			    union power_supply_propval *val);
+	int (*set_property)(struct power_supply *psy,
+			    enum power_supply_property psp,
+			    const union power_supply_propval *val);
+
+	int (*property_is_writeable)(struct power_supply *psy,
+				     enum power_supply_property psp);
+};
+
 struct power_supply {
 	const struct power_supply_desc *desc;
 
@@ -290,6 +306,8 @@  struct power_supply {
 	size_t num_supplies;
 	struct device_node *of_node;
 
+	const struct power_supply_ext *ext;
+
 	/* Driver private data */
 	void *drv_data;
 
@@ -892,6 +910,11 @@  devm_power_supply_register_no_ws(struct device *parent,
 extern void power_supply_unregister(struct power_supply *psy);
 extern int power_supply_powers(struct power_supply *psy, struct device *dev);
 
+extern int power_supply_register_extension(struct power_supply *psy,
+					   const struct power_supply_ext *ext);
+extern void power_supply_unregister_extension(struct power_supply *psy,
+					      const struct power_supply_ext *ext);
+
 #define to_power_supply(device) container_of(device, struct power_supply, dev)
 
 extern void *power_supply_get_drvdata(struct power_supply *psy);