^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Driver for Analog Devices (Linear Technology) LT3651 charger IC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2017, Topic Embedded Products
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/power_supply.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct lt3651_charger {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct power_supply *charger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct power_supply_desc charger_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct gpio_desc *acpr_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct gpio_desc *fault_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct gpio_desc *chrg_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static irqreturn_t lt3651_charger_irq(int irq, void *devid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct power_supply *charger = devid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) power_supply_changed(charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static inline struct lt3651_charger *psy_to_lt3651_charger(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct power_supply *psy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) return power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static int lt3651_charger_get_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) enum power_supply_property psp, union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct lt3651_charger *lt3651_charger = psy_to_lt3651_charger(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) case POWER_SUPPLY_PROP_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (!lt3651_charger->chrg_gpio) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (gpiod_get_value(lt3651_charger->chrg_gpio))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) val->intval = POWER_SUPPLY_STATUS_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) case POWER_SUPPLY_PROP_ONLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) val->intval = gpiod_get_value(lt3651_charger->acpr_gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) case POWER_SUPPLY_PROP_HEALTH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (!lt3651_charger->fault_gpio) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (!gpiod_get_value(lt3651_charger->fault_gpio)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) val->intval = POWER_SUPPLY_HEALTH_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * If the fault pin is active, the chrg pin explains the type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (!lt3651_charger->chrg_gpio) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) val->intval = gpiod_get_value(lt3651_charger->chrg_gpio) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) POWER_SUPPLY_HEALTH_OVERHEAT :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) POWER_SUPPLY_HEALTH_DEAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static enum power_supply_property lt3651_charger_properties[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) POWER_SUPPLY_PROP_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) POWER_SUPPLY_PROP_ONLINE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) POWER_SUPPLY_PROP_HEALTH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) static int lt3651_charger_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) struct power_supply_config psy_cfg = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) struct lt3651_charger *lt3651_charger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct power_supply_desc *charger_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) lt3651_charger = devm_kzalloc(&pdev->dev, sizeof(*lt3651_charger),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (!lt3651_charger)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) lt3651_charger->acpr_gpio = devm_gpiod_get(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) "lltc,acpr", GPIOD_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (IS_ERR(lt3651_charger->acpr_gpio)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) ret = PTR_ERR(lt3651_charger->acpr_gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) dev_err(&pdev->dev, "Failed to acquire acpr GPIO: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) lt3651_charger->fault_gpio = devm_gpiod_get_optional(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) "lltc,fault", GPIOD_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (IS_ERR(lt3651_charger->fault_gpio)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) ret = PTR_ERR(lt3651_charger->fault_gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) dev_err(&pdev->dev, "Failed to acquire fault GPIO: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) lt3651_charger->chrg_gpio = devm_gpiod_get_optional(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) "lltc,chrg", GPIOD_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (IS_ERR(lt3651_charger->chrg_gpio)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) ret = PTR_ERR(lt3651_charger->chrg_gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) dev_err(&pdev->dev, "Failed to acquire chrg GPIO: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) charger_desc = <3651_charger->charger_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) charger_desc->name = pdev->dev.of_node->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) charger_desc->type = POWER_SUPPLY_TYPE_MAINS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) charger_desc->properties = lt3651_charger_properties;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) charger_desc->num_properties = ARRAY_SIZE(lt3651_charger_properties);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) charger_desc->get_property = lt3651_charger_get_property;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) psy_cfg.of_node = pdev->dev.of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) psy_cfg.drv_data = lt3651_charger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) lt3651_charger->charger = devm_power_supply_register(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) charger_desc, &psy_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (IS_ERR(lt3651_charger->charger)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) ret = PTR_ERR(lt3651_charger->charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) dev_err(&pdev->dev, "Failed to register power supply: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * Acquire IRQs for the GPIO pins if possible. If the system does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * support IRQs on these pins, userspace will have to poll the sysfs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * files manually.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (lt3651_charger->acpr_gpio) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) ret = gpiod_to_irq(lt3651_charger->acpr_gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (ret >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) ret = devm_request_any_context_irq(&pdev->dev, ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) lt3651_charger_irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) dev_name(&pdev->dev), lt3651_charger->charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) dev_warn(&pdev->dev, "Failed to request acpr irq\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (lt3651_charger->fault_gpio) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) ret = gpiod_to_irq(lt3651_charger->fault_gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (ret >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) ret = devm_request_any_context_irq(&pdev->dev, ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) lt3651_charger_irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) dev_name(&pdev->dev), lt3651_charger->charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) dev_warn(&pdev->dev, "Failed to request fault irq\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (lt3651_charger->chrg_gpio) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) ret = gpiod_to_irq(lt3651_charger->chrg_gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (ret >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) ret = devm_request_any_context_irq(&pdev->dev, ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) lt3651_charger_irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) dev_name(&pdev->dev), lt3651_charger->charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) dev_warn(&pdev->dev, "Failed to request chrg irq\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) platform_set_drvdata(pdev, lt3651_charger);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static const struct of_device_id lt3651_charger_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) { .compatible = "lltc,ltc3651-charger" }, /* DEPRECATED */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) { .compatible = "lltc,lt3651-charger" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) MODULE_DEVICE_TABLE(of, lt3651_charger_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) static struct platform_driver lt3651_charger_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) .probe = lt3651_charger_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) .name = "lt3651-charger",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) .of_match_table = lt3651_charger_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) module_platform_driver(lt3651_charger_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) MODULE_AUTHOR("Mike Looijmans <mike.looijmans@topic.nl>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) MODULE_DESCRIPTION("Driver for LT3651 charger");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) MODULE_ALIAS("platform:lt3651-charger");