@@ -1408,23 +1408,45 @@ void chgstate_set_manual_voltage(int volt_mv)
1408
1408
/* Force charging off before the battery is full. */
1409
1409
int set_chg_ctrl_mode (enum ec_charge_control_mode mode )
1410
1410
{
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
+
1411
1421
if (mode == CHARGE_CONTROL_NORMAL ) {
1412
- chg_ctl_mode = mode ;
1413
- manual_current = -1 ;
1414
- manual_voltage = -1 ;
1422
+ current = -1 ;
1423
+ voltage = -1 ;
1415
1424
} 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. */
1420
1426
if (!curr .ac )
1421
1427
return EC_ERROR_NOT_POWERED ;
1422
1428
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 ;
1426
1443
}
1427
1444
1445
+ /* Commit all atomically */
1446
+ chg_ctl_mode = mode ;
1447
+ manual_current = current ;
1448
+ manual_voltage = voltage ;
1449
+
1428
1450
return EC_SUCCESS ;
1429
1451
}
1430
1452
@@ -2739,16 +2761,6 @@ charge_command_charge_control(struct host_cmd_handler_args *args)
2739
2761
if (rv != EC_SUCCESS )
2740
2762
return EC_RES_ERROR ;
2741
2763
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
-
2752
2764
return EC_RES_SUCCESS ;
2753
2765
}
2754
2766
DECLARE_HOST_COMMAND (EC_CMD_CHARGE_CONTROL , charge_command_charge_control ,
@@ -2954,7 +2966,6 @@ static int command_chgstate(int argc, char **argv)
2954
2966
CHARGE_CONTROL_NORMAL );
2955
2967
if (rv )
2956
2968
return rv ;
2957
- #ifdef CONFIG_CHARGER_DISCHARGE_ON_AC
2958
2969
} else if (!strcasecmp (argv [1 ], "discharge" )) {
2959
2970
if (argc <= 2 )
2960
2971
return EC_ERROR_PARAM_COUNT ;
@@ -2964,14 +2975,6 @@ static int command_chgstate(int argc, char **argv)
2964
2975
CHARGE_CONTROL_NORMAL );
2965
2976
if (rv )
2966
2977
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 */
2975
2978
} else if (!strcasecmp (argv [1 ], "debug" )) {
2976
2979
if (argc <= 2 )
2977
2980
return EC_ERROR_PARAM_COUNT ;
0 commit comments