iio: adc: ad_sigma_delta: add disable_one callback
authorDumitru Ceclan <dumitru.ceclan@analog.com>
Fri, 7 Jun 2024 14:53:09 +0000 (17:53 +0300)
committerJonathan Cameron <Jonathan.Cameron@huawei.com>
Tue, 25 Jun 2024 20:04:32 +0000 (21:04 +0100)
Sigma delta ADCs with a sequencer need to disable the previously enabled
channel when reading using ad_sigma_delta_single_conversion(). This was
done manually in drivers for devices with sequencers.

This patch implements handling of single channel disabling after a
single conversion.

Reviewed-by: Nuno Sa <nuno.sa@analog.com>
Signed-off-by: Dumitru Ceclan <dumitru.ceclan@analog.com>
Reviewed-by: David Lechner <dlechner@baylibre.com>
Link: https://patch.msgid.link/20240607-ad4111-v7-3-97e3855900a0@analog.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
drivers/iio/adc/ad7124.c
drivers/iio/adc/ad7173.c
drivers/iio/adc/ad_sigma_delta.c
include/linux/iio/adc/ad_sigma_delta.h

index e7b1d51..3beed78 100644 (file)
@@ -555,10 +555,18 @@ static int ad7124_disable_all(struct ad_sigma_delta *sd)
        return 0;
 }
 
+static int ad7124_disable_one(struct ad_sigma_delta *sd, unsigned int chan)
+{
+       struct ad7124_state *st = container_of(sd, struct ad7124_state, sd);
+
+       return ad7124_spi_write_mask(st, AD7124_CHANNEL(chan), AD7124_CHANNEL_EN_MSK, 0, 2);
+}
+
 static const struct ad_sigma_delta_info ad7124_sigma_delta_info = {
        .set_channel = ad7124_set_channel,
        .append_status = ad7124_append_status,
        .disable_all = ad7124_disable_all,
+       .disable_one = ad7124_disable_one,
        .set_mode = ad7124_set_mode,
        .has_registers = true,
        .addr_shift = 0,
@@ -582,12 +590,6 @@ static int ad7124_read_raw(struct iio_dev *indio_dev,
                if (ret < 0)
                        return ret;
 
-               /* After the conversion is performed, disable the channel */
-               ret = ad_sd_write_reg(&st->sd, AD7124_CHANNEL(chan->address), 2,
-                                     st->channels[chan->address].ain | AD7124_CHANNEL_EN(0));
-               if (ret < 0)
-                       return ret;
-
                return IIO_VAL_INT;
        case IIO_CHAN_INFO_SCALE:
                mutex_lock(&st->cfgs_lock);
index b26d457..428676c 100644 (file)
@@ -569,10 +569,16 @@ static int ad7173_disable_all(struct ad_sigma_delta *sd)
        return 0;
 }
 
+static int ad7173_disable_one(struct ad_sigma_delta *sd, unsigned int chan)
+{
+       return ad_sd_write_reg(sd, AD7173_REG_CH(chan), 2, 0);
+}
+
 static struct ad_sigma_delta_info ad7173_sigma_delta_info = {
        .set_channel = ad7173_set_channel,
        .append_status = ad7173_append_status,
        .disable_all = ad7173_disable_all,
+       .disable_one = ad7173_disable_one,
        .set_mode = ad7173_set_mode,
        .has_registers = true,
        .addr_shift = 0,
@@ -668,11 +674,6 @@ static int ad7173_read_raw(struct iio_dev *indio_dev,
                if (ret < 0)
                        return ret;
 
-               /* disable channel after single conversion */
-               ret = ad_sd_write_reg(&st->sd, AD7173_REG_CH(chan->address), 2, 0);
-               if (ret < 0)
-                       return ret;
-
                return IIO_VAL_INT;
        case IIO_CHAN_INFO_SCALE:
                if (chan->type == IIO_TEMP) {
index a2b87f6..8c062b0 100644 (file)
@@ -321,6 +321,7 @@ out:
 
        sigma_delta->keep_cs_asserted = false;
        ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
+       ad_sigma_delta_disable_one(sigma_delta, chan->address);
        sigma_delta->bus_locked = false;
        spi_bus_unlock(sigma_delta->spi->controller);
        iio_device_release_direct_mode(indio_dev);
index 383614e..f8c1d25 100644 (file)
@@ -37,6 +37,10 @@ struct iio_dev;
  * @append_status: Will be called to enable status append at the end of the sample, may be NULL.
  * @set_mode: Will be called to select the current mode, may be NULL.
  * @disable_all: Will be called to disable all channels, may be NULL.
+ * @disable_one: Will be called to disable a single channel after
+ *             ad_sigma_delta_single_conversion(), may be NULL.
+ *             Usage of this callback expects iio_chan_spec.address to contain
+ *             the value required for the driver to identify the channel.
  * @postprocess_sample: Is called for each sampled data word, can be used to
  *             modify or drop the sample data, it, may be NULL.
  * @has_registers: true if the device has writable and readable registers, false
@@ -55,6 +59,7 @@ struct ad_sigma_delta_info {
        int (*append_status)(struct ad_sigma_delta *, bool append);
        int (*set_mode)(struct ad_sigma_delta *, enum ad_sigma_delta_mode mode);
        int (*disable_all)(struct ad_sigma_delta *);
+       int (*disable_one)(struct ad_sigma_delta *, unsigned int chan);
        int (*postprocess_sample)(struct ad_sigma_delta *, unsigned int raw_sample);
        bool has_registers;
        unsigned int addr_shift;
@@ -140,6 +145,15 @@ static inline int ad_sigma_delta_disable_all(struct ad_sigma_delta *sd)
        return 0;
 }
 
+static inline int ad_sigma_delta_disable_one(struct ad_sigma_delta *sd,
+                                            unsigned int chan)
+{
+       if (sd->info->disable_one)
+               return sd->info->disable_one(sd, chan);
+
+       return 0;
+}
+
 static inline int ad_sigma_delta_set_mode(struct ad_sigma_delta *sd,
        unsigned int mode)
 {