blob: c5869b1941acddbe75a1221527238d6641d6daf3 [file] [log] [blame]
Lars-Peter Clausen50704372010-10-21 17:55:01 +02001/*
2 * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
3 * Driver for chargers which report their online status through a GPIO pin
4 *
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.
9 *
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.
13 *
14 */
15
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>
Heiko Stuebnerc8dd9cc2014-09-23 22:42:15 +020025#include <linux/of.h>
26#include <linux/of_gpio.h>
Lars-Peter Clausen50704372010-10-21 17:55:01 +020027
28#include <linux/power/gpio-charger.h>
29
30struct gpio_charger {
31 const struct gpio_charger_platform_data *pdata;
32 unsigned int irq;
Dmitry Eremin-Solenikov4ea81262013-12-11 02:47:16 +040033 bool wakeup_enabled;
Lars-Peter Clausen50704372010-10-21 17:55:01 +020034
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010035 struct power_supply *charger;
36 struct power_supply_desc charger_desc;
Lars-Peter Clausen50704372010-10-21 17:55:01 +020037};
38
39static irqreturn_t gpio_charger_irq(int irq, void *devid)
40{
41 struct power_supply *charger = devid;
42
43 power_supply_changed(charger);
44
45 return IRQ_HANDLED;
46}
47
48static inline struct gpio_charger *psy_to_gpio_charger(struct power_supply *psy)
49{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010050 return power_supply_get_drvdata(psy);
Lars-Peter Clausen50704372010-10-21 17:55:01 +020051}
52
53static int gpio_charger_get_property(struct power_supply *psy,
54 enum power_supply_property psp, union power_supply_propval *val)
55{
56 struct gpio_charger *gpio_charger = psy_to_gpio_charger(psy);
57 const struct gpio_charger_platform_data *pdata = gpio_charger->pdata;
58
59 switch (psp) {
60 case POWER_SUPPLY_PROP_ONLINE:
Heiko Stuebner4d96fb12014-09-23 22:42:16 +020061 val->intval = !!gpio_get_value_cansleep(pdata->gpio);
Lars-Peter Clausen50704372010-10-21 17:55:01 +020062 val->intval ^= pdata->gpio_active_low;
63 break;
64 default:
65 return -EINVAL;
66 }
67
68 return 0;
69}
70
71static enum power_supply_property gpio_charger_properties[] = {
72 POWER_SUPPLY_PROP_ONLINE,
73};
74
Heiko Stuebnerc8dd9cc2014-09-23 22:42:15 +020075static
76struct gpio_charger_platform_data *gpio_charger_parse_dt(struct device *dev)
77{
78 struct device_node *np = dev->of_node;
79 struct gpio_charger_platform_data *pdata;
80 const char *chargetype;
81 enum of_gpio_flags flags;
82 int ret;
83
84 if (!np)
85 return ERR_PTR(-ENOENT);
86
87 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
88 if (!pdata)
89 return ERR_PTR(-ENOMEM);
90
91 pdata->name = np->name;
92
93 pdata->gpio = of_get_gpio_flags(np, 0, &flags);
94 if (pdata->gpio < 0) {
95 if (pdata->gpio != -EPROBE_DEFER)
96 dev_err(dev, "could not get charger gpio\n");
97 return ERR_PTR(pdata->gpio);
98 }
99
100 pdata->gpio_active_low = !!(flags & OF_GPIO_ACTIVE_LOW);
101
102 pdata->type = POWER_SUPPLY_TYPE_UNKNOWN;
103 ret = of_property_read_string(np, "charger-type", &chargetype);
104 if (ret >= 0) {
105 if (!strncmp("unknown", chargetype, 7))
106 pdata->type = POWER_SUPPLY_TYPE_UNKNOWN;
107 else if (!strncmp("battery", chargetype, 7))
108 pdata->type = POWER_SUPPLY_TYPE_BATTERY;
109 else if (!strncmp("ups", chargetype, 3))
110 pdata->type = POWER_SUPPLY_TYPE_UPS;
111 else if (!strncmp("mains", chargetype, 5))
112 pdata->type = POWER_SUPPLY_TYPE_MAINS;
113 else if (!strncmp("usb-sdp", chargetype, 7))
114 pdata->type = POWER_SUPPLY_TYPE_USB;
115 else if (!strncmp("usb-dcp", chargetype, 7))
116 pdata->type = POWER_SUPPLY_TYPE_USB_DCP;
117 else if (!strncmp("usb-cdp", chargetype, 7))
118 pdata->type = POWER_SUPPLY_TYPE_USB_CDP;
119 else if (!strncmp("usb-aca", chargetype, 7))
120 pdata->type = POWER_SUPPLY_TYPE_USB_ACA;
121 else
122 dev_warn(dev, "unknown charger type %s\n", chargetype);
123 }
124
125 return pdata;
126}
127
Bill Pembertonc8afa642012-11-19 13:22:23 -0500128static int gpio_charger_probe(struct platform_device *pdev)
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200129{
130 const struct gpio_charger_platform_data *pdata = pdev->dev.platform_data;
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +0100131 struct power_supply_config psy_cfg = {};
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200132 struct gpio_charger *gpio_charger;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100133 struct power_supply_desc *charger_desc;
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200134 int ret;
135 int irq;
136
137 if (!pdata) {
Heiko Stuebnerc8dd9cc2014-09-23 22:42:15 +0200138 pdata = gpio_charger_parse_dt(&pdev->dev);
139 if (IS_ERR(pdata)) {
140 ret = PTR_ERR(pdata);
141 if (ret != -EPROBE_DEFER)
142 dev_err(&pdev->dev, "No platform data\n");
143 return ret;
144 }
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200145 }
146
147 if (!gpio_is_valid(pdata->gpio)) {
148 dev_err(&pdev->dev, "Invalid gpio pin\n");
149 return -EINVAL;
150 }
151
Jingoo Hanb92ff852013-03-11 15:33:27 +0900152 gpio_charger = devm_kzalloc(&pdev->dev, sizeof(*gpio_charger),
153 GFP_KERNEL);
Lars-Peter Clausen2e9ff5f2010-11-18 23:08:37 +0100154 if (!gpio_charger) {
155 dev_err(&pdev->dev, "Failed to alloc driver structure\n");
156 return -ENOMEM;
157 }
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200158
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100159 charger_desc = &gpio_charger->charger_desc;
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200160
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100161 charger_desc->name = pdata->name ? pdata->name : "gpio-charger";
162 charger_desc->type = pdata->type;
163 charger_desc->properties = gpio_charger_properties;
164 charger_desc->num_properties = ARRAY_SIZE(gpio_charger_properties);
165 charger_desc->get_property = gpio_charger_get_property;
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +0100166
167 psy_cfg.supplied_to = pdata->supplied_to;
168 psy_cfg.num_supplicants = pdata->num_supplicants;
169 psy_cfg.of_node = pdev->dev.of_node;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100170 psy_cfg.drv_data = gpio_charger;
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200171
172 ret = gpio_request(pdata->gpio, dev_name(&pdev->dev));
173 if (ret) {
174 dev_err(&pdev->dev, "Failed to request gpio pin: %d\n", ret);
175 goto err_free;
176 }
177 ret = gpio_direction_input(pdata->gpio);
178 if (ret) {
179 dev_err(&pdev->dev, "Failed to set gpio to input: %d\n", ret);
180 goto err_gpio_free;
181 }
182
Lars-Peter Clausen26eb3872010-11-18 23:08:38 +0100183 gpio_charger->pdata = pdata;
184
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100185 gpio_charger->charger = power_supply_register(&pdev->dev,
186 charger_desc, &psy_cfg);
187 if (IS_ERR(gpio_charger->charger)) {
188 ret = PTR_ERR(gpio_charger->charger);
Lars-Peter Clausen26eb3872010-11-18 23:08:38 +0100189 dev_err(&pdev->dev, "Failed to register power supply: %d\n",
190 ret);
191 goto err_gpio_free;
192 }
193
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200194 irq = gpio_to_irq(pdata->gpio);
195 if (irq > 0) {
Lars-Peter Clausen26eb3872010-11-18 23:08:38 +0100196 ret = request_any_context_irq(irq, gpio_charger_irq,
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200197 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100198 dev_name(&pdev->dev), gpio_charger->charger);
Axel Linf8c63912011-07-04 18:04:15 +0800199 if (ret < 0)
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200200 dev_warn(&pdev->dev, "Failed to request irq: %d\n", ret);
201 else
202 gpio_charger->irq = irq;
203 }
204
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200205 platform_set_drvdata(pdev, gpio_charger);
206
Dmitry Eremin-Solenikov4ea81262013-12-11 02:47:16 +0400207 device_init_wakeup(&pdev->dev, 1);
208
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200209 return 0;
210
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200211err_gpio_free:
212 gpio_free(pdata->gpio);
213err_free:
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200214 return ret;
215}
216
Bill Pemberton415ec692012-11-19 13:26:07 -0500217static int gpio_charger_remove(struct platform_device *pdev)
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200218{
219 struct gpio_charger *gpio_charger = platform_get_drvdata(pdev);
220
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200221 if (gpio_charger->irq)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100222 free_irq(gpio_charger->irq, gpio_charger->charger);
Lars-Peter Clausen26eb3872010-11-18 23:08:38 +0100223
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100224 power_supply_unregister(gpio_charger->charger);
Lars-Peter Clausen26eb3872010-11-18 23:08:38 +0100225
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200226 gpio_free(gpio_charger->pdata->gpio);
227
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200228 return 0;
229}
230
Benson Leungf10513d2011-04-06 16:55:20 -0700231#ifdef CONFIG_PM_SLEEP
Dmitry Eremin-Solenikov4ea81262013-12-11 02:47:16 +0400232static int gpio_charger_suspend(struct device *dev)
233{
234 struct gpio_charger *gpio_charger = dev_get_drvdata(dev);
235
236 if (device_may_wakeup(dev))
237 gpio_charger->wakeup_enabled =
Dmitry Eremin-Solenikovfaeed512015-01-15 05:00:37 +0300238 !enable_irq_wake(gpio_charger->irq);
Dmitry Eremin-Solenikov4ea81262013-12-11 02:47:16 +0400239
240 return 0;
241}
242
Benson Leungf10513d2011-04-06 16:55:20 -0700243static int gpio_charger_resume(struct device *dev)
244{
245 struct platform_device *pdev = to_platform_device(dev);
246 struct gpio_charger *gpio_charger = platform_get_drvdata(pdev);
247
Dmitry Eremin-Solenikovfaeed512015-01-15 05:00:37 +0300248 if (device_may_wakeup(dev) && gpio_charger->wakeup_enabled)
Dmitry Eremin-Solenikov4ea81262013-12-11 02:47:16 +0400249 disable_irq_wake(gpio_charger->irq);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100250 power_supply_changed(gpio_charger->charger);
Benson Leungf10513d2011-04-06 16:55:20 -0700251
252 return 0;
253}
254#endif
255
Dmitry Eremin-Solenikov4ea81262013-12-11 02:47:16 +0400256static SIMPLE_DEV_PM_OPS(gpio_charger_pm_ops,
257 gpio_charger_suspend, gpio_charger_resume);
Benson Leungf10513d2011-04-06 16:55:20 -0700258
Heiko Stuebnerc8dd9cc2014-09-23 22:42:15 +0200259static const struct of_device_id gpio_charger_match[] = {
260 { .compatible = "gpio-charger" },
261 { }
262};
263MODULE_DEVICE_TABLE(of, gpio_charger_match);
264
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200265static struct platform_driver gpio_charger_driver = {
266 .probe = gpio_charger_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -0500267 .remove = gpio_charger_remove,
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200268 .driver = {
269 .name = "gpio-charger",
Benson Leungf10513d2011-04-06 16:55:20 -0700270 .pm = &gpio_charger_pm_ops,
Heiko Stuebnerc8dd9cc2014-09-23 22:42:15 +0200271 .of_match_table = gpio_charger_match,
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200272 },
273};
274
Axel Lin300bac72011-11-26 12:01:10 +0800275module_platform_driver(gpio_charger_driver);
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200276
277MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
278MODULE_DESCRIPTION("Driver for chargers which report their online status through a GPIO");
279MODULE_LICENSE("GPL");
280MODULE_ALIAS("platform:gpio-charger");