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 <linux/platform_device.h>
35 #include <asm/arch/irqs.h>
36 #include <asm/arch/gpio.h>
37 #include <asm/arch/hardware.h>
39 #include <asm/errno.h>
40 #include <asm/mach-types.h>
41 #include <asm/arch/mux.h>
43 #undef NEW_BOARD_LEARNING_MODE
45 static void omap_kp_tasklet(unsigned long);
46 static void omap_kp_timer(unsigned long);
48 static unsigned char keypad_state[8];
49 static unsigned int keypad_irq = INT_KEYBOARD;
52 struct input_dev *input;
53 struct timer_list timer;
56 DECLARE_TASKLET_DISABLED(kp_tasklet, omap_kp_tasklet, 0);
58 #define KEY(col, row, val) (((col) << 28) | ((row) << 24) | (val))
60 static int h2_keymap[] = {
99 static int test_keymap[] = {
104 KEY(1, 1, KEY_ENTER),
107 KEY(1, 2, KEY_RIGHT),
112 static int innovator_keymap[] = {
116 KEY(1, 2, KEY_RIGHT),
120 KEY(3, 2, KEY_ENTER),
125 static int osk_keymap[] = {
128 KEY(1, 1, KEY_LEFTCTRL),
130 KEY(2, 0, KEY_SPACE),
133 KEY(3, 2, KEY_ENTER),
134 KEY(3, 3, KEY_RIGHT),
138 static int p2_keymap[] = {
148 KEY(1,3,KEY_VOLUMEDOWN),
149 KEY(1,4,KEY_VOLUMEUP),
162 KEY(3,5,KEY_HEADSETHOOK),
174 static irqreturn_t omap_kp_interrupt(int irq, void *dev_id,
175 struct pt_regs *regs)
177 /* disable keyboard interrupt and schedule for handling */
178 omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
179 tasklet_schedule(&kp_tasklet);
184 static void omap_kp_timer(unsigned long data)
186 tasklet_schedule(&kp_tasklet);
189 static void omap_kp_scan_keypad(unsigned char *state)
193 /* read the keypad status */
194 omap_writew(0xff, OMAP_MPUIO_BASE + OMAP_MPUIO_KBC);
195 for (col = 0; col < 8; col++) {
196 omap_writew(~(1 << col) & 0xff, OMAP_MPUIO_BASE + OMAP_MPUIO_KBC);
198 if (machine_is_omap_osk() || machine_is_omap_h2() || machine_is_omap_h3()) {
204 state[col] = ~omap_readw(OMAP_MPUIO_BASE + OMAP_MPUIO_KBR_LATCH) & 0xff;
206 omap_writew(0x00, OMAP_MPUIO_BASE + OMAP_MPUIO_KBC);
210 static inline int omap_kp_find_key(int col, int row)
214 key = KEY(col, row, 0);
215 for (i = 0; keymap[i] != 0; i++)
216 if ((keymap[i] & 0xff000000) == key)
217 return keymap[i] & 0x00ffffff;
221 static void omap_kp_tasklet(unsigned long data)
223 struct omap_kp *omap_kp_data = (struct omap_kp *) data;
224 unsigned char new_state[8], changed, key_down = 0;
228 /* check for any changes */
229 omap_kp_scan_keypad(new_state);
231 /* check for changes and print those */
232 for (col = 0; col < 8; col++) {
233 changed = new_state[col] ^ keypad_state[col];
234 key_down |= new_state[col];
238 for (row = 0; row < 8; row++) {
240 if (!(changed & (1 << row)))
242 #ifdef NEW_BOARD_LEARNING_MODE
243 printk(KERN_INFO "omap-keypad: key %d-%d %s\n", col, row, (new_state[col] & (1 << row)) ? "pressed" : "released");
245 key = omap_kp_find_key(col, row);
247 printk(KERN_WARNING "omap-keypad: Spurious key event %d-%d\n",
249 /* We scan again after a couple of seconds */
254 input_report_key(omap_kp_data->input, key,
255 new_state[col] & (1 << row));
259 memcpy(keypad_state, new_state, sizeof(keypad_state));
263 /* some key is pressed - keep irq disabled and use timer
264 * to poll the keypad */
267 mod_timer(&omap_kp_data->timer, jiffies + delay);
269 /* enable interrupts */
270 omap_writew(0, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
275 static int omap_kp_suspend(struct platform_device *dev, pm_message_t state)
282 static int omap_kp_resume(struct platform_device *dev)
289 #define omap_kp_suspend NULL
290 #define omap_kp_resume NULL
293 static int __init omap_kp_probe(struct platform_device *pdev)
295 struct omap_kp *omap_kp;
296 struct input_dev *input_dev;
299 omap_kp = kzalloc(sizeof(struct omap_kp), GFP_KERNEL);
300 input_dev = input_allocate_device();
301 if (!omap_kp || !input_dev) {
303 input_free_device(input_dev);
307 platform_set_drvdata(pdev, omap_kp);
309 omap_kp->input = input_dev;
311 /* Disable the interrupt for the MPUIO keyboard */
312 omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
314 if (machine_is_omap_h2() || machine_is_omap_h3()) {
316 set_bit(EV_REP, input_dev->evbit);
317 } else if (machine_is_omap_innovator()) {
318 keymap = innovator_keymap;
319 } else if (machine_is_omap_osk()) {
321 } else if (machine_is_omap_perseus2()) {
323 keypad_irq = INT_730_MPUIO_KEYPAD;
325 keymap = test_keymap;
328 init_timer(&omap_kp->timer);
329 omap_kp->timer.function = omap_kp_timer;
330 omap_kp->timer.data = (unsigned long) omap_kp;
332 /* get the irq and init timer*/
333 tasklet_enable(&kp_tasklet);
334 if (request_irq(keypad_irq, omap_kp_interrupt, 0,
335 "omap-keypad", 0) < 0)
338 /* setup input device */
339 set_bit(EV_KEY, input_dev->evbit);
340 for (i = 0; keymap[i] != 0; i++)
341 set_bit(keymap[i] & 0x00ffffff, input_dev->keybit);
342 input_dev->name = "omap-keypad";
343 input_dev->cdev.dev = &pdev->dev;
344 input_dev->private = omap_kp;
345 input_register_device(omap_kp->input);
347 if (machine_is_omap_h2() || machine_is_omap_h3() ||
348 machine_is_omap_perseus2()) {
349 omap_writew(0xff, OMAP_MPUIO_BASE + OMAP_MPUIO_GPIO_DEBOUNCING);
351 /* scan current status and enable interrupt */
352 omap_kp_scan_keypad(keypad_state);
353 omap_writew(0, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
358 static int omap_kp_remove(struct platform_device *pdev)
360 struct omap_kp *omap_kp = platform_get_drvdata(pdev);
362 /* disable keypad interrupt handling */
363 tasklet_disable(&kp_tasklet);
364 omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
366 free_irq(keypad_irq, 0);
367 del_timer_sync(&omap_kp->timer);
369 /* unregister everything */
370 input_unregister_device(omap_kp->input);
377 static struct platform_driver omap_kp_driver = {
378 .probe = omap_kp_probe,
379 .remove = omap_kp_remove,
380 .suspend = omap_kp_suspend,
381 .resume = omap_kp_resume,
383 .name = "omap-keypad",
387 static int __devinit omap_kp_init(void)
389 printk(KERN_INFO "OMAP Keypad Driver\n");
390 return platform_driver_register(&omap_kp_driver);
393 static void __exit omap_kp_exit(void)
395 platform_driver_unregister(&omap_kp_driver);
398 module_init(omap_kp_init);
399 module_exit(omap_kp_exit);
401 MODULE_AUTHOR("Timo Teräs");
402 MODULE_DESCRIPTION("OMAP Keypad Driver");
403 MODULE_LICENSE("GPL");