summaryrefslogtreecommitdiff
path: root/chip/lm4/power_button.c
blob: 28519b6e5fc60dacbe4e6e16bffbf57d5f2d33c7 (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
/* 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 "gpio.h"
#include "power_button.h"
#include "task.h"
#include "timer.h"
#include "uart.h"

enum debounce_isr_id {
	DEBOUNCE_LID,
	DEBOUNCE_PWRBTN,
	DEBOUNCE_ISR_ID_MAX
};

struct debounce_isr_t {
	/* TODO: Add a carry bit to indicate timestamp overflow */
	timestamp_t tstamp;
	int started;
	void (*callback)(void);
};

struct debounce_isr_t debounce_isr[DEBOUNCE_ISR_ID_MAX];

enum power_button_state {
	PWRBTN_STATE_STOPPED = 0,
	PWRBTN_STATE_START = 1,
	PWRBTN_STATE_T0 = 2,
	PWRBTN_STATE_T1 = 3,
	PWRBTN_STATE_T2 = 4,
	PWRBTN_STATE_STOPPING = 5,
};
static enum power_button_state pwrbtn_state = PWRBTN_STATE_STOPPED;
/* The next timestamp to move onto next state if power button is still pressed.
 */
static timestamp_t pwrbtn_next_ts = {0};
static int init_called;

#define PWRBTN_DELAY_T0 32000  /* 32ms */
#define PWRBTN_DELAY_T1 (4000000 - PWRBTN_DELAY_T0)  /* 4 secs - t0 */
#define PWRBTN_DELAY_T2 4000000  /* 4 secs */


static void lid_switch_isr(void)
{
	/* TODO: Currently we pass through the LID_SW# pin to R_EC_LID_OUT#
	 * directly. Modify this if we need to consider more conditions. */
#ifdef BOARD_bds
	gpio_set_level(EC_GPIO_LID_SWITCH_OUT,
		       gpio_get_level(EC_GPIO_LID_SWITCH));
#endif
}


/* Power button state machine.
 *
 *   PWRBTN#   ---                      ----
 *     to EC     |______________________|
 *
 *
 *   PWRBTN#   ---  ---------           ----
 *    to PCH     |__|       |___________|
 *                t0    t1       t2
 */
static void set_pwrbtn_to_pch(int high)
{
#ifdef BOARD_link
	gpio_set_level(EC_GPIO_POWER_BUTTON_OUT, high);
#else
	uart_printf("[%d] set_pwrbtn_to_pch(%s)\n",
		    get_time().le.lo, high ? "HIGH" : "LOW");
#endif
}


static void pwrbtn_sm_start(void)
{
	pwrbtn_state = PWRBTN_STATE_START;
	pwrbtn_next_ts = get_time();  /* execute action now! */
}


static void pwrbtn_sm_stop(void)
{
	pwrbtn_state = PWRBTN_STATE_STOPPING;
	pwrbtn_next_ts = get_time();  /* execute action now ! */
}


static void pwrbtn_sm_handle(timestamp_t current)
{
	/* Not the time to move onto next state */
	if (pwrbtn_state == PWRBTN_STATE_STOPPED ||
	    current.val < pwrbtn_next_ts.val)
		return;

	switch (pwrbtn_state) {
	case PWRBTN_STATE_START:
		pwrbtn_next_ts.val = current.val + PWRBTN_DELAY_T0;
		pwrbtn_state = PWRBTN_STATE_T0;
		set_pwrbtn_to_pch(0);
		break;
	case PWRBTN_STATE_T0:
		pwrbtn_next_ts.val = current.val + PWRBTN_DELAY_T1;
		pwrbtn_state = PWRBTN_STATE_T1;
		set_pwrbtn_to_pch(1);
		break;
	case PWRBTN_STATE_T1:
		pwrbtn_next_ts.val = current.val + PWRBTN_DELAY_T2;
		pwrbtn_state = PWRBTN_STATE_T2;
		set_pwrbtn_to_pch(0);
		break;
	case PWRBTN_STATE_T2:
		/* T2 has passed */
	case PWRBTN_STATE_STOPPING:
		set_pwrbtn_to_pch(1);
		pwrbtn_state = PWRBTN_STATE_STOPPED;
		break;
	default:
		break;
	}
}


static void power_button_isr(void)
{
	if (!gpio_get_level(EC_GPIO_POWER_BUTTON)) {
		/* pressed */
		pwrbtn_sm_start();
		/* TODO: implement after chip/lm4/x86_power.c is completed. */
		/* if system is in S5, power_on_system()
		 * elif system is in S3, resume_system()
		 * else S0 i8042_send_host(make_code); */
	} else {
		/* released */
		pwrbtn_sm_stop();
		/* TODO: implement after chip/lm4/x86_power.c is completed. */
		/* if system in S0, i8042_send_host(break_code); */
	}
}


void power_button_interrupt(enum gpio_signal signal)
{
	timestamp_t timelimit;
	int d = (signal == EC_GPIO_LID_SWITCH ? DEBOUNCE_LID : DEBOUNCE_PWRBTN);

	/* TODO: (crosbug.com/p/7456) there's currently a race condition where
	 * we can get an interrupt before clock_init() has been called.  In
	 * this case, get_time crashes().  Work around this by checking if our
	 * init has been called and ignoring interrupts if so. */
	if (!init_called)
		return;

	/* Set 30 ms debounce timelimit */
	timelimit = get_time();
	timelimit.val += 30000;

	/* Handle lid switch and power button debounce */
	debounce_isr[d].tstamp = timelimit;
	debounce_isr[d].started = 1;
}


int power_button_init(void)
{
	debounce_isr[DEBOUNCE_LID].started = 0;
	debounce_isr[DEBOUNCE_LID].callback = lid_switch_isr;
	debounce_isr[DEBOUNCE_PWRBTN].started = 0;
	debounce_isr[DEBOUNCE_PWRBTN].callback = power_button_isr;

	init_called = 1;

	return EC_SUCCESS;
}


void power_button_task(void)
{
	int i;
	timestamp_t ts;

	while (1) {
		usleep(1000);
		ts = get_time();
		for (i = 0; i < DEBOUNCE_ISR_ID_MAX; ++i) {
			if (debounce_isr[i].started &&
				ts.val >= debounce_isr[i].tstamp.val) {
				debounce_isr[i].started = 0;
				debounce_isr[i].callback();
			}
		}

		pwrbtn_sm_handle(ts);
	}
}