]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/input/keyboard/omap-keypad.c
ARM: OMAP: Convert drivers to new irq trigger modes
[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 <linux/platform_device.h>
35 #include <asm/arch/irqs.h>
36 #include <asm/arch/gpio.h>
37 #include <asm/arch/hardware.h>
38 #include <asm/arch/keypad.h>
39 #include <asm/arch/menelaus.h>
40 #include <asm/io.h>
41 #include <asm/errno.h>
42 #include <asm/mach-types.h>
43 #include <asm/arch/mux.h>
44
45 #undef NEW_BOARD_LEARNING_MODE
46
47 static void omap_kp_tasklet(unsigned long);
48 static void omap_kp_timer(unsigned long);
49
50 static unsigned char keypad_state[8];
51
52 struct omap_kp {
53         struct input_dev *input;
54         struct timer_list timer;
55         unsigned int irq;
56         unsigned int rows;
57         unsigned int cols;
58 };
59
60 DECLARE_TASKLET_DISABLED(kp_tasklet, omap_kp_tasklet, 0);
61
62 #define KEY(col, row, val) (((col) << 28) | ((row) << 24) | (val))
63
64 static int test_keymap[] = {
65         KEY(0, 0, KEY_F4),
66         KEY(1, 0, KEY_LEFT),
67         KEY(2, 0, KEY_F1),
68         KEY(0, 1, KEY_DOWN),
69         KEY(1, 1, KEY_ENTER),
70         KEY(2, 1, KEY_UP),
71         KEY(0, 2, KEY_F3),
72         KEY(1, 2, KEY_RIGHT),
73         KEY(2, 2, KEY_F2),
74         0
75 };
76
77 static int *keymap;
78 static unsigned int *row_gpios;
79 static unsigned int *col_gpios;
80
81 #ifdef CONFIG_ARCH_OMAP2
82 static void set_col_gpio_val(struct omap_kp *omap_kp, u8 value)
83 {
84         int col;
85         for (col = 0; col < omap_kp->cols; col++) {
86                 if (value & (1 << col))
87                         omap_set_gpio_dataout(col_gpios[col], 1);
88                 else
89                         omap_set_gpio_dataout(col_gpios[col], 0);
90         }
91 }
92
93 static u8 get_row_gpio_val(struct omap_kp *omap_kp)
94 {
95         int row;
96         u8 value = 0;
97
98         for (row = 0; row < omap_kp->cols; row++) {
99                 if (omap_get_gpio_datain(row_gpios[row]))
100                         value |= (1 << row);
101         }
102         return value;
103 }
104 #else
105 #define         set_col_gpio_val(x, y)  do {} while (0)
106 #define         get_row_gpio_val(x)     0
107 #endif
108
109 static irqreturn_t omap_kp_interrupt(int irq, void *dev_id,
110                                      struct pt_regs *regs)
111 {
112         struct omap_kp *omap_kp = dev_id;
113
114         /* disable keyboard interrupt and schedule for handling */
115         if (cpu_is_omap24xx()) {
116                 int i;
117                 for (i = 0; i < omap_kp->rows; i++)
118                         disable_irq(OMAP_GPIO_IRQ(row_gpios[i]));
119         } else {
120                 /* disable keyboard interrupt and schedule for handling */
121                 omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
122         }
123
124         tasklet_schedule(&kp_tasklet);
125
126         return IRQ_HANDLED;
127 }
128
129 static void omap_kp_timer(unsigned long data)
130 {
131         tasklet_schedule(&kp_tasklet);
132 }
133
134 static void omap_kp_scan_keypad(struct omap_kp *omap_kp, unsigned char *state)
135 {
136         int col = 0;
137
138         /* read the keypad status */
139         if (cpu_is_omap24xx()) {
140                 int i;
141                 for (i = 0; i < omap_kp->rows; i++)
142                         disable_irq(OMAP_GPIO_IRQ(row_gpios[i]));
143         } else {
144                 /* disable keyboard interrupt and schedule for handling */
145                 omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
146         }
147         if (!cpu_is_omap24xx()) {
148                 /* read the keypad status */
149                 omap_writew(0xff, OMAP_MPUIO_BASE + OMAP_MPUIO_KBC);
150                 for (col = 0; col < omap_kp->cols; col++) {
151                         omap_writew(~(1 << col) & 0xff, OMAP_MPUIO_BASE + OMAP_MPUIO_KBC);
152
153                         if (machine_is_omap_osk() || machine_is_omap_h2() || machine_is_omap_h3()) {
154                                 udelay(9);
155                         } else {
156                                 udelay(4);
157                         }
158                         state[col] = ~omap_readw(OMAP_MPUIO_BASE + OMAP_MPUIO_KBR_LATCH) & 0xff;
159                 }
160                 omap_writew(0x00, OMAP_MPUIO_BASE + OMAP_MPUIO_KBC);
161                 udelay(2);
162         } else {
163                 /* read the keypad status */
164                 for (col = 0; col < omap_kp->cols; col++) {
165                         set_col_gpio_val(omap_kp, ~(1 << col));
166                         state[col] = ~(get_row_gpio_val(omap_kp)) & 0x3f;
167                 }
168                 set_col_gpio_val(omap_kp, 0);
169         }
170 }
171
172 static inline int omap_kp_find_key(int col, int row)
173 {
174         int i, key;
175
176         key = KEY(col, row, 0);
177         for (i = 0; keymap[i] != 0; i++)
178                 if ((keymap[i] & 0xff000000) == key)
179                         return keymap[i] & 0x00ffffff;
180         return -1;
181 }
182
183 static void omap_kp_tasklet(unsigned long data)
184 {
185         struct omap_kp *omap_kp_data = (struct omap_kp *) data;
186         unsigned char new_state[8], changed, key_down = 0;
187         int col, row;
188         int spurious = 0;
189
190         /* check for any changes */
191         omap_kp_scan_keypad(omap_kp_data, new_state);
192
193         /* check for changes and print those */
194         for (col = 0; col < omap_kp_data->cols; col++) {
195                 changed = new_state[col] ^ keypad_state[col];
196                 key_down |= new_state[col];
197                 if (changed == 0)
198                         continue;
199
200                 for (row = 0; row < omap_kp_data->rows; row++) {
201                         int key;
202                         if (!(changed & (1 << row)))
203                                 continue;
204 #ifdef NEW_BOARD_LEARNING_MODE
205                         printk(KERN_INFO "omap-keypad: key %d-%d %s\n", col, row, (new_state[col] & (1 << row)) ? "pressed" : "released");
206 #else
207                         key = omap_kp_find_key(col, row);
208                         if (key < 0) {
209                                 printk(KERN_WARNING "omap-keypad: Spurious key event %d-%d\n",
210                                        col, row);
211                                 /* We scan again after a couple of seconds */
212                                 spurious = 1;
213                                 continue;
214                         }
215
216                         input_report_key(omap_kp_data->input, key,
217                                          new_state[col] & (1 << row));
218 #endif
219                 }
220         }
221         memcpy(keypad_state, new_state, sizeof(keypad_state));
222
223         if (key_down) {
224                 int delay = HZ / 20;
225                 /* some key is pressed - keep irq disabled and use timer
226                  * to poll the keypad */
227                 if (spurious)
228                         delay = 2 * HZ;
229                 mod_timer(&omap_kp_data->timer, jiffies + delay);
230         } else {
231                 /* enable interrupts */
232                 if (cpu_is_omap24xx()) {
233                         int i;
234                         for (i = 0; i < omap_kp_data->rows; i++)
235                                 enable_irq(OMAP_GPIO_IRQ(row_gpios[i]));
236                 } else {
237                         omap_writew(0, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
238                 }
239         }
240 }
241
242 #ifdef CONFIG_PM
243 static int omap_kp_suspend(struct platform_device *dev, pm_message_t state)
244 {
245         /* Nothing yet */
246
247         return 0;
248 }
249
250 static int omap_kp_resume(struct platform_device *dev)
251 {
252         /* Nothing yet */
253
254         return 0;
255 }
256 #else
257 #define omap_kp_suspend NULL
258 #define omap_kp_resume  NULL
259 #endif
260
261 static int __init omap_kp_probe(struct platform_device *pdev)
262 {
263         struct omap_kp *omap_kp;
264         struct input_dev *input_dev;
265         struct omap_kp_platform_data *pdata =  pdev->dev.platform_data;
266         int i;
267
268         if (!pdata->rows || !pdata->cols) {
269                 printk(KERN_ERR "No rows and cols from pdata\n");
270                 return -EINVAL;
271         }
272
273         omap_kp = kzalloc(sizeof(struct omap_kp), GFP_KERNEL);
274         input_dev = input_allocate_device();
275         if (!omap_kp || !input_dev) {
276                 kfree(omap_kp);
277                 input_free_device(input_dev);
278                 return -ENOMEM;
279         }
280
281         platform_set_drvdata(pdev, omap_kp);
282
283         omap_kp->input = input_dev;
284
285         /* Disable the interrupt for the MPUIO keyboard */
286         if (!cpu_is_omap24xx())
287                 omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
288
289         if (!pdata->keymap)
290                 keymap = test_keymap;
291         else
292                 keymap = pdata->keymap;
293
294         if (pdata->rep)
295                 set_bit(EV_REP, input_dev->evbit);
296
297         if (pdata->row_gpios && pdata->col_gpios) {
298                 row_gpios = pdata->row_gpios;
299                 col_gpios = pdata->col_gpios;
300         }
301
302         if (cpu_is_omap24xx() && omap_has_menelaus()) {
303                 row_gpios[5] = 0;
304                 col_gpios[2] = 15;
305                 col_gpios[6] = 18;
306         }
307
308         omap_kp->rows = pdata->rows;
309         omap_kp->cols = pdata->cols;
310
311         if (cpu_is_omap24xx()) {
312                 /* Cols: outputs */
313                 for (i = 0; i < omap_kp->cols; i++) {
314                         if (omap_request_gpio(col_gpios[i]) < 0) {
315                                 printk(KERN_ERR "Failed to request"
316            "GPIO%d for keypad\n",
317            col_gpios[i]);
318                                 return -EINVAL;
319                         }
320                         omap_set_gpio_direction(col_gpios[i], 0);
321                 }
322                 /* Rows: inputs */
323                 for (i = 0; i < omap_kp->rows; i++) {
324                         if (omap_request_gpio(row_gpios[i]) < 0) {
325                                 printk(KERN_ERR "Failed to request"
326            "GPIO%d for keypad\n",
327            row_gpios[i]);
328                                 return -EINVAL;
329                         }
330                         omap_set_gpio_direction(row_gpios[i], 1);
331                 }
332         }
333
334         init_timer(&omap_kp->timer);
335         omap_kp->timer.function = omap_kp_timer;
336         omap_kp->timer.data = (unsigned long) omap_kp;
337
338         /* get the irq and init timer*/
339         tasklet_enable(&kp_tasklet);
340         kp_tasklet.data = (unsigned long) omap_kp;
341
342         omap_kp->irq = platform_get_irq(pdev, 0);
343         if (omap_kp->irq) {
344                 if (request_irq(omap_kp->irq, omap_kp_interrupt, 0,
345                                 "omap-keypad", omap_kp) < 0)
346                         return -EINVAL;
347         }
348
349         /* setup input device */
350         set_bit(EV_KEY, input_dev->evbit);
351         for (i = 0; keymap[i] != 0; i++)
352                 set_bit(keymap[i] & 0x00ffffff, input_dev->keybit);
353         input_dev->name = "omap-keypad";
354         input_dev->cdev.dev = &pdev->dev;
355         input_dev->private = omap_kp;
356         input_register_device(omap_kp->input);
357
358         if (machine_is_omap_h2() || machine_is_omap_h3() ||
359             machine_is_omap_perseus2()) {
360                 omap_writew(0xff, OMAP_MPUIO_BASE + OMAP_MPUIO_GPIO_DEBOUNCING);
361         }
362         /* scan current status and enable interrupt */
363         omap_kp_scan_keypad(omap_kp, keypad_state);
364         if (!cpu_is_omap24xx()) {
365                 omap_writew(0, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
366         } else {
367                 for (i = 0; i < omap_kp->rows; i++) {
368                         if (request_irq(OMAP_GPIO_IRQ(row_gpios[i]), omap_kp_interrupt,
369                                         SA_TRIGGER_FALLING, "omap-keypad", omap_kp) < 0)
370                                 return -EINVAL;
371                 }
372         }
373
374         return 0;
375 }
376
377 static int omap_kp_remove(struct platform_device *pdev)
378 {
379         struct omap_kp *omap_kp = platform_get_drvdata(pdev);
380
381         /* disable keypad interrupt handling */
382         tasklet_disable(&kp_tasklet);
383         if (cpu_is_omap24xx()) {
384                 int i;
385                 for (i = 0; i < omap_kp->cols; i++)
386                         omap_free_gpio(col_gpios[i]);
387                 for (i = 0; i < omap_kp->rows; i++) {
388                         omap_free_gpio(row_gpios[i]);
389                         free_irq(OMAP_GPIO_IRQ(row_gpios[i]), 0);
390                 }
391         } else {
392                 omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
393                 free_irq(omap_kp->irq, 0);
394         }
395
396         del_timer_sync(&omap_kp->timer);
397
398         /* unregister everything */
399         input_unregister_device(omap_kp->input);
400
401         kfree(omap_kp);
402
403         return 0;
404 }
405
406 static struct platform_driver omap_kp_driver = {
407         .probe          = omap_kp_probe,
408         .remove         = omap_kp_remove,
409         .suspend        = omap_kp_suspend,
410         .resume         = omap_kp_resume,
411         .driver         = {
412                 .name   = "omap-keypad",
413         },
414 };
415
416 static int __devinit omap_kp_init(void)
417 {
418         printk(KERN_INFO "OMAP Keypad Driver\n");
419         return platform_driver_register(&omap_kp_driver);
420 }
421
422 static void __exit omap_kp_exit(void)
423 {
424         platform_driver_unregister(&omap_kp_driver);
425 }
426
427 module_init(omap_kp_init);
428 module_exit(omap_kp_exit);
429
430 MODULE_AUTHOR("Timo Teräs");
431 MODULE_DESCRIPTION("OMAP Keypad Driver");
432 MODULE_LICENSE("GPL");