Skip to contentSkip to conversation

Add support for AD5706R DAC

PATCH Superseded Alexis Czezar Torreno 7 participants 18 messages Feb 20 – Feb 23

Subsystems (5)

v1 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Alexis Czezar Torreno

author maintainer
#0 lore raw

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

---
Alexis Czezar Torreno (3):
dt-bindings: iio: dac: Add binding for AD5706R
iio: dac: ad5706r: Add support for AD5706R DAC
MAINTAINERS: Add entry for AD5706R DAC driver

 .../devicetree/bindings/iio/dac/adi,ad5706r.yaml   |   96 +
 MAINTAINERS                                        |    8 +
 drivers/iio/dac/Kconfig                            |   11 +
 drivers/iio/dac/Makefile                           |    1 +
 drivers/iio/dac/ad5706r.c                          | 2290 ++++++++++++++++++++
 5 files changed, 2406 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 1/3 dt-bindings: iio: dac: Add binding for AD5706R

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

---
 .../devicetree/bindings/iio/dac/adi,ad5706r.yaml   | 96 ++++++++++++++++++++++
 1 file changed, 96 insertions(+)
Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml new file +96 -0
new file mode 100644
index 0000000000000000000000000000000000000000..dabaf2195a07d2c66d44f69ca60e32e6bc37cf55
@@ -0,0 +1,96 @@
+# 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 16-bit, 4-channel current output digital-to-analog
+  converter with SPI interface.
+
+  The driver supports dynamic SPI clock rate management via an external
+  clock generator (e.g., AXI CLKGEN) referenced by the 'clocks' property.
+  This allows separate read and write SPI speeds to be configured at
+  runtime.
+
+  Datasheet:
+    https://www.analog.com/en/products/ad5706r.html
+
+properties:
+  compatible:
+    enum:
+      - adi,ad5706r
+
+  reg:
+    maxItems: 1
+
+  spi-max-frequency:
+    maximum: 100000000
+
+  clocks:
+    maxItems: 1
+    description:
+      Reference clock for SPI clock rate management.
+
+  clock-names:
+    items:
+      - const: spi_clk
+
+  pwms:
+    maxItems: 1
+
+  pwm-names:
+    items:
+      - const: ad5706r_ldacb
+
+  dac-resetb-gpios:
+    maxItems: 1
+    description: GPIO connected to the active-low reset pin.
+
+  dac-shdn-gpios:
+    maxItems: 1
+    description: GPIO connected to the active-high shutdown pin.
+
+required:
+  - compatible
+  - reg
+  - spi-max-frequency
+  - clocks
+  - clock-names
+  - pwms
+  - pwm-names
+  - dac-resetb-gpios
+  - dac-shdn-gpios
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/gpio/gpio.h>
+    #include <dt-bindings/interrupt-controller/irq.h>
+
+    spi {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        dac@0 {
+            compatible = "adi,ad5706r";
+            reg = <0>;
+            spi-max-frequency = <100000000>;
+
+            clocks = <&spi_clk>;
+            clock-names = "spi_clk";
+
+            dac-resetb-gpios = <&gpio0 86 GPIO_ACTIVE_LOW>;
+            dac-shdn-gpios = <&gpio0 87 GPIO_ACTIVE_HIGH>;
+
+            pwms = <&axi_pwm_gen 0 0>;
+            pwm-names = "ad5706r_ldacb";
+        };
+    };
+...
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/3] dt-bindings: iio: dac: Add binding for AD5706R

Quoted 2 lines · Alexis Czezar Torreno

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

A nit, subject: drop second/last, redundant "binding for". The "dt-bindings" prefix is already stating that these are bindings. See also:
https://elixir.bootlin.com/linux/v6.17-rc3/source/Documentation/devicetree/bindings/submitting-patches.rst#L18

Quoted 18 lines
---
 .../devicetree/bindings/iio/dac/adi,ad5706r.yaml   | 96 ++++++++++++++++++++++
 1 file changed, 96 insertions(+)
Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml new file +6 -0
new file mode 100644
index 0000000000000000000000000000000000000000..dabaf2195a07d2c66d44f69ca60e32e6bc37cf55
@@ -0,0 +1,96 @@
+# 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#
+

Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml line 16

Quoted 10 lines
Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml new file +10 -0
+title: Analog Devices AD5706R 4-Channel Current Output DAC
+
+maintainers:
+  - Alexis Czezar Torreno <[email protected]>
+
+description: |
+  The AD5706R is a 16-bit, 4-channel current output digital-to-analog
+  converter with SPI interface.
+
+  The driver supports dynamic SPI clock rate management via an external

So this is DAC or driver? If driver, what does it drive?

Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml line 19

Quoted 3 lines
Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml new file +3 -0
+  clock generator (e.g., AXI CLKGEN) referenced by the 'clocks' property.
+  This allows separate read and write SPI speeds to be configured at
+  runtime.

How binding has anything to do with runtime? What are you describing here?

Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml line 32

Quoted 13 lines
Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml new file +13 -0
+
+  Datasheet:
+    https://www.analog.com/en/products/ad5706r.html
+
+properties:
+  compatible:
+    enum:
+      - adi,ad5706r
+
+  reg:
+    maxItems: 1
+
+  spi-max-frequency:

So that's SPI then you miss $ref and unevaluatedProps. Look at other bindings.

Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml line 42

Quoted 10 lines
Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml new file +10 -0
+    maximum: 100000000
+
+  clocks:
+    maxItems: 1
+    description:
+      Reference clock for SPI clock rate management.
+
+  clock-names:
+    items:
+      - const: spi_clk

Drop clock names, pretty redundant.

Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml line 49

Quoted 7 lines
Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml new file +7 -0
+
+  pwms:
+    maxItems: 1
+
+  pwm-names:
+    items:
+      - const: ad5706r_ldacb

No point to add device name in the input name.

Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml line 51

Quoted 2 lines
Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml new file +2 -0
+
+  dac-resetb-gpios:

So reset-gpios?

Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml line 55

Quoted 4 lines
Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml new file +4 -0
+    maxItems: 1
+    description: GPIO connected to the active-low reset pin.
+
+  dac-shdn-gpios:

Please read gpio-consumer-common.yaml

Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml line 68

Quoted 13 lines
Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml new file +13 -0
+    maxItems: 1
+    description: GPIO connected to the active-high shutdown pin.
+
+required:
+  - compatible
+  - reg
+  - spi-max-frequency
+  - clocks
+  - clock-names
+  - pwms
+  - pwm-names
+  - dac-resetb-gpios
+  - dac-shdn-gpios

No supplies? How does it get power or reference voltage?

Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml line 69

Quoted 1 line
Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml new file +1 -0
+

Here, missing allOf with ref

Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml line 70

Quoted 1 line
Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml new file +1 -0
+additionalProperties: false

And that is unevaluated.

Best regards,
Krzysztof

reviewer
#3 lore raw

Re: [PATCH 1/3] dt-bindings: iio: dac: Add binding for AD5706R

In reply to Alexis Czezar Torreno
Quoted 23 lines · Alexis Czezar Torreno

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

---
 .../devicetree/bindings/iio/dac/adi,ad5706r.yaml   | 96 ++++++++++++++++++++++
 1 file changed, 96 insertions(+)
Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml new file +9 -0
new file mode 100644
index 0000000000000000000000000000000000000000..dabaf2195a07d2c66d44f69ca60e32e6bc37cf55
@@ -0,0 +1,96 @@
+# 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:

Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml line 19

Quoted 10 lines
Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml new file +10 -0
+  - Alexis Czezar Torreno <[email protected]>
+
+description: |
+  The AD5706R is a 16-bit, 4-channel current output digital-to-analog
+  converter with SPI interface.
+
+  The driver supports dynamic SPI clock rate management via an external
+  clock generator (e.g., AXI CLKGEN) referenced by the 'clocks' property.
+  This allows separate read and write SPI speeds to be configured at
+  runtime.

Devicetree is for describing how it is wired up, not how the driver uses it, so leave this part out.

Quoted 9 lines
Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml new file +9 -0
+
+  Datasheet:
+    https://www.analog.com/en/products/ad5706r.html
+
+properties:
+  compatible:
+    enum:
+      - adi,ad5706r
+

Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml line 38

Quoted 10 lines
Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml new file +10 -0
+  reg:
+    maxItems: 1
+
+  spi-max-frequency:
+    maximum: 100000000
+
+  clocks:
+    maxItems: 1
+    description:
+      Reference clock for SPI clock rate management.

Based on the driver, it sounds like this clock is not connected to this chip at all, but rather the SPI controller clock. So it does not belong here.

Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml line 49

Quoted 11 lines
Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml new file +11 -0
+
+  clock-names:
+    items:
+      - const: spi_clk
+
+  pwms:
+    maxItems: 1
+
+  pwm-names:
+    items:
+      - const: ad5706r_ldacb

Instead of giving it a name, we can just write it as:

pwms:
items:
- description: PWM connected to the LDAC/TGP/DCK pin.

The items: also limits it to exactly one item, so we don't need maxItems:.

Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml line 53

Quoted 4 lines
Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml new file +4 -0
+
+  dac-resetb-gpios:
+    maxItems: 1
+    description: GPIO connected to the active-low reset pin.

The datasheet just has a /RESET pin. Not sure where the "b" comes from.

Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml line 57

Quoted 4 lines
Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml new file +4 -0
+
+  dac-shdn-gpios:
+    maxItems: 1
+    description: GPIO connected to the active-high shutdown pin.

I didn't see a "shutdown" pin in the datasheet. Do you mean the OUT_EN pin?

Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml line 58

Quoted 1 line
Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml new file +1 -0
+

We could also add an io-channel for reading back the MUX_OUT pin from the ADC it is connected to.

Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml line 62

Quoted 4 lines
Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml new file +4 -0
+required:
+  - compatible
+  - reg
+  - spi-max-frequency

Voltage supplies should be required.

Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml line 68

Quoted 6 lines
Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml new file +6 -0
+  - clocks
+  - clock-names
+  - pwms
+  - pwm-names
+  - dac-resetb-gpios
+  - dac-shdn-gpios

I would not expect any of these to be required. Reset could be hardwired if there aren't enough GPIOs. And something other than a PWM could be connected to the LDAC/TGP/DCK pin.

The point of the bindings is to describe how the chip is wired up, and that includes any way it could possibly be wired up, not just what is implemented in the driver.

Quoted 29 lines
Documentation/devicetree/bindings/iio/dac/adi,ad5706r.yaml new file +28 -0
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/gpio/gpio.h>
+    #include <dt-bindings/interrupt-controller/irq.h>
+
+    spi {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        dac@0 {
+            compatible = "adi,ad5706r";
+            reg = <0>;
+            spi-max-frequency = <100000000>;
+
+            clocks = <&spi_clk>;
+            clock-names = "spi_clk";
+
+            dac-resetb-gpios = <&gpio0 86 GPIO_ACTIVE_LOW>;
+            dac-shdn-gpios = <&gpio0 87 GPIO_ACTIVE_HIGH>;
+
+            pwms = <&axi_pwm_gen 0 0>;
+            pwm-names = "ad5706r_ldacb";
+        };
+    };
+...
author maintainer
#4 lore raw

PATCH 2/3 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

---
 drivers/iio/dac/Kconfig   |   11 +
 drivers/iio/dac/Makefile  |    1 +
 drivers/iio/dac/ad5706r.c | 2290 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 2302 insertions(+)
drivers/iio/dac/Kconfig +11 -0
index db9f5c711b3df90641f017652fbbef594cc1627d..20be74a2933049250bab779d12ecd2b9b1f5a2a7 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 IIO_BUFFER
+	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 +2290 -0

2,290 lines not on this page · raw message

Signature · 1 line
2.34.1
#5 lore raw

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

Hi Alexis,

Some initial feedback. I guess some discussion will be needed on the ABI you have. From a quick look it seems that at least some of it can fit in standard one and some might be DT.

Quoted 18 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.

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

---
 drivers/iio/dac/Kconfig   |   11 +
 drivers/iio/dac/Makefile  |    1 +
 drivers/iio/dac/ad5706r.c | 2290 +++++++++++++++++++++++++++++++++++++++++++++

 3 files changed, 2302 insertions(+)

...

drivers/iio/dac/ad5706r.c line 894

Quoted 6 lines
drivers/iio/dac/ad5706r.c new file +5 -0
+
+static void ad5706r_debugs_init(struct iio_dev *indio_dev)
+{
+	struct dentry *d = iio_get_debugfs_dentry(indio_dev);
+

It should have:

if (!IS_ENABLED(CONFIG_DEBUGFS))
return

Quoted 13 lines
drivers/iio/dac/ad5706r.c new file +13 -0
+	debugfs_create_file_unsafe("streaming_addr", 0600, d,
+				   indio_dev, &ad5706r_streaming_addr_fops);
+	debugfs_create_file_unsafe("streaming_len", 0600, d,
+				   indio_dev, &ad5706r_streaming_len_fops);
+	debugfs_create_file_unsafe("streaming_data", 0600, d,
+				   indio_dev, &ad5706r_streaming_data_fops);
+	debugfs_create_file_unsafe("streaming_reg_access", 0600, d,
+				   indio_dev, &ad5706r_streaming_reg_access_fops);
+	debugfs_create_file_unsafe("spi_speed_hz_write", 0600, d,
+				   indio_dev, &ad5706r_spi_speed_write_fops);
+	debugfs_create_file_unsafe("spi_speed_hz_read", 0600, d,
+				   indio_dev, &ad5706r_spi_speed_read_fops);
+}

...

Quoted 45 lines
drivers/iio/dac/ad5706r.c new file +44 -0
+
+static int ad5706r_mux_out_sel_write(struct iio_dev *indio_dev,
+				     const struct iio_chan_spec *chan,
+				     unsigned int item)
+{
+	struct ad5706r_state *st = iio_priv(indio_dev);
+	unsigned int reg_value;
+	int ret;
+
+	/* Validate index */
+	if (item >= ARRAY_SIZE(mux_out_sel_reg_values))
+		return -EINVAL;
+
+	/* Convert index to register value */
+	reg_value = mux_out_sel_reg_values[item];
+
+	ret = ad5706r_spi_write(st, AD5706R_REG_MUX_OUT_SEL,
+				reg_value << st->shift_val);
+	if (ret)
+		return ret;
+
+	st->mux_out_sel = item;
+
+	return 0;
+}
+
+static int ad5706r_mux_out_sel_read(struct iio_dev *indio_dev,
+				    const struct iio_chan_spec *chan)
+{
+	struct ad5706r_state *st = iio_priv(indio_dev);
+	u16 reg_val;
+	u8 reg_byte;
+	int ret;
+	int i;
+
+	ret = ad5706r_spi_read(st, AD5706R_REG_MUX_OUT_SEL, &reg_val);
+	if (ret)
+		return ret;
+
+	/* Extract the 8-bit value */
+	reg_byte = (reg_val >> st->shift_val) & 0xFF;
+
+	/* Find which index has this register value */
+	for (i = 0; i < ARRAY_SIZE(mux_out_sel_reg_values); i++) {

drivers/iio/dac/ad5706r.c line 1268

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+		if (mux_out_sel_reg_values[i] == reg_byte) {
+			st->mux_out_sel = i;
+			return i;  /* Return index, not register value */
+		}
+	}
+
+	/* Unknown value - default to disabled */
+	st->mux_out_sel = MUX_OUT_SEL_DISABLED;
+	return MUX_OUT_SEL_DISABLED;
+}

We already have other parts with a similar setting (in frequency with this). There it supported in DT:

https://elixir.bootlin.com/linux/v6.19-rc5/source/Documentation/devicetree/bindings/iio/frequency/adi,adf4350.yaml#L82

Any benefit for this to be a runtime toggle?

Quoted 83 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+
+static const struct iio_enum ad5706r_mux_out_sel_enum = {
+	.items = mux_out_sel_iio_dev_attr_vals,
+	.num_items = ARRAY_SIZE(mux_out_sel_iio_dev_attr_vals),
+	.set = ad5706r_mux_out_sel_write,
+	.get = ad5706r_mux_out_sel_read,
+};
+
+static ssize_t ad5706r_multi_dac_input_a_write(struct iio_dev *indio_dev,
+					       uintptr_t private, const struct iio_chan_spec

*chan,

drivers/iio/dac/ad5706r.c new file +20 -0
+					       const char *buf, size_t len)
+{
+	struct ad5706r_state *st = iio_priv(indio_dev);
+	unsigned int reg_val;
+	int ret;
+
+	ret = kstrtou32(buf, 16, &reg_val);
+	if (ret)
+		return ret;
+
+	ret = ad5706r_spi_write(st, AD5706R_REG_MULTI_DAC_INPUT_A,
+				AD5706R_MASK_MULTI_DAC_INPUT_A(reg_val));
+	if (ret)
+		return ret;
+
+	return ret ? ret : len;
+}
+
+static ssize_t ad5706r_multi_dac_input_a_read(struct iio_dev *indio_dev,
+					      uintptr_t private, const struct iio_chan_spec

*chan,

drivers/iio/dac/ad5706r.c new file +51 -0
+					      char *buf)
+{
+	struct ad5706r_state *st = iio_priv(indio_dev);
+	u16 reg_val;
+	int ret;
+
+	ret = ad5706r_spi_read(st, AD5706R_REG_MULTI_DAC_INPUT_A, &reg_val);
+	if (ret)
+		return ret;
+
+	return sysfs_emit(buf, "0x%lx\n", AD5706R_MASK_MULTI_DAC_INPUT_A(reg_val));
+}
+
+static int ad5706r_multi_dac_sw_ldac_trigger_write(struct iio_dev *indio_dev,
+						   const struct iio_chan_spec *chan,
+						   unsigned int item)
+{
+	struct ad5706r_state *st = iio_priv(indio_dev);
+	int ret;
+
+	ret = ad5706r_spi_write(st, AD5706R_REG_MULTI_DAC_SW_LDAC, item << st->shift_val);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+static int ad5706r_multi_dac_sw_ldac_trigger_read(struct iio_dev *indio_dev,
+						  const struct iio_chan_spec *chan)
+{
+	struct ad5706r_state *st = iio_priv(indio_dev);
+	u16 reg_val;
+	int ret;
+
+	ret = ad5706r_spi_read(st, AD5706R_REG_MULTI_DAC_SW_LDAC, &reg_val);
+	if (ret)
+		return ret;
+
+	return reg_val;
+}
+
+static const struct iio_enum ad5706r_multi_dac_sw_ldac_trigger_enum = {
+	.items = multi_dac_sw_ldac_trigger_iio_dev_attr_vals,
+	.num_items = ARRAY_SIZE(multi_dac_sw_ldac_trigger_iio_dev_attr_vals),
+	.set = ad5706r_multi_dac_sw_ldac_trigger_write,
+	.get = ad5706r_multi_dac_sw_ldac_trigger_read,
+};
+
+static ssize_t ad5706r_reference_volts_write(struct iio_dev *indio_dev,
+					     uintptr_t private, const struct iio_chan_spec *chan,
+					     const char *buf, size_t len)

drivers/iio/dac/ad5706r.c line 1359

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+{
+	struct ad5706r_state *st = iio_priv(indio_dev);
+	unsigned int reg_val;
+	int ret;
+
+	ret = kstrtou32(buf, 10, &reg_val);
+	if (ret)
+		return ret;
+
+	st->reference_volts = reg_val;

Really, can we get anything the user gives us :)?

drivers/iio/dac/ad5706r.c line 1371

Quoted 12 lines
drivers/iio/dac/ad5706r.c new file +12 -0
+
+	return ret ? ret : len;
+}
+
+static ssize_t ad5706r_reference_volts_read(struct iio_dev *indio_dev,
+					    uintptr_t private, const struct iio_chan_spec *chan,
+					    char *buf)
+{
+	struct ad5706r_state *st = iio_priv(indio_dev);
+
+	return sysfs_emit(buf, "%u\n", st->reference_volts);
+}

Ditto for the above. For DACs, we typically don't mess with the reference level at runtime at the device can be in control of other HW.

Quoted 32 lines
drivers/iio/dac/ad5706r.c new file +32 -0
+
+static int ad5706r_ref_select_write(struct iio_dev *indio_dev,
+				    const struct iio_chan_spec *chan,
+				    unsigned int item)
+{
+	struct ad5706r_state *st = iio_priv(indio_dev);
+	int ret;
+
+	ret = ad5706r_spi_write(st, AD5706R_REG_BANDGAP_CONTROL, item);
+	if (ret)
+		return ret;
+
+	st->ref_select = item;
+
+	return 0;
+}
+
+static int ad5706r_ref_select_read(struct iio_dev *indio_dev,
+				   const struct iio_chan_spec *chan)
+{
+	struct ad5706r_state *st = iio_priv(indio_dev);
+	u16 reg_val;
+	int ret;
+
+	ret = ad5706r_spi_read(st, AD5706R_REG_BANDGAP_CONTROL, &reg_val);
+	if (ret)
+		return ret;
+
+	if (reg_val)
+		st->ref_select = REF_SELECT_INTERNAL;
+	else
+		st->ref_select = REF_SELECT_EXTERNAL;

drivers/iio/dac/ad5706r.c line 1413

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+
+	return st->ref_select;
+}
+
+static const struct iio_enum ad5706r_ref_select_enum = {
+	.items = ref_select_iio_dev_attr_vals,
+	.num_items = ARRAY_SIZE(ref_select_iio_dev_attr_vals),
+	.set = ad5706r_ref_select_write,
+	.get = ad5706r_ref_select_read,
+};

The above does not look like a good idea IIUC. It seems this needs to be supported using regulators.

...

Quoted 36 lines
drivers/iio/dac/ad5706r.c new file +35 -0
+
+static int ad5706r_output_state_read(struct iio_dev *indio_dev,
+				     const struct iio_chan_spec *chan)
+{
+	struct ad5706r_state *st = iio_priv(indio_dev);
+	u16 reg_val;
+	u16 reg_val2;
+	u16 reg_val3;
+	u16 gpio_val;
+	u16 mask_val;
+	int ret;
+
+	ret = ad5706r_spi_read(st, AD5706R_REG_OUT_OPERATING_MODE, &reg_val);
+	if (ret)
+		return ret;
+
+	ret = ad5706r_spi_read(st, AD5706R_REG_OUT_SWITCH_EN, &reg_val2);
+	if (ret)
+		return ret;
+
+	ret = ad5706r_spi_read(st, AD5706R_REG_SHDN_EN, &reg_val3);
+	if (ret)
+		return ret;
+
+	mask_val = 1 << (chan->channel + st->shift_val);
+	reg_val = mask_val & reg_val;
+	reg_val2 = mask_val & reg_val2;
+	reg_val3 = mask_val & reg_val3;
+	gpio_val = gpiod_get_value_cansleep(st->shdn_gpio);
+
+	if (reg_val && !reg_val3)
+		st->output_state[chan->channel] = OUTPUT_STATE_NORMAL_SW;
+	else if (!reg_val && !reg_val2)
+		st->output_state[chan->channel] = OUTPUT_STATE_SHUTDOWN_TO_TRISTATE_SW;
+	else if (!reg_val && reg_val2)

drivers/iio/dac/ad5706r.c line 1753

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+		st->output_state[chan->channel] = OUTPUT_STATE_SHUTDOWN_TO_GND_SW;
+	else if (reg_val && reg_val3 && gpio_val)
+		st->output_state[chan->channel] = OUTPUT_STATE_NORMAL_HW;
+	else if (reg_val && !reg_val2 && reg_val3 && !gpio_val)
+		st->output_state[chan->channel] = OUTPUT_STATE_SHUTDOWN_TO_TRISTATE_HW;
+	else if (reg_val && reg_val2 && reg_val3 && !gpio_val)
+		st->output_state[chan->channel] = OUTPUT_STATE_SHUTDOWN_TO_GND_HW;
+
+	return st->output_state[chan->channel];
+}

Also think we already have some standard ABI that seems to fit the above:

https://elixir.bootlin.com/linux/v6.19-rc5/source/Documentation/ABI/testing/sysfs-bus-iio#L758

Quoted 139 lines
drivers/iio/dac/ad5706r.c new file +139 -0
+
+static const struct iio_enum ad5706r_output_state_enum = {
+	.items = output_state_iio_dev_attr_vals,
+	.num_items = ARRAY_SIZE(output_state_iio_dev_attr_vals),
+	.set = ad5706r_output_state_write,
+	.get = ad5706r_output_state_read,
+};
+
+static int ad5706r_ldac_trigger_chn_write(struct iio_dev *indio_dev,
+					  const struct iio_chan_spec *chan,
+					  unsigned int item)
+{
+	struct ad5706r_state *st = iio_priv(indio_dev);
+	bool val_func_en = 0;
+	bool val_func_mode = 0;
+	bool val_sync_async = 0;
+	bool val_hw_sw = 0;
+	int ret;
+
+	guard(mutex)(&st->lock);
+
+	st->ldac_trigger_chn[chan->channel] = LDAC_TRIGGER_CHN_NONE;
+	st->toggle_trigger_chn[chan->channel] = TOGGLE_TRIGGER_CHN_NONE;
+	st->dither_trigger_chn[chan->channel] = DITHER_TRIGGER_CHN_NONE;
+
+	if (item != LDAC_TRIGGER_CHN_NONE)
+		val_sync_async = 1;	/* Write 1 LDAC_SYNC_ASYNC */
+
+	if (item == LDAC_TRIGGER_CHN_SW_TRIGGER)
+		val_hw_sw = 1;		/* Write 1 LDAC_HW_SW for SW */
+
+	ret = _set_reg_channel_mode(st, chan->channel, val_func_en, val_func_mode,
+				    val_sync_async, val_hw_sw);
+	if (ret)
+		return ret;
+
+	st->ldac_trigger_chn[chan->channel] = item;
+
+	return 0;
+}
+
+static int ad5706r_ldac_trigger_chn_read(struct iio_dev *indio_dev,
+					 const struct iio_chan_spec *chan)
+{
+	struct ad5706r_state *st = iio_priv(indio_dev);
+
+	return st->ldac_trigger_chn[chan->channel];
+}
+
+static const struct iio_enum ad5706r_ldac_trigger_chn_enum = {
+	.items = ldac_trigger_chn_iio_dev_attr_vals,
+	.num_items = ARRAY_SIZE(ldac_trigger_chn_iio_dev_attr_vals),
+	.set = ad5706r_ldac_trigger_chn_write,
+	.get = ad5706r_ldac_trigger_chn_read,
+};
+
+static int ad5706r_toggle_trigger_chn_write(struct iio_dev *indio_dev,
+					    const struct iio_chan_spec *chan,
+					    unsigned int item)
+{
+	struct ad5706r_state *st = iio_priv(indio_dev);
+	bool val_func_en = 0;
+	bool val_func_mode = 0;
+	bool val_sync_async = 0;
+	bool val_hw_sw = 0;
+	int ret;
+
+	guard(mutex)(&st->lock);
+
+	st->ldac_trigger_chn[chan->channel] = LDAC_TRIGGER_CHN_NONE;
+	st->toggle_trigger_chn[chan->channel] = TOGGLE_TRIGGER_CHN_NONE;
+	st->dither_trigger_chn[chan->channel] = DITHER_TRIGGER_CHN_NONE;
+
+	if (item != TOGGLE_TRIGGER_CHN_NONE)
+		val_func_en = 1;	/* Write 1 FUNC_EN */
+	if (item == TOGGLE_TRIGGER_CHN_SW_TRIGGER)
+		val_hw_sw = 1;		/* Write 1 LDAC_HW_SW for SW */
+
+	ret = _set_reg_channel_mode(st, chan->channel, val_func_en, val_func_mode,
+				    val_sync_async, val_hw_sw);
+	if (ret)
+		return ret;
+
+	st->toggle_trigger_chn[chan->channel] = item;
+
+	return 0;
+}
+
+static int ad5706r_toggle_trigger_chn_read(struct iio_dev *indio_dev,
+					   const struct iio_chan_spec *chan)
+{
+	struct ad5706r_state *st = iio_priv(indio_dev);
+
+	return st->toggle_trigger_chn[chan->channel];
+}
+
+static const struct iio_enum ad5706r_toggle_trigger_chn_enum = {
+	.items = toggle_trigger_chn_iio_dev_attr_vals,
+	.num_items = ARRAY_SIZE(toggle_trigger_chn_iio_dev_attr_vals),
+	.set = ad5706r_toggle_trigger_chn_write,
+	.get = ad5706r_toggle_trigger_chn_read,
+};
+
+static int ad5706r_dither_trigger_chn_write(struct iio_dev *indio_dev,
+					    const struct iio_chan_spec *chan,
+					    unsigned int item)
+{
+	struct ad5706r_state *st = iio_priv(indio_dev);
+	bool val_func_en = 0;
+	bool val_func_mode = 1;
+	bool val_sync_async = 0;
+	bool val_hw_sw = 0;
+	int ret;
+
+	guard(mutex)(&st->lock);
+
+	st->ldac_trigger_chn[chan->channel] = LDAC_TRIGGER_CHN_NONE;
+	st->toggle_trigger_chn[chan->channel] = TOGGLE_TRIGGER_CHN_NONE;
+	st->dither_trigger_chn[chan->channel] = DITHER_TRIGGER_CHN_NONE;
+
+	if (item != DITHER_TRIGGER_CHN_NONE)
+		val_func_en = 1;	/* Write 1 FUNC_EN */
+	if (item == DITHER_TRIGGER_CHN_SW_TRIGGER)
+		val_hw_sw = 1;		/* Write 1 LDAC_HW_SW for SW */
+
+	ret = _set_reg_channel_mode(st, chan->channel, val_func_en, val_func_mode,
+				    val_sync_async, val_hw_sw);
+	if (ret)
+		return ret;
+
+	st->dither_trigger_chn[chan->channel] = item;
+
+	return 0;
+}
+
+static int ad5706r_dither_trigger_chn_read(struct iio_dev *indio_dev,
+					   const struct iio_chan_spec *chan)
+{
+	struct ad5706r_state *st = iio_priv(indio_dev);

drivers/iio/dac/ad5706r.c line 1902

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+
+	return st->dither_trigger_chn[chan->channel];
+}
+
+static const struct iio_enum ad5706r_dither_trigger_chn_enum = {
+	.items = dither_trigger_chn_iio_dev_attr_vals,
+	.num_items = ARRAY_SIZE(dither_trigger_chn_iio_dev_attr_vals),
+	.set = ad5706r_dither_trigger_chn_write,
+	.get = ad5706r_dither_trigger_chn_read,
+};

There are already some defined ABI for DACs which have toggle and dither modes:

https://elixir.bootlin.com/linux/v6.19-rc5/source/Documentation/ABI/testing/sysfs-bus-iio-dac

Make sure to see what can be reused and what needs to be added.

Quoted 126 lines
drivers/iio/dac/ad5706r.c new file +81 -0
+
+static int ad5706r_multi_dac_sel_ch_write(struct iio_dev *indio_dev,
+					  const struct iio_chan_spec *chan,
+					  unsigned int item)
+{
+	struct ad5706r_state *st = iio_priv(indio_dev);
+	u16 reg_val;
+	u16 mask_val;
+	int ret;
+
+	ret = ad5706r_spi_read(st, AD5706R_REG_MULTI_DAC_SEL_CH, &reg_val);
+	if (ret)
+		return ret;
+
+	mask_val = BIT(chan->channel + st->shift_val);
+	reg_val = ~mask_val & reg_val;
+
+	if (item == MULTI_DAC_SEL_CH_EXCLUDE)
+		ret = ad5706r_spi_write(st, AD5706R_REG_MULTI_DAC_SEL_CH,
+					reg_val);
+	else
+		ret = ad5706r_spi_write(st, AD5706R_REG_MULTI_DAC_SEL_CH,
+					reg_val | mask_val);
+
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+static int ad5706r_multi_dac_sel_ch_read(struct iio_dev *indio_dev,
+					 const struct iio_chan_spec *chan)
+{
+	struct ad5706r_state *st = iio_priv(indio_dev);
+	u16 reg_val;
+	u16 mask_val;
+	int ret;
+
+	ret = ad5706r_spi_read(st, AD5706R_REG_MULTI_DAC_SEL_CH, &reg_val);
+	if (ret)
+		return ret;
+
+	mask_val = BIT(chan->channel + st->shift_val);
+	reg_val = mask_val & reg_val;
+
+	if (reg_val)
+		st->multi_dac_sel_ch[chan->channel] = MULTI_DAC_SEL_CH_INCLUDE;
+	else
+		st->multi_dac_sel_ch[chan->channel] = MULTI_DAC_SEL_CH_EXCLUDE;
+
+	return st->multi_dac_sel_ch[chan->channel];
+}
+
+static const struct iio_enum ad5706r_multi_dac_sel_ch_enum = {
+	.items = multi_dac_sel_ch_iio_chan_attr_vals,
+	.num_items = ARRAY_SIZE(multi_dac_sel_ch_iio_chan_attr_vals),
+	.set = ad5706r_multi_dac_sel_ch_write,
+	.get = ad5706r_multi_dac_sel_ch_read,
+};
+
+#define AD5706R_CHAN_EXT_INFO(_name, _what, _shared, _read, _write) {	\
+	.name = _name,							\
+	.read = (_read),						\
+	.write = (_write),						\
+	.private = (_what),						\
+	.shared = (_shared),						\
+}
+
+static struct iio_chan_spec_ext_info ad5706r_ext_info[] = {
+	/* device_attribute */
+	AD5706R_CHAN_EXT_INFO("dev_addr", 0, IIO_SHARED_BY_ALL,
+			      ad5706r_dev_addr_read, ad5706r_dev_addr_write),
+
+	IIO_ENUM("addr_ascension", IIO_SHARED_BY_ALL, &ad5706r_addr_ascension_enum),
+	IIO_ENUM_AVAILABLE("addr_ascension", IIO_SHARED_BY_ALL, &ad5706r_addr_ascension_enum),
+
+	IIO_ENUM("single_instr", IIO_SHARED_BY_ALL, &ad5706r_single_instr_enum),
+	IIO_ENUM_AVAILABLE("single_instr", IIO_SHARED_BY_ALL, &ad5706r_single_instr_enum),
+
+	IIO_ENUM("hw_ldac_tg_state", IIO_SHARED_BY_ALL, &ad5706r_hw_ldac_tg_state_enum),
+	IIO_ENUM_AVAILABLE("hw_ldac_tg_state", IIO_SHARED_BY_ALL,

&ad5706r_hw_ldac_tg_state_enum),

drivers/iio/dac/ad5706r.c new file +25 -0
+
+	/* Sampling Frequency part of read/write RAW */
+
+	IIO_ENUM("hw_ldac_tg_pwm", IIO_SHARED_BY_ALL, &ad5706r_hw_ldac_tg_pwm_enum),
+	IIO_ENUM_AVAILABLE("hw_ldac_tg_pwm", IIO_SHARED_BY_ALL, &ad5706r_hw_ldac_tg_pwm_enum),
+
+	IIO_ENUM("mux_out_sel", IIO_SHARED_BY_ALL, &ad5706r_mux_out_sel_enum),
+	IIO_ENUM_AVAILABLE("mux_out_sel", IIO_SHARED_BY_ALL, &ad5706r_mux_out_sel_enum),
+
+	AD5706R_CHAN_EXT_INFO("multi_dac_input_a", 0, IIO_SHARED_BY_ALL,
+			      ad5706r_multi_dac_input_a_read, ad5706r_multi_dac_input_a_write),
+
+	IIO_ENUM("multi_dac_sw_ldac_trigger", IIO_SHARED_BY_ALL,
+		 &ad5706r_multi_dac_sw_ldac_trigger_enum),
+	IIO_ENUM_AVAILABLE("multi_dac_sw_ldac_trigger", IIO_SHARED_BY_ALL,
+			   &ad5706r_multi_dac_sw_ldac_trigger_enum),
+
+	AD5706R_CHAN_EXT_INFO("reference_volts", 0, IIO_SHARED_BY_ALL,
+			      ad5706r_reference_volts_read, ad5706r_reference_volts_write),
+
+	IIO_ENUM("ref_select", IIO_SHARED_BY_ALL, &ad5706r_ref_select_enum),
+	IIO_ENUM_AVAILABLE("ref_select", IIO_SHARED_BY_ALL, &ad5706r_ref_select_enum),
+
+	IIO_ENUM("hw_shutdown_state", IIO_SHARED_BY_ALL, &ad5706r_hw_shutdown_state_enum),
+	IIO_ENUM_AVAILABLE("hw_shutdown_state", IIO_SHARED_BY_ALL,

&ad5706r_hw_shutdown_state_enum),

drivers/iio/dac/ad5706r.c new file +18 -0
+
+	/* Channel Attributes */
+	AD5706R_CHAN_EXT_INFO("input_register_a", 0, IIO_SEPARATE,
+			      ad5706r_input_register_a_read, ad5706r_input_register_a_write),
+
+	AD5706R_CHAN_EXT_INFO("input_register_b", 0, IIO_SEPARATE,
+			      ad5706r_input_register_b_read, ad5706r_input_register_b_write),
+
+	IIO_ENUM("hw_active_edge", IIO_SEPARATE, &ad5706r_hw_active_edge_enum),
+	IIO_ENUM_AVAILABLE("hw_active_edge", IIO_SEPARATE, &ad5706r_hw_active_edge_enum),
+
+	IIO_ENUM("range_sel", IIO_SEPARATE, &ad5706r_range_sel_enum),
+	IIO_ENUM_AVAILABLE("range_sel", IIO_SEPARATE, &ad5706r_range_sel_enum),
+
+	IIO_ENUM("output_state", IIO_SEPARATE, &ad5706r_output_state_enum),
+	IIO_ENUM_AVAILABLE("output_state", IIO_SEPARATE, &ad5706r_output_state_enum),
+
+	IIO_ENUM("ldac_trigger_chn", IIO_SEPARATE, &ad5706r_ldac_trigger_chn_enum),

drivers/iio/dac/ad5706r.c line 2036

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+	IIO_ENUM_AVAILABLE("ldac_trigger_chn", IIO_SEPARATE, &ad5706r_ldac_trigger_chn_enum),
+
+	IIO_ENUM("toggle_trigger_chn", IIO_SEPARATE, &ad5706r_toggle_trigger_chn_enum),
+	IIO_ENUM_AVAILABLE("toggle_trigger_chn", IIO_SEPARATE, &ad5706r_toggle_trigger_chn_enum),
+
+	IIO_ENUM("dither_trigger_chn", IIO_SEPARATE, &ad5706r_dither_trigger_chn_enum),
+	IIO_ENUM_AVAILABLE("dither_trigger_chn", IIO_SEPARATE, &ad5706r_dither_trigger_chn_enum),
+
+	IIO_ENUM("multi_dac_sel_ch", IIO_SEPARATE, &ad5706r_multi_dac_sel_ch_enum),
+	IIO_ENUM_AVAILABLE("multi_dac_sel_ch", IIO_SEPARATE, &ad5706r_multi_dac_sel_ch_enum),

Oh boy, not going through all of the above now but that's a lot of custom ABI. It definitely needs and ABI doc explaining what's being done.

Quoted 30 lines
drivers/iio/dac/ad5706r.c new file +19 -0
+
+	{},
+};
+
+/* Channel */
+static int ad5706r_read_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);
+	u16 reg_val;
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		scoped_guard(mutex, &st->lock) {
+			ret = ad5706r_spi_read(st, AD5706R_REG_DAC_DATA_READBACK_CH(chan-
Quoted 1 line

channel),

drivers/iio/dac/ad5706r.c new file +10 -0
+					       &reg_val);
+
+			if (ret)
+				return ret;
+
+			*val = reg_val;
+		}
+		return IIO_VAL_INT;
+	case IIO_CHAN_INFO_SCALE:
+		switch (st->range_sel[chan->channel]) {

drivers/iio/dac/ad5706r.c line 2075

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+		case RANGE_SEL_50:
+			*val = 50 * HZ_PER_MHZ / AD5706R_DAC_MAX_CODE;
+			break;
+		case RANGE_SEL_150:
+			*val = 150 * HZ_PER_MHZ / AD5706R_DAC_MAX_CODE;
+			break;
+		case RANGE_SEL_200:
+			*val = 200 * HZ_PER_MHZ / AD5706R_DAC_MAX_CODE;
+			break;
+		case RANGE_SEL_300:

Same comment about the prefix.

drivers/iio/dac/ad5706r.c line 2083

Quoted 8 lines
drivers/iio/dac/ad5706r.c new file +8 -0
+		default:
+			*val = 300 * HZ_PER_MHZ / AD5706R_DAC_MAX_CODE;
+			break;
+		}
+		return IIO_VAL_INT;
+	case IIO_CHAN_INFO_OFFSET:
+		*val = 0;
+		return IIO_VAL_INT;

If offset is 0 we should not need it.

Quoted 12 lines
drivers/iio/dac/ad5706r.c new file +12 -0
+	case IIO_CHAN_INFO_SAMP_FREQ:
+		*val = st->sampling_frequency;
+		return IIO_VAL_INT;
+	}
+
+	return -EINVAL;
+}
+
+static int ad5706r_write_raw(struct iio_dev *indio_dev,
+			     struct iio_chan_spec const *chan,
+			     int val,
+			     int val2,

drivers/iio/dac/ad5706r.c line 2105

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+			     long mask)
+{
+	struct ad5706r_state *st = iio_priv(indio_dev);
+	struct pwm_state ldacb_pwm_state;
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_SAMP_FREQ:
+		/* Sets minimum and maximum frequency */
+		val = clamp(val, SAMPLING_FREQUENCY_MIN_HZ, SAMPLING_FREQUENCY_MAX_HZ);

Typically the macros should have the device name as prefix.

Quoted 42 lines
drivers/iio/dac/ad5706r.c new file +42 -0
+
+		scoped_guard(mutex, &st->lock) {
+			pwm_get_state(st->ldacb_pwm, &ldacb_pwm_state);
+			ldacb_pwm_state.duty_cycle = DIV_ROUND_CLOSEST_ULL(NANO, 2 * val);
+			ldacb_pwm_state.period = DIV_ROUND_CLOSEST_ULL(NANO, val);
+			ldacb_pwm_state.enabled = true;
+
+			ret = pwm_apply_might_sleep(st->ldacb_pwm, &ldacb_pwm_state);
+			if (ret)
+				return ret;
+
+			st->sampling_frequency = val;
+		}
+		return 0;
+	}
+
+	return -EINVAL;
+}
+
+static const struct iio_info ad5706r_info = {
+	.read_raw = &ad5706r_read_raw,
+	.write_raw = &ad5706r_write_raw,
+	.debugfs_reg_access = &ad5706r_reg_access,
+};
+
+#define AD5706R_CHAN(_channel) {				\
+	.type = IIO_CURRENT,					\
+	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |		\
+			      BIT(IIO_CHAN_INFO_SCALE) |	\
+			      BIT(IIO_CHAN_INFO_OFFSET),	\
+	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),\
+	.output = 1,						\
+	.indexed = 1,						\
+	.channel = _channel,					\
+	.ext_info = ad5706r_ext_info,				\
+}
+
+static const struct iio_chan_spec ad5706r_channels[] = {
+	AD5706R_CHAN(0),
+	AD5706R_CHAN(1),
+	AD5706R_CHAN(2),
+	AD5706R_CHAN(3),

drivers/iio/dac/ad5706r.c line 2157

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+};
+
+static int _ad5706r_setup(struct ad5706r_state *st)
+{
+	struct pwm_state ldacb_pwm_state;
+	struct device *dev = &st->spi->dev;
+	int ret;
+	int i;
+
+	guard(mutex)(&st->lock);

Hmm, why the above?

drivers/iio/dac/ad5706r.c line 2161

Quoted 4 lines
drivers/iio/dac/ad5706r.c new file +4 -0
+
+	st->debug_streaming_len = 0;
+	st->debug_streaming_data = 0;
+	st->debug_streaming_addr = 0;

The above should not be needed.

Quoted 27 lines
drivers/iio/dac/ad5706r.c new file +27 -0
+	st->debug_spi_speed_hz_write = 10000000;
+	st->debug_spi_speed_hz_read = 10000000;
+
+	st->dev_addr = 0x00;
+	st->addr_ascension = ADDR_ASCENSION_DECREMENT;
+	st->single_instr = SINGLE_INSTR_STREAMING;
+	st->shift_val = 0;
+	st->addr_desc = 1;
+	st->hw_ldac_tg_state = HW_LDAC_TG_STATE_LOW;
+	st->sampling_frequency = 1000000;
+	st->hw_ldac_tg_pwm = HW_LDAC_TG_PWM_DISABLED;
+	st->mux_out_sel = MUX_OUT_SEL_DISABLED;
+	st->multi_dac_input_a = 0;
+	st->reference_volts = 2500;
+	st->ref_select = REF_SELECT_EXTERNAL;
+	st->hw_shutdown_state = HW_SHUTDOWN_STATE_LOW;
+
+	for (i = 0; i < 4; i++) {
+		st->hw_active_edge[i] = HW_ACTIVE_EDGE_RISING_EDGE;
+		st->range_sel[i] = RANGE_SEL_50;
+		st->output_state[i] = OUTPUT_STATE_NORMAL_SW;
+		st->ldac_trigger_chn[i] = LDAC_TRIGGER_CHN_HW_TRIGGER;
+		st->toggle_trigger_chn[i] = TOGGLE_TRIGGER_CHN_HW_TRIGGER;
+		st->dither_trigger_chn[i] = DITHER_TRIGGER_CHN_HW_TRIGGER;
+		st->multi_dac_sel_ch[i] = MULTI_DAC_SEL_CH_EXCLUDE;
+	}
+

drivers/iio/dac/ad5706r.c line 2198

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+	/* get spi_clk axi_clkgen, no enable as spi_engine driver enables it */
+	st->reference_clk = devm_clk_get(dev, "spi_clk");
+	if (IS_ERR(st->reference_clk))
+		return dev_err_probe(dev, PTR_ERR(st->reference_clk),
+				     "Failed to get AXI CLKGEN clock\n");
+
+	st->ldacb_pwm = devm_pwm_get(dev, "ad5706r_ldacb");
+	if (IS_ERR(st->ldacb_pwm))
+		return dev_err_probe(dev, PTR_ERR(st->ldacb_pwm),
+				     "Failed to get LDACB PWM\n");

nit: new line

drivers/iio/dac/ad5706r.c line 2211

Quoted 13 lines
drivers/iio/dac/ad5706r.c new file +13 -0
+	pwm_get_state(st->ldacb_pwm, &ldacb_pwm_state);
+	ldacb_pwm_state.duty_cycle = 0;
+	ldacb_pwm_state.period = DIV_ROUND_CLOSEST_ULL(NANO, st->sampling_frequency);
+	ldacb_pwm_state.enabled = true;
+	ret = pwm_apply_might_sleep(st->ldacb_pwm, &ldacb_pwm_state);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to apply PWM state\n");
+
+	st->resetb_gpio = devm_gpiod_get_optional(dev, "dac-resetb", GPIOD_OUT_LOW);
+	if (IS_ERR(st->resetb_gpio)) {
+		return dev_err_probe(dev, PTR_ERR(st->resetb_gpio),
+				     "Failed to get RESET_B GPIO\n");
+	}

reset gpios can now be handled using the reset subsystem.

Quoted 7 lines
drivers/iio/dac/ad5706r.c new file +7 -0
+
+	st->shdn_gpio = devm_gpiod_get_optional(dev, "dac-shdn", GPIOD_OUT_HIGH);
+	if (IS_ERR(st->shdn_gpio)) {
+		return dev_err_probe(dev, PTR_ERR(st->shdn_gpio),
+				     "Failed to get SHDN GPIO\n");
+	}
+

drivers/iio/dac/ad5706r.c line 2228

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+	/*
+	 * Get SPI max speed from device tree. Allows up to 100MHz.
+	 * If value is taken from spi->max_speed_hz, it is capped at 25MHz.
+	 */
+	ret = device_property_read_u32(dev, "spi-max-frequency", &st->spi_max_speed_hz);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to set SPI Max Speed\n");
+
+	st->spi_max_speed_hz = clamp(st->spi_max_speed_hz, SPI_MIN_SPEED_HZ, SPI_MAX_SPEED_HZ);
+

Hmm, why do we need the above? The spi core should handle it. Why is it capped? Maybe because the controller you tested this on can't handle it?

Quoted 5 lines
drivers/iio/dac/ad5706r.c new file +5 -0
+	return 0;
+}
+
+static int ad5706r_probe(struct spi_device *spi)
+{

drivers/iio/dac/ad5706r.c line 2243

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+	struct iio_dev *indio_dev;
+	struct ad5706r_state *st;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	st = iio_priv(indio_dev);
+	mutex_init(&st->lock);

These days, devm_mutex_init()

Quoted 15 lines
drivers/iio/dac/ad5706r.c new file +15 -0
+	st->spi = spi;
+
+	ret = _ad5706r_setup(st);
+	if (ret)
+		return ret;
+
+	indio_dev->name = "ad5706r";
+	indio_dev->info = &ad5706r_info;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->channels = ad5706r_channels;
+	indio_dev->num_channels = ARRAY_SIZE(ad5706r_channels);
+
+	ret = devm_iio_device_register(&spi->dev, indio_dev);
+	if (ret)
+		return ret;

drivers/iio/dac/ad5706r.c line 2268

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+
+	ad5706r_debugs_init(indio_dev);
+
+	return 0;
+}
+
+static const struct of_device_id ad5706r_of_match[] = {
+	{ .compatible = "adi,ad5706r" },
+	{ },
+};

No need for comma.

drivers/iio/dac/ad5706r.c line 2274

Quoted 6 lines
drivers/iio/dac/ad5706r.c new file +6 -0
+MODULE_DEVICE_TABLE(of, ad5706r_of_match);
+
+static const struct spi_device_id ad5706r_id[] = {
+	{ "ad5706r", 0 },
+	{ }
+};

Drop the 0.

- Nuno Sá

Quoted 1 line

;

#6 lore raw

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

Quoted 1 line · Nuno Sá

On Fri, 2026-02-20 at 16:02 +0800, Alexis Czezar Torreno wrote:

...

Quoted 8 lines

drivers/iio/dac/ad5706r.c line 893

Quoted 3 lines
drivers/iio/dac/ad5706r.c new file +3 -0
+static void ad5706r_debugs_init(struct iio_dev *indio_dev)
+{
+	struct dentry *d = iio_get_debugfs_dentry(indio_dev);

It should have:

if (!IS_ENABLED(CONFIG_DEBUGFS))
return

But why? The debugfs is a stub when disabled, nobody should do that in the cases when the main purpose is not the debugfs code.

Quoted 13 lines
Quoted 13 lines
drivers/iio/dac/ad5706r.c new file +13 -0
+	debugfs_create_file_unsafe("streaming_addr", 0600, d,
+				   indio_dev, &ad5706r_streaming_addr_fops);
+	debugfs_create_file_unsafe("streaming_len", 0600, d,
+				   indio_dev, &ad5706r_streaming_len_fops);
+	debugfs_create_file_unsafe("streaming_data", 0600, d,
+				   indio_dev, &ad5706r_streaming_data_fops);
+	debugfs_create_file_unsafe("streaming_reg_access", 0600, d,
+				   indio_dev, &ad5706r_streaming_reg_access_fops);
+	debugfs_create_file_unsafe("spi_speed_hz_write", 0600, d,
+				   indio_dev, &ad5706r_spi_speed_write_fops);
+	debugfs_create_file_unsafe("spi_speed_hz_read", 0600, d,
+				   indio_dev, &ad5706r_spi_speed_read_fops);
+}

...

Quoted 2 lines

drivers/iio/dac/ad5706r.c line 1258

Quoted 2 lines
drivers/iio/dac/ad5706r.c new file +2 -0
+	/* Find which index has this register value */
+	for (i = 0; i < ARRAY_SIZE(mux_out_sel_reg_values); i++) {
	for (size_t i...)
Quoted 5 lines
Quoted 5 lines
drivers/iio/dac/ad5706r.c new file +5 -0
+		if (mux_out_sel_reg_values[i] == reg_byte) {
+			st->mux_out_sel = i;
+			return i;  /* Return index, not register value */
+		}
+	}

...

Quoted 1 line

Review

Quoted 1 line
(quoted diff) +1 -0
+	return ret ? ret : len;

Use Elvis operator

	return ret ?: len;

...

Quoted 1 line

drivers/iio/dac/ad5706r.c line 2038

Quoted 1 line
drivers/iio/dac/ad5706r.c new file +1 -0
+	{},

IIO has a style for terminator entry, along with confusing trailing comma. If it's a sentinel, it must be one even at a compile time.

Quoted 1 line
Quoted 1 line

+};

...

Quoted 2 lines

drivers/iio/dac/ad5706r.c line 2163

Quoted 2 lines
drivers/iio/dac/ad5706r.c new file +2 -0
+	st->debug_spi_speed_hz_write = 10000000;
+	st->debug_spi_speed_hz_read = 10000000;

units.h and other headers for your help

10 * HZ_PER_MHZ

...

Quoted 1 line

drivers/iio/dac/ad5706r.c line 2171

Quoted 1 line
drivers/iio/dac/ad5706r.c new file +1 -0
+	st->sampling_frequency = 1000000;

In the similar way.

...

Quoted 1 line

drivers/iio/dac/ad5706r.c line 2175

Quoted 1 line
drivers/iio/dac/ad5706r.c new file +1 -0
+	st->reference_volts = 2500;

2.5kV?! I think you mistakenly put volts where should be _mV

...

Quoted 1 line

Review

Quoted 1 line
(quoted diff) +1 -0
+	for (i = 0; i < 4; i++) {

Magic 4.

Quoted 7 lines

drivers/iio/dac/ad5706r.c line 2186

Quoted 7 lines
drivers/iio/dac/ad5706r.c new file +7 -0
+		st->hw_active_edge[i] = HW_ACTIVE_EDGE_RISING_EDGE;
+		st->range_sel[i] = RANGE_SEL_50;
+		st->output_state[i] = OUTPUT_STATE_NORMAL_SW;
+		st->ldac_trigger_chn[i] = LDAC_TRIGGER_CHN_HW_TRIGGER;
+		st->toggle_trigger_chn[i] = TOGGLE_TRIGGER_CHN_HW_TRIGGER;
+		st->dither_trigger_chn[i] = DITHER_TRIGGER_CHN_HW_TRIGGER;
+		st->multi_dac_sel_ch[i] = MULTI_DAC_SEL_CH_EXCLUDE;

Hmm... Perhaps memsetXX()? But original loop with the defined iterator will be okay:

	for (unsigned int i = 0; i < $MAGIC_CONST; i++) {
Quoted 1 line
Quoted 1 line
(quoted diff) +1 -0
+	}

...

Quoted 11 lines

drivers/iio/dac/ad5706r.c line 2217

Quoted 11 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+	st->resetb_gpio = devm_gpiod_get_optional(dev, "dac-resetb", GPIOD_OUT_LOW);
+	if (IS_ERR(st->resetb_gpio)) {
+		return dev_err_probe(dev, PTR_ERR(st->resetb_gpio),
+				     "Failed to get RESET_B GPIO\n");
+	}
 
+	st->shdn_gpio = devm_gpiod_get_optional(dev, "dac-shdn", GPIOD_OUT_HIGH);
+	if (IS_ERR(st->shdn_gpio)) {
+		return dev_err_probe(dev, PTR_ERR(st->shdn_gpio),
+				     "Failed to get SHDN GPIO\n");
+	}

The {} are not needed when the body is a single call.

...

Quoted 3 lines

drivers/iio/dac/ad5706r.c line 2267

Quoted 3 lines
drivers/iio/dac/ad5706r.c new file +3 -0
+static const struct of_device_id ad5706r_of_match[] = {
+	{ .compatible = "adi,ad5706r" },
+	{ },

See above about terminator entry style.

Quoted 1 line
Quoted 1 line

+};

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

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

Quoted 11 lines · Andy Shevchenko
Quoted 1 line · Nuno Sá

On Fri, 2026-02-20 at 16:02 +0800, Alexis Czezar Torreno wrote:

...

Quoted 6 lines

drivers/iio/dac/ad5706r.c line 893

Quoted 3 lines
drivers/iio/dac/ad5706r.c new file +3 -0
+static void ad5706r_debugs_init(struct iio_dev *indio_dev)
+{
+	struct dentry *d = iio_get_debugfs_dentry(indio_dev);

It should have:

Quoted 5 lines
Quoted 2 lines

if (!IS_ENABLED(CONFIG_DEBUGFS))
return

But why? The debugfs is a stub when disabled, nobody should do that in the cases when the main purpose is not the debugfs code.

Because the compiler can then optimize away all of the above code...

- Nuno Sá

Quoted 113 lines
Quoted 14 lines
Quoted 14 lines
drivers/iio/dac/ad5706r.c new file +13 -0
+	debugfs_create_file_unsafe("streaming_addr", 0600, d,
+				   indio_dev, &ad5706r_streaming_addr_fops);
+	debugfs_create_file_unsafe("streaming_len", 0600, d,
+				   indio_dev, &ad5706r_streaming_len_fops);
+	debugfs_create_file_unsafe("streaming_data", 0600, d,
+				   indio_dev, &ad5706r_streaming_data_fops);
+	debugfs_create_file_unsafe("streaming_reg_access", 0600, d,
+				   indio_dev, &ad5706r_streaming_reg_access_fops);
+	debugfs_create_file_unsafe("spi_speed_hz_write", 0600, d,
+				   indio_dev, &ad5706r_spi_speed_write_fops);
+	debugfs_create_file_unsafe("spi_speed_hz_read", 0600, d,
+				   indio_dev, &ad5706r_spi_speed_read_fops);
+}

...

Quoted 2 lines

drivers/iio/dac/ad5706r.c line 1258

Quoted 2 lines
drivers/iio/dac/ad5706r.c new file +2 -0
+	/* Find which index has this register value */
+	for (i = 0; i < ARRAY_SIZE(mux_out_sel_reg_values); i++) {
	for (size_t i...)
Quoted 5 lines
Quoted 5 lines
drivers/iio/dac/ad5706r.c new file +5 -0
+		if (mux_out_sel_reg_values[i] == reg_byte) {
+			st->mux_out_sel = i;
+			return i;  /* Return index, not register value */
+		}
+	}

...

Quoted 1 line

Review

Quoted 1 line
(quoted diff) +1 -0
+	return ret ? ret : len;

Use Elvis operator

	return ret ?: len;

...

Quoted 1 line

drivers/iio/dac/ad5706r.c line 2038

Quoted 1 line
drivers/iio/dac/ad5706r.c new file +1 -0
+	{},

IIO has a style for terminator entry, along with confusing trailing comma. If it's a sentinel, it must be one even at a compile time.

Quoted 1 line
Quoted 1 line

+};

...

Quoted 2 lines

drivers/iio/dac/ad5706r.c line 2163

Quoted 2 lines
drivers/iio/dac/ad5706r.c new file +2 -0
+	st->debug_spi_speed_hz_write = 10000000;
+	st->debug_spi_speed_hz_read = 10000000;

units.h and other headers for your help

10 * HZ_PER_MHZ

...

Quoted 1 line

drivers/iio/dac/ad5706r.c line 2171

Quoted 1 line
drivers/iio/dac/ad5706r.c new file +1 -0
+	st->sampling_frequency = 1000000;

In the similar way.

...

Quoted 1 line

drivers/iio/dac/ad5706r.c line 2175

Quoted 1 line
drivers/iio/dac/ad5706r.c new file +1 -0
+	st->reference_volts = 2500;

2.5kV?! I think you mistakenly put volts where should be _mV

...

Quoted 1 line

Review

Quoted 1 line
(quoted diff) +1 -0
+	for (i = 0; i < 4; i++) {

Magic 4.

Quoted 7 lines

drivers/iio/dac/ad5706r.c line 2186

Quoted 7 lines
drivers/iio/dac/ad5706r.c new file +7 -0
+		st->hw_active_edge[i] = HW_ACTIVE_EDGE_RISING_EDGE;
+		st->range_sel[i] = RANGE_SEL_50;
+		st->output_state[i] = OUTPUT_STATE_NORMAL_SW;
+		st->ldac_trigger_chn[i] = LDAC_TRIGGER_CHN_HW_TRIGGER;
+		st->toggle_trigger_chn[i] = TOGGLE_TRIGGER_CHN_HW_TRIGGER;
+		st->dither_trigger_chn[i] = DITHER_TRIGGER_CHN_HW_TRIGGER;
+		st->multi_dac_sel_ch[i] = MULTI_DAC_SEL_CH_EXCLUDE;

Hmm... Perhaps memsetXX()? But original loop with the defined iterator will be okay:

	for (unsigned int i = 0; i < $MAGIC_CONST; i++) {
Quoted 1 line
Quoted 1 line
(quoted diff) +1 -0
+	}

...

Quoted 11 lines

drivers/iio/dac/ad5706r.c line 2217

Quoted 11 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+	st->resetb_gpio = devm_gpiod_get_optional(dev, "dac-resetb", GPIOD_OUT_LOW);
+	if (IS_ERR(st->resetb_gpio)) {
+		return dev_err_probe(dev, PTR_ERR(st->resetb_gpio),
+				     "Failed to get RESET_B GPIO\n");
+	}
 
+	st->shdn_gpio = devm_gpiod_get_optional(dev, "dac-shdn", GPIOD_OUT_HIGH);
+	if (IS_ERR(st->shdn_gpio)) {
+		return dev_err_probe(dev, PTR_ERR(st->shdn_gpio),
+				     "Failed to get SHDN GPIO\n");
+	}

The {} are not needed when the body is a single call.

...

Quoted 3 lines

drivers/iio/dac/ad5706r.c line 2267

Quoted 3 lines
drivers/iio/dac/ad5706r.c new file +3 -0
+static const struct of_device_id ad5706r_of_match[] = {
+	{ .compatible = "adi,ad5706r" },
+	{ },

See above about terminator entry style.

Quoted 1 line
Quoted 1 line

+};

#8 lore raw

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

Quoted 3 lines · Nuno Sá
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Nuno Sá

On Fri, 2026-02-20 at 16:02 +0800, Alexis Czezar Torreno wrote:

...

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/iio/dac/ad5706r.c line 893

Quoted 3 lines
drivers/iio/dac/ad5706r.c new file +3 -0
+static void ad5706r_debugs_init(struct iio_dev *indio_dev)
+{
+	struct dentry *d = iio_get_debugfs_dentry(indio_dev);

It should have:

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

if (!IS_ENABLED(CONFIG_DEBUGFS))
return

But why? The debugfs is a stub when disabled, nobody should do that in the cases when the main purpose is not the debugfs code.

Because the compiler can then optimize away all of the above code...

How is it different to the code elimination part that is inside in each of the below calls?

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
drivers/iio/dac/ad5706r.c new file +13 -0
+	debugfs_create_file_unsafe("streaming_addr", 0600, d,
+				   indio_dev, &ad5706r_streaming_addr_fops);
+	debugfs_create_file_unsafe("streaming_len", 0600, d,
+				   indio_dev, &ad5706r_streaming_len_fops);
+	debugfs_create_file_unsafe("streaming_data", 0600, d,
+				   indio_dev, &ad5706r_streaming_data_fops);
+	debugfs_create_file_unsafe("streaming_reg_access", 0600, d,
+				   indio_dev, &ad5706r_streaming_reg_access_fops);
+	debugfs_create_file_unsafe("spi_speed_hz_write", 0600, d,
+				   indio_dev, &ad5706r_spi_speed_write_fops);
+	debugfs_create_file_unsafe("spi_speed_hz_read", 0600, d,
+				   indio_dev, &ad5706r_spi_speed_read_fops);
+}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#9 lore raw

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

Quoted 16 lines · Andy Shevchenko
Quoted 3 lines · Nuno Sá
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Nuno Sá

On Fri, 2026-02-20 at 16:02 +0800, Alexis Czezar Torreno wrote:

...

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines

drivers/iio/dac/ad5706r.c line 893

Quoted 3 lines
drivers/iio/dac/ad5706r.c new file +3 -0
+static void ad5706r_debugs_init(struct iio_dev *indio_dev)
+{
+	struct dentry *d = iio_get_debugfs_dentry(indio_dev);

It should have:

if (!IS_ENABLED(CONFIG_DEBUGFS))
return

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

But why? The debugfs is a stub when disabled, nobody should do that in the cases when the main purpose is not the debugfs code.

Because the compiler can then optimize away all of the above code...

How is it different to the code elimination part that is inside in each of the below calls?

Clearly none :). For some reason I thought it would matter. Maybe I was mistaken by some old code that had #ifdef guards on the debug code.

Any ways, I might send some patches cleaning some places where I added the above so people do not copy it around.

- Nuno Sá

Quoted 1 line
#10 lore raw

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

In reply to Alexis Czezar Torreno

Hello,

drivers/iio/dac/ad5706r.c line 991

Quoted 9 lines · Alexis Czezar Torreno
drivers/iio/dac/ad5706r.c new file +9 -0
+static int _set_pwm_duty_cycle(struct ad5706r_state *st, int duty_cycle)
+{
+	struct pwm_state ldacb_pwm_state;
+	int ret;
+
+	pwm_get_state(st->ldacb_pwm, &ldacb_pwm_state);
+
+	ldacb_pwm_state.duty_cycle = duty_cycle == 0 ? 0 :
+		DIV_ROUND_CLOSEST_ULL(NANO, st->sampling_frequency * 100 / duty_cycle);

This being integer math it would benefit when simplifying that to

	10000000 * duty_cycle / st->sampling_frequency

. Consider st->sampling_frequency = 667 and duty_cycle = 99:

	NANO / (st->sampling_frequency * 100 / duty_cycle) = 1484257.8710644676
	DIV_ROUND_CLOSEST_ULL(NANO, st->sampling_frequency * 100 / duty_cycle) = 1485884
	DIV_ROUND_CLOSEST_ULL(10000000 * duty_cycle, st->sampling_frequency) = 1484258

With duty_cycle <= 100 this doesn't even overflow.

Quoted 13 lines
drivers/iio/dac/ad5706r.c new file +13 -0
+	ret = pwm_apply_might_sleep(st->ldacb_pwm, &ldacb_pwm_state);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+/* Device Attributes */
+static ssize_t ad5706r_dev_addr_write(struct iio_dev *indio_dev,
+				      uintptr_t private, const struct iio_chan_spec *chan,
+				      const char *buf, size_t len)
+{
+	struct ad5706r_state *st = iio_priv(indio_dev);

drivers/iio/dac/ad5706r.c line 1015

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+	unsigned int reg_val;
+	int ret;
+
+	ret = kstrtou32(buf, 10, &reg_val);
+	if (ret)
+		return ret;
+
+	st->dev_addr = AD5706R_MASK_DEV_ADDR(reg_val);
+
+	return ret ? ret : len;

This can be written as:

	return ret ?: len;

It's a matter of taste which one you like better.

Review

Quoted 2 lines
(quoted diff) +2 -0
+}
+

Best regards
Uwe

reviewer
#11 lore raw

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

In reply to Alexis Czezar Torreno
Quoted 11 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.

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 6 lines
---
 drivers/iio/dac/Kconfig   |   11 +
 drivers/iio/dac/Makefile  |    1 +
 drivers/iio/dac/ad5706r.c | 2290 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 2302 insertions(+)

This is way too much to try to review in one go. Aim for 500 lines per patch. If it gets close to 1000, it's time to start thinking about splitting it up.

And if you can split it up into sepaerate series, you will get even better reviews. We only have so much time, so the fewer lines sent at a time, the more time we can spend on each line.

It looks like this has a bunch of unusal features, so I would suggest to drop all of that for now and just start with a basic driver. Once that gets merged, we can start looking at the unusual stuff.

Quoted 41 lines
drivers/iio/dac/Kconfig +11 -0
index db9f5c711b3df90641f017652fbbef594cc1627d..20be74a2933049250bab779d12ecd2b9b1f5a2a7 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 IIO_BUFFER
+	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 +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..2d718cf7300bcd1f599fe715aacb3170f72541af

drivers/iio/dac/ad5706r.c line 10

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
@@ -0,0 +1,2290 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * AD5706R 16-bit Current Output Digital to Analog Converter
+ *
+ * Copyright 2026 Analog Devices Inc.
+ *
+ * This driver is designed for use with the AXI SPI Engine and AXI CLKGEN
+ * on Xilinx Zynq platforms. The 'clocks' device tree property references
+ * the AXI CLKGEN output clock, which is used to dynamically control the
+ * SPI clock rate for read and write operations independently.

Normally, we would just use .speed_hz in struct spi_transfer to specify the speed if we need different speeds for different operations. Any reason we can't do that here?

If there is a reason, it needs more explanation. In any case, I don't think that changing the clock rate of the SPI controller like this without going through the SPI subsystem is going to be acceptble.

maintainer
#12 lore raw

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

In reply to Alexis Czezar Torreno
Quoted 11 lines · On Fri, 20 Feb 2026 16:02:57 +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 1 line

Hi Alexis

Welcome to IIO. A few quick comments inline, but as others have observed the patch is too large and needs breaking up. Also there is a lot of custom ABI. Given we are reluctant to merge that at all, it definitely needs documentation and careful consideration of whether it can be done with existing ABI or is more appropriate in DT.

Thanks,

Jonathan

Quoted 17 lines
drivers/iio/dac/ad5706r.c new file +11 -0
new file mode 100644
index 0000000000000000000000000000000000000000..2d718cf7300bcd1f599fe715aacb3170f72541af
@@ -0,0 +1,2290 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * AD5706R 16-bit Current Output Digital to Analog Converter
+ *
+ * Copyright 2026 Analog Devices Inc.
+ *
+ * This driver is designed for use with the AXI SPI Engine and AXI CLKGEN
+ * on Xilinx Zynq platforms. The 'clocks' device tree property references
+ * the AXI CLKGEN output clock, which is used to dynamically control the
+ * SPI clock rate for read and write operations independently.
+ */

drivers/iio/dac/ad5706r.c line 21

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+
+#include <linux/bitfield.h>
+#include <linux/clk.h>
+#include <linux/debugfs.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/gpio/consumer.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+#include <linux/module.h>

Follow approximate include what you use principles for kernel drivers. That means mostly including everything that is used directly with exception of a few headers that we know will always be included by others (usually because they are documented as doing so).

e.g. mutex.h should always be in a driver that uses a mutex. If you are using guards, then cleanup.h needs to be there etc.

Quoted 4 lines
drivers/iio/dac/ad5706r.c new file +4 -0
+#include <linux/pwm.h>
+#include <linux/spi/spi.h>
+#include <linux/unaligned.h>
+#include <linux/units.h>

drivers/iio/dac/ad5706r.c line 35

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+
+/* SPI Defines */
+#define AD5706R_RD_MASK		BIT(15)
+#define AD5706R_ADDR_PIN_MASK	GENMASK(14, 12)
+#define AD5706R_ADDR_MASK	GENMASK(11, 0)
+#define AD5706R_VAL_MASK	GENMASK(7, 0)
+
+/* Registers and Masks */
+#define AD5706R_MASK_RESET			(BIT(7) | BIT(0))
+#define AD5706R_MASK_DEV_ADDR(x)		((x) & GENMASK(2, 0))

Define the mask only then use FIELD_GET() / FIELD_PREP() in the code. That tends to end up more readable than a macro doing the same thing without the compile time checks those bring on values fitting etc.

drivers/iio/dac/ad5706r.c line 83

Quoted 4 lines
drivers/iio/dac/ad5706r.c new file +4 -0
+
+#define NUM_CHANNELS			4
+#define SPI_MAX_SPEED_HZ		(100 * HZ_PER_MHZ)	/* 100 MHz */
+#define SPI_MIN_SPEED_HZ		(3 * HZ_PER_MHZ)	/* 3 MHz */

Very generic names. These are going to clash with something in another header at somepoint. Either push the values into a helper, inline or prefix the defines to avoid that naming clash.

drivers/iio/dac/ad5706r.c line 100

Quoted 2 lines
drivers/iio/dac/ad5706r.c new file +2 -0
+/*
+ * Order of attributes in code:

I'm not sure how this comment helps us.

Quoted 22 lines
drivers/iio/dac/ad5706r.c new file +22 -0
+ *
+ * Device Attributes:
+ *   - dev_addr
+ *   - addr_ascension
+ *   - single_instr
+ *   - hw_ldac_tg_state
+ *   - sampling_frequency
+ *   - hw_ldac_tg_pwm
+ *   - mux_out_sel
+ *   - multi_dac_input_a
+ *   - multi_dac_sw_ldac_trigger
+ *   - reference_volts
+ *   - ref_select
+ *   - hw_shutdown_state
+ *
+ * Channel Attributes:
+ *   - raw
+ *   - scale
+ *   - offset
+ *   - input_register_a
+ *   - input_register_b
+ *   - hw_active_edge

drivers/iio/dac/ad5706r.c line 186

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +9 -0
+ *   - range_sel
+ *   - output_state
+ *   - ldac_trigger_chn
+ *   - toggle_trigger_chn
+ *   - dither_trigger_chn
+ *   - multi_dac_sel_ch
+ */
 
+
+/* ENUM Lists */

Don't give generic comments like this on how you've laid the code out. They just tend to end up wrong after new features are added and don't bring much value in the first place.

Quoted 60 lines
drivers/iio/dac/ad5706r.c new file +58 -0
+enum addr_ascension_iio_dev_attr {
 
 
+static struct iio_chan_spec_ext_info ad5706r_ext_info[] = {
+	/* device_attribute */
+	AD5706R_CHAN_EXT_INFO("dev_addr", 0, IIO_SHARED_BY_ALL,
+			      ad5706r_dev_addr_read, ad5706r_dev_addr_write),
+
+	IIO_ENUM("addr_ascension", IIO_SHARED_BY_ALL, &ad5706r_addr_ascension_enum),
+	IIO_ENUM_AVAILABLE("addr_ascension", IIO_SHARED_BY_ALL, &ad5706r_addr_ascension_enum),
+
+	IIO_ENUM("single_instr", IIO_SHARED_BY_ALL, &ad5706r_single_instr_enum),
+	IIO_ENUM_AVAILABLE("single_instr", IIO_SHARED_BY_ALL, &ad5706r_single_instr_enum),
+
+	IIO_ENUM("hw_ldac_tg_state", IIO_SHARED_BY_ALL, &ad5706r_hw_ldac_tg_state_enum),
+	IIO_ENUM_AVAILABLE("hw_ldac_tg_state", IIO_SHARED_BY_ALL, &ad5706r_hw_ldac_tg_state_enum),
+
+	/* Sampling Frequency part of read/write RAW */
+
+	IIO_ENUM("hw_ldac_tg_pwm", IIO_SHARED_BY_ALL, &ad5706r_hw_ldac_tg_pwm_enum),
+	IIO_ENUM_AVAILABLE("hw_ldac_tg_pwm", IIO_SHARED_BY_ALL, &ad5706r_hw_ldac_tg_pwm_enum),
+
+	IIO_ENUM("mux_out_sel", IIO_SHARED_BY_ALL, &ad5706r_mux_out_sel_enum),
+	IIO_ENUM_AVAILABLE("mux_out_sel", IIO_SHARED_BY_ALL, &ad5706r_mux_out_sel_enum),
+
+	AD5706R_CHAN_EXT_INFO("multi_dac_input_a", 0, IIO_SHARED_BY_ALL,
+			      ad5706r_multi_dac_input_a_read, ad5706r_multi_dac_input_a_write),
+
+	IIO_ENUM("multi_dac_sw_ldac_trigger", IIO_SHARED_BY_ALL,
+		 &ad5706r_multi_dac_sw_ldac_trigger_enum),
+	IIO_ENUM_AVAILABLE("multi_dac_sw_ldac_trigger", IIO_SHARED_BY_ALL,
+			   &ad5706r_multi_dac_sw_ldac_trigger_enum),
+
+	AD5706R_CHAN_EXT_INFO("reference_volts", 0, IIO_SHARED_BY_ALL,
+			      ad5706r_reference_volts_read, ad5706r_reference_volts_write),
+
+	IIO_ENUM("ref_select", IIO_SHARED_BY_ALL, &ad5706r_ref_select_enum),
+	IIO_ENUM_AVAILABLE("ref_select", IIO_SHARED_BY_ALL, &ad5706r_ref_select_enum),
+
+	IIO_ENUM("hw_shutdown_state", IIO_SHARED_BY_ALL, &ad5706r_hw_shutdown_state_enum),
+	IIO_ENUM_AVAILABLE("hw_shutdown_state", IIO_SHARED_BY_ALL, &ad5706r_hw_shutdown_state_enum),
+
+	/* Channel Attributes */
+	AD5706R_CHAN_EXT_INFO("input_register_a", 0, IIO_SEPARATE,
+			      ad5706r_input_register_a_read, ad5706r_input_register_a_write),
+
+	AD5706R_CHAN_EXT_INFO("input_register_b", 0, IIO_SEPARATE,
+			      ad5706r_input_register_b_read, ad5706r_input_register_b_write),
+
+	IIO_ENUM("hw_active_edge", IIO_SEPARATE, &ad5706r_hw_active_edge_enum),
+	IIO_ENUM_AVAILABLE("hw_active_edge", IIO_SEPARATE, &ad5706r_hw_active_edge_enum),
+
+	IIO_ENUM("range_sel", IIO_SEPARATE, &ad5706r_range_sel_enum),
+	IIO_ENUM_AVAILABLE("range_sel", IIO_SEPARATE, &ad5706r_range_sel_enum),
+
+	IIO_ENUM("output_state", IIO_SEPARATE, &ad5706r_output_state_enum),
+	IIO_ENUM_AVAILABLE("output_state", IIO_SEPARATE, &ad5706r_output_state_enum),
+
+	IIO_ENUM("ldac_trigger_chn", IIO_SEPARATE, &ad5706r_ldac_trigger_chn_enum),
+	IIO_ENUM_AVAILABLE("ldac_trigger_chn", IIO_SEPARATE, &ad5706r_ldac_trigger_chn_enum),

drivers/iio/dac/ad5706r.c line 2037

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+
+	IIO_ENUM("toggle_trigger_chn", IIO_SEPARATE, &ad5706r_toggle_trigger_chn_enum),
+	IIO_ENUM_AVAILABLE("toggle_trigger_chn", IIO_SEPARATE, &ad5706r_toggle_trigger_chn_enum),
+
+	IIO_ENUM("dither_trigger_chn", IIO_SEPARATE, &ad5706r_dither_trigger_chn_enum),
+	IIO_ENUM_AVAILABLE("dither_trigger_chn", IIO_SEPARATE, &ad5706r_dither_trigger_chn_enum),
+
+	IIO_ENUM("multi_dac_sel_ch", IIO_SEPARATE, &ad5706r_multi_dac_sel_ch_enum),
+	IIO_ENUM_AVAILABLE("multi_dac_sel_ch", IIO_SEPARATE, &ad5706r_multi_dac_sel_ch_enum),
+

Others have pointed out that custom ABI has various basic problems.

1) Generic userspace has no way to know how to use it. As such, it gets little used and must not be required for basic functionality. So don't bring it in as part of the initial patch, but instead do it as a follow up patch (can be in same series if the series remains reasonably sized)
2) It needs full documentation for us to review it. In Documentation/ABI/testing/sysfs-bus-iio-driver-name

drivers/iio/dac/ad5706r.c line 2041

Quoted 4 lines
drivers/iio/dac/ad5706r.c new file +4 -0
+	{},
+};
+
+/* Channel */

I'm not sure what that comment means. I'd drop it.

drivers/iio/dac/ad5706r.c line 2045

Quoted 4 lines
drivers/iio/dac/ad5706r.c new file +4 -0
+static int ad5706r_read_raw(struct iio_dev *indio_dev,
+			    struct iio_chan_spec const *chan,
+			    int *val,
+			    int *val2,

Put multiple parameters on one line as long as we stay under 80 chars (or a bit over if it helps readability is fine as well)

drivers/iio/dac/ad5706r.c line 2054

Quoted 9 lines
drivers/iio/dac/ad5706r.c new file +9 -0
+			    long mask)
+{
+	struct ad5706r_state *st = iio_priv(indio_dev);
+	u16 reg_val;
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		scoped_guard(mutex, &st->lock) {

As below. Add scope with { } and use guard()
That will reduce the very long lines here.

Quoted 12 lines
drivers/iio/dac/ad5706r.c new file +12 -0
+			ret = ad5706r_spi_read(st, AD5706R_REG_DAC_DATA_READBACK_CH(chan->channel),
+					       &reg_val);
+
+			if (ret)
+				return ret;
+
+			*val = reg_val;
+		}
+		return IIO_VAL_INT;
+	case IIO_CHAN_INFO_SCALE:
+		switch (st->range_sel[chan->channel]) {
+		case RANGE_SEL_50:

drivers/iio/dac/ad5706r.c line 2076

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+			*val = 50 * HZ_PER_MHZ / AD5706R_DAC_MAX_CODE;
+			break;
+		case RANGE_SEL_150:
+			*val = 150 * HZ_PER_MHZ / AD5706R_DAC_MAX_CODE;
+			break;
+		case RANGE_SEL_200:
+			*val = 200 * HZ_PER_MHZ / AD5706R_DAC_MAX_CODE;
+			break;
+		case RANGE_SEL_300:
+		default:

A default often doesn't make much sense. How did you get a different value from the ones being explicitly set.

Quoted 21 lines
drivers/iio/dac/ad5706r.c new file +21 -0
+			*val = 300 * HZ_PER_MHZ / AD5706R_DAC_MAX_CODE;
+			break;
+		}
+		return IIO_VAL_INT;
+	case IIO_CHAN_INFO_OFFSET:
+		*val = 0;
+		return IIO_VAL_INT;
+	case IIO_CHAN_INFO_SAMP_FREQ:
+		*val = st->sampling_frequency;
+		return IIO_VAL_INT;
+	}
+
+	return -EINVAL;
+}
+
+static int ad5706r_write_raw(struct iio_dev *indio_dev,
+			     struct iio_chan_spec const *chan,
+			     int val,
+			     int val2,
+			     long mask)
+{

drivers/iio/dac/ad5706r.c line 2107

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+	struct ad5706r_state *st = iio_priv(indio_dev);
+	struct pwm_state ldacb_pwm_state;
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_SAMP_FREQ:
+		/* Sets minimum and maximum frequency */
+		val = clamp(val, SAMPLING_FREQUENCY_MIN_HZ, SAMPLING_FREQUENCY_MAX_HZ);
+
+		scoped_guard(mutex, &st->lock) {

I would add scope the whole case statement

	case IIO_CHAN_INFO_SAMP_FREQ: {

and then use guard(mutex)(&st->lock) here instead of scoped_guard()

That will reduce the indent of the rest and generally give slightly more readable code.

Quoted 15 lines
drivers/iio/dac/ad5706r.c new file +15 -0
+			pwm_get_state(st->ldacb_pwm, &ldacb_pwm_state);
+			ldacb_pwm_state.duty_cycle = DIV_ROUND_CLOSEST_ULL(NANO, 2 * val);
+			ldacb_pwm_state.period = DIV_ROUND_CLOSEST_ULL(NANO, val);
+			ldacb_pwm_state.enabled = true;
+
+			ret = pwm_apply_might_sleep(st->ldacb_pwm, &ldacb_pwm_state);
+			if (ret)
+				return ret;
+
+			st->sampling_frequency = val;
+		}
+		return 0;
+	}
+
+	return -EINVAL;

drivers/iio/dac/ad5706r.c line 2157

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +9 -0
+}
 
+static int _ad5706r_setup(struct ad5706r_state *st)
+{
+	struct pwm_state ldacb_pwm_state;
+	struct device *dev = &st->spi->dev;
+	int ret;
+	int i;
+
+	guard(mutex)(&st->lock);

Why is the lock needed? This is all (I think) early probe stuff. At that point we are serialized anyway.

drivers/iio/dac/ad5706r.c line 2161

Quoted 4 lines
drivers/iio/dac/ad5706r.c new file +4 -0
+
+	st->debug_streaming_len = 0;
+	st->debug_streaming_data = 0;
+	st->debug_streaming_addr = 0;

Where they are the 'natural' defaults (which it seems these probably are) we can just rely on the whole structure having been kzalloc'd and not set them to 0 one by one.

Quoted 8 lines
drivers/iio/dac/ad5706r.c new file +8 -0
+	st->debug_spi_speed_hz_write = 10000000;
+	st->debug_spi_speed_hz_read = 10000000;
+
+	st->dev_addr = 0x00;
+	st->addr_ascension = ADDR_ASCENSION_DECREMENT;
+	st->single_instr = SINGLE_INSTR_STREAMING;
+	st->shift_val = 0;
+	st->addr_desc = 1;

drivers/iio/dac/ad5706r.c line 2179

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+	st->hw_ldac_tg_state = HW_LDAC_TG_STATE_LOW;
+	st->sampling_frequency = 1000000;
+	st->hw_ldac_tg_pwm = HW_LDAC_TG_PWM_DISABLED;
+	st->mux_out_sel = MUX_OUT_SEL_DISABLED;
+	st->multi_dac_input_a = 0;
+	st->reference_volts = 2500;
+	st->ref_select = REF_SELECT_EXTERNAL;
+	st->hw_shutdown_state = HW_SHUTDOWN_STATE_LOW;
+
+	for (i = 0; i < 4; i++) {

4 is a magic number. Use one of the size macros or define one for the number of channels and use it consistently throughout the code.

drivers/iio/dac/ad5706r.c line 2189

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+		st->hw_active_edge[i] = HW_ACTIVE_EDGE_RISING_EDGE;
+		st->range_sel[i] = RANGE_SEL_50;
+		st->output_state[i] = OUTPUT_STATE_NORMAL_SW;
+		st->ldac_trigger_chn[i] = LDAC_TRIGGER_CHN_HW_TRIGGER;
+		st->toggle_trigger_chn[i] = TOGGLE_TRIGGER_CHN_HW_TRIGGER;
+		st->dither_trigger_chn[i] = DITHER_TRIGGER_CHN_HW_TRIGGER;
+		st->multi_dac_sel_ch[i] = MULTI_DAC_SEL_CH_EXCLUDE;
+	}
+
+	/* get spi_clk axi_clkgen, no enable as spi_engine driver enables it */

don't enable it as the spi_enging driver enables it

This seems odd division of labour though.

Quoted 9 lines
drivers/iio/dac/ad5706r.c new file +9 -0
+	st->reference_clk = devm_clk_get(dev, "spi_clk");
+	if (IS_ERR(st->reference_clk))
+		return dev_err_probe(dev, PTR_ERR(st->reference_clk),
+				     "Failed to get AXI CLKGEN clock\n");
+
+	st->ldacb_pwm = devm_pwm_get(dev, "ad5706r_ldacb");
+	if (IS_ERR(st->ldacb_pwm))
+		return dev_err_probe(dev, PTR_ERR(st->ldacb_pwm),
+				     "Failed to get LDACB PWM\n");

drivers/iio/dac/ad5706r.c line 2208

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+	pwm_get_state(st->ldacb_pwm, &ldacb_pwm_state);
+	ldacb_pwm_state.duty_cycle = 0;
+	ldacb_pwm_state.period = DIV_ROUND_CLOSEST_ULL(NANO, st->sampling_frequency);
+	ldacb_pwm_state.enabled = true;
+	ret = pwm_apply_might_sleep(st->ldacb_pwm, &ldacb_pwm_state);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to apply PWM state\n");
+
+	st->resetb_gpio = devm_gpiod_get_optional(dev, "dac-resetb", GPIOD_OUT_LOW);
+	if (IS_ERR(st->resetb_gpio)) {

It's a bit of a corner case on whether the following justifies the brackets or not. I'd not have them but others do prefer the other way around when there is a line break in the single call like this.

Quoted 20 lines
drivers/iio/dac/ad5706r.c new file +20 -0
+		return dev_err_probe(dev, PTR_ERR(st->resetb_gpio),
+				     "Failed to get RESET_B GPIO\n");
+	}
+
+	st->shdn_gpio = devm_gpiod_get_optional(dev, "dac-shdn", GPIOD_OUT_HIGH);
+	if (IS_ERR(st->shdn_gpio)) {
+		return dev_err_probe(dev, PTR_ERR(st->shdn_gpio),
+				     "Failed to get SHDN GPIO\n");
+	}
+
+	/*
+	 * Get SPI max speed from device tree. Allows up to 100MHz.
+	 * If value is taken from spi->max_speed_hz, it is capped at 25MHz.
+	 */
+	ret = device_property_read_u32(dev, "spi-max-frequency", &st->spi_max_speed_hz);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to set SPI Max Speed\n");
+
+	st->spi_max_speed_hz = clamp(st->spi_max_speed_hz, SPI_MIN_SPEED_HZ, SPI_MAX_SPEED_HZ);
+

drivers/iio/dac/ad5706r.c line 2238

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+	return 0;
+}
+
+static int ad5706r_probe(struct spi_device *spi)
+{
+	struct iio_dev *indio_dev;
+	struct ad5706r_state *st;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));

Probably worth a local

	struct device *dev = &spi->dev; just to shorten the few places

it is repeated in here.

drivers/iio/dac/ad5706r.c line 2243

Quoted 5 lines
drivers/iio/dac/ad5706r.c new file +5 -0
+	if (!indio_dev)
+		return -ENOMEM;
+
+	st = iio_priv(indio_dev);
+	mutex_init(&st->lock);

someone else pointed out
ret = devm_mutex_init() I think.

Quoted 7 lines
drivers/iio/dac/ad5706r.c new file +7 -0
+	st->spi = spi;
+
+	ret = _ad5706r_setup(st);
+	if (ret)
+		return ret;
+
+	indio_dev->name = "ad5706r";

drivers/iio/dac/ad5706r.c line 2260

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+	indio_dev->info = &ad5706r_info;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->channels = ad5706r_channels;
+	indio_dev->num_channels = ARRAY_SIZE(ad5706r_channels);
+
+	ret = devm_iio_device_register(&spi->dev, indio_dev);
+	if (ret)
+		return ret;
+
+	ad5706r_debugs_init(indio_dev);

I'd bring all the debugfs stuff in via a later patch. It probably adds complexity that will take more review time than people will expend all in one go.

Thanks,

Jonathan

Quoted 3 lines
drivers/iio/dac/ad5706r.c new file +3 -0
+
+	return 0;
+}
author maintainer
#13 lore raw

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

Quoted 21 lines
Quoted 11 lines

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 1 line

Hi Alexis

Welcome to IIO. A few quick comments inline, but as others have observed the patch is too large and needs breaking up. Also there is a lot of custom ABI. Given we are reluctant to merge that at all, it definitely needs documentation and careful consideration of whether it can be done with existing ABI or is more appropriate in DT.

Quoted 3 lines

Thanks,

Jonathan

Using this reply to acknowledge all the emails/feedback.

Hi All,

Thanks for the time you spent on this large patch.

It seems I will have to split this up first. I think it would be the basic driver, the ABI + docu, then the debugfs. Merge 1 before I submit the others. Will apply each feedback as I go.

Some naming schemes need to be updated, to properly reflect the datasheet terms rather than the one internally agreed upon.

The most unusual that a lot pointed out is related to the clock/.speed_hz/ spi_engine. I was asked to make it work a bit beyond 50MHz, using a cora fpga. The cora processor spi is limited to 50MHz due to HW, hence the need to use an spi-engine. However, this is limited to the resolution on how small the frequencies can be changed, the spi engine only does integer division, thus leading me to change the clock driver of the spi_engine itself. I'll try to see if the implementation can be changed to something simpler and will make it easier for us.

Thanks!
Alexis

Quoted 504 lines
Quoted 4 lines
drivers/iio/dac/ad5706r.c new renamed from drivers/iio/dac/ad5706r.c +0 -0

file mode 100644 index

0000000000000000000000000000000000000000..2d718cf7300bcd1f599 fe715aacb

Quoted 18 lines

3170f72541af

drivers/iio/dac/ad5706r.c new file +14 -0
@@ -0,0 +1,2290 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * AD5706R 16-bit Current Output Digital to Analog Converter
+ *
+ * Copyright 2026 Analog Devices Inc.
+ *
+ * This driver is designed for use with the AXI SPI Engine and AXI
+CLKGEN
+ * on Xilinx Zynq platforms. The 'clocks' device tree property
+references
+ * the AXI CLKGEN output clock, which is used to dynamically control
+the
+ * SPI clock rate for read and write operations independently.
+ */

drivers/iio/dac/ad5706r.c line 24

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+
+#include <linux/bitfield.h>
+#include <linux/clk.h>
+#include <linux/debugfs.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/gpio/consumer.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+#include <linux/module.h>

Follow approximate include what you use principles for kernel drivers. That means mostly including everything that is used directly with exception of a few headers that we know will always be included by others (usually because they are documented as doing so).

e.g. mutex.h should always be in a driver that uses a mutex. If you are using guards, then cleanup.h needs to be there etc.

Quoted 4 lines
drivers/iio/dac/ad5706r.c new file +4 -0
+#include <linux/pwm.h>
+#include <linux/spi/spi.h>
+#include <linux/unaligned.h>
+#include <linux/units.h>

drivers/iio/dac/ad5706r.c

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+
+/* SPI Defines */
+#define AD5706R_RD_MASK		BIT(15)
+#define AD5706R_ADDR_PIN_MASK	GENMASK(14, 12)
+#define AD5706R_ADDR_MASK	GENMASK(11, 0)
+#define AD5706R_VAL_MASK	GENMASK(7, 0)
+
+/* Registers and Masks */
+#define AD5706R_MASK_RESET			(BIT(7) | BIT(0))
+#define AD5706R_MASK_DEV_ADDR(x)		((x) & GENMASK(2,

0))

Define the mask only then use FIELD_GET() / FIELD_PREP() in the code. That tends to end up more readable than a macro doing the same thing without the compile time checks those bring on values fitting etc.

drivers/iio/dac/ad5706r.c

Quoted 3 lines
drivers/iio/dac/ad5706r.c new file +3 -0
+
+#define NUM_CHANNELS			4
+#define SPI_MAX_SPEED_HZ		(100 * HZ_PER_MHZ)	/* 100 MHz

*/

drivers/iio/dac/ad5706r.c line 83

Quoted 1 line
drivers/iio/dac/ad5706r.c new file +1 -0
+#define SPI_MIN_SPEED_HZ		(3 * HZ_PER_MHZ)	/* 3 MHz */

Very generic names. These are going to clash with something in another header at somepoint. Either push the values into a helper, inline or prefix the defines to avoid that naming clash.

drivers/iio/dac/ad5706r.c line 100

Quoted 2 lines
drivers/iio/dac/ad5706r.c new file +2 -0
+/*
+ * Order of attributes in code:

I'm not sure how this comment helps us.

Quoted 22 lines
drivers/iio/dac/ad5706r.c new file +22 -0
+ *
+ * Device Attributes:
+ *   - dev_addr
+ *   - addr_ascension
+ *   - single_instr
+ *   - hw_ldac_tg_state
+ *   - sampling_frequency
+ *   - hw_ldac_tg_pwm
+ *   - mux_out_sel
+ *   - multi_dac_input_a
+ *   - multi_dac_sw_ldac_trigger
+ *   - reference_volts
+ *   - ref_select
+ *   - hw_shutdown_state
+ *
+ * Channel Attributes:
+ *   - raw
+ *   - scale
+ *   - offset
+ *   - input_register_a
+ *   - input_register_b
+ *   - hw_active_edge

drivers/iio/dac/ad5706r.c line 186

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +9 -0
+ *   - range_sel
+ *   - output_state
+ *   - ldac_trigger_chn
+ *   - toggle_trigger_chn
+ *   - dither_trigger_chn
+ *   - multi_dac_sel_ch
+ */
 
+
+/* ENUM Lists */

Don't give generic comments like this on how you've laid the code out. They just tend to end up wrong after new features are added and don't bring much value in the first place.

drivers/iio/dac/ad5706r.c

Quoted 7 lines
drivers/iio/dac/ad5706r.c new file +5 -0
+enum addr_ascension_iio_dev_attr {
 
 
+static struct iio_chan_spec_ext_info ad5706r_ext_info[] = {
+	/* device_attribute */
+	AD5706R_CHAN_EXT_INFO("dev_addr", 0, IIO_SHARED_BY_ALL,
+			      ad5706r_dev_addr_read,

ad5706r_dev_addr_write),

drivers/iio/dac/ad5706r.c

Quoted 2 lines
drivers/iio/dac/ad5706r.c new file +2 -0
+
+	IIO_ENUM("addr_ascension", IIO_SHARED_BY_ALL,

&ad5706r_addr_ascension_enum),

drivers/iio/dac/ad5706r.c

Quoted 4 lines
drivers/iio/dac/ad5706r.c new file +4 -0
+	IIO_ENUM_AVAILABLE("addr_ascension", IIO_SHARED_BY_ALL,
+&ad5706r_addr_ascension_enum),
+
+	IIO_ENUM("single_instr", IIO_SHARED_BY_ALL,

&ad5706r_single_instr_enum),

drivers/iio/dac/ad5706r.c

Quoted 4 lines
drivers/iio/dac/ad5706r.c new file +4 -0
+	IIO_ENUM_AVAILABLE("single_instr", IIO_SHARED_BY_ALL,
+&ad5706r_single_instr_enum),
+
+	IIO_ENUM("hw_ldac_tg_state", IIO_SHARED_BY_ALL,

&ad5706r_hw_ldac_tg_state_enum),

drivers/iio/dac/ad5706r.c

Quoted 6 lines
drivers/iio/dac/ad5706r.c new file +6 -0
+	IIO_ENUM_AVAILABLE("hw_ldac_tg_state", IIO_SHARED_BY_ALL,
+&ad5706r_hw_ldac_tg_state_enum),
+
+	/* Sampling Frequency part of read/write RAW */
+
+	IIO_ENUM("hw_ldac_tg_pwm", IIO_SHARED_BY_ALL,

&ad5706r_hw_ldac_tg_pwm_enum),

drivers/iio/dac/ad5706r.c

Quoted 4 lines
drivers/iio/dac/ad5706r.c new file +4 -0
+	IIO_ENUM_AVAILABLE("hw_ldac_tg_pwm", IIO_SHARED_BY_ALL,
+&ad5706r_hw_ldac_tg_pwm_enum),
+
+	IIO_ENUM("mux_out_sel", IIO_SHARED_BY_ALL,

&ad5706r_mux_out_sel_enum),

drivers/iio/dac/ad5706r.c

Quoted 4 lines
drivers/iio/dac/ad5706r.c new file +4 -0
+	IIO_ENUM_AVAILABLE("mux_out_sel", IIO_SHARED_BY_ALL,
+&ad5706r_mux_out_sel_enum),
+
+	AD5706R_CHAN_EXT_INFO("multi_dac_input_a", 0,

IIO_SHARED_BY_ALL,

drivers/iio/dac/ad5706r.c

Quoted 6 lines
drivers/iio/dac/ad5706r.c new file +6 -0
+			      ad5706r_multi_dac_input_a_read,
+ad5706r_multi_dac_input_a_write),
+
+	IIO_ENUM("multi_dac_sw_ldac_trigger", IIO_SHARED_BY_ALL,
+		 &ad5706r_multi_dac_sw_ldac_trigger_enum),
+	IIO_ENUM_AVAILABLE("multi_dac_sw_ldac_trigger",

IIO_SHARED_BY_ALL,

drivers/iio/dac/ad5706r.c

Quoted 3 lines
drivers/iio/dac/ad5706r.c new file +3 -0
+			   &ad5706r_multi_dac_sw_ldac_trigger_enum),
+
+	AD5706R_CHAN_EXT_INFO("reference_volts", 0,

IIO_SHARED_BY_ALL,

drivers/iio/dac/ad5706r.c

Quoted 4 lines
drivers/iio/dac/ad5706r.c new file +4 -0
+			      ad5706r_reference_volts_read,
+ad5706r_reference_volts_write),
+
+	IIO_ENUM("ref_select", IIO_SHARED_BY_ALL,

&ad5706r_ref_select_enum),

drivers/iio/dac/ad5706r.c

Quoted 4 lines
drivers/iio/dac/ad5706r.c new file +4 -0
+	IIO_ENUM_AVAILABLE("ref_select", IIO_SHARED_BY_ALL,
+&ad5706r_ref_select_enum),
+
+	IIO_ENUM("hw_shutdown_state", IIO_SHARED_BY_ALL,

&ad5706r_hw_shutdown_state_enum),

drivers/iio/dac/ad5706r.c

Quoted 13 lines
drivers/iio/dac/ad5706r.c new file +13 -0
+	IIO_ENUM_AVAILABLE("hw_shutdown_state", IIO_SHARED_BY_ALL,
+&ad5706r_hw_shutdown_state_enum),
+
+	/* Channel Attributes */
+	AD5706R_CHAN_EXT_INFO("input_register_a", 0, IIO_SEPARATE,
+			      ad5706r_input_register_a_read,
+ad5706r_input_register_a_write),
+
+	AD5706R_CHAN_EXT_INFO("input_register_b", 0, IIO_SEPARATE,
+			      ad5706r_input_register_b_read,
+ad5706r_input_register_b_write),
+
+	IIO_ENUM("hw_active_edge", IIO_SEPARATE,

&ad5706r_hw_active_edge_enum),

drivers/iio/dac/ad5706r.c

Quoted 8 lines
drivers/iio/dac/ad5706r.c new file +8 -0
+	IIO_ENUM_AVAILABLE("hw_active_edge", IIO_SEPARATE,
+&ad5706r_hw_active_edge_enum),
+
+	IIO_ENUM("range_sel", IIO_SEPARATE, &ad5706r_range_sel_enum),
+	IIO_ENUM_AVAILABLE("range_sel", IIO_SEPARATE,
+&ad5706r_range_sel_enum),
+
+	IIO_ENUM("output_state", IIO_SEPARATE,

&ad5706r_output_state_enum),

drivers/iio/dac/ad5706r.c

Quoted 4 lines
drivers/iio/dac/ad5706r.c new file +4 -0
+	IIO_ENUM_AVAILABLE("output_state", IIO_SEPARATE,
+&ad5706r_output_state_enum),
+
+	IIO_ENUM("ldac_trigger_chn", IIO_SEPARATE,

&ad5706r_ldac_trigger_chn_enum),

drivers/iio/dac/ad5706r.c

Quoted 4 lines
drivers/iio/dac/ad5706r.c new file +4 -0
+	IIO_ENUM_AVAILABLE("ldac_trigger_chn", IIO_SEPARATE,
+&ad5706r_ldac_trigger_chn_enum),
+
+	IIO_ENUM("toggle_trigger_chn", IIO_SEPARATE,

&ad5706r_toggle_trigger_chn_enum),

drivers/iio/dac/ad5706r.c

Quoted 4 lines
drivers/iio/dac/ad5706r.c new file +4 -0
+	IIO_ENUM_AVAILABLE("toggle_trigger_chn", IIO_SEPARATE,
+&ad5706r_toggle_trigger_chn_enum),
+
+	IIO_ENUM("dither_trigger_chn", IIO_SEPARATE,

&ad5706r_dither_trigger_chn_enum),

drivers/iio/dac/ad5706r.c

Quoted 4 lines
drivers/iio/dac/ad5706r.c new file +4 -0
+	IIO_ENUM_AVAILABLE("dither_trigger_chn", IIO_SEPARATE,
+&ad5706r_dither_trigger_chn_enum),
+
+	IIO_ENUM("multi_dac_sel_ch", IIO_SEPARATE,

&ad5706r_multi_dac_sel_ch_enum),

drivers/iio/dac/ad5706r.c

Quoted 3 lines
drivers/iio/dac/ad5706r.c new file +3 -0
+	IIO_ENUM_AVAILABLE("multi_dac_sel_ch", IIO_SEPARATE,
+&ad5706r_multi_dac_sel_ch_enum),
+

Others have pointed out that custom ABI has various basic problems.

1) Generic userspace has no way to know how to use it. As such, it gets little used and must not be required for basic functionality. So don't bring it in as part of the initial patch, but instead do it as a follow up patch (can be in same series if the series remains reasonably sized)
2) It needs full documentation for us to review it. In Documentation/ABI/testing/sysfs-bus-iio-driver-name

drivers/iio/dac/ad5706r.c line 2041

Quoted 4 lines
drivers/iio/dac/ad5706r.c new file +4 -0
+	{},
+};
+
+/* Channel */

I'm not sure what that comment means. I'd drop it.

drivers/iio/dac/ad5706r.c line 2045

Quoted 4 lines
drivers/iio/dac/ad5706r.c new file +4 -0
+static int ad5706r_read_raw(struct iio_dev *indio_dev,
+			    struct iio_chan_spec const *chan,
+			    int *val,
+			    int *val2,

Put multiple parameters on one line as long as we stay under 80 chars (or a bit over if it helps readability is fine as well)

drivers/iio/dac/ad5706r.c line 2054

Quoted 9 lines
drivers/iio/dac/ad5706r.c new file +9 -0
+			    long mask)
+{
+	struct ad5706r_state *st = iio_priv(indio_dev);
+	u16 reg_val;
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		scoped_guard(mutex, &st->lock) {

As below. Add scope with { } and use guard() That will reduce the very long lines here.

drivers/iio/dac/ad5706r.c

Quoted 1 line
drivers/iio/dac/ad5706r.c new file +1 -0
+			ret = ad5706r_spi_read(st,

AD5706R_REG_DAC_DATA_READBACK_CH(chan->channel),

drivers/iio/dac/ad5706r.c

Quoted 12 lines
drivers/iio/dac/ad5706r.c new file +12 -0
+					       &reg_val);
+
+			if (ret)
+				return ret;
+
+			*val = reg_val;
+		}
+		return IIO_VAL_INT;
+	case IIO_CHAN_INFO_SCALE:
+		switch (st->range_sel[chan->channel]) {
+		case RANGE_SEL_50:
+			*val = 50 * HZ_PER_MHZ /

AD5706R_DAC_MAX_CODE;

drivers/iio/dac/ad5706r.c

Quoted 3 lines
drivers/iio/dac/ad5706r.c new file +3 -0
+			break;
+		case RANGE_SEL_150:
+			*val = 150 * HZ_PER_MHZ /

AD5706R_DAC_MAX_CODE;

drivers/iio/dac/ad5706r.c

Quoted 3 lines
drivers/iio/dac/ad5706r.c new file +3 -0
+			break;
+		case RANGE_SEL_200:
+			*val = 200 * HZ_PER_MHZ /

AD5706R_DAC_MAX_CODE;

drivers/iio/dac/ad5706r.c line 2076

Quoted 3 lines
drivers/iio/dac/ad5706r.c new file +3 -0
+			break;
+		case RANGE_SEL_300:
+		default:

A default often doesn't make much sense. How did you get a different value from the ones being explicitly set.

drivers/iio/dac/ad5706r.c

Quoted 1 line
drivers/iio/dac/ad5706r.c new file +1 -0
+			*val = 300 * HZ_PER_MHZ /

AD5706R_DAC_MAX_CODE;

Quoted 21 lines
drivers/iio/dac/ad5706r.c new file +21 -0
+			break;
+		}
+		return IIO_VAL_INT;
+	case IIO_CHAN_INFO_OFFSET:
+		*val = 0;
+		return IIO_VAL_INT;
+	case IIO_CHAN_INFO_SAMP_FREQ:
+		*val = st->sampling_frequency;
+		return IIO_VAL_INT;
+	}
+
+	return -EINVAL;
+}
+
+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);

drivers/iio/dac/ad5706r.c line 2107

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+	struct pwm_state ldacb_pwm_state;
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_SAMP_FREQ:
+		/* Sets minimum and maximum frequency */
+		val = clamp(val, SAMPLING_FREQUENCY_MIN_HZ,
+SAMPLING_FREQUENCY_MAX_HZ);
+
+		scoped_guard(mutex, &st->lock) {

I would add scope the whole case statement

	case IIO_CHAN_INFO_SAMP_FREQ: {

and then use guard(mutex)(&st->lock) here instead of scoped_guard()

That will reduce the indent of the rest and generally give slightly more readable code.

drivers/iio/dac/ad5706r.c

Quoted 2 lines
drivers/iio/dac/ad5706r.c new file +2 -0
+			pwm_get_state(st->ldacb_pwm, &ldacb_pwm_state);
+			ldacb_pwm_state.duty_cycle =

DIV_ROUND_CLOSEST_ULL(NANO, 2 * val);

drivers/iio/dac/ad5706r.c

Quoted 1 line
drivers/iio/dac/ad5706r.c new file +1 -0
+			ldacb_pwm_state.period =

DIV_ROUND_CLOSEST_ULL(NANO, val);

drivers/iio/dac/ad5706r.c

Quoted 3 lines
drivers/iio/dac/ad5706r.c new file +3 -0
+			ldacb_pwm_state.enabled = true;
+
+			ret = pwm_apply_might_sleep(st->ldacb_pwm,

&ldacb_pwm_state);

Quoted 8 lines
drivers/iio/dac/ad5706r.c new file +8 -0
+			if (ret)
+				return ret;
+
+			st->sampling_frequency = val;
+		}
+		return 0;
+	}
+

drivers/iio/dac/ad5706r.c line 2157

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +9 -0
+	return -EINVAL;
+}
 
+static int _ad5706r_setup(struct ad5706r_state *st) {
+	struct pwm_state ldacb_pwm_state;
+	struct device *dev = &st->spi->dev;
+	int ret;
+	int i;
+
+	guard(mutex)(&st->lock);

Why is the lock needed? This is all (I think) early probe stuff. At that point we are serialized anyway.

drivers/iio/dac/ad5706r.c line 2161

Quoted 4 lines
drivers/iio/dac/ad5706r.c new file +4 -0
+
+	st->debug_streaming_len = 0;
+	st->debug_streaming_data = 0;
+	st->debug_streaming_addr = 0;

Where they are the 'natural' defaults (which it seems these probably are) we can just rely on the whole structure having been kzalloc'd and not set them to 0 one by one.

Quoted 8 lines
drivers/iio/dac/ad5706r.c new file +8 -0
+	st->debug_spi_speed_hz_write = 10000000;
+	st->debug_spi_speed_hz_read = 10000000;
+
+	st->dev_addr = 0x00;
+	st->addr_ascension = ADDR_ASCENSION_DECREMENT;
+	st->single_instr = SINGLE_INSTR_STREAMING;
+	st->shift_val = 0;
+	st->addr_desc = 1;

drivers/iio/dac/ad5706r.c line 2179

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+	st->hw_ldac_tg_state = HW_LDAC_TG_STATE_LOW;
+	st->sampling_frequency = 1000000;
+	st->hw_ldac_tg_pwm = HW_LDAC_TG_PWM_DISABLED;
+	st->mux_out_sel = MUX_OUT_SEL_DISABLED;
+	st->multi_dac_input_a = 0;
+	st->reference_volts = 2500;
+	st->ref_select = REF_SELECT_EXTERNAL;
+	st->hw_shutdown_state = HW_SHUTDOWN_STATE_LOW;
+
+	for (i = 0; i < 4; i++) {

4 is a magic number. Use one of the size macros or define one for the number of channels and use it consistently throughout the code.

drivers/iio/dac/ad5706r.c

Quoted 5 lines
drivers/iio/dac/ad5706r.c new file +5 -0
+		st->hw_active_edge[i] = HW_ACTIVE_EDGE_RISING_EDGE;
+		st->range_sel[i] = RANGE_SEL_50;
+		st->output_state[i] = OUTPUT_STATE_NORMAL_SW;
+		st->ldac_trigger_chn[i] = LDAC_TRIGGER_CHN_HW_TRIGGER;
+		st->toggle_trigger_chn[i] =

TOGGLE_TRIGGER_CHN_HW_TRIGGER;

drivers/iio/dac/ad5706r.c

Quoted 1 line
drivers/iio/dac/ad5706r.c new file +1 -0
+		st->dither_trigger_chn[i] =

DITHER_TRIGGER_CHN_HW_TRIGGER;

drivers/iio/dac/ad5706r.c line 2222

Quoted 5 lines
drivers/iio/dac/ad5706r.c new file +5 -0
+		st->multi_dac_sel_ch[i] = MULTI_DAC_SEL_CH_EXCLUDE;
+	}
+
+	/* get spi_clk axi_clkgen, no enable as spi_engine driver enables it
+*/

don't enable it as the spi_enging driver enables it

This seems odd division of labour though.

Quoted 13 lines
drivers/iio/dac/ad5706r.c new file +12 -0
+	st->reference_clk = devm_clk_get(dev, "spi_clk");
+	if (IS_ERR(st->reference_clk))
+		return dev_err_probe(dev, PTR_ERR(st->reference_clk),
+				     "Failed to get AXI CLKGEN clock\n");
+
+	st->ldacb_pwm = devm_pwm_get(dev, "ad5706r_ldacb");
+	if (IS_ERR(st->ldacb_pwm))
+		return dev_err_probe(dev, PTR_ERR(st->ldacb_pwm),
+				     "Failed to get LDACB PWM\n");
+	pwm_get_state(st->ldacb_pwm, &ldacb_pwm_state);
+	ldacb_pwm_state.duty_cycle = 0;
+	ldacb_pwm_state.period = DIV_ROUND_CLOSEST_ULL(NANO, st-

sampling_frequency);

drivers/iio/dac/ad5706r.c

Quoted 4 lines
drivers/iio/dac/ad5706r.c new file +4 -0
+	ldacb_pwm_state.enabled = true;
+	ret = pwm_apply_might_sleep(st->ldacb_pwm, &ldacb_pwm_state);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to apply PWM

state\n");

drivers/iio/dac/ad5706r.c

Quoted 2 lines
drivers/iio/dac/ad5706r.c new file +2 -0
+
+	st->resetb_gpio = devm_gpiod_get_optional(dev, "dac-resetb",

GPIOD_OUT_LOW);

drivers/iio/dac/ad5706r.c line 2208

Quoted 1 line
drivers/iio/dac/ad5706r.c new file +1 -0
+	if (IS_ERR(st->resetb_gpio)) {

It's a bit of a corner case on whether the following justifies the brackets or not. I'd not have them but others do prefer the other way around when there is a line break in the single call like this.

drivers/iio/dac/ad5706r.c

Quoted 5 lines
drivers/iio/dac/ad5706r.c new file +5 -0
+		return dev_err_probe(dev, PTR_ERR(st->resetb_gpio),
+				     "Failed to get RESET_B GPIO\n");
+	}
+
+	st->shdn_gpio = devm_gpiod_get_optional(dev, "dac-shdn",

GPIOD_OUT_HIGH);

drivers/iio/dac/ad5706r.c

Quoted 13 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+	if (IS_ERR(st->shdn_gpio)) {
+		return dev_err_probe(dev, PTR_ERR(st->shdn_gpio),
+				     "Failed to get SHDN GPIO\n");
+	}
+
+	/*
+	 * Get SPI max speed from device tree. Allows up to 100MHz.
+	 * If value is taken from spi->max_speed_hz, it is capped at 25MHz.
+	 */
+	ret = device_property_read_u32(dev, "spi-max-frequency", &st-

spi_max_speed_hz);

drivers/iio/dac/ad5706r.c new file +2 -0
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to set SPI Max

Speed\n");

drivers/iio/dac/ad5706r.c

Quoted 2 lines
drivers/iio/dac/ad5706r.c new file +2 -0
+
+	st->spi_max_speed_hz = clamp(st->spi_max_speed_hz,

SPI_MIN_SPEED_HZ,

drivers/iio/dac/ad5706r.c line 2238

Quoted 11 lines
drivers/iio/dac/ad5706r.c new file +11 -0
+SPI_MAX_SPEED_HZ);
+
+	return 0;
+}
+
+static int ad5706r_probe(struct spi_device *spi) {
+	struct iio_dev *indio_dev;
+	struct ad5706r_state *st;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));

Probably worth a local

	struct device *dev = &spi->dev; just to shorten the few places it is

repeated in here.

drivers/iio/dac/ad5706r.c line 2243

Quoted 5 lines
drivers/iio/dac/ad5706r.c new file +5 -0
+	if (!indio_dev)
+		return -ENOMEM;
+
+	st = iio_priv(indio_dev);
+	mutex_init(&st->lock);

someone else pointed out
ret = devm_mutex_init() I think.

Quoted 7 lines
drivers/iio/dac/ad5706r.c new file +7 -0
+	st->spi = spi;
+
+	ret = _ad5706r_setup(st);
+	if (ret)
+		return ret;
+
+	indio_dev->name = "ad5706r";

drivers/iio/dac/ad5706r.c line 2260

Quoted 10 lines
drivers/iio/dac/ad5706r.c new file +10 -0
+	indio_dev->info = &ad5706r_info;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->channels = ad5706r_channels;
+	indio_dev->num_channels = ARRAY_SIZE(ad5706r_channels);
+
+	ret = devm_iio_device_register(&spi->dev, indio_dev);
+	if (ret)
+		return ret;
+
+	ad5706r_debugs_init(indio_dev);

I'd bring all the debugfs stuff in via a later patch. It probably adds complexity that will take more review time than people will expend all in one go.

Thanks,

Jonathan

Quoted 3 lines
drivers/iio/dac/ad5706r.c new file +3 -0
+
+	return 0;
+}
#14 lore raw

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

Quoted 36 lines · Alexis Czezar
Quoted 21 lines
Quoted 11 lines

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 1 line

Hi Alexis

Welcome to IIO. A few quick comments inline, but as others have observed the patch is too large and needs breaking up. Also there is a lot of custom ABI. Given we are reluctant to merge that at all, it definitely needs documentation and careful consideration of whether it can be done with existing ABI or is more appropriate in DT.

Quoted 3 lines

Thanks,

Jonathan

Using this reply to acknowledge all the emails/feedback.

Thanks for the time you spent on this large patch.

It seems I will have to split this up first. I think it would be the basic driver, the ABI + docu, then the debugfs. Merge 1 before I submit the others. Will apply each feedback as I go.

Some naming schemes need to be updated, to properly reflect the datasheet terms rather than the one internally agreed upon.

Quoted 8 lines

The most unusual that a lot pointed out is related to the clock/.speed_hz/ spi_engine. I was asked to make it work a bit beyond 50MHz, using a cora fpga. The cora processor spi is limited to 50MHz due to HW, hence the need to use an spi-engine. However, this is limited to the resolution on how small the frequencies can be changed, the spi engine only does integer division, thus leading me to change the clock driver of the spi_engine itself. I'll try to see if the implementation can be changed to something simpler and will make it easier for us.

Please, do remove the context you are not commenting on! I have listed ~500 lines to understand if there is anything below. Waste of time for each reviewer / follower...

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

PATCH 3/3 MAINTAINERS: Add entry for AD5706R DAC driver

In reply to Alexis Czezar Torreno

Add maintainer entry for the Analog Devices AD5706R DAC driver and device tree binding.

---
 MAINTAINERS | 8 ++++++++
 1 file changed, 8 insertions(+)
MAINTAINERS +8 -0
index 1251965d70bdfa990c66966cd77f7ab52ae3385f..3d7bd98b4d1b55836e40687a9a3ac9f4935a8acb 100644
@@ -1496,6 +1496,14 @@ 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
+F:	drivers/iio/dac/ad5706r.c
+
 ANALOG DEVICES INC AD7091R DRIVER
 M:	Marcelo Schmitt <[email protected]>
 L:	[email protected]
Signature · 1 line
2.34.1
#16 lore raw

Re: [PATCH 3/3] MAINTAINERS: Add entry for AD5706R DAC driver

Quoted 5 lines · Alexis Czezar Torreno

Add maintainer entry for the Analog Devices AD5706R DAC driver and device tree binding.

---

Hi Alexis,

Fairly sure checkpatch will complain in other patches as files are being added. Need to speak with Jorge as I just saw this was not triggered in internal CI.

- Nuno Sá

Quoted 22 lines

 MAINTAINERS | 8 ++++++++

 1 file changed, 8 insertions(+)
MAINTAINERS +0 -0
index 1251965d70bdfa990c66966cd77f7ab52ae3385f..3d7bd98b4d1b55836e40687a9a3ac9f4935a8acb 100644
@@ -1496,6 +1496,14 @@ W:	https://ez.analog.com/linux-software-drivers

 F: Documentation/devicetree/bindings/iio/adc/adi,ad4851.yaml
 F: drivers/iio/adc/ad4851.c

MAINTAINERS +8 -0
+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
+F:	drivers/iio/dac/ad5706r.c
+

 ANALOG DEVICES INC AD7091R DRIVER
 M: Marcelo Schmitt <[email protected]>
 L: [email protected]

#17 lore raw
In reply to Alexis Czezar Torreno
Quoted 11 lines · Alexis Czezar Torreno

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

It's ~2300 LoC file, please split by features, so the first patch brings basics and a few followups that add the rest.

Quoted 5 lines

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.

Quoted 7 lines

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

No blank lines in the tag block.
(Not sure if b4 can spread it over the series either way.)

Quoted 1 line
Signature · 2 lines
With Best Regards,
Andy Shevchenko