Skip to contentSkip to conversation

Add support for AD5706R DAC

PATCH Superseded Alexis Czezar Torreno 5 participants 9 messages Apr 10 – Apr 15

Subsystems (4)

v7 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Alexis Czezar Torreno

author maintainer
#0 lore raw

v7 cover Add support for AD5706R DAC

This series adds support for the Analog Devices AD5706R, a 4-channel 16-bit current output digital-to-analog converter with SPI interface.

The AD5706R features:
- 4 independent current output DAC channels
- Configurable output ranges (50mA, 150mA, 200mA, 300mA)
- Hardware and software LDAC trigger with configurable edge selection
- Toggle and dither modes per channel
- Internal or external voltage reference selection
- PWM-controlled LDAC
- Dynamic change SPI speed

The driver exposes standard IIO raw/scale/offset channel attributes for DAC output control, sampling frequency for PWM-based LDAC timing, and extended attributes for device configuration including output range selection, trigger mode, and multiplexer output.

This driver is developed and tested on the Cora Z7S platform using the AXI SPI Engine and AXI CLKGEN IP cores. The 'clocks' property enables dynamic SPI clock rate management via the CLKGEN.

Datasheet: https://www.analog.com/en/products/ad5706r.html

---
Changes in v7:
- driver:
- Moved/added size validation before data access in write()/read()
- Link to v6: https://lore.kernel.org/r/[email protected]

Changes in v6:
- driver:
- Added size validation in regmap_write()
- Used &st->tx_buf[0] consistently in _be32/be16 calls
- Added missing indent in AD5706R_CHAN
- Link to v5: https://lore.kernel.org/r/[email protected]

Changes in v5:
- dt-bindings:
- Changed out-en-gpios to enable-gpios
- driver:
- Kconfig: Added select REGMAP_SPI
- Headers: Removed device.h, errno.h, string.h; added dev_printk.h
- Use IIO_DMA_MINALIGN instead of ARCH_DMA_MINALIGN
- Replaced memcpy/memset with put_unaligned_be* for consistency
- Added struct device *dev shorthand in probe()
- other minor style edits
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- dt-bindings:
- Reverted pwm and gpio entries.
- Added missing power supply properties
- Clocks not added back as they were driver specific, not device properties
- driver:
- Added missing includes
- Converted to use regmap with custom SPI bus implementation. spi_write_then_read not applied as suggested, prevents future
need to change SPI speed
- removed driver speciifc mutex/guards in favor of regmap internal locking
- Minor style cleanups
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- Added MAINTAINERS entry, files added on each patch
- dt-bindings:
- Added allOf and ref to spi-peripheral-props.yaml
- Changed additionalProperties to unevaluatedProperties
- Added avdd-supply property and added it to required
- driver:
- Removed redundant includes, added respective includes of APIs used
- Simplified bit manipulation in SPI read/write, used feedback from v2
- Fixed inconsistent trailing commas in device ID tables
- Removed zero initialization in spi_device_id
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Stripped driver down to basic DAC functionality (read/write raw, read-only scale) as suggested.
- Removed PWM (LDAC), GPIO (reset/shutdown), clock generator,
SPI engine frequency switching, debugfs streaming, and all
custom ext_info sysfs attributes
- Removed regmap, IIO_BUFFER, and iio/sysfs.h dependencies
- Simplified SPI read/write to use standard spi_sync_transfer
without clock mode logic
- Scale reports default 50mA range as read-only using IIO_VAL_FRACTIONAL_LOG2; writable range selection deferred
to future follow-up series
- Simplified DT binding to only require compatible, reg, and spi-max-frequency
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Alexis Czezar Torreno (2):
dt-bindings: iio: dac: Add ADI AD5706R
iio: dac: ad5706r: Add support for AD5706R DAC

 .../devicetree/bindings/iio/dac/adi,ad5706r.yaml   | 105 +++++++++
 MAINTAINERS                                        |   8 +
 drivers/iio/dac/Kconfig                            |  11 +
 drivers/iio/dac/Makefile                           |   1 +
 drivers/iio/dac/ad5706r.c                          | 253 +++++++++++++++++++++
 5 files changed, 378 insertions(+)

---
base-commit: 3674f3ca92730d9a07b42b311f1337d83c4d5605
change-id: 20260220-dev_ad5706r-2105e1dd29ab

Best regards,

Signature · 1 line
Alexis Czezar Torreno <[email protected]>
author maintainer
#1 lore raw

PATCH v7 1/2 dt-bindings: iio: dac: Add ADI AD5706R

Add device tree binding documentation for the Analog Devices AD5706R 4-channel 16-bit current output digital-to-analog converter.

---
Changes in v5:
- Changed out-en-gpios to enable-gpios.

Changes in v4:
- Reverted pwm and gpio entries
- Added missing power supply properties
- Clocks not added back as they were driver specific

Changes in v3:
- Added allOf and ref to spi-peripheral-props.yaml
- Changed additionalProperties to unevaluatedProperties
- Added avdd-supply property and added it to required

Changes in v1:
- Removed clocks, clock-names, pwms, pwm-names, gpio properties
- Simplified example to use plain SPI bus
---

---
 .../devicetree/bindings/iio/dac/adi,ad5706r.yaml   | 105 +++++++++++++++++++++
 MAINTAINERS                                        |   7 ++
 2 files changed, 112 insertions(+)
Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml new file +105 -0
new file mode 100644
index 0000000000000000000000000000000000000000..19cc744a9f0fc35907de8b8bdd9f088676620b54
@@ -0,0 +1,105 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/dac/adi,ad5706r.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Analog Devices AD5706R 4-Channel Current Output DAC
+
+maintainers:
+  - Alexis Czezar Torreno <[email protected]>
+
+description: |
+  The AD5706R is a 4-channel, 16-bit resolution, current output
+  digital-to-analog converter (DAC) with programmable output current
+  ranges (50mA, 150mA, 200mA, 300mA), an integrated 2.5V voltage
+  reference, and load DAC, A/B toggle, and dither functions.
+
+  Datasheet:
+    https://www.analog.com/en/products/ad5706r.html
+
+properties:
+  compatible:
+    enum:
+      - adi,ad5706r
+
+  reg:
+    maxItems: 1
+
+  avdd-supply:
+    description: Analog power supply (2.9V to 3.6V).
+
+  iovdd-supply:
+    description: Logic power supply (1.14V to 1.89V).
+
+  pvdd0-supply:
+    description: Power supply for IDAC0 channel (1.65V to AVDD).
+
+  pvdd1-supply:
+    description: Power supply for IDAC1 channel (1.65V to AVDD).
+
+  pvdd2-supply:
+    description: Power supply for IDAC2 channel (1.65V to AVDD).
+
+  pvdd3-supply:
+    description: Power supply for IDAC3 channel (1.65V to AVDD).
+
+  vref-supply:
+    description:
+      Optional external 2.5V voltage reference. If not provided, the
+      internal 2.5V reference is used.
+
+  pwms:
+    maxItems: 1
+    description:
+      Optional PWM connected to the LDAC/TGP/DCK pin for hardware
+      triggered DAC updates, toggle, or dither clock generation.
+
+  reset-gpios:
+    maxItems: 1
+    description:
+      GPIO connected to the active low RESET pin. If not provided,
+      software reset is used.
+
+  enable-gpios:
+    maxItems: 1
+    description:
+      GPIO connected to the active low OUT_EN pin. Controls whether
+      the current outputs are enabled or in high-Z/ground state.
+
+required:
+  - compatible
+  - reg
+  - avdd-supply
+  - iovdd-supply
+
+allOf:
+  - $ref: /schemas/spi/spi-peripheral-props.yaml#
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/gpio/gpio.h>
+
+    spi {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        dac@0 {
+            compatible = "adi,ad5706r";
+            reg = <0>;
+            avdd-supply = <&avdd>;
+            iovdd-supply = <&iovdd>;
+            pvdd0-supply = <&pvdd>;
+            pvdd1-supply = <&pvdd>;
+            pvdd2-supply = <&pvdd>;
+            pvdd3-supply = <&pvdd>;
+            vref-supply = <&vref>;
+            spi-max-frequency = <50000000>;
+            pwms = <&pwm0 0 1000000 0>;
+            reset-gpios = <&gpio0 10 GPIO_ACTIVE_LOW>;
+            enable-gpios = <&gpio0 12 GPIO_ACTIVE_LOW>;
+        };
+    };
+...
MAINTAINERS +7 -0
index 1251965d70bdfa990c66966cd77f7ab52ae3385f..17a3d2d45fccb9cd3c93fd35666fb85d17d53cde 100644
@@ -1496,6 +1496,13 @@ W:	https://ez.analog.com/linux-software-drivers
 F:	Documentation/devicetree/bindings/iio/adc/adi,ad4851.yaml
 F:	drivers/iio/adc/ad4851.c
 
+ANALOG DEVICES INC AD5706R DRIVER
+M:	Alexis Czezar Torreno <[email protected]>
+L:	[email protected]
+S:	Supported
+W:	https://ez.analog.com/linux-software-drivers
+F:	Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml
+
 ANALOG DEVICES INC AD7091R DRIVER
 M:	Marcelo Schmitt <[email protected]>
 L:	[email protected]
Signature · 1 line
2.34.1
author maintainer
#2 lore raw

PATCH v7 2/2 iio: dac: ad5706r: Add support for AD5706R DAC

In reply to Alexis Czezar Torreno

Add support for the Analog Devices AD5706R, a 4-channel 16-bit current output digital-to-analog converter with SPI interface.

Features:
- 4 independent DAC channels
- Hardware and software LDAC trigger
- Configurable output range
- PWM-based LDAC control
- Dither and toggle modes
- Dynamically configurable SPI speed

---
Changes in v7:
- Moved/added size validation before data access in write()/read()

Changes in v6:
- Added size validation in regmap_write()
- Used &st->tx_buf[0] consistently _be32/be16 calls
- Added missing indent in AD5706R_CHAN

Changes in v5:
- Kconfig: Added select REGMAP_SPI dependency
- Headers: Removed device.h, errno.h, string.h; added dev_printk.h
- Use IIO_DMA_MINALIGN instead of ARCH_DMA_MINALIGN
- Replaced memcpy/memset with put_unaligned_be* for consistency
- Added struct device *dev shorthand in probe()
- Added newline to error message
- Other minor style edits

Changes in v4:
- Added missing includes
- Converted to use regmap with custom SPI bus implementation
- Removed driver-specific mutex/guards in favor of regmap locking
- Minor style cleanups

Changes in v3:
- Removed redundant includes, added respective includes of APIs used
- Simplified bit manipulation in SPI read/write
- Fixed inconsistent trailing commas in device ID tables
- Removed zero initialization in spi_device_id

Changes in v2:
- Removed PWM, GPIO, clock generator, debugfs, regmap, IIO_BUFFER
- Removed all custom ext_info sysfs attributes
- Simplified to basic raw read/write and read-only scale
- SPI read/write can handle multibyte registers
---

---
 MAINTAINERS               |   1 +
 drivers/iio/dac/Kconfig   |  11 ++
 drivers/iio/dac/Makefile  |   1 +
 drivers/iio/dac/ad5706r.c | 253 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 266 insertions(+)
MAINTAINERS +1 -0
index 17a3d2d45fccb9cd3c93fd35666fb85d17d53cde..3d7bd98b4d1b55836e40687a9a3ac9f4935a8acb 100644
@@ -1502,6 +1502,7 @@ L:	[email protected]
 S:	Supported
 W:	https://ez.analog.com/linux-software-drivers
 F:	Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml
+F:	drivers/iio/dac/ad5706r.c
 
 ANALOG DEVICES INC AD7091R DRIVER
 M:	Marcelo Schmitt <[email protected]>
drivers/iio/dac/Kconfig +11 -0
index db9f5c711b3df90641f017652fbbef594cc1627d..a5a328818233e3d019cddaee369dd5b7b1529031 100644
@@ -178,6 +178,17 @@ config AD5624R_SPI
 	  Say yes here to build support for Analog Devices AD5624R, AD5644R and
 	  AD5664R converters (DAC). This driver uses the common SPI interface.
 
+config AD5706R
+	tristate "Analog Devices AD5706R DAC driver"
+	depends on SPI
+	select REGMAP_SPI
+	help
+	  Say yes here to build support for Analog Devices AD5706R 4-channel,
+	  16-bit current output DAC.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called ad5706r.
+
 config AD9739A
 	tristate "Analog Devices AD9739A RF DAC spi driver"
 	depends on SPI
drivers/iio/dac/Makefile +1 -0
index 2a80bbf4e80ad557da79ed916027cedff286984b..0034317984985035f7987a744899924bfd4612e3 100644
@@ -21,6 +21,7 @@ obj-$(CONFIG_AD5449) += ad5449.o
 obj-$(CONFIG_AD5592R_BASE) += ad5592r-base.o
 obj-$(CONFIG_AD5592R) += ad5592r.o
 obj-$(CONFIG_AD5593R) += ad5593r.o
+obj-$(CONFIG_AD5706R) += ad5706r.o
 obj-$(CONFIG_AD5755) += ad5755.o
 obj-$(CONFIG_AD5758) += ad5758.o
 obj-$(CONFIG_AD5761) += ad5761.o
drivers/iio/dac/ad5706r.c new file +253 -0

253 lines not on this page · raw message

Signature · 1 line
2.34.1
reviewer
#3 lore raw

Re: [PATCH v7 2/2] iio: dac: ad5706r: Add support for AD5706R DAC

Quoted 3 lines · Alexis Czezar Torreno

Add support for the Analog Devices AD5706R, a 4-channel 16-bit current output digital-to-analog converter with SPI interface.

...

Quoted 9 lines
drivers/iio/dac/ad5706r.c new file +9 -0
+static int ad5706r_regmap_write(void *context, const void *data, size_t count)
+{
+	struct ad5706r_state *st = context;
+	unsigned int num_bytes, val;
+	u16 reg;
+
+	if (count != 4)
+		return -EINVAL;
+

drivers/iio/dac/ad5706r.c line 71

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+	reg = get_unaligned_be16(data);
+	num_bytes = ad5706r_reg_len(reg);
+
+	struct spi_transfer xfer = {
+		.tx_buf = st->tx_buf,
+		.len = num_bytes + 2,
+	};
+
+	val = get_unaligned_be32(data);
+	put_unaligned_be32(val, &st->tx_buf[0]);

Can't we just do memcpy() instead of swapping the byte order twice?

drivers/iio/dac/ad5706r.c line 77

Quoted 6 lines
drivers/iio/dac/ad5706r.c new file +6 -0
+
+	/* For single byte, copy the data to the correct position */
+	if (num_bytes == AD5706R_SINGLE_BYTE_LEN)
+		st->tx_buf[2] = st->tx_buf[3];
+
+	return spi_sync_transfer(st->spi, &xfer, 1);

There isn't any special paramters in the xfer struct, so spi_write() should work here and save a bit of code.

	return spi_write(st->spi, data, num_bytes);
Quoted 16 lines
drivers/iio/dac/ad5706r.c new file +16 -0
+}
+
+static int ad5706r_regmap_read(void *context, const void *reg_buf,
+			       size_t reg_size, void *val_buf, size_t val_size)
+{
+	struct ad5706r_state *st = context;
+	unsigned int num_bytes;
+	u16 reg, cmd, val;
+	int ret;
+
+	if (reg_size != 2 || val_size != 2)
+		return -EINVAL;
+
+	reg = get_unaligned_be16(reg_buf);
+	num_bytes = ad5706r_reg_len(reg);
+

drivers/iio/dac/ad5706r.c line 103

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+	/* Full duplex, device responds immediately after command */
+	struct spi_transfer xfer = {
+		.tx_buf = st->tx_buf,
+		.rx_buf = st->rx_buf,
+		.len = 2 + num_bytes,
+	};
+
+	cmd = AD5706R_RD_MASK | (reg & AD5706R_ADDR_MASK);
+	put_unaligned_be16(cmd, &st->tx_buf[0]);
+	put_unaligned_be16(0, &st->tx_buf[2]);

Do we actually need to write 0s while reading?

Usually, we would just do a spi_write_then_read for something like this.

Quoted 6 lines
drivers/iio/dac/ad5706r.c new file +5 -0
+
+	ret = spi_sync_transfer(st->spi, &xfer, 1);
+	if (ret)
+		return ret;
+
 

drivers/iio/dac/ad5706r.c line 117

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +9 -0
 
+	/* Extract value from response (skip 2-byte command echo) */
+	if (num_bytes == AD5706R_SINGLE_BYTE_LEN)
+		val = st->rx_buf[2];
+	else if (num_bytes == AD5706R_DOUBLE_BYTE_LEN)
+		val = get_unaligned_be16(&st->rx_buf[2]);
+	else
+		return -EINVAL;
+
+	put_unaligned_be16(val, val_buf);

Can't this all be simplified to memcpy(val_buf, &st->rx_buf[2], num_bytes); ?

Or the whole thing simplified to:

	return spi_write_then_read(st->spi, reg_buf, 2, val_buf, num_bytes);
Quoted 4 lines
drivers/iio/dac/ad5706r.c new file +4 -0
+
+	return 0;
+}
+
author maintainer
#4 lore raw

RE: [PATCH v7 2/2] iio: dac: ad5706r: Add support for AD5706R DAC

Quoted 13 lines
Quoted 13 lines
drivers/iio/dac/ad5706r.c new file +13 -0
+static int ad5706r_regmap_write(void *context, const void *data,
+size_t count) {
+	struct ad5706r_state *st = context;
+	unsigned int num_bytes, val;
+	u16 reg;
+
+	if (count != 4)
+		return -EINVAL;
+
+	reg = get_unaligned_be16(data);
+	num_bytes = ad5706r_reg_len(reg);
+
+	struct spi_transfer xfer = {
Quoted 8 lines

drivers/iio/dac/ad5706r.c line 71

Quoted 6 lines
drivers/iio/dac/ad5706r.c new file +6 -0
+		.tx_buf = st->tx_buf,
+		.len = num_bytes + 2,
+	};
+
+	val = get_unaligned_be32(data);
+	put_unaligned_be32(val, &st->tx_buf[0]);

Can't we just do memcpy() instead of swapping the byte order twice?

This was memcpy before, was changed to this for consistency as per Andy's suggestion. It was during v5.

Removal of this plus the other mem* commands allowed removal of string.h in the headers.

Quoted 11 lines

drivers/iio/dac/ad5706r.c line 77

Quoted 7 lines
drivers/iio/dac/ad5706r.c new file +6 -0
+
+	/* For single byte, copy the data to the correct position */
+	if (num_bytes == AD5706R_SINGLE_BYTE_LEN)
+		st->tx_buf[2] = st->tx_buf[3];
+
+	return spi_sync_transfer(st->spi, &xfer, 1);

There isn't any special paramters in the xfer struct, so spi_write() should work here and save a bit of code.

Quoted 1 line
	return spi_write(st->spi, data, num_bytes);

Back in v3-v4, Joathan suggested this to be spi_write_then_read() as it was much safer, but I requested to keep it this way since I would be adding more features to this driver like changing spi_speeds, which cannot be done if using spi_write_then_read

Quoted 29 lines
Quoted 16 lines
drivers/iio/dac/ad5706r.c new file +15 -0
+}
+
+static int ad5706r_regmap_read(void *context, const void *reg_buf,
+			       size_t reg_size, void *val_buf, size_t val_size) {
+	struct ad5706r_state *st = context;
+	unsigned int num_bytes;
+	u16 reg, cmd, val;
+	int ret;
+
+	if (reg_size != 2 || val_size != 2)
+		return -EINVAL;
+
+	reg = get_unaligned_be16(reg_buf);
+	num_bytes = ad5706r_reg_len(reg);
+

drivers/iio/dac/ad5706r.c line 103

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+	/* Full duplex, device responds immediately after command */
+	struct spi_transfer xfer = {
+		.tx_buf = st->tx_buf,
+		.rx_buf = st->rx_buf,
+		.len = 2 + num_bytes,
+	};
+
+	cmd = AD5706R_RD_MASK | (reg & AD5706R_ADDR_MASK);
+	put_unaligned_be16(cmd, &st->tx_buf[0]);
+	put_unaligned_be16(0, &st->tx_buf[2]);

Do we actually need to write 0s while reading?

Quoted 1 line

Usually, we would just do a spi_write_then_read for something like this.

Technically it's a don't care data, zero just makes it cleaner signals during debug

Discussed spi_write_then_read in a comment above

Quoted 20 lines
Quoted 7 lines
drivers/iio/dac/ad5706r.c new file +5 -0
+
+	ret = spi_sync_transfer(st->spi, &xfer, 1);
+	if (ret)
+		return ret;
+
 

drivers/iio/dac/ad5706r.c line 117

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +9 -0
 
+	/* Extract value from response (skip 2-byte command echo) */
+	if (num_bytes == AD5706R_SINGLE_BYTE_LEN)
+		val = st->rx_buf[2];
+	else if (num_bytes == AD5706R_DOUBLE_BYTE_LEN)
+		val = get_unaligned_be16(&st->rx_buf[2]);
+	else
+		return -EINVAL;
+
+	put_unaligned_be16(val, val_buf);

Can't this all be simplified to memcpy(val_buf, &st->rx_buf[2], num_bytes); ?

Quoted 4 lines

Or the whole thing simplified to:

	return spi_write_then_read(st->spi, reg_buf, 2, val_buf, num_bytes);

as discussed above about mem* and spi_write_then_read

#5 lore raw

Re: [PATCH v7 2/2] iio: dac: ad5706r: Add support for AD5706R DAC

On Mon, Apr 13, 2026 at 10:16 AM Torreno, Alexis Czezar <[email protected]> wrote:

...

Quoted 11 lines
Quoted 11 lines

drivers/iio/dac/ad5706r.c line 117

Quoted 9 lines
drivers/iio/dac/ad5706r.c new file +9 -0
+   /* Extract value from response (skip 2-byte command echo) */
+   if (num_bytes == AD5706R_SINGLE_BYTE_LEN)
+           val = st->rx_buf[2];
+   else if (num_bytes == AD5706R_DOUBLE_BYTE_LEN)
+           val = get_unaligned_be16(&st->rx_buf[2]);
+   else
+           return -EINVAL;
+
+   put_unaligned_be16(val, val_buf);

Can't this all be simplified to memcpy(val_buf, &st->rx_buf[2], num_bytes); ?

Also note, num_bytes still needs validation.

Quoted 5 lines
Quoted 3 lines

Or the whole thing simplified to:

      return spi_write_then_read(st->spi, reg_buf, 2, val_buf, num_bytes);

as discussed above about mem* and spi_write_then_read

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

Re: [PATCH v7 2/2] iio: dac: ad5706r: Add support for AD5706R DAC

In reply to Alexis Czezar Torreno
Quoted 11 lines · On Fri, 10 Apr 2026 14:48:17 +0800 Alexis Czezar Torreno…

Add support for the Analog Devices AD5706R, a 4-channel 16-bit current output digital-to-analog converter with SPI interface.

Features:
- 4 independent DAC channels
- Hardware and software LDAC trigger
- Configurable output range
- PWM-based LDAC control
- Dither and toggle modes
- Dynamically configurable SPI speed

Quoted 3 lines

Hi Alexis,

A few little comments from me.

Thanks,

Jonathan

Quoted 26 lines
drivers/iio/dac/ad5706r.c new file +20 -0
new file mode 100644
index 0000000000000000000000000000000000000000..026f871ce121cd63331b2a34da8879491b2d0f3c
@@ -0,0 +1,253 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * AD5706R 16-bit Current Output Digital to Analog Converter
+ *
+ * Copyright 2026 Analog Devices Inc.
+ */
+
+#include <linux/array_size.h>
+#include <linux/bits.h>
+#include <linux/dev_printk.h>
+#include <linux/err.h>
+#include <linux/iio/iio.h>
+#include <linux/minmax.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+#include <linux/regmap.h>
+#include <linux/spi/spi.h>
+#include <linux/types.h>
+#include <linux/unaligned.h>
+

drivers/iio/dac/ad5706r.c line 30

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+/* SPI frame layout */
+#define AD5706R_RD_MASK			BIT(15)
+#define AD5706R_ADDR_MASK		GENMASK(11, 0)
+
+/* Registers */
+#define AD5706R_REG_DAC_INPUT_A_CH(x)		(0x60 + ((x) * 2))
+#define AD5706R_REG_DAC_DATA_READBACK_CH(x)	(0x68 + ((x) * 2))
+
+#define AD5706R_DAC_RESOLUTION		16
+#define AD5706R_DAC_MAX_CODE		BIT(16)

Trivial but I'd expect something called MAX_CODE to be GENMASK(15, 0) not BIT(16). E.g. inclusive limit.

drivers/iio/dac/ad5706r.c line 35

Quoted 5 lines
drivers/iio/dac/ad5706r.c new file +5 -0
+#define AD5706R_MULTIBYTE_REG_START	0x14
+#define AD5706R_MULTIBYTE_REG_END	0x71
+#define AD5706R_MAX_REG			0x77
+#define AD5706R_SINGLE_BYTE_LEN		1
+#define AD5706R_DOUBLE_BYTE_LEN		2

See below. I'm not sure these two defines bring us anything.

drivers/iio/dac/ad5706r.c line 48

Quoted 13 lines
drivers/iio/dac/ad5706r.c new file +13 -0
+
+struct ad5706r_state {
+	struct spi_device *spi;
+	struct regmap *regmap;
+
+	u8 tx_buf[4] __aligned(IIO_DMA_MINALIGN);
+	u8 rx_buf[4];
+};
+
+static int ad5706r_reg_len(unsigned int reg)
+{
+	if (reg >= AD5706R_MULTIBYTE_REG_START && reg <= AD5706R_MULTIBYTE_REG_END)
+		return AD5706R_DOUBLE_BYTE_LEN;

What do the defines for 2 == 2-bytes and 1 == 1-byte bring us over using numbers directly? E.g.

	if (reg >= AD5706R_MULTIBYTE_REG_START && reg <= AD5706R_MULTIBYTE_REG_END)
		return 2;
	return 1;
Quoted 17 lines
drivers/iio/dac/ad5706r.c new file +17 -0
+
+	return AD5706R_SINGLE_BYTE_LEN;
+}
+
+static int ad5706r_regmap_write(void *context, const void *data, size_t count)
+{
+	struct ad5706r_state *st = context;
+	unsigned int num_bytes, val;
+	u16 reg;
+
+	if (count != 4)
+		return -EINVAL;
+
+	reg = get_unaligned_be16(data);
+	num_bytes = ad5706r_reg_len(reg);
+
+	struct spi_transfer xfer = {

drivers/iio/dac/ad5706r.c line 75

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+		.tx_buf = st->tx_buf,
+		.len = num_bytes + 2,
+	};
+
+	val = get_unaligned_be32(data);
+	put_unaligned_be32(val, &st->tx_buf[0]);
+
+	/* For single byte, copy the data to the correct position */
+	if (num_bytes == AD5706R_SINGLE_BYTE_LEN)
+		st->tx_buf[2] = st->tx_buf[3];

This does feel a bit odd vs using if / else if as you do in the read case. Also, same as above wrt to single bytes having a length of 1 meaning that just using a 1 might be easier to read.

Quoted 4 lines
drivers/iio/dac/ad5706r.c new file +3 -0
+
+	return spi_sync_transfer(st->spi, &xfer, 1);
+}
 

drivers/iio/dac/ad5706r.c line 157

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+static int ad5706r_write_raw(struct iio_dev *indio_dev,
+			     struct iio_chan_spec const *chan,
+			     int val, int val2, long mask)
+{
+	struct ad5706r_state *st = iio_priv(indio_dev);
+	unsigned int reg;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		if (!in_range(val, 0, AD5706R_DAC_MAX_CODE))

I'm not seeing a strong reason to use in_range() here (hopefully I didn't suggest it in an earlier review ;) It make sense when we have a val >= base && val < base + length. With base as 0 and MAX_CODE not 'obviously' from it's name being the length (it only is becauset he base is 0) this seems odd.

		if (val < 0 || val >= AD5706R_DAC_MAX_CODE)

Though see above on MAX_CODE not being the maximum code...

Quoted 8 lines
drivers/iio/dac/ad5706r.c new file +8 -0
+			return -EINVAL;
+
+		reg = AD5706R_REG_DAC_INPUT_A_CH(chan->channel);
+		return regmap_write(st->regmap, reg, val);
+	default:
+		return -EINVAL;
+	}
+}
author maintainer
#7 lore raw

RE: [PATCH v7 2/2] iio: dac: ad5706r: Add support for AD5706R DAC

Quoted 5 lines

drivers/iio/dac/ad5706r.c line 30

Quoted 2 lines
drivers/iio/dac/ad5706r.c new file +2 -0
+#define AD5706R_DAC_RESOLUTION		16
+#define AD5706R_DAC_MAX_CODE		BIT(16)

Trivial but I'd expect something called MAX_CODE to be GENMASK(15, 0) not BIT(16). E.g. inclusive limit.

Since it's dac code I guess this makes better sense, will edit.

Quoted 26 lines

drivers/iio/dac/ad5706r.c line 35

Quoted 6 lines
drivers/iio/dac/ad5706r.c new file +5 -0
+#define AD5706R_MULTIBYTE_REG_START	0x14
+#define AD5706R_MULTIBYTE_REG_END	0x71
+#define AD5706R_MAX_REG			0x77
+#define AD5706R_SINGLE_BYTE_LEN		1
+#define AD5706R_DOUBLE_BYTE_LEN		2

See below. I'm not sure these two defines bring us anything.

drivers/iio/dac/ad5706r.c

Quoted 11 lines
drivers/iio/dac/ad5706r.c new file +11 -0
+
+struct ad5706r_state {
+	struct spi_device *spi;
+	struct regmap *regmap;
+
+	u8 tx_buf[4] __aligned(IIO_DMA_MINALIGN);
+	u8 rx_buf[4];
+};
+
+static int ad5706r_reg_len(unsigned int reg) {
+	if (reg >= AD5706R_MULTIBYTE_REG_START && reg <=

AD5706R_MULTIBYTE_REG_END)

drivers/iio/dac/ad5706r.c line 48

Quoted 1 line
drivers/iio/dac/ad5706r.c new file +1 -0
+		return AD5706R_DOUBLE_BYTE_LEN;

What do the defines for 2 == 2-bytes and 1 == 1-byte bring us over using numbers directly? E.g.

Quoted 5 lines
	if (reg >= AD5706R_MULTIBYTE_REG_START && reg <=
AD5706R_MULTIBYTE_REG_END)
		return 2;
	return 1;

I suppose not much, it won't change in the future so a hard "1" or "2" can work. Will remove the defines and just put the actual number

Quoted 23 lines
Quoted 23 lines
drivers/iio/dac/ad5706r.c new file +23 -0
+
+	return AD5706R_SINGLE_BYTE_LEN;
+}
+
+static int ad5706r_regmap_write(void *context, const void *data,
+size_t count) {
+	struct ad5706r_state *st = context;
+	unsigned int num_bytes, val;
+	u16 reg;
+
+	if (count != 4)
+		return -EINVAL;
+
+	reg = get_unaligned_be16(data);
+	num_bytes = ad5706r_reg_len(reg);
+
+	struct spi_transfer xfer = {
+		.tx_buf = st->tx_buf,
+		.len = num_bytes + 2,
+	};
+
+	val = get_unaligned_be32(data);
+	put_unaligned_be32(val, &st->tx_buf[0]);
Quoted 8 lines

drivers/iio/dac/ad5706r.c line 75

Quoted 4 lines
drivers/iio/dac/ad5706r.c new file +4 -0
+
+	/* For single byte, copy the data to the correct position */
+	if (num_bytes == AD5706R_SINGLE_BYTE_LEN)
+		st->tx_buf[2] = st->tx_buf[3];

This does feel a bit odd vs using if / else if as you do in the read case. Also, same as above wrt to single bytes having a length of
1 meaning that just using a 1 might be easier to read.

Unlike in the read, I thought it would be simpler to copy the whole data of 4bytes, then just correct the buffer during single_byte.
Should I make it similarly coded with the read()?

Quoted 19 lines

drivers/iio/dac/ad5706r.c line 157

Quoted 13 lines
drivers/iio/dac/ad5706r.c new file +11 -0
+
+	return spi_sync_transfer(st->spi, &xfer, 1); }
 
+static int ad5706r_write_raw(struct iio_dev *indio_dev,
+			     struct iio_chan_spec const *chan,
+			     int val, int val2, long mask) {
+	struct ad5706r_state *st = iio_priv(indio_dev);
+	unsigned int reg;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		if (!in_range(val, 0, AD5706R_DAC_MAX_CODE))

I'm not seeing a strong reason to use in_range() here (hopefully I didn't suggest it in an earlier review ;) It make sense when we have a val >= base && val < base + length. With base as 0 and MAX_CODE not 'obviously' from it's name being the length (it only is becauset he base is 0) this seems odd.

Quoted 3 lines
		if (val < 0 || val >= AD5706R_DAC_MAX_CODE) Though see

above on MAX_CODE not being the maximum code...

I think around v3 Andy suggested the use of in_range, as the function itself helps document what the line does. Is this a style preference?

Regards,
Alexis

#8 lore raw

Re: [PATCH v7 2/2] iio: dac: ad5706r: Add support for AD5706R DAC

On Wed, Apr 15, 2026 at 01:10:09AM +0000, Torreno, Alexis Czezar wrote:

...

Quoted 8 lines
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	case IIO_CHAN_INFO_RAW:
+		if (!in_range(val, 0, AD5706R_DAC_MAX_CODE))

I'm not seeing a strong reason to use in_range() here (hopefully I didn't suggest it in an earlier review ;) It make sense when we have a val >= base && val < base + length. With base as 0 and MAX_CODE not 'obviously' from it's name being the length (it only is becauset he base is 0) this seems odd.

Quoted 5 lines
Quoted 2 lines
		if (val < 0 || val >= AD5706R_DAC_MAX_CODE) Though see

above on MAX_CODE not being the maximum code...

I think around v3 Andy suggested the use of in_range, as the function itself helps document what the line does. Is this a style preference?

I believe it was me, who suggested that, but we can open code it. Indeed, it's just a style preference in this case.

Signature · 2 lines
With Best Regards,
Andy Shevchenko