2 * twl4030_keypad.c - driver for 8x8 keypad controller in twl4030 chips
4 * Copyright (C) 2007 Texas Instruments, Inc.
5 * Copyright (C) 2008 Nokia Corporation
7 * Code re-written for 2430SDP by:
8 * Syed Mohammed Khasim <x0khasim@ti.com>
11 * Manjunatha G K <manjugk@ti.com>
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.
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.
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
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>
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.
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.
48 * See the TPS65950 documentation; that's the general availability
49 * version of the TWL5030 second generation part.
51 #define TWL4030_MAX_ROWS 8 /* TWL4030 hard limit */
52 #define TWL4030_MAX_COLS 8
53 #define TWL4030_ROW_SHIFT 3
54 #define TWL4030_KEYMAP_SIZE (TWL4030_MAX_ROWS * TWL4030_MAX_COLS)
56 struct twl4030_keypad
{
57 unsigned short keymap
[TWL4030_KEYMAP_SIZE
];
58 u16 kp_state
[TWL4030_MAX_ROWS
];
63 struct device
*dbg_dev
;
64 struct input_dev
*input
;
67 /*----------------------------------------------------------------------*/
69 /* arbitrary prescaler value 0..7 */
70 #define PTV_PRESCALER 4
72 /* Register Offsets */
73 #define KEYP_CTRL 0x00
75 #define KEYP_LONG_KEY 0x02
76 #define KEYP_LK_PTV 0x03
77 #define KEYP_TIMEOUT_L 0x04
78 #define KEYP_TIMEOUT_H 0x05
82 #define KEYP_FULL_CODE_7_0 0x09 /* row 0 column status */
83 #define KEYP_FULL_CODE_15_8 0x0a /* ... row 1 ... */
84 #define KEYP_FULL_CODE_23_16 0x0b
85 #define KEYP_FULL_CODE_31_24 0x0c
86 #define KEYP_FULL_CODE_39_32 0x0d
87 #define KEYP_FULL_CODE_47_40 0x0e
88 #define KEYP_FULL_CODE_55_48 0x0f
89 #define KEYP_FULL_CODE_63_56 0x10
90 #define KEYP_ISR1 0x11
91 #define KEYP_IMR1 0x12
92 #define KEYP_ISR2 0x13
93 #define KEYP_IMR2 0x14
95 #define KEYP_EDR 0x16 /* edge triggers */
96 #define KEYP_SIH_CTRL 0x17
98 /* KEYP_CTRL_REG Fields */
99 #define KEYP_CTRL_SOFT_NRST BIT(0)
100 #define KEYP_CTRL_SOFTMODEN BIT(1)
101 #define KEYP_CTRL_LK_EN BIT(2)
102 #define KEYP_CTRL_TOE_EN BIT(3)
103 #define KEYP_CTRL_TOLE_EN BIT(4)
104 #define KEYP_CTRL_RP_EN BIT(5)
105 #define KEYP_CTRL_KBD_ON BIT(6)
107 /* KEYP_DEB, KEYP_LONG_KEY, KEYP_TIMEOUT_x*/
108 #define KEYP_PERIOD_US(t, prescale) ((t) / (31 << (prescale + 1)) - 1)
110 /* KEYP_LK_PTV_REG Fields */
111 #define KEYP_LK_PTV_PTV_SHIFT 5
113 /* KEYP_{IMR,ISR,SIR} Fields */
114 #define KEYP_IMR1_MIS BIT(3)
115 #define KEYP_IMR1_TO BIT(2)
116 #define KEYP_IMR1_LK BIT(1)
117 #define KEYP_IMR1_KP BIT(0)
119 /* KEYP_EDR Fields */
120 #define KEYP_EDR_KP_FALLING 0x01
121 #define KEYP_EDR_KP_RISING 0x02
122 #define KEYP_EDR_KP_BOTH 0x03
123 #define KEYP_EDR_LK_FALLING 0x04
124 #define KEYP_EDR_LK_RISING 0x08
125 #define KEYP_EDR_TO_FALLING 0x10
126 #define KEYP_EDR_TO_RISING 0x20
127 #define KEYP_EDR_MIS_FALLING 0x40
128 #define KEYP_EDR_MIS_RISING 0x80
131 /*----------------------------------------------------------------------*/
133 static int twl4030_kpread(struct twl4030_keypad
*kp
,
134 u8
*data
, u32 reg
, u8 num_bytes
)
136 int ret
= twl4030_i2c_read(TWL4030_MODULE_KEYPAD
, data
, reg
, num_bytes
);
139 dev_warn(kp
->dbg_dev
,
140 "Couldn't read TWL4030: %X - ret %d[%x]\n",
146 static int twl4030_kpwrite_u8(struct twl4030_keypad
*kp
, u8 data
, u32 reg
)
148 int ret
= twl4030_i2c_write_u8(TWL4030_MODULE_KEYPAD
, data
, reg
);
151 dev_warn(kp
->dbg_dev
,
152 "Could not write TWL4030: %X - ret %d[%x]\n",
158 static inline u16
twl4030_col_xlate(struct twl4030_keypad
*kp
, u8 col
)
160 /* If all bits in a row are active for all coloumns then
161 * we have that row line connected to gnd. Mark this
162 * key on as if it was on matrix position n_cols (ie
163 * one higher than the size of the matrix).
166 return 1 << kp
->n_cols
;
168 return col
& ((1 << kp
->n_cols
) - 1);
171 static int twl4030_read_kp_matrix_state(struct twl4030_keypad
*kp
, u16
*state
)
173 u8 new_state
[TWL4030_MAX_ROWS
];
175 int ret
= twl4030_kpread(kp
, new_state
,
176 KEYP_FULL_CODE_7_0
, kp
->n_rows
);
178 for (row
= 0; row
< kp
->n_rows
; row
++)
179 state
[row
] = twl4030_col_xlate(kp
, new_state
[row
]);
184 static int twl4030_is_in_ghost_state(struct twl4030_keypad
*kp
, u16
*key_state
)
189 for (i
= 0; i
< kp
->n_rows
; i
++) {
190 u16 col
= key_state
[i
];
192 if ((col
& check
) && hweight16(col
) > 1)
201 static void twl4030_kp_scan(struct twl4030_keypad
*kp
, bool release_all
)
203 struct input_dev
*input
= kp
->input
;
204 u16 new_state
[TWL4030_MAX_ROWS
];
208 memset(new_state
, 0, sizeof(new_state
));
210 /* check for any changes */
211 int ret
= twl4030_read_kp_matrix_state(kp
, new_state
);
213 if (ret
< 0) /* panic ... */
216 if (twl4030_is_in_ghost_state(kp
, new_state
))
220 /* check for changes and print those */
221 for (row
= 0; row
< kp
->n_rows
; row
++) {
222 int changed
= new_state
[row
] ^ kp
->kp_state
[row
];
227 for (col
= 0; col
< kp
->n_cols
; col
++) {
230 if (!(changed
& (1 << col
)))
233 dev_dbg(kp
->dbg_dev
, "key [%d:%d] %s\n", row
, col
,
234 (new_state
[row
] & (1 << col
)) ?
235 "press" : "release");
237 code
= MATRIX_SCAN_CODE(row
, col
, TWL4030_ROW_SHIFT
);
238 input_event(input
, EV_MSC
, MSC_SCAN
, code
);
239 input_report_key(input
, kp
->keymap
[code
],
240 new_state
[row
] & (1 << col
));
242 kp
->kp_state
[row
] = new_state
[row
];
248 * Keypad interrupt handler
250 static irqreturn_t
do_kp_irq(int irq
, void *_kp
)
252 struct twl4030_keypad
*kp
= _kp
;
256 #ifdef CONFIG_LOCKDEP
257 /* WORKAROUND for lockdep forcing IRQF_DISABLED on us, which
258 * we don't want and can't tolerate. Although it might be
259 * friendlier not to borrow this thread context...
264 /* Read & Clear TWL4030 pending interrupt */
265 ret
= twl4030_kpread(kp
, ®
, KEYP_ISR1
, 1);
267 /* Release all keys if I2C has gone bad or
268 * the KEYP has gone to idle state */
269 if (ret
>= 0 && (reg
& KEYP_IMR1_KP
))
270 twl4030_kp_scan(kp
, false);
272 twl4030_kp_scan(kp
, true);
277 static int __devinit
twl4030_kp_program(struct twl4030_keypad
*kp
)
282 /* Enable controller, with hardware decoding but not autorepeat */
283 reg
= KEYP_CTRL_SOFT_NRST
| KEYP_CTRL_SOFTMODEN
284 | KEYP_CTRL_TOE_EN
| KEYP_CTRL_KBD_ON
;
285 if (twl4030_kpwrite_u8(kp
, reg
, KEYP_CTRL
) < 0)
288 /* NOTE: we could use sih_setup() here to package keypad
289 * event sources as four different IRQs ... but we don't.
292 /* Enable TO rising and KP rising and falling edge detection */
293 reg
= KEYP_EDR_KP_BOTH
| KEYP_EDR_TO_RISING
;
294 if (twl4030_kpwrite_u8(kp
, reg
, KEYP_EDR
) < 0)
297 /* Set PTV prescaler Field */
298 reg
= (PTV_PRESCALER
<< KEYP_LK_PTV_PTV_SHIFT
);
299 if (twl4030_kpwrite_u8(kp
, reg
, KEYP_LK_PTV
) < 0)
302 /* Set key debounce time to 20 ms */
303 i
= KEYP_PERIOD_US(20000, PTV_PRESCALER
);
304 if (twl4030_kpwrite_u8(kp
, i
, KEYP_DEB
) < 0)
307 /* Set timeout period to 100 ms */
308 i
= KEYP_PERIOD_US(200000, PTV_PRESCALER
);
309 if (twl4030_kpwrite_u8(kp
, (i
& 0xFF), KEYP_TIMEOUT_L
) < 0)
312 if (twl4030_kpwrite_u8(kp
, (i
>> 8), KEYP_TIMEOUT_H
) < 0)
316 * Enable Clear-on-Read; disable remembering events that fire
317 * after the IRQ but before our handler acks (reads) them,
319 reg
= TWL4030_SIH_CTRL_COR_MASK
| TWL4030_SIH_CTRL_PENDDIS_MASK
;
320 if (twl4030_kpwrite_u8(kp
, reg
, KEYP_SIH_CTRL
) < 0)
323 /* initialize key state; irqs update it from here on */
324 if (twl4030_read_kp_matrix_state(kp
, kp
->kp_state
) < 0)
331 * Registers keypad device with input subsystem
332 * and configures TWL4030 keypad registers
334 static int __devinit
twl4030_kp_probe(struct platform_device
*pdev
)
336 struct twl4030_keypad_data
*pdata
= pdev
->dev
.platform_data
;
337 const struct matrix_keymap_data
*keymap_data
= pdata
->keymap_data
;
338 struct twl4030_keypad
*kp
;
339 struct input_dev
*input
;
343 if (!pdata
|| !pdata
->rows
|| !pdata
->cols
||
344 pdata
->rows
> TWL4030_MAX_ROWS
|| pdata
->cols
> TWL4030_MAX_COLS
) {
345 dev_err(&pdev
->dev
, "Invalid platform_data\n");
349 kp
= kzalloc(sizeof(*kp
), GFP_KERNEL
);
350 input
= input_allocate_device();
356 /* Get the debug Device */
357 kp
->dbg_dev
= &pdev
->dev
;
360 kp
->n_rows
= pdata
->rows
;
361 kp
->n_cols
= pdata
->cols
;
362 kp
->irq
= platform_get_irq(pdev
, 0);
364 /* setup input device */
365 __set_bit(EV_KEY
, input
->evbit
);
367 /* Enable auto repeat feature of Linux input subsystem */
369 __set_bit(EV_REP
, input
->evbit
);
371 input_set_capability(input
, EV_MSC
, MSC_SCAN
);
373 input
->name
= "TWL4030 Keypad";
374 input
->phys
= "twl4030_keypad/input0";
375 input
->dev
.parent
= &pdev
->dev
;
377 input
->id
.bustype
= BUS_HOST
;
378 input
->id
.vendor
= 0x0001;
379 input
->id
.product
= 0x0001;
380 input
->id
.version
= 0x0003;
382 input
->keycode
= kp
->keymap
;
383 input
->keycodesize
= sizeof(kp
->keymap
[0]);
384 input
->keycodemax
= ARRAY_SIZE(kp
->keymap
);
386 matrix_keypad_build_keymap(keymap_data
, TWL4030_ROW_SHIFT
,
387 input
->keycode
, input
->keybit
);
389 error
= input_register_device(input
);
392 "Unable to register twl4030 keypad device\n");
396 error
= twl4030_kp_program(kp
);
401 * This ISR will always execute in kernel thread context because of
402 * the need to access the TWL4030 over the I2C bus.
404 * NOTE: we assume this host is wired to TWL4040 INT1, not INT2 ...
406 error
= request_irq(kp
->irq
, do_kp_irq
, 0, pdev
->name
, kp
);
408 dev_info(kp
->dbg_dev
, "request_irq failed for irq no=%d\n",
413 /* Enable KP and TO interrupts now. */
414 reg
= (u8
) ~(KEYP_IMR1_KP
| KEYP_IMR1_TO
);
415 if (twl4030_kpwrite_u8(kp
, reg
, KEYP_IMR1
)) {
420 platform_set_drvdata(pdev
, kp
);
424 /* mask all events - we don't care about the result */
425 (void) twl4030_kpwrite_u8(kp
, 0xff, KEYP_IMR1
);
427 free_irq(kp
->irq
, NULL
);
429 input_unregister_device(input
);
432 input_free_device(input
);
437 static int __devexit
twl4030_kp_remove(struct platform_device
*pdev
)
439 struct twl4030_keypad
*kp
= platform_get_drvdata(pdev
);
441 free_irq(kp
->irq
, kp
);
442 input_unregister_device(kp
->input
);
443 platform_set_drvdata(pdev
, NULL
);
450 static int twl4030_kp_suspend(struct platform_device
*pdev
, pm_message_t state
)
452 struct twl4030_keypad_data
*pdata
= pdev
->dev
.platform_data
;
454 if (pdata
->on_suspend
)
455 pdata
->on_suspend(pdata
->pm_state
);
460 static int twl4030_kp_resume(struct platform_device
*pdev
)
462 struct twl4030_keypad_data
*pdata
= pdev
->dev
.platform_data
;
464 if (pdata
->on_resume
)
465 pdata
->on_resume(pdata
->pm_state
);
470 #define twl4030_kp_suspend NULL
471 #define twl4030_kp_resume NULL
476 * NOTE: twl4030 are multi-function devices connected via I2C.
477 * So this device is a child of an I2C parent, thus it needs to
478 * support unplug/replug (which most platform devices don't).
481 static struct platform_driver twl4030_kp_driver
= {
482 .probe
= twl4030_kp_probe
,
483 .remove
= __devexit_p(twl4030_kp_remove
),
484 .suspend
= twl4030_kp_suspend
,
485 .resume
= twl4030_kp_resume
,
487 .name
= "twl4030_keypad",
488 .owner
= THIS_MODULE
,
492 static int __init
twl4030_kp_init(void)
494 return platform_driver_register(&twl4030_kp_driver
);
496 module_init(twl4030_kp_init
);
498 static void __exit
twl4030_kp_exit(void)
500 platform_driver_unregister(&twl4030_kp_driver
);
502 module_exit(twl4030_kp_exit
);
504 MODULE_AUTHOR("Texas Instruments");
505 MODULE_DESCRIPTION("TWL4030 Keypad Driver");
506 MODULE_LICENSE("GPL");
507 MODULE_ALIAS("platform:twl4030_keypad");