Skip to contentSkip to conversation

gpio-charger: add devicetree support

PATCH Superseded Heiko Stuebner 2 participants 6 messages Sep 23, 2014 Device tree bindings

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover gpio-charger: add devicetree support

This series adds devicetree support to the gpio-charger and fixes a small issue with the return value of gpio_get_value.

changes since v2:
- fix (hopefully all) issues pointed out by Doug Anderson
- add missing of_node assignment to charger
changes since v1:
- adapt binding example to show how the charger fits into the power-supplies structure

Heiko Stuebner (3):
dt-bindings: document gpio-charger bindings
power: gpio-charger: add device tree support
power: gpio-charger: do not use gpio value directly

 .../bindings/power_supply/gpio-charger.txt         | 27 ++++++++
 drivers/power/gpio-charger.c                       | 74 +++++++++++++++++++++-
 2 files changed, 98 insertions(+), 3 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/power_supply/gpio-charger.txt
Signature · 6 lines
2.1.0

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#1 lore raw

PATCH v3 1/3 dt-bindings: document gpio-charger bindings

From: Heiko Stuebner <[email protected]>

This documents the binding for the gpio-charger power-supply.

---
 .../bindings/power_supply/gpio-charger.txt         | 27 ++++++++++++++++++++++
 1 file changed, 27 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/power_supply/gpio-charger.txt
Documentation/devicetree/bindings/power_supply/gpio-charger.txt new file +27 -0
new file mode 100644
index 0000000..adbb5dc
@@ -0,0 +1,27 @@
+gpio-charger
+
+Required properties :
+ - compatible : "gpio-charger"
+ - gpios : GPIO indicating the charger presence.
+   See GPIO binding in bindings/gpio/gpio.txt .
+ - charger-type : power supply type, one of
+     unknown
+     battery
+     ups
+     mains
+     usb-sdp (USB standard downstream port)
+     usb-dcp (USB dedicated charging port)
+     usb-cdp (USB charging downstream port)
+     usb-aca (USB accessory charger adapter)
+
+Example:
+
+	usb_charger: charger {
+		compatible = "gpio-charger";
+		charger-type = "usb-sdp";
+		gpios = <&gpf0 2 0 0 0>;
+	}
+
+	battery {
+		power-supplies = <&usb_charger>;
+	};
Signature · 1 line
2.1.0
#2 lore raw

PATCH v3 2/3 power: gpio-charger: add device tree support

In reply to Heiko Stuebner

From: Heiko Stuebner <[email protected]>

Add the ability to parse gpio-charger data from a devicetree node.

---
 drivers/power/gpio-charger.c | 72 ++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 70 insertions(+), 2 deletions(-)
drivers/power/gpio-charger.c +70 -2
index a0024b2..22f084a 100644
@@ -22,6 +22,8 @@
 #include <linux/platform_device.h>
 #include <linux/power_supply.h>
 #include <linux/slab.h>
+#include <linux/of.h>
+#include <linux/of_gpio.h>
 
 #include <linux/power/gpio-charger.h>
 
@@ -69,6 +71,59 @@ static enum power_supply_property gpio_charger_properties[] = {
 	POWER_SUPPLY_PROP_ONLINE,
 };
 
+static
+struct gpio_charger_platform_data *gpio_charger_parse_dt(struct device *dev)
+{
+	struct device_node *np = dev->of_node;
+	struct gpio_charger_platform_data *pdata;
+	const char *chargetype;
+	enum of_gpio_flags flags;
+	int ret;
+
+	if (!np)
+		return ERR_PTR(-ENOENT);
+
+	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
+	if (!pdata)
+		return ERR_PTR(-ENOMEM);
+
+	pdata->name = np->name;
+
+	pdata->gpio = of_get_gpio_flags(np, 0, &flags);
+	if (pdata->gpio < 0) {
+		if (pdata->gpio != -EPROBE_DEFER)
+			dev_err(dev, "could not get charger gpio\n");
+		return ERR_PTR(pdata->gpio);
+	}
+
+	pdata->gpio_active_low = !!(flags & OF_GPIO_ACTIVE_LOW);
+
+	pdata->type = POWER_SUPPLY_TYPE_UNKNOWN;
+	ret = of_property_read_string(np, "charger-type", &chargetype);
+	if (ret >= 0) {
+		if (!strncmp("unknown", chargetype, 7))
+			pdata->type = POWER_SUPPLY_TYPE_UNKNOWN;
+		else if (!strncmp("battery", chargetype, 7))
+			pdata->type = POWER_SUPPLY_TYPE_BATTERY;
+		else if (!strncmp("ups", chargetype, 3))
+			pdata->type = POWER_SUPPLY_TYPE_UPS;
+		else if (!strncmp("mains", chargetype, 5))
+			pdata->type = POWER_SUPPLY_TYPE_MAINS;
+		else if (!strncmp("usb-sdp", chargetype, 7))
+			pdata->type = POWER_SUPPLY_TYPE_USB;
+		else if (!strncmp("usb-dcp", chargetype, 7))
+			pdata->type = POWER_SUPPLY_TYPE_USB_DCP;
+		else if (!strncmp("usb-cdp", chargetype, 7))
+			pdata->type = POWER_SUPPLY_TYPE_USB_CDP;
+		else if (!strncmp("usb-aca", chargetype, 7))
+			pdata->type = POWER_SUPPLY_TYPE_USB_ACA;
+		else
+			dev_warn(dev, "unknown charger type %s\n", chargetype);
+	}
+
+	return pdata;
+}
+
 static int gpio_charger_probe(struct platform_device *pdev)
 {
 	const struct gpio_charger_platform_data *pdata = pdev->dev.platform_data;
@@ -78,8 +133,13 @@ static int gpio_charger_probe(struct platform_device *pdev)
 	int irq;
 
 	if (!pdata) {
-		dev_err(&pdev->dev, "No platform data\n");
-		return -EINVAL;
+		pdata = gpio_charger_parse_dt(&pdev->dev);
+		if (IS_ERR(pdata)) {
+			ret = PTR_ERR(pdata);
+			if (ret != -EPROBE_DEFER)
+				dev_err(&pdev->dev, "No platform data\n");
+			return ret;
+		}
 	}
 
 	if (!gpio_is_valid(pdata->gpio)) {
@@ -103,6 +163,7 @@ static int gpio_charger_probe(struct platform_device *pdev)
 	charger->get_property = gpio_charger_get_property;
 	charger->supplied_to = pdata->supplied_to;
 	charger->num_supplicants = pdata->num_supplicants;
+	charger->of_node = pdev->dev.of_node;
 
 	ret = gpio_request(pdata->gpio, dev_name(&pdev->dev));
 	if (ret) {
@@ -189,6 +250,12 @@ static int gpio_charger_resume(struct device *dev)
 static SIMPLE_DEV_PM_OPS(gpio_charger_pm_ops,
 		gpio_charger_suspend, gpio_charger_resume);
 
+static const struct of_device_id gpio_charger_match[] = {
+	{ .compatible = "gpio-charger" },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, gpio_charger_match);
+
 static struct platform_driver gpio_charger_driver = {
 	.probe = gpio_charger_probe,
 	.remove = gpio_charger_remove,
@@ -196,6 +263,7 @@ static struct platform_driver gpio_charger_driver = {
 		.name = "gpio-charger",
 		.owner = THIS_MODULE,
 		.pm = &gpio_charger_pm_ops,
+		.of_match_table = of_match_ptr(gpio_charger_match),
 	},
 };
 
Signature · 1 line
2.1.0
#4 lore raw

PATCH v3 3/3 power: gpio-charger: do not use gpio value directly

In reply to Heiko Stuebner

From: Heiko Stuebner <[email protected]>

Some gpio implementations return interesting values for gpio_get_value when the value is not 0 - as seen on a imx6sl board. Therefore do not use the value returned from gpio_get_value directly but simply check for 0 or not 0.

---
 drivers/power/gpio-charger.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/power/gpio-charger.c +1 -1
index 22f084a..7d4eab2 100644
@@ -57,7 +57,7 @@ static int gpio_charger_get_property(struct power_supply *psy,
 
 	switch (psp) {
 	case POWER_SUPPLY_PROP_ONLINE:
-		val->intval = gpio_get_value_cansleep(pdata->gpio);
+		val->intval = !!gpio_get_value_cansleep(pdata->gpio);
 		val->intval ^= pdata->gpio_active_low;
 		break;
 	default:
Signature · 1 line
2.1.0
Reviewed-by Tested-by on v3 3/3
#5 lore raw

Re: [PATCH v3 3/3] power: gpio-charger: do not use gpio value directly

Heiko,

Quoted 16 lines · Heiko Stuebner

From: Heiko Stuebner <[email protected]>

Some gpio implementations return interesting values for gpio_get_value when the value is not 0 - as seen on a imx6sl board. Therefore do not use the value returned from gpio_get_value directly but simply check for 0 or not 0.

---
 drivers/power/gpio-charger.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/power/gpio-charger.c +0 -0
index 22f084a..7d4eab2 100644

drivers/power/gpio-charger.c static int gpio_charger_get_property(struct power_supply *psy, · line 61

Quoted 6 lines
drivers/power/gpio-charger.c +1 -1
@@ -57,7 +57,7 @@ static int gpio_charger_get_property(struct power_supply *psy,
 
        switch (psp) {
        case POWER_SUPPLY_PROP_ONLINE:
-               val->intval = gpio_get_value_cansleep(pdata->gpio);
+               val->intval = !!gpio_get_value_cansleep(pdata->gpio);
                val->intval ^= pdata->gpio_active_low;

...so I guess the interface is that gpio_active_low must be exactly 1 or 0. Oh, and that's actually defined in gpio-charger.h! I should have checked that before.

Reviewed-by Tested-by on v3 2/3
#3 lore raw

Re: [PATCH v3 2/3] power: gpio-charger: add device tree support

In reply to Heiko Stuebner

Heiko,

Quoted 120 lines · Heiko Stuebner

From: Heiko Stuebner <[email protected]>

Add the ability to parse gpio-charger data from a devicetree node.

---
 drivers/power/gpio-charger.c | 72 ++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 70 insertions(+), 2 deletions(-)
drivers/power/gpio-charger.c +69 -2
index a0024b2..22f084a 100644
@@ -22,6 +22,8 @@
 #include <linux/platform_device.h>
 #include <linux/power_supply.h>
 #include <linux/slab.h>
+#include <linux/of.h>
+#include <linux/of_gpio.h>
 
 #include <linux/power/gpio-charger.h>
 
@@ -69,6 +71,59 @@ static enum power_supply_property gpio_charger_properties[] = {
        POWER_SUPPLY_PROP_ONLINE,
 };
 
+static
+struct gpio_charger_platform_data *gpio_charger_parse_dt(struct device *dev)
+{
+       struct device_node *np = dev->of_node;
+       struct gpio_charger_platform_data *pdata;
+       const char *chargetype;
+       enum of_gpio_flags flags;
+       int ret;
+
+       if (!np)
+               return ERR_PTR(-ENOENT);
+
+       pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
+       if (!pdata)
+               return ERR_PTR(-ENOMEM);
+
+       pdata->name = np->name;
+
+       pdata->gpio = of_get_gpio_flags(np, 0, &flags);
+       if (pdata->gpio < 0) {
+               if (pdata->gpio != -EPROBE_DEFER)
+                       dev_err(dev, "could not get charger gpio\n");
+               return ERR_PTR(pdata->gpio);
+       }
+
+       pdata->gpio_active_low = !!(flags & OF_GPIO_ACTIVE_LOW);
+
+       pdata->type = POWER_SUPPLY_TYPE_UNKNOWN;
+       ret = of_property_read_string(np, "charger-type", &chargetype);
+       if (ret >= 0) {
+               if (!strncmp("unknown", chargetype, 7))
+                       pdata->type = POWER_SUPPLY_TYPE_UNKNOWN;
+               else if (!strncmp("battery", chargetype, 7))
+                       pdata->type = POWER_SUPPLY_TYPE_BATTERY;
+               else if (!strncmp("ups", chargetype, 3))
+                       pdata->type = POWER_SUPPLY_TYPE_UPS;
+               else if (!strncmp("mains", chargetype, 5))
+                       pdata->type = POWER_SUPPLY_TYPE_MAINS;
+               else if (!strncmp("usb-sdp", chargetype, 7))
+                       pdata->type = POWER_SUPPLY_TYPE_USB;
+               else if (!strncmp("usb-dcp", chargetype, 7))
+                       pdata->type = POWER_SUPPLY_TYPE_USB_DCP;
+               else if (!strncmp("usb-cdp", chargetype, 7))
+                       pdata->type = POWER_SUPPLY_TYPE_USB_CDP;
+               else if (!strncmp("usb-aca", chargetype, 7))
+                       pdata->type = POWER_SUPPLY_TYPE_USB_ACA;
+               else
+                       dev_warn(dev, "unknown charger type %s\n", chargetype);
+       }
+
+       return pdata;
+}
+
 static int gpio_charger_probe(struct platform_device *pdev)
 {
        const struct gpio_charger_platform_data *pdata = pdev->dev.platform_data;
@@ -78,8 +133,13 @@ static int gpio_charger_probe(struct platform_device *pdev)
        int irq;
 
        if (!pdata) {
-               dev_err(&pdev->dev, "No platform data\n");
-               return -EINVAL;
+               pdata = gpio_charger_parse_dt(&pdev->dev);
+               if (IS_ERR(pdata)) {
+                       ret = PTR_ERR(pdata);
+                       if (ret != -EPROBE_DEFER)
+                               dev_err(&pdev->dev, "No platform data\n");
+                       return ret;
+               }
        }
 
        if (!gpio_is_valid(pdata->gpio)) {
@@ -103,6 +163,7 @@ static int gpio_charger_probe(struct platform_device *pdev)
        charger->get_property = gpio_charger_get_property;
        charger->supplied_to = pdata->supplied_to;
        charger->num_supplicants = pdata->num_supplicants;
+       charger->of_node = pdev->dev.of_node;
 
        ret = gpio_request(pdata->gpio, dev_name(&pdev->dev));
        if (ret) {
@@ -189,6 +250,12 @@ static int gpio_charger_resume(struct device *dev)
 static SIMPLE_DEV_PM_OPS(gpio_charger_pm_ops,
                gpio_charger_suspend, gpio_charger_resume);
 
+static const struct of_device_id gpio_charger_match[] = {
+       { .compatible = "gpio-charger" },
+       { }
+};
+MODULE_DEVICE_TABLE(of, gpio_charger_match);
+
 static struct platform_driver gpio_charger_driver = {
        .probe = gpio_charger_probe,
        .remove = gpio_charger_remove,

drivers/power/gpio-charger.c static struct platform_driver gpio_charger_driver = { · line 266

Quoted 4 lines
drivers/power/gpio-charger.c +1 -0
@@ -196,6 +263,7 @@ static struct platform_driver gpio_charger_driver = {
                .name = "gpio-charger",
                .owner = THIS_MODULE,
                .pm = &gpio_charger_pm_ops,
+               .of_match_table = of_match_ptr(gpio_charger_match),

I'm still not convinced about of_match_ptr() here as you can see in my response to v2. ...but at most it could give a warning that would be something like "gpio_charger_match defined but not used" in the !CONFIG_OF case, so I guess it's fine?

In any case, everything else looks reasonable: