blob: aef74bdf7ab31690ebb1d286df3a593e24d18788 [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
35 struct power_supply charger;
36};
37
38static irqreturn_t gpio_charger_irq(int irq, void *devid)
39{
40 struct power_supply *charger = devid;
41
42 power_supply_changed(charger);
43
44 return IRQ_HANDLED;
45}
46
47static inline struct gpio_charger *psy_to_gpio_charger(struct power_supply *psy)
48{
49 return container_of(psy, struct gpio_charger, charger);
50}
51
52static int gpio_charger_get_property(struct power_supply *psy,
53 enum power_supply_property psp, union power_supply_propval *val)
54{
55 struct gpio_charger *gpio_charger = psy_to_gpio_charger(psy);
56 const struct gpio_charger_platform_data *pdata = gpio_charger->pdata;
57
58 switch (psp) {
59 case POWER_SUPPLY_PROP_ONLINE:
Heiko Stuebner4d96fb12014-09-23 22:42:16 +020060 val->intval = !!gpio_get_value_cansleep(pdata->gpio);
Lars-Peter Clausen50704372010-10-21 17:55:01 +020061 val->intval ^= pdata->gpio_active_low;
62 break;
63 default:
64 return -EINVAL;
65 }
66
67 return 0;
68}
69
70static enum power_supply_property gpio_charger_properties[] = {
71 POWER_SUPPLY_PROP_ONLINE,
72};
73
Heiko Stuebnerc8dd9cc2014-09-23 22:42:15 +020074static
75struct gpio_charger_platform_data *gpio_charger_parse_dt(struct device *dev)
76{
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;
81 int ret;
82
83 if (!np)
84 return ERR_PTR(-ENOENT);
85
86 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
87 if (!pdata)
88 return ERR_PTR(-ENOMEM);
89
90 pdata->name = np->name;
91
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);
97 }
98
99 pdata->gpio_active_low = !!(flags & OF_GPIO_ACTIVE_LOW);
100
101 pdata->type = POWER_SUPPLY_TYPE_UNKNOWN;
102 ret = of_property_read_string(np, "charger-type", &chargetype);
103 if (ret >= 0) {
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;
120 else
121 dev_warn(dev, "unknown charger type %s\n", chargetype);
122 }
123
124 return pdata;
125}
126
Bill Pembertonc8afa642012-11-19 13:22:23 -0500127static int gpio_charger_probe(struct platform_device *pdev)
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200128{
129 const struct gpio_charger_platform_data *pdata = pdev->dev.platform_data;
130 struct gpio_charger *gpio_charger;
131 struct power_supply *charger;
132 int ret;
133 int irq;
134
135 if (!pdata) {
Heiko Stuebnerc8dd9cc2014-09-23 22:42:15 +0200136 pdata = gpio_charger_parse_dt(&pdev->dev);
137 if (IS_ERR(pdata)) {
138 ret = PTR_ERR(pdata);
139 if (ret != -EPROBE_DEFER)
140 dev_err(&pdev->dev, "No platform data\n");
141 return ret;
142 }
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200143 }
144
145 if (!gpio_is_valid(pdata->gpio)) {
146 dev_err(&pdev->dev, "Invalid gpio pin\n");
147 return -EINVAL;
148 }
149
Jingoo Hanb92ff852013-03-11 15:33:27 +0900150 gpio_charger = devm_kzalloc(&pdev->dev, sizeof(*gpio_charger),
151 GFP_KERNEL);
Lars-Peter Clausen2e9ff5f2010-11-18 23:08:37 +0100152 if (!gpio_charger) {
153 dev_err(&pdev->dev, "Failed to alloc driver structure\n");
154 return -ENOMEM;
155 }
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200156
157 charger = &gpio_charger->charger;
158
Lars-Peter Clausen80577b8a42010-11-18 23:08:39 +0100159 charger->name = pdata->name ? pdata->name : "gpio-charger";
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200160 charger->type = pdata->type;
161 charger->properties = gpio_charger_properties;
162 charger->num_properties = ARRAY_SIZE(gpio_charger_properties);
Lars-Peter Clausen26eb3872010-11-18 23:08:38 +0100163 charger->get_property = gpio_charger_get_property;
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200164 charger->supplied_to = pdata->supplied_to;
165 charger->num_supplicants = pdata->num_supplicants;
Heiko Stuebnerc8dd9cc2014-09-23 22:42:15 +0200166 charger->of_node = pdev->dev.of_node;
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200167
168 ret = gpio_request(pdata->gpio, dev_name(&pdev->dev));
169 if (ret) {
170 dev_err(&pdev->dev, "Failed to request gpio pin: %d\n", ret);
171 goto err_free;
172 }
173 ret = gpio_direction_input(pdata->gpio);
174 if (ret) {
175 dev_err(&pdev->dev, "Failed to set gpio to input: %d\n", ret);
176 goto err_gpio_free;
177 }
178
Lars-Peter Clausen26eb3872010-11-18 23:08:38 +0100179 gpio_charger->pdata = pdata;
180
181 ret = power_supply_register(&pdev->dev, charger);
182 if (ret < 0) {
183 dev_err(&pdev->dev, "Failed to register power supply: %d\n",
184 ret);
185 goto err_gpio_free;
186 }
187
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200188 irq = gpio_to_irq(pdata->gpio);
189 if (irq > 0) {
Lars-Peter Clausen26eb3872010-11-18 23:08:38 +0100190 ret = request_any_context_irq(irq, gpio_charger_irq,
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200191 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
192 dev_name(&pdev->dev), charger);
Axel Linf8c63912011-07-04 18:04:15 +0800193 if (ret < 0)
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200194 dev_warn(&pdev->dev, "Failed to request irq: %d\n", ret);
195 else
196 gpio_charger->irq = irq;
197 }
198
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200199 platform_set_drvdata(pdev, gpio_charger);
200
Dmitry Eremin-Solenikov4ea81262013-12-11 02:47:16 +0400201 device_init_wakeup(&pdev->dev, 1);
202
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200203 return 0;
204
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200205err_gpio_free:
206 gpio_free(pdata->gpio);
207err_free:
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200208 return ret;
209}
210
Bill Pemberton415ec692012-11-19 13:26:07 -0500211static int gpio_charger_remove(struct platform_device *pdev)
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200212{
213 struct gpio_charger *gpio_charger = platform_get_drvdata(pdev);
214
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200215 if (gpio_charger->irq)
216 free_irq(gpio_charger->irq, &gpio_charger->charger);
Lars-Peter Clausen26eb3872010-11-18 23:08:38 +0100217
218 power_supply_unregister(&gpio_charger->charger);
219
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200220 gpio_free(gpio_charger->pdata->gpio);
221
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200222 return 0;
223}
224
Benson Leungf10513d2011-04-06 16:55:20 -0700225#ifdef CONFIG_PM_SLEEP
Dmitry Eremin-Solenikov4ea81262013-12-11 02:47:16 +0400226static int gpio_charger_suspend(struct device *dev)
227{
228 struct gpio_charger *gpio_charger = dev_get_drvdata(dev);
229
230 if (device_may_wakeup(dev))
231 gpio_charger->wakeup_enabled =
232 enable_irq_wake(gpio_charger->irq);
233
234 return 0;
235}
236
Benson Leungf10513d2011-04-06 16:55:20 -0700237static int gpio_charger_resume(struct device *dev)
238{
239 struct platform_device *pdev = to_platform_device(dev);
240 struct gpio_charger *gpio_charger = platform_get_drvdata(pdev);
241
Dmitry Eremin-Solenikov4ea81262013-12-11 02:47:16 +0400242 if (gpio_charger->wakeup_enabled)
243 disable_irq_wake(gpio_charger->irq);
Benson Leungf10513d2011-04-06 16:55:20 -0700244 power_supply_changed(&gpio_charger->charger);
245
246 return 0;
247}
248#endif
249
Dmitry Eremin-Solenikov4ea81262013-12-11 02:47:16 +0400250static SIMPLE_DEV_PM_OPS(gpio_charger_pm_ops,
251 gpio_charger_suspend, gpio_charger_resume);
Benson Leungf10513d2011-04-06 16:55:20 -0700252
Heiko Stuebnerc8dd9cc2014-09-23 22:42:15 +0200253static const struct of_device_id gpio_charger_match[] = {
254 { .compatible = "gpio-charger" },
255 { }
256};
257MODULE_DEVICE_TABLE(of, gpio_charger_match);
258
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200259static struct platform_driver gpio_charger_driver = {
260 .probe = gpio_charger_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -0500261 .remove = gpio_charger_remove,
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200262 .driver = {
263 .name = "gpio-charger",
Benson Leungf10513d2011-04-06 16:55:20 -0700264 .pm = &gpio_charger_pm_ops,
Heiko Stuebnerc8dd9cc2014-09-23 22:42:15 +0200265 .of_match_table = gpio_charger_match,
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200266 },
267};
268
Axel Lin300bac72011-11-26 12:01:10 +0800269module_platform_driver(gpio_charger_driver);
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200270
271MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
272MODULE_DESCRIPTION("Driver for chargers which report their online status through a GPIO");
273MODULE_LICENSE("GPL");
274MODULE_ALIAS("platform:gpio-charger");