2013-04-01 21:45:43

by Rhyland Klein

[permalink] [raw]
Subject: [PATCH V2 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:
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

--
1.7.9.5


2013-04-01 21:45:46

by Rhyland Klein

[permalink] [raw]
Subject: [PATCH V2 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]>
---
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(-)

diff --git a/drivers/power/power_supply_core.c b/drivers/power/power_supply_core.c
index 5deac43..d843cc9 100644
--- a/drivers/power/power_supply_core.c
+++ b/drivers/power/power_supply_core.c
@@ -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;
}

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

2013-04-01 21:46:06

by Rhyland Klein

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

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

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-04-01 21:46:04

by Rhyland Klein

[permalink] [raw]
Subject: [PATCH V2 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]>
---
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(+)

diff --git a/drivers/power/power_supply_core.c b/drivers/power/power_supply_core.c
index d843cc9..5e84321 100644
--- a/drivers/power/power_supply_core.c
+++ b/drivers/power/power_supply_core.c
@@ -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;
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-04-02 21:31:35

by Stephen Warren

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

On 04/01/2013 03:45 PM, Rhyland Klein wrote:
> 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,
Reviewed-by: Stephen Warren <[email protected]>

> diff --git a/drivers/power/power_supply_core.c b/drivers/power/power_supply_core.c

> +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?

> + /* 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.

2013-04-17 01:59:46

by Anton Vorontsov

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

On Tue, Apr 02, 2013 at 03:31:30PM -0600, Stephen Warren wrote:
> On 04/01/2013 03:45 PM, Rhyland Klein wrote:
> > 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,
> Reviewed-by: Stephen Warren <[email protected]>

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

2013-04-17 01:59:55

by Anton Vorontsov

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

On Mon, Apr 01, 2013 at 05:45:55PM -0400, Rhyland Klein wrote:
> 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.

> +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