From 40a1e7fa75b9fd51cf62ee8d725a5b35d846dbd5 Mon Sep 17 00:00:00 2001 From: Randall Spangler Date: Thu, 7 Nov 2013 14:36:02 -0800 Subject: Cleanly force battery to mAh mode when reading capacity Smart batteries can report capacity in mAh or 10mW units. We forced the units to mAh in charge_state.c's main loop, but that doesn't guarantee that they're actually set before the capacity is read. It's cleaner to check the capacity reporting mode when actually reading the capacity. BUG=chrome-os-partner:20881 BRANCH=none TEST=battery command reports the same capacity data before/after change (on rambi, design=2940 mAh) Change-Id: I4a4c80eaade72bb09627d5d65693c097e264a992 Signed-off-by: Randall Spangler Reviewed-on: https://chromium-review.googlesource.com/176154 --- common/battery.c | 10 +++------- common/charge_state.c | 8 -------- driver/battery/bq27541.c | 13 ------------ driver/battery/smart.c | 52 +++++++++++++++++++++++++----------------------- include/battery.h | 38 ++++------------------------------- 5 files changed, 34 insertions(+), 87 deletions(-) diff --git a/common/battery.c b/common/battery.c index 140b7d22c7..a8ece1d796 100644 --- a/common/battery.c +++ b/common/battery.c @@ -120,7 +120,6 @@ static void print_battery_info(void) { int value; int hour, minute; - const char *unit; print_item_name("Serial:"); if (check_print_error(battery_serial_number(&value))) @@ -134,24 +133,21 @@ static void print_battery_info(void) if (check_print_error(battery_get_mode(&value))) ccprintf("0x%04x\n", value); - battery_is_in_10mw_mode(&value); - unit = value ? "0 mW" : " mAh"; - print_item_name("Abs charge:"); if (check_print_error(battery_state_of_charge_abs(&value))) ccprintf("%d %%\n", value); print_item_name("Remaining:"); if (check_print_error(battery_remaining_capacity(&value))) - ccprintf("%d%s\n", value, unit); + ccprintf("%d mAh\n", value); print_item_name("Cap-full:"); if (check_print_error(battery_full_charge_capacity(&value))) - ccprintf("%d%s\n", value, unit); + ccprintf("%d mAh\n", value); print_item_name(" Design:"); if (check_print_error(battery_design_capacity(&value))) - ccprintf("%d%s\n", value, unit); + ccprintf("%d mAh\n", value); print_item_name("Time-full:"); if (check_print_error(battery_time_to_full(&value))) { diff --git a/common/charge_state.c b/common/charge_state.c index 8f93248c05..be07113a0d 100644 --- a/common/charge_state.c +++ b/common/charge_state.c @@ -361,14 +361,6 @@ static int state_common(struct power_state_context *ctx) if (batt->desired_current > user_current_limit) batt->desired_current = user_current_limit; - if (battery_is_in_10mw_mode(&d)) { - curr->error |= F_BATTERY_MODE; - } else if (d) { - /* Battery capacity mode was set to mW; reset it back to mAh */ - if (battery_set_10mw_mode(0)) - ctx->curr.error |= F_BATTERY_MODE; - } - if (fake_state_of_charge >= 0) *ctx->memmap_batt_cap = fake_state_of_charge * diff --git a/driver/battery/bq27541.c b/driver/battery/bq27541.c index 72ec30b6fd..43b669476b 100644 --- a/driver/battery/bq27541.c +++ b/driver/battery/bq27541.c @@ -179,19 +179,6 @@ int battery_status(int *status) return EC_ERROR_UNIMPLEMENTED; } -int battery_is_in_10mw_mode(int *val) -{ - /* Always using mAh unit */ - *val = 0; - return EC_SUCCESS; -} - -int battery_set_10mw_mode(int enabled) -{ - /* Not supported by this battery chip */ - return EC_ERROR_INVAL; -} - void battery_get_params(struct batt_params *batt) { int v; diff --git a/driver/battery/smart.c b/driver/battery/smart.c index 7feca24bda..8b2bd2f8e2 100644 --- a/driver/battery/smart.c +++ b/driver/battery/smart.c @@ -37,32 +37,23 @@ int battery_get_mode(int *mode) return sb_read(SB_BATTERY_MODE, mode); } -int battery_set_mode(int mode) -{ - return sb_write(SB_BATTERY_MODE, mode); -} - -int battery_is_in_10mw_mode(int *ret) -{ - int val; - int rv = battery_get_mode(&val); - if (rv) - return rv; - *ret = val & MODE_CAPACITY; - return EC_SUCCESS; -} +/** + * Force battery to mAh mode (instead of 10mW mode) for reporting capacity. + * + * @return non-zero if error. + */ -int battery_set_10mw_mode(int enabled) +static int battery_force_mah_mode(void) { int val, rv; rv = battery_get_mode(&val); if (rv) return rv; - if (enabled) - val |= MODE_CAPACITY; - else - val &= ~MODE_CAPACITY; - return battery_set_mode(val); + + if (val & MODE_CAPACITY) + rv = sb_write(SB_BATTERY_MODE, val & ~MODE_CAPACITY); + + return rv; } int battery_state_of_charge_abs(int *percent) @@ -72,11 +63,19 @@ int battery_state_of_charge_abs(int *percent) int battery_remaining_capacity(int *capacity) { + int rv = battery_force_mah_mode(); + if (rv) + return rv; + return sb_read(SB_REMAINING_CAPACITY, capacity); } int battery_full_charge_capacity(int *capacity) { + int rv = battery_force_mah_mode(); + if (rv) + return rv; + return sb_read(SB_FULL_CHARGE_CAPACITY, capacity); } @@ -107,11 +106,12 @@ int battery_cycle_count(int *count) return sb_read(SB_CYCLE_COUNT, count); } -/* Designed battery capacity - * unit: mAh or 10mW depends on battery mode - */ int battery_design_capacity(int *capacity) { + int rv = battery_force_mah_mode(); + if (rv) + return rv; + return sb_read(SB_DESIGN_CAPACITY, capacity); } @@ -228,11 +228,13 @@ void battery_get_params(struct batt_params *batt) if (sb_read(SB_VOLTAGE, &batt->voltage)) batt->flags |= BATT_FLAG_BAD_ANY | BATT_FLAG_BAD_VOLTAGE; + /* Ensure battery current is set to 0 if unable to read it */ v = 0; + if (sb_read(SB_CURRENT, &v)) batt->flags |= BATT_FLAG_BAD_ANY; - else - batt->current = (int16_t)v; + + batt->current = (int16_t)v; if (sb_read(SB_CHARGING_VOLTAGE, &batt->desired_voltage) || sb_read(SB_CHARGING_CURRENT, &batt->desired_current)) diff --git a/include/battery.h b/include/battery.h index cb2a5c32ef..522f3aa80a 100644 --- a/include/battery.h +++ b/include/battery.h @@ -113,38 +113,11 @@ int battery_is_connected(void); * * See MODE_* constants in battery_smart.h * - * @param mode Destination for current mode. + * @param mode Destination for current mode. * @return non-zero if error. */ int battery_get_mode(int *mode); -/** - * Set battery mode. - * - * See MODE_* constants in battery_smart.h - * - * @param mode New mode. - * @return non-zero if error. - */ -int battery_set_mode(int mode); - -/** - * Check if battery is reporting capacity in 10 mW units. - * - * @param val Destination for capacity units; set zero if mAh or - * non-zero if 10 mW. - * @return non-zero if error. - */ -int battery_is_in_10mw_mode(int *val); - -/** - * Set battery capacity units. - * - * @param enabled Set mode to mAh (=0) or 10 mW (=1) - * @return non-zero if error. - */ -int battery_set_10mw_mode(int enabled); - /** * Read nominal voltage battery is designed to supply. * @@ -164,8 +137,7 @@ int battery_state_of_charge_abs(int *percent); /** * Read battery remaining capacity. * - * @param capacity Destination for capacity; units are mAh or 10 mW, - * depending on battery_is_in_10mw_mode(). + * @param capacity Destination for capacity in mAh * @return non-zero if error. */ int battery_remaining_capacity(int *capacity); @@ -173,8 +145,7 @@ int battery_remaining_capacity(int *capacity); /** * Read battery full charge capacity. * - * @param capacity Destination for capacity; units are mAh or 10 mW, - * depending on battery_is_in_10mw_mode(). + * @param capacity Destination for capacity in mAh * @return non-zero if error. */ int battery_full_charge_capacity(int *capacity); @@ -182,8 +153,7 @@ int battery_full_charge_capacity(int *capacity); /** * Read the nominal capacity the battery is designed to supply when new. * - * @param capacity Destination for capacity; units are mAh or 10 mW, - * depending on battery_is_in_10mw_mode(). + * @param capacity Destination for capacity in mAh * @return non-zero if error. */ int battery_design_capacity(int *capacity); -- cgit v1.2.1