]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/input/touchscreen/ads7846.c
[PATCH 3/6] ads7846: replace spin_lock_irqsave
[linux-2.6-omap-h63xx.git] / drivers / input / touchscreen / ads7846.c
index 46d1fec2cfd8952fca752f6e9fe4eb9af496daa0..8200a03ee2d7f7927be9b4c3ff70abeeec30a73f 100644 (file)
@@ -2,6 +2,8 @@
  * ADS7846 based touchscreen and sensor driver
  *
  * Copyright (c) 2005 David Brownell
+ * Copyright (c) 2006 Nokia Corporation
+ * Various changes: Imre Deak <imre.deak@nokia.com>
  *
  * Using code from:
  *  - corgi_ts.c
@@ -15,8 +17,9 @@
  *  it under the terms of the GNU General Public License version 2 as
  *  published by the Free Software Foundation.
  */
-#include <linux/device.h>
+#include <linux/hwmon.h>
 #include <linux/init.h>
+#include <linux/err.h>
 #include <linux/delay.h>
 #include <linux/input.h>
 #include <linux/interrupt.h>
 
 
 /*
- * This code has been lightly tested on an ads7846.
+ * This code has been heavily tested on a Nokia 770, and lightly
+ * tested on other ads7846 devices (OSK/Mistral, Lubbock).
  * 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
- * 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 platform's 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)
@@ -68,15 +76,23 @@ struct ads7846 {
        char                    phys[32];
 
        struct spi_device       *spi;
+       struct class_device     *hwmon;
        u16                     model;
        u16                     vref_delay_usecs;
        u16                     x_plate_ohms;
 
-       u8                      read_x, read_y, read_z1, read_z2;
+       u8                      read_x, read_y, read_z1, read_z2, pwrdown;
+       u16                     dummy;          /* for the pwrdown read */
        struct ts_event         tc;
 
-       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 */
@@ -84,6 +100,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? */
@@ -125,7 +142,9 @@ struct ads7846 {
 #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_PDOWN)   /* LAST */
+
+#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
@@ -140,7 +159,12 @@ struct ads7846 {
 
 /*
  * Non-touchscreen sensors only use single-ended conversions.
+ * The range is GND..vREF. The ads7843 and ads7835 must use external vREF;
+ * ads7846 lets that pin be unconnected, to use internal vREF.
+ *
+ * FIXME make external vREF_mV be a module option, and use that as needed...
  */
+static const unsigned vREF_mV = 2500;
 
 struct ser_req {
        u8                      ref_on;
@@ -152,6 +176,9 @@ struct ser_req {
        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);
@@ -159,50 +186,55 @@ 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                     use_internal;
 
        if (!req)
                return -ENOMEM;
 
-       INIT_LIST_HEAD(&req->msg.transfers);
+       spi_message_init(&req->msg);
 
-       /* activate reference, so it has time to settle; */
-       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;
+       /* FIXME boards with ads7846 might use external vref instead ... */
+       use_internal = (ts->model == 7846);
 
-       /*
-        * for external VREF, 0 usec (and assume it's always on);
-        * for 1uF, use 800 usec;
-        * no cap, 100 usec.
-        */
-       req->xfer[1].delay_usecs = ts->vref_delay_usecs;
+       /* maybe turn on internal vREF, and let it settle */
+       if (use_internal) {
+               req->ref_on = REF_ON;
+               req->xfer[0].tx_buf = &req->ref_on;
+               req->xfer[0].len = 1;
+               spi_message_add_tail(&req->xfer[0], &req->msg);
+
+               req->xfer[1].rx_buf = &req->scratch;
+               req->xfer[1].len = 2;
+
+               /* for 1uF, settle for 800 usec; no cap, 100 usec.  */
+               req->xfer[1].delay_usecs = ts->vref_delay_usecs;
+               spi_message_add_tail(&req->xfer[1], &req->msg);
+       }
 
        /* take sample */
        req->command = (u8) command;
        req->xfer[2].tx_buf = &req->command;
        req->xfer[2].len = 1;
+       spi_message_add_tail(&req->xfer[2], &req->msg);
+
        req->xfer[3].rx_buf = &req->sample;
        req->xfer[3].len = 2;
+       spi_message_add_tail(&req->xfer[3], &req->msg);
 
        /* REVISIT:  take a few more samples, and compare ... */
 
-       /* turn off reference */
-       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;
-
-       CS_CHANGE(req->xfer[5]);
-
-       /* group all the transfers together, so we can't interfere with
-        * reading touchscreen state; disable penirq while sampling
-        */
-       for (i = 0; i < 6; i++)
-               spi_message_add_tail(&req->xfer[i], &req->msg);
+       /* maybe off internal vREF */
+       if (use_internal) {
+               req->ref_off = REF_OFF;
+               req->xfer[4].tx_buf = &req->ref_off;
+               req->xfer[4].len = 1;
+               spi_message_add_tail(&req->xfer[4], &req->msg);
+
+               req->xfer[5].rx_buf = &req->scratch;
+               req->xfer[5].len = 2;
+               CS_CHANGE(req->xfer[5]);
+               spi_message_add_tail(&req->xfer[5], &req->msg);
+       }
 
        disable_irq(spi->irq);
        status = spi_sync(spi, &req->msg);
@@ -210,28 +242,116 @@ static int ads7846_read12_ser(struct device *dev, unsigned command)
 
        if (req->msg.status)
                status = req->msg.status;
+
+       /* on-wire is a must-ignore bit, a BE12 value, then padding */
        sample = be16_to_cpu(req->sample);
-       sample = sample >> 4;
-       kfree(req);
+       sample = sample >> 3;
+       sample &= 0x0fff;
 
+       kfree(req);
        return status ? status : sample;
 }
 
-#define SHOW(name) static ssize_t \
+#define SHOW(name,var,adjust) static ssize_t \
 name ## _show(struct device *dev, struct device_attribute *attr, char *buf) \
 { \
+       struct ads7846 *ts = dev_get_drvdata(dev); \
        ssize_t v = ads7846_read12_ser(dev, \
-                       READ_12BIT_SER(name) | ADS_PD10_ALL_ON); \
+                       READ_12BIT_SER(var) | ADS_PD10_ALL_ON); \
        if (v < 0) \
                return v; \
-       return sprintf(buf, "%u\n", (unsigned) v); \
+       return sprintf(buf, "%u\n", adjust(ts, v)); \
 } \
 static DEVICE_ATTR(name, S_IRUGO, name ## _show, NULL);
 
-SHOW(temp0)
-SHOW(temp1)
-SHOW(vaux)
-SHOW(vbatt)
+
+/* Sysfs conventions report temperatures in millidegrees Celcius.
+ * We could use the low-accuracy two-sample scheme, but can't do the high
+ * accuracy scheme without calibration data.  For now we won't try either;
+ * userspace sees raw sensor values, and must scale appropriately.
+ */
+static inline unsigned null_adjust(struct ads7846 *ts, ssize_t v)
+{
+       return v;
+}
+
+SHOW(temp0, temp0, null_adjust)                // temp1_input
+SHOW(temp1, temp1, null_adjust)                // temp2_input
+
+
+/* sysfs conventions report voltages in millivolts.  We can convert voltages
+ * if we know vREF.  userspace may need to scale vAUX to match the board's
+ * external resistors; we assume that vBATT only uses the internal ones.
+ */
+static inline unsigned vaux_adjust(struct ads7846 *ts, ssize_t v)
+{
+       unsigned retval = v;
+
+       /* external resistors may scale vAUX into 0..vREF */
+       retval *= vREF_mV;
+       retval = retval >> 12;
+       return retval;
+}
+
+static inline unsigned vbatt_adjust(struct ads7846 *ts, ssize_t v)
+{
+       unsigned retval = vaux_adjust(ts, v);
+
+       /* ads7846 has a resistor ladder to scale this signal down */
+       if (ts->model == 7846)
+               retval *= 4;
+       return retval;
+}
+
+SHOW(in0_input, vaux, vaux_adjust)
+SHOW(in1_input, vbatt, vbatt_adjust)
+
+
+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);
+       char *endp;
+       int i;
+
+       i = simple_strtoul(buf, &endp, 10);
+       spin_lock_irq(&ts->lock);
+
+       if (i)
+               ads7846_disable(ts);
+       else
+               ads7846_enable(ts);
+
+       spin_unlock_irq(&ts->lock);
+
+       return count;
+}
+
+static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
 
 /*--------------------------------------------------------------------------*/
 
@@ -252,13 +372,13 @@ static void ads7846_rx(void *ads)
        u16                     x, y, z1, z2;
        unsigned long           flags;
 
-       /* adjust:  12 bit samples (left aligned), built from
-        * two 8 bit values writen msb-first.
+       /* adjust:  on-wire is a must-ignore bit, a BE12 value, then padding;
+        * built from two 8 bit values written 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 = (be16_to_cpu(ts->tc.x) >> 3) & 0x0fff;
+       y = (be16_to_cpu(ts->tc.y) >> 3) & 0x0fff;
+       z1 = (be16_to_cpu(ts->tc.z1) >> 3) & 0x0fff;
+       z2 = (be16_to_cpu(ts->tc.z2) >> 3) & 0x0fff;
 
        /* range filtering */
        if (x == MAX_12BIT)
@@ -326,44 +446,83 @@ 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) {
+                       /* REVISIT irq logic for many ARM chips has cloned a
+                        * bug wherein disabling an irq in its handler won't
+                        * work;(it's disabled lazily, and too late to work.
+                        * until all their irq logic is fixed, we must shadow
+                        * that state here.
+                        */
                        ts->irq_disabled = 1;
+
                        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) {
@@ -379,9 +538,9 @@ ads7846_suspend(struct spi_device *spi, pm_message_t message)
                        mod_timer(&ts->timer, jiffies);
 
                while (ts->pendown || ts->pending) {
-                       spin_unlock_irqrestore(&ts->lock, flags);
-                       udelay(10);
-                       spin_lock_irqsave(&ts->lock, flags);
+                       spin_unlock_irq(&ts->lock);
+                       msleep(1);
+                       spin_lock_irq(&ts->lock);
                }
        }
 
@@ -389,17 +548,46 @@ ads7846_suspend(struct spi_device *spi, pm_message_t message)
         * leave it that way after every request
         */
 
-       spin_unlock_irqrestore(&ts->lock, flags);
+       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);
+
+       spin_lock_irq(&ts->lock);
+
+       spi->dev.power.power_state = message;
+       ads7846_disable(ts);
+
+       spin_unlock_irq(&ts->lock);
+
        return 0;
+
 }
 
 static int ads7846_resume(struct spi_device *spi)
 {
        struct ads7846 *ts = dev_get_drvdata(&spi->dev);
 
-       ts->irq_disabled = 0;
-       enable_irq(ts->spi->irq);
+       spin_lock_irq(&ts->lock);
+
        spi->dev.power.power_state = PMSG_ON;
+       ads7846_enable(ts);
+
+       spin_unlock_irq(&ts->lock);
+
        return 0;
 }
 
@@ -407,7 +595,9 @@ static int __devinit ads7846_probe(struct spi_device *spi)
 {
        struct ads7846                  *ts;
        struct input_dev                *input_dev;
+       struct class_device             *hwmon = ERR_PTR(-ENOMEM);
        struct ads7846_platform_data    *pdata = spi->dev.platform_data;
+       struct spi_message              *m;
        struct spi_transfer             *x;
        int                             err;
 
@@ -431,12 +621,14 @@ static int __devinit ads7846_probe(struct spi_device *spi)
        /* We'd set the wordsize to 12 bits ... except that some controllers
         * will then treat the 8 bit command words as 12 bits (and drop the
         * four MSBs of the 12 bit result).  Result: inputs must be shifted
-        * to discard the four garbage LSBs.
+        * to discard the four garbage LSBs.  (Also, not all controllers can
+        * support 12 bit words.)
         */
 
        ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
        input_dev = input_allocate_device();
-       if (!ts || !input_dev) {
+       hwmon = hwmon_device_register(&spi->dev);
+       if (!ts || !input_dev || IS_ERR(hwmon)) {
                err = -ENOMEM;
                goto err_free_mem;
        }
@@ -446,14 +638,19 @@ static int __devinit ads7846_probe(struct spi_device *spi)
 
        ts->spi = spi;
        ts->input = input_dev;
+       ts->hwmon = hwmon;
 
        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->debounce_max = pdata->debounce_max ? : 1;
+       ts->debounce_tol = pdata->debounce_tol ? : 10;
 
        snprintf(ts->phys, sizeof(ts->phys), "%s/input0", spi->dev.bus_id);
 
@@ -477,60 +674,98 @@ static int __devinit ads7846_probe(struct spi_device *spi)
        /* set up the transfers to read touchscreen state; this assumes we
         * use formula #2 for pressure, not #3.
         */
-       INIT_LIST_HEAD(&ts->msg.transfers);
+       m = &ts->msg[0];
        x = ts->xfer;
 
+       spi_message_init(m);
+
        /* y- still on; turn on only y+ (and ADC) */
        ts->read_y = READ_Y;
        x->tx_buf = &ts->read_y;
        x->len = 1;
-       spi_message_add_tail(x, &ts->msg);
+       spi_message_add_tail(x, m);
 
        x++;
        x->rx_buf = &ts->tc.y;
        x->len = 2;
-       spi_message_add_tail(x, &ts->msg);
+       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) {
+               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, &ts->msg);
+               spi_message_add_tail(x, m);
 
                x++;
                x->rx_buf = &ts->tc.z1;
                x->len = 2;
-               spi_message_add_tail(x, &ts->msg);
+               spi_message_add_tail(x, m);
+
+               m->complete = ads7846_debounce;
+               m->context = ts;
+
+               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, &ts->msg);
+               spi_message_add_tail(x, m);
 
                x++;
                x->rx_buf = &ts->tc.z2;
                x->len = 2;
-               spi_message_add_tail(x, &ts->msg);
+               spi_message_add_tail(x, m);
+
+               m->complete = ads7846_debounce;
+               m->context = ts;
        }
 
-       /* turn y- off, x+ on, then leave in lowpower */
+       /* power down */
+       m++;
+       spi_message_init(m);
+
        x++;
-       ts->read_x = READ_X;
-       x->tx_buf = &ts->read_x;
+       ts->pwrdown = PWRDOWN;
+       x->tx_buf = &ts->pwrdown;
        x->len = 1;
-       spi_message_add_tail(x, &ts->msg);
+       spi_message_add_tail(x, m);
 
        x++;
-       x->rx_buf = &ts->tc.x;
+       x->rx_buf = &ts->dummy;
        x->len = 2;
        CS_CHANGE(*x);
-       spi_message_add_tail(x, &ts->msg);
+       spi_message_add_tail(x, m);
 
-       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,
@@ -540,34 +775,52 @@ static int __devinit ads7846_probe(struct spi_device *spi)
                goto err_free_mem;
        }
 
-       dev_info(&spi->dev, "touchscreen, irq %d\n", spi->irq);
+       dev_info(&spi->dev, "touchscreen + hwmon, irq %d\n", spi->irq);
 
-       /* take a first sample, leaving nPENIRQ active; avoid
+       /* take a first sample, leaving nPENIRQ active and vREF off; avoid
         * the touchscreen, in case it's not connected.
         */
        (void) ads7846_read12_ser(&spi->dev,
                          READ_12BIT_SER(vaux) | ADS_PD10_ALL_ON);
 
        /* ads7843/7845 don't have temperature sensors, and
-        * use the other sensors a bit differently too
+        * use the other ADC lines a bit differently too
         */
        if (ts->model == 7846) {
                device_create_file(&spi->dev, &dev_attr_temp0);
                device_create_file(&spi->dev, &dev_attr_temp1);
        }
+       /* in1 == vBAT (7846), or a non-scaled ADC input */
        if (ts->model != 7845)
-               device_create_file(&spi->dev, &dev_attr_vbatt);
-       device_create_file(&spi->dev, &dev_attr_vaux);
+               device_create_file(&spi->dev, &dev_attr_in1_input);
+       /* in0 == a non-scaled ADC input */
+       device_create_file(&spi->dev, &dev_attr_in0_input);
+
+       /* non-hwmon device attributes */
+       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_free_irq;
+               goto err_remove_attr;
 
        return 0;
 
- err_free_irq:
+ 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_in1_input);
+       device_remove_file(&spi->dev, &dev_attr_in0_input);
+
        free_irq(spi->irq, ts);
  err_free_mem:
+       if (!IS_ERR(hwmon))
+               hwmon_device_unregister(hwmon);
        input_free_device(input_dev);
        kfree(ts);
        return err;
@@ -577,20 +830,25 @@ static int __devexit ads7846_remove(struct spi_device *spi)
 {
        struct ads7846          *ts = dev_get_drvdata(&spi->dev);
 
+       hwmon_device_unregister(ts->hwmon);
+       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);
+               device_remove_file(&spi->dev, &dev_attr_in1_input);
+       device_remove_file(&spi->dev, &dev_attr_in0_input);
+
+       free_irq(ts->spi->irq, ts);
+       if (ts->irq_disabled)
+               enable_irq(ts->spi->irq);
 
-       input_unregister_device(ts->input);
        kfree(ts);
 
        dev_dbg(&spi->dev, "unregistered touchscreen\n");
@@ -638,7 +896,7 @@ static int __init ads7846_init(void)
 
        return spi_register_driver(&ads7846_driver);
 }
-module_init(ads7846_init);
+device_initcall(ads7846_init);
 
 static void __exit ads7846_exit(void)
 {