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>
42 #include "twl4030-keypad.h"
44 #define PTV_PRESCALER 4
46 #define MAX_ROWS 8 /* TWL4030 hardlimit */
47 #define ROWCOL_MASK 0xFF000000
48 #define KEYNUM_MASK 0x00FFFFFF
50 /* Global variables */
54 unsigned int keymapsize;
55 u16 kp_state[MAX_ROWS];
60 struct device *dbg_dev;
61 struct input_dev *omap_twl4030kp;
67 static int twl4030_kpread(struct omap_keypad *kp,
68 u32 module, u8 *data, u32 reg, u8 num_bytes)
72 ret = twl4030_i2c_read(module, data, reg, num_bytes);
75 "Couldn't read TWL4030: %X - ret %d[%x]\n",
82 static int twl4030_kpwrite_u8(struct omap_keypad *kp,
83 u32 module, u8 data, u32 reg)
87 ret = twl4030_i2c_write_u8(module, data, reg);
90 "Could not write TWL4030: %X - ret %d[%x]\n",
97 static int omap_kp_find_key(struct omap_keypad *kp, int col, int row)
101 rc = KEY(col, row, 0);
102 for (i = 0; i < kp->keymapsize; i++)
103 if ((kp->keymap[i] & ROWCOL_MASK) == rc)
104 return kp->keymap[i] & KEYNUM_MASK;
109 static inline u16 omap_kp_col_xlate(struct omap_keypad *kp, u8 col)
111 /* If all bits in a row are active for all coloumns then
112 * we have that row line connected to gnd. Mark this
113 * key on as if it was on matrix position n_cols (ie
114 * one higher than the size of the matrix).
117 return 1 << kp->n_cols;
119 return col & ((1 << kp->n_cols) - 1);
122 static int omap_kp_read_kp_matrix_state(struct omap_keypad *kp, u16 *state)
124 u8 new_state[MAX_ROWS];
126 int ret = twl4030_kpread(kp, TWL4030_MODULE_KEYPAD,
127 new_state, KEYP_FULL_CODE_7_0, kp->n_rows);
129 for (row = 0; row < kp->n_rows; row++)
130 state[row] = omap_kp_col_xlate(kp, new_state[row]);
135 static int omap_kp_is_in_ghost_state(struct omap_keypad *kp, u16 *key_state)
140 for (i = 0; i < kp->n_rows; i++) {
141 u16 col = key_state[i];
143 if ((col & check) && hweight16(col) > 1)
151 static void twl4030_kp_scan(struct omap_keypad *kp, int release_all)
153 u16 new_state[MAX_ROWS];
157 memset(new_state, 0, sizeof(new_state));
159 /* check for any changes */
160 int ret = omap_kp_read_kp_matrix_state(kp, new_state);
161 if (ret < 0) /* panic ... */
164 if (omap_kp_is_in_ghost_state(kp, new_state))
168 mutex_lock(&kp->mutex);
170 /* check for changes and print those */
171 for (row = 0; row < kp->n_rows; row++) {
172 int changed = new_state[row] ^ kp->kp_state[row];
177 for (col = 0; col < kp->n_cols + 1; col++) {
180 if (!(changed & (1 << col)))
183 dev_dbg(kp->dbg_dev, "key [%d:%d] %s\n", row, col,
184 (new_state[row] & (1 << col)) ?
185 "press" : "release");
187 key = omap_kp_find_key(kp, col, row);
189 dev_warn(kp->dbg_dev,
190 "Spurious key event %d-%d\n",
193 input_report_key(kp->omap_twl4030kp, key,
194 new_state[row] & (1 << col));
196 kp->kp_state[row] = new_state[row];
199 mutex_unlock(&kp->mutex);
203 * Keypad interrupt handler
205 static irqreturn_t do_kp_irq(int irq, void *_kp)
207 struct omap_keypad *kp = _kp;
211 /* Read & Clear TWL4030 pending interrupt */
212 ret = twl4030_kpread(kp, TWL4030_MODULE_KEYPAD, ®, KEYP_ISR1, 1);
214 /* Release all keys if I2C has gone bad or
215 * the KEYP has gone to idle state */
216 if ((ret >= 0) && (reg & KEYP_IMR1_KP))
217 twl4030_kp_scan(kp, 0);
219 twl4030_kp_scan(kp, 1);
225 * Registers keypad device with input sub system
226 * and configures TWL4030 keypad registers
228 static int __init omap_kp_probe(struct platform_device *pdev)
233 struct omap_keypad *kp;
234 struct omap_kp_platform_data *pdata = pdev->dev.platform_data;
236 kp = kzalloc(sizeof(*kp), GFP_KERNEL);
240 if (!pdata->rows || !pdata->cols || !pdata->keymap) {
241 dev_err(kp->dbg_dev, "No rows, cols or keymap from pdata\n");
246 dev_set_drvdata(&pdev->dev, kp);
248 /* Get the debug Device */
249 kp->dbg_dev = &pdev->dev;
251 kp->omap_twl4030kp = input_allocate_device();
252 if (!kp->omap_twl4030kp) {
257 mutex_init(&kp->mutex);
259 kp->keymap = pdata->keymap;
260 kp->keymapsize = pdata->keymapsize;
261 kp->n_rows = pdata->rows;
262 kp->n_cols = pdata->cols;
263 kp->irq = pdata->irq;
265 /* setup input device */
266 set_bit(EV_KEY, kp->omap_twl4030kp->evbit);
268 /* Enable auto repeat feature of Linux input subsystem */
270 set_bit(EV_REP, kp->omap_twl4030kp->evbit);
272 for (i = 0; i < kp->keymapsize; i++)
273 set_bit(kp->keymap[i] & KEYNUM_MASK,
274 kp->omap_twl4030kp->keybit);
276 kp->omap_twl4030kp->name = "omap_twl4030keypad";
277 kp->omap_twl4030kp->phys = "omap_twl4030keypad/input0";
278 kp->omap_twl4030kp->dev.parent = &pdev->dev;
280 kp->omap_twl4030kp->id.bustype = BUS_HOST;
281 kp->omap_twl4030kp->id.vendor = 0x0001;
282 kp->omap_twl4030kp->id.product = 0x0001;
283 kp->omap_twl4030kp->id.version = 0x0003;
285 kp->omap_twl4030kp->keycode = kp->keymap;
286 kp->omap_twl4030kp->keycodesize = sizeof(unsigned int);
287 kp->omap_twl4030kp->keycodemax = kp->keymapsize;
289 ret = input_register_device(kp->omap_twl4030kp);
292 "Unable to register twl4030 keypad device\n");
296 /* Disable auto-repeat */
297 reg = KEYP_CTRL_NOAUTORPT;
298 ret = twl4030_kpwrite_u8(kp, TWL4030_MODULE_KEYPAD, reg, KEYP_CTRL);
302 /* Enable TO rising and KP rising and falling edge detection */
303 reg = KEYP_EDR_KP_BOTH | KEYP_EDR_TO_RISING;
304 ret = twl4030_kpwrite_u8(kp, TWL4030_MODULE_KEYPAD, reg, KEYP_EDR);
308 /* Set PTV prescaler Field */
309 reg = (PTV_PRESCALER << KEYP_LK_PTV_PTV_SHIFT);
310 ret = twl4030_kpwrite_u8(kp, TWL4030_MODULE_KEYPAD, reg, KEYP_LK_PTV);
314 /* Set key debounce time to 20 ms */
315 i = KEYP_PERIOD_US(20000, PTV_PRESCALER);
316 ret = twl4030_kpwrite_u8(kp, TWL4030_MODULE_KEYPAD, i, KEYP_DEB);
320 /* Set timeout period to 100 ms */
321 i = KEYP_PERIOD_US(200000, PTV_PRESCALER);
322 ret = twl4030_kpwrite_u8(kp, TWL4030_MODULE_KEYPAD,
323 (i & 0xFF), KEYP_TIMEOUT_L);
327 ret = twl4030_kpwrite_u8(kp, TWL4030_MODULE_KEYPAD,
328 (i >> 8), KEYP_TIMEOUT_H);
332 /* Enable Clear-on-Read */
333 reg = KEYP_SIH_CTRL_COR | KEYP_SIH_CTRL_PEND_DIS;
334 ret = twl4030_kpwrite_u8(kp, TWL4030_MODULE_KEYPAD,
340 * This ISR will always execute in kernel thread context because of
341 * the need to access the TWL4030 over the I2C bus.
343 ret = request_irq(kp->irq, do_kp_irq, IRQF_DISABLED,
344 "TWL4030 Keypad", kp);
346 dev_info(kp->dbg_dev, "request_irq failed for irq no=%d\n",
350 /* Enable KP and TO interrupts now. */
351 reg = ~(KEYP_IMR1_KP | KEYP_IMR1_TO);
352 ret = twl4030_kpwrite_u8(kp, TWL4030_MODULE_KEYPAD,
358 ret = omap_kp_read_kp_matrix_state(kp, kp->kp_state);
364 /* mask all events - we don't care about the result */
365 (void) twl4030_kpwrite_u8(kp, TWL4030_MODULE_KEYPAD, 0xff, KEYP_IMR1);
367 free_irq(kp->irq, NULL);
369 input_unregister_device(kp->omap_twl4030kp);
371 input_free_device(kp->omap_twl4030kp);
376 static int omap_kp_remove(struct platform_device *pdev)
378 struct omap_keypad *kp = dev_get_drvdata(&pdev->dev);
380 free_irq(kp->irq, kp);
381 input_unregister_device(kp->omap_twl4030kp);
388 static struct platform_driver omap_kp_driver = {
389 .probe = omap_kp_probe,
390 .remove = __devexit_p(omap_kp_remove),
392 .name = "omap_twl4030keypad",
393 .owner = THIS_MODULE,
398 * OMAP TWL4030 Keypad init
400 static int __devinit omap_kp_init(void)
402 return platform_driver_register(&omap_kp_driver);
405 static void __exit omap_kp_exit(void)
407 platform_driver_unregister(&omap_kp_driver);
410 module_init(omap_kp_init);
411 module_exit(omap_kp_exit);
412 MODULE_ALIAS("platform:omap_twl4030keypad");
413 MODULE_AUTHOR("Texas Instruments");
414 MODULE_DESCRIPTION("OMAP TWL4030 Keypad Driver");
415 MODULE_LICENSE("GPL");