Skip to contentSkip to conversation

drivers:iio:dac: Add AD3552R driver support

PATCH Superseded Mihail Chindris 3 participants 7 messages Nov 8, 2021 – Nov 15, 2021

Subsystems (4)

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v5 cover drivers:iio:dac: Add AD3552R driver support

Changelog v4 -> v5:
- https://lore.kernel.org/all/[email protected]
- Add changelog for previous version
- Extend lock comment
- Remove unused variable is_custom

Changelog v3 -> v4:
- https://lore.kernel.org/all/[email protected]
- Remove trigger.h
- Use dev_err_probe only where needed
- Add comment about buff size
- Fix mutex order
- Return from all switch cases
- Return int in ad3552r_read_reg_wrapper
- Add goto put_child where missing
- Restructure ad3552r_write_codes
- Move ad3552r_write_codes nex to ad3552r_trigger_handler
- Use memcpy and memcmp in ad3552r_write_codes
- Remove endieness functions in ad3552r_write_codes

Changelog v2 -> v3:
- https://lore.kernel.org/all/[email protected]
- Order compatilbe in alphabetic order
- Fix comments in yaml
- Grup struct by types
- Drop usless "if (err)"
- Handle error in ad3552r_read_reg_wrapper
- ad3552r_find_range: u32 -> s32
- Add fwnode_handle_put(custom_gain_child); in good path too
- Vals[0] -> val
- Fix: fwnode_handle_put in ad3552r_configure_device
- Fix indio_dev->name
- Rename custom_gain_child -> gain_child
- Remove intermediary functions and write code inline where possible
- Add ad3552r_field_prep helper function
- Dev_err -> dev_warn for vref supply check
- Replace dev_err with dev_err_probe
- Remove channel for simultaneous update and do update mask register if both channels values are the same.

Changelog v0 -> v2:
- Split https://lore.kernel.org/all/[email protected]
and move ad3552r driver to this serie.
- Remove precision_mode abi
- Remove adi,synch_channels dt property
- Use vref-supply instead of adi,vref-select
- Remove unimplemented spi modes
- Change output-range format and use enums
- Update description for custom-output-range-config to be more clear
- Add datasheet tag
- Use GENMASK for defines
- Remove tomicro define
- Use get_unaligned_be16 and put_unaligned_be16
- Remove unnecessary checks
- Add comment for AD3552R_CH_DAC_PAGE channel
- Fix indent
- Remove irq trigger
- Remove irelevant checks
- Rename ad3552r_read_reg_pool to ad3552r_read_reg_wrapper.
- Add support for ad3542r

V0:
* Add ad3552r example to https://lore.kernel.org/linux-iio/[email protected]

Mihail Chindris (2):
dt-bindings: iio: dac: Add adi,ad3552r.yaml
drivers:iio:dac: Add AD3552R driver support

 .../bindings/iio/dac/adi,ad3552r.yaml         |  190 +++
 drivers/iio/dac/Kconfig                       |   10 +
 drivers/iio/dac/Makefile                      |    1 +
 drivers/iio/dac/ad3552r.c                     | 1199 +++++++++++++++++
 4 files changed, 1400 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/dac/adi,ad3552r.yaml
 create mode 100644 drivers/iio/dac/ad3552r.c

base-commit: ef226dcf3d88697a06335fbc55c4263ab164b135

Signature · 1 line
2.27.0
#1 lore raw

PATCH v5 1/2 dt-bindings: iio: dac: Add adi,ad3552r.yaml

Add documentation for ad3552r

---
 .../bindings/iio/dac/adi,ad3552r.yaml         | 190 ++++++++++++++++++
 1 file changed, 190 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/dac/adi,ad3552r.yaml
Documentation/devicetree/bindings/iio/dac/adi,ad3552r.yaml new file +190 -0
new file mode 100644
index 000000000000..c6999bb4c7a4
@@ -0,0 +1,190 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+# Copyright 2020 Analog Devices Inc.
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/dac/adi,ad3552r.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Analog Devices AD2552R DAC device driver
+
+maintainers:
+  - Mihail Chindris <[email protected]>
+
+description: |
+  Bindings for the Analog Devices AD3552R DAC device and similar.
+  Datasheet can be found here:
+    https://www.analog.com/media/en/technical-documentation/data-sheets/ad3542r.pdf
+    https://www.analog.com/media/en/technical-documentation/data-sheets/ad3552r.pdf
+properties:
+  compatible:
+    enum:
+      - adi,ad3542r
+      - adi,ad3552r
+
+  reg:
+    maxItems: 1
+
+  spi-max-frequency:
+    maximum: 30000000
+
+  reset-gpios:
+    maxItems: 1
+
+  ldac-gpios:
+    description: |
+      LDAC pin to be used as a hardware trigger to update the DAC channels.
+    maxItems: 1
+
+  vref-supply:
+    description:
+      The regulator to use as an external reference. If it does not exists the
+      internal reference will be used. External reference must be 2.5V
+
+  adi,vref-out-en:
+    description: Vref I/O driven by internal vref to 2.5V. If not set, Vref pin
+      will be floating.
+    type: boolean
+
+  adi,sdo-drive-strength:
+    description: |
+      Configure SDIO0 and SDIO1 strength levels:
+        - 0: low SDO drive strength.
+        - 1: medium low SDO drive strength.
+        - 2: medium high SDO drive strength.
+        - 3: high SDO drive strength
+    $ref: /schemas/types.yaml#/definitions/uint32
+    enum: [0, 1, 2, 3]
+
+patternProperties:
+  "^channel@([0-1])$":
+    type: object
+    description: Configurations of the DAC Channels
+    properties:
+      reg:
+          description: Channel number
+          enum: [0, 1]
+
+      custom-output-range-config:
+        type: object
+        description: Configuration of custom range when
+          adi,output-range-microvolt is not present.
+          The formulas for calculation the output voltages are
+            Vout_fs = 2.5 + [(GainN + Offset/1024) * 2.5 * Rfbx * 1.03]
+            Vout_zs = 2.5 - [(GainP + Offset/1024) * 2.5 * Rfbx * 1.03]
+        properties:
+          adi,gain-offset:
+            description: Gain offset used in the above formula
+            $ref: /schemas/types.yaml#/definitions/int32
+            maximum: 511
+            minimum: -511
+          adi,gain-scaling-p-inv-log2:
+            description: GainP = 1 / ( 2 ^ adi,gain-scaling-p-inv-log2)
+            $ref: /schemas/types.yaml#/definitions/uint32
+            enum: [0, 1, 2, 3]
+          adi,gain-scaling-n-inv-log2:
+            description: GainN = 1 / ( 2 ^ adi,gain-scaling-n-inv-log2)
+            $ref: /schemas/types.yaml#/definitions/uint32
+            enum: [0, 1, 2, 3]
+          adi,rfb-ohms:
+            description: Feedback Resistor
+        required:
+          - adi,gain-offset
+          - adi,gain-scaling-p-inv-log2
+          - adi,gain-scaling-n-inv-log2
+          - adi,rfb-ohms
+    required:
+      - reg
+
+    oneOf:
+      # If adi,output-range-microvolt is missing,
+      # custom-output-range-config must be used
+      - required:
+        - adi,output-range-microvolt
+      - required:
+        - custom-output-range-config
+
+allOf:
+  - if:
+      properties:
+        compatible:
+          contains:
+            const: adi,ad3542r
+    then:
+      patternProperties:
+        "^channel@([0-1])$":
+          type: object
+          properties:
+            adi,output-range-microvolt:
+              description: |
+                Voltage output range of the channel as <minimum, maximum>
+                Required connections:
+                  Rfb1x for: 0 to 2.5 V; 0 to 3V; 0 to 5 V;
+                  Rfb2x for: 0 to 10 V; 2.5 to 7.5V; -5 to 5 V;
+              oneOf:
+                - items:
+                    - const: 0
+                    - enum: [2500000, 3000000, 5000000, 10000000]
+                - items:
+                    - const: -2500000
+                    - const: 7500000
+                - items:
+                    - const: -5000000
+                    - const: 5000000
+          required:
+            - adi,output-range-microvolt
+  - if:
+      properties:
+        compatible:
+          contains:
+            const: adi,ad3552r
+    then:
+      patternProperties:
+        "^channel@([0-1])$":
+          type: object
+          properties:
+            adi,output-range-microvolt:
+              description: |
+                Voltage output range of the channel as <minimum, maximum>
+                Required connections:
+                  Rfb1x for: 0 to 2.5 V; 0 to 5 V;
+                  Rfb2x for: 0 to 10 V; -5 to 5 V;
+                  Rfb4x for: -10 to 10V
+              oneOf:
+                - items:
+                    - const: 0
+                    - enum: [2500000, 5000000, 10000000]
+                - items:
+                    - const: -5000000
+                    - const: 5000000
+                - items:
+                    - const: -10000000
+                    - const: 10000000
+
+required:
+  - compatible
+  - reg
+  - spi-max-frequency
+
+additionalProperties: false
+
+examples:
+  - |
+    ad3552r {
+            compatible = "adi,ad3552r";
+            reg = <0>;
+            spi-max-frequency = <20000000>;
+            channel@0 {
+                    reg = <0>;
+                    adi,output-range-microvolt = <0 10000000>;
+            };
+            channel@1 {
+                    reg = <1>;
+                    custom-output-range-config {
+                            adi,gain-offset = <5>;
+                            adi,gain-scaling-p-inv-log2 = <1>;
+                            adi,gain-scaling-n-inv-log2 = <2>;
+                            adi,rfb-ohms = <1>;
+                    };
+          };
+      };
+...
Signature · 1 line
2.27.0
#2 lore raw

RE: [PATCH v5 1/2] dt-bindings: iio: dac: Add adi,ad3552r.yaml

...

Documentation/devicetree/bindings/iio/dac/adi,ad3552r.yaml line 67

Quoted 10 lines
Documentation/devicetree/bindings/iio/dac/adi,ad3552r.yaml new file +10 -0
+patternProperties:
+  "^channel@([0-1])$":
+    type: object
+    description: Configurations of the DAC Channels
+    properties:
+      reg:
+          description: Channel number
+          enum: [0, 1]
+
+      custom-output-range-config:

Copy unanswered comment from V3

Quoted 9 lines
Quoted 9 lines
Quoted 3 lines

Not a generic property so I think this needs an adi prefix. Jonathan

I tried with adi prefix but I get weird errors while running dt_binding_check for properties with adi prefix and with type:object Do you have any suggestion for this issues?

Mihail

@Rob
Jonathan

Quoted 6 lines
Documentation/devicetree/bindings/iio/dac/adi,ad3552r.yaml new file +6 -0
+        type: object
+        description: Configuration of custom range when
+          adi,output-range-microvolt is not present.
+          The formulas for calculation the output voltages are
+            Vout_fs = 2.5 + [(GainN + Offset/1024) * 2.5 * Rfbx * 1.03]
+            Vout_zs = 2.5 - [(GainP + Offset/1024) * 2.5 * Rfbx * 1.03]

...

#3 lore raw

Re: [PATCH v5 1/2] dt-bindings: iio: dac: Add adi,ad3552r.yaml

Quoted 15 lines · Mihail

...

Documentation/devicetree/bindings/iio/dac/adi,ad3552r.yaml line 67

Quoted 10 lines
Documentation/devicetree/bindings/iio/dac/adi,ad3552r.yaml new file +10 -0
+patternProperties:
+  "^channel@([0-1])$":
+    type: object
+    description: Configurations of the DAC Channels
+    properties:
+      reg:
+          description: Channel number
+          enum: [0, 1]
+
+      custom-output-range-config:

Copy unanswered comment from V3

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Not a generic property so I think this needs an adi prefix. Jonathan

Quoted 7 lines
Quoted 5 lines
Quoted 5 lines

I tried with adi prefix but I get weird errors while running dt_binding_check for properties with adi prefix and with type:object Do you have any suggestion for this issues?

Mihail

@Rob
Jonathan

I had the same problem with the bindings for the xadc driver: https://www.spinics.net/lists/devicetree/msg382081.html

Quoted 7 lines
Quoted 6 lines
Documentation/devicetree/bindings/iio/dac/adi,ad3552r.yaml new file +6 -0
+        type: object
+        description: Configuration of custom range when
+          adi,output-range-microvolt is not present.
+          The formulas for calculation the output voltages are
+            Vout_fs = 2.5 + [(GainN + Offset/1024) * 2.5 * Rfbx * 1.03]
+            Vout_zs = 2.5 - [(GainP + Offset/1024) * 2.5 * Rfbx * 1.03]

...

#4 lore raw

PATCH v5 2/2 drivers:iio:dac: Add AD3552R driver support

In reply to Mihail Chindris

The AD3552R-16 is a low drift ultrafast, 16-bit accuracy, current output digital-to-analog converter (DAC) designed to generate multiple output voltage span ranges. The AD3552R-16 operates with a fixed 2.5V reference.

Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/ad3552r.pdf

---
 drivers/iio/dac/Kconfig   |   10 +
 drivers/iio/dac/Makefile  |    1 +
 drivers/iio/dac/ad3552r.c | 1199 +++++++++++++++++++++++++++++++++++++
 3 files changed, 1210 insertions(+)
 create mode 100644 drivers/iio/dac/ad3552r.c
drivers/iio/dac/Kconfig +10 -0
index 75e1f2b48638..ced6428f2c92 100644
@@ -6,6 +6,16 @@
 
 menu "Digital to analog converters"
 
+config AD3552R
+	tristate "Analog Devices AD3552R DAC driver"
+	depends on SPI_MASTER
+	help
+	  Say yes here to build support for Analog Devices AD3552R
+	  Digital to Analog Converter.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called ad3552r.
+
 config AD5064
 	tristate "Analog Devices AD5064 and similar multi-channel DAC driver"
 	depends on (SPI_MASTER && I2C!=m) || I2C
drivers/iio/dac/Makefile +1 -0
index 33e16f14902a..dffe36efd8ff 100644
@@ -4,6 +4,7 @@
 #
 
 # When adding new entries keep the list in alphabetical order
+obj-$(CONFIG_AD3552R) += ad3552r.o
 obj-$(CONFIG_AD5360) += ad5360.o
 obj-$(CONFIG_AD5380) += ad5380.o
 obj-$(CONFIG_AD5421) += ad5421.o
drivers/iio/dac/ad3552r.c new file +1199 -0

1,199 lines not on this page · raw message

Signature · 1 line
2.27.0
maintainer
#5 lore raw
Quoted 8 lines · On Mon, 8 Nov 2021 08:24:47 +0000 Mihail Chindris <[email protected]>…

The AD3552R-16 is a low drift ultrafast, 16-bit accuracy, current output digital-to-analog converter (DAC) designed to generate multiple output voltage span ranges. The AD3552R-16 operates with a fixed 2.5V reference.

Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/ad3552r.pdf

Hi Mihail,

Main thing here is to give time for Rob and others to take a look at the dt-binding but in meantime I thought I'd give the driver one last look and spotted a few minor things.

Jonathan

Quoted 46 lines
---
 drivers/iio/dac/Kconfig   |   10 +
 drivers/iio/dac/Makefile  |    1 +
 drivers/iio/dac/ad3552r.c | 1199 +++++++++++++++++++++++++++++++++++++
 3 files changed, 1210 insertions(+)
 create mode 100644 drivers/iio/dac/ad3552r.c
drivers/iio/dac/Kconfig +10 -0
index 75e1f2b48638..ced6428f2c92 100644
@@ -6,6 +6,16 @@
 
 menu "Digital to analog converters"
 
+config AD3552R
+	tristate "Analog Devices AD3552R DAC driver"
+	depends on SPI_MASTER
+	help
+	  Say yes here to build support for Analog Devices AD3552R
+	  Digital to Analog Converter.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called ad3552r.
+
 config AD5064
 	tristate "Analog Devices AD5064 and similar multi-channel DAC driver"
 	depends on (SPI_MASTER && I2C!=m) || I2C
drivers/iio/dac/Makefile +1 -0
index 33e16f14902a..dffe36efd8ff 100644
@@ -4,6 +4,7 @@
 #
 
 # When adding new entries keep the list in alphabetical order
+obj-$(CONFIG_AD3552R) += ad3552r.o
 obj-$(CONFIG_AD5360) += ad5360.o
 obj-$(CONFIG_AD5380) += ad5380.o
 obj-$(CONFIG_AD5421) += ad5421.o
drivers/iio/dac/ad3552r.c new file +0 -0
new file mode 100644
index 000000000000..4a98fa734e8a
@@ -0,0 +1,1199 @@

...

drivers/iio/dac/ad3552r.c line 124

Quoted 7 lines
drivers/iio/dac/ad3552r.c new file +7 -0
+
+/* Useful defines */
+#define AD3552R_NUM_CH					2
+#define AD3552R_MASK_CH(ch)				BIT(ch)
+#define AD3552R_MASK_ALL_CH				GENMASK(1, 0)
+#define AD3552R_MAX_REG_SIZE				3
+#define AD3552R_READ_BIT				(1 << 7)

BIT(7)?

Quoted 23 lines
drivers/iio/dac/ad3552r.c new file +22 -0
+#define AD3552R_ADDR_MASK				GENMASK(6, 0)
+#define AD3552R_CRC_ENABLE_VALUE			(BIT(6) | BIT(1))
+#define AD3552R_CRC_DISABLE_VALUE			GENMASK(1, 0)
+#define AD3552R_CRC_POLY				0x07
+#define AD3552R_CRC_SEED				0xA5
+#define AD3552R_MASK_DAC_12B				0xFFF0
+#define AD3552R_DEFAULT_CONFIG_B_VALUE			0x8
+#define AD3552R_SCRATCH_PAD_TEST_VAL1			0x34
+#define AD3552R_SCRATCH_PAD_TEST_VAL2			0xB2
+#define AD3552R_GAIN_SCALE				1000
+#define AD3552R_LDAC_PULSE_US				100
+
 
+
+struct ad3552r_desc {
+	/*
+	 * Used to look the spi bus for atomic operations where needed and
+	 * also protect access to buf_data below
+	 */
+	struct mutex		lock;
+	struct gpio_desc	*gpio_reset;
+	struct gpio_desc	*gpio_ldac;
+	struct spi_device	*spi;

drivers/iio/dac/ad3552r.c

Quoted 10 lines
drivers/iio/dac/ad3552r.c new file +10 -0
+	struct ad3552r_ch_data	ch_data[AD3552R_NUM_CH];
+	struct iio_chan_spec	channels[AD3552R_NUM_CH + 1];
+	unsigned long		enabled_ch;
+	unsigned int		num_ch;
+	enum ad3542r_id		chip_id;
+	/*
+	 * The maximum spi transfer size consist 1 bytes (reg address)
+	 * + 2 registers of 3 bytes + 1 reg of 1 byte (SW LDAC)
+	 */
+	u8 buf_data[8] ____cacheline_aligned;

As mentioned below - given how this is used as a bounce buffer, you could just use spi_write_then_read() in all paths and avoid the complexity.

Quoted 2 lines
drivers/iio/dac/ad3552r.c new file +2 -0
+};
+

...

Quoted 12 lines
drivers/iio/dac/ad3552r.c new file +12 -0
+/* 0 -> reg addr, 1->ch0 mask, 2->ch1 mask */
+static const u16 addr_mask_map_ch[][3] = {
+	[AD3552R_CH_DAC_POWERDOWN] = {
+			AD3552R_REG_ADDR_POWERDOWN_CONFIG,
+			AD3552R_MASK_CH_DAC_POWERDOWN(0),
+			AD3552R_MASK_CH_DAC_POWERDOWN(1)
+	},
+	[AD3552R_CH_AMPLIFIER_POWERDOWN] = {
+			AD3552R_REG_ADDR_POWERDOWN_CONFIG,
+			AD3552R_MASK_CH_AMPLIFIER_POWERDOWN(0),
+			AD3552R_MASK_CH_AMPLIFIER_POWERDOWN(1)
+	},

drivers/iio/dac/ad3552r.c line 373

Quoted 10 lines
drivers/iio/dac/ad3552r.c new file +10 -0
+	[AD3552R_CH_OUTPUT_RANGE_SEL] = {
+			AD3552R_REG_ADDR_CH0_CH1_OUTPUT_RANGE,
+			AD3552R_MASK_CH_OUTPUT_RANGE_SEL(0),
+			AD3552R_MASK_CH_OUTPUT_RANGE_SEL(1)
+	},
+	/*
+	 * This attributes are update by the chip on 16B and 24B no matter to
+	 * what register the write is done
+	 */
+	[AD3552R_CH_TRIGGER_SOFTWARE_LDAC] = {

Is this used?

Quoted 22 lines
drivers/iio/dac/ad3552r.c new file +21 -0
+			AD3552R_REG_ADDR_SW_LDAC_16B,
+			AD3552R_MASK_CH(0),
+			AD3552R_MASK_CH(1)
+	},
+	[AD3552R_CH_HW_LDAC_MASK] = {
+			AD3552R_REG_ADDR_HW_LDAC_16B,
+			AD3552R_MASK_CH(0),
+			AD3552R_MASK_CH(1)
+	},
+	[AD3552R_CH_SELECT] = {
+			AD3552R_REG_ADDR_CH_SELECT_16B,
+			AD3552R_MASK_CH(0),
+			AD3552R_MASK_CH(1)
+	}
+};
+
 
+
+/* SPI transfer to device */
+static int ad3552r_transfer(struct ad3552r_desc *dac, u8 addr, u32 len,
+			    u8 *data, bool is_read)
+{

drivers/iio/dac/ad3552r.c line 425

Quoted 10 lines
drivers/iio/dac/ad3552r.c new file +10 -0
+	u8 instr;
+
+	instr = addr & AD3552R_ADDR_MASK;
+	instr |= is_read ? AD3552R_READ_BIT : 0;
+	if (is_read)
+		return spi_write_then_read(dac->spi, &instr, 1, data, len);
+
+	dac->buf_data[0] = instr;
+	memcpy(dac->buf_data + 1, data, len);
+	return spi_write(dac->spi, dac->buf_data, len + 1);

If you are going to have to do a small memory copy anyway, you could just use spi_write_then_read() with a 0 size read. It ends up very similar to what you are doing here.

drivers/iio/dac/ad3552r.c line 590

Quoted 7 lines
drivers/iio/dac/ad3552r.c new file +6 -0
+}
+
 
+
+/*
+ * Device type specific information.
+ */

Not sure this is a particularly useful comment - I'd drop it.

Quoted 5 lines
drivers/iio/dac/ad3552r.c new file +5 -0
+static const struct iio_info ad3552r_iio_info = {
+	.read_raw = ad3552r_read_raw,
+	.write_raw = ad3552r_write_raw
+};
+

...

Quoted 51 lines
drivers/iio/dac/ad3552r.c new file +51 -0
+static int ad3552r_configure_custom_gain(struct ad3552r_desc *dac,
+					 struct fwnode_handle *child,
+					 u32 ch)
+{
+	struct device *dev = &dac->spi->dev;
+	struct fwnode_handle *gain_child;
+	u32 val;
+	int err;
+	u8 addr;
+	u16 mask = 0, reg = 0, offset;
+
+	gain_child = fwnode_get_named_child_node(child,
+						 "custom-output-range-config");
+	if (IS_ERR(gain_child)) {
+		dev_err(dev,
+			"mandatory custom-output-range-config property missing\n");
+		return PTR_ERR(gain_child);
+	}
+
+	dac->ch_data[ch].range_override = 1;
+	reg |= ad3552r_field_prep(1, AD3552R_MASK_CH_RANGE_OVERRIDE);
+	mask |= AD3552R_MASK_CH_RANGE_OVERRIDE;
+
+	err = fwnode_property_read_u32(gain_child, "adi,gain-scaling-p", &val);
+	if (err) {
+		dev_err(dev, "mandatory adi,gain-scaling-p property missing\n");
+		goto put_child;
+	}
+	reg |= ad3552r_field_prep(val, AD3552R_MASK_CH_GAIN_SCALING_P);
+	mask |= AD3552R_MASK_CH_GAIN_SCALING_P;
+	dac->ch_data[ch].p = val;
+
+	err = fwnode_property_read_u32(gain_child, "adi,gain-scaling-n", &val);
+	if (err) {
+		dev_err(dev, "mandatory adi,gain-scaling-n property missing\n");
+		goto put_child;
+	}
+	reg |= ad3552r_field_prep(val, AD3552R_MASK_CH_GAIN_SCALING_N);
+	mask |= AD3552R_MASK_CH_GAIN_SCALING_N;
+	dac->ch_data[ch].n = val;
+
+	err = fwnode_property_read_u32(gain_child, "adi,rfb-ohms", &val);
+	if (err) {
+		dev_err(dev, "mandatory adi,rfb-ohms property missing\n");
+		goto put_child;
+	}
+	dac->ch_data[ch].rfb = val;
+
+	err = fwnode_property_read_u32(gain_child, "adi,gain-offset", &val);
+	if (err) {
+		dev_err(dev, "mandatory adi,gain-offset property missing\n");

drivers/iio/dac/ad3552r.c line 936

Quoted 10 lines
drivers/iio/dac/ad3552r.c new file +10 -0
+		goto put_child;
+	}
+	dac->ch_data[ch].gain_offset = val;
+
+	offset = abs((s32)val);
+	reg |= ad3552r_field_prep((offset >> 8), AD3552R_MASK_CH_OFFSET_BIT_8);
+	mask |= AD3552R_MASK_CH_OFFSET_BIT_8;
+
+	reg |= ad3552r_field_prep((s32)val < 0, AD3552R_MASK_CH_OFFSET_POLARITY);
+	mask |= AD3552R_MASK_CH_OFFSET_POLARITY;

mask never used? I'm guessing left over from before you introduced your field_prep function.

Quoted 30 lines
drivers/iio/dac/ad3552r.c new file +30 -0
+	addr = AD3552R_REG_ADDR_CH_GAIN(ch);
+	err = ad3552r_write_reg(dac, addr,
+				offset & AD3552R_MASK_CH_OFFSET_BITS_0_7);
+	if (err) {
+		dev_err(dev, "Error writing register\n");
+		goto put_child;
+	}
+
+	err = ad3552r_write_reg(dac, addr, reg);
+	if (err) {
+		dev_err(dev, "Error writing register\n");
+		goto put_child;
+	}
+
+put_child:
+	fwnode_handle_put(gain_child);
+
+	return err;
+}
+
+static int ad3552r_configure_device(struct ad3552r_desc *dac)
+{
+	struct device *dev = &dac->spi->dev;
+	struct fwnode_handle *child;
+	struct regulator *vref;
+	int err, cnt = 0, voltage, delta = 100000;
+	u32 vals[2], val, ch;
+
+	dac->gpio_ldac = devm_gpiod_get_optional(dev, "ldac", GPIOD_OUT_HIGH);
+	if (IS_ERR(dac->gpio_ldac))

drivers/iio/dac/ad3552r.c line 976

Quoted 10 lines
drivers/iio/dac/ad3552r.c new file +10 -0
+		return dev_err_probe(dev, PTR_ERR(dac->gpio_ldac),
+				     "Error getting gpio ldac");
+
+	vref = devm_regulator_get_optional(dev, "vref");
+	if (IS_ERR(vref)) {
+		if (PTR_ERR(vref) != -ENODEV)
+			return dev_err_probe(dev, PTR_ERR(vref),
+					     "Error getting vref");
+		vref = NULL;
+	}

Perhaps use an else branch for this so that we don't need to bother with setting vref = NULL

	if (IS_ERR(vref)) {
		if (PTR_ERR(vref) != -ENODEV)
			return dev_err_probe(dev, PTR_ERR(vref),
					     "Error getting vref");
		if (device_property_read_bool(dev, "adi,vref-out-en"))
			val = AD3552R_INTERNAL_VREF_PIN_2P5V;
		else
			val = AD3552R_INTERNAL_VREF_PIN_FLOATING;
	} else {

// Need to turn it on somewhere, so regulator_enable() + devm handler to turn it off again.

		voltage = regulator_get_voltage(vref);
		if (voltage > 2500000 + delta || voltage < 2500000 - delta) {
			dev_warn(dev, "vref-supply must be 2.5V");
			return -EINVAL;
		}
		val = AD3552R_EXTERNAL_VREF_PIN_INPUT;
	}
Quoted 113 lines
drivers/iio/dac/ad3552r.c new file +113 -0
+	if (vref) {
+		voltage = regulator_get_voltage(vref);
+		if (voltage > 2500000 + delta || voltage < 2500000 - delta) {
+			dev_warn(dev, "vref-supply must be 2.5V");
+			return -EINVAL;
+		}
+		val = AD3552R_EXTERNAL_VREF_PIN_INPUT;
+	} else {
+		if (device_property_read_bool(dev, "adi,vref-out-en"))
+			val = AD3552R_INTERNAL_VREF_PIN_2P5V;
+		else
+			val = AD3552R_INTERNAL_VREF_PIN_FLOATING;
+	}
+	err = ad3552r_update_reg_field(dac,
+				       addr_mask_map[AD3552R_VREF_SELECT][0],
+				       addr_mask_map[AD3552R_VREF_SELECT][1],
+				       val);
+	if (err)
+		return err;
+
+	err = device_property_read_u32(dev, "adi,sdo-drive-strength", &val);
+	if (!err) {
+		if (val > 3) {
+			dev_err(dev, "adi,sdo-drive-strength must be less than 4\n");
+			return -EINVAL;
+		}
+
+		err = ad3552r_update_reg_field(dac,
+					       addr_mask_map[AD3552R_SDO_DRIVE_STRENGTH][0],
+					       addr_mask_map[AD3552R_SDO_DRIVE_STRENGTH][1],
+					       val);
+		if (err)
+			return err;
+	}
+
+	dac->num_ch = device_get_child_node_count(dev);
+	if (!dac->num_ch) {
+		dev_err(dev, "No channels defined\n");
+		return -ENODEV;
+	}
+
+	device_for_each_child_node(dev, child) {
+		err = fwnode_property_read_u32(child, "reg", &ch);
+		if (err) {
+			dev_err(dev, "mandatory reg property missing\n");
+			goto put_child;
+		}
+		if (ch >= AD3552R_NUM_CH) {
+			dev_err(dev, "reg must be less than %d\n",
+				AD3552R_NUM_CH);
+			err = -EINVAL;
+			goto put_child;
+		}
+
+		if (fwnode_property_present(child, "adi,output-range-microvolt")) {
+			err = fwnode_property_read_u32_array(child,
+							     "adi,output-range-microvolt",
+							     vals,
+							     2);
+			if (err) {
+				dev_err(dev,
+					"mandatory adi,output-range-microvolt property missing\n");
+				goto put_child;
+			}
+
+			err = ad3552r_find_range(dac->chip_id, vals);
+			if (err < 0) {
+				dev_err(dev,
+					"Invalid adi,output-range-microvolt value\n");
+				goto put_child;
+			}
+			val = err;
+			err = ad3552r_set_ch_value(dac,
+						   AD3552R_CH_OUTPUT_RANGE_SEL,
+						   ch, val);
+			if (err)
+				goto put_child;
+
+			dac->ch_data[ch].range = val;
+		} else {
+			err = ad3552r_configure_custom_gain(dac, child, ch);
+			if (err)
+				goto put_child;
+		}
+
+		ad3552r_calc_gain_and_offset(dac, ch);
+		dac->enabled_ch |= BIT(ch);
+
+		err = ad3552r_set_ch_value(dac, AD3552R_CH_SELECT, ch, 1);
+		if (err < 0)
+			goto put_child;
+
+		dac->channels[cnt] = AD3552R_CH_DAC(ch);
+		++cnt;
+
+	}
+
+	/* Disable unused channels */
+	for_each_clear_bit(ch, &dac->enabled_ch, AD3552R_NUM_CH) {
+		err = ad3552r_set_ch_value(dac, AD3552R_CH_AMPLIFIER_POWERDOWN,
+					   ch, 0);
+		if (err)
+			return err;
+	}
+
+	dac->num_ch = cnt;
+
+	return 0;
+put_child:
+	fwnode_handle_put(child);
+
+	return err;
+}

...

#6 lore raw
Quoted 12 lines
Quoted 7 lines

The AD3552R-16 is a low drift ultrafast, 16-bit accuracy, current output digital-to-analog converter (DAC) designed to generate multiple output voltage span ranges.
The AD3552R-16 operates with a fixed 2.5V reference.

Datasheet:
https://www.analog.com/media/en/technical-documentation/data-

sheets/ad

Quoted 3 lines

3552r.pdf

Quoted 8 lines

Hi Mihail,

Main thing here is to give time for Rob and others to take a look at the dt- binding but in meantime I thought I'd give the driver one last look and spotted a few minor things.

Jonathan

Hi Jonathan

As per Lars' comment, it seems that the issue was similar and if I understood correctly the replay, it seems that the naming is ok. Anyway, I implemented your latest suggestions locally and I will wait for a feedback on the device tree to send the new version.

Mihail

...