blob: b77725db4bea471a9e00191d57ac11d9b4c992e9 [file] [log] [blame]
Rakesh Iyer11f5b302011-01-19 23:38:47 -08001/*
2 * Keyboard class input driver for the NVIDIA Tegra SoC internal matrix
3 * keyboard controller
4 *
5 * Copyright (c) 2009-2011, NVIDIA Corporation.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21
Rakesh Iyer3f277572011-07-30 12:01:48 -070022#include <linux/kernel.h>
Rakesh Iyer11f5b302011-01-19 23:38:47 -080023#include <linux/module.h>
24#include <linux/input.h>
25#include <linux/platform_device.h>
26#include <linux/delay.h>
27#include <linux/io.h>
28#include <linux/interrupt.h>
Olof Johanssona445c7f2011-12-29 09:58:16 -080029#include <linux/of.h>
Rakesh Iyer11f5b302011-01-19 23:38:47 -080030#include <linux/clk.h>
31#include <linux/slab.h>
32#include <mach/clk.h>
33#include <mach/kbc.h>
34
35#define KBC_MAX_DEBOUNCE_CNT 0x3ffu
36
37/* KBC row scan time and delay for beginning the row scan. */
38#define KBC_ROW_SCAN_TIME 16
39#define KBC_ROW_SCAN_DLY 5
40
41/* KBC uses a 32KHz clock so a cycle = 1/32Khz */
Rakesh Iyer3f277572011-07-30 12:01:48 -070042#define KBC_CYCLE_MS 32
Rakesh Iyer11f5b302011-01-19 23:38:47 -080043
44/* KBC Registers */
45
46/* KBC Control Register */
47#define KBC_CONTROL_0 0x0
48#define KBC_FIFO_TH_CNT_SHIFT(cnt) (cnt << 14)
49#define KBC_DEBOUNCE_CNT_SHIFT(cnt) (cnt << 4)
50#define KBC_CONTROL_FIFO_CNT_INT_EN (1 << 3)
51#define KBC_CONTROL_KBC_EN (1 << 0)
52
53/* KBC Interrupt Register */
54#define KBC_INT_0 0x4
55#define KBC_INT_FIFO_CNT_INT_STATUS (1 << 2)
56
57#define KBC_ROW_CFG0_0 0x8
58#define KBC_COL_CFG0_0 0x18
Rakesh Iyerd0d150e2011-09-08 15:34:11 -070059#define KBC_TO_CNT_0 0x24
Rakesh Iyer11f5b302011-01-19 23:38:47 -080060#define KBC_INIT_DLY_0 0x28
61#define KBC_RPT_DLY_0 0x2c
62#define KBC_KP_ENT0_0 0x30
63#define KBC_KP_ENT1_0 0x34
64#define KBC_ROW0_MASK_0 0x38
65
66#define KBC_ROW_SHIFT 3
67
68struct tegra_kbc {
69 void __iomem *mmio;
70 struct input_dev *idev;
71 unsigned int irq;
Rakesh Iyer11f5b302011-01-19 23:38:47 -080072 spinlock_t lock;
73 unsigned int repoll_dly;
74 unsigned long cp_dly_jiffies;
Rakesh Iyerd0d150e2011-09-08 15:34:11 -070075 unsigned int cp_to_wkup_dly;
Rakesh Iyer4e8b65f2011-02-18 08:38:02 -080076 bool use_fn_map;
Rakesh Iyer34abeeb2011-04-27 23:18:15 -070077 bool use_ghost_filter;
Rakesh Iyer11f5b302011-01-19 23:38:47 -080078 const struct tegra_kbc_platform_data *pdata;
Rakesh Iyer4e8b65f2011-02-18 08:38:02 -080079 unsigned short keycode[KBC_MAX_KEY * 2];
Rakesh Iyer11f5b302011-01-19 23:38:47 -080080 unsigned short current_keys[KBC_MAX_KPENT];
81 unsigned int num_pressed_keys;
82 struct timer_list timer;
83 struct clk *clk;
84};
85
Olof Johanssona445c7f2011-12-29 09:58:16 -080086static const u32 tegra_kbc_default_keymap[] __devinitdata = {
Rakesh Iyer11f5b302011-01-19 23:38:47 -080087 KEY(0, 2, KEY_W),
88 KEY(0, 3, KEY_S),
89 KEY(0, 4, KEY_A),
90 KEY(0, 5, KEY_Z),
91 KEY(0, 7, KEY_FN),
92
Rakesh Iyere7acc842011-01-28 22:05:14 -080093 KEY(1, 7, KEY_LEFTMETA),
Rakesh Iyer11f5b302011-01-19 23:38:47 -080094
95 KEY(2, 6, KEY_RIGHTALT),
96 KEY(2, 7, KEY_LEFTALT),
97
98 KEY(3, 0, KEY_5),
99 KEY(3, 1, KEY_4),
100 KEY(3, 2, KEY_R),
101 KEY(3, 3, KEY_E),
102 KEY(3, 4, KEY_F),
103 KEY(3, 5, KEY_D),
104 KEY(3, 6, KEY_X),
105
106 KEY(4, 0, KEY_7),
107 KEY(4, 1, KEY_6),
108 KEY(4, 2, KEY_T),
109 KEY(4, 3, KEY_H),
110 KEY(4, 4, KEY_G),
111 KEY(4, 5, KEY_V),
112 KEY(4, 6, KEY_C),
113 KEY(4, 7, KEY_SPACE),
114
115 KEY(5, 0, KEY_9),
116 KEY(5, 1, KEY_8),
117 KEY(5, 2, KEY_U),
118 KEY(5, 3, KEY_Y),
119 KEY(5, 4, KEY_J),
120 KEY(5, 5, KEY_N),
121 KEY(5, 6, KEY_B),
122 KEY(5, 7, KEY_BACKSLASH),
123
124 KEY(6, 0, KEY_MINUS),
125 KEY(6, 1, KEY_0),
126 KEY(6, 2, KEY_O),
127 KEY(6, 3, KEY_I),
128 KEY(6, 4, KEY_L),
129 KEY(6, 5, KEY_K),
130 KEY(6, 6, KEY_COMMA),
131 KEY(6, 7, KEY_M),
132
133 KEY(7, 1, KEY_EQUAL),
134 KEY(7, 2, KEY_RIGHTBRACE),
135 KEY(7, 3, KEY_ENTER),
136 KEY(7, 7, KEY_MENU),
137
138 KEY(8, 4, KEY_RIGHTSHIFT),
139 KEY(8, 5, KEY_LEFTSHIFT),
140
141 KEY(9, 5, KEY_RIGHTCTRL),
142 KEY(9, 7, KEY_LEFTCTRL),
143
144 KEY(11, 0, KEY_LEFTBRACE),
145 KEY(11, 1, KEY_P),
146 KEY(11, 2, KEY_APOSTROPHE),
147 KEY(11, 3, KEY_SEMICOLON),
148 KEY(11, 4, KEY_SLASH),
149 KEY(11, 5, KEY_DOT),
150
151 KEY(12, 0, KEY_F10),
152 KEY(12, 1, KEY_F9),
153 KEY(12, 2, KEY_BACKSPACE),
154 KEY(12, 3, KEY_3),
155 KEY(12, 4, KEY_2),
156 KEY(12, 5, KEY_UP),
157 KEY(12, 6, KEY_PRINT),
158 KEY(12, 7, KEY_PAUSE),
159
160 KEY(13, 0, KEY_INSERT),
161 KEY(13, 1, KEY_DELETE),
162 KEY(13, 3, KEY_PAGEUP),
163 KEY(13, 4, KEY_PAGEDOWN),
164 KEY(13, 5, KEY_RIGHT),
165 KEY(13, 6, KEY_DOWN),
166 KEY(13, 7, KEY_LEFT),
167
168 KEY(14, 0, KEY_F11),
169 KEY(14, 1, KEY_F12),
170 KEY(14, 2, KEY_F8),
171 KEY(14, 3, KEY_Q),
172 KEY(14, 4, KEY_F4),
173 KEY(14, 5, KEY_F3),
174 KEY(14, 6, KEY_1),
175 KEY(14, 7, KEY_F7),
176
177 KEY(15, 0, KEY_ESC),
178 KEY(15, 1, KEY_GRAVE),
179 KEY(15, 2, KEY_F5),
180 KEY(15, 3, KEY_TAB),
181 KEY(15, 4, KEY_F1),
182 KEY(15, 5, KEY_F2),
183 KEY(15, 6, KEY_CAPSLOCK),
184 KEY(15, 7, KEY_F6),
Rakesh Iyer4e8b65f2011-02-18 08:38:02 -0800185
186 /* Software Handled Function Keys */
187 KEY(20, 0, KEY_KP7),
188
189 KEY(21, 0, KEY_KP9),
190 KEY(21, 1, KEY_KP8),
191 KEY(21, 2, KEY_KP4),
192 KEY(21, 4, KEY_KP1),
193
194 KEY(22, 1, KEY_KPSLASH),
195 KEY(22, 2, KEY_KP6),
196 KEY(22, 3, KEY_KP5),
197 KEY(22, 4, KEY_KP3),
198 KEY(22, 5, KEY_KP2),
199 KEY(22, 7, KEY_KP0),
200
201 KEY(27, 1, KEY_KPASTERISK),
202 KEY(27, 3, KEY_KPMINUS),
203 KEY(27, 4, KEY_KPPLUS),
204 KEY(27, 5, KEY_KPDOT),
205
206 KEY(28, 5, KEY_VOLUMEUP),
207
208 KEY(29, 3, KEY_HOME),
209 KEY(29, 4, KEY_END),
210 KEY(29, 5, KEY_BRIGHTNESSDOWN),
211 KEY(29, 6, KEY_VOLUMEDOWN),
212 KEY(29, 7, KEY_BRIGHTNESSUP),
213
214 KEY(30, 0, KEY_NUMLOCK),
215 KEY(30, 1, KEY_SCROLLLOCK),
216 KEY(30, 2, KEY_MUTE),
217
218 KEY(31, 4, KEY_HELP),
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800219};
220
Olof Johanssona445c7f2011-12-29 09:58:16 -0800221static const
222struct matrix_keymap_data tegra_kbc_default_keymap_data __devinitdata = {
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800223 .keymap = tegra_kbc_default_keymap,
224 .keymap_size = ARRAY_SIZE(tegra_kbc_default_keymap),
225};
226
227static void tegra_kbc_report_released_keys(struct input_dev *input,
228 unsigned short old_keycodes[],
229 unsigned int old_num_keys,
230 unsigned short new_keycodes[],
231 unsigned int new_num_keys)
232{
233 unsigned int i, j;
234
235 for (i = 0; i < old_num_keys; i++) {
236 for (j = 0; j < new_num_keys; j++)
237 if (old_keycodes[i] == new_keycodes[j])
238 break;
239
240 if (j == new_num_keys)
241 input_report_key(input, old_keycodes[i], 0);
242 }
243}
244
245static void tegra_kbc_report_pressed_keys(struct input_dev *input,
246 unsigned char scancodes[],
247 unsigned short keycodes[],
248 unsigned int num_pressed_keys)
249{
250 unsigned int i;
251
252 for (i = 0; i < num_pressed_keys; i++) {
253 input_event(input, EV_MSC, MSC_SCAN, scancodes[i]);
254 input_report_key(input, keycodes[i], 1);
255 }
256}
257
258static void tegra_kbc_report_keys(struct tegra_kbc *kbc)
259{
260 unsigned char scancodes[KBC_MAX_KPENT];
261 unsigned short keycodes[KBC_MAX_KPENT];
262 u32 val = 0;
263 unsigned int i;
264 unsigned int num_down = 0;
Rakesh Iyer4e8b65f2011-02-18 08:38:02 -0800265 bool fn_keypress = false;
Rakesh Iyer34abeeb2011-04-27 23:18:15 -0700266 bool key_in_same_row = false;
267 bool key_in_same_col = false;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800268
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800269 for (i = 0; i < KBC_MAX_KPENT; i++) {
270 if ((i % 4) == 0)
271 val = readl(kbc->mmio + KBC_KP_ENT0_0 + i);
272
273 if (val & 0x80) {
274 unsigned int col = val & 0x07;
275 unsigned int row = (val >> 3) & 0x0f;
276 unsigned char scancode =
277 MATRIX_SCAN_CODE(row, col, KBC_ROW_SHIFT);
278
279 scancodes[num_down] = scancode;
Rakesh Iyer4e8b65f2011-02-18 08:38:02 -0800280 keycodes[num_down] = kbc->keycode[scancode];
281 /* If driver uses Fn map, do not report the Fn key. */
282 if ((keycodes[num_down] == KEY_FN) && kbc->use_fn_map)
283 fn_keypress = true;
284 else
285 num_down++;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800286 }
287
288 val >>= 8;
289 }
Rakesh Iyer4e8b65f2011-02-18 08:38:02 -0800290
291 /*
Rakesh Iyer34abeeb2011-04-27 23:18:15 -0700292 * Matrix keyboard designs are prone to keyboard ghosting.
293 * Ghosting occurs if there are 3 keys such that -
294 * any 2 of the 3 keys share a row, and any 2 of them share a column.
295 * If so ignore the key presses for this iteration.
296 */
Dmitry Torokhov95439cb2011-09-09 09:24:20 -0700297 if (kbc->use_ghost_filter && num_down >= 3) {
Rakesh Iyer34abeeb2011-04-27 23:18:15 -0700298 for (i = 0; i < num_down; i++) {
299 unsigned int j;
300 u8 curr_col = scancodes[i] & 0x07;
301 u8 curr_row = scancodes[i] >> KBC_ROW_SHIFT;
302
303 /*
304 * Find 2 keys such that one key is in the same row
305 * and the other is in the same column as the i-th key.
306 */
307 for (j = i + 1; j < num_down; j++) {
308 u8 col = scancodes[j] & 0x07;
309 u8 row = scancodes[j] >> KBC_ROW_SHIFT;
310
311 if (col == curr_col)
312 key_in_same_col = true;
313 if (row == curr_row)
314 key_in_same_row = true;
315 }
316 }
317 }
318
319 /*
Rakesh Iyer4e8b65f2011-02-18 08:38:02 -0800320 * If the platform uses Fn keymaps, translate keys on a Fn keypress.
321 * Function keycodes are KBC_MAX_KEY apart from the plain keycodes.
322 */
323 if (fn_keypress) {
324 for (i = 0; i < num_down; i++) {
325 scancodes[i] += KBC_MAX_KEY;
326 keycodes[i] = kbc->keycode[scancodes[i]];
327 }
328 }
329
Rakesh Iyer34abeeb2011-04-27 23:18:15 -0700330 /* Ignore the key presses for this iteration? */
331 if (key_in_same_col && key_in_same_row)
332 return;
333
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800334 tegra_kbc_report_released_keys(kbc->idev,
335 kbc->current_keys, kbc->num_pressed_keys,
336 keycodes, num_down);
337 tegra_kbc_report_pressed_keys(kbc->idev, scancodes, keycodes, num_down);
338 input_sync(kbc->idev);
339
340 memcpy(kbc->current_keys, keycodes, sizeof(kbc->current_keys));
341 kbc->num_pressed_keys = num_down;
342}
343
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700344static void tegra_kbc_set_fifo_interrupt(struct tegra_kbc *kbc, bool enable)
345{
346 u32 val;
347
348 val = readl(kbc->mmio + KBC_CONTROL_0);
349 if (enable)
350 val |= KBC_CONTROL_FIFO_CNT_INT_EN;
351 else
352 val &= ~KBC_CONTROL_FIFO_CNT_INT_EN;
353 writel(val, kbc->mmio + KBC_CONTROL_0);
354}
355
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800356static void tegra_kbc_keypress_timer(unsigned long data)
357{
358 struct tegra_kbc *kbc = (struct tegra_kbc *)data;
359 unsigned long flags;
360 u32 val;
361 unsigned int i;
362
Dmitry Torokhov95439cb2011-09-09 09:24:20 -0700363 spin_lock_irqsave(&kbc->lock, flags);
364
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800365 val = (readl(kbc->mmio + KBC_INT_0) >> 4) & 0xf;
366 if (val) {
367 unsigned long dly;
368
369 tegra_kbc_report_keys(kbc);
370
371 /*
372 * If more than one keys are pressed we need not wait
373 * for the repoll delay.
374 */
375 dly = (val == 1) ? kbc->repoll_dly : 1;
376 mod_timer(&kbc->timer, jiffies + msecs_to_jiffies(dly));
377 } else {
378 /* Release any pressed keys and exit the polling loop */
379 for (i = 0; i < kbc->num_pressed_keys; i++)
380 input_report_key(kbc->idev, kbc->current_keys[i], 0);
381 input_sync(kbc->idev);
382
383 kbc->num_pressed_keys = 0;
384
385 /* All keys are released so enable the keypress interrupt */
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700386 tegra_kbc_set_fifo_interrupt(kbc, true);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800387 }
Dmitry Torokhov95439cb2011-09-09 09:24:20 -0700388
389 spin_unlock_irqrestore(&kbc->lock, flags);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800390}
391
392static irqreturn_t tegra_kbc_isr(int irq, void *args)
393{
394 struct tegra_kbc *kbc = args;
Dmitry Torokhov95439cb2011-09-09 09:24:20 -0700395 unsigned long flags;
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700396 u32 val;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800397
Dmitry Torokhov95439cb2011-09-09 09:24:20 -0700398 spin_lock_irqsave(&kbc->lock, flags);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800399
400 /*
401 * Quickly bail out & reenable interrupts if the fifo threshold
402 * count interrupt wasn't the interrupt source
403 */
404 val = readl(kbc->mmio + KBC_INT_0);
405 writel(val, kbc->mmio + KBC_INT_0);
406
407 if (val & KBC_INT_FIFO_CNT_INT_STATUS) {
408 /*
Dmitry Torokhov95439cb2011-09-09 09:24:20 -0700409 * Until all keys are released, defer further processing to
410 * the polling loop in tegra_kbc_keypress_timer.
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800411 */
Dmitry Torokhov95439cb2011-09-09 09:24:20 -0700412 tegra_kbc_set_fifo_interrupt(kbc, false);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800413 mod_timer(&kbc->timer, jiffies + kbc->cp_dly_jiffies);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800414 }
415
Dmitry Torokhov95439cb2011-09-09 09:24:20 -0700416 spin_unlock_irqrestore(&kbc->lock, flags);
417
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800418 return IRQ_HANDLED;
419}
420
421static void tegra_kbc_setup_wakekeys(struct tegra_kbc *kbc, bool filter)
422{
423 const struct tegra_kbc_platform_data *pdata = kbc->pdata;
424 int i;
425 unsigned int rst_val;
426
Rakesh Iyerbaafb432011-05-11 14:24:10 -0700427 /* Either mask all keys or none. */
428 rst_val = (filter && !pdata->wakeup) ? ~0 : 0;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800429
430 for (i = 0; i < KBC_MAX_ROW; i++)
431 writel(rst_val, kbc->mmio + KBC_ROW0_MASK_0 + i * 4);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800432}
433
434static void tegra_kbc_config_pins(struct tegra_kbc *kbc)
435{
436 const struct tegra_kbc_platform_data *pdata = kbc->pdata;
437 int i;
438
439 for (i = 0; i < KBC_MAX_GPIO; i++) {
440 u32 r_shft = 5 * (i % 6);
441 u32 c_shft = 4 * (i % 8);
Rakesh Iyer7530c4a2011-01-28 22:05:14 -0800442 u32 r_mask = 0x1f << r_shft;
443 u32 c_mask = 0x0f << c_shft;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800444 u32 r_offs = (i / 6) * 4 + KBC_ROW_CFG0_0;
445 u32 c_offs = (i / 8) * 4 + KBC_COL_CFG0_0;
446 u32 row_cfg = readl(kbc->mmio + r_offs);
447 u32 col_cfg = readl(kbc->mmio + c_offs);
448
449 row_cfg &= ~r_mask;
450 col_cfg &= ~c_mask;
451
452 if (pdata->pin_cfg[i].is_row)
453 row_cfg |= ((pdata->pin_cfg[i].num << 1) | 1) << r_shft;
454 else
455 col_cfg |= ((pdata->pin_cfg[i].num << 1) | 1) << c_shft;
456
457 writel(row_cfg, kbc->mmio + r_offs);
458 writel(col_cfg, kbc->mmio + c_offs);
459 }
460}
461
462static int tegra_kbc_start(struct tegra_kbc *kbc)
463{
464 const struct tegra_kbc_platform_data *pdata = kbc->pdata;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800465 unsigned int debounce_cnt;
466 u32 val = 0;
467
468 clk_enable(kbc->clk);
469
470 /* Reset the KBC controller to clear all previous status.*/
471 tegra_periph_reset_assert(kbc->clk);
472 udelay(100);
473 tegra_periph_reset_deassert(kbc->clk);
474 udelay(100);
475
476 tegra_kbc_config_pins(kbc);
477 tegra_kbc_setup_wakekeys(kbc, false);
478
479 writel(pdata->repeat_cnt, kbc->mmio + KBC_RPT_DLY_0);
480
481 /* Keyboard debounce count is maximum of 12 bits. */
482 debounce_cnt = min(pdata->debounce_cnt, KBC_MAX_DEBOUNCE_CNT);
483 val = KBC_DEBOUNCE_CNT_SHIFT(debounce_cnt);
484 val |= KBC_FIFO_TH_CNT_SHIFT(1); /* set fifo interrupt threshold to 1 */
485 val |= KBC_CONTROL_FIFO_CNT_INT_EN; /* interrupt on FIFO threshold */
486 val |= KBC_CONTROL_KBC_EN; /* enable */
487 writel(val, kbc->mmio + KBC_CONTROL_0);
488
489 /*
490 * Compute the delay(ns) from interrupt mode to continuous polling
491 * mode so the timer routine is scheduled appropriately.
492 */
493 val = readl(kbc->mmio + KBC_INIT_DLY_0);
494 kbc->cp_dly_jiffies = usecs_to_jiffies((val & 0xfffff) * 32);
495
496 kbc->num_pressed_keys = 0;
497
498 /*
499 * Atomically clear out any remaining entries in the key FIFO
500 * and enable keyboard interrupts.
501 */
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800502 while (1) {
503 val = readl(kbc->mmio + KBC_INT_0);
504 val >>= 4;
505 if (!val)
506 break;
507
508 val = readl(kbc->mmio + KBC_KP_ENT0_0);
509 val = readl(kbc->mmio + KBC_KP_ENT1_0);
510 }
511 writel(0x7, kbc->mmio + KBC_INT_0);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800512
513 enable_irq(kbc->irq);
514
515 return 0;
516}
517
518static void tegra_kbc_stop(struct tegra_kbc *kbc)
519{
520 unsigned long flags;
521 u32 val;
522
523 spin_lock_irqsave(&kbc->lock, flags);
524 val = readl(kbc->mmio + KBC_CONTROL_0);
525 val &= ~1;
526 writel(val, kbc->mmio + KBC_CONTROL_0);
527 spin_unlock_irqrestore(&kbc->lock, flags);
528
529 disable_irq(kbc->irq);
530 del_timer_sync(&kbc->timer);
531
532 clk_disable(kbc->clk);
533}
534
535static int tegra_kbc_open(struct input_dev *dev)
536{
537 struct tegra_kbc *kbc = input_get_drvdata(dev);
538
539 return tegra_kbc_start(kbc);
540}
541
542static void tegra_kbc_close(struct input_dev *dev)
543{
544 struct tegra_kbc *kbc = input_get_drvdata(dev);
545
546 return tegra_kbc_stop(kbc);
547}
548
549static bool __devinit
550tegra_kbc_check_pin_cfg(const struct tegra_kbc_platform_data *pdata,
551 struct device *dev, unsigned int *num_rows)
552{
553 int i;
554
555 *num_rows = 0;
556
557 for (i = 0; i < KBC_MAX_GPIO; i++) {
558 const struct tegra_kbc_pin_cfg *pin_cfg = &pdata->pin_cfg[i];
559
560 if (pin_cfg->is_row) {
561 if (pin_cfg->num >= KBC_MAX_ROW) {
562 dev_err(dev,
563 "pin_cfg[%d]: invalid row number %d\n",
564 i, pin_cfg->num);
565 return false;
566 }
567 (*num_rows)++;
568 } else {
569 if (pin_cfg->num >= KBC_MAX_COL) {
570 dev_err(dev,
571 "pin_cfg[%d]: invalid column number %d\n",
572 i, pin_cfg->num);
573 return false;
574 }
575 }
576 }
577
578 return true;
579}
580
Olof Johanssona445c7f2011-12-29 09:58:16 -0800581#ifdef CONFIG_OF
582static struct tegra_kbc_platform_data * __devinit
583tegra_kbc_dt_parse_pdata(struct platform_device *pdev)
584{
585 struct tegra_kbc_platform_data *pdata;
586 struct device_node *np = pdev->dev.of_node;
587
588 if (!np)
589 return NULL;
590
591 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
592 if (!pdata)
593 return NULL;
594
595 if (!of_property_read_u32(np, "debounce-delay", &prop))
596 pdata->debounce_cnt = prop;
597
598 if (!of_property_read_u32(np, "repeat-delay", &prop))
599 pdata->repeat_cnt = prop;
600
601 if (of_find_property(np, "needs-ghost-filter", NULL))
602 pdata->use_ghost_filter = true;
603
604 if (of_find_property(np, "wakeup-source", NULL))
605 pdata->wakeup = true;
606
607 /*
608 * All currently known keymaps with device tree support use the same
609 * pin_cfg, so set it up here.
610 */
611 for (i = 0; i < KBC_MAX_ROW; i++) {
612 pdata->pin_cfg[i].num = i;
613 pdata->pin_cfg[i].is_row = true;
614 }
615
616 for (i = 0; i < KBC_MAX_COL; i++) {
617 pdata->pin_cfg[KBC_MAX_ROW + i].num = i;
618 pdata->pin_cfg[KBC_MAX_ROW + i].is_row = false;
619 }
620
621 return pdata;
622}
623#else
624static inline struct tegra_kbc_platform_data *tegra_kbc_dt_parse_pdata(
625 struct platform_device *pdev)
626{
627 return NULL;
628}
629#endif
630
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800631static int __devinit tegra_kbc_probe(struct platform_device *pdev)
632{
633 const struct tegra_kbc_platform_data *pdata = pdev->dev.platform_data;
634 const struct matrix_keymap_data *keymap_data;
635 struct tegra_kbc *kbc;
636 struct input_dev *input_dev;
637 struct resource *res;
638 int irq;
639 int err;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800640 int num_rows = 0;
641 unsigned int debounce_cnt;
642 unsigned int scan_time_rows;
643
644 if (!pdata)
Olof Johanssona445c7f2011-12-29 09:58:16 -0800645 pdata = tegra_kbc_dt_parse_pdata(pdev);
646
647 if (!pdata)
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800648 return -EINVAL;
649
Olof Johanssona445c7f2011-12-29 09:58:16 -0800650 if (!tegra_kbc_check_pin_cfg(pdata, &pdev->dev, &num_rows)) {
651 err = -EINVAL;
652 goto err_free_pdata;
653 }
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800654
655 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
656 if (!res) {
657 dev_err(&pdev->dev, "failed to get I/O memory\n");
Olof Johanssona445c7f2011-12-29 09:58:16 -0800658 err = -ENXIO;
659 goto err_free_pdata;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800660 }
661
662 irq = platform_get_irq(pdev, 0);
663 if (irq < 0) {
664 dev_err(&pdev->dev, "failed to get keyboard IRQ\n");
Olof Johanssona445c7f2011-12-29 09:58:16 -0800665 err = -ENXIO;
666 goto err_free_pdata;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800667 }
668
669 kbc = kzalloc(sizeof(*kbc), GFP_KERNEL);
670 input_dev = input_allocate_device();
671 if (!kbc || !input_dev) {
672 err = -ENOMEM;
673 goto err_free_mem;
674 }
675
676 kbc->pdata = pdata;
677 kbc->idev = input_dev;
678 kbc->irq = irq;
679 spin_lock_init(&kbc->lock);
680 setup_timer(&kbc->timer, tegra_kbc_keypress_timer, (unsigned long)kbc);
681
682 res = request_mem_region(res->start, resource_size(res), pdev->name);
683 if (!res) {
684 dev_err(&pdev->dev, "failed to request I/O memory\n");
685 err = -EBUSY;
686 goto err_free_mem;
687 }
688
689 kbc->mmio = ioremap(res->start, resource_size(res));
690 if (!kbc->mmio) {
691 dev_err(&pdev->dev, "failed to remap I/O memory\n");
692 err = -ENXIO;
693 goto err_free_mem_region;
694 }
695
696 kbc->clk = clk_get(&pdev->dev, NULL);
697 if (IS_ERR(kbc->clk)) {
698 dev_err(&pdev->dev, "failed to get keyboard clock\n");
699 err = PTR_ERR(kbc->clk);
700 goto err_iounmap;
701 }
702
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800703 /*
704 * The time delay between two consecutive reads of the FIFO is
705 * the sum of the repeat time and the time taken for scanning
706 * the rows. There is an additional delay before the row scanning
707 * starts. The repoll delay is computed in milliseconds.
708 */
709 debounce_cnt = min(pdata->debounce_cnt, KBC_MAX_DEBOUNCE_CNT);
710 scan_time_rows = (KBC_ROW_SCAN_TIME + debounce_cnt) * num_rows;
711 kbc->repoll_dly = KBC_ROW_SCAN_DLY + scan_time_rows + pdata->repeat_cnt;
Rakesh Iyer3f277572011-07-30 12:01:48 -0700712 kbc->repoll_dly = DIV_ROUND_UP(kbc->repoll_dly, KBC_CYCLE_MS);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800713
714 input_dev->name = pdev->name;
715 input_dev->id.bustype = BUS_HOST;
716 input_dev->dev.parent = &pdev->dev;
717 input_dev->open = tegra_kbc_open;
718 input_dev->close = tegra_kbc_close;
719
720 input_set_drvdata(input_dev, kbc);
721
Rakesh Iyer5599d2e2011-07-25 00:49:55 -0700722 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800723 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
724
725 input_dev->keycode = kbc->keycode;
726 input_dev->keycodesize = sizeof(kbc->keycode[0]);
Rakesh Iyer4e8b65f2011-02-18 08:38:02 -0800727 input_dev->keycodemax = KBC_MAX_KEY;
728 if (pdata->use_fn_map)
729 input_dev->keycodemax *= 2;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800730
Rakesh Iyer4e8b65f2011-02-18 08:38:02 -0800731 kbc->use_fn_map = pdata->use_fn_map;
Rakesh Iyer34abeeb2011-04-27 23:18:15 -0700732 kbc->use_ghost_filter = pdata->use_ghost_filter;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800733 keymap_data = pdata->keymap_data ?: &tegra_kbc_default_keymap_data;
734 matrix_keypad_build_keymap(keymap_data, KBC_ROW_SHIFT,
735 input_dev->keycode, input_dev->keybit);
736
737 err = request_irq(kbc->irq, tegra_kbc_isr, IRQF_TRIGGER_HIGH,
738 pdev->name, kbc);
739 if (err) {
740 dev_err(&pdev->dev, "failed to request keyboard IRQ\n");
741 goto err_put_clk;
742 }
743
744 disable_irq(kbc->irq);
745
746 err = input_register_device(kbc->idev);
747 if (err) {
748 dev_err(&pdev->dev, "failed to register input device\n");
749 goto err_free_irq;
750 }
751
752 platform_set_drvdata(pdev, kbc);
753 device_init_wakeup(&pdev->dev, pdata->wakeup);
754
755 return 0;
756
757err_free_irq:
758 free_irq(kbc->irq, pdev);
759err_put_clk:
760 clk_put(kbc->clk);
761err_iounmap:
762 iounmap(kbc->mmio);
763err_free_mem_region:
764 release_mem_region(res->start, resource_size(res));
765err_free_mem:
Axel Lin22f83202011-08-11 09:22:45 -0700766 input_free_device(input_dev);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800767 kfree(kbc);
Olof Johanssona445c7f2011-12-29 09:58:16 -0800768err_free_pdata:
769 if (!pdev->dev.platform_data)
770 kfree(pdata);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800771
772 return err;
773}
774
775static int __devexit tegra_kbc_remove(struct platform_device *pdev)
776{
777 struct tegra_kbc *kbc = platform_get_drvdata(pdev);
778 struct resource *res;
779
Olof Johanssona445c7f2011-12-29 09:58:16 -0800780 platform_set_drvdata(pdev, NULL);
781
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800782 free_irq(kbc->irq, pdev);
783 clk_put(kbc->clk);
784
785 input_unregister_device(kbc->idev);
786 iounmap(kbc->mmio);
787 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
788 release_mem_region(res->start, resource_size(res));
789
Olof Johanssona445c7f2011-12-29 09:58:16 -0800790 /*
791 * If we do not have platform data attached to the device we
792 * allocated it ourselves and thus need to free it.
793 */
794 if (!pdev->dev.platform_data)
795 kfree(kbc->pdata);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800796
Olof Johanssona445c7f2011-12-29 09:58:16 -0800797 kfree(kbc);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800798
799 return 0;
800}
801
802#ifdef CONFIG_PM_SLEEP
803static int tegra_kbc_suspend(struct device *dev)
804{
805 struct platform_device *pdev = to_platform_device(dev);
806 struct tegra_kbc *kbc = platform_get_drvdata(pdev);
807
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700808 mutex_lock(&kbc->idev->mutex);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800809 if (device_may_wakeup(&pdev->dev)) {
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700810 disable_irq(kbc->irq);
811 del_timer_sync(&kbc->timer);
812 tegra_kbc_set_fifo_interrupt(kbc, false);
813
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800814 /* Forcefully clear the interrupt status */
815 writel(0x7, kbc->mmio + KBC_INT_0);
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700816 /*
817 * Store the previous resident time of continuous polling mode.
818 * Force the keyboard into interrupt mode.
819 */
820 kbc->cp_to_wkup_dly = readl(kbc->mmio + KBC_TO_CNT_0);
821 writel(0, kbc->mmio + KBC_TO_CNT_0);
822
823 tegra_kbc_setup_wakekeys(kbc, true);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800824 msleep(30);
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700825
826 enable_irq_wake(kbc->irq);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800827 } else {
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800828 if (kbc->idev->users)
829 tegra_kbc_stop(kbc);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800830 }
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700831 mutex_unlock(&kbc->idev->mutex);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800832
833 return 0;
834}
835
836static int tegra_kbc_resume(struct device *dev)
837{
838 struct platform_device *pdev = to_platform_device(dev);
839 struct tegra_kbc *kbc = platform_get_drvdata(pdev);
840 int err = 0;
841
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700842 mutex_lock(&kbc->idev->mutex);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800843 if (device_may_wakeup(&pdev->dev)) {
844 disable_irq_wake(kbc->irq);
845 tegra_kbc_setup_wakekeys(kbc, false);
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700846
847 /* Restore the resident time of continuous polling mode. */
848 writel(kbc->cp_to_wkup_dly, kbc->mmio + KBC_TO_CNT_0);
849
850 tegra_kbc_set_fifo_interrupt(kbc, true);
851
852 enable_irq(kbc->irq);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800853 } else {
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800854 if (kbc->idev->users)
855 err = tegra_kbc_start(kbc);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800856 }
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700857 mutex_unlock(&kbc->idev->mutex);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800858
859 return err;
860}
861#endif
862
863static SIMPLE_DEV_PM_OPS(tegra_kbc_pm_ops, tegra_kbc_suspend, tegra_kbc_resume);
864
Olof Johanssona445c7f2011-12-29 09:58:16 -0800865static const struct of_device_id tegra_kbc_of_match[] = {
866 { .compatible = "nvidia,tegra20-kbc", },
867 { },
868};
869MODULE_DEVICE_TABLE(of, tegra_kbc_of_match);
870
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800871static struct platform_driver tegra_kbc_driver = {
872 .probe = tegra_kbc_probe,
873 .remove = __devexit_p(tegra_kbc_remove),
874 .driver = {
875 .name = "tegra-kbc",
876 .owner = THIS_MODULE,
877 .pm = &tegra_kbc_pm_ops,
Olof Johanssona445c7f2011-12-29 09:58:16 -0800878 .of_match_table = tegra_kbc_of_match,
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800879 },
880};
JJ Ding5146c842011-11-29 11:08:39 -0800881module_platform_driver(tegra_kbc_driver);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800882
883MODULE_LICENSE("GPL");
884MODULE_AUTHOR("Rakesh Iyer <riyer@nvidia.com>");
885MODULE_DESCRIPTION("Tegra matrix keyboard controller driver");
886MODULE_ALIAS("platform:tegra-kbc");