Skip to contentSkip to conversation

AD4130

PATCH Superseded Cosmin Tanislav 4 participants 9 messages Jun 8, 2022 – Jun 18, 2022

Subsystems (5)

v4 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v4 cover AD4130

AD4130-8 is an ultra-low power, high precision, measurement solution for low bandwidth battery operated applications.

The fully integrated AFE (Analog Front-End) includes a multiplexer for up to 16 single-ended or 8 differential inputs, PGA (Programmable Gain Amplifier), 24-bit Sigma-Delta ADC, on-chip reference and oscillator, selectable filter options, smart sequencer, sensor biasing and excitation options, diagnostics, and a FIFO buffer.

V1 -> V2
* add kernel version to ABI file
* merge ABI patch into driver patch
* make copyright header similar to other drivers
* rearrange includes
* use units.h defines where possible and add unit sufix to SOFT_RESET_SLEEP define
* remove ending comma to last members of enums / lists
* remove unused FILTER_MAX define
* use BIT macro for PIN_FN_*
* rearrange SETUP_SIZE definition
* group bools in ad4130_state and ad4130_chan_info
* put scale_tbls definition on one line
* remove newline before reg size == 0 check
* put mask used as value in a variable
* remove useless ret = 0 assignment
* make buffer attrs oneline
* use for_each_set_bit in update_scan_mode
* use if else for internal reference voltage error checking
* inline reference voltage check
* check number of vbias pins
* remove .has_int_pin = false
* remove avail_len for IIO_AVAIL_RANGE
* remove useless enabled_channels check in unlink_slot
* remove unused AD4130_RESET_CLK_COUNT define
* only call fwnode_handle_put for child in case of error
* default adi,reference-select to REFIN1
* default adi,int-ref-en to false
* of_irq_get_byname -> fwnode_irq_get_byname
* P1 -> P2 as interrupt pin options
* add missing comma in db3_freq_avail init
* cast values to u64 to make math using units.h work
* add datasheet reference to IRQ polarity
* add comment about disabling channels in predisable
* add part number prefix find_table_index
* return voltage from get_ref_voltage
* add datasheet reference for internal reference voltage selection
* add comment explaining AIN and GPIO pin sharing
* parse channel setup before parsing excitation pins
* only validate excitation pin if value is not off
* use FIELD_PREP for bipolar and int_ref_en
* put devm_regmap_init call on one line
* introduce a slot_info struct to contain setup_info for each slot
* enable internal reference automatically if needed
* decide mclk sel based on adi,ext-clk-freq and adi,int-clk-out
* dt-bindings: use internal reference explicitly
* dt-bindings: set type for adi,excitation-pin-0
* dt-bindings: set $ref for adi,vbias-pins
* dt-bindings: remove minItems from interrupts property
* dt-bindings: remove adi,int-ref-en default value
* dt-bindings: remove adi,bipolar default value
* dt-bindings: inline adi,int-ref-en description
* dt-bindings: default adi,reference-select to REFIN1
* dt-bindings: clean up description for diff-channels and adi,reference-select
* dt-bindings: add more text to interrupt-names description
* dt-bindings: turn interrupt-names into a single string
* dt-bindings: add maxItems to adi,vbias-pins

V2 -> V3
* dt-bindings: add interrupt controller include to example
* dt-bindings: remove $ref in diff-channels

V3 -> V4:
* handle watermark value as number of datum
* DOUT_OR_INT -> INT
* AD4130_8_NAME -> AD4130_NAME
* return early in case of failure when parsing fw channel
* use IIO_DMA_MINALIGN for aligning buffer
* add comments for fs_to_freq and freq_to_fs
* remove support for other variants because of unavailability of model ids for future chip variants
* remove support for db3 frequency because of inaccuracy when calculating
* remove ternary where possible
* refactor defines
* dt-bindings: add unevaluatedProperties: true to channel node

Cosmin Tanislav (2):
dt-bindings: iio: adc: add AD4130
iio: adc: ad4130: add AD4130 driver

 .../ABI/testing/sysfs-bus-iio-adc-ad4130      |   36 +
 .../bindings/iio/adc/adi,ad4130.yaml          |  262 +++
 MAINTAINERS                                   |    8 +
 drivers/iio/adc/Kconfig                       |   13 +
 drivers/iio/adc/Makefile                      |    1 +
 drivers/iio/adc/ad4130.c                      | 2015 +++++++++++++++++
 6 files changed, 2335 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-adc-ad4130
 create mode 100644 Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml
 create mode 100644 drivers/iio/adc/ad4130.c
Signature · 1 line
2.36.1
#1 lore raw

PATCH v4 1/2 dt-bindings: iio: adc: add AD4130

AD4130-8 is an ultra-low power, high precision, measurement solution for low bandwidth battery operated applications.

The fully integrated AFE (Analog Front-End) includes a multiplexer for up to 16 single-ended or 8 differential inputs, PGA (Programmable Gain Amplifier), 24-bit Sigma-Delta ADC, on-chip reference and oscillator, selectable filter options, smart sequencer, sensor biasing and excitation options, diagnostics, and a FIFO buffer.

---
 .../bindings/iio/adc/adi,ad4130.yaml          | 262 ++++++++++++++++++
 1 file changed, 262 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml
Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml new file +262 -0
new file mode 100644
index 000000000000..c06b8a96d31e
@@ -0,0 +1,262 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+# Copyright 2022 Analog Devices Inc.
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/adc/adi,ad4130.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Analog Devices AD4130 ADC device driver
+
+maintainers:
+  - Cosmin Tanislav <[email protected]>
+
+description: |
+  Bindings for the Analog Devices AD4130 ADC. Datasheet can be found here:
+    https://www.analog.com/media/en/technical-documentation/data-sheets/AD4130-8.pdf
+
+properties:
+  compatible:
+    enum:
+      - adi,ad4130
+
+  reg:
+    maxItems: 1
+
+  clocks:
+    maxItems: 1
+    description: phandle to the master clock (mclk)
+
+  clock-names:
+    items:
+      - const: mclk
+
+  interrupts:
+    maxItems: 1
+
+  interrupt-names:
+    description: |
+      Specify which interrupt pin should be configured as Data Ready / FIFO
+      interrupt.
+      Default if not supplied is int.
+    enum:
+      - int
+      - clk
+      - p2
+      - dout
+
+  '#address-cells':
+    const: 1
+
+  '#size-cells':
+    const: 0
+
+  refin1-supply:
+    description: refin1 supply. Can be used as reference for conversion.
+
+  refin2-supply:
+    description: refin2 supply. Can be used as reference for conversion.
+
+  avdd-supply:
+    description: AVDD voltage supply. Can be used as reference for conversion.
+
+  iovdd-supply:
+    description: IOVDD voltage supply. Used for the chip interface.
+
+  spi-max-frequency:
+    maximum: 5000000
+
+  adi,int-clk-out:
+    description: Specify if the internal clock should be exposed on the CLK pin.
+    type: boolean
+
+  adi,ext-clk-freq:
+    description: Specify the frequency of the external clock.
+    $ref: /schemas/types.yaml#/definitions/uint32
+    enum: [76800, 153600]
+    default: 76800
+
+  adi,bipolar:
+    description: Specify if the device should be used in bipolar mode.
+    type: boolean
+
+  adi,vbias-pins:
+    description: Analog inputs to apply a voltage bias of (AVDD − AVSS) / 2 to.
+    $ref: /schemas/types.yaml#/definitions/uint32-array
+    maxItems: 16
+    items:
+      minimum: 0
+      maximum: 15
+
+required:
+  - compatible
+  - reg
+  - interrupts
+
+patternProperties:
+  "^channel@([0-9]|1[0-5])$":
+    type: object
+    $ref: adc.yaml
+    unevaluatedProperties: false
+
+    properties:
+      reg:
+        description: |
+          The channel number.
+        items:
+          minimum: 0
+          maximum: 15
+
+      diff-channels:
+        description: |
+          Besides the analog inputs available, internal inputs can be used.
+          16: Internal temperature sensor.
+          17: AVSS
+          18: Internal reference
+          19: DGND
+          20: (AVDD − AVSS)/6+
+          21: (AVDD − AVSS)/6-
+          22: (IOVDD − DGND)/6+
+          23: (IOVDD − DGND)/6-
+          24: (ALDO − AVSS)/6+
+          25: (ALDO − AVSS)/6-
+          26: (DLDO − DGND)/6+
+          27: (DLDO − DGND)/6-
+          28: V_MV_P
+          29: V_MV_M
+        items:
+          minimum: 0
+          maximum: 29
+
+      adi,reference-select:
+        description: |
+          Select the reference source to use when converting on the
+          specific channel. Valid values are:
+          0: REFIN1(+)/REFIN1(−)
+          1: REFIN2(+)/REFIN2(−)
+          2: REFOUT/AVSS (Internal reference)
+          3: AVDD/AVSS
+          If not specified, REFIN1 is used.
+        $ref: /schemas/types.yaml#/definitions/uint32
+        enum: [0, 1, 2, 3]
+        default: 0
+
+      adi,excitation-pin-0:
+        description: |
+          Analog input to apply excitation current to while the channel
+          is active.
+        $ref: /schemas/types.yaml#/definitions/uint32
+        minimum: 0
+        maximum: 15
+        default: 0
+
+      adi,excitation-pin-1:
+        description: |
+          Analog input to apply excitation current to while this channel
+          is active.
+        $ref: /schemas/types.yaml#/definitions/uint32
+        minimum: 0
+        maximum: 15
+        default: 0
+
+      adi,excitation-current-0-nanoamps:
+        description: |
+          Excitation current in nanoamps to be applied to pin specified in
+          adi,excitation-pin-0 while this channel is active.
+        $ref: /schemas/types.yaml#/definitions/uint32
+        enum: [0, 100, 10000, 20000, 50000, 100000, 150000, 200000]
+        default: 0
+
+      adi,excitation-current-1-nanoamps:
+        description: |
+          Excitation current in nanoamps to be applied to pin specified in
+          adi,excitation-pin-1 while this channel is active.
+        $ref: /schemas/types.yaml#/definitions/uint32
+        enum: [0, 100, 10000, 20000, 50000, 100000, 150000, 200000]
+        default: 0
+
+      adi,burnout-current-nanoamps:
+        description: |
+          Burnout current in nanoamps to be applied for this channel.
+        $ref: /schemas/types.yaml#/definitions/uint32
+        enum: [0, 500, 2000, 4000]
+        default: 0
+
+      adi,buffered-positive:
+        description: Enable buffered mode for positive input.
+        type: boolean
+
+      adi,buffered-negative:
+        description: Enable buffered mode for negative input.
+        type: boolean
+
+    required:
+      - reg
+      - diff-channels
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+
+    spi {
+      #address-cells = <1>;
+      #size-cells = <0>;
+
+      adc@0 {
+        compatible = "adi,ad4130";
+        reg = <0>;
+
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        spi-max-frequency = <5000000>;
+        interrupts = <27 IRQ_TYPE_EDGE_FALLING>;
+        interrupt-parent = <&gpio>;
+
+        channel@0 {
+          reg = <0>;
+
+          adi,reference-select = <2>;
+
+          /* AIN8, AIN9 */
+          diff-channels = <8 9>;
+        };
+
+        channel@1 {
+          reg = <1>;
+
+          adi,reference-select = <2>;
+
+          /* AIN10, AIN11 */
+          diff-channels = <10 11>;
+        };
+
+        channel@2 {
+          reg = <2>;
+
+          adi,reference-select = <2>;
+
+          /* Temperature Sensor, DGND */
+          diff-channels = <16 19>;
+        };
+
+        channel@3 {
+          reg = <3>;
+
+          adi,reference-select = <2>;
+
+          /* Internal reference, DGND */
+          diff-channels = <18 19>;
+        };
+
+        channel@4 {
+          reg = <4>;
+
+          adi,reference-select = <2>;
+
+          /* DGND, DGND */
+          diff-channels = <19 19>;
+        };
+      };
+    };
Signature · 1 line
2.36.1
maintainer
#2 lore raw

Re: [PATCH v4 1/2] dt-bindings: iio: adc: add AD4130

Quoted 51 lines · Cosmin Tanislav

AD4130-8 is an ultra-low power, high precision, measurement solution for low bandwidth battery operated applications.

The fully integrated AFE (Analog Front-End) includes a multiplexer for up to 16 single-ended or 8 differential inputs, PGA (Programmable Gain Amplifier), 24-bit Sigma-Delta ADC, on-chip reference and oscillator, selectable filter options, smart sequencer, sensor biasing and excitation options, diagnostics, and a FIFO buffer.

---
 .../bindings/iio/adc/adi,ad4130.yaml          | 262 ++++++++++++++++++
 1 file changed, 262 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml
Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml new file +30 -0
new file mode 100644
index 000000000000..c06b8a96d31e
@@ -0,0 +1,262 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+# Copyright 2022 Analog Devices Inc.
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/adc/adi,ad4130.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Analog Devices AD4130 ADC device driver
+
+maintainers:
+  - Cosmin Tanislav <[email protected]>
+
+description: |
+  Bindings for the Analog Devices AD4130 ADC. Datasheet can be found here:
+    https://www.analog.com/media/en/technical-documentation/data-sheets/AD4130-8.pdf
+
+properties:
+  compatible:
+    enum:
+      - adi,ad4130
+
+  reg:
+    maxItems: 1
+
+  clocks:
+    maxItems: 1
+    description: phandle to the master clock (mclk)
+
+  clock-names:
+    items:

Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml line 40

Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml new file +10 -0
+      - const: mclk
+
+  interrupts:
+    maxItems: 1
+
+  interrupt-names:
+    description: |
+      Specify which interrupt pin should be configured as Data Ready / FIFO
+      interrupt.
+      Default if not supplied is int.

default: int

Quoted 24 lines
Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml new file +24 -0
+    enum:
+      - int
+      - clk
+      - p2
+      - dout
+
+  '#address-cells':
+    const: 1
+
+  '#size-cells':
+    const: 0
+
+  refin1-supply:
+    description: refin1 supply. Can be used as reference for conversion.
+
+  refin2-supply:
+    description: refin2 supply. Can be used as reference for conversion.
+
+  avdd-supply:
+    description: AVDD voltage supply. Can be used as reference for conversion.
+
+  iovdd-supply:
+    description: IOVDD voltage supply. Used for the chip interface.
+

Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml line 74

Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml new file +10 -0
+  spi-max-frequency:
+    maximum: 5000000
+
+  adi,int-clk-out:
+    description: Specify if the internal clock should be exposed on the CLK pin.
+    type: boolean
+
+  adi,ext-clk-freq:
+    description: Specify the frequency of the external clock.
+    $ref: /schemas/types.yaml#/definitions/uint32

-hz suffix and drop the type.

Quoted 19 lines
Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml new file +19 -0
+    enum: [76800, 153600]
+    default: 76800
+
+  adi,bipolar:
+    description: Specify if the device should be used in bipolar mode.
+    type: boolean
+
+  adi,vbias-pins:
+    description: Analog inputs to apply a voltage bias of (AVDD − AVSS) / 2 to.
+    $ref: /schemas/types.yaml#/definitions/uint32-array
+    maxItems: 16
+    items:
+      minimum: 0
+      maximum: 15
+
+required:
+  - compatible
+  - reg
+  - interrupts

Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml line 103

Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml new file +10 -0
+
+patternProperties:
+  "^channel@([0-9]|1[0-5])$":
+    type: object
+    $ref: adc.yaml
+    unevaluatedProperties: false
+
+    properties:
+      reg:
+        description: |

Don't need '|'.

Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml line 107

Quoted 4 lines
Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml new file +4 -0
+          The channel number.
+        items:
+          minimum: 0
+          maximum: 15

This allows for more than 1 channel to be listed in reg. Is that intent? If not, drop 'items'. If so and there a valid range, then add minItems/maxItems.

Quoted 44 lines
Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml new file +44 -0
+
+      diff-channels:
+        description: |
+          Besides the analog inputs available, internal inputs can be used.
+          16: Internal temperature sensor.
+          17: AVSS
+          18: Internal reference
+          19: DGND
+          20: (AVDD − AVSS)/6+
+          21: (AVDD − AVSS)/6-
+          22: (IOVDD − DGND)/6+
+          23: (IOVDD − DGND)/6-
+          24: (ALDO − AVSS)/6+
+          25: (ALDO − AVSS)/6-
+          26: (DLDO − DGND)/6+
+          27: (DLDO − DGND)/6-
+          28: V_MV_P
+          29: V_MV_M
+        items:
+          minimum: 0
+          maximum: 29
+
+      adi,reference-select:
+        description: |
+          Select the reference source to use when converting on the
+          specific channel. Valid values are:
+          0: REFIN1(+)/REFIN1(−)
+          1: REFIN2(+)/REFIN2(−)
+          2: REFOUT/AVSS (Internal reference)
+          3: AVDD/AVSS
+          If not specified, REFIN1 is used.
+        $ref: /schemas/types.yaml#/definitions/uint32
+        enum: [0, 1, 2, 3]
+        default: 0
+
+      adi,excitation-pin-0:
+        description: |
+          Analog input to apply excitation current to while the channel
+          is active.
+        $ref: /schemas/types.yaml#/definitions/uint32
+        minimum: 0
+        maximum: 15
+        default: 0
+

Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml line 161

Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml new file +10 -0
+      adi,excitation-pin-1:
+        description: |
+          Analog input to apply excitation current to while this channel
+          is active.
+        $ref: /schemas/types.yaml#/definitions/uint32
+        minimum: 0
+        maximum: 15
+        default: 0
+
+      adi,excitation-current-0-nanoamps:

Prior property was 'adi,excitation-current-nanoamp'. Please match '-nanoamp'.

Quoted 105 lines
Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml new file +101 -0
+        description: |
+          Excitation current in nanoamps to be applied to pin specified in
+          adi,excitation-pin-0 while this channel is active.
+        $ref: /schemas/types.yaml#/definitions/uint32
+        enum: [0, 100, 10000, 20000, 50000, 100000, 150000, 200000]
+        default: 0
+
+      adi,excitation-current-1-nanoamps:
+        description: |
+          Excitation current in nanoamps to be applied to pin specified in
+          adi,excitation-pin-1 while this channel is active.
+        $ref: /schemas/types.yaml#/definitions/uint32
+        enum: [0, 100, 10000, 20000, 50000, 100000, 150000, 200000]
+        default: 0
+
+      adi,burnout-current-nanoamps:
+        description: |
+          Burnout current in nanoamps to be applied for this channel.
+        $ref: /schemas/types.yaml#/definitions/uint32
+        enum: [0, 500, 2000, 4000]
+        default: 0
+
+      adi,buffered-positive:
+        description: Enable buffered mode for positive input.
+        type: boolean
+
+      adi,buffered-negative:
+        description: Enable buffered mode for negative input.
+        type: boolean
+
+    required:
+      - reg
+      - diff-channels
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+
+    spi {
+      #address-cells = <1>;
+      #size-cells = <0>;
+
+      adc@0 {
+        compatible = "adi,ad4130";
+        reg = <0>;
+
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        spi-max-frequency = <5000000>;
+        interrupts = <27 IRQ_TYPE_EDGE_FALLING>;
+        interrupt-parent = <&gpio>;
+
+        channel@0 {
+          reg = <0>;
+
+          adi,reference-select = <2>;
+
+          /* AIN8, AIN9 */
+          diff-channels = <8 9>;
+        };
+
+        channel@1 {
+          reg = <1>;
+
+          adi,reference-select = <2>;
+
+          /* AIN10, AIN11 */
+          diff-channels = <10 11>;
+        };
+
+        channel@2 {
+          reg = <2>;
+
+          adi,reference-select = <2>;
+
+          /* Temperature Sensor, DGND */
+          diff-channels = <16 19>;
+        };
+
+        channel@3 {
+          reg = <3>;
+
+          adi,reference-select = <2>;
+
+          /* Internal reference, DGND */
+          diff-channels = <18 19>;
+        };
+
+        channel@4 {
+          reg = <4>;
+
+          adi,reference-select = <2>;
+
+          /* DGND, DGND */
+          diff-channels = <19 19>;
+        };
+      };
+    };
Signature · 1 line
2.36.1
#3 lore raw

Re: [PATCH v4 1/2] dt-bindings: iio: adc: add AD4130

Quoted 56 lines · Rob Herring
Quoted 55 lines · Cosmin Tanislav

AD4130-8 is an ultra-low power, high precision, measurement solution for low bandwidth battery operated applications.

The fully integrated AFE (Analog Front-End) includes a multiplexer for up to 16 single-ended or 8 differential inputs, PGA (Programmable Gain Amplifier), 24-bit Sigma-Delta ADC, on-chip reference and oscillator, selectable filter options, smart sequencer, sensor biasing and excitation options, diagnostics, and a FIFO buffer.

---
  .../bindings/iio/adc/adi,ad4130.yaml          | 262 ++++++++++++++++++
  1 file changed, 262 insertions(+)

create mode 100644 Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml

Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml new file +34 -0
new file mode 100644
index 000000000000..c06b8a96d31e
@@ -0,0 +1,262 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+# Copyright 2022 Analog Devices Inc.
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/adc/adi,ad4130.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Analog Devices AD4130 ADC device driver
+
+maintainers:
+  - Cosmin Tanislav <[email protected]>
+
+description: |
+  Bindings for the Analog Devices AD4130 ADC. Datasheet can be found here:
+    https://www.analog.com/media/en/technical-documentation/data-sheets/AD4130-8.pdf
+
+properties:
+  compatible:
+    enum:
+      - adi,ad4130
+
+  reg:
+    maxItems: 1
+
+  clocks:
+    maxItems: 1
+    description: phandle to the master clock (mclk)
+
+  clock-names:
+    items:
+      - const: mclk
+
+  interrupts:
+    maxItems: 1
Quoted 8 lines

Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml line 40

Quoted 6 lines
Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml new file +6 -0
+
+  interrupt-names:
+    description: |
+      Specify which interrupt pin should be configured as Data Ready / FIFO
+      interrupt.
+      Default if not supplied is int.

default: int

Violates string array schema. I'm pretty sure I've said this before.

Quoted 242 lines
Quoted 25 lines
Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml new file +24 -0
+    enum:
+      - int
+      - clk
+      - p2
+      - dout
+
+  '#address-cells':
+    const: 1
+
+  '#size-cells':
+    const: 0
+
+  refin1-supply:
+    description: refin1 supply. Can be used as reference for conversion.
+
+  refin2-supply:
+    description: refin2 supply. Can be used as reference for conversion.
+
+  avdd-supply:
+    description: AVDD voltage supply. Can be used as reference for conversion.
+
+  iovdd-supply:
+    description: IOVDD voltage supply. Used for the chip interface.
+

Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml line 74

Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml new file +10 -0
+  spi-max-frequency:
+    maximum: 5000000
+
+  adi,int-clk-out:
+    description: Specify if the internal clock should be exposed on the CLK pin.
+    type: boolean
+
+  adi,ext-clk-freq:
+    description: Specify the frequency of the external clock.
+    $ref: /schemas/types.yaml#/definitions/uint32

-hz suffix and drop the type.

Quoted 19 lines
Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml new file +19 -0
+    enum: [76800, 153600]
+    default: 76800
+
+  adi,bipolar:
+    description: Specify if the device should be used in bipolar mode.
+    type: boolean
+
+  adi,vbias-pins:
+    description: Analog inputs to apply a voltage bias of (AVDD − AVSS) / 2 to.
+    $ref: /schemas/types.yaml#/definitions/uint32-array
+    maxItems: 16
+    items:
+      minimum: 0
+      maximum: 15
+
+required:
+  - compatible
+  - reg
+  - interrupts

Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml line 103

Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml new file +10 -0
+
+patternProperties:
+  "^channel@([0-9]|1[0-5])$":
+    type: object
+    $ref: adc.yaml
+    unevaluatedProperties: false
+
+    properties:
+      reg:
+        description: |

Don't need '|'.

Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml line 107

Quoted 4 lines
Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml new file +4 -0
+          The channel number.
+        items:
+          minimum: 0
+          maximum: 15

This allows for more than 1 channel to be listed in reg. Is that intent? If not, drop 'items'. If so and there a valid range, then add minItems/maxItems.

Quoted 44 lines
Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml new file +44 -0
+
+      diff-channels:
+        description: |
+          Besides the analog inputs available, internal inputs can be used.
+          16: Internal temperature sensor.
+          17: AVSS
+          18: Internal reference
+          19: DGND
+          20: (AVDD − AVSS)/6+
+          21: (AVDD − AVSS)/6-
+          22: (IOVDD − DGND)/6+
+          23: (IOVDD − DGND)/6-
+          24: (ALDO − AVSS)/6+
+          25: (ALDO − AVSS)/6-
+          26: (DLDO − DGND)/6+
+          27: (DLDO − DGND)/6-
+          28: V_MV_P
+          29: V_MV_M
+        items:
+          minimum: 0
+          maximum: 29
+
+      adi,reference-select:
+        description: |
+          Select the reference source to use when converting on the
+          specific channel. Valid values are:
+          0: REFIN1(+)/REFIN1(−)
+          1: REFIN2(+)/REFIN2(−)
+          2: REFOUT/AVSS (Internal reference)
+          3: AVDD/AVSS
+          If not specified, REFIN1 is used.
+        $ref: /schemas/types.yaml#/definitions/uint32
+        enum: [0, 1, 2, 3]
+        default: 0
+
+      adi,excitation-pin-0:
+        description: |
+          Analog input to apply excitation current to while the channel
+          is active.
+        $ref: /schemas/types.yaml#/definitions/uint32
+        minimum: 0
+        maximum: 15
+        default: 0
+

Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml line 161

Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml new file +10 -0
+      adi,excitation-pin-1:
+        description: |
+          Analog input to apply excitation current to while this channel
+          is active.
+        $ref: /schemas/types.yaml#/definitions/uint32
+        minimum: 0
+        maximum: 15
+        default: 0
+
+      adi,excitation-current-0-nanoamps:

Prior property was 'adi,excitation-current-nanoamp'. Please match '-nanoamp'.

Quoted 105 lines
Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml new file +101 -0
+        description: |
+          Excitation current in nanoamps to be applied to pin specified in
+          adi,excitation-pin-0 while this channel is active.
+        $ref: /schemas/types.yaml#/definitions/uint32
+        enum: [0, 100, 10000, 20000, 50000, 100000, 150000, 200000]
+        default: 0
+
+      adi,excitation-current-1-nanoamps:
+        description: |
+          Excitation current in nanoamps to be applied to pin specified in
+          adi,excitation-pin-1 while this channel is active.
+        $ref: /schemas/types.yaml#/definitions/uint32
+        enum: [0, 100, 10000, 20000, 50000, 100000, 150000, 200000]
+        default: 0
+
+      adi,burnout-current-nanoamps:
+        description: |
+          Burnout current in nanoamps to be applied for this channel.
+        $ref: /schemas/types.yaml#/definitions/uint32
+        enum: [0, 500, 2000, 4000]
+        default: 0
+
+      adi,buffered-positive:
+        description: Enable buffered mode for positive input.
+        type: boolean
+
+      adi,buffered-negative:
+        description: Enable buffered mode for negative input.
+        type: boolean
+
+    required:
+      - reg
+      - diff-channels
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+
+    spi {
+      #address-cells = <1>;
+      #size-cells = <0>;
+
+      adc@0 {
+        compatible = "adi,ad4130";
+        reg = <0>;
+
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        spi-max-frequency = <5000000>;
+        interrupts = <27 IRQ_TYPE_EDGE_FALLING>;
+        interrupt-parent = <&gpio>;
+
+        channel@0 {
+          reg = <0>;
+
+          adi,reference-select = <2>;
+
+          /* AIN8, AIN9 */
+          diff-channels = <8 9>;
+        };
+
+        channel@1 {
+          reg = <1>;
+
+          adi,reference-select = <2>;
+
+          /* AIN10, AIN11 */
+          diff-channels = <10 11>;
+        };
+
+        channel@2 {
+          reg = <2>;
+
+          adi,reference-select = <2>;
+
+          /* Temperature Sensor, DGND */
+          diff-channels = <16 19>;
+        };
+
+        channel@3 {
+          reg = <3>;
+
+          adi,reference-select = <2>;
+
+          /* Internal reference, DGND */
+          diff-channels = <18 19>;
+        };
+
+        channel@4 {
+          reg = <4>;
+
+          adi,reference-select = <2>;
+
+          /* DGND, DGND */
+          diff-channels = <19 19>;
+        };
+      };
+    };
Signature · 1 line
2.36.1
#4 lore raw

PATCH v4 2/2 iio: adc: ad4130: add AD4130 driver

In reply to Cosmin Tanislav

AD4130-8 is an ultra-low power, high precision, measurement solution for low bandwidth battery operated applications.

The fully integrated AFE (Analog Front-End) includes a multiplexer for up to 16 single-ended or 8 differential inputs, PGA (Programmable Gain Amplifier), 24-bit Sigma-Delta ADC, on-chip reference and oscillator, selectable filter options, smart sequencer, sensor biasing and excitation options, diagnostics, and a FIFO buffer.

---
 .../ABI/testing/sysfs-bus-iio-adc-ad4130      |   36 +
 MAINTAINERS                                   |    8 +
 drivers/iio/adc/Kconfig                       |   13 +
 drivers/iio/adc/Makefile                      |    1 +
 drivers/iio/adc/ad4130.c                      | 2015 +++++++++++++++++
 5 files changed, 2073 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-adc-ad4130
 create mode 100644 drivers/iio/adc/ad4130.c
Documentation/ABI/testing/sysfs-bus-iio-adc-ad4130 new file +36 -0
new file mode 100644
index 000000000000..09857dbcac32
@@ -0,0 +1,36 @@
+What:		/sys/bus/iio/devices/iio:deviceX/in_voltage-voltage_filter_mode_available
+KernelVersion:	5.18
+Contact:	[email protected]
+Description:
+		Reading returns a list with the possible filter modes.
+		"sinc4"       - Sinc 4. Excellent noise performance. Long 1st
+				conversion time. No natural 50/60Hz rejection.
+		"sinc4+sinc1" - Sinc4 + averaging by 8. Low 1st conversion time.
+		"sinc3"	      - Sinc3. Moderate 1st conversion time. Good noise
+				performance.
+		"sinc3+rej60" - Sinc3 + 60Hz rejection. At a sampling frequency
+				of 50Hz, achieves simultaneous 50Hz and 60Hz
+				rejection.
+		"sinc3+sinc1" - Sinc3 + averaging by 8. Low 1st conversion time.
+				Best used with a sampling frequency of at least
+				216.19Hz.
+		"sinc3+pf1"   - Sinc3 + Post Filter 1.
+				53dB rejection @ 50Hz, 58dB rejection @ 60Hz.
+		"sinc3+pf2"   - Sinc3 + Post Filter 2.
+				70dB rejection @ 50Hz, 70dB rejection @ 60Hz.
+		"sinc3+pf3"   - Sinc3 + Post Filter 3.
+				99dB rejection @ 50Hz, 103dB rejection @ 60Hz.
+		"sinc3+pf4"   - Sinc3 + Post Filter 4.
+				103dB rejection @ 50Hz, 109dB rejection @ 60Hz.
+
+What:		/sys/bus/iio/devices/iio:deviceX/in_voltageY-voltageZ_filter_mode
+KernelVersion:	5.18
+Contact:	[email protected]
+Description:
+		Set the filter mode of the differential channel. When the filter
+		mode changes, the in_voltageY-voltageZ_sampling_frequency and
+		in_voltageY-voltageZ_sampling_frequency_available attributes
+		might also change to accommodate the new filter mode.
+		If the current sampling frequency is out of range for the new
+		filter mode, the sampling frequency will be changed to the
+		closest valid one.
MAINTAINERS +8 -0
index c726bdc2c9b9..94cd70220c30 100644
@@ -1074,6 +1074,14 @@ T:	git git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net.git
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next.git
 F:	drivers/net/amt.c
 
+ANALOG DEVICES INC AD4130 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/adc/adi,ad4130.yaml
+F:	drivers/iio/adc/ad4130.c
+
 ANALOG DEVICES INC AD7192 DRIVER
 M:	Alexandru Tachici <[email protected]>
 L:	[email protected]
drivers/iio/adc/Kconfig +13 -0
index b9e913e25a5d..0276f5df10e2 100644
@@ -21,6 +21,19 @@ config AD_SIGMA_DELTA
 	select IIO_BUFFER
 	select IIO_TRIGGERED_BUFFER
 
+config AD4130
+	tristate "Analog Device AD4130 ADC Driver"
+	depends on SPI
+	select IIO_BUFFER
+	select IIO_KFIFO_BUF
+	select REGMAP_SPI
+	help
+	  Say yes here to build support for Analog Devices AD4130-8 SPI analog
+	  to digital converters (ADC).
+
+	  To compile this driver as a module, choose M here: the module will be
+	  called ad4130.
+
 config AD7091R5
 	tristate "Analog Devices AD7091R5 ADC Driver"
 	depends on I2C
drivers/iio/adc/Makefile +1 -0
index 39d806f6d457..c3aa7e4eca2f 100644
@@ -6,6 +6,7 @@
 # When adding new entries keep the list in alphabetical order
 obj-$(CONFIG_AB8500_GPADC) += ab8500-gpadc.o
 obj-$(CONFIG_AD_SIGMA_DELTA) += ad_sigma_delta.o
+obj-$(CONFIG_AD4130) += ad4130.o
 obj-$(CONFIG_AD7091R5) += ad7091r5.o ad7091r-base.o
 obj-$(CONFIG_AD7124) += ad7124.o
 obj-$(CONFIG_AD7192) += ad7192.o
drivers/iio/adc/ad4130.c new file +2015 -0

2,015 lines not on this page · raw message

Signature · 1 line
2.36.1
#5 lore raw

Re: [PATCH v4 2/2] iio: adc: ad4130: add AD4130 driver

Quoted 9 lines · Cosmin Tanislav

AD4130-8 is an ultra-low power, high precision, measurement solution for low bandwidth battery operated applications.

The fully integrated AFE (Analog Front-End) includes a multiplexer for up to 16 single-ended or 8 differential inputs, PGA (Programmable Gain Amplifier), 24-bit Sigma-Delta ADC, on-chip reference and oscillator, selectable filter options, smart sequencer, sensor biasing and excitation options, diagnostics, and a FIFO buffer.

I believe we may gain a few LoCs by slightly bending the rule of 80. Also see below.

...

drivers/iio/adc/ad4130.c line 126

Quoted 12 lines
drivers/iio/adc/ad4130.c new file +11 -0
+static const unsigned int ad4130_reg_size[] = {
+       [AD4130_STATUS_REG] = 1,
+       [AD4130_ADC_CONTROL_REG] = 2,
+       [AD4130_DATA_REG] = 3,
+       [AD4130_IO_CONTROL_REG] = 2,
+       [AD4130_VBIAS_REG] = 2,
+       [AD4130_ID_REG] = 1,
+       [AD4130_ERROR_REG] = 2,
+       [AD4130_ERROR_EN_REG] = 2,
 
+       [AD4130_CHANNEL_X_REG(0) ...
+        AD4130_CHANNEL_X_REG(AD4130_MAX_CHANNELS)] = 3,

One line?

drivers/iio/adc/ad4130.c line 128

Quoted 2 lines
drivers/iio/adc/ad4130.c new file +2 -0
+       [AD4130_CONFIG_X_REG(0) ...
+        AD4130_CONFIG_X_REG(AD4130_MAX_SETUPS)] = 2,

Ditto.

drivers/iio/adc/ad4130.c line 130

Quoted 2 lines
drivers/iio/adc/ad4130.c new file +2 -0
+       [AD4130_FILTER_X_REG(0) ...
+        AD4130_FILTER_X_REG(AD4130_MAX_SETUPS)] = 3,

Ditto.

Quoted 3 lines
drivers/iio/adc/ad4130.c new file +3 -0
+       [AD4130_FIFO_CONTROL_REG] = 3,
+       [AD4130_FIFO_STATUS_REG] = 1,
+};

...

drivers/iio/adc/ad4130.c line 171

Quoted 10 lines
drivers/iio/adc/ad4130.c new file +10 -0
+enum ad4130_iout {
+       AD4130_IOUT_OFF,
+       AD4130_IOUT_10000NA,
+       AD4130_IOUT_20000NA,
+       AD4130_IOUT_50000NA,
+       AD4130_IOUT_100000NA,
+       AD4130_IOUT_150000NA,
+       AD4130_IOUT_200000NA,
+       AD4130_IOUT_100NA,
+       AD4130_IOUT_MAX,

No comma for MAX if it's not a hardware (datasheet) name.

Quoted 1 line

+};

...

drivers/iio/adc/ad4130.c line 228

Quoted 10 lines
drivers/iio/adc/ad4130.c new file +9 -0
+struct ad4130_setup_info {
+       unsigned int                    iout0_val;
+       unsigned int                    iout1_val;
+       unsigned int                    burnout;
+       unsigned int                    pga;
+       unsigned int                    fs;
 
+       bool                            ref_bufp;
+       bool                            ref_bufm;
+       u32                             ref_sel;

Moving this member before booleans might save a few bytes (depending on how enum will be optimized by the compiler).

Quoted 2 lines
(quoted diff) +2 -0
+       enum ad4130_filter_mode         filter_mode;
+};

...

drivers/iio/adc/ad4130.c line 258

Quoted 3 lines
drivers/iio/adc/ad4130.c new file +3 -0
+struct ad4130_state {
+       struct spi_device               *spi;
+       struct regmap                   *regmap;

Put the one that is mostly used in the code, the first. In such cases the pointer arithmetics will be no-op at compile time.

Quoted 15 lines
drivers/iio/adc/ad4130.c new file +15 -0
+       struct spi_message      fifo_msg;
+       struct spi_transfer     fifo_xfer[2];
+
+       /*
+        * DMA (thus cache coherency maintenance) requires the
+        * transfer buffers to live in their own cache lines.
+        */
+       u8                      reset_buf[AD4130_RESET_BUF_SIZE] __aligned(IIO_DMA_MINALIGN);
+       u8                      reg_write_tx_buf[4];
+       u8                      reg_read_tx_buf[1];
+       u8                      reg_read_rx_buf[3];
+       u8                      fifo_tx_buf[2];
+       u8                      fifo_rx_buf[AD4130_FIFO_SIZE *
+                                           AD4130_FIFO_MAX_SAMPLE_SIZE];
+};

...

drivers/iio/adc/ad4130.c line 390

Quoted 3 lines
drivers/iio/adc/ad4130.c new file +3 -0
+       *size = ad4130_reg_size[reg];
+       if (!*size)
+               return -EINVAL;

Is this check necessary?

...

Quoted 4 lines
drivers/iio/adc/ad4130.c new file +4 -0
+       switch (size) {
+       case 3:
+               *val = get_unaligned_be24(st->reg_read_rx_buf);
+               break;

drivers/iio/adc/ad4130.c line 479

Quoted 10 lines
drivers/iio/adc/ad4130.c new file +9 -0
+       case 2:
+               *val = get_unaligned_be16(st->reg_read_rx_buf);
+               break;
+       case 1:
+               *val = st->reg_read_rx_buf[0];
+               break;
+       default:
 
+               ret = -EINVAL;
+               break;

Direct return.

Quoted 3 lines
drivers/iio/adc/ad4130.c new file +3 -0
+       }
+
+       return 0;

...

drivers/iio/adc/ad4130.c line 499

Quoted 5 lines
drivers/iio/adc/ad4130.c new file +5 -0
+static void ad4130_gpio_set(struct gpio_chip *gc, unsigned int offset,
+                           int value)
+{
+       struct ad4130_state *st = gpiochip_get_data(gc);
+       unsigned int real_offset = st->gpio_offsets[offset];

Can't you use valid_mask instead of this additional array? In such a case the real offset can be got by the number of the set bit, no?

Quoted 6 lines
drivers/iio/adc/ad4130.c new file +6 -0
+       unsigned int mask = FIELD_PREP(AD4130_IO_CONTROL_GPIO_DATA_MASK,
+                                      BIT(real_offset));
+
+       regmap_update_bits(st->regmap, AD4130_IO_CONTROL_REG, mask,
+                          value ? mask : 0);
+}

...

drivers/iio/adc/ad4130.c line 584

Quoted 7 lines
drivers/iio/adc/ad4130.c new file +6 -0
+       for (i = 0; i < AD4130_MAX_SETUPS; i++) {
+               struct ad4130_slot_info *slot_info = &st->slots_info[i];
+
+               /* Immediately accept a matching setup info. */
 
+               if (!memcmp(target_setup_info, &slot_info->setup,
+                           sizeof(*target_setup_info))) {

Instead, you may use crc32 and save it, the matching will be much faster.

The example, where it's done for the same purposes (to compare later)
https://elixir.bootlin.com/linux/latest/source/drivers/acpi/scan.c#L659

drivers/iio/adc/ad4130.c line 593

Quoted 9 lines
drivers/iio/adc/ad4130.c new file +9 -0
+                       *slot = i;
+                       return 0;
+               }
+
+               /* Ignore all setups which are used by enabled channels. */
+               if (slot_info->enabled_channels)
+                       continue;
+
+               /* Find the least used slot. */

Have you considered to use
https://elixir.bootlin.com/linux/latest/source/include/linux/list_lru.h
?

Quoted 4 lines
drivers/iio/adc/ad4130.c new file +4 -0
+               if (*slot == AD4130_INVALID_SLOT ||
+                   slot_info->channels < st->slots_info[*slot].channels)
+                       *slot = i;
+       }

...

drivers/iio/adc/ad4130.c line 695

Quoted 5 lines
drivers/iio/adc/ad4130.c new file +5 -0
+       /*
+        * The following cases need to be handled.
+        *
+        * 1. Enabled and linked channel with setup changes:
+        *    - Find slot. If not possible, return error.

a slot

Review

Quoted 2 lines
(quoted diff) +2 -0
+        *    - Unlink channel from current slot.
+        *    - If slot has channels linked to it, unlink all channels, and

the slot

drivers/iio/adc/ad4130.c line 702

Quoted 5 lines
drivers/iio/adc/ad4130.c new file +5 -0
+        *      write the new setup to it.
+        *    - Link channel to new slot.
+        *
+        * 2. Soon to be enabled and unlinked channel:
+        *    - Find slot. If not possible, return error.

a slot

Quoted 1 line

+ * - If slot has channels linked to it, unlink all channels, and

the slot

Review

Quoted 2 lines
(quoted diff) +2 -0
+        *      write the new setup to it.
+        *    - Link channel to slot.

the slot

Quoted 8 lines
drivers/iio/adc/ad4130.c new file +8 -0
+        *
+        * 3. Disabled and linked channel with setup changes:
+        *    - Unlink channel from current slot.
+        *
+        * 4. Soon to be enabled and linked channel:
+        * 5. Disabled and unlinked channel with setup changes:
+        *    - Do nothing.
+        */

...

drivers/iio/adc/ad4130.c line 724

Quoted 6 lines
drivers/iio/adc/ad4130.c new file +6 -0
+       if (!on_enable && !chan_info->enabled) {
+               if (chan_info->slot != AD4130_INVALID_SLOT)
+                       /* Case 3 */
+                       ad4130_unlink_channel(st, channel);
+
+               /* Case 3 & 5 */

Cases

drivers/iio/adc/ad4130.c line 728

Quoted 4 lines
drivers/iio/adc/ad4130.c new file +4 -0
+               return 0;
+       }
+
+       /* Case 1 & 2 */

Cases

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+       const struct ad4130_filter_config *filter_config =
+               &ad4130_filter_configs[filter_mode];

One line? Or even a helper, since you are using this more than once.

...

drivers/iio/adc/ad4130.c line 823

Quoted 2 lines
drivers/iio/adc/ad4130.c new file +2 -0
+       dividend = filter_config->fs_max * filter_config->odr_div *
+                  ((u64)val * NANO + val2);

One line.

...

Quoted 1 line

+ *val = st->bipolar ? -(1 << (chan->scan_type.realbits - 1)) : 0;

BIT() ?

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+       /*
+        * When the chip enters FIFO mode, IRQ polarity is inversed.

inverted ?

Quoted 3 lines
drivers/iio/adc/ad4130.c new file +3 -0
+        * See datasheet pages: 65, FIFO Watermark Interrupt section,
+        * and 71, Bit Descriptions for STATUS Register, RDYB.
+        */

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+       /*
+        * update_scan_mode is not called in the disable path, disable all

->update_scan_mode() ?

Quoted 2 lines
drivers/iio/adc/ad4130.c new file +2 -0
+        * channels here.
+        */

...

Quoted 11 lines
drivers/iio/adc/ad4130.c new file +11 -0
+       switch (ref_sel) {
+       case AD4130_REF_REFIN1:
+               ret = regulator_get_voltage(st->regulators[2].consumer);
+               break;
+       case AD4130_REF_REFIN2:
+               ret = regulator_get_voltage(st->regulators[3].consumer);
+               break;
+       case AD4130_REF_AVDD_AVSS:
+               ret = regulator_get_voltage(st->regulators[0].consumer);
+               break;
+       case AD4130_REF_REFOUT_AVSS:

drivers/iio/adc/ad4130.c line 1399

Quoted 10 lines
drivers/iio/adc/ad4130.c new file +9 -0
+               ret = st->int_ref_uv;
+               break;
+       default:
+               ret = -EINVAL;
+               break;
+       }
 
+       if (ret < 0)
+               return dev_err_probe(dev, ret, "Cannot use reference %u\n",
+                                    ref_sel);

Can it be moved to the caller where it would cleaner to use, I think? As a good side effect the all above will be shortened to just return directly.

Quoted 1 line

+ return ret;

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+       fwnode_property_read_u32(child, "adi,excitation-current-0-nanoamps",
+                                &current_na);

One line?

...

drivers/iio/adc/ad4130.c line 1420

Quoted 3 lines
drivers/iio/adc/ad4130.c new file +3 -0
+               return dev_err_probe(dev, ret,
+                                    "Invalid excitation current %unA\n",
+                                    current_na);

Ditto or two lines?

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+       fwnode_property_read_u32(child, "adi,excitation-current-1-nanoamps",
+                                &current_na);

Ditto. Moreover you can shorten the name of temporary variable to something like tmp since it makes no sense to have a custom name for many options.

...

drivers/iio/adc/ad4130.c line 1420

Quoted 3 lines
drivers/iio/adc/ad4130.c new file +3 -0
+               return dev_err_probe(dev, ret,
+                                    "Invalid excitation current %unA\n",
+                                    current_na);

One line?

...

drivers/iio/adc/ad4130.c line 1436

Quoted 2 lines
drivers/iio/adc/ad4130.c new file +2 -0
+       fwnode_property_read_u32(child, "adi,burnout-current-nanoamps",
+                                &current_na);

Ditto.

...

drivers/iio/adc/ad4130.c line 1442

Quoted 3 lines
drivers/iio/adc/ad4130.c new file +3 -0
+               return dev_err_probe(dev, ret,
+                                    "Invalid burnout current %unA\n",
+                                    current_na);

Ditto.

...

drivers/iio/adc/ad4130.c line 1450

Quoted 2 lines
drivers/iio/adc/ad4130.c new file +2 -0
+       fwnode_property_read_u32(child, "adi,reference-select",
+                                &setup_info->ref_sel);

Ditto.

...

drivers/iio/adc/ad4130.c line 1454

Quoted 3 lines
drivers/iio/adc/ad4130.c new file +3 -0
+               return dev_err_probe(dev, -EINVAL,
+                                    "Invalid reference selected %u\n",
+                                    setup_info->ref_sel);

Ditto.

...

drivers/iio/adc/ad4130.c line 1463

Quoted 5 lines
drivers/iio/adc/ad4130.c new file +5 -0
+       ret = ad4130_get_ref_voltage(st, setup_info->ref_sel);
+       if (ret < 0)
+               return ret;
+
+       return 0;

In all cases what does the positive return value mean?
If there is no meaning, drop all these ' < 0' parts and esp. here use simply

return ad4130_get_ref_voltage(...);

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+               return dev_err_probe(dev, -EINVAL,
+                                    "Invalid diffreential channel %u\n", pin);

One line?

...

drivers/iio/adc/ad4130.c line 1480

Quoted 3 lines
drivers/iio/adc/ad4130.c new file +3 -0
+               return dev_err_probe(dev, -EINVAL,
+                                    "Pin %u already used with fn %u\n", pin,
+                                    st->pins_fn[pin]);

2 lines?

...

drivers/iio/adc/ad4130.c line 1491

Quoted 6 lines
drivers/iio/adc/ad4130.c new file +5 -0
+static int ad4130_validate_diff_channels(struct ad4130_state *st,
+                                        u32 *pins, unsigned int len)
+{
+       unsigned int i;
 
+       int ret = 0;

Useless assignment.

drivers/iio/adc/ad4130.c line 1496

Quoted 6 lines
drivers/iio/adc/ad4130.c new file +5 -0
+
+       for (i = 0; i < len; i++) {
+               ret = ad4130_validate_diff_channel(st, pins[i]);
+               if (ret)
 
+                       break;

Useless. Return immediately.

Quoted 4 lines
drivers/iio/adc/ad4130.c new file +4 -0
+       }
+
+       return ret;
+}

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+               return dev_err_probe(dev, -EINVAL,
+                                    "Invalid excitation pin %u\n", pin);

One line?

...

drivers/iio/adc/ad4130.c line 1480

Quoted 3 lines
drivers/iio/adc/ad4130.c new file +3 -0
+               return dev_err_probe(dev, -EINVAL,
+                                    "Pin %u already used with fn %u\n", pin,
+                                    st->pins_fn[pin]);

One or two lines?

...

drivers/iio/adc/ad4130.c line 1526

Quoted 2 lines
drivers/iio/adc/ad4130.c new file +2 -0
+               return dev_err_probe(dev, -EINVAL, "Invalid vbias pin %u\n",
+                                    pin);

Ditto.

...

drivers/iio/adc/ad4130.c line 1480

Quoted 3 lines
drivers/iio/adc/ad4130.c new file +3 -0
+               return dev_err_probe(dev, -EINVAL,
+                                    "Pin %u already used with fn %u\n", pin,
+                                    st->pins_fn[pin]);

Ditto.

...

drivers/iio/adc/ad4130.c line 1550

Quoted 10 lines
drivers/iio/adc/ad4130.c new file +10 -0
+       unsigned int i;
+       int ret = 0;
+
+       for (i = 0; i < st->num_vbias_pins; i++) {
+               ret = ad4130_validate_vbias_pin(st, pins[i]);
+               if (ret)
+                       break;
+       }
+
+       return ret;

As per above comments.

...

drivers/iio/adc/ad4130.c line 1581

Quoted 2 lines
drivers/iio/adc/ad4130.c new file +2 -0
+       ret = fwnode_property_read_u32_array(child, "diff-channels", pins,
+                                            ARRAY_SIZE(pins));

One line?

...

drivers/iio/adc/ad4130.c line 1597

Quoted 2 lines
drivers/iio/adc/ad4130.c new file +2 -0
+       fwnode_property_read_u32(child, "adi,excitation-pin-0",
+                                &chan_info->iout0);

Ditto.

...

drivers/iio/adc/ad4130.c line 1605

Quoted 2 lines
drivers/iio/adc/ad4130.c new file +2 -0
+       fwnode_property_read_u32(child, "adi,excitation-pin-1",
+                                &chan_info->iout1);

Ditto.

...

Quoted 1 line

+ bool int_clk_out = false;

Why is this assignment needed?

...

drivers/iio/adc/ad4130.c line 1649

Quoted 2 lines
drivers/iio/adc/ad4130.c new file +2 -0
+               return dev_err_probe(dev, PTR_ERR(st->mclk),
+                                    "Failed to get mclk\n");

One line?

...

drivers/iio/adc/ad4130.c line 1664

Quoted 3 lines
drivers/iio/adc/ad4130.c new file +3 -0
+       if (st->int_pin_sel == AD4130_INT_PIN_DOUT)
+               return dev_err_probe(dev, -EINVAL,
+                                    "Cannot use DOUT as interrupt pin\n");

Ditto.

...

drivers/iio/adc/ad4130.c line 1672

Quoted 4 lines
drivers/iio/adc/ad4130.c new file +4 -0
+       int_clk_out = device_property_read_bool(dev, "adi,int-clk-out");
+       if (st->mclk && int_clk_out)
+               return dev_err_probe(dev, -EINVAL,
+                                    "Cannot expose internal clock\n");

Ditto.

...

drivers/iio/adc/ad4130.c line 1679

Quoted 6 lines
drivers/iio/adc/ad4130.c new file +5 -0
+       if (ext_clk_freq != AD4130_MCLK_FREQ_153_6KHZ &&
+           ext_clk_freq != AD4130_MCLK_FREQ_76_8KHZ)
 
+               return dev_err_probe(dev, -EINVAL,
+                                    "Invalid external clock frequency %u\n",
+                                    ext_clk_freq);

2 lines?

...

drivers/iio/adc/ad4130.c line 1694

Quoted 5 lines
drivers/iio/adc/ad4130.c new file +5 -0
+       if (st->int_pin_sel == AD4130_INT_PIN_CLK &&
+           st->mclk_sel != AD4130_MCLK_76_8KHZ)
+               return dev_err_probe(dev, -EINVAL,
+                                    "Invalid clock %u for interrupt pin %u\n",
+                                    st->mclk_sel, st->int_pin_sel);

2 lines?

...

drivers/iio/adc/ad4130.c line 1713

Quoted 3 lines
drivers/iio/adc/ad4130.c new file +3 -0
+               if (ret > AD4130_MAX_ANALOG_PINS)
+                       return dev_err_probe(dev, -EINVAL,
+                                            "Too many vbias pins %u\n", ret);

One line?

...

drivers/iio/adc/ad4130.c line 1722

Quoted 3 lines
drivers/iio/adc/ad4130.c new file +3 -0
+               if (ret)
+                       return dev_err_probe(dev, ret,
+                                            "Failed to read vbias pins\n");

Ditto.

drivers/iio/adc/ad4130.c line 1763

Quoted 5 lines
drivers/iio/adc/ad4130.c new file +4 -0
+               for (j = 0; j < AD4130_MAX_PGA; j++) {
+                       unsigned int pow = resolution + j - st->bipolar;
 
+                       unsigned int nv = div_u64((((u64)ret * NANO) >>
+                                                  pow), MILLI);

It will be much better if you make it on one line. Moreover, it seems it's ivariamt to the loop, why it's inside the loop?

Quoted 3 lines
drivers/iio/adc/ad4130.c new file +3 -0
+                       st->scale_tbls[i][j][0] = 0;
+                       st->scale_tbls[i][j][1] = nv;
+               }

...

Quoted 1 line

+ unsigned long rate = AD4130_MCLK_FREQ_76_8KHZ;

Please move it closer to the alternative assignments so all of them will be closer to each other for better code reading and understanding. It also applies to ither similar places in the code.

...

drivers/iio/adc/ad4130.c line 1798

Quoted 2 lines
drivers/iio/adc/ad4130.c new file +2 -0
+       ret = devm_add_action_or_reset(dev, ad4130_clk_disable_unprepare,
+                                      st->mclk);

One line?

...

drivers/iio/adc/ad4130.c line 1821

Quoted 3 lines
drivers/iio/adc/ad4130.c new file +3 -0
+       val = FIELD_PREP(AD4130_IO_CONTROL_INT_PIN_SEL_MASK, st->int_pin_sel);
+       for (i = 0; i < st->num_gpios; i++)
+               val |= BIT(st->gpio_offsets[i]);

valid_mask reinvention?

...

drivers/iio/adc/ad4130.c line 1928

Quoted 2 lines
drivers/iio/adc/ad4130.c new file +2 -0
+       ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(st->regulators),
+                                     st->regulators);

One line?

drivers/iio/adc/ad4130.c line 1931

Quoted 3 lines
drivers/iio/adc/ad4130.c new file +3 -0
+       if (ret)
+               return dev_err_probe(dev, ret,
+                                    "Failed to get regulators\n");

Ditto.

drivers/iio/adc/ad4130.c line 1936

Quoted 4 lines
drivers/iio/adc/ad4130.c new file +4 -0
+       ret = regulator_bulk_enable(ARRAY_SIZE(st->regulators), st->regulators);
+       if (ret)
+               return dev_err_probe(dev, ret,
+                                    "Failed to enable regulators\n");

Ditto.

drivers/iio/adc/ad4130.c line 1941

Quoted 4 lines
drivers/iio/adc/ad4130.c new file +4 -0
+       ret = devm_add_action_or_reset(dev, ad4130_disable_regulators, st);
+       if (ret)
+               return dev_err_probe(dev, ret,
+                                    "Failed to add regulators disable action\n");

Ditto.

...

Why below magic is needed? Maybe one line comment?

drivers/iio/adc/ad4130.c line 1991

Quoted 7 lines
drivers/iio/adc/ad4130.c new file +7 -0
+       if (st->irq_trigger & IRQF_TRIGGER_RISING)
+               st->inv_irq_trigger = IRQF_TRIGGER_FALLING;
+       else if (st->irq_trigger & IRQF_TRIGGER_FALLING)
+               st->inv_irq_trigger = IRQF_TRIGGER_RISING;
+       else
+               return dev_err_probe(dev, -EINVAL, "Invalid irq flags: %u\n",
+                                    st->irq_trigger);

One line?

...

drivers/iio/adc/ad4130.c line 2000

Quoted 5 lines
drivers/iio/adc/ad4130.c new file +5 -0
+static const struct of_device_id ad4130_of_match[] = {
+       {
+               .compatible = "adi,ad4130",
+       },
+       { },

No comma for terminator entry.

Quoted 1 line

+};

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

Re: [PATCH v4 2/2] iio: adc: ad4130: add AD4130 driver

Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Cosmin Tanislav

AD4130-8 is an ultra-low power, high precision, measurement solution for low bandwidth battery operated applications.

Quoted 9 lines
Quoted 5 lines

The fully integrated AFE (Analog Front-End) includes a multiplexer for up to 16 single-ended or 8 differential inputs, PGA (Programmable Gain Amplifier), 24-bit Sigma-Delta ADC, on-chip reference and oscillator, selectable filter options, smart sequencer, sensor biasing and excitation options, diagnostics, and a FIFO buffer.

I believe we may gain a few LoCs by slightly bending the rule of 80. Also see below.

I'll only go over the 80 columns limit if Jonathan agrees to it.

Quoted 6 lines

drivers/iio/adc/ad4130.c line 390

Quoted 3 lines
drivers/iio/adc/ad4130.c new file +3 -0
+       *size = ad4130_reg_size[reg];
+       if (!*size)
+               return -EINVAL;

Is this check necessary?

Yes. I haven't described all registers in the table, and the registers can be accessed by the user via the debugfs_reg_access() method.

Quoted 9 lines

drivers/iio/adc/ad4130.c line 499

Quoted 5 lines
drivers/iio/adc/ad4130.c new file +5 -0
+static void ad4130_gpio_set(struct gpio_chip *gc, unsigned int offset,
+                           int value)
+{
+       struct ad4130_state *st = gpiochip_get_data(gc);
+       unsigned int real_offset = st->gpio_offsets[offset];

Can't you use valid_mask instead of this additional array? In such a case the real offset can be got by the number of the set bit, no?

I'm not sure what you mean by this? If valid_mask will prevent all GPIOs equivalent to the bits not set in the mask from being exposed, then yes, it could be useful. I wish I knew about it earlier since it's already the second driver in which I use this approach.

Quoted 5 lines
Quoted 4 lines
drivers/iio/adc/ad4130.c new file +4 -0
+       for (i = 0; i < AD4130_MAX_SETUPS; i++) {
+               struct ad4130_slot_info *slot_info = &st->slots_info[i];
+
+               /* Immediately accept a matching setup info. */
Quoted 8 lines

drivers/iio/adc/ad4130.c line 584

Quoted 2 lines
drivers/iio/adc/ad4130.c new file +2 -0
+               if (!memcmp(target_setup_info, &slot_info->setup,
+                           sizeof(*target_setup_info))) {

Instead, you may use crc32 and save it, the matching will be much faster.

The example, where it's done for the same purposes (to compare later)
https://elixir.bootlin.com/linux/latest/source/drivers/acpi/scan.c#L659

I think it's fine as it is. Most people won't use than many channels anyway.

Quoted 5 lines
Quoted 5 lines
drivers/iio/adc/ad4130.c new file +5 -0
+                       *slot = i;
+                       return 0;
+               }
+
+               /* Ignore all setups which are used by enabled channels. */
Quoted 9 lines

drivers/iio/adc/ad4130.c line 593

Quoted 4 lines
drivers/iio/adc/ad4130.c new file +4 -0
+               if (slot_info->enabled_channels)
+                       continue;
+
+               /* Find the least used slot. */

Have you considered to use
https://elixir.bootlin.com/linux/latest/source/include/linux/list_lru.h
?

No. And I don't think I intend to.

Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+       const struct ad4130_filter_config *filter_config =
+               &ad4130_filter_configs[filter_mode];

One line? Or even a helper, since you are using this more than once.

I don't think creating a helper would be helpful here. I can save like, one character. Or you meant a helper that also declares the filter_config variable? That would make the code even harder to read.

Quoted 18 lines
Quoted 17 lines
drivers/iio/adc/ad4130.c new file +17 -0
+       switch (ref_sel) {
+       case AD4130_REF_REFIN1:
+               ret = regulator_get_voltage(st->regulators[2].consumer);
+               break;
+       case AD4130_REF_REFIN2:
+               ret = regulator_get_voltage(st->regulators[3].consumer);
+               break;
+       case AD4130_REF_AVDD_AVSS:
+               ret = regulator_get_voltage(st->regulators[0].consumer);
+               break;
+       case AD4130_REF_REFOUT_AVSS:
+               ret = st->int_ref_uv;
+               break;
+       default:
+               ret = -EINVAL;
+               break;
+       }
Quoted 7 lines

drivers/iio/adc/ad4130.c line 1399

Quoted 3 lines
drivers/iio/adc/ad4130.c new file +3 -0
+       if (ret < 0)
+               return dev_err_probe(dev, ret, "Cannot use reference %u\n",
+                                    ref_sel);

Can it be moved to the caller where it would cleaner to use, I think? As a good side effect the all above will be shortened to just return directly.

I'm pretty sure I remember Jonathan suggested moving it inside the function.

Quoted 11 lines

drivers/iio/adc/ad4130.c line 1463

Quoted 5 lines
drivers/iio/adc/ad4130.c new file +5 -0
+       ret = ad4130_get_ref_voltage(st, setup_info->ref_sel);
+       if (ret < 0)
+               return ret;
+
+       return 0;

In all cases what does the positive return value mean?
If there is no meaning, drop all these ' < 0' parts and esp. here use simply

return ad4130_get_ref_voltage(...);

ad4130_get_ref_voltage() returns the voltage of the specified reference via its return value.

The voltage would be positive, while an error code would be negative. Same for ad4130_find_table_index() where < 0 is also used.

Quoted 9 lines

drivers/iio/adc/ad4130.c line 1763

Quoted 5 lines
drivers/iio/adc/ad4130.c new file +4 -0
+               for (j = 0; j < AD4130_MAX_PGA; j++) {
+                       unsigned int pow = resolution + j - st->bipolar;
 
+                       unsigned int nv = div_u64((((u64)ret * NANO) >>
+                                                  pow), MILLI);

It will be much better if you make it on one line. Moreover, it seems it's ivariamt to the loop, why it's inside the loop?

pow depends on j, nv depends on pow.

maintainer
#7 lore raw

Re: [PATCH v4 2/2] iio: adc: ad4130: add AD4130 driver

Quoted 12 lines · On Wed, 8 Jun 2022 23:11:51 +0300 Cosmin Tanislav <[email protected]> wrote:
Quoted 11 lines · Andy Shevchenko
Quoted 9 lines · Cosmin Tanislav

AD4130-8 is an ultra-low power, high precision, measurement solution for low bandwidth battery operated applications.

The fully integrated AFE (Analog Front-End) includes a multiplexer for up to 16 single-ended or 8 differential inputs, PGA (Programmable Gain Amplifier), 24-bit Sigma-Delta ADC, on-chip reference and oscillator, selectable filter options, smart sequencer, sensor biasing and excitation options, diagnostics, and a FIFO buffer.

Quoted 5 lines
Quoted 3 lines

I believe we may gain a few LoCs by slightly bending the rule of 80. Also see below.

I'll only go over the 80 columns limit if Jonathan agrees to it.

Where it helps readability I fine with us going past 80. I'm not convinced by every case Andy highlights, but many do make things easier to read.

Quoted 10 lines
Quoted 7 lines

drivers/iio/adc/ad4130.c line 390

Quoted 4 lines
drivers/iio/adc/ad4130.c new file +3 -0
+       *size = ad4130_reg_size[reg];
+       if (!*size)
+               return -EINVAL;  

Is this check necessary?

Yes. I haven't described all registers in the table, and the registers can be accessed by the user via the debugfs_reg_access() method.

For that one, probably worth a comment as not immediately obvious.

Quoted 1 line

...

Quoted 23 lines
Quoted 23 lines
Quoted 22 lines
drivers/iio/adc/ad4130.c new file +20 -0
+       switch (ref_sel) {
+       case AD4130_REF_REFIN1:
+               ret = regulator_get_voltage(st->regulators[2].consumer);
+               break;
+       case AD4130_REF_REFIN2:
+               ret = regulator_get_voltage(st->regulators[3].consumer);
+               break;
+       case AD4130_REF_AVDD_AVSS:
+               ret = regulator_get_voltage(st->regulators[0].consumer);
+               break;
+       case AD4130_REF_REFOUT_AVSS:
+               ret = st->int_ref_uv;
+               break;
+       default:
+               ret = -EINVAL;
+               break;
+       }  
 
+       if (ret < 0)
+               return dev_err_probe(dev, ret, "Cannot use reference %u\n",
+                                    ref_sel);  
Quoted 6 lines
Quoted 3 lines

Can it be moved to the caller where it would cleaner to use, I think? As a good side effect the all above will be shortened to just return directly.

I'm pretty sure I remember Jonathan suggested moving it inside the function.

It's possible, though I don't know what my reasoning would have been...

maintainer
#8 lore raw

Re: [PATCH v4 2/2] iio: adc: ad4130: add AD4130 driver

In reply to Cosmin Tanislav
Quoted 10 lines · On Wed, 8 Jun 2022 12:12:38 +0300 Cosmin Tanislav <[email protected]>…

AD4130-8 is an ultra-low power, high precision, measurement solution for low bandwidth battery operated applications.

The fully integrated AFE (Analog Front-End) includes a multiplexer for up to 16 single-ended or 8 differential inputs, PGA (Programmable Gain Amplifier), 24-bit Sigma-Delta ADC, on-chip reference and oscillator, selectable filter options, smart sequencer, sensor biasing and excitation options, diagnostics, and a FIFO buffer.

FWIW I took another look through and didn't find anything to add to the existing reviews of this version.

Jonathan