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