Skip to contentSkip to conversation

Add DT Binding for Power-Supply power-supplies property

PATCH In review Rhyland Klein 3 participants 7 messages Apr 1, 2013 – Apr 17, 2013

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

v2 cover Add DT Binding for Power-Supply power-supplies property

This series defines a common way for devicetree initialized power_supplies to define their relationships between chargers and supplicants.

This series adds a supplied_from array to complement the supplied_to array and to allow supplies to define the list of supplies which supply them.

Then once this property is supported, we can use a new property for devicetree to define the relationships between nodes, and read in this property to generate the supplied_from list.

With this logic in place, all drivers need to do to add support for this mechanism, is to store their device tree node in the power_supply struct. They should also handle EPROBE_DEFER properly.

Changes since:
v2:
- Changed __power_supply_is_supplied_by to a boolean function and corrected return paths around it
- took loop invariant tests out of loops
- Fixed multiline comment style
- fixed up return paths around power_supplies_check_supplies

RFC v2:
- Changed to official Patch set rather than RFC
- defined supplied_from char ** array rather than complicated struct device_node related array

RFC v1:
- Inverted the logic so that supplies (batteries) contain a list of the supplies (chargers) which supply them.

Rhyland Klein (3):
power_supply: Define Binding for power-supplies
power: power_supply: Add core support for supplied_from
power: power_supply_core: Add support for supplied_from

 .../bindings/power_supply/power_supply.txt         |   23 +++
 drivers/power/power_supply_core.c                  |  187 ++++++++++++++++++--
 include/linux/power_supply.h                       |    6 +
 3 files changed, 203 insertions(+), 13 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/power_supply/power_supply.txt
Signature · 1 line
1.7.9.5
#1 lore raw

PATCH v2 1/3 power_supply: Define Binding for power-supplies

This property is meant to be used in device nodes which represent power_supply devices that wish to provide a list of supplies which provide them power, such as a battery listing its chargers.

---
v2:
- no changes

v1:
- changed from RFC v2 -> patch v1
- made poropery plural as it can be a list
- update example with plural & changed once charger address

v2 (RFC):
- changed property to "power-supply" which should be contained in the battery rather than the charger. Also updated example to match

 .../bindings/power_supply/power_supply.txt         |   23 ++++++++++++++++++++
 1 file changed, 23 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/power_supply/power_supply.txt
Documentation/devicetree/bindings/power_supply/power_supply.txt new file +23 -0
new file mode 100644
index 0000000..8391bfa
@@ -0,0 +1,23 @@
+Power Supply Core Support
+
+Optional Properties:
+ - power-supplies : This property is added to a supply in order to list the
+   devices which supply it power, referenced by their phandles.
+
+Example:
+
+	usb-charger: power@e {
+		compatible = "some,usb-charger";
+		...
+	};
+
+	ac-charger: power@c {
+		compatible = "some,ac-charger";
+		...
+	};
+
+	battery@b {
+		compatible = "some,battery";
+		...
+		power-supplies = <&usb-charger>, <&ac-charger>;
+	};
Signature · 1 line
1.7.9.5
#2 lore raw

PATCH v2 2/3 power: power_supply: Add core support for supplied_from

In reply to Rhyland Klein

This patch adds support for supplies to register a list of char *'s which represent the list of supplies which supply them. This is the opposite as the supplied_to list.

This change maintains support for supplied_to until all drivers which make use of it already are converted.

---
v2:
- changed __power_supply_is_supplied_by to a boolean function
- fixed up return paths with correct return values
- moved loop invariant checks outside of loops

v1:
- changed from RFC v2 -> patch v1
- removed list logic and instead added supplied_from char ** array and num_supplies field

v2 (RFC):
- changed from struct device_node * contained in suppliers to a list stored in the supplies.
- changed logic for the is_supplied_by check to handle the entire loop as the array structure is difference between the 2 paths.

 drivers/power/power_supply_core.c |   47 +++++++++++++++++++++++++++----------
 include/linux/power_supply.h      |    3 +++
 2 files changed, 37 insertions(+), 13 deletions(-)
drivers/power/power_supply_core.c +34 -13
index 5deac43..d843cc9 100644
@@ -26,17 +26,42 @@ EXPORT_SYMBOL_GPL(power_supply_class);
 
 static struct device_type power_supply_dev_type;
 
+static bool __power_supply_is_supplied_by(struct power_supply *supplier,
+					 struct power_supply *supply)
+{
+	int i;
+
+	if (!supply->supplied_from && !supplier->supplied_to)
+		return false;
+
+	/* Support both supplied_to and supplied_from modes */
+	if (supply->supplied_from) {
+		if (!supplier->name)
+			return false;
+		for (i = 0; i < supply->num_supplies; i++)
+			if (!strcmp(supplier->name, supply->supplied_from[i]))
+				return true;
+	} else {
+		if (!supply->name)
+			return false;
+		for (i = 0; i < supplier->num_supplicants; i++)
+			if (!strcmp(supplier->supplied_to[i], supply->name))
+				return true;
+	}
+
+	return false;
+}
+
 static int __power_supply_changed_work(struct device *dev, void *data)
 {
 	struct power_supply *psy = (struct power_supply *)data;
 	struct power_supply *pst = dev_get_drvdata(dev);
-	int i;
 
-	for (i = 0; i < psy->num_supplicants; i++)
-		if (!strcmp(psy->supplied_to[i], pst->name)) {
-			if (pst->external_power_changed)
-				pst->external_power_changed(pst);
-		}
+	if (__power_supply_is_supplied_by(psy, pst)) {
+		if (pst->external_power_changed)
+			pst->external_power_changed(pst);
+	}
+
 	return 0;
 }
 
@@ -68,17 +93,13 @@ static int __power_supply_am_i_supplied(struct device *dev, void *data)
 	union power_supply_propval ret = {0,};
 	struct power_supply *psy = (struct power_supply *)data;
 	struct power_supply *epsy = dev_get_drvdata(dev);
-	int i;
 
-	for (i = 0; i < epsy->num_supplicants; i++) {
-		if (!strcmp(epsy->supplied_to[i], psy->name)) {
-			if (epsy->get_property(epsy,
-				  POWER_SUPPLY_PROP_ONLINE, &ret))
-				continue;
+	if (__power_supply_is_supplied_by(epsy, psy))
+		if (!epsy->get_property(epsy, POWER_SUPPLY_PROP_ONLINE, &ret)) {
 			if (ret.intval)
 				return ret.intval;
 		}
-	}
+
 	return 0;
 }
 
include/linux/power_supply.h +3 -0
index 002a99f..c1cbd5e 100644
@@ -171,6 +171,9 @@ struct power_supply {
 	char **supplied_to;
 	size_t num_supplicants;
 
+	char **supplied_from;
+	size_t num_supplies;
+
 	int (*get_property)(struct power_supply *psy,
 			    enum power_supply_property psp,
 			    union power_supply_propval *val);
Signature · 1 line
1.7.9.5
Reviewed-by on v2 2/3
#3 lore raw

Re: [PATCH V2 2/3] power: power_supply: Add core support for supplied_from

Quoted 3 lines · Rhyland Klein

This patch adds support for supplies to register a list of char *'s which represent the list of supplies which supply them. This is the opposite as the supplied_to list.

This patch resolves the concerns I had before, so,

drivers/power/power_supply_core.c EXPORT_SYMBOL_GPL(power_supply_class); · line 35

Quoted 9 lines
drivers/power/power_supply_core.c +7 -0
 
+static bool __power_supply_is_supplied_by(struct power_supply *supplier,
+					 struct power_supply *supply)
+{
+	int i;
+
+	if (!supply->supplied_from && !supplier->supplied_to)
+		return false;

Nit: that somewhat duplicates the if below, so perhaps could just be removed?

drivers/power/power_supply_core.c EXPORT_SYMBOL_GPL(power_supply_class); · line 40

Quoted 4 lines
drivers/power/power_supply_core.c +4 -0
+	/* Support both supplied_to and supplied_from modes */
+	if (supply->supplied_from) {
+		if (!supplier->name)
+			return false;

Likewise, I'm still not convinced these !name tests should be required.

However, there's no harm here I think, so it's fine.

#4 lore raw

Re: [PATCH V2 2/3] power: power_supply: Add core support for supplied_from

Quoted 7 lines · Stephen Warren
Quoted 3 lines · Rhyland Klein

This patch adds support for supplies to register a list of char *'s which represent the list of supplies which supply them. This is the opposite as the supplied_to list.

This patch resolves the concerns I had before, so,

Thanks a lot for the work, folks! With some fixes (see comments for patch 3/3), this is now in battery-2.6.git tree.

Anton

#5 lore raw

PATCH v2 3/3 power: power_supply_core: Add support for supplied_from

In reply to Rhyland Klein

Adding support for supplied_from char * array. This is meant to store the list of suppliers for a given supply, i.e. chargers for a battery. This list can be populated through devicetree readily as well as passed directly from the driver.

---
v2:
- fixed multiline comment style
- fixed usage of ret to inside loop and directly return result of power_supply_populate_supplied_from

v1:
- Changed from RFC v2 -> patch v1
- removed list logic, added logic to first verify all supplies are present and defer probe until they are.
- after all devices are registered, populate the char ** supplied_from array which simulates the case of dt not being used.
- added of_node element to struct power_supply

v2 (RFC):
- Simplified and renamed the logic to parse dt for the charger list.
- Tied the dt parsing directly to power_supply_register to make fewer changes required for converting existing chargers/supplies.

 drivers/power/power_supply_core.c |  140 +++++++++++++++++++++++++++++++++++++
 include/linux/power_supply.h      |    3 +
 2 files changed, 143 insertions(+)
drivers/power/power_supply_core.c +140 -0
index d843cc9..5e84321 100644
@@ -88,6 +88,139 @@ void power_supply_changed(struct power_supply *psy)
 }
 EXPORT_SYMBOL_GPL(power_supply_changed);
 
+#ifdef CONFIG_OF
+#include <linux/of.h>
+
+static int __power_supply_populate_supplied_from(struct device *dev,
+						 void *data)
+{
+	struct power_supply *psy = (struct power_supply *)data;
+	struct power_supply *epsy = dev_get_drvdata(dev);
+	struct device_node *np;
+	int i = 0;
+
+	do {
+		np = of_parse_phandle(psy->of_node, "power-supplies", i++);
+		if (!np)
+			continue;
+
+		if (np == epsy->of_node) {
+			dev_info(psy->dev, "%s: Found supply : %s\n",
+				psy->name, epsy->name);
+			psy->supplied_from[i-1] = (char *)epsy->name;
+			psy->num_supplies++;
+			break;
+		}
+	} while (np);
+
+	return 0;
+}
+
+int power_supply_populate_supplied_from(struct power_supply *psy)
+{
+	int error;
+
+	error = class_for_each_device(power_supply_class, NULL, psy,
+				      __power_supply_populate_supplied_from);
+
+	dev_dbg(psy->dev, "%s %d\n", __func__, error);
+
+	return error;
+}
+
+static int  __power_supply_find_supply_from_node(struct device *dev,
+						 void *data)
+{
+	struct device_node *np = (struct device_node *)data;
+	struct power_supply *epsy = dev_get_drvdata(dev);
+
+	/* return error breaks out of class_for_each_device loop */
+	if (epsy->of_node == np)
+		return -EINVAL;
+
+	return 0;
+}
+
+int power_supply_find_supply_from_node(struct device_node *supply_node)
+{
+	int error;
+	struct device *dev;
+	struct class_dev_iter iter;
+
+	/*
+	 * Use iterator to see if any other device is registered.
+	 * This is required since class_for_each_device returns 0
+	 * if there are no devices registered.
+	 */
+	class_dev_iter_init(&iter, power_supply_class, NULL, NULL);
+	dev = class_dev_iter_next(&iter);
+
+	if (!dev)
+		return -EPROBE_DEFER;
+
+	/*
+	 * We have to treat the return value as inverted, because if
+	 * we return error on not found, then it won't continue looking.
+	 * So we trick it by returning error on success to stop looking
+	 * once the matching device is found.
+	 */
+	error = class_for_each_device(power_supply_class, NULL, supply_node,
+				       __power_supply_find_supply_from_node);
+
+	return error ? 0 : -EPROBE_DEFER;
+}
+
+int power_supply_check_supplies(struct power_supply *psy)
+{
+	struct device_node *np;
+	int cnt = 0;
+
+	/* If there is already a list honor it */
+	if (psy->supplied_from && psy->num_supplies > 0)
+		return 0;
+
+	/* No device node found, nothing to do */
+	if (!psy->of_node)
+		return 0;
+
+	do {
+		int ret;
+
+		np = of_parse_phandle(psy->of_node, "power-supplies", cnt++);
+		if (!np)
+			continue;
+
+		ret = power_supply_find_supply_from_node(np);
+		if (ret) {
+			dev_dbg(psy->dev, "Failed to find supply, defer!\n");
+			return -EPROBE_DEFER;
+		}
+	} while (np);
+
+	/* All supplies found, allocate char ** array for filling */
+	psy->supplied_from = devm_kzalloc(psy->dev, sizeof(psy->supplied_from),
+					  GFP_KERNEL);
+	if (!psy->supplied_from) {
+		dev_err(psy->dev, "Couldn't allocate memory for supply list\n");
+		return -ENOMEM;
+	}
+
+	*psy->supplied_from = devm_kzalloc(psy->dev, sizeof(char *) * cnt,
+					   GFP_KERNEL);
+	if (!*psy->supplied_from) {
+		dev_err(psy->dev, "Couldn't allocate memory for supply list\n");
+		return -ENOMEM;
+	}
+
+	return power_supply_populate_supplied_from(psy);
+}
+#else
+static inline int power_supply_check_supplies(struct power_supply *psy)
+{
+	return 0;
+}
+#endif
+
 static int __power_supply_am_i_supplied(struct device *dev, void *data)
 {
 	union power_supply_propval ret = {0,};
@@ -357,6 +490,12 @@ int power_supply_register(struct device *parent, struct power_supply *psy)
 
 	INIT_WORK(&psy->changed_work, power_supply_changed_work);
 
+	rc = power_supply_check_supplies(psy);
+	if (rc) {
+		dev_info(dev, "Not all required supplies found, defer probe\n");
+		goto check_supplies_failed;
+	}
+
 	rc = kobject_set_name(&dev->kobj, "%s", psy->name);
 	if (rc)
 		goto kobject_set_name_failed;
@@ -389,6 +528,7 @@ register_thermal_failed:
 	device_del(dev);
 kobject_set_name_failed:
 device_add_failed:
+check_supplies_failed:
 	put_device(dev);
 success:
 	return rc;
include/linux/power_supply.h +3 -0
index c1cbd5e..3828cef 100644
@@ -173,6 +173,9 @@ struct power_supply {
 
 	char **supplied_from;
 	size_t num_supplies;
+#ifdef CONFIG_OF
+	struct device_node *of_node;
+#endif
 
 	int (*get_property)(struct power_supply *psy,
 			    enum power_supply_property psp,
Signature · 1 line
1.7.9.5
#6 lore raw

Re: [PATCH V2 3/3] power: power_supply_core: Add support for supplied_from

Quoted 4 lines · Rhyland Klein

Adding support for supplied_from char * array. This is meant to store the list of suppliers for a given supply, i.e. chargers for a battery. This list can be populated through devicetree readily as well as passed directly from the driver.

The subject doesn't quite reflect the content of the patch... but I fixed it up.

Review

Quoted 2 lines
(quoted diff) +2 -0
+int power_supply_populate_supplied_from(struct power_supply *psy)
+{

This gives me:

CHECK drivers/power/power_supply_core.c
drivers/power/power_supply_core.c:119:5: warning: symbol 'power_supply_populate_supplied_from' was not declared. Should it be static? drivers/power/power_supply_core.c:144:5: warning: symbol 'power_supply_find_supply_from_node' was not declared. Should it be static? drivers/power/power_supply_core.c:173:5: warning: symbol 'power_supply_check_supplies' was not declared. Should it be static?

I fixed it up by making the functions static. Once you need the functions outside of _core.c, feel free to export them.

Thanks,

Anton