18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de> 48c2ecf20Sopenharmony_ci * Driver for chargers which report their online status through a GPIO pin 58c2ecf20Sopenharmony_ci */ 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci#include <linux/device.h> 88c2ecf20Sopenharmony_ci#include <linux/init.h> 98c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 108c2ecf20Sopenharmony_ci#include <linux/kernel.h> 118c2ecf20Sopenharmony_ci#include <linux/module.h> 128c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 138c2ecf20Sopenharmony_ci#include <linux/power_supply.h> 148c2ecf20Sopenharmony_ci#include <linux/slab.h> 158c2ecf20Sopenharmony_ci#include <linux/of.h> 168c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h> 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci#include <linux/power/gpio-charger.h> 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_cistruct gpio_mapping { 218c2ecf20Sopenharmony_ci u32 limit_ua; 228c2ecf20Sopenharmony_ci u32 gpiodata; 238c2ecf20Sopenharmony_ci} __packed; 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_cistruct gpio_charger { 268c2ecf20Sopenharmony_ci struct device *dev; 278c2ecf20Sopenharmony_ci unsigned int irq; 288c2ecf20Sopenharmony_ci unsigned int charge_status_irq; 298c2ecf20Sopenharmony_ci bool wakeup_enabled; 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci struct power_supply *charger; 328c2ecf20Sopenharmony_ci struct power_supply_desc charger_desc; 338c2ecf20Sopenharmony_ci struct gpio_desc *gpiod; 348c2ecf20Sopenharmony_ci struct gpio_desc *charge_status; 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci struct gpio_descs *current_limit_gpios; 378c2ecf20Sopenharmony_ci struct gpio_mapping *current_limit_map; 388c2ecf20Sopenharmony_ci u32 current_limit_map_size; 398c2ecf20Sopenharmony_ci u32 charge_current_limit; 408c2ecf20Sopenharmony_ci}; 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_cistatic irqreturn_t gpio_charger_irq(int irq, void *devid) 438c2ecf20Sopenharmony_ci{ 448c2ecf20Sopenharmony_ci struct power_supply *charger = devid; 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci power_supply_changed(charger); 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci return IRQ_HANDLED; 498c2ecf20Sopenharmony_ci} 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_cistatic inline struct gpio_charger *psy_to_gpio_charger(struct power_supply *psy) 528c2ecf20Sopenharmony_ci{ 538c2ecf20Sopenharmony_ci return power_supply_get_drvdata(psy); 548c2ecf20Sopenharmony_ci} 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_cistatic int set_charge_current_limit(struct gpio_charger *gpio_charger, int val) 578c2ecf20Sopenharmony_ci{ 588c2ecf20Sopenharmony_ci struct gpio_mapping mapping; 598c2ecf20Sopenharmony_ci int ndescs = gpio_charger->current_limit_gpios->ndescs; 608c2ecf20Sopenharmony_ci struct gpio_desc **gpios = gpio_charger->current_limit_gpios->desc; 618c2ecf20Sopenharmony_ci int i; 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci if (!gpio_charger->current_limit_map_size) 648c2ecf20Sopenharmony_ci return -EINVAL; 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci for (i = 0; i < gpio_charger->current_limit_map_size; i++) { 678c2ecf20Sopenharmony_ci if (gpio_charger->current_limit_map[i].limit_ua <= val) 688c2ecf20Sopenharmony_ci break; 698c2ecf20Sopenharmony_ci } 708c2ecf20Sopenharmony_ci mapping = gpio_charger->current_limit_map[i]; 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci for (i = 0; i < ndescs; i++) { 738c2ecf20Sopenharmony_ci bool val = (mapping.gpiodata >> i) & 1; 748c2ecf20Sopenharmony_ci gpiod_set_value_cansleep(gpios[ndescs-i-1], val); 758c2ecf20Sopenharmony_ci } 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci gpio_charger->charge_current_limit = mapping.limit_ua; 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci dev_dbg(gpio_charger->dev, "set charge current limit to %d (requested: %d)\n", 808c2ecf20Sopenharmony_ci gpio_charger->charge_current_limit, val); 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci return 0; 838c2ecf20Sopenharmony_ci} 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_cistatic int gpio_charger_get_property(struct power_supply *psy, 868c2ecf20Sopenharmony_ci enum power_supply_property psp, union power_supply_propval *val) 878c2ecf20Sopenharmony_ci{ 888c2ecf20Sopenharmony_ci struct gpio_charger *gpio_charger = psy_to_gpio_charger(psy); 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci switch (psp) { 918c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_ONLINE: 928c2ecf20Sopenharmony_ci val->intval = gpiod_get_value_cansleep(gpio_charger->gpiod); 938c2ecf20Sopenharmony_ci break; 948c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_STATUS: 958c2ecf20Sopenharmony_ci if (gpiod_get_value_cansleep(gpio_charger->charge_status)) 968c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_STATUS_CHARGING; 978c2ecf20Sopenharmony_ci else 988c2ecf20Sopenharmony_ci val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; 998c2ecf20Sopenharmony_ci break; 1008c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: 1018c2ecf20Sopenharmony_ci val->intval = gpio_charger->charge_current_limit; 1028c2ecf20Sopenharmony_ci break; 1038c2ecf20Sopenharmony_ci default: 1048c2ecf20Sopenharmony_ci return -EINVAL; 1058c2ecf20Sopenharmony_ci } 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci return 0; 1088c2ecf20Sopenharmony_ci} 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_cistatic int gpio_charger_set_property(struct power_supply *psy, 1118c2ecf20Sopenharmony_ci enum power_supply_property psp, const union power_supply_propval *val) 1128c2ecf20Sopenharmony_ci{ 1138c2ecf20Sopenharmony_ci struct gpio_charger *gpio_charger = psy_to_gpio_charger(psy); 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci switch (psp) { 1168c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: 1178c2ecf20Sopenharmony_ci return set_charge_current_limit(gpio_charger, val->intval); 1188c2ecf20Sopenharmony_ci default: 1198c2ecf20Sopenharmony_ci return -EINVAL; 1208c2ecf20Sopenharmony_ci } 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci return 0; 1238c2ecf20Sopenharmony_ci} 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_cistatic int gpio_charger_property_is_writeable(struct power_supply *psy, 1268c2ecf20Sopenharmony_ci enum power_supply_property psp) 1278c2ecf20Sopenharmony_ci{ 1288c2ecf20Sopenharmony_ci switch (psp) { 1298c2ecf20Sopenharmony_ci case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: 1308c2ecf20Sopenharmony_ci return 1; 1318c2ecf20Sopenharmony_ci default: 1328c2ecf20Sopenharmony_ci break; 1338c2ecf20Sopenharmony_ci } 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci return 0; 1368c2ecf20Sopenharmony_ci} 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_cistatic enum power_supply_type gpio_charger_get_type(struct device *dev) 1398c2ecf20Sopenharmony_ci{ 1408c2ecf20Sopenharmony_ci const char *chargetype; 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci if (!device_property_read_string(dev, "charger-type", &chargetype)) { 1438c2ecf20Sopenharmony_ci if (!strcmp("unknown", chargetype)) 1448c2ecf20Sopenharmony_ci return POWER_SUPPLY_TYPE_UNKNOWN; 1458c2ecf20Sopenharmony_ci if (!strcmp("battery", chargetype)) 1468c2ecf20Sopenharmony_ci return POWER_SUPPLY_TYPE_BATTERY; 1478c2ecf20Sopenharmony_ci if (!strcmp("ups", chargetype)) 1488c2ecf20Sopenharmony_ci return POWER_SUPPLY_TYPE_UPS; 1498c2ecf20Sopenharmony_ci if (!strcmp("mains", chargetype)) 1508c2ecf20Sopenharmony_ci return POWER_SUPPLY_TYPE_MAINS; 1518c2ecf20Sopenharmony_ci if (!strcmp("usb-sdp", chargetype)) 1528c2ecf20Sopenharmony_ci return POWER_SUPPLY_TYPE_USB; 1538c2ecf20Sopenharmony_ci if (!strcmp("usb-dcp", chargetype)) 1548c2ecf20Sopenharmony_ci return POWER_SUPPLY_TYPE_USB; 1558c2ecf20Sopenharmony_ci if (!strcmp("usb-cdp", chargetype)) 1568c2ecf20Sopenharmony_ci return POWER_SUPPLY_TYPE_USB; 1578c2ecf20Sopenharmony_ci if (!strcmp("usb-aca", chargetype)) 1588c2ecf20Sopenharmony_ci return POWER_SUPPLY_TYPE_USB; 1598c2ecf20Sopenharmony_ci } 1608c2ecf20Sopenharmony_ci dev_warn(dev, "unknown charger type %s\n", chargetype); 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci return POWER_SUPPLY_TYPE_UNKNOWN; 1638c2ecf20Sopenharmony_ci} 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_cistatic int gpio_charger_get_irq(struct device *dev, void *dev_id, 1668c2ecf20Sopenharmony_ci struct gpio_desc *gpio) 1678c2ecf20Sopenharmony_ci{ 1688c2ecf20Sopenharmony_ci int ret, irq = gpiod_to_irq(gpio); 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci if (irq > 0) { 1718c2ecf20Sopenharmony_ci ret = devm_request_any_context_irq(dev, irq, gpio_charger_irq, 1728c2ecf20Sopenharmony_ci IRQF_TRIGGER_RISING | 1738c2ecf20Sopenharmony_ci IRQF_TRIGGER_FALLING, 1748c2ecf20Sopenharmony_ci dev_name(dev), 1758c2ecf20Sopenharmony_ci dev_id); 1768c2ecf20Sopenharmony_ci if (ret < 0) { 1778c2ecf20Sopenharmony_ci dev_warn(dev, "Failed to request irq: %d\n", ret); 1788c2ecf20Sopenharmony_ci irq = 0; 1798c2ecf20Sopenharmony_ci } 1808c2ecf20Sopenharmony_ci } 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci return irq; 1838c2ecf20Sopenharmony_ci} 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_cistatic int init_charge_current_limit(struct device *dev, 1868c2ecf20Sopenharmony_ci struct gpio_charger *gpio_charger) 1878c2ecf20Sopenharmony_ci{ 1888c2ecf20Sopenharmony_ci int i, len; 1898c2ecf20Sopenharmony_ci u32 cur_limit = U32_MAX; 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ci gpio_charger->current_limit_gpios = devm_gpiod_get_array_optional(dev, 1928c2ecf20Sopenharmony_ci "charge-current-limit", GPIOD_OUT_LOW); 1938c2ecf20Sopenharmony_ci if (IS_ERR(gpio_charger->current_limit_gpios)) { 1948c2ecf20Sopenharmony_ci dev_err(dev, "error getting current-limit GPIOs\n"); 1958c2ecf20Sopenharmony_ci return PTR_ERR(gpio_charger->current_limit_gpios); 1968c2ecf20Sopenharmony_ci } 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ci if (!gpio_charger->current_limit_gpios) 1998c2ecf20Sopenharmony_ci return 0; 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci len = device_property_read_u32_array(dev, "charge-current-limit-mapping", 2028c2ecf20Sopenharmony_ci NULL, 0); 2038c2ecf20Sopenharmony_ci if (len < 0) 2048c2ecf20Sopenharmony_ci return len; 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci if (len == 0 || len % 2) { 2078c2ecf20Sopenharmony_ci dev_err(dev, "invalid charge-current-limit-mapping length\n"); 2088c2ecf20Sopenharmony_ci return -EINVAL; 2098c2ecf20Sopenharmony_ci } 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci gpio_charger->current_limit_map = devm_kmalloc_array(dev, 2128c2ecf20Sopenharmony_ci len / 2, sizeof(*gpio_charger->current_limit_map), GFP_KERNEL); 2138c2ecf20Sopenharmony_ci if (!gpio_charger->current_limit_map) 2148c2ecf20Sopenharmony_ci return -ENOMEM; 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci gpio_charger->current_limit_map_size = len / 2; 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci len = device_property_read_u32_array(dev, "charge-current-limit-mapping", 2198c2ecf20Sopenharmony_ci (u32*) gpio_charger->current_limit_map, len); 2208c2ecf20Sopenharmony_ci if (len < 0) 2218c2ecf20Sopenharmony_ci return len; 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_ci for (i=0; i < gpio_charger->current_limit_map_size; i++) { 2248c2ecf20Sopenharmony_ci if (gpio_charger->current_limit_map[i].limit_ua > cur_limit) { 2258c2ecf20Sopenharmony_ci dev_err(dev, "charge-current-limit-mapping not sorted by current in descending order\n"); 2268c2ecf20Sopenharmony_ci return -EINVAL; 2278c2ecf20Sopenharmony_ci } 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_ci cur_limit = gpio_charger->current_limit_map[i].limit_ua; 2308c2ecf20Sopenharmony_ci } 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci /* default to smallest current limitation for safety reasons */ 2338c2ecf20Sopenharmony_ci len = gpio_charger->current_limit_map_size - 1; 2348c2ecf20Sopenharmony_ci set_charge_current_limit(gpio_charger, 2358c2ecf20Sopenharmony_ci gpio_charger->current_limit_map[len].limit_ua); 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ci return 0; 2388c2ecf20Sopenharmony_ci} 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_ci/* 2418c2ecf20Sopenharmony_ci * The entries will be overwritten by driver's probe routine depending 2428c2ecf20Sopenharmony_ci * on the available features. This list ensures, that the array is big 2438c2ecf20Sopenharmony_ci * enough for all optional features. 2448c2ecf20Sopenharmony_ci */ 2458c2ecf20Sopenharmony_cistatic enum power_supply_property gpio_charger_properties[] = { 2468c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_ONLINE, 2478c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_STATUS, 2488c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, 2498c2ecf20Sopenharmony_ci}; 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_cistatic int gpio_charger_probe(struct platform_device *pdev) 2528c2ecf20Sopenharmony_ci{ 2538c2ecf20Sopenharmony_ci struct device *dev = &pdev->dev; 2548c2ecf20Sopenharmony_ci const struct gpio_charger_platform_data *pdata = dev->platform_data; 2558c2ecf20Sopenharmony_ci struct power_supply_config psy_cfg = {}; 2568c2ecf20Sopenharmony_ci struct gpio_charger *gpio_charger; 2578c2ecf20Sopenharmony_ci struct power_supply_desc *charger_desc; 2588c2ecf20Sopenharmony_ci struct gpio_desc *charge_status; 2598c2ecf20Sopenharmony_ci int charge_status_irq; 2608c2ecf20Sopenharmony_ci int ret; 2618c2ecf20Sopenharmony_ci int num_props = 0; 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ci if (!pdata && !dev->of_node) { 2648c2ecf20Sopenharmony_ci dev_err(dev, "No platform data\n"); 2658c2ecf20Sopenharmony_ci return -ENOENT; 2668c2ecf20Sopenharmony_ci } 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_ci gpio_charger = devm_kzalloc(dev, sizeof(*gpio_charger), GFP_KERNEL); 2698c2ecf20Sopenharmony_ci if (!gpio_charger) 2708c2ecf20Sopenharmony_ci return -ENOMEM; 2718c2ecf20Sopenharmony_ci gpio_charger->dev = dev; 2728c2ecf20Sopenharmony_ci 2738c2ecf20Sopenharmony_ci /* 2748c2ecf20Sopenharmony_ci * This will fetch a GPIO descriptor from device tree, ACPI or 2758c2ecf20Sopenharmony_ci * boardfile descriptor tables. It's good to try this first. 2768c2ecf20Sopenharmony_ci */ 2778c2ecf20Sopenharmony_ci gpio_charger->gpiod = devm_gpiod_get_optional(dev, NULL, GPIOD_IN); 2788c2ecf20Sopenharmony_ci if (IS_ERR(gpio_charger->gpiod)) { 2798c2ecf20Sopenharmony_ci /* Just try again if this happens */ 2808c2ecf20Sopenharmony_ci return dev_err_probe(dev, PTR_ERR(gpio_charger->gpiod), 2818c2ecf20Sopenharmony_ci "error getting GPIO descriptor\n"); 2828c2ecf20Sopenharmony_ci } 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_ci if (gpio_charger->gpiod) { 2858c2ecf20Sopenharmony_ci gpio_charger_properties[num_props] = POWER_SUPPLY_PROP_ONLINE; 2868c2ecf20Sopenharmony_ci num_props++; 2878c2ecf20Sopenharmony_ci } 2888c2ecf20Sopenharmony_ci 2898c2ecf20Sopenharmony_ci charge_status = devm_gpiod_get_optional(dev, "charge-status", GPIOD_IN); 2908c2ecf20Sopenharmony_ci if (IS_ERR(charge_status)) 2918c2ecf20Sopenharmony_ci return PTR_ERR(charge_status); 2928c2ecf20Sopenharmony_ci if (charge_status) { 2938c2ecf20Sopenharmony_ci gpio_charger->charge_status = charge_status; 2948c2ecf20Sopenharmony_ci gpio_charger_properties[num_props] = POWER_SUPPLY_PROP_STATUS; 2958c2ecf20Sopenharmony_ci num_props++; 2968c2ecf20Sopenharmony_ci } 2978c2ecf20Sopenharmony_ci 2988c2ecf20Sopenharmony_ci ret = init_charge_current_limit(dev, gpio_charger); 2998c2ecf20Sopenharmony_ci if (ret < 0) 3008c2ecf20Sopenharmony_ci return ret; 3018c2ecf20Sopenharmony_ci if (gpio_charger->current_limit_map) { 3028c2ecf20Sopenharmony_ci gpio_charger_properties[num_props] = 3038c2ecf20Sopenharmony_ci POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX; 3048c2ecf20Sopenharmony_ci num_props++; 3058c2ecf20Sopenharmony_ci } 3068c2ecf20Sopenharmony_ci 3078c2ecf20Sopenharmony_ci charger_desc = &gpio_charger->charger_desc; 3088c2ecf20Sopenharmony_ci charger_desc->properties = gpio_charger_properties; 3098c2ecf20Sopenharmony_ci charger_desc->num_properties = num_props; 3108c2ecf20Sopenharmony_ci charger_desc->get_property = gpio_charger_get_property; 3118c2ecf20Sopenharmony_ci charger_desc->set_property = gpio_charger_set_property; 3128c2ecf20Sopenharmony_ci charger_desc->property_is_writeable = 3138c2ecf20Sopenharmony_ci gpio_charger_property_is_writeable; 3148c2ecf20Sopenharmony_ci 3158c2ecf20Sopenharmony_ci psy_cfg.of_node = dev->of_node; 3168c2ecf20Sopenharmony_ci psy_cfg.drv_data = gpio_charger; 3178c2ecf20Sopenharmony_ci 3188c2ecf20Sopenharmony_ci if (pdata) { 3198c2ecf20Sopenharmony_ci charger_desc->name = pdata->name; 3208c2ecf20Sopenharmony_ci charger_desc->type = pdata->type; 3218c2ecf20Sopenharmony_ci psy_cfg.supplied_to = pdata->supplied_to; 3228c2ecf20Sopenharmony_ci psy_cfg.num_supplicants = pdata->num_supplicants; 3238c2ecf20Sopenharmony_ci } else { 3248c2ecf20Sopenharmony_ci charger_desc->name = dev->of_node->name; 3258c2ecf20Sopenharmony_ci charger_desc->type = gpio_charger_get_type(dev); 3268c2ecf20Sopenharmony_ci } 3278c2ecf20Sopenharmony_ci 3288c2ecf20Sopenharmony_ci if (!charger_desc->name) 3298c2ecf20Sopenharmony_ci charger_desc->name = pdev->name; 3308c2ecf20Sopenharmony_ci 3318c2ecf20Sopenharmony_ci gpio_charger->charger = devm_power_supply_register(dev, charger_desc, 3328c2ecf20Sopenharmony_ci &psy_cfg); 3338c2ecf20Sopenharmony_ci if (IS_ERR(gpio_charger->charger)) { 3348c2ecf20Sopenharmony_ci ret = PTR_ERR(gpio_charger->charger); 3358c2ecf20Sopenharmony_ci dev_err(dev, "Failed to register power supply: %d\n", ret); 3368c2ecf20Sopenharmony_ci return ret; 3378c2ecf20Sopenharmony_ci } 3388c2ecf20Sopenharmony_ci 3398c2ecf20Sopenharmony_ci gpio_charger->irq = gpio_charger_get_irq(dev, gpio_charger->charger, 3408c2ecf20Sopenharmony_ci gpio_charger->gpiod); 3418c2ecf20Sopenharmony_ci 3428c2ecf20Sopenharmony_ci charge_status_irq = gpio_charger_get_irq(dev, gpio_charger->charger, 3438c2ecf20Sopenharmony_ci gpio_charger->charge_status); 3448c2ecf20Sopenharmony_ci gpio_charger->charge_status_irq = charge_status_irq; 3458c2ecf20Sopenharmony_ci 3468c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, gpio_charger); 3478c2ecf20Sopenharmony_ci 3488c2ecf20Sopenharmony_ci device_init_wakeup(dev, 1); 3498c2ecf20Sopenharmony_ci 3508c2ecf20Sopenharmony_ci return 0; 3518c2ecf20Sopenharmony_ci} 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP 3548c2ecf20Sopenharmony_cistatic int gpio_charger_suspend(struct device *dev) 3558c2ecf20Sopenharmony_ci{ 3568c2ecf20Sopenharmony_ci struct gpio_charger *gpio_charger = dev_get_drvdata(dev); 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_ci if (device_may_wakeup(dev)) 3598c2ecf20Sopenharmony_ci gpio_charger->wakeup_enabled = 3608c2ecf20Sopenharmony_ci !enable_irq_wake(gpio_charger->irq); 3618c2ecf20Sopenharmony_ci 3628c2ecf20Sopenharmony_ci return 0; 3638c2ecf20Sopenharmony_ci} 3648c2ecf20Sopenharmony_ci 3658c2ecf20Sopenharmony_cistatic int gpio_charger_resume(struct device *dev) 3668c2ecf20Sopenharmony_ci{ 3678c2ecf20Sopenharmony_ci struct gpio_charger *gpio_charger = dev_get_drvdata(dev); 3688c2ecf20Sopenharmony_ci 3698c2ecf20Sopenharmony_ci if (device_may_wakeup(dev) && gpio_charger->wakeup_enabled) 3708c2ecf20Sopenharmony_ci disable_irq_wake(gpio_charger->irq); 3718c2ecf20Sopenharmony_ci power_supply_changed(gpio_charger->charger); 3728c2ecf20Sopenharmony_ci 3738c2ecf20Sopenharmony_ci return 0; 3748c2ecf20Sopenharmony_ci} 3758c2ecf20Sopenharmony_ci#endif 3768c2ecf20Sopenharmony_ci 3778c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(gpio_charger_pm_ops, 3788c2ecf20Sopenharmony_ci gpio_charger_suspend, gpio_charger_resume); 3798c2ecf20Sopenharmony_ci 3808c2ecf20Sopenharmony_cistatic const struct of_device_id gpio_charger_match[] = { 3818c2ecf20Sopenharmony_ci { .compatible = "gpio-charger" }, 3828c2ecf20Sopenharmony_ci { } 3838c2ecf20Sopenharmony_ci}; 3848c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, gpio_charger_match); 3858c2ecf20Sopenharmony_ci 3868c2ecf20Sopenharmony_cistatic struct platform_driver gpio_charger_driver = { 3878c2ecf20Sopenharmony_ci .probe = gpio_charger_probe, 3888c2ecf20Sopenharmony_ci .driver = { 3898c2ecf20Sopenharmony_ci .name = "gpio-charger", 3908c2ecf20Sopenharmony_ci .pm = &gpio_charger_pm_ops, 3918c2ecf20Sopenharmony_ci .of_match_table = gpio_charger_match, 3928c2ecf20Sopenharmony_ci }, 3938c2ecf20Sopenharmony_ci}; 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_cimodule_platform_driver(gpio_charger_driver); 3968c2ecf20Sopenharmony_ci 3978c2ecf20Sopenharmony_ciMODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); 3988c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Driver for chargers only communicating via GPIO(s)"); 3998c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 4008c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:gpio-charger"); 401