diff options
author | Dave Parker <dparker@google.com> | 2014-06-12 20:31:26 -0700 |
---|---|---|
committer | chrome-internal-fetch <chrome-internal-fetch@google.com> | 2014-06-15 01:45:21 +0000 |
commit | efd3a8925eecce4559d8c81175258d5974140db6 (patch) | |
tree | 091fee596c1c03a2cd3ea248e3f5cc199557240a /common/battery.c | |
parent | a0119238c7e398135c2b8e4fbb06ba81d4a6a494 (diff) | |
download | chrome-ec-efd3a8925eecce4559d8c81175258d5974140db6.tar.gz |
Add 'at-shutdown' option to batterycutoff host command
If at-shutdown is specified, the battery is cut off
1 seconds after the host has shutdown.
BUG=chrome-os-partner:29292,chrome-os-partner:28887
BRANCH=tot,nyan
TEST=Run batterycutoff ectool command and cutoff console
command with and without 'at-shutdown' option. Verify
the battery is cut off immediately without the option
specified and 1 seconds after shutdown with. View the
console log to see the deferred cutoff occur.
The following tests are verified on big.
console:
cutoff, AC on: system is off after removing AC.
cutoff, AC off: system is off immediately.
at-shutdown, AC on: system is off after "power off" and removing AC.
at-shutdown, AC off: system is off after "power off".
ectool:
batterycutoff, AC on: system is off after removing AC.
batterycutoff, AC off: system is off immediately.
at-shutdown, AC on: battery is cut off after 1s of shutdown.
system is off right after removing AC power.
at-shutdown, AC off: system is off after 1s of shutdown.
[84.058416 power state 3 = S0, in 0x0000]
[84.058803 power lost input; wanted 0x0001, got 0x0000]
[84.059120 power off 3]
[84.072148 Cutting off battery in 1 second(s)]
[84.123896 power shutdown complete]
[84.128790 power state 7 = S0->S3, in 0x0002]
[84.139694 power state 2 = S3, in 0x0002]
[84.150857 power state 8 = S3->S5, in 0x0002]
[84.166975 power state 1 = S5, in 0x0002]
[84.177972 power state 1 = S5, in 0x0002]
[85.080012 Battery cut off succeeded.]
Change-Id: Id4bacf79ad3add885260655f80cb8127bafe1ad6
Signed-off-by: Dave Parker <dparker@google.com>
Signed-off-by: Louis Yung-Chieh Lo <yjlou@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/203694
Reviewed-by: Vic Yang <victoryang@chromium.org>
Diffstat (limited to 'common/battery.c')
-rw-r--r-- | common/battery.c | 98 |
1 files changed, 81 insertions, 17 deletions
diff --git a/common/battery.c b/common/battery.c index 323f3d2790..408ae92c46 100644 --- a/common/battery.c +++ b/common/battery.c @@ -17,8 +17,18 @@ #include "util.h" #include "watchdog.h" +#define CPRINTF(format, args...) cprintf(CC_CHARGER, format, ## args) +#define CPRINTS(format, args...) cprints(CC_CHARGER, format, ## args) + #ifdef CONFIG_BATTERY_CUT_OFF -static int is_cut_off; + +#ifndef CONFIG_BATTERY_CUTOFF_DELAY_US +#define CONFIG_BATTERY_CUTOFF_DELAY_US (1 * SECOND) +#endif + +static enum battery_cutoff_states battery_cutoff_state = + BATTERY_CUTOFF_STATE_NORMAL; + #endif #ifdef CONFIG_BATTERY_PRESENT_GPIO @@ -261,41 +271,95 @@ DECLARE_CONSOLE_COMMAND(battery, command_battery, "Print battery info", NULL); - #ifdef CONFIG_BATTERY_CUT_OFF int battery_is_cut_off(void) { - return is_cut_off; + return (battery_cutoff_state == BATTERY_CUTOFF_STATE_CUT_OFF); } -static void clean_cut_off(void) +static void pending_cutoff_deferred(void) { - if (extpower_is_present()) - is_cut_off = 0; + int rv; + + rv = board_cut_off_battery(); + + if (rv == EC_SUCCESS) + CPRINTF("[%T Battery cut off succeeded.]\n"); + else + CPRINTF("[%T Battery cut off failed!]\n"); +} +DECLARE_DEFERRED(pending_cutoff_deferred); + +static void clear_pending_cutoff(void) +{ + if (extpower_is_present()) { + battery_cutoff_state = BATTERY_CUTOFF_STATE_NORMAL; + hook_call_deferred(pending_cutoff_deferred, -1); + } } -DECLARE_HOOK(HOOK_AC_CHANGE, clean_cut_off, HOOK_PRIO_DEFAULT); +DECLARE_HOOK(HOOK_AC_CHANGE, clear_pending_cutoff, HOOK_PRIO_DEFAULT); -static int battery_command_cut_off(struct host_cmd_handler_args *args) +static int battery_command_cutoff(struct host_cmd_handler_args *args) { - int rv = board_cut_off_battery(); - if (!rv) - is_cut_off = 1; + const struct ec_params_battery_cutoff *p; + int rv; + + if (args->version == 1) { + p = args->params; + if (p->flags & EC_BATTERY_CUTOFF_FLAG_AT_SHUTDOWN) { + battery_cutoff_state = BATTERY_CUTOFF_STATE_PENDING; + CPRINTS("Battery cut off at-shutdown is scheduled"); + return EC_RES_SUCCESS; + } + } + + rv = board_cut_off_battery(); + if (!rv) { + CPRINTS("Battery cut off is successful."); + battery_cutoff_state = BATTERY_CUTOFF_STATE_CUT_OFF; + } else { + CPRINTS("Battery cut off has failed."); + } return rv; } -DECLARE_HOST_COMMAND(EC_CMD_BATTERY_CUT_OFF, battery_command_cut_off, - EC_VER_MASK(0)); +DECLARE_HOST_COMMAND(EC_CMD_BATTERY_CUT_OFF, battery_command_cutoff, + EC_VER_MASK(0) | EC_VER_MASK(1)); + +static void check_pending_cutoff(void) +{ + if (battery_cutoff_state == BATTERY_CUTOFF_STATE_PENDING) { + CPRINTF("[%T Cutting off battery in %d second(s)]\n", + CONFIG_BATTERY_CUTOFF_DELAY_US / SECOND); + hook_call_deferred(pending_cutoff_deferred, + CONFIG_BATTERY_CUTOFF_DELAY_US); + } +} +DECLARE_HOOK(HOOK_CHIPSET_SHUTDOWN, check_pending_cutoff, HOOK_PRIO_LAST); static int command_cutoff(int argc, char **argv) { - int rv = board_cut_off_battery(); - if (!rv) - is_cut_off = 1; + int rv; + + if (argc > 1) { + if (!strcasecmp(argv[1], "at-shutdown")) { + battery_cutoff_state = BATTERY_CUTOFF_STATE_PENDING; + return EC_SUCCESS; + } else { + return EC_ERROR_INVAL; + } + } + + rv = board_cut_off_battery(); + if (!rv) { + ccprintf("[%T Battery cut off]\n"); + battery_cutoff_state = BATTERY_CUTOFF_STATE_CUT_OFF; + } return rv; } DECLARE_CONSOLE_COMMAND(cutoff, command_cutoff, - "", + "[at-shutdown]", "Cut off the battery output", NULL); #else |