diff options
-rw-r--r-- | common/charge_state.c | 74 | ||||
-rw-r--r-- | include/charge_state.h | 7 |
2 files changed, 60 insertions, 21 deletions
diff --git a/common/charge_state.c b/common/charge_state.c index ddf11f6c7e..da5f19c6ed 100644 --- a/common/charge_state.c +++ b/common/charge_state.c @@ -41,6 +41,8 @@ static int state_machine_force_idle = 0; static unsigned user_current_limit = -1U; +static int fake_state_of_charge = -1; + /* Current power state context */ static struct power_state_context task_ctx; @@ -217,16 +219,15 @@ static int state_common(struct power_state_context *ctx) *ctx->memmap_batt_rate = batt->current < 0 ? -batt->current : batt->current; - rv = battery_desired_voltage(&batt->desired_voltage); - if (rv) + if (battery_desired_voltage(&batt->desired_voltage)) curr->error |= F_DESIRED_VOLTAGE; - rv = battery_desired_current(&batt->desired_current); - if (rv) + if (battery_desired_current(&batt->desired_current)) curr->error |= F_DESIRED_CURRENT; - rv = battery_state_of_charge(&batt->state_of_charge); - if (rv) + if (fake_state_of_charge >= 0) + batt->state_of_charge = fake_state_of_charge; + else if (battery_state_of_charge(&batt->state_of_charge)) curr->error |= F_BATTERY_STATE_OF_CHARGE; if (batt->state_of_charge != prev->batt.state_of_charge) { @@ -281,22 +282,21 @@ static int state_common(struct power_state_context *ctx) if (batt->desired_current > user_current_limit) batt->desired_current = user_current_limit; - rv = battery_get_battery_mode(&d); - if (rv) { + if (battery_get_battery_mode(&d)) { curr->error |= F_BATTERY_MODE; - } else { - if (d & MODE_CAPACITY) { - /* Battery capacity mode was set to mW - * reset it back to mAh - */ - d &= ~MODE_CAPACITY; - rv = battery_set_battery_mode(d); - if (rv) - ctx->curr.error |= F_BATTERY_MODE; - } + } else if (d & MODE_CAPACITY) { + /* Battery capacity mode was set to mW reset it back to mAh */ + d &= ~MODE_CAPACITY; + rv = battery_set_battery_mode(d); + if (rv) + ctx->curr.error |= F_BATTERY_MODE; } - rv = battery_remaining_capacity(&d); - if (rv) + + if (fake_state_of_charge >= 0) + *ctx->memmap_batt_cap = + fake_state_of_charge * + *(int *)host_get_memmap(EC_MEMMAP_BATT_LFCC) / 100; + else if (battery_remaining_capacity(&d)) ctx->curr.error |= F_BATTERY_CAPACITY; else *ctx->memmap_batt_cap = d; @@ -568,6 +568,12 @@ int charge_get_percent(void) return task_ctx.curr.batt.state_of_charge; } +int charge_want_shutdown(void) +{ + return (charge_get_state() == PWR_STATE_DISCHARGE) && + charge_get_percent() < BATTERY_LEVEL_SHUTDOWN; +} + static int enter_force_idle_mode(void) { if (!power_ac_present()) @@ -860,3 +866,31 @@ static int charge_command_current_limit(struct host_cmd_handler_args *args) } DECLARE_HOST_COMMAND(EC_CMD_CHARGE_CURRENT_LIMIT, charge_command_current_limit, EC_VER_MASK(0)); + +/*****************************************************************************/ +/* Console commands */ + +static int command_battfake(int argc, char **argv) +{ + char *e; + int v; + + if (argc == 2) { + v = strtoi(argv[1], &e, 0); + if (*e || v < -1 || v > 100) + return EC_ERROR_PARAM1; + + fake_state_of_charge = v; + } + + if (fake_state_of_charge < 0) + ccprintf("Reporting real battery level\n"); + else + ccprintf("Reporting fake battery level %d%%\n", + fake_state_of_charge); + return EC_SUCCESS; +} +DECLARE_CONSOLE_COMMAND(battfake, command_battfake, + "percent (-1 = use real level)", + "Set fake battery level", + NULL); diff --git a/include/charge_state.h b/include/charge_state.h index eda78abfd9..455426f1ed 100644 --- a/include/charge_state.h +++ b/include/charge_state.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved. +/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved. * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. * @@ -134,5 +134,10 @@ enum power_state charge_get_state(void); */ int charge_get_percent(void); +/** + * Return non-zero if discharging and battery so low we should shut down. + */ +int charge_want_shutdown(void); + #endif /* __CROS_EC_CHARGE_STATE_H */ |