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/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 = { 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..d819776df2 100644 --- a/drivers/power/supply/max77759_charger.c +++ b/drivers/power/supply/max77759_charger.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -43,38 +44,97 @@ 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); 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"); +/* + * Charge defender, as the stock Pixel 8 Pro has it (google_charger.c, the + * bd-* values of zuma-shusky-charging.dtsi): after six hours plugged in + * with the battery at 4.27 V or more and 35.0 C or more, stop charging at + * 80 % (buck only) and resume at 79 %, until the cable comes out. Nothing + * drains the battery: above 80 % it stays there until use brings it down. + * Time is counted by the guard work, which does not run while the phone + * sleeps. + */ +static bool charge_defender; +module_param(charge_defender, bool, 0644); +MODULE_PARM_DESC(charge_defender, + "Pixel 8 Pro only: after 6 h plugged in at 4.27 V or more and 35.0 C or more, stop charging at 80 % and resume at 79 % until unplugged (no active drain: a battery above 80 % stays there until used). Counted only while awake, and a charge under way goes on to the float while asleep. Default off."); + +#define DEFEND_UV 4270000 +#define DEFEND_DECIC 350 +#define DEFEND_S 21600 +#define DEFEND_MAX_STEP_S 60 /* most one guard run may count */ +#define DEFEND_END 80 +#define DEFEND_START 79 +#define STOP_RESUME_GAP 5 /* resume at end - 5 when start is 0 */ +#define HUSKY_BOOT_FV_MV 4350 /* float the husky bootloader sets */ + #define GUARD_PERIOD_MS 30000 #define GUARD_RETRY_MS 2000 /* while there is no reading */ #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) \ @@ -132,6 +192,19 @@ 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, and defender, run by the guard work */ + int end_threshold; + int start_threshold; + u32 defend_s; /* time counted towards the defender */ + bool defended; /* defender latched until unplugged */ + bool unplugged; /* psy work saw the source go, under lock */ + ktime_t stop_last; + u32 boot_fv_mv; /* float the bootloader left, 0: unknown */ + bool float_unsafe; /* restore failed: keep the stop on unbind */ + bool apply_failed; /* the chip did not hold the guard's limits */ /* Temperature guard */ struct delayed_work guard_work; @@ -139,6 +212,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; @@ -210,6 +284,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; @@ -338,16 +425,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, @@ -371,15 +476,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, @@ -420,6 +541,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, @@ -429,8 +618,31 @@ 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); +} + +/* The thresholds in force: the defender's, unless the user's are lower */ +static void stop_effective(int end, int start, bool defended, int *e, int *s) +{ + if (defended && end > DEFEND_END) { + *e = DEFEND_END; + *s = DEFEND_START; + } else { + *e = end; + *s = start; + } +} + static int max77759_charger_get_property(struct power_supply *psy, enum power_supply_property psp, union power_supply_propval *pval) @@ -463,6 +675,23 @@ 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: + case POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD: + /* + * The values in force: start reads 75 for an end of 80 and a + * start of 0, and the defender's 80 / 79 while it is latched. + */ + scoped_guard(mutex, &chg->lock) { + int e, s; + + stop_effective(chg->end_threshold, chg->start_threshold, + chg->defended, &e, &s); + if (psp == POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD) + ret = e; + else + ret = e >= 100 ? s : stop_resume(e, s); + } + break; default: ret = -EINVAL; } @@ -471,19 +700,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, @@ -610,6 +889,20 @@ static int max77759_init_irqhandler(struct max77759_charger *chg) return 0; } +/* + * The float to restore, from what the chip holds at probe. On husky a + * float left by an earlier driver run is no bootloader's, so it is capped + * at the bootloader's, and an unreadable one is taken to be it. + */ +static u32 boot_float(bool husky, int read_mv) +{ + u32 mv = read_mv > 0 ? read_mv : 0; + + if (husky) + return mv ? min(mv, HUSKY_BOOT_FV_MV) : HUSKY_BOOT_FV_MV; + return mv; +} + static int max77759_charger_init(struct max77759_charger *chg) { struct power_supply_battery_info *info; @@ -625,6 +918,10 @@ static int max77759_charger_init(struct max77759_charger *chg) if (ret) return ret; + /* The float the bootloader left, before the battery's replaces it */ + chg->boot_fv_mv = boot_float(chg->guard_table == &guard_table_husky, + get_float_voltage(chg)); + if (power_supply_get_battery_info(chg->psy, &info)) { fv = CHG_FV_DEFAULT_MV; fast_chg_curr = CHG_CC_DEFAULT_UA; @@ -721,6 +1018,15 @@ static void psy_work_item(struct work_struct *work) charger_set_mode(chg, MAX77759_CHGR_MODE_CHG_BUCK_ON); } else { charger_set_mode(chg, MAX77759_CHGR_MODE_OFF); + /* + * For the defender: a quick replug still counts as unplugged. + * Only where the TCPM supply notifies us; the guard run's own + * CHGIN read covers the rest. + */ + if (!online.intval) { + scoped_guard(mutex, &chg->lock) + chg->unplugged = true; + } } scoped_guard(mutex, &chg->retry_lock) { @@ -826,22 +1132,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 +1156,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; @@ -882,37 +1223,198 @@ 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; } -/* Back to the limits from the battery description, charging allowed */ +static void guard_set_inhibit(struct max77759_charger *chg, bool inhibit) +{ + charger_set_inhibit(chg, &chg->inhibit, inhibit); +} + +/* + * 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"); } +/* + * One defender step of @dt_s seconds. Time counts while plugged in at + * DEFEND_UV or more and DEFEND_DECIC or more; a lower voltage starts the + * count again, a lower temperature only pauses it. Unplugging clears the + * count and the latch. Returns whether the defender is latched. + */ +static bool defend_step(u32 *count_s, bool latched, bool plugged, int uv, + int decic, u32 dt_s) +{ + if (!plugged) { + *count_s = 0; + return false; + } + if (latched) + return true; + if (uv < DEFEND_UV) { + *count_s = 0; + return false; + } + if (decic >= DEFEND_DECIC) + *count_s += min(dt_s, DEFEND_MAX_STEP_S); + return *count_s >= DEFEND_S; +} + +/* + * Whether charging stays stopped: stop at @soc >= @end, resume at + * @soc <= stop_resume(). An end of 100 never stops. + */ +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 and the defender. With the thresholds at 100 / 0 + * and the defender off this reads nothing and writes nothing. + */ +static void stop_work(struct max77759_charger *chg) +{ + bool defender = charge_defender && chg->guard_table == &guard_table_husky; + ktime_t now = ktime_get_boottime(); + int end, start, e, st, plugged, decic, uv, soc = -1; + bool unplugged, stopped, defended; + s64 dt_ms; + + dt_ms = ktime_ms_delta(now, chg->stop_last); + chg->stop_last = now; + scoped_guard(mutex, &chg->lock) { + unplugged = chg->unplugged; + chg->unplugged = false; + end = chg->end_threshold; + start = chg->start_threshold; + } + + defended = chg->defended; + if (!defender) { + chg->defend_s = 0; + defended = false; + } else { + /* + * Each run reads CHGIN itself: on husky no TCPM supply + * notifies the psy work. An unplug and replug that both fall + * between two runs is missed. + */ + plugged = unplugged ? 0 : charger_input_valid(chg); + if (!plugged) { + if (defended) + dev_info(chg->dev, "unplugged: defender off\n"); + chg->defend_s = 0; + defended = false; + } else if (plugged > 0 && !guard_read(&decic, &uv)) { + bool was = defended; + + defended = defend_step(&chg->defend_s, was, true, uv, + decic, + clamp_val(div_s64(dt_ms, MSEC_PER_SEC), 0, + U32_MAX)); + if (defended && !was) + dev_info(chg->dev, + "defender on: charging stops at %d %%, resumes at %d %%\n", + DEFEND_END, DEFEND_START); + } + } + /* Under the lock: the threshold read-back reports it */ + scoped_guard(mutex, &chg->lock) + chg->defended = defended; + + stop_effective(end, start, defended, &e, &st); + stopped = chg->stop_inhibit; + if (e >= 100) + stopped = false; + else if (!stop_read_soc(chg, &soc)) + stopped = stop_next(stopped, soc, e, st); + + 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 = 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; 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) @@ -935,7 +1437,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 +1452,15 @@ 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)); + 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); - 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); + if (guard_apply(chg, fv, cc)) + goto out; if (band != chg->guard_band || tier != chg->guard_tier) dev_info(chg->dev, @@ -969,10 +1469,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: @@ -994,6 +1490,72 @@ static void max77759_charger_log_state(struct max77759_charger *chg, fv, cc, ilim); } +/* Restore the bootloader's float only to lower the chip's, never to raise it */ +static bool restore_float_needed(u32 cur_mv, u32 boot_mv) +{ + return boot_mv && cur_mv > boot_mv; +} + +/* + * Pixel 8 Pro: leave no float above the bootloader's behind once the + * driver, and with it the guard and the defender, is gone. When the float + * cannot be read or put back, charging is stopped (buck only) and the + * unbind leaves it stopped, rather than charge on a float that may be the + * raised one. + */ +static int restore_float(struct max77759_charger *chg) +{ + u32 want; + int cur; + + if (chg->guard_table != &guard_table_husky) + return 0; + + cur = get_float_voltage(chg); + if (cur < 0) { + dev_err(chg->dev, "could not read the float (%d): charging stopped\n", + cur); + goto fail; + } + if (!restore_float_needed(cur, chg->boot_fv_mv)) + return 0; + + if (fv_quantize(chg->boot_fv_mv, &want) || + set_float_voltage_limit(chg, chg->boot_fv_mv) || + get_float_voltage(chg) != (int)want) { + dev_err(chg->dev, + "could not restore the float to %u mV: charging stopped\n", + chg->boot_fv_mv); + goto fail; + } + dev_info(chg->dev, "float restored to %u mV\n", chg->boot_fv_mv); + return 0; + +fail: + chg->float_unsafe = true; + charger_set_inhibit(chg, &chg->stop_inhibit, true); + return -EIO; +} + +static void max77759_charger_restore_float(void *data) +{ + restore_float(data); +} + +/* + * On unbind, no charge threshold holds charging off any more, unless the + * float could not be restored: then charging stays stopped. + */ +static void max77759_charger_clear_stop(void *data) +{ + struct max77759_charger *chg = data; + + if (chg->float_unsafe) + return; + + charger_set_inhibit(chg, &chg->stop_inhibit, false); +} + static int max77759_charger_probe(struct platform_device *pdev) { struct regulator_config chgin_otg_reg_cfg = { }; @@ -1009,6 +1571,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"); @@ -1038,6 +1601,7 @@ static int max77759_charger_probe(struct platform_device *pdev) max77759_charger_log_state(chg, "found"); + chg->guard_table = guard_select_table(); ret = max77759_charger_init(chg); if (ret) return dev_err_probe(dev, ret, @@ -1056,6 +1620,20 @@ 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 the float is restored, then a threshold stop is cleared, + * and only then is charging left on. + */ + ret = devm_add_action_or_reset(dev, max77759_charger_clear_stop, chg); + if (ret) + return ret; + + ret = devm_add_action_or_reset(dev, max77759_charger_restore_float, + chg); + if (ret) + return ret; + chgin_otg_reg_cfg.dev = dev; chgin_otg_reg_cfg.driver_data = chg; chgin_otg_reg_cfg.of_node = dev_of_node(dev); @@ -1074,6 +1652,7 @@ static int max77759_charger_probe(struct platform_device *pdev) * unregisters the notifier before it cancels the work. */ chg->guard_band = -2; + chg->stop_last = ktime_get_boottime(); ret = devm_delayed_work_autocancel(dev, &chg->guard_work, guard_work_item); if (ret) return dev_err_probe(dev, ret, "Failed to initialize guard work\n"); @@ -1104,6 +1683,15 @@ static int max77759_charger_probe(struct platform_device *pdev) return 0; } +static void max77759_charger_shutdown(struct platform_device *pdev) +{ + struct max77759_charger *chg = platform_get_drvdata(pdev); + + /* No guard run after this, not even one the notifier kicks */ + disable_delayed_work_sync(&chg->guard_work); + max77759_charger_restore_float(chg); +} + static const struct platform_device_id max77759_charger_id[] = { { .name = "max77759-charger" }, { } @@ -1116,10 +1704,15 @@ static struct platform_driver max77759_charger_driver = { .probe_type = PROBE_PREFER_ASYNCHRONOUS, }, .probe = max77759_charger_probe, + .shutdown = max77759_charger_shutdown, .id_table = max77759_charger_id, }; module_platform_driver(max77759_charger_driver); +#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..dcd62941bf --- /dev/null +++ b/drivers/power/supply/max77759_charger_test.c @@ -0,0 +1,1114 @@ +// 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; the + * charge thresholds, the defender, the read-back of the limits and the + * float restore. + * + * This file is included by max77759_charger.c and can not be built alone. + */ + +#include +#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); + } + } +} + +/* 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 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 */ + int fail_reads; /* this many reads fail with -EIO first */ +}; + +static int fake_read(void *ctx, unsigned int reg, unsigned int *val) +{ + struct fake_chip *c = ctx; + + c->nread++; + if (c->fail_reads) { + c->fail_reads--; + return -EIO; + } + *val = c->regs[reg]; + if (reg == c->corrupt_reg) + *val ^= 0x01; + 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, with the defender off or on a board without it: no i2c */ +static void stop_test_inert_no_io(struct kunit *test) +{ + bool saved = charge_defender; + struct fake_chip *c; + struct max77759_charger *chg = fake_charger(test, &c); + + chg->mode = MAX77759_CHGR_MODE_CHG_BUCK_ON; + chg->guard_table = &guard_table_default; + + charge_defender = false; + stop_work(chg); + charge_defender = true; + stop_work(chg); + charge_defender = saved; + + KUNIT_EXPECT_EQ(test, c->nread, 0); + KUNIT_EXPECT_EQ(test, c->nlog, 0); + KUNIT_EXPECT_FALSE(test, chg->stop_inhibit); + KUNIT_EXPECT_FALSE(test, chg->defended); +} + +/* + * 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 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 void stop_test_effective(struct kunit *test) +{ + int e, s; + + stop_effective(100, 0, false, &e, &s); + KUNIT_EXPECT_EQ(test, e, 100); + KUNIT_EXPECT_EQ(test, s, 0); + + stop_effective(100, 0, true, &e, &s); + KUNIT_EXPECT_EQ(test, e, 80); + KUNIT_EXPECT_EQ(test, s, 79); + + /* A lower limit of the user's stays */ + stop_effective(70, 60, true, &e, &s); + KUNIT_EXPECT_EQ(test, e, 70); + KUNIT_EXPECT_EQ(test, s, 60); + + stop_effective(80, 50, true, &e, &s); + KUNIT_EXPECT_EQ(test, e, 80); + KUNIT_EXPECT_EQ(test, s, 50); +} + +/* Run the defender for @n steps of @dt_s at one reading */ +static bool defend_run(u32 *count, bool latched, int uv, int decic, u32 dt_s, + int n) +{ + while (n--) + latched = defend_step(count, latched, true, uv, decic, dt_s); + return latched; +} + +static void defend_test_latch(struct kunit *test) +{ + u32 count = 0; + bool on; + + /* 21,599 s does not latch, 21,600 s does */ + on = defend_run(&count, false, 4270000, 350, 30, 719); + on = defend_step(&count, on, true, 4270000, 350, 29); + KUNIT_EXPECT_EQ(test, count, 21599); + KUNIT_EXPECT_FALSE(test, on); + on = defend_step(&count, on, true, 4270000, 350, 1); + KUNIT_EXPECT_TRUE(test, on); + + /* Latched: stays so whatever the reading, until unplugged */ + KUNIT_EXPECT_TRUE(test, defend_step(&count, on, true, 3800000, 200, 30)); + KUNIT_EXPECT_FALSE(test, defend_step(&count, on, false, 4300000, 400, 30)); + KUNIT_EXPECT_EQ(test, count, 0); +} + +static void defend_test_conditions(struct kunit *test) +{ + u32 count = 0; + + /* 4.269 V starts the count again */ + defend_run(&count, false, 4300000, 400, 30, 100); + KUNIT_EXPECT_EQ(test, count, 3000); + KUNIT_EXPECT_FALSE(test, defend_step(&count, false, true, 4269000, 400, 30)); + KUNIT_EXPECT_EQ(test, count, 0); + + /* 34.9 C does not count, and does not reset either */ + defend_run(&count, false, 4300000, 400, 30, 10); + KUNIT_EXPECT_FALSE(test, defend_run(&count, false, 4300000, 349, 30, + 1000)); + KUNIT_EXPECT_EQ(test, count, 300); + + /* Unplugged clears */ + KUNIT_EXPECT_FALSE(test, defend_step(&count, false, false, 4300000, 400, 30)); + KUNIT_EXPECT_EQ(test, count, 0); + + /* One long gap between runs counts as at most DEFEND_MAX_STEP_S */ + KUNIT_EXPECT_FALSE(test, defend_step(&count, false, true, 4300000, 400, + DEFEND_S)); + KUNIT_EXPECT_EQ(test, count, DEFEND_MAX_STEP_S); +} + +/* An unplug psy work saw clears the defender, and the flag is consumed */ +static void defend_test_unplug_flag(struct kunit *test) +{ + bool saved = charge_defender; + struct fake_chip *c; + struct max77759_charger *chg = fake_charger(test, &c); + + chg->guard_table = &guard_table_husky; + chg->defended = true; + chg->defend_s = DEFEND_S; + chg->unplugged = true; + + charge_defender = true; + stop_work(chg); + charge_defender = saved; + + KUNIT_EXPECT_FALSE(test, chg->defended); + KUNIT_EXPECT_EQ(test, chg->defend_s, 0); + KUNIT_EXPECT_FALSE(test, chg->unplugged); + /* No CHGIN read: the flag alone says unplugged */ + KUNIT_EXPECT_EQ(test, c->nread, 0); +} + +/* With no TCPM notice, the guard run's own CHGIN read clears the defender */ +static void defend_test_unplug_chgin(struct kunit *test) +{ + bool saved = charge_defender; + struct fake_chip *c; + struct max77759_charger *chg = fake_charger(test, &c); + + chg->guard_table = &guard_table_husky; + chg->defended = true; + chg->defend_s = DEFEND_S; + c->regs[MAX77759_CHGR_REG_CHG_INT_OK] = 0; + + charge_defender = true; + stop_work(chg); + charge_defender = saved; + + KUNIT_EXPECT_FALSE(test, chg->defended); + KUNIT_EXPECT_EQ(test, chg->defend_s, 0); +} + +/* While latched, the thresholds read back the defender's 80 / 79 */ +static void defend_test_readback(struct kunit *test) +{ + const enum power_supply_property end = + POWER_SUPPLY_PROP_CHARGE_CONTROL_END_THRESHOLD; + const enum power_supply_property start = + POWER_SUPPLY_PROP_CHARGE_CONTROL_START_THRESHOLD; + struct power_supply_config cfg = { }; + struct power_supply_desc *desc; + struct power_supply *psy; + struct fake_chip *c; + struct max77759_charger *chg = fake_charger(test, &c); + + desc = devm_kmemdup(chg->dev, &max77759_charger_desc, sizeof(*desc), + GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, desc); + desc->name = "max77759-chg-test"; + cfg.drv_data = chg; + psy = devm_power_supply_register(chg->dev, desc, &cfg); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, psy); + + chg->defended = true; + KUNIT_EXPECT_EQ(test, chg_prop_get(psy, end), 80); + KUNIT_EXPECT_EQ(test, chg_prop_get(psy, start), 79); + + /* A lower user end stays in force, and reads back */ + KUNIT_ASSERT_EQ(test, chg_prop_set(psy, end, 70), 0); + KUNIT_EXPECT_EQ(test, chg_prop_get(psy, end), 70); + KUNIT_EXPECT_EQ(test, chg_prop_get(psy, start), 65); + + /* Unlatched: the user's values again */ + KUNIT_ASSERT_EQ(test, chg_prop_set(psy, end, 100), 0); + chg->defended = false; + KUNIT_EXPECT_EQ(test, chg_prop_get(psy, end), 100); + KUNIT_EXPECT_EQ(test, chg_prop_get(psy, start), 0); +} + +static void restore_test_never_raises(struct kunit *test) +{ + /* The husky bootloader leaves 4350 mV */ + KUNIT_EXPECT_FALSE(test, restore_float_needed(4200, 4350)); + KUNIT_EXPECT_FALSE(test, restore_float_needed(4350, 4350)); + KUNIT_EXPECT_TRUE(test, restore_float_needed(4360, 4350)); + KUNIT_EXPECT_TRUE(test, restore_float_needed(4450, 4350)); + /* Unknown boot float: leave it */ + KUNIT_EXPECT_FALSE(test, restore_float_needed(4450, 0)); +} + +static void restore_test_action(struct kunit *test) +{ + struct fake_chip *c; + struct max77759_charger *chg = fake_charger(test, &c); + u8 *fv = &c->regs[MAX77759_CHGR_REG_CHG_CNFG_04]; + + chg->boot_fv_mv = 4350; + + /* husky above the boot float: back to 4350 mV */ + chg->guard_table = &guard_table_husky; + *fv = 0x2d; + max77759_charger_restore_float(chg); + KUNIT_EXPECT_EQ(test, *fv, 0x23); + + /* husky below it: untouched */ + c->nlog = 0; + *fv = 0x14; + max77759_charger_restore_float(chg); + KUNIT_EXPECT_EQ(test, *fv, 0x14); + KUNIT_EXPECT_EQ(test, c->nlog, 0); + + /* Another board: untouched, not even read */ + chg->guard_table = &guard_table_default; + c->nread = 0; + *fv = 0x2d; + max77759_charger_restore_float(chg); + KUNIT_EXPECT_EQ(test, *fv, 0x2d); + KUNIT_EXPECT_EQ(test, c->nlog, 0); + KUNIT_EXPECT_EQ(test, c->nread, 0); +} + +/* A restore that does not hold: charging stopped, and unbind keeps it so */ +static void restore_test_readback_fail(struct kunit *test) +{ + struct fake_chip *c; + struct max77759_charger *chg = fake_charger(test, &c); + u8 *mode = &c->regs[MAX77759_CHGR_REG_CHG_CNFG_00]; + + chg->guard_table = &guard_table_husky; + chg->boot_fv_mv = 4350; + chg->mode = MAX77759_CHGR_MODE_CHG_BUCK_ON; + *mode = MAX77759_CHGR_MODE_CHG_BUCK_ON; + c->regs[MAX77759_CHGR_REG_CHG_CNFG_04] = 0x2d; + c->corrupt_reg = MAX77759_CHGR_REG_CHG_CNFG_04; + KUNIT_EXPECT_EQ(test, restore_float(chg), -EIO); + KUNIT_EXPECT_TRUE(test, chg->stop_inhibit); + KUNIT_EXPECT_EQ(test, *mode, MAX77759_CHGR_MODE_BUCK_ON); + + max77759_charger_clear_stop(chg); + KUNIT_EXPECT_TRUE(test, chg->stop_inhibit); + KUNIT_EXPECT_EQ(test, *mode, MAX77759_CHGR_MODE_BUCK_ON); +} + +/* A float that cannot be read is a failed restore too */ +static void restore_test_read_fail(struct kunit *test) +{ + struct fake_chip *c; + struct max77759_charger *chg = fake_charger(test, &c); + + chg->guard_table = &guard_table_husky; + chg->boot_fv_mv = 4350; + chg->mode = MAX77759_CHGR_MODE_CHG_BUCK_ON; + c->fail_reads = 1; + KUNIT_EXPECT_EQ(test, restore_float(chg), -EIO); + KUNIT_EXPECT_TRUE(test, chg->stop_inhibit); + KUNIT_EXPECT_TRUE(test, chg->float_unsafe); +} + +/* The float to restore: husky caps it and falls back to 4350 mV */ +static void restore_test_boot_float(struct kunit *test) +{ + KUNIT_EXPECT_EQ(test, boot_float(true, 4350), 4350); + KUNIT_EXPECT_EQ(test, boot_float(true, 4200), 4200); + KUNIT_EXPECT_EQ(test, boot_float(true, 4450), 4350); + KUNIT_EXPECT_EQ(test, boot_float(true, 0), 4350); + KUNIT_EXPECT_EQ(test, boot_float(true, -EIO), 4350); + KUNIT_EXPECT_EQ(test, boot_float(false, 4450), 4450); + KUNIT_EXPECT_EQ(test, boot_float(false, -EIO), 0); +} + +static struct kunit_case guard_test_cases[] = { + KUNIT_CASE(guard_test_band_edges), + KUNIT_CASE(guard_test_hysteresis), + 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), + 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), + KUNIT_CASE(readback_test_apply), + KUNIT_CASE(readback_test_mismatch), + KUNIT_CASE(readback_test_inhibit), + KUNIT_CASE(stop_test_effective), + KUNIT_CASE(defend_test_latch), + KUNIT_CASE(defend_test_conditions), + KUNIT_CASE(defend_test_unplug_flag), + KUNIT_CASE(defend_test_unplug_chgin), + KUNIT_CASE(defend_test_readback), + KUNIT_CASE(restore_test_never_raises), + KUNIT_CASE(restore_test_action), + KUNIT_CASE(restore_test_readback_fail), + KUNIT_CASE(restore_test_read_fail), + KUNIT_CASE(restore_test_boot_float), + { } +}; + +static struct kunit_suite guard_test_suite = { + .name = "max77759_charger_guard", + .test_cases = guard_test_cases, +}; + +kunit_test_suite(guard_test_suite);