2 * Keyboard class input driver for the NVIDIA Tegra SoC internal matrix
5 * Copyright (c) 2009-2011, NVIDIA Corporation.
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.
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
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.
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/input.h>
25 #include <linux/platform_device.h>
26 #include <linux/delay.h>
28 #include <linux/interrupt.h>
30 #include <linux/clk.h>
31 #include <linux/slab.h>
35 #define KBC_MAX_DEBOUNCE_CNT 0x3ffu
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
41 /* KBC uses a 32KHz clock so a cycle = 1/32Khz */
42 #define KBC_CYCLE_MS 32
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_KEYPRESS_INT_EN (1 << 1)
52 #define KBC_CONTROL_KBC_EN (1 << 0)
54 /* KBC Interrupt Register */
56 #define KBC_INT_FIFO_CNT_INT_STATUS (1 << 2)
57 #define KBC_INT_KEYPRESS_INT_STATUS (1 << 0)
59 #define KBC_ROW_CFG0_0 0x8
60 #define KBC_COL_CFG0_0 0x18
61 #define KBC_TO_CNT_0 0x24
62 #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
68 #define KBC_ROW_SHIFT 3
72 struct input_dev
*idev
;
75 unsigned int repoll_dly
;
76 unsigned long cp_dly_jiffies
;
77 unsigned int cp_to_wkup_dly
;
79 bool use_ghost_filter
;
80 bool keypress_caused_wake
;
81 const struct tegra_kbc_platform_data
*pdata
;
82 unsigned short keycode
[KBC_MAX_KEY
* 2];
83 unsigned short current_keys
[KBC_MAX_KPENT
];
84 unsigned int num_pressed_keys
;
86 struct timer_list timer
;
90 static const u32 tegra_kbc_default_keymap
[] __devinitdata
= {
97 KEY(1, 7, KEY_LEFTMETA
),
99 KEY(2, 6, KEY_RIGHTALT
),
100 KEY(2, 7, KEY_LEFTALT
),
117 KEY(4, 7, KEY_SPACE
),
126 KEY(5, 7, KEY_BACKSLASH
),
128 KEY(6, 0, KEY_MINUS
),
134 KEY(6, 6, KEY_COMMA
),
137 KEY(7, 1, KEY_EQUAL
),
138 KEY(7, 2, KEY_RIGHTBRACE
),
139 KEY(7, 3, KEY_ENTER
),
142 KEY(8, 4, KEY_RIGHTSHIFT
),
143 KEY(8, 5, KEY_LEFTSHIFT
),
145 KEY(9, 5, KEY_RIGHTCTRL
),
146 KEY(9, 7, KEY_LEFTCTRL
),
148 KEY(11, 0, KEY_LEFTBRACE
),
150 KEY(11, 2, KEY_APOSTROPHE
),
151 KEY(11, 3, KEY_SEMICOLON
),
152 KEY(11, 4, KEY_SLASH
),
157 KEY(12, 2, KEY_BACKSPACE
),
161 KEY(12, 6, KEY_PRINT
),
162 KEY(12, 7, KEY_PAUSE
),
164 KEY(13, 0, KEY_INSERT
),
165 KEY(13, 1, KEY_DELETE
),
166 KEY(13, 3, KEY_PAGEUP
),
167 KEY(13, 4, KEY_PAGEDOWN
),
168 KEY(13, 5, KEY_RIGHT
),
169 KEY(13, 6, KEY_DOWN
),
170 KEY(13, 7, KEY_LEFT
),
182 KEY(15, 1, KEY_GRAVE
),
187 KEY(15, 6, KEY_CAPSLOCK
),
190 /* Software Handled Function Keys */
198 KEY(22, 1, KEY_KPSLASH
),
205 KEY(27, 1, KEY_KPASTERISK
),
206 KEY(27, 3, KEY_KPMINUS
),
207 KEY(27, 4, KEY_KPPLUS
),
208 KEY(27, 5, KEY_KPDOT
),
210 KEY(28, 5, KEY_VOLUMEUP
),
212 KEY(29, 3, KEY_HOME
),
214 KEY(29, 5, KEY_BRIGHTNESSDOWN
),
215 KEY(29, 6, KEY_VOLUMEDOWN
),
216 KEY(29, 7, KEY_BRIGHTNESSUP
),
218 KEY(30, 0, KEY_NUMLOCK
),
219 KEY(30, 1, KEY_SCROLLLOCK
),
220 KEY(30, 2, KEY_MUTE
),
222 KEY(31, 4, KEY_HELP
),
226 struct matrix_keymap_data tegra_kbc_default_keymap_data __devinitdata
= {
227 .keymap
= tegra_kbc_default_keymap
,
228 .keymap_size
= ARRAY_SIZE(tegra_kbc_default_keymap
),
231 static void tegra_kbc_report_released_keys(struct input_dev
*input
,
232 unsigned short old_keycodes
[],
233 unsigned int old_num_keys
,
234 unsigned short new_keycodes
[],
235 unsigned int new_num_keys
)
239 for (i
= 0; i
< old_num_keys
; i
++) {
240 for (j
= 0; j
< new_num_keys
; j
++)
241 if (old_keycodes
[i
] == new_keycodes
[j
])
244 if (j
== new_num_keys
)
245 input_report_key(input
, old_keycodes
[i
], 0);
249 static void tegra_kbc_report_pressed_keys(struct input_dev
*input
,
250 unsigned char scancodes
[],
251 unsigned short keycodes
[],
252 unsigned int num_pressed_keys
)
256 for (i
= 0; i
< num_pressed_keys
; i
++) {
257 input_event(input
, EV_MSC
, MSC_SCAN
, scancodes
[i
]);
258 input_report_key(input
, keycodes
[i
], 1);
262 static void tegra_kbc_report_keys(struct tegra_kbc
*kbc
)
264 unsigned char scancodes
[KBC_MAX_KPENT
];
265 unsigned short keycodes
[KBC_MAX_KPENT
];
268 unsigned int num_down
= 0;
269 bool fn_keypress
= false;
270 bool key_in_same_row
= false;
271 bool key_in_same_col
= false;
273 for (i
= 0; i
< KBC_MAX_KPENT
; i
++) {
275 val
= readl(kbc
->mmio
+ KBC_KP_ENT0_0
+ i
);
278 unsigned int col
= val
& 0x07;
279 unsigned int row
= (val
>> 3) & 0x0f;
280 unsigned char scancode
=
281 MATRIX_SCAN_CODE(row
, col
, KBC_ROW_SHIFT
);
283 scancodes
[num_down
] = scancode
;
284 keycodes
[num_down
] = kbc
->keycode
[scancode
];
285 /* If driver uses Fn map, do not report the Fn key. */
286 if ((keycodes
[num_down
] == KEY_FN
) && kbc
->use_fn_map
)
296 * Matrix keyboard designs are prone to keyboard ghosting.
297 * Ghosting occurs if there are 3 keys such that -
298 * any 2 of the 3 keys share a row, and any 2 of them share a column.
299 * If so ignore the key presses for this iteration.
301 if (kbc
->use_ghost_filter
&& num_down
>= 3) {
302 for (i
= 0; i
< num_down
; i
++) {
304 u8 curr_col
= scancodes
[i
] & 0x07;
305 u8 curr_row
= scancodes
[i
] >> KBC_ROW_SHIFT
;
308 * Find 2 keys such that one key is in the same row
309 * and the other is in the same column as the i-th key.
311 for (j
= i
+ 1; j
< num_down
; j
++) {
312 u8 col
= scancodes
[j
] & 0x07;
313 u8 row
= scancodes
[j
] >> KBC_ROW_SHIFT
;
316 key_in_same_col
= true;
318 key_in_same_row
= true;
324 * If the platform uses Fn keymaps, translate keys on a Fn keypress.
325 * Function keycodes are KBC_MAX_KEY apart from the plain keycodes.
328 for (i
= 0; i
< num_down
; i
++) {
329 scancodes
[i
] += KBC_MAX_KEY
;
330 keycodes
[i
] = kbc
->keycode
[scancodes
[i
]];
334 /* Ignore the key presses for this iteration? */
335 if (key_in_same_col
&& key_in_same_row
)
338 tegra_kbc_report_released_keys(kbc
->idev
,
339 kbc
->current_keys
, kbc
->num_pressed_keys
,
341 tegra_kbc_report_pressed_keys(kbc
->idev
, scancodes
, keycodes
, num_down
);
342 input_sync(kbc
->idev
);
344 memcpy(kbc
->current_keys
, keycodes
, sizeof(kbc
->current_keys
));
345 kbc
->num_pressed_keys
= num_down
;
348 static void tegra_kbc_set_fifo_interrupt(struct tegra_kbc
*kbc
, bool enable
)
352 val
= readl(kbc
->mmio
+ KBC_CONTROL_0
);
354 val
|= KBC_CONTROL_FIFO_CNT_INT_EN
;
356 val
&= ~KBC_CONTROL_FIFO_CNT_INT_EN
;
357 writel(val
, kbc
->mmio
+ KBC_CONTROL_0
);
360 static void tegra_kbc_set_keypress_interrupt(struct tegra_kbc
*kbc
, bool enable
)
364 val
= readl(kbc
->mmio
+ KBC_CONTROL_0
);
366 val
|= KBC_CONTROL_KEYPRESS_INT_EN
;
368 val
&= ~KBC_CONTROL_KEYPRESS_INT_EN
;
369 writel(val
, kbc
->mmio
+ KBC_CONTROL_0
);
372 static void tegra_kbc_keypress_timer(unsigned long data
)
374 struct tegra_kbc
*kbc
= (struct tegra_kbc
*)data
;
379 spin_lock_irqsave(&kbc
->lock
, flags
);
381 val
= (readl(kbc
->mmio
+ KBC_INT_0
) >> 4) & 0xf;
385 tegra_kbc_report_keys(kbc
);
388 * If more than one keys are pressed we need not wait
389 * for the repoll delay.
391 dly
= (val
== 1) ? kbc
->repoll_dly
: 1;
392 mod_timer(&kbc
->timer
, jiffies
+ msecs_to_jiffies(dly
));
394 /* Release any pressed keys and exit the polling loop */
395 for (i
= 0; i
< kbc
->num_pressed_keys
; i
++)
396 input_report_key(kbc
->idev
, kbc
->current_keys
[i
], 0);
397 input_sync(kbc
->idev
);
399 kbc
->num_pressed_keys
= 0;
401 /* All keys are released so enable the keypress interrupt */
402 tegra_kbc_set_fifo_interrupt(kbc
, true);
405 spin_unlock_irqrestore(&kbc
->lock
, flags
);
408 static irqreturn_t
tegra_kbc_isr(int irq
, void *args
)
410 struct tegra_kbc
*kbc
= args
;
414 spin_lock_irqsave(&kbc
->lock
, flags
);
417 * Quickly bail out & reenable interrupts if the fifo threshold
418 * count interrupt wasn't the interrupt source
420 val
= readl(kbc
->mmio
+ KBC_INT_0
);
421 writel(val
, kbc
->mmio
+ KBC_INT_0
);
423 if (val
& KBC_INT_FIFO_CNT_INT_STATUS
) {
425 * Until all keys are released, defer further processing to
426 * the polling loop in tegra_kbc_keypress_timer.
428 tegra_kbc_set_fifo_interrupt(kbc
, false);
429 mod_timer(&kbc
->timer
, jiffies
+ kbc
->cp_dly_jiffies
);
430 } else if (val
& KBC_INT_KEYPRESS_INT_STATUS
) {
431 /* We can be here only through system resume path */
432 kbc
->keypress_caused_wake
= true;
435 spin_unlock_irqrestore(&kbc
->lock
, flags
);
440 static void tegra_kbc_setup_wakekeys(struct tegra_kbc
*kbc
, bool filter
)
442 const struct tegra_kbc_platform_data
*pdata
= kbc
->pdata
;
444 unsigned int rst_val
;
446 /* Either mask all keys or none. */
447 rst_val
= (filter
&& !pdata
->wakeup
) ? ~0 : 0;
449 for (i
= 0; i
< KBC_MAX_ROW
; i
++)
450 writel(rst_val
, kbc
->mmio
+ KBC_ROW0_MASK_0
+ i
* 4);
453 static void tegra_kbc_config_pins(struct tegra_kbc
*kbc
)
455 const struct tegra_kbc_platform_data
*pdata
= kbc
->pdata
;
458 for (i
= 0; i
< KBC_MAX_GPIO
; i
++) {
459 u32 r_shft
= 5 * (i
% 6);
460 u32 c_shft
= 4 * (i
% 8);
461 u32 r_mask
= 0x1f << r_shft
;
462 u32 c_mask
= 0x0f << c_shft
;
463 u32 r_offs
= (i
/ 6) * 4 + KBC_ROW_CFG0_0
;
464 u32 c_offs
= (i
/ 8) * 4 + KBC_COL_CFG0_0
;
465 u32 row_cfg
= readl(kbc
->mmio
+ r_offs
);
466 u32 col_cfg
= readl(kbc
->mmio
+ c_offs
);
471 switch (pdata
->pin_cfg
[i
].type
) {
473 row_cfg
|= ((pdata
->pin_cfg
[i
].num
<< 1) | 1) << r_shft
;
477 col_cfg
|= ((pdata
->pin_cfg
[i
].num
<< 1) | 1) << c_shft
;
484 writel(row_cfg
, kbc
->mmio
+ r_offs
);
485 writel(col_cfg
, kbc
->mmio
+ c_offs
);
489 static int tegra_kbc_start(struct tegra_kbc
*kbc
)
491 const struct tegra_kbc_platform_data
*pdata
= kbc
->pdata
;
492 unsigned int debounce_cnt
;
495 clk_enable(kbc
->clk
);
497 /* Reset the KBC controller to clear all previous status.*/
498 tegra_periph_reset_assert(kbc
->clk
);
500 tegra_periph_reset_deassert(kbc
->clk
);
503 tegra_kbc_config_pins(kbc
);
504 tegra_kbc_setup_wakekeys(kbc
, false);
506 writel(pdata
->repeat_cnt
, kbc
->mmio
+ KBC_RPT_DLY_0
);
508 /* Keyboard debounce count is maximum of 12 bits. */
509 debounce_cnt
= min(pdata
->debounce_cnt
, KBC_MAX_DEBOUNCE_CNT
);
510 val
= KBC_DEBOUNCE_CNT_SHIFT(debounce_cnt
);
511 val
|= KBC_FIFO_TH_CNT_SHIFT(1); /* set fifo interrupt threshold to 1 */
512 val
|= KBC_CONTROL_FIFO_CNT_INT_EN
; /* interrupt on FIFO threshold */
513 val
|= KBC_CONTROL_KBC_EN
; /* enable */
514 writel(val
, kbc
->mmio
+ KBC_CONTROL_0
);
517 * Compute the delay(ns) from interrupt mode to continuous polling
518 * mode so the timer routine is scheduled appropriately.
520 val
= readl(kbc
->mmio
+ KBC_INIT_DLY_0
);
521 kbc
->cp_dly_jiffies
= usecs_to_jiffies((val
& 0xfffff) * 32);
523 kbc
->num_pressed_keys
= 0;
526 * Atomically clear out any remaining entries in the key FIFO
527 * and enable keyboard interrupts.
530 val
= readl(kbc
->mmio
+ KBC_INT_0
);
535 val
= readl(kbc
->mmio
+ KBC_KP_ENT0_0
);
536 val
= readl(kbc
->mmio
+ KBC_KP_ENT1_0
);
538 writel(0x7, kbc
->mmio
+ KBC_INT_0
);
540 enable_irq(kbc
->irq
);
545 static void tegra_kbc_stop(struct tegra_kbc
*kbc
)
550 spin_lock_irqsave(&kbc
->lock
, flags
);
551 val
= readl(kbc
->mmio
+ KBC_CONTROL_0
);
553 writel(val
, kbc
->mmio
+ KBC_CONTROL_0
);
554 spin_unlock_irqrestore(&kbc
->lock
, flags
);
556 disable_irq(kbc
->irq
);
557 del_timer_sync(&kbc
->timer
);
559 clk_disable(kbc
->clk
);
562 static int tegra_kbc_open(struct input_dev
*dev
)
564 struct tegra_kbc
*kbc
= input_get_drvdata(dev
);
566 return tegra_kbc_start(kbc
);
569 static void tegra_kbc_close(struct input_dev
*dev
)
571 struct tegra_kbc
*kbc
= input_get_drvdata(dev
);
573 return tegra_kbc_stop(kbc
);
576 static bool __devinit
577 tegra_kbc_check_pin_cfg(const struct tegra_kbc_platform_data
*pdata
,
578 struct device
*dev
, unsigned int *num_rows
)
584 for (i
= 0; i
< KBC_MAX_GPIO
; i
++) {
585 const struct tegra_kbc_pin_cfg
*pin_cfg
= &pdata
->pin_cfg
[i
];
587 switch (pin_cfg
->type
) {
589 if (pin_cfg
->num
>= KBC_MAX_ROW
) {
591 "pin_cfg[%d]: invalid row number %d\n",
599 if (pin_cfg
->num
>= KBC_MAX_COL
) {
601 "pin_cfg[%d]: invalid column number %d\n",
612 "pin_cfg[%d]: invalid entry type %d\n",
613 pin_cfg
->type
, pin_cfg
->num
);
622 static struct tegra_kbc_platform_data
* __devinit
623 tegra_kbc_dt_parse_pdata(struct platform_device
*pdev
)
625 struct tegra_kbc_platform_data
*pdata
;
626 struct device_node
*np
= pdev
->dev
.of_node
;
633 pdata
= kzalloc(sizeof(*pdata
), GFP_KERNEL
);
637 if (!of_property_read_u32(np
, "nvidia,debounce-delay-ms", &prop
))
638 pdata
->debounce_cnt
= prop
;
640 if (!of_property_read_u32(np
, "nvidia,repeat-delay-ms", &prop
))
641 pdata
->repeat_cnt
= prop
;
643 if (of_find_property(np
, "nvidia,needs-ghost-filter", NULL
))
644 pdata
->use_ghost_filter
= true;
646 if (of_find_property(np
, "nvidia,wakeup-source", NULL
))
647 pdata
->wakeup
= true;
650 * All currently known keymaps with device tree support use the same
651 * pin_cfg, so set it up here.
653 for (i
= 0; i
< KBC_MAX_ROW
; i
++) {
654 pdata
->pin_cfg
[i
].num
= i
;
655 pdata
->pin_cfg
[i
].type
= PIN_CFG_ROW
;
658 for (i
= 0; i
< KBC_MAX_COL
; i
++) {
659 pdata
->pin_cfg
[KBC_MAX_ROW
+ i
].num
= i
;
660 pdata
->pin_cfg
[KBC_MAX_ROW
+ i
].type
= PIN_CFG_COL
;
663 pdata
->keymap_data
= matrix_keyboard_of_fill_keymap(np
, "linux,keymap");
665 /* FIXME: Add handling of linux,fn-keymap here */
670 static inline struct tegra_kbc_platform_data
*tegra_kbc_dt_parse_pdata(
671 struct platform_device
*pdev
)
677 static int __devinit
tegra_kbc_probe(struct platform_device
*pdev
)
679 const struct tegra_kbc_platform_data
*pdata
= pdev
->dev
.platform_data
;
680 const struct matrix_keymap_data
*keymap_data
;
681 struct tegra_kbc
*kbc
;
682 struct input_dev
*input_dev
;
683 struct resource
*res
;
687 unsigned int debounce_cnt
;
688 unsigned int scan_time_rows
;
691 pdata
= tegra_kbc_dt_parse_pdata(pdev
);
696 if (!tegra_kbc_check_pin_cfg(pdata
, &pdev
->dev
, &num_rows
)) {
701 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
703 dev_err(&pdev
->dev
, "failed to get I/O memory\n");
708 irq
= platform_get_irq(pdev
, 0);
710 dev_err(&pdev
->dev
, "failed to get keyboard IRQ\n");
715 kbc
= kzalloc(sizeof(*kbc
), GFP_KERNEL
);
716 input_dev
= input_allocate_device();
717 if (!kbc
|| !input_dev
) {
723 kbc
->idev
= input_dev
;
725 spin_lock_init(&kbc
->lock
);
726 setup_timer(&kbc
->timer
, tegra_kbc_keypress_timer
, (unsigned long)kbc
);
728 res
= request_mem_region(res
->start
, resource_size(res
), pdev
->name
);
730 dev_err(&pdev
->dev
, "failed to request I/O memory\n");
735 kbc
->mmio
= ioremap(res
->start
, resource_size(res
));
737 dev_err(&pdev
->dev
, "failed to remap I/O memory\n");
739 goto err_free_mem_region
;
742 kbc
->clk
= clk_get(&pdev
->dev
, NULL
);
743 if (IS_ERR(kbc
->clk
)) {
744 dev_err(&pdev
->dev
, "failed to get keyboard clock\n");
745 err
= PTR_ERR(kbc
->clk
);
750 * The time delay between two consecutive reads of the FIFO is
751 * the sum of the repeat time and the time taken for scanning
752 * the rows. There is an additional delay before the row scanning
753 * starts. The repoll delay is computed in milliseconds.
755 debounce_cnt
= min(pdata
->debounce_cnt
, KBC_MAX_DEBOUNCE_CNT
);
756 scan_time_rows
= (KBC_ROW_SCAN_TIME
+ debounce_cnt
) * num_rows
;
757 kbc
->repoll_dly
= KBC_ROW_SCAN_DLY
+ scan_time_rows
+ pdata
->repeat_cnt
;
758 kbc
->repoll_dly
= DIV_ROUND_UP(kbc
->repoll_dly
, KBC_CYCLE_MS
);
760 input_dev
->name
= pdev
->name
;
761 input_dev
->id
.bustype
= BUS_HOST
;
762 input_dev
->dev
.parent
= &pdev
->dev
;
763 input_dev
->open
= tegra_kbc_open
;
764 input_dev
->close
= tegra_kbc_close
;
766 input_set_drvdata(input_dev
, kbc
);
768 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_REP
);
769 input_set_capability(input_dev
, EV_MSC
, MSC_SCAN
);
771 input_dev
->keycode
= kbc
->keycode
;
772 input_dev
->keycodesize
= sizeof(kbc
->keycode
[0]);
773 input_dev
->keycodemax
= KBC_MAX_KEY
;
774 if (pdata
->use_fn_map
)
775 input_dev
->keycodemax
*= 2;
777 kbc
->use_fn_map
= pdata
->use_fn_map
;
778 kbc
->use_ghost_filter
= pdata
->use_ghost_filter
;
779 keymap_data
= pdata
->keymap_data
?: &tegra_kbc_default_keymap_data
;
780 matrix_keypad_build_keymap(keymap_data
, KBC_ROW_SHIFT
,
781 input_dev
->keycode
, input_dev
->keybit
);
782 kbc
->wakeup_key
= pdata
->wakeup_key
;
784 err
= request_irq(kbc
->irq
, tegra_kbc_isr
,
785 IRQF_NO_SUSPEND
| IRQF_TRIGGER_HIGH
, pdev
->name
, kbc
);
787 dev_err(&pdev
->dev
, "failed to request keyboard IRQ\n");
791 disable_irq(kbc
->irq
);
793 err
= input_register_device(kbc
->idev
);
795 dev_err(&pdev
->dev
, "failed to register input device\n");
799 platform_set_drvdata(pdev
, kbc
);
800 device_init_wakeup(&pdev
->dev
, pdata
->wakeup
);
802 if (!pdev
->dev
.platform_data
)
803 matrix_keyboard_of_free_keymap(pdata
->keymap_data
);
808 free_irq(kbc
->irq
, pdev
);
814 release_mem_region(res
->start
, resource_size(res
));
816 input_free_device(input_dev
);
819 if (!pdev
->dev
.platform_data
) {
820 matrix_keyboard_of_free_keymap(pdata
->keymap_data
);
827 static int __devexit
tegra_kbc_remove(struct platform_device
*pdev
)
829 struct tegra_kbc
*kbc
= platform_get_drvdata(pdev
);
830 struct resource
*res
;
832 platform_set_drvdata(pdev
, NULL
);
834 free_irq(kbc
->irq
, pdev
);
837 input_unregister_device(kbc
->idev
);
839 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
840 release_mem_region(res
->start
, resource_size(res
));
843 * If we do not have platform data attached to the device we
844 * allocated it ourselves and thus need to free it.
846 if (!pdev
->dev
.platform_data
)
854 #ifdef CONFIG_PM_SLEEP
855 static int tegra_kbc_suspend(struct device
*dev
)
857 struct platform_device
*pdev
= to_platform_device(dev
);
858 struct tegra_kbc
*kbc
= platform_get_drvdata(pdev
);
860 mutex_lock(&kbc
->idev
->mutex
);
861 if (device_may_wakeup(&pdev
->dev
)) {
862 disable_irq(kbc
->irq
);
863 del_timer_sync(&kbc
->timer
);
864 tegra_kbc_set_fifo_interrupt(kbc
, false);
866 /* Forcefully clear the interrupt status */
867 writel(0x7, kbc
->mmio
+ KBC_INT_0
);
869 * Store the previous resident time of continuous polling mode.
870 * Force the keyboard into interrupt mode.
872 kbc
->cp_to_wkup_dly
= readl(kbc
->mmio
+ KBC_TO_CNT_0
);
873 writel(0, kbc
->mmio
+ KBC_TO_CNT_0
);
875 tegra_kbc_setup_wakekeys(kbc
, true);
878 kbc
->keypress_caused_wake
= false;
879 /* Enable keypress interrupt before going into suspend. */
880 tegra_kbc_set_keypress_interrupt(kbc
, true);
881 enable_irq(kbc
->irq
);
882 enable_irq_wake(kbc
->irq
);
884 if (kbc
->idev
->users
)
887 mutex_unlock(&kbc
->idev
->mutex
);
892 static int tegra_kbc_resume(struct device
*dev
)
894 struct platform_device
*pdev
= to_platform_device(dev
);
895 struct tegra_kbc
*kbc
= platform_get_drvdata(pdev
);
898 mutex_lock(&kbc
->idev
->mutex
);
899 if (device_may_wakeup(&pdev
->dev
)) {
900 disable_irq_wake(kbc
->irq
);
901 tegra_kbc_setup_wakekeys(kbc
, false);
902 /* We will use fifo interrupts for key detection. */
903 tegra_kbc_set_keypress_interrupt(kbc
, false);
905 /* Restore the resident time of continuous polling mode. */
906 writel(kbc
->cp_to_wkup_dly
, kbc
->mmio
+ KBC_TO_CNT_0
);
908 tegra_kbc_set_fifo_interrupt(kbc
, true);
910 if (kbc
->keypress_caused_wake
&& kbc
->wakeup_key
) {
912 * We can't report events directly from the ISR
913 * because timekeeping is stopped when processing
914 * wakeup request and we get a nasty warning when
915 * we try to call do_gettimeofday() in evdev
918 input_report_key(kbc
->idev
, kbc
->wakeup_key
, 1);
919 input_sync(kbc
->idev
);
920 input_report_key(kbc
->idev
, kbc
->wakeup_key
, 0);
921 input_sync(kbc
->idev
);
924 if (kbc
->idev
->users
)
925 err
= tegra_kbc_start(kbc
);
927 mutex_unlock(&kbc
->idev
->mutex
);
933 static SIMPLE_DEV_PM_OPS(tegra_kbc_pm_ops
, tegra_kbc_suspend
, tegra_kbc_resume
);
935 static const struct of_device_id tegra_kbc_of_match
[] = {
936 { .compatible
= "nvidia,tegra20-kbc", },
939 MODULE_DEVICE_TABLE(of
, tegra_kbc_of_match
);
941 static struct platform_driver tegra_kbc_driver
= {
942 .probe
= tegra_kbc_probe
,
943 .remove
= __devexit_p(tegra_kbc_remove
),
946 .owner
= THIS_MODULE
,
947 .pm
= &tegra_kbc_pm_ops
,
948 .of_match_table
= tegra_kbc_of_match
,
951 module_platform_driver(tegra_kbc_driver
);
953 MODULE_LICENSE("GPL");
954 MODULE_AUTHOR("Rakesh Iyer <riyer@nvidia.com>");
955 MODULE_DESCRIPTION("Tegra matrix keyboard controller driver");
956 MODULE_ALIAS("platform:tegra-kbc");