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];
50 static struct timer_list kp_timer;
51 DECLARE_TASKLET_DISABLED(kp_tasklet, omap_kp_tasklet, 0);
53 #define KEY(col, row, val) (((col) << 28) | ((row) << 24) | (val))
55 static int h2_keymap[] = {
94 static int test_keymap[] = {
102 KEY(1, 2, KEY_RIGHT),
107 static int innovator_keymap[] = {
111 KEY(1, 2, KEY_RIGHT),
115 KEY(3, 2, KEY_ENTER),
120 static int osk_keymap[] = {
123 KEY(1, 1, KEY_LEFTCTRL),
125 KEY(2, 0, KEY_SPACE),
128 KEY(3, 2, KEY_ENTER),
129 KEY(3, 3, KEY_RIGHT),
135 static irqreturn_t omap_kp_interrupt(int irq, void *dev_id,
136 struct pt_regs *regs)
138 /* disable keyboard interrupt and schedule for handling */
139 omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
140 tasklet_schedule(&kp_tasklet);
145 static void omap_kp_timer(unsigned long data)
147 tasklet_schedule(&kp_tasklet);
150 static void omap_kp_scan_keypad(unsigned char *state)
154 /* read the keypad status */
155 omap_writew(0xff, OMAP_MPUIO_BASE + OMAP_MPUIO_KBC);
156 for (col = 0; col < 8; col++) {
157 omap_writew(~(1 << col) & 0xff, OMAP_MPUIO_BASE + OMAP_MPUIO_KBC);
159 if (machine_is_omap_osk() || machine_is_omap_h2() || machine_is_omap_h3()) {
165 state[col] = ~omap_readw(OMAP_MPUIO_BASE + OMAP_MPUIO_KBR_LATCH) & 0xff;
167 omap_writew(0x00, OMAP_MPUIO_BASE + OMAP_MPUIO_KBC);
171 static inline int omap_kp_find_key(int col, int row)
175 key = KEY(col, row, 0);
176 for (i = 0; keymap[i] != 0; i++)
177 if ((keymap[i] & 0xff000000) == key)
178 return keymap[i] & 0x00ffffff;
182 static void omap_kp_tasklet(unsigned long data)
184 unsigned char new_state[8], changed, key_down = 0;
188 /* check for any changes */
189 omap_kp_scan_keypad(new_state);
191 /* check for changes and print those */
192 for (col = 0; col < 8; col++) {
193 changed = new_state[col] ^ keypad_state[col];
194 key_down |= new_state[col];
198 for (row = 0; row < 8; row++) {
200 if (!(changed & (1 << row)))
202 #ifdef NEW_BOARD_LEARNING_MODE
203 printk(KERN_INFO "omap-keypad: key %d-%d %s\n", col, row, (new_state[col] & (1 << row)) ? "pressed" : "released");
205 key = omap_kp_find_key(col, row);
207 printk(KERN_WARNING "omap-keypad: Spurious key event %d-%d\n",
209 /* We scan again after a couple of seconds */
214 input_report_key(&omap_kp_dev, key,
215 new_state[col] & (1 << row));
219 memcpy(keypad_state, new_state, sizeof(keypad_state));
223 /* some key is pressed - keep irq disabled and use timer
224 * to poll the keypad */
227 mod_timer(&kp_timer, jiffies + delay);
229 /* enable interrupts */
230 omap_writew(0, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
234 static int __init omap_kp_init(void)
238 printk(KERN_INFO "OMAP Keypad Driver\n");
240 /* Disable the interrupt for the MPUIO keyboard */
241 omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
243 if (machine_is_omap_h2() || machine_is_omap_h3()) {
245 set_bit(EV_REP, omap_kp_dev.evbit);
246 } else if (machine_is_omap_innovator()) {
247 keymap = innovator_keymap;
248 } else if (machine_is_omap_osk()) {
251 keymap = test_keymap;
254 init_timer(&kp_timer);
255 kp_timer.function = omap_kp_timer;
257 /* get the irq and init timer*/
258 tasklet_enable(&kp_tasklet);
259 if (request_irq(INT_KEYBOARD, omap_kp_interrupt, 0,
260 "omap-keypad", 0) < 0)
263 /* setup input device */
264 set_bit(EV_KEY, omap_kp_dev.evbit);
265 for (i = 0; keymap[i] != 0; i++)
266 set_bit(keymap[i] & 0x00ffffff, omap_kp_dev.keybit);
267 omap_kp_dev.name = "omap-keypad";
268 input_register_device(&omap_kp_dev);
270 if (machine_is_omap_h2() || machine_is_omap_h3()) {
271 omap_cfg_reg(F18_1610_KBC0);
272 omap_cfg_reg(D20_1610_KBC1);
273 omap_cfg_reg(D19_1610_KBC2);
274 omap_cfg_reg(E18_1610_KBC3);
275 omap_cfg_reg(C21_1610_KBC4);
277 omap_cfg_reg(G18_1610_KBR0);
278 omap_cfg_reg(F19_1610_KBR1);
279 omap_cfg_reg(H14_1610_KBR2);
280 omap_cfg_reg(E20_1610_KBR3);
281 omap_cfg_reg(E19_1610_KBR4);
282 omap_cfg_reg(N19_1610_KBR5);
284 omap_writew(0xff, OMAP_MPUIO_BASE + OMAP_MPUIO_GPIO_DEBOUNCING);
287 /* scan current status and enable interrupt */
288 omap_kp_scan_keypad(keypad_state);
289 omap_writew(0, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
294 static void __exit omap_kp_exit(void)
296 /* disable keypad interrupt handling */
297 tasklet_disable(&kp_tasklet);
298 omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
300 free_irq(INT_KEYBOARD, 0);
301 del_timer_sync(&kp_timer);
303 /* unregister everything */
304 input_unregister_device(&omap_kp_dev);
307 module_init(omap_kp_init);
308 module_exit(omap_kp_exit);
310 MODULE_AUTHOR("Timo Teräs");
311 MODULE_DESCRIPTION("OMAP Keypad Driver");
312 MODULE_LICENSE("GPL");