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 * Added support for keypad and joypad in HP iPAQ h63xx series of devices.
13 * Copyright (C) 2005 Mika Laitio
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/interrupt.h>
33 #include <linux/types.h>
34 #include <linux/input.h>
35 #include <linux/kernel.h>
36 #include <linux/delay.h>
37 #include <linux/platform_device.h>
38 #include <linux/mutex.h>
39 #include <asm/arch/irqs.h>
40 #include <asm/arch/gpio.h>
41 #include <asm/arch/hardware.h>
42 #include <asm/arch/keypad.h>
43 #include <asm/arch/menelaus.h>
45 #include <asm/errno.h>
46 #include <asm/mach-types.h>
47 #include <asm/arch/mux.h>
49 #undef NEW_BOARD_LEARNING_MODE
50 //#define NEW_BOARD_LEARNING_MODE 1
53 * Following 5 keypad events are not really sent to userspace.
54 * Instead if the good combination of them is sent, then that is send.
55 * (up, right, down, left, enter)
57 #define _H6300_JOYPAD_UP_RIGHT 1 // 00001
58 #define _H6300_JOYPAD_DOWN_RIGHT 2 // 00010
59 #define _h6300_JOYPAD_DOWN_LEFT 4 // 00100
60 #define _h6300_JOYPAD_UP_LEFT 8 // 01000
61 #define _H6300_JOYPAD_KEY_OK 16 // 10000
62 #define _H6300_JOYPAD_REPORT_COLUMN 4
64 static void omap_kp_tasklet(unsigned long);
65 static void omap_kp_timer(unsigned long);
67 static unsigned char keypad_state[8];
68 static DEFINE_MUTEX(kp_enable_mutex);
69 static int kp_enable = 1;
70 static int kp_cur_group = -1;
72 static int prevJoypadKeycodePressEmulated;
75 struct input_dev *input;
76 struct timer_list timer;
82 DECLARE_TASKLET_DISABLED(kp_tasklet, omap_kp_tasklet, 0);
85 static unsigned int *row_gpios;
86 static unsigned int *col_gpios;
88 #ifdef CONFIG_ARCH_OMAP2
89 static void set_col_gpio_val(struct omap_kp *omap_kp, u8 value)
92 for (col = 0; col < omap_kp->cols; col++) {
93 if (value & (1 << col))
94 omap_set_gpio_dataout(col_gpios[col], 1);
96 omap_set_gpio_dataout(col_gpios[col], 0);
100 static u8 get_row_gpio_val(struct omap_kp *omap_kp)
105 for (row = 0; row < omap_kp->rows; row++) {
106 if (omap_get_gpio_datain(row_gpios[row]))
112 #define set_col_gpio_val(x, y) do {} while (0)
113 #define get_row_gpio_val(x) 0
116 static irqreturn_t omap_kp_interrupt(int irq, void *dev_id,
117 struct pt_regs *regs)
119 struct omap_kp *omap_kp = dev_id;
121 /* disable keyboard interrupt and schedule for handling */
122 if (cpu_is_omap24xx()) {
124 for (i = 0; i < omap_kp->rows; i++)
125 disable_irq(OMAP_GPIO_IRQ(row_gpios[i]));
127 /* disable keyboard interrupt and schedule for handling */
128 omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
131 tasklet_schedule(&kp_tasklet);
136 static void omap_kp_timer(unsigned long data)
138 tasklet_schedule(&kp_tasklet);
141 static void omap_kp_scan_keypad(struct omap_kp *omap_kp, unsigned char *state)
145 /* read the keypad status */
146 if (cpu_is_omap24xx()) {
148 for (i = 0; i < omap_kp->rows; i++)
149 disable_irq(OMAP_GPIO_IRQ(row_gpios[i]));
151 /* disable keyboard interrupt and schedule for handling */
152 omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
154 if (!cpu_is_omap24xx()) {
155 /* read the keypad status */
156 omap_writew(0xff, OMAP_MPUIO_BASE + OMAP_MPUIO_KBC);
157 for (col = 0; col < omap_kp->cols; col++) {
158 omap_writew(~(1 << col) & 0xff, OMAP_MPUIO_BASE + OMAP_MPUIO_KBC);
160 if (machine_is_omap_osk() || machine_is_omap_h2() || machine_is_omap_h3() || machine_is_omap_h6300()) {
165 state[col] = ~omap_readw(OMAP_MPUIO_BASE + OMAP_MPUIO_KBR_LATCH) & 0xff;
167 omap_writew(0x00, OMAP_MPUIO_BASE + OMAP_MPUIO_KBC);
170 /* read the keypad status */
171 for (col = 0; col < omap_kp->cols; col++) {
172 set_col_gpio_val(omap_kp, ~(1 << col));
173 state[col] = ~(get_row_gpio_val(omap_kp)) & 0x3f;
175 set_col_gpio_val(omap_kp, 0);
179 static inline int omap_kp_find_key(int col, int row)
183 key = KEY(col, row, 0);
184 for (i = 0; keymap[i] != 0; i++)
185 if ((keymap[i] & 0xff000000) == key)
186 return keymap[i] & 0x00ffffff;
190 int is_key_down(unsigned char new_state[],
194 return (new_state[col] & (1 << row)) ? 1 : 0;
197 static void omap_kp_tasklet(unsigned long data)
199 struct omap_kp *omap_kp_data = (struct omap_kp *) data;
200 unsigned char new_state[8], changed, key_down = 0;
203 int report_key, report_col, report_row, joypad_checked; // h6300-joypad specific variables
205 /* check for any changes */
206 omap_kp_scan_keypad(omap_kp_data, new_state);
208 /* check for changes and print those */
210 for (col = 0; col < omap_kp_data->cols; col++) {
211 changed = new_state[col] ^ keypad_state[col];
212 key_down |= new_state[col];
216 for (row = 0; row < omap_kp_data->rows; row++) {
218 if (!(changed & (1 << row)))
220 #ifdef NEW_BOARD_LEARNING_MODE
221 printk(KERN_INFO "omap-keypad: key %d-%d %s\n", col, row, (new_state[col] & (1 << row)) ? "pressed" : "released");
223 key = omap_kp_find_key(col, row);
225 printk(KERN_WARNING "omap-keypad: Spurious key event %d-%d\n",
227 /* We scan again after a couple of seconds */
231 if (machine_is_omap_h6300() &&
232 ((col == 1) || (col == _H6300_JOYPAD_REPORT_COLUMN)))
234 if (col == _H6300_JOYPAD_REPORT_COLUMN)
238 if ((joypad_checked == 0) &&
239 ((key == _H6300_JOYPAD_KEY_OK) ||
240 (key == _h6300_JOYPAD_UP_LEFT) ||
241 (key == _H6300_JOYPAD_UP_RIGHT) ||
242 (key == _H6300_JOYPAD_DOWN_RIGHT) ||
243 (key == _h6300_JOYPAD_DOWN_LEFT)))
245 if (is_key_down(new_state, col, row))
249 * 1 0 0 _H6300_JOYPAD_KEY_OK 0 0
252 if (new_state[1] == 36)
255 prevJoypadKeycodePressEmulated = KEY_ENTER;
256 new_state[_H6300_JOYPAD_REPORT_COLUMN] = 48; //110000
257 report_key = prevJoypadKeycodePressEmulated;
258 report_col = _H6300_JOYPAD_REPORT_COLUMN;
260 input_report_key(omap_kp_data->input,
262 new_state[report_col] & (1 << report_row));
265 * enter, up_left and up_right sensors pressed.
266 * 1 _H6300_JOYPAD_UP_RIGHT 0 _H6300_JOYPAD_KEY_OK 0 _h6300_JOYPAD_UP_LEFT
269 * 1 KEY_UP_RIGHT 0 0 0 _h6300_JOYPAD_UP_LEFT
273 else if ((new_state[1] == 53) ||
274 (new_state[1] == 49))
277 prevJoypadKeycodePressEmulated = KEY_UP;
278 new_state[_H6300_JOYPAD_REPORT_COLUMN] = 40; //101000
279 report_key = prevJoypadKeycodePressEmulated;
280 report_col = _H6300_JOYPAD_REPORT_COLUMN;
282 input_report_key(omap_kp_data->input,
284 new_state[report_col] & (1 << report_row));
287 * enter, down_left and down_right sensors pressed
290 * down_left and down_right
294 else if ((new_state[1] == 46) ||
295 (new_state[1] == 42))
298 prevJoypadKeycodePressEmulated = KEY_DOWN;
299 new_state[_H6300_JOYPAD_REPORT_COLUMN] = 34; //100010
300 report_key = prevJoypadKeycodePressEmulated;
301 report_col = _H6300_JOYPAD_REPORT_COLUMN;
303 input_report_key(omap_kp_data->input,
305 new_state[report_col] & (1 << report_row));
308 * enter, up_right and down_right sensors pressed
311 * down_right and up_right
315 else if ((new_state[1] == 60) ||
316 (new_state[1] == 56))
319 prevJoypadKeycodePressEmulated = KEY_RIGHT;
320 new_state[_H6300_JOYPAD_REPORT_COLUMN] = 33; //100001
321 report_key = prevJoypadKeycodePressEmulated;
322 report_col = _H6300_JOYPAD_REPORT_COLUMN;
324 input_report_key(omap_kp_data->input,
326 new_state[report_col] & (1 << report_row));
329 * enter, up_left and down_left sensors pressed
331 * or up_left and down_left
335 else if ((new_state[1] == 39) ||
336 (new_state[1] == 35))
339 prevJoypadKeycodePressEmulated = KEY_LEFT;
340 new_state[_H6300_JOYPAD_REPORT_COLUMN] = 36; //100100
341 report_key = prevJoypadKeycodePressEmulated;
342 report_col = _H6300_JOYPAD_REPORT_COLUMN;
344 input_report_key(omap_kp_data->input,
346 new_state[report_col] & (1 << report_row));
350 //printk("missed new_state = %d\n", new_state[1]);
355 if (prevJoypadKeycodePressEmulated != 0)
357 // report key up event
359 new_state[_H6300_JOYPAD_REPORT_COLUMN] = 32; //100000
360 report_key = prevJoypadKeycodePressEmulated;
361 report_col = _H6300_JOYPAD_REPORT_COLUMN;
362 switch(prevJoypadKeycodePressEmulated)
380 printk(KERN_WARNING "Unknown iPAQ h6300 column 1 key = %d released. This should newer happen!\n",
384 input_report_key(omap_kp_data->input,
386 new_state[report_col] & (1 << report_row));
387 prevJoypadKeycodePressEmulated = 0;
394 if (!(kp_cur_group == (key & GROUP_MASK) ||
398 kp_cur_group = key & GROUP_MASK;
399 input_report_key(omap_kp_data->input, key & ~GROUP_MASK,
400 !!(new_state[col] & (1 << row)));
405 memcpy(keypad_state, new_state, sizeof(keypad_state));
409 /* some key is pressed - keep irq disabled and use timer
410 * to poll the keypad */
413 mod_timer(&omap_kp_data->timer, jiffies + delay);
415 /* enable interrupts */
416 if (cpu_is_omap24xx()) {
418 for (i = 0; i < omap_kp_data->rows; i++)
419 enable_irq(OMAP_GPIO_IRQ(row_gpios[i]));
421 omap_writew(0, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
427 static ssize_t omap_kp_enable_show(struct device *dev,
428 struct device_attribute *attr, char *buf)
430 return sprintf(buf, "%u\n", kp_enable);
433 static ssize_t omap_kp_enable_store(struct device *dev, struct device_attribute *attr,
434 const char *buf, size_t count)
438 if (sscanf(buf, "%u", &state) != 1)
441 if ((state != 1) && (state != 0))
444 mutex_lock(&kp_enable_mutex);
445 if (state != kp_enable) {
447 enable_irq(INT_KEYBOARD);
449 disable_irq(INT_KEYBOARD);
452 mutex_unlock(&kp_enable_mutex);
454 return strnlen(buf, count);
457 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, omap_kp_enable_show, omap_kp_enable_store);
460 static int omap_kp_suspend(struct platform_device *dev, pm_message_t state)
467 static int omap_kp_resume(struct platform_device *dev)
474 #define omap_kp_suspend NULL
475 #define omap_kp_resume NULL
478 static int __init omap_kp_probe(struct platform_device *pdev)
480 struct omap_kp *omap_kp;
481 struct input_dev *input_dev;
482 struct omap_kp_platform_data *pdata = pdev->dev.platform_data;
485 if (!pdata->rows || !pdata->cols || !pdata->keymap) {
486 printk(KERN_ERR "No rows, cols or keymap from pdata\n");
490 omap_kp = kzalloc(sizeof(struct omap_kp), GFP_KERNEL);
491 input_dev = input_allocate_device();
492 if (!omap_kp || !input_dev) {
494 input_free_device(input_dev);
498 platform_set_drvdata(pdev, omap_kp);
500 omap_kp->input = input_dev;
502 /* Disable the interrupt for the MPUIO keyboard */
503 if (!cpu_is_omap24xx())
504 omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
506 keymap = pdata->keymap;
509 set_bit(EV_REP, input_dev->evbit);
511 if (pdata->row_gpios && pdata->col_gpios) {
512 row_gpios = pdata->row_gpios;
513 col_gpios = pdata->col_gpios;
516 omap_kp->rows = pdata->rows;
517 omap_kp->cols = pdata->cols;
519 if (cpu_is_omap24xx()) {
521 for (i = 0; i < omap_kp->cols; i++) {
522 if (omap_request_gpio(col_gpios[i]) < 0) {
523 printk(KERN_ERR "Failed to request"
524 "GPIO%d for keypad\n",
528 omap_set_gpio_direction(col_gpios[i], 0);
531 for (i = 0; i < omap_kp->rows; i++) {
532 if (omap_request_gpio(row_gpios[i]) < 0) {
533 printk(KERN_ERR "Failed to request"
534 "GPIO%d for keypad\n",
538 omap_set_gpio_direction(row_gpios[i], 1);
541 prevJoypadKeycodePressEmulated = 0;
543 init_timer(&omap_kp->timer);
544 omap_kp->timer.function = omap_kp_timer;
545 omap_kp->timer.data = (unsigned long)omap_kp;
547 /* get the irq and init timer*/
548 tasklet_enable(&kp_tasklet);
549 kp_tasklet.data = (unsigned long) omap_kp;
551 omap_kp->irq = platform_get_irq(pdev, 0);
552 if (omap_kp->irq >= 0) {
553 if (request_irq(omap_kp->irq, omap_kp_interrupt, 0,
554 "omap-keypad", omap_kp) < 0)
558 device_create_file(&pdev->dev, &dev_attr_enable);
560 /* setup input device */
561 set_bit(EV_KEY, input_dev->evbit);
562 for (i = 0; keymap[i] != 0; i++)
563 set_bit(keymap[i] & KEY_MAX, input_dev->keybit);
564 input_dev->name = "omap-keypad";
565 input_dev->cdev.dev = &pdev->dev;
566 input_dev->private = omap_kp;
567 input_register_device(omap_kp->input);
569 if (machine_is_omap_h2() || machine_is_omap_h3() ||
570 machine_is_omap_perseus2() || machine_is_omap_h6300()) {
571 omap_writew(0xff, OMAP_MPUIO_BASE + OMAP_MPUIO_GPIO_DEBOUNCING);
573 /* scan current status and enable interrupt */
574 omap_kp_scan_keypad(omap_kp, keypad_state);
575 if (!cpu_is_omap24xx()) {
576 omap_writew(0, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
578 for (i = 0; i < omap_kp->rows; i++) {
579 if (request_irq(OMAP_GPIO_IRQ(row_gpios[i]), omap_kp_interrupt,
580 SA_TRIGGER_FALLING, "omap-keypad", omap_kp) < 0)
588 static int omap_kp_remove(struct platform_device *pdev)
590 struct omap_kp *omap_kp = platform_get_drvdata(pdev);
592 /* disable keypad interrupt handling */
593 tasklet_disable(&kp_tasklet);
594 if (cpu_is_omap24xx()) {
596 for (i = 0; i < omap_kp->cols; i++)
597 omap_free_gpio(col_gpios[i]);
598 for (i = 0; i < omap_kp->rows; i++) {
599 omap_free_gpio(row_gpios[i]);
600 free_irq(OMAP_GPIO_IRQ(row_gpios[i]), 0);
603 omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
604 free_irq(omap_kp->irq, 0);
607 del_timer_sync(&omap_kp->timer);
609 /* unregister everything */
610 input_unregister_device(omap_kp->input);
617 static struct platform_driver omap_kp_driver = {
618 .probe = omap_kp_probe,
619 .remove = omap_kp_remove,
620 .suspend = omap_kp_suspend,
621 .resume = omap_kp_resume,
623 .name = "omap-keypad",
627 static int __devinit omap_kp_init(void)
629 printk(KERN_INFO "OMAP Keypad Driver\n");
630 return platform_driver_register(&omap_kp_driver);
633 static void __exit omap_kp_exit(void)
635 platform_driver_unregister(&omap_kp_driver);
638 module_init(omap_kp_init);
639 module_exit(omap_kp_exit);
641 MODULE_AUTHOR("Timo Teräs");
642 MODULE_DESCRIPTION("OMAP Keypad Driver");
643 MODULE_LICENSE("GPL");