diff options
author | Daisuke Nojiri <dnojiri@chromium.org> | 2021-05-28 10:48:14 -0700 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2021-06-25 16:36:48 +0000 |
commit | 4f2e6792dd09c24805a0926c6239909b63acf1c5 (patch) | |
tree | a27d2f37f82c7bb08ca2eaec3fb649a5035f809b /common/charge_state_v2.c | |
parent | 594cd10300325d2ef983444218843098316acbeb (diff) | |
download | chrome-ec-4f2e6792dd09c24805a0926c6239909b63acf1c5.tar.gz |
Update EC_CMD_CHARGE_CONTROL to version 2
Version 2 of EC_CMD_CHARGE_CONTROL can control battery sustainer.
It allows the host to set the upper and lower thresholds between
which the EC tries to keep the battery state of charge.
Version 2 of EC_CMD_CHARGE_CONTROL also supports 'GET' request.
It allows the host to query the current charge control settings.
localhost ~ # ectool chargecontrol
Charge mode = NORMAL (0)
Battery sustainer = off (-1% ~ -1%)
localhost ~ # ectool chargecontrol normal 66 66
Charge state machine is in normal mode with sustainer enabled.
localhost ~ # ectool chargecontrol
Charge mode = NORMAL (0)
Battery sustainer = on (66% ~ 66%)
localhost ~ # ectool chargecontrol normal
Charge state machine is in normal mode.
BUG=b:188457962
BRANCH=none
TEST=Atlas. See above.
Change-Id: I81ec62172b4f159c46334fc0f940a2adae3f2b8a
Signed-off-by: Daisuke Nojiri <dnojiri@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2929340
Reviewed-by: Vincent Palatin <vpalatin@chromium.org>
Diffstat (limited to 'common/charge_state_v2.c')
-rw-r--r-- | common/charge_state_v2.c | 63 |
1 files changed, 62 insertions, 1 deletions
diff --git a/common/charge_state_v2.c b/common/charge_state_v2.c index 67a9f5d79b..cb4f883839 100644 --- a/common/charge_state_v2.c +++ b/common/charge_state_v2.c @@ -91,6 +91,7 @@ static int manual_current; /* Manual current override (-1 = no override) */ static unsigned int user_current_limit = -1U; test_export_static timestamp_t shutdown_target_time; static timestamp_t precharge_start_time; +static struct sustain_soc sustain_soc; /* * The timestamp when the battery charging current becomes stable. @@ -203,6 +204,38 @@ static void problem(enum problem_type p, int v) problems_exist = 1; } +static int battery_sustainer_set(int8_t lower, int8_t upper) +{ + if (lower == -1 || upper == -1) { + CPRINTS("Sustain mode disabled"); + sustain_soc.lower = -1; + sustain_soc.upper = -1; + return EC_SUCCESS; + } + + if (lower <= upper && 0 <= lower && upper <= 100) { + /* Currently sustainer requires discharge_on_ac. */ + if (!IS_ENABLED(CONFIG_CHARGER_DISCHARGE_ON_AC)) + return EC_RES_UNAVAILABLE; + sustain_soc.lower = lower; + sustain_soc.upper = upper; + return EC_SUCCESS; + } + + CPRINTS("Invalid param: %s(%d, %d)", __func__, lower, upper); + return EC_ERROR_INVAL; +} + +static void battery_sustainer_disable(void) +{ + battery_sustainer_set(-1, -1); +} + +static bool battery_sustainer_enabled(void) +{ + return sustain_soc.lower != -1 && sustain_soc.upper != -1; +} + #ifdef CONFIG_EC_EC_COMM_BATTERY_CLIENT /* * Parameters for dual-battery policy. @@ -1158,6 +1191,9 @@ static void dump_charge_state(void) battery_seems_to_be_disconnected); ccprintf("battery_was_removed = %d\n", battery_was_removed); ccprintf("debug output = %s\n", debugging ? "on" : "off"); + ccprintf("Battery sustainer = %s (%d%% ~ %d%%)\n", + battery_sustainer_enabled() ? "on" : "off", + sustain_soc.lower, sustain_soc.upper); #undef DUMP } @@ -2637,8 +2673,33 @@ static enum ec_status charge_command_charge_control(struct host_cmd_handler_args *args) { const struct ec_params_charge_control *p = args->params; + struct ec_response_charge_control *r = args->response; int rv; + if (args->version >= 2) { + if (p->cmd == EC_CHARGE_CONTROL_CMD_SET) { + if (chg_ctl_mode == CHARGE_CONTROL_NORMAL) { + rv = battery_sustainer_set( + p->sustain_soc.lower, + p->sustain_soc.upper); + if (rv == EC_RES_UNAVAILABLE) + return EC_RES_UNAVAILABLE; + if (rv) + return EC_RES_INVALID_PARAM; + } else { + battery_sustainer_disable(); + } + } else if (p->cmd == EC_CHARGE_CONTROL_CMD_GET) { + r->mode = chg_ctl_mode; + r->sustain_soc.lower = sustain_soc.lower; + r->sustain_soc.upper = sustain_soc.upper; + args->response_size = sizeof(*r); + return EC_RES_SUCCESS; + } else { + return EC_RES_INVALID_PARAM; + } + } + rv = set_chg_ctrl_mode(p->mode); if (rv != EC_SUCCESS) return EC_RES_ERROR; @@ -2656,7 +2717,7 @@ charge_command_charge_control(struct host_cmd_handler_args *args) return EC_RES_SUCCESS; } DECLARE_HOST_COMMAND(EC_CMD_CHARGE_CONTROL, charge_command_charge_control, - EC_VER_MASK(1)); + EC_VER_MASK(1) | EC_VER_MASK(2)); static void reset_current_limit(void) { |