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..87917cad37 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,146 @@ 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 */ + +/* + * DONE check of the stock husky charger (max77759_is_full()): in the last + * tier the chip's DONE counts as full only within DONE_FULL_MARGIN_UV of + * the float; below it charging is restarted, as stock does, at most once + * per DONE_RESTART_GAP_S and DONE_RESTART_MAX times per plug-in. A full + * battery is charged again once the capacity falls to DONE_RECHARGE_SOC or + * the voltage DONE_RECHARGE_UV below the float. + */ +#define DONE_FULL_MARGIN_UV 50000 +#define DONE_RECHARGE_UV 250000 +#define DONE_RECHARGE_SOC 95 +#define DONE_RESTART_GAP_S 600 +#define DONE_RESTART_MAX 5 + +enum done_action { + DONE_NONE, /* nothing to do */ + DONE_FULL, /* done counts as full */ + DONE_RESTART, /* restart charging */ + DONE_REFUSED, /* would restart, but out of restarts */ + DONE_LIMIT, /* the band's own limit, below the last tier */ +}; + +#define DONE_UNCHECKED -1 /* no DONE check ran */ + +/** + * struct done_state - DONE check state, kept per plug-in + * @full: the battery was found full and stays so until recharge + * @restarts: restarts since plug-in + * @last_s: time of the last restart, in seconds + */ +struct done_state { + bool full; + int restarts; + s64 last_s; +}; + #define GUARD_PERIOD_MS 30000 #define GUARD_RETRY_MS 2000 /* while there is no reading */ #define GUARD_HYST_DECIC 20 /* 2.0 C into a band before relaxing */ #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 + * @fv_max_mv: when set, the guard's float cap in place of the battery + * description's, so the float goes above that only while the + * guard runs + */ +struct guard_table { + int ntiers; + u32 fv_max_mv; + u32 cv_mv[GUARD_MAX_TIERS]; + u8 cc_pct[GUARD_NBANDS][GUARD_MAX_TIERS]; +}; + +/* 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. + * + * The battery node keeps the float at 4.20 V, so a kernel booted without + * the guard never charges higher. While the guard runs it caps the + * table at 4.45 V instead: under the board's google,fv-max-uv of 4.48 V + * (dts/zuma-shusky-charging.dtsi), which google,aafv-ref-cycles and + * google,aafv-ref-offset (dts/zuma-husky-battery.dtsi) lower by 20 mV + * per 200 cycles, to 4.38 V from 1000 cycles. 4.45 V is a choice, not + * from that schedule. + */ +static const struct guard_table guard_table_husky = { + .ntiers = 4, + .fv_max_mv = 4450, + .cv_mv = { 4200, 4300, 4350, 4480 }, + .cc_pct = { + { 50, 30, 0, 0 }, /* 0-10 C */ + { 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 +241,24 @@ 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 */ + struct done_state done; /* DONE check, husky only */ + bool done_warned; /* out of restarts said once */ + int done_last; /* last DONE check, or DONE_UNCHECKED, under lock */ + bool done_active; /* the DONE check is in force, under lock */ + bool mode_rewrite; /* a restart left the mode off, under lock */ + ktime_t stop_last; + u32 boot_fv_mv; /* float the bootloader left, 0: unknown */ + bool float_unsafe; /* restore failed: keep the stop on unbind */ + bool apply_failed; /* the chip did not hold the guard's limits */ /* Temperature guard */ struct delayed_work guard_work; @@ -139,6 +266,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; @@ -203,6 +331,16 @@ static int get_status(struct max77759_charger *chg) case MAX77759_CHGR_CHG_DTLS_TO: return POWER_SUPPLY_STATUS_CHARGING; case MAX77759_CHGR_CHG_DTLS_DONE: + /* + * While the DONE check runs (husky, temp_guard on), Full only + * for a DONE it accepted; until it has looked, for one it + * restarts or refused, and while the guard has no reading or + * holds charging off, the charge has stopped short of full. + */ + scoped_guard(mutex, &chg->lock) { + if (chg->done_active && chg->done_last != DONE_FULL) + return POWER_SUPPLY_STATUS_NOT_CHARGING; + } return POWER_SUPPLY_STATUS_FULL; case MAX77759_CHGR_CHG_DTLS_TIMER_FAULT: case MAX77759_CHGR_CHG_DTLS_SUSP_BATT_THM: @@ -210,6 +348,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 +489,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 +540,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 +605,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 +682,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 +739,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 +764,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 +953,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 +982,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 +1082,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 +1196,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 +1220,57 @@ 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); +} + +/* The guard's float cap: the table's own, else the battery description's */ +static u32 guard_fv_cap(const struct guard_table *tbl, u32 fv_max_mv) +{ + return tbl->fv_max_mv ?: fv_max_mv; +} + +static const struct guard_table *guard_select_table(void) +{ + if (of_machine_is_compatible("google,zuma-husky")) + return &guard_table_husky; + return &guard_table_default; +} + static int guard_read(int *decic, int *uv) { union power_supply_propval val; @@ -882,60 +1293,394 @@ 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"); } -static void guard_work_item(struct work_struct *work) +/* + * 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) { - struct max77759_charger *chg = - container_of(work, struct max77759_charger, guard_work.work); - int decic = 0, uv = 0, band, tier; - unsigned int next_ms = GUARD_PERIOD_MS; - bool read_ok; - u32 fv, cc; + 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; +} - if (!temp_guard) { - WRITE_ONCE(chg->guard_no_reading, false); - if (chg->guard_active) - guard_release(chg); - goto out; +/* + * 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; +} + +/* + * One DONE check. @done: the chip reports DONE; @band_last: the band + * charges up to the table's last tier; @last_tier: charging in that tier; + * @uv: battery voltage; @fv_mv: the float in force; @soc: capacity, or + * 100 when unknown; @now_s: a monotonic time in s. + */ +static enum done_action done_step(struct done_state *st, bool done, + bool band_last, bool last_tier, int uv, + u32 fv_mv, int soc, s64 now_s) +{ + s64 fv_uv = (s64)fv_mv * 1000; + + if (!done) { + st->full = false; + return DONE_NONE; + } + /* Capped below the last tier: done there is the band's limit */ + if (!band_last) { + st->full = false; + return DONE_LIMIT; + } + /* + * Below the last tier done is never full, as in stock: a small + * adapter under load can reach top-off far from it. Restart. + */ + if (last_tier && uv >= fv_uv - DONE_FULL_MARGIN_UV) { + st->full = true; + return DONE_FULL; + } + if (last_tier && st->full && soc > DONE_RECHARGE_SOC && + uv >= fv_uv - DONE_RECHARGE_UV) + return DONE_FULL; + st->full = false; + + if (st->restarts >= DONE_RESTART_MAX) + return DONE_REFUSED; + if (st->restarts && now_s - st->last_s < DONE_RESTART_GAP_S) + return DONE_NONE; + return DONE_RESTART; +} + +/* Count a restart that ran */ +static void done_restarted(struct done_state *st, s64 now_s) +{ + st->restarts++; + st->last_s = now_s; +} + +/* + * The tier whose float is the one in force: the first whose voltage + * reaches @fv_cap_mv, the cap the guard applies. At a 4.20 V cap that is + * the first tier, at 4.45 V on husky the last. @band_last: the band + * charges up to that tier; @last_tier: charging in it. + */ +static void done_tiers(const struct guard_table *tbl, int band, int tier, + u32 fv_cap_mv, bool *band_last, bool *last_tier) +{ + int full = 0; + + while (full < tbl->ntiers - 1 && tbl->cv_mv[full] < fv_cap_mv) + full++; + *band_last = guard_top_tier(tbl, band) >= full; + *last_tier = *band_last && tier >= full; +} + +#define DONE_RESTART_TRIES 3 + +/* + * Charge mode off, then back on, as stock restarts a charge. Returns + * whether it ran, that is the mode went off. Putting the mode back is + * tried DONE_RESTART_TRIES times; if that fails it says so and marks + * the mode for the next guard run to write again. + */ +static bool charger_restart(struct max77759_charger *chg) +{ + int i, ret; + + guard(mutex)(&chg->lock); + + if (chg->mode != MAX77759_CHGR_MODE_CHG_BUCK_ON || + chg->inhibit || chg->stop_inhibit) + return false; + if (regmap_update_bits(chg->regmap, MAX77759_CHGR_REG_CHG_CNFG_00, + MAX77759_CHGR_REG_CHG_CNFG_00_MODE, + MAX77759_CHGR_MODE_OFF)) + return false; + for (i = 0; i < DONE_RESTART_TRIES; i++) { + ret = charger_write_mode(chg, chg->mode); + if (!ret) + return true; + } + dev_err(chg->dev, "charging left off after a restart (%d)\n", ret); + chg->mode_rewrite = true; + return true; +} + +/* A mode left off by a failed restart: write it again, under the lock */ +static void charger_mode_fix(struct max77759_charger *chg) +{ + guard(mutex)(&chg->lock); + + if (chg->mode_rewrite && !charger_write_mode(chg, chg->mode)) { + chg->mode_rewrite = false; + dev_info(chg->dev, "charge mode written again after a restart\n"); } +} + +/* + * Husky: a new plug-in gets its restarts back. No TCPM supply notifies the + * psy work there, so read CHGIN each run, as the defender does. An unplug + * and replug that both fall between two runs is missed. + */ +static void done_plug_check(struct max77759_charger *chg) +{ + if (!temp_guard || chg->guard_table != &guard_table_husky || + charger_input_valid(chg) != 0) + return; + chg->done = (struct done_state){ }; + chg->done_warned = false; +} +/* + * @active: the DONE check is in force (husky, temp_guard on), so status + * reads Full only for a DONE it accepted. + */ +static void done_set_last(struct max77759_charger *chg, bool active, + int action) +{ + guard(mutex)(&chg->lock); + chg->done_active = active; + chg->done_last = action; +} + +/* + * Husky: the DONE check, after the guard has set the band and tier. + * Returns the action, or DONE_UNCHECKED on other boards or a failed read. + */ +static int done_work(struct max77759_charger *chg, int band, int tier, int uv, + u32 fv_mv) +{ + const struct guard_table *tbl = chg->guard_table; + s64 now_s = div_s64(ktime_get_boottime_ns(), NSEC_PER_SEC); + enum done_action action; + bool band_last, last_tier; + int soc = 100; + u32 val; + + if (tbl != &guard_table_husky) + return DONE_UNCHECKED; + if (regmap_read(chg->regmap, MAX77759_CHGR_REG_CHG_DETAILS_01, &val)) + return DONE_UNCHECKED; + + done_tiers(tbl, band, tier, guard_fv_cap(tbl, chg->fv_max_mv), + &band_last, &last_tier); + if (chg->done.full) + stop_read_soc(chg, &soc); + + action = done_step(&chg->done, + FIELD_GET(MAX77759_CHGR_REG_CHG_DETAILS_01_CHG_DTLS, val) == + MAX77759_CHGR_CHG_DTLS_DONE, + band_last, last_tier, uv, fv_mv, soc, now_s); + switch (action) { + case DONE_RESTART: + if (!charger_restart(chg)) + break; + done_restarted(&chg->done, now_s); + dev_info(chg->dev, "done at %d mV, below %u mV: restart %d\n", + uv / 1000, fv_mv, chg->done.restarts); + break; + case DONE_REFUSED: + if (!chg->done_warned) + dev_warn(chg->dev, + "done at %d mV after %d restarts: no more until unplugged\n", + uv / 1000, DONE_RESTART_MAX); + chg->done_warned = true; + break; + default: + break; + } + return action; +} + +/* + * The charge thresholds and the defender. With the thresholds at 100 / 0 + * and the defender off this reads nothing and writes nothing. + */ +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; + /* A new plug-in gets its restarts back */ + if (unplugged) { + chg->done = (struct done_state){ }; + chg->done_warned = false; + } + end = chg->end_threshold; + start = chg->start_threshold; + } + + 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); +} + +/* + * One guard run with temp_guard on, after the gauge read: @read_ok, and + * then @decic and @uv. Returns the delay to the next run, in ms. + */ +static unsigned int guard_run(struct max77759_charger *chg, bool read_ok, + int decic, int uv) +{ + const struct guard_table *tbl = chg->guard_table; + unsigned int next_ms = GUARD_PERIOD_MS; + int done = DONE_UNCHECKED; + int band, tier; + u32 fv, cc; + + charger_mode_fix(chg); + done_plug_check(chg); if (!chg->guard_active) { chg->guard_active = true; chg->guard_band = -2; dev_info(chg->dev, "temperature guard on (%s)\n", guard_psy); } - /* - * No reading, no charging. A missing reading is usually the gauge - * still starting (it answers once its model load is done), so look - * again soon instead of a guard period later. - */ - read_ok = !guard_read(&decic, &uv); WRITE_ONCE(chg->guard_no_reading, !read_ok); if (!read_ok) next_ms = GUARD_RETRY_MS; - 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 +1695,16 @@ 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, + guard_fv_cap(tbl, 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,13 +1713,41 @@ 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; + done = done_work(chg, band, tier, uv, fv); out: + done_set_last(chg, tbl == &guard_table_husky, done); + return next_ms; +} + +static void guard_work_item(struct work_struct *work) +{ + struct max77759_charger *chg = + container_of(work, struct max77759_charger, guard_work.work); + unsigned int next_ms = GUARD_PERIOD_MS; + int decic = 0, uv = 0; + + /* + * The charge thresholds first: a stop they call for is in place + * before the guard below can clear its own inhibit. + */ + stop_work(chg); + + if (!temp_guard) { + WRITE_ONCE(chg->guard_no_reading, false); + charger_mode_fix(chg); + if (chg->guard_active) + guard_release(chg); + done_set_last(chg, false, DONE_UNCHECKED); + } else { + /* + * No reading, no charging. A missing reading is usually the + * gauge still starting (it answers once its model load is + * done), so look again soon instead of a guard period later. + */ + next_ms = guard_run(chg, !guard_read(&decic, &uv), decic, uv); + } schedule_delayed_work(&chg->guard_work, msecs_to_jiffies(next_ms)); } @@ -994,6 +1766,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 +1847,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 +1877,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 +1896,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 +1928,8 @@ static int max77759_charger_probe(struct platform_device *pdev) * unregisters the notifier before it cancels the work. */ chg->guard_band = -2; + chg->done_last = DONE_UNCHECKED; + chg->stop_last = ktime_get_boottime(); ret = devm_delayed_work_autocancel(dev, &chg->guard_work, guard_work_item); if (ret) return dev_err_probe(dev, ret, "Failed to initialize guard work\n"); @@ -1104,6 +1960,79 @@ static int max77759_charger_probe(struct platform_device *pdev) return 0; } +/* + * Before sleep: no guard runs while asleep, so a float the guard raised + * above the battery description's would hold for as long as the phone + * sleeps on the cable, whatever the temperature does. Put the + * description's float back; the first guard run after resume raises it + * again if the band allows. If the chip does not take it, inhibit + * charging instead; that run clears the inhibit once it applies its + * limits. Returns 0 when the float is down or charging is inhibited, + * else an error: then the phone must not sleep. + */ +static int guard_sleep_float(struct max77759_charger *chg) +{ + int cur = get_float_voltage(chg); + u32 want; + int ret; + + if (cur >= 0 && cur <= (int)chg->fv_max_mv) + return 0; + ret = cur < 0 ? cur : fv_quantize(chg->fv_max_mv, &want); + if (!ret) + ret = set_float_voltage_limit(chg, chg->fv_max_mv); + if (!ret && get_float_voltage(chg) != (int)want) + ret = -EIO; + if (!ret) + return 0; + guard_set_inhibit(chg, true); + if (!chg->inhibit) { + dev_err(chg->dev, "could not lower the float or stop charging for sleep (%d): sleep refused\n", + ret); + return -EIO; + } + dev_err(chg->dev, "could not lower the float for sleep (%d): charging inhibited\n", + ret); + return 0; +} + +static int max77759_charger_suspend(struct device *dev) +{ + struct max77759_charger *chg = dev_get_drvdata(dev); + + int ret = 0; + + disable_delayed_work_sync(&chg->guard_work); + if (chg->guard_active && chg->guard_table->fv_max_mv) + ret = guard_sleep_float(chg); + if (ret) { + enable_delayed_work(&chg->guard_work); + schedule_delayed_work(&chg->guard_work, 0); + } + return ret; +} + +static int max77759_charger_resume(struct device *dev) +{ + struct max77759_charger *chg = dev_get_drvdata(dev); + + enable_delayed_work(&chg->guard_work); + schedule_delayed_work(&chg->guard_work, 0); + return 0; +} + +static DEFINE_SIMPLE_DEV_PM_OPS(max77759_charger_pm, max77759_charger_suspend, + max77759_charger_resume); + +static void max77759_charger_shutdown(struct platform_device *pdev) +{ + struct max77759_charger *chg = platform_get_drvdata(pdev); + + /* 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" }, { } @@ -1114,12 +2043,18 @@ static struct platform_driver max77759_charger_driver = { .driver = { .name = "max77759-charger", .probe_type = PROBE_PREFER_ASYNCHRONOUS, + .pm = pm_sleep_ptr(&max77759_charger_pm), }, .probe = max77759_charger_probe, + .shutdown = max77759_charger_shutdown, .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..955e8c7587 --- /dev/null +++ b/drivers/power/supply/max77759_charger_test.c @@ -0,0 +1,1617 @@ +// 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 husky table carries its own float cap, so its guard raises the + * float above the battery node's 4.20 V, to 4.45 V and no further; the + * default table has none and keeps the node's. + */ +static void guard_test_fv_cap(struct kunit *test) +{ + const struct guard_table *tbl = &guard_table_husky; + u32 fv, cc; + + KUNIT_EXPECT_EQ(test, guard_table_husky.fv_max_mv, 4450); + KUNIT_EXPECT_EQ(test, guard_table_default.fv_max_mv, 0); + + guard_limits(tbl, 2, 3, 4950000, guard_fv_cap(tbl, 4200), 1485000, &fv, &cc); + KUNIT_EXPECT_EQ(test, fv, 4450); + guard_limits(tbl, 2, 2, 4950000, guard_fv_cap(tbl, 4200), 1485000, &fv, &cc); + KUNIT_EXPECT_EQ(test, fv, 4450); + /* Bands capped below the last tier keep their own float */ + guard_limits(tbl, 4, 0, 4950000, guard_fv_cap(tbl, 4200), 1485000, &fv, &cc); + KUNIT_EXPECT_EQ(test, fv, 4300); + guard_limits(tbl, 5, 0, 4950000, guard_fv_cap(tbl, 4200), 1485000, &fv, &cc); + KUNIT_EXPECT_EQ(test, fv, 4200); + + /* What the guard run passes: the table's cap, else the node's */ + KUNIT_EXPECT_EQ(test, guard_fv_cap(&guard_table_husky, 4200), 4450); + KUNIT_EXPECT_EQ(test, guard_fv_cap(&guard_table_default, 4200), 4200); +} + +/* The float is the step at or below the request, as stock rounds it */ +static void round_test_float(struct kunit *test) +{ + 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 pass_writes; /* ... after this many writes that work */ + int fail_reads; /* this many reads fail with -EIO first */ + u8 vlog[32]; /* the value of each logged write */ +}; + +static int fake_read(void *ctx, unsigned int reg, unsigned int *val) +{ + 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->pass_writes) { + c->pass_writes--; + } else if (c->fail_writes) { + c->fail_writes--; + return -EIO; + } + c->regs[reg] = val; + if (c->nlog < ARRAY_SIZE(c->log)) { + c->vlog[c->nlog] = val; + 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; + chg->done_last = DONE_UNCHECKED; + 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); +} + +/* DONE in the last tier at 4450 mV: full from 4400 mV, restart below */ +static void done_test_full_rule(struct kunit *test) +{ + struct done_state st = { }; + + KUNIT_EXPECT_EQ(test, done_step(&st, true, true, true, 4400000, 4450, 100, 0), + DONE_FULL); + KUNIT_EXPECT_TRUE(test, st.full); + + st = (struct done_state){ }; + KUNIT_EXPECT_EQ(test, done_step(&st, true, true, true, 4399000, 4450, 100, 0), + DONE_RESTART); + /* Only a restart that ran is counted, by the caller */ + KUNIT_EXPECT_EQ(test, st.restarts, 0); + + /* Not done: nothing; capped below the last tier: the band's limit */ + st = (struct done_state){ .full = true }; + KUNIT_EXPECT_EQ(test, done_step(&st, false, true, true, 4000000, 4450, 50, 0), + DONE_NONE); + st.full = true; + KUNIT_EXPECT_EQ(test, done_step(&st, true, false, false, 4000000, 4450, 50, 0), + DONE_LIMIT); + KUNIT_EXPECT_FALSE(test, st.full); + KUNIT_EXPECT_EQ(test, st.restarts, 0); +} + +/* Once full, done stays full until 95 % or 250 mV below the float */ +static void done_test_recharge(struct kunit *test) +{ + struct done_state st = { }; + + done_step(&st, true, true, true, 4440000, 4450, 100, 0); + KUNIT_EXPECT_EQ(test, done_step(&st, true, true, true, 4300000, 4450, 96, 10), + DONE_FULL); + KUNIT_EXPECT_EQ(test, done_step(&st, true, true, true, 4200000, 4450, 96, 20), + DONE_FULL); + KUNIT_EXPECT_EQ(test, done_step(&st, true, true, true, 4300000, 4450, 95, 30), + DONE_RESTART); + KUNIT_EXPECT_FALSE(test, st.full); + + st = (struct done_state){ }; + done_step(&st, true, true, true, 4440000, 4450, 100, 0); + KUNIT_EXPECT_EQ(test, done_step(&st, true, true, true, 4199000, 4450, 99, 10), + DONE_RESTART); + + /* Charging again clears full */ + st = (struct done_state){ .full = true }; + done_step(&st, false, true, true, 4300000, 4450, 99, 0); + KUNIT_EXPECT_FALSE(test, st.full); +} + +/* At most once per 10 min, 5 per plug-in, the 6th refused */ +static void done_test_rate_limit(struct kunit *test) +{ + struct done_state st = { }; + s64 t = 1000; + int i; + + KUNIT_EXPECT_EQ(test, done_step(&st, true, true, true, 4300000, 4450, 90, t), + DONE_RESTART); + done_restarted(&st, t); + KUNIT_EXPECT_EQ(test, done_step(&st, true, true, true, 4300000, 4450, 90, + t + 599), DONE_NONE); + for (i = 1; i < DONE_RESTART_MAX; i++) { + t += 600; + KUNIT_EXPECT_EQ_MSG(test, + done_step(&st, true, true, true, 4300000, 4450, 90, t), + DONE_RESTART, "restart %d", i + 1); + done_restarted(&st, t); + } + KUNIT_EXPECT_EQ(test, st.restarts, DONE_RESTART_MAX); + KUNIT_EXPECT_EQ(test, done_step(&st, true, true, true, 4300000, 4450, 90, + t + 6000), DONE_REFUSED); +} + +/* A restart writes mode 0 then the charge mode, and never while inhibited */ +static void done_test_restart_writes(struct kunit *test) +{ + struct fake_chip *c; + struct max77759_charger *chg = fake_charger(test, &c); + u8 *mode = &c->regs[MAX77759_CHGR_REG_CHG_CNFG_00]; + + chg->mode = MAX77759_CHGR_MODE_CHG_BUCK_ON; + *mode = MAX77759_CHGR_MODE_CHG_BUCK_ON; + KUNIT_EXPECT_TRUE(test, charger_restart(chg)); + KUNIT_ASSERT_EQ(test, c->nlog, 2); + KUNIT_EXPECT_EQ(test, c->log[0], MAX77759_CHGR_REG_CHG_CNFG_00); + KUNIT_EXPECT_EQ(test, c->vlog[0] & MAX77759_CHGR_REG_CHG_CNFG_00_MODE, + MAX77759_CHGR_MODE_OFF); + KUNIT_EXPECT_EQ(test, c->log[1], MAX77759_CHGR_REG_CHG_CNFG_00); + KUNIT_EXPECT_EQ(test, c->vlog[1] & MAX77759_CHGR_REG_CHG_CNFG_00_MODE, + MAX77759_CHGR_MODE_CHG_BUCK_ON); + + /* Inhibited: no write, not run */ + chg->stop_inhibit = true; + *mode = MAX77759_CHGR_MODE_BUCK_ON; + KUNIT_EXPECT_FALSE(test, charger_restart(chg)); + KUNIT_EXPECT_EQ(test, c->nlog, 2); + chg->stop_inhibit = false; + + /* The off write fails: not run */ + *mode = MAX77759_CHGR_MODE_CHG_BUCK_ON; + c->fail_writes = 1; + KUNIT_EXPECT_FALSE(test, charger_restart(chg)); + KUNIT_EXPECT_EQ(test, *mode, MAX77759_CHGR_MODE_CHG_BUCK_ON); +} + +/* The tier whose float is in force, at the 4.20 V node and at 4.45 V */ +static void done_test_tiers(struct kunit *test) +{ + const struct guard_table *tbl = &guard_table_husky; + bool band_last, last_tier; + + /* 4.20 V: the first tier already has the float in force */ + done_tiers(tbl, 2, 0, 4200, &band_last, &last_tier); + KUNIT_EXPECT_TRUE(test, band_last); + KUNIT_EXPECT_TRUE(test, last_tier); + done_tiers(tbl, 5, 0, 4200, &band_last, &last_tier); + KUNIT_EXPECT_TRUE(test, last_tier); + + /* 4.45 V: only the 4.48 V tier, in a band that reaches it */ + done_tiers(tbl, 2, 2, 4450, &band_last, &last_tier); + KUNIT_EXPECT_TRUE(test, band_last); + KUNIT_EXPECT_FALSE(test, last_tier); + done_tiers(tbl, 2, 3, 4450, &band_last, &last_tier); + KUNIT_EXPECT_TRUE(test, last_tier); + done_tiers(tbl, 4, 1, 4450, &band_last, &last_tier); + KUNIT_EXPECT_FALSE(test, band_last); + KUNIT_EXPECT_FALSE(test, last_tier); +} + +/* A husky charger in band 20-42 C with the cable in, guard on */ +static struct max77759_charger *done_charger(struct kunit *test, + struct fake_chip **c, u32 fv_mv) +{ + struct max77759_charger *chg = fake_charger(test, c); + + chg->guard_table = &guard_table_husky; + chg->guard_band = -2; + chg->fv_max_mv = fv_mv; + chg->cc_max_ua = 1485000; + chg->design_uah = 4950000; + chg->mode = MAX77759_CHGR_MODE_CHG_BUCK_ON; + (*c)->regs[MAX77759_CHGR_REG_CHG_CNFG_00] = + MAX77759_CHGR_MODE_CHG_BUCK_ON; + (*c)->regs[MAX77759_CHGR_REG_CHG_INT_OK] = + MAX77759_CHGR_REG_CHG_INT_CHGIN; + (*c)->regs[MAX77759_CHGR_REG_CHG_DETAILS_01] = + FIELD_PREP(MAX77759_CHGR_REG_CHG_DETAILS_01_CHG_DTLS, + MAX77759_CHGR_CHG_DTLS_DONE); + return chg; +} + +/* + * A guard run end to end: with the 4.20 V node the husky guard raises + * the float to 4.45 V, and a DONE at 4.42 V in the last tier is full + */ +static void done_test_guard_run_full(struct kunit *test) +{ + bool saved = temp_guard; + struct fake_chip *c; + struct max77759_charger *chg; + + temp_guard = true; + chg = done_charger(test, &c, 4200); + guard_run(chg, true, 300, 4420000); + KUNIT_EXPECT_EQ(test, get_float_voltage(chg), 4450); + KUNIT_EXPECT_EQ(test, chg->guard_tier, 3); + KUNIT_EXPECT_EQ(test, chg->done_last, DONE_FULL); + KUNIT_EXPECT_EQ(test, get_status(chg), POWER_SUPPLY_STATUS_FULL); + KUNIT_EXPECT_EQ(test, chg->done.restarts, 0); + temp_guard = saved; +} + +/* One at 4.10 V restarts once, and a replug gives the count back */ +static void done_test_guard_run_restart(struct kunit *test) +{ + bool saved = temp_guard; + struct fake_chip *c; + struct max77759_charger *chg; + + temp_guard = true; + chg = done_charger(test, &c, 4200); + guard_run(chg, true, 300, 4100000); + KUNIT_EXPECT_EQ(test, chg->done_last, DONE_RESTART); + KUNIT_EXPECT_EQ(test, chg->done.restarts, 1); + KUNIT_EXPECT_EQ(test, c->regs[MAX77759_CHGR_REG_CHG_CNFG_00], + MAX77759_CHGR_MODE_CHG_BUCK_ON); + KUNIT_EXPECT_EQ(test, get_status(chg), + POWER_SUPPLY_STATUS_NOT_CHARGING); + + /* Unplugged: the count comes back */ + c->regs[MAX77759_CHGR_REG_CHG_INT_OK] = 0; + c->regs[MAX77759_CHGR_REG_CHG_DETAILS_01] = + FIELD_PREP(MAX77759_CHGR_REG_CHG_DETAILS_01_CHG_DTLS, + MAX77759_CHGR_CHG_DTLS_OFF); + guard_run(chg, true, 300, 4100000); + KUNIT_EXPECT_EQ(test, chg->done.restarts, 0); + temp_guard = saved; +} + +/* No reading: inhibited, unchecked, and DONE is not Full */ +static void done_test_guard_run_no_reading(struct kunit *test) +{ + bool saved = temp_guard; + struct fake_chip *c; + struct max77759_charger *chg; + + temp_guard = true; + chg = done_charger(test, &c, 4200); + guard_run(chg, false, 0, 0); + KUNIT_EXPECT_EQ(test, chg->done_last, DONE_UNCHECKED); + KUNIT_EXPECT_EQ(test, get_status(chg), + POWER_SUPPLY_STATUS_NOT_CHARGING); + + temp_guard = saved; +} + +/* The mode-on write fails three times: the next guard run writes it */ +static void done_test_mode_rewrite(struct kunit *test) +{ + bool saved = temp_guard; + struct fake_chip *c; + struct max77759_charger *chg; + u8 *mode; + + temp_guard = true; + chg = done_charger(test, &c, 4200); + mode = &c->regs[MAX77759_CHGR_REG_CHG_CNFG_00]; + c->pass_writes = 1; + c->fail_writes = DONE_RESTART_TRIES; + KUNIT_EXPECT_TRUE(test, charger_restart(chg)); + KUNIT_EXPECT_EQ(test, *mode, MAX77759_CHGR_MODE_OFF); + KUNIT_EXPECT_TRUE(test, chg->mode_rewrite); + + c->regs[MAX77759_CHGR_REG_CHG_DETAILS_01] = 0; + guard_run(chg, true, 300, 4000000); + KUNIT_EXPECT_EQ(test, *mode, MAX77759_CHGR_MODE_CHG_BUCK_ON); + KUNIT_EXPECT_FALSE(test, chg->mode_rewrite); + temp_guard = saved; +} + +static void done_test_noop_work(struct work_struct *work) +{ +} + +/* With temp_guard off, the guard work writes a mode left off again */ +static void done_test_mode_rewrite_guard_off(struct kunit *test) +{ + bool saved = temp_guard; + struct fake_chip *c; + struct max77759_charger *chg; + u8 *mode; + + temp_guard = false; + chg = done_charger(test, &c, 4200); + INIT_DELAYED_WORK(&chg->guard_work, done_test_noop_work); + mode = &c->regs[MAX77759_CHGR_REG_CHG_CNFG_00]; + *mode = MAX77759_CHGR_MODE_OFF; + chg->mode_rewrite = true; + guard_work_item(&chg->guard_work.work); + cancel_delayed_work_sync(&chg->guard_work); + KUNIT_EXPECT_EQ(test, *mode, MAX77759_CHGR_MODE_CHG_BUCK_ON); + KUNIT_EXPECT_FALSE(test, chg->mode_rewrite); + temp_guard = saved; +} + +/* + * A band that reaches the last tier, DONE in a lower tier: never full, + * even near the float, and full does not carry over; restart. + */ +static void done_test_below_last_tier(struct kunit *test) +{ + struct done_state st = { }; + + KUNIT_EXPECT_EQ(test, done_step(&st, true, true, false, 4440000, 4450, 100, 0), + DONE_RESTART); + KUNIT_EXPECT_FALSE(test, st.full); + + st = (struct done_state){ .full = true }; + KUNIT_EXPECT_EQ(test, done_step(&st, true, true, false, 4440000, 4450, 99, 0), + DONE_RESTART); + KUNIT_EXPECT_FALSE(test, st.full); +} + +/* Husky, guard on: CHGIN gone gives the restarts back, CHGIN there keeps them */ +static void done_test_plug_chgin(struct kunit *test) +{ + bool saved = temp_guard; + struct fake_chip *c; + struct max77759_charger *chg = fake_charger(test, &c); + + chg->guard_table = &guard_table_husky; + temp_guard = true; + + chg->done = (struct done_state){ .restarts = DONE_RESTART_MAX, + .last_s = 100 }; + chg->done_warned = true; + c->regs[MAX77759_CHGR_REG_CHG_INT_OK] = MAX77759_CHGR_REG_CHG_INT_CHGIN; + done_plug_check(chg); + KUNIT_EXPECT_EQ(test, chg->done.restarts, DONE_RESTART_MAX); + KUNIT_EXPECT_TRUE(test, chg->done_warned); + + c->regs[MAX77759_CHGR_REG_CHG_INT_OK] = 0; + done_plug_check(chg); + KUNIT_EXPECT_EQ(test, chg->done.restarts, 0); + KUNIT_EXPECT_FALSE(test, chg->done_warned); + + /* Other boards: no read */ + chg->guard_table = &guard_table_default; + chg->done.restarts = 3; + c->nread = 0; + done_plug_check(chg); + KUNIT_EXPECT_EQ(test, c->nread, 0); + KUNIT_EXPECT_EQ(test, chg->done.restarts, 3); + temp_guard = saved; +} + +/* While the check is in force, DONE reads Full only when it accepted it */ +static void done_test_status(struct kunit *test) +{ + static const int not_full[] = { DONE_NONE, DONE_RESTART, DONE_REFUSED, + DONE_LIMIT, DONE_UNCHECKED }; + struct fake_chip *c; + struct max77759_charger *chg = fake_charger(test, &c); + int i; + + c->regs[MAX77759_CHGR_REG_CHG_DETAILS_01] = + FIELD_PREP(MAX77759_CHGR_REG_CHG_DETAILS_01_CHG_DTLS, + MAX77759_CHGR_CHG_DTLS_DONE); + + /* No check in force (other boards, guard off): the chip's word */ + for (i = 0; i < ARRAY_SIZE(not_full); i++) { + chg->done_last = not_full[i]; + KUNIT_EXPECT_EQ(test, get_status(chg), POWER_SUPPLY_STATUS_FULL); + } + chg->done_active = true; + chg->done_last = DONE_FULL; + KUNIT_EXPECT_EQ(test, get_status(chg), POWER_SUPPLY_STATUS_FULL); + for (i = 0; i < ARRAY_SIZE(not_full); i++) { + chg->done_last = not_full[i]; + KUNIT_EXPECT_EQ_MSG(test, get_status(chg), + POWER_SUPPLY_STATUS_NOT_CHARGING, + "action %d", not_full[i]); + } +} + +/* + * Before sleep the raised float goes back to the node's 4.20 V and the + * next guard run raises it again; a float at or under it is left alone. + */ +static void guard_test_sleep_float(struct kunit *test) +{ + bool saved = temp_guard; + struct fake_chip *c; + struct max77759_charger *chg; + + temp_guard = true; + chg = done_charger(test, &c, 4200); + c->regs[MAX77759_CHGR_REG_CHG_DETAILS_01] = 0; + guard_run(chg, true, 300, 4000000); + KUNIT_ASSERT_EQ(test, get_float_voltage(chg), 4450); + + KUNIT_EXPECT_EQ(test, guard_sleep_float(chg), 0); + KUNIT_EXPECT_EQ(test, get_float_voltage(chg), 4200); + KUNIT_EXPECT_FALSE(test, chg->inhibit); + + c->nlog = 0; + KUNIT_EXPECT_EQ(test, guard_sleep_float(chg), 0); + KUNIT_EXPECT_EQ(test, c->nlog, 0); + + guard_run(chg, true, 300, 4000000); + KUNIT_EXPECT_EQ(test, get_float_voltage(chg), 4450); + + /* The chip does not take it: charging inhibited, sleep allowed */ + c->fail_writes = 1; + KUNIT_EXPECT_EQ(test, guard_sleep_float(chg), 0); + KUNIT_EXPECT_TRUE(test, chg->inhibit); + KUNIT_EXPECT_EQ(test, c->regs[MAX77759_CHGR_REG_CHG_CNFG_00], + MAX77759_CHGR_MODE_BUCK_ON); + temp_guard = saved; +} + +static int guard_test_work_runs; + +static void guard_test_count_work(struct work_struct *work) +{ + guard_test_work_runs++; +} + +/* + * The hooks: suspend lowers the raised float; when neither the float + * nor the inhibit write works it refuses the sleep; resume runs the + * guard work again. + */ +static void guard_test_suspend_hooks(struct kunit *test) +{ + bool saved = temp_guard; + struct fake_chip *c; + struct max77759_charger *chg; + + temp_guard = true; + chg = done_charger(test, &c, 4200); + INIT_DELAYED_WORK(&chg->guard_work, guard_test_count_work); + guard_test_work_runs = 0; + dev_set_drvdata(chg->dev, chg); + c->regs[MAX77759_CHGR_REG_CHG_DETAILS_01] = 0; + guard_run(chg, true, 300, 4000000); + KUNIT_ASSERT_EQ(test, get_float_voltage(chg), 4450); + + KUNIT_EXPECT_EQ(test, max77759_charger_suspend(chg->dev), 0); + KUNIT_EXPECT_EQ(test, get_float_voltage(chg), 4200); + /* Disabled while asleep: a queue attempt does nothing */ + KUNIT_EXPECT_FALSE(test, schedule_delayed_work(&chg->guard_work, 0)); + KUNIT_EXPECT_EQ(test, max77759_charger_resume(chg->dev), 0); + flush_delayed_work(&chg->guard_work); + KUNIT_EXPECT_EQ(test, guard_test_work_runs, 1); + + guard_run(chg, true, 300, 4000000); + KUNIT_ASSERT_EQ(test, get_float_voltage(chg), 4450); + c->fail_writes = 2; + KUNIT_EXPECT_LT(test, max77759_charger_suspend(chg->dev), 0); + KUNIT_EXPECT_FALSE(test, chg->inhibit); + /* Refused: the work is enabled and queued again */ + flush_delayed_work(&chg->guard_work); + KUNIT_EXPECT_EQ(test, guard_test_work_runs, 2); + KUNIT_EXPECT_TRUE(test, schedule_delayed_work(&chg->guard_work, 0)); + flush_delayed_work(&chg->guard_work); + KUNIT_EXPECT_EQ(test, guard_test_work_runs, 3); + temp_guard = saved; +} + +/* + * 46-48 C with the 4.20 V node: the band stops at 4.30 V, under the + * husky cap, so its DONE is the band's limit. With the node's 4.20 V as + * the cap it would count as full. + */ +static void done_test_guard_run_limit(struct kunit *test) +{ + bool saved = temp_guard; + struct fake_chip *c; + struct max77759_charger *chg; + + temp_guard = true; + chg = done_charger(test, &c, 4200); + guard_run(chg, true, 470, 4290000); + KUNIT_EXPECT_EQ(test, get_float_voltage(chg), 4300); + KUNIT_EXPECT_EQ(test, chg->done_last, DONE_LIMIT); + temp_guard = saved; +} + +/* Either inhibit puts the chip in buck-only mode; both must clear */ +static void inhibit_test_two_flags(struct kunit *test) +{ + 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(guard_test_fv_cap), + KUNIT_CASE(guard_test_sleep_float), + KUNIT_CASE(guard_test_suspend_hooks), + KUNIT_CASE(done_test_guard_run_limit), + KUNIT_CASE(round_test_float), + KUNIT_CASE(round_test_current), + KUNIT_CASE(stop_test_set_threshold), + 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), + KUNIT_CASE(done_test_full_rule), + KUNIT_CASE(done_test_recharge), + KUNIT_CASE(done_test_rate_limit), + KUNIT_CASE(done_test_restart_writes), + KUNIT_CASE(done_test_below_last_tier), + KUNIT_CASE(done_test_plug_chgin), + KUNIT_CASE(done_test_status), + KUNIT_CASE(done_test_tiers), + KUNIT_CASE(done_test_mode_rewrite), + KUNIT_CASE(done_test_mode_rewrite_guard_off), + KUNIT_CASE(done_test_guard_run_full), + KUNIT_CASE(done_test_guard_run_restart), + KUNIT_CASE(done_test_guard_run_no_reading), + { } +}; + +static struct kunit_suite guard_test_suite = { + .name = "max77759_charger_guard", + .test_cases = guard_test_cases, +}; + +kunit_test_suite(guard_test_suite);