14
14
#include "chipset.h"
15
15
#include "common.h"
16
16
#include "console.h"
17
- #include "ec_ec_comm_master.h"
18
- #include "ec_ec_comm_slave.h"
17
+ #include "ec_commands.h"
18
+ #include "ec_ec_comm_client.h"
19
+ #include "ec_ec_comm_server.h"
19
20
#include "extpower.h"
20
21
#include "gpio.h"
21
22
#include "hooks.h"
@@ -1120,6 +1121,9 @@ static const char * const batt_pres[] = {
1120
1121
"NO" , "YES" , "NOT_SURE" ,
1121
1122
};
1122
1123
1124
+ const char * mode_text [] = EC_CHARGE_MODE_TEXT ;
1125
+ BUILD_ASSERT (ARRAY_SIZE (mode_text ) == CHARGE_CONTROL_COUNT );
1126
+
1123
1127
static void dump_charge_state (void )
1124
1128
{
1125
1129
#define DUMP (FLD , FMT ) ccprintf(#FLD " = " FMT "\n", curr.FLD)
@@ -1180,7 +1184,9 @@ static void dump_charge_state(void)
1180
1184
#ifdef CONFIG_EC_EC_COMM_BATTERY_MASTER
1181
1185
DUMP (input_voltage , "%dmV" );
1182
1186
#endif
1183
- ccprintf ("chg_ctl_mode = %d\n" , chg_ctl_mode );
1187
+ ccprintf ("chg_ctl_mode = %s (%d)\n" ,
1188
+ chg_ctl_mode < CHARGE_CONTROL_COUNT
1189
+ ? mode_text [chg_ctl_mode ] : "UNDEF" , chg_ctl_mode );
1184
1190
ccprintf ("manual_voltage = %d\n" , manual_voltage );
1185
1191
ccprintf ("manual_current = %d\n" , manual_current );
1186
1192
ccprintf ("user_current_limit = %dmA\n" , user_current_limit );
@@ -1667,41 +1673,47 @@ static int battery_outside_charging_temperature(void)
1667
1673
}
1668
1674
#endif
1669
1675
1670
- static void sustain_soc_disable (void )
1676
+ static void sustain_battery_soc (void )
1671
1677
{
1672
- sustain_soc . lower = -1 ;
1673
- sustain_soc . upper = -1 ;
1674
- }
1678
+ enum ec_charge_control_mode mode = chg_ctl_mode ;
1679
+ int soc ;
1680
+ int rv ;
1675
1681
1676
- static int sustain_soc_set (int16_t lower , int16_t upper )
1677
- {
1678
- if (sustain_soc .lower < sustain_soc .upper
1679
- && 0 <= sustain_soc .lower && sustain_soc .upper <= 100 ) {
1680
- sustain_soc .lower = lower ;
1681
- sustain_soc .upper = upper ;
1682
- return EC_SUCCESS ;
1683
- }
1682
+ /* If either AC or battery is not present, nothing to do. */
1683
+ if (!curr .ac || curr .batt .is_present != BP_YES
1684
+ || !battery_sustainer_enabled ())
1685
+ return ;
1684
1686
1685
- CPRINTS ("Invalid param: %s(%d, %d)" , __func__ , lower , upper );
1686
- return EC_ERROR_INVAL ;
1687
- }
1687
+ soc = charge_get_display_charge () / 10 ;
1688
1688
1689
- static bool sustain_soc_enabled (void )
1690
- {
1691
- return sustain_soc .lower != -1 && sustain_soc .upper != -1 ;
1692
- }
1689
+ switch (chg_ctl_mode ) {
1690
+ case CHARGE_CONTROL_NORMAL :
1691
+ /* Going up */
1692
+ if (sustain_soc .upper < soc )
1693
+ mode = CHARGE_CONTROL_DISCHARGE ;
1694
+ break ;
1695
+ case CHARGE_CONTROL_IDLE :
1696
+ /* discharging naturally */
1697
+ if (soc < sustain_soc .lower )
1698
+ /* TODO: Charge slowly */
1699
+ mode = CHARGE_CONTROL_NORMAL ;
1700
+ break ;
1701
+ case CHARGE_CONTROL_DISCHARGE :
1702
+ /* discharging rapidly (discharge_on_ac) */
1703
+ if (soc < sustain_soc .upper )
1704
+ mode = CHARGE_CONTROL_IDLE ;
1705
+ break ;
1706
+ default :
1707
+ return ;
1708
+ }
1693
1709
1694
- static void sustain_battery_soc (void )
1695
- {
1696
- /* If both of AC and battery aren't present, nothing to do. */
1697
- if (!curr .ac || curr .batt .is_present != BP_YES
1698
- || !sustain_soc_enabled ())
1710
+ if (mode == chg_ctl_mode )
1699
1711
return ;
1700
1712
1701
- if ( curr . batt . state_of_charge < sustain_soc . lower )
1702
- set_chg_ctrl_mode ( CHARGE_CONTROL_NORMAL );
1703
- else if ( sustain_soc . upper < curr . batt . state_of_charge )
1704
- set_chg_ctrl_mode ( CHARGE_CONTROL_DISCHARGE );
1713
+ rv = set_chg_ctrl_mode ( mode );
1714
+ CPRINTS ( "%s: %s control mode to %s" ,
1715
+ __func__ , rv == EC_SUCCESS ? "Switched" : "Failed to switch" ,
1716
+ mode_text [ mode ] );
1705
1717
}
1706
1718
1707
1719
/*****************************************************************************/
@@ -1720,7 +1732,7 @@ void charger_init(void)
1720
1732
*/
1721
1733
battery_get_params (& curr .batt );
1722
1734
1723
- sustain_soc_disable ();
1735
+ battery_sustainer_disable ();
1724
1736
}
1725
1737
DECLARE_HOOK (HOOK_INIT , charger_init , HOOK_PRIO_DEFAULT );
1726
1738
@@ -1865,7 +1877,7 @@ void charger_task(void *u)
1865
1877
}
1866
1878
} else {
1867
1879
/* Some things are only meaningful on AC */
1868
- chg_ctl_mode = CHARGE_CONTROL_NORMAL ;
1880
+ set_chg_ctrl_mode ( CHARGE_CONTROL_NORMAL ) ;
1869
1881
battery_seems_to_be_dead = 0 ;
1870
1882
prev_ac = curr .ac ;
1871
1883
}
@@ -2988,7 +3000,7 @@ static int command_chgstate(int argc, char **argv)
2988
3000
upper = strtoi (argv [3 ], & e , 0 );
2989
3001
if (* e )
2990
3002
return EC_ERROR_PARAM3 ;
2991
- rv = sustain_soc_set (lower , upper );
3003
+ rv = battery_sustainer_set (lower , upper );
2992
3004
if (rv )
2993
3005
return EC_ERROR_INVAL ;
2994
3006
} else {
@@ -3001,7 +3013,7 @@ static int command_chgstate(int argc, char **argv)
3001
3013
}
3002
3014
DECLARE_CONSOLE_COMMAND (chgstate , command_chgstate ,
3003
3015
"[idle|discharge|debug on|off]"
3004
- "\n[sustain lower upper]" ,
3016
+ "\n[sustain < lower> < upper> ]" ,
3005
3017
"Get/set charge state machine status" );
3006
3018
3007
3019
#ifdef CONFIG_EC_EC_COMM_BATTERY_MASTER
0 commit comments