Skip to content

Commit 1d85f6d

Browse files
committed
power: supply: gpio-charger: Make gpios optional
While strongly recommended, not all devices have a gpio to detect if the charger is connected. This moves the 'gpios' from required to optional section. This also modifies error handling for the GPIO a bit: We no longer fallback to pdata, if a GPIO is specified using GPIO descriptor tables. This is a bit cleaner and does not have any real impact: There are only two mainline pdata users (arm/mach-sa1100/collie.c, arm/mach-pxa/tosa.c) and none of them specify the GPIO via gpiod descriptor tables. Once both have been converted the driver's support for specifying GPIOs numbers in pdata will be dropped. Reviewed-by: Linus Walleij <[email protected]> Acked-by: Rob Herring <[email protected]> Signed-off-by: Sebastian Reichel <[email protected]>
1 parent dad980f commit 1d85f6d

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

Documentation/devicetree/bindings/power/supply/gpio-charger.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ properties:
4141

4242
required:
4343
- compatible
44-
- gpios
44+
45+
anyOf:
46+
- required:
47+
- gpios
48+
- required:
49+
- charge-status-gpios
4550

4651
additionalProperties: false
4752

drivers/power/supply/gpio-charger.c

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,14 @@ static int gpio_charger_get_irq(struct device *dev, void *dev_id,
112112
return irq;
113113
}
114114

115+
/*
116+
* The entries will be overwritten by driver's probe routine depending
117+
* on the available features. This list ensures, that the array is big
118+
* enough for all optional features.
119+
*/
115120
static enum power_supply_property gpio_charger_properties[] = {
116121
POWER_SUPPLY_PROP_ONLINE,
117-
POWER_SUPPLY_PROP_STATUS /* Must always be last in the array. */
122+
POWER_SUPPLY_PROP_STATUS,
118123
};
119124

120125
static int gpio_charger_probe(struct platform_device *pdev)
@@ -128,6 +133,7 @@ static int gpio_charger_probe(struct platform_device *pdev)
128133
int charge_status_irq;
129134
unsigned long flags;
130135
int ret;
136+
int num_props = 0;
131137

132138
if (!pdata && !dev->of_node) {
133139
dev_err(dev, "No platform data\n");
@@ -142,13 +148,13 @@ static int gpio_charger_probe(struct platform_device *pdev)
142148
* This will fetch a GPIO descriptor from device tree, ACPI or
143149
* boardfile descriptor tables. It's good to try this first.
144150
*/
145-
gpio_charger->gpiod = devm_gpiod_get(dev, NULL, GPIOD_IN);
151+
gpio_charger->gpiod = devm_gpiod_get_optional(dev, NULL, GPIOD_IN);
146152

147153
/*
148-
* If this fails and we're not using device tree, try the
149-
* legacy platform data method.
154+
* Fallback to legacy platform data method, if no GPIO is specified
155+
* using boardfile descriptor tables.
150156
*/
151-
if (IS_ERR(gpio_charger->gpiod) && !dev->of_node) {
157+
if (!gpio_charger->gpiod && pdata) {
152158
/* Non-DT: use legacy GPIO numbers */
153159
if (!gpio_is_valid(pdata->gpio)) {
154160
dev_err(dev, "Invalid gpio pin in pdata\n");
@@ -173,17 +179,23 @@ static int gpio_charger_probe(struct platform_device *pdev)
173179
return PTR_ERR(gpio_charger->gpiod);
174180
}
175181

182+
if (gpio_charger->gpiod) {
183+
gpio_charger_properties[num_props] = POWER_SUPPLY_PROP_ONLINE;
184+
num_props++;
185+
}
186+
176187
charge_status = devm_gpiod_get_optional(dev, "charge-status", GPIOD_IN);
177-
gpio_charger->charge_status = charge_status;
178-
if (IS_ERR(gpio_charger->charge_status))
179-
return PTR_ERR(gpio_charger->charge_status);
188+
if (IS_ERR(charge_status))
189+
return PTR_ERR(charge_status);
190+
if (charge_status) {
191+
gpio_charger->charge_status = charge_status;
192+
gpio_charger_properties[num_props] = POWER_SUPPLY_PROP_STATUS;
193+
num_props++;
194+
}
180195

181196
charger_desc = &gpio_charger->charger_desc;
182197
charger_desc->properties = gpio_charger_properties;
183-
charger_desc->num_properties = ARRAY_SIZE(gpio_charger_properties);
184-
/* Remove POWER_SUPPLY_PROP_STATUS from the supported properties. */
185-
if (!gpio_charger->charge_status)
186-
charger_desc->num_properties -= 1;
198+
charger_desc->num_properties = num_props;
187199
charger_desc->get_property = gpio_charger_get_property;
188200

189201
psy_cfg.of_node = dev->of_node;
@@ -269,6 +281,6 @@ static struct platform_driver gpio_charger_driver = {
269281
module_platform_driver(gpio_charger_driver);
270282

271283
MODULE_AUTHOR("Lars-Peter Clausen <[email protected]>");
272-
MODULE_DESCRIPTION("Driver for chargers which report their online status through a GPIO");
284+
MODULE_DESCRIPTION("Driver for chargers only communicating via GPIO(s)");
273285
MODULE_LICENSE("GPL");
274286
MODULE_ALIAS("platform:gpio-charger");

0 commit comments

Comments
 (0)