]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/input/keyboard/omap-keypad.c
d57d2cb718fc4867c8c48c5fb9fede04ebc996cc
[linux-2.6-omap-h63xx.git] / drivers / input / keyboard / omap-keypad.c
1 /*
2  * linux/drivers/input/keyboard/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 <linux/mutex.h>
36 #include <linux/spinlock.h>
37 #include <linux/errno.h>
38 #include <linux/i2c/menelaus.h>
39 #include <mach/gpio.h>
40 #include <mach/keypad.h>
41 #include <asm/irq.h>
42 #include <mach/hardware.h>
43 #include <asm/io.h>
44 #include <asm/mach-types.h>
45 #include <mach/mux.h>
46
47 #undef NEW_BOARD_LEARNING_MODE
48
49 static void omap_kp_tasklet(unsigned long);
50 static void omap_kp_timer(unsigned long);
51
52 static unsigned char keypad_state[8];
53 static DEFINE_MUTEX(kp_enable_mutex);
54 static int kp_enable = 1;
55 static int kp_cur_group = -1;
56
57 struct omap_kp {
58         struct input_dev *input;
59         struct timer_list timer;
60         int irq;
61         unsigned int rows;
62         unsigned int cols;
63         unsigned long delay;
64         unsigned int debounce;
65         int suspended;
66         spinlock_t suspend_lock;
67 };
68
69 static DECLARE_TASKLET_DISABLED(kp_tasklet, omap_kp_tasklet, 0);
70
71 static int *keymap;
72 static unsigned int *row_gpios;
73 static unsigned int *col_gpios;
74
75 #ifdef CONFIG_ARCH_OMAP2
76 static void set_col_gpio_val(struct omap_kp *omap_kp, u8 value)
77 {
78         int col;
79
80         for (col = 0; col < omap_kp->cols; col++)
81                 gpio_set_value(col_gpios[col], value & (1 << col));
82 }
83
84 static u8 get_row_gpio_val(struct omap_kp *omap_kp)
85 {
86         int row;
87         u8 value = 0;
88
89         for (row = 0; row < omap_kp->rows; row++) {
90                 if (gpio_get_value(row_gpios[row]))
91                         value |= (1 << row);
92         }
93         return value;
94 }
95 #else
96 #define         set_col_gpio_val(x, y)  do {} while (0)
97 #define         get_row_gpio_val(x)     0
98 #endif
99
100 static irqreturn_t omap_kp_interrupt(int irq, void *dev_id)
101 {
102         struct omap_kp *omap_kp = dev_id;
103         unsigned long flags;
104
105         spin_lock_irqsave(&omap_kp->suspend_lock, flags);
106         if (omap_kp->suspended) {
107                 spin_unlock_irqrestore(&omap_kp->suspend_lock, flags);
108                 return IRQ_HANDLED;
109         }
110         spin_unlock_irqrestore(&omap_kp->suspend_lock, flags);
111
112         /* disable keyboard interrupt and schedule for handling */
113         if (cpu_is_omap24xx()) {
114                 int i;
115                 for (i = 0; i < omap_kp->rows; i++)
116                         disable_irq(OMAP_GPIO_IRQ(row_gpios[i]));
117         } else
118                 /* disable keyboard interrupt and schedule for handling */
119                 omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
120
121         tasklet_schedule(&kp_tasklet);
122
123         return IRQ_HANDLED;
124 }
125
126 static void omap_kp_timer(unsigned long data)
127 {
128         tasklet_schedule(&kp_tasklet);
129 }
130
131 static void omap_kp_scan_keypad(struct omap_kp *omap_kp, unsigned char *state)
132 {
133         int col = 0;
134
135         /* read the keypad status */
136         if (cpu_is_omap24xx()) {
137                 int i;
138                 for (i = 0; i < omap_kp->rows; i++)
139                         disable_irq(OMAP_GPIO_IRQ(row_gpios[i]));
140
141                 /* read the keypad status */
142                 for (col = 0; col < omap_kp->cols; col++) {
143                         set_col_gpio_val(omap_kp, ~(1 << col));
144                         state[col] = ~(get_row_gpio_val(omap_kp)) & 0x3f;
145                 }
146                 set_col_gpio_val(omap_kp, 0);
147
148         } else {
149                 /* disable keyboard interrupt and schedule for handling */
150                 omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
151
152                 /* read the keypad status */
153                 omap_writew(0xff, OMAP_MPUIO_BASE + OMAP_MPUIO_KBC);
154                 for (col = 0; col < omap_kp->cols; col++) {
155                         omap_writew(~(1 << col) & 0xff,
156                                     OMAP_MPUIO_BASE + OMAP_MPUIO_KBC);
157
158                         udelay(omap_kp->delay);
159
160                         state[col] = ~omap_readw(OMAP_MPUIO_BASE +
161                                                  OMAP_MPUIO_KBR_LATCH) & 0xff;
162                 }
163                 omap_writew(0x00, OMAP_MPUIO_BASE + OMAP_MPUIO_KBC);
164                 udelay(2);
165         }
166 }
167
168 static inline int omap_kp_find_key(int col, int row)
169 {
170         int i, key;
171
172         key = KEY(col, row, 0);
173         for (i = 0; keymap[i] != 0; i++)
174                 if ((keymap[i] & 0xff000000) == key)
175                         return keymap[i] & 0x00ffffff;
176         return -1;
177 }
178
179 static void omap_kp_tasklet(unsigned long data)
180 {
181         struct omap_kp *omap_kp_data = (struct omap_kp *) data;
182         unsigned char new_state[8], changed, key_down = 0;
183         int col, row;
184         int spurious = 0;
185
186         /* check for any changes */
187         omap_kp_scan_keypad(omap_kp_data, new_state);
188
189         /* check for changes and print those */
190         for (col = 0; col < omap_kp_data->cols; col++) {
191                 changed = new_state[col] ^ keypad_state[col];
192                 key_down |= new_state[col];
193                 if (changed == 0)
194                         continue;
195
196                 for (row = 0; row < omap_kp_data->rows; row++) {
197                         int key;
198                         if (!(changed & (1 << row)))
199                                 continue;
200 #ifdef NEW_BOARD_LEARNING_MODE
201                         printk(KERN_INFO "omap-keypad: key %d-%d %s\n", col,
202                                row, (new_state[col] & (1 << row)) ?
203                                "pressed" : "released");
204 #else
205                         key = omap_kp_find_key(col, row);
206                         if (key < 0) {
207                                 printk(KERN_WARNING
208                                       "omap-keypad: Spurious key event %d-%d\n",
209                                        col, row);
210                                 /* We scan again after a couple of seconds */
211                                 spurious = 1;
212                                 continue;
213                         }
214
215                         if (!(kp_cur_group == (key & GROUP_MASK) ||
216                               kp_cur_group == -1))
217                                 continue;
218
219                         kp_cur_group = key & GROUP_MASK;
220                         input_report_key(omap_kp_data->input, key & ~GROUP_MASK,
221                                          new_state[col] & (1 << row));
222 #endif
223                 }
224         }
225         memcpy(keypad_state, new_state, sizeof(keypad_state));
226
227         if (key_down) {
228                 int delay = HZ / 20;
229                 /* some key is pressed - keep irq disabled and use timer
230                  * to poll the keypad */
231                 if (spurious)
232                         delay = 2 * HZ;
233                 mod_timer(&omap_kp_data->timer, jiffies + delay);
234         } else {
235                 /* enable interrupts */
236                 if (cpu_is_omap24xx()) {
237                         int i;
238                         for (i = 0; i < omap_kp_data->rows; i++)
239                                 enable_irq(OMAP_GPIO_IRQ(row_gpios[i]));
240                 } else {
241                         omap_writew(0, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
242                         kp_cur_group = -1;
243                 }
244         }
245 }
246
247 static ssize_t omap_kp_enable_show(struct device *dev,
248                                    struct device_attribute *attr, char *buf)
249 {
250         return sprintf(buf, "%u\n", kp_enable);
251 }
252
253 static ssize_t omap_kp_enable_store(struct device *dev, struct device_attribute *attr,
254                                     const char *buf, size_t count)
255 {
256         int state;
257
258         if (sscanf(buf, "%u", &state) != 1)
259                 return -EINVAL;
260
261         if ((state != 1) && (state != 0))
262                 return -EINVAL;
263
264         mutex_lock(&kp_enable_mutex);
265         if (state != kp_enable) {
266                 if (state)
267                         enable_irq(INT_KEYBOARD);
268                 else
269                         disable_irq(INT_KEYBOARD);
270                 kp_enable = state;
271         }
272         mutex_unlock(&kp_enable_mutex);
273
274         return strnlen(buf, count);
275 }
276
277 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, omap_kp_enable_show, omap_kp_enable_store);
278
279 #ifdef CONFIG_PM
280 static int omap_kp_suspend(struct platform_device *dev, pm_message_t state)
281 {
282         struct omap_kp *omap_kp = platform_get_drvdata(dev);
283         unsigned long flags;
284         spin_lock_irqsave(&omap_kp->suspend_lock, flags);
285
286         /*
287          * Re-enable the interrupt in case it has been masked by the
288          * handler and a key is still pressed.  We need the interrupt
289          * to wake us up from suspended.
290          */
291         if (cpu_class_is_omap1())
292                 omap_writew(0, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
293
294         omap_kp->suspended = 1;
295
296         spin_unlock_irqrestore(&omap_kp->suspend_lock, flags);
297         return 0;
298 }
299
300 static int omap_kp_resume(struct platform_device *dev)
301 {
302         struct omap_kp *omap_kp = platform_get_drvdata(dev);
303
304         omap_kp->suspended = 0;
305         return 0;
306 }
307 #else
308 #define omap_kp_suspend NULL
309 #define omap_kp_resume  NULL
310 #endif
311
312 static int __init omap_kp_probe(struct platform_device *pdev)
313 {
314         struct omap_kp *omap_kp;
315         struct input_dev *input_dev;
316         struct omap_kp_platform_data *pdata =  pdev->dev.platform_data;
317         int i, col_idx = 0, row_idx = 0, irq_idx, ret;
318
319         if (!pdata->rows || !pdata->cols || !pdata->keymap) {
320                 printk(KERN_ERR "No rows, cols or keymap from pdata\n");
321                 return -EINVAL;
322         }
323
324         omap_kp = kzalloc(sizeof(struct omap_kp), GFP_KERNEL);
325         input_dev = input_allocate_device();
326         if (!omap_kp || !input_dev) {
327                 kfree(omap_kp);
328                 input_free_device(input_dev);
329                 return -ENOMEM;
330         }
331
332         platform_set_drvdata(pdev, omap_kp);
333
334         spin_lock_init(&omap_kp->suspend_lock);
335         omap_kp->input = input_dev;
336         omap_kp->suspended = 0;
337
338         /* Disable the interrupt for the MPUIO keyboard */
339         if (!cpu_is_omap24xx())
340                 omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
341
342         keymap = pdata->keymap;
343
344         if (pdata->rep)
345                 __set_bit(EV_REP, input_dev->evbit);
346
347         if (pdata->delay)
348                 omap_kp->delay = pdata->delay;
349
350         if (pdata->row_gpios && pdata->col_gpios) {
351                 row_gpios = pdata->row_gpios;
352                 col_gpios = pdata->col_gpios;
353         }
354
355         omap_kp->rows = pdata->rows;
356         omap_kp->cols = pdata->cols;
357
358         if (cpu_is_omap24xx()) {
359                 /* Cols: outputs */
360                 for (col_idx = 0; col_idx < omap_kp->cols; col_idx++) {
361                         if (gpio_request(col_gpios[col_idx], "omap_kp_col") < 0) {
362                                 printk(KERN_ERR "Failed to request"
363                                        "GPIO%d for keypad\n",
364                                        col_gpios[col_idx]);
365                                 goto err1;
366                         }
367                         gpio_direction_output(col_gpios[col_idx], 0);
368                 }
369                 /* Rows: inputs */
370                 for (row_idx = 0; row_idx < omap_kp->rows; row_idx++) {
371                         if (gpio_request(row_gpios[row_idx], "omap_kp_row") < 0) {
372                                 printk(KERN_ERR "Failed to request"
373                                        "GPIO%d for keypad\n",
374                                        row_gpios[row_idx]);
375                                 goto err2;
376                         }
377                         gpio_direction_input(row_gpios[row_idx]);
378                 }
379         } else {
380                 col_idx = 0;
381                 row_idx = 0;
382         }
383
384         setup_timer(&omap_kp->timer, omap_kp_timer, (unsigned long)omap_kp);
385
386         /* get the irq and init timer*/
387         tasklet_enable(&kp_tasklet);
388         kp_tasklet.data = (unsigned long) omap_kp;
389
390         ret = device_create_file(&pdev->dev, &dev_attr_enable);
391         if (ret < 0)
392                 goto err2;
393
394         /* setup input device */
395         __set_bit(EV_KEY, input_dev->evbit);
396         for (i = 0; keymap[i] != 0; i++)
397                 __set_bit(keymap[i] & KEY_MAX, input_dev->keybit);
398         input_dev->name = "omap-keypad";
399         input_dev->phys = "omap-keypad/input0";
400         input_dev->dev.parent = &pdev->dev;
401
402         input_dev->id.bustype = BUS_HOST;
403         input_dev->id.vendor = 0x0001;
404         input_dev->id.product = 0x0001;
405         input_dev->id.version = 0x0100;
406
407         ret = input_register_device(omap_kp->input);
408         if (ret < 0) {
409                 printk(KERN_ERR "Unable to register omap-keypad input device\n");
410                 goto err3;
411         }
412
413         if (pdata->dbounce)
414                 omap_writew(0xff, OMAP_MPUIO_BASE + OMAP_MPUIO_GPIO_DEBOUNCING);
415
416         /* scan current status and enable interrupt */
417         omap_kp_scan_keypad(omap_kp, keypad_state);
418         if (!cpu_is_omap24xx()) {
419                 omap_kp->irq = platform_get_irq(pdev, 0);
420                 if (omap_kp->irq >= 0) {
421                         if (request_irq(omap_kp->irq, omap_kp_interrupt, 0,
422                                         "omap-keypad", omap_kp) < 0)
423                                 goto err4;
424                 }
425                 omap_writew(0, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
426         } else {
427                 for (irq_idx = 0; irq_idx < omap_kp->rows; irq_idx++) {
428                         if (request_irq(OMAP_GPIO_IRQ(row_gpios[irq_idx]),
429                                         omap_kp_interrupt,
430                                         IRQF_TRIGGER_FALLING,
431                                         "omap-keypad", omap_kp) < 0)
432                                 goto err5;
433                 }
434         }
435         return 0;
436 err5:
437         for (i = irq_idx - 1; i >=0; i--)
438                 free_irq(row_gpios[i], 0);
439 err4:
440         input_unregister_device(omap_kp->input);
441         input_dev = NULL;
442 err3:
443         device_remove_file(&pdev->dev, &dev_attr_enable);
444 err2:
445         for (i = row_idx - 1; i >=0; i--)
446                 gpio_free(row_gpios[i]);
447 err1:
448         for (i = col_idx - 1; i >=0; i--)
449                 gpio_free(col_gpios[i]);
450
451         kfree(omap_kp);
452         input_free_device(input_dev);
453
454         return -EINVAL;
455 }
456
457 static int omap_kp_remove(struct platform_device *pdev)
458 {
459         struct omap_kp *omap_kp = platform_get_drvdata(pdev);
460
461         /* disable keypad interrupt handling */
462         tasklet_disable(&kp_tasklet);
463         if (cpu_is_omap24xx()) {
464                 int i;
465                 for (i = 0; i < omap_kp->cols; i++)
466                         gpio_free(col_gpios[i]);
467                 for (i = 0; i < omap_kp->rows; i++) {
468                         gpio_free(row_gpios[i]);
469                         free_irq(OMAP_GPIO_IRQ(row_gpios[i]), 0);
470                 }
471         } else {
472                 omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
473                 free_irq(omap_kp->irq, 0);
474         }
475
476         del_timer_sync(&omap_kp->timer);
477         tasklet_kill(&kp_tasklet);
478
479         /* unregister everything */
480         input_unregister_device(omap_kp->input);
481
482         kfree(omap_kp);
483
484         return 0;
485 }
486
487 static struct platform_driver omap_kp_driver = {
488         .probe          = omap_kp_probe,
489         .remove         = omap_kp_remove,
490         .suspend        = omap_kp_suspend,
491         .resume         = omap_kp_resume,
492         .driver         = {
493                 .name   = "omap-keypad",
494                 .owner  = THIS_MODULE,
495         },
496 };
497
498 static int __devinit omap_kp_init(void)
499 {
500         printk(KERN_INFO "OMAP Keypad Driver\n");
501         return platform_driver_register(&omap_kp_driver);
502 }
503
504 static void __exit omap_kp_exit(void)
505 {
506         platform_driver_unregister(&omap_kp_driver);
507 }
508
509 module_init(omap_kp_init);
510 module_exit(omap_kp_exit);
511
512 MODULE_AUTHOR("Timo Teräs");
513 MODULE_DESCRIPTION("OMAP Keypad Driver");
514 MODULE_LICENSE("GPL");
515 MODULE_ALIAS("platform:omap-keypad");