PCHG: Add host command

This patch adds a host command to get the peripheral charge port
count and status.

$ ectool pchg
1

$ ectool pchg 0
State: CHARGING (4)
Battery: 50%
Flags: 0x0

$ ectool pchg 0 foo
Invalid parameter count

  Usage1: pchg
  Usage2: pchg <port>

  Usage1 prints the number of ports.
  Usage2 prints the status of a port.

$ ectool pchg 100
Bad port index

BUG=b:173235954
BRANCH=Trogdor
TEST=Done on CoachZ. See the description above.

Signed-off-by: Daisuke Nojiri <dnojiri@chromium.org>
Change-Id: I33f261e48b16d5933b6f3ca9f3c12fec476edda3
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2555628
Reviewed-by: Vincent Palatin <vpalatin@chromium.org>
This commit is contained in:
Daisuke Nojiri 2020-11-23 15:08:55 -08:00 committed by Commit Bot
parent 9b039dc93e
commit d9b7ea8ff2
4 changed files with 151 additions and 20 deletions

View File

@ -6,6 +6,7 @@
#include "atomic.h"
#include "common.h"
#include "hooks.h"
#include "host_command.h"
#include "peripheral_charger.h"
#include "queue.h"
#include "stdbool.h"
@ -32,13 +33,7 @@ static void pchg_queue_event(struct pchg *ctx, enum pchg_event event)
static const char *_text_state(enum pchg_state state)
{
/* TODO: Use "S%d" for normal build. */
static const char * const state_names[] = {
[PCHG_STATE_RESET] = "RESET",
[PCHG_STATE_INITIALIZED] = "INITIALIZED",
[PCHG_STATE_ENABLED] = "ENABLED",
[PCHG_STATE_DETECTED] = "DETECTED",
[PCHG_STATE_CHARGING] = "CHARGING",
};
static const char * const state_names[] = EC_PCHG_STATE_TEXT;
if (state >= sizeof(state_names))
return "UNDEF";
@ -329,6 +324,39 @@ void pchg_task(void *u)
}
}
static enum ec_status hc_pchg_count(struct host_cmd_handler_args *args)
{
struct ec_response_pchg_count *r = args->response;
r->port_count = pchg_count;
args->response_size = sizeof(*r);
return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_PCHG_COUNT, hc_pchg_count, EC_VER_MASK(0));
static enum ec_status hc_pchg(struct host_cmd_handler_args *args)
{
const struct ec_params_pchg *p = args->params;
struct ec_response_pchg *r = args->response;
int port = p->port;
struct pchg *ctx;
if (port >= pchg_count)
return EC_RES_INVALID_PARAM;
ctx = &pchgs[port];
r->state = ctx->state;
r->battery_percentage = ctx->battery_percent;
r->error = ctx->error;
args->response_size = sizeof(*r);
return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_PCHG, hc_pchg, EC_VER_MASK(0));
static int cc_pchg(int argc, char **argv)
{
int port;

View File

@ -6642,6 +6642,53 @@ struct ec_response_typec_status {
uint32_t sink_cap_pdos[7]; /* Max 7 PDOs can be present */
} __ec_align1;
/**
* Get the number of peripheral charge ports
*/
#define EC_CMD_PCHG_COUNT 0x0134
#define EC_PCHG_MAX_PORTS 8
struct ec_response_pchg_count {
uint8_t port_count;
} __ec_align1;
/**
* Get the status of a peripheral charge port
*/
#define EC_CMD_PCHG 0x0135
struct ec_params_pchg {
uint8_t port;
} __ec_align1;
struct ec_response_pchg {
uint32_t error; /* enum pchg_error */
uint8_t state; /* enum pchg_state state */
uint8_t battery_percentage;
} __ec_align2;
enum pchg_state {
/* Charger is reset and not initialized. */
PCHG_STATE_RESET = 0,
/* Charger is initialized or disabled. */
PCHG_STATE_INITIALIZED,
/* Charger is enabled and ready to detect a device. */
PCHG_STATE_ENABLED,
/* Device is detected in proximity. */
PCHG_STATE_DETECTED,
/* Device is being charged. */
PCHG_STATE_CHARGING,
};
#define EC_PCHG_STATE_TEXT { \
[PCHG_STATE_RESET] = "RESET", \
[PCHG_STATE_INITIALIZED] = "INITIALIZED", \
[PCHG_STATE_ENABLED] = "ENABLED", \
[PCHG_STATE_DETECTED] = "DETECTED", \
[PCHG_STATE_CHARGING] = "CHARGING", \
}
/*****************************************************************************/
/* The command range 0x200-0x2FF is reserved for Rotor. */

View File

@ -90,19 +90,6 @@ enum pchg_event {
PCHG_EVENT_DISABLE,
};
enum pchg_state {
/* Charger is reset and not initialized. */
PCHG_STATE_RESET = 0,
/* Charger is initialized or disabled. */
PCHG_STATE_INITIALIZED,
/* Charger is enabled and ready to detect a device. */
PCHG_STATE_ENABLED,
/* Device is detected in proximity. */
PCHG_STATE_DETECTED,
/* Device is being charged. */
PCHG_STATE_CHARGING,
};
enum pchg_error {
PCHG_ERROR_NONE = 0,
/* Error initiated by host. */

View File

@ -210,6 +210,8 @@ const char help_str[] =
" Prints saved panic info\n"
" pause_in_s5 [on|off]\n"
" Whether or not the AP should pause in S5 on shutdown\n"
" pchg [<port>]\n"
" Get peripheral charge port count and status\n"
" pdcontrol [suspend|resume|reset|disable|on]\n"
" Controls the PD chip\n"
" pdchipinfo <port>\n"
@ -9328,6 +9330,72 @@ int cmd_charge_port_override(int argc, char *argv[])
return 0;
}
static void cmd_pchg_help(char *cmd)
{
fprintf(stderr,
" Usage1: %s\n"
" Usage2: %s <port>\n"
"\n"
" Usage1 prints the number of ports.\n"
" Usage2 prints the status of a port.\n",
cmd, cmd);
}
int cmd_pchg(int argc, char *argv[])
{
int port, port_count;
char *e;
int rv;
struct ec_response_pchg_count *rsp_count = ec_inbuf;
static const char * const pchg_state_text[] = EC_PCHG_STATE_TEXT;
rv = ec_command(EC_CMD_PCHG_COUNT, 0, NULL, 0, ec_inbuf, ec_max_insize);
if (rv < 0) {
fprintf(stderr, "Failed to get port count: %d\n", rv);
return rv;
}
port_count = rsp_count->port_count;
if (argc == 1) {
/* Usage1 */
printf("%d\n", port_count);
return 0;
}
port = strtol(argv[1], &e, 0);
if ((e && *e) || port >= port_count) {
fprintf(stderr, "Bad port index\n");
return -1;
}
if (argc < 3) {
/* Usage2 */
struct ec_params_pchg *p = ec_outbuf;
struct ec_response_pchg *r = ec_inbuf;
p->port = port;
rv = ec_command(EC_CMD_PCHG, 0, ec_outbuf, sizeof(*p),
ec_inbuf, ec_max_insize);
if (rv < 0) {
fprintf(stderr, "Error code: %d\n", rv);
return rv;
}
printf("State: %s (%d)\n",
r->state < sizeof(pchg_state_text) ?
pchg_state_text[r->state] : "UNDEF",
r->state);
printf("Battery: %d%%\n", r->battery_percentage);
printf("Flags: 0x%x\n", r->error);
return 0;
}
fprintf(stderr, "Invalid parameter count\n\n");
cmd_pchg_help(argv[0]);
return -1;
}
int cmd_pd_log(int argc, char *argv[])
{
union {
@ -10296,6 +10364,7 @@ const struct command commands[] = {
{"nextevent", cmd_next_event},
{"panicinfo", cmd_panic_info},
{"pause_in_s5", cmd_s5},
{"pchg", cmd_pchg},
{"pdgetmode", cmd_pd_get_amode},
{"pdsetmode", cmd_pd_set_amode},
{"port80read", cmd_port80_read},