xref: /linux/drivers/power/supply/gpio-charger.c (revision f9aa1fb9f8c0542f5f6e6e620de320995d5622ad)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
4  *  Driver for chargers which report their online status through a GPIO pin
5  */
6 
7 #include <linux/device.h>
8 #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>
15 #include <linux/of.h>
16 #include <linux/gpio/consumer.h>
17 
18 #include <linux/power/gpio-charger.h>
19 
20 struct gpio_mapping {
21 	u32 limit_ua;
22 	u32 gpiodata;
23 } __packed;
24 
25 struct gpio_charger {
26 	struct device *dev;
27 	unsigned int irq;
28 	unsigned int charge_status_irq;
29 	bool wakeup_enabled;
30 
31 	struct power_supply *charger;
32 	struct power_supply_desc charger_desc;
33 	struct gpio_desc *gpiod;
34 	struct gpio_desc *charge_status;
35 
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;
40 };
41 
42 static 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 
51 static inline struct gpio_charger *psy_to_gpio_charger(struct power_supply *psy)
52 {
53 	return power_supply_get_drvdata(psy);
54 }
55 
56 static 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 
71 	/*
72 	 * If a valid charge current limit isn't found, default to smallest
73 	 * current limitation for safety reasons.
74 	 */
75 	if (i >= gpio_charger->current_limit_map_size)
76 		i = gpio_charger->current_limit_map_size - 1;
77 
78 	mapping = gpio_charger->current_limit_map[i];
79 
80 	for (i = 0; i < ndescs; i++) {
81 		bool val = (mapping.gpiodata >> i) & 1;
82 		gpiod_set_value_cansleep(gpios[ndescs-i-1], val);
83 	}
84 
85 	gpio_charger->charge_current_limit = mapping.limit_ua;
86 
87 	dev_dbg(gpio_charger->dev, "set charge current limit to %d (requested: %d)\n",
88 		gpio_charger->charge_current_limit, val);
89 
90 	return 0;
91 }
92 
93 static int gpio_charger_get_property(struct power_supply *psy,
94 		enum power_supply_property psp, union power_supply_propval *val)
95 {
96 	struct gpio_charger *gpio_charger = psy_to_gpio_charger(psy);
97 
98 	switch (psp) {
99 	case POWER_SUPPLY_PROP_ONLINE:
100 		val->intval = gpiod_get_value_cansleep(gpio_charger->gpiod);
101 		break;
102 	case POWER_SUPPLY_PROP_STATUS:
103 		if (gpiod_get_value_cansleep(gpio_charger->charge_status))
104 			val->intval = POWER_SUPPLY_STATUS_CHARGING;
105 		else
106 			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
107 		break;
108 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
109 		val->intval = gpio_charger->charge_current_limit;
110 		break;
111 	default:
112 		return -EINVAL;
113 	}
114 
115 	return 0;
116 }
117 
118 static int gpio_charger_set_property(struct power_supply *psy,
119 	enum power_supply_property psp, const union power_supply_propval *val)
120 {
121 	struct gpio_charger *gpio_charger = psy_to_gpio_charger(psy);
122 
123 	switch (psp) {
124 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
125 		return set_charge_current_limit(gpio_charger, val->intval);
126 	default:
127 		return -EINVAL;
128 	}
129 
130 	return 0;
131 }
132 
133 static int gpio_charger_property_is_writeable(struct power_supply *psy,
134 					      enum power_supply_property psp)
135 {
136 	switch (psp) {
137 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
138 		return 1;
139 	default:
140 		break;
141 	}
142 
143 	return 0;
144 }
145 
146 static enum power_supply_type gpio_charger_get_type(struct device *dev)
147 {
148 	const char *chargetype;
149 
150 	if (!device_property_read_string(dev, "charger-type", &chargetype)) {
151 		if (!strcmp("unknown", chargetype))
152 			return POWER_SUPPLY_TYPE_UNKNOWN;
153 		if (!strcmp("battery", chargetype))
154 			return POWER_SUPPLY_TYPE_BATTERY;
155 		if (!strcmp("ups", chargetype))
156 			return POWER_SUPPLY_TYPE_UPS;
157 		if (!strcmp("mains", chargetype))
158 			return POWER_SUPPLY_TYPE_MAINS;
159 		if (!strcmp("usb-sdp", chargetype))
160 			return POWER_SUPPLY_TYPE_USB;
161 		if (!strcmp("usb-dcp", chargetype))
162 			return POWER_SUPPLY_TYPE_USB;
163 		if (!strcmp("usb-cdp", chargetype))
164 			return POWER_SUPPLY_TYPE_USB;
165 		if (!strcmp("usb-aca", chargetype))
166 			return POWER_SUPPLY_TYPE_USB;
167 	}
168 	dev_warn(dev, "unknown charger type %s\n", chargetype);
169 
170 	return POWER_SUPPLY_TYPE_UNKNOWN;
171 }
172 
173 static int gpio_charger_get_irq(struct device *dev, void *dev_id,
174 				struct gpio_desc *gpio)
175 {
176 	int ret, irq = gpiod_to_irq(gpio);
177 
178 	if (irq > 0) {
179 		ret = devm_request_any_context_irq(dev, irq, gpio_charger_irq,
180 						   IRQF_TRIGGER_RISING |
181 						   IRQF_TRIGGER_FALLING,
182 						   dev_name(dev),
183 						   dev_id);
184 		if (ret < 0) {
185 			dev_warn(dev, "Failed to request irq: %d\n", ret);
186 			irq = 0;
187 		}
188 	}
189 
190 	return irq;
191 }
192 
193 static int init_charge_current_limit(struct device *dev,
194 				    struct gpio_charger *gpio_charger)
195 {
196 	int i, len;
197 	u32 cur_limit = U32_MAX;
198 
199 	gpio_charger->current_limit_gpios = devm_gpiod_get_array_optional(dev,
200 		"charge-current-limit", GPIOD_OUT_LOW);
201 	if (IS_ERR(gpio_charger->current_limit_gpios)) {
202 		dev_err(dev, "error getting current-limit GPIOs\n");
203 		return PTR_ERR(gpio_charger->current_limit_gpios);
204 	}
205 
206 	if (!gpio_charger->current_limit_gpios)
207 		return 0;
208 
209 	len = device_property_read_u32_array(dev, "charge-current-limit-mapping",
210 		NULL, 0);
211 	if (len < 0)
212 		return len;
213 
214 	if (len == 0 || len % 2) {
215 		dev_err(dev, "invalid charge-current-limit-mapping length\n");
216 		return -EINVAL;
217 	}
218 
219 	gpio_charger->current_limit_map = devm_kmalloc_array(dev,
220 		len / 2, sizeof(*gpio_charger->current_limit_map), GFP_KERNEL);
221 	if (!gpio_charger->current_limit_map)
222 		return -ENOMEM;
223 
224 	gpio_charger->current_limit_map_size = len / 2;
225 
226 	len = device_property_read_u32_array(dev, "charge-current-limit-mapping",
227 		(u32*) gpio_charger->current_limit_map, len);
228 	if (len < 0)
229 		return len;
230 
231 	for (i=0; i < gpio_charger->current_limit_map_size; i++) {
232 		if (gpio_charger->current_limit_map[i].limit_ua > cur_limit) {
233 			dev_err(dev, "charge-current-limit-mapping not sorted by current in descending order\n");
234 			return -EINVAL;
235 		}
236 
237 		cur_limit = gpio_charger->current_limit_map[i].limit_ua;
238 	}
239 
240 	/* default to smallest current limitation for safety reasons */
241 	len = gpio_charger->current_limit_map_size - 1;
242 	set_charge_current_limit(gpio_charger,
243 		gpio_charger->current_limit_map[len].limit_ua);
244 
245 	return 0;
246 }
247 
248 /*
249  * The entries will be overwritten by driver's probe routine depending
250  * on the available features. This list ensures, that the array is big
251  * enough for all optional features.
252  */
253 static enum power_supply_property gpio_charger_properties[] = {
254 	POWER_SUPPLY_PROP_ONLINE,
255 	POWER_SUPPLY_PROP_STATUS,
256 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
257 };
258 
259 static int gpio_charger_probe(struct platform_device *pdev)
260 {
261 	struct device *dev = &pdev->dev;
262 	const struct gpio_charger_platform_data *pdata = dev->platform_data;
263 	struct power_supply_config psy_cfg = {};
264 	struct gpio_charger *gpio_charger;
265 	struct power_supply_desc *charger_desc;
266 	struct gpio_desc *charge_status;
267 	int charge_status_irq;
268 	int ret;
269 	int num_props = 0;
270 
271 	if (!pdata && !dev->of_node) {
272 		dev_err(dev, "No platform data\n");
273 		return -ENOENT;
274 	}
275 
276 	gpio_charger = devm_kzalloc(dev, sizeof(*gpio_charger), GFP_KERNEL);
277 	if (!gpio_charger)
278 		return -ENOMEM;
279 	gpio_charger->dev = dev;
280 
281 	/*
282 	 * This will fetch a GPIO descriptor from device tree, ACPI or
283 	 * boardfile descriptor tables. It's good to try this first.
284 	 */
285 	gpio_charger->gpiod = devm_gpiod_get_optional(dev, NULL, GPIOD_IN);
286 	if (IS_ERR(gpio_charger->gpiod)) {
287 		/* Just try again if this happens */
288 		return dev_err_probe(dev, PTR_ERR(gpio_charger->gpiod),
289 				     "error getting GPIO descriptor\n");
290 	}
291 
292 	if (gpio_charger->gpiod) {
293 		gpio_charger_properties[num_props] = POWER_SUPPLY_PROP_ONLINE;
294 		num_props++;
295 	}
296 
297 	charge_status = devm_gpiod_get_optional(dev, "charge-status", GPIOD_IN);
298 	if (IS_ERR(charge_status))
299 		return PTR_ERR(charge_status);
300 	if (charge_status) {
301 		gpio_charger->charge_status = charge_status;
302 		gpio_charger_properties[num_props] = POWER_SUPPLY_PROP_STATUS;
303 		num_props++;
304 	}
305 
306 	ret = init_charge_current_limit(dev, gpio_charger);
307 	if (ret < 0)
308 		return ret;
309 	if (gpio_charger->current_limit_map) {
310 		gpio_charger_properties[num_props] =
311 			POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX;
312 		num_props++;
313 	}
314 
315 	charger_desc = &gpio_charger->charger_desc;
316 	charger_desc->properties = gpio_charger_properties;
317 	charger_desc->num_properties = num_props;
318 	charger_desc->get_property = gpio_charger_get_property;
319 	charger_desc->set_property = gpio_charger_set_property;
320 	charger_desc->property_is_writeable =
321 					gpio_charger_property_is_writeable;
322 
323 	psy_cfg.of_node = dev->of_node;
324 	psy_cfg.drv_data = gpio_charger;
325 
326 	if (pdata) {
327 		charger_desc->name = pdata->name;
328 		charger_desc->type = pdata->type;
329 		psy_cfg.supplied_to = pdata->supplied_to;
330 		psy_cfg.num_supplicants = pdata->num_supplicants;
331 	} else {
332 		charger_desc->name = dev->of_node->name;
333 		charger_desc->type = gpio_charger_get_type(dev);
334 	}
335 
336 	if (!charger_desc->name)
337 		charger_desc->name = pdev->name;
338 
339 	gpio_charger->charger = devm_power_supply_register(dev, charger_desc,
340 							   &psy_cfg);
341 	if (IS_ERR(gpio_charger->charger)) {
342 		ret = PTR_ERR(gpio_charger->charger);
343 		dev_err(dev, "Failed to register power supply: %d\n", ret);
344 		return ret;
345 	}
346 
347 	gpio_charger->irq = gpio_charger_get_irq(dev, gpio_charger->charger,
348 						 gpio_charger->gpiod);
349 
350 	charge_status_irq = gpio_charger_get_irq(dev, gpio_charger->charger,
351 						 gpio_charger->charge_status);
352 	gpio_charger->charge_status_irq = charge_status_irq;
353 
354 	platform_set_drvdata(pdev, gpio_charger);
355 
356 	device_init_wakeup(dev, 1);
357 
358 	return 0;
359 }
360 
361 #ifdef CONFIG_PM_SLEEP
362 static int gpio_charger_suspend(struct device *dev)
363 {
364 	struct gpio_charger *gpio_charger = dev_get_drvdata(dev);
365 
366 	if (device_may_wakeup(dev))
367 		gpio_charger->wakeup_enabled =
368 			!enable_irq_wake(gpio_charger->irq);
369 
370 	return 0;
371 }
372 
373 static int gpio_charger_resume(struct device *dev)
374 {
375 	struct gpio_charger *gpio_charger = dev_get_drvdata(dev);
376 
377 	if (device_may_wakeup(dev) && gpio_charger->wakeup_enabled)
378 		disable_irq_wake(gpio_charger->irq);
379 	power_supply_changed(gpio_charger->charger);
380 
381 	return 0;
382 }
383 #endif
384 
385 static SIMPLE_DEV_PM_OPS(gpio_charger_pm_ops,
386 		gpio_charger_suspend, gpio_charger_resume);
387 
388 static const struct of_device_id gpio_charger_match[] = {
389 	{ .compatible = "gpio-charger" },
390 	{ }
391 };
392 MODULE_DEVICE_TABLE(of, gpio_charger_match);
393 
394 static struct platform_driver gpio_charger_driver = {
395 	.probe = gpio_charger_probe,
396 	.driver = {
397 		.name = "gpio-charger",
398 		.pm = &gpio_charger_pm_ops,
399 		.of_match_table = gpio_charger_match,
400 	},
401 };
402 
403 module_platform_driver(gpio_charger_driver);
404 
405 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
406 MODULE_DESCRIPTION("Driver for chargers only communicating via GPIO(s)");
407 MODULE_LICENSE("GPL");
408 MODULE_ALIAS("platform:gpio-charger");
409