2013-03-04 20:11:21

by Rhyland Klein

[permalink] [raw]
Subject: [Patch v1 0/3] 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:
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

--
1.7.9.5


2013-03-04 20:11:23

by Rhyland Klein

[permalink] [raw]
Subject: [Patch v1 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.

Signed-off-by: Rhyland Klein <[email protected]>
---
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

diff --git a/Documentation/devicetree/bindings/power_supply/power_supply.txt b/Documentation/devicetree/bindings/power_supply/power_supply.txt
new file mode 100644
index 0000000..8391bfa
--- /dev/null
+++ b/Documentation/devicetree/bindings/power_supply/power_supply.txt
@@ -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>;
+ };
--
1.7.9.5

2013-03-04 20:11:45

by Rhyland Klein

[permalink] [raw]
Subject: [Patch v1 3/3] power: power_supply_core: Add support for supplied_from

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.

Signed-off-by: Rhyland Klein <[email protected]>
---
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 | 138 +++++++++++++++++++++++++++++++++++++
include/linux/power_supply.h | 3 +
2 files changed, 141 insertions(+)

diff --git a/drivers/power/power_supply_core.c b/drivers/power/power_supply_core.c
index dd675ae..c16666c 100644
--- a/drivers/power/power_supply_core.c
+++ b/drivers/power/power_supply_core.c
@@ -90,6 +90,133 @@ 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;
+ int ret = 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 {
+ 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;
+ }
+
+ ret = power_supply_populate_supplied_from(psy);
+
+ return ret;
+}
+#endif
+
static int __power_supply_am_i_supplied(struct device *dev, void *data)
{
union power_supply_propval ret = {0,};
@@ -359,6 +486,14 @@ int power_supply_register(struct device *parent, struct power_supply *psy)

INIT_WORK(&psy->changed_work, power_supply_changed_work);

+#ifdef CONFIG_OF
+ rc = power_supply_check_supplies(psy);
+ if (rc) {
+ dev_info(dev, "Not all required supplies found, defer probe\n");
+ goto check_supplies_failed;
+ }
+#endif
+
rc = kobject_set_name(&dev->kobj, "%s", psy->name);
if (rc)
goto kobject_set_name_failed;
@@ -391,6 +526,9 @@ register_thermal_failed:
device_del(dev);
kobject_set_name_failed:
device_add_failed:
+#ifdef CONFIG_OF
+check_supplies_failed:
+#endif
put_device(dev);
success:
return rc;
diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h
index c1cbd5e..3828cef 100644
--- a/include/linux/power_supply.h
+++ b/include/linux/power_supply.h
@@ -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,
--
1.7.9.5

2013-03-04 20:12:13

by Rhyland Klein

[permalink] [raw]
Subject: [Patch v1 2/3] power: power_supply: Add core support for supplied_from

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.

Signed-off-by: Rhyland Klein <[email protected]>
---
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 | 49 +++++++++++++++++++++++++++----------
include/linux/power_supply.h | 3 +++
2 files changed, 39 insertions(+), 13 deletions(-)

diff --git a/drivers/power/power_supply_core.c b/drivers/power/power_supply_core.c
index 5deac43..dd675ae 100644
--- a/drivers/power/power_supply_core.c
+++ b/drivers/power/power_supply_core.c
@@ -26,17 +26,44 @@ EXPORT_SYMBOL_GPL(power_supply_class);

static struct device_type power_supply_dev_type;

+static int __power_supply_is_supplied_by(struct power_supply *supplier,
+ struct power_supply *supply)
+{
+ int i;
+
+ if (!supply->supplied_from && !supplier->supplied_to)
+ return -EINVAL;
+
+ /* Support both supplied_to and supplied_from modes */
+ if (supply->supplied_from) {
+ for (i = 0; i < supply->num_supplies; i++) {
+ if (!supplier->name)
+ continue;
+ if (!strcmp(supplier->name, supply->supplied_from[i]))
+ return 0;
+ }
+ } else {
+ for (i = 0; i < supplier->num_supplicants; i++) {
+ if (!supply->name)
+ continue;
+ if (!strcmp(supplier->supplied_to[i], supply->name))
+ return 0;
+ }
+ }
+
+ return -EINVAL;
+}
+
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 +95,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;
}

diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h
index 002a99f..c1cbd5e 100644
--- a/include/linux/power_supply.h
+++ b/include/linux/power_supply.h
@@ -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);
--
1.7.9.5