From 06c83d473b2682203fcf9ce8f64c399abff9e3a6 Mon Sep 17 00:00:00 2001 From: Subrata Banik Date: Wed, 24 Dec 2025 08:49:36 +0000 Subject: [PATCH] ec/google/chromeec: Add function to read battery state of charge Implement google_chromeec_read_batt_state_of_charge() to retrieve the current battery percentage from the Embedded Controller. The function uses the CHARGE_STATE_CMD_GET_STATE host command to fetch the State of Charge (SoC) as calculated by the EC's fuel gauge logic. This provides a high-level percentage (0-100) suitable for power management decisions or UI display. Change-Id: Iec88476214088476214088476214088476214088 Signed-off-by: Subrata Banik Reviewed-on: https://review.coreboot.org/c/coreboot/+/90614 Tested-by: build bot (Jenkins) Reviewed-by: Kapil Porwal --- src/ec/google/chromeec/ec.c | 24 ++++++++++++++++++++++++ src/ec/google/chromeec/ec.h | 8 ++++++++ 2 files changed, 32 insertions(+) diff --git a/src/ec/google/chromeec/ec.c b/src/ec/google/chromeec/ec.c index 03ae01c03d..d7e956068c 100644 --- a/src/ec/google/chromeec/ec.c +++ b/src/ec/google/chromeec/ec.c @@ -1860,3 +1860,27 @@ void platform_do_early_poweroff(void) google_chromeec_reboot(EC_REBOOT_COLD_AP_OFF, 0); halt(); } + +/* + * Reads the current battery charge percentage. + * + * This function communicates with the Embedded Controller (EC) via a host + * command to retrieve the "State of Charge" (SoC) as calculated by the battery fuel gauge. + * + * Return: 0 on success, -1 on failure (communication error or EC rejection). + * Return Value (state): Pointer to a uint32_t where the battery state of charge + * (0-100) will be stored. + */ +int google_chromeec_read_batt_state_of_charge(uint32_t *state) +{ + struct ec_params_charge_state params; + struct ec_response_charge_state resp; + + params.cmd = CHARGE_STATE_CMD_GET_STATE; + + if (ec_cmd_charge_state(PLAT_EC, ¶ms, &resp) < 0) + return -1; + + *state = resp.get_state.batt_state_of_charge; + return 0; +} diff --git a/src/ec/google/chromeec/ec.h b/src/ec/google/chromeec/ec.h index 2f29b9d21c..e4120dabeb 100644 --- a/src/ec/google/chromeec/ec.h +++ b/src/ec/google/chromeec/ec.h @@ -538,4 +538,12 @@ bool chipset_emi_write_bytes(u16 port, size_t length, u8 *msg, u8 *csum); */ void chipset_ioport_range(uint16_t *base, size_t *size); +/* + * Reads the current battery charge percentage. + * + * @param state Pointer to a uint32_t where the battery state of charge (0-100) will be + * stored. + */ +int google_chromeec_read_batt_state_of_charge(uint32_t *state); + #endif /* _EC_GOOGLE_CHROMEEC_EC_H */