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/delay.h>
35 #include <linux/bitops.h>
36 #include <linux/platform_device.h>
37 #include <linux/i2c.h>
38 #include <linux/i2c/twl4030.h>
39 #include <linux/irq.h>
40 #include <asm/arch/keypad.h>
41 #include "twl4030-keypad.h"
43 #define PTV_PRESCALER 4
45 #define MAX_ROWS 8 /* TWL4030 hardlimit */
46 #define ROWCOL_MASK 0xFF000000
47 #define KEYNUM_MASK 0x00FFFFFF
49 /* Global variables */
51 static u16 kp_state[MAX_ROWS];
52 static int n_rows, n_cols;
54 static struct device *dbg_dev;
55 static struct input_dev *omap_twl4030kp;
57 static int twl4030_kpread(u32 module, u8 *data, u32 reg, u8 num_bytes)
61 ret = twl4030_i2c_read(module, data, reg, num_bytes);
63 dev_warn(dbg_dev, "Couldn't read TWL4030: %X - ret %d[%x]\n",
70 static int twl4030_kpwrite_u8(u32 module, u8 data, u32 reg)
74 ret = twl4030_i2c_write_u8(module, data, reg);
76 dev_warn(dbg_dev, "Could not write TWL4030: %X - ret %d[%x]\n",
83 static int omap_kp_find_key(int col, int row)
87 rc = KEY(col, row, 0);
88 for (i = 0; keymap[i] != 0; i++)
89 if ((keymap[i] & ROWCOL_MASK) == rc)
90 return keymap[i] & KEYNUM_MASK;
95 static inline u16 omap_kp_col_xlate(u8 col)
97 /* If all bits in a row are active for all coloumns then
98 * we have that row line connected to gnd. Mark this
99 * key on as if it was on matrix position n_cols (ie
100 * one higher than the size of the matrix).
103 return (1 << n_cols);
105 return col & ((1 << n_cols) - 1);
108 static int omap_kp_read_kp_matrix_state(u16 *state)
110 u8 new_state[MAX_ROWS];
112 int ret = twl4030_kpread(TWL4030_MODULE_KEYPAD,
113 new_state, KEYP_FULL_CODE_7_0, n_rows);
115 for (row = 0; row < n_rows; row++)
116 state[row] = omap_kp_col_xlate(new_state[row]);
121 static int omap_kp_is_in_ghost_state(u16 *key_state)
126 for (i = 0; i < n_rows; i++) {
127 u16 col = key_state[i];
129 if ((col & check) && hweight16(col) > 1)
137 static void twl4030_kp_scan(int release_all)
139 u16 new_state[MAX_ROWS];
143 memset(new_state, 0, sizeof(new_state));
145 /* check for any changes */
146 int ret = omap_kp_read_kp_matrix_state(new_state);
147 if (ret < 0) /* panic ... */
150 if (omap_kp_is_in_ghost_state(new_state))
154 /* check for changes and print those */
155 for (row = 0; row < n_rows; row++) {
156 int changed = new_state[row] ^ kp_state[row];
161 for (col = 0; col < n_cols + 1; col++) {
164 if (!(changed & (1 << col)))
167 dev_dbg(dbg_dev, "key [%d:%d] %s\n", row, col,
168 (new_state[row] & (1 << col)) ?
169 "press" : "release");
171 key = omap_kp_find_key(col, row);
173 dev_warn(dbg_dev, "Spurious key event %d-%d\n",
176 input_report_key(omap_twl4030kp, key,
177 new_state[row] & (1 << col));
179 kp_state[row] = new_state[row];
184 * Keypad interrupt handler
186 static irqreturn_t do_kp_irq(int irq, void *dev_id)
191 /* Read & Clear TWL4030 pending interrupt */
192 ret = twl4030_kpread(TWL4030_MODULE_KEYPAD, ®, KEYP_ISR1, 1);
194 /* Release all keys if I2C has gone bad or
195 * the KEYP has gone to idle state */
196 if ((ret >= 0) && (reg & KEYP_IMR1_KP))
205 * Registers keypad device with input sub system
206 * and configures TWL4030 keypad registers
208 static int __init omap_kp_probe(struct platform_device *pdev)
213 struct omap_kp_platform_data *pdata = pdev->dev.platform_data;
215 /* Get the debug Device */
216 dbg_dev = &(pdev->dev);
218 if (!pdata->rows || !pdata->cols || !pdata->keymap) {
219 dev_err(dbg_dev, "No rows, cols or keymap from pdata\n");
223 omap_twl4030kp = input_allocate_device();
224 if (omap_twl4030kp == NULL)
227 keymap = pdata->keymap;
228 n_rows = pdata->rows;
229 n_cols = pdata->cols;
231 /* setup input device */
232 set_bit(EV_KEY, omap_twl4030kp->evbit);
234 /* Enable auto repeat feature of Linux input subsystem */
236 set_bit(EV_REP, omap_twl4030kp->evbit);
238 for (i = 0; keymap[i] != 0; i++)
239 set_bit(keymap[i] & KEYNUM_MASK, omap_twl4030kp->keybit);
241 omap_twl4030kp->name = "omap_twl4030keypad";
242 omap_twl4030kp->phys = "omap_twl4030keypad/input0";
243 omap_twl4030kp->dev.parent = &pdev->dev;
245 omap_twl4030kp->id.bustype = BUS_HOST;
246 omap_twl4030kp->id.vendor = 0x0001;
247 omap_twl4030kp->id.product = 0x0001;
248 omap_twl4030kp->id.version = 0x0003;
250 omap_twl4030kp->keycode = keymap;
251 omap_twl4030kp->keycodesize = sizeof(unsigned int);
252 omap_twl4030kp->keycodemax = pdata->keymapsize;
254 ret = input_register_device(omap_twl4030kp);
256 dev_err(dbg_dev, "Unable to register twl4030 keypad device\n");
260 /* Disable auto-repeat */
261 reg = KEYP_CTRL_NOAUTORPT;
262 ret = twl4030_kpwrite_u8(TWL4030_MODULE_KEYPAD, reg, KEYP_CTRL);
266 /* Enable TO rising and KP rising and falling edge detection */
267 reg = KEYP_EDR_KP_BOTH | KEYP_EDR_TO_RISING;
268 ret = twl4030_kpwrite_u8(TWL4030_MODULE_KEYPAD, reg, KEYP_EDR);
272 /* Set PTV prescaler Field */
273 reg = (PTV_PRESCALER << KEYP_LK_PTV_PTV_SHIFT);
274 ret = twl4030_kpwrite_u8(TWL4030_MODULE_KEYPAD, reg, KEYP_LK_PTV);
278 /* Set key debounce time to 20 ms */
279 i = KEYP_PERIOD_US(20000, PTV_PRESCALER);
280 ret = twl4030_kpwrite_u8(TWL4030_MODULE_KEYPAD, i, KEYP_DEB);
284 /* Set timeout period to 100 ms */
285 i = KEYP_PERIOD_US(200000, PTV_PRESCALER);
286 ret = twl4030_kpwrite_u8(TWL4030_MODULE_KEYPAD,
287 (i & 0xFF), KEYP_TIMEOUT_L);
291 ret = twl4030_kpwrite_u8(TWL4030_MODULE_KEYPAD,
292 (i >> 8), KEYP_TIMEOUT_H);
296 /* Enable Clear-on-Read */
297 reg = KEYP_SIH_CTRL_COR | KEYP_SIH_CTRL_PEND_DIS;
298 ret = twl4030_kpwrite_u8(TWL4030_MODULE_KEYPAD,
304 * This ISR will always execute in kernel thread context because of
305 * the need to access the TWL4030 over the I2C bus.
307 ret = request_irq(TWL4030_MODIRQ_KEYPAD, do_kp_irq,
308 IRQF_DISABLED, "TWL4030 Keypad", omap_twl4030kp);
310 dev_info(dbg_dev, "request_irq failed for irq no=%d\n",
311 TWL4030_MODIRQ_KEYPAD);
314 /* Enable KP and TO interrupts now. */
315 reg = ~(KEYP_IMR1_KP | KEYP_IMR1_TO);
316 ret = twl4030_kpwrite_u8(TWL4030_MODULE_KEYPAD,
322 ret = omap_kp_read_kp_matrix_state(kp_state);
328 /* mask all events - we don't care about the result */
329 (void) twl4030_kpwrite_u8(TWL4030_MODULE_KEYPAD, 0xff, KEYP_IMR1);
331 free_irq(TWL4030_MODIRQ_KEYPAD, NULL);
333 input_unregister_device(omap_twl4030kp);
335 input_free_device(omap_twl4030kp);
339 static int omap_kp_remove(struct platform_device *pdev)
341 free_irq(TWL4030_MODIRQ_KEYPAD, NULL);
343 input_unregister_device(omap_twl4030kp);
348 static struct platform_driver omap_kp_driver = {
349 .probe = omap_kp_probe,
350 .remove = omap_kp_remove,
352 .name = "omap_twl4030keypad",
357 * OMAP TWL4030 Keypad init
359 static int __devinit omap_kp_init(void)
361 return platform_driver_register(&omap_kp_driver);
364 static void __exit omap_kp_exit(void)
366 platform_driver_unregister(&omap_kp_driver);
369 module_init(omap_kp_init);
370 module_exit(omap_kp_exit);
371 MODULE_AUTHOR("Texas Instruments");
372 MODULE_DESCRIPTION("OMAP TWL4030 Keypad Driver");
373 MODULE_LICENSE("GPL");