From 4863bd6683d2bbdaea58b8a12aee970fd99a2b95 Mon Sep 17 00:00:00 2001 From: Daisuke Nojiri Date: Fri, 25 Sep 2020 13:42:38 -0700 Subject: Battery: Apply faked state of charge to remaining capacity Currently, only the state of charge is modified by battfake command. This patch applies the faked state of charge to the remaining capacity as well. BUG=b:163721887 BRANCH=Octopus TEST=Verified Fleex wakes up host on BATTERY_LEVEL_CRITICAL. TEST=Verified powerd reports faked state of charge. Signed-off-by: Daisuke Nojiri Change-Id: I34eb409bab129804a983abbe2a1d021298ce044e Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2432865 Reviewed-by: Todd Broch Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3340213 Reviewed-by: Aseda Aboagye --- driver/battery/smart.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/driver/battery/smart.c b/driver/battery/smart.c index 2e347914fb..c8f9f868ec 100644 --- a/driver/battery/smart.c +++ b/driver/battery/smart.c @@ -306,6 +306,24 @@ int battery_get_avg_voltage(void) } #endif /* CONFIG_CMD_PWR_AVG */ +static void apply_fake_state_of_charge(struct batt_params *batt) +{ + int full; + + if (fake_state_of_charge < 0) + return; + + if (batt->flags & BATT_FLAG_BAD_FULL_CAPACITY) + battery_design_capacity(&full); + else + full = batt->full_capacity; + + batt->state_of_charge = fake_state_of_charge; + batt->remaining_capacity = full * fake_state_of_charge / 100; + batt->flags &= ~BATT_FLAG_BAD_STATE_OF_CHARGE; + batt->flags &= ~BATT_FLAG_BAD_REMAINING_CAPACITY; +} + void battery_get_params(struct batt_params *batt) { struct batt_params batt_new = {0}; @@ -318,10 +336,6 @@ void battery_get_params(struct batt_params *batt) && fake_state_of_charge < 0) batt_new.flags |= BATT_FLAG_BAD_STATE_OF_CHARGE; - /* If soc is faked, override with faked data */ - if (fake_state_of_charge >= 0) - batt_new.state_of_charge = fake_state_of_charge; - if (sb_read(SB_VOLTAGE, &batt_new.voltage)) batt_new.flags |= BATT_FLAG_BAD_VOLTAGE; @@ -401,6 +415,9 @@ void battery_get_params(struct batt_params *batt) board_battery_compensate_params(&batt_new); #endif + if (IS_ENABLED(CONFIG_CMD_BATTFAKE)) + apply_fake_state_of_charge(&batt_new); + /* Update visible battery parameters */ memcpy(batt, &batt_new, sizeof(*batt)); } @@ -441,8 +458,7 @@ static int command_battfake(int argc, char **argv) } if (fake_state_of_charge >= 0) - ccprintf("Fake batt %d%%\n", - fake_state_of_charge); + ccprintf("Fake batt %d%%\n", fake_state_of_charge); return EC_SUCCESS; } -- cgit v1.2.1