2 * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
3 * Driver for chargers which report their online status through a GPIO pin
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
10 * You should have received a copy of the GNU General Public License along
11 * with this program; if not, write to the Free Software Foundation, Inc.,
12 * 675 Mass Ave, Cambridge, MA 02139, USA.
16 #include <linux/device.h>
17 #include <linux/gpio.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/power_supply.h>
24 #include <linux/slab.h>
26 #include <linux/of_gpio.h>
28 #include <linux/power/gpio-charger.h>
31 const struct gpio_charger_platform_data
*pdata
;
35 struct power_supply charger
;
38 static irqreturn_t
gpio_charger_irq(int irq
, void *devid
)
40 struct power_supply
*charger
= devid
;
42 power_supply_changed(charger
);
47 static inline struct gpio_charger
*psy_to_gpio_charger(struct power_supply
*psy
)
49 return container_of(psy
, struct gpio_charger
, charger
);
52 static int gpio_charger_get_property(struct power_supply
*psy
,
53 enum power_supply_property psp
, union power_supply_propval
*val
)
55 struct gpio_charger
*gpio_charger
= psy_to_gpio_charger(psy
);
56 const struct gpio_charger_platform_data
*pdata
= gpio_charger
->pdata
;
59 case POWER_SUPPLY_PROP_ONLINE
:
60 val
->intval
= !!gpio_get_value_cansleep(pdata
->gpio
);
61 val
->intval
^= pdata
->gpio_active_low
;
70 static enum power_supply_property gpio_charger_properties
[] = {
71 POWER_SUPPLY_PROP_ONLINE
,
75 struct gpio_charger_platform_data
*gpio_charger_parse_dt(struct device
*dev
)
77 struct device_node
*np
= dev
->of_node
;
78 struct gpio_charger_platform_data
*pdata
;
79 const char *chargetype
;
80 enum of_gpio_flags flags
;
84 return ERR_PTR(-ENOENT
);
86 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
88 return ERR_PTR(-ENOMEM
);
90 pdata
->name
= np
->name
;
92 pdata
->gpio
= of_get_gpio_flags(np
, 0, &flags
);
93 if (pdata
->gpio
< 0) {
94 if (pdata
->gpio
!= -EPROBE_DEFER
)
95 dev_err(dev
, "could not get charger gpio\n");
96 return ERR_PTR(pdata
->gpio
);
99 pdata
->gpio_active_low
= !!(flags
& OF_GPIO_ACTIVE_LOW
);
101 pdata
->type
= POWER_SUPPLY_TYPE_UNKNOWN
;
102 ret
= of_property_read_string(np
, "charger-type", &chargetype
);
104 if (!strncmp("unknown", chargetype
, 7))
105 pdata
->type
= POWER_SUPPLY_TYPE_UNKNOWN
;
106 else if (!strncmp("battery", chargetype
, 7))
107 pdata
->type
= POWER_SUPPLY_TYPE_BATTERY
;
108 else if (!strncmp("ups", chargetype
, 3))
109 pdata
->type
= POWER_SUPPLY_TYPE_UPS
;
110 else if (!strncmp("mains", chargetype
, 5))
111 pdata
->type
= POWER_SUPPLY_TYPE_MAINS
;
112 else if (!strncmp("usb-sdp", chargetype
, 7))
113 pdata
->type
= POWER_SUPPLY_TYPE_USB
;
114 else if (!strncmp("usb-dcp", chargetype
, 7))
115 pdata
->type
= POWER_SUPPLY_TYPE_USB_DCP
;
116 else if (!strncmp("usb-cdp", chargetype
, 7))
117 pdata
->type
= POWER_SUPPLY_TYPE_USB_CDP
;
118 else if (!strncmp("usb-aca", chargetype
, 7))
119 pdata
->type
= POWER_SUPPLY_TYPE_USB_ACA
;
121 dev_warn(dev
, "unknown charger type %s\n", chargetype
);
127 static int gpio_charger_probe(struct platform_device
*pdev
)
129 const struct gpio_charger_platform_data
*pdata
= pdev
->dev
.platform_data
;
130 struct gpio_charger
*gpio_charger
;
131 struct power_supply
*charger
;
136 pdata
= gpio_charger_parse_dt(&pdev
->dev
);
138 ret
= PTR_ERR(pdata
);
139 if (ret
!= -EPROBE_DEFER
)
140 dev_err(&pdev
->dev
, "No platform data\n");
145 if (!gpio_is_valid(pdata
->gpio
)) {
146 dev_err(&pdev
->dev
, "Invalid gpio pin\n");
150 gpio_charger
= devm_kzalloc(&pdev
->dev
, sizeof(*gpio_charger
),
153 dev_err(&pdev
->dev
, "Failed to alloc driver structure\n");
157 charger
= &gpio_charger
->charger
;
159 charger
->name
= pdata
->name
? pdata
->name
: "gpio-charger";
160 charger
->type
= pdata
->type
;
161 charger
->properties
= gpio_charger_properties
;
162 charger
->num_properties
= ARRAY_SIZE(gpio_charger_properties
);
163 charger
->get_property
= gpio_charger_get_property
;
164 charger
->supplied_to
= pdata
->supplied_to
;
165 charger
->num_supplicants
= pdata
->num_supplicants
;
166 charger
->of_node
= pdev
->dev
.of_node
;
168 ret
= gpio_request(pdata
->gpio
, dev_name(&pdev
->dev
));
170 dev_err(&pdev
->dev
, "Failed to request gpio pin: %d\n", ret
);
173 ret
= gpio_direction_input(pdata
->gpio
);
175 dev_err(&pdev
->dev
, "Failed to set gpio to input: %d\n", ret
);
179 gpio_charger
->pdata
= pdata
;
181 ret
= power_supply_register(&pdev
->dev
, charger
);
183 dev_err(&pdev
->dev
, "Failed to register power supply: %d\n",
188 irq
= gpio_to_irq(pdata
->gpio
);
190 ret
= request_any_context_irq(irq
, gpio_charger_irq
,
191 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
,
192 dev_name(&pdev
->dev
), charger
);
194 dev_warn(&pdev
->dev
, "Failed to request irq: %d\n", ret
);
196 gpio_charger
->irq
= irq
;
199 platform_set_drvdata(pdev
, gpio_charger
);
201 device_init_wakeup(&pdev
->dev
, 1);
206 gpio_free(pdata
->gpio
);
211 static int gpio_charger_remove(struct platform_device
*pdev
)
213 struct gpio_charger
*gpio_charger
= platform_get_drvdata(pdev
);
215 if (gpio_charger
->irq
)
216 free_irq(gpio_charger
->irq
, &gpio_charger
->charger
);
218 power_supply_unregister(&gpio_charger
->charger
);
220 gpio_free(gpio_charger
->pdata
->gpio
);
225 #ifdef CONFIG_PM_SLEEP
226 static int gpio_charger_suspend(struct device
*dev
)
228 struct gpio_charger
*gpio_charger
= dev_get_drvdata(dev
);
230 if (device_may_wakeup(dev
))
231 gpio_charger
->wakeup_enabled
=
232 !enable_irq_wake(gpio_charger
->irq
);
237 static int gpio_charger_resume(struct device
*dev
)
239 struct platform_device
*pdev
= to_platform_device(dev
);
240 struct gpio_charger
*gpio_charger
= platform_get_drvdata(pdev
);
242 if (device_may_wakeup(dev
) && gpio_charger
->wakeup_enabled
)
243 disable_irq_wake(gpio_charger
->irq
);
244 power_supply_changed(&gpio_charger
->charger
);
250 static SIMPLE_DEV_PM_OPS(gpio_charger_pm_ops
,
251 gpio_charger_suspend
, gpio_charger_resume
);
253 static const struct of_device_id gpio_charger_match
[] = {
254 { .compatible
= "gpio-charger" },
257 MODULE_DEVICE_TABLE(of
, gpio_charger_match
);
259 static struct platform_driver gpio_charger_driver
= {
260 .probe
= gpio_charger_probe
,
261 .remove
= gpio_charger_remove
,
263 .name
= "gpio-charger",
264 .pm
= &gpio_charger_pm_ops
,
265 .of_match_table
= gpio_charger_match
,
269 module_platform_driver(gpio_charger_driver
);
271 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
272 MODULE_DESCRIPTION("Driver for chargers which report their online status through a GPIO");
273 MODULE_LICENSE("GPL");
274 MODULE_ALIAS("platform:gpio-charger");