blob: 4526bdd3602259d25aa908ff4feb24da8665362e [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 Warrena85442a2012-09-04 20:27:38 -070032#include <linux/input/tegra_kbc.h>
Rakesh Iyer11f5b302011-01-19 23:38:47 -080033#include <mach/clk.h>
Rakesh Iyer11f5b302011-01-19 23:38:47 -080034
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)
Rakesh Iyerb6834b02012-01-22 23:27:54 -080051#define KBC_CONTROL_KEYPRESS_INT_EN (1 << 1)
Rakesh Iyer11f5b302011-01-19 23:38:47 -080052#define KBC_CONTROL_KBC_EN (1 << 0)
53
54/* KBC Interrupt Register */
55#define KBC_INT_0 0x4
56#define KBC_INT_FIFO_CNT_INT_STATUS (1 << 2)
Rakesh Iyerfd0fc212011-12-29 19:27:44 -080057#define KBC_INT_KEYPRESS_INT_STATUS (1 << 0)
Rakesh Iyer11f5b302011-01-19 23:38:47 -080058
59#define KBC_ROW_CFG0_0 0x8
60#define KBC_COL_CFG0_0 0x18
Rakesh Iyerd0d150e2011-09-08 15:34:11 -070061#define KBC_TO_CNT_0 0x24
Rakesh Iyer11f5b302011-01-19 23:38:47 -080062#define KBC_INIT_DLY_0 0x28
63#define KBC_RPT_DLY_0 0x2c
64#define KBC_KP_ENT0_0 0x30
65#define KBC_KP_ENT1_0 0x34
66#define KBC_ROW0_MASK_0 0x38
67
68#define KBC_ROW_SHIFT 3
69
70struct tegra_kbc {
71 void __iomem *mmio;
72 struct input_dev *idev;
73 unsigned int irq;
Rakesh Iyer11f5b302011-01-19 23:38:47 -080074 spinlock_t lock;
75 unsigned int repoll_dly;
76 unsigned long cp_dly_jiffies;
Rakesh Iyerd0d150e2011-09-08 15:34:11 -070077 unsigned int cp_to_wkup_dly;
Rakesh Iyer4e8b65f2011-02-18 08:38:02 -080078 bool use_fn_map;
Rakesh Iyer34abeeb2011-04-27 23:18:15 -070079 bool use_ghost_filter;
Rakesh Iyerfd0fc212011-12-29 19:27:44 -080080 bool keypress_caused_wake;
Rakesh Iyer11f5b302011-01-19 23:38:47 -080081 const struct tegra_kbc_platform_data *pdata;
Rakesh Iyer4e8b65f2011-02-18 08:38:02 -080082 unsigned short keycode[KBC_MAX_KEY * 2];
Rakesh Iyer11f5b302011-01-19 23:38:47 -080083 unsigned short current_keys[KBC_MAX_KPENT];
84 unsigned int num_pressed_keys;
Rakesh Iyerfd0fc212011-12-29 19:27:44 -080085 u32 wakeup_key;
Rakesh Iyer11f5b302011-01-19 23:38:47 -080086 struct timer_list timer;
87 struct clk *clk;
88};
89
Rakesh Iyer11f5b302011-01-19 23:38:47 -080090static void tegra_kbc_report_released_keys(struct input_dev *input,
91 unsigned short old_keycodes[],
92 unsigned int old_num_keys,
93 unsigned short new_keycodes[],
94 unsigned int new_num_keys)
95{
96 unsigned int i, j;
97
98 for (i = 0; i < old_num_keys; i++) {
99 for (j = 0; j < new_num_keys; j++)
100 if (old_keycodes[i] == new_keycodes[j])
101 break;
102
103 if (j == new_num_keys)
104 input_report_key(input, old_keycodes[i], 0);
105 }
106}
107
108static void tegra_kbc_report_pressed_keys(struct input_dev *input,
109 unsigned char scancodes[],
110 unsigned short keycodes[],
111 unsigned int num_pressed_keys)
112{
113 unsigned int i;
114
115 for (i = 0; i < num_pressed_keys; i++) {
116 input_event(input, EV_MSC, MSC_SCAN, scancodes[i]);
117 input_report_key(input, keycodes[i], 1);
118 }
119}
120
121static void tegra_kbc_report_keys(struct tegra_kbc *kbc)
122{
123 unsigned char scancodes[KBC_MAX_KPENT];
124 unsigned short keycodes[KBC_MAX_KPENT];
125 u32 val = 0;
126 unsigned int i;
127 unsigned int num_down = 0;
Rakesh Iyer4e8b65f2011-02-18 08:38:02 -0800128 bool fn_keypress = false;
Rakesh Iyer34abeeb2011-04-27 23:18:15 -0700129 bool key_in_same_row = false;
130 bool key_in_same_col = false;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800131
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800132 for (i = 0; i < KBC_MAX_KPENT; i++) {
133 if ((i % 4) == 0)
134 val = readl(kbc->mmio + KBC_KP_ENT0_0 + i);
135
136 if (val & 0x80) {
137 unsigned int col = val & 0x07;
138 unsigned int row = (val >> 3) & 0x0f;
139 unsigned char scancode =
140 MATRIX_SCAN_CODE(row, col, KBC_ROW_SHIFT);
141
142 scancodes[num_down] = scancode;
Rakesh Iyer4e8b65f2011-02-18 08:38:02 -0800143 keycodes[num_down] = kbc->keycode[scancode];
144 /* If driver uses Fn map, do not report the Fn key. */
145 if ((keycodes[num_down] == KEY_FN) && kbc->use_fn_map)
146 fn_keypress = true;
147 else
148 num_down++;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800149 }
150
151 val >>= 8;
152 }
Rakesh Iyer4e8b65f2011-02-18 08:38:02 -0800153
154 /*
Rakesh Iyer34abeeb2011-04-27 23:18:15 -0700155 * Matrix keyboard designs are prone to keyboard ghosting.
156 * Ghosting occurs if there are 3 keys such that -
157 * any 2 of the 3 keys share a row, and any 2 of them share a column.
158 * If so ignore the key presses for this iteration.
159 */
Dmitry Torokhov95439cb2011-09-09 09:24:20 -0700160 if (kbc->use_ghost_filter && num_down >= 3) {
Rakesh Iyer34abeeb2011-04-27 23:18:15 -0700161 for (i = 0; i < num_down; i++) {
162 unsigned int j;
163 u8 curr_col = scancodes[i] & 0x07;
164 u8 curr_row = scancodes[i] >> KBC_ROW_SHIFT;
165
166 /*
167 * Find 2 keys such that one key is in the same row
168 * and the other is in the same column as the i-th key.
169 */
170 for (j = i + 1; j < num_down; j++) {
171 u8 col = scancodes[j] & 0x07;
172 u8 row = scancodes[j] >> KBC_ROW_SHIFT;
173
174 if (col == curr_col)
175 key_in_same_col = true;
176 if (row == curr_row)
177 key_in_same_row = true;
178 }
179 }
180 }
181
182 /*
Rakesh Iyer4e8b65f2011-02-18 08:38:02 -0800183 * If the platform uses Fn keymaps, translate keys on a Fn keypress.
184 * Function keycodes are KBC_MAX_KEY apart from the plain keycodes.
185 */
186 if (fn_keypress) {
187 for (i = 0; i < num_down; i++) {
188 scancodes[i] += KBC_MAX_KEY;
189 keycodes[i] = kbc->keycode[scancodes[i]];
190 }
191 }
192
Rakesh Iyer34abeeb2011-04-27 23:18:15 -0700193 /* Ignore the key presses for this iteration? */
194 if (key_in_same_col && key_in_same_row)
195 return;
196
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800197 tegra_kbc_report_released_keys(kbc->idev,
198 kbc->current_keys, kbc->num_pressed_keys,
199 keycodes, num_down);
200 tegra_kbc_report_pressed_keys(kbc->idev, scancodes, keycodes, num_down);
201 input_sync(kbc->idev);
202
203 memcpy(kbc->current_keys, keycodes, sizeof(kbc->current_keys));
204 kbc->num_pressed_keys = num_down;
205}
206
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700207static void tegra_kbc_set_fifo_interrupt(struct tegra_kbc *kbc, bool enable)
208{
209 u32 val;
210
211 val = readl(kbc->mmio + KBC_CONTROL_0);
212 if (enable)
213 val |= KBC_CONTROL_FIFO_CNT_INT_EN;
214 else
215 val &= ~KBC_CONTROL_FIFO_CNT_INT_EN;
216 writel(val, kbc->mmio + KBC_CONTROL_0);
217}
218
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800219static void tegra_kbc_keypress_timer(unsigned long data)
220{
221 struct tegra_kbc *kbc = (struct tegra_kbc *)data;
222 unsigned long flags;
223 u32 val;
224 unsigned int i;
225
Dmitry Torokhov95439cb2011-09-09 09:24:20 -0700226 spin_lock_irqsave(&kbc->lock, flags);
227
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800228 val = (readl(kbc->mmio + KBC_INT_0) >> 4) & 0xf;
229 if (val) {
230 unsigned long dly;
231
232 tegra_kbc_report_keys(kbc);
233
234 /*
235 * If more than one keys are pressed we need not wait
236 * for the repoll delay.
237 */
238 dly = (val == 1) ? kbc->repoll_dly : 1;
239 mod_timer(&kbc->timer, jiffies + msecs_to_jiffies(dly));
240 } else {
241 /* Release any pressed keys and exit the polling loop */
242 for (i = 0; i < kbc->num_pressed_keys; i++)
243 input_report_key(kbc->idev, kbc->current_keys[i], 0);
244 input_sync(kbc->idev);
245
246 kbc->num_pressed_keys = 0;
247
248 /* All keys are released so enable the keypress interrupt */
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700249 tegra_kbc_set_fifo_interrupt(kbc, true);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800250 }
Dmitry Torokhov95439cb2011-09-09 09:24:20 -0700251
252 spin_unlock_irqrestore(&kbc->lock, flags);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800253}
254
255static irqreturn_t tegra_kbc_isr(int irq, void *args)
256{
257 struct tegra_kbc *kbc = args;
Dmitry Torokhov95439cb2011-09-09 09:24:20 -0700258 unsigned long flags;
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700259 u32 val;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800260
Dmitry Torokhov95439cb2011-09-09 09:24:20 -0700261 spin_lock_irqsave(&kbc->lock, flags);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800262
263 /*
264 * Quickly bail out & reenable interrupts if the fifo threshold
265 * count interrupt wasn't the interrupt source
266 */
267 val = readl(kbc->mmio + KBC_INT_0);
268 writel(val, kbc->mmio + KBC_INT_0);
269
270 if (val & KBC_INT_FIFO_CNT_INT_STATUS) {
271 /*
Dmitry Torokhov95439cb2011-09-09 09:24:20 -0700272 * Until all keys are released, defer further processing to
273 * the polling loop in tegra_kbc_keypress_timer.
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800274 */
Dmitry Torokhov95439cb2011-09-09 09:24:20 -0700275 tegra_kbc_set_fifo_interrupt(kbc, false);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800276 mod_timer(&kbc->timer, jiffies + kbc->cp_dly_jiffies);
Rakesh Iyerfd0fc212011-12-29 19:27:44 -0800277 } else if (val & KBC_INT_KEYPRESS_INT_STATUS) {
278 /* We can be here only through system resume path */
279 kbc->keypress_caused_wake = true;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800280 }
281
Dmitry Torokhov95439cb2011-09-09 09:24:20 -0700282 spin_unlock_irqrestore(&kbc->lock, flags);
283
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800284 return IRQ_HANDLED;
285}
286
287static void tegra_kbc_setup_wakekeys(struct tegra_kbc *kbc, bool filter)
288{
289 const struct tegra_kbc_platform_data *pdata = kbc->pdata;
290 int i;
291 unsigned int rst_val;
292
Rakesh Iyerbaafb432011-05-11 14:24:10 -0700293 /* Either mask all keys or none. */
294 rst_val = (filter && !pdata->wakeup) ? ~0 : 0;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800295
296 for (i = 0; i < KBC_MAX_ROW; i++)
297 writel(rst_val, kbc->mmio + KBC_ROW0_MASK_0 + i * 4);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800298}
299
300static void tegra_kbc_config_pins(struct tegra_kbc *kbc)
301{
302 const struct tegra_kbc_platform_data *pdata = kbc->pdata;
303 int i;
304
305 for (i = 0; i < KBC_MAX_GPIO; i++) {
306 u32 r_shft = 5 * (i % 6);
307 u32 c_shft = 4 * (i % 8);
Rakesh Iyer7530c4a2011-01-28 22:05:14 -0800308 u32 r_mask = 0x1f << r_shft;
309 u32 c_mask = 0x0f << c_shft;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800310 u32 r_offs = (i / 6) * 4 + KBC_ROW_CFG0_0;
311 u32 c_offs = (i / 8) * 4 + KBC_COL_CFG0_0;
312 u32 row_cfg = readl(kbc->mmio + r_offs);
313 u32 col_cfg = readl(kbc->mmio + c_offs);
314
315 row_cfg &= ~r_mask;
316 col_cfg &= ~c_mask;
317
Shridhar Rasal023cea02012-02-03 00:27:30 -0800318 switch (pdata->pin_cfg[i].type) {
319 case PIN_CFG_ROW:
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800320 row_cfg |= ((pdata->pin_cfg[i].num << 1) | 1) << r_shft;
Shridhar Rasal023cea02012-02-03 00:27:30 -0800321 break;
322
323 case PIN_CFG_COL:
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800324 col_cfg |= ((pdata->pin_cfg[i].num << 1) | 1) << c_shft;
Shridhar Rasal023cea02012-02-03 00:27:30 -0800325 break;
326
327 case PIN_CFG_IGNORE:
328 break;
329 }
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800330
331 writel(row_cfg, kbc->mmio + r_offs);
332 writel(col_cfg, kbc->mmio + c_offs);
333 }
334}
335
336static int tegra_kbc_start(struct tegra_kbc *kbc)
337{
338 const struct tegra_kbc_platform_data *pdata = kbc->pdata;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800339 unsigned int debounce_cnt;
340 u32 val = 0;
341
Prashant Gaikwadf7624702012-06-05 09:59:39 +0530342 clk_prepare_enable(kbc->clk);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800343
344 /* Reset the KBC controller to clear all previous status.*/
345 tegra_periph_reset_assert(kbc->clk);
346 udelay(100);
347 tegra_periph_reset_deassert(kbc->clk);
348 udelay(100);
349
350 tegra_kbc_config_pins(kbc);
351 tegra_kbc_setup_wakekeys(kbc, false);
352
353 writel(pdata->repeat_cnt, kbc->mmio + KBC_RPT_DLY_0);
354
355 /* Keyboard debounce count is maximum of 12 bits. */
356 debounce_cnt = min(pdata->debounce_cnt, KBC_MAX_DEBOUNCE_CNT);
357 val = KBC_DEBOUNCE_CNT_SHIFT(debounce_cnt);
358 val |= KBC_FIFO_TH_CNT_SHIFT(1); /* set fifo interrupt threshold to 1 */
359 val |= KBC_CONTROL_FIFO_CNT_INT_EN; /* interrupt on FIFO threshold */
360 val |= KBC_CONTROL_KBC_EN; /* enable */
361 writel(val, kbc->mmio + KBC_CONTROL_0);
362
363 /*
364 * Compute the delay(ns) from interrupt mode to continuous polling
365 * mode so the timer routine is scheduled appropriately.
366 */
367 val = readl(kbc->mmio + KBC_INIT_DLY_0);
368 kbc->cp_dly_jiffies = usecs_to_jiffies((val & 0xfffff) * 32);
369
370 kbc->num_pressed_keys = 0;
371
372 /*
373 * Atomically clear out any remaining entries in the key FIFO
374 * and enable keyboard interrupts.
375 */
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800376 while (1) {
377 val = readl(kbc->mmio + KBC_INT_0);
378 val >>= 4;
379 if (!val)
380 break;
381
382 val = readl(kbc->mmio + KBC_KP_ENT0_0);
383 val = readl(kbc->mmio + KBC_KP_ENT1_0);
384 }
385 writel(0x7, kbc->mmio + KBC_INT_0);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800386
387 enable_irq(kbc->irq);
388
389 return 0;
390}
391
392static void tegra_kbc_stop(struct tegra_kbc *kbc)
393{
394 unsigned long flags;
395 u32 val;
396
397 spin_lock_irqsave(&kbc->lock, flags);
398 val = readl(kbc->mmio + KBC_CONTROL_0);
399 val &= ~1;
400 writel(val, kbc->mmio + KBC_CONTROL_0);
401 spin_unlock_irqrestore(&kbc->lock, flags);
402
403 disable_irq(kbc->irq);
404 del_timer_sync(&kbc->timer);
405
Prashant Gaikwadf7624702012-06-05 09:59:39 +0530406 clk_disable_unprepare(kbc->clk);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800407}
408
409static int tegra_kbc_open(struct input_dev *dev)
410{
411 struct tegra_kbc *kbc = input_get_drvdata(dev);
412
413 return tegra_kbc_start(kbc);
414}
415
416static void tegra_kbc_close(struct input_dev *dev)
417{
418 struct tegra_kbc *kbc = input_get_drvdata(dev);
419
420 return tegra_kbc_stop(kbc);
421}
422
Bill Pemberton5298cc42012-11-23 21:38:25 -0800423static bool
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800424tegra_kbc_check_pin_cfg(const struct tegra_kbc_platform_data *pdata,
425 struct device *dev, unsigned int *num_rows)
426{
427 int i;
428
429 *num_rows = 0;
430
431 for (i = 0; i < KBC_MAX_GPIO; i++) {
432 const struct tegra_kbc_pin_cfg *pin_cfg = &pdata->pin_cfg[i];
433
Shridhar Rasal023cea02012-02-03 00:27:30 -0800434 switch (pin_cfg->type) {
435 case PIN_CFG_ROW:
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800436 if (pin_cfg->num >= KBC_MAX_ROW) {
437 dev_err(dev,
438 "pin_cfg[%d]: invalid row number %d\n",
439 i, pin_cfg->num);
440 return false;
441 }
442 (*num_rows)++;
Shridhar Rasal023cea02012-02-03 00:27:30 -0800443 break;
444
445 case PIN_CFG_COL:
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800446 if (pin_cfg->num >= KBC_MAX_COL) {
447 dev_err(dev,
448 "pin_cfg[%d]: invalid column number %d\n",
449 i, pin_cfg->num);
450 return false;
451 }
Shridhar Rasal023cea02012-02-03 00:27:30 -0800452 break;
453
454 case PIN_CFG_IGNORE:
455 break;
456
457 default:
458 dev_err(dev,
459 "pin_cfg[%d]: invalid entry type %d\n",
460 pin_cfg->type, pin_cfg->num);
461 return false;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800462 }
463 }
464
465 return true;
466}
467
Olof Johanssona445c7f2011-12-29 09:58:16 -0800468#ifdef CONFIG_OF
Bill Pemberton5298cc42012-11-23 21:38:25 -0800469static struct tegra_kbc_platform_data *tegra_kbc_dt_parse_pdata(
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700470 struct platform_device *pdev)
Olof Johanssona445c7f2011-12-29 09:58:16 -0800471{
472 struct tegra_kbc_platform_data *pdata;
473 struct device_node *np = pdev->dev.of_node;
Olof Johansson145e9732012-03-13 21:36:29 -0700474 u32 prop;
475 int i;
Laxman Dewangan88390242013-01-06 18:32:22 -0800476 u32 num_rows = 0;
477 u32 num_cols = 0;
478 u32 cols_cfg[KBC_MAX_GPIO];
479 u32 rows_cfg[KBC_MAX_GPIO];
480 int proplen;
481 int ret;
Olof Johanssona445c7f2011-12-29 09:58:16 -0800482
Laxman Dewangan88390242013-01-06 18:32:22 -0800483 if (!np) {
484 dev_err(&pdev->dev, "device tree data is missing\n");
485 return ERR_PTR(-ENOENT);
486 }
Olof Johanssona445c7f2011-12-29 09:58:16 -0800487
Laxman Dewangan00eb81e2013-01-06 18:31:20 -0800488 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
Olof Johanssona445c7f2011-12-29 09:58:16 -0800489 if (!pdata)
Laxman Dewangan88390242013-01-06 18:32:22 -0800490 return ERR_PTR(-ENOMEM);
Olof Johanssona445c7f2011-12-29 09:58:16 -0800491
Olof Johansson145e9732012-03-13 21:36:29 -0700492 if (!of_property_read_u32(np, "nvidia,debounce-delay-ms", &prop))
Olof Johanssona445c7f2011-12-29 09:58:16 -0800493 pdata->debounce_cnt = prop;
494
Olof Johansson145e9732012-03-13 21:36:29 -0700495 if (!of_property_read_u32(np, "nvidia,repeat-delay-ms", &prop))
Olof Johanssona445c7f2011-12-29 09:58:16 -0800496 pdata->repeat_cnt = prop;
497
Olof Johansson145e9732012-03-13 21:36:29 -0700498 if (of_find_property(np, "nvidia,needs-ghost-filter", NULL))
Olof Johanssona445c7f2011-12-29 09:58:16 -0800499 pdata->use_ghost_filter = true;
500
Olof Johansson145e9732012-03-13 21:36:29 -0700501 if (of_find_property(np, "nvidia,wakeup-source", NULL))
Olof Johanssona445c7f2011-12-29 09:58:16 -0800502 pdata->wakeup = true;
503
Laxman Dewangan88390242013-01-06 18:32:22 -0800504 if (!of_get_property(np, "nvidia,kbc-row-pins", &proplen)) {
505 dev_err(&pdev->dev, "property nvidia,kbc-row-pins not found\n");
506 return ERR_PTR(-ENOENT);
507 }
508 num_rows = proplen / sizeof(u32);
509
510 if (!of_get_property(np, "nvidia,kbc-col-pins", &proplen)) {
511 dev_err(&pdev->dev, "property nvidia,kbc-col-pins not found\n");
512 return ERR_PTR(-ENOENT);
513 }
514 num_cols = proplen / sizeof(u32);
515
516 if (!of_get_property(np, "linux,keymap", &proplen)) {
517 dev_err(&pdev->dev, "property linux,keymap not found\n");
518 return ERR_PTR(-ENOENT);
Olof Johanssona445c7f2011-12-29 09:58:16 -0800519 }
520
Laxman Dewangan88390242013-01-06 18:32:22 -0800521 if (!num_rows || !num_cols || ((num_rows + num_cols) > KBC_MAX_GPIO)) {
522 dev_err(&pdev->dev,
523 "keypad rows/columns not porperly specified\n");
524 return ERR_PTR(-EINVAL);
525 }
526
527 /* Set all pins as non-configured */
528 for (i = 0; i < KBC_MAX_GPIO; i++)
529 pdata->pin_cfg[i].type = PIN_CFG_IGNORE;
530
531 ret = of_property_read_u32_array(np, "nvidia,kbc-row-pins",
532 rows_cfg, num_rows);
533 if (ret < 0) {
534 dev_err(&pdev->dev, "Rows configurations are not proper\n");
535 return ERR_PTR(-EINVAL);
536 }
537
538 ret = of_property_read_u32_array(np, "nvidia,kbc-col-pins",
539 cols_cfg, num_cols);
540 if (ret < 0) {
541 dev_err(&pdev->dev, "Cols configurations are not proper\n");
542 return ERR_PTR(-EINVAL);
543 }
544
545 for (i = 0; i < num_rows; i++) {
546 pdata->pin_cfg[rows_cfg[i]].type = PIN_CFG_ROW;
547 pdata->pin_cfg[rows_cfg[i]].num = i;
548 }
549
550 for (i = 0; i < num_cols; i++) {
551 pdata->pin_cfg[cols_cfg[i]].type = PIN_CFG_COL;
552 pdata->pin_cfg[cols_cfg[i]].num = i;
Olof Johanssona445c7f2011-12-29 09:58:16 -0800553 }
554
555 return pdata;
556}
557#else
558static inline struct tegra_kbc_platform_data *tegra_kbc_dt_parse_pdata(
559 struct platform_device *pdev)
560{
Laxman Dewangan88390242013-01-06 18:32:22 -0800561 dev_err(&pdev->dev, "platform data is missing\n");
562 return ERR_PTR(-EINVAL);
Olof Johanssona445c7f2011-12-29 09:58:16 -0800563}
564#endif
565
Bill Pemberton5298cc42012-11-23 21:38:25 -0800566static int tegra_kbc_probe(struct platform_device *pdev)
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800567{
568 const struct tegra_kbc_platform_data *pdata = pdev->dev.platform_data;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800569 struct tegra_kbc *kbc;
570 struct input_dev *input_dev;
571 struct resource *res;
572 int irq;
573 int err;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800574 int num_rows = 0;
575 unsigned int debounce_cnt;
576 unsigned int scan_time_rows;
Laxman Dewangan914e5972013-01-06 18:34:48 -0800577 unsigned int keymap_rows = KBC_MAX_KEY;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800578
579 if (!pdata)
Olof Johanssona445c7f2011-12-29 09:58:16 -0800580 pdata = tegra_kbc_dt_parse_pdata(pdev);
581
Laxman Dewangan88390242013-01-06 18:32:22 -0800582 if (IS_ERR(pdata))
583 return PTR_ERR(pdata);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800584
Laxman Dewangan00eb81e2013-01-06 18:31:20 -0800585 if (!tegra_kbc_check_pin_cfg(pdata, &pdev->dev, &num_rows))
586 return -EINVAL;
587
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800588 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
589 if (!res) {
590 dev_err(&pdev->dev, "failed to get I/O memory\n");
Laxman Dewangan00eb81e2013-01-06 18:31:20 -0800591 return -ENXIO;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800592 }
593
594 irq = platform_get_irq(pdev, 0);
595 if (irq < 0) {
596 dev_err(&pdev->dev, "failed to get keyboard IRQ\n");
Laxman Dewangan00eb81e2013-01-06 18:31:20 -0800597 return -ENXIO;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800598 }
599
Laxman Dewangan00eb81e2013-01-06 18:31:20 -0800600 kbc = devm_kzalloc(&pdev->dev, sizeof(*kbc), GFP_KERNEL);
601 if (!kbc) {
602 dev_err(&pdev->dev, "failed to alloc memory for kbc\n");
603 return -ENOMEM;
604 }
605
606 input_dev = devm_input_allocate_device(&pdev->dev);
607 if (!input_dev) {
608 dev_err(&pdev->dev, "failed to allocate input device\n");
609 return -ENOMEM;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800610 }
611
612 kbc->pdata = pdata;
613 kbc->idev = input_dev;
614 kbc->irq = irq;
615 spin_lock_init(&kbc->lock);
616 setup_timer(&kbc->timer, tegra_kbc_keypress_timer, (unsigned long)kbc);
617
Laxman Dewangan00eb81e2013-01-06 18:31:20 -0800618 kbc->mmio = devm_request_and_ioremap(&pdev->dev, res);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800619 if (!kbc->mmio) {
Laxman Dewangan00eb81e2013-01-06 18:31:20 -0800620 dev_err(&pdev->dev, "Cannot request memregion/iomap address\n");
621 return -EBUSY;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800622 }
623
Laxman Dewangan00eb81e2013-01-06 18:31:20 -0800624 kbc->clk = devm_clk_get(&pdev->dev, NULL);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800625 if (IS_ERR(kbc->clk)) {
626 dev_err(&pdev->dev, "failed to get keyboard clock\n");
Laxman Dewangan00eb81e2013-01-06 18:31:20 -0800627 return PTR_ERR(kbc->clk);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800628 }
629
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800630 /*
631 * The time delay between two consecutive reads of the FIFO is
632 * the sum of the repeat time and the time taken for scanning
633 * the rows. There is an additional delay before the row scanning
634 * starts. The repoll delay is computed in milliseconds.
635 */
636 debounce_cnt = min(pdata->debounce_cnt, KBC_MAX_DEBOUNCE_CNT);
637 scan_time_rows = (KBC_ROW_SCAN_TIME + debounce_cnt) * num_rows;
638 kbc->repoll_dly = KBC_ROW_SCAN_DLY + scan_time_rows + pdata->repeat_cnt;
Rakesh Iyer3f277572011-07-30 12:01:48 -0700639 kbc->repoll_dly = DIV_ROUND_UP(kbc->repoll_dly, KBC_CYCLE_MS);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800640
Dmitry Torokhov19328112012-05-10 22:37:08 -0700641 kbc->wakeup_key = pdata->wakeup_key;
642 kbc->use_fn_map = pdata->use_fn_map;
643 kbc->use_ghost_filter = pdata->use_ghost_filter;
644
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800645 input_dev->name = pdev->name;
646 input_dev->id.bustype = BUS_HOST;
647 input_dev->dev.parent = &pdev->dev;
648 input_dev->open = tegra_kbc_open;
649 input_dev->close = tegra_kbc_close;
650
Laxman Dewangan914e5972013-01-06 18:34:48 -0800651 if (pdata->keymap_data && pdata->use_fn_map)
652 keymap_rows *= 2;
653
654 err = matrix_keypad_build_keymap(pdata->keymap_data, NULL,
655 keymap_rows, KBC_MAX_COL,
656 kbc->keycode, input_dev);
Dmitry Torokhov19328112012-05-10 22:37:08 -0700657 if (err) {
Dmitry Torokhovb45c8f32012-05-10 22:37:15 -0700658 dev_err(&pdev->dev, "failed to setup keymap\n");
Laxman Dewangan00eb81e2013-01-06 18:31:20 -0800659 return err;
Dmitry Torokhov19328112012-05-10 22:37:08 -0700660 }
661
662 __set_bit(EV_REP, input_dev->evbit);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800663 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
664
Dmitry Torokhov19328112012-05-10 22:37:08 -0700665 input_set_drvdata(input_dev, kbc);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800666
Laxman Dewangan00eb81e2013-01-06 18:31:20 -0800667 err = devm_request_irq(&pdev->dev, kbc->irq, tegra_kbc_isr,
Rakesh Iyerfd0fc212011-12-29 19:27:44 -0800668 IRQF_NO_SUSPEND | IRQF_TRIGGER_HIGH, pdev->name, kbc);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800669 if (err) {
670 dev_err(&pdev->dev, "failed to request keyboard IRQ\n");
Laxman Dewangan00eb81e2013-01-06 18:31:20 -0800671 return err;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800672 }
673
674 disable_irq(kbc->irq);
675
676 err = input_register_device(kbc->idev);
677 if (err) {
678 dev_err(&pdev->dev, "failed to register input device\n");
Laxman Dewangan00eb81e2013-01-06 18:31:20 -0800679 return err;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800680 }
681
682 platform_set_drvdata(pdev, kbc);
683 device_init_wakeup(&pdev->dev, pdata->wakeup);
684
685 return 0;
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800686}
687
688#ifdef CONFIG_PM_SLEEP
Laxman Dewangan1c407a12013-01-06 18:30:21 -0800689static void tegra_kbc_set_keypress_interrupt(struct tegra_kbc *kbc, bool enable)
690{
691 u32 val;
692
693 val = readl(kbc->mmio + KBC_CONTROL_0);
694 if (enable)
695 val |= KBC_CONTROL_KEYPRESS_INT_EN;
696 else
697 val &= ~KBC_CONTROL_KEYPRESS_INT_EN;
698 writel(val, kbc->mmio + KBC_CONTROL_0);
699}
700
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800701static int tegra_kbc_suspend(struct device *dev)
702{
703 struct platform_device *pdev = to_platform_device(dev);
704 struct tegra_kbc *kbc = platform_get_drvdata(pdev);
705
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700706 mutex_lock(&kbc->idev->mutex);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800707 if (device_may_wakeup(&pdev->dev)) {
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700708 disable_irq(kbc->irq);
709 del_timer_sync(&kbc->timer);
710 tegra_kbc_set_fifo_interrupt(kbc, false);
711
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800712 /* Forcefully clear the interrupt status */
713 writel(0x7, kbc->mmio + KBC_INT_0);
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700714 /*
715 * Store the previous resident time of continuous polling mode.
716 * Force the keyboard into interrupt mode.
717 */
718 kbc->cp_to_wkup_dly = readl(kbc->mmio + KBC_TO_CNT_0);
719 writel(0, kbc->mmio + KBC_TO_CNT_0);
720
721 tegra_kbc_setup_wakekeys(kbc, true);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800722 msleep(30);
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700723
Rakesh Iyerfd0fc212011-12-29 19:27:44 -0800724 kbc->keypress_caused_wake = false;
Rakesh Iyerb6834b02012-01-22 23:27:54 -0800725 /* Enable keypress interrupt before going into suspend. */
726 tegra_kbc_set_keypress_interrupt(kbc, true);
Rakesh Iyerfd0fc212011-12-29 19:27:44 -0800727 enable_irq(kbc->irq);
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700728 enable_irq_wake(kbc->irq);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800729 } else {
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800730 if (kbc->idev->users)
731 tegra_kbc_stop(kbc);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800732 }
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700733 mutex_unlock(&kbc->idev->mutex);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800734
735 return 0;
736}
737
738static int tegra_kbc_resume(struct device *dev)
739{
740 struct platform_device *pdev = to_platform_device(dev);
741 struct tegra_kbc *kbc = platform_get_drvdata(pdev);
742 int err = 0;
743
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700744 mutex_lock(&kbc->idev->mutex);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800745 if (device_may_wakeup(&pdev->dev)) {
746 disable_irq_wake(kbc->irq);
747 tegra_kbc_setup_wakekeys(kbc, false);
Rakesh Iyerb6834b02012-01-22 23:27:54 -0800748 /* We will use fifo interrupts for key detection. */
749 tegra_kbc_set_keypress_interrupt(kbc, false);
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700750
751 /* Restore the resident time of continuous polling mode. */
752 writel(kbc->cp_to_wkup_dly, kbc->mmio + KBC_TO_CNT_0);
753
754 tegra_kbc_set_fifo_interrupt(kbc, true);
755
Rakesh Iyerfd0fc212011-12-29 19:27:44 -0800756 if (kbc->keypress_caused_wake && kbc->wakeup_key) {
757 /*
758 * We can't report events directly from the ISR
759 * because timekeeping is stopped when processing
760 * wakeup request and we get a nasty warning when
761 * we try to call do_gettimeofday() in evdev
762 * handler.
763 */
764 input_report_key(kbc->idev, kbc->wakeup_key, 1);
765 input_sync(kbc->idev);
766 input_report_key(kbc->idev, kbc->wakeup_key, 0);
767 input_sync(kbc->idev);
768 }
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800769 } else {
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800770 if (kbc->idev->users)
771 err = tegra_kbc_start(kbc);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800772 }
Rakesh Iyerd0d150e2011-09-08 15:34:11 -0700773 mutex_unlock(&kbc->idev->mutex);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800774
775 return err;
776}
777#endif
778
779static SIMPLE_DEV_PM_OPS(tegra_kbc_pm_ops, tegra_kbc_suspend, tegra_kbc_resume);
780
Olof Johanssona445c7f2011-12-29 09:58:16 -0800781static const struct of_device_id tegra_kbc_of_match[] = {
782 { .compatible = "nvidia,tegra20-kbc", },
783 { },
784};
785MODULE_DEVICE_TABLE(of, tegra_kbc_of_match);
786
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800787static struct platform_driver tegra_kbc_driver = {
788 .probe = tegra_kbc_probe,
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800789 .driver = {
790 .name = "tegra-kbc",
791 .owner = THIS_MODULE,
792 .pm = &tegra_kbc_pm_ops,
Olof Johanssona445c7f2011-12-29 09:58:16 -0800793 .of_match_table = tegra_kbc_of_match,
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800794 },
795};
JJ Ding5146c842011-11-29 11:08:39 -0800796module_platform_driver(tegra_kbc_driver);
Rakesh Iyer11f5b302011-01-19 23:38:47 -0800797
798MODULE_LICENSE("GPL");
799MODULE_AUTHOR("Rakesh Iyer <riyer@nvidia.com>");
800MODULE_DESCRIPTION("Tegra matrix keyboard controller driver");
801MODULE_ALIAS("platform:tegra-kbc");