[PATCH] drivers/mfd: header included twice
[linux-2.6.git] / drivers / mfd / ucb1x00-ts.c
index 52e0699..79fd062 100644 (file)
@@ -1,7 +1,8 @@
 /*
- *  linux/drivers/mfd/ucb1x00-ts.c
+ *  Touchscreen driver for UCB1x00-based touchscreens
  *
  *  Copyright (C) 2001 Russell King, All Rights Reserved.
+ *  Copyright (C) 2005 Pavel Machek
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
 #include <linux/device.h>
 #include <linux/suspend.h>
 #include <linux/slab.h>
+#include <linux/kthread.h>
 
 #include <asm/dma.h>
 #include <asm/semaphore.h>
+#include <asm/arch/collie.h>
+#include <asm/mach-types.h>
 
 #include "ucb1x00.h"
 
 
 struct ucb1x00_ts {
-       struct input_dev        idev;
+       struct input_dev        *idev;
        struct ucb1x00          *ucb;
 
        wait_queue_head_t       irq_wait;
-       struct semaphore        sem;
-       struct completion       init_exit;
        struct task_struct      *rtask;
-       int                     use_count;
        u16                     x_res;
        u16                     y_res;
 
-       int                     restart:1;
-       int                     adcsync:1;
+       unsigned int            restart:1;
+       unsigned int            adcsync:1;
 };
 
 static int adcsync;
 
 static inline void ucb1x00_ts_evt_add(struct ucb1x00_ts *ts, u16 pressure, u16 x, u16 y)
 {
-       input_report_abs(&ts->idev, ABS_X, x);
-       input_report_abs(&ts->idev, ABS_Y, y);
-       input_report_abs(&ts->idev, ABS_PRESSURE, pressure);
-       input_sync(&ts->idev);
+       struct input_dev *idev = ts->idev;
+       input_report_abs(idev, ABS_X, x);
+       input_report_abs(idev, ABS_Y, y);
+       input_report_abs(idev, ABS_PRESSURE, pressure);
+       input_sync(idev);
 }
 
 static inline void ucb1x00_ts_event_release(struct ucb1x00_ts *ts)
 {
-       input_report_abs(&ts->idev, ABS_PRESSURE, 0);
-       input_sync(&ts->idev);
+       struct input_dev *idev = ts->idev;
+       input_report_abs(idev, ABS_PRESSURE, 0);
+       input_sync(idev);
 }
 
 /*
@@ -86,12 +89,23 @@ static inline void ucb1x00_ts_mode_int(struct ucb1x00_ts *ts)
  */
 static inline unsigned int ucb1x00_ts_read_pressure(struct ucb1x00_ts *ts)
 {
-       ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
-                       UCB_TS_CR_TSMX_POW | UCB_TS_CR_TSPX_POW |
-                       UCB_TS_CR_TSMY_GND | UCB_TS_CR_TSPY_GND |
-                       UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
+       if (machine_is_collie()) {
+               ucb1x00_io_write(ts->ucb, COLLIE_TC35143_GPIO_TBL_CHK, 0);
+               ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
+                                 UCB_TS_CR_TSPX_POW | UCB_TS_CR_TSMX_POW |
+                                 UCB_TS_CR_MODE_POS | UCB_TS_CR_BIAS_ENA);
 
-       return ucb1x00_adc_read(ts->ucb, UCB_ADC_INP_TSPY, ts->adcsync);
+               udelay(55);
+
+               return ucb1x00_adc_read(ts->ucb, UCB_ADC_INP_AD2, ts->adcsync);
+       } else {
+               ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
+                                 UCB_TS_CR_TSMX_POW | UCB_TS_CR_TSPX_POW |
+                                 UCB_TS_CR_TSMY_GND | UCB_TS_CR_TSPY_GND |
+                                 UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
+
+               return ucb1x00_adc_read(ts->ucb, UCB_ADC_INP_TSPY, ts->adcsync);
+       }
 }
 
 /*
@@ -102,12 +116,16 @@ static inline unsigned int ucb1x00_ts_read_pressure(struct ucb1x00_ts *ts)
  */
 static inline unsigned int ucb1x00_ts_read_xpos(struct ucb1x00_ts *ts)
 {
-       ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
-                       UCB_TS_CR_TSMX_GND | UCB_TS_CR_TSPX_POW |
-                       UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
-       ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
-                       UCB_TS_CR_TSMX_GND | UCB_TS_CR_TSPX_POW |
-                       UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
+       if (machine_is_collie())
+               ucb1x00_io_write(ts->ucb, 0, COLLIE_TC35143_GPIO_TBL_CHK);
+       else {
+               ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
+                                 UCB_TS_CR_TSMX_GND | UCB_TS_CR_TSPX_POW |
+                                 UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
+               ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
+                                 UCB_TS_CR_TSMX_GND | UCB_TS_CR_TSPX_POW |
+                                 UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
+       }
        ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
                        UCB_TS_CR_TSMX_GND | UCB_TS_CR_TSPX_POW |
                        UCB_TS_CR_MODE_POS | UCB_TS_CR_BIAS_ENA);
@@ -125,12 +143,17 @@ static inline unsigned int ucb1x00_ts_read_xpos(struct ucb1x00_ts *ts)
  */
 static inline unsigned int ucb1x00_ts_read_ypos(struct ucb1x00_ts *ts)
 {
-       ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
-                       UCB_TS_CR_TSMY_GND | UCB_TS_CR_TSPY_POW |
-                       UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
-       ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
-                       UCB_TS_CR_TSMY_GND | UCB_TS_CR_TSPY_POW |
-                       UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
+       if (machine_is_collie())
+               ucb1x00_io_write(ts->ucb, 0, COLLIE_TC35143_GPIO_TBL_CHK);
+       else {
+               ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
+                                 UCB_TS_CR_TSMY_GND | UCB_TS_CR_TSPY_POW |
+                                 UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
+               ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
+                                 UCB_TS_CR_TSMY_GND | UCB_TS_CR_TSPY_POW |
+                                 UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
+       }
+
        ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
                        UCB_TS_CR_TSMY_GND | UCB_TS_CR_TSPY_POW |
                        UCB_TS_CR_MODE_POS | UCB_TS_CR_BIAS_ENA);
@@ -164,6 +187,15 @@ static inline unsigned int ucb1x00_ts_read_yres(struct ucb1x00_ts *ts)
        return ucb1x00_adc_read(ts->ucb, 0, ts->adcsync);
 }
 
+static inline int ucb1x00_ts_pen_down(struct ucb1x00_ts *ts)
+{
+       unsigned int val = ucb1x00_reg_read(ts->ucb, UCB_TS_CR);
+       if (machine_is_collie())
+               return (!(val & (UCB_TS_CR_TSPX_LOW)));
+       else
+               return (val & (UCB_TS_CR_TSPX_LOW | UCB_TS_CR_TSMX_LOW));
+}
+
 /*
  * This is a RT kernel thread that handles the ADC accesses
  * (mainly so we can use semaphores in the UCB1200 core code
@@ -176,12 +208,6 @@ static int ucb1x00_thread(void *_ts)
        DECLARE_WAITQUEUE(wait, tsk);
        int valid;
 
-       ts->rtask = tsk;
-
-       daemonize("ktsd");
-       /* only want to receive SIGKILL */
-       allow_signal(SIGKILL);
-
        /*
         * We could run as a real-time thread.  However, thus far
         * this doesn't seem to be necessary.
@@ -189,13 +215,11 @@ static int ucb1x00_thread(void *_ts)
 //     tsk->policy = SCHED_FIFO;
 //     tsk->rt_priority = 1;
 
-       complete(&ts->init_exit);
-
        valid = 0;
 
        add_wait_queue(&ts->irq_wait, &wait);
-       for (;;) {
-               unsigned int x, y, p, val;
+       while (!kthread_should_stop()) {
+               unsigned int x, y, p;
                signed long timeout;
 
                ts->restart = 0;
@@ -212,18 +236,15 @@ static int ucb1x00_thread(void *_ts)
                ucb1x00_ts_mode_int(ts);
                ucb1x00_adc_disable(ts->ucb);
 
-               set_task_state(tsk, TASK_UNINTERRUPTIBLE);
-               schedule_timeout(HZ / 100);
-               if (signal_pending(tsk))
-                       break;
+               msleep(10);
 
                ucb1x00_enable(ts->ucb);
-               val = ucb1x00_reg_read(ts->ucb, UCB_TS_CR);
 
-               if (val & (UCB_TS_CR_TSPX_LOW | UCB_TS_CR_TSMX_LOW)) {
+
+               if (ucb1x00_ts_pen_down(ts)) {
                        set_task_state(tsk, TASK_INTERRUPTIBLE);
 
-                       ucb1x00_enable_irq(ts->ucb, UCB_IRQ_TSPX, UCB_FALLING);
+                       ucb1x00_enable_irq(ts->ucb, UCB_IRQ_TSPX, machine_is_collie() ? UCB_RISING : UCB_FALLING);
                        ucb1x00_disable(ts->ucb);
 
                        /*
@@ -256,14 +277,12 @@ static int ucb1x00_thread(void *_ts)
                try_to_freeze();
 
                schedule_timeout(timeout);
-               if (signal_pending(tsk))
-                       break;
        }
 
        remove_wait_queue(&ts->irq_wait, &wait);
 
        ts->rtask = NULL;
-       complete_and_exit(&ts->init_exit, 0);
+       return 0;
 }
 
 /*
@@ -279,17 +298,10 @@ static void ucb1x00_ts_irq(int idx, void *id)
 
 static int ucb1x00_ts_open(struct input_dev *idev)
 {
-       struct ucb1x00_ts *ts = (struct ucb1x00_ts *)idev;
+       struct ucb1x00_ts *ts = idev->private;
        int ret = 0;
 
-       if (down_interruptible(&ts->sem))
-               return -EINTR;
-
-       if (ts->use_count++ != 0)
-               goto out;
-
-       if (ts->rtask)
-               panic("ucb1x00: rtask running?");
+       BUG_ON(ts->rtask);
 
        init_waitqueue_head(&ts->irq_wait);
        ret = ucb1x00_hook_irq(ts->ucb, UCB_IRQ_TSPX, ucb1x00_ts_irq, ts);
@@ -305,19 +317,16 @@ static int ucb1x00_ts_open(struct input_dev *idev)
        ts->y_res = ucb1x00_ts_read_yres(ts);
        ucb1x00_adc_disable(ts->ucb);
 
-       init_completion(&ts->init_exit);
-       ret = kernel_thread(ucb1x00_thread, ts, CLONE_KERNEL);
-       if (ret >= 0) {
-               wait_for_completion(&ts->init_exit);
+       ts->rtask = kthread_run(ucb1x00_thread, ts, "ktsd");
+       if (!IS_ERR(ts->rtask)) {
                ret = 0;
        } else {
                ucb1x00_free_irq(ts->ucb, UCB_IRQ_TSPX, ts);
+               ts->rtask = NULL;
+               ret = -EFAULT;
        }
 
  out:
-       if (ret)
-               ts->use_count--;
-       up(&ts->sem);
        return ret;
 }
 
@@ -326,21 +335,15 @@ static int ucb1x00_ts_open(struct input_dev *idev)
  */
 static void ucb1x00_ts_close(struct input_dev *idev)
 {
-       struct ucb1x00_ts *ts = (struct ucb1x00_ts *)idev;
+       struct ucb1x00_ts *ts = idev->private;
 
-       down(&ts->sem);
-       if (--ts->use_count == 0) {
-               if (ts->rtask) {
-                       send_sig(SIGKILL, ts->rtask, 1);
-                       wait_for_completion(&ts->init_exit);
-               }
+       if (ts->rtask)
+               kthread_stop(ts->rtask);
 
-               ucb1x00_enable(ts->ucb);
-               ucb1x00_free_irq(ts->ucb, UCB_IRQ_TSPX, ts);
-               ucb1x00_reg_write(ts->ucb, UCB_TS_CR, 0);
-               ucb1x00_disable(ts->ucb);
-       }
-       up(&ts->sem);
+       ucb1x00_enable(ts->ucb);
+       ucb1x00_free_irq(ts->ucb, UCB_IRQ_TSPX, ts);
+       ucb1x00_reg_write(ts->ucb, UCB_TS_CR, 0);
+       ucb1x00_disable(ts->ucb);
 }
 
 #ifdef CONFIG_PM
@@ -371,27 +374,31 @@ static int ucb1x00_ts_add(struct ucb1x00_dev *dev)
 {
        struct ucb1x00_ts *ts;
 
-       ts = kmalloc(sizeof(struct ucb1x00_ts), GFP_KERNEL);
+       ts = kzalloc(sizeof(struct ucb1x00_ts), GFP_KERNEL);
        if (!ts)
                return -ENOMEM;
 
-       memset(ts, 0, sizeof(struct ucb1x00_ts));
+       ts->idev = input_allocate_device();
+       if (!ts->idev) {
+               kfree(ts);
+               return -ENOMEM;
+       }
 
        ts->ucb = dev->ucb;
        ts->adcsync = adcsync ? UCB_SYNC : UCB_NOSYNC;
-       init_MUTEX(&ts->sem);
 
-       ts->idev.name       = "Touchscreen panel";
-       ts->idev.id.product = ts->ucb->id;
-       ts->idev.open       = ucb1x00_ts_open;
-       ts->idev.close      = ucb1x00_ts_close;
+       ts->idev->private = ts;
+       ts->idev->name       = "Touchscreen panel";
+       ts->idev->id.product = ts->ucb->id;
+       ts->idev->open       = ucb1x00_ts_open;
+       ts->idev->close      = ucb1x00_ts_close;
 
-       __set_bit(EV_ABS, ts->idev.evbit);
-       __set_bit(ABS_X, ts->idev.absbit);
-       __set_bit(ABS_Y, ts->idev.absbit);
-       __set_bit(ABS_PRESSURE, ts->idev.absbit);
+       __set_bit(EV_ABS, ts->idev->evbit);
+       __set_bit(ABS_X, ts->idev->absbit);
+       __set_bit(ABS_Y, ts->idev->absbit);
+       __set_bit(ABS_PRESSURE, ts->idev->absbit);
 
-       input_register_device(&ts->idev);
+       input_register_device(ts->idev);
 
        dev->priv = ts;
 
@@ -401,7 +408,8 @@ static int ucb1x00_ts_add(struct ucb1x00_dev *dev)
 static void ucb1x00_ts_remove(struct ucb1x00_dev *dev)
 {
        struct ucb1x00_ts *ts = dev->priv;
-       input_unregister_device(&ts->idev);
+
+       input_unregister_device(ts->idev);
        kfree(ts);
 }