From 8e28608f952f5ab20781d85fe1f0da27b3b318a4 Mon Sep 17 00:00:00 2001 From: Daisuke Nojiri Date: Thu, 10 Mar 2022 01:27:38 +0000 Subject: battery/smart: Don't update fields if reading fails Currently, battery_get_params starts with batt_new = {0} and ends with memcpy(batt, batt_new). That is, if battery_get_params fails to read a field, we set a BATT_FLAG_BAD_* and clear the field. This behavior forces us to check the flags everywhere fields are read (to distinguish 0 v.s. invalid 0). It also makes EC forget the last known good values. Instead, we will always keep good values in the global batt_params. That is, we update fields only if reads are successful. (That's how other implementations of battery_get_params behave.) Thus, if display_soc is zero, it was computed legitimately as zero (except the very initial value). BUG=b:217401040,b:222722611 BRANCH=None TEST=Guybrush Signed-off-by: Daisuke Nojiri Change-Id: I5b69b708d149d1c750551d8cf957eab3fdc8aa64 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3518073 Reviewed-by: caveh jalali --- driver/battery/smart.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/driver/battery/smart.c b/driver/battery/smart.c index f30b74d71b..4497129bdd 100644 --- a/driver/battery/smart.c +++ b/driver/battery/smart.c @@ -342,9 +342,17 @@ static void apply_fake_state_of_charge(struct batt_params *batt) void battery_get_params(struct batt_params *batt) { - struct batt_params batt_new = {0}; + struct batt_params batt_new; int v; + /* + * Start with a copy so that only valid fields will be updated. Note + * sb_read doesn't change the value if I2C fails. So, the current value + * will be preserved. + */ + memcpy(&batt_new, batt, sizeof(*batt)); + batt_new.flags = 0; + if (sb_read(SB_TEMPERATURE, &batt_new.temperature)) batt_new.flags |= BATT_FLAG_BAD_TEMPERATURE; -- cgit v1.2.1