Merge rsync://rsync.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-drm-fsl-dcu.git] / drivers / input / touchscreen / ads7846.c
index 1494175ac6fe43d0666e3cfed2426cfb0e622ed5..c6164b6f476a3ecb5fde27af07ddd05c38557d01 100644 (file)
 
 
 /*
- * This code has been tested on an ads7846 / N770 device.
+ * 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:  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
@@ -79,6 +76,7 @@ struct ads7846 {
        char                    phys[32];
 
        struct spi_device       *spi;
+       struct attribute_group  *attr_group;
        u16                     model;
        u16                     vref_delay_usecs;
        u16                     x_plate_ohms;
@@ -192,7 +190,7 @@ static int ads7846_read12_ser(struct device *dev, unsigned command)
 {
        struct spi_device       *spi = to_spi_device(dev);
        struct ads7846          *ts = dev_get_drvdata(dev);
-       struct ser_req          *req = kzalloc(sizeof *req, SLAB_KERNEL);
+       struct ser_req          *req = kzalloc(sizeof *req, GFP_KERNEL);
        int                     status;
        int                     sample;
        int                     i;
@@ -248,10 +246,13 @@ 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;
 }
 
@@ -317,6 +318,48 @@ static ssize_t ads7846_disable_store(struct device *dev,
 
 static DEVICE_ATTR(disable, 0664, ads7846_disable_show, ads7846_disable_store);
 
+static struct attribute *ads7846_attributes[] = {
+       &dev_attr_temp0.attr,
+       &dev_attr_temp1.attr,
+       &dev_attr_vbatt.attr,
+       &dev_attr_vaux.attr,
+       &dev_attr_pen_down.attr,
+       &dev_attr_disable.attr,
+       NULL,
+};
+
+static struct attribute_group ads7846_attr_group = {
+       .attrs = ads7846_attributes,
+};
+
+/*
+ * ads7843/7845 don't have temperature sensors, and
+ * use the other sensors a bit differently too
+ */
+
+static struct attribute *ads7843_attributes[] = {
+       &dev_attr_vbatt.attr,
+       &dev_attr_vaux.attr,
+       &dev_attr_pen_down.attr,
+       &dev_attr_disable.attr,
+       NULL,
+};
+
+static struct attribute_group ads7843_attr_group = {
+       .attrs = ads7843_attributes,
+};
+
+static struct attribute *ads7845_attributes[] = {
+       &dev_attr_vaux.attr,
+       &dev_attr_pen_down.attr,
+       &dev_attr_disable.attr,
+       NULL,
+};
+
+static struct attribute_group ads7845_attr_group = {
+       .attrs = ads7845_attributes,
+};
+
 /*--------------------------------------------------------------------------*/
 
 /*
@@ -336,13 +379,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)
@@ -420,7 +463,7 @@ static void ads7846_debounce(void *ads)
 
        m = &ts->msg[ts->msg_idx];
        t = list_entry(m->transfers.prev, struct spi_transfer, transfer_list);
-       val = (*(u16 *)t->rx_buf) >> 3;
+       val = (be16_to_cpu(*(__be16 *)t->rx_buf) >> 3) & 0x0fff;
        if (!ts->read_cnt || (abs(ts->last_read - val) > ts->debounce_tol)) {
                /* Repeat it, if this was the first read or the read
                 * wasn't consistent enough. */
@@ -469,7 +512,7 @@ static void ads7846_timer(unsigned long handle)
        spin_lock_irq(&ts->lock);
 
        if (unlikely(ts->msg_idx && !ts->pendown)) {
-               /* measurment cycle ended */
+               /* measurement cycle ended */
                if (!device_suspended(&ts->spi->dev)) {
                        ts->irq_disabled = 0;
                        enable_irq(ts->spi->irq);
@@ -487,7 +530,7 @@ static void ads7846_timer(unsigned long handle)
        spin_unlock_irq(&ts->lock);
 }
 
-static irqreturn_t ads7846_irq(int irq, void *handle, struct pt_regs *regs)
+static irqreturn_t ads7846_irq(int irq, void *handle)
 {
        struct ads7846 *ts = handle;
        unsigned long flags;
@@ -495,11 +538,10 @@ static irqreturn_t ads7846_irq(int irq, void *handle, struct pt_regs *regs)
        spin_lock_irqsave(&ts->lock, flags);
        if (likely(ts->get_pendown_state())) {
                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.
+                       /* The ARM do_simple_IRQ() dispatcher doesn't act
+                        * like the other dispatchers:  it will report IRQs
+                        * even after they've been disabled.  We work around
+                        * that here.  (The "generic irq" framework may help...)
                         */
                        ts->irq_disabled = 1;
                        disable_irq(ts->spi->irq);
@@ -609,16 +651,20 @@ static int __devinit ads7846_probe(struct spi_device *spi)
                return -EINVAL;
        }
 
+       /* REVISIT when the irq can be triggered active-low, or if for some
+        * reason the touchscreen isn't hooked up, we don't need to access
+        * the pendown state.
+        */
        if (pdata->get_pendown_state == NULL) {
                dev_dbg(&spi->dev, "no get_pendown_state function?\n");
                return -EINVAL;
        }
 
-       /* 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.
+       /* We'd set TX wordsize 8 bits and RX wordsize to 13 bits ... except
+        * that even if the hardware can do that, the SPI controller driver
+        * may not.  So we stick to very-portable 8 bit words, both RX and TX.
         */
+       spi->bits_per_word = 8;
 
        ts = kzalloc(sizeof(struct ads7846), GFP_KERNEL);
        input_dev = input_allocate_device();
@@ -770,9 +816,8 @@ static int __devinit ads7846_probe(struct spi_device *spi)
 
        ts->last_msg = m;
 
-       if (request_irq(spi->irq, ads7846_irq,
-                       SA_SAMPLE_RANDOM | SA_TRIGGER_FALLING,
-                       spi->dev.bus_id, ts)) {
+       if (request_irq(spi->irq, ads7846_irq, IRQF_TRIGGER_FALLING,
+                       spi->dev.driver->name, ts)) {
                dev_dbg(&spi->dev, "irq %d busy?\n", spi->irq);
                err = -EBUSY;
                goto err_free_mem;
@@ -786,38 +831,30 @@ static int __devinit ads7846_probe(struct spi_device *spi)
        (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
-        */
-       if (ts->model == 7846) {
-               device_create_file(&spi->dev, &dev_attr_temp0);
-               device_create_file(&spi->dev, &dev_attr_temp1);
+       switch (ts->model) {
+       case 7846:
+               ts->attr_group = &ads7846_attr_group;
+               break;
+       case 7845:
+               ts->attr_group = &ads7845_attr_group;
+               break;
+       default:
+               ts->attr_group = &ads7843_attr_group;
+               break;
        }
-       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_pen_down);
-
-       device_create_file(&spi->dev, &dev_attr_disable);
+       err = sysfs_create_group(&spi->dev.kobj, ts->attr_group);
+       if (err)
+               goto err_free_irq;
 
        err = input_register_device(input_dev);
        if (err)
-               goto err_remove_attr;
+               goto err_remove_attr_group;
 
        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);
-
+ err_remove_attr_group:
+       sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
+ err_free_irq:
        free_irq(spi->irq, ts);
  err_free_mem:
        input_free_device(input_dev);
@@ -833,15 +870,7 @@ static int __devexit ads7846_remove(struct spi_device *spi)
 
        ads7846_suspend(spi, PMSG_SUSPEND);
 
-       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);
+       sysfs_remove_group(&spi->dev.kobj, ts->attr_group);
 
        free_irq(ts->spi->irq, ts);
        /* suspend left the IRQ disabled */