[PATCH 11/22] cros_ec: Allow reading the battery-charge state

Simon Glass sjg at chromium.org
Tue Jul 6 00:32:49 CEST 2021


Add a function to read this information from the EC. It is useful for
determining whether the battery has enough charge to boot.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

 drivers/misc/cros_ec.c | 17 +++++++++++++++++
 include/cros_ec.h      |  8 ++++++++
 2 files changed, 25 insertions(+)

diff --git a/drivers/misc/cros_ec.c b/drivers/misc/cros_ec.c
index 7904d5cc72d..0818ef8ad6e 100644
--- a/drivers/misc/cros_ec.c
+++ b/drivers/misc/cros_ec.c
@@ -1661,6 +1661,23 @@ int cros_ec_get_switches(struct udevice *dev)
 	return ret;
 }
 
+int cros_ec_read_batt_charge(struct udevice *dev, uint *chargep)
+{
+	struct ec_params_charge_state req;
+	struct ec_response_charge_state resp;
+	int ret;
+
+	req.cmd = CHARGE_STATE_CMD_GET_STATE;
+	ret = ec_command(dev, EC_CMD_CHARGE_STATE, 0, &req, sizeof(req),
+			 &resp, sizeof(resp));
+	if (ret)
+		return log_msg_ret("read", ret);
+
+	*chargep = resp.get_state.batt_state_of_charge;
+
+	return 0;
+}
+
 UCLASS_DRIVER(cros_ec) = {
 	.id		= UCLASS_CROS_EC,
 	.name		= "cros-ec",
diff --git a/include/cros_ec.h b/include/cros_ec.h
index 9396b4d2466..9dab6cdf9ba 100644
--- a/include/cros_ec.h
+++ b/include/cros_ec.h
@@ -652,4 +652,12 @@ int cros_ec_vstore_read(struct udevice *dev, int slot, uint8_t *data);
 int cros_ec_vstore_write(struct udevice *dev, int slot, const uint8_t *data,
 			 size_t size);
 
+/**
+ * cros_ec_read_batt_charge() - Read the battery-charge state
+ *
+ * @dev: CROS-EC device
+ * @chargep: Return battery-charge state as a percentage
+ */
+int cros_ec_read_batt_charge(struct udevice *dev, uint *chargep);
+
 #endif
-- 
2.32.0.93.g670b81a890-goog



More information about the U-Boot mailing list