blob: 68b2b65024d655346303c25f0cdb35576df151ae [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>
Stephen Warren9eee07d2013-02-15 17:04:12 -080032#include <linux/input/matrix_keypad.h>
Prashant Gaikwad61fd2902013-01-11 13:16:26 +053033#include <linux/clk/tegra.h>
Sachin Kamatba52a7f2013-03-17 21:30:05 -070034#include <linux/err.h>
Rakesh Iyer11f5b302011-01-19 23:38:47 -080035
Stephen Warren9eee07d2013-02-15 17:04:12 -080036#define KBC_MAX_GPIO 24
37#define KBC_MAX_KPENT 8
38
39#define KBC_MAX_ROW 16
40#define KBC_MAX_COL 8
41#define KBC_MAX_KEY (KBC_MAX_ROW * KBC_MAX_COL)
42
Rakesh Iyer11f5b302011-01-19 23:38:47 -080043#define KBC_MAX_DEBOUNCE_CNT 0x3ffu
44
45/* KBC row scan time and delay for beginning the row scan. */
46#define KBC_ROW_SCAN_TIME 16
47#define KBC_ROW_SCAN_DLY 5
48
49/* KBC uses a 32KHz clock so a cycle = 1/32Khz */
Rakesh Iyer3f277572011-07-30 12:01:48 -070050#define KBC_CYCLE_MS 32
Rakesh Iyer11f5b302011-01-19 23:38:47 -080051
52/* KBC Registers */
53
54/* KBC Control Register */
55#define KBC_CONTROL_0 0x0
56#define KBC_FIFO_TH_CNT_SHIFT(cnt) (cnt << 14)
57#define KBC_DEBOUNCE_CNT_SHIFT(cnt) (cnt << 4)
58#define KBC_CONTROL_FIFO_CNT_INT_EN (1 << 3)
Rakesh Iyerb6834b02012-01-22 23:27:54 -080059#define KBC_CONTROL_KEYPRESS_INT_EN (1 << 1)
Rakesh Iyer11f5b302011-01-19 23:38:47 -080060#define KBC_CONTROL_KBC_EN (1 << 0)
61
62/* KBC Interrupt Register */
63#define KBC_INT_0 0x4
64#define KBC_INT_FIFO_CNT_INT_STATUS (1 << 2)
Rakesh Iyerfd0fc212011-12-29 19:27:44 -080065#define KBC_INT_KEYPRESS_INT_STATUS (1 << 0)
Rakesh Iyer11f5b302011-01-19 23:38:47 -080066
67#define KBC_ROW_CFG0_0 0x8
68#define KBC_COL_CFG0_0 0x18
Rakesh Iyerd0d150e2011-09-08 15:34:11 -070069#define KBC_TO_CNT_0 0x24
Rakesh Iyer11f5b302011-01-19 23:38:47 -080070#define KBC_INIT_DLY_0 0x28
71#define KBC_RPT_DLY_0 0x2c
72#define KBC_KP_ENT0_0 0x30
73#define KBC_KP_ENT1_0 0x34
74#define KBC_ROW0_MASK_0 0x38
75
76#define KBC_ROW_SHIFT 3
77
Stephen Warren9eee07d2013-02-15 17:04:12 -080078enum tegra_pin_type {
79 PIN_CFG_IGNORE,
80 PIN_CFG_COL,
81 PIN_CFG_ROW,
82};
83
84struct tegra_kbc_pin_cfg {
85 enum tegra_pin_type type;
86 unsigned char num;
87};
88
Rakesh Iyer11f5b302011-01-19 23:38:47 -080089struct tegra_kbc {
Stephen Warren9eee07d2013-02-15 17:04:12 -080090 struct device *dev;
91 unsigned int debounce_cnt;
92 unsigned int repeat_cnt;
93 struct tegra_kbc_pin_cfg pin_cfg[KBC_MAX_GPIO];
94 const struct matrix_keymap_data *keymap_data;
95 bool wakeup;
Rakesh Iyer11f5b302011-01-19 23:38:47 -080096 void __iomem *mmio;
97 struct input_dev *idev;
Stephen Warren9eee07d2013-02-15 17:04:12 -080098 int irq;
Rakesh Iyer11f5b302011-01-19 23:38:47 -080099 spinlock_t lock;
100 unsigned int repoll_dly;
101 unsigned long cp_dly_jiffies;
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700102 unsigned int cp_to_wkup_dly;
Rakesh Iyer4e8b65f2011-02-18 08:38:02 -0800103 bool use_fn_map;
Rakesh Iyer34abeeb2011-04-27 23:18:15 -0700104 bool use_ghost_filter;
Rakesh Iyerfd0fc212011-12-29 19:27:44 -0800105 bool keypress_caused_wake;
Rakesh Iyer4e8b65f2011-02-18 08:38:02 -0800106 unsigned short keycode[KBC_MAX_KEY * 2];
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800107 unsigned short current_keys[KBC_MAX_KPENT];
108 unsigned int num_pressed_keys;
Rakesh Iyerfd0fc212011-12-29 19:27:44 -0800109 u32 wakeup_key;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800110 struct timer_list timer;
111 struct clk *clk;
112};
113
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800114static void tegra_kbc_report_released_keys(struct input_dev *input,
115 unsigned short old_keycodes[],
116 unsigned int old_num_keys,
117 unsigned short new_keycodes[],
118 unsigned int new_num_keys)
119{
120 unsigned int i, j;
121
122 for (i = 0; i < old_num_keys; i++) {
123 for (j = 0; j < new_num_keys; j++)
124 if (old_keycodes[i] == new_keycodes[j])
125 break;
126
127 if (j == new_num_keys)
128 input_report_key(input, old_keycodes[i], 0);
129 }
130}
131
132static void tegra_kbc_report_pressed_keys(struct input_dev *input,
133 unsigned char scancodes[],
134 unsigned short keycodes[],
135 unsigned int num_pressed_keys)
136{
137 unsigned int i;
138
139 for (i = 0; i < num_pressed_keys; i++) {
140 input_event(input, EV_MSC, MSC_SCAN, scancodes[i]);
141 input_report_key(input, keycodes[i], 1);
142 }
143}
144
145static void tegra_kbc_report_keys(struct tegra_kbc *kbc)
146{
147 unsigned char scancodes[KBC_MAX_KPENT];
148 unsigned short keycodes[KBC_MAX_KPENT];
149 u32 val = 0;
150 unsigned int i;
151 unsigned int num_down = 0;
Rakesh Iyer4e8b65f2011-02-18 08:38:02 -0800152 bool fn_keypress = false;
Rakesh Iyer34abeeb2011-04-27 23:18:15 -0700153 bool key_in_same_row = false;
154 bool key_in_same_col = false;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800155
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800156 for (i = 0; i < KBC_MAX_KPENT; i++) {
157 if ((i % 4) == 0)
158 val = readl(kbc->mmio + KBC_KP_ENT0_0 + i);
159
160 if (val & 0x80) {
161 unsigned int col = val & 0x07;
162 unsigned int row = (val >> 3) & 0x0f;
163 unsigned char scancode =
164 MATRIX_SCAN_CODE(row, col, KBC_ROW_SHIFT);
165
166 scancodes[num_down] = scancode;
Rakesh Iyer4e8b65f2011-02-18 08:38:02 -0800167 keycodes[num_down] = kbc->keycode[scancode];
168 /* If driver uses Fn map, do not report the Fn key. */
169 if ((keycodes[num_down] == KEY_FN) && kbc->use_fn_map)
170 fn_keypress = true;
171 else
172 num_down++;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800173 }
174
175 val >>= 8;
176 }
Rakesh Iyer4e8b65f2011-02-18 08:38:02 -0800177
178 /*
Rakesh Iyer34abeeb2011-04-27 23:18:15 -0700179 * Matrix keyboard designs are prone to keyboard ghosting.
180 * Ghosting occurs if there are 3 keys such that -
181 * any 2 of the 3 keys share a row, and any 2 of them share a column.
182 * If so ignore the key presses for this iteration.
183 */
Dmitry Torokhov95439cb2011-09-09 09:24:20 -0700184 if (kbc->use_ghost_filter && num_down >= 3) {
Rakesh Iyer34abeeb2011-04-27 23:18:15 -0700185 for (i = 0; i < num_down; i++) {
186 unsigned int j;
187 u8 curr_col = scancodes[i] & 0x07;
188 u8 curr_row = scancodes[i] >> KBC_ROW_SHIFT;
189
190 /*
191 * Find 2 keys such that one key is in the same row
192 * and the other is in the same column as the i-th key.
193 */
194 for (j = i + 1; j < num_down; j++) {
195 u8 col = scancodes[j] & 0x07;
196 u8 row = scancodes[j] >> KBC_ROW_SHIFT;
197
198 if (col == curr_col)
199 key_in_same_col = true;
200 if (row == curr_row)
201 key_in_same_row = true;
202 }
203 }
204 }
205
206 /*
Rakesh Iyer4e8b65f2011-02-18 08:38:02 -0800207 * If the platform uses Fn keymaps, translate keys on a Fn keypress.
208 * Function keycodes are KBC_MAX_KEY apart from the plain keycodes.
209 */
210 if (fn_keypress) {
211 for (i = 0; i < num_down; i++) {
212 scancodes[i] += KBC_MAX_KEY;
213 keycodes[i] = kbc->keycode[scancodes[i]];
214 }
215 }
216
Rakesh Iyer34abeeb2011-04-27 23:18:15 -0700217 /* Ignore the key presses for this iteration? */
218 if (key_in_same_col && key_in_same_row)
219 return;
220
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800221 tegra_kbc_report_released_keys(kbc->idev,
222 kbc->current_keys, kbc->num_pressed_keys,
223 keycodes, num_down);
224 tegra_kbc_report_pressed_keys(kbc->idev, scancodes, keycodes, num_down);
225 input_sync(kbc->idev);
226
227 memcpy(kbc->current_keys, keycodes, sizeof(kbc->current_keys));
228 kbc->num_pressed_keys = num_down;
229}
230
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700231static void tegra_kbc_set_fifo_interrupt(struct tegra_kbc *kbc, bool enable)
232{
233 u32 val;
234
235 val = readl(kbc->mmio + KBC_CONTROL_0);
236 if (enable)
237 val |= KBC_CONTROL_FIFO_CNT_INT_EN;
238 else
239 val &= ~KBC_CONTROL_FIFO_CNT_INT_EN;
240 writel(val, kbc->mmio + KBC_CONTROL_0);
241}
242
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800243static void tegra_kbc_keypress_timer(unsigned long data)
244{
245 struct tegra_kbc *kbc = (struct tegra_kbc *)data;
246 unsigned long flags;
247 u32 val;
248 unsigned int i;
249
Dmitry Torokhov95439cb2011-09-09 09:24:20 -0700250 spin_lock_irqsave(&kbc->lock, flags);
251
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800252 val = (readl(kbc->mmio + KBC_INT_0) >> 4) & 0xf;
253 if (val) {
254 unsigned long dly;
255
256 tegra_kbc_report_keys(kbc);
257
258 /*
259 * If more than one keys are pressed we need not wait
260 * for the repoll delay.
261 */
262 dly = (val == 1) ? kbc->repoll_dly : 1;
263 mod_timer(&kbc->timer, jiffies + msecs_to_jiffies(dly));
264 } else {
265 /* Release any pressed keys and exit the polling loop */
266 for (i = 0; i < kbc->num_pressed_keys; i++)
267 input_report_key(kbc->idev, kbc->current_keys[i], 0);
268 input_sync(kbc->idev);
269
270 kbc->num_pressed_keys = 0;
271
272 /* All keys are released so enable the keypress interrupt */
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700273 tegra_kbc_set_fifo_interrupt(kbc, true);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800274 }
Dmitry Torokhov95439cb2011-09-09 09:24:20 -0700275
276 spin_unlock_irqrestore(&kbc->lock, flags);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800277}
278
279static irqreturn_t tegra_kbc_isr(int irq, void *args)
280{
281 struct tegra_kbc *kbc = args;
Dmitry Torokhov95439cb2011-09-09 09:24:20 -0700282 unsigned long flags;
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700283 u32 val;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800284
Dmitry Torokhov95439cb2011-09-09 09:24:20 -0700285 spin_lock_irqsave(&kbc->lock, flags);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800286
287 /*
288 * Quickly bail out & reenable interrupts if the fifo threshold
289 * count interrupt wasn't the interrupt source
290 */
291 val = readl(kbc->mmio + KBC_INT_0);
292 writel(val, kbc->mmio + KBC_INT_0);
293
294 if (val & KBC_INT_FIFO_CNT_INT_STATUS) {
295 /*
Dmitry Torokhov95439cb2011-09-09 09:24:20 -0700296 * Until all keys are released, defer further processing to
297 * the polling loop in tegra_kbc_keypress_timer.
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800298 */
Dmitry Torokhov95439cb2011-09-09 09:24:20 -0700299 tegra_kbc_set_fifo_interrupt(kbc, false);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800300 mod_timer(&kbc->timer, jiffies + kbc->cp_dly_jiffies);
Rakesh Iyerfd0fc212011-12-29 19:27:44 -0800301 } else if (val & KBC_INT_KEYPRESS_INT_STATUS) {
302 /* We can be here only through system resume path */
303 kbc->keypress_caused_wake = true;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800304 }
305
Dmitry Torokhov95439cb2011-09-09 09:24:20 -0700306 spin_unlock_irqrestore(&kbc->lock, flags);
307
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800308 return IRQ_HANDLED;
309}
310
311static void tegra_kbc_setup_wakekeys(struct tegra_kbc *kbc, bool filter)
312{
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800313 int i;
314 unsigned int rst_val;
315
Rakesh Iyerbaafb432011-05-11 14:24:10 -0700316 /* Either mask all keys or none. */
Stephen Warren9eee07d2013-02-15 17:04:12 -0800317 rst_val = (filter && !kbc->wakeup) ? ~0 : 0;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800318
319 for (i = 0; i < KBC_MAX_ROW; i++)
320 writel(rst_val, kbc->mmio + KBC_ROW0_MASK_0 + i * 4);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800321}
322
323static void tegra_kbc_config_pins(struct tegra_kbc *kbc)
324{
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800325 int i;
326
327 for (i = 0; i < KBC_MAX_GPIO; i++) {
328 u32 r_shft = 5 * (i % 6);
329 u32 c_shft = 4 * (i % 8);
Rakesh Iyer7530c4a2011-01-28 22:05:14 -0800330 u32 r_mask = 0x1f << r_shft;
331 u32 c_mask = 0x0f << c_shft;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800332 u32 r_offs = (i / 6) * 4 + KBC_ROW_CFG0_0;
333 u32 c_offs = (i / 8) * 4 + KBC_COL_CFG0_0;
334 u32 row_cfg = readl(kbc->mmio + r_offs);
335 u32 col_cfg = readl(kbc->mmio + c_offs);
336
337 row_cfg &= ~r_mask;
338 col_cfg &= ~c_mask;
339
Stephen Warren9eee07d2013-02-15 17:04:12 -0800340 switch (kbc->pin_cfg[i].type) {
Shridhar Rasal023cea02012-02-03 00:27:30 -0800341 case PIN_CFG_ROW:
Stephen Warren9eee07d2013-02-15 17:04:12 -0800342 row_cfg |= ((kbc->pin_cfg[i].num << 1) | 1) << r_shft;
Shridhar Rasal023cea02012-02-03 00:27:30 -0800343 break;
344
345 case PIN_CFG_COL:
Stephen Warren9eee07d2013-02-15 17:04:12 -0800346 col_cfg |= ((kbc->pin_cfg[i].num << 1) | 1) << c_shft;
Shridhar Rasal023cea02012-02-03 00:27:30 -0800347 break;
348
349 case PIN_CFG_IGNORE:
350 break;
351 }
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800352
353 writel(row_cfg, kbc->mmio + r_offs);
354 writel(col_cfg, kbc->mmio + c_offs);
355 }
356}
357
358static int tegra_kbc_start(struct tegra_kbc *kbc)
359{
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800360 unsigned int debounce_cnt;
361 u32 val = 0;
362
Prashant Gaikwadf7624702012-06-05 09:59:39 +0530363 clk_prepare_enable(kbc->clk);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800364
365 /* Reset the KBC controller to clear all previous status.*/
366 tegra_periph_reset_assert(kbc->clk);
367 udelay(100);
368 tegra_periph_reset_deassert(kbc->clk);
369 udelay(100);
370
371 tegra_kbc_config_pins(kbc);
372 tegra_kbc_setup_wakekeys(kbc, false);
373
Stephen Warren9eee07d2013-02-15 17:04:12 -0800374 writel(kbc->repeat_cnt, kbc->mmio + KBC_RPT_DLY_0);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800375
376 /* Keyboard debounce count is maximum of 12 bits. */
Stephen Warren9eee07d2013-02-15 17:04:12 -0800377 debounce_cnt = min(kbc->debounce_cnt, KBC_MAX_DEBOUNCE_CNT);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800378 val = KBC_DEBOUNCE_CNT_SHIFT(debounce_cnt);
379 val |= KBC_FIFO_TH_CNT_SHIFT(1); /* set fifo interrupt threshold to 1 */
380 val |= KBC_CONTROL_FIFO_CNT_INT_EN; /* interrupt on FIFO threshold */
381 val |= KBC_CONTROL_KBC_EN; /* enable */
382 writel(val, kbc->mmio + KBC_CONTROL_0);
383
384 /*
385 * Compute the delay(ns) from interrupt mode to continuous polling
386 * mode so the timer routine is scheduled appropriately.
387 */
388 val = readl(kbc->mmio + KBC_INIT_DLY_0);
389 kbc->cp_dly_jiffies = usecs_to_jiffies((val & 0xfffff) * 32);
390
391 kbc->num_pressed_keys = 0;
392
393 /*
394 * Atomically clear out any remaining entries in the key FIFO
395 * and enable keyboard interrupts.
396 */
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800397 while (1) {
398 val = readl(kbc->mmio + KBC_INT_0);
399 val >>= 4;
400 if (!val)
401 break;
402
403 val = readl(kbc->mmio + KBC_KP_ENT0_0);
404 val = readl(kbc->mmio + KBC_KP_ENT1_0);
405 }
406 writel(0x7, kbc->mmio + KBC_INT_0);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800407
408 enable_irq(kbc->irq);
409
410 return 0;
411}
412
413static void tegra_kbc_stop(struct tegra_kbc *kbc)
414{
415 unsigned long flags;
416 u32 val;
417
418 spin_lock_irqsave(&kbc->lock, flags);
419 val = readl(kbc->mmio + KBC_CONTROL_0);
420 val &= ~1;
421 writel(val, kbc->mmio + KBC_CONTROL_0);
422 spin_unlock_irqrestore(&kbc->lock, flags);
423
424 disable_irq(kbc->irq);
425 del_timer_sync(&kbc->timer);
426
Prashant Gaikwadf7624702012-06-05 09:59:39 +0530427 clk_disable_unprepare(kbc->clk);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800428}
429
430static int tegra_kbc_open(struct input_dev *dev)
431{
432 struct tegra_kbc *kbc = input_get_drvdata(dev);
433
434 return tegra_kbc_start(kbc);
435}
436
437static void tegra_kbc_close(struct input_dev *dev)
438{
439 struct tegra_kbc *kbc = input_get_drvdata(dev);
440
441 return tegra_kbc_stop(kbc);
442}
443
Stephen Warren9eee07d2013-02-15 17:04:12 -0800444static bool tegra_kbc_check_pin_cfg(const struct tegra_kbc *kbc,
445 unsigned int *num_rows)
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800446{
447 int i;
448
449 *num_rows = 0;
450
451 for (i = 0; i < KBC_MAX_GPIO; i++) {
Stephen Warren9eee07d2013-02-15 17:04:12 -0800452 const struct tegra_kbc_pin_cfg *pin_cfg = &kbc->pin_cfg[i];
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800453
Shridhar Rasal023cea02012-02-03 00:27:30 -0800454 switch (pin_cfg->type) {
455 case PIN_CFG_ROW:
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800456 if (pin_cfg->num >= KBC_MAX_ROW) {
Stephen Warren9eee07d2013-02-15 17:04:12 -0800457 dev_err(kbc->dev,
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800458 "pin_cfg[%d]: invalid row number %d\n",
459 i, pin_cfg->num);
460 return false;
461 }
462 (*num_rows)++;
Shridhar Rasal023cea02012-02-03 00:27:30 -0800463 break;
464
465 case PIN_CFG_COL:
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800466 if (pin_cfg->num >= KBC_MAX_COL) {
Stephen Warren9eee07d2013-02-15 17:04:12 -0800467 dev_err(kbc->dev,
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800468 "pin_cfg[%d]: invalid column number %d\n",
469 i, pin_cfg->num);
470 return false;
471 }
Shridhar Rasal023cea02012-02-03 00:27:30 -0800472 break;
473
474 case PIN_CFG_IGNORE:
475 break;
476
477 default:
Stephen Warren9eee07d2013-02-15 17:04:12 -0800478 dev_err(kbc->dev,
Shridhar Rasal023cea02012-02-03 00:27:30 -0800479 "pin_cfg[%d]: invalid entry type %d\n",
480 pin_cfg->type, pin_cfg->num);
481 return false;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800482 }
483 }
484
485 return true;
486}
487
Stephen Warren9eee07d2013-02-15 17:04:12 -0800488static int tegra_kbc_parse_dt(struct tegra_kbc *kbc)
Olof Johanssona445c7f2011-12-29 09:58:16 -0800489{
Stephen Warren9eee07d2013-02-15 17:04:12 -0800490 struct device_node *np = kbc->dev->of_node;
Olof Johansson145e9732012-03-13 21:36:29 -0700491 u32 prop;
492 int i;
Laxman Dewangan88390242013-01-06 18:32:22 -0800493 u32 num_rows = 0;
494 u32 num_cols = 0;
495 u32 cols_cfg[KBC_MAX_GPIO];
496 u32 rows_cfg[KBC_MAX_GPIO];
497 int proplen;
498 int ret;
Olof Johanssona445c7f2011-12-29 09:58:16 -0800499
Olof Johansson145e9732012-03-13 21:36:29 -0700500 if (!of_property_read_u32(np, "nvidia,debounce-delay-ms", &prop))
Stephen Warren9eee07d2013-02-15 17:04:12 -0800501 kbc->debounce_cnt = prop;
Olof Johanssona445c7f2011-12-29 09:58:16 -0800502
Olof Johansson145e9732012-03-13 21:36:29 -0700503 if (!of_property_read_u32(np, "nvidia,repeat-delay-ms", &prop))
Stephen Warren9eee07d2013-02-15 17:04:12 -0800504 kbc->repeat_cnt = prop;
Olof Johanssona445c7f2011-12-29 09:58:16 -0800505
Olof Johansson145e9732012-03-13 21:36:29 -0700506 if (of_find_property(np, "nvidia,needs-ghost-filter", NULL))
Stephen Warren9eee07d2013-02-15 17:04:12 -0800507 kbc->use_ghost_filter = true;
Olof Johanssona445c7f2011-12-29 09:58:16 -0800508
Olof Johansson145e9732012-03-13 21:36:29 -0700509 if (of_find_property(np, "nvidia,wakeup-source", NULL))
Stephen Warren9eee07d2013-02-15 17:04:12 -0800510 kbc->wakeup = true;
Olof Johanssona445c7f2011-12-29 09:58:16 -0800511
Laxman Dewangan88390242013-01-06 18:32:22 -0800512 if (!of_get_property(np, "nvidia,kbc-row-pins", &proplen)) {
Stephen Warren9eee07d2013-02-15 17:04:12 -0800513 dev_err(kbc->dev, "property nvidia,kbc-row-pins not found\n");
514 return -ENOENT;
Laxman Dewangan88390242013-01-06 18:32:22 -0800515 }
516 num_rows = proplen / sizeof(u32);
517
518 if (!of_get_property(np, "nvidia,kbc-col-pins", &proplen)) {
Stephen Warren9eee07d2013-02-15 17:04:12 -0800519 dev_err(kbc->dev, "property nvidia,kbc-col-pins not found\n");
520 return -ENOENT;
Laxman Dewangan88390242013-01-06 18:32:22 -0800521 }
522 num_cols = proplen / sizeof(u32);
523
524 if (!of_get_property(np, "linux,keymap", &proplen)) {
Stephen Warren9eee07d2013-02-15 17:04:12 -0800525 dev_err(kbc->dev, "property linux,keymap not found\n");
526 return -ENOENT;
Olof Johanssona445c7f2011-12-29 09:58:16 -0800527 }
528
Laxman Dewangan88390242013-01-06 18:32:22 -0800529 if (!num_rows || !num_cols || ((num_rows + num_cols) > KBC_MAX_GPIO)) {
Stephen Warren9eee07d2013-02-15 17:04:12 -0800530 dev_err(kbc->dev,
Laxman Dewangan88390242013-01-06 18:32:22 -0800531 "keypad rows/columns not porperly specified\n");
Stephen Warren9eee07d2013-02-15 17:04:12 -0800532 return -EINVAL;
Laxman Dewangan88390242013-01-06 18:32:22 -0800533 }
534
535 /* Set all pins as non-configured */
536 for (i = 0; i < KBC_MAX_GPIO; i++)
Stephen Warren9eee07d2013-02-15 17:04:12 -0800537 kbc->pin_cfg[i].type = PIN_CFG_IGNORE;
Laxman Dewangan88390242013-01-06 18:32:22 -0800538
539 ret = of_property_read_u32_array(np, "nvidia,kbc-row-pins",
540 rows_cfg, num_rows);
541 if (ret < 0) {
Stephen Warren9eee07d2013-02-15 17:04:12 -0800542 dev_err(kbc->dev, "Rows configurations are not proper\n");
543 return -EINVAL;
Laxman Dewangan88390242013-01-06 18:32:22 -0800544 }
545
546 ret = of_property_read_u32_array(np, "nvidia,kbc-col-pins",
547 cols_cfg, num_cols);
548 if (ret < 0) {
Stephen Warren9eee07d2013-02-15 17:04:12 -0800549 dev_err(kbc->dev, "Cols configurations are not proper\n");
550 return -EINVAL;
Laxman Dewangan88390242013-01-06 18:32:22 -0800551 }
552
553 for (i = 0; i < num_rows; i++) {
Stephen Warren9eee07d2013-02-15 17:04:12 -0800554 kbc->pin_cfg[rows_cfg[i]].type = PIN_CFG_ROW;
555 kbc->pin_cfg[rows_cfg[i]].num = i;
Laxman Dewangan88390242013-01-06 18:32:22 -0800556 }
557
558 for (i = 0; i < num_cols; i++) {
Stephen Warren9eee07d2013-02-15 17:04:12 -0800559 kbc->pin_cfg[cols_cfg[i]].type = PIN_CFG_COL;
560 kbc->pin_cfg[cols_cfg[i]].num = i;
Olof Johanssona445c7f2011-12-29 09:58:16 -0800561 }
562
Stephen Warren9eee07d2013-02-15 17:04:12 -0800563 return 0;
Olof Johanssona445c7f2011-12-29 09:58:16 -0800564}
Olof Johanssona445c7f2011-12-29 09:58:16 -0800565
Bill Pemberton5298cc42012-11-23 21:38:25 -0800566static int tegra_kbc_probe(struct platform_device *pdev)
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800567{
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800568 struct tegra_kbc *kbc;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800569 struct resource *res;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800570 int err;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800571 int num_rows = 0;
572 unsigned int debounce_cnt;
573 unsigned int scan_time_rows;
Laxman Dewangan914e5972013-01-06 18:34:48 -0800574 unsigned int keymap_rows = KBC_MAX_KEY;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800575
Stephen Warren9eee07d2013-02-15 17:04:12 -0800576 kbc = devm_kzalloc(&pdev->dev, sizeof(*kbc), GFP_KERNEL);
577 if (!kbc) {
578 dev_err(&pdev->dev, "failed to alloc memory for kbc\n");
579 return -ENOMEM;
580 }
Olof Johanssona445c7f2011-12-29 09:58:16 -0800581
Stephen Warren9eee07d2013-02-15 17:04:12 -0800582 kbc->dev = &pdev->dev;
583 spin_lock_init(&kbc->lock);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800584
Stephen Warren9eee07d2013-02-15 17:04:12 -0800585 err = tegra_kbc_parse_dt(kbc);
586 if (err)
587 return err;
588
589 if (!tegra_kbc_check_pin_cfg(kbc, &num_rows))
Laxman Dewangan00eb81e2013-01-06 18:31:20 -0800590 return -EINVAL;
591
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800592 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
593 if (!res) {
594 dev_err(&pdev->dev, "failed to get I/O memory\n");
Laxman Dewangan00eb81e2013-01-06 18:31:20 -0800595 return -ENXIO;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800596 }
597
Stephen Warren9eee07d2013-02-15 17:04:12 -0800598 kbc->irq = platform_get_irq(pdev, 0);
599 if (kbc->irq < 0) {
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800600 dev_err(&pdev->dev, "failed to get keyboard IRQ\n");
Laxman Dewangan00eb81e2013-01-06 18:31:20 -0800601 return -ENXIO;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800602 }
603
Stephen Warren9eee07d2013-02-15 17:04:12 -0800604 kbc->idev = devm_input_allocate_device(&pdev->dev);
605 if (!kbc->idev) {
Laxman Dewangan00eb81e2013-01-06 18:31:20 -0800606 dev_err(&pdev->dev, "failed to allocate input device\n");
607 return -ENOMEM;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800608 }
609
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800610 setup_timer(&kbc->timer, tegra_kbc_keypress_timer, (unsigned long)kbc);
611
Sachin Kamatba52a7f2013-03-17 21:30:05 -0700612 kbc->mmio = devm_ioremap_resource(&pdev->dev, res);
613 if (IS_ERR(kbc->mmio))
614 return PTR_ERR(kbc->mmio);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800615
Laxman Dewangan00eb81e2013-01-06 18:31:20 -0800616 kbc->clk = devm_clk_get(&pdev->dev, NULL);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800617 if (IS_ERR(kbc->clk)) {
618 dev_err(&pdev->dev, "failed to get keyboard clock\n");
Laxman Dewangan00eb81e2013-01-06 18:31:20 -0800619 return PTR_ERR(kbc->clk);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800620 }
621
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800622 /*
623 * The time delay between two consecutive reads of the FIFO is
624 * the sum of the repeat time and the time taken for scanning
625 * the rows. There is an additional delay before the row scanning
626 * starts. The repoll delay is computed in milliseconds.
627 */
Stephen Warren9eee07d2013-02-15 17:04:12 -0800628 debounce_cnt = min(kbc->debounce_cnt, KBC_MAX_DEBOUNCE_CNT);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800629 scan_time_rows = (KBC_ROW_SCAN_TIME + debounce_cnt) * num_rows;
Stephen Warren9eee07d2013-02-15 17:04:12 -0800630 kbc->repoll_dly = KBC_ROW_SCAN_DLY + scan_time_rows + kbc->repeat_cnt;
Rakesh Iyer3f277572011-07-30 12:01:48 -0700631 kbc->repoll_dly = DIV_ROUND_UP(kbc->repoll_dly, KBC_CYCLE_MS);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800632
Stephen Warren9eee07d2013-02-15 17:04:12 -0800633 kbc->idev->name = pdev->name;
634 kbc->idev->id.bustype = BUS_HOST;
635 kbc->idev->dev.parent = &pdev->dev;
636 kbc->idev->open = tegra_kbc_open;
637 kbc->idev->close = tegra_kbc_close;
Dmitry Torokhov19328112012-05-10 22:37:08 -0700638
Stephen Warren9eee07d2013-02-15 17:04:12 -0800639 if (kbc->keymap_data && kbc->use_fn_map)
Laxman Dewangan914e5972013-01-06 18:34:48 -0800640 keymap_rows *= 2;
641
Stephen Warren9eee07d2013-02-15 17:04:12 -0800642 err = matrix_keypad_build_keymap(kbc->keymap_data, NULL,
Laxman Dewangan914e5972013-01-06 18:34:48 -0800643 keymap_rows, KBC_MAX_COL,
Stephen Warren9eee07d2013-02-15 17:04:12 -0800644 kbc->keycode, kbc->idev);
Dmitry Torokhov19328112012-05-10 22:37:08 -0700645 if (err) {
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700646 dev_err(&pdev->dev, "failed to setup keymap\n");
Laxman Dewangan00eb81e2013-01-06 18:31:20 -0800647 return err;
Dmitry Torokhov19328112012-05-10 22:37:08 -0700648 }
649
Stephen Warren9eee07d2013-02-15 17:04:12 -0800650 __set_bit(EV_REP, kbc->idev->evbit);
651 input_set_capability(kbc->idev, EV_MSC, MSC_SCAN);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800652
Stephen Warren9eee07d2013-02-15 17:04:12 -0800653 input_set_drvdata(kbc->idev, kbc);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800654
Laxman Dewangan00eb81e2013-01-06 18:31:20 -0800655 err = devm_request_irq(&pdev->dev, kbc->irq, tegra_kbc_isr,
Rakesh Iyerfd0fc212011-12-29 19:27:44 -0800656 IRQF_NO_SUSPEND | IRQF_TRIGGER_HIGH, pdev->name, kbc);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800657 if (err) {
658 dev_err(&pdev->dev, "failed to request keyboard IRQ\n");
Laxman Dewangan00eb81e2013-01-06 18:31:20 -0800659 return err;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800660 }
661
662 disable_irq(kbc->irq);
663
664 err = input_register_device(kbc->idev);
665 if (err) {
666 dev_err(&pdev->dev, "failed to register input device\n");
Laxman Dewangan00eb81e2013-01-06 18:31:20 -0800667 return err;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800668 }
669
670 platform_set_drvdata(pdev, kbc);
Stephen Warren9eee07d2013-02-15 17:04:12 -0800671 device_init_wakeup(&pdev->dev, kbc->wakeup);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800672
673 return 0;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800674}
675
676#ifdef CONFIG_PM_SLEEP
Laxman Dewangan1c407a12013-01-06 18:30:21 -0800677static void tegra_kbc_set_keypress_interrupt(struct tegra_kbc *kbc, bool enable)
678{
679 u32 val;
680
681 val = readl(kbc->mmio + KBC_CONTROL_0);
682 if (enable)
683 val |= KBC_CONTROL_KEYPRESS_INT_EN;
684 else
685 val &= ~KBC_CONTROL_KEYPRESS_INT_EN;
686 writel(val, kbc->mmio + KBC_CONTROL_0);
687}
688
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800689static int tegra_kbc_suspend(struct device *dev)
690{
691 struct platform_device *pdev = to_platform_device(dev);
692 struct tegra_kbc *kbc = platform_get_drvdata(pdev);
693
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700694 mutex_lock(&kbc->idev->mutex);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800695 if (device_may_wakeup(&pdev->dev)) {
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700696 disable_irq(kbc->irq);
697 del_timer_sync(&kbc->timer);
698 tegra_kbc_set_fifo_interrupt(kbc, false);
699
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800700 /* Forcefully clear the interrupt status */
701 writel(0x7, kbc->mmio + KBC_INT_0);
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700702 /*
703 * Store the previous resident time of continuous polling mode.
704 * Force the keyboard into interrupt mode.
705 */
706 kbc->cp_to_wkup_dly = readl(kbc->mmio + KBC_TO_CNT_0);
707 writel(0, kbc->mmio + KBC_TO_CNT_0);
708
709 tegra_kbc_setup_wakekeys(kbc, true);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800710 msleep(30);
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700711
Rakesh Iyerfd0fc212011-12-29 19:27:44 -0800712 kbc->keypress_caused_wake = false;
Rakesh Iyerb6834b02012-01-22 23:27:54 -0800713 /* Enable keypress interrupt before going into suspend. */
714 tegra_kbc_set_keypress_interrupt(kbc, true);
Rakesh Iyerfd0fc212011-12-29 19:27:44 -0800715 enable_irq(kbc->irq);
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700716 enable_irq_wake(kbc->irq);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800717 } else {
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800718 if (kbc->idev->users)
719 tegra_kbc_stop(kbc);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800720 }
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700721 mutex_unlock(&kbc->idev->mutex);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800722
723 return 0;
724}
725
726static int tegra_kbc_resume(struct device *dev)
727{
728 struct platform_device *pdev = to_platform_device(dev);
729 struct tegra_kbc *kbc = platform_get_drvdata(pdev);
730 int err = 0;
731
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700732 mutex_lock(&kbc->idev->mutex);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800733 if (device_may_wakeup(&pdev->dev)) {
734 disable_irq_wake(kbc->irq);
735 tegra_kbc_setup_wakekeys(kbc, false);
Rakesh Iyerb6834b02012-01-22 23:27:54 -0800736 /* We will use fifo interrupts for key detection. */
737 tegra_kbc_set_keypress_interrupt(kbc, false);
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700738
739 /* Restore the resident time of continuous polling mode. */
740 writel(kbc->cp_to_wkup_dly, kbc->mmio + KBC_TO_CNT_0);
741
742 tegra_kbc_set_fifo_interrupt(kbc, true);
743
Rakesh Iyerfd0fc212011-12-29 19:27:44 -0800744 if (kbc->keypress_caused_wake && kbc->wakeup_key) {
745 /*
746 * We can't report events directly from the ISR
747 * because timekeeping is stopped when processing
748 * wakeup request and we get a nasty warning when
749 * we try to call do_gettimeofday() in evdev
750 * handler.
751 */
752 input_report_key(kbc->idev, kbc->wakeup_key, 1);
753 input_sync(kbc->idev);
754 input_report_key(kbc->idev, kbc->wakeup_key, 0);
755 input_sync(kbc->idev);
756 }
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800757 } else {
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800758 if (kbc->idev->users)
759 err = tegra_kbc_start(kbc);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800760 }
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700761 mutex_unlock(&kbc->idev->mutex);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800762
763 return err;
764}
765#endif
766
767static SIMPLE_DEV_PM_OPS(tegra_kbc_pm_ops, tegra_kbc_suspend, tegra_kbc_resume);
768
Olof Johanssona445c7f2011-12-29 09:58:16 -0800769static const struct of_device_id tegra_kbc_of_match[] = {
770 { .compatible = "nvidia,tegra20-kbc", },
771 { },
772};
773MODULE_DEVICE_TABLE(of, tegra_kbc_of_match);
774
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800775static struct platform_driver tegra_kbc_driver = {
776 .probe = tegra_kbc_probe,
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800777 .driver = {
778 .name = "tegra-kbc",
779 .owner = THIS_MODULE,
780 .pm = &tegra_kbc_pm_ops,
Olof Johanssona445c7f2011-12-29 09:58:16 -0800781 .of_match_table = tegra_kbc_of_match,
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800782 },
783};
JJ Ding5146c842011-11-29 11:08:39 -0800784module_platform_driver(tegra_kbc_driver);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800785
786MODULE_LICENSE("GPL");
787MODULE_AUTHOR("Rakesh Iyer <riyer@nvidia.com>");
788MODULE_DESCRIPTION("Tegra matrix keyboard controller driver");
789MODULE_ALIAS("platform:tegra-kbc");