From 0d68188c380e3087be13813e7f0364a598806123 Mon Sep 17 00:00:00 2001 From: Lef Date: Sat, 14 Nov 2026 13:06:18 +0200 Subject: [PATCH 1/9] power: supply: max77759: add a guard table per board and one for husky The temperature guard takes its voltage tiers and its charge currents from three constant arrays, the step-charging table of the gs201 phones. The Pixel 8 Pro (husky) has another battery and another table: four tiers, 4.20/4.30/4.35/4.48 V, instead of three. Put the tiers and the currents in a struct guard_table, with the number of tiers in use, and let the driver pick the table at probe. The default table holds the numbers the arrays held, so nothing changes for the Pixel 7 Pro, the Pixel 6 Pro and every other board. A machine compatible with "google,zuma-husky" gets the husky table: the chg-cv-limits and chg-cc-limits of the google,battery node in Google's tree (kernel/devices/google/shusky, branch android-gs-shusky-6.1-android16): dts/zuma-shusky-battery.dtsi for the temperature and voltage limits, dts/zuma-husky-battery.dtsi for the 4950 mAh and the six rows, in percent of that design capacity. The temperature bands are the same in both and stay as they are. The tier and limit arithmetic of the guard work moves into two functions without side effects, guard_next_tier() and guard_limits(), so that it can be tested. The limits from the battery description still cap the float voltage and the charge current, and temp_guard is still off by default. With the husky battery node as it is today, 4.20 V and 1.485 A, the float stays 4.20 V in every band and the current 1.485 A, the lowest row of the husky table being 30 % of 4950 mAh. One case differs from the default table on that board: from 0 to 10 C with the battery at 4.20 V or more the current limit was 495 mA and is 1.485 A now, as the Google table has it. With the battery node's 1.485 A cap, every cell of the husky table that charges comes out at the cap, so the current does not change from band to band. What the table changes is the float per band (4.30 V at 0-10 C and 46-48 C, 4.20 V at 48-55 C), which only shows once the battery node asks for more than 4.20 V, and the one cold cell above. The same Google file also ages the table: google,aact-cc-limits lowers the 20-42 C row with use, and google,aafv-ref-offset lowers the float by up to 100 mV with cycles. That is not done here; it doesn't matter at 4.20 V and 0.3 C. Add KUnit tests, built into the driver with CHARGER_MAX77759_KUNIT_TEST: each band edge, the hysteresis, the tier steps of both tables, both tables against their numbers, and the limits with and without the caps of the battery description. They pass under UML (10 of 10), and the driver builds and links as an arm64 module, W=1 clean, with and without the tests. The MAINTAINERS entry of the driver now matches the test file too. Not run on a phone, neither a Pixel 8 Pro nor a Pixel 7 Pro. The choice of the table by the machine compatible is not covered by the tests. Signed-off-by: Lef --- MAINTAINERS | 2 +- drivers/power/supply/Kconfig | 15 + drivers/power/supply/max77759_charger.c | 131 ++++++-- drivers/power/supply/max77759_charger_test.c | 318 +++++++++++++++++++ 4 files changed, 436 insertions(+), 30 deletions(-) create mode 100644 drivers/power/supply/max77759_charger_test.c diff --git a/MAINTAINERS b/MAINTAINERS index a44d4a1fbe..c683560cca 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -16206,7 +16206,7 @@ MAXIM MAX77759 BATTERY CHARGER DRIVER M: Amit Sunil Dhamne L: linux-kernel@vger.kernel.org S: Maintained -F: drivers/power/supply/max77759_charger.c +F: drivers/power/supply/max77759_charger* MAXIM MAX77779 FUEL GAUGE AND CHARGER DRIVERS M: W4HO <87742609+W4HO-ham@users.noreply.github.com> diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig index 1e63a06a74..ff0d96fd18 100644 --- a/drivers/power/supply/Kconfig +++ b/drivers/power/supply/Kconfig @@ -678,6 +678,21 @@ config CHARGER_MAX77759 If built as a module, it will be called max77759_charger. +config CHARGER_MAX77759_KUNIT_TEST + bool "KUnit tests for the MAX77759 charger temperature guard" if !KUNIT_ALL_TESTS + depends on CHARGER_MAX77759 && KUNIT + depends on KUNIT=y || CHARGER_MAX77759=m + default KUNIT_ALL_TESTS + help + KUnit tests for the step-charging tables of the MAX77759 charger + driver's battery temperature guard: the temperature bands and + their hysteresis, the voltage tiers, the limits from the battery + description and the table of each board. + + The tests are built into the driver. They touch no hardware. + + If unsure, say N. + config CHARGER_MAX77976 tristate "Maxim MAX77976 battery charger driver" depends on I2C diff --git a/drivers/power/supply/max77759_charger.c b/drivers/power/supply/max77759_charger.c index a264e828f7..c5d6a10eeb 100644 --- a/drivers/power/supply/max77759_charger.c +++ b/drivers/power/supply/max77759_charger.c @@ -43,6 +43,9 @@ MODULE_PARM_DESC(default_ilim_ua, * Battery temperature guard: the step-charging policy of the stock phone * (Google's google,battery node on gs201 phones: chg-temp-limits, * chg-cv-limits, chg-cc-limits). Off by default. + * + * The temperature bands are the same on every board; the voltage tiers and + * the currents come from a table per board, see guard_select_table(). */ static bool temp_guard; module_param(temp_guard, bool, 0644); @@ -57,24 +60,57 @@ MODULE_PARM_DESC(guard_psy, "Battery power supply whose temp and voltage_now the #define GUARD_RETRY_MS 2000 /* while there is no reading */ #define GUARD_HYST_DECIC 20 /* 2.0 C into a band before relaxing */ #define GUARD_NBANDS 6 -#define GUARD_NTIERS 3 +#define GUARD_MAX_TIERS 4 /* Band i is [guard_temp_decic[i], guard_temp_decic[i + 1]), in 0.1 C */ static const int guard_temp_decic[GUARD_NBANDS + 1] = { 0, 100, 200, 420, 460, 480, 550 }; -/* Tier j charges up to guard_cv_mv[j] */ -static const u32 guard_cv_mv[GUARD_NTIERS] = { 4200, 4300, 4450 }; - -/* Charge current per band and tier, in percent of the design capacity */ -static const u8 guard_cc_pct[GUARD_NBANDS][GUARD_NTIERS] = { - { 30, 10, 0 }, /* 0-10 C */ - { 50, 30, 30 }, /* 10-20 C */ - { 100, 70, 50 }, /* 20-42 C */ - { 80, 50, 50 }, /* 42-46 C */ - { 50, 50, 0 }, /* 46-48 C */ - { 30, 0, 0 }, /* 48-55 C */ +/** + * struct guard_table - step-charging table of one battery + * @ntiers: number of voltage tiers in use, at most GUARD_MAX_TIERS + * @cv_mv: tier j charges up to cv_mv[j] + * @cc_pct: charge current per band and tier, in percent of the design + * capacity; 0 means the band does not charge in that tier + */ +struct guard_table { + int ntiers; + u32 cv_mv[GUARD_MAX_TIERS]; + u8 cc_pct[GUARD_NBANDS][GUARD_MAX_TIERS]; +}; + +/* Google's gs201 table, for every board without a table of its own */ +static const struct guard_table guard_table_default = { + .ntiers = 3, + .cv_mv = { 4200, 4300, 4450 }, + .cc_pct = { + { 30, 10, 0 }, /* 0-10 C */ + { 50, 30, 30 }, /* 10-20 C */ + { 100, 70, 50 }, /* 20-42 C */ + { 80, 50, 50 }, /* 42-46 C */ + { 50, 50, 0 }, /* 46-48 C */ + { 30, 0, 0 }, /* 48-55 C */ + }, +}; + +/* + * Pixel 8 Pro (husky): the google,battery node in Google's + * kernel/devices/google/shusky, branch android-gs-shusky-6.1-android16: + * temperature and voltage limits from dts/zuma-shusky-battery.dtsi, + * the 4950 mAh and the six rows from dts/zuma-husky-battery.dtsi. + */ +static const struct guard_table guard_table_husky = { + .ntiers = 4, + .cv_mv = { 4200, 4300, 4350, 4480 }, + .cc_pct = { + { 50, 30, 0, 0 }, /* 0-10 C */ + { 70, 30, 30, 30 }, /* 10-20 C */ + { 120, 120, 120, 50 }, /* 20-42 C */ + { 100, 100, 100, 50 }, /* 42-46 C */ + { 50, 50, 0, 0 }, /* 46-48 C */ + { 30, 0, 0, 0 }, /* 48-55 C */ + }, }; #define FOREACH_IRQ(S) \ @@ -139,6 +175,7 @@ struct max77759_charger { int guard_band; /* -1: charging inhibited */ bool guard_no_reading; /* inhibited for want of a reading */ int guard_tier; + const struct guard_table *guard_table; u32 fv_max_mv; /* limits from the battery description */ u32 cc_max_ua; u32 design_uah; @@ -826,22 +863,22 @@ static int guard_classify(int decic) } /* Charging allowed in a band, for comparing how restrictive two bands are */ -static unsigned int guard_band_score(int band) +static unsigned int guard_band_score(const struct guard_table *tbl, int band) { - return band < 0 ? 0 : guard_cc_pct[band][0]; + return band < 0 ? 0 : tbl->cc_pct[band][0]; } /* * Move to a more restrictive band at once; to a less restrictive one only * when the temperature is GUARD_HYST_DECIC inside it on both sides. */ -static int guard_next_band(int cur, int decic) +static int guard_next_band(const struct guard_table *tbl, int cur, int decic) { int band = guard_classify(decic); /* First reading (cur -2): take it as it is */ if (cur < -1 || band == cur || - guard_band_score(band) < guard_band_score(cur)) + guard_band_score(tbl, band) < guard_band_score(tbl, cur)) return band; if (guard_classify(decic - GUARD_HYST_DECIC) == band && guard_classify(decic + GUARD_HYST_DECIC) == band) @@ -850,16 +887,51 @@ static int guard_next_band(int cur, int decic) } /* Highest tier of a band that may charge at all */ -static int guard_top_tier(int band) +static int guard_top_tier(const struct guard_table *tbl, int band) { int t; - for (t = GUARD_NTIERS - 1; t > 0; t--) - if (guard_cc_pct[band][t]) + for (t = tbl->ntiers - 1; t > 0; t--) + if (tbl->cc_pct[band][t]) break; return t; } +/* + * Tiers only rise within a band, as the voltage does while charging: from + * @tier, step up past every tier whose voltage the battery has reached, up + * to the highest tier the band charges in. + */ +static int guard_next_tier(const struct guard_table *tbl, int band, int tier, + int uv) +{ + while (tier < tbl->ntiers - 1 && uv >= tbl->cv_mv[tier] * 1000) + tier++; + return min(tier, guard_top_tier(tbl, band)); +} + +/* + * Float voltage and charge current for a band and tier, both capped by the + * limits from the battery description. + */ +static void guard_limits(const struct guard_table *tbl, int band, int tier, + u32 design_uah, u32 fv_max_mv, u32 cc_max_ua, + u32 *fv_mv, u32 *cc_ua) +{ + u32 cc; + + cc = div_u64((u64)design_uah * tbl->cc_pct[band][tier], 100); + *fv_mv = min(tbl->cv_mv[guard_top_tier(tbl, band)], fv_max_mv); + *cc_ua = min(cc, cc_max_ua); +} + +static const struct guard_table *guard_select_table(void) +{ + if (of_machine_is_compatible("google,zuma-husky")) + return &guard_table_husky; + return &guard_table_default; +} + static int guard_read(int *decic, int *uv) { union power_supply_propval val; @@ -908,6 +980,7 @@ static void guard_work_item(struct work_struct *work) { struct max77759_charger *chg = container_of(work, struct max77759_charger, guard_work.work); + const struct guard_table *tbl = chg->guard_table; int decic = 0, uv = 0, band, tier; unsigned int next_ms = GUARD_PERIOD_MS; bool read_ok; @@ -935,7 +1008,7 @@ static void guard_work_item(struct work_struct *work) WRITE_ONCE(chg->guard_no_reading, !read_ok); if (!read_ok) next_ms = GUARD_RETRY_MS; - band = read_ok ? guard_next_band(chg->guard_band, decic) : -1; + band = read_ok ? guard_next_band(tbl, chg->guard_band, decic) : -1; if (band < 0) { if (chg->guard_band != -1 && !read_ok) @@ -950,17 +1023,12 @@ static void guard_work_item(struct work_struct *work) goto out; } - /* Tiers only rise within a band, as the voltage does while charging */ + /* A new band starts again from the lowest tier */ if (band != chg->guard_band) chg->guard_tier = 0; - tier = chg->guard_tier; - while (tier < GUARD_NTIERS - 1 && uv >= guard_cv_mv[tier] * 1000) - tier++; - tier = min(tier, guard_top_tier(band)); - - fv = min(guard_cv_mv[guard_top_tier(band)], chg->fv_max_mv); - cc = div_u64((u64)chg->design_uah * guard_cc_pct[band][tier], 100); - cc = min(cc, chg->cc_max_ua); + tier = guard_next_tier(tbl, band, chg->guard_tier, uv); + guard_limits(tbl, band, tier, chg->design_uah, chg->fv_max_mv, + chg->cc_max_ua, &fv, &cc); if (band != chg->guard_band || tier != chg->guard_tier) dev_info(chg->dev, @@ -1074,6 +1142,7 @@ static int max77759_charger_probe(struct platform_device *pdev) * unregisters the notifier before it cancels the work. */ chg->guard_band = -2; + chg->guard_table = guard_select_table(); ret = devm_delayed_work_autocancel(dev, &chg->guard_work, guard_work_item); if (ret) return dev_err_probe(dev, ret, "Failed to initialize guard work\n"); @@ -1120,6 +1189,10 @@ static struct platform_driver max77759_charger_driver = { }; module_platform_driver(max77759_charger_driver); +#if IS_ENABLED(CONFIG_CHARGER_MAX77759_KUNIT_TEST) +#include "max77759_charger_test.c" +#endif + MODULE_AUTHOR("Amit Sunil Dhamne "); MODULE_DESCRIPTION("Maxim MAX77759 charger driver"); MODULE_LICENSE("GPL"); diff --git a/drivers/power/supply/max77759_charger_test.c b/drivers/power/supply/max77759_charger_test.c new file mode 100644 index 0000000000..47ee96256c --- /dev/null +++ b/drivers/power/supply/max77759_charger_test.c @@ -0,0 +1,318 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * KUnit tests for the battery temperature guard of the MAX77759 charger + * driver: bands, hysteresis, tiers, limits and the tables themselves. + * + * This file is included by max77759_charger.c and can not be built alone. + */ + +#include + +/* Temperatures are in 0.1 C, as the power supply class reports them */ +static void guard_test_band_edges(struct kunit *test) +{ + static const struct { + int decic; + int band; + } c[] = { + { -1, -1 }, { 0, 0 }, { 1, 0 }, + { 99, 0 }, { 100, 1 }, { 101, 1 }, + { 199, 1 }, { 200, 2 }, { 201, 2 }, + { 419, 2 }, { 420, 3 }, { 421, 3 }, + { 459, 3 }, { 460, 4 }, { 461, 4 }, + { 479, 4 }, { 480, 5 }, { 481, 5 }, + { 549, 5 }, { 550, -1 }, { 551, -1 }, + { -200, -1 }, { 250, 2 }, { 900, -1 }, + }; + int i; + + for (i = 0; i < ARRAY_SIZE(c); i++) + KUNIT_EXPECT_EQ_MSG(test, guard_classify(c[i].decic), c[i].band, + "at %d", c[i].decic); +} + +static void guard_test_hysteresis(struct kunit *test) +{ + static const struct { + int cur; + int decic; + int next; + } c[] = { + /* The first reading (-2) is taken as it is */ + { -2, 250, 2 }, { -2, 200, 2 }, { -2, 460, 4 }, + { -2, -1, -1 }, { -2, 550, -1 }, + /* No change inside the band */ + { 2, 200, 2 }, { 2, 419, 2 }, + /* A more restrictive band at once */ + { 2, 199, 1 }, { 2, 420, 3 }, { 1, 99, 0 }, + { 3, 460, 4 }, { 4, 480, 5 }, + { 0, -1, -1 }, { 5, 550, -1 }, + /* A less restrictive one only 2.0 C inside it on both sides */ + { 1, 200, 1 }, { 1, 219, 1 }, { 1, 220, 2 }, + { 0, 100, 0 }, { 0, 119, 0 }, { 0, 120, 1 }, + { 3, 419, 3 }, { 3, 400, 3 }, { 3, 399, 2 }, + /* Out of the inhibit, cold and hot */ + { -1, 0, -1 }, { -1, 19, -1 }, { -1, 20, 0 }, + { -1, 549, -1 }, { -1, 530, -1 }, { -1, 529, 5 }, + /* + * 42-46 C and 46-48 C are no wider than twice the + * hysteresis: cooling never relaxes into them. + */ + { 5, 470, 5 }, { 5, 440, 5 }, { 5, 400, 5 }, + { 5, 399, 2 }, + /* Between bands that are not neighbours */ + { 2, 50, 0 }, { 2, 500, 5 }, { 4, 50, 0 }, + { 0, 250, 2 }, { 5, 250, 2 }, { 0, 205, 0 }, + }; + static const struct guard_table *const tbl[] = { + &guard_table_default, &guard_table_husky, + }; + int i, t; + + for (t = 0; t < ARRAY_SIZE(tbl); t++) + for (i = 0; i < ARRAY_SIZE(c); i++) + KUNIT_EXPECT_EQ_MSG(test, + guard_next_band(tbl[t], c[i].cur, + c[i].decic), + c[i].next, + "table %d: band %d at %d", t, + c[i].cur, c[i].decic); +} + +static void guard_test_top_tier(struct kunit *test) +{ + static const int def[GUARD_NBANDS] = { 1, 2, 2, 2, 1, 0 }; + static const int husky[GUARD_NBANDS] = { 1, 3, 3, 3, 1, 0 }; + int b; + + for (b = 0; b < GUARD_NBANDS; b++) { + KUNIT_EXPECT_EQ_MSG(test, guard_top_tier(&guard_table_default, b), + def[b], "band %d", b); + KUNIT_EXPECT_EQ_MSG(test, guard_top_tier(&guard_table_husky, b), + husky[b], "band %d", b); + } +} + +struct guard_tier_case { + int band; + int tier; + int uv; + int next; +}; + +static void guard_test_tiers(struct kunit *test, const struct guard_table *tbl, + const struct guard_tier_case *c, int n) +{ + int i; + + for (i = 0; i < n; i++) + KUNIT_EXPECT_EQ_MSG(test, + guard_next_tier(tbl, c[i].band, c[i].tier, + c[i].uv), + c[i].next, "band %d tier %d at %d uV", + c[i].band, c[i].tier, c[i].uv); +} + +static void guard_test_tier_steps_default(struct kunit *test) +{ + static const struct guard_tier_case c[] = { + { 2, 0, 3700000, 0 }, { 2, 0, 4199999, 0 }, + { 2, 0, 4200000, 1 }, { 2, 0, 4299999, 1 }, + { 2, 0, 4300000, 2 }, { 2, 0, 4450000, 2 }, + { 2, 1, 4299999, 1 }, { 2, 1, 4300000, 2 }, + /* A tier does not fall when the voltage does */ + { 2, 1, 4100000, 1 }, { 2, 2, 3700000, 2 }, + /* Not above the highest tier the band charges in */ + { 0, 0, 4199999, 0 }, { 0, 0, 4400000, 1 }, + { 4, 0, 4400000, 1 }, { 5, 0, 4400000, 0 }, + { 1, 0, 4400000, 2 }, { 3, 0, 4400000, 2 }, + }; + + guard_test_tiers(test, &guard_table_default, c, ARRAY_SIZE(c)); +} + +static void guard_test_tier_steps_husky(struct kunit *test) +{ + static const struct guard_tier_case c[] = { + { 2, 0, 3700000, 0 }, { 2, 0, 4199999, 0 }, + { 2, 0, 4200000, 1 }, { 2, 0, 4299999, 1 }, + { 2, 0, 4300000, 2 }, { 2, 0, 4349999, 2 }, + { 2, 0, 4350000, 3 }, { 2, 0, 4480000, 3 }, + { 2, 2, 4349999, 2 }, { 2, 2, 4350000, 3 }, + { 2, 1, 4100000, 1 }, { 2, 3, 3700000, 3 }, + { 0, 0, 4199999, 0 }, { 0, 0, 4400000, 1 }, + { 4, 0, 4400000, 1 }, { 5, 0, 4400000, 0 }, + { 1, 0, 4400000, 3 }, { 3, 0, 4400000, 3 }, + }; + + guard_test_tiers(test, &guard_table_husky, c, ARRAY_SIZE(c)); +} + +static void guard_test_table(struct kunit *test, const struct guard_table *tbl, + int ntiers, const u32 *cv_mv, + const u8 (*cc_pct)[GUARD_MAX_TIERS]) +{ + int b, t; + + KUNIT_ASSERT_EQ(test, tbl->ntiers, ntiers); + KUNIT_ASSERT_LE(test, tbl->ntiers, GUARD_MAX_TIERS); + for (t = 0; t < GUARD_MAX_TIERS; t++) + KUNIT_EXPECT_EQ_MSG(test, tbl->cv_mv[t], cv_mv[t], "tier %d", t); + for (b = 0; b < GUARD_NBANDS; b++) + for (t = 0; t < GUARD_MAX_TIERS; t++) + KUNIT_EXPECT_EQ_MSG(test, tbl->cc_pct[b][t], cc_pct[b][t], + "band %d tier %d", b, t); +} + +/* The numbers the driver had before it had more than one table */ +static void guard_test_default_table(struct kunit *test) +{ + static const u32 cv_mv[GUARD_MAX_TIERS] = { 4200, 4300, 4450 }; + static const u8 cc_pct[GUARD_NBANDS][GUARD_MAX_TIERS] = { + { 30, 10, 0 }, + { 50, 30, 30 }, + { 100, 70, 50 }, + { 80, 50, 50 }, + { 50, 50, 0 }, + { 30, 0, 0 }, + }; + + guard_test_table(test, &guard_table_default, 3, cv_mv, cc_pct); + KUNIT_EXPECT_EQ(test, guard_temp_decic[0], 0); + KUNIT_EXPECT_EQ(test, guard_temp_decic[1], 100); + KUNIT_EXPECT_EQ(test, guard_temp_decic[2], 200); + KUNIT_EXPECT_EQ(test, guard_temp_decic[3], 420); + KUNIT_EXPECT_EQ(test, guard_temp_decic[4], 460); + KUNIT_EXPECT_EQ(test, guard_temp_decic[5], 480); + KUNIT_EXPECT_EQ(test, guard_temp_decic[6], 550); +} + +static void guard_test_husky_table(struct kunit *test) +{ + static const u32 cv_mv[GUARD_MAX_TIERS] = { 4200, 4300, 4350, 4480 }; + static const u8 cc_pct[GUARD_NBANDS][GUARD_MAX_TIERS] = { + { 50, 30, 0, 0 }, + { 70, 30, 30, 30 }, + { 120, 120, 120, 50 }, + { 100, 100, 100, 50 }, + { 50, 50, 0, 0 }, + { 30, 0, 0, 0 }, + }; + + guard_test_table(test, &guard_table_husky, 4, cv_mv, cc_pct); +} + +struct guard_limit_case { + int band; + int tier; + u32 fv_mv; + u32 cc_ua; +}; + +static void guard_test_limit_cases(struct kunit *test, + const struct guard_table *tbl, + u32 design_uah, u32 fv_max_mv, u32 cc_max_ua, + const struct guard_limit_case *c, int n) +{ + u32 fv, cc; + int i; + + for (i = 0; i < n; i++) { + guard_limits(tbl, c[i].band, c[i].tier, design_uah, fv_max_mv, + cc_max_ua, &fv, &cc); + KUNIT_EXPECT_EQ_MSG(test, fv, c[i].fv_mv, "band %d tier %d", + c[i].band, c[i].tier); + KUNIT_EXPECT_EQ_MSG(test, cc, c[i].cc_ua, "band %d tier %d", + c[i].band, c[i].tier); + } +} + +/* A 4926 mAh battery whose description allows more than the table asks */ +static void guard_test_limits_default(struct kunit *test) +{ + static const struct guard_limit_case c[] = { + { 0, 0, 4300, 1477800 }, { 0, 1, 4300, 492600 }, + { 1, 0, 4450, 2463000 }, { 1, 1, 4450, 1477800 }, + { 1, 2, 4450, 1477800 }, + { 2, 0, 4450, 4926000 }, { 2, 1, 4450, 3448200 }, + { 2, 2, 4450, 2463000 }, + { 3, 0, 4450, 3940800 }, { 3, 1, 4450, 2463000 }, + { 3, 2, 4450, 2463000 }, + { 4, 0, 4300, 2463000 }, { 4, 1, 4300, 2463000 }, + { 5, 0, 4200, 1477800 }, + }; + + guard_test_limit_cases(test, &guard_table_default, 4926000, 4500, + 6000000, c, ARRAY_SIZE(c)); +} + +/* The 4950 mAh battery, again with nothing capped by its description */ +static void guard_test_limits_husky(struct kunit *test) +{ + static const struct guard_limit_case c[] = { + { 0, 0, 4300, 2475000 }, { 0, 1, 4300, 1485000 }, + { 1, 0, 4480, 3465000 }, { 1, 1, 4480, 1485000 }, + { 1, 2, 4480, 1485000 }, { 1, 3, 4480, 1485000 }, + { 2, 0, 4480, 5940000 }, { 2, 1, 4480, 5940000 }, + { 2, 2, 4480, 5940000 }, { 2, 3, 4480, 2475000 }, + { 3, 0, 4480, 4950000 }, { 3, 1, 4480, 4950000 }, + { 3, 2, 4480, 4950000 }, { 3, 3, 4480, 2475000 }, + { 4, 0, 4300, 2475000 }, { 4, 1, 4300, 2475000 }, + { 5, 0, 4200, 1485000 }, + }; + + guard_test_limit_cases(test, &guard_table_husky, 4950000, 4500, + 6000000, c, ARRAY_SIZE(c)); +} + +/* The limits from the battery description cap whatever the table says */ +static void guard_test_limits_capped(struct kunit *test) +{ + static const struct guard_limit_case def[] = { + { 0, 0, 4300, 1477800 }, { 0, 1, 4300, 492600 }, + { 2, 0, 4350, 2000000 }, { 2, 1, 4350, 2000000 }, + { 2, 2, 4350, 2000000 }, { 5, 0, 4200, 1477800 }, + }; + const struct guard_table *tbl = &guard_table_husky; + u32 fv, cc; + int b, t; + + guard_test_limit_cases(test, &guard_table_default, 4926000, 4350, + 2000000, def, ARRAY_SIZE(def)); + + /* + * The husky battery node today: 4.20 V and 1.485 A, which is 30 % of + * 4950 mAh, the lowest row of its table. Every band and tier that + * charges gets exactly these two limits. + */ + for (b = 0; b < GUARD_NBANDS; b++) { + for (t = 0; t <= guard_top_tier(tbl, b); t++) { + guard_limits(tbl, b, t, 4950000, 4200, 1485000, &fv, &cc); + KUNIT_EXPECT_EQ_MSG(test, fv, 4200, "band %d tier %d", + b, t); + KUNIT_EXPECT_EQ_MSG(test, cc, 1485000, "band %d tier %d", + b, t); + } + } +} + +static struct kunit_case guard_test_cases[] = { + KUNIT_CASE(guard_test_band_edges), + KUNIT_CASE(guard_test_hysteresis), + KUNIT_CASE(guard_test_top_tier), + KUNIT_CASE(guard_test_tier_steps_default), + KUNIT_CASE(guard_test_tier_steps_husky), + KUNIT_CASE(guard_test_default_table), + KUNIT_CASE(guard_test_husky_table), + KUNIT_CASE(guard_test_limits_default), + KUNIT_CASE(guard_test_limits_husky), + KUNIT_CASE(guard_test_limits_capped), + { } +}; + +static struct kunit_suite guard_test_suite = { + .name = "max77759_charger_guard", + .test_cases = guard_test_cases, +}; + +kunit_test_suite(guard_test_suite); From ef1e2c4addb6e71c2d5cc34effa15b1d0407714d Mon Sep 17 00:00:00 2001 From: Lef Date: Mon, 16 Nov 2026 18:17:00 +0200 Subject: [PATCH 2/9] power: supply: max77759: round the charge current and float down set_fast_charge_current_limit() and set_float_voltage_limit() pick the step at or above the requested value (linear_range_get_selector_high_array()), so a request between two steps is written as the next step up. On husky the battery node's 1485000 uA becomes 1533400 uA, 48 mA above the battery's limit. Take the step at or below the request instead, for both, as Google's driver does (max77759_set_charger_current_max_ua() in google-modules/bms rounds down too). The current, at probe: cheetah and raven ask for 1466730 uA, an exact step, so probe writes the same value as before. akita's 1320000 uA goes from 1333390 uA to 1266720 uA. husky's 1485000 uA becomes 1466730 uA. A request above the highest step (4000190 uA) or below the lowest is still -EINVAL, through a new cc_selector() like the float's: a battery node without constant-charge-current-max-microamp leaves -EINVAL in the field, and max77759_charger_init() copies it into a u32, which must stay a failed probe and not become a 4 A current with no cap. The current, under the temperature guard (temp_guard=1), this changes the Pixel 7 Pro, the 6 Pro and the Pixel 7 too: every guard current that falls between two steps is now one step lower. At 0-10 C between 4.20 and 4.30 V the default table asks for 10 % of the capacity, 492.6 mA on the 7 Pro, which was written as 533 mA and is now 467 mA (the 6 Pro's 490.5 mA the same); on the Pixel 7, 427.7 mA was 467 mA and is now 400 mA. Stock rounds down there, so this is a fix for those phones as well. The float: a new fv_selector() takes the step at or below the request, 3900 mV between the two ranges, and still refuses a request below the lowest step or above the highest (4500 mV) with -EINVAL rather than clamping it. Every float in use (4200, 4300, 4450 mV and the guard table's) is a multiple of 10 mV, an exact step, so no written float changes today on any board. Tested: KUnit under UML (new cases for the float steps, the range gap and the limits, and for the current: 1485000, 4000190, 4000191 and (u32)-EINVAL); arm64 objects at W=1 with and without the KUnit option. On a Pixel 8 Pro, 2026-11-18, slot B, kernel 7.3.0-rc6-omamobile-push1-gc4d94a1c1fe5 (an earlier head of this series, linux-omarchy-mobile#266, which still had the charge defender and the husky float restore, now in their own PR, before the -EINVAL above the top current step and the read-back values in the dev_err, which KUnit covers, on the published base, nothing else on top; the charger options from /etc/modprobe.d, temp_guard=1 charge_defender=1 default_ilim_ua=500000; a 500 mA desktop USB port): the probe logged "fast charge 1466730 uA" for husky's 1485000 uA (the bootloader had left 1933420 uA). The float written at probe, 4200 mV, is an exact step, so the float rounding is covered by KUnit only. Not tested: anything on a Pixel 7 Pro, 6 Pro, Pixel 7 or akita; a guard current between two steps on hardware. Signed-off-by: Lef --- drivers/power/supply/max77759_charger.c | 54 ++++++++++++++++---- drivers/power/supply/max77759_charger_test.c | 47 +++++++++++++++++ 2 files changed, 91 insertions(+), 10 deletions(-) diff --git a/drivers/power/supply/max77759_charger.c b/drivers/power/supply/max77759_charger.c index c5d6a10eeb..c72e71a621 100644 --- a/drivers/power/supply/max77759_charger.c +++ b/drivers/power/supply/max77759_charger.c @@ -375,16 +375,34 @@ static int get_fast_charge_current(struct max77759_charger *chg) return ret ? ret : val; } +/* + * The fast charge current step at or below @cc_ua, never one above it. A + * request below the lowest step or above the highest (4000190 uA) is + * -EINVAL, not clamped: a battery node without + * constant-charge-current-max-microamp leaves -EINVAL in the field, which + * must stay a failed probe and not become the top current. + */ +static int cc_selector(u32 cc_ua, u32 *sel) +{ + const struct linear_range *top = + &chgcc_limit_ranges[ARRAY_SIZE(chgcc_limit_ranges) - 1]; + bool found; + + if (cc_ua > linear_range_get_max_value(top) || + linear_range_get_selector_low_array(chgcc_limit_ranges, + ARRAY_SIZE(chgcc_limit_ranges), + cc_ua, sel, &found)) + return -EINVAL; + + return 0; +} + static int set_fast_charge_current_limit(struct max77759_charger *chg, u32 cc_max_ua) { - bool found; u32 regval; - linear_range_get_selector_high_array(chgcc_limit_ranges, - ARRAY_SIZE(chgcc_limit_ranges), - cc_max_ua, ®val, &found); - if (!found) + if (cc_selector(cc_max_ua, ®val)) return -EINVAL; return regmap_update_bits(chg->regmap, MAX77759_CHGR_REG_CHG_CNFG_02, @@ -408,15 +426,31 @@ static int get_float_voltage(struct max77759_charger *chg) return ret ? ret : val; } +/* + * The float voltage step at or below @fv_mv, never one above it. Between + * the two ranges (3900 to 4000 mV) that is 3900 mV. A request below the + * lowest step or above the highest is -EINVAL. + */ +static int fv_selector(u32 fv_mv, u32 *sel) +{ + const struct linear_range *top = + &chg_cv_prm_ranges[ARRAY_SIZE(chg_cv_prm_ranges) - 1]; + bool found; + + if (fv_mv > linear_range_get_max_value(top) || + linear_range_get_selector_low_array(chg_cv_prm_ranges, + ARRAY_SIZE(chg_cv_prm_ranges), + fv_mv, sel, &found)) + return -EINVAL; + + return 0; +} + static int set_float_voltage_limit(struct max77759_charger *chg, u32 fv_mv) { u32 regval; - bool found; - linear_range_get_selector_high_array(chg_cv_prm_ranges, - ARRAY_SIZE(chg_cv_prm_ranges), - fv_mv, ®val, &found); - if (!found) + if (fv_selector(fv_mv, ®val)) return -EINVAL; return regmap_update_bits(chg->regmap, MAX77759_CHGR_REG_CHG_CNFG_04, diff --git a/drivers/power/supply/max77759_charger_test.c b/drivers/power/supply/max77759_charger_test.c index 47ee96256c..e641c9579e 100644 --- a/drivers/power/supply/max77759_charger_test.c +++ b/drivers/power/supply/max77759_charger_test.c @@ -296,6 +296,51 @@ static void guard_test_limits_capped(struct kunit *test) } } +/* The float is the step at or below the request, as stock rounds it */ +static void round_test_float(struct kunit *test) +{ + u32 sel, mv; + + KUNIT_EXPECT_EQ(test, fv_selector(4200, &sel), 0); + KUNIT_EXPECT_EQ(test, sel, 0x14); + KUNIT_EXPECT_EQ(test, fv_selector(4209, &sel), 0); + KUNIT_EXPECT_EQ(test, sel, 0x14); + KUNIT_EXPECT_EQ(test, fv_selector(4450, &sel), 0); + KUNIT_EXPECT_EQ(test, sel, 0x2d); + KUNIT_EXPECT_EQ(test, fv_selector(4500, &sel), 0); + KUNIT_EXPECT_EQ(test, sel, 0x32); + + /* Between the two ranges: the 3900 mV step below */ + KUNIT_EXPECT_EQ(test, fv_selector(3950, &sel), 0); + KUNIT_EXPECT_EQ(test, linear_range_get_value_array(chg_cv_prm_ranges, + ARRAY_SIZE(chg_cv_prm_ranges), + sel, &mv), 0); + KUNIT_EXPECT_EQ(test, mv, 3900); + + /* Out of range */ + KUNIT_EXPECT_EQ(test, fv_selector(3799, &sel), -EINVAL); + KUNIT_EXPECT_EQ(test, fv_selector(4501, &sel), -EINVAL); +} + +/* + * The current is the step at or below the request; above the top step + * (4000190 uA) is refused, not clamped, and so is the -EINVAL a battery + * node without constant-charge-current-max-microamp leaves in the u32 + */ +static void round_test_current(struct kunit *test) +{ + u32 sel; + + KUNIT_EXPECT_EQ(test, cc_selector(1485000, &sel), 0); + KUNIT_EXPECT_EQ(test, sel, 0x16); + KUNIT_EXPECT_EQ(test, cc_selector(4000190, &sel), 0); + KUNIT_EXPECT_EQ(test, sel, 0x3c); + + KUNIT_EXPECT_EQ(test, cc_selector(4000191, &sel), -EINVAL); + KUNIT_EXPECT_EQ(test, cc_selector((u32)-EINVAL, &sel), -EINVAL); + KUNIT_EXPECT_EQ(test, cc_selector(133329, &sel), -EINVAL); +} + static struct kunit_case guard_test_cases[] = { KUNIT_CASE(guard_test_band_edges), KUNIT_CASE(guard_test_hysteresis), @@ -307,6 +352,8 @@ static struct kunit_case guard_test_cases[] = { KUNIT_CASE(guard_test_limits_default), KUNIT_CASE(guard_test_limits_husky), KUNIT_CASE(guard_test_limits_capped), + KUNIT_CASE(round_test_float), + KUNIT_CASE(round_test_current), { } }; From 58e11ff45d96bb26ae3620add4fd364b1d27d978 Mon Sep 17 00:00:00 2001 From: Lef Date: Wed, 18 Nov 2026 09:08:30 +0200 Subject: [PATCH 3/9] power: supply: max77759: add charge control thresholds Add charge_control_end_threshold and charge_control_start_threshold to the charger. At 100 / 0, the default, they do nothing and the guard work reads and writes nothing more than before. With a lower end, charging stops (buck only, as the temperature guard does) once the battery's capacity reaches it, and resumes at the start threshold, or 5 % below the end when start is 0 or not below the end. Each threshold is checked on its own, so the order of two writes does not matter: an end at or below the start sets the start back to 0. charge_control_start_threshold reads back the value in force, so with an end of 80 and a start of 0 it reads 75, the level charging resumes at. With the end at 100 it reads the stored start. The two inhibits, the temperature guard's and the threshold's, are kept apart so that neither clears the other. The thresholds run first in each guard run, so a stop is in place before the guard clears its own inhibit, and a flag only changes once the mode write has worked, so a failed write is tried again at the next run. The thresholds are checked by the guard work, at most 30 s after a change in capacity. On unbind, after the guard work is cancelled, a threshold stop is cleared before charging is left on. While either inhibit holds the charge with the cable in (buck only, the chip reports CHG_DTLS off and CHGIN valid), status now reads Not charging instead of Discharging. With no input it reads Discharging, whatever the flags and the last mode written. This reaches the Pixel 7 Pro, the 6 Pro and the Pixel 7 too when they run with temp_guard=1: while the temperature guard inhibits charging with the cable in, their status changes from Discharging to Not charging. One more change for them with temp_guard=1: the guard's inhibit flag is now rolled back when its mode write fails, and the write is tried again at the next run, where before the flag stayed set with the chip still charging. The guard work is delayed work and s2idle stops it, so the thresholds are only checked while the phone is awake: asleep on the charger, the chip carries on to the float, and a stop already in place stays in place. temp_guard's description now says the guard work only runs while the phone is awake. The rules are side-effect free functions with KUnit tests; the two inhibits, the status and the stop and resume on a fake capacity are tested on a regmap backed by an array, and the tests read their own fake battery and register their charger supply under a test name, so they leave guard_psy and the real max77759-charger alone. Tested: KUnit under UML; arm64 objects at W=1 with and without the KUnit option. On a Pixel 8 Pro, 2026-11-18, slot B, kernel 7.3.0-rc6-omamobile-push1-gc4d94a1c1fe5 (an earlier head of this series, linux-omarchy-mobile#266, which still had the charge defender and the husky float restore, now in their own PR, before the -EINVAL above the top current step and the read-back values in the dev_err, which KUnit covers, on the published base, nothing else on top; the charger options from /etc/modprobe.d, temp_guard=1 charge_defender=1 default_ilim_ua=500000; a 500 mA desktop USB port): an end of 90, saved by the Settings unit and applied at boot, stopped charging 36 s after boot ("charge stopped at 90 %"), status read Not charging with the cable in and the start read back 85. Unplugged, online read 0 and status Discharging; plugged back in at 89 %, status stayed Not charging (resume at 85 % or less). An end of 100 read the start back as 0, logged "charge resumed: no limit" and status read Charging; an end of 90 again at 89 % did not stop it (the stop is at 90 % or more) and the start read 85. After a reboot the bootloader had put the charger mode back (a threshold stop does not survive a reboot) and the saved end of 90 and start of 85 came back. Status read Full at 89 % once, with the battery at 4.26 V above the 4.20 V float. Not tested: a stop that reaches the start and resumes on its own, suspend while plugged in, poweroff (that base lacks the rtc fix, so reboots only), anything on a Pixel 7 Pro, 6 Pro or Pixel 7. Signed-off-by: Lef --- drivers/power/supply/max77759_charger.c | 204 +++++++++- drivers/power/supply/max77759_charger_test.c | 402 ++++++++++++++++++- 2 files changed, 596 insertions(+), 10 deletions(-) diff --git a/drivers/power/supply/max77759_charger.c b/drivers/power/supply/max77759_charger.c index c72e71a621..c5de25d2b3 100644 --- a/drivers/power/supply/max77759_charger.c +++ b/drivers/power/supply/max77759_charger.c @@ -50,12 +50,14 @@ MODULE_PARM_DESC(default_ilim_ua, static bool temp_guard; module_param(temp_guard, bool, 0644); MODULE_PARM_DESC(temp_guard, - "Limit charging by battery temperature as the stock phone does: no charge below 0 or from 55 C, lower float voltage and current outside 10-46 C. Reads the battery power supply named by guard_psy every 30 s. Default off."); + "Limit charging by battery temperature as the stock phone does: no charge below 0 or from 55 C, lower float voltage and current outside 10-46 C. Reads the battery power supply named by guard_psy every 30 s while awake: s2idle stops the guard, and the charge thresholds with it. Default off."); static char guard_psy[32] = "max170xx_battery"; module_param_string(guard_psy, guard_psy, sizeof(guard_psy), 0444); MODULE_PARM_DESC(guard_psy, "Battery power supply whose temp and voltage_now the guard reads"); +#define STOP_RESUME_GAP 5 /* resume at end - 5 when start is 0 */ + #define GUARD_PERIOD_MS 30000 #define GUARD_RETRY_MS 2000 /* while there is no reading */ #define GUARD_HYST_DECIC 20 /* 2.0 C into a band before relaxing */ @@ -168,6 +170,12 @@ struct max77759_charger { struct mutex lock; /* protects the state below */ enum max77759_chgr_mode mode; bool inhibit; /* charge suspended by the temperature guard */ + bool stop_inhibit; /* charge suspended at the end threshold */ + const char *bat_psy; /* capacity source, NULL: guard_psy */ + + /* Charge thresholds, in percent, run by the guard work */ + int end_threshold; + int start_threshold; /* Temperature guard */ struct delayed_work guard_work; @@ -247,6 +255,19 @@ static int get_status(struct max77759_charger *chg) case MAX77759_CHGR_CHG_DTLS_SUSP_JEITA: return POWER_SUPPLY_STATUS_NOT_CHARGING; case MAX77759_CHGR_CHG_DTLS_OFF: + /* + * Buck only with the cable in, while the temperature guard or + * a charge threshold holds the charge: not charging, the + * battery is not what feeds the phone. With the cable out it + * is discharging, whatever the flags say. + */ + if (charger_input_valid(chg) <= 0) + return POWER_SUPPLY_STATUS_DISCHARGING; + scoped_guard(mutex, &chg->lock) { + if (chg->mode == MAX77759_CHGR_MODE_CHG_BUCK_ON && + (chg->inhibit || chg->stop_inhibit)) + return POWER_SUPPLY_STATUS_NOT_CHARGING; + } return POWER_SUPPLY_STATUS_DISCHARGING; default: break; @@ -500,8 +521,19 @@ static const enum power_supply_property max77759_charger_props[] = { POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, + POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD, + POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD, }; +/* + * The charge at which a threshold stop resumes: the start threshold, or + * end - STOP_RESUME_GAP when start is 0 or not below the end. + */ +static int stop_resume(int end, int start) +{ + return start && start < end ? start : max(end - STOP_RESUME_GAP, 0); +} + static int max77759_charger_get_property(struct power_supply *psy, enum power_supply_property psp, union power_supply_propval *pval) @@ -534,6 +566,17 @@ static int max77759_charger_get_property(struct power_supply *psy, case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: ret = get_input_current_limit(chg); break; + case POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD: + /* The value in force: 75 for an end of 80 and a start of 0 */ + scoped_guard(mutex, &chg->lock) + ret = chg->end_threshold >= 100 ? chg->start_threshold : + stop_resume(chg->end_threshold, + chg->start_threshold); + break; + case POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD: + scoped_guard(mutex, &chg->lock) + ret = chg->end_threshold; + break; default: ret = -EINVAL; } @@ -542,19 +585,69 @@ static int max77759_charger_get_property(struct power_supply *psy, return ret < 0 ? ret : 0; } +/* + * A write of one threshold, in percent. Each is checked on its own so the + * order of two writes does not matter: an end at or below the start takes + * the start back to 0, and a start at or above the end is kept but + * stop_resume() then resumes at end - STOP_RESUME_GAP. + */ +static int stop_set_threshold(bool is_end, int val, int *end, int *start) +{ + if (is_end) { + if (val < 1 || val > 100) + return -EINVAL; + *end = val; + if (*start >= val) + *start = 0; + } else { + if (val < 0 || val >= 100) + return -EINVAL; + *start = val; + } + return 0; +} + +static int max77759_charger_set_property(struct power_supply *psy, + enum power_supply_property psp, + const union power_supply_propval *pval) +{ + struct max77759_charger *chg = power_supply_get_drvdata(psy); + + if (psp != POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD && + psp != POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD) + return -EINVAL; + + guard(mutex)(&chg->lock); + + /* The guard work takes them at its next run */ + return stop_set_threshold(psp == POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD, + pval->intval, &chg->end_threshold, + &chg->start_threshold); +} + +static int max77759_charger_property_is_writeable(struct power_supply *psy, + enum power_supply_property psp) +{ + return psp == POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD || + psp == POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD; +} + static const struct power_supply_desc max77759_charger_desc = { .name = "max77759-charger", .type = POWER_SUPPLY_TYPE_USB, .properties = max77759_charger_props, .num_properties = ARRAY_SIZE(max77759_charger_props), .get_property = max77759_charger_get_property, + .set_property = max77759_charger_set_property, + .property_is_writeable = max77759_charger_property_is_writeable, }; -/* The mode the hardware gets: buck only while the guard inhibits charging */ +/* The mode the hardware gets: buck only while either inhibit holds */ static int charger_write_mode(struct max77759_charger *chg, enum max77759_chgr_mode mode) { - if (mode == MAX77759_CHGR_MODE_CHG_BUCK_ON && chg->inhibit) + if (mode == MAX77759_CHGR_MODE_CHG_BUCK_ON && + (chg->inhibit || chg->stop_inhibit)) mode = MAX77759_CHGR_MODE_BUCK_ON; return regmap_update_bits(chg->regmap, MAX77759_CHGR_REG_CHG_CNFG_00, @@ -988,15 +1081,27 @@ static int guard_read(int *decic, int *uv) return ret; } -static void guard_set_inhibit(struct max77759_charger *chg, bool inhibit) +/* Set one of the two inhibit flags; the mode follows the two together */ +static void charger_set_inhibit(struct max77759_charger *chg, bool *flag, + bool inhibit) { + bool was, old; + guard(mutex)(&chg->lock); - if (chg->inhibit == inhibit) - return; - chg->inhibit = inhibit; - if (chg->mode == MAX77759_CHGR_MODE_CHG_BUCK_ON) - charger_write_mode(chg, chg->mode); + was = chg->inhibit || chg->stop_inhibit; + old = *flag; + *flag = inhibit; + /* A failed write keeps the old flag, so the next call tries again */ + if (was != (chg->inhibit || chg->stop_inhibit) && + chg->mode == MAX77759_CHGR_MODE_CHG_BUCK_ON && + charger_write_mode(chg, chg->mode)) + *flag = old; +} + +static void guard_set_inhibit(struct max77759_charger *chg, bool inhibit) +{ + charger_set_inhibit(chg, &chg->inhibit, inhibit); } /* Back to the limits from the battery description, charging allowed */ @@ -1010,6 +1115,63 @@ static void guard_release(struct max77759_charger *chg) dev_info(chg->dev, "temperature guard off\n"); } +/* + * Whether charging stays stopped: stop at @soc >= @end, resume at + * @soc <= stop_resume(). An end of 100 never stops. + */ +static bool stop_next(bool stopped, int soc, int end, int start) +{ + if (end >= 100) + return false; + if (!stopped) + return soc >= end; + return soc > stop_resume(end, start); +} + +static int stop_read_soc(struct max77759_charger *chg, int *soc) +{ + union power_supply_propval val; + struct power_supply *psy; + int ret; + + psy = power_supply_get_by_name(chg->bat_psy ?: guard_psy); + if (!psy) + return -ENODEV; + ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_CAPACITY, &val); + power_supply_put(psy); + if (!ret) + *soc = val.intval; + return ret; +} + +/* + * The charge thresholds. At 100 / 0 this reads nothing and writes + * nothing. + */ +static void stop_work(struct max77759_charger *chg) +{ + int end, start, soc = -1; + bool stopped; + + scoped_guard(mutex, &chg->lock) { + end = chg->end_threshold; + start = chg->start_threshold; + } + + stopped = chg->stop_inhibit; + if (end >= 100) + stopped = false; + else if (!stop_read_soc(chg, &soc)) + stopped = stop_next(stopped, soc, end, start); + + if (stopped != chg->stop_inhibit && soc >= 0) + dev_info(chg->dev, "charge %s at %d %%\n", + stopped ? "stopped" : "resumed", soc); + else if (stopped != chg->stop_inhibit) + dev_info(chg->dev, "charge resumed: no limit\n"); + charger_set_inhibit(chg, &chg->stop_inhibit, stopped); +} + static void guard_work_item(struct work_struct *work) { struct max77759_charger *chg = @@ -1020,6 +1182,12 @@ static void guard_work_item(struct work_struct *work) bool read_ok; u32 fv, cc; + /* + * The charge thresholds first: a stop they call for is in place + * before the guard below can clear its own inhibit. + */ + stop_work(chg); + if (!temp_guard) { WRITE_ONCE(chg->guard_no_reading, false); if (chg->guard_active) @@ -1096,6 +1264,14 @@ static void max77759_charger_log_state(struct max77759_charger *chg, fv, cc, ilim); } +/* On unbind, no charge threshold holds charging off any more */ +static void max77759_charger_clear_stop(void *data) +{ + struct max77759_charger *chg = data; + + charger_set_inhibit(chg, &chg->stop_inhibit, false); +} + static int max77759_charger_probe(struct platform_device *pdev) { struct regulator_config chgin_otg_reg_cfg = { }; @@ -1111,6 +1287,7 @@ static int max77759_charger_probe(struct platform_device *pdev) platform_set_drvdata(pdev, chg); chg->dev = dev; + chg->end_threshold = 100; /* 100 / 0: no charge limit */ chg->regmap = dev_get_regmap(dev->parent, "charger"); if (!chg->regmap) return dev_err_probe(dev, -ENODEV, "Missing regmap\n"); @@ -1158,6 +1335,15 @@ static int max77759_charger_probe(struct platform_device *pdev) "Failed to start charging\n"); } + /* + * devm undoes in reverse: on unbind the guard work is cancelled, + * then a threshold stop is cleared, and only then is charging + * left on. + */ + ret = devm_add_action_or_reset(dev, max77759_charger_clear_stop, chg); + if (ret) + return ret; + chgin_otg_reg_cfg.dev = dev; chgin_otg_reg_cfg.driver_data = chg; chgin_otg_reg_cfg.of_node = dev_of_node(dev); diff --git a/drivers/power/supply/max77759_charger_test.c b/drivers/power/supply/max77759_charger_test.c index e641c9579e..6f2d56853f 100644 --- a/drivers/power/supply/max77759_charger_test.c +++ b/drivers/power/supply/max77759_charger_test.c @@ -1,11 +1,13 @@ // SPDX-License-Identifier: GPL-2.0-only /* * KUnit tests for the battery temperature guard of the MAX77759 charger - * driver: bands, hysteresis, tiers, limits and the tables themselves. + * driver: bands, hysteresis, tiers, limits and the tables themselves; the + * charge thresholds. * * This file is included by max77759_charger.c and can not be built alone. */ +#include #include /* Temperatures are in 0.1 C, as the power supply class reports them */ @@ -341,6 +343,393 @@ static void round_test_current(struct kunit *test) KUNIT_EXPECT_EQ(test, cc_selector(133329, &sel), -EINVAL); } +static void stop_test_set_threshold(struct kunit *test) +{ + int end = 100, start = 0; + + /* End first, then start */ + KUNIT_EXPECT_EQ(test, stop_set_threshold(true, 80, &end, &start), 0); + KUNIT_EXPECT_EQ(test, stop_set_threshold(false, 70, &end, &start), 0); + KUNIT_EXPECT_EQ(test, end, 80); + KUNIT_EXPECT_EQ(test, start, 70); + + /* Start first, then end: the same pair */ + end = 100; + start = 0; + KUNIT_EXPECT_EQ(test, stop_set_threshold(false, 70, &end, &start), 0); + KUNIT_EXPECT_EQ(test, stop_set_threshold(true, 80, &end, &start), 0); + KUNIT_EXPECT_EQ(test, end, 80); + KUNIT_EXPECT_EQ(test, start, 70); + + /* Lowering end to 60 then start to 50, from 80 / 70 */ + KUNIT_EXPECT_EQ(test, stop_set_threshold(true, 60, &end, &start), 0); + KUNIT_EXPECT_EQ(test, start, 0); + KUNIT_EXPECT_EQ(test, stop_set_threshold(false, 50, &end, &start), 0); + KUNIT_EXPECT_EQ(test, end, 60); + KUNIT_EXPECT_EQ(test, start, 50); + + /* Raising start to 90 then end to 95, from 60 / 50 */ + KUNIT_EXPECT_EQ(test, stop_set_threshold(false, 90, &end, &start), 0); + KUNIT_EXPECT_EQ(test, stop_set_threshold(true, 95, &end, &start), 0); + KUNIT_EXPECT_EQ(test, end, 95); + KUNIT_EXPECT_EQ(test, start, 90); + + /* Out of range: nothing changes */ + KUNIT_EXPECT_EQ(test, stop_set_threshold(true, 0, &end, &start), -EINVAL); + KUNIT_EXPECT_EQ(test, stop_set_threshold(true, 101, &end, &start), -EINVAL); + KUNIT_EXPECT_EQ(test, stop_set_threshold(false, -1, &end, &start), -EINVAL); + KUNIT_EXPECT_EQ(test, stop_set_threshold(false, 100, &end, &start), -EINVAL); + KUNIT_EXPECT_EQ(test, end, 95); + KUNIT_EXPECT_EQ(test, start, 90); +} + +/* 100 / 0, the default, never stops charging, whatever the charge */ +static void stop_test_default_inert(struct kunit *test) +{ + int soc; + + for (soc = 0; soc <= 100; soc++) { + KUNIT_EXPECT_FALSE_MSG(test, stop_next(false, soc, 100, 0), + "soc %d", soc); + KUNIT_EXPECT_FALSE_MSG(test, stop_next(true, soc, 100, 0), + "soc %d", soc); + } +} + +static void stop_test_end_80(struct kunit *test) +{ + /* Start 0: resume at 75 */ + KUNIT_EXPECT_FALSE(test, stop_next(false, 79, 80, 0)); + KUNIT_EXPECT_TRUE(test, stop_next(false, 80, 80, 0)); + KUNIT_EXPECT_TRUE(test, stop_next(true, 79, 80, 0)); + KUNIT_EXPECT_TRUE(test, stop_next(true, 76, 80, 0)); + KUNIT_EXPECT_FALSE(test, stop_next(true, 75, 80, 0)); + KUNIT_EXPECT_FALSE(test, stop_next(false, 76, 80, 0)); + + /* Start 70: resume at 70 */ + KUNIT_EXPECT_TRUE(test, stop_next(true, 71, 80, 70)); + KUNIT_EXPECT_FALSE(test, stop_next(true, 70, 80, 70)); + + /* Start one below the end: resume at 79 */ + KUNIT_EXPECT_TRUE(test, stop_next(false, 80, 80, 79)); + KUNIT_EXPECT_TRUE(test, stop_next(true, 80, 80, 79)); + KUNIT_EXPECT_FALSE(test, stop_next(true, 79, 80, 79)); + + /* A start not below the end resumes at end - 5 */ + KUNIT_EXPECT_TRUE(test, stop_next(true, 76, 80, 90)); + KUNIT_EXPECT_FALSE(test, stop_next(true, 75, 80, 90)); + KUNIT_EXPECT_FALSE(test, stop_next(true, 75, 80, 80)); + + /* An end below the gap resumes at 0 */ + KUNIT_EXPECT_TRUE(test, stop_next(true, 1, 3, 0)); + KUNIT_EXPECT_FALSE(test, stop_next(true, 0, 3, 0)); +} + +/* A charger regmap backed by an array, logging the writes */ +struct fake_chip { + u8 regs[256]; + u8 log[32]; + int nlog; + int nread; + int corrupt_reg; /* reads of this register come back wrong */ + int fail_writes; /* this many writes fail with -EIO first */ +}; + +static int fake_read(void *ctx, unsigned int reg, unsigned int *val) +{ + struct fake_chip *c = ctx; + + c->nread++; + *val = c->regs[reg]; + if (reg == c->corrupt_reg) + *val ^= 0x01; + return 0; +} + +static int fake_write(void *ctx, unsigned int reg, unsigned int val) +{ + struct fake_chip *c = ctx; + + if (c->fail_writes) { + c->fail_writes--; + return -EIO; + } + c->regs[reg] = val; + if (c->nlog < ARRAY_SIZE(c->log)) + c->log[c->nlog++] = reg; + return 0; +} + +static const struct regmap_config fake_regmap_config = { + .reg_bits = 8, + .val_bits = 8, + .max_register = 0xcc, + .reg_read = fake_read, + .reg_write = fake_write, + .cache_type = REGCACHE_NONE, +}; + +static struct max77759_charger *fake_charger(struct kunit *test, + struct fake_chip **chip) +{ + struct max77759_charger *chg; + struct device *dev; + + dev = kunit_device_register(test, "max77759-chg-test"); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev); + *chip = kunit_kzalloc(test, sizeof(**chip), GFP_KERNEL); + chg = kunit_kzalloc(test, sizeof(*chg), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, *chip); + KUNIT_ASSERT_NOT_NULL(test, chg); + (*chip)->corrupt_reg = -1; + chg->dev = dev; + chg->regmap = devm_regmap_init(dev, NULL, *chip, &fake_regmap_config); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, chg->regmap); + mutex_init(&chg->lock); + chg->end_threshold = 100; + return chg; +} + +/* Entering charge mode with a threshold stop held gives buck only */ +static void inhibit_test_set_mode(struct kunit *test) +{ + struct fake_chip *c; + struct max77759_charger *chg = fake_charger(test, &c); + u8 *mode = &c->regs[MAX77759_CHGR_REG_CHG_CNFG_00]; + + chg->mode = MAX77759_CHGR_MODE_OFF; + chg->stop_inhibit = true; + KUNIT_EXPECT_EQ(test, charger_set_mode(chg, MAX77759_CHGR_MODE_CHG_BUCK_ON), 0); + KUNIT_EXPECT_EQ(test, *mode, MAX77759_CHGR_MODE_BUCK_ON); + KUNIT_EXPECT_EQ(test, chg->mode, MAX77759_CHGR_MODE_CHG_BUCK_ON); + + /* The unbind action clears it */ + max77759_charger_clear_stop(chg); + KUNIT_EXPECT_FALSE(test, chg->stop_inhibit); + KUNIT_EXPECT_EQ(test, *mode, MAX77759_CHGR_MODE_CHG_BUCK_ON); +} + +/* A mode write that fails is tried again at the next call */ +static void inhibit_test_write_retry(struct kunit *test) +{ + struct fake_chip *c; + struct max77759_charger *chg = fake_charger(test, &c); + u8 *mode = &c->regs[MAX77759_CHGR_REG_CHG_CNFG_00]; + + chg->mode = MAX77759_CHGR_MODE_CHG_BUCK_ON; + *mode = MAX77759_CHGR_MODE_CHG_BUCK_ON; + + c->fail_writes = 1; + charger_set_inhibit(chg, &chg->stop_inhibit, true); + KUNIT_EXPECT_FALSE(test, chg->stop_inhibit); + KUNIT_EXPECT_EQ(test, *mode, MAX77759_CHGR_MODE_CHG_BUCK_ON); + + charger_set_inhibit(chg, &chg->stop_inhibit, true); + KUNIT_EXPECT_TRUE(test, chg->stop_inhibit); + KUNIT_EXPECT_EQ(test, *mode, MAX77759_CHGR_MODE_BUCK_ON); +} + +/* A fake battery power supply for stop_work() to read the capacity of */ +static int fake_bat_capacity; + +static int fake_bat_get(struct power_supply *psy, + enum power_supply_property psp, + union power_supply_propval *val) +{ + if (psp != POWER_SUPPLY_PROP_CAPACITY) + return -EINVAL; + val->intval = fake_bat_capacity; + return 0; +} + +static const enum power_supply_property fake_bat_props[] = { + POWER_SUPPLY_PROP_CAPACITY, +}; + +static const struct power_supply_desc fake_bat_desc = { + .name = "max77759-test-bat", + .type = POWER_SUPPLY_TYPE_BATTERY, + .properties = fake_bat_props, + .num_properties = ARRAY_SIZE(fake_bat_props), + .get_property = fake_bat_get, +}; + +/* stop_work() stops at the end threshold and resumes at the start one */ +static void stop_test_work_capacity(struct kunit *test) +{ + struct power_supply_config cfg = { }; + struct power_supply *bat; + struct fake_chip *c; + struct max77759_charger *chg = fake_charger(test, &c); + u8 *mode = &c->regs[MAX77759_CHGR_REG_CHG_CNFG_00]; + + bat = devm_power_supply_register(chg->dev, &fake_bat_desc, &cfg); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bat); + /* Not guard_psy: the module's own driver may be reading that */ + chg->bat_psy = fake_bat_desc.name; + + chg->guard_table = &guard_table_default; + chg->mode = MAX77759_CHGR_MODE_CHG_BUCK_ON; + *mode = MAX77759_CHGR_MODE_CHG_BUCK_ON; + chg->end_threshold = 80; + chg->start_threshold = 70; + + fake_bat_capacity = 79; + stop_work(chg); + KUNIT_EXPECT_FALSE(test, chg->stop_inhibit); + fake_bat_capacity = 80; + stop_work(chg); + KUNIT_EXPECT_TRUE(test, chg->stop_inhibit); + KUNIT_EXPECT_EQ(test, *mode, MAX77759_CHGR_MODE_BUCK_ON); + fake_bat_capacity = 71; + stop_work(chg); + KUNIT_EXPECT_TRUE(test, chg->stop_inhibit); + fake_bat_capacity = 70; + stop_work(chg); + KUNIT_EXPECT_FALSE(test, chg->stop_inhibit); + KUNIT_EXPECT_EQ(test, *mode, MAX77759_CHGR_MODE_CHG_BUCK_ON); +} + +/* Either inhibit puts the chip in buck-only mode; both must clear */ +static void inhibit_test_two_flags(struct kunit *test) +{ + struct fake_chip *c; + struct max77759_charger *chg = fake_charger(test, &c); + u8 *mode = &c->regs[MAX77759_CHGR_REG_CHG_CNFG_00]; + + chg->mode = MAX77759_CHGR_MODE_CHG_BUCK_ON; + *mode = MAX77759_CHGR_MODE_CHG_BUCK_ON; + + guard_set_inhibit(chg, true); + KUNIT_EXPECT_EQ(test, *mode, MAX77759_CHGR_MODE_BUCK_ON); + charger_set_inhibit(chg, &chg->stop_inhibit, true); + guard_set_inhibit(chg, false); + KUNIT_EXPECT_EQ(test, *mode, MAX77759_CHGR_MODE_BUCK_ON); + charger_set_inhibit(chg, &chg->stop_inhibit, false); + KUNIT_EXPECT_EQ(test, *mode, MAX77759_CHGR_MODE_CHG_BUCK_ON); + /* One write to inhibit, one to resume */ + KUNIT_EXPECT_EQ(test, c->nlog, 2); +} + +/* At 100 / 0: no i2c at all */ +static void stop_test_inert_no_io(struct kunit *test) +{ + struct fake_chip *c; + struct max77759_charger *chg = fake_charger(test, &c); + + chg->mode = MAX77759_CHGR_MODE_CHG_BUCK_ON; + chg->guard_table = &guard_table_default; + + stop_work(chg); + + KUNIT_EXPECT_EQ(test, c->nread, 0); + KUNIT_EXPECT_EQ(test, c->nlog, 0); + KUNIT_EXPECT_FALSE(test, chg->stop_inhibit); +} + +/* + * Buck only with the cable in and an inhibit held reads Not charging; with + * the cable out it reads Discharging, inhibit or not. + */ +static void inhibit_test_status(struct kunit *test) +{ + struct fake_chip *c; + struct max77759_charger *chg = fake_charger(test, &c); + u8 *int_ok = &c->regs[MAX77759_CHGR_REG_CHG_INT_OK]; + + c->regs[MAX77759_CHGR_REG_CHG_DETAILS_01] = + FIELD_PREP(MAX77759_CHGR_REG_CHG_DETAILS_01_CHG_DTLS, + MAX77759_CHGR_CHG_DTLS_OFF); + *int_ok = MAX77759_CHGR_REG_CHG_INT_CHGIN; + + chg->mode = MAX77759_CHGR_MODE_OFF; + KUNIT_EXPECT_EQ(test, get_status(chg), POWER_SUPPLY_STATUS_DISCHARGING); + + chg->mode = MAX77759_CHGR_MODE_CHG_BUCK_ON; + KUNIT_EXPECT_EQ(test, get_status(chg), POWER_SUPPLY_STATUS_DISCHARGING); + chg->stop_inhibit = true; + KUNIT_EXPECT_EQ(test, get_status(chg), POWER_SUPPLY_STATUS_NOT_CHARGING); + chg->stop_inhibit = false; + chg->inhibit = true; + KUNIT_EXPECT_EQ(test, get_status(chg), POWER_SUPPLY_STATUS_NOT_CHARGING); +} + +/* An inhibit held with no input: the battery feeds the phone */ +static void inhibit_test_status_unplugged(struct kunit *test) +{ + struct fake_chip *c; + struct max77759_charger *chg = fake_charger(test, &c); + + c->regs[MAX77759_CHGR_REG_CHG_DETAILS_01] = + FIELD_PREP(MAX77759_CHGR_REG_CHG_DETAILS_01_CHG_DTLS, + MAX77759_CHGR_CHG_DTLS_OFF); + c->regs[MAX77759_CHGR_REG_CHG_INT_OK] = 0; + + /* The mode stays as it was: the psy work may not have run */ + chg->mode = MAX77759_CHGR_MODE_CHG_BUCK_ON; + chg->stop_inhibit = true; + KUNIT_EXPECT_EQ(test, get_status(chg), POWER_SUPPLY_STATUS_DISCHARGING); + chg->stop_inhibit = false; + chg->inhibit = true; + KUNIT_EXPECT_EQ(test, get_status(chg), POWER_SUPPLY_STATUS_DISCHARGING); +} + +static int chg_prop_get(struct power_supply *psy, + enum power_supply_property psp) +{ + union power_supply_propval val; + int ret = power_supply_get_property(psy, psp, &val); + + return ret ? ret : val.intval; +} + +static int chg_prop_set(struct power_supply *psy, + enum power_supply_property psp, int v) +{ + union power_supply_propval val = { .intval = v }; + + return power_supply_set_property(psy, psp, &val); +} + +/* charge_control_start_threshold reads the value in force */ +static void stop_test_start_readback(struct kunit *test) +{ + const enum power_supply_property end = + POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD; + const enum power_supply_property start = + POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD; + struct power_supply_config cfg = { }; + struct power_supply_desc *desc; + struct power_supply *psy; + struct fake_chip *c; + struct max77759_charger *chg = fake_charger(test, &c); + + /* The charger's own ops, under a name no real supply has */ + desc = devm_kmemdup(chg->dev, &max77759_charger_desc, sizeof(*desc), + GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, desc); + desc->name = "max77759-chg-test"; + cfg.drv_data = chg; + psy = devm_power_supply_register(chg->dev, desc, &cfg); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, psy); + + /* 100 / 0: no limit, start reads 0 */ + KUNIT_EXPECT_EQ(test, chg_prop_get(psy, start), 0); + + /* End 80, start 0: resumes at 75, and says so */ + KUNIT_ASSERT_EQ(test, chg_prop_set(psy, end, 80), 0); + KUNIT_EXPECT_EQ(test, chg_prop_get(psy, start), 75); + KUNIT_EXPECT_EQ(test, chg_prop_get(psy, end), 80); + + /* Start 70 */ + KUNIT_ASSERT_EQ(test, chg_prop_set(psy, start, 70), 0); + KUNIT_EXPECT_EQ(test, chg_prop_get(psy, start), 70); + + /* A start at or above the end resumes at end - 5 */ + KUNIT_ASSERT_EQ(test, chg_prop_set(psy, start, 90), 0); + KUNIT_EXPECT_EQ(test, chg_prop_get(psy, start), 75); +} + static struct kunit_case guard_test_cases[] = { KUNIT_CASE(guard_test_band_edges), KUNIT_CASE(guard_test_hysteresis), @@ -354,6 +743,17 @@ static struct kunit_case guard_test_cases[] = { KUNIT_CASE(guard_test_limits_capped), KUNIT_CASE(round_test_float), KUNIT_CASE(round_test_current), + KUNIT_CASE(stop_test_set_threshold), + KUNIT_CASE(stop_test_default_inert), + KUNIT_CASE(stop_test_end_80), + KUNIT_CASE(inhibit_test_two_flags), + KUNIT_CASE(stop_test_inert_no_io), + KUNIT_CASE(inhibit_test_set_mode), + KUNIT_CASE(inhibit_test_write_retry), + KUNIT_CASE(stop_test_work_capacity), + KUNIT_CASE(inhibit_test_status), + KUNIT_CASE(inhibit_test_status_unplugged), + KUNIT_CASE(stop_test_start_readback), { } }; From 9f9a2e3c02e5bedf186bb0427b1bf4700ce42b5a Mon Sep 17 00:00:00 2001 From: Lef Date: Mon, 16 Nov 2026 17:11:50 +0200 Subject: [PATCH 4/9] mfd: max77759: make the charger mode, current and float registers volatile The charger regmap caches every register but the interrupt and status ones. CHG_CNFG_00 to CHG_CNFG_04 hold the charger mode, the fast charge current and the float voltage. The charger driver is going to read them back after it writes them, to make sure the chip holds what was asked, and a cached read would only return the value it just wrote. Mark 0xb9-0xbd volatile so reads go to the chip. Writes are the same as before; regmap_update_bits() now reads the chip instead of the cache first, one more i2c read per update. This reaches every board with the MAX77759, the Pixel 7 Pro, the 6 Pro and the Pixel 7 too: every read of these registers, the sysfs reads of the current and the float and every update of them, now goes to the chip, and with temp_guard=1 every guard run talks to the chip. The MFD has no cache sync or cache-only path on suspend, so no resume path changes. Tested: arm64 objects at W=1 with and without the KUnit option; the charger's KUnit tests under UML. On a Pixel 8 Pro, 2026-11-18, slot B, kernel 7.3.0-rc6-omamobile-push1-gc4d94a1c1fe5 (an earlier head of this series, linux-omarchy-mobile#266, which still had the charge defender and the husky float restore, now in their own PR, before the -EINVAL above the top current step and the read-back values in the dev_err, which KUnit covers, on the published base, nothing else on top; the charger options from /etc/modprobe.d, temp_guard=1 charge_defender=1 default_ilim_ua=500000; a 500 mA desktop USB port): probe, the thresholds and the guard ran with no visible change from this; read-only through regmap debugfs the charger registers matched what the driver logged (a 4200 mV float written over the bootloader's 4350 mV, again after a reboot). On 2026-11-16, on an earlier form of this series, 0xbd read 0x2d after probe and 0x23 after unbind (the float restore, since moved to its own PR), the values the driver wrote. Not tested: anything on a Pixel 7 Pro, 6 Pro or Pixel 7. Signed-off-by: Lef --- drivers/mfd/max77759.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/drivers/mfd/max77759.c b/drivers/mfd/max77759.c index 72b608a1ab..dfcccdfe12 100644 --- a/drivers/mfd/max77759.c +++ b/drivers/mfd/max77759.c @@ -154,6 +154,12 @@ static const struct regmap_range max77759_charger_ro_registers[] = { static const struct regmap_range max77759_charger_volatile_registers[] = { regmap_reg_range(0xb0, 0xb1), /* INTx */ regmap_reg_range(0xb4, 0xb8), + /* + * CNFG_00 to CNFG_04 (mode, current, float): the charger driver reads + * them back to check what the chip really holds, which a cached + * read would not tell. + */ + regmap_reg_range(0xb9, 0xbd), }; static const struct regmap_access_table max77759_charger_wr_table = { From 6a785153a5e8678633c041e21009dad5e0b7335a Mon Sep 17 00:00:00 2001 From: Lef Date: Wed, 18 Nov 2026 09:09:37 +0200 Subject: [PATCH 5/9] power: supply: max77759: read the guard's limits back from the chip The temperature guard writes the float voltage and the charge current and has so far ignored a failed write. Read both back from the chip after writing them (the registers are volatile since the previous patch) and keep charging inhibited when the chip does not hold the step the request maps to, or when a write or a read fails. The next guard run tries again; the error is logged once, when it starts, with the float and current asked for and what the chip holds. The release back to the battery's own limits does the same and stays active until the chip is seen to hold them. The write order is unchanged: float, then current. This runs on every board with temp_guard=1, the Pixel 7 Pro, the 6 Pro and the Pixel 7 too: each guard run now writes and reads the two registers on the chip, and a mismatch or a failed read now stops charging (buck only) where a failed write used to be ignored. With temp_guard=0, the default, nothing changes. The expected values come from the same step rounding the setters use, at or below the request for both, so a request between two steps is no mismatch. Tested on a regmap backed by an array: the write order and values, a wrong read-back of the float and of the current gives -EIO with the values the chip holds and puts the chip in buck-only mode, a request refused before the write reads nothing back, and a good one resumes charging. KUnit under UML; arm64 objects at W=1 with and without the KUnit option. On a Pixel 8 Pro, 2026-11-18, slot B, kernel 7.3.0-rc6-omamobile-push1-gc4d94a1c1fe5 (an earlier head of this series, linux-omarchy-mobile#266, which still had the charge defender and the husky float restore, now in their own PR, before the -EINVAL above the top current step and the read-back values in the dev_err, which KUnit covers, on the published base, nothing else on top; the charger options from /etc/modprobe.d, temp_guard=1 charge_defender=1 default_ilim_ua=500000; a 500 mA desktop USB port): the guard logged "battery 38.6 C, 4308 mV: band 20-42 C, float 4200 mV, charge 1485000 uA", and no read-back error or inhibit was logged through the run and a reboot. Not tested: a real mismatch or failed read on hardware (the failure path is KUnit only), anything on a Pixel 7 Pro, 6 Pro or Pixel 7. Signed-off-by: Lef --- drivers/power/supply/max77759_charger.c | 104 +++++++++++++++++-- drivers/power/supply/max77759_charger_test.c | 81 ++++++++++++++- 2 files changed, 176 insertions(+), 9 deletions(-) diff --git a/drivers/power/supply/max77759_charger.c b/drivers/power/supply/max77759_charger.c index c5de25d2b3..e55e903c0d 100644 --- a/drivers/power/supply/max77759_charger.c +++ b/drivers/power/supply/max77759_charger.c @@ -176,6 +176,7 @@ struct max77759_charger { /* Charge thresholds, in percent, run by the guard work */ int end_threshold; int start_threshold; + bool apply_failed; /* the chip did not hold the guard's limits */ /* Temperature guard */ struct delayed_work guard_work; @@ -512,6 +513,74 @@ static int set_input_current_limit(struct max77759_charger *chg, int ilim_ua) regval); } +/* The float voltage the chip gets for a request, as fv_selector() picks it */ +static int fv_quantize(u32 fv_mv, u32 *out_mv) +{ + u32 sel; + + if (fv_selector(fv_mv, &sel)) + return -EINVAL; + return linear_range_get_value_array(chg_cv_prm_ranges, + ARRAY_SIZE(chg_cv_prm_ranges), + sel, out_mv); +} + +/* + * The fast charge current the chip gets for a request: the step at or + * below it, as cc_selector() picks it + */ +static int cc_quantize(u32 cc_ua, u32 *out_ua) +{ + u32 sel; + + if (cc_selector(cc_ua, &sel)) + return -EINVAL; + return linear_range_get_value_array(chgcc_limit_ranges, + ARRAY_SIZE(chgcc_limit_ranges), + sel, out_ua); +} + +/* + * Write the float voltage, then the fast charge current, and read both + * back from the chip (the registers are volatile, so this is no cached + * value). -EIO when the chip does not hold what was written. *@held_fv and + * *@held_cc are what it holds, each -1 when it was not read back. + */ +static int charger_apply_limits(struct max77759_charger *chg, u32 fv_mv, + u32 cc_ua, int *held_fv, int *held_cc) +{ + u32 want_fv, want_cc; + int ret, got_fv, got_cc; + + *held_fv = -1; + *held_cc = -1; + + ret = fv_quantize(fv_mv, &want_fv); + if (!ret) + ret = cc_quantize(cc_ua, &want_cc); + if (!ret) + ret = set_float_voltage_limit(chg, fv_mv); + if (!ret) + ret = set_fast_charge_current_limit(chg, cc_ua); + if (ret) + return ret; + + got_fv = get_float_voltage(chg); + got_cc = get_fast_charge_current(chg); + if (got_fv >= 0) + *held_fv = got_fv; + if (got_cc >= 0) + *held_cc = got_cc; + if (got_fv < 0) + return got_fv; + if (got_cc < 0) + return got_cc; + if (got_fv != (int)want_fv || got_cc != (int)want_cc) + return -EIO; + + return 0; +} + static const enum power_supply_property max77759_charger_props[] = { POWER_SUPPLY_PROP_ONLINE, POWER_SUPPLY_PROP_PRESENT, @@ -1104,12 +1173,32 @@ static void guard_set_inhibit(struct max77759_charger *chg, bool inhibit) charger_set_inhibit(chg, &chg->inhibit, inhibit); } -/* Back to the limits from the battery description, charging allowed */ +/* + * Charging only on limits the chip is seen to hold: inhibit when it does + * not take them. Returns 0 when it does. + */ +static int guard_apply(struct max77759_charger *chg, u32 fv_mv, u32 cc_ua) +{ + int held_fv, held_cc; + int ret = charger_apply_limits(chg, fv_mv, cc_ua, &held_fv, &held_cc); + + if (ret && !chg->apply_failed) + dev_err(chg->dev, + "chip did not take float %u mV, charge %u uA (%d), it holds %d mV, %d uA: charging inhibited\n", + fv_mv, cc_ua, ret, held_fv, held_cc); + chg->apply_failed = ret; + guard_set_inhibit(chg, ret); + return ret; +} + +/* + * Back to the limits from the battery description, charging allowed. If + * the chip does not take them, stay inhibited and try again next run. + */ static void guard_release(struct max77759_charger *chg) { - set_float_voltage_limit(chg, chg->fv_max_mv); - set_fast_charge_current_limit(chg, chg->cc_max_ua); - guard_set_inhibit(chg, false); + if (guard_apply(chg, chg->fv_max_mv, chg->cc_max_ua)) + return; chg->guard_active = false; chg->guard_band = -2; dev_info(chg->dev, "temperature guard off\n"); @@ -1232,6 +1321,9 @@ static void guard_work_item(struct work_struct *work) guard_limits(tbl, band, tier, chg->design_uah, chg->fv_max_mv, chg->cc_max_ua, &fv, &cc); + if (guard_apply(chg, fv, cc)) + goto out; + if (band != chg->guard_band || tier != chg->guard_tier) dev_info(chg->dev, "battery %s%d.%d C, %d mV: band %d-%d C, float %u mV, charge %u uA\n", @@ -1239,10 +1331,6 @@ static void guard_work_item(struct work_struct *work) uv / 1000, guard_temp_decic[band] / 10, guard_temp_decic[band + 1] / 10, fv, cc); - - set_float_voltage_limit(chg, fv); - set_fast_charge_current_limit(chg, cc); - guard_set_inhibit(chg, false); chg->guard_band = band; chg->guard_tier = tier; out: diff --git a/drivers/power/supply/max77759_charger_test.c b/drivers/power/supply/max77759_charger_test.c index 6f2d56853f..f89333814a 100644 --- a/drivers/power/supply/max77759_charger_test.c +++ b/drivers/power/supply/max77759_charger_test.c @@ -2,7 +2,7 @@ /* * KUnit tests for the battery temperature guard of the MAX77759 charger * driver: bands, hysteresis, tiers, limits and the tables themselves; the - * charge thresholds. + * charge thresholds and the read-back of the limits. * * This file is included by max77759_charger.c and can not be built alone. */ @@ -730,6 +730,82 @@ static void stop_test_start_readback(struct kunit *test) KUNIT_EXPECT_EQ(test, chg_prop_get(psy, start), 75); } +static void readback_test_apply(struct kunit *test) +{ + struct fake_chip *c; + struct max77759_charger *chg = fake_charger(test, &c); + int hf, hc; + + /* Float first, then current, as before the read-back */ + KUNIT_EXPECT_EQ(test, charger_apply_limits(chg, 4200, 1485000, &hf, &hc), 0); + KUNIT_ASSERT_EQ(test, c->nlog, 2); + KUNIT_EXPECT_EQ(test, c->log[0], MAX77759_CHGR_REG_CHG_CNFG_04); + KUNIT_EXPECT_EQ(test, c->log[1], MAX77759_CHGR_REG_CHG_CNFG_02); + KUNIT_EXPECT_EQ(test, c->regs[MAX77759_CHGR_REG_CHG_CNFG_04], 0x14); + /* + * The current is the step at or below the request: 1485000 uA is + * written as 1466730 uA, selector 0x16, never above it. + */ + KUNIT_EXPECT_EQ(test, c->regs[MAX77759_CHGR_REG_CHG_CNFG_02], 0x16); + KUNIT_EXPECT_EQ(test, get_fast_charge_current(chg), 1466730); + + /* An exact step stays as it is (cheetah, raven) */ + KUNIT_EXPECT_EQ(test, charger_apply_limits(chg, 4450, 1466730, &hf, &hc), 0); + KUNIT_EXPECT_EQ(test, get_fast_charge_current(chg), 1466730); + /* akita's 1320000 uA */ + KUNIT_EXPECT_EQ(test, charger_apply_limits(chg, 4450, 1320000, &hf, &hc), 0); + KUNIT_EXPECT_EQ(test, get_fast_charge_current(chg), 1266720); + + /* A float between two steps reads back as the step below */ + KUNIT_EXPECT_EQ(test, charger_apply_limits(chg, 4205, 1466730, &hf, &hc), 0); + KUNIT_EXPECT_EQ(test, c->regs[MAX77759_CHGR_REG_CHG_CNFG_04], 0x14); +} + +static void readback_test_mismatch(struct kunit *test) +{ + struct fake_chip *c; + struct max77759_charger *chg = fake_charger(test, &c); + int hf, hc; + + /* -EIO, and what the chip holds comes back for the one dev_err */ + c->corrupt_reg = MAX77759_CHGR_REG_CHG_CNFG_04; + KUNIT_EXPECT_EQ(test, charger_apply_limits(chg, 4200, 1485000, &hf, &hc), -EIO); + KUNIT_EXPECT_NE(test, hf, 4200); + KUNIT_EXPECT_EQ(test, hc, 1466730); + + c->corrupt_reg = MAX77759_CHGR_REG_CHG_CNFG_02; + KUNIT_EXPECT_EQ(test, charger_apply_limits(chg, 4300, 1000000, &hf, &hc), -EIO); + KUNIT_EXPECT_EQ(test, hf, 4300); + KUNIT_EXPECT_NE(test, hc, 933370); + + /* A request refused before any write reads nothing back */ + KUNIT_EXPECT_EQ(test, charger_apply_limits(chg, 4501, 1466730, &hf, &hc), -EINVAL); + KUNIT_EXPECT_EQ(test, hf, -1); + KUNIT_EXPECT_EQ(test, hc, -1); + KUNIT_EXPECT_EQ(test, charger_apply_limits(chg, 4200, 4000191, &hf, &hc), -EINVAL); + KUNIT_EXPECT_EQ(test, charger_apply_limits(chg, 4200, (u32)-EINVAL, &hf, &hc), -EINVAL); +} + +/* A limit the chip does not hold inhibits charging, a good one resumes it */ +static void readback_test_inhibit(struct kunit *test) +{ + struct fake_chip *c; + struct max77759_charger *chg = fake_charger(test, &c); + u8 *mode = &c->regs[MAX77759_CHGR_REG_CHG_CNFG_00]; + + chg->mode = MAX77759_CHGR_MODE_CHG_BUCK_ON; + *mode = MAX77759_CHGR_MODE_CHG_BUCK_ON; + + c->corrupt_reg = MAX77759_CHGR_REG_CHG_CNFG_04; + KUNIT_EXPECT_EQ(test, guard_apply(chg, 4200, 1485000), -EIO); + KUNIT_EXPECT_EQ(test, *mode, MAX77759_CHGR_MODE_BUCK_ON); + KUNIT_EXPECT_EQ(test, c->log[c->nlog - 1], MAX77759_CHGR_REG_CHG_CNFG_00); + + c->corrupt_reg = -1; + KUNIT_EXPECT_EQ(test, guard_apply(chg, 4200, 1485000), 0); + KUNIT_EXPECT_EQ(test, *mode, MAX77759_CHGR_MODE_CHG_BUCK_ON); +} + static struct kunit_case guard_test_cases[] = { KUNIT_CASE(guard_test_band_edges), KUNIT_CASE(guard_test_hysteresis), @@ -754,6 +830,9 @@ static struct kunit_case guard_test_cases[] = { KUNIT_CASE(inhibit_test_status), KUNIT_CASE(inhibit_test_status_unplugged), KUNIT_CASE(stop_test_start_readback), + KUNIT_CASE(readback_test_apply), + KUNIT_CASE(readback_test_mismatch), + KUNIT_CASE(readback_test_inhibit), { } }; From b98dce0efb11adbdfd5c0eea472fd02ae5e56d06 Mon Sep 17 00:00:00 2001 From: Lef Date: Wed, 18 Nov 2026 10:45:09 +0200 Subject: [PATCH 6/9] power: supply: max77759: add the Pixel 8 Pro charge defender Add the charge defender of the stock Pixel 8 Pro charger driver, behind a new charge_defender module parameter that is off by default and only acts on husky: plugged in at 4.27 V or more, time at 35.0 C or more is counted, and after 6 h charging stops at 80 % and resumes at 79 % until the cable comes out. It only stops charging (buck only); nothing drains the battery, so one already above 80 % when it latches stays there until use brings it down. A voltage below 4.27 V starts the count again; a lower temperature only pauses it. A user end threshold below 80 % stays in force. While it is latched, charge_control_end_threshold and charge_control_start_threshold read back the values in force, 80 and 79 (or the user's lower ones), as the start threshold already reads 75 for an end of 80. The numbers are the stock ones from zuma-shusky-charging.dtsi (kernel/devices/google/shusky, branch android-gs-shusky-6.1-android16): google,bd-trigger-voltage 4270000, bd-trigger-temp 350, bd-trigger-time 21600, bd-drainto-soc 80 and bd-recharge-soc 79. Stock's resume conditions (google,bd-resume-abs-temp, bd-resume-soc, bd-resume-time, bd-resume-temp) are not copied: here the stop lasts until unplugged. Each guard run reads CHGIN itself and an input gone clears the defender. On husky that read is what sees an unplug: no TCPM supply notifies this driver there, so the psy work does not run. An unplug and replug that both fall between two guard runs (30 s apart) is missed. On boards where the psy work runs, an unplug it sees is kept for the next guard run, so a quick replug still counts there. The same dtsi also sets google,bd-temp-dry-run. With it, google_charger.c (google-modules/bms) still counts and, on trigger, still calls bd_batt_set_state(), but bd_recharge_logic() returns before it turns charging off, so stock only stops charging once dry run is cleared at run time (its bd_temp_dry_run sysfs file or vote). Whether Android clears it on the 8 Pro is not known here. This driver has no dry run: with charge_defender=1 it stops charging. It uses the threshold stop of the earlier patch, so it is buck only with the cable in and status reads Not charging. The defender runs in the guard work, which is delayed work: s2idle stops it. Each run counts at most 60 s, so time asleep never counts toward the six hours. While the phone sleeps on the charger, a stop already in place stays (buck only), but a charge below 80 % goes on to the float with nothing to stop it at 80 %. The parameter's description says so too. Nothing changes on other boards, with the parameter on or off. The defender step and the choice of thresholds are side-effect free functions with KUnit tests (21599 s does not latch, 21600 s does, the latch holds until unplugged, 4.269 V resets, 34.9 C pauses, one long gap counts at most 60 s); an unplug seen by the psy work, an unplug seen only by the guard run's CHGIN read, and the read-back while latched are tested on a regmap backed by an array. Tested: KUnit under UML; arm64 objects at W=1 with and without the KUnit option. On a Pixel 8 Pro, 2026-11-18, slot B, kernel 7.3.0-rc6-omamobile-push1-gc4d94a1c1fe5 (an earlier head of this series, linux-omarchy-mobile#266, before the -EINVAL above the top current step and the read-back values in the dev_err, which KUnit covers, on the published base, nothing else on top; the charger options from /etc/modprobe.d, temp_guard=1 charge_defender=1 default_ilim_ua=500000; a 500 mA desktop USB port): the charger loaded and charged normally with charge_defender=1, and the threshold stop it uses worked (see the thresholds patch). The defender itself was not exercised: with the husky float at 4.20 V the battery never reaches 4.27 V, so it cannot latch. Not tested: the defender firing on the phone (it needs a float above 4.27 V and 6 h plugged in at 35 C or more), suspend while plugged in, the unplug seen by the guard run while latched. Until a float above 4.27 V it is covered by KUnit only. Signed-off-by: Lef --- drivers/power/supply/max77759_charger.c | 150 ++++++++++++++-- drivers/power/supply/max77759_charger_test.c | 172 ++++++++++++++++++- 2 files changed, 304 insertions(+), 18 deletions(-) diff --git a/drivers/power/supply/max77759_charger.c b/drivers/power/supply/max77759_charger.c index e55e903c0d..adce0ccc1f 100644 --- a/drivers/power/supply/max77759_charger.c +++ b/drivers/power/supply/max77759_charger.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -56,6 +57,26 @@ static char guard_psy[32] = "max170xx_battery"; module_param_string(guard_psy, guard_psy, sizeof(guard_psy), 0444); MODULE_PARM_DESC(guard_psy, "Battery power supply whose temp and voltage_now the guard reads"); +/* + * Charge defender, as the stock Pixel 8 Pro has it (google_charger.c, the + * bd-* values of zuma-shusky-charging.dtsi): after six hours plugged in + * with the battery at 4.27 V or more and 35.0 C or more, stop charging at + * 80 % (buck only) and resume at 79 %, until the cable comes out. Nothing + * drains the battery: above 80 % it stays there until use brings it down. + * Time is counted by the guard work, which does not run while the phone + * sleeps. + */ +static bool charge_defender; +module_param(charge_defender, bool, 0644); +MODULE_PARM_DESC(charge_defender, + "Pixel 8 Pro only: after 6 h plugged in at 4.27 V or more and 35.0 C or more, stop charging at 80 % and resume at 79 % until unplugged (no active drain: a battery above 80 % stays there until used). Counted only while awake, and a charge under way goes on to the float while asleep. Default off."); + +#define DEFEND_UV 4270000 +#define DEFEND_DECIC 350 +#define DEFEND_S 21600 +#define DEFEND_MAX_STEP_S 60 /* most one guard run may count */ +#define DEFEND_END 80 +#define DEFEND_START 79 #define STOP_RESUME_GAP 5 /* resume at end - 5 when start is 0 */ #define GUARD_PERIOD_MS 30000 @@ -173,9 +194,13 @@ struct max77759_charger { bool stop_inhibit; /* charge suspended at the end threshold */ const char *bat_psy; /* capacity source, NULL: guard_psy */ - /* Charge thresholds, in percent, run by the guard work */ + /* Charge thresholds, in percent, and defender, run by the guard work */ int end_threshold; int start_threshold; + u32 defend_s; /* time counted towards the defender */ + bool defended; /* defender latched until unplugged */ + bool unplugged; /* psy work saw the source go, under lock */ + ktime_t stop_last; bool apply_failed; /* the chip did not hold the guard's limits */ /* Temperature guard */ @@ -603,6 +628,18 @@ static int stop_resume(int end, int start) return start && start < end ? start : max(end - STOP_RESUME_GAP, 0); } +/* The thresholds in force: the defender's, unless the user's are lower */ +static void stop_effective(int end, int start, bool defended, int *e, int *s) +{ + if (defended && end > DEFEND_END) { + *e = DEFEND_END; + *s = DEFEND_START; + } else { + *e = end; + *s = start; + } +} + static int max77759_charger_get_property(struct power_supply *psy, enum power_supply_property psp, union power_supply_propval *pval) @@ -636,15 +673,21 @@ static int max77759_charger_get_property(struct power_supply *psy, ret = get_input_current_limit(chg); break; case POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD: - /* The value in force: 75 for an end of 80 and a start of 0 */ - scoped_guard(mutex, &chg->lock) - ret = chg->end_threshold >= 100 ? chg->start_threshold : - stop_resume(chg->end_threshold, - chg->start_threshold); - break; case POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD: - scoped_guard(mutex, &chg->lock) - ret = chg->end_threshold; + /* + * The values in force: start reads 75 for an end of 80 and a + * start of 0, and the defender's 80 / 79 while it is latched. + */ + scoped_guard(mutex, &chg->lock) { + int e, s; + + stop_effective(chg->end_threshold, chg->start_threshold, + chg->defended, &e, &s); + if (psp == POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD) + ret = e; + else + ret = e >= 100 ? s : stop_resume(e, s); + } break; default: ret = -EINVAL; @@ -954,6 +997,15 @@ static void psy_work_item(struct work_struct *work) charger_set_mode(chg, MAX77759_CHGR_MODE_CHG_BUCK_ON); } else { charger_set_mode(chg, MAX77759_CHGR_MODE_OFF); + /* + * For the defender: a quick replug still counts as unplugged. + * Only where the TCPM supply notifies us; the guard run's own + * CHGIN read covers the rest. + */ + if (!online.intval) { + scoped_guard(mutex, &chg->lock) + chg->unplugged = true; + } } scoped_guard(mutex, &chg->retry_lock) { @@ -1204,6 +1256,30 @@ static void guard_release(struct max77759_charger *chg) dev_info(chg->dev, "temperature guard off\n"); } +/* + * One defender step of @dt_s seconds. Time counts while plugged in at + * DEFEND_UV or more and DEFEND_DECIC or more; a lower voltage starts the + * count again, a lower temperature only pauses it. Unplugging clears the + * count and the latch. Returns whether the defender is latched. + */ +static bool defend_step(u32 *count_s, bool latched, bool plugged, int uv, + int decic, u32 dt_s) +{ + if (!plugged) { + *count_s = 0; + return false; + } + if (latched) + return true; + if (uv < DEFEND_UV) { + *count_s = 0; + return false; + } + if (decic >= DEFEND_DECIC) + *count_s += min(dt_s, DEFEND_MAX_STEP_S); + return *count_s >= DEFEND_S; +} + /* * Whether charging stays stopped: stop at @soc >= @end, resume at * @soc <= stop_resume(). An end of 100 never stops. @@ -1234,24 +1310,65 @@ static int stop_read_soc(struct max77759_charger *chg, int *soc) } /* - * The charge thresholds. At 100 / 0 this reads nothing and writes - * nothing. + * The charge thresholds and the defender. With the thresholds at 100 / 0 + * and the defender off this reads nothing and writes nothing. */ static void stop_work(struct max77759_charger *chg) { - int end, start, soc = -1; - bool stopped; - + bool defender = charge_defender && chg->guard_table == &guard_table_husky; + ktime_t now = ktime_get_boottime(); + int end, start, e, st, plugged, decic, uv, soc = -1; + bool unplugged, stopped, defended; + s64 dt_ms; + + dt_ms = ktime_ms_delta(now, chg->stop_last); + chg->stop_last = now; scoped_guard(mutex, &chg->lock) { + unplugged = chg->unplugged; + chg->unplugged = false; end = chg->end_threshold; start = chg->start_threshold; } + defended = chg->defended; + if (!defender) { + chg->defend_s = 0; + defended = false; + } else { + /* + * Each run reads CHGIN itself: on husky no TCPM supply + * notifies the psy work. An unplug and replug that both fall + * between two runs is missed. + */ + plugged = unplugged ? 0 : charger_input_valid(chg); + if (!plugged) { + if (defended) + dev_info(chg->dev, "unplugged: defender off\n"); + chg->defend_s = 0; + defended = false; + } else if (plugged > 0 && !guard_read(&decic, &uv)) { + bool was = defended; + + defended = defend_step(&chg->defend_s, was, true, uv, + decic, + clamp_val(div_s64(dt_ms, MSEC_PER_SEC), 0, + U32_MAX)); + if (defended && !was) + dev_info(chg->dev, + "defender on: charging stops at %d %%, resumes at %d %%\n", + DEFEND_END, DEFEND_START); + } + } + /* Under the lock: the threshold read-back reports it */ + scoped_guard(mutex, &chg->lock) + chg->defended = defended; + + stop_effective(end, start, defended, &e, &st); stopped = chg->stop_inhibit; - if (end >= 100) + if (e >= 100) stopped = false; else if (!stop_read_soc(chg, &soc)) - stopped = stop_next(stopped, soc, end, start); + stopped = stop_next(stopped, soc, e, st); if (stopped != chg->stop_inhibit && soc >= 0) dev_info(chg->dev, "charge %s at %d %%\n", @@ -1451,6 +1568,7 @@ static int max77759_charger_probe(struct platform_device *pdev) */ chg->guard_band = -2; chg->guard_table = guard_select_table(); + chg->stop_last = ktime_get_boottime(); ret = devm_delayed_work_autocancel(dev, &chg->guard_work, guard_work_item); if (ret) return dev_err_probe(dev, ret, "Failed to initialize guard work\n"); diff --git a/drivers/power/supply/max77759_charger_test.c b/drivers/power/supply/max77759_charger_test.c index f89333814a..94841f16c9 100644 --- a/drivers/power/supply/max77759_charger_test.c +++ b/drivers/power/supply/max77759_charger_test.c @@ -2,7 +2,7 @@ /* * KUnit tests for the battery temperature guard of the MAX77759 charger * driver: bands, hysteresis, tiers, limits and the tables themselves; the - * charge thresholds and the read-back of the limits. + * charge thresholds, the read-back of the limits and the defender. * * This file is included by max77759_charger.c and can not be built alone. */ @@ -611,20 +611,26 @@ static void inhibit_test_two_flags(struct kunit *test) KUNIT_EXPECT_EQ(test, c->nlog, 2); } -/* At 100 / 0: no i2c at all */ +/* At 100 / 0, with the defender off or on a board without it: no i2c */ static void stop_test_inert_no_io(struct kunit *test) { + bool saved = charge_defender; struct fake_chip *c; struct max77759_charger *chg = fake_charger(test, &c); chg->mode = MAX77759_CHGR_MODE_CHG_BUCK_ON; chg->guard_table = &guard_table_default; + charge_defender = false; stop_work(chg); + charge_defender = true; + stop_work(chg); + charge_defender = saved; KUNIT_EXPECT_EQ(test, c->nread, 0); KUNIT_EXPECT_EQ(test, c->nlog, 0); KUNIT_EXPECT_FALSE(test, chg->stop_inhibit); + KUNIT_EXPECT_FALSE(test, chg->defended); } /* @@ -806,6 +812,162 @@ static void readback_test_inhibit(struct kunit *test) KUNIT_EXPECT_EQ(test, *mode, MAX77759_CHGR_MODE_CHG_BUCK_ON); } +static void stop_test_effective(struct kunit *test) +{ + int e, s; + + stop_effective(100, 0, false, &e, &s); + KUNIT_EXPECT_EQ(test, e, 100); + KUNIT_EXPECT_EQ(test, s, 0); + + stop_effective(100, 0, true, &e, &s); + KUNIT_EXPECT_EQ(test, e, 80); + KUNIT_EXPECT_EQ(test, s, 79); + + /* A lower limit of the user's stays */ + stop_effective(70, 60, true, &e, &s); + KUNIT_EXPECT_EQ(test, e, 70); + KUNIT_EXPECT_EQ(test, s, 60); + + stop_effective(80, 50, true, &e, &s); + KUNIT_EXPECT_EQ(test, e, 80); + KUNIT_EXPECT_EQ(test, s, 50); +} + +/* Run the defender for @n steps of @dt_s at one reading */ +static bool defend_run(u32 *count, bool latched, int uv, int decic, u32 dt_s, + int n) +{ + while (n--) + latched = defend_step(count, latched, true, uv, decic, dt_s); + return latched; +} + +static void defend_test_latch(struct kunit *test) +{ + u32 count = 0; + bool on; + + /* 21,599 s does not latch, 21,600 s does */ + on = defend_run(&count, false, 4270000, 350, 30, 719); + on = defend_step(&count, on, true, 4270000, 350, 29); + KUNIT_EXPECT_EQ(test, count, 21599); + KUNIT_EXPECT_FALSE(test, on); + on = defend_step(&count, on, true, 4270000, 350, 1); + KUNIT_EXPECT_TRUE(test, on); + + /* Latched: stays so whatever the reading, until unplugged */ + KUNIT_EXPECT_TRUE(test, defend_step(&count, on, true, 3800000, 200, 30)); + KUNIT_EXPECT_FALSE(test, defend_step(&count, on, false, 4300000, 400, 30)); + KUNIT_EXPECT_EQ(test, count, 0); +} + +static void defend_test_conditions(struct kunit *test) +{ + u32 count = 0; + + /* 4.269 V starts the count again */ + defend_run(&count, false, 4300000, 400, 30, 100); + KUNIT_EXPECT_EQ(test, count, 3000); + KUNIT_EXPECT_FALSE(test, defend_step(&count, false, true, 4269000, 400, 30)); + KUNIT_EXPECT_EQ(test, count, 0); + + /* 34.9 C does not count, and does not reset either */ + defend_run(&count, false, 4300000, 400, 30, 10); + KUNIT_EXPECT_FALSE(test, defend_run(&count, false, 4300000, 349, 30, + 1000)); + KUNIT_EXPECT_EQ(test, count, 300); + + /* Unplugged clears */ + KUNIT_EXPECT_FALSE(test, defend_step(&count, false, false, 4300000, 400, 30)); + KUNIT_EXPECT_EQ(test, count, 0); + + /* One long gap between runs counts as at most DEFEND_MAX_STEP_S */ + KUNIT_EXPECT_FALSE(test, defend_step(&count, false, true, 4300000, 400, + DEFEND_S)); + KUNIT_EXPECT_EQ(test, count, DEFEND_MAX_STEP_S); +} + +/* An unplug psy work saw clears the defender, and the flag is consumed */ +static void defend_test_unplug_flag(struct kunit *test) +{ + bool saved = charge_defender; + struct fake_chip *c; + struct max77759_charger *chg = fake_charger(test, &c); + + chg->guard_table = &guard_table_husky; + chg->defended = true; + chg->defend_s = DEFEND_S; + chg->unplugged = true; + + charge_defender = true; + stop_work(chg); + charge_defender = saved; + + KUNIT_EXPECT_FALSE(test, chg->defended); + KUNIT_EXPECT_EQ(test, chg->defend_s, 0); + KUNIT_EXPECT_FALSE(test, chg->unplugged); + /* No CHGIN read: the flag alone says unplugged */ + KUNIT_EXPECT_EQ(test, c->nread, 0); +} + +/* With no TCPM notice, the guard run's own CHGIN read clears the defender */ +static void defend_test_unplug_chgin(struct kunit *test) +{ + bool saved = charge_defender; + struct fake_chip *c; + struct max77759_charger *chg = fake_charger(test, &c); + + chg->guard_table = &guard_table_husky; + chg->defended = true; + chg->defend_s = DEFEND_S; + c->regs[MAX77759_CHGR_REG_CHG_INT_OK] = 0; + + charge_defender = true; + stop_work(chg); + charge_defender = saved; + + KUNIT_EXPECT_FALSE(test, chg->defended); + KUNIT_EXPECT_EQ(test, chg->defend_s, 0); +} + +/* While latched, the thresholds read back the defender's 80 / 79 */ +static void defend_test_readback(struct kunit *test) +{ + const enum power_supply_property end = + POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD; + const enum power_supply_property start = + POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD; + struct power_supply_config cfg = { }; + struct power_supply_desc *desc; + struct power_supply *psy; + struct fake_chip *c; + struct max77759_charger *chg = fake_charger(test, &c); + + desc = devm_kmemdup(chg->dev, &max77759_charger_desc, sizeof(*desc), + GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, desc); + desc->name = "max77759-chg-test"; + cfg.drv_data = chg; + psy = devm_power_supply_register(chg->dev, desc, &cfg); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, psy); + + chg->defended = true; + KUNIT_EXPECT_EQ(test, chg_prop_get(psy, end), 80); + KUNIT_EXPECT_EQ(test, chg_prop_get(psy, start), 79); + + /* A lower user end stays in force, and reads back */ + KUNIT_ASSERT_EQ(test, chg_prop_set(psy, end, 70), 0); + KUNIT_EXPECT_EQ(test, chg_prop_get(psy, end), 70); + KUNIT_EXPECT_EQ(test, chg_prop_get(psy, start), 65); + + /* Unlatched: the user's values again */ + KUNIT_ASSERT_EQ(test, chg_prop_set(psy, end, 100), 0); + chg->defended = false; + KUNIT_EXPECT_EQ(test, chg_prop_get(psy, end), 100); + KUNIT_EXPECT_EQ(test, chg_prop_get(psy, start), 0); +} + static struct kunit_case guard_test_cases[] = { KUNIT_CASE(guard_test_band_edges), KUNIT_CASE(guard_test_hysteresis), @@ -833,6 +995,12 @@ static struct kunit_case guard_test_cases[] = { KUNIT_CASE(readback_test_apply), KUNIT_CASE(readback_test_mismatch), KUNIT_CASE(readback_test_inhibit), + KUNIT_CASE(stop_test_effective), + KUNIT_CASE(defend_test_latch), + KUNIT_CASE(defend_test_conditions), + KUNIT_CASE(defend_test_unplug_flag), + KUNIT_CASE(defend_test_unplug_chgin), + KUNIT_CASE(defend_test_readback), { } }; From 9a079c7449491d92319b49a9eb5d841a6a766293 Mon Sep 17 00:00:00 2001 From: Lef Date: Wed, 18 Nov 2026 10:46:33 +0200 Subject: [PATCH 7/9] power: supply: max77759: put the husky float back on unbind and shutdown On husky, unbind and a new .shutdown put back the float the bootloader left (read at probe before the battery's float replaces it, and capped at the 4.35 V the husky bootloader sets, so a float left by an earlier driver run is not taken for the bootloader's, and taken to be 4.35 V when it cannot be read), but only when the chip's float is above it, so this never raises the float. The restore is read back. When the float cannot be read, or the chip does not hold the restored one, that is logged and charging is stopped (buck only); unbind then leaves it stopped instead of clearing the stop, so the phone does not charge on what may be the raised float. Charging comes back when the driver is bound again, or at the next boot when the bootloader rewrites the charger mode. On unbind the order is: the guard work is cancelled, the float is restored, a threshold stop is cleared (unless the restore failed), and only then is charging left on, so charging never resumes on the raised float. .shutdown disables the guard work on every board, the Pixel 7 Pro, the 6 Pro and the Pixel 7 too, so no guard run, not even one the notifier kicks, writes to the chip after it; the float restore that follows acts on husky only. .shutdown does not clear a threshold or defender stop: a stop in place at shutdown (buck only) stays until the bootloader rewrites the charger mode. With the husky battery node at 4.20 V and the bootloader at 4.35 V the restore does nothing today; it matters once the float is raised. The restore rule is a side-effect free function with KUnit tests (4200 / 4350 and 4350 / 4350 leave it, 4360 / 4350 lowers it, an unknown boot float leaves it), as is the boot float (husky caps it at 4350 mV and falls back to 4350 mV); the restore on husky, the untouched other boards, and a failed read and a failed read-back (charging stays stopped through unbind) are tested on a regmap backed by an array. Tested: KUnit under UML; arm64 objects at W=1 with and without the KUnit option. On a Pixel 8 Pro, 2026-11-18, slot B, kernel 7.3.0-rc6-omamobile-push1-gc4d94a1c1fe5 (an earlier head of this series, linux-omarchy-mobile#266, before the -EINVAL above the top current step and the read-back values in the dev_err, which KUnit covers, on the published base, nothing else on top; the charger options from /etc/modprobe.d, temp_guard=1 charge_defender=1 default_ilim_ua=500000; a 500 mA desktop USB port): the probe read the bootloader's 4350 mV and wrote 4200 mV, so nothing was raised and the restore had nothing to do; a reboot went through .shutdown and the next boot found the bootloader's mode 0x5 and 4350 mV again. On 2026-11-16, on an earlier form of this series with the float raised to 4.45 V, unbinding put it back to 4.35 V (0xbd 0x2d to 0x23). Not tested: a restore that lowers the float on the head of this series (unbind or .shutdown), a failed restore on hardware, poweroff, anything on a Pixel 7 Pro, 6 Pro or Pixel 7. Signed-off-by: Lef --- drivers/power/supply/max77759_charger.c | 102 +++++++++++++++++- drivers/power/supply/max77759_charger_test.c | 104 ++++++++++++++++++- 2 files changed, 201 insertions(+), 5 deletions(-) diff --git a/drivers/power/supply/max77759_charger.c b/drivers/power/supply/max77759_charger.c index adce0ccc1f..d819776df2 100644 --- a/drivers/power/supply/max77759_charger.c +++ b/drivers/power/supply/max77759_charger.c @@ -78,6 +78,7 @@ MODULE_PARM_DESC(charge_defender, #define DEFEND_END 80 #define DEFEND_START 79 #define STOP_RESUME_GAP 5 /* resume at end - 5 when start is 0 */ +#define HUSKY_BOOT_FV_MV 4350 /* float the husky bootloader sets */ #define GUARD_PERIOD_MS 30000 #define GUARD_RETRY_MS 2000 /* while there is no reading */ @@ -201,6 +202,8 @@ struct max77759_charger { bool defended; /* defender latched until unplugged */ bool unplugged; /* psy work saw the source go, under lock */ ktime_t stop_last; + u32 boot_fv_mv; /* float the bootloader left, 0: unknown */ + bool float_unsafe; /* restore failed: keep the stop on unbind */ bool apply_failed; /* the chip did not hold the guard's limits */ /* Temperature guard */ @@ -886,6 +889,20 @@ static int max77759_init_irqhandler(struct max77759_charger *chg) return 0; } +/* + * The float to restore, from what the chip holds at probe. On husky a + * float left by an earlier driver run is no bootloader's, so it is capped + * at the bootloader's, and an unreadable one is taken to be it. + */ +static u32 boot_float(bool husky, int read_mv) +{ + u32 mv = read_mv > 0 ? read_mv : 0; + + if (husky) + return mv ? min(mv, HUSKY_BOOT_FV_MV) : HUSKY_BOOT_FV_MV; + return mv; +} + static int max77759_charger_init(struct max77759_charger *chg) { struct power_supply_battery_info *info; @@ -901,6 +918,10 @@ static int max77759_charger_init(struct max77759_charger *chg) if (ret) return ret; + /* The float the bootloader left, before the battery's replaces it */ + chg->boot_fv_mv = boot_float(chg->guard_table == &guard_table_husky, + get_float_voltage(chg)); + if (power_supply_get_battery_info(chg->psy, &info)) { fv = CHG_FV_DEFAULT_MV; fast_chg_curr = CHG_CC_DEFAULT_UA; @@ -1469,11 +1490,69 @@ static void max77759_charger_log_state(struct max77759_charger *chg, fv, cc, ilim); } -/* On unbind, no charge threshold holds charging off any more */ +/* Restore the bootloader's float only to lower the chip's, never to raise it */ +static bool restore_float_needed(u32 cur_mv, u32 boot_mv) +{ + return boot_mv && cur_mv > boot_mv; +} + +/* + * Pixel 8 Pro: leave no float above the bootloader's behind once the + * driver, and with it the guard and the defender, is gone. When the float + * cannot be read or put back, charging is stopped (buck only) and the + * unbind leaves it stopped, rather than charge on a float that may be the + * raised one. + */ +static int restore_float(struct max77759_charger *chg) +{ + u32 want; + int cur; + + if (chg->guard_table != &guard_table_husky) + return 0; + + cur = get_float_voltage(chg); + if (cur < 0) { + dev_err(chg->dev, "could not read the float (%d): charging stopped\n", + cur); + goto fail; + } + if (!restore_float_needed(cur, chg->boot_fv_mv)) + return 0; + + if (fv_quantize(chg->boot_fv_mv, &want) || + set_float_voltage_limit(chg, chg->boot_fv_mv) || + get_float_voltage(chg) != (int)want) { + dev_err(chg->dev, + "could not restore the float to %u mV: charging stopped\n", + chg->boot_fv_mv); + goto fail; + } + dev_info(chg->dev, "float restored to %u mV\n", chg->boot_fv_mv); + return 0; + +fail: + chg->float_unsafe = true; + charger_set_inhibit(chg, &chg->stop_inhibit, true); + return -EIO; +} + +static void max77759_charger_restore_float(void *data) +{ + restore_float(data); +} + +/* + * On unbind, no charge threshold holds charging off any more, unless the + * float could not be restored: then charging stays stopped. + */ static void max77759_charger_clear_stop(void *data) { struct max77759_charger *chg = data; + if (chg->float_unsafe) + return; + charger_set_inhibit(chg, &chg->stop_inhibit, false); } @@ -1522,6 +1601,7 @@ static int max77759_charger_probe(struct platform_device *pdev) max77759_charger_log_state(chg, "found"); + chg->guard_table = guard_select_table(); ret = max77759_charger_init(chg); if (ret) return dev_err_probe(dev, ret, @@ -1542,13 +1622,18 @@ static int max77759_charger_probe(struct platform_device *pdev) /* * devm undoes in reverse: on unbind the guard work is cancelled, - * then a threshold stop is cleared, and only then is charging - * left on. + * then the float is restored, then a threshold stop is cleared, + * and only then is charging left on. */ ret = devm_add_action_or_reset(dev, max77759_charger_clear_stop, chg); if (ret) return ret; + ret = devm_add_action_or_reset(dev, max77759_charger_restore_float, + chg); + if (ret) + return ret; + chgin_otg_reg_cfg.dev = dev; chgin_otg_reg_cfg.driver_data = chg; chgin_otg_reg_cfg.of_node = dev_of_node(dev); @@ -1567,7 +1652,6 @@ static int max77759_charger_probe(struct platform_device *pdev) * unregisters the notifier before it cancels the work. */ chg->guard_band = -2; - chg->guard_table = guard_select_table(); chg->stop_last = ktime_get_boottime(); ret = devm_delayed_work_autocancel(dev, &chg->guard_work, guard_work_item); if (ret) @@ -1599,6 +1683,15 @@ static int max77759_charger_probe(struct platform_device *pdev) return 0; } +static void max77759_charger_shutdown(struct platform_device *pdev) +{ + struct max77759_charger *chg = platform_get_drvdata(pdev); + + /* No guard run after this, not even one the notifier kicks */ + disable_delayed_work_sync(&chg->guard_work); + max77759_charger_restore_float(chg); +} + static const struct platform_device_id max77759_charger_id[] = { { .name = "max77759-charger" }, { } @@ -1611,6 +1704,7 @@ static struct platform_driver max77759_charger_driver = { .probe_type = PROBE_PREFER_ASYNCHRONOUS, }, .probe = max77759_charger_probe, + .shutdown = max77759_charger_shutdown, .id_table = max77759_charger_id, }; module_platform_driver(max77759_charger_driver); diff --git a/drivers/power/supply/max77759_charger_test.c b/drivers/power/supply/max77759_charger_test.c index 94841f16c9..dcd62941bf 100644 --- a/drivers/power/supply/max77759_charger_test.c +++ b/drivers/power/supply/max77759_charger_test.c @@ -2,7 +2,8 @@ /* * KUnit tests for the battery temperature guard of the MAX77759 charger * driver: bands, hysteresis, tiers, limits and the tables themselves; the - * charge thresholds, the read-back of the limits and the defender. + * charge thresholds, the defender, the read-back of the limits and the + * float restore. * * This file is included by max77759_charger.c and can not be built alone. */ @@ -433,6 +434,7 @@ struct fake_chip { int nread; int corrupt_reg; /* reads of this register come back wrong */ int fail_writes; /* this many writes fail with -EIO first */ + int fail_reads; /* this many reads fail with -EIO first */ }; static int fake_read(void *ctx, unsigned int reg, unsigned int *val) @@ -440,6 +442,10 @@ static int fake_read(void *ctx, unsigned int reg, unsigned int *val) struct fake_chip *c = ctx; c->nread++; + if (c->fail_reads) { + c->fail_reads--; + return -EIO; + } *val = c->regs[reg]; if (reg == c->corrupt_reg) *val ^= 0x01; @@ -968,6 +974,97 @@ static void defend_test_readback(struct kunit *test) KUNIT_EXPECT_EQ(test, chg_prop_get(psy, start), 0); } +static void restore_test_never_raises(struct kunit *test) +{ + /* The husky bootloader leaves 4350 mV */ + KUNIT_EXPECT_FALSE(test, restore_float_needed(4200, 4350)); + KUNIT_EXPECT_FALSE(test, restore_float_needed(4350, 4350)); + KUNIT_EXPECT_TRUE(test, restore_float_needed(4360, 4350)); + KUNIT_EXPECT_TRUE(test, restore_float_needed(4450, 4350)); + /* Unknown boot float: leave it */ + KUNIT_EXPECT_FALSE(test, restore_float_needed(4450, 0)); +} + +static void restore_test_action(struct kunit *test) +{ + struct fake_chip *c; + struct max77759_charger *chg = fake_charger(test, &c); + u8 *fv = &c->regs[MAX77759_CHGR_REG_CHG_CNFG_04]; + + chg->boot_fv_mv = 4350; + + /* husky above the boot float: back to 4350 mV */ + chg->guard_table = &guard_table_husky; + *fv = 0x2d; + max77759_charger_restore_float(chg); + KUNIT_EXPECT_EQ(test, *fv, 0x23); + + /* husky below it: untouched */ + c->nlog = 0; + *fv = 0x14; + max77759_charger_restore_float(chg); + KUNIT_EXPECT_EQ(test, *fv, 0x14); + KUNIT_EXPECT_EQ(test, c->nlog, 0); + + /* Another board: untouched, not even read */ + chg->guard_table = &guard_table_default; + c->nread = 0; + *fv = 0x2d; + max77759_charger_restore_float(chg); + KUNIT_EXPECT_EQ(test, *fv, 0x2d); + KUNIT_EXPECT_EQ(test, c->nlog, 0); + KUNIT_EXPECT_EQ(test, c->nread, 0); +} + +/* A restore that does not hold: charging stopped, and unbind keeps it so */ +static void restore_test_readback_fail(struct kunit *test) +{ + struct fake_chip *c; + struct max77759_charger *chg = fake_charger(test, &c); + u8 *mode = &c->regs[MAX77759_CHGR_REG_CHG_CNFG_00]; + + chg->guard_table = &guard_table_husky; + chg->boot_fv_mv = 4350; + chg->mode = MAX77759_CHGR_MODE_CHG_BUCK_ON; + *mode = MAX77759_CHGR_MODE_CHG_BUCK_ON; + c->regs[MAX77759_CHGR_REG_CHG_CNFG_04] = 0x2d; + c->corrupt_reg = MAX77759_CHGR_REG_CHG_CNFG_04; + KUNIT_EXPECT_EQ(test, restore_float(chg), -EIO); + KUNIT_EXPECT_TRUE(test, chg->stop_inhibit); + KUNIT_EXPECT_EQ(test, *mode, MAX77759_CHGR_MODE_BUCK_ON); + + max77759_charger_clear_stop(chg); + KUNIT_EXPECT_TRUE(test, chg->stop_inhibit); + KUNIT_EXPECT_EQ(test, *mode, MAX77759_CHGR_MODE_BUCK_ON); +} + +/* A float that cannot be read is a failed restore too */ +static void restore_test_read_fail(struct kunit *test) +{ + struct fake_chip *c; + struct max77759_charger *chg = fake_charger(test, &c); + + chg->guard_table = &guard_table_husky; + chg->boot_fv_mv = 4350; + chg->mode = MAX77759_CHGR_MODE_CHG_BUCK_ON; + c->fail_reads = 1; + KUNIT_EXPECT_EQ(test, restore_float(chg), -EIO); + KUNIT_EXPECT_TRUE(test, chg->stop_inhibit); + KUNIT_EXPECT_TRUE(test, chg->float_unsafe); +} + +/* The float to restore: husky caps it and falls back to 4350 mV */ +static void restore_test_boot_float(struct kunit *test) +{ + KUNIT_EXPECT_EQ(test, boot_float(true, 4350), 4350); + KUNIT_EXPECT_EQ(test, boot_float(true, 4200), 4200); + KUNIT_EXPECT_EQ(test, boot_float(true, 4450), 4350); + KUNIT_EXPECT_EQ(test, boot_float(true, 0), 4350); + KUNIT_EXPECT_EQ(test, boot_float(true, -EIO), 4350); + KUNIT_EXPECT_EQ(test, boot_float(false, 4450), 4450); + KUNIT_EXPECT_EQ(test, boot_float(false, -EIO), 0); +} + static struct kunit_case guard_test_cases[] = { KUNIT_CASE(guard_test_band_edges), KUNIT_CASE(guard_test_hysteresis), @@ -1001,6 +1098,11 @@ static struct kunit_case guard_test_cases[] = { KUNIT_CASE(defend_test_unplug_flag), KUNIT_CASE(defend_test_unplug_chgin), KUNIT_CASE(defend_test_readback), + KUNIT_CASE(restore_test_never_raises), + KUNIT_CASE(restore_test_action), + KUNIT_CASE(restore_test_readback_fail), + KUNIT_CASE(restore_test_read_fail), + KUNIT_CASE(restore_test_boot_float), { } }; From 617d8c6a3ab75a9adbf346f05f18434be680866e Mon Sep 17 00:00:00 2001 From: Lef Date: Mon, 16 Nov 2026 18:17:00 +0200 Subject: [PATCH 8/9] power: supply: max77759: restart a charge that ends early on husky The chip can report DONE below the float: on a husky left at the bootloader's 4.35 V float, CHG_DTLS read DONE with the gauge at 4.296 V and 92 %. The stock husky charger (max77759_is_full()) only takes that as full in the last voltage tier and within 50 mV of the float; otherwise it turns the charger off and on again so the charge goes on. Do the same in the temperature guard, on husky only. The last tier is the first one whose voltage reaches the float the guard applies: at the battery node's 4.20 V that is the first tier, so a DONE at 4.20 V is full there and does not restart. - in the last tier, DONE at vbatt >= float - 50 mV is full, and stays full until the capacity falls to 95 % or the voltage 250 mV below the float; - in a band that reaches the last tier, DONE in a lower tier is never full (a small adapter under load can reach top-off there, as Google's comment at the top-off interrupt says); - otherwise set the mode off and back to charging, at most once per 10 minutes and 5 times per plug-in, then warn once. Only a restart that ran counts: none while charging is held off or when the off write fails. Putting the mode back is tried three times; then an error is logged and the next guard run, with temp_guard on or off, writes the mode again; - in a band capped below the last tier, DONE is that band's limit and nothing is done, and a full found earlier is cleared. A plug-in gets its restarts back when the psy work sees the source go or, since husky's Type-C node is disabled and no TCPM supply notifies, when the guard run reads CHGIN clear, as the defender does. An unplug and replug that both fall between two runs is missed. While the check runs, status reads Full only for a DONE it accepted; a DONE it restarts, refuses or takes as a band's limit, has not looked at yet (up to one guard period), or sees while the guard has no reading or holds charging off, reads Not charging. It runs only with temp_guard on (off by default), and never while the guard or a charge threshold holds charging off. Other boards are unchanged. The guard run after the gauge read is split out as guard_run(), so KUnit can drive it whole. The rule is a side-effect free function with tests for the full margin, a DONE below the last tier, the tier in force, the recharge levels and the rate limit; the restart writes (off then on), the CHGIN reset, the status and whole guard runs are tested on the array-backed regmap. Tested: KUnit under UML (50 cases pass); an arm64 W=1 build of the driver without the KUnit option. Not tested: anything on hardware at this head. The restart (mode off, then on, with the cable in), its count, and the count coming back after a replug have only run in KUnit; they need one attended run on a weak port with the screen on, at the 4.20 V float, which is not a raised one. Whether CHGIN stays set in CHG_INT_OK across the off/on restart is not known; if it drops, a guard run that sees it clear gives the count back. Nothing on a Pixel 7 Pro, 6 Pro or Pixel 7. Signed-off-by: Lef --- drivers/power/supply/max77759_charger.c | 304 +++++++++++++-- drivers/power/supply/max77759_charger_test.c | 366 ++++++++++++++++++- 2 files changed, 644 insertions(+), 26 deletions(-) diff --git a/drivers/power/supply/max77759_charger.c b/drivers/power/supply/max77759_charger.c index d819776df2..f43004c579 100644 --- a/drivers/power/supply/max77759_charger.c +++ b/drivers/power/supply/max77759_charger.c @@ -80,6 +80,42 @@ MODULE_PARM_DESC(charge_defender, #define STOP_RESUME_GAP 5 /* resume at end - 5 when start is 0 */ #define HUSKY_BOOT_FV_MV 4350 /* float the husky bootloader sets */ +/* + * DONE check of the stock husky charger (max77759_is_full()): in the last + * tier the chip's DONE counts as full only within DONE_FULL_MARGIN_UV of + * the float; below it charging is restarted, as stock does, at most once + * per DONE_RESTART_GAP_S and DONE_RESTART_MAX times per plug-in. A full + * battery is charged again once the capacity falls to DONE_RECHARGE_SOC or + * the voltage DONE_RECHARGE_UV below the float. + */ +#define DONE_FULL_MARGIN_UV 50000 +#define DONE_RECHARGE_UV 250000 +#define DONE_RECHARGE_SOC 95 +#define DONE_RESTART_GAP_S 600 +#define DONE_RESTART_MAX 5 + +enum done_action { + DONE_NONE, /* nothing to do */ + DONE_FULL, /* done counts as full */ + DONE_RESTART, /* restart charging */ + DONE_REFUSED, /* would restart, but out of restarts */ + DONE_LIMIT, /* the band's own limit, below the last tier */ +}; + +#define DONE_UNCHECKED -1 /* no DONE check ran */ + +/** + * struct done_state - DONE check state, kept per plug-in + * @full: the battery was found full and stays so until recharge + * @restarts: restarts since plug-in + * @last_s: time of the last restart, in seconds + */ +struct done_state { + bool full; + int restarts; + s64 last_s; +}; + #define GUARD_PERIOD_MS 30000 #define GUARD_RETRY_MS 2000 /* while there is no reading */ #define GUARD_HYST_DECIC 20 /* 2.0 C into a band before relaxing */ @@ -201,6 +237,11 @@ struct max77759_charger { u32 defend_s; /* time counted towards the defender */ bool defended; /* defender latched until unplugged */ bool unplugged; /* psy work saw the source go, under lock */ + struct done_state done; /* DONE check, husky only */ + bool done_warned; /* out of restarts said once */ + int done_last; /* last DONE check, or DONE_UNCHECKED, under lock */ + bool done_active; /* the DONE check is in force, under lock */ + bool mode_rewrite; /* a restart left the mode off, under lock */ ktime_t stop_last; u32 boot_fv_mv; /* float the bootloader left, 0: unknown */ bool float_unsafe; /* restore failed: keep the stop on unbind */ @@ -277,6 +318,16 @@ static int get_status(struct max77759_charger *chg) case MAX77759_CHGR_CHG_DTLS_TO: return POWER_SUPPLY_STATUS_CHARGING; case MAX77759_CHGR_CHG_DTLS_DONE: + /* + * While the DONE check runs (husky, temp_guard on), Full only + * for a DONE it accepted; until it has looked, for one it + * restarts or refused, and while the guard has no reading or + * holds charging off, the charge has stopped short of full. + */ + scoped_guard(mutex, &chg->lock) { + if (chg->done_active && chg->done_last != DONE_FULL) + return POWER_SUPPLY_STATUS_NOT_CHARGING; + } return POWER_SUPPLY_STATUS_FULL; case MAX77759_CHGR_CHG_DTLS_TIMER_FAULT: case MAX77759_CHGR_CHG_DTLS_SUSP_BATT_THM: @@ -1330,6 +1381,187 @@ static int stop_read_soc(struct max77759_charger *chg, int *soc) return ret; } +/* + * One DONE check. @done: the chip reports DONE; @band_last: the band + * charges up to the table's last tier; @last_tier: charging in that tier; + * @uv: battery voltage; @fv_mv: the float in force; @soc: capacity, or + * 100 when unknown; @now_s: a monotonic time in s. + */ +static enum done_action done_step(struct done_state *st, bool done, + bool band_last, bool last_tier, int uv, + u32 fv_mv, int soc, s64 now_s) +{ + s64 fv_uv = (s64)fv_mv * 1000; + + if (!done) { + st->full = false; + return DONE_NONE; + } + /* Capped below the last tier: done there is the band's limit */ + if (!band_last) { + st->full = false; + return DONE_LIMIT; + } + /* + * Below the last tier done is never full, as in stock: a small + * adapter under load can reach top-off far from it. Restart. + */ + if (last_tier && uv >= fv_uv - DONE_FULL_MARGIN_UV) { + st->full = true; + return DONE_FULL; + } + if (last_tier && st->full && soc > DONE_RECHARGE_SOC && + uv >= fv_uv - DONE_RECHARGE_UV) + return DONE_FULL; + st->full = false; + + if (st->restarts >= DONE_RESTART_MAX) + return DONE_REFUSED; + if (st->restarts && now_s - st->last_s < DONE_RESTART_GAP_S) + return DONE_NONE; + return DONE_RESTART; +} + +/* Count a restart that ran */ +static void done_restarted(struct done_state *st, s64 now_s) +{ + st->restarts++; + st->last_s = now_s; +} + +/* + * The tier whose float is the one in force: the first whose voltage + * reaches @fv_cap_mv, the cap the guard applies. At a 4.20 V cap that is + * the first tier, at 4.45 V on husky the last. @band_last: the band + * charges up to that tier; @last_tier: charging in it. + */ +static void done_tiers(const struct guard_table *tbl, int band, int tier, + u32 fv_cap_mv, bool *band_last, bool *last_tier) +{ + int full = 0; + + while (full < tbl->ntiers - 1 && tbl->cv_mv[full] < fv_cap_mv) + full++; + *band_last = guard_top_tier(tbl, band) >= full; + *last_tier = *band_last && tier >= full; +} + +#define DONE_RESTART_TRIES 3 + +/* + * Charge mode off, then back on, as stock restarts a charge. Returns + * whether it ran, that is the mode went off. Putting the mode back is + * tried DONE_RESTART_TRIES times; if that fails it says so and marks + * the mode for the next guard run to write again. + */ +static bool charger_restart(struct max77759_charger *chg) +{ + int i, ret; + + guard(mutex)(&chg->lock); + + if (chg->mode != MAX77759_CHGR_MODE_CHG_BUCK_ON || + chg->inhibit || chg->stop_inhibit) + return false; + if (regmap_update_bits(chg->regmap, MAX77759_CHGR_REG_CHG_CNFG_00, + MAX77759_CHGR_REG_CHG_CNFG_00_MODE, + MAX77759_CHGR_MODE_OFF)) + return false; + for (i = 0; i < DONE_RESTART_TRIES; i++) { + ret = charger_write_mode(chg, chg->mode); + if (!ret) + return true; + } + dev_err(chg->dev, "charging left off after a restart (%d)\n", ret); + chg->mode_rewrite = true; + return true; +} + +/* A mode left off by a failed restart: write it again, under the lock */ +static void charger_mode_fix(struct max77759_charger *chg) +{ + guard(mutex)(&chg->lock); + + if (chg->mode_rewrite && !charger_write_mode(chg, chg->mode)) { + chg->mode_rewrite = false; + dev_info(chg->dev, "charge mode written again after a restart\n"); + } +} + +/* + * Husky: a new plug-in gets its restarts back. No TCPM supply notifies the + * psy work there, so read CHGIN each run, as the defender does. An unplug + * and replug that both fall between two runs is missed. + */ +static void done_plug_check(struct max77759_charger *chg) +{ + if (!temp_guard || chg->guard_table != &guard_table_husky || + charger_input_valid(chg) != 0) + return; + chg->done = (struct done_state){ }; + chg->done_warned = false; +} + +/* + * @active: the DONE check is in force (husky, temp_guard on), so status + * reads Full only for a DONE it accepted. + */ +static void done_set_last(struct max77759_charger *chg, bool active, + int action) +{ + guard(mutex)(&chg->lock); + chg->done_active = active; + chg->done_last = action; +} + +/* + * Husky: the DONE check, after the guard has set the band and tier. + * Returns the action, or DONE_UNCHECKED on other boards or a failed read. + */ +static int done_work(struct max77759_charger *chg, int band, int tier, int uv, + u32 fv_mv) +{ + const struct guard_table *tbl = chg->guard_table; + s64 now_s = div_s64(ktime_get_boottime_ns(), NSEC_PER_SEC); + enum done_action action; + bool band_last, last_tier; + int soc = 100; + u32 val; + + if (tbl != &guard_table_husky) + return DONE_UNCHECKED; + if (regmap_read(chg->regmap, MAX77759_CHGR_REG_CHG_DETAILS_01, &val)) + return DONE_UNCHECKED; + + done_tiers(tbl, band, tier, chg->fv_max_mv, &band_last, &last_tier); + if (chg->done.full) + stop_read_soc(chg, &soc); + + action = done_step(&chg->done, + FIELD_GET(MAX77759_CHGR_REG_CHG_DETAILS_01_CHG_DTLS, val) == + MAX77759_CHGR_CHG_DTLS_DONE, + band_last, last_tier, uv, fv_mv, soc, now_s); + switch (action) { + case DONE_RESTART: + if (!charger_restart(chg)) + break; + done_restarted(&chg->done, now_s); + dev_info(chg->dev, "done at %d mV, below %u mV: restart %d\n", + uv / 1000, fv_mv, chg->done.restarts); + break; + case DONE_REFUSED: + if (!chg->done_warned) + dev_warn(chg->dev, + "done at %d mV after %d restarts: no more until unplugged\n", + uv / 1000, DONE_RESTART_MAX); + chg->done_warned = true; + break; + default: + break; + } + return action; +} + /* * The charge thresholds and the defender. With the thresholds at 100 / 0 * and the defender off this reads nothing and writes nothing. @@ -1347,6 +1579,11 @@ static void stop_work(struct max77759_charger *chg) scoped_guard(mutex, &chg->lock) { unplugged = chg->unplugged; chg->unplugged = false; + /* A new plug-in gets its restarts back */ + if (unplugged) { + chg->done = (struct done_state){ }; + chg->done_warned = false; + } end = chg->end_threshold; start = chg->start_threshold; } @@ -1399,41 +1636,27 @@ static void stop_work(struct max77759_charger *chg) charger_set_inhibit(chg, &chg->stop_inhibit, stopped); } -static void guard_work_item(struct work_struct *work) +/* + * One guard run with temp_guard on, after the gauge read: @read_ok, and + * then @decic and @uv. Returns the delay to the next run, in ms. + */ +static unsigned int guard_run(struct max77759_charger *chg, bool read_ok, + int decic, int uv) { - struct max77759_charger *chg = - container_of(work, struct max77759_charger, guard_work.work); const struct guard_table *tbl = chg->guard_table; - int decic = 0, uv = 0, band, tier; unsigned int next_ms = GUARD_PERIOD_MS; - bool read_ok; + int done = DONE_UNCHECKED; + int band, tier; u32 fv, cc; - /* - * The charge thresholds first: a stop they call for is in place - * before the guard below can clear its own inhibit. - */ - stop_work(chg); - - if (!temp_guard) { - WRITE_ONCE(chg->guard_no_reading, false); - if (chg->guard_active) - guard_release(chg); - goto out; - } - + charger_mode_fix(chg); + done_plug_check(chg); if (!chg->guard_active) { chg->guard_active = true; chg->guard_band = -2; dev_info(chg->dev, "temperature guard on (%s)\n", guard_psy); } - /* - * No reading, no charging. A missing reading is usually the gauge - * still starting (it answers once its model load is done), so look - * again soon instead of a guard period later. - */ - read_ok = !guard_read(&decic, &uv); WRITE_ONCE(chg->guard_no_reading, !read_ok); if (!read_ok) next_ms = GUARD_RETRY_MS; @@ -1471,7 +1694,39 @@ static void guard_work_item(struct work_struct *work) fv, cc); chg->guard_band = band; chg->guard_tier = tier; + done = done_work(chg, band, tier, uv, fv); out: + done_set_last(chg, tbl == &guard_table_husky, done); + return next_ms; +} + +static void guard_work_item(struct work_struct *work) +{ + struct max77759_charger *chg = + container_of(work, struct max77759_charger, guard_work.work); + unsigned int next_ms = GUARD_PERIOD_MS; + int decic = 0, uv = 0; + + /* + * The charge thresholds first: a stop they call for is in place + * before the guard below can clear its own inhibit. + */ + stop_work(chg); + + if (!temp_guard) { + WRITE_ONCE(chg->guard_no_reading, false); + charger_mode_fix(chg); + if (chg->guard_active) + guard_release(chg); + done_set_last(chg, false, DONE_UNCHECKED); + } else { + /* + * No reading, no charging. A missing reading is usually the + * gauge still starting (it answers once its model load is + * done), so look again soon instead of a guard period later. + */ + next_ms = guard_run(chg, !guard_read(&decic, &uv), decic, uv); + } schedule_delayed_work(&chg->guard_work, msecs_to_jiffies(next_ms)); } @@ -1652,6 +1907,7 @@ static int max77759_charger_probe(struct platform_device *pdev) * unregisters the notifier before it cancels the work. */ chg->guard_band = -2; + chg->done_last = DONE_UNCHECKED; chg->stop_last = ktime_get_boottime(); ret = devm_delayed_work_autocancel(dev, &chg->guard_work, guard_work_item); if (ret) diff --git a/drivers/power/supply/max77759_charger_test.c b/drivers/power/supply/max77759_charger_test.c index dcd62941bf..df0be87d3e 100644 --- a/drivers/power/supply/max77759_charger_test.c +++ b/drivers/power/supply/max77759_charger_test.c @@ -434,7 +434,9 @@ struct fake_chip { int nread; int corrupt_reg; /* reads of this register come back wrong */ int fail_writes; /* this many writes fail with -EIO first */ + int pass_writes; /* ... after this many writes that work */ int fail_reads; /* this many reads fail with -EIO first */ + u8 vlog[32]; /* the value of each logged write */ }; static int fake_read(void *ctx, unsigned int reg, unsigned int *val) @@ -456,13 +458,17 @@ static int fake_write(void *ctx, unsigned int reg, unsigned int val) { struct fake_chip *c = ctx; - if (c->fail_writes) { + if (c->pass_writes) { + c->pass_writes--; + } else if (c->fail_writes) { c->fail_writes--; return -EIO; } c->regs[reg] = val; - if (c->nlog < ARRAY_SIZE(c->log)) + if (c->nlog < ARRAY_SIZE(c->log)) { + c->vlog[c->nlog] = val; c->log[c->nlog++] = reg; + } return 0; } @@ -493,6 +499,7 @@ static struct max77759_charger *fake_charger(struct kunit *test, KUNIT_ASSERT_NOT_ERR_OR_NULL(test, chg->regmap); mutex_init(&chg->lock); chg->end_threshold = 100; + chg->done_last = DONE_UNCHECKED; return chg; } @@ -596,6 +603,348 @@ static void stop_test_work_capacity(struct kunit *test) KUNIT_EXPECT_EQ(test, *mode, MAX77759_CHGR_MODE_CHG_BUCK_ON); } +/* DONE in the last tier at 4450 mV: full from 4400 mV, restart below */ +static void done_test_full_rule(struct kunit *test) +{ + struct done_state st = { }; + + KUNIT_EXPECT_EQ(test, done_step(&st, true, true, true, 4400000, 4450, 100, 0), + DONE_FULL); + KUNIT_EXPECT_TRUE(test, st.full); + + st = (struct done_state){ }; + KUNIT_EXPECT_EQ(test, done_step(&st, true, true, true, 4399000, 4450, 100, 0), + DONE_RESTART); + /* Only a restart that ran is counted, by the caller */ + KUNIT_EXPECT_EQ(test, st.restarts, 0); + + /* Not done: nothing; capped below the last tier: the band's limit */ + st = (struct done_state){ .full = true }; + KUNIT_EXPECT_EQ(test, done_step(&st, false, true, true, 4000000, 4450, 50, 0), + DONE_NONE); + st.full = true; + KUNIT_EXPECT_EQ(test, done_step(&st, true, false, false, 4000000, 4450, 50, 0), + DONE_LIMIT); + KUNIT_EXPECT_FALSE(test, st.full); + KUNIT_EXPECT_EQ(test, st.restarts, 0); +} + +/* Once full, done stays full until 95 % or 250 mV below the float */ +static void done_test_recharge(struct kunit *test) +{ + struct done_state st = { }; + + done_step(&st, true, true, true, 4440000, 4450, 100, 0); + KUNIT_EXPECT_EQ(test, done_step(&st, true, true, true, 4300000, 4450, 96, 10), + DONE_FULL); + KUNIT_EXPECT_EQ(test, done_step(&st, true, true, true, 4200000, 4450, 96, 20), + DONE_FULL); + KUNIT_EXPECT_EQ(test, done_step(&st, true, true, true, 4300000, 4450, 95, 30), + DONE_RESTART); + KUNIT_EXPECT_FALSE(test, st.full); + + st = (struct done_state){ }; + done_step(&st, true, true, true, 4440000, 4450, 100, 0); + KUNIT_EXPECT_EQ(test, done_step(&st, true, true, true, 4199000, 4450, 99, 10), + DONE_RESTART); + + /* Charging again clears full */ + st = (struct done_state){ .full = true }; + done_step(&st, false, true, true, 4300000, 4450, 99, 0); + KUNIT_EXPECT_FALSE(test, st.full); +} + +/* At most once per 10 min, 5 per plug-in, the 6th refused */ +static void done_test_rate_limit(struct kunit *test) +{ + struct done_state st = { }; + s64 t = 1000; + int i; + + KUNIT_EXPECT_EQ(test, done_step(&st, true, true, true, 4300000, 4450, 90, t), + DONE_RESTART); + done_restarted(&st, t); + KUNIT_EXPECT_EQ(test, done_step(&st, true, true, true, 4300000, 4450, 90, + t + 599), DONE_NONE); + for (i = 1; i < DONE_RESTART_MAX; i++) { + t += 600; + KUNIT_EXPECT_EQ_MSG(test, + done_step(&st, true, true, true, 4300000, 4450, 90, t), + DONE_RESTART, "restart %d", i + 1); + done_restarted(&st, t); + } + KUNIT_EXPECT_EQ(test, st.restarts, DONE_RESTART_MAX); + KUNIT_EXPECT_EQ(test, done_step(&st, true, true, true, 4300000, 4450, 90, + t + 6000), DONE_REFUSED); +} + +/* A restart writes mode 0 then the charge mode, and never while inhibited */ +static void done_test_restart_writes(struct kunit *test) +{ + struct fake_chip *c; + struct max77759_charger *chg = fake_charger(test, &c); + u8 *mode = &c->regs[MAX77759_CHGR_REG_CHG_CNFG_00]; + + chg->mode = MAX77759_CHGR_MODE_CHG_BUCK_ON; + *mode = MAX77759_CHGR_MODE_CHG_BUCK_ON; + KUNIT_EXPECT_TRUE(test, charger_restart(chg)); + KUNIT_ASSERT_EQ(test, c->nlog, 2); + KUNIT_EXPECT_EQ(test, c->log[0], MAX77759_CHGR_REG_CHG_CNFG_00); + KUNIT_EXPECT_EQ(test, c->vlog[0] & MAX77759_CHGR_REG_CHG_CNFG_00_MODE, + MAX77759_CHGR_MODE_OFF); + KUNIT_EXPECT_EQ(test, c->log[1], MAX77759_CHGR_REG_CHG_CNFG_00); + KUNIT_EXPECT_EQ(test, c->vlog[1] & MAX77759_CHGR_REG_CHG_CNFG_00_MODE, + MAX77759_CHGR_MODE_CHG_BUCK_ON); + + /* Inhibited: no write, not run */ + chg->stop_inhibit = true; + *mode = MAX77759_CHGR_MODE_BUCK_ON; + KUNIT_EXPECT_FALSE(test, charger_restart(chg)); + KUNIT_EXPECT_EQ(test, c->nlog, 2); + chg->stop_inhibit = false; + + /* The off write fails: not run */ + *mode = MAX77759_CHGR_MODE_CHG_BUCK_ON; + c->fail_writes = 1; + KUNIT_EXPECT_FALSE(test, charger_restart(chg)); + KUNIT_EXPECT_EQ(test, *mode, MAX77759_CHGR_MODE_CHG_BUCK_ON); +} + +/* The tier whose float is in force, at the 4.20 V node and at 4.45 V */ +static void done_test_tiers(struct kunit *test) +{ + const struct guard_table *tbl = &guard_table_husky; + bool band_last, last_tier; + + /* 4.20 V: the first tier already has the float in force */ + done_tiers(tbl, 2, 0, 4200, &band_last, &last_tier); + KUNIT_EXPECT_TRUE(test, band_last); + KUNIT_EXPECT_TRUE(test, last_tier); + done_tiers(tbl, 5, 0, 4200, &band_last, &last_tier); + KUNIT_EXPECT_TRUE(test, last_tier); + + /* 4.45 V: only the 4.48 V tier, in a band that reaches it */ + done_tiers(tbl, 2, 2, 4450, &band_last, &last_tier); + KUNIT_EXPECT_TRUE(test, band_last); + KUNIT_EXPECT_FALSE(test, last_tier); + done_tiers(tbl, 2, 3, 4450, &band_last, &last_tier); + KUNIT_EXPECT_TRUE(test, last_tier); + done_tiers(tbl, 4, 1, 4450, &band_last, &last_tier); + KUNIT_EXPECT_FALSE(test, band_last); + KUNIT_EXPECT_FALSE(test, last_tier); +} + +/* A husky charger in band 20-42 C with the cable in, guard on */ +static struct max77759_charger *done_charger(struct kunit *test, + struct fake_chip **c, u32 fv_mv) +{ + struct max77759_charger *chg = fake_charger(test, c); + + chg->guard_table = &guard_table_husky; + chg->guard_band = -2; + chg->fv_max_mv = fv_mv; + chg->cc_max_ua = 1485000; + chg->design_uah = 4950000; + chg->mode = MAX77759_CHGR_MODE_CHG_BUCK_ON; + (*c)->regs[MAX77759_CHGR_REG_CHG_CNFG_00] = + MAX77759_CHGR_MODE_CHG_BUCK_ON; + (*c)->regs[MAX77759_CHGR_REG_CHG_INT_OK] = + MAX77759_CHGR_REG_CHG_INT_CHGIN; + (*c)->regs[MAX77759_CHGR_REG_CHG_DETAILS_01] = + FIELD_PREP(MAX77759_CHGR_REG_CHG_DETAILS_01_CHG_DTLS, + MAX77759_CHGR_CHG_DTLS_DONE); + return chg; +} + +/* A guard run end to end: at the 4.20 V node a DONE at 4.19 V is full */ +static void done_test_guard_run_full(struct kunit *test) +{ + bool saved = temp_guard; + struct fake_chip *c; + struct max77759_charger *chg; + + temp_guard = true; + chg = done_charger(test, &c, 4200); + guard_run(chg, true, 300, 4190000); + KUNIT_EXPECT_EQ(test, chg->done_last, DONE_FULL); + KUNIT_EXPECT_EQ(test, get_status(chg), POWER_SUPPLY_STATUS_FULL); + KUNIT_EXPECT_EQ(test, chg->done.restarts, 0); + temp_guard = saved; +} + +/* One at 4.10 V restarts once, and a replug gives the count back */ +static void done_test_guard_run_restart(struct kunit *test) +{ + bool saved = temp_guard; + struct fake_chip *c; + struct max77759_charger *chg; + + temp_guard = true; + chg = done_charger(test, &c, 4200); + guard_run(chg, true, 300, 4100000); + KUNIT_EXPECT_EQ(test, chg->done_last, DONE_RESTART); + KUNIT_EXPECT_EQ(test, chg->done.restarts, 1); + KUNIT_EXPECT_EQ(test, c->regs[MAX77759_CHGR_REG_CHG_CNFG_00], + MAX77759_CHGR_MODE_CHG_BUCK_ON); + KUNIT_EXPECT_EQ(test, get_status(chg), + POWER_SUPPLY_STATUS_NOT_CHARGING); + + /* Unplugged: the count comes back */ + c->regs[MAX77759_CHGR_REG_CHG_INT_OK] = 0; + c->regs[MAX77759_CHGR_REG_CHG_DETAILS_01] = + FIELD_PREP(MAX77759_CHGR_REG_CHG_DETAILS_01_CHG_DTLS, + MAX77759_CHGR_CHG_DTLS_OFF); + guard_run(chg, true, 300, 4100000); + KUNIT_EXPECT_EQ(test, chg->done.restarts, 0); + temp_guard = saved; +} + +/* No reading: inhibited, unchecked, and DONE is not Full */ +static void done_test_guard_run_no_reading(struct kunit *test) +{ + bool saved = temp_guard; + struct fake_chip *c; + struct max77759_charger *chg; + + temp_guard = true; + chg = done_charger(test, &c, 4200); + guard_run(chg, false, 0, 0); + KUNIT_EXPECT_EQ(test, chg->done_last, DONE_UNCHECKED); + KUNIT_EXPECT_EQ(test, get_status(chg), + POWER_SUPPLY_STATUS_NOT_CHARGING); + + temp_guard = saved; +} + +/* The mode-on write fails three times: the next guard run writes it */ +static void done_test_mode_rewrite(struct kunit *test) +{ + bool saved = temp_guard; + struct fake_chip *c; + struct max77759_charger *chg; + u8 *mode; + + temp_guard = true; + chg = done_charger(test, &c, 4200); + mode = &c->regs[MAX77759_CHGR_REG_CHG_CNFG_00]; + c->pass_writes = 1; + c->fail_writes = DONE_RESTART_TRIES; + KUNIT_EXPECT_TRUE(test, charger_restart(chg)); + KUNIT_EXPECT_EQ(test, *mode, MAX77759_CHGR_MODE_OFF); + KUNIT_EXPECT_TRUE(test, chg->mode_rewrite); + + c->regs[MAX77759_CHGR_REG_CHG_DETAILS_01] = 0; + guard_run(chg, true, 300, 4000000); + KUNIT_EXPECT_EQ(test, *mode, MAX77759_CHGR_MODE_CHG_BUCK_ON); + KUNIT_EXPECT_FALSE(test, chg->mode_rewrite); + temp_guard = saved; +} + +static void done_test_noop_work(struct work_struct *work) +{ +} + +/* With temp_guard off, the guard work writes a mode left off again */ +static void done_test_mode_rewrite_guard_off(struct kunit *test) +{ + bool saved = temp_guard; + struct fake_chip *c; + struct max77759_charger *chg; + u8 *mode; + + temp_guard = false; + chg = done_charger(test, &c, 4200); + INIT_DELAYED_WORK(&chg->guard_work, done_test_noop_work); + mode = &c->regs[MAX77759_CHGR_REG_CHG_CNFG_00]; + *mode = MAX77759_CHGR_MODE_OFF; + chg->mode_rewrite = true; + guard_work_item(&chg->guard_work.work); + cancel_delayed_work_sync(&chg->guard_work); + KUNIT_EXPECT_EQ(test, *mode, MAX77759_CHGR_MODE_CHG_BUCK_ON); + KUNIT_EXPECT_FALSE(test, chg->mode_rewrite); + temp_guard = saved; +} + +/* + * A band that reaches the last tier, DONE in a lower tier: never full, + * even near the float, and full does not carry over; restart. + */ +static void done_test_below_last_tier(struct kunit *test) +{ + struct done_state st = { }; + + KUNIT_EXPECT_EQ(test, done_step(&st, true, true, false, 4440000, 4450, 100, 0), + DONE_RESTART); + KUNIT_EXPECT_FALSE(test, st.full); + + st = (struct done_state){ .full = true }; + KUNIT_EXPECT_EQ(test, done_step(&st, true, true, false, 4440000, 4450, 99, 0), + DONE_RESTART); + KUNIT_EXPECT_FALSE(test, st.full); +} + +/* Husky, guard on: CHGIN gone gives the restarts back, CHGIN there keeps them */ +static void done_test_plug_chgin(struct kunit *test) +{ + bool saved = temp_guard; + struct fake_chip *c; + struct max77759_charger *chg = fake_charger(test, &c); + + chg->guard_table = &guard_table_husky; + temp_guard = true; + + chg->done = (struct done_state){ .restarts = DONE_RESTART_MAX, + .last_s = 100 }; + chg->done_warned = true; + c->regs[MAX77759_CHGR_REG_CHG_INT_OK] = MAX77759_CHGR_REG_CHG_INT_CHGIN; + done_plug_check(chg); + KUNIT_EXPECT_EQ(test, chg->done.restarts, DONE_RESTART_MAX); + KUNIT_EXPECT_TRUE(test, chg->done_warned); + + c->regs[MAX77759_CHGR_REG_CHG_INT_OK] = 0; + done_plug_check(chg); + KUNIT_EXPECT_EQ(test, chg->done.restarts, 0); + KUNIT_EXPECT_FALSE(test, chg->done_warned); + + /* Other boards: no read */ + chg->guard_table = &guard_table_default; + chg->done.restarts = 3; + c->nread = 0; + done_plug_check(chg); + KUNIT_EXPECT_EQ(test, c->nread, 0); + KUNIT_EXPECT_EQ(test, chg->done.restarts, 3); + temp_guard = saved; +} + +/* While the check is in force, DONE reads Full only when it accepted it */ +static void done_test_status(struct kunit *test) +{ + static const int not_full[] = { DONE_NONE, DONE_RESTART, DONE_REFUSED, + DONE_LIMIT, DONE_UNCHECKED }; + struct fake_chip *c; + struct max77759_charger *chg = fake_charger(test, &c); + int i; + + c->regs[MAX77759_CHGR_REG_CHG_DETAILS_01] = + FIELD_PREP(MAX77759_CHGR_REG_CHG_DETAILS_01_CHG_DTLS, + MAX77759_CHGR_CHG_DTLS_DONE); + + /* No check in force (other boards, guard off): the chip's word */ + for (i = 0; i < ARRAY_SIZE(not_full); i++) { + chg->done_last = not_full[i]; + KUNIT_EXPECT_EQ(test, get_status(chg), POWER_SUPPLY_STATUS_FULL); + } + chg->done_active = true; + chg->done_last = DONE_FULL; + KUNIT_EXPECT_EQ(test, get_status(chg), POWER_SUPPLY_STATUS_FULL); + for (i = 0; i < ARRAY_SIZE(not_full); i++) { + chg->done_last = not_full[i]; + KUNIT_EXPECT_EQ_MSG(test, get_status(chg), + POWER_SUPPLY_STATUS_NOT_CHARGING, + "action %d", not_full[i]); + } +} + /* Either inhibit puts the chip in buck-only mode; both must clear */ static void inhibit_test_two_flags(struct kunit *test) { @@ -1103,6 +1452,19 @@ static struct kunit_case guard_test_cases[] = { KUNIT_CASE(restore_test_readback_fail), KUNIT_CASE(restore_test_read_fail), KUNIT_CASE(restore_test_boot_float), + KUNIT_CASE(done_test_full_rule), + KUNIT_CASE(done_test_recharge), + KUNIT_CASE(done_test_rate_limit), + KUNIT_CASE(done_test_restart_writes), + KUNIT_CASE(done_test_below_last_tier), + KUNIT_CASE(done_test_plug_chgin), + KUNIT_CASE(done_test_status), + KUNIT_CASE(done_test_tiers), + KUNIT_CASE(done_test_mode_rewrite), + KUNIT_CASE(done_test_mode_rewrite_guard_off), + KUNIT_CASE(done_test_guard_run_full), + KUNIT_CASE(done_test_guard_run_restart), + KUNIT_CASE(done_test_guard_run_no_reading), { } }; From c4aca6f27570fd399936030a8c567d1b7756884a Mon Sep 17 00:00:00 2001 From: Lef Date: Wed, 18 Nov 2026 19:24:40 +0200 Subject: [PATCH 9/9] power: supply: max77759: raise the husky float only while the guard runs The husky battery node asks for a 4.20 V float, and stays at that: a device tree that carries a higher float applies wherever that kernel is booted, guard or no guard, and so does a driver loaded by hand or with a stale options file. Give the guard table its own float cap, used in place of the battery description's while the guard runs, and set it to 4.45 V for husky. With temp_guard on, the guard takes the battery through the stock tiers up to 4.45 V, with the DONE check, whose last tier follows the cap. When temp_guard is set to 0 at run time, the next guard run puts the node's 4.20 V back, so the raised float can hold for up to one guard period (30 s) after. On unbind or shutdown the float is put back to the bootloader's (at most 4.35 V). With temp_guard off from boot (the default) nothing changes: 4.20 V at probe. Other boards' tables have no cap of their own and keep their node's float. No guard runs while the phone sleeps, so add suspend and resume hooks: suspend stops the guard work and, when the guard has raised the float, puts the node's 4.20 V back and reads it; if the chip does not take it, charging is inhibited instead, and if that write fails too the sleep is refused with -EIO. Resume starts the guard work again, and its first run raises the float if the band allows. Google's husky data (kernel/devices/google/shusky, branch android-gs-shusky-6.1-android16) has google,fv-max-uv = <4480000> in dts/zuma-shusky-charging.dtsi, lowered by google,aafv-ref-cycles and google,aafv-ref-offset in dts/zuma-husky-battery.dtsi by 20 mV per 200 cycles, to 4.38 V from 1000 cycles. 4.45 V is in neither: it is a choice, under the maximum for a new pack and over the schedule past 400 cycles, and stays open until the float is decided. Tested: KUnit under UML (54 cases pass), with cases for the cap the guard run uses on husky and on the default table, a whole guard run raising the float and taking a DONE at 4.42 V as full, a 46-48 C run whose DONE is the band's limit only under the husky cap, the sleep float (lowered, left alone at 4.20 V, raised again by the next run, inhibit when the write fails), and the suspend and resume hooks (float lowered, sleep refused when both writes fail, work queued again on resume). Not tested: anything on hardware, including the suspend and resume hooks on a real s2idle. Whether the chip still reports DONE right after a resume from a sleep at 4.20 V is not known: if it does, the first run after resume, back at 4.45 V, can spend one restart on it. An attended s2idle on the cable settles it. Nothing has charged at this float on this head, and nothing will until the limits on omarchy-mobile#761 are reviewed. Signed-off-by: Lef --- drivers/power/supply/max77759_charger.c | 90 +++++++++++- drivers/power/supply/max77759_charger_test.c | 145 ++++++++++++++++++- 2 files changed, 231 insertions(+), 4 deletions(-) diff --git a/drivers/power/supply/max77759_charger.c b/drivers/power/supply/max77759_charger.c index f43004c579..87917cad37 100644 --- a/drivers/power/supply/max77759_charger.c +++ b/drivers/power/supply/max77759_charger.c @@ -133,9 +133,13 @@ static const int guard_temp_decic[GUARD_NBANDS + 1] = { * @cv_mv: tier j charges up to cv_mv[j] * @cc_pct: charge current per band and tier, in percent of the design * capacity; 0 means the band does not charge in that tier + * @fv_max_mv: when set, the guard's float cap in place of the battery + * description's, so the float goes above that only while the + * guard runs */ struct guard_table { int ntiers; + u32 fv_max_mv; u32 cv_mv[GUARD_MAX_TIERS]; u8 cc_pct[GUARD_NBANDS][GUARD_MAX_TIERS]; }; @@ -159,9 +163,18 @@ static const struct guard_table guard_table_default = { * kernel/devices/google/shusky, branch android-gs-shusky-6.1-android16: * temperature and voltage limits from dts/zuma-shusky-battery.dtsi, * the 4950 mAh and the six rows from dts/zuma-husky-battery.dtsi. + * + * The battery node keeps the float at 4.20 V, so a kernel booted without + * the guard never charges higher. While the guard runs it caps the + * table at 4.45 V instead: under the board's google,fv-max-uv of 4.48 V + * (dts/zuma-shusky-charging.dtsi), which google,aafv-ref-cycles and + * google,aafv-ref-offset (dts/zuma-husky-battery.dtsi) lower by 20 mV + * per 200 cycles, to 4.38 V from 1000 cycles. 4.45 V is a choice, not + * from that schedule. */ static const struct guard_table guard_table_husky = { .ntiers = 4, + .fv_max_mv = 4450, .cv_mv = { 4200, 4300, 4350, 4480 }, .cc_pct = { { 50, 30, 0, 0 }, /* 0-10 C */ @@ -1245,6 +1258,12 @@ static void guard_limits(const struct guard_table *tbl, int band, int tier, *cc_ua = min(cc, cc_max_ua); } +/* The guard's float cap: the table's own, else the battery description's */ +static u32 guard_fv_cap(const struct guard_table *tbl, u32 fv_max_mv) +{ + return tbl->fv_max_mv ?: fv_max_mv; +} + static const struct guard_table *guard_select_table(void) { if (of_machine_is_compatible("google,zuma-husky")) @@ -1533,7 +1552,8 @@ static int done_work(struct max77759_charger *chg, int band, int tier, int uv, if (regmap_read(chg->regmap, MAX77759_CHGR_REG_CHG_DETAILS_01, &val)) return DONE_UNCHECKED; - done_tiers(tbl, band, tier, chg->fv_max_mv, &band_last, &last_tier); + done_tiers(tbl, band, tier, guard_fv_cap(tbl, chg->fv_max_mv), + &band_last, &last_tier); if (chg->done.full) stop_read_soc(chg, &soc); @@ -1679,7 +1699,8 @@ static unsigned int guard_run(struct max77759_charger *chg, bool read_ok, if (band != chg->guard_band) chg->guard_tier = 0; tier = guard_next_tier(tbl, band, chg->guard_tier, uv); - guard_limits(tbl, band, tier, chg->design_uah, chg->fv_max_mv, + guard_limits(tbl, band, tier, chg->design_uah, + guard_fv_cap(tbl, chg->fv_max_mv), chg->cc_max_ua, &fv, &cc); if (guard_apply(chg, fv, cc)) @@ -1939,6 +1960,70 @@ static int max77759_charger_probe(struct platform_device *pdev) return 0; } +/* + * Before sleep: no guard runs while asleep, so a float the guard raised + * above the battery description's would hold for as long as the phone + * sleeps on the cable, whatever the temperature does. Put the + * description's float back; the first guard run after resume raises it + * again if the band allows. If the chip does not take it, inhibit + * charging instead; that run clears the inhibit once it applies its + * limits. Returns 0 when the float is down or charging is inhibited, + * else an error: then the phone must not sleep. + */ +static int guard_sleep_float(struct max77759_charger *chg) +{ + int cur = get_float_voltage(chg); + u32 want; + int ret; + + if (cur >= 0 && cur <= (int)chg->fv_max_mv) + return 0; + ret = cur < 0 ? cur : fv_quantize(chg->fv_max_mv, &want); + if (!ret) + ret = set_float_voltage_limit(chg, chg->fv_max_mv); + if (!ret && get_float_voltage(chg) != (int)want) + ret = -EIO; + if (!ret) + return 0; + guard_set_inhibit(chg, true); + if (!chg->inhibit) { + dev_err(chg->dev, "could not lower the float or stop charging for sleep (%d): sleep refused\n", + ret); + return -EIO; + } + dev_err(chg->dev, "could not lower the float for sleep (%d): charging inhibited\n", + ret); + return 0; +} + +static int max77759_charger_suspend(struct device *dev) +{ + struct max77759_charger *chg = dev_get_drvdata(dev); + + int ret = 0; + + disable_delayed_work_sync(&chg->guard_work); + if (chg->guard_active && chg->guard_table->fv_max_mv) + ret = guard_sleep_float(chg); + if (ret) { + enable_delayed_work(&chg->guard_work); + schedule_delayed_work(&chg->guard_work, 0); + } + return ret; +} + +static int max77759_charger_resume(struct device *dev) +{ + struct max77759_charger *chg = dev_get_drvdata(dev); + + enable_delayed_work(&chg->guard_work); + schedule_delayed_work(&chg->guard_work, 0); + return 0; +} + +static DEFINE_SIMPLE_DEV_PM_OPS(max77759_charger_pm, max77759_charger_suspend, + max77759_charger_resume); + static void max77759_charger_shutdown(struct platform_device *pdev) { struct max77759_charger *chg = platform_get_drvdata(pdev); @@ -1958,6 +2043,7 @@ static struct platform_driver max77759_charger_driver = { .driver = { .name = "max77759-charger", .probe_type = PROBE_PREFER_ASYNCHRONOUS, + .pm = pm_sleep_ptr(&max77759_charger_pm), }, .probe = max77759_charger_probe, .shutdown = max77759_charger_shutdown, diff --git a/drivers/power/supply/max77759_charger_test.c b/drivers/power/supply/max77759_charger_test.c index df0be87d3e..955e8c7587 100644 --- a/drivers/power/supply/max77759_charger_test.c +++ b/drivers/power/supply/max77759_charger_test.c @@ -299,6 +299,34 @@ static void guard_test_limits_capped(struct kunit *test) } } +/* + * The husky table carries its own float cap, so its guard raises the + * float above the battery node's 4.20 V, to 4.45 V and no further; the + * default table has none and keeps the node's. + */ +static void guard_test_fv_cap(struct kunit *test) +{ + const struct guard_table *tbl = &guard_table_husky; + u32 fv, cc; + + KUNIT_EXPECT_EQ(test, guard_table_husky.fv_max_mv, 4450); + KUNIT_EXPECT_EQ(test, guard_table_default.fv_max_mv, 0); + + guard_limits(tbl, 2, 3, 4950000, guard_fv_cap(tbl, 4200), 1485000, &fv, &cc); + KUNIT_EXPECT_EQ(test, fv, 4450); + guard_limits(tbl, 2, 2, 4950000, guard_fv_cap(tbl, 4200), 1485000, &fv, &cc); + KUNIT_EXPECT_EQ(test, fv, 4450); + /* Bands capped below the last tier keep their own float */ + guard_limits(tbl, 4, 0, 4950000, guard_fv_cap(tbl, 4200), 1485000, &fv, &cc); + KUNIT_EXPECT_EQ(test, fv, 4300); + guard_limits(tbl, 5, 0, 4950000, guard_fv_cap(tbl, 4200), 1485000, &fv, &cc); + KUNIT_EXPECT_EQ(test, fv, 4200); + + /* What the guard run passes: the table's cap, else the node's */ + KUNIT_EXPECT_EQ(test, guard_fv_cap(&guard_table_husky, 4200), 4450); + KUNIT_EXPECT_EQ(test, guard_fv_cap(&guard_table_default, 4200), 4200); +} + /* The float is the step at or below the request, as stock rounds it */ static void round_test_float(struct kunit *test) { @@ -756,7 +784,10 @@ static struct max77759_charger *done_charger(struct kunit *test, return chg; } -/* A guard run end to end: at the 4.20 V node a DONE at 4.19 V is full */ +/* + * A guard run end to end: with the 4.20 V node the husky guard raises + * the float to 4.45 V, and a DONE at 4.42 V in the last tier is full + */ static void done_test_guard_run_full(struct kunit *test) { bool saved = temp_guard; @@ -765,7 +796,9 @@ static void done_test_guard_run_full(struct kunit *test) temp_guard = true; chg = done_charger(test, &c, 4200); - guard_run(chg, true, 300, 4190000); + guard_run(chg, true, 300, 4420000); + KUNIT_EXPECT_EQ(test, get_float_voltage(chg), 4450); + KUNIT_EXPECT_EQ(test, chg->guard_tier, 3); KUNIT_EXPECT_EQ(test, chg->done_last, DONE_FULL); KUNIT_EXPECT_EQ(test, get_status(chg), POWER_SUPPLY_STATUS_FULL); KUNIT_EXPECT_EQ(test, chg->done.restarts, 0); @@ -945,6 +978,110 @@ static void done_test_status(struct kunit *test) } } +/* + * Before sleep the raised float goes back to the node's 4.20 V and the + * next guard run raises it again; a float at or under it is left alone. + */ +static void guard_test_sleep_float(struct kunit *test) +{ + bool saved = temp_guard; + struct fake_chip *c; + struct max77759_charger *chg; + + temp_guard = true; + chg = done_charger(test, &c, 4200); + c->regs[MAX77759_CHGR_REG_CHG_DETAILS_01] = 0; + guard_run(chg, true, 300, 4000000); + KUNIT_ASSERT_EQ(test, get_float_voltage(chg), 4450); + + KUNIT_EXPECT_EQ(test, guard_sleep_float(chg), 0); + KUNIT_EXPECT_EQ(test, get_float_voltage(chg), 4200); + KUNIT_EXPECT_FALSE(test, chg->inhibit); + + c->nlog = 0; + KUNIT_EXPECT_EQ(test, guard_sleep_float(chg), 0); + KUNIT_EXPECT_EQ(test, c->nlog, 0); + + guard_run(chg, true, 300, 4000000); + KUNIT_EXPECT_EQ(test, get_float_voltage(chg), 4450); + + /* The chip does not take it: charging inhibited, sleep allowed */ + c->fail_writes = 1; + KUNIT_EXPECT_EQ(test, guard_sleep_float(chg), 0); + KUNIT_EXPECT_TRUE(test, chg->inhibit); + KUNIT_EXPECT_EQ(test, c->regs[MAX77759_CHGR_REG_CHG_CNFG_00], + MAX77759_CHGR_MODE_BUCK_ON); + temp_guard = saved; +} + +static int guard_test_work_runs; + +static void guard_test_count_work(struct work_struct *work) +{ + guard_test_work_runs++; +} + +/* + * The hooks: suspend lowers the raised float; when neither the float + * nor the inhibit write works it refuses the sleep; resume runs the + * guard work again. + */ +static void guard_test_suspend_hooks(struct kunit *test) +{ + bool saved = temp_guard; + struct fake_chip *c; + struct max77759_charger *chg; + + temp_guard = true; + chg = done_charger(test, &c, 4200); + INIT_DELAYED_WORK(&chg->guard_work, guard_test_count_work); + guard_test_work_runs = 0; + dev_set_drvdata(chg->dev, chg); + c->regs[MAX77759_CHGR_REG_CHG_DETAILS_01] = 0; + guard_run(chg, true, 300, 4000000); + KUNIT_ASSERT_EQ(test, get_float_voltage(chg), 4450); + + KUNIT_EXPECT_EQ(test, max77759_charger_suspend(chg->dev), 0); + KUNIT_EXPECT_EQ(test, get_float_voltage(chg), 4200); + /* Disabled while asleep: a queue attempt does nothing */ + KUNIT_EXPECT_FALSE(test, schedule_delayed_work(&chg->guard_work, 0)); + KUNIT_EXPECT_EQ(test, max77759_charger_resume(chg->dev), 0); + flush_delayed_work(&chg->guard_work); + KUNIT_EXPECT_EQ(test, guard_test_work_runs, 1); + + guard_run(chg, true, 300, 4000000); + KUNIT_ASSERT_EQ(test, get_float_voltage(chg), 4450); + c->fail_writes = 2; + KUNIT_EXPECT_LT(test, max77759_charger_suspend(chg->dev), 0); + KUNIT_EXPECT_FALSE(test, chg->inhibit); + /* Refused: the work is enabled and queued again */ + flush_delayed_work(&chg->guard_work); + KUNIT_EXPECT_EQ(test, guard_test_work_runs, 2); + KUNIT_EXPECT_TRUE(test, schedule_delayed_work(&chg->guard_work, 0)); + flush_delayed_work(&chg->guard_work); + KUNIT_EXPECT_EQ(test, guard_test_work_runs, 3); + temp_guard = saved; +} + +/* + * 46-48 C with the 4.20 V node: the band stops at 4.30 V, under the + * husky cap, so its DONE is the band's limit. With the node's 4.20 V as + * the cap it would count as full. + */ +static void done_test_guard_run_limit(struct kunit *test) +{ + bool saved = temp_guard; + struct fake_chip *c; + struct max77759_charger *chg; + + temp_guard = true; + chg = done_charger(test, &c, 4200); + guard_run(chg, true, 470, 4290000); + KUNIT_EXPECT_EQ(test, get_float_voltage(chg), 4300); + KUNIT_EXPECT_EQ(test, chg->done_last, DONE_LIMIT); + temp_guard = saved; +} + /* Either inhibit puts the chip in buck-only mode; both must clear */ static void inhibit_test_two_flags(struct kunit *test) { @@ -1425,6 +1562,10 @@ static struct kunit_case guard_test_cases[] = { KUNIT_CASE(guard_test_limits_default), KUNIT_CASE(guard_test_limits_husky), KUNIT_CASE(guard_test_limits_capped), + KUNIT_CASE(guard_test_fv_cap), + KUNIT_CASE(guard_test_sleep_float), + KUNIT_CASE(guard_test_suspend_hooks), + KUNIT_CASE(done_test_guard_run_limit), KUNIT_CASE(round_test_float), KUNIT_CASE(round_test_current), KUNIT_CASE(stop_test_set_threshold),