2121#define CPRINTS (format , args ...) cprints(CC_USBCHARGE, format, ## args)
2222#define CPRINTF (format , args ...) cprintf(CC_USBCHARGE, format, ## args)
2323
24- #define POWER_LIMIT_1_W 50
25-
26- static int pl1_watt ;
27- static int pl2_watt ;
28- static int pl4_watt ;
24+ // Loaded on EC reset
25+ #define POWER_LIMIT_1_W_DEFAULT 40
26+ #define POWER_LIMIT_2_W_DEFAULT 64
27+ #define POWER_LIMIT_4_W_DEFAULT 121
28+
29+ // Loaded by ectool command "cpupower default"
30+ #define POWER_LIMIT_1_W_USER_DEFAULT 28
31+ #define POWER_LIMIT_2_W_USER_DEFAULT 64
32+ #define POWER_LIMIT_4_W_USER_DEFAULT 121
33+
34+ static int POWER_LIMIT_1_W = POWER_LIMIT_1_W_DEFAULT ;
35+ static int POWER_LIMIT_2_W = POWER_LIMIT_2_W_DEFAULT ;
36+ static int POWER_LIMIT_4_W = POWER_LIMIT_4_W_DEFAULT ;
37+
38+ static int pl1_watt = POWER_LIMIT_1_W_DEFAULT ;
39+ static int pl2_watt = POWER_LIMIT_2_W_DEFAULT ;
40+ static int pl4_watt = POWER_LIMIT_4_W_DEFAULT ;
2941static int psys_watt ;
3042bool manual_ctl ;
3143
@@ -47,6 +59,7 @@ void update_soc_power_limit(bool force_update, bool force_no_adapter)
4759 int pps_power_budget ;
4860 int battery_percent ;
4961
62+ static int old_pl1_watt = -1 ;
5063 static int old_pl2_watt = -1 ;
5164 static int old_pl4_watt = -1 ;
5265 static int old_psys_watt = -1 ;
@@ -72,23 +85,23 @@ void update_soc_power_limit(bool force_update, bool force_no_adapter)
7285 psys_watt = ((active_power * 95 ) / 100 ) - pps_power_budget ;
7386 } else {
7487 /* ADP > 55W and Battery percentage >= 30% */
75- pl2_watt = 64 ;
76- pl4_watt = 121 ;
88+ pl1_watt = POWER_LIMIT_1_W ;
89+ pl2_watt = POWER_LIMIT_2_W ;
90+ pl4_watt = POWER_LIMIT_4_W ;
7791 /* psys watt = adp watt * 0.95 + battery watt(55 W) * 0.7 - pps power budget */
7892 psys_watt = ((active_power * 95 ) / 100 ) + 39 - pps_power_budget ;
7993 }
8094 if (pl2_watt != old_pl2_watt || pl4_watt != old_pl4_watt ||
81- psys_watt != old_psys_watt || force_update ) {
95+ psys_watt != old_psys_watt || force_update ||
96+ pl1_watt != old_pl1_watt ) {
97+ old_pl1_watt = pl1_watt ;
8298 old_psys_watt = psys_watt ;
8399 old_pl4_watt = pl4_watt ;
84100 old_pl2_watt = pl2_watt ;
85101
86- pl1_watt = POWER_LIMIT_1_W ;
87- if (manual_ctl == false) {
88- CPRINTS ("Updating SOC Power Limits: PL2 %d, PL4 %d, Psys %d, Adapter %d" ,
89- pl2_watt , pl4_watt , psys_watt , active_power );
90- set_pl_limits (pl1_watt , pl2_watt , pl4_watt , psys_watt );
91- }
102+ CPRINTS ("Updating SOC Power Limits: PL1 %d, PL2 %d, PL4 %d, Psys %d, Adapter %d" ,
103+ pl1_watt , pl2_watt , pl4_watt , psys_watt , active_power );
104+ set_pl_limits (pl1_watt , pl2_watt , pl4_watt , psys_watt );
92105 }
93106}
94107
@@ -179,3 +192,41 @@ static int cmd_fan_mode(int argc, char **argv)
179192DECLARE_CONSOLE_COMMAND (fanmode , cmd_fan_mode ,
180193 "[silent|normal|extreme]" ,
181194 "Set/Get fan mode" );
195+
196+ /* Host command handler for CPU power limits */
197+ static enum ec_status host_command_cpu_power (struct host_cmd_handler_args * args )
198+ {
199+ const struct ec_params_cpu_power * p = args -> params ;
200+ struct ec_response_cpu_power * r = args -> response ;
201+
202+ /* If parameters provided, set the values */
203+ if (args -> params_size > 0 && p ) {
204+ /*
205+ * "default" from ectool is encoded as an all-zero payload.
206+ */
207+ if (p -> pl1_mW == 0 && p -> pl2_mW == 0 && p -> pl4_mW == 0 ) {
208+ POWER_LIMIT_1_W = POWER_LIMIT_1_W_USER_DEFAULT ;
209+ POWER_LIMIT_2_W = POWER_LIMIT_2_W_USER_DEFAULT ;
210+ POWER_LIMIT_4_W = POWER_LIMIT_4_W_USER_DEFAULT ;
211+ } else {
212+ if (p -> pl1_mW != 0 )
213+ POWER_LIMIT_1_W = p -> pl1_mW / 1000 ;
214+ if (p -> pl2_mW != 0 )
215+ POWER_LIMIT_2_W = p -> pl2_mW / 1000 ;
216+ if (p -> pl4_mW != 0 )
217+ POWER_LIMIT_4_W = p -> pl4_mW / 1000 ;
218+ }
219+ update_soc_power_limit (true, false);
220+ }
221+
222+ /* Return current power limits in mW */
223+ r -> pl1_mW = pl1_watt * 1000 ;
224+ r -> pl2_mW = pl2_watt * 1000 ;
225+ r -> pl4_mW = pl4_watt * 1000 ;
226+ r -> psys_mW = psys_watt * 1000 ;
227+
228+ args -> response_size = sizeof (* r );
229+ return EC_RES_SUCCESS ;
230+ }
231+
232+ DECLARE_HOST_COMMAND (EC_CMD_CPU_POWER , host_command_cpu_power , EC_VER_MASK (0 ));
0 commit comments