Skip to contentSkip to conversation

Add SPI offload support to AD4030

PATCH Superseded Marcelo Schmitt 6 participants 18 messages Oct 14, 2025 – Oct 18, 2025

Subsystems (5)

v5 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v5 cover Add SPI offload support to AD4030

Hi,

Thanks for all reviews and suggestions to v4.

Most changes in v5 are improvements to the SPI offload support patch.

Change log v4 -> v5
[PWM]
- Dropped PWM patch.
Since there are no longer changes to the PWM subsystem, I've also dropped the associated addresses from the recipients list.
[DT]
- Dropped leftover adi,pga-value.
[IIO]
- Made Kconfig entry depend on PWM and select other features.
- Reused ad4030_exit_config_mode() in ad4030_offload_buffer_postenable().
- Dropped common-mode voltage support on SPI offload setup.
- Adjusted offload trigger period calculation.
- No longer setting data frame mode from ad4030_set_avg_frame_len().
- Rearranged code to reduce patch diff.
- Dropped leftover adaq4216_hw_gains_db[].
- Tweaked ad4030_get_chan_scale() so val and val2 don't get overwritten.

Note there is also a patch to the SPI subsystem [1] that contains a feature required by AD4030 offload support.

[1]: https://lore.kernel.org/linux-spi/cd315e95c0bd8523f00e91c400abcd6a418e5924.1759760519.git.marcelo.schmitt@analog.com/

Thanks,
Marcelo

Marcelo Schmitt (7):
dt-bindings: iio: adc: adi,ad4030: Reference spi-peripheral-props
Docs: iio: ad4030: Add double PWM SPI offload doc
dt-bindings: iio: adc: adi,ad4030: Add PWM
iio: adc: ad4030: Use BIT macro to improve code readability
iio: adc: ad4030: Add SPI offload support
dt-bindings: iio: adc: adi,ad4030: Add ADAQ4216 and ADAQ4224
iio: adc: ad4030: Add support for ADAQ4216 and ADAQ4224

 .../bindings/iio/adc/adi,ad4030.yaml          |  76 ++-
 Documentation/iio/ad4030.rst                  |  39 ++
 drivers/iio/adc/Kconfig                       |   5 +
 drivers/iio/adc/ad4030.c                      | 636 +++++++++++++++++-
 4 files changed, 733 insertions(+), 23 deletions(-)

base-commit: 4b17a60d1e1c2d9d2ccbd58642f6f4ac2fa364ba

Signature · 1 line
2.39.2
#1 lore raw

PATCH v5 1/7 dt-bindings: iio: adc: adi,ad4030: Reference spi-peripheral-props

AD4030 and similar devices all connect to the system as SPI peripherals. Reference spi-peripheral-props so common SPI peripheral can be used from ad4030 dt-binding.

---
 Documentation/devicetree/bindings/iio/adc/adi,ad4030.yaml | 2 ++
 1 file changed, 2 insertions(+)
Documentation/devicetree/bindings/iio/adc/adi,ad4030.yaml +2 -0
index 54e7349317b7..a8fee4062d0e 100644
@@ -20,6 +20,8 @@ description: |
   * https://www.analog.com/media/en/technical-documentation/data-sheets/ad4630-24_ad4632-24.pdf
   * https://www.analog.com/media/en/technical-documentation/data-sheets/ad4630-16-4632-16.pdf
 
+$ref: /schemas/spi/spi-peripheral-props.yaml#
+
 properties:
   compatible:
     enum:
Signature · 1 line
2.39.2
#2 lore raw

PATCH v5 2/7 Docs: iio: ad4030: Add double PWM SPI offload doc

In reply to Marcelo Schmitt

Document double PWM setup SPI offload wiring schema.

---
 Documentation/iio/ad4030.rst | 39 ++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
Documentation/iio/ad4030.rst +39 -0
index b57424b650a8..9caafa4148b0 100644
@@ -92,6 +92,45 @@ Interleaved mode
 In this mode, both channels conversion results are bit interleaved one SDO line.
 As such the wiring is the same as `One lane mode`_.
 
+SPI offload wiring
+^^^^^^^^^^^^^^^^^^
+
+.. code-block::
+
+    +-------------+         +-------------+
+    |         CNV |<-----+--| GPIO        |
+    |             |      +--| PWM0        |
+    |             |         |             |
+    |             |      +--| PWM1        |
+    |             |      |  +-------------+
+    |             |      +->| TRIGGER     |
+    |          CS |<--------| CS          |
+    |             |         |             |
+    |     ADC     |         |     SPI     |
+    |             |         |             |
+    |         SDI |<--------| SDO         |
+    |         SDO |-------->| SDI         |
+    |        SCLK |<--------| SCLK        |
+    +-------------+         +-------------+
+
+In this mode, both the ``cnv-gpios`` and a ``pwms`` properties are required.
+The ``pwms`` property specifies the PWM that is connected to the ADC CNV pin.
+The SPI offload will have a ``trigger-sources`` property to indicate the SPI
+offload (PWM) trigger source. For AD4030 and similar ADCs, there are two
+possible data transfer zones for sample N. One of them (zone 1) starts after the
+data conversion for sample N is complete while the other one (zone 2) starts 9.8
+nanoseconds after the rising edge of CNV for sample N + 1.
+
+The configuration depicted in the above diagram is intended to perform data
+transfer in zone 2. To achieve high sample rates while meeting ADC timing
+requirements, an offset is added between the rising edges of PWM0 and PWM1 to
+delay the SPI transfer until 9.8 nanoseconds after CNV rising edge. This
+requires a specialized PWM controller that can provide such an offset.
+The `AD4630-FMC HDL project`_, for example, can be configured to sample AD4030
+data during zone 2 data read window.
+
+.. _AD4630-FMC HDL project: https://analogdevicesinc.github.io/hdl/projects/ad4630_fmc/index.html
+
 SPI Clock mode
 --------------
 
Signature · 1 line
2.39.2
#3 lore raw

PATCH v5 3/7 dt-bindings: iio: adc: adi,ad4030: Add PWM

In reply to Marcelo Schmitt

In setups designed for high speed data rate capture, a PWM is used to generate the CNV signal that issues data captures from the ADC. Document the use of a PWM for AD4030 and similar devices.

---
 Documentation/devicetree/bindings/iio/adc/adi,ad4030.yaml | 4 ++++
 1 file changed, 4 insertions(+)
Documentation/devicetree/bindings/iio/adc/adi,ad4030.yaml +4 -0
index a8fee4062d0e..564b6f67a96e 100644
@@ -64,6 +64,10 @@ properties:
       The Reset Input (/RST). Used for asynchronous device reset.
     maxItems: 1
 
+  pwms:
+    description: PWM signal connected to the CNV pin.
+    maxItems: 1
+
   interrupts:
     description:
       The BUSY pin is used to signal that the conversions results are available
Signature · 1 line
2.39.2
#4 lore raw

PATCH v5 4/7 iio: adc: ad4030: Use BIT macro to improve code readability

In reply to Marcelo Schmitt

Use BIT macro to make the list of average modes more readable.

---
 drivers/iio/adc/ad4030.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/iio/adc/ad4030.c +5 -3
index 4393160c7c77..b2847fd90271 100644
@@ -233,9 +233,11 @@ struct ad4030_state {
 }
 
 static const int ad4030_average_modes[] = {
-	1, 2, 4, 8, 16, 32, 64, 128,
-	256, 512, 1024, 2048, 4096, 8192, 16384, 32768,
-	65536,
+	BIT(0),					/* No averaging/oversampling */
+	BIT(1), BIT(2), BIT(3), BIT(4),		/* 2 to 16 */
+	BIT(5), BIT(6), BIT(7), BIT(8),		/* 32 to 256 */
+	BIT(9), BIT(10), BIT(11), BIT(12),	/* 512 to 4096 */
+	BIT(13), BIT(14), BIT(15), BIT(16),	/* 8192 to 65536 */
 };
 
 static int ad4030_enter_config_mode(struct ad4030_state *st)
Signature · 1 line
2.39.2
Reviewed-by on v5 4/7
#5 lore raw

Re: [PATCH v5 4/7] iio: adc: ad4030: Use BIT macro to improve code readability

Quoted 7 lines · Marcelo Schmitt

Use BIT macro to make the list of average modes more readable.

Link:
https://lore.kernel.org/linux-iio/CAHp75Vfu-C3Hd0ZXTj4rxEgRe_O84cfo6jiRCPFxZJnYrvROWQ@mail.gmail.com/

---

I don't find the link particular useful in here. Seems redundant with the Suggested-by tag. Anyways:

Quoted 23 lines

 drivers/iio/adc/ad4030.c | 8 +++++---

 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/iio/adc/ad4030.c +0 -0
index 4393160c7c77..b2847fd90271 100644
@@ -233,9 +233,11 @@ struct ad4030_state {
  }


 static const int ad4030_average_modes[] = {

drivers/iio/adc/ad4030.c +1 -3
-	1, 2, 4, 8, 16, 32, 64, 128,
-	256, 512, 1024, 2048, 4096, 8192, 16384, 32768,
-	65536,
+	BIT(0),					/* No

averaging/oversampling */

drivers/iio/adc/ad4030.c +4 -0
+	BIT(1), BIT(2), BIT(3), BIT(4),		/* 2 to 16 */
+	BIT(5), BIT(6), BIT(7), BIT(8),		/* 32 to 256 */
+	BIT(9), BIT(10), BIT(11), BIT(12),	/* 512 to 4096 */
+	BIT(13), BIT(14), BIT(15), BIT(16),	/* 8192 to 65536 */

 };

 static int ad4030_enter_config_mode(struct ad4030_state *st)

#6 lore raw

Re: [PATCH v5 4/7] iio: adc: ad4030: Use BIT macro to improve code readability

In reply to Marcelo Schmitt
Quoted 3 lines · Marcelo Schmitt

Use BIT macro to make the list of average modes more readable.

Okay, but...

...

drivers/iio/adc/ad4030.c struct ad4030_state { · line 240

Quoted 9 lines

static const int ad4030_average_modes[] = {

drivers/iio/adc/ad4030.c +5 -3
-	1, 2, 4, 8, 16, 32, 64, 128,
-	256, 512, 1024, 2048, 4096, 8192, 16384, 32768,
-	65536,
+	BIT(0),					/* No averaging/oversampling */
+	BIT(1), BIT(2), BIT(3), BIT(4),		/* 2 to 16 */
+	BIT(5), BIT(6), BIT(7), BIT(8),		/* 32 to 256 */
+	BIT(9), BIT(10), BIT(11), BIT(12),	/* 512 to 4096 */
+	BIT(13), BIT(14), BIT(15), BIT(16),	/* 8192 to 65536 */

...the comments now a bit odd as it's unclear in which step the values are. Taking this into account I would rather drop the comments for all bits but 0.

Or even drop all and make a top comment to explain the meaning of values 0, 1, and bit permutations, if any.

Quoted 1 line

};

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw

PATCH v5 5/7 iio: adc: ad4030: Add SPI offload support

In reply to Marcelo Schmitt

AD4030 and similar ADCs can capture data at sample rates up to 2 mega samples per second (MSPS). Not all SPI controllers are able to achieve such high throughputs and even when the controller is fast enough to run transfers at the required speed, it may be costly to the CPU to handle transfer data at such high sample rates. Add SPI offload support for AD4030 and similar ADCs to enable data capture at maximum sample rates.

---
Change log v4 -> v5
- Made Kconfig entry depend on PWM and select other features.
- Reused ad4030_exit_config_mode() in ad4030_offload_buffer_postenable().
- Dropped common-mode voltage support on SPI offload setup.
- Adjusted offload trigger period calculation.
- No longer setting data frame mode from ad4030_set_avg_frame_len().
- Rearranged code to reduce patch diff.

 drivers/iio/adc/Kconfig  |   5 +
 drivers/iio/adc/ad4030.c | 425 +++++++++++++++++++++++++++++++++++++--
 2 files changed, 416 insertions(+), 14 deletions(-)
drivers/iio/adc/Kconfig +5 -0
index b0580fcefef5..f76df0609b3d 100644
@@ -60,9 +60,14 @@ config AD4030
 	tristate "Analog Devices AD4030 ADC Driver"
 	depends on SPI
 	depends on GPIOLIB
+	depends on PWM
 	select REGMAP
 	select IIO_BUFFER
+	select IIO_BUFFER_DMA
+	select IIO_BUFFER_DMAENGINE
 	select IIO_TRIGGERED_BUFFER
+	select SPI_OFFLOAD
+	select SPI_OFFLOAD_TRIGGER_PWM
 	help
 	  Say yes here to build support for Analog Devices AD4030 and AD4630 high speed
 	  SPI analog to digital converters (ADC).
drivers/iio/adc/ad4030.c +411 -14

605 lines not on this page · raw message

Signature · 1 line
2.39.2
reviewer Reviewed-by on v5 5/7
#8 lore raw

Re: [PATCH v5 5/7] iio: adc: ad4030: Add SPI offload support

Quoted 12 lines · Marcelo Schmitt

AD4030 and similar ADCs can capture data at sample rates up to 2 mega samples per second (MSPS). Not all SPI controllers are able to achieve such high throughputs and even when the controller is fast enough to run transfers at the required speed, it may be costly to the CPU to handle transfer data at such high sample rates. Add SPI offload support for AD4030 and similar ADCs to enable data capture at maximum sample rates.

Quoted 5 lines

---
Change log v4 -> v5
- Made Kconfig entry depend on PWM and select other features.
- Reused ad4030_exit_config_mode() in ad4030_offload_buffer_postenable().
- Dropped common-mode voltage support on SPI offload setup.

Curious why you chose this. I guess it will be fine to add it later if anyone ever actually needs it.

Quoted 8 lines

- Adjusted offload trigger period calculation.
- No longer setting data frame mode from ad4030_set_avg_frame_len().
- Rearranged code to reduce patch diff.

 drivers/iio/adc/Kconfig  |   5 +
 drivers/iio/adc/ad4030.c | 425 +++++++++++++++++++++++++++++++++++++--
 2 files changed, 416 insertions(+), 14 deletions(-)

...

Quoted 4 lines
drivers/iio/adc/ad4030.c +0 -0
@@ -512,11 +643,30 @@ static int ad4030_set_avg_frame_len(struct iio_dev *dev, int avg_val)
 	struct ad4030_state *st = iio_priv(dev);
 	unsigned int avg_log2 = ilog2(avg_val);
 	unsigned int last_avg_idx = ARRAY_SIZE(ad4030_average_modes) - 1;

drivers/iio/adc/ad4030.c static int ad4030_set_avg_frame_len(struct iio_dev *dev, int avg_val) · line 655

Quoted 10 lines
drivers/iio/adc/ad4030.c +5 -0
+	int freq_hz;
 	int ret;
 
 	if (avg_val < 0 || avg_val > ad4030_average_modes[last_avg_idx])
 		return -EINVAL;
 
+	if (st->offload_trigger) {
+		/*
+		 * The sample averaging and sampling frequency configurations
+		 * are mutually dependent one from another. That's because the

s/one from another/on each other/

"one from another" makes it sound like they are independent rather than dependent.

Quoted 4 lines
drivers/iio/adc/ad4030.c +4 -0
+		 * effective data sample rate is fCNV / 2^N, where N is the
+		 * number of samples being averaged.
+		 *
+		 * When SPI offload is supported and we have control over the

drivers/iio/adc/ad4030.c static int ad4030_set_avg_frame_len(struct iio_dev *dev, int avg_val) · line 669

Quoted 10 lines
drivers/iio/adc/ad4030.c +10 -0
+		 * sample rate, the conversion start signal (CNV) and the SPI
+		 * offload trigger frequencies must be re-evaluated so data is
+		 * fetched only after 'avg_val' conversions.
+		 */
+		ad4030_get_sampling_freq(st, &freq_hz);
+		ret = ad4030_update_conversion_rate(st, freq_hz, avg_log2);
+		if (ret)
+			return ret;
+	}
+

LGTM.

#9 lore raw

Re: [PATCH v5 5/7] iio: adc: ad4030: Add SPI offload support

Quoted 19 lines · David Lechner
Quoted 12 lines · Marcelo Schmitt

AD4030 and similar ADCs can capture data at sample rates up to 2 mega samples per second (MSPS). Not all SPI controllers are able to achieve such high throughputs and even when the controller is fast enough to run transfers at the required speed, it may be costly to the CPU to handle transfer data at such high sample rates. Add SPI offload support for AD4030 and similar ADCs to enable data capture at maximum sample rates.

Quoted 5 lines

---
Change log v4 -> v5
- Made Kconfig entry depend on PWM and select other features.
- Reused ad4030_exit_config_mode() in ad4030_offload_buffer_postenable().
- Dropped common-mode voltage support on SPI offload setup.

Quoted 3 lines

Curious why you chose this. I guess it will be fine to add it later if anyone ever actually needs it.

I had coded that in a way I think would work for the dual channel devices, but it didn't really work for single-channel adaq4216. And yes, if anyone asks for offload with common-mode data, we shall probably be able to it that later.

Quoted 28 lines
Quoted 8 lines

- Adjusted offload trigger period calculation.
- No longer setting data frame mode from ad4030_set_avg_frame_len().
- Rearranged code to reduce patch diff.

 drivers/iio/adc/Kconfig  |   5 +
 drivers/iio/adc/ad4030.c | 425 +++++++++++++++++++++++++++++++++++++--
 2 files changed, 416 insertions(+), 14 deletions(-)

...

Quoted 4 lines
drivers/iio/adc/ad4030.c +0 -0
@@ -512,11 +643,30 @@ static int ad4030_set_avg_frame_len(struct iio_dev *dev, int avg_val)
 	struct ad4030_state *st = iio_priv(dev);
 	unsigned int avg_log2 = ilog2(avg_val);
 	unsigned int last_avg_idx = ARRAY_SIZE(ad4030_average_modes) - 1;

drivers/iio/adc/ad4030.c static int ad4030_set_avg_frame_len(struct iio_dev *dev, int avg_val) · line 655

Quoted 10 lines
drivers/iio/adc/ad4030.c +5 -0
+	int freq_hz;
 	int ret;
 
 	if (avg_val < 0 || avg_val > ad4030_average_modes[last_avg_idx])
 		return -EINVAL;
 
+	if (st->offload_trigger) {
+		/*
+		 * The sample averaging and sampling frequency configurations
+		 * are mutually dependent one from another. That's because the

s/one from another/on each other/

Quoted 2 lines

"one from another" makes it sound like they are independent rather than dependent.

Ack.

Quoted 17 lines
Quoted 5 lines
drivers/iio/adc/ad4030.c +4 -0
+		 * effective data sample rate is fCNV / 2^N, where N is the
+		 * number of samples being averaged.
+		 *
+		 * When SPI offload is supported and we have control over the

drivers/iio/adc/ad4030.c static int ad4030_set_avg_frame_len(struct iio_dev *dev, int avg_val) · line 669

Quoted 10 lines
drivers/iio/adc/ad4030.c +10 -0
+		 * sample rate, the conversion start signal (CNV) and the SPI
+		 * offload trigger frequencies must be re-evaluated so data is
+		 * fetched only after 'avg_val' conversions.
+		 */
+		ad4030_get_sampling_freq(st, &freq_hz);
+		ret = ad4030_update_conversion_rate(st, freq_hz, avg_log2);
+		if (ret)
+			return ret;
+	}
+

LGTM.

Quoted 2 lines

Thanks,
Marcelo

#10 lore raw

Re: [PATCH v5 5/7] iio: adc: ad4030: Add SPI offload support

Quoted 22 lines · Marcelo Schmitt
Quoted 21 lines · David Lechner
Quoted 19 lines · Marcelo Schmitt

AD4030 and similar ADCs can capture data at sample rates up to 2 mega samples per second (MSPS). Not all SPI controllers are able to achieve such
high throughputs and even when the controller is fast enough to run transfers at the required speed, it may be costly to the CPU to handle transfer data at such high sample rates. Add SPI offload support for AD4030
and similar ADCs to enable data capture at maximum sample rates.

---
Change log v4 -> v5
- Made Kconfig entry depend on PWM and select other features.
- Reused ad4030_exit_config_mode() in ad4030_offload_buffer_postenable().
- Dropped common-mode voltage support on SPI offload setup.

Quoted 6 lines
Quoted 3 lines

Curious why you chose this. I guess it will be fine to add it later if anyone ever actually needs it.

I had coded that in a way I think would work for the dual channel devices, but it didn't really work for single-channel adaq4216. And yes, if anyone asks for offload with common-mode data, we shall probably be able to it that later.

I guess that if someone really wants it, the data will still be available in the sample. But yes, it would be nice to properly support it at some point.

- Nuno Sá

#11 lore raw

Re: [PATCH v5 5/7] iio: adc: ad4030: Add SPI offload support

In reply to Marcelo Schmitt
Quoted 7 lines · Marcelo Schmitt

AD4030 and similar ADCs can capture data at sample rates up to 2 mega samples per second (MSPS). Not all SPI controllers are able to achieve such high throughputs and even when the controller is fast enough to run transfers at the required speed, it may be costly to the CPU to handle transfer data at such high sample rates. Add SPI offload support for AD4030 and similar ADCs to enable data capture at maximum sample rates.

Quoted 6 lines

---

Hi Marcelo,

Quoted 59 lines

Change log v4 -> v5
- Made Kconfig entry depend on PWM and select other features.
- Reused ad4030_exit_config_mode() in ad4030_offload_buffer_postenable().
- Dropped common-mode voltage support on SPI offload setup.
- Adjusted offload trigger period calculation.
- No longer setting data frame mode from ad4030_set_avg_frame_len().
- Rearranged code to reduce patch diff.

 drivers/iio/adc/Kconfig  |   5 +
 drivers/iio/adc/ad4030.c | 425 +++++++++++++++++++++++++++++++++++++--

 2 files changed, 416 insertions(+), 14 deletions(-)
drivers/iio/adc/Kconfig +0 -0
index b0580fcefef5..f76df0609b3d 100644
@@ -60,9 +60,14 @@ config AD4030

  tristate "Analog Devices AD4030 ADC Driver" depends on SPI
  depends on GPIOLIB

drivers/iio/adc/Kconfig +1 -0
+	depends on PWM

  select REGMAP
  select IIO_BUFFER

drivers/iio/adc/Kconfig +2 -0
+	select IIO_BUFFER_DMA
+	select IIO_BUFFER_DMAENGINE

  select IIO_TRIGGERED_BUFFER

drivers/iio/adc/Kconfig +2 -0
+	select SPI_OFFLOAD
+	select SPI_OFFLOAD_TRIGGER_PWM

  help
    Say yes here to build support for Analog Devices AD4030 and AD4630 high speed
    SPI analog to digital converters (ADC).

drivers/iio/adc/ad4030.c +0 -0
index b2847fd90271..3df0b593c808 100644
@@ -14,15 +14,25 @@
   */


 #include <linux/bitfield.h>
+#include <linux/cleanup.h>
 #include <linux/clk.h>

drivers/iio/adc/ad4030.c +2 -0
+#include <linux/dmaengine.h>
+#include <linux/iio/buffer-dmaengine.h>

 #include <linux/iio/iio.h>
 #include <linux/iio/trigger_consumer.h>
 #include <linux/iio/triggered_buffer.h>

drivers/iio/adc/ad4030.c +5 -0
+#include <linux/limits.h>
+#include <linux/log2.h>
+#include <linux/math64.h>
+#include <linux/minmax.h>
+#include <linux/pwm.h>

 #include <linux/regmap.h>
 #include <linux/regulator/consumer.h>
+#include <linux/spi/offload/consumer.h>
 #include <linux/spi/spi.h>
 #include <linux/unaligned.h>
 #include <linux/units.h>
+#include <linux/types.h>

...

drivers/iio/adc/ad4030.c static int ad4030_get_chan_calibbias(struct iio_dev *indio_dev, · line 579

Quoted 13 lines
drivers/iio/adc/ad4030.c +11 -0
+
+static int ad4030_set_sampling_freq(struct iio_dev *indio_dev, int freq_hz)
+{
+	struct ad4030_state *st = iio_priv(indio_dev);
+
+	/*
+	 * We have no control over the sampling frequency without SPI offload
+	 * triggering.
+	 */
+	if (!st->offload_trigger)
+		return -ENODEV;

Isn't the frequency control only available for offload channels? If I'm not missing nothing the trigger isn't optional either so I would say the above should never happen.

Quoted 49 lines
drivers/iio/adc/ad4030.c +6 -0
+	if (!in_range(freq_hz, 1, st->chip->max_sample_rate_hz))
+		return -EINVAL;
+
+	return ad4030_update_conversion_rate(st, freq_hz, st->avg_log2);
+}
+

 static int ad4030_set_chan_calibscale(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int gain_int,

drivers/iio/adc/ad4030.c +1 -0
@@ -512,11 +643,30 @@ static int ad4030_set_avg_frame_len(struct iio_dev *dev,
 int avg_val)
  	struct ad4030_state *st = iio_priv(dev);
  	unsigned int avg_log2 = ilog2(avg_val);
  	unsigned int last_avg_idx = ARRAY_SIZE(ad4030_average_modes) - 1;
+	int freq_hz;
  	int ret;


  if (avg_val < 0 || avg_val > ad4030_average_modes[last_avg_idx]) return -EINVAL;

drivers/iio/adc/ad4030.c +4 -0
+	if (st->offload_trigger) {
+		/*
+		 * The sample averaging and sampling frequency configurations
+		 * are mutually dependent one from another. That's because

the

drivers/iio/adc/ad4030.c +6 -0
+		 * effective data sample rate is fCNV / 2^N, where N is the
+		 * number of samples being averaged.
+		 *
+		 * When SPI offload is supported and we have control over the
+		 * sample rate, the conversion start signal (CNV) and the SPI
+		 * offload trigger frequencies must be re-evaluated so data

is

drivers/iio/adc/ad4030.c +8 -0
+		 * fetched only after 'avg_val' conversions.
+		 */
+		ad4030_get_sampling_freq(st, &freq_hz);
+		ret = ad4030_update_conversion_rate(st, freq_hz, avg_log2);
+		if (ret)
+			return ret;
+	}
+

  ret = regmap_write(st->regmap, AD4030_REG_AVG, AD4030_REG_AVG_MASK_AVG_SYNC | FIELD_PREP(AD4030_REG_AVG_MASK_AVG_VAL, avg_log2));

drivers/iio/adc/ad4030.c +0 -0
@@ -769,6 +919,13 @@ static int ad4030_read_raw_dispatch(struct iio_dev
 *indio_dev,
  		*val = BIT(st->avg_log2);
  		return IIO_VAL_INT;

drivers/iio/adc/ad4030.c static int ad4030_read_raw_dispatch(struct iio_dev *indio_dev, · line 924

Quoted 3 lines
drivers/iio/adc/ad4030.c +3 -0
+	case IIO_CHAN_INFO_SAMP_FREQ:
+		if (!st->offload_trigger)
+			return -ENODEV;

same

Quoted 84 lines
drivers/iio/adc/ad4030.c +4 -0
+
+		ad4030_get_sampling_freq(st, val);
+		return IIO_VAL_INT;
+

  default:
  return -EINVAL;
  }

drivers/iio/adc/ad4030.c +0 -0
@@ -809,6 +966,9 @@ static int ad4030_write_raw_dispatch(struct iio_dev
 *indio_dev,

  case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  return ad4030_set_avg_frame_len(indio_dev, val);

drivers/iio/adc/ad4030.c +3 -0
+	case IIO_CHAN_INFO_SAMP_FREQ:
+		return ad4030_set_sampling_freq(indio_dev, val);
+

  default:
  return -EINVAL;
  }

drivers/iio/adc/ad4030.c +0 -0
@@ -898,6 +1058,104 @@ static const struct iio_buffer_setup_ops
 ad4030_buffer_setup_ops = {
  	.validate_scan_mask = ad4030_validate_scan_mask,
  };

drivers/iio/adc/ad4030.c +14 -0
+static void ad4030_prepare_offload_msg(struct iio_dev *indio_dev)
+{
+	struct ad4030_state *st = iio_priv(indio_dev);
+	u8 offload_bpw;
+
+	if (st->mode == AD4030_OUT_DATA_MD_30_AVERAGED_DIFF)
+		offload_bpw = 32;
+	else
+		offload_bpw = st->chip->precision_bits;
+
+	st->offload_xfer.bits_per_word = offload_bpw;
+	st->offload_xfer.len = spi_bpw_to_bytes(offload_bpw);
+	st->offload_xfer.offload_flags = SPI_OFFLOAD_XFER_RX_STREAM;
+	spi_message_init_with_transfers(&st->offload_msg, &st->offload_xfer,

1);

drivers/iio/adc/ad4030.c +11 -0
+}
+
+static int ad4030_offload_buffer_postenable(struct iio_dev *indio_dev)
+{
+	struct ad4030_state *st = iio_priv(indio_dev);
+	unsigned int reg_modes;
+	int ret, ret2;
+
+	/*
+	 * When data from 2 analog input channels is output through a single
+	 * bus line (interleaved mode (LANE_MD == 0b11)) and gets pushed

through

drivers/iio/adc/ad4030.c +1 -0
+	 * DMA, extra hardware is required to do the de-interleaving. While

we

drivers/iio/adc/ad4030.c +1 -0
+	 * don't support such hardware configurations, disallow interleaved

mode

drivers/iio/adc/ad4030.c +8 -0
+	 * when using SPI offload.
+	 */
+	ret = regmap_read(st->regmap, AD4030_REG_MODES, &reg_modes);
+	if (ret)
+		return ret;
+
+	if (st->chip->num_voltage_inputs > 1 &&
+	    FIELD_GET(AD4030_REG_MODES_MASK_LANE_MODE, reg_modes) ==

AD4030_LANE_MD_INTERLEAVED)

drivers/iio/adc/ad4030.c +13 -0
+		return -EINVAL;
+
+	ret = ad4030_exit_config_mode(st);
+	if (ret)
+		return ret;
+
+	ad4030_prepare_offload_msg(indio_dev);
+	st->offload_msg.offload = st->offload;
+	ret = spi_optimize_message(st->spi, &st->offload_msg);
+	if (ret)
+		goto out_reset_mode;
+
+	ret = pwm_set_waveform_might_sleep(st->cnv_trigger, &st->cnv_wf,

false);

drivers/iio/adc/ad4030.c +7 -0
+	if (ret)
+		goto out_unoptimize;
+
+	ret = spi_offload_trigger_enable(st->offload, st->offload_trigger,
+					 &st->offload_trigger_config);
+	if (ret)
+		goto out_pwm_disable;

drivers/iio/adc/ad4030.c static const struct iio_buffer_setup_ops ad4030_buffer_setup_ops = { · line 1125

Quoted 10 lines
drivers/iio/adc/ad4030.c +10 -0
+
+	return 0;
+
+out_pwm_disable:
+	pwm_disable(st->cnv_trigger);
+out_unoptimize:
+	spi_unoptimize_message(&st->offload_msg);
+out_reset_mode:
+	/* reenter register configuration mode */
+	ret2 = ad4030_enter_config_mode(st);

nit: if ret2 is not being used at all, maybe just

if (ad4030_enter_config_mode(st))

Quoted 8 lines
drivers/iio/adc/ad4030.c +7 -0
+	if (ret2)
+		dev_err(&st->spi->dev,
+			"couldn't reenter register configuration mode: %d\n",
+			ret2);
+
+	return ret;
+}

...

Quoted 61 lines

 static int ad4030_probe(struct spi_device *spi)
 {
  struct device *dev = &spi->dev;

drivers/iio/adc/ad4030.c +0 -0
@@ -1045,24 +1346,61 @@ static int ad4030_probe(struct spi_device *spi)
  		return dev_err_probe(dev, PTR_ERR(st->cnv_gpio),
  				     "Failed to get cnv gpio\n");

drivers/iio/adc/ad4030.c +0 -5
-	/*
-	 * One hardware channel is split in two software channels when using
-	 * common byte mode. Add one more channel for the timestamp.
-	 */
-	indio_dev->num_channels = 2 * st->chip->num_voltage_inputs + 1;

  indio_dev->name = st->chip->name;
  indio_dev->modes = INDIO_DIRECT_MODE;
  indio_dev->info = &ad4030_iio_info;

drivers/iio/adc/ad4030.c +0 -1
-	indio_dev->channels = st->chip->channels;

  indio_dev->available_scan_masks = st->chip->available_masks;

drivers/iio/adc/ad4030.c +9 -7
-	ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
-					      iio_pollfunc_store_time,
-					      ad4030_trigger_handler,
-					      &ad4030_buffer_setup_ops);
-	if (ret)
-		return dev_err_probe(dev, ret,
-				     "Failed to setup triggered buffer\n");
+	st->offload = devm_spi_offload_get(dev, spi, &ad4030_offload_config);
+	ret = PTR_ERR_OR_ZERO(st->offload);
+	if (ret && ret != -ENODEV)
+		return dev_err_probe(dev, ret, "failed to get offload\n");
+
+	/* Fall back to low speed usage when no SPI offload is available. */
+	if (ret == -ENODEV) {
+		/*
+		 * One hardware channel is split in two software channels

when

drivers/iio/adc/ad4030.c +1 -0
+		 * using common byte mode. Add one more channel for the

timestamp.

drivers/iio/adc/ad4030.c +2 -0
+		 */
+		indio_dev->num_channels = 2 * st->chip->num_voltage_inputs +

1;

drivers/iio/adc/ad4030.c +4 -0
+		indio_dev->channels = st->chip->channels;
+
+		ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
+						     

iio_pollfunc_store_time,

drivers/iio/adc/ad4030.c +2 -0
+						      ad4030_trigger_handler,
+						     

&ad4030_buffer_setup_ops);

drivers/iio/adc/ad4030.c +3 -0
+		if (ret)
+			return dev_err_probe(dev, ret,
+					     "Failed to setup triggered

buffer\n");

drivers/iio/adc/ad4030.c +3 -0
+	} else {
+		/*
+		 * One hardware channel is split in two software channels

when

drivers/iio/adc/ad4030.c +1 -0
+		 * using common byte mode. Offloaded SPI transfers can't

support

drivers/iio/adc/ad4030.c +1 -0
+		 * software timestamp so no additional timestamp channel is

added.

drivers/iio/adc/ad4030.c

Quoted 2 lines
drivers/iio/adc/ad4030.c +2 -0
+		 */
+		indio_dev->num_channels = 2 * st->chip->num_voltage_inputs;

Maybe I'm missing something but common mode is not supported for now so isn't the above wrong?

- Nuno Sá

#12 lore raw

Re: [PATCH v5 5/7] iio: adc: ad4030: Add SPI offload support

Quoted 15 lines · Nuno Sá
Quoted 13 lines · Marcelo Schmitt

AD4030 and similar ADCs can capture data at sample rates up to 2 mega samples per second (MSPS). Not all SPI controllers are able to achieve such high throughputs and even when the controller is fast enough to run transfers at the required speed, it may be costly to the CPU to handle transfer data at such high sample rates. Add SPI offload support for AD4030 and similar ADCs to enable data capture at maximum sample rates.

---

...

Quoted 6 lines
Quoted 6 lines
drivers/iio/adc/ad4030.c +6 -0
+static int ad4030_offload_buffer_postenable(struct iio_dev *indio_dev)
+{
+	struct ad4030_state *st = iio_priv(indio_dev);
+	unsigned int reg_modes;
+	int ret, ret2;
+

...

Quoted 8 lines
Quoted 8 lines
drivers/iio/adc/ad4030.c +8 -0
+	ret = spi_offload_trigger_enable(st->offload, st->offload_trigger,
+					 &st->offload_trigger_config);
+	if (ret)
+		goto out_pwm_disable;
+
+	return 0;
+
+out_pwm_disable:
Quoted 8 lines

drivers/iio/adc/ad4030.c static const struct iio_buffer_setup_ops ad4030_buffer_setup_ops = { · line 1125

Quoted 6 lines
drivers/iio/adc/ad4030.c +6 -0
+	pwm_disable(st->cnv_trigger);
+out_unoptimize:
+	spi_unoptimize_message(&st->offload_msg);
+out_reset_mode:
+	/* reenter register configuration mode */
+	ret2 = ad4030_enter_config_mode(st);

nit: if ret2 is not being used at all, maybe just

ret2 is logged on the error message below so I guess I'll keep it as it is.

Quoted 7 lines

if (ad4030_enter_config_mode(st))

drivers/iio/adc/ad4030.c static const struct iio_buffer_setup_ops ad4030_buffer_setup_ops = { · line 1129

Quoted 4 lines
drivers/iio/adc/ad4030.c +4 -0
+	if (ret2)
+		dev_err(&st->spi->dev,
+			"couldn't reenter register configuration mode: %d\n",
+			ret2);

here we log the error code. We only reach it if reg access fails after something on offload buffer enable have failed first. We cannot reuse ret here because we would be shadowing the original error code.

Quoted 7 lines
Quoted 4 lines
drivers/iio/adc/ad4030.c +3 -0
+
+	return ret;
+}

...

...

Quoted 5 lines
Quoted 5 lines
drivers/iio/adc/ad4030.c +3 -0
+	} else {
+		/*
+		 * One hardware channel is split in two software channels

when

(quoted diff) +1 -0
+		 * using common byte mode. Offloaded SPI transfers can't
Quoted 9 lines

Review

Quoted 5 lines

support

(quoted diff) +1 -0
+		 * software timestamp so no additional timestamp channel is

added.

(quoted diff) +2 -0
+		 */
+		indio_dev->num_channels = 2 * st->chip->num_voltage_inputs;

Maybe I'm missing something but common mode is not supported for now so isn't the above wrong?

Yes, that was buggy. Dropping common-mode channels as last minute change was a bad idea. I did another set of tests with ADAQ4216 and fixed that for v6. I'll also re-add common-mode channel support as a separate patch.

Thanks,
Marcelo

#13 lore raw

Re: [PATCH v5 5/7] iio: adc: ad4030: Add SPI offload support

Quoted 47 lines · Marcelo Schmitt
Quoted 17 lines · Nuno Sá
Quoted 15 lines · Marcelo Schmitt

AD4030 and similar ADCs can capture data at sample rates up to 2 mega samples per second (MSPS). Not all SPI controllers are able to achieve such
high throughputs and even when the controller is fast enough to run transfers at the required speed, it may be costly to the CPU to handle transfer data at such high sample rates. Add SPI offload support for AD4030
and similar ADCs to enable data capture at maximum sample rates.

---

...

Quoted 6 lines
Quoted 6 lines
drivers/iio/adc/ad4030.c +6 -0
+static int ad4030_offload_buffer_postenable(struct iio_dev *indio_dev)
+{
+	struct ad4030_state *st = iio_priv(indio_dev);
+	unsigned int reg_modes;
+	int ret, ret2;
+

...

Quoted 17 lines
Quoted 5 lines
(quoted diff) +1 -0
+	ret = spi_offload_trigger_enable(st->offload, st-
Quoted 1 line

offload_trigger,

drivers/iio/adc/ad4030.c +3 -0
+					 &st->offload_trigger_config);
+	if (ret)
+		goto out_pwm_disable;

drivers/iio/adc/ad4030.c static const struct iio_buffer_setup_ops ad4030_buffer_setup_ops = { · line 1125

Quoted 10 lines
drivers/iio/adc/ad4030.c +10 -0
+
+	return 0;
+
+out_pwm_disable:
+	pwm_disable(st->cnv_trigger);
+out_unoptimize:
+	spi_unoptimize_message(&st->offload_msg);
+out_reset_mode:
+	/* reenter register configuration mode */
+	ret2 = ad4030_enter_config_mode(st);

nit: if ret2 is not being used at all, maybe just

ret2 is logged on the error message below so I guess I'll keep it as it is.

Quoted 3 lines

if (ad4030_enter_config_mode(st))

Quoted 8 lines
Quoted 5 lines

drivers/iio/adc/ad4030.c static const struct iio_buffer_setup_ops ad4030_buffer_setup_ops = { · line 1129

Quoted 5 lines
drivers/iio/adc/ad4030.c +3 -0
+	if (ret2)
+		dev_err(&st->spi->dev,
+			"couldn't reenter register configuration mode:

%d\n",

drivers/iio/adc/ad4030.c +1 -0
+			ret2);

here we log the error code. We only reach it if reg access fails after something on offload buffer enable have failed first. We cannot reuse ret here because we would be shadowing the original error code.

Right :facepalm:

#14 lore raw

PATCH v5 6/7 dt-bindings: iio: adc: adi,ad4030: Add ADAQ4216 and ADAQ4224

In reply to Marcelo Schmitt

ADAQ4216 and ADAQ4224 are similar to AD4030 except that ADAQ devices have a PGA (programmable gain amplifier) that scales the input signal prior to it reaching the ADC inputs. The PGA is controlled through a couple of pins (A0 and A1) that set one of four possible signal gain configurations.

---
Change log v4 -> v5
- Dropped leftover adi,pga-value.

 .../bindings/iio/adc/adi,ad4030.yaml          | 70 +++++++++++++++++--
 1 file changed, 65 insertions(+), 5 deletions(-)
Documentation/devicetree/bindings/iio/adc/adi,ad4030.yaml +65 -5
index 564b6f67a96e..3890cd4ba93e 100644
@@ -19,6 +19,8 @@ description: |
   * https://www.analog.com/media/en/technical-documentation/data-sheets/ad4030-24-4032-24.pdf
   * https://www.analog.com/media/en/technical-documentation/data-sheets/ad4630-24_ad4632-24.pdf
   * https://www.analog.com/media/en/technical-documentation/data-sheets/ad4630-16-4632-16.pdf
+  * https://www.analog.com/media/en/technical-documentation/data-sheets/adaq4216.pdf
+  * https://www.analog.com/media/en/technical-documentation/data-sheets/adaq4224.pdf
 
 $ref: /schemas/spi/spi-peripheral-props.yaml#
 
@@ -31,6 +33,8 @@ properties:
       - adi,ad4630-24
       - adi,ad4632-16
       - adi,ad4632-24
+      - adi,adaq4216
+      - adi,adaq4224
 
   reg:
     maxItems: 1
@@ -54,6 +58,14 @@ properties:
     description:
       Internal buffered Reference. Used when ref-supply is not connected.
 
+  vddh-supply:
+    description:
+      PGIA Positive Power Supply.
+
+  vdd-fda-supply:
+    description:
+      FDA Positive Power Supply.
+
   cnv-gpios:
     description:
       The Convert Input (CNV). It initiates the sampling conversions.
@@ -64,6 +76,13 @@ properties:
       The Reset Input (/RST). Used for asynchronous device reset.
     maxItems: 1
 
+  pga-gpios:
+    description:
+      A0 and A1 pins for gain selection. For devices that have PGA configuration
+      input pins, pga-gpios should be defined.
+    minItems: 2
+    maxItems: 2
+
   pwms:
     description: PWM signal connected to the CNV pin.
     maxItems: 1
@@ -86,11 +105,29 @@ required:
   - vio-supply
   - cnv-gpios
 
-oneOf:
-  - required:
-      - ref-supply
-  - required:
-      - refin-supply
+allOf:
+  - oneOf:
+      - required:
+          - ref-supply
+      - required:
+          - refin-supply
+  # ADAQ devices require a gain property to indicate how hardware PGA is set
+  - if:
+      properties:
+        compatible:
+          contains:
+            pattern: ^adi,adaq
+    then:
+      required:
+        - vddh-supply
+        - vdd-fda-supply
+        - pga-gpios
+      properties:
+        ref-supply: false
+    else:
+      properties:
+        pga-gpios: false
+
 
 unevaluatedProperties: false
 
@@ -114,3 +151,26 @@ examples:
             reset-gpios = <&gpio0 1 GPIO_ACTIVE_LOW>;
         };
     };
+  - |
+    #include <dt-bindings/gpio/gpio.h>
+    spi {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        adc@0 {
+            compatible = "adi,adaq4216";
+            reg = <0>;
+            spi-max-frequency = <80000000>;
+            vdd-5v-supply = <&supply_5V>;
+            vdd-1v8-supply = <&supply_1_8V>;
+            vio-supply = <&supply_1_8V>;
+            refin-supply = <&refin_sup>;
+            vddh-supply = <&vddh>;
+            vdd-fda-supply = <&vdd_fda>;
+            cnv-gpios = <&gpio0 0 GPIO_ACTIVE_HIGH>;
+            reset-gpios = <&gpio0 1 GPIO_ACTIVE_LOW>;
+            pga-gpios = <&gpio0 2 GPIO_ACTIVE_HIGH>,
+                        <&gpio0 3 GPIO_ACTIVE_HIGH>;
+        };
+    };
+...
Signature · 1 line
2.39.2
Reviewed-by on v5 6/7
#15 lore raw

Re: [PATCH v5 6/7] dt-bindings: iio: adc: adi,ad4030: Add ADAQ4216 and ADAQ4224

Quoted 9 lines · Marcelo Schmitt

ADAQ4216 and ADAQ4224 are similar to AD4030 except that ADAQ devices have a PGA (programmable gain amplifier) that scales the input signal prior to it reaching the ADC inputs. The PGA is controlled through a couple of pins (A0 and A1) that set one of four possible signal gain configurations.

---
Change log v4 -> v5
- Dropped leftover adi,pga-value.

pw-bot: not-applicable

#16 lore raw

PATCH v5 7/7 iio: adc: ad4030: Add support for ADAQ4216 and ADAQ4224

In reply to Marcelo Schmitt

ADAQ4216 and ADAQ4224 are similar to AD4030, but feature a PGA circuitry that scales the analog input signal prior to it reaching the ADC. The PGA is controlled through a pair of pins (A0 and A1) whose state define the gain that is applied to the input signal.

Add support for ADAQ4216 and ADAQ4224. Provide a list of PGA options through the IIO device channel scale available interface and enable control of the PGA through the channel scale interface.

---
Change log v4 -> v5
- Dropped leftover adaq4216_hw_gains_db[].
- Tweaked ad4030_get_chan_scale() so val and val2 don't get overwritten.

 drivers/iio/adc/ad4030.c | 209 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 205 insertions(+), 4 deletions(-)
drivers/iio/adc/ad4030.c +205 -4
index 3df0b593c808..1ba49890a3f3 100644
@@ -47,6 +47,8 @@
 #define     AD4030_REG_CHIP_GRADE_AD4630_24_GRADE	0x00
 #define     AD4030_REG_CHIP_GRADE_AD4632_16_GRADE	0x05
 #define     AD4030_REG_CHIP_GRADE_AD4632_24_GRADE	0x02
+#define     AD4030_REG_CHIP_GRADE_ADAQ4216_GRADE	0x1E
+#define     AD4030_REG_CHIP_GRADE_ADAQ4224_GRADE	0x1C
 #define     AD4030_REG_CHIP_GRADE_MASK_CHIP_GRADE	GENMASK(7, 3)
 #define AD4030_REG_SCRATCH_PAD			0x0A
 #define AD4030_REG_SPI_REVISION			0x0B
@@ -124,6 +126,10 @@
 /* Datasheet says 9.8ns, so use the closest integer value */
 #define AD4030_TQUIET_CNV_DELAY_NS	10
 
+/* HARDWARE_GAIN */
+#define ADAQ4616_PGA_PINS		2
+#define ADAQ4616_PGA_GAIN_MAX_NANO	(NANO * 2 / 3)
+
 enum ad4030_out_mode {
 	AD4030_OUT_DATA_MD_DIFF,
 	AD4030_OUT_DATA_MD_16_DIFF_8_COM,
@@ -144,6 +150,23 @@ enum {
 	AD4030_SCAN_TYPE_AVG,
 };
 
+/*
+ * Gains computed as fractions of 1000 so they can be expressed by integers.
+ */
+static const int adaq4216_hw_gains_vpv[] = {
+	MILLI / 3,		/* 333 */
+	(5 * MILLI / 9),	/* 555 */
+	(20 * MILLI / 9),	/* 2222 */
+	(20 * MILLI / 3),	/* 6666 */
+};
+
+static const int adaq4216_hw_gains_frac[][2] = {
+	{ 1, 3 },  /* 1/3 V/V gain */
+	{ 5, 9 },  /* 5/9 V/V gain */
+	{ 20, 9 }, /* 20/9 V/V gain */
+	{ 20, 3 }, /* 20/3 V/V gain */
+};
+
 struct ad4030_chip_info {
 	const char *name;
 	const unsigned long *available_masks;
@@ -151,6 +174,7 @@ struct ad4030_chip_info {
 	const struct iio_chan_spec offload_channels[AD4030_MAX_IIO_CHANNEL_NB];
 	u8 grade;
 	u8 precision_bits;
+	bool has_pga;
 	/* Number of hardware channels */
 	int num_voltage_inputs;
 	unsigned int tcyc_ns;
@@ -174,7 +198,11 @@ struct ad4030_state {
 	struct spi_offload_trigger *offload_trigger;
 	struct spi_offload_trigger_config offload_trigger_config;
 	struct pwm_device *cnv_trigger;
+	size_t scale_avail_size;
 	struct pwm_waveform cnv_wf;
+	unsigned int scale_avail[ARRAY_SIZE(adaq4216_hw_gains_vpv)][2];
+	struct gpio_descs *pga_gpios;
+	unsigned int pga_index;
 
 	/*
 	 * DMA (thus cache coherency maintenance) requires the transfer buffers
@@ -231,7 +259,7 @@ struct ad4030_state {
  * - voltage0-voltage1
  * - voltage2-voltage3
  */
-#define __AD4030_CHAN_DIFF(_idx, _scan_type, _offload) {		\
+#define __AD4030_CHAN_DIFF(_idx, _scan_type, _offload, _pga) {		\
 	.info_mask_shared_by_all =					\
 		(_offload ? BIT(IIO_CHAN_INFO_SAMP_FREQ) : 0) |		\
 		BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),			\
@@ -242,6 +270,7 @@ struct ad4030_state {
 		BIT(IIO_CHAN_INFO_CALIBBIAS) |				\
 		BIT(IIO_CHAN_INFO_RAW),					\
 	.info_mask_separate_available = BIT(IIO_CHAN_INFO_CALIBBIAS) |	\
+		(_pga ? BIT(IIO_CHAN_INFO_SCALE) : 0) |			\
 		BIT(IIO_CHAN_INFO_CALIBSCALE),				\
 	.type = IIO_VOLTAGE,						\
 	.indexed = 1,							\
@@ -256,10 +285,16 @@ struct ad4030_state {
 }
 
 #define AD4030_CHAN_DIFF(_idx, _scan_type)				\
-	__AD4030_CHAN_DIFF(_idx, _scan_type, 0)
+	__AD4030_CHAN_DIFF(_idx, _scan_type, 0, 0)
 
 #define AD4030_OFFLOAD_CHAN_DIFF(_idx, _scan_type)			\
-	__AD4030_CHAN_DIFF(_idx, _scan_type, 1)
+	__AD4030_CHAN_DIFF(_idx, _scan_type, 1, 0)
+
+#define ADAQ4216_CHAN_DIFF(_idx, _scan_type)				\
+	__AD4030_CHAN_DIFF(_idx, _scan_type, 0, 1)
+
+#define ADAQ4216_OFFLOAD_CHAN_DIFF(_idx, _scan_type)			\
+	__AD4030_CHAN_DIFF(_idx, _scan_type, 1, 1)
 
 static const int ad4030_average_modes[] = {
 	BIT(0),					/* No averaging/oversampling */
@@ -413,6 +448,65 @@ static const struct regmap_config ad4030_regmap_config = {
 	.max_register = AD4030_REG_DIG_ERR,
 };
 
+static void ad4030_fill_scale_avail(struct ad4030_state *st)
+{
+	unsigned int mag_bits, int_part, fract_part, i;
+	u64 range;
+
+	/*
+	 * The maximum precision of differential channels is retrieved from the
+	 * chip properties. The output code of differential channels is in two's
+	 * complement format (i.e. signed), so the MSB is the sign bit and only
+	 * (precision_bits - 1) bits express voltage magnitude.
+	 */
+	mag_bits = st->chip->precision_bits - 1;
+
+	for (i = 0; i < ARRAY_SIZE(adaq4216_hw_gains_frac); i++) {
+		range = mult_frac(st->vref_uv, adaq4216_hw_gains_frac[i][1],
+				  adaq4216_hw_gains_frac[i][0]);
+		/*
+		 * If range were in mV, we would multiply it by NANO below.
+		 * Though, range is in µV so multiply it by MICRO only so the
+		 * result after right shift and division scales output codes to
+		 * millivolts.
+		 */
+		int_part = div_u64_rem(((u64)range * MICRO) >> mag_bits, NANO, &fract_part);
+		st->scale_avail[i][0] = int_part;
+		st->scale_avail[i][1] = fract_part;
+	}
+}
+
+static int ad4030_set_pga_gain(struct ad4030_state *st)
+{
+	DECLARE_BITMAP(bitmap, ADAQ4616_PGA_PINS) = { };
+
+	bitmap_write(bitmap, st->pga_index, 0, ADAQ4616_PGA_PINS);
+
+	return gpiod_multi_set_value_cansleep(st->pga_gpios, bitmap);
+}
+
+static int ad4030_set_pga(struct iio_dev *indio_dev, int gain_int, int gain_fract)
+{
+	struct ad4030_state *st = iio_priv(indio_dev);
+	unsigned int mag_bits = st->chip->precision_bits - 1;
+	u64 gain_nano, tmp;
+
+	if (!st->pga_gpios)
+		return -EINVAL;
+
+	gain_nano = gain_int * NANO + gain_fract;
+
+	if (!in_range(gain_nano, 1, ADAQ4616_PGA_GAIN_MAX_NANO))
+		return -EINVAL;
+
+	tmp = DIV_ROUND_CLOSEST_ULL(gain_nano << mag_bits, NANO);
+	gain_nano = DIV_ROUND_CLOSEST_ULL(st->vref_uv, tmp);
+	st->pga_index = find_closest(gain_nano, adaq4216_hw_gains_vpv,
+				     ARRAY_SIZE(adaq4216_hw_gains_vpv));
+
+	return ad4030_set_pga_gain(st);
+}
+
 static int ad4030_get_chan_scale(struct iio_dev *indio_dev,
 				 struct iio_chan_spec const *chan,
 				 int *val,
@@ -425,6 +519,13 @@ static int ad4030_get_chan_scale(struct iio_dev *indio_dev,
 	if (IS_ERR(scan_type))
 		return PTR_ERR(scan_type);
 
+	/* The LSB of the 8-bit common-mode data is always vref/256. */
+	if (st->chip->has_pga && scan_type->realbits != 8) {
+		*val = st->scale_avail[st->pga_index][0];
+		*val2 = st->scale_avail[st->pga_index][1];
+		return IIO_VAL_INT_PLUS_NANO;
+	}
+
 	if (chan->differential)
 		*val = (st->vref_uv * 2) / MILLI;
 	else
@@ -432,7 +533,14 @@ static int ad4030_get_chan_scale(struct iio_dev *indio_dev,
 
 	*val2 = scan_type->realbits;
 
-	return IIO_VAL_FRACTIONAL_LOG2;
+	/* The LSB of the 8-bit common-mode data is always vref/256. */
+	if (scan_type->realbits == 8 || !st->chip->has_pga)
+		return IIO_VAL_FRACTIONAL_LOG2;
+
+	*val = st->scale_avail[st->pga_index][0];
+	*val2 = st->scale_avail[st->pga_index][1];
+
+	return IIO_VAL_INT_PLUS_NANO;
 }
 
 static int ad4030_get_chan_calibscale(struct iio_dev *indio_dev,
@@ -894,6 +1002,15 @@ static int ad4030_read_avail(struct iio_dev *indio_dev,
 		*length = ARRAY_SIZE(ad4030_average_modes);
 		return IIO_AVAIL_LIST;
 
+	case IIO_CHAN_INFO_SCALE:
+		if (st->scale_avail_size == 1)
+			*vals = (int *)st->scale_avail[st->pga_index];
+		else
+			*vals = (int *)st->scale_avail;
+		*length = st->scale_avail_size * 2; /* print int and nano part */
+		*type = IIO_VAL_INT_PLUS_NANO;
+		return IIO_AVAIL_LIST;
+
 	default:
 		return -EINVAL;
 	}
@@ -969,6 +1086,9 @@ static int ad4030_write_raw_dispatch(struct iio_dev *indio_dev,
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		return ad4030_set_sampling_freq(indio_dev, val);
 
+	case IIO_CHAN_INFO_SCALE:
+		return ad4030_set_pga(indio_dev, val, val2);
+
 	default:
 		return -EINVAL;
 	}
@@ -990,6 +1110,17 @@ static int ad4030_write_raw(struct iio_dev *indio_dev,
 	return ret;
 }
 
+static int ad4030_write_raw_get_fmt(struct iio_dev *indio_dev,
+				    struct iio_chan_spec const *chan, long mask)
+{
+	switch (mask) {
+	case IIO_CHAN_INFO_SCALE:
+		return IIO_VAL_INT_PLUS_NANO;
+	default:
+		return IIO_VAL_INT_PLUS_MICRO;
+	}
+}
+
 static int ad4030_reg_access(struct iio_dev *indio_dev, unsigned int reg,
 			     unsigned int writeval, unsigned int *readval)
 {
@@ -1036,6 +1167,7 @@ static const struct iio_info ad4030_iio_info = {
 	.read_avail = ad4030_read_avail,
 	.read_raw = ad4030_read_raw,
 	.write_raw = ad4030_write_raw,
+	.write_raw_get_fmt = &ad4030_write_raw_get_fmt,
 	.debugfs_reg_access = ad4030_reg_access,
 	.read_label = ad4030_read_label,
 	.get_current_scan_type = ad4030_get_current_scan_type,
@@ -1295,6 +1427,25 @@ static int ad4030_spi_offload_setup(struct iio_dev *indio_dev,
 							   IIO_BUFFER_DIRECTION_IN);
 }
 
+static int ad4030_setup_pga(struct device *dev, struct iio_dev *indio_dev,
+			    struct ad4030_state *st)
+{
+	/* Setup GPIOs for PGA control */
+	st->pga_gpios = devm_gpiod_get_array(dev, "pga", GPIOD_OUT_LOW);
+	if (IS_ERR(st->pga_gpios))
+		return dev_err_probe(dev, PTR_ERR(st->pga_gpios),
+				     "Failed to get PGA gpios.\n");
+
+	if (st->pga_gpios->ndescs != ADAQ4616_PGA_PINS)
+		return dev_err_probe(dev, -EINVAL,
+				     "Expected 2 GPIOs for PGA control.\n");
+
+	st->scale_avail_size = ARRAY_SIZE(adaq4216_hw_gains_vpv);
+	st->pga_index = 0;
+
+	return 0;
+}
+
 static int ad4030_probe(struct spi_device *spi)
 {
 	struct device *dev = &spi->dev;
@@ -1337,6 +1488,14 @@ static int ad4030_probe(struct spi_device *spi)
 	if (ret)
 		return ret;
 
+	if (st->chip->has_pga) {
+		ret = ad4030_setup_pga(dev, indio_dev, st);
+		if (ret)
+			return ret;
+
+		ad4030_fill_scale_avail(st);
+	}
+
 	ret = ad4030_config(st);
 	if (ret)
 		return ret;
@@ -1591,12 +1750,52 @@ static const struct ad4030_chip_info ad4632_24_chip_info = {
 	.max_sample_rate_hz = 500 * HZ_PER_KHZ,
 };
 
+static const struct ad4030_chip_info adaq4216_chip_info = {
+	.name = "adaq4216",
+	.available_masks = ad4030_channel_masks,
+	.channels = {
+		ADAQ4216_CHAN_DIFF(0, ad4030_16_scan_types),
+		AD4030_CHAN_CMO(1, 0),
+		IIO_CHAN_SOFT_TIMESTAMP(2),
+	},
+	.offload_channels = {
+		ADAQ4216_OFFLOAD_CHAN_DIFF(0, ad4030_16_offload_scan_types),
+	},
+	.grade = AD4030_REG_CHIP_GRADE_ADAQ4216_GRADE,
+	.precision_bits = 16,
+	.has_pga = true,
+	.num_voltage_inputs = 1,
+	.tcyc_ns = AD4030_TCYC_ADJUSTED_NS,
+	.max_sample_rate_hz = 2 * HZ_PER_MHZ,
+};
+
+static const struct ad4030_chip_info adaq4224_chip_info = {
+	.name = "adaq4224",
+	.available_masks = ad4030_channel_masks,
+	.channels = {
+		ADAQ4216_CHAN_DIFF(0, ad4030_24_scan_types),
+		AD4030_CHAN_CMO(1, 0),
+		IIO_CHAN_SOFT_TIMESTAMP(2),
+	},
+	.offload_channels = {
+		ADAQ4216_OFFLOAD_CHAN_DIFF(0, ad4030_24_offload_scan_types),
+	},
+	.grade = AD4030_REG_CHIP_GRADE_ADAQ4224_GRADE,
+	.precision_bits = 24,
+	.has_pga = true,
+	.num_voltage_inputs = 1,
+	.tcyc_ns = AD4030_TCYC_ADJUSTED_NS,
+	.max_sample_rate_hz = 2 * HZ_PER_MHZ,
+};
+
 static const struct spi_device_id ad4030_id_table[] = {
 	{ "ad4030-24", (kernel_ulong_t)&ad4030_24_chip_info },
 	{ "ad4630-16", (kernel_ulong_t)&ad4630_16_chip_info },
 	{ "ad4630-24", (kernel_ulong_t)&ad4630_24_chip_info },
 	{ "ad4632-16", (kernel_ulong_t)&ad4632_16_chip_info },
 	{ "ad4632-24", (kernel_ulong_t)&ad4632_24_chip_info },
+	{ "adaq4216", (kernel_ulong_t)&adaq4216_chip_info },
+	{ "adaq4224", (kernel_ulong_t)&adaq4224_chip_info },
 	{ }
 };
 MODULE_DEVICE_TABLE(spi, ad4030_id_table);
@@ -1607,6 +1806,8 @@ static const struct of_device_id ad4030_of_match[] = {
 	{ .compatible = "adi,ad4630-24", .data = &ad4630_24_chip_info },
 	{ .compatible = "adi,ad4632-16", .data = &ad4632_16_chip_info },
 	{ .compatible = "adi,ad4632-24", .data = &ad4632_24_chip_info },
+	{ .compatible = "adi,adaq4216", .data = &adaq4216_chip_info },
+	{ .compatible = "adi,adaq4224", .data = &adaq4224_chip_info },
 	{ }
 };
 MODULE_DEVICE_TABLE(of, ad4030_of_match);
Signature · 1 line
2.39.2
#17 lore raw

Re: [PATCH v5 7/7] iio: adc: ad4030: Add support for ADAQ4216 and ADAQ4224

Quoted 8 lines · Marcelo Schmitt

ADAQ4216 and ADAQ4224 are similar to AD4030, but feature a PGA circuitry that scales the analog input signal prior to it reaching the ADC. The PGA is controlled through a pair of pins (A0 and A1) whose state define the gain that is applied to the input signal.

Add support for ADAQ4216 and ADAQ4224. Provide a list of PGA options through the IIO device channel scale available interface and enable control of the PGA through the channel scale interface.

...

drivers/iio/adc/ad4030.c enum { · line 160

Quoted 8 lines
drivers/iio/adc/ad4030.c +8 -0
+/*
+ * Gains computed as fractions of 1000 so they can be expressed by integers.
+ */
+static const int adaq4216_hw_gains_vpv[] = {
+	MILLI / 3,		/* 333 */
+	(5 * MILLI / 9),	/* 555 */
+	(20 * MILLI / 9),	/* 2222 */
+	(20 * MILLI / 3),	/* 6666 */

Redundant parentheses, or do you mean to make multiplication first? E.g., (5 * MILL) / 9 ?

Quoted 1 line

+};

...

Quoted 13 lines
drivers/iio/adc/ad4030.c +13 -0
+static void ad4030_fill_scale_avail(struct ad4030_state *st)
+{
+	unsigned int mag_bits, int_part, fract_part, i;
+	u64 range;
+
+	/*
+	 * The maximum precision of differential channels is retrieved from the
+	 * chip properties. The output code of differential channels is in two's
+	 * complement format (i.e. signed), so the MSB is the sign bit and only
+	 * (precision_bits - 1) bits express voltage magnitude.
+	 */
+	mag_bits = st->chip->precision_bits - 1;
+

drivers/iio/adc/ad4030.c static const struct regmap_config ad4030_regmap_config = { · line 473

Quoted 10 lines
drivers/iio/adc/ad4030.c +10 -0
+	for (i = 0; i < ARRAY_SIZE(adaq4216_hw_gains_frac); i++) {
+		range = mult_frac(st->vref_uv, adaq4216_hw_gains_frac[i][1],
+				  adaq4216_hw_gains_frac[i][0]);
+		/*
+		 * If range were in mV, we would multiply it by NANO below.
+		 * Though, range is in µV so multiply it by MICRO only so the
+		 * result after right shift and division scales output codes to
+		 * millivolts.
+		 */
+		int_part = div_u64_rem(((u64)range * MICRO) >> mag_bits, NANO, &fract_part);

The "range" is of type u64. Any specific reason why cast?

Quoted 4 lines
drivers/iio/adc/ad4030.c +4 -0
+		st->scale_avail[i][0] = int_part;
+		st->scale_avail[i][1] = fract_part;
+	}
+}

...

drivers/iio/adc/ad4030.c static int ad4030_spi_offload_setup(struct iio_dev *indio_dev, · line 1441

Quoted 12 lines
drivers/iio/adc/ad4030.c +12 -0
+static int ad4030_setup_pga(struct device *dev, struct iio_dev *indio_dev,
+			    struct ad4030_state *st)
+{
+	/* Setup GPIOs for PGA control */
+	st->pga_gpios = devm_gpiod_get_array(dev, "pga", GPIOD_OUT_LOW);
+	if (IS_ERR(st->pga_gpios))
+		return dev_err_probe(dev, PTR_ERR(st->pga_gpios),
+				     "Failed to get PGA gpios.\n");
+
+	if (st->pga_gpios->ndescs != ADAQ4616_PGA_PINS)
+		return dev_err_probe(dev, -EINVAL,
+				     "Expected 2 GPIOs for PGA control.\n");

2 --> %d and constant or __stringify(MY_COOL_CONSTANT). However, I am not sure if the latter is acceptable in IIO.

Quoted 6 lines
drivers/iio/adc/ad4030.c +6 -0
+
+	st->scale_avail_size = ARRAY_SIZE(adaq4216_hw_gains_vpv);
+	st->pga_index = 0;
+
+	return 0;
+}
Signature · 2 lines
With Best Regards,
Andy Shevchenko