[PATCH v4 4/4] iio: adc: update ad7779 to use IIO backend

Ioana Risteiu posted 4 patches 1 month, 2 weeks ago
There is a newer version of this series
[PATCH v4 4/4] iio: adc: update ad7779 to use IIO backend
Posted by Ioana Risteiu 1 month, 2 weeks ago
Add a new functionality to ad7779 driver that streams data through data
output interface using IIO backend interface.

Signed-off-by: Ioana Risteiu <Ioana.Risteiu@analog.com>
---
changes in v4:
 - grouped includes in alphabetical order
 - reordered fields in struct ad7779_state
 - modified logic of setting the number of lanes 
 - validating devicetree value
 drivers/iio/adc/ad7779.c | 116 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 115 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/adc/ad7779.c b/drivers/iio/adc/ad7779.c
index f7e681c0e8c0..adca490061c9 100644
--- a/drivers/iio/adc/ad7779.c
+++ b/drivers/iio/adc/ad7779.c
@@ -25,6 +25,7 @@
 #include <linux/units.h>
 
 #include <linux/iio/iio.h>
+#include <linux/iio/backend.h>
 #include <linux/iio/buffer.h>
 #include <linux/iio/sysfs.h>
 #include <linux/iio/trigger.h>
@@ -116,6 +117,12 @@
 #define AD7779_CRC8_POLY			0x07
 DECLARE_CRC8_TABLE(ad7779_crc8_table);
 
+enum ad7779_data_lines {
+	AD7779_1LINE = 1,
+	AD7779_2LINES = 2,
+	AD7779_4LINES = 4,
+};
+
 enum ad7779_filter {
 	AD7779_SINC3,
 	AD7779_SINC5,
@@ -145,6 +152,7 @@ struct ad7779_state {
 	struct completion completion;
 	unsigned int sampling_freq;
 	enum ad7779_filter filter_enabled;
+	struct iio_backend *back;
 	/*
 	 * DMA (thus cache coherency maintenance) requires the
 	 * transfer buffers to live in their own cache lines.
@@ -630,12 +638,38 @@ static int ad7779_reset(struct iio_dev *indio_dev, struct gpio_desc *reset_gpio)
 	return ret;
 }
 
+static int ad7779_update_scan_mode(struct iio_dev *indio_dev,
+				   const unsigned long *scan_mask)
+{
+	struct ad7779_state *st = iio_priv(indio_dev);
+	unsigned int c;
+	int ret;
+
+	for (c = 0; c < AD7779_NUM_CHANNELS; c++) {
+		if (test_bit(c, scan_mask))
+			ret = iio_backend_chan_enable(st->back, c);
+		else
+			ret = iio_backend_chan_disable(st->back, c);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
 static const struct iio_info ad7779_info = {
 	.read_raw = ad7779_read_raw,
 	.write_raw = ad7779_write_raw,
 	.debugfs_reg_access = &ad7779_reg_access,
 };
 
+static const struct iio_info ad7779_info_data = {
+	.read_raw = ad7779_read_raw,
+	.write_raw = ad7779_write_raw,
+	.debugfs_reg_access = &ad7779_reg_access,
+	.update_scan_mode = &ad7779_update_scan_mode,
+};
+
 static const struct iio_enum ad7779_filter_enum = {
 	.items = ad7779_filter_type,
 	.num_items = ARRAY_SIZE(ad7779_filter_type),
@@ -752,6 +786,49 @@ static int ad7779_conf(struct ad7779_state *st, struct gpio_desc *start_gpio)
 	return 0;
 }
 
+static int ad7779_set_data_lines(struct iio_dev *indio_dev,
+				 unsigned int num_lanes)
+{
+	struct ad7779_state *st = iio_priv(indio_dev);
+	int ret = -EINVAL;
+
+	if (num_lanes != AD7779_1LINE &&
+		num_lanes != AD7779_2LINES &&
+		num_lanes != AD7779_4LINES)
+		return ret;
+
+	ret = ad7779_set_sampling_frequency(st, num_lanes * AD7779_DEFAULT_SAMPLING_1LINE);
+	if (ret)
+		return ret;
+
+	ret = iio_backend_num_lanes_set(st->back, num_lanes);
+	if (ret)
+		return ret;
+
+	return ad7779_spi_write_mask(st, AD7779_REG_DOUT_FORMAT,
+				    AD7779_DOUT_FORMAT_MSK,
+				    FIELD_PREP(AD7779_DOUT_FORMAT_MSK, 2 - ilog2(num_lanes)));
+}
+
+static int ad7779_setup_channels(struct iio_dev *indio_dev, const struct ad7779_state *st)
+{
+	struct iio_chan_spec *channels;
+	struct device *dev = &st->spi->dev;
+
+	channels = devm_kmemdup_array(dev, st->chip_info->channels,
+					ARRAY_SIZE(ad7779_channels),
+					sizeof(*channels), GFP_KERNEL);
+	if (!channels)
+		return -ENOMEM;
+
+	for (int i = 0; i < ARRAY_SIZE(ad7779_channels); i++)
+		channels[i].scan_type.endianness = IIO_CPU;
+
+	indio_dev->channels = channels;
+
+	return 0;
+}
+
 static int ad7779_setup_without_backend(struct ad7779_state *st, struct iio_dev *indio_dev)
 {
 	int ret;
@@ -796,6 +873,39 @@ static int ad7779_setup_without_backend(struct ad7779_state *st, struct iio_dev
 				    FIELD_PREP(AD7779_DCLK_CLK_DIV_MSK, 7));
 }
 
+static int ad7779_setup_backend(struct ad7779_state *st, struct iio_dev *indio_dev)
+{
+	struct device *dev = &st->spi->dev;
+	int ret = -EINVAL;
+	int num_lanes;
+
+	indio_dev->info = &ad7779_info_data;
+
+	ret = ad7779_setup_channels(indio_dev, st);
+	if (ret)
+		return ret;
+
+	st->back = devm_iio_backend_get(dev, NULL);
+	if (IS_ERR(st->back))
+		return dev_err_probe(dev, PTR_ERR(st->back),
+				     "failed to get iio backend");
+
+	ret = devm_iio_backend_request_buffer(dev, st->back, indio_dev);
+	if (ret)
+		return ret;
+
+	ret = devm_iio_backend_enable(dev, st->back);
+	if (ret)
+		return ret;
+
+	num_lanes = 4;
+	ret = device_property_read_u32(dev, "adi,num-lanes", &num_lanes);
+	if (ret && ret != -EINVAL)
+		return ret;
+
+	return ad7779_set_data_lines(indio_dev, num_lanes);
+}
+
 static int ad7779_probe(struct spi_device *spi)
 {
 	struct iio_dev *indio_dev;
@@ -848,7 +958,10 @@ static int ad7779_probe(struct spi_device *spi)
 	indio_dev->modes = INDIO_DIRECT_MODE;
 	indio_dev->num_channels = ARRAY_SIZE(ad7779_channels);
 
-	ret = ad7779_setup_without_backend(st, indio_dev);
+	if (device_property_present(dev, "io-backends"))
+		ret = ad7779_setup_backend(st, indio_dev);
+	else
+		ret = ad7779_setup_without_backend(st, indio_dev);
 
 	if (ret)
 		return ret;
@@ -943,3 +1056,4 @@ module_spi_driver(ad7779_driver);
 MODULE_AUTHOR("Ramona Alexandra Nechita <ramona.nechita@analog.com>");
 MODULE_DESCRIPTION("Analog Devices AD7779 ADC");
 MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS("IIO_BACKEND");
-- 
2.47.2
Re: [PATCH v4 4/4] iio: adc: update ad7779 to use IIO backend
Posted by Andy Shevchenko 1 month, 2 weeks ago
On Wed, Aug 20, 2025 at 03:02:45PM +0300, Ioana Risteiu wrote:
> Add a new functionality to ad7779 driver that streams data through data
> output interface using IIO backend interface.

...

> +static int ad7779_set_data_lines(struct iio_dev *indio_dev,
> +				 unsigned int num_lanes)
> +{
> +	struct ad7779_state *st = iio_priv(indio_dev);

> +	int ret = -EINVAL;

In general the split assignment is easier to maintain and less prone to subtle
errors. In this case it's even worse as it's not needed...

> +	if (num_lanes != AD7779_1LINE &&
> +		num_lanes != AD7779_2LINES &&
> +		num_lanes != AD7779_4LINES)
> +		return ret;

...just return the error code directly here.

> +	ret = ad7779_set_sampling_frequency(st, num_lanes * AD7779_DEFAULT_SAMPLING_1LINE);
> +	if (ret)
> +		return ret;
> +
> +	ret = iio_backend_num_lanes_set(st->back, num_lanes);
> +	if (ret)
> +		return ret;
> +
> +	return ad7779_spi_write_mask(st, AD7779_REG_DOUT_FORMAT,
> +				    AD7779_DOUT_FORMAT_MSK,
> +				    FIELD_PREP(AD7779_DOUT_FORMAT_MSK, 2 - ilog2(num_lanes)));
> +}

...

> +static int ad7779_setup_channels(struct iio_dev *indio_dev, const struct ad7779_state *st)
> +{
> +	struct iio_chan_spec *channels;
> +	struct device *dev = &st->spi->dev;
> +
> +	channels = devm_kmemdup_array(dev, st->chip_info->channels,
> +					ARRAY_SIZE(ad7779_channels),
> +					sizeof(*channels), GFP_KERNEL);

Indentation...

> +	if (!channels)
> +		return -ENOMEM;
> +
> +	for (int i = 0; i < ARRAY_SIZE(ad7779_channels); i++)

Why signed iterator?

> +		channels[i].scan_type.endianness = IIO_CPU;
> +
> +	indio_dev->channels = channels;
> +
> +	return 0;
> +}

...

> +static int ad7779_setup_backend(struct ad7779_state *st, struct iio_dev *indio_dev)
> +{
> +	struct device *dev = &st->spi->dev;

> +	int ret = -EINVAL;

Why?!

> +	int num_lanes;

Can it be negatie?

> +	indio_dev->info = &ad7779_info_data;
> +
> +	ret = ad7779_setup_channels(indio_dev, st);
> +	if (ret)
> +		return ret;
> +
> +	st->back = devm_iio_backend_get(dev, NULL);
> +	if (IS_ERR(st->back))
> +		return dev_err_probe(dev, PTR_ERR(st->back),
> +				     "failed to get iio backend");
> +
> +	ret = devm_iio_backend_request_buffer(dev, st->back, indio_dev);
> +	if (ret)
> +		return ret;
> +
> +	ret = devm_iio_backend_enable(dev, st->back);
> +	if (ret)
> +		return ret;
> +
> +	num_lanes = 4;
> +	ret = device_property_read_u32(dev, "adi,num-lanes", &num_lanes);
> +	if (ret && ret != -EINVAL)
> +		return ret;
> +
> +	return ad7779_set_data_lines(indio_dev, num_lanes);
> +}

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH v4 4/4] iio: adc: update ad7779 to use IIO backend
Posted by Jonathan Cameron 1 month, 1 week ago
On Wed, 20 Aug 2025 17:18:05 +0300
Andy Shevchenko <andriy.shevchenko@intel.com> wrote:

> On Wed, Aug 20, 2025 at 03:02:45PM +0300, Ioana Risteiu wrote:
> > Add a new functionality to ad7779 driver that streams data through data
> > output interface using IIO backend interface.  

A few follow ups from me.

Jonathan

> ...
> 
> > +static int ad7779_set_data_lines(struct iio_dev *indio_dev,
> > +				 unsigned int num_lanes)
> > +{
> > +	struct ad7779_state *st = iio_priv(indio_dev);  
> 
> > +	int ret = -EINVAL;  
> 
> In general the split assignment is easier to maintain and less prone to subtle
> errors. In this case it's even worse as it's not needed...
> 
> > +	if (num_lanes != AD7779_1LINE &&
> > +		num_lanes != AD7779_2LINES &&
> > +		num_lanes != AD7779_4LINES)
> > +		return ret;  
> 
> ...just return the error code directly here.

Those enum values in general seem like a bad idea.  Just use numbers.
If the enum was used as a type it would be a different situation but here it
is used as a glorified way to give the 1 lane to the value 1 etc which just
makes it harder to read.


> 
> > +	ret = ad7779_set_sampling_frequency(st, num_lanes * AD7779_DEFAULT_SAMPLING_1LINE);
> > +	if (ret)
> > +		return ret;
> > +
> > +	ret = iio_backend_num_lanes_set(st->back, num_lanes);
> > +	if (ret)
> > +		return ret;
> > +
> > +	return ad7779_spi_write_mask(st, AD7779_REG_DOUT_FORMAT,
> > +				    AD7779_DOUT_FORMAT_MSK,
> > +				    FIELD_PREP(AD7779_DOUT_FORMAT_MSK, 2 - ilog2(num_lanes)));
> > +}  
> 
> ...
> 
> > +static int ad7779_setup_channels(struct iio_dev *indio_dev, const struct ad7779_state *st)
> > +{
> > +	struct iio_chan_spec *channels;
> > +	struct device *dev = &st->spi->dev;
> > +
> > +	channels = devm_kmemdup_array(dev, st->chip_info->channels,
> > +					ARRAY_SIZE(ad7779_channels),
> > +					sizeof(*channels), GFP_KERNEL);  
> 
> Indentation...
> 
> > +	if (!channels)
> > +		return -ENOMEM;
> > +
> > +	for (int i = 0; i < ARRAY_SIZE(ad7779_channels); i++)  
> 
> Why signed iterator?
> 
> > +		channels[i].scan_type.endianness = IIO_CPU;
> > +
> > +	indio_dev->channels = channels;
> > +
> > +	return 0;
> > +}  
> 
> ...
> 
> > +static int ad7779_setup_backend(struct ad7779_state *st, struct iio_dev *indio_dev)
> > +{
> > +	struct device *dev = &st->spi->dev;  
> 
> > +	int ret = -EINVAL;  
> 
> Why?!
> 
> > +	int num_lanes;  
> 
> Can it be negatie?

Good spot.

Even beyond sign it needs to be a u32 given how it is used.

> 
> > +	indio_dev->info = &ad7779_info_data;
> > +
> > +	ret = ad7779_setup_channels(indio_dev, st);
> > +	if (ret)
> > +		return ret;
> > +
> > +	st->back = devm_iio_backend_get(dev, NULL);
> > +	if (IS_ERR(st->back))
> > +		return dev_err_probe(dev, PTR_ERR(st->back),
> > +				     "failed to get iio backend");
> > +
> > +	ret = devm_iio_backend_request_buffer(dev, st->back, indio_dev);
> > +	if (ret)
> > +		return ret;
> > +
> > +	ret = devm_iio_backend_enable(dev, st->back);
> > +	if (ret)
> > +		return ret;
> > +
> > +	num_lanes = 4;
> > +	ret = device_property_read_u32(dev, "adi,num-lanes", &num_lanes);
> > +	if (ret && ret != -EINVAL)
> > +		return ret;
> > +
> > +	return ad7779_set_data_lines(indio_dev, num_lanes);
> > +}  
>