1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/* rk3399 chipset power control module for Chrome EC */
#include "charge_state.h"
#include "chipset.h"
#include "common.h"
#include "console.h"
#include "ec_commands.h"
#include "gpio.h"
#include "hooks.h"
#include "lid_switch.h"
#include "power.h"
#include "power_button.h"
#include "system.h"
#include "task.h"
#include "timer.h"
#include "usb_charge.h"
#include "util.h"
#include "wireless.h"
/* Console output macros */
#define CPUTS(outstr) cputs(CC_CHIPSET, outstr)
#define CPRINTS(format, args...) cprints(CC_CHIPSET, format, ## args)
/* Input state flags */
#define IN_PGOOD_PP5000 POWER_SIGNAL_MASK(PP5000_PWR_GOOD)
#define IN_PGOOD_SYS POWER_SIGNAL_MASK(SYS_PWR_GOOD)
#define IN_PGOOD_AP POWER_SIGNAL_MASK(AP_PWR_GOOD)
#define IN_SUSPEND_DEASSERTED POWER_SIGNAL_MASK(SUSPEND_DEASSERTED)
/* All always-on supplies */
#define IN_PGOOD_ALWAYS_ON (IN_PGOOD_SYS)
/* Rails requires for S3 */
#define IN_PGOOD_S3 (IN_PGOOD_ALWAYS_ON | IN_PGOOD_PP5000)
/* Rails required for S0 */
#define IN_PGOOD_S0 (IN_PGOOD_S3 | IN_PGOOD_AP)
/* All inputs in the right state for S0 */
#define IN_ALL_S0 (IN_PGOOD_S0 | IN_SUSPEND_DEASSERTED)
/* Long power key press to force shutdown in S0 */
#define FORCED_SHUTDOWN_DELAY (8 * SECOND)
#define CHARGER_INITIALIZED_DELAY_MS 100
#define CHARGER_INITIALIZED_TRIES 40
static int forcing_shutdown;
void chipset_force_shutdown(void)
{
CPRINTS("%s()", __func__);
/*
* Force power off. This condition will reset once the state machine
* transitions to G3.
*/
forcing_shutdown = 1;
task_wake(TASK_ID_CHIPSET);
}
void chipset_reset(int cold_reset)
{
/* TODO: handle cold_reset */
CPRINTS("%s(%d)", __func__, cold_reset);
/* Pulse SYS_RST */
gpio_set_level(GPIO_SYS_RST_L, 0);
udelay(10);
gpio_set_level(GPIO_SYS_RST_L, 1);
}
enum power_state power_chipset_init(void)
{
if (system_jumped_to_this_image()) {
if ((power_get_signals() & IN_ALL_S0) == IN_ALL_S0) {
disable_sleep(SLEEP_MASK_AP_RUN);
CPRINTS("already in S0");
return POWER_S0;
}
wireless_set_state(WIRELESS_OFF);
} else if (!(system_get_reset_flags() & RESET_FLAG_AP_OFF))
/* Auto-power on */
chipset_exit_hard_off();
return POWER_G3;
}
static void force_shutdown(void)
{
forcing_shutdown = 1;
task_wake(TASK_ID_CHIPSET);
}
DECLARE_DEFERRED(force_shutdown);
enum power_state power_handle_state(enum power_state state)
{
static int sys_reset_needed;
int tries = 0;
switch (state) {
case POWER_G3:
break;
case POWER_S5:
if (forcing_shutdown)
return POWER_S5G3;
else
return POWER_S5S3;
break;
case POWER_S3:
if (!power_has_signals(IN_PGOOD_S3) || forcing_shutdown)
return POWER_S3S5;
else if (!gpio_get_level(GPIO_AP_EC_S3_S0_L))
return POWER_S3S0;
break;
case POWER_S0:
if (!power_has_signals(IN_PGOOD_S0) || forcing_shutdown)
return POWER_S0S3;
else if (gpio_get_level(GPIO_AP_EC_S3_S0_L))
return POWER_S0S3;
break;
case POWER_G3S5:
forcing_shutdown = 0;
/*
* Allow time for charger to be initialized, in case we're
* trying to boot the AP with no battery.
*/
while (charge_prevent_power_on(0) &&
tries++ < CHARGER_INITIALIZED_TRIES) {
msleep(CHARGER_INITIALIZED_DELAY_MS);
}
/* Return to G3 if battery level is too low. */
if (charge_want_shutdown() ||
tries > CHARGER_INITIALIZED_TRIES) {
CPRINTS("power-up inhibited");
chipset_force_shutdown();
return POWER_G3;
}
/* Power up to next state */
return POWER_S5;
case POWER_S5S3:
gpio_set_level(GPIO_PPVAR_LOGIC_EN, 1);
gpio_set_level(GPIO_PP900_AP_EN, 1);
msleep(2);
gpio_set_level(GPIO_PP900_PMU_EN, 1);
gpio_set_level(GPIO_PP900_PLL_EN, 1);
gpio_set_level(GPIO_PP900_USB_EN, 1);
gpio_set_level(GPIO_PP900_DDRPLL_EN, 1);
gpio_set_level(GPIO_PP900_PCIE_EN, 1);
msleep(2);
gpio_set_level(GPIO_PP1800_PMU_EN_L, 0);
gpio_set_level(GPIO_PPVAR_CLOGIC_EN, 1);
msleep(2);
gpio_set_level(GPIO_PP1800_USB_EN_L, 0);
gpio_set_level(GPIO_PP1800_AP_AVDD_EN_L, 0);
msleep(2);
gpio_set_level(GPIO_LPDDR_PWR_EN, 1);
gpio_set_level(GPIO_PP5000_EN, 1);
msleep(2);
gpio_set_level(GPIO_PP1800_SIXAXIS_EN_L, 0);
msleep(2);
gpio_set_level(GPIO_PP3300_TRACKPAD_EN_L, 0);
/*
* TODO: Consider ADC_PP900_AP / ADC_PP1200_LPDDR analog
* voltage levels for state transition.
*/
if (power_wait_signals(IN_PGOOD_S3)) {
chipset_force_shutdown();
return POWER_S5;
}
/* Call hooks now that rails are up */
hook_notify(HOOK_CHIPSET_STARTUP);
/* We came from S5, so be sure to pulse sys_rst later */
sys_reset_needed = 1;
/* Power up to next state */
return POWER_S3;
case POWER_S3S0:
gpio_set_level(GPIO_AP_CORE_EN, 1);
msleep(2);
gpio_set_level(GPIO_PP1800_S0_EN_L, 0);
msleep(2);
gpio_set_level(GPIO_PP3300_S0_EN_L, 0);
msleep(2);
gpio_set_level(GPIO_PP3300_USB_EN_L, 0);
msleep(2);
if (sys_reset_needed) {
/* Pulse SYS_RST if we came from S5 */
gpio_set_level(GPIO_SYS_RST_L, 0);
msleep(10);
gpio_set_level(GPIO_SYS_RST_L, 1);
sys_reset_needed = 0;
}
gpio_set_level(GPIO_PP1800_LID_EN_L, 0);
gpio_set_level(GPIO_PP1800_SENSOR_EN_L, 0);
if (power_wait_signals(IN_PGOOD_S0)) {
chipset_force_shutdown();
return POWER_S3;
}
/* Enable wireless */
wireless_set_state(WIRELESS_ON);
/* Call hooks now that rails are up */
hook_notify(HOOK_CHIPSET_RESUME);
/*
* Disable idle task deep sleep. This means that the low
* power idle task will not go into deep sleep while in S0.
*/
disable_sleep(SLEEP_MASK_AP_RUN);
/* Power up to next state */
return POWER_S0;
case POWER_S0S3:
/* Call hooks before we remove power rails */
hook_notify(HOOK_CHIPSET_SUSPEND);
/* Suspend wireless */
wireless_set_state(WIRELESS_SUSPEND);
gpio_set_level(GPIO_PP1800_SENSOR_EN_L, 1);
gpio_set_level(GPIO_PP1800_LID_EN_L, 1);
msleep(10);
gpio_set_level(GPIO_PP3300_USB_EN_L, 1);
msleep(10);
gpio_set_level(GPIO_PP3300_S0_EN_L, 1);
msleep(10);
gpio_set_level(GPIO_PP1800_S0_EN_L, 1);
msleep(10);
gpio_set_level(GPIO_AP_CORE_EN, 0);
/*
* Enable idle task deep sleep. Allow the low power idle task
* to go into deep sleep in S3 or lower.
*/
enable_sleep(SLEEP_MASK_AP_RUN);
/*
* In case the power button is held awaiting power-off timeout,
* power off immediately now that we're entering S3.
*/
if (power_button_is_pressed()) {
forcing_shutdown = 1;
hook_call_deferred(&force_shutdown_data, -1);
}
return POWER_S3;
case POWER_S3S5:
/* Call hooks before we remove power rails */
hook_notify(HOOK_CHIPSET_SHUTDOWN);
/* Disable wireless */
wireless_set_state(WIRELESS_OFF);
gpio_set_level(GPIO_PP3300_TRACKPAD_EN_L, 1);
msleep(10);
gpio_set_level(GPIO_PP1800_SIXAXIS_EN_L, 1);
msleep(10);
gpio_set_level(GPIO_PP5000_EN, 0);
gpio_set_level(GPIO_LPDDR_PWR_EN, 0);
msleep(10);
gpio_set_level(GPIO_PP1800_AP_AVDD_EN_L, 1);
gpio_set_level(GPIO_PP1800_USB_EN_L, 1);
msleep(10);
gpio_set_level(GPIO_PPVAR_CLOGIC_EN, 0);
gpio_set_level(GPIO_PP1800_PMU_EN_L, 1);
msleep(10);
gpio_set_level(GPIO_PP900_PCIE_EN, 0);
gpio_set_level(GPIO_PP900_DDRPLL_EN, 0);
gpio_set_level(GPIO_PP900_USB_EN, 0);
gpio_set_level(GPIO_PP900_PLL_EN, 0);
gpio_set_level(GPIO_PP900_PMU_EN, 0);
msleep(10);
gpio_set_level(GPIO_PP900_AP_EN, 0);
gpio_set_level(GPIO_PPVAR_LOGIC_EN, 0);
/* Start shutting down */
return POWER_S5;
case POWER_S5G3:
return POWER_G3;
}
return state;
}
static void power_button_changed(void)
{
if (power_button_is_pressed()) {
if (chipset_in_state(CHIPSET_STATE_ANY_OFF))
/* Power up from off */
chipset_exit_hard_off();
else if (!chipset_in_state(CHIPSET_STATE_ON)) {
/* Power down immediately from S3 */
force_shutdown();
return;
}
/* Delayed power down from S0, cancel on PB release */
hook_call_deferred(&force_shutdown_data,
FORCED_SHUTDOWN_DELAY);
} else {
/* Power button released, cancel deferred shutdown */
hook_call_deferred(&force_shutdown_data, -1);
}
}
DECLARE_HOOK(HOOK_POWER_BUTTON_CHANGE, power_button_changed, HOOK_PRIO_DEFAULT);
static void lid_changed(void)
{
/* Power-up from off on lid open */
if (lid_is_open() && chipset_in_state(CHIPSET_STATE_ANY_OFF))
chipset_exit_hard_off();
}
DECLARE_HOOK(HOOK_LID_CHANGE, lid_changed, HOOK_PRIO_DEFAULT);
|