Thomas Gleixner | a912e80 | 2019-05-27 08:55:00 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 2 | /* |
| 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 Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/device.h> |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 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> |
Heiko Stuebner | c8dd9cc | 2014-09-23 22:42:15 +0200 | [diff] [blame] | 15 | #include <linux/of.h> |
Linus Walleij | fb9a33a | 2016-12-26 21:18:55 +0100 | [diff] [blame] | 16 | #include <linux/gpio/consumer.h> |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 17 | |
| 18 | #include <linux/power/gpio-charger.h> |
| 19 | |
Sebastian Reichel | be2919d | 2020-06-06 00:44:00 +0200 | [diff] [blame] | 20 | struct gpio_mapping { |
| 21 | u32 limit_ua; |
| 22 | u32 gpiodata; |
| 23 | } __packed; |
| 24 | |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 25 | struct gpio_charger { |
Sebastian Reichel | be2919d | 2020-06-06 00:44:00 +0200 | [diff] [blame] | 26 | struct device *dev; |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 27 | unsigned int irq; |
Artur Rojek | df68c44 | 2019-04-14 14:40:39 +0200 | [diff] [blame] | 28 | unsigned int charge_status_irq; |
Dmitry Eremin-Solenikov | 4ea8126 | 2013-12-11 02:47:16 +0400 | [diff] [blame] | 29 | bool wakeup_enabled; |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 30 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 31 | struct power_supply *charger; |
| 32 | struct power_supply_desc charger_desc; |
Linus Walleij | fb9a33a | 2016-12-26 21:18:55 +0100 | [diff] [blame] | 33 | struct gpio_desc *gpiod; |
Artur Rojek | df68c44 | 2019-04-14 14:40:39 +0200 | [diff] [blame] | 34 | struct gpio_desc *charge_status; |
Sebastian Reichel | be2919d | 2020-06-06 00:44:00 +0200 | [diff] [blame] | 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; |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 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 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 53 | return power_supply_get_drvdata(psy); |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 54 | } |
| 55 | |
Sebastian Reichel | be2919d | 2020-06-06 00:44:00 +0200 | [diff] [blame] | 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 | 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 Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 85 | static 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 Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 89 | |
| 90 | switch (psp) { |
| 91 | case POWER_SUPPLY_PROP_ONLINE: |
Linus Walleij | fb9a33a | 2016-12-26 21:18:55 +0100 | [diff] [blame] | 92 | val->intval = gpiod_get_value_cansleep(gpio_charger->gpiod); |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 93 | break; |
Artur Rojek | df68c44 | 2019-04-14 14:40:39 +0200 | [diff] [blame] | 94 | 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 Reichel | be2919d | 2020-06-06 00:44:00 +0200 | [diff] [blame] | 100 | case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: |
| 101 | val->intval = gpio_charger->charge_current_limit; |
| 102 | break; |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 103 | default: |
| 104 | return -EINVAL; |
| 105 | } |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
Sebastian Reichel | be2919d | 2020-06-06 00:44:00 +0200 | [diff] [blame] | 110 | static 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 | |
| 125 | static 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 Michl | d47c1e4 | 2018-03-05 19:05:55 +0100 | [diff] [blame] | 138 | static 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 Reichel | fb4d494 | 2018-09-29 10:03:27 +0200 | [diff] [blame] | 154 | return POWER_SUPPLY_TYPE_USB; |
Ladislav Michl | d47c1e4 | 2018-03-05 19:05:55 +0100 | [diff] [blame] | 155 | if (!strcmp("usb-cdp", chargetype)) |
Sebastian Reichel | fb4d494 | 2018-09-29 10:03:27 +0200 | [diff] [blame] | 156 | return POWER_SUPPLY_TYPE_USB; |
Ladislav Michl | d47c1e4 | 2018-03-05 19:05:55 +0100 | [diff] [blame] | 157 | if (!strcmp("usb-aca", chargetype)) |
Sebastian Reichel | fb4d494 | 2018-09-29 10:03:27 +0200 | [diff] [blame] | 158 | return POWER_SUPPLY_TYPE_USB; |
Ladislav Michl | d47c1e4 | 2018-03-05 19:05:55 +0100 | [diff] [blame] | 159 | } |
| 160 | dev_warn(dev, "unknown charger type %s\n", chargetype); |
| 161 | |
| 162 | return POWER_SUPPLY_TYPE_UNKNOWN; |
| 163 | } |
| 164 | |
Artur Rojek | df68c44 | 2019-04-14 14:40:39 +0200 | [diff] [blame] | 165 | static 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 Reichel | be2919d | 2020-06-06 00:44:00 +0200 | [diff] [blame] | 185 | static 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 Reichel | 1d85f6d | 2020-06-06 00:43:59 +0200 | [diff] [blame] | 240 | /* |
| 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 Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 245 | static enum power_supply_property gpio_charger_properties[] = { |
| 246 | POWER_SUPPLY_PROP_ONLINE, |
Sebastian Reichel | 1d85f6d | 2020-06-06 00:43:59 +0200 | [diff] [blame] | 247 | POWER_SUPPLY_PROP_STATUS, |
Sebastian Reichel | be2919d | 2020-06-06 00:44:00 +0200 | [diff] [blame] | 248 | POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 249 | }; |
| 250 | |
Bill Pemberton | c8afa64 | 2012-11-19 13:22:23 -0500 | [diff] [blame] | 251 | static int gpio_charger_probe(struct platform_device *pdev) |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 252 | { |
Ladislav Michl | 416a1ae | 2018-01-17 21:31:49 +0100 | [diff] [blame] | 253 | struct device *dev = &pdev->dev; |
| 254 | const struct gpio_charger_platform_data *pdata = dev->platform_data; |
Krzysztof Kozlowski | 2dc9215 | 2015-03-12 08:44:02 +0100 | [diff] [blame] | 255 | struct power_supply_config psy_cfg = {}; |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 256 | struct gpio_charger *gpio_charger; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 257 | struct power_supply_desc *charger_desc; |
Artur Rojek | df68c44 | 2019-04-14 14:40:39 +0200 | [diff] [blame] | 258 | struct gpio_desc *charge_status; |
| 259 | int charge_status_irq; |
Artur Rojek | df68c44 | 2019-04-14 14:40:39 +0200 | [diff] [blame] | 260 | int ret; |
Sebastian Reichel | 1d85f6d | 2020-06-06 00:43:59 +0200 | [diff] [blame] | 261 | int num_props = 0; |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 262 | |
Ladislav Michl | d47c1e4 | 2018-03-05 19:05:55 +0100 | [diff] [blame] | 263 | if (!pdata && !dev->of_node) { |
| 264 | dev_err(dev, "No platform data\n"); |
| 265 | return -ENOENT; |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 266 | } |
| 267 | |
Ladislav Michl | 416a1ae | 2018-01-17 21:31:49 +0100 | [diff] [blame] | 268 | gpio_charger = devm_kzalloc(dev, sizeof(*gpio_charger), GFP_KERNEL); |
Ladislav Michl | d433d04 | 2018-03-05 19:04:14 +0100 | [diff] [blame] | 269 | if (!gpio_charger) |
Lars-Peter Clausen | 2e9ff5f | 2010-11-18 23:08:37 +0100 | [diff] [blame] | 270 | return -ENOMEM; |
Sebastian Reichel | be2919d | 2020-06-06 00:44:00 +0200 | [diff] [blame] | 271 | gpio_charger->dev = dev; |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 272 | |
Linus Walleij | fb9a33a | 2016-12-26 21:18:55 +0100 | [diff] [blame] | 273 | /* |
| 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 Reichel | 1d85f6d | 2020-06-06 00:43:59 +0200 | [diff] [blame] | 277 | gpio_charger->gpiod = devm_gpiod_get_optional(dev, NULL, GPIOD_IN); |
Linus Walleij | 17529bc | 2020-08-27 10:48:28 +0200 | [diff] [blame] | 278 | if (IS_ERR(gpio_charger->gpiod)) { |
Linus Walleij | fb9a33a | 2016-12-26 21:18:55 +0100 | [diff] [blame] | 279 | /* Just try again if this happens */ |
Krzysztof Kozlowski | 52a023a | 2020-08-26 16:48:56 +0200 | [diff] [blame] | 280 | return dev_err_probe(dev, PTR_ERR(gpio_charger->gpiod), |
| 281 | "error getting GPIO descriptor\n"); |
Linus Walleij | fb9a33a | 2016-12-26 21:18:55 +0100 | [diff] [blame] | 282 | } |
| 283 | |
Sebastian Reichel | 1d85f6d | 2020-06-06 00:43:59 +0200 | [diff] [blame] | 284 | if (gpio_charger->gpiod) { |
| 285 | gpio_charger_properties[num_props] = POWER_SUPPLY_PROP_ONLINE; |
| 286 | num_props++; |
| 287 | } |
| 288 | |
Artur Rojek | df68c44 | 2019-04-14 14:40:39 +0200 | [diff] [blame] | 289 | charge_status = devm_gpiod_get_optional(dev, "charge-status", GPIOD_IN); |
Sebastian Reichel | 1d85f6d | 2020-06-06 00:43:59 +0200 | [diff] [blame] | 290 | 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 Rojek | df68c44 | 2019-04-14 14:40:39 +0200 | [diff] [blame] | 297 | |
Sebastian Reichel | be2919d | 2020-06-06 00:44:00 +0200 | [diff] [blame] | 298 | 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 Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 307 | charger_desc = &gpio_charger->charger_desc; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 308 | charger_desc->properties = gpio_charger_properties; |
Sebastian Reichel | 1d85f6d | 2020-06-06 00:43:59 +0200 | [diff] [blame] | 309 | charger_desc->num_properties = num_props; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 310 | charger_desc->get_property = gpio_charger_get_property; |
Sebastian Reichel | be2919d | 2020-06-06 00:44:00 +0200 | [diff] [blame] | 311 | charger_desc->set_property = gpio_charger_set_property; |
| 312 | charger_desc->property_is_writeable = |
| 313 | gpio_charger_property_is_writeable; |
Krzysztof Kozlowski | 2dc9215 | 2015-03-12 08:44:02 +0100 | [diff] [blame] | 314 | |
Ladislav Michl | 416a1ae | 2018-01-17 21:31:49 +0100 | [diff] [blame] | 315 | psy_cfg.of_node = dev->of_node; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 316 | psy_cfg.drv_data = gpio_charger; |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 317 | |
Ladislav Michl | d47c1e4 | 2018-03-05 19:05:55 +0100 | [diff] [blame] | 318 | 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 Michl | 416a1ae | 2018-01-17 21:31:49 +0100 | [diff] [blame] | 331 | gpio_charger->charger = devm_power_supply_register(dev, charger_desc, |
| 332 | &psy_cfg); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 333 | if (IS_ERR(gpio_charger->charger)) { |
| 334 | ret = PTR_ERR(gpio_charger->charger); |
Ladislav Michl | 416a1ae | 2018-01-17 21:31:49 +0100 | [diff] [blame] | 335 | dev_err(dev, "Failed to register power supply: %d\n", ret); |
Ladislav Michl | 419cac5 | 2018-01-17 21:31:14 +0100 | [diff] [blame] | 336 | return ret; |
Lars-Peter Clausen | 26eb387 | 2010-11-18 23:08:38 +0100 | [diff] [blame] | 337 | } |
| 338 | |
Artur Rojek | df68c44 | 2019-04-14 14:40:39 +0200 | [diff] [blame] | 339 | 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 Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 345 | |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 346 | platform_set_drvdata(pdev, gpio_charger); |
| 347 | |
Ladislav Michl | 416a1ae | 2018-01-17 21:31:49 +0100 | [diff] [blame] | 348 | device_init_wakeup(dev, 1); |
Dmitry Eremin-Solenikov | 4ea8126 | 2013-12-11 02:47:16 +0400 | [diff] [blame] | 349 | |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 350 | return 0; |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 351 | } |
| 352 | |
Benson Leung | f10513d | 2011-04-06 16:55:20 -0700 | [diff] [blame] | 353 | #ifdef CONFIG_PM_SLEEP |
Dmitry Eremin-Solenikov | 4ea8126 | 2013-12-11 02:47:16 +0400 | [diff] [blame] | 354 | static 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-Solenikov | faeed51 | 2015-01-15 05:00:37 +0300 | [diff] [blame] | 360 | !enable_irq_wake(gpio_charger->irq); |
Dmitry Eremin-Solenikov | 4ea8126 | 2013-12-11 02:47:16 +0400 | [diff] [blame] | 361 | |
| 362 | return 0; |
| 363 | } |
| 364 | |
Benson Leung | f10513d | 2011-04-06 16:55:20 -0700 | [diff] [blame] | 365 | static int gpio_charger_resume(struct device *dev) |
| 366 | { |
Wolfram Sang | bb772d0 | 2018-04-19 16:06:11 +0200 | [diff] [blame] | 367 | struct gpio_charger *gpio_charger = dev_get_drvdata(dev); |
Benson Leung | f10513d | 2011-04-06 16:55:20 -0700 | [diff] [blame] | 368 | |
Dmitry Eremin-Solenikov | faeed51 | 2015-01-15 05:00:37 +0300 | [diff] [blame] | 369 | if (device_may_wakeup(dev) && gpio_charger->wakeup_enabled) |
Dmitry Eremin-Solenikov | 4ea8126 | 2013-12-11 02:47:16 +0400 | [diff] [blame] | 370 | disable_irq_wake(gpio_charger->irq); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 371 | power_supply_changed(gpio_charger->charger); |
Benson Leung | f10513d | 2011-04-06 16:55:20 -0700 | [diff] [blame] | 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | #endif |
| 376 | |
Dmitry Eremin-Solenikov | 4ea8126 | 2013-12-11 02:47:16 +0400 | [diff] [blame] | 377 | static SIMPLE_DEV_PM_OPS(gpio_charger_pm_ops, |
| 378 | gpio_charger_suspend, gpio_charger_resume); |
Benson Leung | f10513d | 2011-04-06 16:55:20 -0700 | [diff] [blame] | 379 | |
Heiko Stuebner | c8dd9cc | 2014-09-23 22:42:15 +0200 | [diff] [blame] | 380 | static const struct of_device_id gpio_charger_match[] = { |
| 381 | { .compatible = "gpio-charger" }, |
| 382 | { } |
| 383 | }; |
| 384 | MODULE_DEVICE_TABLE(of, gpio_charger_match); |
| 385 | |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 386 | static struct platform_driver gpio_charger_driver = { |
| 387 | .probe = gpio_charger_probe, |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 388 | .driver = { |
| 389 | .name = "gpio-charger", |
Benson Leung | f10513d | 2011-04-06 16:55:20 -0700 | [diff] [blame] | 390 | .pm = &gpio_charger_pm_ops, |
Heiko Stuebner | c8dd9cc | 2014-09-23 22:42:15 +0200 | [diff] [blame] | 391 | .of_match_table = gpio_charger_match, |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 392 | }, |
| 393 | }; |
| 394 | |
Axel Lin | 300bac7 | 2011-11-26 12:01:10 +0800 | [diff] [blame] | 395 | module_platform_driver(gpio_charger_driver); |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 396 | |
| 397 | MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); |
Sebastian Reichel | 1d85f6d | 2020-06-06 00:43:59 +0200 | [diff] [blame] | 398 | MODULE_DESCRIPTION("Driver for chargers only communicating via GPIO(s)"); |
Lars-Peter Clausen | 5070437 | 2010-10-21 17:55:01 +0200 | [diff] [blame] | 399 | MODULE_LICENSE("GPL"); |
| 400 | MODULE_ALIAS("platform:gpio-charger"); |