Merge current mainline tree into linux-omap tree
[pandora-kernel.git] / drivers / input / keyboard / omap-twl4030keypad.c
1 /*
2  * drivers/input/keyboard/omap-twl4030keypad.c
3  *
4  * Copyright (C) 2007 Texas Instruments, Inc.
5  * Copyright (C) 2008 Nokia Corporation
6  *
7  * Code re-written for 2430SDP by:
8  * Syed Mohammed Khasim <x0khasim@ti.com>
9  *
10  * Initial Code:
11  * Manjunatha G K <manjugk@ti.com>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26  */
27
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/interrupt.h>
31 #include <linux/types.h>
32 #include <linux/input.h>
33 #include <linux/kernel.h>
34 #include <linux/mutex.h>
35 #include <linux/delay.h>
36 #include <linux/bitops.h>
37 #include <linux/platform_device.h>
38 #include <linux/i2c.h>
39 #include <linux/i2c/twl4030.h>
40 #include <linux/irq.h>
41 #include "twl4030-keypad.h"
42
43 #define PTV_PRESCALER           4
44
45 #define MAX_ROWS                8 /* TWL4030 hardlimit */
46 #define ROWCOL_MASK             0xFF000000
47 #define KEYNUM_MASK             0x00FFFFFF
48 #define KEY(col, row, val) (((col) << 28) | ((row) << 24) | (val))
49
50 /* Global variables */
51
52 struct omap_keypad {
53         int             *keymap;
54         unsigned int    keymapsize;
55         u16             kp_state[MAX_ROWS];
56         int             n_rows;
57         int             n_cols;
58         int             irq;
59
60         struct device   *dbg_dev;
61         struct input_dev *omap_twl4030kp;
62
63         /* sync read/write */
64         struct mutex    mutex;
65 };
66
67 static int twl4030_kpread(struct omap_keypad *kp,
68                 u32 module, u8 *data, u32 reg, u8 num_bytes)
69 {
70         int ret;
71
72         ret = twl4030_i2c_read(module, data, reg, num_bytes);
73         if (ret < 0) {
74                 dev_warn(kp->dbg_dev,
75                         "Couldn't read TWL4030: %X - ret %d[%x]\n",
76                          reg, ret, ret);
77                 return ret;
78         }
79         return ret;
80 }
81
82 static int twl4030_kpwrite_u8(struct omap_keypad *kp,
83                 u32 module, u8 data, u32 reg)
84 {
85         int ret;
86
87         ret = twl4030_i2c_write_u8(module, data, reg);
88         if (ret < 0) {
89                 dev_warn(kp->dbg_dev,
90                         "Could not write TWL4030: %X - ret %d[%x]\n",
91                          reg, ret, ret);
92                 return ret;
93         }
94         return ret;
95 }
96
97 static int omap_kp_find_key(struct omap_keypad *kp, int col, int row)
98 {
99         int i, rc;
100
101         rc = KEY(col, row, 0);
102         for (i = 0; i < kp->keymapsize; i++)
103                 if ((kp->keymap[i] & ROWCOL_MASK) == rc)
104                         return kp->keymap[i] & KEYNUM_MASK;
105
106         return -EINVAL;
107 }
108
109 static inline u16 omap_kp_col_xlate(struct omap_keypad *kp, u8 col)
110 {
111         /* If all bits in a row are active for all coloumns then
112          * we have that row line connected to gnd. Mark this
113          * key on as if it was on matrix position n_cols (ie
114          * one higher than the size of the matrix).
115          */
116         if (col == 0xFF)
117                 return 1 << kp->n_cols;
118         else
119                 return col & ((1 << kp->n_cols) - 1);
120 }
121
122 static int omap_kp_read_kp_matrix_state(struct omap_keypad *kp, u16 *state)
123 {
124         u8 new_state[MAX_ROWS];
125         int row;
126         int ret = twl4030_kpread(kp, TWL4030_MODULE_KEYPAD,
127                                  new_state, KEYP_FULL_CODE_7_0, kp->n_rows);
128         if (ret >= 0) {
129                 for (row = 0; row < kp->n_rows; row++)
130                         state[row] = omap_kp_col_xlate(kp, new_state[row]);
131         }
132         return ret;
133 }
134
135 static int omap_kp_is_in_ghost_state(struct omap_keypad *kp, u16 *key_state)
136 {
137         int i;
138         u16 check = 0;
139
140         for (i = 0; i < kp->n_rows; i++) {
141                 u16 col = key_state[i];
142
143                 if ((col & check) && hweight16(col) > 1)
144                         return 1;
145                 check |= col;
146         }
147
148         return 0;
149 }
150
151 static void twl4030_kp_scan(struct omap_keypad *kp, int release_all)
152 {
153         u16 new_state[MAX_ROWS];
154         int col, row;
155
156         if (release_all)
157                 memset(new_state, 0, sizeof(new_state));
158         else {
159                 /* check for any changes */
160                 int ret = omap_kp_read_kp_matrix_state(kp, new_state);
161                 if (ret < 0)    /* panic ... */
162                         return;
163
164                 if (omap_kp_is_in_ghost_state(kp, new_state))
165                         return;
166         }
167
168         mutex_lock(&kp->mutex);
169
170         /* check for changes and print those */
171         for (row = 0; row < kp->n_rows; row++) {
172                 int changed = new_state[row] ^ kp->kp_state[row];
173
174                 if (!changed)
175                         continue;
176
177                 for (col = 0; col < kp->n_cols + 1; col++) {
178                         int key;
179
180                         if (!(changed & (1 << col)))
181                                 continue;
182
183                         dev_dbg(kp->dbg_dev, "key [%d:%d] %s\n", row, col,
184                                 (new_state[row] & (1 << col)) ?
185                                 "press" : "release");
186
187                         key = omap_kp_find_key(kp, col, row);
188                         if (key < 0)
189                                 dev_warn(kp->dbg_dev,
190                                         "Spurious key event %d-%d\n",
191                                          col, row);
192                         else
193                                 input_report_key(kp->omap_twl4030kp, key,
194                                                  new_state[row] & (1 << col));
195                 }
196                 kp->kp_state[row] = new_state[row];
197         }
198
199         mutex_unlock(&kp->mutex);
200 }
201
202 /*
203  * Keypad interrupt handler
204  */
205 static irqreturn_t do_kp_irq(int irq, void *_kp)
206 {
207         struct omap_keypad *kp = _kp;
208         u8 reg;
209         int ret;
210
211 #ifdef CONFIG_LOCKDEP
212         /* WORKAROUND for lockdep forcing IRQF_DISABLED on us, which
213          * we don't want and can't tolerate.  Although it might be
214          * friendlier not to borrow this thread context...
215          */
216         local_irq_enable();
217 #endif
218
219         /* Read & Clear TWL4030 pending interrupt */
220         ret = twl4030_kpread(kp, TWL4030_MODULE_KEYPAD, &reg, KEYP_ISR1, 1);
221
222         /* Release all keys if I2C has gone bad or
223          * the KEYP has gone to idle state */
224         if ((ret >= 0) && (reg & KEYP_IMR1_KP))
225                 twl4030_kp_scan(kp, 0);
226         else
227                 twl4030_kp_scan(kp, 1);
228
229         return IRQ_HANDLED;
230 }
231
232 /*
233  * Registers keypad device with input sub system
234  * and configures TWL4030 keypad registers
235  */
236 static int __init omap_kp_probe(struct platform_device *pdev)
237 {
238         u8 reg;
239         int i;
240         int ret = 0;
241         struct omap_keypad *kp;
242         struct twl4030_keypad_data *pdata = pdev->dev.platform_data;
243
244         kp = kzalloc(sizeof(*kp), GFP_KERNEL);
245         if (!kp)
246                 return -ENOMEM;
247
248         if (!pdata->rows || !pdata->cols || !pdata->keymap) {
249                 dev_err(&pdev->dev, "No rows, cols or keymap from pdata\n");
250                 kfree(kp);
251                 return -EINVAL;
252         }
253
254         dev_set_drvdata(&pdev->dev, kp);
255
256         /* Get the debug Device */
257         kp->dbg_dev = &pdev->dev;
258
259         kp->omap_twl4030kp = input_allocate_device();
260         if (!kp->omap_twl4030kp) {
261                 kfree(kp);
262                 return -ENOMEM;
263         }
264
265         mutex_init(&kp->mutex);
266
267         kp->keymap = pdata->keymap;
268         kp->keymapsize = pdata->keymapsize;
269         kp->n_rows = pdata->rows;
270         kp->n_cols = pdata->cols;
271         kp->irq = pdata->irq;
272
273         /* setup input device */
274         set_bit(EV_KEY, kp->omap_twl4030kp->evbit);
275
276         /* Enable auto repeat feature of Linux input subsystem */
277         if (pdata->rep)
278                 set_bit(EV_REP, kp->omap_twl4030kp->evbit);
279
280         for (i = 0; i < kp->keymapsize; i++)
281                 set_bit(kp->keymap[i] & KEYNUM_MASK,
282                                 kp->omap_twl4030kp->keybit);
283
284         kp->omap_twl4030kp->name        = "omap_twl4030keypad";
285         kp->omap_twl4030kp->phys        = "omap_twl4030keypad/input0";
286         kp->omap_twl4030kp->dev.parent  = &pdev->dev;
287
288         kp->omap_twl4030kp->id.bustype  = BUS_HOST;
289         kp->omap_twl4030kp->id.vendor   = 0x0001;
290         kp->omap_twl4030kp->id.product  = 0x0001;
291         kp->omap_twl4030kp->id.version  = 0x0003;
292
293         kp->omap_twl4030kp->keycode     = kp->keymap;
294         kp->omap_twl4030kp->keycodesize = sizeof(unsigned int);
295         kp->omap_twl4030kp->keycodemax  = kp->keymapsize;
296
297         ret = input_register_device(kp->omap_twl4030kp);
298         if (ret < 0) {
299                 dev_err(kp->dbg_dev,
300                         "Unable to register twl4030 keypad device\n");
301                 goto err2;
302         }
303
304         /* Disable auto-repeat */
305         reg = KEYP_CTRL_NOAUTORPT;
306         ret = twl4030_kpwrite_u8(kp, TWL4030_MODULE_KEYPAD, reg, KEYP_CTRL);
307         if (ret < 0)
308                 goto err3;
309
310         /* Enable TO rising and KP rising and falling edge detection */
311         reg = KEYP_EDR_KP_BOTH | KEYP_EDR_TO_RISING;
312         ret = twl4030_kpwrite_u8(kp, TWL4030_MODULE_KEYPAD, reg, KEYP_EDR);
313         if (ret < 0)
314                 goto err3;
315
316         /* Set PTV prescaler Field */
317         reg = (PTV_PRESCALER << KEYP_LK_PTV_PTV_SHIFT);
318         ret = twl4030_kpwrite_u8(kp, TWL4030_MODULE_KEYPAD, reg, KEYP_LK_PTV);
319         if (ret < 0)
320                 goto err3;
321
322         /* Set key debounce time to 20 ms */
323         i = KEYP_PERIOD_US(20000, PTV_PRESCALER);
324         ret = twl4030_kpwrite_u8(kp, TWL4030_MODULE_KEYPAD, i, KEYP_DEB);
325         if (ret < 0)
326                 goto err3;
327
328         /* Set timeout period to 100 ms */
329         i = KEYP_PERIOD_US(200000, PTV_PRESCALER);
330         ret = twl4030_kpwrite_u8(kp, TWL4030_MODULE_KEYPAD,
331                                  (i & 0xFF), KEYP_TIMEOUT_L);
332         if (ret < 0)
333                 goto err3;
334
335         ret = twl4030_kpwrite_u8(kp, TWL4030_MODULE_KEYPAD,
336                                  (i >> 8), KEYP_TIMEOUT_H);
337         if (ret < 0)
338                 goto err3;
339
340         /* Enable Clear-on-Read */
341         reg = KEYP_SIH_CTRL_COR | KEYP_SIH_CTRL_PEND_DIS;
342         ret = twl4030_kpwrite_u8(kp, TWL4030_MODULE_KEYPAD,
343                                  reg, KEYP_SIH_CTRL);
344         if (ret < 0)
345                 goto err3;
346
347         /*
348          * This ISR will always execute in kernel thread context because of
349          * the need to access the TWL4030 over the I2C bus.
350          */
351         ret = request_irq(kp->irq, do_kp_irq, 0, pdev->name, kp);
352         if (ret < 0) {
353                 dev_info(kp->dbg_dev, "request_irq failed for irq no=%d\n",
354                         kp->irq);
355                 goto err3;
356         } else {
357                 /* Enable KP and TO interrupts now. */
358                 reg = ~(KEYP_IMR1_KP | KEYP_IMR1_TO);
359                 ret = twl4030_kpwrite_u8(kp, TWL4030_MODULE_KEYPAD,
360                                          reg, KEYP_IMR1);
361                 if (ret < 0)
362                         goto err5;
363         }
364
365         ret = omap_kp_read_kp_matrix_state(kp, kp->kp_state);
366         if (ret < 0)
367                 goto err4;
368
369         return ret;
370 err5:
371         /* mask all events - we don't care about the result */
372         (void) twl4030_kpwrite_u8(kp, TWL4030_MODULE_KEYPAD, 0xff, KEYP_IMR1);
373 err4:
374         free_irq(kp->irq, NULL);
375 err3:
376         input_unregister_device(kp->omap_twl4030kp);
377 err2:
378         input_free_device(kp->omap_twl4030kp);
379
380         return -ENODEV;
381 }
382
383 static int omap_kp_remove(struct platform_device *pdev)
384 {
385         struct omap_keypad *kp = dev_get_drvdata(&pdev->dev);
386
387         free_irq(kp->irq, kp);
388         input_unregister_device(kp->omap_twl4030kp);
389         kfree(kp);
390
391         return 0;
392 }
393
394
395 static struct platform_driver omap_kp_driver = {
396         .probe          = omap_kp_probe,
397         .remove         = __devexit_p(omap_kp_remove),
398         .driver         = {
399                 .name   = "twl4030_keypad",
400                 .owner  = THIS_MODULE,
401         },
402 };
403
404 /*
405  * OMAP TWL4030 Keypad init
406  */
407 static int __devinit omap_kp_init(void)
408 {
409         return platform_driver_register(&omap_kp_driver);
410 }
411
412 static void __exit omap_kp_exit(void)
413 {
414         platform_driver_unregister(&omap_kp_driver);
415 }
416
417 module_init(omap_kp_init);
418 module_exit(omap_kp_exit);
419 MODULE_ALIAS("platform:twl4030_keypad");
420 MODULE_AUTHOR("Texas Instruments");
421 MODULE_DESCRIPTION("OMAP TWL4030 Keypad Driver");
422 MODULE_LICENSE("GPL");