summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorDaisuke Nojiri <dnojiri@chromium.org>2021-04-23 11:37:14 -0700
committerCommit Bot <commit-bot@chromium.org>2022-02-21 07:48:42 +0000
commita792fe9dda7a417e20d944d0d08cd08a64482b4d (patch)
tree32d6f543003b4ba6e50d57366010cde7a6124994 /common
parenta83c1c41fe0e34ac1605b04f82f4633a7e1f34ef (diff)
downloadchrome-ec-a792fe9dda7a417e20d944d0d08cd08a64482b4d.tar.gz
Battery: Add command to export display SoC
Currently, CrOS EC passes the battery remaining capacity (mAh) and the full capacity (mAh) through ACPI to the AP so that the host can calculate the battery SoC. The host further manipulates the SoC to get the display SoC, which is used to determine user visible behaviors. To get consistent behaviors in all power states, this change enables the EC to send the display SoC to the host via EC_CMD_DISPLAY_SOC command. The Powerd's part is I5bd1371f2569d21d55df1b50a3d709b98bbf0325. BUG=b:174433637, b:181506409, b:80270446, b:109954565 BRANCH=dedede, trogdor, nami, hatch TEST=Storo, CoachZ Signed-off-by: Daisuke Nojiri <dnojiri@chromium.org> Change-Id: Idc6992625d992a73be141987d02ed220508d3b74 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2853142 Reviewed-by: Tim Wawrzynczak <twawrzynczak@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3469842
Diffstat (limited to 'common')
-rw-r--r--common/battery.c57
1 files changed, 44 insertions, 13 deletions
diff --git a/common/battery.c b/common/battery.c
index 117bbbb111..f048a1db91 100644
--- a/common/battery.c
+++ b/common/battery.c
@@ -203,8 +203,7 @@ static void print_battery_info(void)
print_item_name("Cap-full:");
if (check_print_error(battery_full_charge_capacity(&value)))
- ccprintf("%d mAh (%d mAh with %d %% compensation)\n",
- value, value*batt_full_factor/100, batt_full_factor);
+ ccprintf("%d mAh\n", value);
#ifdef CONFIG_CHARGER_V2
print_item_name("Display:");
@@ -239,6 +238,12 @@ static void print_battery_info(void)
}
ccprintf("%dh:%d\n", hour, minute);
}
+
+ print_item_name("full_factor:");
+ ccprintf("0.%d\n", batt_host_full_factor);
+
+ print_item_name("shutdown_soc:");
+ ccprintf("%d %%\n", batt_host_shutdown_pct);
}
void print_battery_debug(void)
@@ -582,22 +587,33 @@ void battery_compensate_params(struct batt_params *batt)
if (*remain <= 0 || *full <= 0)
return;
- /* full_factor is effectively disabled in powerd. */
- *full = *full * batt_full_factor / 100;
+ /* Some batteries don't update full capacity as often. */
+ if (!IS_ENABLED(CONFIG_BATTERY_EXPORT_DISPLAY_SOC))
+ /* full_factor is effectively disabled in powerd. */
+ *full = *full * batt_full_factor / 100;
if (*remain > *full)
*remain = *full;
/*
- * Powerd uses the following equation to calculate display percentage:
- * charge = 100 * remain / full
- * display = 100 * (charge - shutdown_pct) /
- * (full_factor - shutdown_pct)
- * = 100 * ((100 * remain / full) - shutdown_pct) /
- * (full_factor - shutdown_pct)
- * = 100 * ((100 * remain) - (full * shutdown_pct)) /
- * (full * (full_factor - shutdown_pct))
+ * EC calculates the display SoC like how Powerd used to do. Powerd
+ * reads the display SoC from the EC. This design allows the system to
+ * behave consistently on a single SoC value across all power states.
+ *
+ * Display SoC is computed as follows:
*
- * The unit of the following batt->display_charge is 0.1%.
+ * actual_soc = 100 * remain / full
+ *
+ * actual_soc - shutdown_pct
+ * display_soc = --------------------------- x 1000
+ * full_factor - shutdown_pct
+ *
+ * (100 * remain / full) - shutdown_pct
+ * = ------------------------------------ x 1000
+ * full_factor - shutdown_pct
+ *
+ * 100 x remain - full x shutdown_pct
+ * = ----------------------------------- x 1000
+ * full x (full_factor - shutdown_pct)
*/
numer = 1000 * ((100 * *remain) - (*full * batt_host_shutdown_pct));
denom = *full * (batt_host_full_factor - batt_host_shutdown_pct);
@@ -609,6 +625,21 @@ void battery_compensate_params(struct batt_params *batt)
batt->display_charge = 1000;
}
+#ifdef CONFIG_BATTERY_EXPORT_DISPLAY_SOC
+static int battery_display_soc(struct host_cmd_handler_args *args)
+{
+ struct ec_response_display_soc *r = args->response;
+
+ r->display_soc = charge_get_display_charge();
+ r->full_factor = batt_host_full_factor * 10;
+ r->shutdown_soc = batt_host_shutdown_pct * 10;
+ args->response_size = sizeof(*r);
+
+ return EC_RES_SUCCESS;
+}
+DECLARE_HOST_COMMAND(EC_CMD_DISPLAY_SOC, battery_display_soc, EC_VER_MASK(0));
+#endif
+
__overridable void board_battery_compensate_params(struct batt_params *batt)
{
}