2 * linux/drivers/char/omap-keypad.c
6 * Copyright (C) 2003 Nokia Corporation
7 * Written by Timo Teräs <ext-timo.teras@nokia.com>
9 * Added support for H2 & H3 Keypad
10 * Copyright (C) 2004 Texas Instruments
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/interrupt.h>
30 #include <linux/types.h>
31 #include <linux/input.h>
32 #include <linux/kernel.h>
33 #include <linux/delay.h>
34 #include <asm/arch/irqs.h>
35 #include <asm/arch/gpio.h>
36 #include <asm/arch/hardware.h>
38 #include <asm/errno.h>
39 #include <asm/mach-types.h>
40 #include <asm/arch/mux.h>
42 #undef NEW_BOARD_LEARNING_MODE
44 static void omap_kp_tasklet(unsigned long);
45 static void omap_kp_timer(unsigned long);
47 static struct input_dev omap_kp_dev;
48 static unsigned char keypad_state[8];
49 static unsigned int keypad_irq = INT_KEYBOARD;
51 static struct timer_list kp_timer;
52 DECLARE_TASKLET_DISABLED(kp_tasklet, omap_kp_tasklet, 0);
54 #define KEY(col, row, val) (((col) << 28) | ((row) << 24) | (val))
56 static int h2_keymap[] = {
95 static int test_keymap[] = {
100 KEY(1, 1, KEY_ENTER),
103 KEY(1, 2, KEY_RIGHT),
108 static int innovator_keymap[] = {
112 KEY(1, 2, KEY_RIGHT),
116 KEY(3, 2, KEY_ENTER),
121 static int osk_keymap[] = {
124 KEY(1, 1, KEY_LEFTCTRL),
126 KEY(2, 0, KEY_SPACE),
129 KEY(3, 2, KEY_ENTER),
130 KEY(3, 3, KEY_RIGHT),
134 static int p2_keymap[] = {
144 KEY(1,3,KEY_VOLUMEDOWN),
145 KEY(1,4,KEY_VOLUMEUP),
158 KEY(3,5,KEY_HEADSETHOOK),
170 static irqreturn_t omap_kp_interrupt(int irq, void *dev_id,
171 struct pt_regs *regs)
173 /* disable keyboard interrupt and schedule for handling */
174 omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
175 tasklet_schedule(&kp_tasklet);
180 static void omap_kp_timer(unsigned long data)
182 tasklet_schedule(&kp_tasklet);
185 static void omap_kp_scan_keypad(unsigned char *state)
189 /* read the keypad status */
190 omap_writew(0xff, OMAP_MPUIO_BASE + OMAP_MPUIO_KBC);
191 for (col = 0; col < 8; col++) {
192 omap_writew(~(1 << col) & 0xff, OMAP_MPUIO_BASE + OMAP_MPUIO_KBC);
194 if (machine_is_omap_osk() || machine_is_omap_h2() || machine_is_omap_h3()) {
200 state[col] = ~omap_readw(OMAP_MPUIO_BASE + OMAP_MPUIO_KBR_LATCH) & 0xff;
202 omap_writew(0x00, OMAP_MPUIO_BASE + OMAP_MPUIO_KBC);
206 static inline int omap_kp_find_key(int col, int row)
210 key = KEY(col, row, 0);
211 for (i = 0; keymap[i] != 0; i++)
212 if ((keymap[i] & 0xff000000) == key)
213 return keymap[i] & 0x00ffffff;
217 static void omap_kp_tasklet(unsigned long data)
219 unsigned char new_state[8], changed, key_down = 0;
223 /* check for any changes */
224 omap_kp_scan_keypad(new_state);
226 /* check for changes and print those */
227 for (col = 0; col < 8; col++) {
228 changed = new_state[col] ^ keypad_state[col];
229 key_down |= new_state[col];
233 for (row = 0; row < 8; row++) {
235 if (!(changed & (1 << row)))
237 #ifdef NEW_BOARD_LEARNING_MODE
238 printk(KERN_INFO "omap-keypad: key %d-%d %s\n", col, row, (new_state[col] & (1 << row)) ? "pressed" : "released");
240 key = omap_kp_find_key(col, row);
242 printk(KERN_WARNING "omap-keypad: Spurious key event %d-%d\n",
244 /* We scan again after a couple of seconds */
249 input_report_key(&omap_kp_dev, key,
250 new_state[col] & (1 << row));
254 memcpy(keypad_state, new_state, sizeof(keypad_state));
258 /* some key is pressed - keep irq disabled and use timer
259 * to poll the keypad */
262 mod_timer(&kp_timer, jiffies + delay);
264 /* enable interrupts */
265 omap_writew(0, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
269 static int __init omap_kp_init(void)
273 printk(KERN_INFO "OMAP Keypad Driver\n");
275 /* Disable the interrupt for the MPUIO keyboard */
276 omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
278 if (machine_is_omap_h2() || machine_is_omap_h3()) {
280 set_bit(EV_REP, omap_kp_dev.evbit);
281 } else if (machine_is_omap_innovator()) {
282 keymap = innovator_keymap;
283 } else if (machine_is_omap_osk()) {
285 } else if (machine_is_omap_perseus2()) {
287 keypad_irq = INT_730_MPUIO_KEYPAD;
289 keymap = test_keymap;
292 init_timer(&kp_timer);
293 kp_timer.function = omap_kp_timer;
295 /* get the irq and init timer*/
296 tasklet_enable(&kp_tasklet);
297 if (request_irq(keypad_irq, omap_kp_interrupt, 0,
298 "omap-keypad", 0) < 0)
301 /* setup input device */
302 set_bit(EV_KEY, omap_kp_dev.evbit);
303 for (i = 0; keymap[i] != 0; i++)
304 set_bit(keymap[i] & 0x00ffffff, omap_kp_dev.keybit);
305 omap_kp_dev.name = "omap-keypad";
306 input_register_device(&omap_kp_dev);
308 if (machine_is_omap_h2() || machine_is_omap_h3()) {
309 omap_cfg_reg(F18_1610_KBC0);
310 omap_cfg_reg(D20_1610_KBC1);
311 omap_cfg_reg(D19_1610_KBC2);
312 omap_cfg_reg(E18_1610_KBC3);
313 omap_cfg_reg(C21_1610_KBC4);
315 omap_cfg_reg(G18_1610_KBR0);
316 omap_cfg_reg(F19_1610_KBR1);
317 omap_cfg_reg(H14_1610_KBR2);
318 omap_cfg_reg(E20_1610_KBR3);
319 omap_cfg_reg(E19_1610_KBR4);
320 omap_cfg_reg(N19_1610_KBR5);
322 omap_writew(0xff, OMAP_MPUIO_BASE + OMAP_MPUIO_GPIO_DEBOUNCING);
323 } else if (machine_is_omap_perseus2()) {
324 omap_cfg_reg(E2_730_KBR0);
325 omap_cfg_reg(J7_730_KBR1);
326 omap_cfg_reg(E1_730_KBR2);
327 omap_cfg_reg(F3_730_KBR3);
328 omap_cfg_reg(D2_730_KBR4);
330 omap_cfg_reg(C2_730_KBC0);
331 omap_cfg_reg(D3_730_KBC1);
332 omap_cfg_reg(E4_730_KBC2);
333 omap_cfg_reg(F4_730_KBC3);
334 omap_cfg_reg(E3_730_KBC4);
336 omap_writew(0xff, OMAP_MPUIO_BASE + OMAP_MPUIO_GPIO_DEBOUNCING);
339 /* scan current status and enable interrupt */
340 omap_kp_scan_keypad(keypad_state);
341 omap_writew(0, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
346 static void __exit omap_kp_exit(void)
348 /* disable keypad interrupt handling */
349 tasklet_disable(&kp_tasklet);
350 omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
352 free_irq(keypad_irq, 0);
353 del_timer_sync(&kp_timer);
355 /* unregister everything */
356 input_unregister_device(&omap_kp_dev);
359 module_init(omap_kp_init);
360 module_exit(omap_kp_exit);
362 MODULE_AUTHOR("Timo Teräs");
363 MODULE_DESCRIPTION("OMAP Keypad Driver");
364 MODULE_LICENSE("GPL");