From 7db78001c2c262f332f5e00fdf6d86cf05ff897e Mon Sep 17 00:00:00 2001 From: Vijay Hiremath Date: Thu, 5 Jan 2017 23:17:37 -0800 Subject: [PATCH] charge_state_v2: Correct Smart battery charging/discharging status AverageTimeToFull() and AverageTimeToEmpty() are the predicted time based on the AverageCurrent(), these do not reflect the instant time of charging or discharging. Hence we observe huge number of time (1092h because register has 65535) to full time when battery starts accepting current upon reaching 100%. To overcome this issue, explicitly checking if the AverageTimeToFull() and AverageTimeToEmpty() register values are updated with the valid data. BUG=chrome-os-partner:60802 BRANCH=none TEST=Manually tested on Reef. Charge the battery to 100%, once the battery starts accepting current again observed time to full is not huge number. Change-Id: I6d6c21b72ab824dbe47e44e1e77f1c5319ac2720 Signed-off-by: Vijay Hiremath Reviewed-on: https://chromium-review.googlesource.com/425324 Commit-Ready: Vijay P Hiremath Tested-by: Vijay P Hiremath Reviewed-by: Aaron Durbin --- common/charge_state_v2.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/common/charge_state_v2.c b/common/charge_state_v2.c index 94dd210972..bd1404be11 100644 --- a/common/charge_state_v2.c +++ b/common/charge_state_v2.c @@ -314,8 +314,31 @@ static void dump_charge_state(void) static void show_charging_progress(void) { - int rv, minutes, to_full; + int rv = 0, minutes, to_full; +#ifdef CONFIG_BATTERY_SMART + /* + * Predicted remaining battery capacity based on AverageCurrent(). + * 65535 = Battery is not being discharged. + */ + if (!battery_time_to_empty(&minutes) && minutes != 65535) + to_full = 0; + /* + * Predicted time-to-full charge based on AverageCurrent(). + * 65535 = Battery is not being discharged. + */ + else if (!battery_time_to_full(&minutes) && minutes != 65535) + to_full = 1; + /* + * If both time to empty and time to full have invalid data, consider + * measured current from the coulomb counter and ac present status to + * decide whether battery is about to full or empty. + */ + else { + to_full = curr.batt_is_charging; + rv = EC_ERROR_UNKNOWN; + } +#else if (!curr.batt_is_charging) { rv = battery_time_to_empty(&minutes); to_full = 0; @@ -323,6 +346,7 @@ static void show_charging_progress(void) rv = battery_time_to_full(&minutes); to_full = 1; } +#endif if (rv) CPRINTS("Battery %d%% / ??h:?? %s%s",