]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/input/touchscreen/ads7846.c
ads7846: filtering based on pressure value
[linux-2.6-omap-h63xx.git] / drivers / input / touchscreen / ads7846.c
index b45a45ca7cc961c6152cb702cde8ae768b8b156e..71fbcc4c58b6e122f7f793a7065995e078055e99 100644 (file)
@@ -2,6 +2,7 @@
  * ADS7846 based touchscreen and sensor driver
  *
  * Copyright (c) 2005 David Brownell
+ * Copyright (c) 2006 Imre Deak <imre.deak@nokia.com>
  *
  * Using code from:
  *  - corgi_ts.c
 
 
 /*
- * This code has been lightly tested on an ads7846.
+ * This code has been heavily tested on a Nokia 770, and lightly
+ * tested on an other ads7846 device.
  * Support for ads7843 and ads7845 has only been stubbed in.
  *
- * Not yet done:  investigate the values reported.  Are x/y/pressure
- * event values sane enough for X11?  How accurate are the temperature
- * and voltage readings?  (System-specific calibration should support
+ * Not yet done:  How accurate are the temperature and voltage
+ * readings? (System-specific calibration should support
  * accuracy of 0.3 degrees C; otherwise it's 2.0 degrees.)
  *
+ * IRQ handling needs a workaround because of a shortcoming in handling
+ * edge triggered IRQs on some platforms like the OMAP1/2. These
+ * platforms don't handle the ARM lazy IRQ disabling properly, thus we
+ * have to maintain our own SW IRQ disabled status. This should be
+ * removed as soon as the affected platforms' IRQ handling is fixed.
+ *
  * app note sbaa036 talks in more detail about accurate sampling...
  * that ought to help in situations like LCDs inducing noise (which
  * can also be helped by using synch signals) and more generally.
+ * This driver tries to utilize the measures described in the app
+ * note. The strength of filtering can be set in the board-* specific
+ * files.
  */
 
 #define        TS_POLL_PERIOD  msecs_to_jiffies(10)
 
+/* this driver doesn't aim at the peak continuous sample rate */
+#define        SAMPLE_BITS     (8 /*cmd*/ + 16 /*sample*/ + 2 /* before, after */)
+
 struct ts_event {
        /* For portability, we can't read 12 bit values using SPI (which
         * would make the controller deliver them as native byteorder u16
-        * with msbs zeroed).  Instead, we read them as two 8-byte values,
+        * with msbs zeroed).  Instead, we read them as two 8-bit values,
         * which need byteswapping then range adjustment.
         */
        __be16 x;
@@ -60,18 +73,30 @@ struct ts_event {
 };
 
 struct ads7846 {
-       struct input_dev        input;
+       struct input_dev        *input;
        char                    phys[32];
 
        struct spi_device       *spi;
        u16                     model;
        u16                     vref_delay_usecs;
        u16                     x_plate_ohms;
+       u16                     pressure_max;
 
+       u8                      read_x, read_y, read_z1, read_z2, pwrdown;
+       u16                     dummy;          /* for the pwrdown read */
        struct ts_event         tc;
+       u16                     last_x;
+       u16                     last_y;
+       u16                     last_pressure;
 
-       struct spi_transfer     xfer[8];
-       struct spi_message      msg;
+       struct spi_transfer     xfer[10];
+       struct spi_message      msg[5];
+       int                     msg_idx;
+       int                     read_cnt;
+       int                     last_read;
+
+       u16                     debounce_max;
+       u16                     debounce_tol;
 
        spinlock_t              lock;
        struct timer_list       timer;          /* P: lock */
@@ -79,6 +104,7 @@ struct ads7846 {
        unsigned                pending:1;      /* P: lock */
 // FIXME remove "irq_disabled"
        unsigned                irq_disabled:1; /* P: lock */
+       unsigned                disabled:1;
 };
 
 /* leave chip selected when we're done, for quicker re-select? */
@@ -117,10 +143,12 @@ struct ads7846 {
 #define        READ_12BIT_DFR(x) (ADS_START | ADS_A2A1A0_d_ ## x \
        | ADS_12_BIT | ADS_DFR)
 
-static const u8        read_y  = READ_12BIT_DFR(y)  | ADS_PD10_ADC_ON;
-static const u8        read_z1 = READ_12BIT_DFR(z1) | ADS_PD10_ADC_ON;
-static const u8        read_z2 = READ_12BIT_DFR(z2) | ADS_PD10_ADC_ON;
-static const u8        read_x  = READ_12BIT_DFR(x)  | ADS_PD10_PDOWN;  /* LAST */
+#define        READ_Y  (READ_12BIT_DFR(y)  | ADS_PD10_ADC_ON)
+#define        READ_Z1 (READ_12BIT_DFR(z1) | ADS_PD10_ADC_ON)
+#define        READ_Z2 (READ_12BIT_DFR(z2) | ADS_PD10_ADC_ON)
+
+#define        READ_X  (READ_12BIT_DFR(x)  | ADS_PD10_ADC_ON)
+#define        PWRDOWN (READ_12BIT_DFR(y)  | ADS_PD10_PDOWN)   /* LAST */
 
 /* single-ended samples need to first power up reference voltage;
  * we leave both ADC and VREF powered
@@ -128,8 +156,8 @@ static const u8     read_x  = READ_12BIT_DFR(x)  | ADS_PD10_PDOWN;  /* LAST */
 #define        READ_12BIT_SER(x) (ADS_START | ADS_A2A1A0_ ## x \
        | ADS_12_BIT | ADS_SER)
 
-static const u8        ref_on = READ_12BIT_DFR(x) | ADS_PD10_ALL_ON;
-static const u8        ref_off = READ_12BIT_DFR(y) | ADS_PD10_PDOWN;
+#define        REF_ON  (READ_12BIT_DFR(x) | ADS_PD10_ALL_ON)
+#define        REF_OFF (READ_12BIT_DFR(y) | ADS_PD10_PDOWN)
 
 /*--------------------------------------------------------------------------*/
 
@@ -138,13 +166,18 @@ static const u8   ref_off = READ_12BIT_DFR(y) | ADS_PD10_PDOWN;
  */
 
 struct ser_req {
+       u8                      ref_on;
        u8                      command;
+       u8                      ref_off;
        u16                     scratch;
        __be16                  sample;
        struct spi_message      msg;
        struct spi_transfer     xfer[6];
 };
 
+static void ads7846_enable(struct ads7846 *ts);
+static void ads7846_disable(struct ads7846 *ts);
+
 static int ads7846_read12_ser(struct device *dev, unsigned command)
 {
        struct spi_device       *spi = to_spi_device(dev);
@@ -152,15 +185,16 @@ static int ads7846_read12_ser(struct device *dev, unsigned command)
        struct ser_req          *req = kzalloc(sizeof *req, SLAB_KERNEL);
        int                     status;
        int                     sample;
-       int                     i;
+       int                     i;
 
        if (!req)
                return -ENOMEM;
 
-       INIT_LIST_HEAD(&req->msg.transfers);
+       spi_message_init(&req->msg);
 
        /* activate reference, so it has time to settle; */
-       req->xfer[0].tx_buf = &ref_on;
+       req->ref_on = REF_ON;
+       req->xfer[0].tx_buf = &req->ref_on;
        req->xfer[0].len = 1;
        req->xfer[1].rx_buf = &req->scratch;
        req->xfer[1].len = 2;
@@ -182,7 +216,8 @@ static int ads7846_read12_ser(struct device *dev, unsigned command)
        /* REVISIT:  take a few more samples, and compare ... */
 
        /* turn off reference */
-       req->xfer[4].tx_buf = &ref_off;
+       req->ref_off = REF_OFF;
+       req->xfer[4].tx_buf = &req->ref_off;
        req->xfer[4].len = 1;
        req->xfer[5].rx_buf = &req->scratch;
        req->xfer[5].len = 2;
@@ -224,6 +259,53 @@ SHOW(temp1)
 SHOW(vaux)
 SHOW(vbatt)
 
+static int is_pen_down(struct device *dev)
+{
+       struct ads7846          *ts = dev_get_drvdata(dev);
+
+       return ts->pendown;
+}
+
+static ssize_t ads7846_pen_down_show(struct device *dev,
+                                    struct device_attribute *attr, char *buf)
+{
+       return sprintf(buf, "%u\n", is_pen_down(dev));
+}
+
+static DEVICE_ATTR(pen_down, S_IRUGO, ads7846_pen_down_show, NULL);
+
+static ssize_t ads7846_disable_show(struct device *dev,
+                                    struct device_attribute *attr, char *buf)
+{
+       struct ads7846  *ts = dev_get_drvdata(dev);
+
+       return sprintf(buf, "%u\n", ts->disabled);
+}
+
+static ssize_t ads7846_disable_store(struct device *dev,
+                                    struct device_attribute *attr,
+                                    const char *buf, size_t count)
+{
+       struct ads7846 *ts = dev_get_drvdata(dev);
+       unsigned long flags;
+       char *endp;
+       int i;
+
+       i = simple_strtoul(buf, &endp, 10);
+       spin_lock_irqsave(&ts->lock, flags);
+
+       if (i)
+               ads7846_disable(ts);
+       else
+               ads7846_enable(ts);
+
+       spin_unlock_irqrestore(&ts->lock, flags);
+
+       return count;
+}
+
+static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
+
 /*--------------------------------------------------------------------------*/
 
 /*
@@ -236,19 +318,20 @@ SHOW(vbatt)
 
 static void ads7846_rx(void *ads)
 {
-       struct ads7846  *ts = ads;
-       unsigned        Rt;
-       unsigned        sync = 0;
-       u16             x, y, z1, z2;
-       unsigned long   flags;
+       struct ads7846          *ts = ads;
+       struct input_dev        *input_dev = ts->input;
+       unsigned                Rt;
+       unsigned                sync = 0;
+       u16                     x, y, z1, z2;
+       unsigned long           flags;
 
        /* adjust:  12 bit samples (left aligned), built from
         * two 8 bit values writen msb-first.
         */
-       x = be16_to_cpu(ts->tc.x) >> 4;
-       y = be16_to_cpu(ts->tc.y) >> 4;
-       z1 = be16_to_cpu(ts->tc.z1) >> 4;
-       z2 = be16_to_cpu(ts->tc.z2) >> 4;
+       x = ts->tc.x >> 3;
+       y = ts->tc.y >> 3;
+       z1 = ts->tc.z1 >> 3;
+       z2 = ts->tc.z2 >> 3;
 
        /* range filtering */
        if (x == MAX_12BIT)
@@ -265,6 +348,18 @@ static void ads7846_rx(void *ads)
        } else
                Rt = 0;
 
+       if (Rt > ts->pressure_max) {
+               if (ts->last_pressure) {
+                       x = ts->last_x;
+                       y = ts->last_y;
+               }
+               Rt = ts->pressure_max;
+       }
+
+       ts->last_x = x;
+       ts->last_y = y;
+       ts->last_pressure = Rt;
+
        /* NOTE:  "pendown" is inferred from pressure; we don't rely on
         * being able to check nPENIRQ status, or "friendly" trigger modes
         * (both-edges is much better than just-falling or low-level).
@@ -276,21 +371,21 @@ static void ads7846_rx(void *ads)
         * won't notice that, even if nPENIRQ never fires ...
         */
        if (!ts->pendown && Rt != 0) {
-               input_report_key(&ts->input, BTN_TOUCH, 1);
+               input_report_key(input_dev, BTN_TOUCH, 1);
                sync = 1;
        } else if (ts->pendown && Rt == 0) {
-               input_report_key(&ts->input, BTN_TOUCH, 0);
+               input_report_key(input_dev, BTN_TOUCH, 0);
                sync = 1;
        }
 
        if (Rt) {
-               input_report_abs(&ts->input, ABS_X, x);
-               input_report_abs(&ts->input, ABS_Y, y);
-               input_report_abs(&ts->input, ABS_PRESSURE, Rt);
+               input_report_abs(input_dev, ABS_X, x);
+               input_report_abs(input_dev, ABS_Y, y);
+               input_report_abs(input_dev, ABS_PRESSURE, Rt);
                sync = 1;
        }
        if (sync)
-               input_sync(&ts->input);
+               input_sync(input_dev);
 
 #ifdef VERBOSE
        if (Rt || ts->pendown)
@@ -316,44 +411,78 @@ static void ads7846_rx(void *ads)
        spin_unlock_irqrestore(&ts->lock, flags);
 }
 
+static void ads7846_debounce(void *ads)
+{
+       struct ads7846          *ts = ads;
+       struct spi_message      *m;
+       struct spi_transfer     *t;
+       u16                     val;
+       int                     status;
+
+       m = &ts->msg[ts->msg_idx];
+       t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
+       val = (*(u16 *)t->rx_buf) >> 3;
+
+       if (!ts->read_cnt || (abs(ts->last_read - val) > ts->debounce_tol &&
+                             ts->read_cnt < ts->debounce_max)) {
+               /* Repeat it, if this was the first read or the read wasn't
+                * consistent enough */
+               ts->read_cnt++;
+               ts->last_read = val;
+       } else {
+               /* Go for the next read */
+               ts->msg_idx++;
+               ts->read_cnt = 0;
+               m++;
+       }
+       status = spi_async(ts->spi, m);
+       if (status)
+               dev_err(&ts->spi->dev, "spi_async --> %d\n",
+                               status);
+}
+
 static void ads7846_timer(unsigned long handle)
 {
        struct ads7846  *ts = (void *)handle;
        int             status = 0;
-       unsigned long   flags;
+
+       ts->msg_idx = 0;
+       status = spi_async(ts->spi, &ts->msg[0]);
+       if (status)
+               dev_err(&ts->spi->dev, "spi_async --> %d\n", status);
+}
+
+static irqreturn_t ads7846_irq(int irq, void *handle, struct pt_regs *regs)
+{
+       struct ads7846 *ts = handle;
+       unsigned long flags;
 
        spin_lock_irqsave(&ts->lock, flags);
-       if (!ts->pending) {
-               ts->pending = 1;
+       if (likely(!ts->irq_disabled && !ts->disabled)) {
                if (!ts->irq_disabled) {
                        ts->irq_disabled = 1;
+                       /* The following has at the moment no effect whatsoever
+                        * on OMAP, that's why we maintain the disabled
+                        * state ourselves */
                        disable_irq(ts->spi->irq);
                }
-               status = spi_async(ts->spi, &ts->msg);
-               if (status)
-                       dev_err(&ts->spi->dev, "spi_async --> %d\n",
-                                       status);
+               if (!ts->pending) {
+                       ts->pending = 1;
+                       mod_timer(&ts->timer, jiffies);
+               }
        }
        spin_unlock_irqrestore(&ts->lock, flags);
-}
 
-static irqreturn_t ads7846_irq(int irq, void *handle, struct pt_regs *regs)
-{
-       ads7846_timer((unsigned long) handle);
        return IRQ_HANDLED;
 }
 
 /*--------------------------------------------------------------------------*/
 
-static int
-ads7846_suspend(struct spi_device *spi, pm_message_t message)
+/* Must be called with ts->lock held */
+static void ads7846_disable(struct ads7846 *ts)
 {
-       struct ads7846 *ts = dev_get_drvdata(&spi->dev);
-       unsigned long   flags;
-
-       spin_lock_irqsave(&ts->lock, flags);
-
-       spi->dev.power.power_state = message;
+       if (ts->disabled)
+               return;
 
        /* are we waiting for IRQ, or polling? */
        if (!ts->pendown) {
@@ -362,6 +491,8 @@ ads7846_suspend(struct spi_device *spi, pm_message_t message)
                        disable_irq(ts->spi->irq);
                }
        } else {
+               unsigned long flags;
+
                /* polling; force a final SPI completion;
                 * that will clean things up neatly
                 */
@@ -370,7 +501,7 @@ ads7846_suspend(struct spi_device *spi, pm_message_t message)
 
                while (ts->pendown || ts->pending) {
                        spin_unlock_irqrestore(&ts->lock, flags);
-                       udelay(10);
+                       msleep(1);
                        spin_lock_irqsave(&ts->lock, flags);
                }
        }
@@ -379,26 +510,59 @@ ads7846_suspend(struct spi_device *spi, pm_message_t message)
         * leave it that way after every request
         */
 
+       ts->disabled = 1;
+}
+
+/* Must be called with ts->lock held */
+static void ads7846_enable(struct ads7846 *ts)
+{
+       if (!ts->disabled)
+               return;
+
+       ts->disabled = 0;
+       ts->irq_disabled = 0;
+       enable_irq(ts->spi->irq);
+}
+
+static int ads7846_suspend(struct spi_device *spi, pm_message_t message)
+{
+       struct ads7846 *ts = dev_get_drvdata(&spi->dev);
+       unsigned long   flags;
+
+       spin_lock_irqsave(&ts->lock, flags);
+
+       spi->dev.power.power_state = message;
+       ads7846_disable(ts);
+
        spin_unlock_irqrestore(&ts->lock, flags);
+
        return 0;
+
 }
 
 static int ads7846_resume(struct spi_device *spi)
 {
        struct ads7846 *ts = dev_get_drvdata(&spi->dev);
+       unsigned long flags;
+
+       spin_lock_irqsave(&ts->lock, flags);
 
-       ts->irq_disabled = 0;
-       enable_irq(ts->spi->irq);
        spi->dev.power.power_state = PMSG_ON;
+       ads7846_enable(ts);
+
+       spin_unlock_irqrestore(&ts->lock, flags);
+
        return 0;
 }
 
 static int __devinit ads7846_probe(struct spi_device *spi)
 {
        struct ads7846                  *ts;
+       struct input_dev                *input_dev;
        struct ads7846_platform_data    *pdata = spi->dev.platform_data;
+       struct spi_message              *m;
        struct spi_transfer             *x;
-       int                             i;
+       int                             err;
 
        if (!spi->irq) {
                dev_dbg(&spi->dev, "no IRQ?\n");
@@ -411,9 +575,9 @@ static int __devinit ads7846_probe(struct spi_device *spi)
        }
 
        /* don't exceed max specified sample rate */
-       if (spi->max_speed_hz > (125000 * 16)) {
+       if (spi->max_speed_hz > (125000 * SAMPLE_BITS)) {
                dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
-                               (spi->max_speed_hz/16)/1000);
+                               (spi->max_speed_hz/SAMPLE_BITS)/1000);
                return -EINVAL;
        }
 
@@ -423,96 +587,153 @@ static int __devinit ads7846_probe(struct spi_device *spi)
         * to discard the four garbage LSBs.
         */
 
-       if (!(ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL)))
-               return -ENOMEM;
+       ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
+       input_dev = input_allocate_device();
+       if (!ts || !input_dev) {
+               err = -ENOMEM;
+               goto err_free_mem;
+       }
 
        dev_set_drvdata(&spi->dev, ts);
+       spi->dev.power.power_state = PMSG_ON;
 
        ts->spi = spi;
-       spi->dev.power.power_state = PMSG_ON;
+       ts->input = input_dev;
 
        init_timer(&ts->timer);
        ts->timer.data = (unsigned long) ts;
        ts->timer.function = ads7846_timer;
 
+       spin_lock_init(&ts->lock);
+
        ts->model = pdata->model ? : 7846;
        ts->vref_delay_usecs = pdata->vref_delay_usecs ? : 100;
        ts->x_plate_ohms = pdata->x_plate_ohms ? : 400;
+       ts->pressure_max = pdata->pressure_max ? : ~0;
+       ts->debounce_max = pdata->debounce_max ? : 1;
+       ts->debounce_tol = pdata->debounce_tol ? : 10;
 
-       init_input_dev(&ts->input);
+       snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
 
-       ts->input.dev = &spi->dev;
-       ts->input.name = "ADS784x Touchscreen";
-       snprintf(ts->phys, sizeof ts->phys, "%s/input0", spi->dev.bus_id);
-       ts->input.phys = ts->phys;
+       input_dev->name = "ADS784x Touchscreen";
+       input_dev->phys = ts->phys;
+       input_dev->cdev.dev = &spi->dev;
 
-       ts->input.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
-       ts->input.keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
-       input_set_abs_params(&ts->input, ABS_X,
+       input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
+       input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
+       input_set_abs_params(input_dev, ABS_X,
                        pdata->x_min ? : 0,
                        pdata->x_max ? : MAX_12BIT,
                        0, 0);
-       input_set_abs_params(&ts->input, ABS_Y,
+       input_set_abs_params(input_dev, ABS_Y,
                        pdata->y_min ? : 0,
                        pdata->y_max ? : MAX_12BIT,
                        0, 0);
-       input_set_abs_params(&ts->input, ABS_PRESSURE,
+       input_set_abs_params(input_dev, ABS_PRESSURE,
                        pdata->pressure_min, pdata->pressure_max, 0, 0);
 
-       input_register_device(&ts->input);
-
        /* set up the transfers to read touchscreen state; this assumes we
         * use formula #2 for pressure, not #3.
         */
+       m = &ts->msg[0];
        x = ts->xfer;
 
+       spi_message_init(m);
+
        /* y- still on; turn on only y+ (and ADC) */
-       x->tx_buf = &read_y;
+       ts->read_y = READ_Y;
+       x->tx_buf = &ts->read_y;
        x->len = 1;
+       spi_message_add_tail(x, m);
+
        x++;
        x->rx_buf = &ts->tc.y;
        x->len = 2;
+       spi_message_add_tail(x, m);
+
+       m->complete = ads7846_debounce;
+       m->context = ts;
+
+       m++;
+       spi_message_init(m);
+
+       /* turn y- off, x+ on, then leave in lowpower */
+       x++;
+       ts->read_x = READ_X;
+       x->tx_buf = &ts->read_x;
+       x->len = 1;
+       spi_message_add_tail(x, m);
+
        x++;
+       x->rx_buf = &ts->tc.x;
+       x->len = 2;
+       spi_message_add_tail(x, m);
+
+       m->complete = ads7846_debounce;
+       m->context = ts;
 
        /* turn y+ off, x- on; we'll use formula #2 */
        if (ts->model == 7846) {
-               x->tx_buf = &read_z1;
+               m++;
+               spi_message_init(m);
+
+               x++;
+               ts->read_z1 = READ_Z1;
+               x->tx_buf = &ts->read_z1;
                x->len = 1;
+               spi_message_add_tail(x, m);
+
                x++;
                x->rx_buf = &ts->tc.z1;
                x->len = 2;
-               x++;
+               spi_message_add_tail(x, m);
+
+               m->complete = ads7846_debounce;
+               m->context = ts;
 
-               x->tx_buf = &read_z2;
+               m++;
+               spi_message_init(m);
+
+               x++;
+               ts->read_z2 = READ_Z2;
+               x->tx_buf = &ts->read_z2;
                x->len = 1;
+               spi_message_add_tail(x, m);
+
                x++;
                x->rx_buf = &ts->tc.z2;
                x->len = 2;
-               x++;
+               spi_message_add_tail(x, m);
+
+               m->complete = ads7846_debounce;
+               m->context = ts;
        }
 
-       /* turn y- off, x+ on, then leave in lowpower */
-       x->tx_buf = &read_x;
+       /* power down */
+       m++;
+       spi_message_init(m);
+
+       x++;
+       ts->pwrdown = PWRDOWN;
+       x->tx_buf = &ts->pwrdown;
        x->len = 1;
+       spi_message_add_tail(x, m);
+
        x++;
-       x->rx_buf = &ts->tc.x;
+       x->rx_buf = &ts->dummy;
        x->len = 2;
-       x++;
-
-       CS_CHANGE(x[-1]);
+       CS_CHANGE(*x);
+       spi_message_add_tail(x, m);
 
-       for (i = 0; i < x - ts->xfer; i++)
-               spi_message_add_tail(&ts->xfer[i], &ts->msg);
-       ts->msg.complete = ads7846_rx;
-       ts->msg.context = ts;
+       m->complete = ads7846_rx;
+       m->context = ts;
 
        if (request_irq(spi->irq, ads7846_irq,
                        SA_SAMPLE_RANDOM | SA_TRIGGER_FALLING,
                        spi->dev.bus_id, ts)) {
                dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
-               input_unregister_device(&ts->input);
-               kfree(ts);
-               return -EBUSY;
+               err = -EBUSY;
+               goto err_free_mem;
        }
 
        dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
@@ -534,27 +755,56 @@ static int __devinit ads7846_probe(struct spi_device *spi)
                device_create_file(&spi->dev, &dev_attr_vbatt);
        device_create_file(&spi->dev, &dev_attr_vaux);
 
+       device_create_file(&spi->dev, &dev_attr_pen_down);
+
+       device_create_file(&spi->dev, &dev_attr_disable);
+
+       err = input_register_device(input_dev);
+       if (err)
+               goto err_remove_attr;
+
        return 0;
+
+ err_remove_attr:
+       device_remove_file(&spi->dev, &dev_attr_disable);
+       device_remove_file(&spi->dev, &dev_attr_pen_down);
+       if (ts->model == 7846) {
+               device_remove_file(&spi->dev, &dev_attr_temp1);
+               device_remove_file(&spi->dev, &dev_attr_temp0);
+       }
+       if (ts->model != 7845)
+               device_remove_file(&spi->dev, &dev_attr_vbatt);
+       device_remove_file(&spi->dev, &dev_attr_vaux);
+
+       free_irq(spi->irq, ts);
+ err_free_mem:
+       input_free_device(input_dev);
+       kfree(ts);
+       return err;
 }
 
 static int __devexit ads7846_remove(struct spi_device *spi)
 {
        struct ads7846          *ts = dev_get_drvdata(&spi->dev);
 
+       input_unregister_device(ts->input);
+
        ads7846_suspend(spi, PMSG_SUSPEND);
-       free_irq(ts->spi->irq, ts);
-       if (ts->irq_disabled)
-               enable_irq(ts->spi->irq);
 
+       device_remove_file(&spi->dev, &dev_attr_disable);
+       device_remove_file(&spi->dev, &dev_attr_pen_down);
        if (ts->model == 7846) {
-               device_remove_file(&spi->dev, &dev_attr_temp0);
                device_remove_file(&spi->dev, &dev_attr_temp1);
+               device_remove_file(&spi->dev, &dev_attr_temp0);
        }
        if (ts->model != 7845)
                device_remove_file(&spi->dev, &dev_attr_vbatt);
        device_remove_file(&spi->dev, &dev_attr_vaux);
 
-       input_unregister_device(&ts->input);
+       free_irq(ts->spi->irq, ts);
+       if (ts->irq_disabled)
+               enable_irq(ts->spi->irq);
+
        kfree(ts);
 
        dev_dbg(&spi->dev, "unregistered touchscreen\n");