]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/input/keyboard/omap-keypad.c
h63xx: button and joypad support
[linux-2.6-omap-h63xx.git] / drivers / input / keyboard / omap-keypad.c
index ef30720a1f5de2297fb64c3ce55939751d45013a..2895070360422385e14f8be2c60ed9745f1ebee7 100644 (file)
@@ -9,6 +9,9 @@
  * Added support for H2 & H3 Keypad
  * Copyright (C) 2004 Texas Instruments
  *
+ * Added support for keypad and joypad in HP iPAQ h63xx series of devices.
+ * Copyright (C) 2005 Mika Laitio
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
@@ -32,6 +35,7 @@
 #include <linux/kernel.h>
 #include <linux/delay.h>
 #include <linux/platform_device.h>
+#include <linux/mutex.h>
 #include <asm/arch/irqs.h>
 #include <asm/arch/gpio.h>
 #include <asm/arch/hardware.h>
 #include <asm/arch/mux.h>
 
 #undef NEW_BOARD_LEARNING_MODE
+//#define NEW_BOARD_LEARNING_MODE 1
+
+/*
+ * Following 5 keypad events are not really sent to userspace. 
+ * Instead if the good combination of them is sent, then that is send.
+ * (up, right, down, left, enter)
+ */
+#define        _H6300_JOYPAD_UP_RIGHT          1       // 00001
+#define _H6300_JOYPAD_DOWN_RIGHT       2       // 00010
+#define _h6300_JOYPAD_DOWN_LEFT                4       // 00100
+#define _h6300_JOYPAD_UP_LEFT          8       // 01000
+#define _H6300_JOYPAD_KEY_OK           16      // 10000
+#define _H6300_JOYPAD_REPORT_COLUMN    4
 
 static void omap_kp_tasklet(unsigned long);
 static void omap_kp_timer(unsigned long);
 
 static unsigned char keypad_state[8];
+static DEFINE_MUTEX(kp_enable_mutex);
+static int kp_enable = 1;
+static int kp_cur_group = -1;
+
+static int prevJoypadKeycodePressEmulated;
 
 struct omap_kp {
        struct input_dev *input;
        struct timer_list timer;
-       unsigned int irq;
+       int irq;
        unsigned int rows;
        unsigned int cols;
 };
 
 DECLARE_TASKLET_DISABLED(kp_tasklet, omap_kp_tasklet, 0);
 
-#define KEY(col, row, val) (((col) << 28) | ((row) << 24) | (val))
-
-static int test_keymap[] = {
-       KEY(0, 0, KEY_F4),
-       KEY(1, 0, KEY_LEFT),
-       KEY(2, 0, KEY_F1),
-       KEY(0, 1, KEY_DOWN),
-       KEY(1, 1, KEY_ENTER),
-       KEY(2, 1, KEY_UP),
-       KEY(0, 2, KEY_F3),
-       KEY(1, 2, KEY_RIGHT),
-       KEY(2, 2, KEY_F2),
-       0
-};
-
 static int *keymap;
 static unsigned int *row_gpios;
 static unsigned int *col_gpios;
@@ -95,7 +102,7 @@ static u8 get_row_gpio_val(struct omap_kp *omap_kp)
        int row;
        u8 value = 0;
 
-       for (row = 0; row < omap_kp->cols; row++) {
+       for (row = 0; row < omap_kp->rows; row++) {
                if (omap_get_gpio_datain(row_gpios[row]))
                        value |= (1 << row);
        }
@@ -150,7 +157,7 @@ static void omap_kp_scan_keypad(struct omap_kp *omap_kp, unsigned char *state)
                for (col = 0; col < omap_kp->cols; col++) {
                        omap_writew(~(1 << col) & 0xff, OMAP_MPUIO_BASE + OMAP_MPUIO_KBC);
 
-                       if (machine_is_omap_osk() || machine_is_omap_h2() || machine_is_omap_h3()) {
+                       if (machine_is_omap_osk() || machine_is_omap_h2() || machine_is_omap_h3() || machine_is_omap_h6300()) {
                                udelay(9);
                        } else {
                                udelay(4);
@@ -180,17 +187,26 @@ static inline int omap_kp_find_key(int col, int row)
        return -1;
 }
 
+int is_key_down(unsigned char new_state[],
+               int col,
+               int row)
+{
+       return (new_state[col] & (1 << row)) ? 1 : 0;
+}
+
 static void omap_kp_tasklet(unsigned long data)
 {
        struct omap_kp *omap_kp_data = (struct omap_kp *) data;
        unsigned char new_state[8], changed, key_down = 0;
        int col, row;
        int spurious = 0;
+       int report_key, report_col, report_row, joypad_checked; // h6300-joypad specific variables
 
        /* check for any changes */
        omap_kp_scan_keypad(omap_kp_data, new_state);
 
        /* check for changes and print those */
+       joypad_checked  = 0;
        for (col = 0; col < omap_kp_data->cols; col++) {
                changed = new_state[col] ^ keypad_state[col];
                key_down |= new_state[col];
@@ -212,9 +228,177 @@ static void omap_kp_tasklet(unsigned long data)
                                spurious = 1;
                                continue;
                        }
-
-                       input_report_key(omap_kp_data->input, key,
-                                        new_state[col] & (1 << row));
+                       if (machine_is_omap_h6300() && 
+                          ((col == 1) || (col == _H6300_JOYPAD_REPORT_COLUMN)))
+                       {
+                               if (col == _H6300_JOYPAD_REPORT_COLUMN)
+                               {
+                                       continue;
+                               }
+                               if ((joypad_checked == 0) &&
+                                   ((key == _H6300_JOYPAD_KEY_OK) ||
+                                    (key == _h6300_JOYPAD_UP_LEFT) ||
+                                    (key == _H6300_JOYPAD_UP_RIGHT) ||
+                                    (key == _H6300_JOYPAD_DOWN_RIGHT) ||
+                                    (key == _h6300_JOYPAD_DOWN_LEFT)))
+                               {
+                                       if (is_key_down(new_state, col, row))
+                                       {
+                                               /*
+                                                * only enter pressed
+                                                * 1 0 0 _H6300_JOYPAD_KEY_OK 0 0
+                                                * --> 100100 == 36
+                                                */
+                                                if (new_state[1] == 36)
+                                                {
+                                                       joypad_checked  = 1;
+                                                       prevJoypadKeycodePressEmulated  = KEY_ENTER;
+                                                       new_state[_H6300_JOYPAD_REPORT_COLUMN]  = 48;   //110000
+                                                       report_key      = prevJoypadKeycodePressEmulated;
+                                                       report_col      = _H6300_JOYPAD_REPORT_COLUMN;
+                                                       report_row      = 4;
+                                                       input_report_key(omap_kp_data->input,
+                                                                       report_key,
+                                                                       new_state[report_col] & (1 << report_row));
+                                                }                                              
+                                               /*
+                                                * enter, up_left and up_right sensors pressed.
+                                                * 1 _H6300_JOYPAD_UP_RIGHT 0 _H6300_JOYPAD_KEY_OK 0 _h6300_JOYPAD_UP_LEFT
+                                                * --> 110101 == 53
+                                                * OR
+                                                * 1 KEY_UP_RIGHT 0 0 0 _h6300_JOYPAD_UP_LEFT
+                                                * --> 110001 == 42
+                                                * --> move to up
+                                                */
+                                               else if ((new_state[1] == 53) ||
+                                                        (new_state[1] == 49))
+                                               {
+                                                       joypad_checked  = 1;
+                                                       prevJoypadKeycodePressEmulated  = KEY_UP;
+                                                       new_state[_H6300_JOYPAD_REPORT_COLUMN]  = 40;   //101000
+                                                       report_key      = prevJoypadKeycodePressEmulated;
+                                                       report_col      = _H6300_JOYPAD_REPORT_COLUMN;
+                                                       report_row      = 3;
+                                                       input_report_key(omap_kp_data->input,
+                                                                       report_key,
+                                                                       new_state[report_col] & (1 << report_row));
+                                               }
+                                               /*
+                                                * enter, down_left and down_right sensors pressed
+                                                * --> 101110 == 46
+                                                * OR
+                                                * down_left and down_right
+                                                * -->101010 == 42
+                                                * --> move to down
+                                                */
+                                               else if ((new_state[1] == 46) ||
+                                                        (new_state[1] == 42))
+                                               {
+                                                       joypad_checked  = 1;
+                                                       prevJoypadKeycodePressEmulated  = KEY_DOWN;
+                                                       new_state[_H6300_JOYPAD_REPORT_COLUMN]  = 34;   //100010
+                                                       report_key      = prevJoypadKeycodePressEmulated;
+                                                       report_col      = _H6300_JOYPAD_REPORT_COLUMN;
+                                                       report_row      = 1;
+                                                       input_report_key(omap_kp_data->input,
+                                                                       report_key,
+                                                                       new_state[report_col] & (1 << report_row));
+                                               }                                                                                               
+                                               /*
+                                                * enter, up_right and down_right sensors pressed
+                                                * --> 111100 == 60
+                                                * or
+                                                * down_right and up_right
+                                                * --> 111000 == 56
+                                                * --> move to right
+                                                */
+                                               else if ((new_state[1] == 60) ||
+                                                        (new_state[1] == 56))
+                                               {
+                                                       joypad_checked  = 1;
+                                                       prevJoypadKeycodePressEmulated  = KEY_RIGHT;
+                                                       new_state[_H6300_JOYPAD_REPORT_COLUMN]  = 33;   //100001
+                                                       report_key      = prevJoypadKeycodePressEmulated;
+                                                       report_col      = _H6300_JOYPAD_REPORT_COLUMN;
+                                                       report_row      = 0;
+                                                       input_report_key(omap_kp_data->input,
+                                                                       report_key,
+                                                                       new_state[report_col] & (1 << report_row));
+                                               }
+                                               /*
+                                                * enter, up_left and down_left sensors pressed
+                                                * --> 100111 == 39
+                                                * or up_left and down_left
+                                                * --> 100011 == 35
+                                                * --> move to left
+                                                */
+                                               else if ((new_state[1] == 39) ||
+                                                        (new_state[1] == 35))
+                                               {
+                                                       joypad_checked  = 1;
+                                                       prevJoypadKeycodePressEmulated  = KEY_LEFT;
+                                                       new_state[_H6300_JOYPAD_REPORT_COLUMN]  = 36;   //100100
+                                                       report_key      = prevJoypadKeycodePressEmulated;
+                                                       report_col      = _H6300_JOYPAD_REPORT_COLUMN;
+                                                       report_row      = 2;
+                                                       input_report_key(omap_kp_data->input,
+                                                                       report_key,
+                                                                       new_state[report_col] & (1 << report_row));
+                                               }
+                                               else
+                                               {
+                                                       //printk("missed new_state = %d\n", new_state[1]);
+                                               }
+                                       }
+                                       else
+                                       {
+                                               if (prevJoypadKeycodePressEmulated != 0)
+                                               {
+                                                       // report key up event
+                                                       joypad_checked  = 1;
+                                                       new_state[_H6300_JOYPAD_REPORT_COLUMN]  = 32;   //100000
+                                                       report_key      = prevJoypadKeycodePressEmulated;
+                                                       report_col      = _H6300_JOYPAD_REPORT_COLUMN;
+                                                       switch(prevJoypadKeycodePressEmulated)
+                                                       {
+                                                               case KEY_RIGHT:
+                                                                       report_row      = 0;
+                                                                       break;
+                                                               case KEY_DOWN:
+                                                                       report_row      = 1;
+                                                                       break;
+                                                               case KEY_LEFT:
+                                                                       report_row      = 2;
+                                                                       break;
+                                                               case KEY_UP:
+                                                                       report_row      = 3;
+                                                                       break;
+                                                               case KEY_ENTER:
+                                                                       report_row      = 4;
+                                                                       break;
+                                                               default:
+                                                                       printk(KERN_WARNING "Unknown iPAQ h6300 column 1 key = %d released. This should newer happen!\n",
+                                                                               key);
+                                                                       report_row      = 0;
+                                                       }
+                                                       input_report_key(omap_kp_data->input,
+                                                                       report_key,
+                                                                       new_state[report_col] & (1 << report_row));
+                                                       prevJoypadKeycodePressEmulated  = 0;
+                                               }
+                                       }
+                               }
+                       }
+                       else
+                       {
+                               if (!(kp_cur_group == (key & GROUP_MASK) ||
+                                     kp_cur_group == -1))
+                                       continue;
+
+                               kp_cur_group = key & GROUP_MASK;
+                               input_report_key(omap_kp_data->input, key & ~GROUP_MASK,
+                                                !!(new_state[col] & (1 << row)));
+                       }
 #endif
                }
        }
@@ -235,10 +419,43 @@ static void omap_kp_tasklet(unsigned long data)
                                enable_irq(OMAP_GPIO_IRQ(row_gpios[i]));
                } else {
                        omap_writew(0, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
+                       kp_cur_group = -1;
                }
        }
 }
 
+static ssize_t omap_kp_enable_show(struct device *dev, 
+                                  struct device_attribute *attr, char *buf)
+{
+       return sprintf(buf, "%u\n", kp_enable);
+}
+
+static ssize_t omap_kp_enable_store(struct device *dev, struct device_attribute *attr,
+                                   const char *buf, size_t count)
+{
+       int state;
+
+       if (sscanf(buf, "%u", &state) != 1)
+               return -EINVAL;
+
+       if ((state != 1) && (state != 0))
+               return -EINVAL;
+
+       mutex_lock(&kp_enable_mutex);
+       if (state != kp_enable) {
+               if (state)
+                       enable_irq(INT_KEYBOARD);
+               else
+                       disable_irq(INT_KEYBOARD);
+               kp_enable = state;
+       }
+       mutex_unlock(&kp_enable_mutex);
+
+       return strnlen(buf, count);
+}
+
+static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, omap_kp_enable_show, omap_kp_enable_store);
+
 #ifdef CONFIG_PM
 static int omap_kp_suspend(struct platform_device *dev, pm_message_t state)
 {
@@ -265,8 +482,8 @@ static int __init omap_kp_probe(struct platform_device *pdev)
        struct omap_kp_platform_data *pdata =  pdev->dev.platform_data;
        int i;
 
-       if (!pdata->rows || !pdata->cols) {
-               printk(KERN_ERR "No rows and cols from pdata\n");
+       if (!pdata->rows || !pdata->cols || !pdata->keymap) {
+               printk(KERN_ERR "No rows, cols or keymap from pdata\n");
                return -EINVAL;
        }
 
@@ -286,10 +503,7 @@ static int __init omap_kp_probe(struct platform_device *pdev)
        if (!cpu_is_omap24xx())
                omap_writew(1, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
 
-       if (!pdata->keymap)
-               keymap = test_keymap;
-       else
-               keymap = pdata->keymap;
+       keymap = pdata->keymap;
 
        if (pdata->rep)
                set_bit(EV_REP, input_dev->evbit);
@@ -299,12 +513,6 @@ static int __init omap_kp_probe(struct platform_device *pdev)
                col_gpios = pdata->col_gpios;
        }
 
-       if (cpu_is_omap24xx() && omap_has_menelaus()) {
-               row_gpios[5] = 0;
-               col_gpios[2] = 15;
-               col_gpios[6] = 18;
-       }
-
        omap_kp->rows = pdata->rows;
        omap_kp->cols = pdata->cols;
 
@@ -313,8 +521,8 @@ static int __init omap_kp_probe(struct platform_device *pdev)
                for (i = 0; i < omap_kp->cols; i++) {
                        if (omap_request_gpio(col_gpios[i]) < 0) {
                                printk(KERN_ERR "Failed to request"
-          "GPIO%d for keypad\n",
-          col_gpios[i]);
+                                      "GPIO%d for keypad\n",
+                                      col_gpios[i]);
                                return -EINVAL;
                        }
                        omap_set_gpio_direction(col_gpios[i], 0);
@@ -323,40 +531,43 @@ static int __init omap_kp_probe(struct platform_device *pdev)
                for (i = 0; i < omap_kp->rows; i++) {
                        if (omap_request_gpio(row_gpios[i]) < 0) {
                                printk(KERN_ERR "Failed to request"
-          "GPIO%d for keypad\n",
-          row_gpios[i]);
+                                      "GPIO%d for keypad\n",
+                                      row_gpios[i]);
                                return -EINVAL;
                        }
                        omap_set_gpio_direction(row_gpios[i], 1);
                }
        }
+       prevJoypadKeycodePressEmulated          = 0;
 
        init_timer(&omap_kp->timer);
        omap_kp->timer.function = omap_kp_timer;
-       omap_kp->timer.data = (unsigned long) omap_kp;
+       omap_kp->timer.data = (unsigned long)omap_kp;
 
        /* get the irq and init timer*/
        tasklet_enable(&kp_tasklet);
        kp_tasklet.data = (unsigned long) omap_kp;
 
        omap_kp->irq = platform_get_irq(pdev, 0);
-       if (omap_kp->irq) {
+       if (omap_kp->irq >= 0) {
                if (request_irq(omap_kp->irq, omap_kp_interrupt, 0,
                                "omap-keypad", omap_kp) < 0)
                        return -EINVAL;
        }
 
+       device_create_file(&pdev->dev, &dev_attr_enable);
+
        /* setup input device */
        set_bit(EV_KEY, input_dev->evbit);
        for (i = 0; keymap[i] != 0; i++)
-               set_bit(keymap[i] & 0x00ffffff, input_dev->keybit);
+               set_bit(keymap[i] & KEY_MAX, input_dev->keybit);
        input_dev->name = "omap-keypad";
        input_dev->cdev.dev = &pdev->dev;
        input_dev->private = omap_kp;
        input_register_device(omap_kp->input);
 
        if (machine_is_omap_h2() || machine_is_omap_h3() ||
-           machine_is_omap_perseus2()) {
+           machine_is_omap_perseus2() || machine_is_omap_h6300()) {
                omap_writew(0xff, OMAP_MPUIO_BASE + OMAP_MPUIO_GPIO_DEBOUNCING);
        }
        /* scan current status and enable interrupt */
@@ -365,10 +576,9 @@ static int __init omap_kp_probe(struct platform_device *pdev)
                omap_writew(0, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
        } else {
                for (i = 0; i < omap_kp->rows; i++) {
-                       if (request_irq(OMAP_GPIO_IRQ(row_gpios[i]), omap_kp_interrupt, 0,
-                                               "omap-keypad", omap_kp) < 0)
+                       if (request_irq(OMAP_GPIO_IRQ(row_gpios[i]), omap_kp_interrupt,
+                                       SA_TRIGGER_FALLING, "omap-keypad", omap_kp) < 0)
                                return -EINVAL;
-                       set_irq_type(OMAP_GPIO_IRQ(row_gpios[i]), IRQT_FALLING);
                }
        }