diff options
-rw-r--r-- | common/battery.c | 34 | ||||
-rw-r--r-- | driver/battery/smart.c | 3 | ||||
-rw-r--r-- | 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,10 +500,15 @@ * 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 */ |