From 7f3f8308afb350e6d35cce707ca220d87a8aad21 Mon Sep 17 00:00:00 2001 From: Daisuke Nojiri Date: Fri, 10 Aug 2018 11:32:28 -0700 Subject: Nami: Report error for disconnected battery on Sona On Sona, when a battery is cutoff, the gas gauge does not clear BATTERY_DISCHARGING_DISABLED or BATTERY_CHARGING_DISABLED until a precharge current is supplied. This patch makes battery_is_present return BP_NOT_SURE for the first 5 seconds after the battery is found electrically disconnected. After 5 seconds, it reports BP_NO as it normally does. Signed-off-by: Daisuke Nojiri BUG=b:112292720 BRANCH=nami TEST=Verify the battery wakes up from cutoff mode. Verify a bad battery eventually stops being charged. Change-Id: I4d0e9e5ed0e9631b7bbf8565ce62c17da8a0a848 Reviewed-on: https://chromium-review.googlesource.com/1171259 Reviewed-by: Aaron Durbin Commit-Queue: Daisuke Nojiri Tested-by: Daisuke Nojiri (cherry picked from commit 4363e098c546f09cdfcc9c10de09b0c517d52e63) Reviewed-on: https://chromium-review.googlesource.com/1177721 Commit-Ready: Daisuke Nojiri Reviewed-by: Daisuke Nojiri --- board/nami/battery.c | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/board/nami/battery.c b/board/nami/battery.c index b34c5e18e8..d32a1b74ff 100644 --- a/board/nami/battery.c +++ b/board/nami/battery.c @@ -213,6 +213,19 @@ static int battery_init(void) !!(batt_status & STATUS_INITIALIZED); } +enum battery_disconnect_grace_period { + BATTERY_DISCONNECT_GRACE_PERIOD_OFF, + BATTERY_DISCONNECT_GRACE_PERIOD_ON, + BATTERY_DISCONNECT_GRACE_PERIOD_OVER, +}; +static enum battery_disconnect_grace_period disconnect_grace_period; + +static void battery_disconnect_timer(void) +{ + disconnect_grace_period = BATTERY_DISCONNECT_GRACE_PERIOD_OVER; +} +DECLARE_DEFERRED(battery_disconnect_timer); + /* * Check for case where both XCHG and XDSG bits are set indicating that even * though the FG can be read from the battery, the battery is not able to be @@ -238,8 +251,29 @@ static int battery_check_disconnect_ti_bq40z50(void) if ((data[3] & (BATTERY_DISCHARGING_DISABLED | BATTERY_CHARGING_DISABLED)) == - (BATTERY_DISCHARGING_DISABLED | BATTERY_CHARGING_DISABLED)) - return BATTERY_DISCONNECTED; + (BATTERY_DISCHARGING_DISABLED | BATTERY_CHARGING_DISABLED)) { + if (oem != PROJECT_SONA) + return BATTERY_DISCONNECTED; + /* + * For Sona, we need a workaround to wake up a battery from + * cutoff. We return DISCONNECT_ERROR for the 5 seconds after + * the first call BP_NOT_SURE is reported to chgstv2. It will + * supply precharge current and wakes up the battery from + * cutoff. If the battery is good, we won't come back here. + * If not, after 5 seconds, we will return DISCONNECTED to + * stop charging and avoid damaging the battery. + */ + if (disconnect_grace_period == + BATTERY_DISCONNECT_GRACE_PERIOD_OVER) + return BATTERY_DISCONNECTED; + if (disconnect_grace_period == + BATTERY_DISCONNECT_GRACE_PERIOD_OFF) + hook_call_deferred(&battery_disconnect_timer_data, + 5 * SECOND); + ccprintf("Battery disconnect grace period\n"); + disconnect_grace_period = BATTERY_DISCONNECT_GRACE_PERIOD_ON; + return BATTERY_DISCONNECT_ERROR; + } return BATTERY_NOT_DISCONNECTED; } -- cgit v1.2.1