From 9aee2a19e81949eca86eb0b113c99d1782eaa2d2 Mon Sep 17 00:00:00 2001 From: Philip Chen Date: Mon, 19 Mar 2018 13:47:05 -0700 Subject: [PATCH] battery/max17055: Implement battery_status() BUG=b:74841068 BRANCH=scarlet TEST='/sys/class/power_supply/sbs-9-000b/status' shows discharging/charging/full status correctly Change-Id: I4216ba2d95ac82a9f600d8685d993cb5b37206d8 Signed-off-by: Philip Chen Reviewed-on: https://chromium-review.googlesource.com/969747 Commit-Ready: ChromeOS CL Exonerator Bot Tested-by: Philip Chen Reviewed-by: Aseda Aboagye --- driver/battery/max17055.c | 22 +++++++++++++++++++++- driver/battery/max17055.h | 5 +++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/driver/battery/max17055.c b/driver/battery/max17055.c index 9be2a14651..e02a4aaa42 100644 --- a/driver/battery/max17055.c +++ b/driver/battery/max17055.c @@ -197,7 +197,24 @@ int battery_get_mode(int *mode) int battery_status(int *status) { - return EC_ERROR_UNIMPLEMENTED; + int rv; + int reg; + + *status = 0; + + rv = max17055_read(REG_FSTAT, ®); + if (rv) + return rv; + if (reg & FSTAT_FQ) + *status |= BATTERY_FULLY_CHARGED; + + rv = max17055_read(REG_CURRENT, ®); + if (rv) + return rv; + if (reg >> 15) + *status |= BATTERY_DISCHARGING; + + return EC_SUCCESS; } enum battery_present battery_is_present(void) @@ -267,6 +284,9 @@ void battery_get_params(struct batt_params *batt) batt->desired_current && batt->state_of_charge < BATTERY_LEVEL_FULL) batt->flags |= BATT_FLAG_WANT_CHARGE; + + if (battery_status(&batt->status)) + batt->flags |= BATT_FLAG_BAD_STATUS; } #ifdef CONFIG_CMD_PWR_AVG diff --git a/driver/battery/max17055.h b/driver/battery/max17055.h index fb49e69c22..be070e9896 100644 --- a/driver/battery/max17055.h +++ b/driver/battery/max17055.h @@ -54,11 +54,16 @@ /* FStat reg (0x3d) flags */ #define FSTAT_DNR 0x0001 +#define FSTAT_FQ 0x0080 /* ModelCfg reg (0xdb) flags */ #define MODELCFG_REFRESH 0x8000 #define MODELCFG_VCHG 0x0400 +/* Smart battery status bits (sbs reg 0x16) */ +#define BATTERY_DISCHARGING 0x40 +#define BATTERY_FULLY_CHARGED 0x20 + /* * Before we have the battery fully characterized, we use these macros to * convert basic battery parameters to max17055 reg values for ez config.