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 <subratabanik@google.com>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/90614
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Kapil Porwal <kapilporwal@google.com>
This commit is contained in:
Subrata Banik 2025-12-24 08:49:36 +00:00
commit 06c83d473b
2 changed files with 32 additions and 0 deletions

View file

@ -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, &params, &resp) < 0)
return -1;
*state = resp.get_state.batt_state_of_charge;
return 0;
}

View file

@ -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 */