blob: 88471d3edf253e432458a44fb0c8d5ed29317d47 [file] [log] [blame]
Rakesh Iyer6642a682012-04-17 09:01:35 +00001/*
2 * (C) Copyright 2011
3 * NVIDIA Corporation <www.nvidia.com>
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24#include <common.h>
25#include <fdtdec.h>
26#include <input.h>
27#include <key_matrix.h>
28#include <stdio_dev.h>
29#include <tegra-kbc.h>
30#include <asm/io.h>
31#include <asm/arch/clock.h>
32#include <asm/arch/funcmux.h>
Tom Warren150c2492012-09-19 15:50:56 -070033#include <asm/arch-tegra/timer.h>
Rakesh Iyer6642a682012-04-17 09:01:35 +000034#include <linux/input.h>
35
36DECLARE_GLOBAL_DATA_PTR;
37
38enum {
39 KBC_MAX_GPIO = 24,
40 KBC_MAX_KPENT = 8, /* size of keypress entry queue */
41};
42
43#define KBC_FIFO_TH_CNT_SHIFT 14
44#define KBC_DEBOUNCE_CNT_SHIFT 4
45#define KBC_CONTROL_FIFO_CNT_INT_EN (1 << 3)
46#define KBC_CONTROL_KBC_EN (1 << 0)
47#define KBC_INT_FIFO_CNT_INT_STATUS (1 << 2)
48#define KBC_KPENT_VALID (1 << 7)
49#define KBC_ST_STATUS (1 << 3)
50
51enum {
52 KBC_DEBOUNCE_COUNT = 2,
53 KBC_REPEAT_RATE_MS = 30,
54 KBC_REPEAT_DELAY_MS = 240,
55 KBC_CLOCK_KHZ = 32, /* Keyboard uses a 32KHz clock */
56};
57
58/* keyboard controller config and state */
59static struct keyb {
60 struct input_config input; /* The input layer */
61 struct key_matrix matrix; /* The key matrix layer */
62
63 struct kbc_tegra *kbc; /* tegra keyboard controller */
64 unsigned char inited; /* 1 if keyboard has been inited */
65 unsigned char first_scan; /* 1 if this is our first key scan */
Allen Martin1ed0b512012-11-01 13:41:16 +000066 unsigned char created; /* 1 if driver has been created */
Rakesh Iyer6642a682012-04-17 09:01:35 +000067
68 /*
69 * After init we must wait a short time before polling the keyboard.
70 * This gives the tegra keyboard controller time to react after reset
71 * and lets us grab keys pressed during reset.
72 */
73 unsigned int init_dly_ms; /* Delay before we can read keyboard */
74 unsigned int start_time_ms; /* Time that we inited (in ms) */
75 unsigned int last_poll_ms; /* Time we should last polled */
76 unsigned int next_repeat_ms; /* Next time we repeat a key */
77} config;
78
79/**
80 * reads the keyboard fifo for current keypresses
81 *
82 * @param config Keyboard config
83 * @param fifo Place to put fifo results
84 * @param max_keycodes Maximum number of key codes to put in the fifo
85 * @return number of items put into fifo
86 */
87static int tegra_kbc_find_keys(struct keyb *config, int *fifo,
88 int max_keycodes)
89{
90 struct key_matrix_key keys[KBC_MAX_KPENT], *key;
91 u32 kp_ent = 0;
92 int i;
93
94 for (key = keys, i = 0; i < KBC_MAX_KPENT; i++, key++) {
95 /* Get next word */
96 if (!(i & 3))
97 kp_ent = readl(&config->kbc->kp_ent[i / 4]);
98
99 key->valid = (kp_ent & KBC_KPENT_VALID) != 0;
100 key->row = (kp_ent >> 3) & 0xf;
101 key->col = kp_ent & 0x7;
102
103 /* Shift to get next entry */
104 kp_ent >>= 8;
105 }
106 return key_matrix_decode(&config->matrix, keys, KBC_MAX_KPENT, fifo,
107 max_keycodes);
108}
109
110/**
111 * Process all the keypress sequences in fifo and send key codes
112 *
113 * The fifo contains zero or more keypress sets. Each set
114 * consists of from 1-8 keycodes, representing the keycodes which
115 * were simultaneously pressed during that scan.
116 *
117 * This function works through each set and generates ASCII characters
118 * for each. Not that one set may produce more than one ASCII characters -
119 * for example holding down 'd' and 'f' at the same time will generate
120 * two ASCII characters.
121 *
122 * Note: if fifo_cnt is 0, we will tell the input layer that no keys are
123 * pressed.
124 *
125 * @param config Keyboard config
126 * @param fifo_cnt Number of entries in the keyboard fifo
127 */
128static void process_fifo(struct keyb *config, int fifo_cnt)
129{
130 int fifo[KBC_MAX_KPENT];
131 int cnt = 0;
132
133 /* Always call input_send_keycodes() at least once */
134 do {
135 if (fifo_cnt)
136 cnt = tegra_kbc_find_keys(config, fifo, KBC_MAX_KPENT);
137
138 input_send_keycodes(&config->input, fifo, cnt);
139 } while (--fifo_cnt > 0);
140}
141
142/**
143 * Check the keyboard controller and emit ASCII characters for any keys that
144 * are pressed.
145 *
146 * @param config Keyboard config
147 */
148static void check_for_keys(struct keyb *config)
149{
150 int fifo_cnt;
151
152 if (!config->first_scan &&
153 get_timer(config->last_poll_ms) < KBC_REPEAT_RATE_MS)
154 return;
155 config->last_poll_ms = get_timer(0);
156 config->first_scan = 0;
157
158 /*
159 * Once we get here we know the keyboard has been scanned. So if there
160 * scan waiting for us, we know that nothing is held down.
161 */
162 fifo_cnt = (readl(&config->kbc->interrupt) >> 4) & 0xf;
163 process_fifo(config, fifo_cnt);
164}
165
166/**
167 * In order to detect keys pressed on boot, wait for the hardware to
168 * complete scanning the keys. This includes time to transition from
169 * Wkup mode to Continous polling mode and the repoll time. We can
170 * deduct the time that's already elapsed.
171 *
172 * @param config Keyboard config
173 */
174static void kbd_wait_for_fifo_init(struct keyb *config)
175{
176 if (!config->inited) {
177 unsigned long elapsed_time;
178 long delay_ms;
179
180 elapsed_time = get_timer(config->start_time_ms);
181 delay_ms = config->init_dly_ms - elapsed_time;
182 if (delay_ms > 0) {
183 udelay(delay_ms * 1000);
184 debug("%s: delay %ldms\n", __func__, delay_ms);
185 }
186
187 config->inited = 1;
188 }
189}
190
191/**
192 * Check the tegra keyboard, and send any keys that are pressed.
193 *
194 * This is called by input_tstc() and input_getc() when they need more
195 * characters
196 *
197 * @param input Input configuration
198 * @return 1, to indicate that we have something to look at
199 */
200int tegra_kbc_check(struct input_config *input)
201{
202 kbd_wait_for_fifo_init(&config);
203 check_for_keys(&config);
204
205 return 1;
206}
207
208/**
209 * Test if keys are available to be read
210 *
211 * @return 0 if no keys available, 1 if keys are available
212 */
213static int kbd_tstc(void)
214{
215 /* Just get input to do this for us */
216 return input_tstc(&config.input);
217}
218
219/**
220 * Read a key
221 *
222 * TODO: U-Boot wants 0 for no key, but Ctrl-@ is a valid key...
223 *
224 * @return ASCII key code, or 0 if no key, or -1 if error
225 */
226static int kbd_getc(void)
227{
228 /* Just get input to do this for us */
229 return input_getc(&config.input);
230}
231
232/* configures keyboard GPIO registers to use the rows and columns */
233static void config_kbc_gpio(struct kbc_tegra *kbc)
234{
235 int i;
236
237 for (i = 0; i < KBC_MAX_GPIO; i++) {
238 u32 row_cfg, col_cfg;
239 u32 r_shift = 5 * (i % 6);
240 u32 c_shift = 4 * (i % 8);
241 u32 r_mask = 0x1f << r_shift;
242 u32 c_mask = 0xf << c_shift;
243 u32 r_offs = i / 6;
244 u32 c_offs = i / 8;
245
246 row_cfg = readl(&kbc->row_cfg[r_offs]);
247 col_cfg = readl(&kbc->col_cfg[c_offs]);
248
249 row_cfg &= ~r_mask;
250 col_cfg &= ~c_mask;
251
252 if (i < config.matrix.num_rows) {
253 row_cfg |= ((i << 1) | 1) << r_shift;
254 } else {
255 col_cfg |= (((i - config.matrix.num_rows) << 1) | 1)
256 << c_shift;
257 }
258
259 writel(row_cfg, &kbc->row_cfg[r_offs]);
260 writel(col_cfg, &kbc->col_cfg[c_offs]);
261 }
262}
263
264/**
265 * Start up the keyboard device
266 */
267static void tegra_kbc_open(void)
268{
269 struct kbc_tegra *kbc = config.kbc;
270 unsigned int scan_period;
271 u32 val;
272
273 /*
274 * We will scan at twice the keyboard repeat rate, so that there is
275 * always a scan ready when we check it in check_for_keys().
276 */
277 scan_period = KBC_REPEAT_RATE_MS / 2;
278 writel(scan_period * KBC_CLOCK_KHZ, &kbc->rpt_dly);
279 writel(scan_period * KBC_CLOCK_KHZ, &kbc->init_dly);
280 /*
281 * Before reading from the keyboard we must wait for the init_dly
282 * plus the rpt_delay, plus 2ms for the row scan time.
283 */
284 config.init_dly_ms = scan_period * 2 + 2;
285
286 val = KBC_DEBOUNCE_COUNT << KBC_DEBOUNCE_CNT_SHIFT;
287 val |= 1 << KBC_FIFO_TH_CNT_SHIFT; /* fifo interrupt threshold */
288 val |= KBC_CONTROL_KBC_EN; /* enable */
289 writel(val, &kbc->control);
290
291 config.start_time_ms = get_timer(0);
292 config.last_poll_ms = config.next_repeat_ms = get_timer(0);
293 config.first_scan = 1;
294}
295
296/**
297 * Set up the tegra keyboard. This is called by the stdio device handler
298 *
299 * We want to do this init when the keyboard is actually used rather than
300 * at start-up, since keyboard input may not currently be selected.
301 *
302 * Once the keyboard starts there will be a period during which we must
303 * wait for the keyboard to init. We do this only when a key is first
304 * read - see kbd_wait_for_fifo_init().
305 *
306 * @return 0 if ok, -ve on error
307 */
308static int init_tegra_keyboard(void)
309{
Allen Martin1ed0b512012-11-01 13:41:16 +0000310 /* check if already created */
311 if (config.created)
312 return 0;
313
Rakesh Iyer6642a682012-04-17 09:01:35 +0000314#ifdef CONFIG_OF_CONTROL
315 int node;
316
317 node = fdtdec_next_compatible(gd->fdt_blob, 0,
318 COMPAT_NVIDIA_TEGRA20_KBC);
319 if (node < 0) {
320 debug("%s: cannot locate keyboard node\n", __func__);
321 return node;
322 }
323 config.kbc = (struct kbc_tegra *)fdtdec_get_addr(gd->fdt_blob,
324 node, "reg");
325 if ((fdt_addr_t)config.kbc == FDT_ADDR_T_NONE) {
326 debug("%s: No keyboard register found\n", __func__);
327 return -1;
328 }
Simon Glass1b1d3e62012-09-27 15:18:41 +0000329 input_set_delays(&config.input, KBC_REPEAT_DELAY_MS,
330 KBC_REPEAT_RATE_MS);
Rakesh Iyer6642a682012-04-17 09:01:35 +0000331
332 /* Decode the keyboard matrix information (16 rows, 8 columns) */
Simon Glass71dc6bc2012-09-27 15:18:42 +0000333 if (key_matrix_init(&config.matrix, 16, 8, 1)) {
Rakesh Iyer6642a682012-04-17 09:01:35 +0000334 debug("%s: Could not init key matrix\n", __func__);
335 return -1;
336 }
337 if (key_matrix_decode_fdt(&config.matrix, gd->fdt_blob, node)) {
338 debug("%s: Could not decode key matrix from fdt\n", __func__);
339 return -1;
340 }
341 if (config.matrix.fn_keycode) {
342 if (input_add_table(&config.input, KEY_FN, -1,
343 config.matrix.fn_keycode,
344 config.matrix.key_count))
345 return -1;
346 }
347#else
348#error "Tegra keyboard driver requires FDT definitions"
349#endif
350
351 /* Set up pin mux and enable the clock */
352 funcmux_select(PERIPH_ID_KBC, FUNCMUX_DEFAULT);
353 clock_enable(PERIPH_ID_KBC);
354 config_kbc_gpio(config.kbc);
355
356 tegra_kbc_open();
Allen Martin1ed0b512012-11-01 13:41:16 +0000357 config.created = 1;
Rakesh Iyer6642a682012-04-17 09:01:35 +0000358 debug("%s: Tegra keyboard ready\n", __func__);
359
360 return 0;
361}
362
363int drv_keyboard_init(void)
364{
365 struct stdio_dev dev;
Allen Martin1ed0b512012-11-01 13:41:16 +0000366 char *stdinname = getenv("stdin");
367 int error;
Rakesh Iyer6642a682012-04-17 09:01:35 +0000368
Simon Glass1b1d3e62012-09-27 15:18:41 +0000369 if (input_init(&config.input, 0)) {
Rakesh Iyer6642a682012-04-17 09:01:35 +0000370 debug("%s: Cannot set up input\n", __func__);
371 return -1;
372 }
373 config.input.read_keys = tegra_kbc_check;
374
375 memset(&dev, '\0', sizeof(dev));
376 strcpy(dev.name, "tegra-kbc");
377 dev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
378 dev.getc = kbd_getc;
379 dev.tstc = kbd_tstc;
380 dev.start = init_tegra_keyboard;
381
382 /* Register the device. init_tegra_keyboard() will be called soon */
Allen Martin1ed0b512012-11-01 13:41:16 +0000383 error = input_stdio_register(&dev);
384 if (error)
385 return error;
386#ifdef CONFIG_CONSOLE_MUX
387 error = iomux_doenv(stdin, stdinname);
388 if (error)
389 return error;
390#endif
391 return 0;
Rakesh Iyer6642a682012-04-17 09:01:35 +0000392}