summaryrefslogtreecommitdiff
path: root/chip/lm4/keyboard_scan.c
blob: f7a5872a163f9da06e1855a6addf11dfba4c0059 (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
/* Copyright (c) 2011 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.
 */

/* Keyboard scanner module for Chrome EC */

#include "console.h"
#include "keyboard.h"
#include "keyboard_scan.h"
#include "registers.h"
#include "task.h"
#include "timer.h"
#include "uart.h"
#include "util.h"


/* Notes:
 *
 * EVT board:
 *
 *   Columns (outputs):
 *      KSO0 - KSO7  = PP0:7
 *      KSO8 - KSO12 = PQ0:4
 *
 *   Rows (inputs):
 *      KSI0 - KSI7  = PN0:7
 *
 *   Other:
 *      PWR_BTN#     = PK7
 *
 *
 * Hacked board:
 *
 *   Columns (outputs):
 *      KSO0 - KSO7  = PQ0:7
 *      KSO8 - KSO11 = PK0:3
 *      KSO12        = PN2
 *   Rows (inputs):
 *      KSI0 - KSI7  = PH0:7
 *   Other:
 *      PWR_BTN#     = PC5
 */


/* used for select_column() */
enum COLUMN_INDEX {
	COLUMN_ASSERT_ALL = -2,
	COLUMN_TRI_STATE_ALL = -1,
	/* 0 ~ 12 for the corresponding column */
};

#define POLLING_MODE_TIMEOUT 1000000  /* 1 sec */
#define SCAN_LOOP_DELAY 10000         /* 10 ms */

#undef EVT  /* FIXME: define this for EVT board. */

#define KB_COLS 13

static uint8_t raw_state[KB_COLS];

/* Mask with 1 bits only for keys that actually exist */
static const uint8_t *actual_key_mask;

/* All actual key masks (todo: move to keyboard matrix definition */
/* TODO: (crosbug.com/p/7485) fill in real key mask with 0-bits for coords that
   aren't keys */
static const uint8_t actual_key_masks[4][KB_COLS] = {
	{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
	{0},
	{0},
	{0},
	};

/* Drives the specified column low; other columns are tri-stated */
static void select_column(int col)
{
#if defined(EVT)
	if (col == COLUMN_ASSERT_ALL) {
		LM4_GPIO_DIR(LM4_GPIO_P) = 0xff;
		LM4_GPIO_DIR(LM4_GPIO_Q) |= 0x1f;
		LM4_GPIO_DATA(LM4_GPIO_P, 0xff) = 0;
		LM4_GPIO_DATA(LM4_GPIO_Q, 0xff) &= ~0x1f;
	} else if (col == COLUMN_TRI_STATE_ALL) {
		LM4_GPIO_DIR(LM4_GPIO_P) &= ~0xff;
		LM4_GPIO_DIR(LM4_GPIO_Q) &= ~0x1f;
	} else if (col < 8) {
		LM4_GPIO_DIR(LM4_GPIO_P) &= ~0xff;
		LM4_GPIO_DIR(LM4_GPIO_Q) &= ~0x1f;
		LM4_GPIO_DATA(LM4_GPIO_P, 0xff) = ~(1 << col);
		LM4_GPIO_DIR(LM4_GPIO_P) = (1 << col) & 0xff;
	} else {
		LM4_GPIO_DIR(LM4_GPIO_P) &= ~0xff;
		LM4_GPIO_DIR(LM4_GPIO_Q) &= ~0x1f;
		LM4_GPIO_DATA(LM4_GPIO_Q, 0xff) = ~(1 << (col - 8));
		LM4_GPIO_DIR(LM4_GPIO_Q) |= 1 << (col - 8);
	}
#else  /* BDS definition */
	/* Somehow the col 10 and 11 are swapped on bds. */
	if (col == 10) {
		col = 11;
	} else if (col == 11) {
		col = 10;
	}

	if (col == COLUMN_ASSERT_ALL) {
		LM4_GPIO_DIR(LM4_GPIO_Q) = 0xff;
		LM4_GPIO_DIR(LM4_GPIO_K) |= 0x0f;
		LM4_GPIO_DIR(LM4_GPIO_N) |= 0x04;
		LM4_GPIO_DATA(LM4_GPIO_Q, 0xff) = 0;
		LM4_GPIO_DATA(LM4_GPIO_K, 0xff) &= ~0x0f;
		LM4_GPIO_DATA(LM4_GPIO_N, 0xff) &= ~0x04;
	} else if (col == COLUMN_TRI_STATE_ALL) {
		/* All tri-stated */
		LM4_GPIO_DIR(LM4_GPIO_Q) = 0;
		LM4_GPIO_DIR(LM4_GPIO_K) &= ~0x0f;
		LM4_GPIO_DIR(LM4_GPIO_N) &= ~0x04;
	} else if (col < 8) {
		LM4_GPIO_DIR(LM4_GPIO_Q) = 1 << col;
		LM4_GPIO_DIR(LM4_GPIO_K) &= ~0x0f;
		LM4_GPIO_DIR(LM4_GPIO_N) &= ~0x04;
		LM4_GPIO_DATA(LM4_GPIO_Q, 0xff) = ~(1 << col);
	} else if (col < 12) {
		LM4_GPIO_DIR(LM4_GPIO_Q) = 0;
		LM4_GPIO_DIR(LM4_GPIO_K) = (LM4_GPIO_DIR(LM4_GPIO_K) & ~0x0f) |
			(1 << (col - 8));
		LM4_GPIO_DIR(LM4_GPIO_N) &= ~0x04;
		LM4_GPIO_DATA(LM4_GPIO_K, 0x0f) = ~(1 << (col - 8));
	} else {  /* col == 12 */
		LM4_GPIO_DIR(LM4_GPIO_Q) = 0;
		LM4_GPIO_DIR(LM4_GPIO_K) &= ~0x0f;
		LM4_GPIO_DIR(LM4_GPIO_N) |= 0x04;
		LM4_GPIO_DATA(LM4_GPIO_N, 0x04) = ~0x04;
	}
#endif
}

int keyboard_scan_init(void)
{
	volatile uint32_t scratch  __attribute__((unused));
	int i;

        /* Enable GPIOs */
#if defined(EVT)
	/* Enable clock to GPIO modules C,H,K,N,P,Q */
	LM4_SYSTEM_RCGCGPIO |= 0x7284;
#else
	/* Enable clock to GPIO modules C,H,K,N,Q */
	LM4_SYSTEM_RCGCGPIO |= 0x5284;
#endif
	scratch = LM4_SYSTEM_RCGCGPIO;

	/* Clear GPIOAFSEL and enable digital function for PC5, PH0:7,
         * PK0:3, PN2, PQ0:7. */
	LM4_GPIO_AFSEL(LM4_GPIO_C) &= ~0x20;
	LM4_GPIO_DEN(LM4_GPIO_C) |= 0x20;
#if defined(EVT)
	LM4_GPIO_AFSEL(LM4_GPIO_N) &= 0xff;  /* KSI[7:0] */
	LM4_GPIO_DEN(LM4_GPIO_N) |= 0xff;
	LM4_GPIO_AFSEL(LM4_GPIO_P) &= 0xff;  /* KSO[7:0] */
	LM4_GPIO_DEN(LM4_GPIO_P) |= 0xff;
	LM4_GPIO_AFSEL(LM4_GPIO_Q) &= 0x1f;  /* KSO[12:8] */
	LM4_GPIO_DEN(LM4_GPIO_Q) |= 0x1f;
#else
	LM4_GPIO_AFSEL(LM4_GPIO_H) = 0;      /* KSI[7:0] */
	LM4_GPIO_DEN(LM4_GPIO_H) = 0xff;
	LM4_GPIO_AFSEL(LM4_GPIO_K) &= ~0x0f;
	LM4_GPIO_DEN(LM4_GPIO_K) |= 0x0f;
	LM4_GPIO_AFSEL(LM4_GPIO_N) &= ~0x04;
	LM4_GPIO_DEN(LM4_GPIO_N) |= 0x04;
	LM4_GPIO_AFSEL(LM4_GPIO_Q) = 0;
	LM4_GPIO_DEN(LM4_GPIO_Q) = 0xff;
#endif

#if defined(EVT)
	/* Set PN0:7 as inputs with pull-up */
	LM4_GPIO_DIR(LM4_GPIO_N) = 0;
	LM4_GPIO_PUR(LM4_GPIO_N) = 0xff;
#else
	/* Set PH0:7 as inputs with pull-up */
	LM4_GPIO_DIR(LM4_GPIO_H) = 0;
	LM4_GPIO_PUR(LM4_GPIO_H) = 0xff;
#endif

	/* Set PC5 as input with pull-up. */
	/* TODO: no need for pull-up on real circuit, since it'll be
	 * externally pulled up. */
	LM4_GPIO_DIR(LM4_GPIO_C) &= ~0x04;
	LM4_GPIO_PUR(LM4_GPIO_C) |= 0x04;

	/* Tri-state the columns */
	select_column(COLUMN_TRI_STATE_ALL);

	/* Initialize raw state */
	for (i = 0; i < KB_COLS; i++)
		raw_state[i] = 0;

	/* TODO: method to set which keyboard we have, so we set the actual
	 * key mask properly */
	actual_key_mask = actual_key_masks[0];

	return EC_SUCCESS;
}


static uint32_t clear_matrix_interrupt_status(void) {
#if defined(EVT)
	uint32_t port = LM4_GPIO_N;
#else
	uint32_t port = LM4_GPIO_H;
#endif
	uint32_t ris = LM4_GPIO_RIS(port);
	LM4_GPIO_ICR(port) = ris;

	return ris;
}


void wait_for_interrupt(void)
{
	uart_printf("Enter %s() ...\n", __func__);

	/* Assert all outputs would trigger un-wanted interrupts.
	 * Clear them before enable interrupt. */
	select_column(COLUMN_ASSERT_ALL);
	clear_matrix_interrupt_status();

#if defined(EVT)
	LM4_GPIO_IS(LM4_GPIO_N) = 0;      /* 0: edge-sensitive */
	LM4_GPIO_IBE(LM4_GPIO_N) = 0xff;  /* 1: both edge */
	LM4_GPIO_IM(LM4_GPIO_N) = 0xff;   /* 1: enable interrupt */
#else
	LM4_GPIO_IS(LM4_GPIO_H) = 0;      /* 0: edge-sensitive */
	LM4_GPIO_IBE(LM4_GPIO_H) = 0xff;  /* 1: both edge */
	LM4_GPIO_IM(LM4_GPIO_H) = 0xff;   /* 1: enable interrupt */
#endif
}


void enter_polling_mode(void)
{
	uart_printf("Enter %s() ...\n", __func__);
#if defined(EVT)
	LM4_GPIO_IM(LM4_GPIO_N) = 0;  /* 0: disable interrupt */
#else
	LM4_GPIO_IM(LM4_GPIO_H) = 0;  /* 0: disable interrupt */
#endif
	select_column(COLUMN_TRI_STATE_ALL);
}


/* Returns 1 if any key is still pressed. 0 if no key is pressed. */
static int check_keys_changed(void)
{
	int c;
	uint8_t r;
	int change = 0;
	int num_press = 0;

	for (c = 0; c < KB_COLS; c++) {
		/* Select column, then wait a bit for it to settle */
		select_column(c);
		usleep(20);
		/* Read the row state */
#if defined(EVT)
		r = LM4_GPIO_DATA(LM4_GPIO_N, 0xff);
#else
		r = LM4_GPIO_DATA(LM4_GPIO_H, 0xff);
#endif
		/* Invert it so 0=not pressed, 1=pressed */
		r ^= 0xff;
		/* Mask off keys that don't exist so they never show
		 * as pressed */
		r &= actual_key_mask[c];

#ifdef OR_WITH_CURRENT_STATE_FOR_TESTING
		/* KLUDGE - or current state in, so we can make sure
		 * all the lines are hooked up */
		r |= raw_state[c];
#endif

		/* Check for changes */
		if (r != raw_state[c]) {
			int i;
			for (i = 0; i < 8; ++i) {
				uint8_t prev = (raw_state[c] >> i) & 1;
				uint8_t now = (r >> i) & 1;
				if (prev != now) {
					keyboard_state_changed(i, c, now);
				}
			}
			raw_state[c] = r;
			change = 1;
		}
	}
	select_column(COLUMN_TRI_STATE_ALL);

	if (change) {
		uart_puts("[Keyboard state:");
		for (c = 0; c < KB_COLS; c++) {
			if (raw_state[c]) {
				uart_printf(" %02x", raw_state[c]);
			} else {
				uart_puts(" --");
			}
		}
		uart_puts("]\n");
	}

	/* Count number of key pressed */
	for (c = 0; c < KB_COLS; c++) {
		if (raw_state[c]) ++num_press;
	}

	return num_press ? 1 : 0;
}


void keyboard_scan_task(void)
{
	int key_press_timer = 0;

	keyboard_scan_init();

	while (1) {
		wait_for_interrupt();
		task_wait_msg(-1);

		enter_polling_mode();
		/* Busy polling keyboard state. */
		while (1) {
			/* sleep for debounce. */
			usleep(SCAN_LOOP_DELAY);
			/* Check for keys down */
			if (check_keys_changed()) {
				key_press_timer = 0;
			} else {
				if (++key_press_timer >=
				    (POLLING_MODE_TIMEOUT / SCAN_LOOP_DELAY)) {
					key_press_timer = 0;
					break;  /* exit the while loop */
				}
			}
		}
		/* TODO: (crosbug.com/p/7484) A race condition here.
		 *       If a key state is changed here (before interrupt is
		 *       enabled), it will be lost.
		 */
	}
}



static void matrix_interrupt(void)
{
	uint32_t ris = clear_matrix_interrupt_status();

	if (ris) {
		task_send_msg(TASK_ID_KEYSCAN, TASK_ID_KEYSCAN, 0);
	}
}

#if defined(EVT)
DECLARE_IRQ(LM4_IRQ_GPION, matrix_interrupt, 3);
#else
DECLARE_IRQ(LM4_IRQ_GPIOH, matrix_interrupt, 3);
#endif