Skip to contentSkip to conversation

Add AD74413R driver

PATCH Superseded Cosmin Tanislav 3 participants 7 messages Nov 5, 2021 – Nov 13, 2021

Subsystems (5)

v3 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v3 cover Add AD74413R driver

V1 -> V2
* sign off using company email

V2 -> V3
* replace gpo config firmware flag with one flag specifying whether gpo is in comparator mode
* create two separate gpiochips, one output-only for GPO pins not in comparator mode and one input-only for the value of digital input channels
* wire up all gpo functionalities using pinconf
* keep number of characters per line under 80
* rework locking
* do not invalidate other chip revisions
* do not set indio device parent
* print probe error for refin regulator
* move conversion from range register value to range / offset / raw offset into separate function
* module.h -> mod_devicetable.h
* use generic firmware interface functions
* add comment regarding cache alignment
* add comment regarding ADC channels buffered read setup
* un-inline comment regarding 100us delay for conversion start
* inline return statements
* remove assignments to val2 where not necessary
* local_channels -> chans
* index -> i
* channel_config -> config
* IIO_ALTVOLTAGE -> IIO_VOLTAGE
* .info_mask_shared_by_type_available -> .info_mask_separate_available
* remove unlikely probe error messages
* use an array indexed by channel function for retrieving iio channels
* count iio channels while parsing
* move HART rate rejection outside of setter
* move channel function validation outside of setter
* use SPI messages for read and write
* validate DAC code earlier
* simplify switches to only handle existing iio channels
* pass indio_dev into functions needing access to it
* pass spi into devm_regmap_init
* dt-bindings: sort compatibles
* dt-bindings: remove driver word from description
* dt-bindings: remove refin supply description
* dt-bindings: specify channel function default value
* dt-bindings: remove maxItems from scalar value

Cosmin Tanislav (3):
iio: add adddac subdirectory
dt-bindings: iio: add AD74413R
iio: addac: add AD74413R driver

 .../bindings/iio/addac/adi,ad74413r.yaml      |  153 ++
 MAINTAINERS                                   |    9 +
 drivers/iio/Kconfig                           |    1 +
 drivers/iio/Makefile                          |    1 +
 drivers/iio/addac/Kconfig                     |   20 +
 drivers/iio/addac/Makefile                    |    7 +
 drivers/iio/addac/ad74413r.c                  | 1492 +++++++++++++++++
 include/dt-bindings/iio/addac/adi,ad74413r.h  |   21 +
 8 files changed, 1704 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/addac/adi,ad74413r.yaml
 create mode 100644 drivers/iio/addac/Kconfig
 create mode 100644 drivers/iio/addac/Makefile
 create mode 100644 drivers/iio/addac/ad74413r.c
 create mode 100644 include/dt-bindings/iio/addac/adi,ad74413r.h
Signature · 1 line
2.33.1
#1 lore raw

PATCH v3 1/3 iio: add adddac subdirectory

From: Cosmin Tanislav <[email protected]>

For IIO devices that expose both ADC and DAC functionality.

---
 drivers/iio/Kconfig        | 1 +
 drivers/iio/Makefile       | 1 +
 drivers/iio/addac/Kconfig  | 8 ++++++++
 drivers/iio/addac/Makefile | 6 ++++++
 4 files changed, 16 insertions(+)
 create mode 100644 drivers/iio/addac/Kconfig
 create mode 100644 drivers/iio/addac/Makefile
drivers/iio/Kconfig +1 -0
index 2334ad249b46..4fb4321a72cb 100644
@@ -70,6 +70,7 @@ config IIO_TRIGGERED_EVENT
 
 source "drivers/iio/accel/Kconfig"
 source "drivers/iio/adc/Kconfig"
+source "drivers/iio/addac/Kconfig"
 source "drivers/iio/afe/Kconfig"
 source "drivers/iio/amplifiers/Kconfig"
 source "drivers/iio/cdc/Kconfig"
drivers/iio/Makefile +1 -0
index 65e39bd4f934..8d48c70fee4d 100644
@@ -15,6 +15,7 @@ obj-$(CONFIG_IIO_TRIGGERED_EVENT) += industrialio-triggered-event.o
 
 obj-y += accel/
 obj-y += adc/
+obj-y += addac/
 obj-y += afe/
 obj-y += amplifiers/
 obj-y += buffer/
drivers/iio/addac/Kconfig new file +8 -0
new file mode 100644
index 000000000000..2e64d7755d5e
@@ -0,0 +1,8 @@
+#
+# ADC DAC drivers
+#
+# When adding new entries keep the list in alphabetical order
+
+menu "Analog to digital and digital to analog converters"
+
+endmenu
drivers/iio/addac/Makefile new file +6 -0
new file mode 100644
index 000000000000..b888b9ee12da
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Makefile for industrial I/O ADDAC drivers
+#
+
+# When adding new entries keep the list in alphabetical order
Signature · 1 line
2.33.1
#2 lore raw

PATCH v3 2/3 dt-bindings: iio: add AD74413R

In reply to Cosmin Tanislav

Add device tree bindings for AD74413R.

---
 .../bindings/iio/addac/adi,ad74413r.yaml      | 153 ++++++++++++++++++
 include/dt-bindings/iio/addac/adi,ad74413r.h  |  21 +++
 2 files changed, 174 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/addac/adi,ad74413r.yaml
 create mode 100644 include/dt-bindings/iio/addac/adi,ad74413r.h
Documentation/devicetree/bindings/iio/addac/adi,ad74413r.yaml new file +153 -0
new file mode 100644
index 000000000000..a1add5a2bce2
@@ -0,0 +1,153 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/addac/adi,ad74413r.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Analog Devices AD74413R/AD74412R device
+
+maintainers:
+  - Cosmin Tanislav <[email protected]>
+
+description: |
+  The AD74413R and AD74412R are quad-channel software configurable input/output
+  solutions for building and process control applications. They contain
+  functionality for analog output, analog input, digital input, resistance
+  temperature detector, and thermocouple measurements integrated
+  into a single chip solution with an SPI interface.
+  The devices feature a 16-bit ADC and four configurable 13-bit DACs to provide
+  four configurable input/output channels and a suite of diagnostic functions.
+
+properties:
+  compatible:
+    enum:
+      - adi,ad74412r
+      - adi,ad74413r
+
+  reg:
+    maxItems: 1
+
+  '#address-cells':
+    const: 1
+
+  '#size-cells':
+    const: 0
+
+  spi-max-frequency:
+    maximum: 1000000
+
+  spi-cpol: true
+
+  interrupts:
+    maxItems: 1
+
+  refin-supply: true
+
+  adi,rsense-resistance-ohms:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    description:
+      RSense resistance values in Ohms.
+
+required:
+  - compatible
+  - reg
+  - spi-max-frequency
+  - spi-cpol
+  - refin-supply
+  - adi,rsense-resistance-ohm
+
+additionalProperties: false
+
+patternProperties:
+  "^channel@[0-3]$":
+    type: object
+    description: Represents the external channels which are connected to the device.
+
+    properties:
+      reg:
+        description: |
+          The channel number. It can have up to 4 channels numbered from 0 to 3.
+        minimum: 0
+        maximum: 3
+
+      adi,ch-func:
+        $ref: /schemas/types.yaml#/definitions/uint32
+        description: |
+          Channel function.
+          HART functions are not supported on AD74412R.
+          0 - CH_FUNC_HIGH_IMPEDANCE
+          1 - CH_FUNC_VOLTAGE_OUTPUT
+          2 - CH_FUNC_CURRENT_OUTPUT
+          3 - CH_FUNC_VOLTAGE_INPUT
+          4 - CH_FUNC_CURRENT_INPUT_EXT_POWER
+          5 - CH_FUNC_CURRENT_INPUT_LOOP_POWER
+          6 - CH_FUNC_RESISTANCE_INPUT
+          7 - CH_FUNC_DIGITAL_INPUT_LOGIC
+          8 - CH_FUNC_DIGITAL_INPUT_LOOP_POWER
+          9 - CH_FUNC_CURRENT_INPUT_EXT_POWER_HART
+          10 - CH_FUNC_CURRENT_INPUT_LOOP_POWER_HART
+        minimum: 0
+        maximum: 10
+        default: 0
+
+      adi,gpo-comparator:
+        type: boolean
+        description: |
+          Whether to configure GPO as a comparator or not.
+          When not configured as a comparator, the GPO will be treated as an
+          output-only GPIO.
+
+    required:
+      - reg
+
+examples:
+  - |
+    spi0 {
+      #address-cells = <1>;
+      #size-cells = <0>;
+
+      cs-gpios = <&gpio 17 GPIO_ACTIVE_LOW>;
+      status = "okay";
+
+      ad74413r@0 {
+        compatible = "adi,ad74413r";
+        reg = <0>;
+        spi-max-frequency = <1000000>;
+        spi-cpol;
+
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        interrupt-parent = <&gpio>;
+        interrupts = <26 0>;
+
+        refin-supply = <&ad74413r_refin>;
+        adi,rsense-resistance-ohm = <100>;
+
+        channel@0 {
+          reg = <0>;
+
+          adi,ch-func = <CH_FUNC_VOLTAGE_OUTPUT>;
+        };
+
+        channel@1 {
+          reg = <1>;
+
+          adi,ch-func = <CH_FUNC_CURRENT_OUTPUT>;
+        };
+
+        channel@2 {
+          reg = <2>;
+
+          adi,ch-func = <CH_FUNC_DIGITAL_INPUT_LOGIC>;
+          adi,gpo-comparator;
+        };
+
+        channel@3 {
+          reg = <3>;
+
+          adi,ch-func = <CH_FUNC_CURRENT_INPUT_EXT_POWER>;
+        };
+      };
+    };
+...
include/dt-bindings/iio/addac/adi,ad74413r.h new file +21 -0
new file mode 100644
index 000000000000..a43b010f974f
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef _DT_BINDINGS_ADI_AD74413R_H
+#define _DT_BINDINGS_ADI_AD74413R_H
+
+#define CH_FUNC_HIGH_IMPEDANCE					0x0
+#define CH_FUNC_VOLTAGE_OUTPUT					0x1
+#define CH_FUNC_CURRENT_OUTPUT					0x2
+#define CH_FUNC_VOLTAGE_INPUT					0x3
+#define CH_FUNC_CURRENT_INPUT_EXT_POWER			0x4
+#define CH_FUNC_CURRENT_INPUT_LOOP_POWER		0x5
+#define CH_FUNC_RESISTANCE_INPUT				0x6
+#define CH_FUNC_DIGITAL_INPUT_LOGIC				0x7
+#define CH_FUNC_DIGITAL_INPUT_LOOP_POWER		0x8
+#define CH_FUNC_CURRENT_INPUT_EXT_POWER_HART	0x9
+#define CH_FUNC_CURRENT_INPUT_LOOP_POWER_HART	0xA
+
+#define CH_FUNC_MIN		CH_FUNC_HIGH_IMPEDANCE
+#define CH_FUNC_MAX		CH_FUNC_CURRENT_INPUT_LOOP_POWER_HART
+
+#endif /* _DT_BINDINGS_ADI_AD74413R_H */
Signature · 1 line
2.33.1
maintainer
#3 lore raw

Re: [PATCH v3 2/3] dt-bindings: iio: add AD74413R

Quoted 10 lines · Cosmin Tanislav

Add device tree bindings for AD74413R.

---
 .../bindings/iio/addac/adi,ad74413r.yaml      | 153 ++++++++++++++++++
 include/dt-bindings/iio/addac/adi,ad74413r.h  |  21 +++
 2 files changed, 174 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/addac/adi,ad74413r.yaml
 create mode 100644 include/dt-bindings/iio/addac/adi,ad74413r.h

My bot found errors running 'make DT_CHECKER_FLAGS=-m dt_binding_check' on your patch (DT_CHECKER_FLAGS is new in v5.13):

yamllint warnings/errors:

dtschema/dtc warnings/errors:
/builds/robherring/linux-dt-review/Documentation/devicetree/bindings/iio/addac/adi,ad74413r.yaml: properties:adi,rsense-resistance-ohms: '$ref' should not be valid under {'const': '$ref'}
hint: Standard unit suffix properties don't need a type $ref
from schema $id: http://devicetree.org/meta-schemas/core.yaml#
/builds/robherring/linux-dt-review/Documentation/devicetree/bindings/iio/addac/adi,ad74413r.yaml: ignoring, error in schema: properties: adi,rsense-resistance-ohms
warning: no schema found in file: ./Documentation/devicetree/bindings/iio/addac/adi,ad74413r.yaml
Error: Documentation/devicetree/bindings/iio/addac/adi,ad74413r.example.dts:23.32-33 syntax error
FATAL ERROR: Unable to parse input tree
make[1]: *** [scripts/Makefile.lib:385: Documentation/devicetree/bindings/iio/addac/adi,ad74413r.example.dt.yaml] Error 1
make[1]: *** Waiting for unfinished jobs....
make: *** [Makefile:1441: dt_binding_check] Error 2

doc reference errors (make refcheckdocs):

See https://patchwork.ozlabs.org/patch/1551417

This check can fail if there are any dependencies. The base for a patch series is generally the most recent rc1.

If you already ran 'make dt_binding_check' and didn't see the above error(s), then make sure 'yamllint' is installed and dt-schema is up to date:

pip3 install dtschema --upgrade

Please check and re-submit.

maintainer
#4 lore raw

Re: [PATCH v3 2/3] dt-bindings: iio: add AD74413R

In reply to Cosmin Tanislav
Quoted 3 lines · On Fri, 5 Nov 2021 16:35:49 +0200 Cosmin Tanislav <[email protected]>…

Add device tree bindings for AD74413R.

Hi Cosmin,

Other than the unnecessary $ref that Rob's scripts already pointed out this looks good to me.

Thanks,

Jonathan

Quoted 50 lines
---
 .../bindings/iio/addac/adi,ad74413r.yaml      | 153 ++++++++++++++++++
 include/dt-bindings/iio/addac/adi,ad74413r.h  |  21 +++
 2 files changed, 174 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/addac/adi,ad74413r.yaml
 create mode 100644 include/dt-bindings/iio/addac/adi,ad74413r.h
Documentation/devicetree/bindings/iio/addac/adi,ad74413r.yaml new file +37 -0
new file mode 100644
index 000000000000..a1add5a2bce2
@@ -0,0 +1,153 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/addac/adi,ad74413r.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Analog Devices AD74413R/AD74412R device
+
+maintainers:
+  - Cosmin Tanislav <[email protected]>
+
+description: |
+  The AD74413R and AD74412R are quad-channel software configurable input/output
+  solutions for building and process control applications. They contain
+  functionality for analog output, analog input, digital input, resistance
+  temperature detector, and thermocouple measurements integrated
+  into a single chip solution with an SPI interface.
+  The devices feature a 16-bit ADC and four configurable 13-bit DACs to provide
+  four configurable input/output channels and a suite of diagnostic functions.
+
+properties:
+  compatible:
+    enum:
+      - adi,ad74412r
+      - adi,ad74413r
+
+  reg:
+    maxItems: 1
+
+  '#address-cells':
+    const: 1
+
+  '#size-cells':
+    const: 0
+
+  spi-max-frequency:
+    maximum: 1000000

Documentation/devicetree/bindings/iio/addac/adi,ad74413r.yaml line 47

Quoted 10 lines
Documentation/devicetree/bindings/iio/addac/adi,ad74413r.yaml new file +10 -0
+
+  spi-cpol: true
+
+  interrupts:
+    maxItems: 1
+
+  refin-supply: true
+
+  adi,rsense-resistance-ohms:
+    $ref: /schemas/types.yaml#/definitions/uint32

As Rob's scripts pointed out: Now you have the ending specifying the units there is not need to also have $ref. The dt-schema special cases a bunch of unit types for us.

Quoted 133 lines
Documentation/devicetree/bindings/iio/addac/adi,ad74413r.yaml new file +106 -0
+    description:
+      RSense resistance values in Ohms.
+
+required:
+  - compatible
+  - reg
+  - spi-max-frequency
+  - spi-cpol
+  - refin-supply
+  - adi,rsense-resistance-ohm
+
+additionalProperties: false
+
+patternProperties:
+  "^channel@[0-3]$":
+    type: object
+    description: Represents the external channels which are connected to the device.
+
+    properties:
+      reg:
+        description: |
+          The channel number. It can have up to 4 channels numbered from 0 to 3.
+        minimum: 0
+        maximum: 3
+
+      adi,ch-func:
+        $ref: /schemas/types.yaml#/definitions/uint32
+        description: |
+          Channel function.
+          HART functions are not supported on AD74412R.
+          0 - CH_FUNC_HIGH_IMPEDANCE
+          1 - CH_FUNC_VOLTAGE_OUTPUT
+          2 - CH_FUNC_CURRENT_OUTPUT
+          3 - CH_FUNC_VOLTAGE_INPUT
+          4 - CH_FUNC_CURRENT_INPUT_EXT_POWER
+          5 - CH_FUNC_CURRENT_INPUT_LOOP_POWER
+          6 - CH_FUNC_RESISTANCE_INPUT
+          7 - CH_FUNC_DIGITAL_INPUT_LOGIC
+          8 - CH_FUNC_DIGITAL_INPUT_LOOP_POWER
+          9 - CH_FUNC_CURRENT_INPUT_EXT_POWER_HART
+          10 - CH_FUNC_CURRENT_INPUT_LOOP_POWER_HART
+        minimum: 0
+        maximum: 10
+        default: 0
+
+      adi,gpo-comparator:
+        type: boolean
+        description: |
+          Whether to configure GPO as a comparator or not.
+          When not configured as a comparator, the GPO will be treated as an
+          output-only GPIO.
+
+    required:
+      - reg
+
+examples:
+  - |
+    spi0 {
+      #address-cells = <1>;
+      #size-cells = <0>;
+
+      cs-gpios = <&gpio 17 GPIO_ACTIVE_LOW>;
+      status = "okay";
+
+      ad74413r@0 {
+        compatible = "adi,ad74413r";
+        reg = <0>;
+        spi-max-frequency = <1000000>;
+        spi-cpol;
+
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        interrupt-parent = <&gpio>;
+        interrupts = <26 0>;
+
+        refin-supply = <&ad74413r_refin>;
+        adi,rsense-resistance-ohm = <100>;
+
+        channel@0 {
+          reg = <0>;
+
+          adi,ch-func = <CH_FUNC_VOLTAGE_OUTPUT>;
+        };
+
+        channel@1 {
+          reg = <1>;
+
+          adi,ch-func = <CH_FUNC_CURRENT_OUTPUT>;
+        };
+
+        channel@2 {
+          reg = <2>;
+
+          adi,ch-func = <CH_FUNC_DIGITAL_INPUT_LOGIC>;
+          adi,gpo-comparator;
+        };
+
+        channel@3 {
+          reg = <3>;
+
+          adi,ch-func = <CH_FUNC_CURRENT_INPUT_EXT_POWER>;
+        };
+      };
+    };
+...
include/dt-bindings/iio/addac/adi,ad74413r.h new file +21 -0
new file mode 100644
index 000000000000..a43b010f974f
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef _DT_BINDINGS_ADI_AD74413R_H
+#define _DT_BINDINGS_ADI_AD74413R_H
+
+#define CH_FUNC_HIGH_IMPEDANCE					0x0
+#define CH_FUNC_VOLTAGE_OUTPUT					0x1
+#define CH_FUNC_CURRENT_OUTPUT					0x2
+#define CH_FUNC_VOLTAGE_INPUT					0x3
+#define CH_FUNC_CURRENT_INPUT_EXT_POWER			0x4
+#define CH_FUNC_CURRENT_INPUT_LOOP_POWER		0x5
+#define CH_FUNC_RESISTANCE_INPUT				0x6
+#define CH_FUNC_DIGITAL_INPUT_LOGIC				0x7
+#define CH_FUNC_DIGITAL_INPUT_LOOP_POWER		0x8
+#define CH_FUNC_CURRENT_INPUT_EXT_POWER_HART	0x9
+#define CH_FUNC_CURRENT_INPUT_LOOP_POWER_HART	0xA
+
+#define CH_FUNC_MIN		CH_FUNC_HIGH_IMPEDANCE
+#define CH_FUNC_MAX		CH_FUNC_CURRENT_INPUT_LOOP_POWER_HART
+
+#endif /* _DT_BINDINGS_ADI_AD74413R_H */
#5 lore raw

PATCH v3 3/3 iio: addac: add AD74413R driver

In reply to Cosmin Tanislav

Add IIO ADDAC driver for AD74413R.

---
 MAINTAINERS                  |    9 +
 drivers/iio/addac/Kconfig    |   12 +
 drivers/iio/addac/Makefile   |    1 +
 drivers/iio/addac/ad74413r.c | 1492 ++++++++++++++++++++++++++++++++++
 4 files changed, 1514 insertions(+)
 create mode 100644 drivers/iio/addac/ad74413r.c
MAINTAINERS +9 -0
index 37982beaf91f..174f0fd3d71c 100644
@@ -1069,6 +1069,15 @@ W:	http://ez.analog.com/community/linux-device-drivers
 F:	Documentation/devicetree/bindings/iio/adc/adi,ad7780.yaml
 F:	drivers/iio/adc/ad7780.c
 
+ANALOG DEVICES INC AD74413R DRIVER
+M:	Cosmin Tanislav <[email protected]>
+L:	[email protected]
+S:	Supported
+W:	http://ez.analog.com/community/linux-device-drivers
+F:	Documentation/devicetree/bindings/iio/addac/adi,ad74413r.yaml
+F:	drivers/iio/addac/ad74413r.c
+F:	include/dt-bindings/iio/addac/adi,ad74413r.h
+
 ANALOG DEVICES INC AD9389B DRIVER
 M:	Hans Verkuil <[email protected]>
 L:	[email protected]
drivers/iio/addac/Kconfig +12 -0
index 2e64d7755d5e..06b26f8d0144 100644
@@ -5,4 +5,16 @@
 
 menu "Analog to digital and digital to analog converters"
 
+config AD74413R
+	tristate "Analog Devices AD74413R/AD74412R driver"
+	depends on GPIOLIB && SPI
+	select REGMAP_SPI
+	select CRC8
+	help
+	  Say yes here to build support for Analog Devices AD74413R/AD74412R
+	  quad-channel software configurable input/output solution.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called ad74413r.
+
 endmenu
drivers/iio/addac/Makefile +1 -0
index b888b9ee12da..cfd4bbe64ad3 100644
@@ -4,3 +4,4 @@
 #
 
 # When adding new entries keep the list in alphabetical order
+obj-$(CONFIG_AD74413R) += ad74413r.o
drivers/iio/addac/ad74413r.c new file +1492 -0

1,492 lines not on this page · raw message

Signature · 1 line
2.33.1
maintainer
#6 lore raw

Re: [PATCH v3 3/3] iio: addac: add AD74413R driver

Quoted 1 line · On Fri, 5 Nov 2021 16:35:50 +0200 Cosmin Tanislav <[email protected]>…

Add IIO ADDAC driver for AD74413R.

Nice to have a tiny bit more detail in the description particularly as you support multiple parts but only mention one here or in the patch title. Generally a short description of the part + highlights of the features supported are useful things to have in the description of a new driver patch.

Also nice to have is an example of what the file structure in sysfs looks like. In this particular driver that would be really useful as it is rather non obvious!

Quoted 2 lines

I'll be looking for an ack from Linus W for this as it is also a GPIO driver and a slightly odd one at that for inputs anyway.

Given it's a complex driver I've probably spotted things I missed in earlier reviews as we are getting closer to where we want to be.

...

Quoted 5 lines
drivers/iio/addac/Kconfig +0 -0
index 2e64d7755d5e..06b26f8d0144 100644

drivers/iio/addac/Kconfig line 14

Quoted 10 lines
drivers/iio/addac/Kconfig +7 -0
@@ -5,4 +5,16 @@
 
 menu "Analog to digital and digital to analog converters"
 
+config AD74413R
+	tristate "Analog Devices AD74413R/AD74412R driver"
+	depends on GPIOLIB && SPI
+	select REGMAP_SPI
+	select CRC8
+	help
+	  Say yes here to build support for Analog Devices AD74413R/AD74412R

Put them in numeric order in descriptions. If we end up adding additional parts later, it is then obvious where they should go.

Quoted 6 lines
drivers/iio/addac/Kconfig +5 -0
+	  quad-channel software configurable input/output solution.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called ad74413r.
+
 endmenu

...

Quoted 80 lines
drivers/iio/addac/ad74413r.c new file +74 -0
new file mode 100644
index 000000000000..112e2810b4bc
@@ -0,0 +1,1492 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2021 Analog Devices, Inc.
+ * Author: Cosmin Tanislav <[email protected]>
+ */
+
+#include <linux/bitfield.h>
+#include <linux/crc8.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/gpio/driver.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+#include <linux/iio/trigger.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/triggered_buffer.h>
+#include <linux/interrupt.h>
+#include <linux/mod_devicetable.h>
+#include <linux/property.h>
+#include <linux/regmap.h>
+#include <linux/regulator/consumer.h>
+#include <linux/spi/spi.h>
+
+#include <dt-bindings/iio/addac/adi,ad74413r.h>
+
+#define AD74413R_CRC_POLYNOMIAL	0x7
+DECLARE_CRC8_TABLE(ad74413r_crc8_table);
+
+#define AD74413R_CHANNEL_MAX	4
+
+struct ad74413r_config {
+	bool hart_support;
+};
+
+struct ad74413r_channel_config {
+	u32 func;
+	bool gpo_comparator;
+	bool initialized;
+};
+
+struct ad74413r_channels {
+	struct iio_chan_spec *channels;
+	unsigned int num_channels;
+};
+
+struct ad74413r_state {
+	struct ad74413r_channel_config	channel_configs[AD74413R_CHANNEL_MAX];
+	unsigned int			gpo_gpio_offsets[AD74413R_CHANNEL_MAX];
+	unsigned int			comp_gpio_offsets[AD74413R_CHANNEL_MAX];
+	struct gpio_chip		gpo_gpiochip;
+	struct gpio_chip		comp_gpiochip;
+	struct mutex			lock;
+	struct completion		adc_data_completion;
+	unsigned int			num_gpo_gpios;
+	unsigned int			num_comparator_gpios;
+
+	struct spi_device		*spi;
+	struct regulator		*refin_reg;
+	struct regmap			*regmap;
+	struct device			*dev;
+	struct iio_trigger		*trig;
+	const struct ad74413r_config	*config;
+
+	u32				rsense_resistance_ohms;
+
+	struct spi_message		reg_read_msg;
+	struct spi_transfer		reg_read_xfer[2];
+
+	struct spi_message		reg_write_msg;
+	struct spi_transfer		reg_write_xfer[1];
+
+	size_t				adc_active_channels;
+	struct spi_message		adc_samples_msg;

drivers/iio/addac/ad74413r.c line 84

Quoted 10 lines
drivers/iio/addac/ad74413r.c new file +10 -0
+	struct spi_transfer		adc_samples_xfer[AD74413R_CHANNEL_MAX + 1];
+
+	/*
+	 * DMA (thus cache coherency maintenance) requires the
+	 * transfer buffers to live in their own cache lines.
+	 */
+	struct {
+		__be32 rx_buf[AD74413R_CHANNEL_MAX];
+		s64 timestamp;
+	} adc_samples ____cacheline_aligned;

I would rename this structure as it shadows the first part of the tx_buf that follows and that makes it a tiny bit tricky to spot the difference where these are used.

drivers/iio/addac/ad74413r.c line 87

Quoted 3 lines
drivers/iio/addac/ad74413r.c new file +3 -0
+	__be32				adc_samples_tx_buf[AD74413R_CHANNEL_MAX];
+	__be32				reg_read_buf[2];
+	__be32				reg_write_buf[1];

This doesn't seem to be treated as a __be32 in the driver, so perhaps that's not the appropriate type?

drivers/iio/addac/ad74413r.c line 140

Quoted 4 lines
drivers/iio/addac/ad74413r.c new file +3 -0
+};
+
 
+#define AD74413R_ADC_RESULT_MAX		((1 << 16) - 1)

GENMASK(15, 0) might be clearer?

drivers/iio/addac/ad74413r.c line 163

Quoted 4 lines
drivers/iio/addac/ad74413r.c new file +4 -0
+static void ad74413r_format_reg_write(unsigned int reg, unsigned int val,
+				      u8 *buf)
+{
+	buf[0] = reg & 0xff;

So reg is a u8? Make it one in the function definition and move any necessary masking to the callers.

drivers/iio/addac/ad74413r.c line 165

Quoted 2 lines
drivers/iio/addac/ad74413r.c new file +2 -0
+	buf[1] = (val >> 8) & 0xff;
+	buf[2] = val & 0xff;

This in put_unaligned_be16() I think. Use that to make this explicit if possible.

drivers/iio/addac/ad74413r.c line 172

Quoted 7 lines
drivers/iio/addac/ad74413r.c new file +7 -0
+	buf[3] = ad74413r_crc(buf);
+}
+
+static int ad74413r_reg_write(void *context, unsigned int reg, unsigned int val)
+{
+	struct spi_device *spi = context;
+	struct ad74413r_state *st = spi_get_drvdata(spi);

This is a bad sign that the driver structure is wrong. We should always be calling this sort of function from a place where we already have access to the top of a tree of structures (typically the iio_dev in this case).

I would pass either the struct iio_dev or the struct ad74413r_state into the regmap registration as the context.

drivers/iio/addac/ad74413r.c line 173

Quoted 1 line
drivers/iio/addac/ad74413r.c new file +1 -0
+	u8 *buf = (u8 *)&st->reg_write_buf[0];

As noted above, reg_write_buf[] gets treated as a u8 array so cleaner to make it such an array.

Quoted 15 lines
drivers/iio/addac/ad74413r.c new file +15 -0
+
+	ad74413r_format_reg_write(reg, val, buf);
+
+	return spi_sync(spi, &st->reg_write_msg);
+}
+
+static int ad74413r_crc_check(struct ad74413r_state *st, u8 *buf)
+{
+	u8 expected_crc = ad74413r_crc(buf);
+
+	if (buf[3] != expected_crc) {
+		dev_err(st->dev, "Bad CRC %02x for %02x%02x%02x\n",
+			buf[3], buf[0], buf[1], buf[2]);
+		return -EINVAL;
+	}

drivers/iio/addac/ad74413r.c line 198

Quoted 10 lines
drivers/iio/addac/ad74413r.c new file +10 -0
+
+	return 0;
+}
+
+static int ad74413r_reg_read(void *context, unsigned int reg, unsigned int *val)
+{
+	struct spi_device *spi = context;
+	struct ad74413r_state *st = spi_get_drvdata(spi);
+	u8 *tx_buf = (u8 *)&st->reg_read_buf[0];
+	u8 *rx_buf = (u8 *)&st->reg_read_buf[1];

Seems this is being treated as a u8 array, perhaps make it one to avoid the need to cast in lots of places.

drivers/iio/addac/ad74413r.c line 211

Quoted 13 lines
drivers/iio/addac/ad74413r.c new file +13 -0
+	int ret;
+
+	ad74413r_format_reg_write(AD74413R_REG_READ_SELECT, reg, tx_buf);
+
+	ret = spi_sync(spi, &st->reg_read_msg);
+	if (ret)
+		return ret;
+
+	ret = ad74413r_crc_check(st, rx_buf);
+	if (ret)
+		return ret;
+
+	*val = (rx_buf[1] << 8) | rx_buf[2];

get_unaligned_be16() to make it explicit what is going on here - also might be more efficient on some architectures.

Quoted 11 lines
drivers/iio/addac/ad74413r.c new file +11 -0
+
+	return 0;
+}
+
+const struct regmap_config ad74413r_regmap_config = {
+	.reg_bits = 8,
+	.val_bits = 16,
+	.reg_read = ad74413r_reg_read,
+	.reg_write = ad74413r_reg_write,
+};
+

...

Quoted 9 lines
drivers/iio/addac/ad74413r.c new file +9 -0
+
+static int ad74413r_range_to_voltage_offset_raw(struct ad74413r_state *st,
+						unsigned int range, int *val)
+{
+	switch (range) {
+	case AD74413R_ADC_RANGE_10V:
+	case AD74413R_ADC_RANGE_2P5V_EXT_POW:
+		*val = 0;
+		return 0;

drivers/iio/addac/ad74413r.c line 579

Quoted 10 lines
drivers/iio/addac/ad74413r.c new file +10 -0
+	case AD74413R_ADC_RANGE_2P5V_INT_POW:
+		*val = -AD74413R_ADC_RESULT_MAX;
+		return 0;
+	case AD74413R_ADC_RANGE_5V_BI_DIR:
+		*val = -AD74413R_ADC_RESULT_MAX / 2;
+		return 0;
+	}
+
+	dev_err(st->dev, "ADC range invalid\n");
+	return -EINVAL;

I'd prefer if this was in a default: branch of the above switch as makes it really obvious to compilers that we are handling all the cases.

Same for similar code elsewhere in the driver.

Quoted 1 line

+}

...

Quoted 30 lines
drivers/iio/addac/ad74413r.c new file +30 -0
+static int _ad74413r_get_single_adc_result(struct ad74413r_state *st,
+					   unsigned int channel, int *val)
+{
+	unsigned int uval;
+	int ret;
+
+	reinit_completion(&st->adc_data_completion);
+
+	ret = ad74413r_set_adc_channel_enable(st, channel, true);
+	if (ret)
+		return ret;
+
+	ret = ad74413r_set_adc_conv_seq(st, AD74413R_CONV_SEQ_SINGLE);
+	if (ret)
+		return ret;
+
+	ret = wait_for_completion_timeout(&st->adc_data_completion,
+					  msecs_to_jiffies(1000));
+	if (!ret) {
+		ret = -ETIMEDOUT;
+		return ret;
+	}
+
+	ret = regmap_read(st->regmap, AD74413R_REG_ADC_RESULT_X(channel),
+			  &uval);
+	if (ret)
+		return ret;
+
+	ret = ad74413r_set_adc_conv_seq(st, AD74413R_CONV_SEQ_OFF);
+	if (ret)

drivers/iio/addac/ad74413r.c line 789

Quoted 10 lines
drivers/iio/addac/ad74413r.c new file +10 -0
+		return ret;
+
+	ret = ad74413r_set_adc_channel_enable(st, channel, false);
+	if (ret)
+		return ret;
+
+	*val = uval;
+	ret = IIO_VAL_INT;
+
+	return ret;
	return IIO_VAL_INT;
Quoted 2 lines
drivers/iio/addac/ad74413r.c new file +2 -0
+}
+

...

Quoted 25 lines
drivers/iio/addac/ad74413r.c new file +25 -0
+
+static int ad74413r_update_scan_mode(struct iio_dev *indio_dev,
+				     const unsigned long *active_scan_mask)
+{
+	struct ad74413r_state *st = iio_priv(indio_dev);
+	struct spi_transfer *xfer;
+	unsigned int channel;
+	int index = 0;
+	int ret;
+
+	mutex_lock(&st->lock);
+
+	spi_message_init(&st->adc_samples_msg);
+	st->adc_active_channels = 0;
+
+	for_each_clear_bit(channel, active_scan_mask, AD74413R_CHANNEL_MAX) {
+		ret = ad74413r_set_adc_channel_enable(st, channel, false);
+		if (ret)
+			goto out;
+	}
+
+	if (*active_scan_mask == 0)
+		goto out;
+
+	/*

drivers/iio/addac/ad74413r.c line 854

Quoted 10 lines
drivers/iio/addac/ad74413r.c new file +10 -0
+	 * The read select register is used to select which register's value
+	 * will be sent by the slave on the next SPI frame.
+	 *
+	 * Create an SPI message that, on each step, writes to the read select
+	 * register to select the ADC result of the next enabled channel, and
+	 * reads the ADC result of the previous enabled channel.
+	 *
+	 * Example:
+	 * W: [WCH1] [WCH2] [WCH2] [WCH3] [    ]
+	 * R: [    ] [RCH1] [RCH2] [RCH3] [RCH4]

Nice explanation. Thanks.

Quoted 9 lines
drivers/iio/addac/ad74413r.c new file +9 -0
+	 */
+
+	for_each_set_bit(channel, active_scan_mask, AD74413R_CHANNEL_MAX) {
+		u8 *tx_buf;
+
+		ret = ad74413r_set_adc_channel_enable(st, channel, true);
+		if (ret)
+			goto out;
+

drivers/iio/addac/ad74413r.c line 873

Quoted 10 lines
drivers/iio/addac/ad74413r.c new file +10 -0
+		st->adc_active_channels++;
+
+		xfer = &st->adc_samples_xfer[index];
+
+		if (index == 0)
+			xfer->rx_buf = NULL;
+		else
+			xfer->rx_buf = &st->adc_samples.rx_buf[index - 1];
+
+		tx_buf = (u8 *)&st->adc_samples_tx_buf[index];

Whilst it would make the code a little more complex, I'd suggest making adc_samples_tx_buf a u8 array as it is not a __be32 one in any real sense.

Perhaps get tx_buf = st->adc_samples_tx_buf; outside the loop and then increment that pointer by 4 in here. That will match nicely with the len value as you set it so it will be obvious what is going on.

Quoted 10 lines
drivers/iio/addac/ad74413r.c new file +10 -0
+
+		xfer->tx_buf = tx_buf;
+		xfer->len = 4;
+		xfer->cs_change = 1;
+
+		ad74413r_format_reg_write(AD74413R_REG_READ_SELECT,
+					  AD74413R_REG_ADC_RESULT_X(channel),
+					  tx_buf);
+
+		spi_message_add_tail(xfer, &st->adc_samples_msg);

drivers/iio/addac/ad74413r.c line 893

Quoted 10 lines
drivers/iio/addac/ad74413r.c new file +10 -0
+
+		index++;
+	}
+
+	xfer = &st->adc_samples_xfer[index];
+
+	xfer->tx_buf = NULL;
+	xfer->rx_buf = &st->adc_samples.rx_buf[index - 1];
+	xfer->len = 4;
+	xfer->cs_change = 0;

Unlike the register read write equivalents of this in probe() setting this sequence up makes much more sense as it's both larger and in a performance sensitive path.

Quoted 9 lines
drivers/iio/addac/ad74413r.c new file +9 -0
+
+	spi_message_add_tail(xfer, &st->adc_samples_msg);
+
+out:
+	mutex_unlock(&st->lock);
+
+	return ret;
+}
+

...

Quoted 29 lines
drivers/iio/addac/ad74413r.c new file +29 -0
+
+static int ad74413r_read_raw(struct iio_dev *indio_dev,
+			     struct iio_chan_spec const *chan,
+			     int *val, int *val2, long info)
+{
+	struct ad74413r_state *st = iio_priv(indio_dev);
+
+	switch (info) {
+	case IIO_CHAN_INFO_SCALE:
+		switch (chan->type) {
+		case IIO_VOLTAGE:
+			if (chan->output)
+				return ad74413r_get_output_voltage_scale(st,
+					val, val2);
+			else
+				return ad74413r_get_input_voltage_scale(st,
+					chan->channel, val, val2);
+		case IIO_CURRENT:
+			if (chan->output)
+				return ad74413r_get_output_current_scale(st,
+					val, val2);
+			else
+				return ad74413r_get_input_current_scale(st,
+					chan->channel, val, val2);
+		default:
+			return -EINVAL;
+		}
+	case IIO_CHAN_INFO_OFFSET:
+		switch (chan->type) {

drivers/iio/addac/ad74413r.c line 954

Quoted 10 lines
drivers/iio/addac/ad74413r.c new file +10 -0
+		case IIO_VOLTAGE:
+			return ad74413r_get_input_voltage_offset(st,
+				chan->channel, val);
+		case IIO_CURRENT:
+			return ad74413_get_input_current_offset(st,
+				chan->channel, val);
+		default:
+			return -EINVAL;
+		}
+	case IIO_CHAN_INFO_RAW: {

Only need to define scope if you are going to have local variables. There aren't any so in this case brackets add nothing useful.

Quoted 13 lines
drivers/iio/addac/ad74413r.c new file +13 -0
+		if (chan->output)
+			return -EINVAL;
+
+		return ad74413r_get_single_adc_result(indio_dev, chan->channel,
+						      val);
+	}
+	case IIO_CHAN_INFO_PROCESSED: {
+		int ret;
+
+		ret = ad74413r_get_single_adc_result(indio_dev, chan->channel,
+						     val);
+		if (ret < 0)
+			return ret;

drivers/iio/addac/ad74413r.c line 977

Quoted 10 lines
drivers/iio/addac/ad74413r.c new file +10 -0
+
+		ad74413r_adc_to_resistance_result(*val, val);
+
+		return ret;
+	}
+	case IIO_CHAN_INFO_SAMP_FREQ:
+		return ad74413r_get_adc_rate(st, chan->channel, val);
+	}
+
+	return -EINVAL;

Move to default: in the switch

Quoted 14 lines
drivers/iio/addac/ad74413r.c new file +14 -0
+}
+
+static int ad74413r_write_raw(struct iio_dev *indio_dev,
+			      struct iio_chan_spec const *chan,
+			      int val, int val2, long info)
+{
+	struct ad74413r_state *st = iio_priv(indio_dev);
+
+	switch (info) {
+	case IIO_CHAN_INFO_RAW:
+		if (!chan->output)
+			return -EINVAL;
+
+		if (val > AD74413R_DAC_CODE_MAX) {

drivers/iio/addac/ad74413r.c line 1001

Quoted 10 lines
drivers/iio/addac/ad74413r.c new file +10 -0
+			dev_err(st->dev, "Invalid DAC code\n");
+			return -EINVAL;
+		}
+
+		return ad74413r_set_channel_dac_code(st, chan->channel, val);
+	case IIO_CHAN_INFO_SAMP_FREQ:
+		return ad74413r_set_adc_rate(st, chan->channel, val);
+	}
+
+	return -EINVAL;

Move that to a default: entry to suppress any 'unhandled case' warnings from static analysis.

Quoted 1 line

+}

...

drivers/iio/addac/ad74413r.c line 1043

Quoted 8 lines
drivers/iio/addac/ad74413r.c new file +8 -0
+static const struct iio_info ad74413r_info = {
+	.read_raw = &ad74413r_read_raw,
+	.write_raw = &ad74413r_write_raw,
+	.read_avail = &ad74413r_read_avail,
+	.update_scan_mode = &ad74413r_update_scan_mode,
+};
+
+#define AD74413R_CHANNEL(_type, _output, extra_mask_separate)		\

I'd be tempted to drop this first definition and just have the ADC and DAC ones below. Might cost you a few repeated lines, but reduce the levels of macros that a reviewer needs to look at.

drivers/iio/addac/ad74413r.c line 1046

Quoted 3 lines
drivers/iio/addac/ad74413r.c new file +3 -0
+		.type = _type,						\
+		.indexed = 1,						\
+		.output = _output,					\

For macros you should have () around all variables. Avoids odd results if particular weird strings are passed in. Normally one of the static checkers will complain about this.

Quoted 23 lines
drivers/iio/addac/ad74413r.c new file +23 -0
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW)		\
+				      | extra_mask_separate
+
+#define AD74413R_DAC_CHANNEL(_type, extra_mask_separate)		\
+	{								\
+		AD74413R_CHANNEL(_type, 1, extra_mask_separate),	\
+	}
+
+#define AD74413R_ADC_CHANNEL(_type, extra_mask_separate)		\
+	{								\
+		AD74413R_CHANNEL(_type, 0, extra_mask_separate		\
+			| BIT(IIO_CHAN_INFO_SAMP_FREQ)),		\
+		.info_mask_separate_available =				\
+			BIT(IIO_CHAN_INFO_SAMP_FREQ),			\
+		.scan_type = {						\
+			.sign = 'u',					\
+			.realbits = 16,					\
+			.storagebits = 32,				\
+			.shift = 8,					\
+			.endianness = IIO_BE,				\
+		},							\
+	}
+

drivers/iio/addac/ad74413r.c line 1079

Quoted 10 lines
drivers/iio/addac/ad74413r.c new file +10 -0
+#define AD74413R_ADC_VOLTAGE_CHANNEL					\
+	AD74413R_ADC_CHANNEL(IIO_VOLTAGE, BIT(IIO_CHAN_INFO_SCALE)	\
+			     | BIT(IIO_CHAN_INFO_OFFSET))
+
+#define AD74413R_ADC_CURRENT_CHANNEL					\
+	AD74413R_ADC_CHANNEL(IIO_CURRENT,  BIT(IIO_CHAN_INFO_SCALE)	\
+			     | BIT(IIO_CHAN_INFO_OFFSET))
+
+static struct iio_chan_spec ad74413r_high_impedance_channels[] = {
+	AD74413R_ADC_VOLTAGE_CHANNEL,

This is the same as the voltage_input_channels one below, perhaps just have one and point to it from both places?

Quoted 16 lines
drivers/iio/addac/ad74413r.c new file +16 -0
+};
+
+static struct iio_chan_spec ad74413r_voltage_output_channels[] = {
+	AD74413R_DAC_CHANNEL(IIO_VOLTAGE, BIT(IIO_CHAN_INFO_SCALE)),
+	AD74413R_ADC_CURRENT_CHANNEL,
+};
+
+static struct iio_chan_spec ad74413r_current_output_channels[] = {
+	AD74413R_DAC_CHANNEL(IIO_CURRENT, BIT(IIO_CHAN_INFO_SCALE)),
+	AD74413R_ADC_VOLTAGE_CHANNEL,
+};
+
+static struct iio_chan_spec ad74413r_voltage_input_channels[] = {
+	AD74413R_ADC_VOLTAGE_CHANNEL,
+};
+

drivers/iio/addac/ad74413r.c line 1105

Quoted 10 lines
drivers/iio/addac/ad74413r.c new file +10 -0
+static struct iio_chan_spec ad74413r_current_input_channels[] = {
+	AD74413R_ADC_CURRENT_CHANNEL,
+};
+
+static struct iio_chan_spec ad74413r_resistance_input_channels[] = {
+	AD74413R_ADC_CHANNEL(IIO_RESISTANCE, BIT(IIO_CHAN_INFO_PROCESSED)),
+};
+
+static struct iio_chan_spec ad74413r_digital_input_channels[] = {
+	AD74413R_ADC_VOLTAGE_CHANNEL,

I wonder if this indicates that we shouldn't be treating those
as digital inputs at all, but rather as IIO threshold events? That way the threshold programming etc will be natural. It's documented as the ADC being 'available' in digital input mode though so perhaps we are better sticking with what people expect to see?

I also see we have a counter on the inputs so I guess at somepoint we may end up with counter subsystem support in here as well.

Quoted 25 lines
drivers/iio/addac/ad74413r.c new file +25 -0
+};
+
+#define _AD74413R_CHANNELS(_channels)			\
+	{						\
+		.channels = _channels,			\
+		.num_channels = ARRAY_SIZE(_channels),	\
+	}
+
+#define AD74413R_CHANNELS(name) \
+	_AD74413R_CHANNELS(ad74413r_ ## name ## _channels)
+
+static const struct ad74413r_channels ad74413r_channels_map[] = {
+	[CH_FUNC_HIGH_IMPEDANCE] = AD74413R_CHANNELS(high_impedance),
+	[CH_FUNC_VOLTAGE_OUTPUT] = AD74413R_CHANNELS(voltage_output),
+	[CH_FUNC_CURRENT_OUTPUT] = AD74413R_CHANNELS(current_output),
+	[CH_FUNC_VOLTAGE_INPUT] = AD74413R_CHANNELS(voltage_input),
+	[CH_FUNC_CURRENT_INPUT_EXT_POWER] = AD74413R_CHANNELS(current_input),
+	[CH_FUNC_CURRENT_INPUT_LOOP_POWER] = AD74413R_CHANNELS(current_input),
+	[CH_FUNC_RESISTANCE_INPUT] = AD74413R_CHANNELS(current_input),
+	[CH_FUNC_DIGITAL_INPUT_LOGIC] = AD74413R_CHANNELS(current_input),
+	[CH_FUNC_DIGITAL_INPUT_LOOP_POWER] = AD74413R_CHANNELS(resistance_input),
+	[CH_FUNC_CURRENT_INPUT_EXT_POWER_HART] = AD74413R_CHANNELS(digital_input),
+	[CH_FUNC_CURRENT_INPUT_LOOP_POWER_HART] = AD74413R_CHANNELS(digital_input),
+};
+

...

Quoted 12 lines
drivers/iio/addac/ad74413r.c new file +12 -0
+static int ad74413r_setup_gpios(struct ad74413r_state *st)
+{
+	struct ad74413r_channel_config *config;
+	unsigned int gpio_i;
+	unsigned int i;
+	u8 gpo_config;
+	int ret;
+
+	for (i = 0, gpio_i = 0; i < AD74413R_CHANNEL_MAX; i++) {
+		config = &st->channel_configs[i];
+
+		if (config->gpo_comparator)

drivers/iio/addac/ad74413r.c line 1272

Quoted 10 lines
drivers/iio/addac/ad74413r.c new file +10 -0
+			gpo_config = AD74413R_GPO_CONFIG_COMPARATOR;
+		else
+			gpo_config = AD74413R_GPO_CONFIG_LOGIC;
+
+		ret = ad74413r_set_gpo_config(st, i, gpo_config);
+		if (ret)
+			return ret;
+
+		if (config->gpo_comparator)
+			continue;

Perhaps combine the two loops and use separate gpio_X indexes to deal with the two paths? Or am I missing a reason you can't do that?

Quoted 18 lines
drivers/iio/addac/ad74413r.c new file +18 -0
+
+		st->gpo_gpio_offsets[gpio_i++] = i;
+	}
+
+	for (i = 0, gpio_i = 0; i < AD74413R_CHANNEL_MAX; i++) {
+		config = &st->channel_configs[i];
+
+		if (config->func != CH_FUNC_DIGITAL_INPUT_LOGIC
+			|| config->func != CH_FUNC_DIGITAL_INPUT_LOOP_POWER) {
+			continue;
+		}
+
+		st->comp_gpio_offsets[gpio_i++] = i;
+	}
+
+	return 0;
+}
+

...

drivers/iio/addac/ad74413r.c line 1308

Quoted 13 lines
drivers/iio/addac/ad74413r.c new file +13 -0
+static int ad74413r_probe(struct spi_device *spi)
+{
+	struct ad74413r_state *st;
+	struct iio_dev *indio_dev;
+	const char *name;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
+	if (indio_dev == NULL)
+		return -ENOMEM;
+
+	st = iio_priv(indio_dev);
+	spi_set_drvdata(spi, st);

As noted above, this shouldn't be needed as we should be passing st of indio_dev to the regmap registration and hence be able to get them from context.

drivers/iio/addac/ad74413r.c line 1316

Quoted 8 lines
drivers/iio/addac/ad74413r.c new file +8 -0
+
+	st->spi = spi;
+	st->dev = &spi->dev;
+	st->config = device_get_match_data(&spi->dev);
+	mutex_init(&st->lock);
+	init_completion(&st->adc_data_completion);
+
+	name = dev_name(st->dev);

what does this return? For indio_dev->name it should be the part number and I'm not sure this is.

drivers/iio/addac/ad74413r.c line 1319

Quoted 3 lines
drivers/iio/addac/ad74413r.c new file +3 -0
+
+	st->reg_read_xfer[0].tx_buf = &st->reg_read_buf[0];
+	st->reg_read_xfer[0].len = 4;

If you can structure this as ARRAY_SIZE() of appropriate buffer and hence only have the size in one place that would be good.

I'd be tempted to have a reg_tx_buf[] and reg_rx_buf[] and reuse the tx_buf for both the read and write paths.

I think that would be cleaner than thes offsets into reG_read_buf

drivers/iio/addac/ad74413r.c line 1323

Quoted 4 lines
drivers/iio/addac/ad74413r.c new file +4 -0
+	st->reg_read_xfer[0].cs_change = 1;
+	st->reg_read_xfer[1].rx_buf = &st->reg_read_buf[1];
+	st->reg_read_xfer[1].len = 4;
+	st->reg_read_xfer[1].cs_change = 0;

cs_change == 0 will be the default and is the 'obvious' one at that so no need to set it here - just in the case when its different from normal.

drivers/iio/addac/ad74413r.c line 1331

Quoted 8 lines
drivers/iio/addac/ad74413r.c new file +8 -0
+	spi_message_init_with_transfers(&st->reg_read_msg,
+					st->reg_read_xfer, 2);
+
+	st->reg_write_xfer[0].tx_buf = &st->reg_write_buf[0];
+	st->reg_write_xfer[0].len = 4;
+	st->reg_write_xfer[0].cs_change = 0;
+	spi_message_init_with_transfers(&st->reg_write_msg,
+					st->reg_write_xfer, 1);

I'm a little doubtful that pre initializing these simple transfers is worth while. The write is just an spi_write() and the read could be nicely mapped onto spi_write_then_read() if that supported the cs_change flag. Unfortunately it doesn't, but but this is still not a particularly expensive thing to set up on each call allowing you to make it local to the calls and avoid the need for access to st. That would make the regmap code look a lot more like standard regmap_spi which would give me warn and fuzzy feelings :)

drivers/iio/addac/ad74413r.c line 1333

Quoted 2 lines
drivers/iio/addac/ad74413r.c new file +2 -0
+
+	st->regmap = devm_regmap_init(st->dev, NULL, spi,

Mentioned above, but given you need access to contents of st, I'd pass that rather than spi in here.

The pattern of transfers in this driver is reasonably common (I think) so it might be worth thinking about adding this crc checked spi transfer to the generic code at some point

drivers/iio/addac/ad74413r.c line 1340

Quoted 7 lines
drivers/iio/addac/ad74413r.c new file +7 -0
+				      &ad74413r_regmap_config);
+	if (IS_ERR(st->regmap))
+		return PTR_ERR(st->regmap);;
+
+	st->refin_reg = devm_regulator_get(st->dev, "refin");
+	if (IS_ERR(st->refin_reg))
+		return PTR_ERR(st->refin_reg);

return dev_err_probe()
for calls that we know can fail. It will print a message if not deferred but importantly it will add debug info for the reason the driver load was deferred which can be very helpful to have.

Quoted 11 lines
drivers/iio/addac/ad74413r.c new file +11 -0
+
+	ret = regulator_enable(st->refin_reg);
+	if (ret)
+		return ret;
+
+	ret = devm_add_action_or_reset(st->dev, ad74413r_regulator_disable,
+				       st->refin_reg);
+	if (ret)
+		return ret;
+
+	st->rsense_resistance_ohms = 100;

drivers/iio/addac/ad74413r.c line 1361

Quoted 10 lines
drivers/iio/addac/ad74413r.c new file +10 -0
+	device_property_read_u32(st->dev, "adi,rsense-resistance-ohms",
+				 &st->rsense_resistance_ohms);
+
+	st->trig = devm_iio_trigger_alloc(st->dev, "%s-dev%d", name,
+					  iio_device_id(indio_dev));
+	if (!st->trig)
+		return -ENOMEM;
+
+	st->trig->ops = &ad74413r_trigger_ops;
+	st->trig->dev.parent = st->dev;

The parent should be set by the devm_iio_trigger_alloc call above.

drivers/iio/addac/ad74413r.c line 1369

Quoted 8 lines
drivers/iio/addac/ad74413r.c new file +8 -0
+	iio_trigger_set_drvdata(st->trig, st);
+
+	ret = devm_iio_trigger_register(st->dev, st->trig);
+	if (ret)
+		return ret;
+
+	indio_dev->name = name;
+	indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;

INDIO_BUFFER_SOFTWARE should be set by the core in devm_iio_triggered_buffer_setup()

Quoted 24 lines
drivers/iio/addac/ad74413r.c new file +24 -0
+	indio_dev->info = &ad74413r_info;
+	indio_dev->trig = iio_trigger_get(st->trig);
+
+	ret = ad74413r_parse_channel_configs(indio_dev);
+	if (ret)
+		return ret;
+
+	ret = ad74413r_setup_channels(indio_dev);
+	if (ret) {
+		dev_err(st->dev, "Failed to setup channels: %d\n", ret);
+		return ret;
+	}
+
+	ret = ad74413r_setup_gpios(st);
+	if (ret) {
+		return ret;
+	}
+
+	if (st->num_gpo_gpios) {
+		st->gpo_gpiochip.owner = THIS_MODULE;
+		st->gpo_gpiochip.label = name;
+		st->gpo_gpiochip.base = -1;
+		st->gpo_gpiochip.ngpio = st->num_gpo_gpios;
+		st->gpo_gpiochip.parent = st->dev;

drivers/iio/addac/ad74413r.c line 1403

Quoted 10 lines
drivers/iio/addac/ad74413r.c new file +10 -0
+		st->gpo_gpiochip.can_sleep = true;
+		st->gpo_gpiochip.set = ad74413r_gpio_set;
+		st->gpo_gpiochip.set_multiple = ad74413r_gpio_set_multiple;
+		st->gpo_gpiochip.set_config = ad74413r_gpio_set_gpo_config;
+		st->gpo_gpiochip.get_direction =
+			ad74413r_gpio_get_gpo_direction;
+
+		ret = devm_gpiochip_add_data(st->dev, &st->gpo_gpiochip, st);
+		if (ret)
+			return ret;

Good idea to present this as two separate gpiochips.

Quoted 16 lines
drivers/iio/addac/ad74413r.c new file +16 -0
+	}
+
+	if (st->num_comparator_gpios) {
+		st->comp_gpiochip.owner = THIS_MODULE;
+		st->comp_gpiochip.label = name;
+		st->comp_gpiochip.base = -1;
+		st->comp_gpiochip.ngpio = st->num_comparator_gpios;
+		st->comp_gpiochip.parent = st->dev;
+		st->comp_gpiochip.can_sleep = true;
+		st->comp_gpiochip.get = ad74413r_gpio_get;
+		st->comp_gpiochip.get_multiple = ad74413r_gpio_get_multiple;
+		st->comp_gpiochip.set_config = ad74413r_gpio_set_comp_config;
+		st->comp_gpiochip.get_direction =
+			ad74413r_gpio_get_comp_direction;
+
+		ret = devm_gpiochip_add_data(st->dev, &st->comp_gpiochip, st);

drivers/iio/addac/ad74413r.c line 1429

Quoted 10 lines
drivers/iio/addac/ad74413r.c new file +10 -0
+		if (ret)
+			return ret;
+	}
+
+	ret = ad74413r_set_adc_conv_seq(st, AD74413R_CONV_SEQ_OFF);
+	if (ret)
+		return ret;
+
+	ret = devm_request_irq(st->dev, spi->irq, ad74413r_adc_data_interrupt,
+			       IRQF_TRIGGER_FALLING, name, indio_dev);

Generally we try to avoid forcing the direction of irqs in a driver. They should be correctly established by firmware and it is annoying common for someone to stick an inverter in the path as a cheap level converter.

Quoted 13 lines
drivers/iio/addac/ad74413r.c new file +13 -0
+	if (ret)
+		return ret;
+
+	ret = devm_iio_triggered_buffer_setup(st->dev, indio_dev,
+					      &iio_pollfunc_store_time,
+					      &ad74413r_trigger_handler,
+					      &ad74413r_buffer_ops);
+	if (ret)
+		return ret;
+
+	return devm_iio_device_register(st->dev, indio_dev);
+}
+