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