From ac063ff4df03f23ab06d6c7275321b1fdcfaae86 Mon Sep 17 00:00:00 2001 From: Daisuke Nojiri Date: Fri, 9 Nov 2018 09:43:42 -0800 Subject: Battery: Use host full capacity to compute display percentage The full capacity known by the host may slightly differ from the full capacity known by the EC because we notify the host of the new capacity only if the difference is larger than 5 mAh. This patch makes the EC use the host's full capacity instead of the local full capacity to compute the display percentage. Signed-off-by: Daisuke Nojiri BUG=b:109954565,b:80270446 BRANCH=none TEST=Verify display percentages printed by EC and power_supply_info move up synchronously on charge and the LED and the taskbar icon turn to full at the same time. TEST=buildall Change-Id: Ie695a9937a22fc7a769b82448f4600d4491935b3 Reviewed-on: https://chromium-review.googlesource.com/1330101 Commit-Ready: Daisuke Nojiri Tested-by: Daisuke Nojiri Reviewed-by: Jett Rink Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3872564 Auto-Submit: Daisuke Nojiri Commit-Queue: Daisuke Nojiri --- common/battery.c | 34 ++++++++++++++++++---------------- driver/battery/smart.c | 3 +++ include/config.h | 7 ++++++- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/common/battery.c b/common/battery.c index 8ed0626af2..621bb14c0f 100644 --- a/common/battery.c +++ b/common/battery.c @@ -22,9 +22,9 @@ #define CPRINTS(format, args...) cprints(CC_CHARGER, format, ## args) /* See config.h for details */ -static int batt_full_factor = CONFIG_BATT_FULL_FACTOR; -static int batt_host_full_factor = CONFIG_BATT_HOST_FULL_FACTOR; -static int batt_host_shutdown_pct = CONFIG_BATT_HOST_SHUTDOWN_PERCENTAGE; +const static int batt_full_factor = CONFIG_BATT_FULL_FACTOR; +const static int batt_host_full_factor = CONFIG_BATT_HOST_FULL_FACTOR; +const static int batt_host_shutdown_pct = CONFIG_BATT_HOST_SHUTDOWN_PERCENTAGE; #ifdef CONFIG_BATTERY_V2 /* @@ -567,6 +567,7 @@ void battery_compensate_params(struct batt_params *batt) int numer, denom; int remain = batt->remaining_capacity; int full = batt->full_capacity; + int lfcc = *(int *)host_get_memmap(EC_MEMMAP_BATT_LFCC); if ((batt->flags & BATT_FLAG_BAD_FULL_CAPACITY) || (batt->flags & BATT_FLAG_BAD_REMAINING_CAPACITY)) @@ -575,18 +576,19 @@ void battery_compensate_params(struct batt_params *batt) if (remain <= 0 || full <= 0) return; - if (batt_host_full_factor == 100) { - /* full_factor is effectively disabled in powerd. */ - batt->full_capacity = full * batt_full_factor / 100; - full = batt->full_capacity; - if (remain > full) { - batt->remaining_capacity = full; - remain = batt->remaining_capacity; - } - } else if (remain * 100 > full * batt_full_factor) { - batt->remaining_capacity = full; - batt->display_charge = 1000; + /* full_factor != 100 isn't supported. EC and host are not able to + * act on soc changes synchronously. */ + if (batt_host_full_factor != 100) return; + + /* full_factor is effectively disabled in powerd. */ + batt->full_capacity = full * batt_full_factor / 100; + if (lfcc == 0) + /* EC just reset. Assume host full is equal. */ + lfcc = batt->full_capacity; + if (remain > lfcc) { + batt->remaining_capacity = lfcc; + remain = batt->remaining_capacity; } /* @@ -594,8 +596,8 @@ void battery_compensate_params(struct batt_params *batt) * charge = 100 * remain/full; * 100 * (charge - shutdown_pct) / (full_factor - shutdown_pct); */ - numer = (100 * remain - full * batt_host_shutdown_pct) * 1000; - denom = full * (batt_host_full_factor - batt_host_shutdown_pct); + numer = (100 * remain - lfcc * batt_host_shutdown_pct) * 1000; + denom = lfcc * (100 - batt_host_shutdown_pct); /* Rounding (instead of truncating) */ batt->display_charge = (numer + denom / 2) / denom; if (batt->display_charge < 0) diff --git a/driver/battery/smart.c b/driver/battery/smart.c index fd27e55ab9..cc3ef73c29 100644 --- a/driver/battery/smart.c +++ b/driver/battery/smart.c @@ -395,7 +395,10 @@ void battery_get_params(struct batt_params *batt) /* Force both to zero */ batt_new.desired_voltage = batt_new.desired_current = 0; +#ifdef HAS_TASK_HOSTCMD + /* if there is no host, we don't care about compensation */ battery_compensate_params(&batt_new); +#endif /* Update visible battery parameters */ memcpy(batt, &batt_new, sizeof(*batt)); diff --git a/include/config.h b/include/config.h index 712edde714..c3abb4bf93 100644 --- a/include/config.h +++ b/include/config.h @@ -500,9 +500,14 @@ * will be equal to full_capacity eventually. This used to be done in ACPI. */ #define CONFIG_BATT_FULL_FACTOR 98 -#define CONFIG_BATT_HOST_FULL_FACTOR 94 #define CONFIG_BATT_HOST_SHUTDOWN_PERCENTAGE 4 +/* + * Powerd's full_factor. It has to be 100(%) to get display battery percentage. + * Otherwise, display percentages will be always zero. + */ +#define CONFIG_BATT_HOST_FULL_FACTOR 94 + /* * Expose some data when it is needed. * For example, battery disconnect state -- cgit v1.2.1