summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaisuke Nojiri <dnojiri@chromium.org>2019-03-08 09:33:07 -0800
committerChromeOS Commit Bot <chromeos-commit-bot@chromium.org>2019-04-09 16:07:51 +0000
commit1087ae9482e7ef8f24cee471b7377d79d985da90 (patch)
tree186ada66cd3cd0b3926e910f38bc16a835bf3356
parent22f3fd0edb9d563152e6b1d47c2e7028fb63fafd (diff)
downloadchrome-ec-1087ae9482e7ef8f24cee471b7377d79d985da90.tar.gz
chgstv2: Refactor shutdown_on_critical_battery
This patch refactors shutdown_on_critical_battery. There is no change in its functionality. Signed-off-by: Daisuke Nojiri <dnojiri@chromium.org> BUG=b/123727148 BRANCH=nami,strago,coral TEST=Verify Vayne cuts off battery when soc <= 4% in S0 and S5. Change-Id: Ia6d3e2166d01803ae8983afd2d4e15d254845065 Reviewed-on: https://chromium-review.googlesource.com/1512620 Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com> Tested-by: Daisuke Nojiri <dnojiri@chromium.org> Reviewed-by: Aseda Aboagye <aaboagye@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1518294 Reviewed-by: Daisuke Nojiri <dnojiri@chromium.org> Commit-Queue: Daisuke Nojiri <dnojiri@chromium.org>
-rw-r--r--common/charge_state_v2.c93
-rw-r--r--test/sbs_charging_v2.c4
2 files changed, 54 insertions, 43 deletions
diff --git a/common/charge_state_v2.c b/common/charge_state_v2.c
index ef7bd30980..b55a826309 100644
--- a/common/charge_state_v2.c
+++ b/common/charge_state_v2.c
@@ -87,7 +87,7 @@ static enum ec_charge_control_mode chg_ctl_mode;
static int manual_voltage; /* Manual voltage override (-1 = no override) */
static int manual_current; /* Manual current override (-1 = no override) */
static unsigned int user_current_limit = -1U;
-test_export_static timestamp_t shutdown_warning_time;
+test_export_static timestamp_t shutdown_target_time;
static timestamp_t precharge_start_time;
#ifdef CONFIG_EC_EC_COMM_BATTERY_MASTER
@@ -1301,70 +1301,81 @@ enum critical_shutdown board_critical_shutdown_check(
#endif
}
-/*
- * Send host event to the AP if the battery is temperature or charge level
- * is critical. Force-shutdown if the problem isn't corrected after timeout.
- */
-static int shutdown_on_critical_battery(void)
+static int is_battery_critical(void)
{
- int batt_temp_c;
- int battery_critical = 0;
+ int batt_temp_c = DECI_KELVIN_TO_CELSIUS(curr.batt.temperature);
/*
* TODO(crosbug.com/p/27642): The thermal loop should watch the battery
* temp, so it can turn fans on.
*/
- batt_temp_c = DECI_KELVIN_TO_CELSIUS(curr.batt.temperature);
if (battery_too_hot(batt_temp_c)) {
CPRINTS("Batt temp out of range: %dC", batt_temp_c);
- battery_critical = 1;
+ return 1;
}
if (battery_too_low() && !curr.batt_is_charging) {
CPRINTS("Low battery: %d%%, %dmV",
curr.batt.state_of_charge, curr.batt.voltage);
- battery_critical = 1;
+ return 1;
}
- if (!battery_critical) {
+ return 0;
+}
+
+ /*
+ * If the battery is at extremely low charge (and discharging) or extremely
+ * high temperature, the EC will notify the AP and start a timer. If the
+ * critical condition is not corrected before the timeout expires, the EC
+ * will shut down the AP (if the AP is not already off) and then optionally
+ * hibernate or cut off battery.
+ */
+static int shutdown_on_critical_battery(void)
+{
+ if (!is_battery_critical()) {
/* Reset shutdown warning time */
- shutdown_warning_time.val = 0;
- return battery_critical;
+ shutdown_target_time.val = 0;
+ return 0;
}
- if (!shutdown_warning_time.val) {
- CPRINTS("charge warn shutdown due to critical battery");
- shutdown_warning_time = get_time();
+ if (!shutdown_target_time.val) {
+ /* Start count down timer */
+ CPRINTS("Start shutdown due to critical battery");
+ shutdown_target_time.val = get_time().val
+ + CRITICAL_BATTERY_SHUTDOWN_TIMEOUT_US;
#ifdef CONFIG_HOSTCMD_EVENTS
if (!chipset_in_state(CHIPSET_STATE_ANY_OFF))
host_set_single_event(EC_HOST_EVENT_BATTERY_SHUTDOWN);
#endif
- } else if (get_time().val > shutdown_warning_time.val +
- CRITICAL_BATTERY_SHUTDOWN_TIMEOUT_US) {
- if (chipset_in_state(CHIPSET_STATE_ANY_OFF)) {
- /* Timeout waiting for charger to provide more power */
- switch (board_critical_shutdown_check(&curr)) {
- case CRITICAL_SHUTDOWN_HIBERNATE:
- CPRINTS("Hibernate due to critical battery");
- system_hibernate(0, 0);
- break;
- case CRITICAL_SHUTDOWN_CUTOFF:
- CPRINTS("Cutoff due to critical battery");
- board_cut_off_battery();
- break;
- case CRITICAL_SHUTDOWN_IGNORE:
- default:
- break;
- }
- } else {
- /* Timeout waiting for AP to shut down, so kill it */
- CPRINTS(
- "charge force shutdown due to critical battery");
- chipset_force_shutdown();
+ return 1;
+ }
+
+ if (!timestamp_expired(shutdown_target_time, 0))
+ return 1;
+
+ /* Timer has expired */
+ if (chipset_in_state(CHIPSET_STATE_ANY_OFF)) {
+ switch (board_critical_shutdown_check(&curr)) {
+ case CRITICAL_SHUTDOWN_HIBERNATE:
+ CPRINTS("Hibernate due to critical battery");
+ system_hibernate(0, 0);
+ break;
+ case CRITICAL_SHUTDOWN_CUTOFF:
+ CPRINTS("Cutoff due to critical battery");
+ board_cut_off_battery();
+ break;
+ case CRITICAL_SHUTDOWN_IGNORE:
+ default:
+ break;
}
+ } else {
+ /* Timeout waiting for AP to shut down, so kill it */
+ CPRINTS(
+ "charge force shutdown due to critical battery");
+ chipset_force_shutdown();
}
- return battery_critical;
+ return 1;
}
/*
@@ -1530,7 +1541,7 @@ void charger_task(void *u)
prev_ac = prev_charge = prev_disp_charge = -1;
chg_ctl_mode = CHARGE_CONTROL_NORMAL;
- shutdown_warning_time.val = 0UL;
+ shutdown_target_time.val = 0UL;
battery_seems_to_be_dead = 0;
#ifdef CONFIG_EC_EC_COMM_BATTERY_MASTER
base_responsive = 0;
diff --git a/test/sbs_charging_v2.c b/test/sbs_charging_v2.c
index d2dde07cc4..f8cda0cc6c 100644
--- a/test/sbs_charging_v2.c
+++ b/test/sbs_charging_v2.c
@@ -26,14 +26,14 @@ static int is_hibernated;
static int override_voltage, override_current, override_usec;
/* The simulation doesn't really hibernate, so we must reset this ourselves */
-extern timestamp_t shutdown_warning_time;
+extern timestamp_t shutdown_target_time;
static void reset_mocks(void)
{
mock_chipset_state = CHIPSET_STATE_ON;
is_shutdown = is_force_discharge = is_hibernated = 0;
override_voltage = override_current = override_usec = 0;
- shutdown_warning_time.val = 0ULL;
+ shutdown_target_time.val = 0ULL;
}
int board_cut_off_battery(void)