2 * drivers/input/keyboard/omap-twl4030keypad.c
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/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>
43 #include "twl4030-keypad.h"
45 #define PTV_PRESCALER 4
47 #define MAX_ROWS 8 /* TWL4030 hardlimit */
49 /* Global variables */
53 unsigned int keymapsize;
54 u16 kp_state[MAX_ROWS];
59 struct device *dbg_dev;
60 struct input_dev *omap_twl4030kp;
66 static int twl4030_kpread(struct omap_keypad *kp,
67 u32 module, u8 *data, u32 reg, u8 num_bytes)
71 ret = twl4030_i2c_read(module, data, reg, num_bytes);
74 "Couldn't read TWL4030: %X - ret %d[%x]\n",
81 static int twl4030_kpwrite_u8(struct omap_keypad *kp,
82 u32 module, u8 data, u32 reg)
86 ret = twl4030_i2c_write_u8(module, data, reg);
89 "Could not write TWL4030: %X - ret %d[%x]\n",
96 static int omap_kp_find_key(struct omap_keypad *kp, int col, int row)
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);
108 static inline u16 omap_kp_col_xlate(struct omap_keypad *kp, u8 col)
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).
116 return 1 << kp->n_cols;
118 return col & ((1 << kp->n_cols) - 1);
121 static int omap_kp_read_kp_matrix_state(struct omap_keypad *kp, u16 *state)
123 u8 new_state[MAX_ROWS];
125 int ret = twl4030_kpread(kp, TWL4030_MODULE_KEYPAD,
126 new_state, KEYP_FULL_CODE_7_0, kp->n_rows);
128 for (row = 0; row < kp->n_rows; row++)
129 state[row] = omap_kp_col_xlate(kp, new_state[row]);
134 static int omap_kp_is_in_ghost_state(struct omap_keypad *kp, u16 *key_state)
139 for (i = 0; i < kp->n_rows; i++) {
140 u16 col = key_state[i];
142 if ((col & check) && hweight16(col) > 1)
150 static void twl4030_kp_scan(struct omap_keypad *kp, int release_all)
152 u16 new_state[MAX_ROWS];
156 memset(new_state, 0, sizeof(new_state));
158 /* check for any changes */
159 int ret = omap_kp_read_kp_matrix_state(kp, new_state);
160 if (ret < 0) /* panic ... */
163 if (omap_kp_is_in_ghost_state(kp, new_state))
167 mutex_lock(&kp->mutex);
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];
176 for (col = 0; col < kp->n_cols; col++) {
179 if (!(changed & (1 << col)))
182 dev_dbg(kp->dbg_dev, "key [%d:%d] %s\n", row, col,
183 (new_state[row] & (1 << col)) ?
184 "press" : "release");
186 key = omap_kp_find_key(kp, col, row);
188 dev_warn(kp->dbg_dev,
189 "Spurious key event %d-%d\n",
191 else if (key & KEY_PERSISTENT)
194 input_report_key(kp->omap_twl4030kp, key,
195 new_state[row] & (1 << col));
197 kp->kp_state[row] = new_state[row];
200 mutex_unlock(&kp->mutex);
204 * Keypad interrupt handler
206 static irqreturn_t do_kp_irq(int irq, void *_kp)
208 struct omap_keypad *kp = _kp;
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...
220 /* Read & Clear TWL4030 pending interrupt */
221 ret = twl4030_kpread(kp, TWL4030_MODULE_KEYPAD, ®, KEYP_ISR1, 1);
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);
228 twl4030_kp_scan(kp, 1);
234 * Registers keypad device with input sub system
235 * and configures TWL4030 keypad registers
237 static int __init omap_kp_probe(struct platform_device *pdev)
242 struct omap_keypad *kp;
243 struct twl4030_keypad_data *pdata = pdev->dev.platform_data;
245 kp = kzalloc(sizeof(*kp), GFP_KERNEL);
249 if (!pdata->rows || !pdata->cols || !pdata->keymap) {
250 dev_err(&pdev->dev, "No rows, cols or keymap from pdata\n");
255 dev_set_drvdata(&pdev->dev, kp);
257 /* Get the debug Device */
258 kp->dbg_dev = &pdev->dev;
260 kp->omap_twl4030kp = input_allocate_device();
261 if (!kp->omap_twl4030kp) {
266 mutex_init(&kp->mutex);
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;
274 /* setup input device */
275 set_bit(EV_KEY, kp->omap_twl4030kp->evbit);
277 /* Enable auto repeat feature of Linux input subsystem */
279 set_bit(EV_REP, kp->omap_twl4030kp->evbit);
281 for (i = 0; i < kp->keymapsize; i++)
282 set_bit(kp->keymap[i] & KEYNUM_MASK,
283 kp->omap_twl4030kp->keybit);
285 kp->omap_twl4030kp->name = "omap_twl4030keypad";
286 kp->omap_twl4030kp->phys = "omap_twl4030keypad/input0";
287 kp->omap_twl4030kp->dev.parent = &pdev->dev;
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;
294 kp->omap_twl4030kp->keycode = kp->keymap;
295 kp->omap_twl4030kp->keycodesize = sizeof(unsigned int);
296 kp->omap_twl4030kp->keycodemax = kp->keymapsize;
298 ret = input_register_device(kp->omap_twl4030kp);
301 "Unable to register twl4030 keypad device\n");
305 /* Disable auto-repeat */
306 reg = KEYP_CTRL_NOAUTORPT;
307 ret = twl4030_kpwrite_u8(kp, TWL4030_MODULE_KEYPAD, reg, KEYP_CTRL);
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);
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);
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);
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);
336 ret = twl4030_kpwrite_u8(kp, TWL4030_MODULE_KEYPAD,
337 (i >> 8), KEYP_TIMEOUT_H);
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,
349 * This ISR will always execute in kernel thread context because of
350 * the need to access the TWL4030 over the I2C bus.
352 ret = request_irq(kp->irq, do_kp_irq, 0, pdev->name, kp);
354 dev_info(kp->dbg_dev, "request_irq failed for irq no=%d\n",
358 /* Enable KP and TO interrupts now. */
359 reg = ~(KEYP_IMR1_KP | KEYP_IMR1_TO);
360 ret = twl4030_kpwrite_u8(kp, TWL4030_MODULE_KEYPAD,
366 ret = omap_kp_read_kp_matrix_state(kp, kp->kp_state);
372 /* mask all events - we don't care about the result */
373 (void) twl4030_kpwrite_u8(kp, TWL4030_MODULE_KEYPAD, 0xff, KEYP_IMR1);
375 free_irq(kp->irq, NULL);
377 input_unregister_device(kp->omap_twl4030kp);
379 input_free_device(kp->omap_twl4030kp);
384 static int omap_kp_remove(struct platform_device *pdev)
386 struct omap_keypad *kp = dev_get_drvdata(&pdev->dev);
388 free_irq(kp->irq, kp);
389 input_unregister_device(kp->omap_twl4030kp);
396 static struct platform_driver omap_kp_driver = {
397 .probe = omap_kp_probe,
398 .remove = __devexit_p(omap_kp_remove),
400 .name = "twl4030_keypad",
401 .owner = THIS_MODULE,
406 * OMAP TWL4030 Keypad init
408 static int __devinit omap_kp_init(void)
410 return platform_driver_register(&omap_kp_driver);
413 static void __exit omap_kp_exit(void)
415 platform_driver_unregister(&omap_kp_driver);
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");