Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 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> |
| 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 Stuebner | c8dd9cc | 2014-09-23 22:42:15 +0200 | [diff] [blame] | 25 | #include <linux/of.h> |
| 26 | #include <linux/of_gpio.h> |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 27 | |
| 28 | #include <linux/power/gpio-charger.h> |
| 29 | |
| 30 | struct gpio_charger { |
| 31 | const struct gpio_charger_platform_data *pdata; |
| 32 | unsigned int irq; |
Dmitry Eremin-Solenikov | 4ea8126 | 2013-12-11 02:47:16 +0400 | [diff] [blame] | 33 | bool wakeup_enabled; |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 34 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 35 | struct power_supply *charger; |
| 36 | struct power_supply_desc charger_desc; |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | static 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 | |
| 48 | static inline struct gpio_charger *psy_to_gpio_charger(struct power_supply *psy) |
| 49 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 50 | return power_supply_get_drvdata(psy); |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | static 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 Stuebner | 4d96fb1 | 2014-09-23 22:42:16 +0200 | [diff] [blame] | 61 | val->intval = !!gpio_get_value_cansleep(pdata->gpio); |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 62 | val->intval ^= pdata->gpio_active_low; |
| 63 | break; |
| 64 | default: |
| 65 | return -EINVAL; |
| 66 | } |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | static enum power_supply_property gpio_charger_properties[] = { |
| 72 | POWER_SUPPLY_PROP_ONLINE, |
| 73 | }; |
| 74 | |
Heiko Stuebner | c8dd9cc | 2014-09-23 22:42:15 +0200 | [diff] [blame] | 75 | static |
| 76 | struct 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 Pemberton | c8afa64 | 2012-11-19 13:22:23 -0500 | [diff] [blame] | 128 | static int gpio_charger_probe(struct platform_device *pdev) |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 129 | { |
| 130 | const struct gpio_charger_platform_data *pdata = pdev->dev.platform_data; |
Krzysztof Kozlowski | 2dc9215 | 2015-03-12 08:44:02 +0100 | [diff] [blame] | 131 | struct power_supply_config psy_cfg = {}; |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 132 | struct gpio_charger *gpio_charger; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 133 | struct power_supply_desc *charger_desc; |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 134 | int ret; |
| 135 | int irq; |
| 136 | |
| 137 | if (!pdata) { |
Heiko Stuebner | c8dd9cc | 2014-09-23 22:42:15 +0200 | [diff] [blame] | 138 | 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 Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | if (!gpio_is_valid(pdata->gpio)) { |
| 148 | dev_err(&pdev->dev, "Invalid gpio pin\n"); |
| 149 | return -EINVAL; |
| 150 | } |
| 151 | |
Jingoo Han | b92ff85 | 2013-03-11 15:33:27 +0900 | [diff] [blame] | 152 | gpio_charger = devm_kzalloc(&pdev->dev, sizeof(*gpio_charger), |
| 153 | GFP_KERNEL); |
Lars-Peter Clausen | 2e9ff5f | 2010-11-18 23:08:37 +0100 | [diff] [blame] | 154 | if (!gpio_charger) { |
| 155 | dev_err(&pdev->dev, "Failed to alloc driver structure\n"); |
| 156 | return -ENOMEM; |
| 157 | } |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 158 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 159 | charger_desc = &gpio_charger->charger_desc; |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 160 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 161 | 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 Kozlowski | 2dc9215 | 2015-03-12 08:44:02 +0100 | [diff] [blame] | 166 | |
| 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 Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 170 | psy_cfg.drv_data = gpio_charger; |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 171 | |
| 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 Clausen | 26eb387 | 2010-11-18 23:08:38 +0100 | [diff] [blame] | 183 | gpio_charger->pdata = pdata; |
| 184 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 185 | 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 Clausen | 26eb387 | 2010-11-18 23:08:38 +0100 | [diff] [blame] | 189 | dev_err(&pdev->dev, "Failed to register power supply: %d\n", |
| 190 | ret); |
| 191 | goto err_gpio_free; |
| 192 | } |
| 193 | |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 194 | irq = gpio_to_irq(pdata->gpio); |
| 195 | if (irq > 0) { |
Lars-Peter Clausen | 26eb387 | 2010-11-18 23:08:38 +0100 | [diff] [blame] | 196 | ret = request_any_context_irq(irq, gpio_charger_irq, |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 197 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 198 | dev_name(&pdev->dev), gpio_charger->charger); |
Axel Lin | f8c6391 | 2011-07-04 18:04:15 +0800 | [diff] [blame] | 199 | if (ret < 0) |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 200 | dev_warn(&pdev->dev, "Failed to request irq: %d\n", ret); |
| 201 | else |
| 202 | gpio_charger->irq = irq; |
| 203 | } |
| 204 | |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 205 | platform_set_drvdata(pdev, gpio_charger); |
| 206 | |
Dmitry Eremin-Solenikov | 4ea8126 | 2013-12-11 02:47:16 +0400 | [diff] [blame] | 207 | device_init_wakeup(&pdev->dev, 1); |
| 208 | |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 209 | return 0; |
| 210 | |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 211 | err_gpio_free: |
| 212 | gpio_free(pdata->gpio); |
| 213 | err_free: |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 214 | return ret; |
| 215 | } |
| 216 | |
Bill Pemberton | 415ec69 | 2012-11-19 13:26:07 -0500 | [diff] [blame] | 217 | static int gpio_charger_remove(struct platform_device *pdev) |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 218 | { |
| 219 | struct gpio_charger *gpio_charger = platform_get_drvdata(pdev); |
| 220 | |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 221 | if (gpio_charger->irq) |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 222 | free_irq(gpio_charger->irq, gpio_charger->charger); |
Lars-Peter Clausen | 26eb387 | 2010-11-18 23:08:38 +0100 | [diff] [blame] | 223 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 224 | power_supply_unregister(gpio_charger->charger); |
Lars-Peter Clausen | 26eb387 | 2010-11-18 23:08:38 +0100 | [diff] [blame] | 225 | |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 226 | gpio_free(gpio_charger->pdata->gpio); |
| 227 | |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 228 | return 0; |
| 229 | } |
| 230 | |
Benson Leung | f10513d | 2011-04-06 16:55:20 -0700 | [diff] [blame] | 231 | #ifdef CONFIG_PM_SLEEP |
Dmitry Eremin-Solenikov | 4ea8126 | 2013-12-11 02:47:16 +0400 | [diff] [blame] | 232 | static 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-Solenikov | faeed51 | 2015-01-15 05:00:37 +0300 | [diff] [blame] | 238 | !enable_irq_wake(gpio_charger->irq); |
Dmitry Eremin-Solenikov | 4ea8126 | 2013-12-11 02:47:16 +0400 | [diff] [blame] | 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
Benson Leung | f10513d | 2011-04-06 16:55:20 -0700 | [diff] [blame] | 243 | static 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-Solenikov | faeed51 | 2015-01-15 05:00:37 +0300 | [diff] [blame] | 248 | if (device_may_wakeup(dev) && gpio_charger->wakeup_enabled) |
Dmitry Eremin-Solenikov | 4ea8126 | 2013-12-11 02:47:16 +0400 | [diff] [blame] | 249 | disable_irq_wake(gpio_charger->irq); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 250 | power_supply_changed(gpio_charger->charger); |
Benson Leung | f10513d | 2011-04-06 16:55:20 -0700 | [diff] [blame] | 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | #endif |
| 255 | |
Dmitry Eremin-Solenikov | 4ea8126 | 2013-12-11 02:47:16 +0400 | [diff] [blame] | 256 | static SIMPLE_DEV_PM_OPS(gpio_charger_pm_ops, |
| 257 | gpio_charger_suspend, gpio_charger_resume); |
Benson Leung | f10513d | 2011-04-06 16:55:20 -0700 | [diff] [blame] | 258 | |
Heiko Stuebner | c8dd9cc | 2014-09-23 22:42:15 +0200 | [diff] [blame] | 259 | static const struct of_device_id gpio_charger_match[] = { |
| 260 | { .compatible = "gpio-charger" }, |
| 261 | { } |
| 262 | }; |
| 263 | MODULE_DEVICE_TABLE(of, gpio_charger_match); |
| 264 | |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 265 | static struct platform_driver gpio_charger_driver = { |
| 266 | .probe = gpio_charger_probe, |
Bill Pemberton | 28ea73f | 2012-11-19 13:20:40 -0500 | [diff] [blame] | 267 | .remove = gpio_charger_remove, |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 268 | .driver = { |
| 269 | .name = "gpio-charger", |
Benson Leung | f10513d | 2011-04-06 16:55:20 -0700 | [diff] [blame] | 270 | .pm = &gpio_charger_pm_ops, |
Heiko Stuebner | c8dd9cc | 2014-09-23 22:42:15 +0200 | [diff] [blame] | 271 | .of_match_table = gpio_charger_match, |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 272 | }, |
| 273 | }; |
| 274 | |
Axel Lin | 300bac7 | 2011-11-26 12:01:10 +0800 | [diff] [blame] | 275 | module_platform_driver(gpio_charger_driver); |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 276 | |
| 277 | MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); |
| 278 | MODULE_DESCRIPTION("Driver for chargers which report their online status through a GPIO"); |
| 279 | MODULE_LICENSE("GPL"); |
| 280 | MODULE_ALIAS("platform:gpio-charger"); |