]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/input/keyboard/omap-keypad.c
5fe8e7a64f8c67b1c36e57a06760e353d4070af5
[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 static unsigned int keypad_irq = INT_KEYBOARD;
50
51 static struct timer_list kp_timer;
52 DECLARE_TASKLET_DISABLED(kp_tasklet, omap_kp_tasklet, 0);
53
54 #define KEY(col, row, val) (((col) << 28) | ((row) << 24) | (val))
55
56 static int h2_keymap[] = {
57         KEY(0, 0, KEY_LEFT),
58         KEY(0, 1, KEY_RIGHT),
59         KEY(0, 2, KEY_3),
60         KEY(0, 3, KEY_F10),
61         KEY(0, 4, KEY_F5),
62         KEY(0, 5, KEY_9),
63         KEY(1, 0, KEY_DOWN),
64         KEY(1, 1, KEY_UP),
65         KEY(1, 2, KEY_2),
66         KEY(1, 3, KEY_F9),
67         KEY(1, 4, KEY_F7),
68         KEY(1, 5, KEY_0),
69         KEY(2, 0, KEY_ENTER),
70         KEY(2, 1, KEY_6),
71         KEY(2, 2, KEY_1),
72         KEY(2, 3, KEY_F2),
73         KEY(2, 4, KEY_F6),
74         KEY(2, 5, KEY_HOME),
75         KEY(3, 0, KEY_8),
76         KEY(3, 1, KEY_5),
77         KEY(3, 2, KEY_F12),
78         KEY(3, 3, KEY_F3),
79         KEY(3, 4, KEY_F8),
80         KEY(3, 5, KEY_END),
81         KEY(4, 0, KEY_7),
82         KEY(4, 1, KEY_4),
83         KEY(4, 2, KEY_F11),
84         KEY(4, 3, KEY_F1),
85         KEY(4, 4, KEY_F4),
86         KEY(4, 5, KEY_ESC),
87         KEY(5, 0, KEY_F13),
88         KEY(5, 1, KEY_F14),
89         KEY(5, 2, KEY_F15),
90         KEY(5, 3, KEY_F16),
91         KEY(5, 4, KEY_SLEEP),
92         0
93 };
94
95 static int test_keymap[] = {
96         KEY(0, 0, KEY_F4),
97         KEY(1, 0, KEY_LEFT),
98         KEY(2, 0, KEY_F1),
99         KEY(0, 1, KEY_DOWN),
100         KEY(1, 1, KEY_ENTER),
101         KEY(2, 1, KEY_UP),
102         KEY(0, 2, KEY_F3),
103         KEY(1, 2, KEY_RIGHT),
104         KEY(2, 2, KEY_F2),
105         0
106 };
107
108 static int innovator_keymap[] = {
109         KEY(0, 0, KEY_F1),
110         KEY(0, 3, KEY_DOWN),
111         KEY(1, 1, KEY_F2),
112         KEY(1, 2, KEY_RIGHT),
113         KEY(2, 0, KEY_F3),
114         KEY(2, 1, KEY_F4),
115         KEY(2, 2, KEY_UP),
116         KEY(3, 2, KEY_ENTER),
117         KEY(3, 3, KEY_LEFT),
118         0
119 };
120
121 static int osk_keymap[] = {
122         KEY(0, 0, KEY_F1),
123         KEY(0, 3, KEY_UP),
124         KEY(1, 1, KEY_LEFTCTRL),
125         KEY(1, 2, KEY_LEFT),
126         KEY(2, 0, KEY_SPACE),
127         KEY(2, 1, KEY_ESC),
128         KEY(2, 2, KEY_DOWN),
129         KEY(3, 2, KEY_ENTER),
130         KEY(3, 3, KEY_RIGHT),
131         0 
132 }; 
133
134 static int p2_keymap[] = {
135         KEY(0,0,KEY_UP),
136         KEY(0,1,KEY_RIGHT),
137         KEY(0,2,KEY_LEFT),
138         KEY(0,3,KEY_DOWN),
139         KEY(0,4,KEY_CENTER),
140         KEY(0,5,KEY_0_5),
141         KEY(1,0,KEY_SOFT2),
142         KEY(1,1,KEY_SEND),
143         KEY(1,2,KEY_END),
144         KEY(1,3,KEY_VOLUMEDOWN),
145         KEY(1,4,KEY_VOLUMEUP),
146         KEY(1,5,KEY_RECORD),
147         KEY(2,0,KEY_SOFT1),
148         KEY(2,1,KEY_3),
149         KEY(2,2,KEY_6),
150         KEY(2,3,KEY_9),
151         KEY(2,4,KEY_SHARP),
152         KEY(2,5,KEY_2_5),
153         KEY(3,0,KEY_BACK),
154         KEY(3,1,KEY_2),
155         KEY(3,2,KEY_5),
156         KEY(3,3,KEY_8),
157         KEY(3,4,KEY_0),
158         KEY(3,5,KEY_HEADSETHOOK),
159         KEY(4,0,KEY_HOME),
160         KEY(4,1,KEY_1),
161         KEY(4,2,KEY_4),
162         KEY(4,3,KEY_7),
163         KEY(4,4,KEY_STAR),
164         KEY(4,5,KEY_POWER),
165         0
166 };
167
168 static int *keymap;
169
170 static irqreturn_t omap_kp_interrupt(int irq, void *dev_id,
171                                      struct pt_regs *regs)
172 {
173         /* disable keyboard interrupt and schedule for handling */
174         omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
175         tasklet_schedule(&kp_tasklet);
176
177         return IRQ_HANDLED;
178 }
179
180 static void omap_kp_timer(unsigned long data)
181 {
182         tasklet_schedule(&kp_tasklet);
183 }
184
185 static void omap_kp_scan_keypad(unsigned char *state)
186 {
187         int col = 0;
188
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);
193
194                 if (machine_is_omap_osk() || machine_is_omap_h2() || machine_is_omap_h3()) {
195                         udelay(9);
196                 } else {
197                         udelay(4);
198                 }
199
200                 state[col] = ~omap_readw(OMAP_MPUIO_BASE + OMAP_MPUIO_KBR_LATCH) & 0xff;
201         }
202         omap_writew(0x00, OMAP_MPUIO_BASE + OMAP_MPUIO_KBC);
203         udelay(2);
204 }
205
206 static inline int omap_kp_find_key(int col, int row)
207 {
208         int i, key;
209
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;
214         return -1;
215 }
216
217 static void omap_kp_tasklet(unsigned long data)
218 {
219         unsigned char new_state[8], changed, key_down = 0;
220         int col, row;
221         int spurious = 0;
222
223         /* check for any changes */
224         omap_kp_scan_keypad(new_state);
225
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];
230                 if (changed == 0)
231                         continue;
232
233                 for (row = 0; row < 8; row++) {
234                         int key;
235                         if (!(changed & (1 << row)))
236                                 continue;
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");
239 #else
240                         key = omap_kp_find_key(col, row);
241                         if (key < 0) {
242                                 printk(KERN_WARNING "omap-keypad: Spurious key event %d-%d\n",
243                                        col, row);
244                                 /* We scan again after a couple of seconds */
245                                 spurious = 1;
246                                 continue;
247                         }
248
249                         input_report_key(&omap_kp_dev, key,
250                                          new_state[col] & (1 << row));
251 #endif
252                 }
253         }
254         memcpy(keypad_state, new_state, sizeof(keypad_state));
255
256         if (key_down) {
257                 int delay = HZ / 20;
258                 /* some key is pressed - keep irq disabled and use timer
259                  * to poll the keypad */
260                 if (spurious)
261                         delay = 2 * HZ;
262                 mod_timer(&kp_timer, jiffies + delay);
263         } else {
264                 /* enable interrupts */
265                 omap_writew(0, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
266         }
267 }
268
269 static int __init omap_kp_init(void)
270 {
271         int i;
272
273         printk(KERN_INFO "OMAP Keypad Driver\n");
274
275         /* Disable the interrupt for the MPUIO keyboard */
276         omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
277
278         if (machine_is_omap_h2() || machine_is_omap_h3()) {
279                 keymap = h2_keymap;
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()) {
284                 keymap = osk_keymap;
285         } else if (machine_is_omap_perseus2()) {
286                 keymap = p2_keymap;
287                 keypad_irq = INT_730_MPUIO_KEYPAD;
288         } else {
289                 keymap = test_keymap;
290         }
291
292         init_timer(&kp_timer);
293         kp_timer.function = omap_kp_timer;
294
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)
299                 return -EINVAL;
300
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);
307
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);
314
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);
321
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);
329
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);
335
336                 omap_writew(0xff, OMAP_MPUIO_BASE + OMAP_MPUIO_GPIO_DEBOUNCING);
337         }
338
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);
342
343         return 0;
344 }
345
346 static void __exit omap_kp_exit(void)
347 {
348         /* disable keypad interrupt handling */
349         tasklet_disable(&kp_tasklet);
350         omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
351
352         free_irq(keypad_irq, 0);
353         del_timer_sync(&kp_timer);
354
355         /* unregister everything */
356         input_unregister_device(&omap_kp_dev);
357 }
358
359 module_init(omap_kp_init);
360 module_exit(omap_kp_exit);
361
362 MODULE_AUTHOR("Timo Teräs");
363 MODULE_DESCRIPTION("OMAP Keypad Driver");
364 MODULE_LICENSE("GPL");