Add host command to set battery vendor parameters

Also adds 'battparam' console command.

BUG=chrome-os-partner:25145
BRANCH=ToT
TEST=Run 'ectool batteryparam set 0 0x1234'
         'ectool batteryparam get 0'
and on the console:
         'battparam 0'
         'battparam 0 0x1234'
on a board that implements parameter 0.

Change-Id: I9cc54d001631f53dd39ae64cfdeececaa1747181
Original-Change-Id: Ib2812f57f2484309d613b23dab12ad43e0417bd2
Signed-off-by: Dave Parker <dparker@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/195824
Reviewed-by: Randall Spangler <rspangler@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/197162
This commit is contained in:
Dave Parker
2014-04-19 14:04:03 -07:00
committed by chrome-internal-fetch
parent 5aeff69cdc
commit 4e1472e303
5 changed files with 179 additions and 0 deletions

View File

@@ -299,3 +299,69 @@ int battery_is_cut_off(void)
return 0; /* Always return NOT cut off */
}
#endif /* CONFIG_BATTERY_CUT_OFF */
#ifdef CONFIG_BATTERY_VENDOR_PARAM
static int console_command_battery_vendor_param(int argc, char **argv)
{
uint32_t param;
uint32_t value;
char *e;
int rv;
if (argc < 2)
return EC_ERROR_INVAL;
param = strtoi(argv[1], &e, 0);
if (*e) {
ccputs("Invalid param\n");
return EC_ERROR_INVAL;
}
if (argc > 2) {
value = strtoi(argv[2], &e, 0);
if (*e) {
ccputs("Invalid value\n");
return EC_ERROR_INVAL;
}
rv = battery_set_vendor_param(param, value);
if (rv != EC_SUCCESS)
return rv;
}
rv = battery_get_vendor_param(param, &value);
if (rv != EC_SUCCESS)
return rv;
ccprintf("0x%08x\n", value);
return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(battparam, console_command_battery_vendor_param,
"<param> [value]",
"Get or set battery vendor parameters",
NULL);
static int host_command_battery_vendor_param(struct host_cmd_handler_args *args)
{
int rv;
const struct ec_params_battery_vendor_param *p = args->params;
struct ec_response_battery_vendor_param *r = args->response;
args->response_size = sizeof(*r);
if (p->mode != BATTERY_VENDOR_PARAM_MODE_GET &&
p->mode != BATTERY_VENDOR_PARAM_MODE_SET)
return EC_RES_INVALID_PARAM;
if (p->mode == BATTERY_VENDOR_PARAM_MODE_SET) {
rv = battery_set_vendor_param(p->param, p->value);
if (rv != EC_SUCCESS)
return rv;
}
rv = battery_get_vendor_param(p->param, &r->value);
return rv;
}
DECLARE_HOST_COMMAND(EC_CMD_BATTERY_VENDOR_PARAM,
host_command_battery_vendor_param,
EC_VER_MASK(0));
#endif /* CONFIG_BATTERY_VENDOR_PARAM */

View File

@@ -280,4 +280,26 @@ int board_cut_off_battery(void);
*/
int battery_is_cut_off(void);
/**
* Read battery vendor parameter.
*
* Vendor parameter handlers are implemented in a board-specific battery.c
*
* @param param Parameter identifier.
* @param value Location to store retrieved value.
* @return non-zero if error.
*/
int battery_get_vendor_param(uint32_t param, uint32_t *value);
/**
* Write battery vendor parameter.
*
* Vendor parameter handlers are implemented in a board-specific battery.c
*
* @param param Parameter identifier.
* @param value Value to write to the battery.
* @return non-zero if error.
*/
int battery_set_vendor_param(uint32_t param, uint32_t value);
#endif /* __CROS_EC_BATTERY_H */

View File

@@ -136,6 +136,13 @@
*/
#undef CONFIG_BATTERY_CUT_OFF
/*
* The board-specific battery.c implements get and set functions to read and
* write arbirary vendor-specific parameters stored in the battery.
* See include/battery.h for prototypes.
*/
#undef CONFIG_BATTERY_VENDOR_PARAM
/*****************************************************************************/
/*

View File

@@ -2129,6 +2129,33 @@ struct ec_params_sb_wr_block {
uint16_t data[32];
} __packed;
/*****************************************************************************/
/* Battery vendor parameters
*
* Get or set vendor-specific parameters in the battery. Implementations may
* differ between boards or batteries. On a set operation, the response
* contains the actual value set, which may be rounded or clipped from the
* requested value.
*/
#define EC_CMD_BATTERY_VENDOR_PARAM 0xb4
enum ec_battery_vendor_param_mode {
BATTERY_VENDOR_PARAM_MODE_GET = 0,
BATTERY_VENDOR_PARAM_MODE_SET,
};
struct ec_params_battery_vendor_param {
uint32_t param;
uint32_t value;
uint8_t mode;
} __packed;
struct ec_response_battery_vendor_param {
uint32_t value;
} __packed;
/*****************************************************************************/
/* System commands */

View File

@@ -36,6 +36,8 @@ const char help_str[] =
" Prints battery info\n"
" batterycutoff\n"
" Cut off battery output power\n"
" batteryparam\n"
" Read or write board-specific battery parameter\n"
" boardversion\n"
" Prints the board version\n"
" chargecurrentlimit\n"
@@ -3325,6 +3327,60 @@ int cmd_battery_cut_off(int argc, char *argv[])
return rv;
}
int cmd_battery_vendor_param(int argc, char *argv[])
{
struct ec_params_battery_vendor_param p;
struct ec_response_battery_vendor_param r;
char *e;
int rv;
if (argc < 3)
goto cmd_battery_vendor_param_usage;
if (!strcasecmp(argv[1], "get"))
p.mode = BATTERY_VENDOR_PARAM_MODE_GET;
else if (!strcasecmp(argv[1], "set"))
p.mode = BATTERY_VENDOR_PARAM_MODE_SET;
else
goto cmd_battery_vendor_param_usage;
p.param = strtol(argv[2], &e, 0);
if (e && *e) {
fprintf(stderr, "Invalid param.\n");
goto cmd_battery_vendor_param_usage;
}
if (p.mode == BATTERY_VENDOR_PARAM_MODE_SET) {
if (argc != 4) {
fprintf(stderr, "Missing value.\n");
goto cmd_battery_vendor_param_usage;
}
p.value = strtol(argv[3], &e, 0);
if (e && *e) {
fprintf(stderr, "Invalid value.\n");
goto cmd_battery_vendor_param_usage;
}
}
rv = ec_command(EC_CMD_BATTERY_VENDOR_PARAM, 0, &p, sizeof(p),
&r, sizeof(r));
if (rv < 0)
return rv;
printf("0x%08x\n", r.value);
return 0;
cmd_battery_vendor_param_usage:
fprintf(stderr,
"Usage:\t %s get <param>\n"
"\t %s set <param> <value>\n",
argv[0], argv[0]);
return -1;
}
int cmd_board_version(int argc, char *argv[])
{
struct ec_response_board_version response;
@@ -3922,6 +3978,7 @@ const struct command commands[] = {
{"backlight", cmd_lcd_backlight},
{"battery", cmd_battery},
{"batterycutoff", cmd_battery_cut_off},
{"batteryparam", cmd_battery_vendor_param},
{"boardversion", cmd_board_version},
{"chargecurrentlimit", cmd_charge_current_limit},
{"chargecontrol", cmd_charge_control},