Skip to contentSkip to conversation

power: supply: ug3105_battery: Switch to power_supply_batinfo_ocv2cap()

PATCH Applied Hans de Goede 3 participants 15 messages Jun 8, 2025 – Jul 6, 2025

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover power: supply: ug3105_battery: Switch to power_supply_batinfo_ocv2cap()

Hi All,

This series converts the ug3105_battery driver from using a static ovc - capcity mapping table + its own DIY code to using the ovc-capacity-table(s) from the battery fwnode and using the power_supply_batinfo_ocv2cap() helper.

Since the ug3105_battery driver is used on ACPI platforms this relies on sre's "[PATCH v2 0/5] power: supply: core: convert to fwnode" series for completenes sake I've added a copy of that series + 2 fixup patches to "[PATCH v2 3/5] power: supply: core: battery-info: fully switch to fwnode", the original patches are unmodified.

While working on this I also noticed somewhat of a memleak in the bq24190 driver, so the final patch in the series is a fix for that.

Regards,

Hans

Hans de Goede (5):
fixup! power: supply: core: battery-info: fully switch to fwnode fixup! power: supply: core: battery-info: fully switch to fwnode
power: supply: ug3105_battery: Use psy->battery_info
power: supply: ug3105_battery: Switch to power_supply_batinfo_ocv2cap()
power: supply: bq24190: Free battery_info

Sebastian Reichel (5):
regulator: act8865-regulator: switch psy_cfg from of_node to fwnode
power: supply: core: remove of_node from power_supply_config
power: supply: core: battery-info: fully switch to fwnode
power: supply: core: convert to fwnnode
power: supply: core: rename power_supply_get_by_phandle to
power_supply_get_by_reference

 drivers/phy/allwinner/phy-sun4i-usb.c    |   2 +-
 drivers/power/supply/bq2415x_charger.c   |   2 +-
 drivers/power/supply/bq24190_charger.c   |   2 +
 drivers/power/supply/power_supply_core.c | 185 ++++++++++++-----------
 drivers/power/supply/ug3105_battery.c    |  81 ++--------
 drivers/regulator/act8865-regulator.c    |   2 +-
 include/linux/power_supply.h             |  16 +-
 7 files changed, 118 insertions(+), 172 deletions(-)
Signature · 1 line
2.49.0
#1 lore raw

PATCH 1/10 regulator: act8865-regulator: switch psy_cfg from of_node to fwnode

From: Sebastian Reichel <[email protected]>

In order to remove .of_node from the power_supply_config struct, use .fwnode instead.

---
 drivers/regulator/act8865-regulator.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/regulator/act8865-regulator.c +1 -1
index 0457af23c55a..b2a6ddc6f56d 100644
@@ -643,7 +643,7 @@ static int act8600_charger_probe(struct device *dev, struct regmap *regmap)
 	struct power_supply *charger;
 	struct power_supply_config cfg = {
 		.drv_data = regmap,
-		.of_node = dev->of_node,
+		.fwnode = dev_fwnode(dev),
 	};
 
 	charger = devm_power_supply_register(dev, &act8600_charger_desc, &cfg);
Signature · 1 line
2.49.0
#2 lore raw

PATCH 2/10 power: supply: core: remove of_node from power_supply_config

In reply to Hans de Goede

From: Sebastian Reichel <[email protected]>

All drivers have been migrated from .of_node to .fwnode, so let's kill the former.

---
 drivers/power/supply/power_supply_core.c | 3 +--
 include/linux/power_supply.h             | 1 -
 2 files changed, 1 insertion(+), 3 deletions(-)
drivers/power/supply/power_supply_core.c +1 -2
index 33a5bfce4604..89947f1fe610 100644
@@ -1529,10 +1529,9 @@ __power_supply_register(struct device *parent,
 	dev_set_drvdata(dev, psy);
 	psy->desc = desc;
 	if (cfg) {
+		device_set_node(dev, cfg->fwnode);
 		dev->groups = cfg->attr_grp;
 		psy->drv_data = cfg->drv_data;
-		dev->of_node =
-			cfg->fwnode ? to_of_node(cfg->fwnode) : cfg->of_node;
 		psy->supplied_to = cfg->supplied_to;
 		psy->num_supplicants = cfg->num_supplicants;
 	}
include/linux/power_supply.h +0 -1
index 03786c8c2efe..3e52883f866b 100644
@@ -232,7 +232,6 @@ struct power_supply;
 
 /* Run-time specific power supply configuration */
 struct power_supply_config {
-	struct device_node *of_node;
 	struct fwnode_handle *fwnode;
 
 	/* Driver private data */
Signature · 1 line
2.49.0
#3 lore raw

PATCH 3/10 power: supply: core: battery-info: fully switch to fwnode

In reply to Hans de Goede

From: Sebastian Reichel <[email protected]>

Also use fwnode based parsing for "ocv-capacity-celsius" and "resistance-temp-table", so that any DT specific bits are removed from the power-supply core.

---
 drivers/power/supply/power_supply_core.c | 109 +++++++++++++----------
 1 file changed, 63 insertions(+), 46 deletions(-)
drivers/power/supply/power_supply_core.c +63 -46
index 89947f1fe610..a8d1fe66e248 100644
@@ -585,32 +585,19 @@ int power_supply_get_battery_info(struct power_supply *psy,
 {
 	struct power_supply_resistance_temp_table *resist_table;
 	struct power_supply_battery_info *info;
-	struct device_node *battery_np = NULL;
-	struct fwnode_reference_args args;
-	struct fwnode_handle *fwnode = NULL;
+	struct fwnode_handle *srcnode, *fwnode;
 	const char *value;
-	int err, len, index;
-	const __be32 *list;
+	int err, len, index, proplen;
+	u32 *propdata;
 	u32 min_max[2];
 
-	if (psy->dev.of_node) {
-		battery_np = of_parse_phandle(psy->dev.of_node, "monitored-battery", 0);
-		if (!battery_np)
-			return -ENODEV;
+	srcnode = dev_fwnode(&psy->dev);
+	if (!srcnode && psy->dev.parent)
+		srcnode = dev_fwnode(psy->dev.parent);
 
-		fwnode = fwnode_handle_get(of_fwnode_handle(battery_np));
-	} else if (psy->dev.parent) {
-		err = fwnode_property_get_reference_args(
-					dev_fwnode(psy->dev.parent),
-					"monitored-battery", NULL, 0, 0, &args);
-		if (err)
-			return err;
-
-		fwnode = args.fwnode;
-	}
-
-	if (!fwnode)
-		return -ENOENT;
+	fwnode = fwnode_find_reference(srcnode, "monitored-battery", 0);
+	if (IS_ERR(fwnode))
+		return PTR_ERR(fwnode);
 
 	err = fwnode_property_read_string(fwnode, "compatible", &value);
 	if (err)
@@ -740,15 +727,7 @@ int power_supply_get_battery_info(struct power_supply *psy,
 		info->temp_max = min_max[1];
 	}
 
-	/*
-	 * The below code uses raw of-data parsing to parse
-	 * /schemas/types.yaml#/definitions/uint32-matrix
-	 * data, so for now this is only support with of.
-	 */
-	if (!battery_np)
-		goto out_ret_pointer;
-
-	len = of_property_count_u32_elems(battery_np, "ocv-capacity-celsius");
+	len = fwnode_property_count_u32(fwnode, "ocv-capacity-celsius");
 	if (len < 0 && len != -EINVAL) {
 		err = len;
 		goto out_put_node;
@@ -757,13 +736,13 @@ int power_supply_get_battery_info(struct power_supply *psy,
 		err = -EINVAL;
 		goto out_put_node;
 	} else if (len > 0) {
-		of_property_read_u32_array(battery_np, "ocv-capacity-celsius",
+		fwnode_property_read_u32_array(fwnode, "ocv-capacity-celsius",
 					   info->ocv_temp, len);
 	}
 
 	for (index = 0; index < len; index++) {
 		struct power_supply_battery_ocv_table *table;
-		int i, tab_len, size;
+		int i, tab_len;
 
 		char *propname __free(kfree) = kasprintf(GFP_KERNEL, "ocv-capacity-table-%d",
 							 index);
@@ -772,60 +751,98 @@ int power_supply_get_battery_info(struct power_supply *psy,
 			err = -ENOMEM;
 			goto out_put_node;
 		}
-		list = of_get_property(battery_np, propname, &size);
-		if (!list || !size) {
+		proplen = fwnode_property_count_u32(fwnode, propname);
+		if (proplen < 0 || proplen % 2 != 0) {
 			dev_err(&psy->dev, "failed to get %s\n", propname);
 			power_supply_put_battery_info(psy, info);
 			err = -EINVAL;
 			goto out_put_node;
 		}
+		propdata = kcalloc(proplen, sizeof(*propdata), GFP_KERNEL);
+		if (!propdata) {
+			kfree(propname);
+			power_supply_put_battery_info(psy, info);
+			err = -EINVAL;
+			goto out_put_node;
+		}
+		err = fwnode_property_read_u32_array(fwnode, propname, propdata, proplen);
+		if (err < 0) {
+			dev_err(&psy->dev, "failed to get %s\n", propname);
+			kfree(propname);
+			kfree(propdata);
+			power_supply_put_battery_info(psy, info);
+			goto out_put_node;
+		}
 
-		tab_len = size / (2 * sizeof(__be32));
+		tab_len = proplen / 2;
 		info->ocv_table_size[index] = tab_len;
 
 		info->ocv_table[index] = table =
 			devm_kcalloc(&psy->dev, tab_len, sizeof(*table), GFP_KERNEL);
 		if (!info->ocv_table[index]) {
+			kfree(propdata);
 			power_supply_put_battery_info(psy, info);
 			err = -ENOMEM;
 			goto out_put_node;
 		}
 
 		for (i = 0; i < tab_len; i++) {
-			table[i].ocv = be32_to_cpu(*list);
-			list++;
-			table[i].capacity = be32_to_cpu(*list);
-			list++;
+			table[i].ocv = propdata[i*2];
+			table[i].capacity = propdata[i*2+1];
 		}
+
+		kfree(propdata);
 	}
 
-	list = of_get_property(battery_np, "resistance-temp-table", &len);
-	if (!list || !len)
+	proplen = fwnode_property_count_u32(fwnode, "resistance-temp-table");
+	if (proplen < 0 || proplen % 2 != 0) {
+		power_supply_put_battery_info(psy, info);
+		err = -ENOMEM;
 		goto out_ret_pointer;
+	} else if (proplen == 0) {
+		goto out_ret_pointer;
+	}
 
-	info->resist_table_size = len / (2 * sizeof(__be32));
+	propdata = kcalloc(proplen, sizeof(*propdata), GFP_KERNEL);
+	if (!propdata) {
+		power_supply_put_battery_info(psy, info);
+		err = -ENOMEM;
+		goto out_ret_pointer;
+	}
+
+	err = fwnode_property_read_u32_array(fwnode, "resistance-temp-table",
+					     propdata, proplen);
+	if (err < 0) {
+		kfree(propdata);
+		power_supply_put_battery_info(psy, info);
+		goto out_put_node;
+	}
+
+	info->resist_table_size = proplen / 2;
 	info->resist_table = resist_table = devm_kcalloc(&psy->dev,
 							 info->resist_table_size,
 							 sizeof(*resist_table),
 							 GFP_KERNEL);
 	if (!info->resist_table) {
+		kfree(propdata);
 		power_supply_put_battery_info(psy, info);
 		err = -ENOMEM;
 		goto out_put_node;
 	}
 
 	for (index = 0; index < info->resist_table_size; index++) {
-		resist_table[index].temp = be32_to_cpu(*list++);
-		resist_table[index].resistance = be32_to_cpu(*list++);
+		resist_table[index].temp = propdata[index*2];
+		resist_table[index].resistance = propdata[index*2+1];
 	}
 
+	kfree(propdata);
+
 out_ret_pointer:
 	/* Finally return the whole thing */
 	*info_out = info;
 
 out_put_node:
 	fwnode_handle_put(fwnode);
-	of_node_put(battery_np);
 	return err;
 }
 EXPORT_SYMBOL_GPL(power_supply_get_battery_info);
Signature · 1 line
2.49.0
#4 lore raw

PATCH 4/10 fixup! power: supply: core: battery-info: fully switch to fwnode

In reply to Hans de Goede

After ("power: supply: core: battery-info: fully switch to fwnode") power_supply_get_battery_info() will always fail for battery fwnodes which do not define a "resistance-temp-table".

Fix this by cleanly exiting on both 0 and EINVAL returns from fwnode_property_count_u32(fwnode, "resistance-temp-table") which indicates that the property is empty or not there.

While at it also fix:

1. The weird -ENOMEM return for other errors. For other errors propagate the existing error or -EINVAL for an odd proplen.
2. Wrongly using "goto out_ret_pointer" on errors, out_ret_pointer should only be used on success, error paths should use out_put_node;

---
 drivers/power/supply/power_supply_core.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
drivers/power/supply/power_supply_core.c +7 -6
index a8d1fe66e248..9bbc3be2e483 100644
@@ -795,19 +795,20 @@ int power_supply_get_battery_info(struct power_supply *psy,
 	}
 
 	proplen = fwnode_property_count_u32(fwnode, "resistance-temp-table");
-	if (proplen < 0 || proplen % 2 != 0) {
+	if (proplen == 0 || proplen == -EINVAL) {
+		err = 0;
+		goto out_ret_pointer;
+	} else if (proplen < 0 || proplen % 2 != 0) {
 		power_supply_put_battery_info(psy, info);
-		err = -ENOMEM;
-		goto out_ret_pointer;
-	} else if (proplen == 0) {
-		goto out_ret_pointer;
+		err = (proplen < 0) ? proplen : -EINVAL;
+		goto out_put_node;
 	}
 
 	propdata = kcalloc(proplen, sizeof(*propdata), GFP_KERNEL);
 	if (!propdata) {
 		power_supply_put_battery_info(psy, info);
 		err = -ENOMEM;
-		goto out_ret_pointer;
+		goto out_put_node;
 	}
 
 	err = fwnode_property_read_u32_array(fwnode, "resistance-temp-table",
Signature · 1 line
2.49.0
#5 lore raw

PATCH 5/10 fixup! power: supply: core: battery-info: fully switch to fwnode

In reply to Hans de Goede

propname must NOT be manually free-ed here since it is marked __free(kfree), freeing it will cause a double-free bug.

Also move propdata to be a __free(kfree) value for consistency and also for cleaner code.

---
 drivers/power/supply/power_supply_core.c | 15 +++------------
 1 file changed, 3 insertions(+), 12 deletions(-)
drivers/power/supply/power_supply_core.c +3 -12
index 9bbc3be2e483..f2c79f15838d 100644
@@ -588,7 +588,7 @@ int power_supply_get_battery_info(struct power_supply *psy,
 	struct fwnode_handle *srcnode, *fwnode;
 	const char *value;
 	int err, len, index, proplen;
-	u32 *propdata;
+	u32 *propdata __free(kfree) = NULL;
 	u32 min_max[2];
 
 	srcnode = dev_fwnode(&psy->dev);
@@ -758,9 +758,9 @@ int power_supply_get_battery_info(struct power_supply *psy,
 			err = -EINVAL;
 			goto out_put_node;
 		}
-		propdata = kcalloc(proplen, sizeof(*propdata), GFP_KERNEL);
+
+		u32 *propdata __free(kfree) = kcalloc(proplen, sizeof(*propdata), GFP_KERNEL);
 		if (!propdata) {
-			kfree(propname);
 			power_supply_put_battery_info(psy, info);
 			err = -EINVAL;
 			goto out_put_node;
@@ -768,8 +768,6 @@ int power_supply_get_battery_info(struct power_supply *psy,
 		err = fwnode_property_read_u32_array(fwnode, propname, propdata, proplen);
 		if (err < 0) {
 			dev_err(&psy->dev, "failed to get %s\n", propname);
-			kfree(propname);
-			kfree(propdata);
 			power_supply_put_battery_info(psy, info);
 			goto out_put_node;
 		}
@@ -780,7 +778,6 @@ int power_supply_get_battery_info(struct power_supply *psy,
 		info->ocv_table[index] = table =
 			devm_kcalloc(&psy->dev, tab_len, sizeof(*table), GFP_KERNEL);
 		if (!info->ocv_table[index]) {
-			kfree(propdata);
 			power_supply_put_battery_info(psy, info);
 			err = -ENOMEM;
 			goto out_put_node;
@@ -790,8 +787,6 @@ int power_supply_get_battery_info(struct power_supply *psy,
 			table[i].ocv = propdata[i*2];
 			table[i].capacity = propdata[i*2+1];
 		}
-
-		kfree(propdata);
 	}
 
 	proplen = fwnode_property_count_u32(fwnode, "resistance-temp-table");
@@ -814,7 +809,6 @@ int power_supply_get_battery_info(struct power_supply *psy,
 	err = fwnode_property_read_u32_array(fwnode, "resistance-temp-table",
 					     propdata, proplen);
 	if (err < 0) {
-		kfree(propdata);
 		power_supply_put_battery_info(psy, info);
 		goto out_put_node;
 	}
@@ -825,7 +819,6 @@ int power_supply_get_battery_info(struct power_supply *psy,
 							 sizeof(*resist_table),
 							 GFP_KERNEL);
 	if (!info->resist_table) {
-		kfree(propdata);
 		power_supply_put_battery_info(psy, info);
 		err = -ENOMEM;
 		goto out_put_node;
@@ -836,8 +829,6 @@ int power_supply_get_battery_info(struct power_supply *psy,
 		resist_table[index].resistance = propdata[index*2+1];
 	}
 
-	kfree(propdata);
-
 out_ret_pointer:
 	/* Finally return the whole thing */
 	*info_out = info;
Signature · 1 line
2.49.0
#6 lore raw

PATCH 6/10 power: supply: core: convert to fwnnode

In reply to Hans de Goede

From: Sebastian Reichel <[email protected]>

Replace any DT specific code with fwnode in the power-supply core.

---
 drivers/power/supply/bq2415x_charger.c   |  2 +-
 drivers/power/supply/power_supply_core.c | 65 ++++++++++++------------
 include/linux/power_supply.h             |  2 +-
 3 files changed, 34 insertions(+), 35 deletions(-)
drivers/power/supply/bq2415x_charger.c +1 -1
index 9e3b9181ee76..1ecbca510bba 100644
@@ -1674,7 +1674,7 @@ static int bq2415x_probe(struct i2c_client *client)
 	/* Query for initial reported_mode and set it */
 	if (bq->nb.notifier_call) {
 		if (np) {
-			notify_psy = power_supply_get_by_phandle(np,
+			notify_psy = power_supply_get_by_phandle(of_fwnode_handle(np),
 						"ti,usb-charger-detection");
 			if (IS_ERR(notify_psy))
 				notify_psy = NULL;
drivers/power/supply/power_supply_core.c +32 -33

166 lines not on this page · raw message

include/linux/power_supply.h +1 -1
index 3e52883f866b..c22ecb46098f 100644
@@ -807,7 +807,7 @@ static inline struct power_supply *power_supply_get_by_name(const char *name)
 { return NULL; }
 #endif
 #ifdef CONFIG_OF
-extern struct power_supply *power_supply_get_by_phandle(struct device_node *np,
+extern struct power_supply *power_supply_get_by_phandle(struct fwnode_handle *fwnode,
 							const char *property);
 extern struct power_supply *devm_power_supply_get_by_phandle(
 				    struct device *dev, const char *property);
Signature · 1 line
2.49.0
#7 lore raw

PATCH 7/10 power: supply: core: rename power_supply_get_by_phandle to power_supply_get_by_reference

In reply to Hans de Goede

From: Sebastian Reichel <[email protected]>

(devm_)power_supply_get_by_phandle now internally uses fwnode and are no longer DT specific. Thus drop the ifdef check for CONFIG_OF and rename to (devm_)power_supply_get_by_reference to avoid the DT terminology.

---
 drivers/phy/allwinner/phy-sun4i-usb.c    |  2 +-
 drivers/power/supply/bq2415x_charger.c   |  2 +-
 drivers/power/supply/power_supply_core.c | 22 ++++++++++------------
 include/linux/power_supply.h             | 15 +++------------
 4 files changed, 15 insertions(+), 26 deletions(-)
drivers/phy/allwinner/phy-sun4i-usb.c +1 -1
index 29b8fd4b9351..8873aed3a52a 100644
@@ -754,7 +754,7 @@ static int sun4i_usb_phy_probe(struct platform_device *pdev)
 	}
 
 	if (of_property_present(np, "usb0_vbus_power-supply")) {
-		data->vbus_power_supply = devm_power_supply_get_by_phandle(dev,
+		data->vbus_power_supply = devm_power_supply_get_by_reference(dev,
 						     "usb0_vbus_power-supply");
 		if (IS_ERR(data->vbus_power_supply)) {
 			dev_err(dev, "Couldn't get the VBUS power supply\n");
drivers/power/supply/bq2415x_charger.c +1 -1
index 1ecbca510bba..917c26ee56bc 100644
@@ -1674,7 +1674,7 @@ static int bq2415x_probe(struct i2c_client *client)
 	/* Query for initial reported_mode and set it */
 	if (bq->nb.notifier_call) {
 		if (np) {
-			notify_psy = power_supply_get_by_phandle(of_fwnode_handle(np),
+			notify_psy = power_supply_get_by_reference(of_fwnode_handle(np),
 						"ti,usb-charger-detection");
 			if (IS_ERR(notify_psy))
 				notify_psy = NULL;
drivers/power/supply/power_supply_core.c +10 -12
index 2d83bb125a48..aedb20c1d276 100644
@@ -496,14 +496,13 @@ void power_supply_put(struct power_supply *psy)
 }
 EXPORT_SYMBOL_GPL(power_supply_put);
 
-#ifdef CONFIG_OF
 static int power_supply_match_device_fwnode(struct device *dev, const void *data)
 {
 	return dev->parent && dev_fwnode(dev->parent) == data;
 }
 
 /**
- * power_supply_get_by_phandle() - Search for a power supply and returns its ref
+ * power_supply_get_by_reference() - Search for a power supply and returns its ref
  * @fwnode: Pointer to fwnode holding phandle property
  * @property: Name of property holding a power supply name
  *
@@ -514,8 +513,8 @@ static int power_supply_match_device_fwnode(struct device *dev, const void *data
  * Return: On success returns a reference to a power supply with
  * matching name equals to value under @property, NULL or ERR_PTR otherwise.
  */
-struct power_supply *power_supply_get_by_phandle(struct fwnode_handle *fwnode,
-						 const char *property)
+struct power_supply *power_supply_get_by_reference(struct fwnode_handle *fwnode,
+						   const char *property)
 {
 	struct fwnode_handle *power_supply_fwnode;
 	struct power_supply *psy = NULL;
@@ -537,7 +536,7 @@ struct power_supply *power_supply_get_by_phandle(struct fwnode_handle *fwnode,
 
 	return psy;
 }
-EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);
+EXPORT_SYMBOL_GPL(power_supply_get_by_reference);
 
 static void devm_power_supply_put(struct device *dev, void *res)
 {
@@ -547,16 +546,16 @@ static void devm_power_supply_put(struct device *dev, void *res)
 }
 
 /**
- * devm_power_supply_get_by_phandle() - Resource managed version of
- *  power_supply_get_by_phandle()
+ * devm_power_supply_get_by_reference() - Resource managed version of
+ *  power_supply_get_by_reference()
  * @dev: Pointer to device holding phandle property
  * @property: Name of property holding a power supply phandle
  *
  * Return: On success returns a reference to a power supply with
  * matching name equals to value under @property, NULL or ERR_PTR otherwise.
  */
-struct power_supply *devm_power_supply_get_by_phandle(struct device *dev,
-						      const char *property)
+struct power_supply *devm_power_supply_get_by_reference(struct device *dev,
+							const char *property)
 {
 	struct power_supply **ptr, *psy;
 
@@ -567,7 +566,7 @@ struct power_supply *devm_power_supply_get_by_phandle(struct device *dev,
 	if (!ptr)
 		return ERR_PTR(-ENOMEM);
 
-	psy = power_supply_get_by_phandle(dev_fwnode(dev), property);
+	psy = power_supply_get_by_reference(dev_fwnode(dev), property);
 	if (IS_ERR_OR_NULL(psy)) {
 		devres_free(ptr);
 	} else {
@@ -576,8 +575,7 @@ struct power_supply *devm_power_supply_get_by_phandle(struct device *dev,
 	}
 	return psy;
 }
-EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle);
-#endif /* CONFIG_OF */
+EXPORT_SYMBOL_GPL(devm_power_supply_get_by_reference);
 
 int power_supply_get_battery_info(struct power_supply *psy,
 				  struct power_supply_battery_info **info_out)
include/linux/power_supply.h +3 -12
index c22ecb46098f..73bf20031449 100644
@@ -806,19 +806,10 @@ static inline void power_supply_put(struct power_supply *psy) {}
 static inline struct power_supply *power_supply_get_by_name(const char *name)
 { return NULL; }
 #endif
-#ifdef CONFIG_OF
-extern struct power_supply *power_supply_get_by_phandle(struct fwnode_handle *fwnode,
-							const char *property);
-extern struct power_supply *devm_power_supply_get_by_phandle(
+extern struct power_supply *power_supply_get_by_reference(struct fwnode_handle *fwnode,
+							  const char *property);
+extern struct power_supply *devm_power_supply_get_by_reference(
 				    struct device *dev, const char *property);
-#else /* !CONFIG_OF */
-static inline struct power_supply *
-power_supply_get_by_phandle(struct device_node *np, const char *property)
-{ return NULL; }
-static inline struct power_supply *
-devm_power_supply_get_by_phandle(struct device *dev, const char *property)
-{ return NULL; }
-#endif /* CONFIG_OF */
 
 extern const enum power_supply_property power_supply_battery_info_properties[];
 extern const size_t power_supply_battery_info_properties_size;
Signature · 1 line
2.49.0
#8 lore raw

PATCH 8/10 power: supply: ug3105_battery: Use psy->battery_info

In reply to Hans de Goede

For POWER_SUPPLY_TYPE_BATTERY power-supplies the core already calls power_supply_get_battery_info() and stores the result in psy->battery_info.

Use psy->battery_info instead of having the driver call power_supply_get_battery_info() itself.

---
 drivers/power/supply/ug3105_battery.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)
drivers/power/supply/ug3105_battery.c +7 -10
index 38e23bdd4603..806f6892e189 100644
@@ -69,7 +69,6 @@
 struct ug3105_chip {
 	struct i2c_client *client;
 	struct power_supply *psy;
-	struct power_supply_battery_info *info;
 	struct delayed_work work;
 	struct mutex lock;
 	int ocv[UG3105_MOV_AVG_WINDOW];		/* micro-volt */
@@ -103,7 +102,8 @@ static int ug3105_read_word(struct i2c_client *client, u8 reg)
 
 static int ug3105_get_status(struct ug3105_chip *chip)
 {
-	int full = chip->info->constant_charge_voltage_max_uv - UG3105_FULL_BAT_HYST_UV;
+	int full = chip->psy->battery_info->constant_charge_voltage_max_uv -
+		   UG3105_FULL_BAT_HYST_UV;
 
 	if (chip->curr > UG3105_CURR_HYST_UA)
 		return POWER_SUPPLY_STATUS_CHARGING;
@@ -164,7 +164,7 @@ static int ug3105_get_capacity(struct ug3105_chip *chip)
 		ocv_diff = ocv_capacity_tbl[i] - chip->ocv_avg;
 		ocv_step = ocv_capacity_tbl[i] - ocv_capacity_tbl[i - 1];
 		/* scale 0-110% down to 0-100% for LiPo HV */
-		if (chip->info->constant_charge_voltage_max_uv >= 4300000)
+		if (chip->psy->battery_info->constant_charge_voltage_max_uv >= 4300000)
 			return (i * 500 - ocv_diff * 500 / ocv_step) / 110;
 		else
 			return i * 5 - ocv_diff * 5 / ocv_step;
@@ -401,12 +401,9 @@ static int ug3105_probe(struct i2c_client *client)
 	if (IS_ERR(psy))
 		return PTR_ERR(psy);
 
-	ret = power_supply_get_battery_info(psy, &chip->info);
-	if (ret)
-		return ret;
-
-	if (chip->info->factory_internal_resistance_uohm == -EINVAL ||
-	    chip->info->constant_charge_voltage_max_uv == -EINVAL) {
+	if (!psy->battery_info ||
+	    psy->battery_info->factory_internal_resistance_uohm == -EINVAL ||
+	    psy->battery_info->constant_charge_voltage_max_uv == -EINVAL) {
 		dev_err(dev, "error required properties are missing\n");
 		return -ENODEV;
 	}
@@ -422,7 +419,7 @@ static int ug3105_probe(struct i2c_client *client)
 	chip->ua_per_unit = 8100000 / curr_sense_res_uohm;
 
 	/* Use provided internal resistance as start point (in milli-ohm) */
-	chip->intern_res_avg = chip->info->factory_internal_resistance_uohm / 1000;
+	chip->intern_res_avg = psy->battery_info->factory_internal_resistance_uohm / 1000;
 	/* Also add it to the internal resistance moving average window */
 	chip->intern_res[0] = chip->intern_res_avg;
 	chip->intern_res_avg_index = 1;
Signature · 1 line
2.49.0
#9 lore raw

PATCH 9/10 power: supply: ug3105_battery: Switch to power_supply_batinfo_ocv2cap()

In reply to Hans de Goede

Replace the hardcoded ocv -> capacity table and the ug3105_get_capacity() helper with using the generic power_supply_batinfo_ocv2cap() function.

Note this relies on the battery fwnode providing at least 1 "ocv-capacity-table", if that is missing probe() will now fail with EINVAL.

---
 drivers/power/supply/ug3105_battery.c | 68 ++++-----------------------
 1 file changed, 10 insertions(+), 58 deletions(-)
drivers/power/supply/ug3105_battery.c +10 -58
index 806f6892e189..e8a1de7cade0 100644
@@ -66,6 +66,8 @@
 #define UG3105_LOW_BAT_UV					3700000
 #define UG3105_FULL_BAT_HYST_UV					38000
 
+#define AMBIENT_TEMP_CELCIUS					25
+
 struct ug3105_chip {
 	struct i2c_client *client;
 	struct power_supply *psy;
@@ -117,62 +119,6 @@ static int ug3105_get_status(struct ug3105_chip *chip)
 	return POWER_SUPPLY_STATUS_NOT_CHARGING;
 }
 
-static int ug3105_get_capacity(struct ug3105_chip *chip)
-{
-	/*
-	 * OCV voltages in uV for 0-110% in 5% increments, the 100-110% is
-	 * for LiPo HV (High-Voltage) bateries which can go up to 4.35V
-	 * instead of the usual 4.2V.
-	 */
-	static const int ocv_capacity_tbl[23] = {
-		3350000,
-		3610000,
-		3690000,
-		3710000,
-		3730000,
-		3750000,
-		3770000,
-		3786667,
-		3803333,
-		3820000,
-		3836667,
-		3853333,
-		3870000,
-		3907500,
-		3945000,
-		3982500,
-		4020000,
-		4075000,
-		4110000,
-		4150000,
-		4200000,
-		4250000,
-		4300000,
-	};
-	int i, ocv_diff, ocv_step;
-
-	if (chip->ocv_avg < ocv_capacity_tbl[0])
-		return 0;
-
-	if (chip->status == POWER_SUPPLY_STATUS_FULL)
-		return 100;
-
-	for (i = 1; i < ARRAY_SIZE(ocv_capacity_tbl); i++) {
-		if (chip->ocv_avg > ocv_capacity_tbl[i])
-			continue;
-
-		ocv_diff = ocv_capacity_tbl[i] - chip->ocv_avg;
-		ocv_step = ocv_capacity_tbl[i] - ocv_capacity_tbl[i - 1];
-		/* scale 0-110% down to 0-100% for LiPo HV */
-		if (chip->psy->battery_info->constant_charge_voltage_max_uv >= 4300000)
-			return (i * 500 - ocv_diff * 500 / ocv_step) / 110;
-		else
-			return i * 5 - ocv_diff * 5 / ocv_step;
-	}
-
-	return 100;
-}
-
 static void ug3105_work(struct work_struct *work)
 {
 	struct ug3105_chip *chip = container_of(work, struct ug3105_chip,
@@ -231,7 +177,12 @@ static void ug3105_work(struct work_struct *work)
 
 	chip->supplied = power_supply_am_i_supplied(psy);
 	chip->status = ug3105_get_status(chip);
-	chip->capacity = ug3105_get_capacity(chip);
+	if (chip->status == POWER_SUPPLY_STATUS_FULL)
+		chip->capacity = 100;
+	else
+		chip->capacity = power_supply_batinfo_ocv2cap(chip->psy->battery_info,
+							      chip->ocv_avg,
+							      AMBIENT_TEMP_CELCIUS);
 
 	/*
 	 * Skip internal resistance calc on charger [un]plug and
@@ -403,7 +354,8 @@ static int ug3105_probe(struct i2c_client *client)
 
 	if (!psy->battery_info ||
 	    psy->battery_info->factory_internal_resistance_uohm == -EINVAL ||
-	    psy->battery_info->constant_charge_voltage_max_uv == -EINVAL) {
+	    psy->battery_info->constant_charge_voltage_max_uv == -EINVAL ||
+	    !psy->battery_info->ocv_table[0]) {
 		dev_err(dev, "error required properties are missing\n");
 		return -ENODEV;
 	}
Signature · 1 line
2.49.0
#10 lore raw

PATCH 10/10 power: supply: bq24190: Free battery_info

In reply to Hans de Goede

Call power_supply_put_battery_info() when bq24190_get_config() is done with it. The "struct power_supply_battery_info *info" pointer runs out of scope at the end of bq24190_get_config() so there is no need to keep it around after this.

Note technically this is not a memleak fix, since all battery_info data is devm_alloc()-ed so it would still be free-ed when the driver is unbound. This just frees it as soon as the driver is done with it.

---
 drivers/power/supply/bq24190_charger.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/power/supply/bq24190_charger.c +2 -0
index f0d97ab45bd8..cc6dad8f8e58 100644
@@ -1982,6 +1982,8 @@ static int bq24190_get_config(struct bq24190_dev_info *bdi)
 		v = info->constant_charge_voltage_max_uv;
 		if (v >= bq24190_cvc_vreg_values[0] && v <= bdi->vreg_max)
 			bdi->vreg = bdi->vreg_max = v;
+
+		power_supply_put_battery_info(bdi->charger, info);
 	}
 
 	return 0;
Signature · 1 line
2.49.0
#11 lore raw

Re: (subset) [PATCH 00/10] power: supply: ug3105_battery: Switch to power_supply_batinfo_ocv2cap()

In reply to Hans de Goede
Quoted 5 lines · Hans de Goede

This series converts the ug3105_battery driver from using a static ovc - capcity mapping table + its own DIY code to using the ovc-capacity-table(s) from the battery fwnode and using the power_supply_batinfo_ocv2cap() helper.

Quoted 7 lines

Since the ug3105_battery driver is used on ACPI platforms this relies on sre's "[PATCH v2 0/5] power: supply: core: convert to fwnode" series for completenes sake I've added a copy of that series + 2 fixup patches to "[PATCH v2 3/5] power: supply: core: battery-info: fully switch to fwnode", the original patches are unmodified.

[...]

Applied, thanks!

[08/10] power: supply: ug3105_battery: Use psy->battery_info

        commit: 8842bd00a74bf758fb1abf572ec1c7d70c09dedb
[09/10] power: supply: ug3105_battery: Switch to power_supply_batinfo_ocv2cap()
        commit: 2986e5b213cd84ac290ae68e73b7629ec8f184a6
[10/10] power: supply: bq24190: Free battery_info
        commit: 6aa1c3a72b99abeb7ddc649047073d701ede2c91

Best regards,

Signature · 1 line
Sebastian Reichel <[email protected]>
#12 lore raw

Re: (subset) [PATCH 00/10] power: supply: ug3105_battery: Switch to power_supply_batinfo_ocv2cap()

Hi,

Quoted 15 lines · Sebastian Reichel
Quoted 5 lines · Hans de Goede

This series converts the ug3105_battery driver from using a static ovc - capcity mapping table + its own DIY code to using the ovc-capacity-table(s) from the battery fwnode and using the power_supply_batinfo_ocv2cap() helper.

Quoted 7 lines

Since the ug3105_battery driver is used on ACPI platforms this relies on sre's "[PATCH v2 0/5] power: supply: core: convert to fwnode" series for completenes sake I've added a copy of that series + 2 fixup patches to "[PATCH v2 3/5] power: supply: core: battery-info: fully switch to fwnode", the original patches are unmodified.

[...]

Quoted 8 lines

Applied, thanks!

[08/10] power: supply: ug3105_battery: Use psy->battery_info

        commit: 8842bd00a74bf758fb1abf572ec1c7d70c09dedb
[09/10] power: supply: ug3105_battery: Switch to power_supply_batinfo_ocv2cap()
        commit: 2986e5b213cd84ac290ae68e73b7629ec8f184a6
[10/10] power: supply: bq24190: Free battery_info
        commit: 6aa1c3a72b99abeb7ddc649047073d701ede2c91

I obviously also applied 1-7, but without b4 as that required some manual work to prepare the immutable branch for regulator and squashing the fixup patches :)

Here is the pull information for regulator:

-------------------------------------------------------------------------------
The following changes since commit 19272b37aa4f83ca52bdf9c16d5d81bdd1354494:

Linux 6.16-rc1 (2025-06-08 13:44:43 -0700)

are available in the Git repository at:

https://git.kernel.org/pub/scm/linux/kernel/git/sre/linux-power-supply.git tags/ib-regulator-psy-for-v6.17-signed

for you to fetch changes up to ceed13630489fb9afbaa1326d2adc793d91fa48b:

regulator: act8865-regulator: switch psy_cfg from of_node to fwnode (2025-06-22 01:39:51 +0200)

----------------------------------------------------------------
Immutable branch for power_supply_config.of_node removal for v6.17

This immutable branch updates the remaining user of power_supply_config.of_node to use .fwnode instead, so that the any code related to power_supply_config.of_node can be removed.

----------------------------------------------------------------
Sebastian Reichel (1):
regulator: act8865-regulator: switch psy_cfg from of_node to fwnode

 drivers/regulator/act8865-regulator.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

-------------------------------------------------------------------------------

Greetings,

-- Sebastian

maintainer
#13 lore raw

Re: (subset) [PATCH 00/10] power: supply: ug3105_battery: Switch to power_supply_batinfo_ocv2cap()

Quoted 5 lines · Sebastian Reichel

I obviously also applied 1-7, but without b4 as that required some manual work to prepare the immutable branch for regulator and squashing the fixup patches :)

Here is the pull information for regulator:

Uh, JFTR I hadn't actually reviewed this - there was a !fixup in the middle of the series so it looked like an obvious missend and I just binned it.

#14 lore raw

Re: (subset) [PATCH 00/10] power: supply: ug3105_battery: Switch to power_supply_batinfo_ocv2cap()

Hi,

Quoted 11 lines · Mark Brown
Quoted 5 lines · Sebastian Reichel

I obviously also applied 1-7, but without b4 as that required some manual work to prepare the immutable branch for regulator and squashing the fixup patches :)

Here is the pull information for regulator:

Uh, JFTR I hadn't actually reviewed this - there was a !fixup in the middle of the series so it looked like an obvious missend and I just binned it.

oh sorry, Hans' patch series was based on a much older series from me, which has been pending for the last two kernel cycles. I somehow misremembered, that you already Ack'd it, since it was so trivial. I should have checked before merging this.

Greetings,

-- Sebastian