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