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