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 | |
| 35 | struct power_supply charger; |
| 36 | }; |
| 37 | |
| 38 | static 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 | |
| 47 | static inline struct gpio_charger *psy_to_gpio_charger(struct power_supply *psy) |
| 48 | { |
| 49 | return container_of(psy, struct gpio_charger, charger); |
| 50 | } |
| 51 | |
| 52 | static 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 Stuebner | 4d96fb1 | 2014-09-23 22:42:16 +0200 | [diff] [blame] | 60 | val->intval = !!gpio_get_value_cansleep(pdata->gpio); |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 61 | val->intval ^= pdata->gpio_active_low; |
| 62 | break; |
| 63 | default: |
| 64 | return -EINVAL; |
| 65 | } |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static enum power_supply_property gpio_charger_properties[] = { |
| 71 | POWER_SUPPLY_PROP_ONLINE, |
| 72 | }; |
| 73 | |
Heiko Stuebner | c8dd9cc | 2014-09-23 22:42:15 +0200 | [diff] [blame] | 74 | static |
| 75 | struct 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 Pemberton | c8afa64 | 2012-11-19 13:22:23 -0500 | [diff] [blame] | 127 | static int gpio_charger_probe(struct platform_device *pdev) |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 128 | { |
| 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 Stuebner | c8dd9cc | 2014-09-23 22:42:15 +0200 | [diff] [blame] | 136 | 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 Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | if (!gpio_is_valid(pdata->gpio)) { |
| 146 | dev_err(&pdev->dev, "Invalid gpio pin\n"); |
| 147 | return -EINVAL; |
| 148 | } |
| 149 | |
Jingoo Han | b92ff85 | 2013-03-11 15:33:27 +0900 | [diff] [blame] | 150 | gpio_charger = devm_kzalloc(&pdev->dev, sizeof(*gpio_charger), |
| 151 | GFP_KERNEL); |
Lars-Peter Clausen | 2e9ff5f | 2010-11-18 23:08:37 +0100 | [diff] [blame] | 152 | if (!gpio_charger) { |
| 153 | dev_err(&pdev->dev, "Failed to alloc driver structure\n"); |
| 154 | return -ENOMEM; |
| 155 | } |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 156 | |
| 157 | charger = &gpio_charger->charger; |
| 158 | |
Lars-Peter Clausen | 80577b8a4 | 2010-11-18 23:08:39 +0100 | [diff] [blame] | 159 | charger->name = pdata->name ? pdata->name : "gpio-charger"; |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 160 | charger->type = pdata->type; |
| 161 | charger->properties = gpio_charger_properties; |
| 162 | charger->num_properties = ARRAY_SIZE(gpio_charger_properties); |
Lars-Peter Clausen | 26eb387 | 2010-11-18 23:08:38 +0100 | [diff] [blame] | 163 | charger->get_property = gpio_charger_get_property; |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 164 | charger->supplied_to = pdata->supplied_to; |
| 165 | charger->num_supplicants = pdata->num_supplicants; |
Heiko Stuebner | c8dd9cc | 2014-09-23 22:42:15 +0200 | [diff] [blame] | 166 | charger->of_node = pdev->dev.of_node; |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 167 | |
| 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 Clausen | 26eb387 | 2010-11-18 23:08:38 +0100 | [diff] [blame] | 179 | 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 Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 188 | irq = gpio_to_irq(pdata->gpio); |
| 189 | if (irq > 0) { |
Lars-Peter Clausen | 26eb387 | 2010-11-18 23:08:38 +0100 | [diff] [blame] | 190 | ret = request_any_context_irq(irq, gpio_charger_irq, |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 191 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 192 | dev_name(&pdev->dev), charger); |
Axel Lin | f8c6391 | 2011-07-04 18:04:15 +0800 | [diff] [blame] | 193 | if (ret < 0) |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 194 | dev_warn(&pdev->dev, "Failed to request irq: %d\n", ret); |
| 195 | else |
| 196 | gpio_charger->irq = irq; |
| 197 | } |
| 198 | |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 199 | platform_set_drvdata(pdev, gpio_charger); |
| 200 | |
Dmitry Eremin-Solenikov | 4ea8126 | 2013-12-11 02:47:16 +0400 | [diff] [blame] | 201 | device_init_wakeup(&pdev->dev, 1); |
| 202 | |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 203 | return 0; |
| 204 | |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 205 | err_gpio_free: |
| 206 | gpio_free(pdata->gpio); |
| 207 | err_free: |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 208 | return ret; |
| 209 | } |
| 210 | |
Bill Pemberton | 415ec69 | 2012-11-19 13:26:07 -0500 | [diff] [blame] | 211 | static int gpio_charger_remove(struct platform_device *pdev) |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 212 | { |
| 213 | struct gpio_charger *gpio_charger = platform_get_drvdata(pdev); |
| 214 | |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 215 | if (gpio_charger->irq) |
| 216 | free_irq(gpio_charger->irq, &gpio_charger->charger); |
Lars-Peter Clausen | 26eb387 | 2010-11-18 23:08:38 +0100 | [diff] [blame] | 217 | |
| 218 | power_supply_unregister(&gpio_charger->charger); |
| 219 | |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 220 | gpio_free(gpio_charger->pdata->gpio); |
| 221 | |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 222 | return 0; |
| 223 | } |
| 224 | |
Benson Leung | f10513d | 2011-04-06 16:55:20 -0700 | [diff] [blame] | 225 | #ifdef CONFIG_PM_SLEEP |
Dmitry Eremin-Solenikov | 4ea8126 | 2013-12-11 02:47:16 +0400 | [diff] [blame] | 226 | static 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 = |
Dmitry Eremin-Solenikov | faeed51 | 2015-01-15 05:00:37 +0300 | [diff] [blame] | 232 | !enable_irq_wake(gpio_charger->irq); |
Dmitry Eremin-Solenikov | 4ea8126 | 2013-12-11 02:47:16 +0400 | [diff] [blame] | 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
Benson Leung | f10513d | 2011-04-06 16:55:20 -0700 | [diff] [blame] | 237 | static 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-Solenikov | faeed51 | 2015-01-15 05:00:37 +0300 | [diff] [blame] | 242 | if (device_may_wakeup(dev) && gpio_charger->wakeup_enabled) |
Dmitry Eremin-Solenikov | 4ea8126 | 2013-12-11 02:47:16 +0400 | [diff] [blame] | 243 | disable_irq_wake(gpio_charger->irq); |
Benson Leung | f10513d | 2011-04-06 16:55:20 -0700 | [diff] [blame] | 244 | power_supply_changed(&gpio_charger->charger); |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | #endif |
| 249 | |
Dmitry Eremin-Solenikov | 4ea8126 | 2013-12-11 02:47:16 +0400 | [diff] [blame] | 250 | static SIMPLE_DEV_PM_OPS(gpio_charger_pm_ops, |
| 251 | gpio_charger_suspend, gpio_charger_resume); |
Benson Leung | f10513d | 2011-04-06 16:55:20 -0700 | [diff] [blame] | 252 | |
Heiko Stuebner | c8dd9cc | 2014-09-23 22:42:15 +0200 | [diff] [blame] | 253 | static const struct of_device_id gpio_charger_match[] = { |
| 254 | { .compatible = "gpio-charger" }, |
| 255 | { } |
| 256 | }; |
| 257 | MODULE_DEVICE_TABLE(of, gpio_charger_match); |
| 258 | |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 259 | static struct platform_driver gpio_charger_driver = { |
| 260 | .probe = gpio_charger_probe, |
Bill Pemberton | 28ea73f | 2012-11-19 13:20:40 -0500 | [diff] [blame] | 261 | .remove = gpio_charger_remove, |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 262 | .driver = { |
| 263 | .name = "gpio-charger", |
Benson Leung | f10513d | 2011-04-06 16:55:20 -0700 | [diff] [blame] | 264 | .pm = &gpio_charger_pm_ops, |
Heiko Stuebner | c8dd9cc | 2014-09-23 22:42:15 +0200 | [diff] [blame] | 265 | .of_match_table = gpio_charger_match, |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 266 | }, |
| 267 | }; |
| 268 | |
Axel Lin | 300bac7 | 2011-11-26 12:01:10 +0800 | [diff] [blame] | 269 | module_platform_driver(gpio_charger_driver); |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 270 | |
| 271 | MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); |
| 272 | MODULE_DESCRIPTION("Driver for chargers which report their online status through a GPIO"); |
| 273 | MODULE_LICENSE("GPL"); |
| 274 | MODULE_ALIAS("platform:gpio-charger"); |