Skip to contentSkip to conversation

Adding support for Microchip MCP3564 ADC family

PATCH Superseded [email protected] 3 participants 9 messages Aug 4, 2023 – Aug 28, 2023

Subsystems (3)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

v3 cover Adding support for Microchip MCP3564 ADC family

From: Marius Cristea <[email protected]>

Adding support for Microchip family of 153.6 ksps, Low-Noise 16/24-Bit Delta-Sigma ADCs with an SPI interface. This driver covers the following part numbers:
- MCP3561, MCP3562, MCP3564, MCP3561R, MCP3562R, MCP3564R,
- MCP3461, MCP3462, MCP3464, MCP3461R, MCP3462R and MCP3464R.

Differences related to previous patch:
v3:
- fix review comments:
- fix and update the device tree bindings
- enable "auto_zeroing_ref_enable" attribute only
when internal reference is used
- remove unused headers
- fix comments (kernel-docs)
- remove scan_type
- replace "extend_name" with read_label
- print label for each channel (label could be added into the dt)
- add comment to explain the maximum channels numbers
- add protection around critical region
- fallback compatible in device tree to deal with some newer part number

- Open questions:
- whether or not to add a spi-mux type of thing to deal with the part number address in case there are multiple devices connected to the same chip select.
- discussion related to the "custom property". Last time around a consensus wasn't reached.

v2:
- fix review comments:
- change the device tree bindings
- change the ADC channel creation (starting from DT)
- use defines, masks and FIELD_PREP() instead of hardcoded values
- mode the PGA from Hardware Gain to scale
- add a current output channel from burnout current
- fix coding style issues
- use self-explanatory naming to drop the comment
- renumbered the versioning (start with v1 instead of v0)

v1:
- first version committed to review

Marius Cristea (2):
dt-bindings: iio: adc: adding MCP3564 ADC
iio: adc: adding support for MCP3564 ADC

 .../ABI/testing/sysfs-bus-iio-adc-mcp3564     |   53 +
 .../bindings/iio/adc/microchip,mcp3564.yaml   |  200 +++
 MAINTAINERS                                   |    7 +
 drivers/iio/adc/Kconfig                       |   13 +
 drivers/iio/adc/Makefile                      |    1 +
 drivers/iio/adc/mcp3564.c                     | 1541 +++++++++++++++++
 6 files changed, 1815 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-adc-mcp3564
 create mode 100644 Documentation/devicetree/bindings/iio/adc/microchip,mcp3564.yaml
 create mode 100644 drivers/iio/adc/mcp3564.c

base-commit: 9e66fb52449538406cea43e9f3889c391350e76e

Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH v3 1/2 dt-bindings: iio: adc: adding MCP3564 ADC

From: Marius Cristea <[email protected]>

This is the device tree schema for iio driver for Microchip family of 153.6 ksps, Low-Noise 16/24-Bit Delta-Sigma ADCs with an SPI interface (Microchip's MCP3461, MCP3462, MCP3464, MCP3461R, MCP3462R, MCP3464R, MCP3561, MCP3562, MCP3564, MCP3561R, MCP3562R and MCP3564R analog to digital converters).

---
 .../bindings/iio/adc/microchip,mcp3564.yaml   | 200 ++++++++++++++++++
 1 file changed, 200 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/adc/microchip,mcp3564.yaml
Documentation/devicetree/bindings/iio/adc/microchip,mcp3564.yaml new file +200 -0
new file mode 100644
index 000000000000..4a264f48a90e
@@ -0,0 +1,200 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/adc/microchip,mcp3564.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Microchip MCP346X and MCP356X ADC Family
+
+maintainers:
+  - Marius Cristea <[email protected]>
+
+description: |
+  Bindings for the Microchip family of 153.6 ksps, Low-Noise 16/24-Bit
+  Delta-Sigma ADCs with an SPI interface. Datasheet can be found here:
+  Datasheet for MCP3561, MCP3562, MCP3564 can be found here:
+    https://ww1.microchip.com/downloads/aemDocuments/documents/MSLD/ProductDocuments/DataSheets/MCP3561-2-4-Family-Data-Sheet-DS20006181C.pdf
+  Datasheet for MCP3561R, MCP3562R, MCP3564R can be found here:
+    https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3561_2_4R-Data-Sheet-DS200006391C.pdf
+  Datasheet for MCP3461, MCP3462, MCP3464 can be found here:
+    https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4-Two-Four-Eight-Channel-153.6-ksps-Low-Noise-16-Bit-Delta-Sigma-ADC-Data-Sheet-20006180D.pdf
+  Datasheet for MCP3461R, MCP3462R, MCP3464R can be found here:
+    https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf
+
+properties:
+  compatible:
+    enum:
+      - microchip,mcp3461
+      - microchip,mcp3462
+      - microchip,mcp3464
+      - microchip,mcp3461r
+      - microchip,mcp3462r
+      - microchip,mcp3464r
+      - microchip,mcp3561
+      - microchip,mcp3562
+      - microchip,mcp3564
+      - microchip,mcp3561r
+      - microchip,mcp3562r
+      - microchip,mcp3564r
+
+  reg:
+    maxItems: 1
+
+  spi-max-frequency:
+    maximum: 20000000
+
+  spi-cpha: true
+
+  spi-cpol: true
+
+  vdd-supply: true
+
+  avdd-supply: true
+
+  clocks:
+    description:
+      Phandle and clock identifier for external sampling clock.
+      If not specified, the internal crystal oscillator will be used.
+    maxItems: 1
+
+  interrupts:
+    description: IRQ line of the ADC
+    maxItems: 1
+
+  drive-open-drain:
+    description:
+      Whether to drive the IRQ signal as push-pull (default) or open-drain. Note
+      that the device requires this pin to become "high", otherwise it will stop
+      converting.
+    type: boolean
+
+  vref-supply:
+    description:
+      Some devices have a specific reference voltage supplied on a different
+      pin to the other supplies. Needed to be able to establish channel scaling
+      unless there is also an internal reference available (e.g. mcp3564r). In
+      case of "r" devices (e. g. mcp3564r), if it does not exists the internal
+      reference will be used.
+
+  microchip,hw-device-address:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    minimum: 0
+    maximum: 3
+    description:
+      The address is set on a per-device basis by fuses in the factory,
+      configured on request. If not requested, the fuses are set for 0x1.
+      The device address is part of the device markings to avoid
+      potential confusion. This address is coded on two bits, so four possible
+      addresses are available when multiple devices are present on the same
+      SPI bus with only one Chip Select line for all devices.
+      Each device communication starts by a CS falling edge, followed by the
+      clocking of the device address (BITS[7:6] - top two bits of COMMAND BYTE
+      which is first one on the wire).
+
+  "#io-channel-cells":
+    const: 1
+
+  "#address-cells":
+    const: 1
+
+  "#size-cells":
+    const: 0
+
+patternProperties:
+  "^channel@([0-9]|([1-7][0-9]))$":
+    $ref: adc.yaml
+    type: object
+    unevaluatedProperties: false
+    description: Represents the external channels which are connected to the ADC.
+
+    properties:
+      reg:
+        description: The channel number in single-ended and differential mode.
+        minimum: 0
+        maximum: 79
+
+    required:
+      - reg
+
+dependencies:
+  spi-cpol: [ spi-cpha ]
+  spi-cpha: [ spi-cpol ]
+
+required:
+  - compatible
+  - reg
+  - microchip,hw-device-address
+  - spi-max-frequency
+
+allOf:
+  - $ref: /schemas/spi/spi-peripheral-props.yaml#
+  - # External vref, no internal reference
+    if:
+      properties:
+        compatible:
+          contains:
+            enum:
+              - microchip,mcp3461
+              - microchip,mcp3462
+              - microchip,mcp3464
+              - microchip,mcp3561
+              - microchip,mcp3562
+              - microchip,mcp3564
+    then:
+      required:
+        - vref-supply
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    spi {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        adc@0 {
+            compatible = "microchip,mcp3564r";
+            reg = <0>;
+            vref-supply = <&vref_reg>;
+            spi-cpha;
+            spi-cpol;
+            spi-max-frequency = <10000000>;
+            microchip,hw-device-address = <1>;
+
+            #address-cells = <1>;
+            #size-cells = <0>;
+
+            channel@0 {
+                /* CH0 to AGND */
+                reg = <0>;
+            };
+
+            channel@1 {
+                /* CH1 to AGND */
+                reg = <1>;
+            };
+
+            /* diff-channels */
+            channel@11 {
+                reg = <11>;
+
+                /* CN0, CN1 */
+                diff-channels = <0 1>;
+            };
+
+            channel@22 {
+                reg = <0x22>;
+
+                /* CN1, CN2 */
+                diff-channels = <1 2>;
+            };
+
+            channel@23 {
+                reg = <0x23>;
+
+                /* CN1, CN3 */
+                diff-channels = <1 3>;
+            };
+        };
+    };
+...
Signature · 1 line
2.34.1
author maintainer
#3 lore raw

PATCH v3 2/2 iio: adc: adding support for MCP3564 ADC

In reply to [email protected]

From: Marius Cristea <[email protected]>

This is the iio driver for Microchip family of 153.6 ksps, Low-Noise 16/24-Bit Delta-Sigma ADCs with an SPI interface (Microchip's MCP3461, MCP3462, MCP3464, MCP3461R, MCP3462R, MCP3464R, MCP3561, MCP3562, MCP3564, MCP3561R, MCP3562R and MCP3564R analog to digital converters).

---
 .../ABI/testing/sysfs-bus-iio-adc-mcp3564     |   53 +
 MAINTAINERS                                   |    7 +
 drivers/iio/adc/Kconfig                       |   13 +
 drivers/iio/adc/Makefile                      |    1 +
 drivers/iio/adc/mcp3564.c                     | 1541 +++++++++++++++++
 5 files changed, 1615 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-adc-mcp3564
 create mode 100644 drivers/iio/adc/mcp3564.c
Documentation/ABI/testing/sysfs-bus-iio-adc-mcp3564 new file +53 -0
new file mode 100644
index 000000000000..feed2f596537
@@ -0,0 +1,53 @@
+What:		/sys/bus/iio/devices/iio:deviceX/boost_current
+KernelVersion:	6.4
+Contact:	[email protected]
+Description:
+		This attribute is used to set the biasing circuit of the
+		Delta-Sigma modulator. The different BOOST settings are applied
+		to the entire modulator circuit, including the voltage reference
+		buffers.
+
+What:		/sys/bus/iio/devices/iio:deviceX/boost_current_available
+KernelVersion:	6.4
+Contact:	[email protected]
+Description:
+		Reading returns a list with the possible values for
+		the current biasing circuit of the Delta-Sigma modulator.
+
+What:		/sys/bus/iio/devices/iio:deviceX/auto_zeroing_mux_enable
+KernelVersion:	6.4
+Contact:	[email protected]
+Description:
+		This attribute is used to enable the analog input multiplexer
+		auto-zeroing algorithm (the input multiplexer and the ADC
+		include an offset cancellation algorithm that cancels the offset
+		contribution of the ADC). When the offset cancellation algorithm
+		is enabled, ADC takes two conversions, one with the differential
+		input as VIN+/VIN-, one with VIN+/VIN- inverted. In this case the
+		conversion time is multiplied by two compared to the default
+		case where the algorithm is disabled. This technique allows the
+		cancellation of the ADC offset error and the achievement of
+		ultra-low offset without any digital calibration. The resulting
+		offset is the residue of the difference between the two
+		conversions, which is on the order of magnitude of the noise
+		floor. This offset is effectively canceled at every conversion,
+		so the residual offset error temperature drift is extremely low.
+		Write '1' to enable it, write '0' to disable it.
+
+What:		/sys/bus/iio/devices/iio:deviceX/auto_zeroing_ref_enable
+KernelVersion:	6.4
+Contact:	[email protected]
+Description:
+		This attribute is used to enable the chopping algorithm for the 
+		internal voltage reference buffer. This setting has no effect
+		when external voltage reference is selected.
+		Internal voltage reference buffer injects a certain quantity of
+		1/f noise into the system that can be modulated with the
+		incoming input signals and can limit the SNR performance at
+		higher Oversampling Ratio values (over 256). To overcome this
+		limitation, the buffer includes an auto-zeroing algorithm that
+		greatly reduces (cancels out) the 1/f noise and cancels the
+		offset value of the reference buffer. As a result, the SNR of
+		the system is not affected by this 1/f noise component of the
+		reference buffer, even at maximum oversampling ratio values.
+		Write '1' to enable it, write '0' to disable it.
MAINTAINERS +7 -0
index 6969a2962ffe..50c879f85e1e 100644
@@ -13798,6 +13798,13 @@ S:	Supported
 F:	Documentation/devicetree/bindings/regulator/mcp16502-regulator.txt
 F:	drivers/regulator/mcp16502.c
 
+MICROCHIP MCP3564 ADC DRIVER
+M:      Marius Cristea <[email protected]>
+L:      [email protected]
+S:      Supported
+F:      Documentation/devicetree/bindings/iio/adc/microchip,mcp3564.yaml
+F:      drivers/iio/adc/mcp3564.c
+
 MICROCHIP MCP3911 ADC DRIVER
 M:	Marcus Folkesson <[email protected]>
 M:	Kent Gustavsson <[email protected]>
drivers/iio/adc/Kconfig +13 -0
index 53098aca06ea..782bf5eec8ed 100644
@@ -768,6 +768,19 @@ config MCP3422
 	  This driver can also be built as a module. If so, the module will be
 	  called mcp3422.
 
+config MCP3564
+        tristate "Microchip Technology MCP3461/2/4/R, MCP3561/2/4/R driver"
+        depends on SPI
+        depends on IIO
+        help
+          Say yes here to build support for Microchip Technology's MCP3461,
+          MCP3462, MCP3464, MCP3461R, MCP3462R, MCP3464R, MCP3561, MCP3562,
+          MCP3564, MCP3561R, MCP3562R and MCP3564R analog to digital
+          converters.
+
+          This driver can also be built as a module. If so, the module will be
+          called mcp3564.
+
 config MCP3911
 	tristate "Microchip Technology MCP3911 driver"
 	depends on SPI
drivers/iio/adc/Makefile +1 -0
index e07e4a3e6237..91f713af574f 100644
@@ -70,6 +70,7 @@ obj-$(CONFIG_MAX1363) += max1363.o
 obj-$(CONFIG_MAX9611) += max9611.o
 obj-$(CONFIG_MCP320X) += mcp320x.o
 obj-$(CONFIG_MCP3422) += mcp3422.o
+obj-$(CONFIG_MCP3564) += mcp3564.o
 obj-$(CONFIG_MCP3911) += mcp3911.o
 obj-$(CONFIG_MEDIATEK_MT6360_ADC) += mt6360-adc.o
 obj-$(CONFIG_MEDIATEK_MT6370_ADC) += mt6370-adc.o
drivers/iio/adc/mcp3564.c new file +1541 -0

1,541 lines not on this page · raw message

Signature · 1 line
2.34.1
#7 lore raw
In reply to [email protected]
Quoted 22 lines · On Fri, Aug 04, 2023 at 05:28:18PM +0300, [email protected] wrote:

From: Marius Cristea <[email protected]>

Adding support for Microchip family of 153.6 ksps, Low-Noise 16/24-Bit Delta-Sigma ADCs with an SPI interface. This driver covers the following part numbers:
- MCP3561, MCP3562, MCP3564, MCP3561R, MCP3562R, MCP3564R,
- MCP3461, MCP3462, MCP3464, MCP3461R, MCP3462R and MCP3464R.

Differences related to previous patch:
v3:
- fix review comments:
- fix and update the device tree bindings
- enable "auto_zeroing_ref_enable" attribute only
when internal reference is used
- remove unused headers
- fix comments (kernel-docs)
- remove scan_type
- replace "extend_name" with read_label
- print label for each channel (label could be added into the dt)
- add comment to explain the maximum channels numbers
- add protection around critical region

Quoted 1 line

- fallback compatible in device tree to deal with some newer part number

The compatibles are all still in an enum in the binding with no fallbacks. Did you forget to commit this?

Thanks,
Conor.

Quoted 7 lines

- Open questions:
- whether or not to add a spi-mux type of thing to deal with the part number address in case there are multiple devices connected to the same chip select.
- discussion related to the "custom property". Last time around a consensus wasn't reached.

maintainer
#8 lore raw
In reply to [email protected]
Quoted 23 lines · On Fri, 4 Aug 2023 17:28:18 +0300 <[email protected]> wrote:

From: Marius Cristea <[email protected]>

Adding support for Microchip family of 153.6 ksps, Low-Noise 16/24-Bit Delta-Sigma ADCs with an SPI interface. This driver covers the following part numbers:
- MCP3561, MCP3562, MCP3564, MCP3561R, MCP3562R, MCP3564R,
- MCP3461, MCP3462, MCP3464, MCP3461R, MCP3462R and MCP3464R.

Differences related to previous patch:
v3:
- fix review comments:
- fix and update the device tree bindings
- enable "auto_zeroing_ref_enable" attribute only
when internal reference is used
- remove unused headers
- fix comments (kernel-docs)
- remove scan_type
- replace "extend_name" with read_label
- print label for each channel (label could be added into the dt)
- add comment to explain the maximum channels numbers
- add protection around critical region
- fallback compatible in device tree to deal with some newer part number

Quoted 4 lines

- Open questions:
- whether or not to add a spi-mux type of thing to deal with the part number address in case there are multiple devices connected to the same chip select.

I'd failed to register (until noticing it in a review a few mins ago) that we have have precedence for devices doing this device-address in the SPI transfer thing. The mcp3911 does it as well. Obviously that doesn't rule out us doing something different with this one though.

I think we should take the view this is relatively uncommon and go with this simple vendor specific dt-binding approach. Always nice to do something more general, but sometimes it isn't worth the effort.

Quoted 34 lines

- discussion related to the "custom property". Last time around a consensus wasn't reached.

v2:
- fix review comments:
- change the device tree bindings
- change the ADC channel creation (starting from DT)
- use defines, masks and FIELD_PREP() instead of hardcoded values
- mode the PGA from Hardware Gain to scale
- add a current output channel from burnout current
- fix coding style issues
- use self-explanatory naming to drop the comment
- renumbered the versioning (start with v1 instead of v0)

v1:
- first version committed to review

Marius Cristea (2):
dt-bindings: iio: adc: adding MCP3564 ADC
iio: adc: adding support for MCP3564 ADC

 .../ABI/testing/sysfs-bus-iio-adc-mcp3564     |   53 +
 .../bindings/iio/adc/microchip,mcp3564.yaml   |  200 +++
 MAINTAINERS                                   |    7 +
 drivers/iio/adc/Kconfig                       |   13 +
 drivers/iio/adc/Makefile                      |    1 +
 drivers/iio/adc/mcp3564.c                     | 1541 +++++++++++++++++
 6 files changed, 1815 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-adc-mcp3564
 create mode 100644 Documentation/devicetree/bindings/iio/adc/microchip,mcp3564.yaml
 create mode 100644 drivers/iio/adc/mcp3564.c

base-commit: 9e66fb52449538406cea43e9f3889c391350e76e

maintainer
#2 lore raw

Re: [PATCH v3 1/2] dt-bindings: iio: adc: adding MCP3564 ADC

In reply to [email protected]
Quoted 10 lines · On Fri, 4 Aug 2023 17:28:19 +0300 <[email protected]> wrote:

From: Marius Cristea <[email protected]>

This is the device tree schema for iio driver for Microchip family of 153.6 ksps, Low-Noise 16/24-Bit Delta-Sigma ADCs with an SPI interface (Microchip's MCP3461, MCP3462, MCP3464, MCP3461R, MCP3462R, MCP3464R, MCP3561, MCP3562, MCP3564, MCP3561R, MCP3562R and MCP3564R analog to digital converters).

Given driver handles the channel label binding, nice to have that in the example here.

Quoted 53 lines
Documentation/devicetree/bindings/iio/adc/microchip,mcp3564.yaml new file +53 -0
+
+examples:
+  - |
+    spi {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        adc@0 {
+            compatible = "microchip,mcp3564r";
+            reg = <0>;
+            vref-supply = <&vref_reg>;
+            spi-cpha;
+            spi-cpol;
+            spi-max-frequency = <10000000>;
+            microchip,hw-device-address = <1>;
+
+            #address-cells = <1>;
+            #size-cells = <0>;
+
+            channel@0 {
+                /* CH0 to AGND */
+                reg = <0>;
+            };
+
+            channel@1 {
+                /* CH1 to AGND */
+                reg = <1>;
+            };
+
+            /* diff-channels */
+            channel@11 {
+                reg = <11>;
+
+                /* CN0, CN1 */
+                diff-channels = <0 1>;
+            };
+
+            channel@22 {
+                reg = <0x22>;
+
+                /* CN1, CN2 */
+                diff-channels = <1 2>;
+            };
+
+            channel@23 {
+                reg = <0x23>;
+
+                /* CN1, CN3 */
+                diff-channels = <1 3>;
+            };
+        };
+    };
+...
maintainer
#4 lore raw

Re: [PATCH v3 2/2] iio: adc: adding support for MCP3564 ADC

In reply to [email protected]
Quoted 9 lines · On Fri, 4 Aug 2023 17:28:20 +0300 <[email protected]> wrote:

From: Marius Cristea <[email protected]>

This is the iio driver for Microchip family of 153.6 ksps, Low-Noise 16/24-Bit Delta-Sigma ADCs with an SPI interface (Microchip's MCP3461, MCP3462, MCP3464, MCP3461R, MCP3462R, MCP3464R, MCP3561, MCP3562, MCP3564, MCP3561R, MCP3562R and MCP3564R analog to digital converters).

Hi Marius

Various things inline. Driver is coming together nicely so mostly suggestions on how to tidy things up a little + give an easier to review / maintain driver.

Thanks,

Jonathan

Quoted 16 lines
---
 .../ABI/testing/sysfs-bus-iio-adc-mcp3564     |   53 +
 MAINTAINERS                                   |    7 +
 drivers/iio/adc/Kconfig                       |   13 +
 drivers/iio/adc/Makefile                      |    1 +
 drivers/iio/adc/mcp3564.c                     | 1541 +++++++++++++++++
 5 files changed, 1615 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-adc-mcp3564
 create mode 100644 drivers/iio/adc/mcp3564.c
Documentation/ABI/testing/sysfs-bus-iio-adc-mcp3564 new file +0 -0
new file mode 100644
index 000000000000..feed2f596537

Documentation/ABI/testing/sysfs-bus-iio-adc-mcp3564 line 8

Quoted 8 lines
Documentation/ABI/testing/sysfs-bus-iio-adc-mcp3564 new file +8 -0
@@ -0,0 +1,53 @@
+What:		/sys/bus/iio/devices/iio:deviceX/boost_current
+KernelVersion:	6.4
+Contact:	[email protected]
+Description:
+		This attribute is used to set the biasing circuit of the
+		Delta-Sigma modulator. The different BOOST settings are applied
+		to the entire modulator circuit, including the voltage reference
+		buffers.

Units? Should be mA given that is what we use for other current attributes in IIO.

This one may cause us problems with generality of the description if we reuse if for other devices (and hence move it to the more general files), but we can figure this out at the time.

Quoted 45 lines
Documentation/ABI/testing/sysfs-bus-iio-adc-mcp3564 new file +45 -0
+
+What:		/sys/bus/iio/devices/iio:deviceX/boost_current_available
+KernelVersion:	6.4
+Contact:	[email protected]
+Description:
+		Reading returns a list with the possible values for
+		the current biasing circuit of the Delta-Sigma modulator.
+
+What:		/sys/bus/iio/devices/iio:deviceX/auto_zeroing_mux_enable
+KernelVersion:	6.4
+Contact:	[email protected]
+Description:
+		This attribute is used to enable the analog input multiplexer
+		auto-zeroing algorithm (the input multiplexer and the ADC
+		include an offset cancellation algorithm that cancels the offset
+		contribution of the ADC). When the offset cancellation algorithm
+		is enabled, ADC takes two conversions, one with the differential
+		input as VIN+/VIN-, one with VIN+/VIN- inverted. In this case the
+		conversion time is multiplied by two compared to the default
+		case where the algorithm is disabled. This technique allows the
+		cancellation of the ADC offset error and the achievement of
+		ultra-low offset without any digital calibration. The resulting
+		offset is the residue of the difference between the two
+		conversions, which is on the order of magnitude of the noise
+		floor. This offset is effectively canceled at every conversion,
+		so the residual offset error temperature drift is extremely low.
+		Write '1' to enable it, write '0' to disable it.
+
+What:		/sys/bus/iio/devices/iio:deviceX/auto_zeroing_ref_enable
+KernelVersion:	6.4
+Contact:	[email protected]
+Description:
+		This attribute is used to enable the chopping algorithm for the 
+		internal voltage reference buffer. This setting has no effect
+		when external voltage reference is selected.
+		Internal voltage reference buffer injects a certain quantity of
+		1/f noise into the system that can be modulated with the
+		incoming input signals and can limit the SNR performance at
+		higher Oversampling Ratio values (over 256). To overcome this
+		limitation, the buffer includes an auto-zeroing algorithm that
+		greatly reduces (cancels out) the 1/f noise and cancels the
+		offset value of the reference buffer. As a result, the SNR of
+		the system is not affected by this 1/f noise component of the
+		reference buffer, even at maximum oversampling ratio values.
+		Write '1' to enable it, write '0' to disable it.

...

Quoted 25 lines
drivers/iio/adc/Makefile +1 -0
index e07e4a3e6237..91f713af574f 100644
@@ -70,6 +70,7 @@ obj-$(CONFIG_MAX1363) += max1363.o
 obj-$(CONFIG_MAX9611) += max9611.o
 obj-$(CONFIG_MCP320X) += mcp320x.o
 obj-$(CONFIG_MCP3422) += mcp3422.o
+obj-$(CONFIG_MCP3564) += mcp3564.o
 obj-$(CONFIG_MCP3911) += mcp3911.o
 obj-$(CONFIG_MEDIATEK_MT6360_ADC) += mt6360-adc.o
 obj-$(CONFIG_MEDIATEK_MT6370_ADC) += mt6370-adc.o
drivers/iio/adc/mcp3564.c new file +7 -0
new file mode 100644
index 000000000000..60d6a803c0c2
@@ -0,0 +1,1541 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * IIO driver for MCP356X/MCP356XR and MCP346X/MCP346XR series ADC chip family
+ *
+ * Copyright (C) 2022-2023 Microchip Technology Inc. and its subsidiaries
+ *
+ * Author: Marius Cristea <[email protected]>

drivers/iio/adc/mcp3564.c line 17

Quoted 10 lines
drivers/iio/adc/mcp3564.c new file +10 -0
+ *
+ * Datasheet for MCP3561, MCP3562, MCP3564 can be found here:
+ * https://ww1.microchip.com/downloads/aemDocuments/documents/MSLD/ProductDocuments/DataSheets/MCP3561-2-4-Family-Data-Sheet-DS20006181C.pdf
+ * Datasheet for MCP3561R, MCP3562R, MCP3564R can be found here:
+ * https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3561_2_4R-Data-Sheet-DS200006391C.pdf
+ * Datasheet for MCP3461, MCP3462, MCP3464 can be found here:
+ * https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4-Two-Four-Eight-Channel-153.6-ksps-Low-Noise-16-Bit-Delta-Sigma-ADC-Data-Sheet-20006180D.pdf
+ * Datasheet for MCP3461R, MCP3462R, MCP3464R can be found here:
+ * https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf
+ *

Last blank line here adds nothing so I'd drop it.

drivers/iio/adc/mcp3564.c line 30

Quoted 13 lines
drivers/iio/adc/mcp3564.c new file +13 -0
+ */
+
+#include <linux/bitfield.h>
+#include <linux/iopoll.h>
+#include <linux/regulator/consumer.h>
+#include <linux/spi/spi.h>
+#include <linux/units.h>
+#include <linux/util_macros.h>
+
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+
+#define MCP3564_ADCDATA		0x00

Always good to name register addresses so it is clear what they are. _REG or _ADDR or similar.

Quoted 6 lines
drivers/iio/adc/mcp3564.c new file +6 -0
+#define MCP3564_CONFIG0		0x01
+#define MCP3564_CONFIG1		0x02
+#define MCP3564_CONFIG2		0x03
+#define MCP3564_CONFIG3		0x04
+#define MCP3564_IRQ		0x05
+#define MCP3564_MUX		0x06

drivers/iio/adc/mcp3564.c line 46

Quoted 10 lines
drivers/iio/adc/mcp3564.c new file +10 -0
+#define MCP3564_SCAN		0x07
+#define MCP3564_TIMER		0x08
+#define MCP3564_OFFSETCAL	0x09
+#define MCP3564_GAINCAL		0x0A
+#define MCP3564_RESERVED_B	0x0B
+#define MCP3564_RESERVED_C	0x0C
+#define MCP3564_LOCK		0x0D
+#define MCP3564_RESERVED_E	0x0E
+#define MCP3564_CRCCFG		0x0F
+

Lots of the comments that follow apply across all the register field definitions. Main theme is I'm looking for explicit naming of everything and minimal number of defines.

Quoted 10 lines
drivers/iio/adc/mcp3564.c new file +10 -0
+/*
+ * CONFIG 3 Register - Conversion Mode Selection
+ * 0b11 = Continuous Conversion mode or continuous conversion cycle in SCAN mode.
+ * 0b10 = One-shot conversion or one-shot cycle in SCAN mode. It sets ADC_MODE[1:0] to ‘10’
+ *        (standby) at the end of the conversion or at the end of the conversion cycle in SCAN mode.
+ * 0b0x = One-shot conversion or one-shot cycle in SCAN mode. It sets ADC_MODE[1:0] to ‘0x’ (ADC
+ *        Shutdown) at the end of the conversion or at the end of the conversion cycle in SCAN
+ *        mode (default).
+ */
+#define MCP3464_CONV_MODE_CONTINUOUS		3

drivers/iio/adc/mcp3564.c line 162

Quoted 10 lines
drivers/iio/adc/mcp3564.c new file +10 -0
+#define MCP3464_CONV_MODE_ONE_SHOT_STANDBY	2
+#define MCP3464_CONV_MODE_ONE_SHOT_SHUTDOWN	0
+#define MCP3464_CONV_MODE_MASK			GENMASK(7, 6)
+#define MCP3564_CONV_MODE_SET(x)		((u8)FIELD_PREP(MCP3464_CONV_MODE_MASK, (x)))
+
+/*
+ * CONFIG 3 Register - ADC Output Data Format Selection
+ * 0b11 = 32-bit (25-bit right justified data + Channel ID):
+ *        CHID[3:0] + SGN extension (4 bits) + 24-bit ADC data.
+ *        It allows overrange with the SGN extension.

I'd put these immediately before each define rather than in a block up here.

Quoted 4 lines
drivers/iio/adc/mcp3564.c new file +4 -0
+ * 0b10 = 32-bit (25-bit right justified data):
+ *        SGN extension (8-bit) + 24-bit ADC data.
+ *        It allows overrange with the SGN extension.
+ * 0b01 = 32-bit (24-bit left justified data):

drivers/iio/adc/mcp3564.c line 176

Quoted 10 lines
drivers/iio/adc/mcp3564.c new file +10 -0
+ *        24-bit ADC data + 0x00 (8-bit).
+ *        It does not allow overrange (ADC code locked to 0xFFFFFF or 0x800000).
+ * 0b00 = 24-bit (default ADC coding): 24-bit ADC data.
+ *        It does not allow overrange (ADC code locked to 0xFFFFFF or 0x800000).
+ */
+#define MCP3456_DATA_FORMAT_32B_WITH_CH_ID	3
+#define MCP3456_DATA_FORMAT_32B_SGN_EXT		2
+#define MCP3456_DATA_FORMAT_32B_LEFT_JUSTIFIED	1
+#define MCP3456_DATA_FORMAT_24B			0
+#define MCP3464_DATA_FORMAT_MASK		GENMASK(5, 4)

I'd generally like to see the register name in these field names as well. Perhaps

#define MCP3464_CONFIG3_DATA_FMT_...
as it makes it easy to see if thy are being written to the write registers.

Same for all other register fields. Sometimes the code is a bit longer doing this but it makes it easier to tell if there is a mismatch.

drivers/iio/adc/mcp3564.c line 186

Quoted 10 lines
drivers/iio/adc/mcp3564.c new file +10 -0
+#define MCP3564_DATA_FORMAT_SET(x)		((u8)FIELD_PREP(MCP3464_DATA_FORMAT_MASK, (x)))
+
+#define MCP3464_CRC_FORMAT		BIT(3)
+#define MCP3464_EN_CRCCOM		BIT(2)
+#define MCP3464_EN_OFFCAL		BIT(1)
+#define MCP3464_EN_GAINCAL		BIT(0)
+
+/* IRQ REGISTER */
+#define MCP3564_DATA_READY_MASK		BIT(6)
+#define MCP3564_CRCCFG_STATUS		BIT(5)

For consistency name these all _MASK as they are all fields that can have values 0 or 1 in them.

drivers/iio/adc/mcp3564.c line 197

Quoted 11 lines
drivers/iio/adc/mcp3564.c new file +11 -0
+#define MCP3564_POR_STATUS		BIT(4)
+#define MCP3464_IRQ_MODE_1		BIT(3)
+#define MCP3464_IRQ_MODE_0		BIT(2)
+#define MCP3464_EN_FASTCMD		BIT(1)
+#define MCP3464_EN_STP			BIT(0)
+
+/* MULTIPLEXER (MUX) REGISTER */
+#define MCP3564_MUX_VIN_P_MASK		GENMASK(7, 4)
+#define MCP3564_MUX_VIN_N_MASK		GENMASK(3, 0)
+#define MCP3564_MUX_SET(x, y)		((u8)FIELD_PREP(MCP3564_MUX_VIN_P_MASK, (x)) |	\
+					 (u8)FIELD_PREP(MCP3564_MUX_VIN_N_MASK, (y)))

This one (unlike below) is probably worth having, but why are the casts needed?

drivers/iio/adc/mcp3564.c line 217

Quoted 6 lines
drivers/iio/adc/mcp3564.c new file +6 -0
+/* OFFSETCAL REGISTER */
+#define MCP3564_DEFAULT_OFFSETCAL	0
+#define MCP3564_OFFSETCAL_MASK		GENMASK(23, 0)
+#define MCP3564_OFFSETCAL_SET(x)	((u32)FIELD_PREP(MCP3564_OFFSETCAL_MASK, (x)))
+
+/* GAINCAL REGISTER */

You could move these up to next to the register definition.
A bit of playing with indenting can then be used to separate the fields from the register itself. That tends to make things more self documenting so you can get rid of the comments and end up with tidier code.

e.g.

#define MCP3564_GAINCAL_REG 0x0A
#define MCP3564_DEFAULT_GAINCAL 0x00800000
#define MCP3564_GAINCAL_MASK GENMASK(23, 0)

drivers/iio/adc/mcp3564.c line 220

Quoted 3 lines
drivers/iio/adc/mcp3564.c new file +3 -0
+#define MCP3564_DEFAULT_GAINCAL		0x00800000
+#define MCP3564_GAINCAL_MASK		GENMASK(23, 0)
+#define MCP3564_GAINCAL_SET(x)		((u32)FIELD_PREP(MCP3564_GAINCAL_MASK, (x)))

Are these _SET macros worth having?
FIELD_PREP(MCP3565_GAINCAL_MASK, x) is pretty clear without needing a macro to name it.

drivers/iio/adc/mcp3564.c line 230

Quoted 3 lines
drivers/iio/adc/mcp3564.c new file +3 -0
+
+
+#define MCP3564_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)

For 3 uses? I'd be tempted to just have them long hand

drivers/iio/adc/mcp3564.c line 566

Quoted 10 lines
drivers/iio/adc/mcp3564.c new file +10 -0
+
+static const struct iio_chan_spec_ext_info mcp3564_ext_info[] = {
+	IIO_ENUM("boost_current", IIO_SHARED_BY_ALL, &mcp3564_current_boost_mode_enum),
+	{
+		.name = "boost_current_available",
+		.shared = IIO_SHARED_BY_ALL,
+		.read = iio_enum_available_read,
+		.private = (uintptr_t)&mcp3564_current_boost_mode_enum,
+	},
+	{}
	{ }

for consistency with some cases below.

Quoted 22 lines
drivers/iio/adc/mcp3564.c new file +21 -0
+};
 
+static int mcp3564_read_single_value(struct iio_dev *indio_dev,
+				     struct iio_chan_spec const *channel,
+				     int *val)
+{
+	struct mcp3564_state *adc = iio_priv(indio_dev);
+	int ret;
+	u8 tmp;
+	int ret_read = 0;
+
+	ret = mcp3564_write_8bits(adc, MCP3564_MUX, channel->address);
+	if (ret)
+		return ret;
+
+	/* Start ADC Conversion using fast command (overwrites ADC_MODE[1:0] = 11) */
+	ret = mcp3564_fast_cmd(adc, MCP3564_FASTCMD_START);
+	if (ret)
+		return ret;
+
+	/*
+	 * Check if the conversion is ready. If not, wait a little bit, and

drivers/iio/adc/mcp3564.c line 798

Quoted 10 lines
drivers/iio/adc/mcp3564.c new file +10 -0
+	 * in case of timeout exit with an error.
+	 */
+	ret = read_poll_timeout(mcp3564_read_8bits, ret_read,
+				ret_read || !(tmp & MCP3564_DATA_READY_MASK),
+				20000, MCP3564_DATA_READY_TIMEOUT_MS * 1000, true,
+				adc, MCP3564_IRQ, &tmp);
+
+	/* failed to read status register */
+	if (ret_read)
+		return ret;

Probably want to to return ret_read in this case.

drivers/iio/adc/mcp3564.c line 801

Quoted 3 lines
drivers/iio/adc/mcp3564.c new file +3 -0
+
+	if (ret)
+		return -ETIMEDOUT;

ret should be -ETIMEDOUT so just return ret and save anyone reading the docs :)

Quoted 19 lines
drivers/iio/adc/mcp3564.c new file +18 -0
+
+	if (tmp & MCP3564_DATA_READY_MASK)
+		/* failing to finish conversion */
+		return -EBUSY;
+
+	return mcp3564_read_32bits(adc, MCP3564_ADCDATA, val);
+}
 
+static int mcp3564_read_raw(struct iio_dev *indio_dev,
+			    struct iio_chan_spec const *channel,
+			    int *val, int *val2, long mask)
+{
+	struct mcp3564_state *adc = iio_priv(indio_dev);
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		if (channel->output) {
+			mutex_lock(&adc->lock);

drivers/iio/adc/mcp3564.c line 869

Quoted 10 lines
drivers/iio/adc/mcp3564.c new file +10 -0
+			*val = mcp3564_burnout_avail[adc->burnout_mode][0];
+			*val2 = mcp3564_burnout_avail[adc->burnout_mode][1];
+			mutex_unlock(&adc->lock);
+			return IIO_VAL_INT_PLUS_MICRO;
+		}
+
+		ret = mcp3564_read_single_value(indio_dev, channel, val);
+		if (ret)
+			return -EINVAL;
+		else

Drop the else.

Quoted 24 lines
drivers/iio/adc/mcp3564.c new file +23 -0
+			return IIO_VAL_INT;
+	case IIO_CHAN_INFO_SCALE:
+		mutex_lock(&adc->lock);
+		*val = adc->scale_tbls[adc->hwgain][0];
+		*val2 = adc->scale_tbls[adc->hwgain][1];
+		mutex_unlock(&adc->lock);
+		return IIO_VAL_INT_PLUS_NANO;
+	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
+		*val = mcp3564_oversampling_avail[adc->oversampling];
+		return IIO_VAL_INT;
+	case IIO_CHAN_INFO_CALIBBIAS:
+		*val = adc->calib_bias;
+		return IIO_VAL_INT;
+	case IIO_CHAN_INFO_CALIBSCALE:
+		*val = adc->calib_scale;
+		return IIO_VAL_INT;
+	default:
+		return -EINVAL;
+	}
+}
 
+static int mcp3564_write_raw(struct iio_dev *indio_dev,
+			     struct iio_chan_spec const *channel, int val,
+			     int val2, long mask)

drivers/iio/adc/mcp3564.c line 921

Quoted 10 lines
drivers/iio/adc/mcp3564.c new file +10 -0
+{
+	struct mcp3564_state *adc = iio_priv(indio_dev);
+	int tmp;
+	unsigned int hwgain;
+	enum mcp3564_burnout burnout;
+	int ret = 0;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		if (channel->output) {

Flip logic to cut down on indent

		if (!channel->output)
			return -EINVAL;

		for (..
Quoted 27 lines
drivers/iio/adc/mcp3564.c new file +27 -0
+			for (burnout = 0; burnout < MCP3564_MAX_BURNOUT_IDX; burnout++) {
+				if (val == mcp3564_burnout_avail[burnout][0] &&
+				    val2 == mcp3564_burnout_avail[burnout][1])
+					break;
+			}
+
+			if (burnout == MCP3564_MAX_BURNOUT_IDX)
+				return -EINVAL;
+
+			if (burnout == adc->burnout_mode)
+				return ret;
+
+			mutex_lock(&adc->lock);
+			ret = mcp3564_update_8bits(adc, MCP3564_CONFIG0,
+						   MCP3564_CS_SEL_MASK,
+						   MCP3564_CS_SEL_SET(burnout));
+
+			if (ret)
+				dev_err(&indio_dev->dev, "Failed to configure burnout current\n");
+			else
+				adc->burnout_mode = burnout;
+			mutex_unlock(&adc->lock);
+			return ret;
+		}
+		return -EINVAL;
+	case IIO_CHAN_INFO_CALIBBIAS:
+		if (val < mcp3564_calib_bias[0] && val > mcp3564_calib_bias[2])

drivers/iio/adc/mcp3564.c line 958

Quoted 10 lines
drivers/iio/adc/mcp3564.c new file +10 -0
+			return -EINVAL;
+
+		mutex_lock(&adc->lock);
+		ret = mcp3564_write_24bits(adc, MCP3564_OFFSETCAL, val);
+		if (!ret)
+			adc->calib_bias = val;
+		mutex_unlock(&adc->lock);
+		return ret;
+	case IIO_CHAN_INFO_CALIBSCALE:
+		if (val < mcp3564_calib_bias[0] && val > mcp3564_calib_bias[2])

calib_scale for scale?

Quoted 54 lines
drivers/iio/adc/mcp3564.c new file +54 -0
+			return -EINVAL;
+
+		if (adc->calib_scale == val)
+			return ret;
+
+		mutex_lock(&adc->lock);
+		ret = mcp3564_write_24bits(adc, MCP3564_GAINCAL, val);
+		if (!ret)
+			adc->calib_scale = val;
+		mutex_unlock(&adc->lock);
+		return ret;
+	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
+		if (val < 0)
+			return -EINVAL;
+
+		tmp = find_closest(val, mcp3564_oversampling_avail,
+				   ARRAY_SIZE(mcp3564_oversampling_avail));
+
+		if (adc->oversampling == tmp)
+			return ret;
+
+		mutex_lock(&adc->lock);
+		ret = mcp3564_update_8bits(adc, MCP3564_CONFIG1, MCP3564_OVERSPL_RATIO_MASK,
+					   MCP3564_OVERSAMPLING_RATIO_SET(adc->oversampling));
+		if (!ret)
+			adc->oversampling = tmp;
+		mutex_unlock(&adc->lock);
+		return ret;
+	case IIO_CHAN_INFO_SCALE:
+		for (hwgain = 0; hwgain < MCP3564_MAX_PGA; hwgain++) {
+			if (val == adc->scale_tbls[hwgain][0] &&
+			    val2 == adc->scale_tbls[hwgain][1])
+				break;
+		}
+
+		if (hwgain == MCP3564_MAX_PGA)
+			return -EINVAL;
+
+		if (hwgain == adc->hwgain)
+			return ret;
+
+		mutex_lock(&adc->lock);
+		/* Update GAIN in CONFIG2[5:3] -> GAIN[2:0]*/
+		ret = mcp3564_update_8bits(adc, MCP3564_CONFIG2, MCP3564_HARDWARE_GAIN_MASK,
+					   MCP3564_HARDWARE_GAIN_SET(hwgain));
+		if (!ret)
+			adc->hwgain = hwgain;
+
+		mutex_unlock(&adc->lock);
+		return ret;
+	default:
+		return -EINVAL;
+	}
+}

...

Quoted 91 lines
drivers/iio/adc/mcp3564.c new file +91 -0
+static int mcp3564_config(struct iio_dev *indio_dev)
+{
+	struct mcp3564_state *adc = iio_priv(indio_dev);
+	struct device *dev = &adc->spi->dev;
+	const struct spi_device_id *dev_id;
+	u8 tmp_reg;
+	u16 tmp_u16;
+	enum mcp3564_ids ids;
+	int ret = 0;
+	unsigned int tmp = 0x01;
+	bool err = true;
+
+	/*
+	 * The address is set on a per-device basis by fuses in the factory,
+	 * configured on request. If not requested, the fuses are set for 0x1.
+	 * The device address is part of the device markings to avoid
+	 * potential confusion. This address is coded on two bits, so four possible
+	 * addresses are available when multiple devices are present on the same
+	 * SPI bus with only one Chip Select line for all devices.
+	 */
+	device_property_read_u32(dev, "microchip,hw-device-address", &tmp);
+
+	if (tmp > 3) {
+		dev_err_probe(dev, tmp,
+			      "invalid device address. Must be in range 0-3.\n");
+		return -EINVAL;
+	}
+
+	adc->dev_addr = MCP3564_HW_ADDR_MASK & tmp;
+
+	dev_dbg(dev, "use device address %i\n", adc->dev_addr);
+
+	ret = mcp3564_read_8bits(adc, MCP3564_RESERVED_C, &tmp_reg);
+	if (ret < 0)
+		return ret;
+
+	switch (tmp_reg) {
+	case MCP3564_REG_C_DEFAULT:
+		adc->has_vref = false;
+		break;
+	case MCP3564R_REG_C_DEFAULT:
+		adc->has_vref = true;
+		break;
+	default:
+		dev_info(dev, "Unknown chip found: %d\n", tmp_reg);
+		err = true;
+	}
+
+	if (!err) {
+		ret = mcp3564_read_16bits(adc, MCP3564_RESERVED_E, &tmp_u16);
+		if (ret < 0)
+			return ret;
+
+		switch (tmp_u16 & MCP3564_HW_ID_MASK) {
+		case MCP3461_HW_ID:
+			if (adc->has_vref)
+				ids = mcp3461r;
+			else
+				ids = mcp3461;
+			break;
+		case MCP3462_HW_ID:
+			if (adc->has_vref)
+				ids = mcp3462r;
+			else
+				ids = mcp3462;
+			break;
+		case MCP3464_HW_ID:
+			if (adc->has_vref)
+				ids = mcp3464r;
+			else
+				ids = mcp3464;
+			break;
+		case MCP3561_HW_ID:
+			if (adc->has_vref)
+				ids = mcp3561r;
+			else
+				ids = mcp3561;
+			break;
+		case MCP3562_HW_ID:
+			if (adc->has_vref)
+				ids = mcp3562r;
+			else
+				ids = mcp3562;
+			break;
+		case MCP3564_HW_ID:
+			if (adc->has_vref)
+				ids = mcp3564r;
+			else
+				ids = mcp3564;
+			break;
+		default:

drivers/iio/adc/mcp3564.c line 1242

Quoted 10 lines
drivers/iio/adc/mcp3564.c new file +10 -0
+			dev_info(dev, "Unknown chip found: %d\n", tmp_u16);
+			err = true;
+		}
+	}
+
+	if (err) {
+		/*
+		 * If failed to identify the hardware based on internal registers,
+		 * try using fallback compatible in device tree to deal with some newer part number.
+		 */

Can we use spi_get_device_match_data() here?

Quoted 10 lines
drivers/iio/adc/mcp3564.c new file +10 -0
+		adc->chip_info = device_get_match_data(&adc->spi->dev);
+		if (!adc->chip_info) {
+			dev_id = spi_get_device_id(adc->spi);
+			adc->chip_info = (const struct mcp3564_chip_info *)dev_id->driver_data;
+		}
+
+		adc->has_vref = adc->chip_info->has_vref;
+	} else {
+		adc->chip_info = &mcp3564_chip_infos_tbl[ids];
+	}

drivers/iio/adc/mcp3564.c line 1261

Quoted 10 lines
drivers/iio/adc/mcp3564.c new file +9 -0
+
+	dev_dbg(dev, "Found %s chip\n", adc->chip_info->name);
+
+	adc->vref = devm_regulator_get_optional(dev, "vref");
 
+	if (IS_ERR(adc->vref)) {
+		if (PTR_ERR(adc->vref) == -ENODEV) {
+			/* Check if chip has internal vref */
+			if (!adc->has_vref)
+				return dev_err_probe(dev, -EINVAL,

ERR_PTR(adc->vref) would be better as might tell us something more useful.

Quoted 47 lines
drivers/iio/adc/mcp3564.c new file +47 -0
+					     "Unknown Vref\n");
+			adc->vref = NULL;
+			dev_dbg(dev, "%s: Using internal Vref\n", __func__);
+		} else {
+			return dev_err_probe(dev, PTR_ERR(adc->vref),
+					     "failed to get regulator\n");
+		}
+	} else {
+		ret = regulator_enable(adc->vref);
+		if (ret)
+			return ret;
+
+		ret = devm_add_action_or_reset(dev, mcp3564_disable_reg,
+					       adc->vref);
+		if (ret)
+			return ret;
+
+		dev_dbg(dev, "%s: Using External Vref\n", __func__);
+
+		ret = regulator_get_voltage(adc->vref);
+		if (ret < 0) {
+			return dev_err_probe(dev, ret,
+					     "Failed to read vref regulator\n");
+		}
+
+		adc->vref_mv = ret / MILLI;
+	}
+
+	ret = mcp3564_parse_fw_children(indio_dev);
+	if (ret)
+		return ret;
+
+	/*
+	 * Command sequence that ensures a recovery with the desired settings
+	 * in any cases of loss-of-power scenario (Full Chip Reset):
+	 *  - Write LOCK register to 0xA5
+	 *  - Write IRQ register to 0x03
+	 *  - Send "Device Full Reset" fast command
+	 *  - Wait 1ms for "Full Reset" to complete
+	 */
+	ret = mcp3564_write_8bits(adc, MCP3564_LOCK, MCP3564_LOCK_WRITE_ACCESS_PASSWORD);
+	if (ret)
+		return ret;
+
+	ret = mcp3564_write_8bits(adc, MCP3564_IRQ, 0x03);
+	if (ret)
+		return ret;

drivers/iio/adc/mcp3564.c line 1318

Quoted 10 lines
drivers/iio/adc/mcp3564.c new file +10 -0
+
+	ret = mcp3564_fast_cmd(adc, MCP3564_FASTCMD_RESET);
+	if (ret)
+		return ret;
+
+	mdelay(1);
+
+	/* set a gain of 1x for GAINCAL */
+	ret = mcp3564_write_24bits(adc, MCP3564_GAINCAL,
+				   MCP3564_GAINCAL_SET(MCP3564_DEFAULT_GAINCAL));

It's the default register value - I wouldn't expect it to be masked via the set function (it's not the default of just that field).

Quoted 75 lines
drivers/iio/adc/mcp3564.c new file +75 -0
+	if (ret)
+		return ret;
+
+	adc->calib_scale = MCP3564_GAINCAL_SET(MCP3564_DEFAULT_GAINCAL);
+
+	ret = mcp3564_write_24bits(adc, MCP3564_OFFSETCAL,
+				   MCP3564_OFFSETCAL_SET(MCP3564_DEFAULT_OFFSETCAL));
+	if (ret)
+		return ret;
+
+	ret = mcp3564_write_24bits(adc, MCP3564_TIMER,
+				   MCP3564_TIMER_SET(MCP3564_TIMER_DEFAULT_VALUE));
+	if (ret)
+		return ret;
+
+	ret = mcp3564_write_24bits(adc, MCP3564_SCAN,
+				   MCP3564_DELAY_TIME_SET(MCP3564_NO_DELAY) |
+				   MCP3564_SCAN_CH_SEL_SET(MCP3564_SCAN_DEFAULT_VALUE));
+	if (ret)
+		return ret;
+
+	ret = mcp3564_write_8bits(adc, MCP3564_MUX, MCP3564_MUX_SET(MCP3564_CH0, MCP3564_CH1));
+	if (ret)
+		return ret;
+
+	ret = mcp3564_write_8bits(adc, MCP3564_IRQ, MCP3464_EN_FASTCMD | MCP3464_EN_STP);
+	if (ret)
+		return ret;
+
+	tmp_reg = MCP3564_CONV_MODE_SET(MCP3464_CONV_MODE_ONE_SHOT_STANDBY);
+	tmp_reg |= MCP3564_DATA_FORMAT_SET(MCP3456_DATA_FORMAT_32B_SGN_EXT);
+	tmp_reg |= MCP3464_EN_OFFCAL;
+	tmp_reg |= MCP3464_EN_GAINCAL;
+
+	ret = mcp3564_write_8bits(adc, MCP3564_CONFIG3, tmp_reg);
+	if (ret)
+		return ret;
+
+	tmp_reg = MCP3564_BOOST_CURRENT_SET(MCP3564_BOOST_CURRENT_x1_00);
+	tmp_reg |= MCP3564_HARDWARE_GAIN_SET(0x01);
+	tmp_reg |= MCP3564_AZ_MUX_SET(1);
+
+	ret = mcp3564_write_8bits(adc, MCP3564_CONFIG2, tmp_reg);
+	if (ret)
+		return ret;
+
+	adc->hwgain = 0x01;
+	adc->auto_zeroing_mux = true;
+	adc->auto_zeroing_ref = false;
+	adc->current_boost_mode = MCP3564_BOOST_CURRENT_x1_00;
+
+	tmp_reg = MCP3564_OVERSAMPLING_RATIO_SET(MCP3564_OVERSAMPLING_RATIO_98304);
+
+	ret = mcp3564_write_8bits(adc, MCP3564_CONFIG1, tmp_reg);
+	if (ret)
+		return ret;
+
+	adc->oversampling = MCP3564_OVERSAMPLING_RATIO_98304;
+
+	tmp_reg = MCP3564_ADC_MODE_MASK_SET(MCP3564_ADC_MODE_STANDBY);
+	tmp_reg |= MCP3564_CS_SEL_SET(MCP3564_CS_SEL_0_0_uA);
+	tmp_reg |= MCP3564_CLK_SEL_SET(MCP3564_USE_INTERNAL_CLK);
+	tmp_reg |= MCP3456_CONFIG0_BIT6_DEFAULT;
+
+	if (!adc->vref) {
+		tmp_reg |= MCP3564_VREF_SET(1);
+		adc->vref_mv = MCP3564R_INT_VREF_MV;
+	}
+
+	ret = mcp3564_write_8bits(adc, MCP3564_CONFIG0, tmp_reg);
+
+	adc->burnout_mode = MCP3564_CS_SEL_0_0_uA;
+
+	return ret;
+}

...

Quoted 24 lines
drivers/iio/adc/mcp3564.c new file +24 -0
+
+static const struct of_device_id mcp3564_dt_ids[] = {
+	{ .compatible = "microchip,mcp3461", .data = &mcp3564_chip_infos_tbl[mcp3461] },
+	{ .compatible = "microchip,mcp3462", .data = &mcp3564_chip_infos_tbl[mcp3462] },
+	{ .compatible = "microchip,mcp3464", .data = &mcp3564_chip_infos_tbl[mcp3464] },
+	{ .compatible = "microchip,mcp3561", .data = &mcp3564_chip_infos_tbl[mcp3561] },
+	{ .compatible = "microchip,mcp3562", .data = &mcp3564_chip_infos_tbl[mcp3562] },
+	{ .compatible = "microchip,mcp3564", .data = &mcp3564_chip_infos_tbl[mcp3564] },
+	{ .compatible = "microchip,mcp3461r", .data = &mcp3564_chip_infos_tbl[mcp3461r] },
+	{ .compatible = "microchip,mcp3462r", .data = &mcp3564_chip_infos_tbl[mcp3462r] },
+	{ .compatible = "microchip,mcp3464r", .data = &mcp3564_chip_infos_tbl[mcp3464r] },
+	{ .compatible = "microchip,mcp3561r", .data = &mcp3564_chip_infos_tbl[mcp3561r] },
+	{ .compatible = "microchip,mcp3562r", .data = &mcp3564_chip_infos_tbl[mcp3562r] },
+	{ .compatible = "microchip,mcp3564r", .data = &mcp3564_chip_infos_tbl[mcp3564r] },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, mcp3564_dt_ids);
+
+static const struct spi_device_id mcp3564_id[] = {
+	{ "mcp3461", (kernel_ulong_t)&mcp3564_chip_infos_tbl[mcp3461] },
+	{ "mcp3462", (kernel_ulong_t)&mcp3564_chip_infos_tbl[mcp3462] },
+	{ "mcp3464", (kernel_ulong_t)&mcp3564_chip_infos_tbl[mcp3464] },
+	{ "mcp3561", (kernel_ulong_t)&mcp3564_chip_infos_tbl[mcp3561] },
+	{ "mcp3562", (kernel_ulong_t)&mcp3564_chip_infos_tbl[mcp3562] },

drivers/iio/adc/mcp3564.c line 1525

Quoted 10 lines
drivers/iio/adc/mcp3564.c new file +10 -0
+	{ "mcp3564", (kernel_ulong_t)&mcp3564_chip_infos_tbl[mcp3564] },
+	{ "mcp3461r", (kernel_ulong_t)&mcp3564_chip_infos_tbl[mcp3461r] },
+	{ "mcp3462r", (kernel_ulong_t)&mcp3564_chip_infos_tbl[mcp3462r] },
+	{ "mcp3464r", (kernel_ulong_t)&mcp3564_chip_infos_tbl[mcp3464r] },
+	{ "mcp3561r", (kernel_ulong_t)&mcp3564_chip_infos_tbl[mcp3561r] },
+	{ "mcp3562r", (kernel_ulong_t)&mcp3564_chip_infos_tbl[mcp3562r] },
+	{ "mcp3564r", (kernel_ulong_t)&mcp3564_chip_infos_tbl[mcp3564r] },
+	{ }
+};
+

No blank line here (for consistency with above)

Quoted 1 line

+MODULE_DEVICE_TABLE(spi, mcp3564_id);

author maintainer
#5 lore raw

Re: [PATCH v3 2/2] iio: adc: adding support for MCP3564 ADC

Hi Jonathan,

Quoted 10 lines
Quoted 10 lines
(quoted diff) +1 -0
@@ -0,0 +1,53 @@
+What:               

/sys/bus/iio/devices/iio:deviceX/boost_current

Documentation/ABI/testing/sysfs-bus-iio-adc-mcp3564 new file +4 -0
+KernelVersion:       6.4
+Contact:     [email protected]
+Description:
+             This attribute is used to set the biasing circuit of

the
+             Delta-Sigma modulator. The different BOOST settings

Quoted 9 lines
Quoted 4 lines

are applied
+             to the entire modulator circuit, including the voltage reference
+             buffers.

Units?  Should be  mA given that is what we use for other current attributes
in IIO.

This should have no units because is a "gain" for the bias current. I think it will be better to change the name of the attribute to "boost_current_gain".

Quoted 7 lines

This one may cause us problems with generality of the description if we reuse
if for other devices (and hence move it to the more general files), but we
can figure this out at the time.

Quoted 1 line

Thanks and Best Regards,
Marius

maintainer
#6 lore raw

Re: [PATCH v3 2/2] iio: adc: adding support for MCP3564 ADC

Quoted 22 lines · On Fri, 18 Aug 2023 16:29:26 +0000 <[email protected]> wrote:

Hi Jonathan,

Quoted 10 lines
Quoted 10 lines
(quoted diff) +1 -0
@@ -0,0 +1,53 @@
+What:               

/sys/bus/iio/devices/iio:deviceX/boost_current

Documentation/ABI/testing/sysfs-bus-iio-adc-mcp3564 new file +4 -0
+KernelVersion:       6.4
+Contact:     [email protected]
+Description:
+             This attribute is used to set the biasing circuit of

the
+             Delta-Sigma modulator. The different BOOST settings

Quoted 9 lines
Quoted 4 lines

are applied
+             to the entire modulator circuit, including the voltage reference
+             buffers.

Units?  Should be  mA given that is what we use for other current attributes
in IIO.

Quoted 3 lines

This should have no units because is a "gain" for the bias current. I think it will be better to change the name of the attribute to "boost_current_gain".

That's sounds reasonable to me as well.

Quoted 13 lines
Quoted 9 lines

This one may cause us problems with generality of the description if we reuse
if for other devices (and hence move it to the more general files), but we
can figure this out at the time.

Quoted 1 line

Thanks and Best Regards,
Marius