OSDN Git Service

iio: adc: ad7606: Add software configuration
authorBeniamin Bia <beniamin.bia@analog.com>
Mon, 27 May 2019 12:56:48 +0000 (15:56 +0300)
committerJonathan Cameron <Jonathan.Cameron@huawei.com>
Sun, 16 Jun 2019 15:45:22 +0000 (16:45 +0100)
Because this driver will support multiple configurations for software,
the software configuration was made generic.

Signed-off-by: Beniamin Bia <beniamin.bia@analog.com>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
drivers/iio/adc/ad7606.c
drivers/iio/adc/ad7606.h

index c66ff22..aba0fd1 100644 (file)
@@ -140,7 +140,7 @@ static int ad7606_read_raw(struct iio_dev *indio_dev,
                           int *val2,
                           long m)
 {
-       int ret;
+       int ret, ch = 0;
        struct ad7606_state *st = iio_priv(indio_dev);
 
        switch (m) {
@@ -157,8 +157,10 @@ static int ad7606_read_raw(struct iio_dev *indio_dev,
                *val = (short)ret;
                return IIO_VAL_INT;
        case IIO_CHAN_INFO_SCALE:
+               if (st->sw_mode_en)
+                       ch = chan->address;
                *val = 0;
-               *val2 = st->scale_avail[st->range[0]];
+               *val2 = st->scale_avail[st->range[ch]];
                return IIO_VAL_INT_PLUS_MICRO;
        case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
                *val = st->oversampling;
@@ -233,7 +235,9 @@ static int ad7606_write_raw(struct iio_dev *indio_dev,
        case IIO_CHAN_INFO_SCALE:
                mutex_lock(&st->lock);
                i = find_closest(val2, st->scale_avail, st->num_scales);
-               ret = st->write_scale(indio_dev, chan->address, i);
+               if (st->sw_mode_en)
+                       ch = chan->address;
+               ret = st->write_scale(indio_dev, ch, i);
                if (ret < 0) {
                        mutex_unlock(&st->lock);
                        return ret;
@@ -616,6 +620,36 @@ int ad7606_probe(struct device *dev, int irq, void __iomem *base_address,
        st->write_scale = ad7606_write_scale_hw;
        st->write_os = ad7606_write_os_hw;
 
+       if (st->chip_info->sw_mode_config)
+               st->sw_mode_en = device_property_present(st->dev,
+                                                        "adi,sw-mode");
+
+       if (st->sw_mode_en) {
+               /* After reset, in software mode, ±10 V is set by default */
+               memset32(st->range, 2, ARRAY_SIZE(st->range));
+               indio_dev->info = &ad7606_info_os_and_range;
+
+               /*
+                * In software mode, the range gpio has no longer its function.
+                * Instead, the scale can be configured individually for each
+                * channel from the range registers.
+                */
+               if (st->chip_info->write_scale_sw)
+                       st->write_scale = st->chip_info->write_scale_sw;
+
+               /*
+                * In software mode, the oversampling is no longer configured
+                * with GPIO pins. Instead, the oversampling can be configured
+                * in configuratiion register.
+                */
+               if (st->chip_info->write_os_sw)
+                       st->write_os = st->chip_info->write_os_sw;
+
+               ret = st->chip_info->sw_mode_config(indio_dev);
+               if (ret < 0)
+                       return ret;
+       }
+
        st->trig = devm_iio_trigger_alloc(dev, "%s-dev%d",
                                          indio_dev->name, indio_dev->id);
        if (!st->trig)
index 143c301..d8a509c 100644 (file)
@@ -43,6 +43,7 @@ struct ad7606_chip_info {
  * @range              voltage range selection, selects which scale to apply
  * @oversampling       oversampling selection
  * @base_address       address from where to read data in parallel operation
+ * @sw_mode_en         software mode enabled
  * @scale_avail                pointer to the array which stores the available scales
  * @num_scales         number of elements stored in the scale_avail array
  * @oversampling_avail pointer to the array which stores the available
@@ -71,6 +72,7 @@ struct ad7606_state {
        unsigned int                    range[16];
        unsigned int                    oversampling;
        void __iomem                    *base_address;
+       bool                            sw_mode_en;
        const unsigned int              *scale_avail;
        unsigned int                    num_scales;
        const unsigned int              *oversampling_avail;