From 27bbb0a2d895edb466bb7d5bbb6e1c3a2c18982e Mon Sep 17 00:00:00 2001 From: Louis Yung-Chieh Lo Date: Mon, 27 Aug 2012 22:14:09 +0800 Subject: Support battery cut-off mechanism for factory (R22). The cut-off command is manufacturer-specific. Thus the logic is implemented in gas gauge IC code. For those boards using this gas gauge, define the CONFIG_BATTERY_BQ20Z453 in board.h. Cherry-pick to R22 release image, mainly for ectool.c. BUG=chrome-os-partner:12962, BRANCH=snow Signed-off-by: Louis Yung-Chieh Lo TEST=Tested on snow ectool batterycutoff ; expect system is off immediately ; if AC power is not connected. Original-Change-Id: Idd290c76439f3263c1c812b236b79623878f73b2 (cherry picked from commit 84babb0520e44ea01100b91abfe0b1bd503fe88e) Change-Id: I626bba25e889269d0216991468676331c3aa726d Reviewed-on: https://gerrit.chromium.org/gerrit/31607 Reviewed-by: Randall Spangler Reviewed-by: Yung-Chieh Lo Tested-by: Yung-Chieh Lo --- board/snow/board.h | 3 +++ common/battery_bq20z453.c | 38 ++++++++++++++++++++++++++++++++++++++ common/build.mk | 1 + include/ec_commands.h | 8 ++++++++ util/ectool.c | 27 +++++++++++++++++++++++++++ 5 files changed, 77 insertions(+) create mode 100644 common/battery_bq20z453.c diff --git a/board/snow/board.h b/board/snow/board.h index f84917a814..b055fcc9ca 100644 --- a/board/snow/board.h +++ b/board/snow/board.h @@ -52,6 +52,9 @@ #define CONFIG_CMD_PMU +/* Battery */ +#define CONFIG_BATTERY_BQ20Z453 + /* GPIO signal list */ enum gpio_signal { /* Inputs with interrupt handlers are first for efficiency */ diff --git a/common/battery_bq20z453.c b/common/battery_bq20z453.c new file mode 100644 index 0000000000..f3ac7dd4d4 --- /dev/null +++ b/common/battery_bq20z453.c @@ -0,0 +1,38 @@ +/* Copyright (c) 2012 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. + * + * Smart battery driver for BQ20Z453. + */ + +#include "host_command.h" +#include "smart_battery.h" +#include "timer.h" + +#define PARAM_CUT_OFF 0x0010 + +int battery_command_cut_off(struct host_cmd_handler_args *args) +{ + /* + * TODO: Since this is a host command, the i2c bus is claimed by host. + * Thus, we would send back the response in advanced so that + * the host can release the bus and then EC can send command to + * battery. + * + * Refactoring this via task is a way. However, it is wasteful. + * Need a light-weight solution. + */ + args->result = EC_RES_SUCCESS; + args->send_response(args); + + /* This function would try to claim i2c and then send to battery. */ + sb_write(SB_MANUFACTURER_ACCESS, PARAM_CUT_OFF); + + return EC_RES_SUCCESS; + /* + * Not sure if there is a side-effect since this could send result + * back to host TWICE. + */ +} +DECLARE_HOST_COMMAND(EC_CMD_BATTERY_CUT_OFF, battery_command_cut_off, + EC_VER_MASK(0)); diff --git a/common/build.mk b/common/build.mk index 10dbb23d8c..fdc65185da 100644 --- a/common/build.mk +++ b/common/build.mk @@ -9,6 +9,7 @@ common-y=main.o util.o console_output.o uart_buffering.o common-y+=memory_commands.o shared_mem.o system_common.o hooks.o common-y+=gpio_commands.o version.o printf.o queue.o +common-$(CONFIG_BATTERY_BQ20Z453)+=battery_bq20z453.o common-$(CONFIG_BATTERY_LINK)+=battery_link.o common-$(CONFIG_CHARGER_BQ24725)+=charger_bq24725.o common-$(CONFIG_PMU_TPS65090)+=pmu_tps65090.o pmu_tps65090_charger.o diff --git a/include/ec_commands.h b/include/ec_commands.h index c94fed5c96..a3661bbd70 100644 --- a/include/ec_commands.h +++ b/include/ec_commands.h @@ -916,6 +916,14 @@ struct ec_params_force_idle { uint8_t enabled; } __packed; +/* + * Cut off battery power output if the battery supports. + * + * For unsupported battery, just don't implement this command and lets EC + * return EC_RES_INVALID_COMMAND. + */ +#define EC_CMD_BATTERY_CUT_OFF 0x99 + /*****************************************************************************/ /* System commands */ diff --git a/util/ectool.c b/util/ectool.c index 3acedb0d5c..a5249930e3 100644 --- a/util/ectool.c +++ b/util/ectool.c @@ -33,6 +33,8 @@ const char help_str[] = " Enable/disable LCD backlight\n" " battery\n" " Prints battery info\n" + " batterycutoff\n" + " Cut off battery output power\n" " chargeforceidle\n" " Force charge state machine to stop in idle mode\n" " chipinfo\n" @@ -1949,6 +1951,30 @@ cmd_error: return -1; } +int cmd_battery_cut_off(int argc, char *argv[]) +{ + int rv; + + rv = ec_command(EC_CMD_BATTERY_CUT_OFF, 0, NULL, 0, NULL, 0); + rv = (rv < 0 ? rv : 0); + + if (rv < 0) { + fprintf(stderr, "Failed to cut off battery, rv=%d\n", rv); + fprintf(stderr, "It is expected if the rv is -%d " + "(EC_RES_INVALID_COMMAND) if the battery " + "doesn't support cut-off function.\n", + EC_RES_INVALID_COMMAND); + } else { + printf("\n"); + printf("SUCCESS. The battery has arranged a cut-off and\n"); + printf("the system should be shutdown immediately.\n"); + printf("\n"); + printf("If the system is still alive, you could remove\n"); + printf("the AC power and try again.\n"); + } + return rv; +} + int cmd_chipinfo(int argc, char *argv[]) { struct ec_response_get_chip_info info; @@ -2100,6 +2126,7 @@ const struct command commands[] = { {"autofanctrl", cmd_thermal_auto_fan_ctrl}, {"backlight", cmd_lcd_backlight}, {"battery", cmd_battery}, + {"batterycutoff", cmd_battery_cut_off}, {"chargeforceidle", cmd_charge_force_idle}, {"chipinfo", cmd_chipinfo}, {"cmdversions", cmd_cmdversions}, -- cgit v1.2.1