From 9e4d0aeb6ed2321c54e91d049a1566d60dcbc716 Mon Sep 17 00:00:00 2001 From: W4HO <87742609+W4HO-ham@users.noreply.github.com> Date: Tue, 27 Oct 2026 12:20:29 -0647 Subject: [PATCH 1/2] dt-bindings: power: supply: Add the Maxim MAX77779 charger The MAX77779 power management IC's switching charger sits on the same I2C bus as its fuel gauge (maxim,max77779-fg), at 0x69. It charges the battery from USB and powers the system; the gauge names it in its power-supplies. Signed-off-by: W4HO <87742609+W4HO-ham@users.noreply.github.com> --- .../power/supply/maxim,max77779-charger.yaml | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 Documentation/devicetree/bindings/power/supply/maxim,max77779-charger.yaml diff --git a/Documentation/devicetree/bindings/power/supply/maxim,max77779-charger.yaml b/Documentation/devicetree/bindings/power/supply/maxim,max77779-charger.yaml new file mode 100644 index 0000000000..0842c0d00a --- /dev/null +++ b/Documentation/devicetree/bindings/power/supply/maxim,max77779-charger.yaml @@ -0,0 +1,58 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/power/supply/maxim,max77779-charger.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Maxim MAX77779 battery charger + +maintainers: + - W4HO <87742609+W4HO-ham@users.noreply.github.com> + +description: + The switching charger of the Maxim MAX77779 power management IC, on the + same I2C bus as its fuel gauge. It charges the battery from USB and + powers the system from it. The MAX77779's charger, fuel gauge and Type-C + controller answer at their own I2C addresses with their own register + maps, so each is described as its own I2C device rather than as a cell + of one multi-function device. + +allOf: + - $ref: power-supply.yaml# + +properties: + compatible: + const: maxim,max77779-charger + + reg: + maxItems: 1 + + interrupts: + maxItems: 1 + description: The charger's interrupt, through the PMIC's interrupt + controller. + +required: + - compatible + - reg + +unevaluatedProperties: false + +examples: + - | + i2c { + #address-cells = <1>; + #size-cells = <0>; + + charger: charger@69 { + compatible = "maxim,max77779-charger"; + reg = <0x69>; + }; + + fuel-gauge@36 { + compatible = "maxim,max77779-fg"; + reg = <0x36>; + shunt-resistor-micro-ohms = <2000>; + power-supplies = <&charger>; + }; + }; From 97e64a8f0293c8b9a939576f6c058cbeaedeaddc Mon Sep 17 00:00:00 2001 From: W4HO <87742609+W4HO-ham@users.noreply.github.com> Date: Tue, 27 Oct 2026 12:20:29 -0647 Subject: [PATCH 2/2] power: supply: Add the Maxim MAX77779 charger Add a driver for the MAX77779's charger, which the Pixel 9a runs under its OTP defaults. It reports whether an input (USB or the wireless receiver) is present, the charge status and type from CHG_DETAILS_01, the input current limit and the charge current, and supplies the fuel gauge, so the battery's status follows the charger. charge_behaviour inhibit-charge stops charging: the charger goes to buck-only mode (CHG_CNFG_00 mode 4; that register isn't behind CHGPROT) and USB powers the system, though near full the battery still supplies part of the load; auto, unbinding and shutdown put it back in charge- and-buck mode. An inhibit survives a warm reboot, since the PMIC stays powered, so probe takes the inhibit state from the mode it finds. Nothing else is written, and the interrupt registers (clear on read) are not read: the state is polled every 10 s. Tested on the Pixel 9a, with the device created at 0x69 by hand in place of the out-of-tree driver it replaces: online, status, charge type, input current limit (1.5 A) and charge current read as the chip reports them; inhibit-charge gives "Not charging" (mode 4) and auto "Charging" (mode 5) again; unbinding while inhibited leaves mode 5; and probing with the charger found in mode 4 reports inhibit-charge and "Not charging", and auto then restores mode 5. At 99 % the battery still gave about 450 mA while inhibited, measured the same way with the out-of-tree driver. The gauge's link to the charger comes from the DT and was not part of this test. Register fields from Google's MAX77779 charger driver and register header. Link: https://android.googlesource.com/kernel/google-modules/bms/+/183509aff980610401869deca9f6ce8836980fe7/max77779_charger.c Link: https://android.googlesource.com/kernel/google-modules/bms/+/183509aff980610401869deca9f6ce8836980fe7/max77779_v20240115_regs.h Signed-off-by: W4HO <87742609+W4HO-ham@users.noreply.github.com> --- MAINTAINERS | 4 +- drivers/power/supply/Kconfig | 14 + drivers/power/supply/Makefile | 1 + drivers/power/supply/max77779_charger.c | 401 ++++++++++++++++++++++++ 4 files changed, 419 insertions(+), 1 deletion(-) create mode 100644 drivers/power/supply/max77779_charger.c diff --git a/MAINTAINERS b/MAINTAINERS index 00e06c4795..1f9d7fbbd9 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -16190,12 +16190,14 @@ L: linux-kernel@vger.kernel.org S: Maintained F: drivers/power/supply/max77759_charger.c -MAXIM MAX77779 FUEL GAUGE DRIVER +MAXIM MAX77779 FUEL GAUGE AND CHARGER DRIVERS M: W4HO <87742609+W4HO-ham@users.noreply.github.com> L: linux-pm@vger.kernel.org S: Maintained +F: Documentation/devicetree/bindings/power/supply/maxim,max77779-charger.yaml F: Documentation/devicetree/bindings/power/supply/maxim,max77779-fg.yaml F: drivers/power/supply/max77779_battery.c +F: drivers/power/supply/max77779_charger.c MAXIM MAX77802 PMIC REGULATOR DEVICE DRIVER M: Javier Martinez Canillas diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig index 691781cd61..1e63a06a74 100644 --- a/drivers/power/supply/Kconfig +++ b/drivers/power/supply/Kconfig @@ -470,6 +470,20 @@ config BATTERY_MAX77779 The module is named max77779_battery. +config CHARGER_MAX77779 + tristate "Maxim MAX77779 battery charger" + depends on I2C + depends on OF + select REGMAP_I2C + help + Say Y or M here to report the state of the charger in the Maxim + MAX77779 power management IC, found in the Google Pixel 9a, and + to let userspace stop and restart charging. The charger keeps + its own (OTP) charge current and voltage; the driver only reads + them. + + The module is named max77779_charger. + config BATTERY_MAX1720X tristate "Maxim MAX17201/MAX17205 Fuel Gauge" depends on I2C diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile index 0f9ae2cb99..fd1fd91e4c 100644 --- a/drivers/power/supply/Makefile +++ b/drivers/power/supply/Makefile @@ -59,6 +59,7 @@ obj-$(CONFIG_BATTERY_DA9150) += da9150-fg.o obj-$(CONFIG_BATTERY_MAX17040) += max17040_battery.o obj-$(CONFIG_BATTERY_MAX17042) += max17042_battery.o obj-$(CONFIG_BATTERY_MAX77779) += max77779_battery.o +obj-$(CONFIG_CHARGER_MAX77779) += max77779_charger.o obj-$(CONFIG_BATTERY_MAX1720X) += max1720x_battery.o obj-$(CONFIG_BATTERY_MAX1721X) += max1721x_battery.o obj-$(CONFIG_BATTERY_RT5033) += rt5033_battery.o diff --git a/drivers/power/supply/max77779_charger.c b/drivers/power/supply/max77779_charger.c new file mode 100644 index 0000000000..8d41ddffee --- /dev/null +++ b/drivers/power/supply/max77779_charger.c @@ -0,0 +1,401 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Maxim MAX77779 battery charger: status and charge inhibit. + * + * The charger runs under its own (OTP) defaults. This driver reports what + * it is doing and can stop charging: inhibit-charge puts it in buck-only + * mode, so the battery is no longer charged and USB powers the system, + * though with the battery near full the battery still supplies part of the + * load. Nothing else is written; the interrupt registers (clear on read) + * are never read, and the state is polled. + * + * Register fields from Google's driver (google-modules/bms at 183509aff980: + * max77779_charger.c, GPL, and max77779_v20240115_regs.h). + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MAX77779_CHG_INT_OK 0xb6 +#define MAX77779_CHGIN_OK BIT(6) +#define MAX77779_WCIN_OK BIT(5) +#define MAX77779_INPUT_OK (MAX77779_CHGIN_OK | MAX77779_WCIN_OK) +#define MAX77779_CHG_DETAILS_01 0xb8 +#define MAX77779_CHG_DTLS GENMASK(3, 0) +#define MAX77779_CHG_CNFG_00 0xbc +#define MAX77779_MODE GENMASK(3, 0) +#define MAX77779_MODE_BUCK 0x4 +#define MAX77779_MODE_CHG_BUCK 0x5 +#define MAX77779_CHG_CNFG_02 0xbe +#define MAX77779_CHGCC GENMASK(5, 0) +#define MAX77779_CHG_CNFG_09 0xc5 +#define MAX77779_CHGIN_ILIM GENMASK(6, 0) + +#define MAX77779_POLL_MS 10000 + +/* CHG_DETAILS_01.CHG_DTLS */ +enum { + MAX77779_DTLS_DEAD_BATTERY = 0x0, + MAX77779_DTLS_FAST_CC = 0x1, + MAX77779_DTLS_FAST_CV = 0x2, + MAX77779_DTLS_TOP_OFF = 0x3, + MAX77779_DTLS_DONE = 0x4, +}; + +struct max77779_charger { + struct device *dev; + struct regmap *map; + struct power_supply *psy; + struct delayed_work poll; + /* serialises the mode changes */ + struct mutex lock; + unsigned int last; + int behaviour; + bool held; +}; + +static bool max77779_charger_readable(struct device *dev, unsigned int reg) +{ + return reg >= MAX77779_CHG_INT_OK && reg <= MAX77779_CHG_CNFG_09; +} + +static bool max77779_charger_writeable(struct device *dev, unsigned int reg) +{ + return reg == MAX77779_CHG_CNFG_00; +} + +static const struct regmap_config max77779_charger_regmap = { + .reg_bits = 8, + .val_bits = 8, + .max_register = MAX77779_CHG_CNFG_09, + .readable_reg = max77779_charger_readable, + .writeable_reg = max77779_charger_writeable, + .cache_type = REGCACHE_NONE, +}; + +/* CHG_CNFG_00 is not behind CHGPROT (unlike CNFG_01, 03, 07-08 and 13 on). */ +static int max77779_charger_set_mode(struct max77779_charger *chg, + unsigned int mode) +{ + return regmap_update_bits(chg->map, MAX77779_CHG_CNFG_00, + MAX77779_MODE, mode); +} + +static int max77779_charger_status(struct max77779_charger *chg, int *status) +{ + unsigned int ok, dtls; + int ret; + + ret = regmap_read(chg->map, MAX77779_CHG_INT_OK, &ok); + if (ret) + return ret; + ret = regmap_read(chg->map, MAX77779_CHG_DETAILS_01, &dtls); + if (ret) + return ret; + + if (!(ok & MAX77779_INPUT_OK)) + *status = POWER_SUPPLY_STATUS_DISCHARGING; + else if (chg->held) + *status = POWER_SUPPLY_STATUS_NOT_CHARGING; + else + switch (FIELD_GET(MAX77779_CHG_DTLS, dtls)) { + case MAX77779_DTLS_DEAD_BATTERY: + case MAX77779_DTLS_FAST_CC: + case MAX77779_DTLS_FAST_CV: + case MAX77779_DTLS_TOP_OFF: + *status = POWER_SUPPLY_STATUS_CHARGING; + break; + case MAX77779_DTLS_DONE: + *status = POWER_SUPPLY_STATUS_FULL; + break; + default: + *status = POWER_SUPPLY_STATUS_NOT_CHARGING; + } + + return 0; +} + +static int max77779_charger_charge_type(struct max77779_charger *chg, int *type) +{ + unsigned int ok, dtls; + int ret; + + ret = regmap_read(chg->map, MAX77779_CHG_INT_OK, &ok); + if (ret) + return ret; + ret = regmap_read(chg->map, MAX77779_CHG_DETAILS_01, &dtls); + if (ret) + return ret; + + if (!(ok & MAX77779_INPUT_OK) || chg->held) { + *type = POWER_SUPPLY_CHARGE_TYPE_NONE; + return 0; + } + + switch (FIELD_GET(MAX77779_CHG_DTLS, dtls)) { + case MAX77779_DTLS_DEAD_BATTERY: + *type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE; + break; + case MAX77779_DTLS_FAST_CC: + *type = POWER_SUPPLY_CHARGE_TYPE_FAST; + break; + case MAX77779_DTLS_FAST_CV: + case MAX77779_DTLS_TOP_OFF: + *type = POWER_SUPPLY_CHARGE_TYPE_STANDARD; + break; + default: + *type = POWER_SUPPLY_CHARGE_TYPE_NONE; + } + + return 0; +} + +static int max77779_charger_get_property(struct power_supply *psy, + enum power_supply_property psp, + union power_supply_propval *val) +{ + struct max77779_charger *chg = power_supply_get_drvdata(psy); + unsigned int v; + int ret; + + switch (psp) { + case POWER_SUPPLY_PROP_ONLINE: + ret = regmap_read(chg->map, MAX77779_CHG_INT_OK, &v); + if (ret) + return ret; + val->intval = !!(v & MAX77779_INPUT_OK); + return 0; + case POWER_SUPPLY_PROP_STATUS: + return max77779_charger_status(chg, &val->intval); + case POWER_SUPPLY_PROP_CHARGE_TYPE: + return max77779_charger_charge_type(chg, &val->intval); + case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: + /* CHGIN_ILIM: 0 off, 1-3 100 mA, then 125 mA in 25 mA steps */ + ret = regmap_read(chg->map, MAX77779_CHG_CNFG_09, &v); + if (ret) + return ret; + v = FIELD_GET(MAX77779_CHGIN_ILIM, v); + val->intval = !v ? 0 : v < 4 ? 100000 : 125000 + (v - 4) * 25000; + return 0; + case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: + /* CHGCC as Google's driver reads it: 0 off, 1-2 133 mA, 4 A max */ + ret = regmap_read(chg->map, MAX77779_CHG_CNFG_02, &v); + if (ret) + return ret; + v = FIELD_GET(MAX77779_CHGCC, v); + if (!v) + val->intval = 0; + else if (v < 3) + val->intval = 133000; + else if (v >= 0x3c) + val->intval = 4000000; + else + val->intval = 133000 + (v - 2) * 66670; + return 0; + case POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR: + val->intval = chg->behaviour; + return 0; + default: + return -EINVAL; + } +} + +static int max77779_charger_set_property(struct power_supply *psy, + enum power_supply_property psp, + const union power_supply_propval *val) +{ + struct max77779_charger *chg = power_supply_get_drvdata(psy); + bool hold; + int ret; + + if (psp != POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR) + return -EINVAL; + if (val->intval != POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO && + val->intval != POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE) + return -EINVAL; + + hold = val->intval == POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE; + + scoped_guard(mutex, &chg->lock) { + if (hold != chg->held) { + ret = max77779_charger_set_mode(chg, hold ? + MAX77779_MODE_BUCK : + MAX77779_MODE_CHG_BUCK); + if (ret) + return ret; + chg->held = hold; + } + chg->behaviour = val->intval; + } + + power_supply_changed(chg->psy); + return 0; +} + +static int max77779_charger_property_is_writeable(struct power_supply *psy, + enum power_supply_property psp) +{ + return psp == POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR; +} + +static const enum power_supply_property max77779_charger_props[] = { + POWER_SUPPLY_PROP_ONLINE, + POWER_SUPPLY_PROP_STATUS, + POWER_SUPPLY_PROP_CHARGE_TYPE, + POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, + POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT, + POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR, +}; + +static const struct power_supply_desc max77779_charger_desc = { + .name = "max77779-charger", + .type = POWER_SUPPLY_TYPE_USB, + .properties = max77779_charger_props, + .num_properties = ARRAY_SIZE(max77779_charger_props), + .get_property = max77779_charger_get_property, + .set_property = max77779_charger_set_property, + .property_is_writeable = max77779_charger_property_is_writeable, + .charge_behaviours = BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO) | + BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE), +}; + +/* Tell the gauge and userspace when the input or the charge state changes. */ +static void max77779_charger_poll(struct work_struct *work) +{ + struct max77779_charger *chg = + container_of(to_delayed_work(work), struct max77779_charger, poll); + unsigned int ok = 0, dtls = 0, now; + + if (!regmap_read(chg->map, MAX77779_CHG_INT_OK, &ok) && + !regmap_read(chg->map, MAX77779_CHG_DETAILS_01, &dtls)) { + now = (ok & MAX77779_INPUT_OK) << 8 | + FIELD_GET(MAX77779_CHG_DTLS, dtls); + if (now != chg->last) { + chg->last = now; + power_supply_changed(chg->psy); + } + } + + queue_delayed_work(system_freezable_wq, &chg->poll, + msecs_to_jiffies(MAX77779_POLL_MS)); +} + +/* Leave the charger charging, as it is normally found. */ +static void max77779_charger_restore(void *data) +{ + struct max77779_charger *chg = data; + + if (chg->held) + max77779_charger_set_mode(chg, MAX77779_MODE_CHG_BUCK); +} + +static void max77779_charger_cancel_poll(void *data) +{ + struct max77779_charger *chg = data; + + cancel_delayed_work_sync(&chg->poll); +} + +static int max77779_charger_probe(struct i2c_client *client) +{ + struct power_supply_config cfg = {}; + struct device *dev = &client->dev; + struct max77779_charger *chg; + unsigned int mode; + int ret; + + chg = devm_kzalloc(dev, sizeof(*chg), GFP_KERNEL); + if (!chg) + return -ENOMEM; + + chg->dev = dev; + i2c_set_clientdata(client, chg); + chg->behaviour = POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO; + chg->last = ~0U; + INIT_DELAYED_WORK(&chg->poll, max77779_charger_poll); + + ret = devm_mutex_init(dev, &chg->lock); + if (ret) + return ret; + + chg->map = devm_regmap_init_i2c(client, &max77779_charger_regmap); + if (IS_ERR(chg->map)) + return PTR_ERR(chg->map); + + ret = regmap_read(chg->map, MAX77779_CHG_CNFG_00, &mode); + if (ret) + return dev_err_probe(dev, ret, "failed to read the mode\n"); + /* + * An inhibit survives a warm reboot (the PMIC stays powered): start + * from the mode found, so auto restores charging. Leaving inhibit + * always goes back to charging and buck. + */ + mode = FIELD_GET(MAX77779_MODE, mode); + if (mode == MAX77779_MODE_BUCK) { + chg->held = true; + chg->behaviour = POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE; + } else if (mode != MAX77779_MODE_CHG_BUCK) { + dev_warn(dev, "found in mode %#x, not charging and buck\n", mode); + } + + /* Unwound in reverse: poll stopped, supply gone, then charging back. */ + ret = devm_add_action_or_reset(dev, max77779_charger_restore, chg); + if (ret) + return ret; + + cfg.drv_data = chg; + cfg.fwnode = dev_fwnode(dev); + chg->psy = devm_power_supply_register(dev, &max77779_charger_desc, &cfg); + if (IS_ERR(chg->psy)) + return dev_err_probe(dev, PTR_ERR(chg->psy), + "failed to register the power supply\n"); + + ret = devm_add_action_or_reset(dev, max77779_charger_cancel_poll, chg); + if (ret) + return ret; + + queue_delayed_work(system_freezable_wq, &chg->poll, 0); + return 0; +} + +/* Don't let an inhibit outlive the system: reboot with charging on. */ +static void max77779_charger_shutdown(struct i2c_client *client) +{ + struct max77779_charger *chg = i2c_get_clientdata(client); + + cancel_delayed_work_sync(&chg->poll); + max77779_charger_restore(chg); +} + +static const struct of_device_id max77779_charger_of_match[] = { + { .compatible = "maxim,max77779-charger" }, + { } +}; +MODULE_DEVICE_TABLE(of, max77779_charger_of_match); + +static const struct i2c_device_id max77779_charger_id[] = { + { "max77779-charger" }, + { } +}; +MODULE_DEVICE_TABLE(i2c, max77779_charger_id); + +static struct i2c_driver max77779_charger_driver = { + .driver = { + .name = "max77779-charger", + .of_match_table = max77779_charger_of_match, + }, + .probe = max77779_charger_probe, + .shutdown = max77779_charger_shutdown, + .id_table = max77779_charger_id, +}; +module_i2c_driver(max77779_charger_driver); + +MODULE_DESCRIPTION("Maxim MAX77779 battery charger status and charge inhibit"); +MODULE_LICENSE("GPL");