]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/input/keyboard/twl4030_keypad.c
Merge branch 'omap-fixes'
[linux-2.6-omap-h63xx.git] / drivers / input / keyboard / twl4030_keypad.c
1 /*
2  * twl4030_keypad.c - driver for 8x8 keypad controller in twl4030 chips
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/kernel.h>
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/interrupt.h>
32 #include <linux/input.h>
33 #include <linux/platform_device.h>
34 #include <linux/i2c/twl4030.h>
35
36
37 /*
38  * The TWL4030 family chips include a keypad controller that supports
39  * up to an 8x8 switch matrix.  The controller can issue system wakeup
40  * events, since it uses only the always-on 32KiHz oscillator, and has
41  * an internal state machine that decodes pressed keys, including
42  * multi-key combinations.
43  *
44  * This driver lets boards define what keycodes they wish to report for
45  * which scancodes, as part of the "struct twl4030_keypad_data" used in
46  * the probe() routine.
47  *
48  * See the TPS65950 documentation; that's the general availability
49  * version of the TWL5030 second generation part.
50  */
51 #define MAX_ROWS                8       /* TWL4030 hard limit */
52
53 struct twl4030_keypad {
54         unsigned        *keymap;
55         unsigned int    keymapsize;
56         u16             kp_state[MAX_ROWS];
57         unsigned        n_rows;
58         unsigned        n_cols;
59         unsigned        irq;
60
61         struct device   *dbg_dev;
62         struct input_dev *input;
63 };
64
65 #define ROWCOL_MASK     KEY(0xf, 0xf, 0)
66 #define KEYNUM_MASK     ~PERSISTENT_KEY(0xf, 0xf)
67
68 /*----------------------------------------------------------------------*/
69
70 /* arbitrary prescaler value 0..7 */
71 #define PTV_PRESCALER                   4
72
73 /* Register Offsets */
74 #define KEYP_CTRL                       0x00
75 #define KEYP_DEB                        0x01
76 #define KEYP_LONG_KEY                   0x02
77 #define KEYP_LK_PTV                     0x03
78 #define KEYP_TIMEOUT_L                  0x04
79 #define KEYP_TIMEOUT_H                  0x05
80 #define KEYP_KBC                        0x06
81 #define KEYP_KBR                        0x07
82 #define KEYP_SMS                        0x08
83 #define KEYP_FULL_CODE_7_0              0x09    /* row 0 column status */
84 #define KEYP_FULL_CODE_15_8             0x0a    /* ... row 1 ... */
85 #define KEYP_FULL_CODE_23_16            0x0b
86 #define KEYP_FULL_CODE_31_24            0x0c
87 #define KEYP_FULL_CODE_39_32            0x0d
88 #define KEYP_FULL_CODE_47_40            0x0e
89 #define KEYP_FULL_CODE_55_48            0x0f
90 #define KEYP_FULL_CODE_63_56            0x10
91 #define KEYP_ISR1                       0x11
92 #define KEYP_IMR1                       0x12
93 #define KEYP_ISR2                       0x13
94 #define KEYP_IMR2                       0x14
95 #define KEYP_SIR                        0x15
96 #define KEYP_EDR                        0x16    /* edge triggers */
97 #define KEYP_SIH_CTRL                   0x17
98
99 /* KEYP_CTRL_REG Fields */
100 #define KEYP_CTRL_SOFT_NRST             BIT(0)
101 #define KEYP_CTRL_SOFTMODEN             BIT(1)
102 #define KEYP_CTRL_LK_EN                 BIT(2)
103 #define KEYP_CTRL_TOE_EN                BIT(3)
104 #define KEYP_CTRL_TOLE_EN               BIT(4)
105 #define KEYP_CTRL_RP_EN                 BIT(5)
106 #define KEYP_CTRL_KBD_ON                BIT(6)
107
108 /* KEYP_DEB, KEYP_LONG_KEY, KEYP_TIMEOUT_x*/
109 #define KEYP_PERIOD_US(t, prescale)     ((t) / (31 << (prescale + 1)) - 1)
110
111 /* KEYP_LK_PTV_REG Fields */
112 #define KEYP_LK_PTV_PTV_SHIFT           5
113
114 /* KEYP_{IMR,ISR,SIR} Fields */
115 #define KEYP_IMR1_MIS                   BIT(3)
116 #define KEYP_IMR1_TO                    BIT(2)
117 #define KEYP_IMR1_LK                    BIT(1)
118 #define KEYP_IMR1_KP                    BIT(0)
119
120 /* KEYP_EDR Fields */
121 #define KEYP_EDR_KP_FALLING             0x01
122 #define KEYP_EDR_KP_RISING              0x02
123 #define KEYP_EDR_KP_BOTH                0x03
124 #define KEYP_EDR_LK_FALLING             0x04
125 #define KEYP_EDR_LK_RISING              0x08
126 #define KEYP_EDR_TO_FALLING             0x10
127 #define KEYP_EDR_TO_RISING              0x20
128 #define KEYP_EDR_MIS_FALLING            0x40
129 #define KEYP_EDR_MIS_RISING             0x80
130
131
132 /*----------------------------------------------------------------------*/
133
134 static int twl4030_kpread(struct twl4030_keypad *kp,
135                 u8 *data, u32 reg, u8 num_bytes)
136 {
137         int ret;
138
139         ret = twl4030_i2c_read(TWL4030_MODULE_KEYPAD, data, reg, num_bytes);
140         if (ret < 0) {
141                 dev_warn(kp->dbg_dev,
142                         "Couldn't read TWL4030: %X - ret %d[%x]\n",
143                          reg, ret, ret);
144                 return ret;
145         }
146         return ret;
147 }
148
149 static int twl4030_kpwrite_u8(struct twl4030_keypad *kp, u8 data, u32 reg)
150 {
151         int ret;
152
153         ret = twl4030_i2c_write_u8(TWL4030_MODULE_KEYPAD, data, reg);
154         if (ret < 0) {
155                 dev_warn(kp->dbg_dev,
156                         "Could not write TWL4030: %X - ret %d[%x]\n",
157                          reg, ret, ret);
158                 return ret;
159         }
160         return ret;
161 }
162
163 static int twl4030_find_key(struct twl4030_keypad *kp, int col, int row)
164 {
165         int i, rc;
166
167         rc = KEY(col, row, 0);
168         for (i = 0; i < kp->keymapsize; i++)
169                 if ((kp->keymap[i] & ROWCOL_MASK) == rc)
170                         return kp->keymap[i] & (KEYNUM_MASK | KEY_PERSISTENT);
171
172         return -EINVAL;
173 }
174
175 static inline u16 twl4030_col_xlate(struct twl4030_keypad *kp, u8 col)
176 {
177         /* If all bits in a row are active for all coloumns then
178          * we have that row line connected to gnd. Mark this
179          * key on as if it was on matrix position n_cols (ie
180          * one higher than the size of the matrix).
181          */
182         if (col == 0xFF)
183                 return 1 << kp->n_cols;
184         else
185                 return col & ((1 << kp->n_cols) - 1);
186 }
187
188 static int twl4030_read_kp_matrix_state(struct twl4030_keypad *kp, u16 *state)
189 {
190         u8 new_state[MAX_ROWS];
191         int row;
192         int ret = twl4030_kpread(kp,
193                                  new_state, KEYP_FULL_CODE_7_0, kp->n_rows);
194         if (ret >= 0) {
195                 for (row = 0; row < kp->n_rows; row++)
196                         state[row] = twl4030_col_xlate(kp, new_state[row]);
197         }
198         return ret;
199 }
200
201 static int twl4030_is_in_ghost_state(struct twl4030_keypad *kp, u16 *key_state)
202 {
203         int i;
204         u16 check = 0;
205
206         for (i = 0; i < kp->n_rows; i++) {
207                 u16 col = key_state[i];
208
209                 if ((col & check) && hweight16(col) > 1)
210                         return 1;
211                 check |= col;
212         }
213
214         return 0;
215 }
216
217 static void twl4030_kp_scan(struct twl4030_keypad *kp, int release_all)
218 {
219         u16 new_state[MAX_ROWS];
220         int col, row;
221
222         if (release_all)
223                 memset(new_state, 0, sizeof(new_state));
224         else {
225                 /* check for any changes */
226                 int ret = twl4030_read_kp_matrix_state(kp, new_state);
227
228                 if (ret < 0)    /* panic ... */
229                         return;
230                 if (twl4030_is_in_ghost_state(kp, new_state))
231                         return;
232         }
233
234         /* check for changes and print those */
235         for (row = 0; row < kp->n_rows; row++) {
236                 int changed = new_state[row] ^ kp->kp_state[row];
237
238                 if (!changed)
239                         continue;
240
241                 for (col = 0; col < kp->n_cols; col++) {
242                         int key;
243
244                         if (!(changed & (1 << col)))
245                                 continue;
246
247                         dev_dbg(kp->dbg_dev, "key [%d:%d] %s\n", row, col,
248                                 (new_state[row] & (1 << col)) ?
249                                 "press" : "release");
250
251                         key = twl4030_find_key(kp, col, row);
252                         if (key < 0)
253                                 dev_warn(kp->dbg_dev,
254                                         "Spurious key event %d-%d\n",
255                                          col, row);
256                         else if (key & KEY_PERSISTENT)
257                                 continue;
258                         else
259                                 input_report_key(kp->input, key,
260                                                  new_state[row] & (1 << col));
261                 }
262                 kp->kp_state[row] = new_state[row];
263         }
264         input_sync(kp->input);
265 }
266
267 /*
268  * Keypad interrupt handler
269  */
270 static irqreturn_t do_kp_irq(int irq, void *_kp)
271 {
272         struct twl4030_keypad *kp = _kp;
273         u8 reg;
274         int ret;
275
276 #ifdef CONFIG_LOCKDEP
277         /* WORKAROUND for lockdep forcing IRQF_DISABLED on us, which
278          * we don't want and can't tolerate.  Although it might be
279          * friendlier not to borrow this thread context...
280          */
281         local_irq_enable();
282 #endif
283
284         /* Read & Clear TWL4030 pending interrupt */
285         ret = twl4030_kpread(kp, &reg, KEYP_ISR1, 1);
286
287         /* Release all keys if I2C has gone bad or
288          * the KEYP has gone to idle state */
289         if ((ret >= 0) && (reg & KEYP_IMR1_KP))
290                 twl4030_kp_scan(kp, 0);
291         else
292                 twl4030_kp_scan(kp, 1);
293
294         return IRQ_HANDLED;
295 }
296
297 /*
298  * Registers keypad device with input subsystem
299  * and configures TWL4030 keypad registers
300  */
301 static int __devinit twl4030_kp_probe(struct platform_device *pdev)
302 {
303         u8 reg;
304         int i;
305         int ret = 0;
306         struct twl4030_keypad *kp;
307         struct twl4030_keypad_data *pdata = pdev->dev.platform_data;
308
309         if (!pdata || !pdata->rows || !pdata->cols || !pdata->keymap
310                         || pdata->rows > 8 || pdata->cols > 8) {
311                 dev_err(&pdev->dev, "Invalid platform_data\n");
312                 return -EINVAL;
313         }
314
315         kp = kzalloc(sizeof(*kp), GFP_KERNEL);
316         if (!kp)
317                 return -ENOMEM;
318
319         platform_set_drvdata(pdev, kp);
320
321         /* Get the debug Device */
322         kp->dbg_dev = &pdev->dev;
323
324         kp->input = input_allocate_device();
325         if (!kp->input) {
326                 kfree(kp);
327                 return -ENOMEM;
328         }
329
330         kp->keymap = pdata->keymap;
331         kp->keymapsize = pdata->keymapsize;
332         kp->n_rows = pdata->rows;
333         kp->n_cols = pdata->cols;
334         kp->irq = platform_get_irq(pdev, 0);
335
336         /* setup input device */
337         __set_bit(EV_KEY, kp->input->evbit);
338
339         /* Enable auto repeat feature of Linux input subsystem */
340         if (pdata->rep)
341                 __set_bit(EV_REP, kp->input->evbit);
342
343         for (i = 0; i < kp->keymapsize; i++)
344                 __set_bit(kp->keymap[i] & KEYNUM_MASK,
345                                 kp->input->keybit);
346
347         kp->input->name         = "TWL4030 Keypad";
348         kp->input->phys         = "twl4030_keypad/input0";
349         kp->input->dev.parent   = &pdev->dev;
350
351         kp->input->id.bustype   = BUS_HOST;
352         kp->input->id.vendor    = 0x0001;
353         kp->input->id.product   = 0x0001;
354         kp->input->id.version   = 0x0003;
355
356         kp->input->keycode      = kp->keymap;
357         kp->input->keycodesize  = sizeof(unsigned int);
358         kp->input->keycodemax   = kp->keymapsize;
359
360         ret = input_register_device(kp->input);
361         if (ret < 0) {
362                 dev_err(kp->dbg_dev,
363                         "Unable to register twl4030 keypad device\n");
364                 goto err2;
365         }
366
367         /* Enable controller, with hardware decoding but not autorepeat */
368         reg = KEYP_CTRL_SOFT_NRST | KEYP_CTRL_SOFTMODEN
369                 | KEYP_CTRL_TOE_EN | KEYP_CTRL_KBD_ON;
370         ret = twl4030_kpwrite_u8(kp, reg, KEYP_CTRL);
371         if (ret < 0)
372                 goto err3;
373
374         /* NOTE:  we could use sih_setup() here to package keypad
375          * event sources as four different IRQs ... but we don't.
376          */
377
378         /* Enable TO rising and KP rising and falling edge detection */
379         reg = KEYP_EDR_KP_BOTH | KEYP_EDR_TO_RISING;
380         ret = twl4030_kpwrite_u8(kp, reg, KEYP_EDR);
381         if (ret < 0)
382                 goto err3;
383
384         /* Set PTV prescaler Field */
385         reg = (PTV_PRESCALER << KEYP_LK_PTV_PTV_SHIFT);
386         ret = twl4030_kpwrite_u8(kp, reg, KEYP_LK_PTV);
387         if (ret < 0)
388                 goto err3;
389
390         /* Set key debounce time to 20 ms */
391         i = KEYP_PERIOD_US(20000, PTV_PRESCALER);
392         ret = twl4030_kpwrite_u8(kp, i, KEYP_DEB);
393         if (ret < 0)
394                 goto err3;
395
396         /* Set timeout period to 100 ms */
397         i = KEYP_PERIOD_US(200000, PTV_PRESCALER);
398         ret = twl4030_kpwrite_u8(kp, (i & 0xFF), KEYP_TIMEOUT_L);
399         if (ret < 0)
400                 goto err3;
401         ret = twl4030_kpwrite_u8(kp, (i >> 8), KEYP_TIMEOUT_H);
402         if (ret < 0)
403                 goto err3;
404
405         /* Enable Clear-on-Read; disable remembering events that fire
406          * after the IRQ but before our handler acks (reads) them,
407          */
408         reg = TWL4030_SIH_CTRL_COR_MASK | TWL4030_SIH_CTRL_PENDDIS_MASK;
409         ret = twl4030_kpwrite_u8(kp, reg, KEYP_SIH_CTRL);
410         if (ret < 0)
411                 goto err3;
412
413         /* initialize key state; irqs update it from here on */
414         ret = twl4030_read_kp_matrix_state(kp, kp->kp_state);
415         if (ret < 0)
416                 goto err3;
417
418         /*
419          * This ISR will always execute in kernel thread context because of
420          * the need to access the TWL4030 over the I2C bus.
421          *
422          * NOTE:  we assume this host is wired to TWL4040 INT1, not INT2 ...
423          */
424         ret = request_irq(kp->irq, do_kp_irq, 0, pdev->name, kp);
425         if (ret < 0) {
426                 dev_info(kp->dbg_dev, "request_irq failed for irq no=%d\n",
427                         kp->irq);
428                 goto err3;
429         } else {
430                 /* Enable KP and TO interrupts now. */
431                 reg = (u8) ~(KEYP_IMR1_KP | KEYP_IMR1_TO);
432                 ret = twl4030_kpwrite_u8(kp, reg, KEYP_IMR1);
433                 if (ret < 0)
434                         goto err5;
435         }
436
437         return ret;
438 err5:
439         /* mask all events - we don't care about the result */
440         (void) twl4030_kpwrite_u8(kp, 0xff, KEYP_IMR1);
441         free_irq(kp->irq, NULL);
442 err3:
443         input_unregister_device(kp->input);
444         kp->input = NULL;
445 err2:
446         input_free_device(kp->input);
447         kfree(kp);
448         return -ENODEV;
449 }
450
451 static int __devexit twl4030_kp_remove(struct platform_device *pdev)
452 {
453         struct twl4030_keypad *kp = platform_get_drvdata(pdev);
454
455         free_irq(kp->irq, kp);
456         input_unregister_device(kp->input);
457         kfree(kp);
458
459         return 0;
460 }
461
462 /*
463  * NOTE: twl4030 are multi-function devices connected via I2C.
464  * So this device is a child of an I2C parent, thus it needs to
465  * support unplug/replug (which most platform devices don't).
466  */
467
468 MODULE_ALIAS("platform:twl4030_keypad");
469
470 static struct platform_driver twl4030_kp_driver = {
471         .probe          = twl4030_kp_probe,
472         .remove         = __devexit_p(twl4030_kp_remove),
473         .driver         = {
474                 .name   = "twl4030_keypad",
475                 .owner  = THIS_MODULE,
476         },
477 };
478
479 static int __init twl4030_kp_init(void)
480 {
481         return platform_driver_register(&twl4030_kp_driver);
482 }
483 module_init(twl4030_kp_init);
484
485 static void __exit twl4030_kp_exit(void)
486 {
487         platform_driver_unregister(&twl4030_kp_driver);
488 }
489 module_exit(twl4030_kp_exit);
490
491 MODULE_AUTHOR("Texas Instruments");
492 MODULE_DESCRIPTION("TWL4030 Keypad Driver");
493 MODULE_LICENSE("GPL");