Skip to content

Commit b8d7b29

Browse files
dnojirikiram9
authored andcommitted
chgstv2: Refactor charger_discharge_on_ac
This patch makes charger_discharge_on_ac call board_discharge_on_ac. It also makes set_chg_ctrl_mode call charger_discharge_on_ac. This makes sense since when the charge control mode changes, discharge-on-ac also needs to be enabled or disabled. BUG=b:188457962 BRANCH=none TEST=make runhosttests Change-Id: I65ec09f580afc987cc86f4c60c15c1f90ead6c3c Signed-off-by: Daisuke Nojiri <[email protected]> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2986848
1 parent f73c258 commit b8d7b29

File tree

3 files changed

+36
-30
lines changed

3 files changed

+36
-30
lines changed

common/charge_state_v2.c

+32-29
Original file line numberDiff line numberDiff line change
@@ -1408,23 +1408,45 @@ void chgstate_set_manual_voltage(int volt_mv)
14081408
/* Force charging off before the battery is full. */
14091409
int set_chg_ctrl_mode(enum ec_charge_control_mode mode)
14101410
{
1411+
bool discharge_on_ac = false;
1412+
int current, voltage;
1413+
int rv;
1414+
1415+
current = manual_current;
1416+
voltage = manual_voltage;
1417+
1418+
if (mode >= CHARGE_CONTROL_COUNT)
1419+
return EC_ERROR_INVAL;
1420+
14111421
if (mode == CHARGE_CONTROL_NORMAL) {
1412-
chg_ctl_mode = mode;
1413-
manual_current = -1;
1414-
manual_voltage = -1;
1422+
current = -1;
1423+
voltage = -1;
14151424
} else {
1416-
/*
1417-
* Changing mode is only meaningful if external power is
1418-
* present. If it's not present we can't charge anyway.
1419-
*/
1425+
/* Changing mode is only meaningful if AC is present. */
14201426
if (!curr.ac)
14211427
return EC_ERROR_NOT_POWERED;
14221428

1423-
chg_ctl_mode = mode;
1424-
manual_current = 0;
1425-
manual_voltage = 0;
1429+
if (mode == CHARGE_CONTROL_DISCHARGE) {
1430+
if (!IS_ENABLED(CONFIG_CHARGER_DISCHARGE_ON_AC))
1431+
return EC_ERROR_UNIMPLEMENTED;
1432+
discharge_on_ac = true;
1433+
} else if (mode == CHARGE_CONTROL_IDLE) {
1434+
current = 0;
1435+
voltage = 0;
1436+
}
1437+
}
1438+
1439+
if (IS_ENABLED(CONFIG_CHARGER_DISCHARGE_ON_AC)) {
1440+
rv = charger_discharge_on_ac(discharge_on_ac);
1441+
if (rv != EC_SUCCESS)
1442+
return rv;
14261443
}
14271444

1445+
/* Commit all atomically */
1446+
chg_ctl_mode = mode;
1447+
manual_current = current;
1448+
manual_voltage = voltage;
1449+
14281450
return EC_SUCCESS;
14291451
}
14301452

@@ -2739,16 +2761,6 @@ charge_command_charge_control(struct host_cmd_handler_args *args)
27392761
if (rv != EC_SUCCESS)
27402762
return EC_RES_ERROR;
27412763

2742-
#ifdef CONFIG_CHARGER_DISCHARGE_ON_AC
2743-
#ifdef CONFIG_CHARGER_DISCHARGE_ON_AC_CUSTOM
2744-
rv = board_discharge_on_ac(p->mode == CHARGE_CONTROL_DISCHARGE);
2745-
#else
2746-
rv = charger_discharge_on_ac(p->mode == CHARGE_CONTROL_DISCHARGE);
2747-
#endif
2748-
if (rv != EC_SUCCESS)
2749-
return EC_RES_ERROR;
2750-
#endif
2751-
27522764
return EC_RES_SUCCESS;
27532765
}
27542766
DECLARE_HOST_COMMAND(EC_CMD_CHARGE_CONTROL, charge_command_charge_control,
@@ -2954,7 +2966,6 @@ static int command_chgstate(int argc, char **argv)
29542966
CHARGE_CONTROL_NORMAL);
29552967
if (rv)
29562968
return rv;
2957-
#ifdef CONFIG_CHARGER_DISCHARGE_ON_AC
29582969
} else if (!strcasecmp(argv[1], "discharge")) {
29592970
if (argc <= 2)
29602971
return EC_ERROR_PARAM_COUNT;
@@ -2964,14 +2975,6 @@ static int command_chgstate(int argc, char **argv)
29642975
CHARGE_CONTROL_NORMAL);
29652976
if (rv)
29662977
return rv;
2967-
#ifdef CONFIG_CHARGER_DISCHARGE_ON_AC_CUSTOM
2968-
rv = board_discharge_on_ac(val);
2969-
#else
2970-
rv = charger_discharge_on_ac(val);
2971-
#endif /* CONFIG_CHARGER_DISCHARGE_ON_AC_CUSTOM */
2972-
if (rv)
2973-
return rv;
2974-
#endif /* CONFIG_CHARGER_DISCHARGE_ON_AC */
29752978
} else if (!strcasecmp(argv[1], "debug")) {
29762979
if (argc <= 2)
29772980
return EC_ERROR_PARAM_COUNT;

common/charger.c

+3
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,9 @@ enum ec_error_list charger_discharge_on_ac(int enable)
441441
int chgnum;
442442
int rv = EC_ERROR_UNIMPLEMENTED;
443443

444+
if (IS_ENABLED(CONFIG_CHARGER_DISCHARGE_ON_AC_CUSTOM))
445+
return board_discharge_on_ac(enable);
446+
444447
/*
445448
* When discharge on AC is selected, cycle through all chargers to
446449
* enable or disable this feature.

include/ec_commands.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -4174,7 +4174,7 @@ enum ec_charge_control_mode {
41744174
CHARGE_CONTROL_IDLE,
41754175
CHARGE_CONTROL_DISCHARGE,
41764176
/* Add no more entry below. */
4177-
CHARGE_CONTROL_COUNT,
4177+
CHARGE_CONTROL_COUNT,
41784178
};
41794179

41804180
#define EC_CHARGE_MODE_TEXT { \

0 commit comments

Comments
 (0)