From 0d68188c380e3087be13813e7f0364a598806123 Mon Sep 17 00:00:00 2001 From: Lef Date: Sat, 14 Nov 2026 13:06:18 +0200 Subject: [PATCH 1/5] 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/5] 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/5] 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/5] 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/5] 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), { } };