From c98d5be08caa9ba0a4f3b2bd612382ba98702d85 Mon Sep 17 00:00:00 2001 From: Henrik Klee Date: Sat, 5 Dec 2026 05:20:49 +0800 Subject: [PATCH 1/2] power: supply: apple_tristar_charger: Move input sag detection into a chip hook The VBUS droop backoff ran inline in the poll loop and only knew the Orion VBUS ADC. Boards without one (the A10's J127/J128, for example) can still tell a sagging source, from the PMIC's input-collapse state, but had no place to plug that in. Add an input_sag() hook to tristar_chip_info that reports none, unsure, droop or fault, and act on it in one shared place, called from the ramp and the guard. The VBUS ADC reader becomes the hook of both PMICs; it does nothing on boards whose device tree gives no VBUS channel. The thresholds, the read retries and the one-step backoff are unchanged. Assisted-by: LLM Signed-off-by: Henrik Klee --- drivers/power/supply/apple_tristar_charger.c | 171 ++++++++++++------- drivers/power/supply/apple_tristar_d2355.h | 1 + 2 files changed, 112 insertions(+), 60 deletions(-) diff --git a/drivers/power/supply/apple_tristar_charger.c b/drivers/power/supply/apple_tristar_charger.c index 54fe3a01e77626..f9a47910aa42c2 100644 --- a/drivers/power/supply/apple_tristar_charger.c +++ b/drivers/power/supply/apple_tristar_charger.c @@ -68,8 +68,24 @@ struct tristar_charger; struct tristar_pack; +/* How the source holds up under the present grant, as a chip can tell. */ +enum tristar_sag { + TRISTAR_SAG_NONE, /* the input holds */ + TRISTAR_SAG_UNSURE, /* no usable sample this time: hold the step */ + TRISTAR_SAG_DROOP, /* the source sags: back off a step */ + TRISTAR_SAG_FAULT, /* outside the safe range: fall back */ +}; + +static enum tristar_sag tristar_vbus_sag(struct tristar_charger *chg); + /* Keep shared charging policy independent of the PMIC input-limit registers. */ struct tristar_chip_info { + /* + * Optional: tell input sag while a grant is above the fallback or + * aims above 1 A. A board with a VBUS ADC reads it; one without + * could report the PMIC's input-collapse state instead. + */ + enum tristar_sag (*input_sag)(struct tristar_charger *chg); int (*prepare)(struct tristar_charger *chg); bool (*pack_allows)(struct tristar_charger *chg, const struct tristar_pack *pack); unsigned int status_size, control_size; @@ -261,9 +277,9 @@ struct tristar_charger { u32 adc_base; unsigned int max_ma; struct iio_channel *vbus; - unsigned int vbus_limit_ma; + unsigned int sag_limit_ma; /* input cap found by sag, 0 for none */ unsigned int vbus_read_errors; - bool vbus_fault; + bool input_fault; /* Serializes policy state and PMIC/Tristar sequences. */ struct mutex lock; @@ -350,6 +366,7 @@ static int tristar_d2365_read_status(struct tristar_charger *chg, } static const struct tristar_chip_info tristar_d2365_chip = { + .input_sag = tristar_vbus_sag, .adc_error_mask = 0x80, .read_limit = tristar_d2365_read_limit, .write_limit = tristar_d2365_write_limit, @@ -900,8 +917,8 @@ static void tristar_set_usb(struct tristar_charger *chg, bool on) static unsigned int tristar_target_code(struct tristar_charger *chg) { unsigned int ma = max(chg->source_ma, tristar_usb_ma(chg)); - unsigned int cap = chg->vbus_limit_ma ? - min(chg->max_ma, chg->vbus_limit_ma) : chg->max_ma; + unsigned int cap = chg->sag_limit_ma ? + min(chg->max_ma, chg->sag_limit_ma) : chg->max_ma; if (ma <= tristar_code_ma(chg, chg->chip->fallback_code)) return chg->chip->fallback_code; @@ -938,9 +955,9 @@ static void tristar_new_attachment(struct tristar_charger *chg, u64 now) /* Before anything else: a host must not start a session yet. */ tristar_set_usb(chg, false); chg->present = true; - chg->vbus_limit_ma = 0; + chg->sag_limit_ma = 0; chg->vbus_read_errors = 0; - chg->vbus_fault = false; + chg->input_fault = false; chg->phase = TRISTAR_SETTLING; chg->attach_ns = now; chg->hold_ns = now; @@ -1080,12 +1097,93 @@ static bool tristar_debouncing(struct tristar_charger *chg, u64 now) (s64)(now - line_ns) < (s64)TRISTAR_EVENT_DEBOUNCE_MS * NSEC_PER_MSEC; } +/* + * VBUS at the connector, after the cable's and adapter's IR drop, for boards + * whose PMIC samples it. A failed read holds the step a few times before it + * counts as a fault. + */ +static enum tristar_sag tristar_vbus_sag(struct tristar_charger *chg) +{ + int mv, ret; + + if (!IS_REACHABLE(CONFIG_IIO) || !chg->vbus) + return TRISTAR_SAG_NONE; + ret = iio_read_channel_processed(chg->vbus, &mv); + if (ret) { + if (++chg->vbus_read_errors >= TRISTAR_VBUS_READ_RETRIES) { + dev_warn(chg->dev, "VBUS unreadable (error %d after %u attempts)\n", + ret, chg->vbus_read_errors); + return TRISTAR_SAG_FAULT; + } + dev_dbg(chg->dev, "VBUS read error %d (%u/%u), holding step\n", + ret, chg->vbus_read_errors, TRISTAR_VBUS_READ_RETRIES); + return TRISTAR_SAG_UNSURE; + } + chg->vbus_read_errors = 0; + if (chg->phase == TRISTAR_RAMPING) + dev_dbg(chg->dev, "VBUS %d mV at %u mA (target %u mA)\n", mv, + tristar_code_ma(chg, chg->code), + tristar_code_ma(chg, chg->target_code)); + if (mv < TRISTAR_VBUS_FAULT_MV || mv > TRISTAR_VBUS_MAX_MV) { + dev_warn(chg->dev, "VBUS %d mV outside the safe range\n", mv); + return TRISTAR_SAG_FAULT; + } + if (mv < TRISTAR_VBUS_DROOP_MV) { + dev_dbg(chg->dev, "VBUS droop: %d mV\n", mv); + return TRISTAR_SAG_DROOP; + } + return TRISTAR_SAG_NONE; +} + +/* + * Ask the chip how the source holds up while there is a grant worth + * watching: a fault falls back, a sag backs off one step and caps the input + * for this attachment. Returns true when the ramp or guard must not go on + * this time. Caller holds lock. + */ +static bool tristar_check_sag(struct tristar_charger *chg) +{ + unsigned int cur_ma = tristar_code_ma(chg, chg->code); + unsigned int fallback_ma = tristar_code_ma(chg, chg->chip->fallback_code); + + if (!chg->chip->input_sag || chg->input_fault) + return false; + if (cur_ma <= fallback_ma && tristar_code_ma(chg, chg->target_code) <= 1000) + return false; + + switch (chg->chip->input_sag(chg)) { + case TRISTAR_SAG_NONE: + return false; + case TRISTAR_SAG_UNSURE: + return true; + case TRISTAR_SAG_FAULT: + chg->input_fault = true; + chg->phase = TRISTAR_FAULT; + tristar_withdraw(chg, chg->chip->fallback_code); + dev_warn(chg->dev, "input guard tripped; holding the fallback\n"); + return true; + case TRISTAR_SAG_DROOP: + chg->sag_limit_ma = cur_ma > fallback_ma + TRISTAR_RAMP_STEP_MA ? + cur_ma - TRISTAR_RAMP_STEP_MA : fallback_ma; + chg->target_code = tristar_target_code(chg); + tristar_withdraw(chg, chg->target_code); + chg->phase = TRISTAR_CHARGING; + dev_info(chg->dev, "input sags at %u mA; capping input to %u mA\n", + cur_ma, tristar_code_ma(chg, chg->target_code)); + return true; + } + return false; +} + static void tristar_ramp_step(struct tristar_charger *chg, u64 now) { struct tristar_pack pack; - unsigned int next, from_ma = tristar_code_ma(chg, chg->code); + unsigned int next, from_ma; u64 wait; + if (tristar_check_sag(chg)) + return; + from_ma = tristar_code_ma(chg, chg->code); if (tristar_debouncing(chg, now) || (chg->step_ns && now - chg->step_ns < chg->step_wait_ns)) return; @@ -1186,6 +1284,8 @@ static void tristar_guard(struct tristar_charger *chg, u64 now) { struct tristar_pack pack; + if (tristar_check_sag(chg)) + return; if (now - chg->guard_ns < NSEC_PER_SEC) return; chg->guard_ns = now; @@ -1291,7 +1391,7 @@ static void tristar_poll(struct work_struct *work) struct tristar_charger *chg = container_of(to_delayed_work(work), struct tristar_charger, poll_work); unsigned int status, old_code; - bool old_online, old_present, old_usb_on, vbus_hold = false; + bool old_online, old_present, old_usb_on; u64 now = ktime_get_ns(), line_ns; mutex_lock(&chg->lock); @@ -1316,7 +1416,7 @@ static void tristar_poll(struct work_struct *work) chg->phase = TRISTAR_EMPTY; chg->source[0] = '\0'; chg->source_ma = 0; - chg->vbus_limit_ma = 0; + chg->sag_limit_ma = 0; chg->vbus_read_errors = 0; chg->usb_ma = 0; chg->usb_type = POWER_SUPPLY_USB_TYPE_UNKNOWN; @@ -1337,55 +1437,7 @@ static void tristar_poll(struct work_struct *work) tristar_new_attachment(chg, now); goto again; } - if (IS_REACHABLE(CONFIG_IIO) && chg->vbus && !chg->vbus_fault && - (chg->phase == TRISTAR_RAMPING || chg->phase == TRISTAR_CHARGING)) { - unsigned int cur_ma = tristar_code_ma(chg, chg->code); - unsigned int target_ma = tristar_code_ma(chg, chg->target_code); - unsigned int fallback_ma = tristar_code_ma(chg, chg->chip->fallback_code); - - if (cur_ma > fallback_ma || target_ma > 1000) { - int mv, ret; - - ret = iio_read_channel_processed(chg->vbus, &mv); - if (ret) { - if (++chg->vbus_read_errors >= TRISTAR_VBUS_READ_RETRIES) { - chg->vbus_fault = true; - chg->phase = TRISTAR_FAULT; - dev_warn(chg->dev, - "VBUS guard tripped (sample 0 mV, error %d after %u attempts)\n", - ret, chg->vbus_read_errors); - } else { - dev_dbg(chg->dev, - "VBUS read error %d (%u/%u), holding step\n", - ret, chg->vbus_read_errors, - TRISTAR_VBUS_READ_RETRIES); - vbus_hold = true; - } - } else { - chg->vbus_read_errors = 0; - if (chg->phase == TRISTAR_RAMPING) - dev_dbg(chg->dev, "VBUS %d mV at %u mA (target %u mA)\n", - mv, cur_ma, target_ma); - if (mv < TRISTAR_VBUS_FAULT_MV || mv > TRISTAR_VBUS_MAX_MV) { - chg->vbus_fault = true; - chg->phase = TRISTAR_FAULT; - dev_warn(chg->dev, "VBUS guard tripped (sample %d mV, error 0)\n", - mv); - } else if (mv < TRISTAR_VBUS_DROOP_MV) { - unsigned int limit_ma = cur_ma > fallback_ma + TRISTAR_RAMP_STEP_MA ? - cur_ma - TRISTAR_RAMP_STEP_MA : fallback_ma; - - chg->vbus_limit_ma = limit_ma; - chg->target_code = tristar_target_code(chg); - tristar_withdraw(chg, chg->target_code); - chg->phase = TRISTAR_CHARGING; - dev_info(chg->dev, "VBUS droop (%d mV at %u mA); capping input to %u mA\n", - mv, cur_ma, tristar_code_ma(chg, chg->target_code)); - } - } - } - } - if (chg->vbus_fault) + if (chg->input_fault) tristar_withdraw(chg, chg->chip->fallback_code); if (chg->phase == TRISTAR_FAULT) { /* USB data must not depend on charging working. */ @@ -1418,8 +1470,7 @@ static void tristar_poll(struct work_struct *work) tristar_run_identification(chg, now); break; case TRISTAR_RAMPING: - if (!vbus_hold) - tristar_ramp_step(chg, now); + tristar_ramp_step(chg, now); break; case TRISTAR_CHARGING: tristar_guard(chg, now); diff --git a/drivers/power/supply/apple_tristar_d2355.h b/drivers/power/supply/apple_tristar_d2355.h index 9a42d1c82b7b8f..ead158e4a5f9be 100644 --- a/drivers/power/supply/apple_tristar_d2355.h +++ b/drivers/power/supply/apple_tristar_d2355.h @@ -258,6 +258,7 @@ static int tristar_d2355_prepare(struct tristar_charger *chg) } static const struct tristar_chip_info tristar_d2355_chip = { + .input_sag = tristar_vbus_sag, .prepare = tristar_d2355_prepare, .pack_allows = tristar_d2355_pack_allows, .status_size = 10, From c101a4dc54cffaa211ff2224e99b650330068c93 Mon Sep 17 00:00:00 2001 From: Henrik Klee Date: Sat, 5 Dec 2026 05:21:02 +0800 Subject: [PATCH 2/2] power: supply: apple_tristar_charger: Confirm input sag before lowering the limit One VBUS sample below 4.5 V lowered the input limit for the rest of the attachment. A short dip during a load step, or one noisy conversion, is enough for that, and the attachment then charges slower until unplug. Hold the step on the first sag sample and lower the limit only when the next sample still shows it, as the SN2400 driver's foldback debounce does. Faults (overvoltage, brownout, a VBUS still unreadable after its retries) fall back at once as before. Assisted-by: LLM Signed-off-by: Henrik Klee --- drivers/power/supply/apple_tristar_charger.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/drivers/power/supply/apple_tristar_charger.c b/drivers/power/supply/apple_tristar_charger.c index f9a47910aa42c2..54a9023d77cf90 100644 --- a/drivers/power/supply/apple_tristar_charger.c +++ b/drivers/power/supply/apple_tristar_charger.c @@ -279,6 +279,7 @@ struct tristar_charger { struct iio_channel *vbus; unsigned int sag_limit_ma; /* input cap found by sag, 0 for none */ unsigned int vbus_read_errors; + bool sag_seen; /* one sag sample, awaiting a second */ bool input_fault; /* Serializes policy state and PMIC/Tristar sequences. */ @@ -957,6 +958,7 @@ static void tristar_new_attachment(struct tristar_charger *chg, u64 now) chg->present = true; chg->sag_limit_ma = 0; chg->vbus_read_errors = 0; + chg->sag_seen = false; chg->input_fault = false; chg->phase = TRISTAR_SETTLING; chg->attach_ns = now; @@ -1138,20 +1140,30 @@ static enum tristar_sag tristar_vbus_sag(struct tristar_charger *chg) /* * Ask the chip how the source holds up while there is a grant worth * watching: a fault falls back, a sag backs off one step and caps the input - * for this attachment. Returns true when the ramp or guard must not go on - * this time. Caller holds lock. + * for this attachment. A sag must show in two samples in a row before the + * limit is lowered, as the SN2400 driver's foldback debounce does: a single + * low sample during a load step must not cost the attachment its current. + * Returns true when the ramp or guard must not go on this time. Caller + * holds lock. */ static bool tristar_check_sag(struct tristar_charger *chg) { unsigned int cur_ma = tristar_code_ma(chg, chg->code); unsigned int fallback_ma = tristar_code_ma(chg, chg->chip->fallback_code); + enum tristar_sag sag; if (!chg->chip->input_sag || chg->input_fault) return false; if (cur_ma <= fallback_ma && tristar_code_ma(chg, chg->target_code) <= 1000) return false; - switch (chg->chip->input_sag(chg)) { + sag = chg->chip->input_sag(chg); + if (sag == TRISTAR_SAG_DROOP && !chg->sag_seen) { + chg->sag_seen = true; + return true; + } + chg->sag_seen = false; + switch (sag) { case TRISTAR_SAG_NONE: return false; case TRISTAR_SAG_UNSURE: @@ -1418,6 +1430,7 @@ static void tristar_poll(struct work_struct *work) chg->source_ma = 0; chg->sag_limit_ma = 0; chg->vbus_read_errors = 0; + chg->sag_seen = false; chg->usb_ma = 0; chg->usb_type = POWER_SUPPLY_USB_TYPE_UNKNOWN; if (tristar_set_code(chg, chg->chip->handoff_code))