1 /*
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> /* For legacy platform data */
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>
25 #include <linux/of.h>
26 #include <linux/gpio/consumer.h>
27 
28 #include <linux/power/gpio-charger.h>
29 
30 struct gpio_charger {
31 	unsigned int irq;
32 	unsigned int charge_status_irq;
33 	bool wakeup_enabled;
34 
35 	struct power_supply *charger;
36 	struct power_supply_desc charger_desc;
37 	struct gpio_desc *gpiod;
38 	struct gpio_desc *charge_status;
39 };
40 
41 static irqreturn_t gpio_charger_irq(int irq, void *devid)
42 {
43 	struct power_supply *charger = devid;
44 
45 	power_supply_changed(charger);
46 
47 	return IRQ_HANDLED;
48 }
49 
50 static inline struct gpio_charger *psy_to_gpio_charger(struct power_supply *psy)
51 {
52 	return power_supply_get_drvdata(psy);
53 }
54 
55 static int gpio_charger_get_property(struct power_supply *psy,
56 		enum power_supply_property psp, union power_supply_propval *val)
57 {
58 	struct gpio_charger *gpio_charger = psy_to_gpio_charger(psy);
59 
60 	switch (psp) {
61 	case POWER_SUPPLY_PROP_ONLINE:
62 		val->intval = gpiod_get_value_cansleep(gpio_charger->gpiod);
63 		break;
64 	case POWER_SUPPLY_PROP_STATUS:
65 		if (gpiod_get_value_cansleep(gpio_charger->charge_status))
66 			val->intval = POWER_SUPPLY_STATUS_CHARGING;
67 		else
68 			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
69 		break;
70 	default:
71 		return -EINVAL;
72 	}
73 
74 	return 0;
75 }
76 
77 static enum power_supply_type gpio_charger_get_type(struct device *dev)
78 {
79 	const char *chargetype;
80 
81 	if (!device_property_read_string(dev, "charger-type", &chargetype)) {
82 		if (!strcmp("unknown", chargetype))
83 			return POWER_SUPPLY_TYPE_UNKNOWN;
84 		if (!strcmp("battery", chargetype))
85 			return POWER_SUPPLY_TYPE_BATTERY;
86 		if (!strcmp("ups", chargetype))
87 			return POWER_SUPPLY_TYPE_UPS;
88 		if (!strcmp("mains", chargetype))
89 			return POWER_SUPPLY_TYPE_MAINS;
90 		if (!strcmp("usb-sdp", chargetype))
91 			return POWER_SUPPLY_TYPE_USB;
92 		if (!strcmp("usb-dcp", chargetype))
93 			return POWER_SUPPLY_TYPE_USB;
94 		if (!strcmp("usb-cdp", chargetype))
95 			return POWER_SUPPLY_TYPE_USB;
96 		if (!strcmp("usb-aca", chargetype))
97 			return POWER_SUPPLY_TYPE_USB;
98 	}
99 	dev_warn(dev, "unknown charger type %s\n", chargetype);
100 
101 	return POWER_SUPPLY_TYPE_UNKNOWN;
102 }
103 
104 static int gpio_charger_get_irq(struct device *dev, void *dev_id,
105 				struct gpio_desc *gpio)
106 {
107 	int ret, irq = gpiod_to_irq(gpio);
108 
109 	if (irq > 0) {
110 		ret = devm_request_any_context_irq(dev, irq, gpio_charger_irq,
111 						   IRQF_TRIGGER_RISING |
112 						   IRQF_TRIGGER_FALLING,
113 						   dev_name(dev),
114 						   dev_id);
115 		if (ret < 0) {
116 			dev_warn(dev, "Failed to request irq: %d\n", ret);
117 			irq = 0;
118 		}
119 	}
120 
121 	return irq;
122 }
123 
124 static enum power_supply_property gpio_charger_properties[] = {
125 	POWER_SUPPLY_PROP_ONLINE,
126 	POWER_SUPPLY_PROP_STATUS /* Must always be last in the array. */
127 };
128 
129 static int gpio_charger_probe(struct platform_device *pdev)
130 {
131 	struct device *dev = &pdev->dev;
132 	const struct gpio_charger_platform_data *pdata = dev->platform_data;
133 	struct power_supply_config psy_cfg = {};
134 	struct gpio_charger *gpio_charger;
135 	struct power_supply_desc *charger_desc;
136 	struct gpio_desc *charge_status;
137 	int charge_status_irq;
138 	unsigned long flags;
139 	int ret;
140 
141 	if (!pdata && !dev->of_node) {
142 		dev_err(dev, "No platform data\n");
143 		return -ENOENT;
144 	}
145 
146 	gpio_charger = devm_kzalloc(dev, sizeof(*gpio_charger), GFP_KERNEL);
147 	if (!gpio_charger)
148 		return -ENOMEM;
149 
150 	/*
151 	 * This will fetch a GPIO descriptor from device tree, ACPI or
152 	 * boardfile descriptor tables. It's good to try this first.
153 	 */
154 	gpio_charger->gpiod = devm_gpiod_get(dev, NULL, GPIOD_IN);
155 
156 	/*
157 	 * If this fails and we're not using device tree, try the
158 	 * legacy platform data method.
159 	 */
160 	if (IS_ERR(gpio_charger->gpiod) && !dev->of_node) {
161 		/* Non-DT: use legacy GPIO numbers */
162 		if (!gpio_is_valid(pdata->gpio)) {
163 			dev_err(dev, "Invalid gpio pin in pdata\n");
164 			return -EINVAL;
165 		}
166 		flags = GPIOF_IN;
167 		if (pdata->gpio_active_low)
168 			flags |= GPIOF_ACTIVE_LOW;
169 		ret = devm_gpio_request_one(dev, pdata->gpio, flags,
170 					    dev_name(dev));
171 		if (ret) {
172 			dev_err(dev, "Failed to request gpio pin: %d\n", ret);
173 			return ret;
174 		}
175 		/* Then convert this to gpiod for now */
176 		gpio_charger->gpiod = gpio_to_desc(pdata->gpio);
177 	} else if (IS_ERR(gpio_charger->gpiod)) {
178 		/* Just try again if this happens */
179 		if (PTR_ERR(gpio_charger->gpiod) == -EPROBE_DEFER)
180 			return -EPROBE_DEFER;
181 		dev_err(dev, "error getting GPIO descriptor\n");
182 		return PTR_ERR(gpio_charger->gpiod);
183 	}
184 
185 	charge_status = devm_gpiod_get_optional(dev, "charge-status", GPIOD_IN);
186 	gpio_charger->charge_status = charge_status;
187 	if (IS_ERR(gpio_charger->charge_status))
188 		return PTR_ERR(gpio_charger->charge_status);
189 
190 	charger_desc = &gpio_charger->charger_desc;
191 	charger_desc->properties = gpio_charger_properties;
192 	charger_desc->num_properties = ARRAY_SIZE(gpio_charger_properties);
193 	/* Remove POWER_SUPPLY_PROP_STATUS from the supported properties. */
194 	if (!gpio_charger->charge_status)
195 		charger_desc->num_properties -= 1;
196 	charger_desc->get_property = gpio_charger_get_property;
197 
198 	psy_cfg.of_node = dev->of_node;
199 	psy_cfg.drv_data = gpio_charger;
200 
201 	if (pdata) {
202 		charger_desc->name = pdata->name;
203 		charger_desc->type = pdata->type;
204 		psy_cfg.supplied_to = pdata->supplied_to;
205 		psy_cfg.num_supplicants = pdata->num_supplicants;
206 	} else {
207 		charger_desc->name = dev->of_node->name;
208 		charger_desc->type = gpio_charger_get_type(dev);
209 	}
210 
211 	if (!charger_desc->name)
212 		charger_desc->name = pdev->name;
213 
214 	gpio_charger->charger = devm_power_supply_register(dev, charger_desc,
215 							   &psy_cfg);
216 	if (IS_ERR(gpio_charger->charger)) {
217 		ret = PTR_ERR(gpio_charger->charger);
218 		dev_err(dev, "Failed to register power supply: %d\n", ret);
219 		return ret;
220 	}
221 
222 	gpio_charger->irq = gpio_charger_get_irq(dev, gpio_charger->charger,
223 						 gpio_charger->gpiod);
224 
225 	charge_status_irq = gpio_charger_get_irq(dev, gpio_charger->charger,
226 						 gpio_charger->charge_status);
227 	gpio_charger->charge_status_irq = charge_status_irq;
228 
229 	platform_set_drvdata(pdev, gpio_charger);
230 
231 	device_init_wakeup(dev, 1);
232 
233 	return 0;
234 }
235 
236 #ifdef CONFIG_PM_SLEEP
237 static int gpio_charger_suspend(struct device *dev)
238 {
239 	struct gpio_charger *gpio_charger = dev_get_drvdata(dev);
240 
241 	if (device_may_wakeup(dev))
242 		gpio_charger->wakeup_enabled =
243 			!enable_irq_wake(gpio_charger->irq);
244 
245 	return 0;
246 }
247 
248 static int gpio_charger_resume(struct device *dev)
249 {
250 	struct gpio_charger *gpio_charger = dev_get_drvdata(dev);
251 
252 	if (device_may_wakeup(dev) && gpio_charger->wakeup_enabled)
253 		disable_irq_wake(gpio_charger->irq);
254 	power_supply_changed(gpio_charger->charger);
255 
256 	return 0;
257 }
258 #endif
259 
260 static SIMPLE_DEV_PM_OPS(gpio_charger_pm_ops,
261 		gpio_charger_suspend, gpio_charger_resume);
262 
263 static const struct of_device_id gpio_charger_match[] = {
264 	{ .compatible = "gpio-charger" },
265 	{ }
266 };
267 MODULE_DEVICE_TABLE(of, gpio_charger_match);
268 
269 static struct platform_driver gpio_charger_driver = {
270 	.probe = gpio_charger_probe,
271 	.driver = {
272 		.name = "gpio-charger",
273 		.pm = &gpio_charger_pm_ops,
274 		.of_match_table = gpio_charger_match,
275 	},
276 };
277 
278 module_platform_driver(gpio_charger_driver);
279 
280 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
281 MODULE_DESCRIPTION("Driver for chargers which report their online status through a GPIO");
282 MODULE_LICENSE("GPL");
283 MODULE_ALIAS("platform:gpio-charger");
284