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