summaryrefslogtreecommitdiff
path: root/chip/lm4/power_button.c
blob: 836a6634037e138f4da9a1497bbeac3a5fcb72f7 (plain)
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/* Copyright (c) 2012 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.
 */

/* Power button and lid switch module for Chrome EC */

#include "chipset.h"
#include "console.h"
#include "eoption.h"
#include "gpio.h"
#include "hooks.h"
#include "keyboard.h"
#include "keyboard_scan.h"
#include "lpc.h"
#include "lpc_commands.h"
#include "power_button.h"
#include "pwm.h"
#include "system.h"
#include "task.h"
#include "timer.h"
#include "util.h"

/* Console output macros */
#define CPUTS(outstr) cputs(CC_POWERBTN, outstr)
#define CPRINTF(format, args...) cprintf(CC_POWERBTN, format, ## args)

/* Power button state machine.
 *
 *   PWRBTN#   ---                      ----
 *     to EC     |______________________|
 *
 *
 *   PWRBTN#   ---  ---------           ----
 *    to PCH     |__|       |___________|
 *                t0    t1    held down
 *
 *   scan code   |                      |
 *    to host    v                      v
 *     @S0   make code             break code
 */
#define PWRBTN_DEBOUNCE_US 30000  /* Debounce time for power button */
#define PWRBTN_DELAY_T0    32000  /* 32ms (PCH requires >16ms) */
#define PWRBTN_DELAY_T1    (4000000 - PWRBTN_DELAY_T0)  /* 4 secs - t0 */
#define PWRBTN_INITIAL_US  200000 /* Length of time to stretch initial power
				   * button press to give chipset a chance to
				   * wake up (~100ms) and react to the press
				   * (~16ms).  Also used as pulse length for
				   * simulated power button presses when the
				   * system is off. */

#define LID_DEBOUNCE_US    30000  /* Debounce time for lid switch */

enum power_button_state {
	PWRBTN_STATE_STOPPED = 0,
	PWRBTN_STATE_START,
	PWRBTN_STATE_T0,
	PWRBTN_STATE_T1,
	PWRBTN_STATE_HELD_DOWN,
	PWRBTN_STATE_STOPPING,
	PWRBTN_STATE_BOOT_RESET,
	PWRBTN_STATE_BOOT_RECOVERY,
	PWRBTN_STATE_WAS_OFF,
};
static enum power_button_state pwrbtn_state = PWRBTN_STATE_STOPPED;

/* Time for next state transition of power button state machine, or 0 if the
 * state doesn't have a timeout. */
static uint64_t tnext_state;

/* Debounce timeouts for power button and lid switch.  0 means the signal is
 * stable (not being debounced). */
static uint64_t tdebounce_lid;
static uint64_t tdebounce_pwr;

static uint8_t *memmap_switches;
static int debounced_lid_open;

/* Update status of non-debounced switches */
static void update_other_switches(void)
{
	if (gpio_get_level(GPIO_WRITE_PROTECT) == 0)
		*memmap_switches |= EC_LPC_SWITCH_WRITE_PROTECT_DISABLED;
	else
		*memmap_switches &= ~EC_LPC_SWITCH_WRITE_PROTECT_DISABLED;

	if (keyboard_scan_recovery_pressed())
		*memmap_switches |= EC_LPC_SWITCH_KEYBOARD_RECOVERY;
	else
		*memmap_switches &= ~EC_LPC_SWITCH_KEYBOARD_RECOVERY;

	if (gpio_get_level(GPIO_RECOVERYn) == 0)
		*memmap_switches |= EC_LPC_SWITCH_DEDICATED_RECOVERY;
	else
		*memmap_switches &= ~EC_LPC_SWITCH_DEDICATED_RECOVERY;

	/* Was this a reboot requesting recovery? */
	/* TODO: should use a different flag, not the dedicated recovery
	 * switch flag! */
	if (system_get_recovery_required())
		*memmap_switches |= EC_LPC_SWITCH_DEDICATED_RECOVERY;

#ifdef CONFIG_FAKE_DEV_SWITCH
	if (eoption_get_bool(EOPTION_BOOL_FAKE_DEV))
		*memmap_switches |= EC_LPC_SWITCH_FAKE_DEVELOPER;
	else
		*memmap_switches &= ~EC_LPC_SWITCH_FAKE_DEVELOPER;
#endif
}


static void set_pwrbtn_to_pch(int high)
{
	CPRINTF("[%T PB PCH pwrbtn=%s]\n", high ? "HIGH" : "LOW");
	gpio_set_level(GPIO_PCH_PWRBTNn, high);
}


/* Return 1 if power button is pressed, 0 if not pressed. */
static int get_power_button_pressed(void)
{
	return gpio_get_level(GPIO_POWER_BUTTONn) ? 0 : 1;
}


static void update_backlight(void)
{
	/* Only enable the backlight if the lid is open */
	if (gpio_get_level(GPIO_PCH_BKLTEN) && debounced_lid_open)
		gpio_set_level(GPIO_ENABLE_BACKLIGHT, 1);
	else
		gpio_set_level(GPIO_ENABLE_BACKLIGHT, 0);

	/* Same with keyboard backlight */
	pwm_enable_keyboard_backlight(debounced_lid_open);
}


/* Power button state machine.  Passed current time from usec counter. */
static void state_machine(uint64_t tnow)
{
	/* Not the time to move onto next state */
	if (tnow < tnext_state)
		return;

	/* States last forever unless otherwise specified */
	tnext_state = 0;

	switch (pwrbtn_state) {
	case PWRBTN_STATE_START:
		if (chipset_in_state(CHIPSET_STATE_ANY_OFF)) {
			/* Chipset is off, so wake the chipset and send it a
			 * long enough pulse to wake up.  After that we'll
			 * reflect the true power button state.  If we don't
			 * stretch the pulse here, the user may release the
			 * power button before the chipset finishes waking from
			 * hard off state. */
			chipset_exit_hard_off();
			tnext_state = tnow + PWRBTN_INITIAL_US;
			pwrbtn_state = PWRBTN_STATE_WAS_OFF;
		} else {
			/* Chipset is on, so send the chipset a pulse */
			tnext_state = tnow + PWRBTN_DELAY_T0;
			pwrbtn_state = PWRBTN_STATE_T0;
		}
		set_pwrbtn_to_pch(0);
		break;
	case PWRBTN_STATE_T0:
		tnext_state = tnow + PWRBTN_DELAY_T1;
		pwrbtn_state = PWRBTN_STATE_T1;
		set_pwrbtn_to_pch(1);
		break;
	case PWRBTN_STATE_T1:
		/* If the chipset is already off, don't tell it the power
		 * button is down; it'll just cause the chipset to turn on
		 * again. */
		if (chipset_in_state(CHIPSET_STATE_ANY_OFF))
			CPRINTF("[%T PB chipset already off]\n");
		else
			set_pwrbtn_to_pch(0);
		pwrbtn_state = PWRBTN_STATE_HELD_DOWN;
		break;
	case PWRBTN_STATE_STOPPING:
		set_pwrbtn_to_pch(1);
		pwrbtn_state = PWRBTN_STATE_STOPPED;
		break;
	case PWRBTN_STATE_BOOT_RECOVERY:
		set_pwrbtn_to_pch(1);
		pwrbtn_state = PWRBTN_STATE_BOOT_RESET;
		break;
	case PWRBTN_STATE_WAS_OFF:
		if (get_power_button_pressed()) {
			/* User is still holding the power button */
			pwrbtn_state = PWRBTN_STATE_HELD_DOWN;
		} else {
			/* Stop stretching the power button press */
			*memmap_switches &= ~EC_LPC_SWITCH_POWER_BUTTON_PRESSED;
			keyboard_set_power_button(0);
			pwrbtn_state = PWRBTN_STATE_STOPPING;
		}
		break;
	case PWRBTN_STATE_STOPPED:
	case PWRBTN_STATE_HELD_DOWN:
	case PWRBTN_STATE_BOOT_RESET:
		/* Do nothing */
		break;
	}
}


/* Handle debounced power button changing state */
static void power_button_changed(uint64_t tnow)
{
	if (pwrbtn_state == PWRBTN_STATE_BOOT_RECOVERY) {
		/* Ignore all power button changes during the recovery pulse */
		CPRINTF("[%T PB changed during recovery pulse]\n");
	} else if (pwrbtn_state == PWRBTN_STATE_WAS_OFF) {
		/* Ignore all power button changes during an initial pulse */
		CPRINTF("[%T PB changed during initial pulse]\n");
	} else if (get_power_button_pressed()) {
		/* Power button pressed */
		CPRINTF("[%T PB pressed]\n");
		pwrbtn_state = PWRBTN_STATE_START;
		tnext_state = tnow;
		*memmap_switches |= EC_LPC_SWITCH_POWER_BUTTON_PRESSED;
		keyboard_set_power_button(1);
		lpc_set_host_events(
			EC_LPC_HOST_EVENT_MASK(EC_LPC_HOST_EVENT_POWER_BUTTON));
	} else if (pwrbtn_state == PWRBTN_STATE_BOOT_RESET) {
		/* Ignore the first power button release after a
		 * keyboard-controlled reset, since we already told the PCH the
		 * power button was released. */
		CPRINTF("[%T PB released after keyboard reset]\n");
		pwrbtn_state = PWRBTN_STATE_STOPPED;
	} else {
		/* Power button released normally (outside of a
		 * keyboard-controlled reset) */
		CPRINTF("[%T PB released]\n");
		pwrbtn_state = PWRBTN_STATE_STOPPING;
		tnext_state = tnow;
		*memmap_switches &= ~EC_LPC_SWITCH_POWER_BUTTON_PRESSED;
		keyboard_set_power_button(0);
	}
}


/* Lid open */
static void lid_switch_open(uint64_t tnow)
{
	CPRINTF("[%T PB lid open]\n");

	debounced_lid_open = 1;
	*memmap_switches |= EC_LPC_SWITCH_LID_OPEN;

	lpc_set_host_events(EC_LPC_HOST_EVENT_MASK(
			    EC_LPC_HOST_EVENT_LID_OPEN));

	/* If the chipset is off, send a power button pulse to wake up the
	 * chipset. */
	if (chipset_in_state(CHIPSET_STATE_ANY_OFF)) {
		chipset_exit_hard_off();
		set_pwrbtn_to_pch(0);
		pwrbtn_state = PWRBTN_STATE_STOPPING;
		tnext_state = tnow + PWRBTN_INITIAL_US;
		task_wake(TASK_ID_POWERBTN);
	}
}


/* Lid close */
static void lid_switch_close(uint64_t tnow)
{
	CPRINTF("[%T PB lid close]\n");

	debounced_lid_open = 0;
	*memmap_switches &= ~EC_LPC_SWITCH_LID_OPEN;

	lpc_set_host_events(EC_LPC_HOST_EVENT_MASK(
			    EC_LPC_HOST_EVENT_LID_CLOSED));
}


/* Handle debounced lid switch changing state */
static void lid_switch_changed(uint64_t tnow)
{
	if (gpio_get_level(GPIO_LID_SWITCHn))
		lid_switch_open(tnow);
	else
		lid_switch_close(tnow);

	update_backlight();
}


void power_button_interrupt(enum gpio_signal signal)
{
	/* Reset debounce time for the changed signal */
	switch (signal) {
	case GPIO_LID_SWITCHn:
		tdebounce_lid = get_time().val + LID_DEBOUNCE_US;
		break;
	case GPIO_POWER_BUTTONn:
		tdebounce_pwr = get_time().val + PWRBTN_DEBOUNCE_US;
		break;
	case GPIO_PCH_BKLTEN:
		update_backlight();
		break;
	default:
		/* Non-debounced switches; we'll update their state
		 * automatically the next time through the task loop. */
		break;
	}

	/* We don't have a way to tell the task to wake up at the end of the
         * debounce interval; wake it up now so it can go back to sleep for the
         * remainder of the interval.  The alternative would be to have the
         * task wake up _every_ debounce_us on its own; that's less desirable
         * when the EC should be sleeping. */
	task_wake(TASK_ID_POWERBTN);
}


static int power_button_init(void)
{
	/* Set up memory-mapped switch positions */
	memmap_switches = lpc_get_memmap_range() + EC_LPC_MEMMAP_SWITCHES;
	*memmap_switches = 0;
	if (gpio_get_level(GPIO_LID_SWITCHn) != 0) {
		debounced_lid_open = 1;
		*memmap_switches |= EC_LPC_SWITCH_LID_OPEN;
	}
	update_other_switches();
	update_backlight();

	if (system_get_reset_cause() == SYSTEM_RESET_RESET_PIN) {
		/* Reset triggered by keyboard-controlled reset, so override
		 * the power button signal to the PCH. */
		if (keyboard_scan_recovery_pressed()) {
			/* In recovery mode, so send a power button pulse to
			 * the PCH so it powers on. */
			set_pwrbtn_to_pch(0);
			pwrbtn_state = PWRBTN_STATE_BOOT_RECOVERY;
			tnext_state = get_time().val + PWRBTN_INITIAL_US;
		} else {
			/* Keyboard-controlled reset, so don't let the PCH see
			 * that the power button was pressed.  Otherwise, it
			 * might power on. */
			set_pwrbtn_to_pch(1);
			pwrbtn_state = PWRBTN_STATE_BOOT_RESET;
		}
	} else {
		/* Copy initial power button state to PCH and memory-mapped
		 * switch positions. */
		set_pwrbtn_to_pch(get_power_button_pressed() ? 0 : 1);
		if (get_power_button_pressed())
			*memmap_switches |= EC_LPC_SWITCH_POWER_BUTTON_PRESSED;
	}

	/* Enable interrupts, now that we've initialized */
	gpio_enable_interrupt(GPIO_POWER_BUTTONn);
	gpio_enable_interrupt(GPIO_LID_SWITCHn);
	gpio_enable_interrupt(GPIO_WRITE_PROTECT);
	gpio_enable_interrupt(GPIO_RECOVERYn);

	return EC_SUCCESS;
}
DECLARE_HOOK(HOOK_INIT, power_button_init, HOOK_PRIO_DEFAULT);


void power_button_task(void)
{
	uint64_t t;
	uint64_t tsleep;
	while (1) {
		t = get_time().val;

		/* Handle debounce timeouts for power button and lid switch */
		if (tdebounce_pwr && t >= tdebounce_pwr) {
			tdebounce_pwr = 0;
			power_button_changed(t);
		}
		if (tdebounce_lid && t >= tdebounce_lid) {
			tdebounce_lid = 0;
			lid_switch_changed(t);
		}

		/* Handle non-debounced switches */
		update_other_switches();

		/* Update state machine */
		state_machine(t);

		/* Sleep until our next timeout */
		tsleep = -1;
		if (tdebounce_pwr && tdebounce_pwr < tsleep)
			tsleep = tdebounce_pwr;
		if (tdebounce_lid && tdebounce_lid < tsleep)
			tsleep = tdebounce_lid;
		if (tnext_state && tnext_state < tsleep)
			tsleep = tnext_state;
		t = get_time().val;
		if (tsleep > t) {
			unsigned d = tsleep == -1 ? -1 : (unsigned)(tsleep - t);
			/* (Yes, the conversion from uint64_t to unsigned could
			 * theoretically overflow if we wanted to sleep for
			 * more than 2^32 us, but our timeouts are small enough
			 * that can't happen - and even if it did, we'd just go
			 * back to sleep after deciding that we woke up too
			 * early.) */
			CPRINTF("[%T PB task %d wait %d]\n", pwrbtn_state, d);
			task_wait_event(d);
		}
	}
}

/*****************************************************************************/
/* Console commands */

static int command_powerbtn(int argc, char **argv)
{
	int ms = PWRBTN_INITIAL_US / 1000;  /* Press duration in ms */
	char *e;

	if (argc > 1) {
		ms = strtoi(argv[1], &e, 0);
		if (*e) {
			ccputs("Invalid duration.\n"
			       "Usage: powerbtn [duration_ms]\n");
			return EC_ERROR_INVAL;
		}
	}

	/* Note that this only simulates the raw power button signal to the
	 * PCH.  It does not simulate the full state machine which sends SMIs
	 * and other events to other parts of the EC and chipset. */
	ccprintf("Simulating %d ms power button press.\n", ms);
	chipset_exit_hard_off();
	set_pwrbtn_to_pch(0);
	usleep(ms * 1000);
	set_pwrbtn_to_pch(1);
	return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(powerbtn, command_powerbtn);


static int command_lidopen(int argc, char **argv)
{
	lid_switch_open(get_time().val);
	return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(lidopen, command_lidopen);


static int command_lidclose(int argc, char **argv)
{
	lid_switch_close(get_time().val);
	return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(lidclose, command_lidclose);