summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYilun Lin <yllin@google.com>2018-10-11 15:13:39 +0800
committerchrome-bot <chrome-bot@chromium.org>2018-10-12 11:43:26 -0700
commit6ef20c4cf5ca6cf17b1b539f30912bf6e8d19030 (patch)
tree465a772ca6c2e1ab1750bb8666b3cad98d9fa674
parentc640f67e04b87fb30f05686c49e2337fba058b08 (diff)
downloadchrome-ec-6ef20c4cf5ca6cf17b1b539f30912bf6e8d19030.tar.gz
max17055: Support alert function.
MAX17055 support 4 kinds of alert: voltage, current, state-of-charge, and temperatore. This CL enables this functionality. datasheet: https://datasheets.maximintegrated.com/en/ds/MAX17055.pdf TEST=manually test on Kukui. BUG=b:111378620 BRANCH=None Change-Id: I94126fdc541f9456c8f48f42383bc6e2188257e2 Signed-off-by: Yilun Lin <yllin@google.com> Reviewed-on: https://chromium-review.googlesource.com/1276006 Commit-Ready: Yilun Lin <yllin@chromium.org> Tested-by: Yilun Lin <yllin@chromium.org> Reviewed-by: Nicolas Boichat <drinkcat@chromium.org>
-rw-r--r--driver/battery/max17055.c23
-rw-r--r--driver/battery/max17055.h111
-rw-r--r--include/config.h6
3 files changed, 137 insertions, 3 deletions
diff --git a/driver/battery/max17055.c b/driver/battery/max17055.c
index 07ecc473dd..9cdd107d20 100644
--- a/driver/battery/max17055.c
+++ b/driver/battery/max17055.c
@@ -423,6 +423,10 @@ static void max17055_init(void)
{
int reg;
int retries = 80;
+#ifdef CONFIG_BATTERY_MAX17055_ALERT
+ const struct max17055_alert_profile *alert_profile =
+ max17055_get_alert_profile();
+#endif
if (!max17055_probe()) {
CPRINTS("Wrong max17055 id!");
@@ -481,6 +485,25 @@ static void max17055_init(void)
}
}
+#ifdef CONFIG_BATTERY_MAX17055_ALERT
+ /* Set voltage alert range */
+ MAX17055_WRITE_DEBUG(REG_VALRTTH, alert_profile->v_alert_mxmn);
+ /* Set temperature alert range */
+ MAX17055_WRITE_DEBUG(REG_TALRTTH, alert_profile->t_alert_mxmn);
+ /* Set state-of-charge alert range */
+ MAX17055_WRITE_DEBUG(REG_SALRTTH, alert_profile->s_alert_mxmn);
+ /* Set current alert range */
+ MAX17055_WRITE_DEBUG(REG_IALRTTH, alert_profile->i_alert_mxmn);
+
+ /* Disable all sticky bits; enable alert AEN */
+ MAX17055_READ_DEBUG(REG_CONFIG, &reg);
+ MAX17055_WRITE_DEBUG(REG_CONFIG, (reg & ~CONF_ALL_STICKY) | CONF_AEN);
+
+ /* Clear alerts */
+ MAX17055_READ_DEBUG(REG_STATUS, &reg);
+ MAX17055_WRITE_DEBUG(REG_STATUS, reg & ~STATUS_ALL_ALRT);
+#endif
+
CPRINTS("max17055 configuration succeeded!");
}
DECLARE_HOOK(HOOK_INIT, max17055_init, HOOK_PRIO_DEFAULT);
diff --git a/driver/battery/max17055.h b/driver/battery/max17055.h
index 5a3764d9de..f223fa15e8 100644
--- a/driver/battery/max17055.h
+++ b/driver/battery/max17055.h
@@ -12,6 +12,9 @@
#define MAX17055_DEVICE_ID 0x4010
#define REG_STATUS 0x00
+#define REG_VALRTTH 0x01
+#define REG_TALRTTH 0x02
+#define REG_SALRTTH 0x03
#define REG_AT_RATE 0x04
#define REG_REMAINING_CAPACITY 0x05
#define REG_STATE_OF_CHARGE 0x06
@@ -27,6 +30,9 @@
#define REG_CYCLE_COUNT 0x17
#define REG_DESIGN_CAPACITY 0x18
#define REG_AVERAGE_VOLTAGE 0x19
+#define REG_MAX_MIN_TEMP 0x1a
+#define REG_MAX_MIN_VOLT 0x1b
+#define REG_MAX_MIN_CURR 0x1c
#define REG_CHARGE_TERM_CURRENT 0x1e
#define REG_TIME_TO_FULL 0x20
#define REG_DEVICE_NAME 0x21
@@ -42,17 +48,41 @@
#define REG_DQACC 0x45
#define REG_DPACC 0x46
#define REG_STATUS2 0xb0
+#define REG_IALRTTH 0xb4
#define REG_HIBCFG 0xba
#define REG_CONFIG2 0xbb
#define REG_TIMERH 0xbe
#define REG_MODELCFG 0xdb
/* Status reg (0x00) flags */
-#define STATUS_POR 0x0002
-#define STATUS_BST 0x0008
+#define STATUS_POR (1 << 1)
+#define STATUS_IMN (1 << 2)
+#define STATUS_BST (1 << 3)
+#define STATUS_IMX (1 << 6)
+#define STATUS_VMN (1 << 8)
+#define STATUS_TMN (1 << 9)
+#define STATUS_SMN (1 << 10)
+#define STATUS_VMX (1 << 12)
+#define STATUS_TMX (1 << 13)
+#define STATUS_SMX (1 << 14)
+#define STATUS_ALL_ALRT \
+ (STATUS_IMN | STATUS_IMX | STATUS_VMN | STATUS_VMX | STATUS_TMN | \
+ STATUS_TMX | STATUS_SMN | STATUS_SMX)
+
+/* Alert disable values (0x01, 0x02, 0x03, 0xb4) */
+#define VALRT_DISABLE 0xff00
+#define TALRT_DISABLE 0x7f80
+#define SALRT_DISABLE 0xff00
+#define IALRT_DISABLE 0x7f80
/* Config reg (0x1d) flags */
-#define CONF_TSEL 0x8000
+#define CONF_AEN (1 << 2)
+#define CONF_IS (1 << 11)
+#define CONF_VS (1 << 12)
+#define CONF_TS (1 << 13)
+#define CONF_SS (1 << 14)
+#define CONF_TSEL (1 << 15)
+#define CONF_ALL_STICKY (CONF_IS | CONF_VS | CONF_TS | CONF_SS)
/* FStat reg (0x3d) flags */
#define FSTAT_DNR 0x0001
@@ -86,6 +116,27 @@
#define MAX17055_VEMPTY_REG(ve_mv, vr_mv) \
(((ve_mv / 10) << 7) | (vr_mv / 40))
+#define MAX17055_MAX_MIN_REG(mx, mn) ((((int16_t)(mx)) << 8) | ((mn)))
+/* Converts voltages alert range for VALRTTH_REG */
+#define MAX17055_VALRTTH_RESOLUTION 20
+#define MAX17055_VALRTTH_REG(mx, mn) \
+ MAX17055_MAX_MIN_REG((uint8_t)(mx / MAX17055_VALRTTH_RESOLUTION), \
+ (uint8_t)(mn / MAX17055_VALRTTH_RESOLUTION))
+/* Converts temperature alert range for TALRTTH_REG */
+#define MAX17055_TALRTTH_REG(mx, mn) \
+ MAX17055_MAX_MIN_REG((int8_t)(mx), (int8_t)(mn))
+/* Converts state-of-charge alert range for SALRTTH_REG */
+#define MAX17055_SALRTTH_REG(mx, mn) \
+ MAX17055_MAX_MIN_REG((uint8_t)(mx), (uint8_t)(mn))
+/* Converts current alert range for IALRTTH_REG */
+/* Current resolution: 0.4mV/RSENSE */
+#define MAX17055_IALRTTH_MUL (10 * BATTERY_MAX17055_RSENSE)
+#define MAX17055_IALRTTH_DIV 4
+#define MAX17055_IALRTTH_REG(mx, mn) \
+ MAX17055_MAX_MIN_REG( \
+ (int8_t)(mx * MAX17055_IALRTTH_MUL / MAX17055_IALRTTH_DIV), \
+ (int8_t)(mn * MAX17055_IALRTTH_MUL / MAX17055_IALRTTH_DIV))
+
/*
* max17055 needs some special battery parameters for fuel gauge
* learning algorithm. Maxim can help characterize the battery pack
@@ -127,4 +178,58 @@ struct max17055_batt_profile {
/* Return the special battery parameters max17055 needs. */
const struct max17055_batt_profile *max17055_get_batt_profile(void);
+#ifdef CONFIG_BATTERY_MAX17055_ALERT
+/*
+ * max17055 supports alert on voltage, current, state-of-charge, and
+ * temperature. To enable this feature, the information of the limit range is
+ * needed.
+ */
+struct max17055_alert_profile {
+ /*
+ * Sets voltage upper and lower limits that generate an alert if
+ * voltage is outside of the v_alert_mxmn range.
+ * The upper 8 bits set the maximum value and the lower 8 bits set the
+ * minimum value. Interrupt threshold limits are selectable with 20mV
+ * resolution.
+ * Use MAX17055_VALRTTH_REG(max, min) to setup the desired range,
+ * VALRT_DISABLE to disable the alert.
+ */
+ const uint16_t v_alert_mxmn;
+ /*
+ * Sets temperature upper and lower limits that generate an alert if
+ * temperature is outside of the t_alert_mxmn range.
+ * The upper 8 bits set the maximum value and the lower 8 bits set the
+ * minimum value. Interrupt threshold limits are stored in
+ * 2’s-complement format with 1°C resolution.
+ * Use MAX17055_TALRTTH_REG(max, min) to setup the desired range,
+ * TALRT_DISABLE to disable the alert.
+ */
+ const uint16_t t_alert_mxmn;
+ /*
+ * Sets reported state-of-charge upper and lower limits that generate
+ * an alert if SOC is outside of the s_alert_mxmn range.
+ * The upper 8 bits set the maximum value and the lower 8 bits set the
+ * minimum value. Interrupt threshold limits are configurable with 1%
+ * resolution.
+ * Use MAX17055_SALRTTH_REG(max, min) to setup the desired range,
+ * SALRT_DISABLE to disable the alert.
+ */
+ const uint16_t s_alert_mxmn;
+ /*
+ * Sets current upper and lower limits that generate an alert if
+ * current is outside of the i_alert_mxmn range.
+ * The upper 8 bits set the maximum value and the lower 8 bits set the
+ * minimum value. Interrupt threshold limits are selectable with
+ * 0.4mV/R SENSE resolution.
+ * Use MAX17055_IALRTTH_REG(max, min) to setup the desired range,
+ * IALRT_DISABLE to disable the alert.
+ */
+ const uint16_t i_alert_mxmn;
+};
+
+/*
+ * Return the battery/system's alert threshoulds that max17055 needs.
+ */
+const struct max17055_alert_profile *max17055_get_alert_profile(void);
+#endif /* CONFIG_BATTERY_MAX17055_ALERT */
#endif /* __CROS_EC_MAX17055_H */
diff --git a/include/config.h b/include/config.h
index e5eeb7d7e1..eb40d5e48c 100644
--- a/include/config.h
+++ b/include/config.h
@@ -275,6 +275,12 @@
#undef CONFIG_BATTERY_BQ4050
#undef CONFIG_BATTERY_MAX17055
+/*
+ * MAX17055 support alert on voltage, current, temperature, and state-of-charge.
+ */
+#undef CONFIG_BATTERY_MAX17055_ALERT
+
+
/* Compile mock battery support; used by tests. */
#undef CONFIG_BATTERY_MOCK