diff options
-rw-r--r-- | common/charge_state_v2.c | 65 | ||||
-rw-r--r-- | include/ec_commands.h | 2 |
2 files changed, 38 insertions, 29 deletions
diff --git a/common/charge_state_v2.c b/common/charge_state_v2.c index 602bf3b25e..8bff5a19a0 100644 --- a/common/charge_state_v2.c +++ b/common/charge_state_v2.c @@ -23,6 +23,7 @@ #include "i2c.h" #include "math_util.h" #include "printf.h" +#include "stdbool.h" #include "system.h" #include "task.h" #include "throttle_ap.h" @@ -1243,23 +1244,48 @@ void chgstate_set_manual_voltage(int volt_mv) /* Force charging off before the battery is full. */ static int set_chg_ctrl_mode(enum ec_charge_control_mode mode) { + bool discharge_on_ac = false; + int current, voltage; + int rv; + + current = manual_current; + voltage = manual_voltage; + + if (mode >= CHARGE_CONTROL_COUNT) + return EC_ERROR_INVAL; + if (mode == CHARGE_CONTROL_NORMAL) { - chg_ctl_mode = mode; - manual_current = -1; - manual_voltage = -1; + current = -1; + voltage = -1; } else { - /* - * Changing mode is only meaningful if external power is - * present. If it's not present we can't charge anyway. - */ + /* Changing mode is only meaningful if AC is present. */ if (!curr.ac) return EC_ERROR_NOT_POWERED; - chg_ctl_mode = mode; - manual_current = 0; - manual_voltage = 0; + if (mode == CHARGE_CONTROL_DISCHARGE) { + if (!IS_ENABLED(CONFIG_CHARGER_DISCHARGE_ON_AC)) + return EC_ERROR_UNIMPLEMENTED; + discharge_on_ac = true; + } else if (mode == CHARGE_CONTROL_IDLE) { + current = 0; + voltage = 0; + } + } + + if (IS_ENABLED(CONFIG_CHARGER_DISCHARGE_ON_AC)) { + if (IS_ENABLED(CONFIG_CHARGER_DISCHARGE_ON_AC_CUSTOM)) + rv = board_discharge_on_ac(discharge_on_ac); + else + rv = charger_discharge_on_ac(discharge_on_ac); + if (rv != EC_SUCCESS) + return rv; } + /* Commit all atomically */ + chg_ctl_mode = mode; + manual_current = current; + manual_voltage = voltage; + return EC_SUCCESS; } @@ -2221,16 +2247,6 @@ static int charge_command_charge_control(struct host_cmd_handler_args *args) if (rv != EC_SUCCESS) return EC_RES_ERROR; -#ifdef CONFIG_CHARGER_DISCHARGE_ON_AC -#ifdef CONFIG_CHARGER_DISCHARGE_ON_AC_CUSTOM - rv = board_discharge_on_ac(p->mode == CHARGE_CONTROL_DISCHARGE); -#else - rv = charger_discharge_on_ac(p->mode == CHARGE_CONTROL_DISCHARGE); -#endif - if (rv != EC_SUCCESS) - return EC_RES_ERROR; -#endif - return EC_RES_SUCCESS; } DECLARE_HOST_COMMAND(EC_CMD_CHARGE_CONTROL, charge_command_charge_control, @@ -2429,7 +2445,6 @@ static int command_chgstate(int argc, char **argv) CHARGE_CONTROL_NORMAL); if (rv) return rv; -#ifdef CONFIG_CHARGER_DISCHARGE_ON_AC } else if (!strcasecmp(argv[1], "discharge")) { if (argc <= 2) return EC_ERROR_PARAM_COUNT; @@ -2439,14 +2454,6 @@ static int command_chgstate(int argc, char **argv) CHARGE_CONTROL_NORMAL); if (rv) return rv; -#ifdef CONFIG_CHARGER_DISCHARGE_ON_AC_CUSTOM - rv = board_discharge_on_ac(val); -#else - rv = charger_discharge_on_ac(val); -#endif /* CONFIG_CHARGER_DISCHARGE_ON_AC_CUSTOM */ - if (rv) - return rv; -#endif /* CONFIG_CHARGER_DISCHARGE_ON_AC */ } else if (!strcasecmp(argv[1], "debug")) { if (argc <= 2) return EC_ERROR_PARAM_COUNT; diff --git a/include/ec_commands.h b/include/ec_commands.h index 9650701695..7a7e49eccb 100644 --- a/include/ec_commands.h +++ b/include/ec_commands.h @@ -3837,6 +3837,8 @@ enum ec_charge_control_mode { CHARGE_CONTROL_NORMAL = 0, CHARGE_CONTROL_IDLE, CHARGE_CONTROL_DISCHARGE, + /* Add no more entry below. */ + CHARGE_CONTROL_COUNT, }; struct ec_params_charge_control { |