From 81eeda15fbe097e92c36962e7f954c4677c9b22d Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Fri, 5 May 2023 12:09:30 -0600 Subject: charger: Avoid accessing curr.ac in base_check_extpower() We want to move this function into its own file where it will not have access to the 'curr' state. Pass in the ac value and use the return value to indicate whether it should be zeroed. Pass in prev_ac as well, for the same reason. This changes the ordering of curr.ac being set to 0, so it happens after board_base_reset() has been called. From what I can tell, this does not matter, although there is a lot of code what could be affected by the base_detect_deferred hook or the call to base_detect_change(). So this makes no functional change. BUG=b:218332694 TEST=zmake build dev-posix Check size on lux: *** 69552 bytes in flash and 1152 bytes in RAM lux RO **** *** 69448 bytes in flash and 1120 bytes in RAM lux RW **** Change-Id: Ie2171016b692d233fda4f7bd527e74cbb965702e Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/4510243 Commit-Queue: Simon Glass Reviewed-by: Tomasz Michalec Tested-by: Simon Glass --- common/charge_state_v2.c | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/common/charge_state_v2.c b/common/charge_state_v2.c index 880756e384..41cb86753b 100644 --- a/common/charge_state_v2.c +++ b/common/charge_state_v2.c @@ -1724,21 +1724,33 @@ static void charger_setup(const struct charger_info *info) battery_level_shutdown = board_set_battery_level_shutdown(); } -/* Check base external-power settings and react as needed */ -static void base_check_extpower(void) +/* + * Check base external-power settings and react as needed + * + * @param ac Current ac value from struct charge_state_data + * @param prev_ac Previous value of ac + * Returns true if ac should be zeroed, false to leave it along + */ +static bool base_check_extpower(int ac, int prev_ac) { /* This #ifdef protects access to vars that are otherwise unavailable */ #ifdef CONFIG_EC_EC_COMM_BATTERY_CLIENT + bool zero_ac = false; + /* - * When base is powering the system, make sure curr.ac stays 0. + * When base is powering the system, make sure ac stays 0. * TODO(b:71723024): Fix extpower_is_present() in hardware instead. */ - if (base_responsive && prev_current_base < 0) - curr.ac = 0; + if (base_responsive && prev_current_base < 0) { + ac = 0; + zero_ac = true; + } /* System is off: if AC gets connected, reset the base. */ - if (chipset_in_state(CHIPSET_STATE_ANY_OFF) && !prev_ac && curr.ac) + if (chipset_in_state(CHIPSET_STATE_ANY_OFF) && !prev_ac && ac) board_base_reset(); + + return zero_ac; #endif } @@ -2084,8 +2096,10 @@ int calculate_sleep_dur(int battery_critical, int sleep_usec) static void check_extpower(int chgnum) { curr.ac = extpower_is_present(); - if (IS_ENABLED(CONFIG_EC_EC_COMM_BATTERY_CLIENT)) - base_check_extpower(); + if (IS_ENABLED(CONFIG_EC_EC_COMM_BATTERY_CLIENT)) { + if (base_check_extpower(curr.ac, prev_ac)) + curr.ac = 0; + } if (curr.ac != prev_ac) process_ac_change(chgnum); -- cgit v1.2.1