diff options
author | Daisuke Nojiri <dnojiri@chromium.org> | 2020-11-23 15:08:55 -0800 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2020-12-10 03:16:46 +0000 |
commit | d9b7ea8ff23a744e7a5e0f28fe7ca0e36ef11a9d (patch) | |
tree | 0ae315575f0ab2e6804b9e9e494d7e50cb1739fb | |
parent | 9b039dc93e156e720a6916cdc804b4e4575bb0c5 (diff) | |
download | chrome-ec-d9b7ea8ff23a744e7a5e0f28fe7ca0e36ef11a9d.tar.gz |
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>
-rw-r--r-- | common/peripheral_charger.c | 42 | ||||
-rw-r--r-- | include/ec_commands.h | 47 | ||||
-rw-r--r-- | include/peripheral_charger.h | 13 | ||||
-rw-r--r-- | util/ectool.c | 69 |
4 files changed, 151 insertions, 20 deletions
diff --git a/common/peripheral_charger.c b/common/peripheral_charger.c index dee6203aa5..2faa315253 100644 --- a/common/peripheral_charger.c +++ b/common/peripheral_charger.c @@ -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; diff --git a/include/ec_commands.h b/include/ec_commands.h index 2fd55c0216..7b3e6de6d4 100644 --- a/include/ec_commands.h +++ b/include/ec_commands.h @@ -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. */ diff --git a/include/peripheral_charger.h b/include/peripheral_charger.h index 99fd4e3bb0..1f7b3236f7 100644 --- a/include/peripheral_charger.h +++ b/include/peripheral_charger.h @@ -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. */ diff --git a/util/ectool.c b/util/ectool.c index 8c6fbdfb61..7c1ed74f67 100644 --- a/util/ectool.c +++ b/util/ectool.c @@ -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}, |