summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaisuke Nojiri <dnojiri@chromium.org>2018-10-29 14:10:08 -0700
committerChromeOS Commit Bot <chromeos-commit-bot@chromium.org>2018-11-08 17:14:09 +0000
commitb40bf9759df1887dc87e46cd74eafe74dd6c3c2f (patch)
treef4834e678fb40e571dd79106c22fc5c61dc88077
parentedf6911aacf1ea1707311cdb971a3c399358b838 (diff)
downloadchrome-ec-b40bf9759df1887dc87e46cd74eafe74dd6c3c2f.tar.gz
Battery: Get display charge percentage
This patch converts the actual battery charge to the display percentage using the same conversion used by Powerd. EC can use this number to control LEDs synchronously to the value on the display. Signed-off-by: Daisuke Nojiri <dnojiri@chromium.org> BUG=b:109954565,b:80270446 BRANCH=none TEST=Verify charge LED changes to white (full) on Sona synchronously to the display percentage. TEST=Verify charge LED changes to blinking white (low) on Sona within 30 seconds synchronously to the display percentage. Change-Id: I2041cb768dee27b8dba94a32db0eb62dfa14c73b Reviewed-on: https://chromium-review.googlesource.com/1309033 Commit-Ready: Daisuke Nojiri <dnojiri@chromium.org> Tested-by: Daisuke Nojiri <dnojiri@chromium.org> Reviewed-by: Jett Rink <jettrink@chromium.org> (cherry picked from commit 776eadc1b4b1a92688c441cbebfacf1eea6471fc) Reviewed-on: https://chromium-review.googlesource.com/c/1327101 Reviewed-by: Daisuke Nojiri <dnojiri@chromium.org> Commit-Queue: Daisuke Nojiri <dnojiri@chromium.org>
-rw-r--r--common/battery.c17
-rw-r--r--include/battery.h1
-rw-r--r--include/config.h1
3 files changed, 18 insertions, 1 deletions
diff --git a/common/battery.c b/common/battery.c
index e012a75e4c..a21dcef66c 100644
--- a/common/battery.c
+++ b/common/battery.c
@@ -26,6 +26,7 @@
* TODO: Allow host (powerd) to update it.
*/
static int batt_full_factor = CONFIG_BATT_FULL_FACTOR;
+static int batt_host_shutdown_pct = CONFIG_BATT_HOST_SHUTDOWN_PERCENTAGE;
#ifdef CONFIG_BATTERY_V2
/*
@@ -564,6 +565,7 @@ DECLARE_HOOK(HOOK_INIT, battery_init, HOOK_PRIO_DEFAULT);
void battery_compensate_params(struct batt_params *batt)
{
+ int numer, denom;
int remain = batt->remaining_capacity;
int full = batt->full_capacity;
@@ -574,6 +576,19 @@ void battery_compensate_params(struct batt_params *batt)
if (remain <= 0 || full <= 0)
return;
- if (remain * 100 > full * batt_full_factor)
+ if (remain * 100 > full * batt_full_factor) {
batt->remaining_capacity = full;
+ batt->display_charge = 1000;
+ return;
+ }
+
+ /*
+ * Powerd uses the following equation to calculate display percentage:
+ * charge = remain/full;
+ * 100 * (charge - shutdown_pct) / (full_factor - shutdown_pct);
+ */
+ numer = (100 * remain - full * batt_host_shutdown_pct) * 1000;
+ denom = full * (batt_full_factor - batt_host_shutdown_pct);
+ /* Rounding (instead of truncating) */
+ batt->display_charge = (numer + denom / 2) / denom;
}
diff --git a/include/battery.h b/include/battery.h
index 38d7dd32aa..e3eec84164 100644
--- a/include/battery.h
+++ b/include/battery.h
@@ -85,6 +85,7 @@ struct batt_params {
int desired_current; /* Charging current desired by battery (mA) */
int remaining_capacity; /* Remaining capacity in mAh */
int full_capacity; /* Capacity in mAh (might change occasionally) */
+ int display_charge; /* Display charge in 10ths of a % (1000=100.0%) */
int status; /* Battery status */
enum battery_present is_present; /* Is the battery physically present */
int flags; /* Flags */
diff --git a/include/config.h b/include/config.h
index 4b1403bca1..b4d885ac14 100644
--- a/include/config.h
+++ b/include/config.h
@@ -412,6 +412,7 @@
* the host boots.
*/
#define CONFIG_BATT_FULL_FACTOR 98
+#define CONFIG_BATT_HOST_SHUTDOWN_PERCENTAGE 4
/*
* Expose some data when it is needed.