staging:iio:adc:ad7993 stop reading from buffer for sysfs raw read.
[cascardo/linux.git] / drivers / staging / iio / adc / ad7793.c
index a831b92..4047c5d 100644 (file)
 #include <linux/err.h>
 #include <linux/sched.h>
 #include <linux/delay.h>
+#include <linux/module.h>
 
 #include "../iio.h"
 #include "../sysfs.h"
-#include "../buffer_generic.h"
+#include "../buffer.h"
 #include "../ring_sw.h"
 #include "../trigger.h"
 #include "../trigger_consumer.h"
@@ -315,25 +316,6 @@ out:
        return ret;
 }
 
-static int ad7793_scan_from_ring(struct ad7793_state *st, unsigned ch, int *val)
-{
-       struct iio_buffer *ring = iio_priv_to_dev(st)->buffer;
-       int ret;
-       s64 dat64[2];
-       u32 *dat32 = (u32 *)dat64;
-
-       if (!(test_bit(ch, ring->scan_mask)))
-               return  -EBUSY;
-
-       ret = ring->access->read_last(ring, (u8 *) &dat64);
-       if (ret)
-               return ret;
-
-       *val = *dat32;
-
-       return 0;
-}
-
 static int ad7793_ring_preenable(struct iio_dev *indio_dev)
 {
        struct ad7793_state *st = iio_priv(indio_dev);
@@ -341,14 +323,15 @@ static int ad7793_ring_preenable(struct iio_dev *indio_dev)
        size_t d_size;
        unsigned channel;
 
-       if (!ring->scan_count)
+       if (bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength))
                return -EINVAL;
 
-       channel = find_first_bit(ring->scan_mask,
+       channel = find_first_bit(indio_dev->active_scan_mask,
                                 indio_dev->masklength);
 
-       d_size = ring->scan_count *
-                indio_dev->channels[0].scan_type.storagebits / 8;
+       d_size = bitmap_weight(indio_dev->active_scan_mask,
+                              indio_dev->masklength) *
+               indio_dev->channels[0].scan_type.storagebits / 8;
 
        if (ring->scan_timestamp) {
                d_size += sizeof(s64);
@@ -410,7 +393,7 @@ static irqreturn_t ad7793_trigger_handler(int irq, void *p)
        s64 dat64[2];
        s32 *dat32 = (s32 *)dat64;
 
-       if (ring->scan_count)
+       if (!bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength))
                __ad7793_read_reg(st, 1, 1, AD7793_REG_DATA,
                                  dat32,
                                  indio_dev->channels[0].scan_type.realbits/8);
@@ -458,7 +441,7 @@ static int ad7793_register_ring_funcs_and_init(struct iio_dev *indio_dev)
        }
 
        /* Ring buffer functions - here trigger setup related */
-       indio_dev->buffer->setup_ops = &ad7793_ring_setup_ops;
+       indio_dev->setup_ops = &ad7793_ring_setup_ops;
 
        /* Flag that polled ring buffering is possible */
        indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
@@ -648,8 +631,7 @@ static int ad7793_read_raw(struct iio_dev *indio_dev,
        case 0:
                mutex_lock(&indio_dev->mlock);
                if (iio_buffer_enabled(indio_dev))
-                       ret = ad7793_scan_from_ring(st,
-                                       chan->scan_index, &smpl);
+                       ret = -EBUSY;
                else
                        ret = ad7793_read(st, chan->address,
                                        chan->scan_type.realbits / 8, &smpl);
@@ -666,19 +648,21 @@ static int ad7793_read_raw(struct iio_dev *indio_dev,
 
                return IIO_VAL_INT;
 
-       case (1 << IIO_CHAN_INFO_SCALE_SHARED):
-               *val = st->scale_avail[(st->conf >> 8) & 0x7][0];
-               *val2 = st->scale_avail[(st->conf >> 8) & 0x7][1];
-
-               return IIO_VAL_INT_PLUS_NANO;
-
-       case (1 << IIO_CHAN_INFO_SCALE_SEPARATE):
+       case IIO_CHAN_INFO_SCALE:
                switch (chan->type) {
                case IIO_VOLTAGE:
-                       /* 1170mV / 2^23 * 6 */
-                       scale_uv = (1170ULL * 100000000ULL * 6ULL)
-                               >> (chan->scan_type.realbits -
-                               (unipolar ? 0 : 1));
+                       if (chan->differential) {
+                               *val = st->
+                                       scale_avail[(st->conf >> 8) & 0x7][0];
+                               *val2 = st->
+                                       scale_avail[(st->conf >> 8) & 0x7][1];
+                               return IIO_VAL_INT_PLUS_NANO;
+                       } else {
+                               /* 1170mV / 2^23 * 6 */
+                               scale_uv = (1170ULL * 100000000ULL * 6ULL)
+                                       >> (chan->scan_type.realbits -
+                                           (unipolar ? 0 : 1));
+                       }
                        break;
                case IIO_TEMP:
                        /* Always uses unity gain and internal ref */
@@ -715,7 +699,7 @@ static int ad7793_write_raw(struct iio_dev *indio_dev,
        }
 
        switch (mask) {
-       case (1 << IIO_CHAN_INFO_SCALE_SHARED):
+       case IIO_CHAN_INFO_SCALE:
                ret = -EINVAL;
                for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++)
                        if (val2 == st->scale_avail[i][1]) {
@@ -774,7 +758,7 @@ static const struct ad7793_chip_info ad7793_chip_info_tbl[] = {
                        .channel = 0,
                        .channel2 = 0,
                        .address = AD7793_CH_AIN1P_AIN1M,
-                       .info_mask = (1 << IIO_CHAN_INFO_SCALE_SHARED),
+                       .info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT,
                        .scan_index = 0,
                        .scan_type = IIO_ST('s', 24, 32, 0)
                },
@@ -785,7 +769,7 @@ static const struct ad7793_chip_info ad7793_chip_info_tbl[] = {
                        .channel = 1,
                        .channel2 = 1,
                        .address = AD7793_CH_AIN2P_AIN2M,
-                       .info_mask = (1 << IIO_CHAN_INFO_SCALE_SHARED),
+                       .info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT,
                        .scan_index = 1,
                        .scan_type = IIO_ST('s', 24, 32, 0)
                },
@@ -796,7 +780,7 @@ static const struct ad7793_chip_info ad7793_chip_info_tbl[] = {
                        .channel = 2,
                        .channel2 = 2,
                        .address = AD7793_CH_AIN3P_AIN3M,
-                       .info_mask = (1 << IIO_CHAN_INFO_SCALE_SHARED),
+                       .info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT,
                        .scan_index = 2,
                        .scan_type = IIO_ST('s', 24, 32, 0)
                },
@@ -808,7 +792,7 @@ static const struct ad7793_chip_info ad7793_chip_info_tbl[] = {
                        .channel = 2,
                        .channel2 = 2,
                        .address = AD7793_CH_AIN1M_AIN1M,
-                       .info_mask = (1 << IIO_CHAN_INFO_SCALE_SHARED),
+                       .info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT,
                        .scan_index = 2,
                        .scan_type = IIO_ST('s', 24, 32, 0)
                },
@@ -817,7 +801,7 @@ static const struct ad7793_chip_info ad7793_chip_info_tbl[] = {
                        .indexed = 1,
                        .channel = 0,
                        .address = AD7793_CH_TEMP,
-                       .info_mask = (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
+                       .info_mask = IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
                        .scan_index = 4,
                        .scan_type = IIO_ST('s', 24, 32, 0),
                },
@@ -827,7 +811,7 @@ static const struct ad7793_chip_info ad7793_chip_info_tbl[] = {
                        .indexed = 1,
                        .channel = 4,
                        .address = AD7793_CH_AVDD_MONITOR,
-                       .info_mask = (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
+                       .info_mask = IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
                        .scan_index = 5,
                        .scan_type = IIO_ST('s', 24, 32, 0),
                },
@@ -841,7 +825,7 @@ static const struct ad7793_chip_info ad7793_chip_info_tbl[] = {
                        .channel = 0,
                        .channel2 = 0,
                        .address = AD7793_CH_AIN1P_AIN1M,
-                       .info_mask = (1 << IIO_CHAN_INFO_SCALE_SHARED),
+                       .info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT,
                        .scan_index = 0,
                        .scan_type = IIO_ST('s', 16, 32, 0)
                },
@@ -852,7 +836,7 @@ static const struct ad7793_chip_info ad7793_chip_info_tbl[] = {
                        .channel = 1,
                        .channel2 = 1,
                        .address = AD7793_CH_AIN2P_AIN2M,
-                       .info_mask = (1 << IIO_CHAN_INFO_SCALE_SHARED),
+                       .info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT,
                        .scan_index = 1,
                        .scan_type = IIO_ST('s', 16, 32, 0)
                },
@@ -863,7 +847,7 @@ static const struct ad7793_chip_info ad7793_chip_info_tbl[] = {
                        .channel = 2,
                        .channel2 = 2,
                        .address = AD7793_CH_AIN3P_AIN3M,
-                       .info_mask = (1 << IIO_CHAN_INFO_SCALE_SHARED),
+                       .info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT,
                        .scan_index = 2,
                        .scan_type = IIO_ST('s', 16, 32, 0)
                },
@@ -875,7 +859,7 @@ static const struct ad7793_chip_info ad7793_chip_info_tbl[] = {
                        .channel = 2,
                        .channel2 = 2,
                        .address = AD7793_CH_AIN1M_AIN1M,
-                       .info_mask = (1 << IIO_CHAN_INFO_SCALE_SHARED),
+                       .info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT,
                        .scan_index = 2,
                        .scan_type = IIO_ST('s', 16, 32, 0)
                },
@@ -884,7 +868,7 @@ static const struct ad7793_chip_info ad7793_chip_info_tbl[] = {
                        .indexed = 1,
                        .channel = 0,
                        .address = AD7793_CH_TEMP,
-                       .info_mask = (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
+                       .info_mask = IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
                        .scan_index = 4,
                        .scan_type = IIO_ST('s', 16, 32, 0),
                },
@@ -894,7 +878,7 @@ static const struct ad7793_chip_info ad7793_chip_info_tbl[] = {
                        .indexed = 1,
                        .channel = 4,
                        .address = AD7793_CH_AVDD_MONITOR,
-                       .info_mask = (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
+                       .info_mask = IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
                        .scan_index = 5,
                        .scan_type = IIO_ST('s', 16, 32, 0),
                },
@@ -1033,11 +1017,11 @@ static const struct spi_device_id ad7793_id[] = {
        {"ad7793", ID_AD7793},
        {}
 };
+MODULE_DEVICE_TABLE(spi, ad7793_id);
 
 static struct spi_driver ad7793_driver = {
        .driver = {
                .name   = "ad7793",
-               .bus    = &spi_bus_type,
                .owner  = THIS_MODULE,
        },
        .probe          = ad7793_probe,