Merge git://git.kernel.org/pub/scm/linux/kernel/git/paulus/powerpc-merge
[powerpc.git] / drivers / input / keyboard / corgikbd.c
index 43d6edb..e301ee4 100644 (file)
  */
 
 #include <linux/delay.h>
-#include <linux/device.h>
+#include <linux/platform_device.h>
 #include <linux/init.h>
 #include <linux/input.h>
 #include <linux/interrupt.h>
 #include <linux/jiffies.h>
 #include <linux/module.h>
 #include <linux/slab.h>
-#include <asm/irq.h>
 
 #include <asm/arch/corgi.h>
 #include <asm/arch/hardware.h>
@@ -33,7 +32,6 @@
 /* zero code, 124 scancodes + 3 hinge combinations */
 #define        NR_SCANCODES            ( SCANCODE(KB_ROWS-1,KB_COLS-1) +1 +1 +3 )
 #define SCAN_INTERVAL          (HZ/10)
-#define CORGIKBD_PRESSED       1
 
 #define HINGE_SCAN_INTERVAL            (HZ/4)
 
@@ -71,12 +69,9 @@ static unsigned char corgikbd_keycode[NR_SCANCODES] = {
 
 struct corgikbd {
        unsigned char keycode[ARRAY_SIZE(corgikbd_keycode)];
-       struct input_dev input;
-       char phys[32];
+       struct input_dev *input;
 
-       unsigned char state[ARRAY_SIZE(corgikbd_keycode)];
        spinlock_t lock;
-
        struct timer_list timer;
        struct timer_list htimer;
 
@@ -84,22 +79,6 @@ struct corgikbd {
        unsigned long suspend_jiffies;
 };
 
-static void handle_scancode(unsigned int pressed,unsigned int scancode, struct corgikbd *corgikbd_data)
-{
-       if (pressed && !(corgikbd_data->state[scancode] & CORGIKBD_PRESSED)) {
-               corgikbd_data->state[scancode] |= CORGIKBD_PRESSED;
-               input_report_key(&corgikbd_data->input, corgikbd_data->keycode[scancode], 1);
-               if ((corgikbd_data->keycode[scancode] == CORGI_KEY_OFF)
-                               && time_after(jiffies, corgikbd_data->suspend_jiffies + HZ)) {
-                       input_event(&corgikbd_data->input, EV_PWR, CORGI_KEY_OFF, 1);
-                       corgikbd_data->suspend_jiffies=jiffies;
-               }
-       } else if (!pressed && corgikbd_data->state[scancode] & CORGIKBD_PRESSED) {
-               corgikbd_data->state[scancode] &= ~CORGIKBD_PRESSED;
-               input_report_key(&corgikbd_data->input, corgikbd_data->keycode[scancode], 0);
-       }
-}
-
 #define KB_DISCHARGE_DELAY     10
 #define KB_ACTIVATE_DELAY      10
 
@@ -112,36 +91,36 @@ static void handle_scancode(unsigned int pressed,unsigned int scancode, struct c
  */
 static inline void corgikbd_discharge_all(void)
 {
-       // STROBE All HiZ
+       /* STROBE All HiZ */
        GPCR2  = CORGI_GPIO_ALL_STROBE_BIT;
        GPDR2 &= ~CORGI_GPIO_ALL_STROBE_BIT;
 }
 
 static inline void corgikbd_activate_all(void)
 {
-       // STROBE ALL -> High
+       /* STROBE ALL -> High */
        GPSR2  = CORGI_GPIO_ALL_STROBE_BIT;
        GPDR2 |= CORGI_GPIO_ALL_STROBE_BIT;
 
        udelay(KB_DISCHARGE_DELAY);
 
-       // Clear any interrupts we may have triggered when altering the GPIO lines
+       /* Clear any interrupts we may have triggered when altering the GPIO lines */
        GEDR1 = CORGI_GPIO_HIGH_SENSE_BIT;
        GEDR2 = CORGI_GPIO_LOW_SENSE_BIT;
 }
 
 static inline void corgikbd_activate_col(int col)
 {
-       // STROBE col -> High, not col -> HiZ
+       /* STROBE col -> High, not col -> HiZ */
        GPSR2 = CORGI_GPIO_STROBE_BIT(col);
        GPDR2 = (GPDR2 & ~CORGI_GPIO_ALL_STROBE_BIT) | CORGI_GPIO_STROBE_BIT(col);
 }
 
 static inline void corgikbd_reset_col(int col)
 {
-       // STROBE col -> Low
+       /* STROBE col -> Low */
        GPCR2 = CORGI_GPIO_STROBE_BIT(col);
-       // STROBE col -> out, not col -> HiZ
+       /* STROBE col -> out, not col -> HiZ */
        GPDR2 = (GPDR2 & ~CORGI_GPIO_ALL_STROBE_BIT) | CORGI_GPIO_STROBE_BIT(col);
 }
 
@@ -156,7 +135,7 @@ static inline void corgikbd_reset_col(int col)
 /* Scan the hardware keyboard and push any changes up through the input layer */
 static void corgikbd_scankeyboard(struct corgikbd *corgikbd_data, struct pt_regs *regs)
 {
-       unsigned int row, col, rowd, scancode;
+       unsigned int row, col, rowd;
        unsigned long flags;
        unsigned int num_pressed;
 
@@ -166,7 +145,7 @@ static void corgikbd_scankeyboard(struct corgikbd *corgikbd_data, struct pt_regs
        spin_lock_irqsave(&corgikbd_data->lock, flags);
 
        if (regs)
-               input_regs(&corgikbd_data->input, regs);
+               input_regs(corgikbd_data->input, regs);
 
        num_pressed = 0;
        for (col = 0; col < KB_COLS; col++) {
@@ -183,17 +162,28 @@ static void corgikbd_scankeyboard(struct corgikbd *corgikbd_data, struct pt_regs
 
                rowd = GET_ROWS_STATUS(col);
                for (row = 0; row < KB_ROWS; row++) {
+                       unsigned int scancode, pressed;
+
                        scancode = SCANCODE(row, col);
-                       handle_scancode((rowd & KB_ROWMASK(row)), scancode, corgikbd_data);
-                       if (rowd & KB_ROWMASK(row))
+                       pressed = rowd & KB_ROWMASK(row);
+
+                       input_report_key(corgikbd_data->input, corgikbd_data->keycode[scancode], pressed);
+
+                       if (pressed)
                                num_pressed++;
+
+                       if (pressed && (corgikbd_data->keycode[scancode] == CORGI_KEY_OFF)
+                                       && time_after(jiffies, corgikbd_data->suspend_jiffies + HZ)) {
+                               input_event(corgikbd_data->input, EV_PWR, CORGI_KEY_OFF, 1);
+                               corgikbd_data->suspend_jiffies=jiffies;
+                       }
                }
                corgikbd_reset_col(col);
        }
 
        corgikbd_activate_all();
 
-       input_sync(&corgikbd_data->input);
+       input_sync(corgikbd_data->input);
 
        /* if any keys are pressed, enable the timer */
        if (num_pressed)
@@ -231,8 +221,11 @@ static void corgikbd_timer_callback(unsigned long data)
  * The hinge switches generate no interrupt so they need to be
  * monitored by a timer.
  *
- * When we detect changes, we debounce it and then pass the three
- * positions the system can take as keypresses to the input system.
+ * We debounce the switches and pass them to the input system.
+ *
+ *  gprr == 0x00 - Keyboard with Landscape Screen
+ *          0x08 - No Keyboard with Portrait Screen
+ *          0x0c - Keyboard and Screen Closed
  */
 
 #define HINGE_STABLE_COUNT 2
@@ -254,10 +247,9 @@ static void corgikbd_hinge_timer(unsigned long data)
                if (hinge_count >= HINGE_STABLE_COUNT) {
                        spin_lock_irqsave(&corgikbd_data->lock, flags);
 
-                       handle_scancode((sharpsl_hinge_state == 0x00), 125, corgikbd_data); /* Keyboard with Landscape Screen */
-                       handle_scancode((sharpsl_hinge_state == 0x08), 126, corgikbd_data); /* No Keyboard with Portrait Screen */
-                       handle_scancode((sharpsl_hinge_state == 0x0c), 127, corgikbd_data); /* Keyboard and Screen Closed  */
-                       input_sync(&corgikbd_data->input);
+                       input_report_switch(corgikbd_data->input, SW_0, ((sharpsl_hinge_state & CORGI_SCP_SWA) != 0));
+                       input_report_switch(corgikbd_data->input, SW_1, ((sharpsl_hinge_state & CORGI_SCP_SWB) != 0));
+                       input_sync(corgikbd_data->input);
 
                        spin_unlock_irqrestore(&corgikbd_data->lock, flags);
                }
@@ -266,24 +258,22 @@ static void corgikbd_hinge_timer(unsigned long data)
 }
 
 #ifdef CONFIG_PM
-static int corgikbd_suspend(struct device *dev, pm_message_t state, uint32_t level)
+static int corgikbd_suspend(struct platform_device *dev, pm_message_t state)
 {
-       if (level == SUSPEND_POWER_DOWN) {
-               struct corgikbd *corgikbd = dev_get_drvdata(dev);
-               corgikbd->suspended = 1;
-       }
+       struct corgikbd *corgikbd = platform_get_drvdata(dev);
+       corgikbd->suspended = 1;
+
        return 0;
 }
 
-static int corgikbd_resume(struct device *dev, uint32_t level)
+static int corgikbd_resume(struct platform_device *dev)
 {
-       if (level == RESUME_POWER_ON) {
-               struct corgikbd *corgikbd = dev_get_drvdata(dev);
+       struct corgikbd *corgikbd = platform_get_drvdata(dev);
+
+       /* Upon resume, ignore the suspend key for a short while */
+       corgikbd->suspend_jiffies=jiffies;
+       corgikbd->suspended = 0;
 
-               /* Upon resume, ignore the suspend key for a short while */
-               corgikbd->suspend_jiffies=jiffies;
-               corgikbd->suspended = 0;
-       }
        return 0;
 }
 #else
@@ -291,18 +281,23 @@ static int corgikbd_resume(struct device *dev, uint32_t level)
 #define corgikbd_resume                NULL
 #endif
 
-static int __init corgikbd_probe(struct device *dev)
+static int __init corgikbd_probe(struct platform_device *pdev)
 {
-       int i;
        struct corgikbd *corgikbd;
+       struct input_dev *input_dev;
+       int i;
 
        corgikbd = kzalloc(sizeof(struct corgikbd), GFP_KERNEL);
-       if (!corgikbd)
+       input_dev = input_allocate_device();
+       if (!corgikbd || !input_dev) {
+               kfree(corgikbd);
+               input_free_device(input_dev);
                return -ENOMEM;
+       }
 
-       dev_set_drvdata(dev,corgikbd);
-       strcpy(corgikbd->phys, "corgikbd/input0");
+       platform_set_drvdata(pdev, corgikbd);
 
+       corgikbd->input = input_dev;
        spin_lock_init(&corgikbd->lock);
 
        /* Init Keyboard rescan timer */
@@ -317,51 +312,52 @@ static int __init corgikbd_probe(struct device *dev)
 
        corgikbd->suspend_jiffies=jiffies;
 
-       init_input_dev(&corgikbd->input);
-       corgikbd->input.private = corgikbd;
-       corgikbd->input.name = "Corgi Keyboard";
-       corgikbd->input.dev = dev;
-       corgikbd->input.phys = corgikbd->phys;
-       corgikbd->input.id.bustype = BUS_HOST;
-       corgikbd->input.id.vendor = 0x0001;
-       corgikbd->input.id.product = 0x0001;
-       corgikbd->input.id.version = 0x0100;
-       corgikbd->input.evbit[0] = BIT(EV_KEY) | BIT(EV_REP) | BIT(EV_PWR);
-       corgikbd->input.keycode = corgikbd->keycode;
-       corgikbd->input.keycodesize = sizeof(unsigned char);
-       corgikbd->input.keycodemax = ARRAY_SIZE(corgikbd_keycode);
-
        memcpy(corgikbd->keycode, corgikbd_keycode, sizeof(corgikbd->keycode));
+
+       input_dev->name = "Corgi Keyboard";
+       input_dev->phys = "corgikbd/input0";
+       input_dev->id.bustype = BUS_HOST;
+       input_dev->id.vendor = 0x0001;
+       input_dev->id.product = 0x0001;
+       input_dev->id.version = 0x0100;
+       input_dev->cdev.dev = &pdev->dev;
+       input_dev->private = corgikbd;
+
+       input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP) | BIT(EV_PWR) | BIT(EV_SW);
+       input_dev->keycode = corgikbd->keycode;
+       input_dev->keycodesize = sizeof(unsigned char);
+       input_dev->keycodemax = ARRAY_SIZE(corgikbd_keycode);
+
        for (i = 0; i < ARRAY_SIZE(corgikbd_keycode); i++)
-               set_bit(corgikbd->keycode[i], corgikbd->input.keybit);
-       clear_bit(0, corgikbd->input.keybit);
+               set_bit(corgikbd->keycode[i], input_dev->keybit);
+       clear_bit(0, input_dev->keybit);
+       set_bit(SW_0, input_dev->swbit);
+       set_bit(SW_1, input_dev->swbit);
+
+       input_register_device(corgikbd->input);
 
-       input_register_device(&corgikbd->input);
        mod_timer(&corgikbd->htimer, jiffies + HINGE_SCAN_INTERVAL);
 
        /* Setup sense interrupts - RisingEdge Detect, sense lines as inputs */
        for (i = 0; i < CORGI_KEY_SENSE_NUM; i++) {
                pxa_gpio_mode(CORGI_GPIO_KEY_SENSE(i) | GPIO_IN);
                if (request_irq(CORGI_IRQ_GPIO_KEY_SENSE(i), corgikbd_interrupt,
-                                               SA_INTERRUPT, "corgikbd", corgikbd))
+                               SA_INTERRUPT | SA_TRIGGER_RISING,
+                               "corgikbd", corgikbd))
                        printk(KERN_WARNING "corgikbd: Can't get IRQ: %d!\n", i);
-               else
-                       set_irq_type(CORGI_IRQ_GPIO_KEY_SENSE(i),IRQT_RISING);
        }
 
        /* Set Strobe lines as outputs - set high */
        for (i = 0; i < CORGI_KEY_STROBE_NUM; i++)
                pxa_gpio_mode(CORGI_GPIO_KEY_STROBE(i) | GPIO_OUT | GPIO_DFLT_HIGH);
 
-       printk(KERN_INFO "input: Corgi Keyboard Registered\n");
-
        return 0;
 }
 
-static int corgikbd_remove(struct device *dev)
+static int corgikbd_remove(struct platform_device *pdev)
 {
        int i;
-       struct corgikbd *corgikbd = dev_get_drvdata(dev);
+       struct corgikbd *corgikbd = platform_get_drvdata(pdev);
 
        for (i = 0; i < CORGI_KEY_SENSE_NUM; i++)
                free_irq(CORGI_IRQ_GPIO_KEY_SENSE(i), corgikbd);
@@ -369,30 +365,31 @@ static int corgikbd_remove(struct device *dev)
        del_timer_sync(&corgikbd->htimer);
        del_timer_sync(&corgikbd->timer);
 
-       input_unregister_device(&corgikbd->input);
+       input_unregister_device(corgikbd->input);
 
        kfree(corgikbd);
 
        return 0;
 }
 
-static struct device_driver corgikbd_driver = {
-       .name           = "corgi-keyboard",
-       .bus            = &platform_bus_type,
+static struct platform_driver corgikbd_driver = {
        .probe          = corgikbd_probe,
        .remove         = corgikbd_remove,
        .suspend        = corgikbd_suspend,
        .resume         = corgikbd_resume,
+       .driver         = {
+               .name   = "corgi-keyboard",
+       },
 };
 
 static int __devinit corgikbd_init(void)
 {
-       return driver_register(&corgikbd_driver);
+       return platform_driver_register(&corgikbd_driver);
 }
 
 static void __exit corgikbd_exit(void)
 {
-       driver_unregister(&corgikbd_driver);
+       platform_driver_unregister(&corgikbd_driver);
 }
 
 module_init(corgikbd_init);