blob: 68212b39785beabfe5536a18fa15bc249f7b1eea [file] [log] [blame]
Thomas Gleixnera912e802019-05-27 08:55:00 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Lars-Peter Clausen50704372010-10-21 17:55:01 +02002/*
3 * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
4 * Driver for chargers which report their online status through a GPIO pin
Lars-Peter Clausen50704372010-10-21 17:55:01 +02005 */
6
7#include <linux/device.h>
Lars-Peter Clausen50704372010-10-21 17:55:01 +02008#include <linux/init.h>
9#include <linux/interrupt.h>
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/platform_device.h>
13#include <linux/power_supply.h>
14#include <linux/slab.h>
Heiko Stuebnerc8dd9cc2014-09-23 22:42:15 +020015#include <linux/of.h>
Linus Walleijfb9a33a2016-12-26 21:18:55 +010016#include <linux/gpio/consumer.h>
Lars-Peter Clausen50704372010-10-21 17:55:01 +020017
18#include <linux/power/gpio-charger.h>
19
Sebastian Reichelbe2919d2020-06-06 00:44:00 +020020struct gpio_mapping {
21 u32 limit_ua;
22 u32 gpiodata;
23} __packed;
24
Lars-Peter Clausen50704372010-10-21 17:55:01 +020025struct gpio_charger {
Sebastian Reichelbe2919d2020-06-06 00:44:00 +020026 struct device *dev;
Lars-Peter Clausen50704372010-10-21 17:55:01 +020027 unsigned int irq;
Artur Rojekdf68c442019-04-14 14:40:39 +020028 unsigned int charge_status_irq;
Dmitry Eremin-Solenikov4ea81262013-12-11 02:47:16 +040029 bool wakeup_enabled;
Lars-Peter Clausen50704372010-10-21 17:55:01 +020030
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010031 struct power_supply *charger;
32 struct power_supply_desc charger_desc;
Linus Walleijfb9a33a2016-12-26 21:18:55 +010033 struct gpio_desc *gpiod;
Artur Rojekdf68c442019-04-14 14:40:39 +020034 struct gpio_desc *charge_status;
Sebastian Reichelbe2919d2020-06-06 00:44:00 +020035
36 struct gpio_descs *current_limit_gpios;
37 struct gpio_mapping *current_limit_map;
38 u32 current_limit_map_size;
39 u32 charge_current_limit;
Lars-Peter Clausen50704372010-10-21 17:55:01 +020040};
41
42static irqreturn_t gpio_charger_irq(int irq, void *devid)
43{
44 struct power_supply *charger = devid;
45
46 power_supply_changed(charger);
47
48 return IRQ_HANDLED;
49}
50
51static inline struct gpio_charger *psy_to_gpio_charger(struct power_supply *psy)
52{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010053 return power_supply_get_drvdata(psy);
Lars-Peter Clausen50704372010-10-21 17:55:01 +020054}
55
Sebastian Reichelbe2919d2020-06-06 00:44:00 +020056static int set_charge_current_limit(struct gpio_charger *gpio_charger, int val)
57{
58 struct gpio_mapping mapping;
59 int ndescs = gpio_charger->current_limit_gpios->ndescs;
60 struct gpio_desc **gpios = gpio_charger->current_limit_gpios->desc;
61 int i;
62
63 if (!gpio_charger->current_limit_map_size)
64 return -EINVAL;
65
66 for (i = 0; i < gpio_charger->current_limit_map_size; i++) {
67 if (gpio_charger->current_limit_map[i].limit_ua <= val)
68 break;
69 }
70 mapping = gpio_charger->current_limit_map[i];
71
72 for (i = 0; i < ndescs; i++) {
73 bool val = (mapping.gpiodata >> i) & 1;
74 gpiod_set_value_cansleep(gpios[ndescs-i-1], val);
75 }
76
77 gpio_charger->charge_current_limit = mapping.limit_ua;
78
79 dev_dbg(gpio_charger->dev, "set charge current limit to %d (requested: %d)\n",
80 gpio_charger->charge_current_limit, val);
81
82 return 0;
83}
84
Lars-Peter Clausen50704372010-10-21 17:55:01 +020085static int gpio_charger_get_property(struct power_supply *psy,
86 enum power_supply_property psp, union power_supply_propval *val)
87{
88 struct gpio_charger *gpio_charger = psy_to_gpio_charger(psy);
Lars-Peter Clausen50704372010-10-21 17:55:01 +020089
90 switch (psp) {
91 case POWER_SUPPLY_PROP_ONLINE:
Linus Walleijfb9a33a2016-12-26 21:18:55 +010092 val->intval = gpiod_get_value_cansleep(gpio_charger->gpiod);
Lars-Peter Clausen50704372010-10-21 17:55:01 +020093 break;
Artur Rojekdf68c442019-04-14 14:40:39 +020094 case POWER_SUPPLY_PROP_STATUS:
95 if (gpiod_get_value_cansleep(gpio_charger->charge_status))
96 val->intval = POWER_SUPPLY_STATUS_CHARGING;
97 else
98 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
99 break;
Sebastian Reichelbe2919d2020-06-06 00:44:00 +0200100 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
101 val->intval = gpio_charger->charge_current_limit;
102 break;
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200103 default:
104 return -EINVAL;
105 }
106
107 return 0;
108}
109
Sebastian Reichelbe2919d2020-06-06 00:44:00 +0200110static int gpio_charger_set_property(struct power_supply *psy,
111 enum power_supply_property psp, const union power_supply_propval *val)
112{
113 struct gpio_charger *gpio_charger = psy_to_gpio_charger(psy);
114
115 switch (psp) {
116 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
117 return set_charge_current_limit(gpio_charger, val->intval);
118 default:
119 return -EINVAL;
120 }
121
122 return 0;
123}
124
125static int gpio_charger_property_is_writeable(struct power_supply *psy,
126 enum power_supply_property psp)
127{
128 switch (psp) {
129 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
130 return 1;
131 default:
132 break;
133 }
134
135 return 0;
136}
137
Ladislav Michld47c1e42018-03-05 19:05:55 +0100138static enum power_supply_type gpio_charger_get_type(struct device *dev)
139{
140 const char *chargetype;
141
142 if (!device_property_read_string(dev, "charger-type", &chargetype)) {
143 if (!strcmp("unknown", chargetype))
144 return POWER_SUPPLY_TYPE_UNKNOWN;
145 if (!strcmp("battery", chargetype))
146 return POWER_SUPPLY_TYPE_BATTERY;
147 if (!strcmp("ups", chargetype))
148 return POWER_SUPPLY_TYPE_UPS;
149 if (!strcmp("mains", chargetype))
150 return POWER_SUPPLY_TYPE_MAINS;
151 if (!strcmp("usb-sdp", chargetype))
152 return POWER_SUPPLY_TYPE_USB;
153 if (!strcmp("usb-dcp", chargetype))
Sebastian Reichelfb4d4942018-09-29 10:03:27 +0200154 return POWER_SUPPLY_TYPE_USB;
Ladislav Michld47c1e42018-03-05 19:05:55 +0100155 if (!strcmp("usb-cdp", chargetype))
Sebastian Reichelfb4d4942018-09-29 10:03:27 +0200156 return POWER_SUPPLY_TYPE_USB;
Ladislav Michld47c1e42018-03-05 19:05:55 +0100157 if (!strcmp("usb-aca", chargetype))
Sebastian Reichelfb4d4942018-09-29 10:03:27 +0200158 return POWER_SUPPLY_TYPE_USB;
Ladislav Michld47c1e42018-03-05 19:05:55 +0100159 }
160 dev_warn(dev, "unknown charger type %s\n", chargetype);
161
162 return POWER_SUPPLY_TYPE_UNKNOWN;
163}
164
Artur Rojekdf68c442019-04-14 14:40:39 +0200165static int gpio_charger_get_irq(struct device *dev, void *dev_id,
166 struct gpio_desc *gpio)
167{
168 int ret, irq = gpiod_to_irq(gpio);
169
170 if (irq > 0) {
171 ret = devm_request_any_context_irq(dev, irq, gpio_charger_irq,
172 IRQF_TRIGGER_RISING |
173 IRQF_TRIGGER_FALLING,
174 dev_name(dev),
175 dev_id);
176 if (ret < 0) {
177 dev_warn(dev, "Failed to request irq: %d\n", ret);
178 irq = 0;
179 }
180 }
181
182 return irq;
183}
184
Sebastian Reichelbe2919d2020-06-06 00:44:00 +0200185static int init_charge_current_limit(struct device *dev,
186 struct gpio_charger *gpio_charger)
187{
188 int i, len;
189 u32 cur_limit = U32_MAX;
190
191 gpio_charger->current_limit_gpios = devm_gpiod_get_array_optional(dev,
192 "charge-current-limit", GPIOD_OUT_LOW);
193 if (IS_ERR(gpio_charger->current_limit_gpios)) {
194 dev_err(dev, "error getting current-limit GPIOs\n");
195 return PTR_ERR(gpio_charger->current_limit_gpios);
196 }
197
198 if (!gpio_charger->current_limit_gpios)
199 return 0;
200
201 len = device_property_read_u32_array(dev, "charge-current-limit-mapping",
202 NULL, 0);
203 if (len < 0)
204 return len;
205
206 if (len == 0 || len % 2) {
207 dev_err(dev, "invalid charge-current-limit-mapping length\n");
208 return -EINVAL;
209 }
210
211 gpio_charger->current_limit_map = devm_kmalloc_array(dev,
212 len / 2, sizeof(*gpio_charger->current_limit_map), GFP_KERNEL);
213 if (!gpio_charger->current_limit_map)
214 return -ENOMEM;
215
216 gpio_charger->current_limit_map_size = len / 2;
217
218 len = device_property_read_u32_array(dev, "charge-current-limit-mapping",
219 (u32*) gpio_charger->current_limit_map, len);
220 if (len < 0)
221 return len;
222
223 for (i=0; i < gpio_charger->current_limit_map_size; i++) {
224 if (gpio_charger->current_limit_map[i].limit_ua > cur_limit) {
225 dev_err(dev, "charge-current-limit-mapping not sorted by current in descending order\n");
226 return -EINVAL;
227 }
228
229 cur_limit = gpio_charger->current_limit_map[i].limit_ua;
230 }
231
232 /* default to smallest current limitation for safety reasons */
233 len = gpio_charger->current_limit_map_size - 1;
234 set_charge_current_limit(gpio_charger,
235 gpio_charger->current_limit_map[len].limit_ua);
236
237 return 0;
238}
239
Sebastian Reichel1d85f6d2020-06-06 00:43:59 +0200240/*
241 * The entries will be overwritten by driver's probe routine depending
242 * on the available features. This list ensures, that the array is big
243 * enough for all optional features.
244 */
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200245static enum power_supply_property gpio_charger_properties[] = {
246 POWER_SUPPLY_PROP_ONLINE,
Sebastian Reichel1d85f6d2020-06-06 00:43:59 +0200247 POWER_SUPPLY_PROP_STATUS,
Sebastian Reichelbe2919d2020-06-06 00:44:00 +0200248 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200249};
250
Bill Pembertonc8afa642012-11-19 13:22:23 -0500251static int gpio_charger_probe(struct platform_device *pdev)
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200252{
Ladislav Michl416a1ae2018-01-17 21:31:49 +0100253 struct device *dev = &pdev->dev;
254 const struct gpio_charger_platform_data *pdata = dev->platform_data;
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +0100255 struct power_supply_config psy_cfg = {};
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200256 struct gpio_charger *gpio_charger;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100257 struct power_supply_desc *charger_desc;
Artur Rojekdf68c442019-04-14 14:40:39 +0200258 struct gpio_desc *charge_status;
259 int charge_status_irq;
Artur Rojekdf68c442019-04-14 14:40:39 +0200260 int ret;
Sebastian Reichel1d85f6d2020-06-06 00:43:59 +0200261 int num_props = 0;
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200262
Ladislav Michld47c1e42018-03-05 19:05:55 +0100263 if (!pdata && !dev->of_node) {
264 dev_err(dev, "No platform data\n");
265 return -ENOENT;
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200266 }
267
Ladislav Michl416a1ae2018-01-17 21:31:49 +0100268 gpio_charger = devm_kzalloc(dev, sizeof(*gpio_charger), GFP_KERNEL);
Ladislav Michld433d042018-03-05 19:04:14 +0100269 if (!gpio_charger)
Lars-Peter Clausen2e9ff5f2010-11-18 23:08:37 +0100270 return -ENOMEM;
Sebastian Reichelbe2919d2020-06-06 00:44:00 +0200271 gpio_charger->dev = dev;
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200272
Linus Walleijfb9a33a2016-12-26 21:18:55 +0100273 /*
274 * This will fetch a GPIO descriptor from device tree, ACPI or
275 * boardfile descriptor tables. It's good to try this first.
276 */
Sebastian Reichel1d85f6d2020-06-06 00:43:59 +0200277 gpio_charger->gpiod = devm_gpiod_get_optional(dev, NULL, GPIOD_IN);
Linus Walleij17529bc2020-08-27 10:48:28 +0200278 if (IS_ERR(gpio_charger->gpiod)) {
Linus Walleijfb9a33a2016-12-26 21:18:55 +0100279 /* Just try again if this happens */
Krzysztof Kozlowski52a023a2020-08-26 16:48:56 +0200280 return dev_err_probe(dev, PTR_ERR(gpio_charger->gpiod),
281 "error getting GPIO descriptor\n");
Linus Walleijfb9a33a2016-12-26 21:18:55 +0100282 }
283
Sebastian Reichel1d85f6d2020-06-06 00:43:59 +0200284 if (gpio_charger->gpiod) {
285 gpio_charger_properties[num_props] = POWER_SUPPLY_PROP_ONLINE;
286 num_props++;
287 }
288
Artur Rojekdf68c442019-04-14 14:40:39 +0200289 charge_status = devm_gpiod_get_optional(dev, "charge-status", GPIOD_IN);
Sebastian Reichel1d85f6d2020-06-06 00:43:59 +0200290 if (IS_ERR(charge_status))
291 return PTR_ERR(charge_status);
292 if (charge_status) {
293 gpio_charger->charge_status = charge_status;
294 gpio_charger_properties[num_props] = POWER_SUPPLY_PROP_STATUS;
295 num_props++;
296 }
Artur Rojekdf68c442019-04-14 14:40:39 +0200297
Sebastian Reichelbe2919d2020-06-06 00:44:00 +0200298 ret = init_charge_current_limit(dev, gpio_charger);
299 if (ret < 0)
300 return ret;
301 if (gpio_charger->current_limit_map) {
302 gpio_charger_properties[num_props] =
303 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX;
304 num_props++;
305 }
306
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100307 charger_desc = &gpio_charger->charger_desc;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100308 charger_desc->properties = gpio_charger_properties;
Sebastian Reichel1d85f6d2020-06-06 00:43:59 +0200309 charger_desc->num_properties = num_props;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100310 charger_desc->get_property = gpio_charger_get_property;
Sebastian Reichelbe2919d2020-06-06 00:44:00 +0200311 charger_desc->set_property = gpio_charger_set_property;
312 charger_desc->property_is_writeable =
313 gpio_charger_property_is_writeable;
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +0100314
Ladislav Michl416a1ae2018-01-17 21:31:49 +0100315 psy_cfg.of_node = dev->of_node;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100316 psy_cfg.drv_data = gpio_charger;
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200317
Ladislav Michld47c1e42018-03-05 19:05:55 +0100318 if (pdata) {
319 charger_desc->name = pdata->name;
320 charger_desc->type = pdata->type;
321 psy_cfg.supplied_to = pdata->supplied_to;
322 psy_cfg.num_supplicants = pdata->num_supplicants;
323 } else {
324 charger_desc->name = dev->of_node->name;
325 charger_desc->type = gpio_charger_get_type(dev);
326 }
327
328 if (!charger_desc->name)
329 charger_desc->name = pdev->name;
330
Ladislav Michl416a1ae2018-01-17 21:31:49 +0100331 gpio_charger->charger = devm_power_supply_register(dev, charger_desc,
332 &psy_cfg);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100333 if (IS_ERR(gpio_charger->charger)) {
334 ret = PTR_ERR(gpio_charger->charger);
Ladislav Michl416a1ae2018-01-17 21:31:49 +0100335 dev_err(dev, "Failed to register power supply: %d\n", ret);
Ladislav Michl419cac52018-01-17 21:31:14 +0100336 return ret;
Lars-Peter Clausen26eb3872010-11-18 23:08:38 +0100337 }
338
Artur Rojekdf68c442019-04-14 14:40:39 +0200339 gpio_charger->irq = gpio_charger_get_irq(dev, gpio_charger->charger,
340 gpio_charger->gpiod);
341
342 charge_status_irq = gpio_charger_get_irq(dev, gpio_charger->charger,
343 gpio_charger->charge_status);
344 gpio_charger->charge_status_irq = charge_status_irq;
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200345
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200346 platform_set_drvdata(pdev, gpio_charger);
347
Ladislav Michl416a1ae2018-01-17 21:31:49 +0100348 device_init_wakeup(dev, 1);
Dmitry Eremin-Solenikov4ea81262013-12-11 02:47:16 +0400349
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200350 return 0;
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200351}
352
Benson Leungf10513d2011-04-06 16:55:20 -0700353#ifdef CONFIG_PM_SLEEP
Dmitry Eremin-Solenikov4ea81262013-12-11 02:47:16 +0400354static int gpio_charger_suspend(struct device *dev)
355{
356 struct gpio_charger *gpio_charger = dev_get_drvdata(dev);
357
358 if (device_may_wakeup(dev))
359 gpio_charger->wakeup_enabled =
Dmitry Eremin-Solenikovfaeed512015-01-15 05:00:37 +0300360 !enable_irq_wake(gpio_charger->irq);
Dmitry Eremin-Solenikov4ea81262013-12-11 02:47:16 +0400361
362 return 0;
363}
364
Benson Leungf10513d2011-04-06 16:55:20 -0700365static int gpio_charger_resume(struct device *dev)
366{
Wolfram Sangbb772d02018-04-19 16:06:11 +0200367 struct gpio_charger *gpio_charger = dev_get_drvdata(dev);
Benson Leungf10513d2011-04-06 16:55:20 -0700368
Dmitry Eremin-Solenikovfaeed512015-01-15 05:00:37 +0300369 if (device_may_wakeup(dev) && gpio_charger->wakeup_enabled)
Dmitry Eremin-Solenikov4ea81262013-12-11 02:47:16 +0400370 disable_irq_wake(gpio_charger->irq);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100371 power_supply_changed(gpio_charger->charger);
Benson Leungf10513d2011-04-06 16:55:20 -0700372
373 return 0;
374}
375#endif
376
Dmitry Eremin-Solenikov4ea81262013-12-11 02:47:16 +0400377static SIMPLE_DEV_PM_OPS(gpio_charger_pm_ops,
378 gpio_charger_suspend, gpio_charger_resume);
Benson Leungf10513d2011-04-06 16:55:20 -0700379
Heiko Stuebnerc8dd9cc2014-09-23 22:42:15 +0200380static const struct of_device_id gpio_charger_match[] = {
381 { .compatible = "gpio-charger" },
382 { }
383};
384MODULE_DEVICE_TABLE(of, gpio_charger_match);
385
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200386static struct platform_driver gpio_charger_driver = {
387 .probe = gpio_charger_probe,
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200388 .driver = {
389 .name = "gpio-charger",
Benson Leungf10513d2011-04-06 16:55:20 -0700390 .pm = &gpio_charger_pm_ops,
Heiko Stuebnerc8dd9cc2014-09-23 22:42:15 +0200391 .of_match_table = gpio_charger_match,
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200392 },
393};
394
Axel Lin300bac72011-11-26 12:01:10 +0800395module_platform_driver(gpio_charger_driver);
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200396
397MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
Sebastian Reichel1d85f6d2020-06-06 00:43:59 +0200398MODULE_DESCRIPTION("Driver for chargers only communicating via GPIO(s)");
Lars-Peter Clausen50704372010-10-21 17:55:01 +0200399MODULE_LICENSE("GPL");
400MODULE_ALIAS("platform:gpio-charger");