2 * linux/drivers/char/omap-keypad.c
6 * Copyright (C) 2003 Nokia Corporation
7 * Written by Timo Teräs <ext-timo.teras@nokia.com>
9 * Added support for H2 & H3 Keypad
10 * Copyright (C) 2004 Texas Instruments
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/interrupt.h>
30 #include <linux/types.h>
31 #include <linux/input.h>
32 #include <linux/kernel.h>
33 #include <linux/delay.h>
34 #include <linux/platform_device.h>
35 #include <asm/arch/irqs.h>
36 #include <asm/arch/gpio.h>
37 #include <asm/arch/hardware.h>
38 #include <asm/arch/keypad.h>
39 #include <asm/arch/menelaus.h>
41 #include <asm/errno.h>
42 #include <asm/mach-types.h>
43 #include <asm/arch/mux.h>
45 #undef NEW_BOARD_LEARNING_MODE
47 static void omap_kp_tasklet(unsigned long);
48 static void omap_kp_timer(unsigned long);
50 static unsigned char keypad_state[8];
53 struct input_dev *input;
54 struct timer_list timer;
60 DECLARE_TASKLET_DISABLED(kp_tasklet, omap_kp_tasklet, 0);
62 #define KEY(col, row, val) (((col) << 28) | ((row) << 24) | (val))
64 static int test_keymap[] = {
78 static unsigned int *row_gpios;
79 static unsigned int *col_gpios;
81 #ifdef CONFIG_ARCH_OMAP2
82 static void set_col_gpio_val(struct omap_kp *omap_kp, u8 value)
85 for (col = 0; col < omap_kp->cols; col++) {
86 if (value & (1 << col))
87 omap_set_gpio_dataout(col_gpios[col], 1);
89 omap_set_gpio_dataout(col_gpios[col], 0);
93 static u8 get_row_gpio_val(struct omap_kp *omap_kp)
98 for (row = 0; row < omap_kp->cols; row++) {
99 if (omap_get_gpio_datain(row_gpios[row]))
105 #define set_col_gpio_val(x, y) do {} while (0)
106 #define get_row_gpio_val(x) 0
109 static irqreturn_t omap_kp_interrupt(int irq, void *dev_id,
110 struct pt_regs *regs)
112 struct omap_kp *omap_kp = dev_id;
114 /* disable keyboard interrupt and schedule for handling */
115 if (cpu_is_omap24xx()) {
117 for (i = 0; i < omap_kp->rows; i++)
118 disable_irq(OMAP_GPIO_IRQ(row_gpios[i]));
120 /* disable keyboard interrupt and schedule for handling */
121 omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
124 tasklet_schedule(&kp_tasklet);
129 static void omap_kp_timer(unsigned long data)
131 tasklet_schedule(&kp_tasklet);
134 static void omap_kp_scan_keypad(struct omap_kp *omap_kp, unsigned char *state)
138 /* read the keypad status */
139 if (cpu_is_omap24xx()) {
141 for (i = 0; i < omap_kp->rows; i++)
142 disable_irq(OMAP_GPIO_IRQ(row_gpios[i]));
144 /* disable keyboard interrupt and schedule for handling */
145 omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
147 if (!cpu_is_omap24xx()) {
148 /* read the keypad status */
149 omap_writew(0xff, OMAP_MPUIO_BASE + OMAP_MPUIO_KBC);
150 for (col = 0; col < omap_kp->cols; col++) {
151 omap_writew(~(1 << col) & 0xff, OMAP_MPUIO_BASE + OMAP_MPUIO_KBC);
153 if (machine_is_omap_osk() || machine_is_omap_h2() || machine_is_omap_h3()) {
158 state[col] = ~omap_readw(OMAP_MPUIO_BASE + OMAP_MPUIO_KBR_LATCH) & 0xff;
160 omap_writew(0x00, OMAP_MPUIO_BASE + OMAP_MPUIO_KBC);
163 /* read the keypad status */
164 for (col = 0; col < omap_kp->cols; col++) {
165 set_col_gpio_val(omap_kp, ~(1 << col));
166 state[col] = ~(get_row_gpio_val(omap_kp)) & 0x3f;
168 set_col_gpio_val(omap_kp, 0);
172 static inline int omap_kp_find_key(int col, int row)
176 key = KEY(col, row, 0);
177 for (i = 0; keymap[i] != 0; i++)
178 if ((keymap[i] & 0xff000000) == key)
179 return keymap[i] & 0x00ffffff;
183 static void omap_kp_tasklet(unsigned long data)
185 struct omap_kp *omap_kp_data = (struct omap_kp *) data;
186 unsigned char new_state[8], changed, key_down = 0;
190 /* check for any changes */
191 omap_kp_scan_keypad(omap_kp_data, new_state);
193 /* check for changes and print those */
194 for (col = 0; col < omap_kp_data->cols; col++) {
195 changed = new_state[col] ^ keypad_state[col];
196 key_down |= new_state[col];
200 for (row = 0; row < omap_kp_data->rows; row++) {
202 if (!(changed & (1 << row)))
204 #ifdef NEW_BOARD_LEARNING_MODE
205 printk(KERN_INFO "omap-keypad: key %d-%d %s\n", col, row, (new_state[col] & (1 << row)) ? "pressed" : "released");
207 key = omap_kp_find_key(col, row);
209 printk(KERN_WARNING "omap-keypad: Spurious key event %d-%d\n",
211 /* We scan again after a couple of seconds */
216 input_report_key(omap_kp_data->input, key,
217 new_state[col] & (1 << row));
221 memcpy(keypad_state, new_state, sizeof(keypad_state));
225 /* some key is pressed - keep irq disabled and use timer
226 * to poll the keypad */
229 mod_timer(&omap_kp_data->timer, jiffies + delay);
231 /* enable interrupts */
232 if (cpu_is_omap24xx()) {
234 for (i = 0; i < omap_kp_data->rows; i++)
235 enable_irq(OMAP_GPIO_IRQ(row_gpios[i]));
237 omap_writew(0, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
243 static int omap_kp_suspend(struct platform_device *dev, pm_message_t state)
250 static int omap_kp_resume(struct platform_device *dev)
257 #define omap_kp_suspend NULL
258 #define omap_kp_resume NULL
261 static int __init omap_kp_probe(struct platform_device *pdev)
263 struct omap_kp *omap_kp;
264 struct input_dev *input_dev;
265 struct omap_kp_platform_data *pdata = pdev->dev.platform_data;
268 if (!pdata->rows || !pdata->cols) {
269 printk(KERN_ERR "No rows and cols from pdata\n");
273 omap_kp = kzalloc(sizeof(struct omap_kp), GFP_KERNEL);
274 input_dev = input_allocate_device();
275 if (!omap_kp || !input_dev) {
277 input_free_device(input_dev);
281 platform_set_drvdata(pdev, omap_kp);
283 omap_kp->input = input_dev;
285 /* Disable the interrupt for the MPUIO keyboard */
286 if (!cpu_is_omap24xx())
287 omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
290 keymap = test_keymap;
292 keymap = pdata->keymap;
295 set_bit(EV_REP, input_dev->evbit);
297 if (pdata->row_gpios && pdata->col_gpios) {
298 row_gpios = pdata->row_gpios;
299 col_gpios = pdata->col_gpios;
302 if (cpu_is_omap24xx() && omap_has_menelaus()) {
308 omap_kp->rows = pdata->rows;
309 omap_kp->cols = pdata->cols;
311 if (cpu_is_omap24xx()) {
313 for (i = 0; i < omap_kp->cols; i++) {
314 if (omap_request_gpio(col_gpios[i]) < 0) {
315 printk(KERN_ERR "Failed to request"
316 "GPIO%d for keypad\n",
320 omap_set_gpio_direction(col_gpios[i], 0);
323 for (i = 0; i < omap_kp->rows; i++) {
324 if (omap_request_gpio(row_gpios[i]) < 0) {
325 printk(KERN_ERR "Failed to request"
326 "GPIO%d for keypad\n",
330 omap_set_gpio_direction(row_gpios[i], 1);
334 init_timer(&omap_kp->timer);
335 omap_kp->timer.function = omap_kp_timer;
336 omap_kp->timer.data = (unsigned long) omap_kp;
338 /* get the irq and init timer*/
339 tasklet_enable(&kp_tasklet);
340 kp_tasklet.data = (unsigned long) omap_kp;
342 omap_kp->irq = platform_get_irq(pdev, 0);
344 if (request_irq(omap_kp->irq, omap_kp_interrupt, 0,
345 "omap-keypad", omap_kp) < 0)
349 /* setup input device */
350 set_bit(EV_KEY, input_dev->evbit);
351 for (i = 0; keymap[i] != 0; i++)
352 set_bit(keymap[i] & 0x00ffffff, input_dev->keybit);
353 input_dev->name = "omap-keypad";
354 input_dev->cdev.dev = &pdev->dev;
355 input_dev->private = omap_kp;
356 input_register_device(omap_kp->input);
358 if (machine_is_omap_h2() || machine_is_omap_h3() ||
359 machine_is_omap_perseus2()) {
360 omap_writew(0xff, OMAP_MPUIO_BASE + OMAP_MPUIO_GPIO_DEBOUNCING);
362 /* scan current status and enable interrupt */
363 omap_kp_scan_keypad(omap_kp, keypad_state);
364 if (!cpu_is_omap24xx()) {
365 omap_writew(0, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
367 for (i = 0; i < omap_kp->rows; i++) {
368 if (request_irq(OMAP_GPIO_IRQ(row_gpios[i]), omap_kp_interrupt,
369 SA_TRIGGER_FALLING, "omap-keypad", omap_kp) < 0)
377 static int omap_kp_remove(struct platform_device *pdev)
379 struct omap_kp *omap_kp = platform_get_drvdata(pdev);
381 /* disable keypad interrupt handling */
382 tasklet_disable(&kp_tasklet);
383 if (cpu_is_omap24xx()) {
385 for (i = 0; i < omap_kp->cols; i++)
386 omap_free_gpio(col_gpios[i]);
387 for (i = 0; i < omap_kp->rows; i++) {
388 omap_free_gpio(row_gpios[i]);
389 free_irq(OMAP_GPIO_IRQ(row_gpios[i]), 0);
392 omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
393 free_irq(omap_kp->irq, 0);
396 del_timer_sync(&omap_kp->timer);
398 /* unregister everything */
399 input_unregister_device(omap_kp->input);
406 static struct platform_driver omap_kp_driver = {
407 .probe = omap_kp_probe,
408 .remove = omap_kp_remove,
409 .suspend = omap_kp_suspend,
410 .resume = omap_kp_resume,
412 .name = "omap-keypad",
416 static int __devinit omap_kp_init(void)
418 printk(KERN_INFO "OMAP Keypad Driver\n");
419 return platform_driver_register(&omap_kp_driver);
422 static void __exit omap_kp_exit(void)
424 platform_driver_unregister(&omap_kp_driver);
427 module_init(omap_kp_init);
428 module_exit(omap_kp_exit);
430 MODULE_AUTHOR("Timo Teräs");
431 MODULE_DESCRIPTION("OMAP Keypad Driver");
432 MODULE_LICENSE("GPL");