]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/input/keyboard/omap-keypad.c
Merge with ../linux-2.6
[linux-2.6-omap-h63xx.git] / drivers / input / keyboard / omap-keypad.c
1 /*
2  * linux/drivers/char/omap-keypad.c
3  *
4  * OMAP Keypad Driver
5  *
6  * Copyright (C) 2003 Nokia Corporation
7  * Written by Timo Teräs <ext-timo.teras@nokia.com>
8  *
9  * Added support for H2 & H3 Keypad
10  * Copyright (C) 2004 Texas Instruments
11  *
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.
16  *
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.
21  *
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
25  */
26
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>
37 #include <asm/io.h>
38 #include <asm/errno.h>
39 #include <asm/mach-types.h>
40 #include <asm/arch/mux.h>
41
42 #undef NEW_BOARD_LEARNING_MODE
43
44 static void omap_kp_tasklet(unsigned long);
45 static void omap_kp_timer(unsigned long);
46
47 static struct input_dev omap_kp_dev;
48 static unsigned char keypad_state[8];
49
50 static struct timer_list kp_timer;
51 DECLARE_TASKLET_DISABLED(kp_tasklet, omap_kp_tasklet, 0);
52
53 #define KEY(col, row, val) (((col) << 28) | ((row) << 24) | (val))
54
55 static int h2_keymap[] = {
56         KEY(0, 0, KEY_LEFT),
57         KEY(0, 1, KEY_RIGHT),
58         KEY(0, 2, KEY_3),
59         KEY(0, 3, KEY_F10),
60         KEY(0, 4, KEY_F5),
61         KEY(0, 5, KEY_9),
62         KEY(1, 0, KEY_DOWN),
63         KEY(1, 1, KEY_UP),
64         KEY(1, 2, KEY_2),
65         KEY(1, 3, KEY_F9),
66         KEY(1, 4, KEY_F7),
67         KEY(1, 5, KEY_0),
68         KEY(2, 0, KEY_ENTER),
69         KEY(2, 1, KEY_6),
70         KEY(2, 2, KEY_1),
71         KEY(2, 3, KEY_F2),
72         KEY(2, 4, KEY_F6),
73         KEY(2, 5, KEY_HOME),
74         KEY(3, 0, KEY_8),
75         KEY(3, 1, KEY_5),
76         KEY(3, 2, KEY_F12),
77         KEY(3, 3, KEY_F3),
78         KEY(3, 4, KEY_F8),
79         KEY(3, 5, KEY_END),
80         KEY(4, 0, KEY_7),
81         KEY(4, 1, KEY_4),
82         KEY(4, 2, KEY_F11),
83         KEY(4, 3, KEY_F1),
84         KEY(4, 4, KEY_F4),
85         KEY(4, 5, KEY_ESC),
86         KEY(5, 0, KEY_F13),
87         KEY(5, 1, KEY_F14),
88         KEY(5, 2, KEY_F15),
89         KEY(5, 3, KEY_F16),
90         KEY(5, 4, KEY_SLEEP),
91         0
92 };
93
94 static int test_keymap[] = {
95         KEY(0, 0, KEY_F4),
96         KEY(1, 0, KEY_LEFT),
97         KEY(2, 0, KEY_F1),
98         KEY(0, 1, KEY_DOWN),
99         KEY(1, 1, KEY_ENTER),
100         KEY(2, 1, KEY_UP),
101         KEY(0, 2, KEY_F3),
102         KEY(1, 2, KEY_RIGHT),
103         KEY(2, 2, KEY_F2),
104         0
105 };
106
107 static int innovator_keymap[] = {
108         KEY(0, 0, KEY_F1),
109         KEY(0, 3, KEY_DOWN),
110         KEY(1, 1, KEY_F2),
111         KEY(1, 2, KEY_RIGHT),
112         KEY(2, 0, KEY_F3),
113         KEY(2, 1, KEY_F4),
114         KEY(2, 2, KEY_UP),
115         KEY(3, 2, KEY_ENTER),
116         KEY(3, 3, KEY_LEFT),
117         0
118 };
119
120 static int osk_keymap[] = {
121         KEY(0, 0, KEY_F1),
122         KEY(0, 3, KEY_UP),
123         KEY(1, 1, KEY_LEFTCTRL),
124         KEY(1, 2, KEY_LEFT),
125         KEY(2, 0, KEY_SPACE),
126         KEY(2, 1, KEY_ESC),
127         KEY(2, 2, KEY_DOWN),
128         KEY(3, 2, KEY_ENTER),
129         KEY(3, 3, KEY_RIGHT),
130         0 
131 }; 
132
133 static int *keymap;
134
135 static irqreturn_t omap_kp_interrupt(int irq, void *dev_id,
136                                      struct pt_regs *regs)
137 {
138         /* disable keyboard interrupt and schedule for handling */
139         omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
140         tasklet_schedule(&kp_tasklet);
141
142         return IRQ_HANDLED;
143 }
144
145 static void omap_kp_timer(unsigned long data)
146 {
147         tasklet_schedule(&kp_tasklet);
148 }
149
150 static void omap_kp_scan_keypad(unsigned char *state)
151 {
152         int col = 0;
153
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);
158
159                 if (machine_is_omap_osk() || machine_is_omap_h2() || machine_is_omap_h3()) {
160                         udelay(9);
161                 } else {
162                         udelay(2);
163                 }
164
165                 state[col] = ~omap_readw(OMAP_MPUIO_BASE + OMAP_MPUIO_KBR_LATCH) & 0xff;
166         }
167         omap_writew(0x00, OMAP_MPUIO_BASE + OMAP_MPUIO_KBC);
168         udelay(2);
169 }
170
171 static inline int omap_kp_find_key(int col, int row)
172 {
173         int i, key;
174
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;
179         return -1;
180 }
181
182 static void omap_kp_tasklet(unsigned long data)
183 {
184         unsigned char new_state[8], changed, key_down = 0;
185         int col, row;
186         int spurious = 0;
187
188         /* check for any changes */
189         omap_kp_scan_keypad(new_state);
190
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];
195                 if (changed == 0)
196                         continue;
197
198                 for (row = 0; row < 8; row++) {
199                         int key;
200                         if (!(changed & (1 << row)))
201                                 continue;
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");
204 #else
205                         key = omap_kp_find_key(col, row);
206                         if (key < 0) {
207                                 printk(KERN_WARNING "omap-keypad: Spurious key event %d-%d\n",
208                                        col, row);
209                                 /* We scan again after a couple of seconds */
210                                 spurious = 1;
211                                 continue;
212                         }
213
214                         input_report_key(&omap_kp_dev, key,
215                                          new_state[col] & (1 << row));
216 #endif
217                 }
218         }
219         memcpy(keypad_state, new_state, sizeof(keypad_state));
220
221         if (key_down) {
222                 int delay = HZ / 20;
223                 /* some key is pressed - keep irq disabled and use timer
224                  * to poll the keypad */
225                 if (spurious)
226                         delay = 2 * HZ;
227                 mod_timer(&kp_timer, jiffies + delay);
228         } else {
229                 /* enable interrupts */
230                 omap_writew(0, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
231         }
232 }
233
234 static int __init omap_kp_init(void)
235 {
236         int i;
237
238         printk(KERN_INFO "OMAP Keypad Driver\n");
239
240         /* Disable the interrupt for the MPUIO keyboard */
241         omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
242
243         if (machine_is_omap_h2() || machine_is_omap_h3()) {
244                 keymap = h2_keymap;
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()) {
249                 keymap = osk_keymap;
250         } else {
251                 keymap = test_keymap;
252         }
253
254         init_timer(&kp_timer);
255         kp_timer.function = omap_kp_timer;
256
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)
261                 return -EINVAL;
262
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);
269
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);
276
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);
283
284                 omap_writew(0xff, OMAP_MPUIO_BASE + OMAP_MPUIO_GPIO_DEBOUNCING);
285         }
286
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);
290
291         return 0;
292 }
293
294 static void __exit omap_kp_exit(void)
295 {
296         /* disable keypad interrupt handling */
297         tasklet_disable(&kp_tasklet);
298         omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
299
300         free_irq(INT_KEYBOARD, 0);
301         del_timer_sync(&kp_timer);
302
303         /* unregister everything */
304         input_unregister_device(&omap_kp_dev);
305 }
306
307 module_init(omap_kp_init);
308 module_exit(omap_kp_exit);
309
310 MODULE_AUTHOR("Timo Teräs");
311 MODULE_DESCRIPTION("OMAP Keypad Driver");
312 MODULE_LICENSE("GPL");