From 5c502af92bfb0544b0ef0ce5983a6caab4f9fc4e Mon Sep 17 00:00:00 2001 From: ricktav <82380971+ricktav@users.noreply.github.com> Date: Tue, 24 Nov 2026 18:00:00 +0100 Subject: [PATCH] power: supply: apple_sn2400_charger: Let userspace limit charging The charger charges whenever the input allows it; userspace can neither stop at a charge level nor lower the charge current. iOS 15.8.8's AppleSN2400ChargerFunction (iPhone 6s Plus kernelcache) writes 0x0d = min(mA, 2550) / 10 from AppleARMPMUCharger's chargeLimit, which is capped at the ADT charge-limits[0] (1924 mA on the n66; the monitored battery's constant-charge-current-max on all six A9 iPhones). A chargeLimit of 0 writes 0x0d = 0 and leaves the input on: that is how iOS stops charging for IsCharging false, PredictiveChargingInhibit (optimized charging at 80 %) and its PoSM state-of-charge hold, which stops at an upper threshold until the charge falls to a lower one. Turning the input off (0x10 OFF, 0x09 = 0) is iOS's separate inflow inhibit. In control mode without apple,keep-charge-limit: - constant_charge_current (100 mA up to the battery's constant-charge-current-max, 10 mA steps) caps 0x0d; - charge_behaviour inhibit-charge writes 0x0d = 0, force-discharge also turns the input off as the temperature guard does; - the gauge's battery gets charge_control_end_threshold and start_threshold (and charge_behaviour) as a power-supply extension: charging stops at the end threshold and resumes below the start threshold, or 5 % below the end without one. The driver reads the gauge's capacity on the temperature guard's 5 s poll, only while charge control is in use. Without a capacity younger than a minute, or at 10 % and below, it charges as normal. The temperature guard still turns the input off or reduces it. Asleep the work does not run, so the suspend handler also stops charging from the start threshold up; force-discharge keeps the input off. jiffies stand still in s2idle, so resume reads the gauge again before deciding. 0x0d is written again after every cable change and read back while charging is held. Remove and shutdown restore the full limit: a 0 would survive into iBoot and the next boot. A panic or an emergency restart skips shutdown, and the I2C controller cannot write from a restart handler, so probe restores a 0 it finds, also with apple,keep-charge-limit, and warns that a crashed kernel left it. The charger's status bit stays set while 0x0d is 0, so the USB supply reports Not charging while the charge is held and Discharging with force-discharge. Every board whose SN2400 node is in control mode gets this: the A9 iPhones (s800x-iphone.dtsi), the iPhone 6 (N61) and the iPhone 7 and 7 Plus (t8010-7.dtsi). Read-only and apple,keep-charge-limit nodes keep their attributes read-only and register no extension. Tested on an iPhone 6s Plus (N66), RAM-booted, on a Mac's USB port (2026-11-25): inhibit-charge writes 0x0d = 0 and holds the battery at 0 A while the phone runs from the cable (IBUS 270-350 mA), with usb/status Not charging; auto charges again within a second. constant_charge_current 500000 gives 0x0d = 0x32, 123456 rounds to 120000, and values above the battery's limit or below 100 mA are refused. An end threshold below the charge stops charging within 7 s; force-discharge turns the input off (IBUS 0, battery -0.36 A). 0x0d stays 0 across an unplug and replug, and a clean reboot while held hands iOS a charger that charges. The current cap at a wall adapter, the thresholds across suspend and a crash while held were not tested, nor were the N61 and the iPhone 7. Signed-off-by: ricktav <82380971+ricktav@users.noreply.github.com> Assisted-by: LLM Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SFMaVf5sMxs7xEyPFZWGEj --- drivers/power/supply/Kconfig | 7 + drivers/power/supply/apple_sn2400_charger.c | 618 ++++++++++++++++++-- 2 files changed, 578 insertions(+), 47 deletions(-) diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig index 8dbbbd0a48a289..5972800e9b736e 100644 --- a/drivers/power/supply/Kconfig +++ b/drivers/power/supply/Kconfig @@ -351,6 +351,13 @@ config CHARGER_APPLE_SN2400 state and the input limit the bootloader left, and writes no charger register. The control module parameter overrides the DT. + Userspace can lower the battery charge current (constant_charge_current, + never above the monitored battery's limit), stop charging while the + phone keeps running from the cable (charge_behaviour inhibit-charge) or + run it from its battery (force-discharge), and hold the battery at a + charge level (charge_control_end_threshold and start_threshold on the + gauge's battery). Without a battery capacity it charges as normal. + config APPLE_BELLATRIX_POWER tristate "Apple Bellatrix USB power status" depends on I2C diff --git a/drivers/power/supply/apple_sn2400_charger.c b/drivers/power/supply/apple_sn2400_charger.c index c71821446196b5..ea1b4dba9cb8e2 100644 --- a/drivers/power/supply/apple_sn2400_charger.c +++ b/drivers/power/supply/apple_sn2400_charger.c @@ -25,7 +25,16 @@ #define SN2400_STATUS_CHARGING BIT(7) #define SN2400_REG_INPUT_LIMIT 0x09 +/* + * iOS AppleSN2400ChargerFunction writes the battery charge-current cap here, + * min(mA, 2550) / 10, and 0 to stop charging while the input stays on: that + * is how AppleARMPMUCharger's chargeLimit 0 (IsCharging false, + * PredictiveChargingInhibit, the PoSM SoC hold) reaches the charger. It reads + * back 0x0e (10 mA/LSB) and 0x0f (3.0 V + 10 mV/LSB). + */ #define SN2400_REG_CHARGE_LIMIT 0x0d +#define SN2400_REG_CHARGE_CURRENT 0x0e +#define SN2400_REG_CHARGE_VOLTAGE 0x0f #define SN2400_REG_CONTROL 0x10 #define SN2400_CONTROL_SPREAD_SPECTRUM GENMASK(1, 0) #define SN2400_CONTROL_RESERVED BIT(2) @@ -76,6 +85,27 @@ #define SN2400_THERMAL_HYST_DC 20 #define SN2400_THERMAL_REDUCED_UA 500000 +/* + * Charge control from userspace (control mode without apple,keep-charge-limit): + * constant_charge_current caps 0x0d between SN2400_CHARGE_MIN_UA and the + * monitored battery's constant-charge-current-max (iOS's own ceiling, ADT + * charge-limits[0]); charge_behaviour inhibit-charge writes 0x0d = 0 and keeps + * the input on, force-discharge also turns the input off; the battery's + * charge_control_end_threshold stops charging at that capacity until it falls + * below charge_control_start_threshold (end - SN2400_START_HYST_PCT when the + * start threshold is 0 or not below the end). Anything but normal charging + * needs a gauge capacity no older than SN2400_THERMAL_STALE_MS and above + * SN2400_CHARGE_FLOOR_PCT; without one the charger charges as normal. + */ +#define SN2400_CHARGE_MIN_UA 100000 +#define SN2400_END_THRESHOLD_MIN 20 +#define SN2400_START_HYST_PCT 5 +#define SN2400_CHARGE_FLOOR_PCT 10 +#define SN2400_CHARGE_BEHAVIOURS \ + (BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO) | \ + BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE) | \ + BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE)) + /* * A board whose charger node has "apple,read-only" gets a driver that only * reads the status, input-limit and control registers and leaves the charger @@ -95,6 +125,12 @@ enum sn2400_thermal_level { SN2400_THERMAL_OFF, }; +enum sn2400_charge_mode { + SN2400_CHARGE_NORMAL, + SN2400_CHARGE_INHIBIT, + SN2400_CHARGE_DISCHARGE, +}; + enum sn2400_foldback_state { SN2400_FOLDBACK_IDLE, SN2400_FOLDBACK_NOLOAD, @@ -146,6 +182,28 @@ struct sn2400_charger { unsigned long thermal_checked; unsigned long temp_read_at; bool temp_read; + /* The input is known to be off (0x10 OFF, 0x09 = 0) */ + bool input_off; + /* 0x0d may be changed at runtime: control mode, no keep-charge-limit */ + bool charge_control; + /* The same, decided before the USB supply registers: its sysfs modes */ + bool charge_control_allowed; + /* Userspace settings, under lock */ + unsigned int charge_ua; + enum power_supply_charge_behaviour behaviour; + int start_threshold; + int end_threshold; + /* Work state: last 0x0d written (-1 unknown), the applied mode */ + int charge_reg; + enum sn2400_charge_mode mode; + /* The end threshold was reached; held until below the start */ + bool hold; + /* At or below SN2400_CHARGE_FLOOR_PCT; cleared 5 % above it */ + bool low; + int capacity; + unsigned long capacity_read_at; + bool capacity_read; + bool ext_registered; }; static int sn2400_read_adc(struct sn2400_charger *charger, @@ -238,12 +296,15 @@ static int sn2400_set_input_limit(struct sn2400_charger *charger, if (!ret) ret = regmap_write(charger->regmap, SN2400_REG_INPUT_LIMIT, 0); + charger->input_off = !ret; if (!ret) *applied_ua = 0; return ret; } + charger->input_off = false; + /* * Give the charger the HDQ bus before enabling its input path. The * control-before-limit order is required by the D111 firmware sequence. @@ -420,8 +481,8 @@ static int sn2400_match_gauge(struct power_supply *psy, void *data) } /* - * Follow the battery temperature: the gauge is found once it has registered, - * and read every SN2400_THERMAL_POLL_MS. Returns whether the level changed. + * Follow the battery temperature, read from the gauge by + * sn2400_update_gauge(). Returns whether the level changed. */ static bool sn2400_update_thermal(struct sn2400_charger *charger) { @@ -429,19 +490,6 @@ static bool sn2400_update_thermal(struct sn2400_charger *charger) union power_supply_propval val; int ret = -ENODEV; - if (!charger->thermal || - (charger->thermal_checked && - time_is_after_jiffies(charger->thermal_checked + - msecs_to_jiffies(SN2400_THERMAL_POLL_MS)))) - return false; - charger->thermal_checked = jiffies ?: 1; - - if (!charger->gauge) { - struct sn2400_gauge_match match = { .battery = charger->battery }; - - power_supply_for_each_psy(&match, sn2400_match_gauge); - charger->gauge = match.psy; - } if (charger->gauge) ret = power_supply_get_property(charger->gauge, POWER_SUPPLY_PROP_TEMP, &val); @@ -482,6 +530,246 @@ static bool sn2400_update_thermal(struct sn2400_charger *charger) return true; } +static const struct power_supply_ext sn2400_battery_ext; + +/* + * The gauge is found once it has registered. A charger that may change its + * charge limit then gives the gauge's battery the charge thresholds and + * charge_behaviour, where desktops look for them. + */ +static void sn2400_find_gauge(struct sn2400_charger *charger) +{ + struct sn2400_gauge_match match = { .battery = charger->battery }; + int ret; + + if (charger->gauge) + return; + + power_supply_for_each_psy(&match, sn2400_match_gauge); + charger->gauge = match.psy; + if (!charger->gauge || !charger->charge_control) + return; + + ret = power_supply_register_extension(charger->gauge, + &sn2400_battery_ext, + charger->dev, charger); + if (ret) { + dev_warn(charger->dev, "no charge thresholds on %s: %d\n", + charger->gauge->desc->name, ret); + return; + } + charger->ext_registered = true; + dev_info(charger->dev, "charge thresholds on %s\n", + charger->gauge->desc->name); +} + +static bool sn2400_charge_control_active(struct sn2400_charger *charger) +{ + bool active; + + mutex_lock(&charger->lock); + active = charger->behaviour != POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO || + charger->end_threshold < 100; + mutex_unlock(&charger->lock); + + return active; +} + +static void sn2400_read_capacity(struct sn2400_charger *charger) +{ + union power_supply_propval val; + int ret = -ENODEV; + + if (charger->gauge) + ret = power_supply_get_property(charger->gauge, + POWER_SUPPLY_PROP_CAPACITY, + &val); + if (ret || val.intval < 0 || val.intval > 100) + return; + + charger->capacity = val.intval; + charger->capacity_read_at = jiffies; + charger->capacity_read = true; +} + +/* + * Every SN2400_THERMAL_POLL_MS: the battery temperature for the guard, and + * the capacity while charge control needs it. + */ +static void sn2400_update_gauge(struct sn2400_charger *charger) +{ + unsigned long checked = READ_ONCE(charger->thermal_checked); + + if (!charger->thermal && !charger->charge_control) + return; + if (checked && time_is_after_jiffies(checked + + msecs_to_jiffies(SN2400_THERMAL_POLL_MS))) + return; + WRITE_ONCE(charger->thermal_checked, jiffies ?: 1); + + sn2400_find_gauge(charger); + if (charger->charge_control && sn2400_charge_control_active(charger)) + sn2400_read_capacity(charger); + if (charger->thermal) + sn2400_update_thermal(charger); +} + +static bool sn2400_capacity_fresh(struct sn2400_charger *charger) +{ + return charger->capacity_read && + time_is_after_jiffies(charger->capacity_read_at + + msecs_to_jiffies(SN2400_THERMAL_STALE_MS)); +} + +/* Charging begins below this: the start threshold, or end - 5 % without one */ +static int sn2400_resume_below(int start, int end) +{ + if (start && start < end) + return start; + + return end > SN2400_START_HYST_PCT ? end - SN2400_START_HYST_PCT : 0; +} + +/* + * What userspace asked for, as far as the battery allows it: anything but + * normal charging needs a fresh capacity above SN2400_CHARGE_FLOOR_PCT. The + * end threshold latches a hold, as iOS's PoSM does, until the capacity falls + * below the start threshold. + */ +static enum sn2400_charge_mode sn2400_charge_mode(struct sn2400_charger *charger) +{ + enum power_supply_charge_behaviour behaviour; + int start, end; + + if (!charger->charge_control) + return SN2400_CHARGE_NORMAL; + + mutex_lock(&charger->lock); + behaviour = charger->behaviour; + start = charger->start_threshold; + end = charger->end_threshold; + mutex_unlock(&charger->lock); + + if (behaviour == POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO && end >= 100) { + /* Thresholds set later start held within the band. */ + charger->hold = true; + return SN2400_CHARGE_NORMAL; + } + if (!sn2400_capacity_fresh(charger)) + return SN2400_CHARGE_NORMAL; + if (charger->capacity <= SN2400_CHARGE_FLOOR_PCT) + charger->low = true; + else if (charger->capacity > + SN2400_CHARGE_FLOOR_PCT + SN2400_START_HYST_PCT) + charger->low = false; + if (charger->low) + return SN2400_CHARGE_NORMAL; + + if (behaviour == POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE) + return SN2400_CHARGE_DISCHARGE; + if (behaviour == POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE) + return SN2400_CHARGE_INHIBIT; + + if (charger->capacity >= end) + charger->hold = true; + else if (charger->capacity < sn2400_resume_below(start, end)) + charger->hold = false; + + return charger->hold ? SN2400_CHARGE_INHIBIT : SN2400_CHARGE_NORMAL; +} + +/* Asleep nothing re-checks the capacity: do not charge through the band. */ +static bool sn2400_in_band(struct sn2400_charger *charger) +{ + int start, end; + + mutex_lock(&charger->lock); + start = charger->start_threshold; + end = charger->end_threshold; + mutex_unlock(&charger->lock); + + return charger->charge_control && end < 100 && + sn2400_capacity_fresh(charger) && !charger->low && + charger->capacity >= sn2400_resume_below(start, end); +} + +static void sn2400_log_charge_mode(struct sn2400_charger *charger, + enum sn2400_charge_mode mode) +{ + unsigned int current_reg = 0, voltage_reg = 0; + bool fresh = sn2400_capacity_fresh(charger); + + regmap_read(charger->regmap, SN2400_REG_CHARGE_CURRENT, ¤t_reg); + regmap_read(charger->regmap, SN2400_REG_CHARGE_VOLTAGE, &voltage_reg); + + if (mode == SN2400_CHARGE_NORMAL && charger->mode != mode && + sn2400_charge_control_active(charger) && + (!fresh || charger->low)) + dev_warn(charger->dev, + "battery %s: charging as normal despite charge control\n", + fresh ? "low" : "capacity unknown"); + dev_info(charger->dev, + "charge %s at %d %%: 0x0d %#04x, 0x0e %#04x, 0x0f %#04x\n", + mode == SN2400_CHARGE_DISCHARGE ? "off, input off" : + mode == SN2400_CHARGE_INHIBIT ? "inhibited" : "on", + fresh ? charger->capacity : -1, charger->charge_reg, + current_reg, voltage_reg); +} + +/* + * 0x0d: the charge-current cap, or 0 to stop charging with the input on. The + * suspend handler's choice is quiet: it is undone at resume. + */ +static int sn2400_apply_charge_mode(struct sn2400_charger *charger, + enum sn2400_charge_mode mode, bool quiet) +{ + unsigned int charge_ua; + int reg; + int ret; + + if (!charger->charge_control) + return 0; + + mutex_lock(&charger->lock); + charge_ua = min(charger->charge_ua, charger->max_charge_ua); + mutex_unlock(&charger->lock); + + reg = mode == SN2400_CHARGE_NORMAL ? + charge_ua / SN2400_INPUT_REG_STEP_UA : 0; + + /* A held charge must stay held: check that nothing reset 0x0d. */ + if (mode != SN2400_CHARGE_NORMAL && charger->charge_reg >= 0) { + unsigned int now; + + if (!regmap_read(charger->regmap, SN2400_REG_CHARGE_LIMIT, + &now) && now != charger->charge_reg) { + dev_warn_ratelimited(charger->dev, + "charge limit %#04x became %#04x\n", + charger->charge_reg, now); + charger->charge_reg = -1; + } + } + + if (reg != charger->charge_reg) { + ret = regmap_write(charger->regmap, SN2400_REG_CHARGE_LIMIT, + reg); + if (ret) { + charger->charge_reg = -1; + return ret; + } + charger->charge_reg = reg; + } + + if (!quiet && mode != charger->mode) { + sn2400_log_charge_mode(charger, mode); + WRITE_ONCE(charger->mode, mode); + if (charger->psy) + power_supply_changed(charger->psy); + } + + return 0; +} + static void sn2400_log_usb_host(struct sn2400_charger *charger) { unsigned int grant = READ_ONCE(charger->usb_grant_ua); @@ -539,6 +827,7 @@ static void sn2400_charger_work(struct work_struct *work) container_of(to_delayed_work(work), struct sn2400_charger, work); unsigned int input_limit_ua = charger->input_limit_ua; unsigned int next_poll_ms = SN2400_STABLE_POLL_MS; + enum sn2400_charge_mode mode; unsigned int status; int ibus_ua = 0; int vbus_uv = 0; @@ -548,6 +837,10 @@ static void sn2400_charger_work(struct work_struct *work) if (ret) goto fail_safe; + /* Give the battery its charge thresholds even before a cable comes. */ + if (charger->thermal || charger->charge_control) + sn2400_find_gauge(charger); + if (!(status & SN2400_STATUS_VBUS)) { if (input_limit_ua) { ret = sn2400_disable_input(charger); @@ -557,6 +850,9 @@ static void sn2400_charger_work(struct work_struct *work) } charger->state = SN2400_FOLDBACK_IDLE; charger->target_limit_ua = 0; + /* Write the charge limit again for the next cable. */ + if (charger->charge_control) + charger->charge_reg = -1; /* A new cable needs a new configuration to count as a host. */ WRITE_ONCE(charger->usb_configured, false); if (!charger->vbus_absent) { @@ -579,17 +875,26 @@ static void sn2400_charger_work(struct work_struct *work) charger->vbus_absent = false; sn2400_log_usb_host(charger); - if (sn2400_update_thermal(charger) && - charger->thermal_level == SN2400_THERMAL_OFF) { - ret = sn2400_disable_input(charger); - if (ret) - goto fail_safe; + sn2400_update_gauge(charger); + mode = sn2400_charge_mode(charger); + ret = sn2400_apply_charge_mode(charger, mode, false); + if (ret) + goto fail_safe; + + /* + * A hot or cold battery, or force-discharge: no input, the phone runs + * on its battery. Off until allowed again, then probe anew. + */ + if (charger->thermal_level == SN2400_THERMAL_OFF || + mode == SN2400_CHARGE_DISCHARGE) { + if (!charger->input_off) { + ret = sn2400_disable_input(charger); + if (ret) + goto fail_safe; + } input_limit_ua = 0; - /* Off until the battery is back in range, then probe anew. */ charger->state = SN2400_FOLDBACK_IDLE; charger->target_limit_ua = 0; - } - if (charger->thermal_level == SN2400_THERMAL_OFF) { sn2400_cache(charger, 0, status, 0, 0, 0); goto out_schedule; } @@ -762,6 +1067,9 @@ static void sn2400_put_battery(void *data) { struct sn2400_charger *charger = data; + if (charger->ext_registered) + power_supply_unregister_extension(charger->gauge, + &sn2400_battery_ext); if (charger->gauge) power_supply_put(charger->gauge); fwnode_handle_put(charger->battery); @@ -780,8 +1088,15 @@ static int sn2400_charger_get_property(struct power_supply *psy, ret = charger->status_error; if (ret) break; - if (!(charger->status & SN2400_STATUS_VBUS)) + /* + * The status bit stays set while charge control holds the + * charge (0x0d = 0): report what the driver applied. + */ + if (!(charger->status & SN2400_STATUS_VBUS) || + READ_ONCE(charger->mode) == SN2400_CHARGE_DISCHARGE) val->intval = POWER_SUPPLY_STATUS_DISCHARGING; + else if (READ_ONCE(charger->mode) == SN2400_CHARGE_INHIBIT) + val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; else if (charger->status & SN2400_STATUS_CHARGING) val->intval = POWER_SUPPLY_STATUS_CHARGING; else @@ -823,6 +1138,12 @@ static int sn2400_charger_get_property(struct power_supply *psy, case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: val->intval = charger->max_charge_ua; break; + case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: + val->intval = charger->charge_ua; + break; + case POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR: + val->intval = charger->behaviour; + break; case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: val->intval = charger->usb_grant_ua; break; @@ -842,9 +1163,92 @@ static const enum power_supply_property sn2400_charger_properties[] = { POWER_SUPPLY_PROP_CURRENT_NOW, POWER_SUPPLY_PROP_CURRENT_MAX, POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, + POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT, + POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR, POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, }; +/* + * Userspace charge control, shared by the charger and the gauge's battery. + * The work applies it at once; the limits are checked here. + */ +static int sn2400_set_charge_control(struct sn2400_charger *charger, + enum power_supply_property property, + int value) +{ + if (!charger->charge_control) + return -EPERM; + + switch (property) { + case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: + /* Never above the battery's limit, iOS's own ceiling. */ + if (value < SN2400_CHARGE_MIN_UA || + value > charger->max_charge_ua) + return -EINVAL; + value -= value % SN2400_INPUT_REG_STEP_UA; + break; + case POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR: + if (value < 0 || + value > POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE || + !(SN2400_CHARGE_BEHAVIOURS & BIT(value))) + return -EINVAL; + /* Holding the charge needs the battery's capacity. */ + if (value != POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO && + !charger->battery) + return -ENODEV; + break; + case POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD: + if (value < 0 || value > 100) + return -EINVAL; + break; + case POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD: + if (value < SN2400_END_THRESHOLD_MIN || value > 100) + return -EINVAL; + break; + default: + return -EINVAL; + } + + mutex_lock(&charger->lock); + switch (property) { + case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: + charger->charge_ua = value; + break; + case POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR: + charger->behaviour = value; + break; + case POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD: + charger->start_threshold = value; + break; + default: + charger->end_threshold = value; + break; + } + /* Read the capacity and apply the change at the next run, now. */ + WRITE_ONCE(charger->thermal_checked, 0); + if (!charger->stopped) + mod_delayed_work(system_percpu_wq, &charger->work, 0); + mutex_unlock(&charger->lock); + + power_supply_changed(charger->psy); + return 0; +} + +static int sn2400_charger_property_is_writeable(struct power_supply *psy, + enum power_supply_property property) +{ + struct sn2400_charger *charger = power_supply_get_drvdata(psy); + + /* input_current_limit stays the USB controller's, kernel only. */ + switch (property) { + case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: + case POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR: + return charger->charge_control_allowed; + default: + return 0; + } +} + /* * The USB device controller reports what the host allows (usb-psy-name): * 0 without a session, 100 mA unconfigured, bMaxPower when configured, more @@ -859,7 +1263,7 @@ static int sn2400_charger_set_property(struct power_supply *psy, unsigned int grant; if (property != POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT) - return -EINVAL; + return sn2400_set_charge_control(charger, property, val->intval); grant = max(val->intval, 0); mutex_lock(&charger->lock); @@ -888,10 +1292,73 @@ static const struct power_supply_desc sn2400_charger_desc = { .num_properties = ARRAY_SIZE(sn2400_charger_properties), .get_property = sn2400_charger_get_property, .set_property = sn2400_charger_set_property, + .property_is_writeable = sn2400_charger_property_is_writeable, + .charge_behaviours = SN2400_CHARGE_BEHAVIOURS, +}; + +/* The gauge's battery: charge thresholds and charge_behaviour (UPower, TLP). */ +static int sn2400_battery_ext_get(struct power_supply *psy, + const struct power_supply_ext *ext, + void *data, enum power_supply_property property, + union power_supply_propval *val) +{ + struct sn2400_charger *charger = data; + + mutex_lock(&charger->lock); + switch (property) { + case POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD: + val->intval = charger->start_threshold; + break; + case POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD: + val->intval = charger->end_threshold; + break; + case POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR: + val->intval = charger->behaviour; + break; + default: + mutex_unlock(&charger->lock); + return -EINVAL; + } + mutex_unlock(&charger->lock); + + return 0; +} + +static int sn2400_battery_ext_set(struct power_supply *psy, + const struct power_supply_ext *ext, + void *data, enum power_supply_property property, + const union power_supply_propval *val) +{ + return sn2400_set_charge_control(data, property, val->intval); +} + +static int sn2400_battery_ext_is_writeable(struct power_supply *psy, + const struct power_supply_ext *ext, + void *data, + enum power_supply_property property) +{ + return 1; +} + +static const enum power_supply_property sn2400_battery_ext_properties[] = { + POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD, + POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD, + POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR, +}; + +static const struct power_supply_ext sn2400_battery_ext = { + .name = "apple_sn2400_charger", + .charge_behaviours = SN2400_CHARGE_BEHAVIOURS, + .properties = sn2400_battery_ext_properties, + .num_properties = ARRAY_SIZE(sn2400_battery_ext_properties), + .get_property = sn2400_battery_ext_get, + .set_property = sn2400_battery_ext_set, + .property_is_writeable = sn2400_battery_ext_is_writeable, }; static int sn2400_charger_stop(struct sn2400_charger *charger) { + int max_reg; bool changed; int ret; @@ -903,6 +1370,15 @@ static int sn2400_charger_stop(struct sn2400_charger *charger) if (!charger->control) return 0; + /* + * Leave the charge limit as probe set it: a 0 would survive into + * iBoot and the next kernel, and the phone would not charge. + */ + max_reg = charger->max_charge_ua / SN2400_INPUT_REG_STEP_UA; + if (charger->charge_control && charger->charge_reg != max_reg && + regmap_write(charger->regmap, SN2400_REG_CHARGE_LIMIT, max_reg)) + dev_err(charger->dev, "failed to restore the charge limit\n"); + ret = sn2400_disable_input(charger); mutex_lock(&charger->lock); changed = charger->status_error != ret || @@ -1031,7 +1507,8 @@ static int sn2400_charger_probe(struct platform_device *pdev) struct sn2400_charger *charger; struct device *hwmon; u32 input_limit_ua; - unsigned int status; + unsigned int status, boot_reg; + bool keep; int ret; charger = devm_kzalloc(&pdev->dev, sizeof(*charger), GFP_KERNEL); @@ -1066,6 +1543,9 @@ static int sn2400_charger_probe(struct platform_device *pdev) charger->session_max_input_ua = input_limit_ua; charger->vbus_since = jiffies; charger->usb_host_logged = -1; + charger->charge_reg = -1; + charger->end_threshold = 100; + charger->hold = true; ret = devm_mutex_init(&pdev->dev, &charger->adc_lock); if (ret) @@ -1094,6 +1574,14 @@ static int sn2400_charger_probe(struct platform_device *pdev) charger->status = status; charger->status_error = 0; + /* + * The power-supply core sets the sysfs modes from + * property_is_writeable() at registration, before the charge limit is + * programmed below: decide now whether userspace may change it. + */ + keep = device_property_read_bool(&pdev->dev, "apple,keep-charge-limit"); + charger->charge_control_allowed = charger->control && !keep; + psy_config.drv_data = charger; psy_config.fwnode = dev_fwnode(&pdev->dev); psy_config.no_wakeup_source = true; @@ -1121,17 +1609,16 @@ static int sn2400_charger_probe(struct platform_device *pdev) battery_info->temp_alert_min != INT_MIN || battery_info->temp_alert_max != INT_MAX; power_supply_put_battery_info(charger->psy, battery_info); + /* The gauge of the same battery: temperature and capacity. */ + charger->battery = fwnode_find_reference(dev_fwnode(&pdev->dev), + "monitored-battery", 0); + if (IS_ERR(charger->battery)) + return dev_err_probe(&pdev->dev, PTR_ERR(charger->battery), + "no monitored battery\n"); + ret = devm_add_action_or_reset(&pdev->dev, sn2400_put_battery, charger); + if (ret) + return ret; if (charger->thermal) { - charger->battery = fwnode_find_reference(dev_fwnode(&pdev->dev), - "monitored-battery", 0); - if (IS_ERR(charger->battery)) - return dev_err_probe(&pdev->dev, - PTR_ERR(charger->battery), - "no monitored battery\n"); - ret = devm_add_action_or_reset(&pdev->dev, sn2400_put_battery, - charger); - if (ret) - return ret; dev_info(&pdev->dev, "battery %d..%d C, full input %d..%d C\n", charger->temp_min / 10, charger->temp_max / 10, @@ -1168,7 +1655,11 @@ static int sn2400_charger_probe(struct platform_device *pdev) "read-only: status %#04x input %#04x control %#04x charge %#04x hdq %#04x\n", status, input_reg, control_reg, charge_reg, hdq_reg); /* Report the limit the bootloader left (D111 encoding: 10 mA steps). */ + if (!charge_reg) + dev_warn(&pdev->dev, + "charging is stopped (0x0d = 0), likely left by a crashed kernel; a controlling boot restores it\n"); charger->max_charge_ua = charge_reg * SN2400_INPUT_REG_STEP_UA; + charger->charge_ua = charger->max_charge_ua; sn2400_cache(charger, 0, status, 0, 0, sn2400_monitor_input_limit(control_reg, input_reg)); @@ -1178,16 +1669,24 @@ static int sn2400_charger_probe(struct platform_device *pdev) /* * "apple,keep-charge-limit": leave 0x0d as the bootloader set it, as * the iPhone 6s Plus's iOS driver does, and report what it holds. + * + * Except a 0, which is how charge control stops charging. Nothing + * restores it when a kernel dies with charging held (a panic or an + * emergency restart skips shutdown, and the I2C controller has no + * atomic transfers for a restart handler), and iBoot may not either. + * A controlling probe never keeps a 0, whatever the mode. */ - if (device_property_read_bool(&pdev->dev, "apple,keep-charge-limit")) { - unsigned int charge_reg; - - ret = regmap_read(charger->regmap, SN2400_REG_CHARGE_LIMIT, - &charge_reg); - if (ret) - return dev_err_probe(&pdev->dev, ret, - "failed to read charge limit\n"); - charger->max_charge_ua = charge_reg * SN2400_INPUT_REG_STEP_UA; + ret = regmap_read(charger->regmap, SN2400_REG_CHARGE_LIMIT, &boot_reg); + if (ret) + return dev_err_probe(&pdev->dev, ret, + "failed to read charge limit\n"); + if (!boot_reg) + dev_warn(&pdev->dev, + "charging was left stopped (0x0d = 0), likely by a crashed kernel: restoring %u mA\n", + charger->max_charge_ua / 1000); + + if (keep && boot_reg) { + charger->max_charge_ua = boot_reg * SN2400_INPUT_REG_STEP_UA; } else { ret = regmap_write(charger->regmap, SN2400_REG_CHARGE_LIMIT, charger->max_charge_ua / @@ -1195,7 +1694,12 @@ static int sn2400_charger_probe(struct platform_device *pdev) if (ret) return dev_err_probe(&pdev->dev, ret, "failed to program battery charge current limit\n"); + /* A keep-charge-limit board only gets its 0 replaced. */ + charger->charge_reg = + charger->max_charge_ua / SN2400_INPUT_REG_STEP_UA; + charger->charge_control = !keep; } + charger->charge_ua = charger->max_charge_ua; hwmon = devm_hwmon_device_register_with_info(&pdev->dev, "sn2400", charger, @@ -1233,6 +1737,7 @@ static void sn2400_charger_shutdown(struct platform_device *pdev) static int sn2400_charger_suspend(struct device *dev) { struct sn2400_charger *charger = dev_get_drvdata(dev); + enum sn2400_charge_mode mode; unsigned int input_limit_ua; unsigned int status; int ret; @@ -1249,9 +1754,22 @@ static int sn2400_charger_suspend(struct device *dev) if (ret) goto restart; - /* Asleep the work does not run: a hot or cold battery gets no input. */ + /* + * Asleep the work does not run. Hold the charge from the start + * threshold up, so a night on a charger does not pass the end + * threshold; below it the phone charges and may. + */ + mode = sn2400_charge_mode(charger); + if (mode == SN2400_CHARGE_NORMAL && sn2400_in_band(charger)) + mode = SN2400_CHARGE_INHIBIT; + ret = sn2400_apply_charge_mode(charger, mode, true); + if (ret) + goto restart; + + /* A hot or cold battery, or force-discharge, gets no input. */ if ((status & SN2400_STATUS_VBUS) && - charger->thermal_level != SN2400_THERMAL_OFF) { + charger->thermal_level != SN2400_THERMAL_OFF && + mode != SN2400_CHARGE_DISCHARGE) { input_limit_ua = min(charger->session_max_input_ua, SN2400_INPUT_START_UA); ret = sn2400_set_input_limit(charger, input_limit_ua, @@ -1275,6 +1793,12 @@ static int sn2400_charger_resume(struct device *dev) { struct sn2400_charger *charger = dev_get_drvdata(dev); + /* + * jiffies stood still asleep: the last capacity is as old as the + * sleep. Read the gauge before deciding anything. + */ + charger->capacity_read = false; + WRITE_ONCE(charger->thermal_checked, 0); sn2400_charger_start(charger, -EAGAIN); return 0;