iio: adc: add support for ADE9000 Energy Monitoring IC
Antoniu Miclaus
maintainerv3 cover iio: adc: add support for ADE9000 Energy Monitoring IC
This patch series adds support for the Analog Devices ADE9000, a highly accurate, fully integrated, multiphase energy and power quality monitoring device. The ADE9000 is capable of measuring energy consumption and power quality parameters in industrial and commercial applications.
The series includes:
1. New IIO modifiers for power and energy measurement devices, including support for active/reactive/apparent power, RMS masurements.
2. Device tree bindings for the ADE9000, supporting waveform buffer configuration, phase configuration, and trigger settings.
3. Complete driver implementation supporting:
- Multi-phase energy measurement (3-phase support)
- Power quality monitoring (voltage swell/dip detection)
- Waveform buffer capture with configurable triggering
- Energy accumulation with configurable time windows
- IIO buffer interface for continuous data streaming
- Event-based notifications for power quality events
The driver provides a comprehensive interface for energy monitoring applications through the IIO framework, enabling userspace applications to monitor power consumption, quality, and waveform data.
The driver will be extended in the future to support multiple parts such as ade9039.
Antoniu Miclaus (4):
iio: add power and energy measurement modifiers
dt-bindings: iio: adc: add ade9000
iio: adc: add ade9000 support
Documentation: ABI: iio: add sinc4+lp
Documentation/ABI/testing/sysfs-bus-iio | 12 + .../bindings/iio/adc/adi,ade9000.yaml | 110 + drivers/iio/adc/Kconfig | 19 + drivers/iio/adc/Makefile | 2 + drivers/iio/adc/ade9000.c | 2033 +++++++++++++++++ drivers/iio/industrialio-core.c | 5 + include/linux/iio/types.h | 1 + include/uapi/linux/iio/types.h | 4 + 8 files changed, 2186 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/adc/adi,ade9000.yaml create mode 100644 drivers/iio/adc/ade9000.c
Signature · 1 line
2.43.0
Antoniu Miclaus
maintainerPATCH v3 1/4 iio: add power and energy measurement modifiers
Add new IIO modifiers to support power and energy measurement devices:
Power modifiers:
- IIO_MOD_ACTIVE: Real power consumed by the load
- IIO_MOD_REACTIVE: Power that oscillates between source and load
- IIO_MOD_APPARENT: Magnitude of complex power
- IIO_MOD_FUND_REACTIVE: Reactive power at fundamental frequency
- IIO_MOD_FACTOR: Power factor (ratio of active to apparent power)
Signal quality modifiers:
- IIO_MOD_RMS: Root Mean Square value
These modifiers enable proper representation of power measurement devices like energy meters and power analyzers.
---
changes in v3:
- remove accumulated energy modifiers
- rename power_factor to powerfactor for consistency
Documentation/ABI/testing/sysfs-bus-iio | 11 +++++++++++ drivers/iio/industrialio-core.c | 5 +++++ include/linux/iio/types.h | 1 + include/uapi/linux/iio/types.h | 4 ++++ 4 files changed, 21 insertions(+)
Documentation/ABI/testing/sysfs-bus-iio +11 -0
@@ -167,7 +167,12 @@ Description: is required is a consistent labeling. Units after application of scale and offset are millivolts. +What: /sys/bus/iio/devices/iio:deviceX/in_altvoltageY_rms_raw + What: /sys/bus/iio/devices/iio:deviceX/in_powerY_raw +What: /sys/bus/iio/devices/iio:deviceX/in_powerY_active_raw +What: /sys/bus/iio/devices/iio:deviceX/in_powerY_reactive_raw +What: /sys/bus/iio/devices/iio:deviceX/in_powerY_apparent_raw KernelVersion: 4.5 Contact: [email protected] Description:
@@ -176,6 +181,8 @@ Description: unique to allow association with event codes. Units after application of scale and offset are milliwatts. +What: /sys/bus/iio/devices/iio:deviceX/in_powerY_powerfactor + What: /sys/bus/iio/devices/iio:deviceX/in_capacitanceY_raw KernelVersion: 3.2 Contact: [email protected]
@@ -1569,6 +1576,9 @@ Description: What: /sys/.../iio:deviceX/in_energy_input What: /sys/.../iio:deviceX/in_energy_raw +What: /sys/.../iio:deviceX/in_energyY_active_raw +What: /sys/.../iio:deviceX/in_energyY_reactive_raw +What: /sys/.../iio:deviceX/in_energyY_apparent_raw KernelVersion: 4.0 Contact: [email protected] Description:
@@ -1692,6 +1702,7 @@ Description: What: /sys/bus/iio/devices/iio:deviceX/in_currentY_raw What: /sys/bus/iio/devices/iio:deviceX/in_currentY_supply_raw +What: /sys/bus/iio/devices/iio:deviceX/in_currentY_rms_raw KernelVersion: 3.17 Contact: [email protected] Description:
drivers/iio/industrialio-core.c +5 -0
@@ -152,6 +152,10 @@ static const char * const iio_modifier_names[] = { [IIO_MOD_PITCH] = "pitch", [IIO_MOD_YAW] = "yaw", [IIO_MOD_ROLL] = "roll", + [IIO_MOD_RMS] = "rms", + [IIO_MOD_ACTIVE] = "active", + [IIO_MOD_REACTIVE] = "reactive", + [IIO_MOD_APPARENT] = "apparent", };/* relies on pairs of these shared then separate */
@@ -189,6 +193,7 @@ static const char * const iio_chan_info_postfix[] = { [IIO_CHAN_INFO_ZEROPOINT] = "zeropoint", [IIO_CHAN_INFO_TROUGH] = "trough_raw", [IIO_CHAN_INFO_CONVDELAY] = "convdelay", + [IIO_CHAN_INFO_POWERFACTOR] = "powerfactor", };/** * iio_device_id() - query the unique ID for the device
include/linux/iio/types.h +1 -0
@@ -70,6 +70,7 @@ enum iio_chan_info_enum {IIO_CHAN_INFO_ZEROPOINT ,IIO_CHAN_INFO_TROUGH ,IIO_CHAN_INFO_CONVDELAY , + IIO_CHAN_INFO_POWERFACTOR, };#endif /* _IIO_TYPES_H_ */
include/uapi/linux/iio/types.h +4 -0
@@ -108,6 +108,10 @@ enum iio_modifier {IIO_MOD_ROLL ,IIO_MOD_LIGHT_UVA ,IIO_MOD_LIGHT_UVB , +IIO_MOD_RMS , +IIO_MOD_ACTIVE , +IIO_MOD_REACTIVE , + IIO_MOD_APPARENT, }; enum iio_event_type {
Signature · 1 line
2.43.0
David Lechner
reviewerRe: [PATCH v3 1/4] iio: add power and energy measurement modifiers
Quoted 31 lines · Antoniu Miclaus
Add new IIO modifiers to support power and energy measurement devices:
Power modifiers:
-IIO_MOD_ACTIVE: Real power consumed by the load
-IIO_MOD_REACTIVE: Power that oscillates between source and load
-IIO_MOD_APPARENT: Magnitude of complex power
-IIO_MOD_FUND_REACTIVE: Reactive power at fundamental frequency
-IIO_MOD_FACTOR: Power factor (ratio of active to apparent power)Signal quality modifiers:
-IIO_MOD_RMS: Root Mean Square valueThese modifiers enable proper representation of power measurement devices like energy meters and power analyzers.
Signed-off-by Antoniu Miclaus <[email protected]>---
changes in v3:
- remove accumulated energy modifiers
- renamepower_factorto powerfactor for consistencyDocumentation/ABI/testing/sysfs-bus-iio | 11 +++++++++++ drivers/iio/industrialio-core.c | 5 +++++ include/linux/iio/types.h | 1 + include/uapi/linux/iio/types.h | 4 ++++ 4 files changed, 21 insertions(+)Documentation/ABI/testing/sysfs-bus-iio +0
-0
Documentation/ABI/testing/sysfs-bus-iio Description: · line 170
Quoted 4 lines
Documentation/ABI/testing/sysfs-bus-iio +1
-0@@ -167,7 +167,12 @@ Description: is required is a consistent labeling. Units after application of scale and offset are millivolts. +What: /sys/bus/iio/devices/iio:deviceX/in_altvoltageY_rms_raw
This looks out of place. There is no existing in_altvoltage_raw, so we need to start a new section.
Documentation/ABI/testing/sysfs-bus-iio Description: · line 184
Quoted 13 lines
Documentation/ABI/testing/sysfs-bus-iio +5
-0+ What: /sys/bus/iio/devices/iio:deviceX/in_powerY_raw +What: /sys/bus/iio/devices/iio:deviceX/in_powerY_active_raw +What: /sys/bus/iio/devices/iio:deviceX/in_powerY_reactive_raw +What: /sys/bus/iio/devices/iio:deviceX/in_powerY_apparent_raw KernelVersion: 4.5 Contact: [email protected] Description:@@ -176,6 +181,8 @@ Description: unique to allow association with event codes. Units after application of scale and offset are milliwatts. +What: /sys/bus/iio/devices/iio:deviceX/in_powerY_powerfactor
This one also needs a new section with Description: etc.
Quoted 15 lines
Documentation/ABI/testing/sysfs-bus-iio +4
-0+ What: /sys/bus/iio/devices/iio:deviceX/in_capacitanceY_raw KernelVersion: 3.2 Contact: [email protected]@@ -1569,6 +1576,9 @@ Description: What: /sys/.../iio:deviceX/in_energy_input What: /sys/.../iio:deviceX/in_energy_raw +What: /sys/.../iio:deviceX/in_energyY_active_raw +What: /sys/.../iio:deviceX/in_energyY_reactive_raw +What: /sys/.../iio:deviceX/in_energyY_apparent_raw KernelVersion: 4.0 Contact: [email protected] Description:
Documentation/ABI/testing/sysfs-bus-iio Description: · line 1705
Quoted 4 lines
Documentation/ABI/testing/sysfs-bus-iio +1
-0@@ -1692,6 +1702,7 @@ Description: What: /sys/bus/iio/devices/iio:deviceX/in_currentY_raw What: /sys/bus/iio/devices/iio:deviceX/in_currentY_supply_raw +What: /sys/bus/iio/devices/iio:deviceX/in_currentY_rms_raw
Shouldn't this be in_altcurrentY_rms_raw? in_currentY_raw is DC current, not AC. And we will likely need IIO_ALTCURRENT to go with it.
Quoted 3 lines
Documentation/ABI/testing/sysfs-bus-iio +0
-0KernelVersion: 3.17 Contact: [email protected] Description:
Antoniu Miclaus
maintainerPATCH v3 2/4 dt-bindings: iio: adc: add ade9000
In reply to Antoniu MiclausAdd devicetree bindings support for ade9000.
---
changes in v3:
- fix $id schema URL format
- remove address/size-cells and channel subnodes
- add dready interrupt support
- add clock input/output support
- simplify device tree structure
.../bindings/iio/adc/adi,ade9000.yaml | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/adc/adi,ade9000.yaml
Documentation/devicetree/bindings/iio/adc/adi,ade9000.yaml new file +110 -0
@@ -0,0 +1,110 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +# Copyright 2025 Analog Devices Inc. +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/adc/adi,ade9000.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Analog Devices ADE9000 High Performance, Polyphase Energy Metering driver + +maintainers: + - Antoniu Miclaus <[email protected]> + +description: | + The ADE9000 s a highly accurate, fully integrated, multiphase energy and power + quality monitoring device. Superior analog performance and a digital signal + processing (DSP) core enable accurate energy monitoring over a wide dynamic + range. An integrated high end reference ensures low drift over temperature + with a combined drift of less than ±25 ppm/°C maximum for the entire channel + including a programmable gain amplifier (PGA) and an analog-to- digital + converter (ADC). + + https://www.analog.com/media/en/technical-documentation/data-sheets/ADE9000.pdf + +$ref: /schemas/spi/spi-peripheral-props.yaml# + +properties: + compatible: + enum: + - adi,ade9000 + + reg: + maxItems: 1 + + spi-max-frequency: + maximum: 20000000 + + interrupts: + maxItems: 3 + + interrupt-names: + items: + - const: irq0 + - const: irq1 + - const: dready + + reset-gpios: + description: | + Must be the device tree identifier of the RESET pin. As the line is + active low, it should be marked GPIO_ACTIVE_LOW. + maxItems: 1 + + vdd-supply: true + + vref-supply: true + + clocks: + description: External clock source when not using crystal + maxItems: 1 + + clock-names: + items: + - const: clkin + + "#clock-cells": + description: ADE9000 can provide clock output via CLKOUT pin with external buffer + const: 0 + + clock-output-names: + items: + - const: clkout + +required: + - compatible + - reg + - reset-gpios + - interrupts + - interrupt-names + - vdd-supply + +allOf: + - $ref: /schemas/spi/spi-peripheral-props.yaml# + +unevaluatedProperties: false + +examples: + - | + #include <dt-bindings/gpio/gpio.h> + #include <dt-bindings/interrupt-controller/irq.h> + + spi { + #address-cells = <1>; + #size-cells = <0>; + + adc@0 { + compatible = "adi,ade9000"; + reg = <0>; + spi-max-frequency = <7000000>; + + #clock-cells = <0>; + reset-gpios = <&gpio 4 GPIO_ACTIVE_LOW>; + interrupts = <2 IRQ_TYPE_EDGE_FALLING>, <3 IRQ_TYPE_EDGE_FALLING>, <4 IRQ_TYPE_EDGE_FALLING>; + interrupt-names = "irq0", "irq1", "dready"; + interrupt-parent = <&gpio>; + /* Optional: external clock instead of crystal */ + /* clocks = <&ext_clock_24576khz>; */ + /* clock-names = "clkin"; */ + clock-output-names = "clkout"; + vdd-supply = <&vdd_reg>; + }; + };
Signature · 1 line
2.43.0
Re: [PATCH v3 2/4] dt-bindings: iio: adc: add ade9000
Hi Antoniu,
Didn't manage to a full review of the bindings as this has many features. Though, I've noticed one small nit below.
Quoted 45 lines · Antoniu Miclaus
Add devicetree bindings support for ade9000.
Signed-off-by Antoniu Miclaus <[email protected]>---
changes in v3:
- fix $id schema URL format
- remove address/size-cells and channel subnodes
- add dready interrupt support
- add clock input/output support
- simplify device tree structure.../bindings/iio/adc/adi,ade9000.yaml | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/adc/adi,ade9000.yamlDocumentation/devicetree/bindings/iio/adc/adi,ade9000.yaml new file +25
-0@@ -0,0 +1,110 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +# Copyright 2025 Analog Devices Inc. +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/adc/adi,ade9000.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Analog Devices ADE9000 High Performance, Polyphase Energy Metering driver + +maintainers: + - Antoniu Miclaus <[email protected]> + +description: | + The ADE9000 s a highly accurate, fully integrated, multiphase energy and power + quality monitoring device. Superior analog performance and a digital signal + processing (DSP) core enable accurate energy monitoring over a wide dynamic + range. An integrated high end reference ensures low drift over temperature + with a combined drift of less than ±25 ppm/°C maximum for the entire channel + including a programmable gain amplifier (PGA) and an analog-to- digital + converter (ADC). + + https://www.analog.com/media/en/technical-documentation/data-sheets/ADE9000.pdf + +$ref: /schemas/spi/spi-peripheral-props.yaml# +
...
Documentation/devicetree/bindings/iio/adc/adi,ade9000.yaml line 81
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/adi,ade9000.yaml new file +10
-0+required: + - compatible + - reg + - reset-gpios + - interrupts + - interrupt-names + - vdd-supply + +allOf: + - $ref: /schemas/spi/spi-peripheral-props.yaml#
spi-peripheral-props is alread referenced right after the dt-binding general description. Is it needed to also reference it here?
Rob Herring
maintainerRe: [PATCH v3 2/4] dt-bindings: iio: adc: add ade9000
In reply to Antoniu MiclausQuoted 57 lines · Antoniu Miclaus
Add devicetree bindings support for ade9000.
Signed-off-by Antoniu Miclaus <[email protected]>---
changes in v3:
- fix $id schema URL format
- remove address/size-cells and channel subnodes
- add dready interrupt support
- add clock input/output support
- simplify device tree structure.../bindings/iio/adc/adi,ade9000.yaml | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/adc/adi,ade9000.yamlDocumentation/devicetree/bindings/iio/adc/adi,ade9000.yaml new file +37
-0@@ -0,0 +1,110 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +# Copyright 2025 Analog Devices Inc. +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/adc/adi,ade9000.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Analog Devices ADE9000 High Performance, Polyphase Energy Metering driver + +maintainers: + - Antoniu Miclaus <[email protected]> + +description: | + The ADE9000 s a highly accurate, fully integrated, multiphase energy and power + quality monitoring device. Superior analog performance and a digital signal + processing (DSP) core enable accurate energy monitoring over a wide dynamic + range. An integrated high end reference ensures low drift over temperature + with a combined drift of less than ±25 ppm/°C maximum for the entire channel + including a programmable gain amplifier (PGA) and an analog-to- digital + converter (ADC). + + https://www.analog.com/media/en/technical-documentation/data-sheets/ADE9000.pdf + +$ref: /schemas/spi/spi-peripheral-props.yaml# + +properties: + compatible: + enum: + - adi,ade9000 + + reg: + maxItems: 1 + + spi-max-frequency: + maximum: 20000000 + + interrupts:
Documentation/devicetree/bindings/iio/adc/adi,ade9000.yaml line 47
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/adi,ade9000.yaml new file +10
-0+ maxItems: 3 + + interrupt-names: + items: + - const: irq0 + - const: irq1 + - const: dready + + reset-gpios: + description: |
Don't need '|'.
Quoted 8 lines
Documentation/devicetree/bindings/iio/adc/adi,ade9000.yaml new file +8
-0+ Must be the device tree identifier of the RESET pin. As the line is + active low, it should be marked GPIO_ACTIVE_LOW. + maxItems: 1 + + vdd-supply: true + + vref-supply: true +
Documentation/devicetree/bindings/iio/adc/adi,ade9000.yaml line 65
Quoted 10 lines
Documentation/devicetree/bindings/iio/adc/adi,ade9000.yaml new file +10
-0+ clocks: + description: External clock source when not using crystal + maxItems: 1 + + clock-names: + items: + - const: clkin + + "#clock-cells": + description: ADE9000 can provide clock output via CLKOUT pin with external buffer
Wrap at 80 char. Or start on the next line.
Quoted 48 lines
Documentation/devicetree/bindings/iio/adc/adi,ade9000.yaml new file +45
-0+ const: 0 + + clock-output-names: + items: + - const: clkout + +required: + - compatible + - reg + - reset-gpios + - interrupts + - interrupt-names + - vdd-supply + +allOf: + - $ref: /schemas/spi/spi-peripheral-props.yaml# + +unevaluatedProperties: false + +examples: + - | + #include <dt-bindings/gpio/gpio.h> + #include <dt-bindings/interrupt-controller/irq.h> + + spi { + #address-cells = <1>; + #size-cells = <0>; + + adc@0 { + compatible = "adi,ade9000"; + reg = <0>; + spi-max-frequency = <7000000>; + + #clock-cells = <0>; + reset-gpios = <&gpio 4 GPIO_ACTIVE_LOW>; + interrupts = <2 IRQ_TYPE_EDGE_FALLING>, <3 IRQ_TYPE_EDGE_FALLING>, <4 IRQ_TYPE_EDGE_FALLING>; + interrupt-names = "irq0", "irq1", "dready"; + interrupt-parent = <&gpio>; + /* Optional: external clock instead of crystal */ + /* clocks = <&ext_clock_24576khz>; */ + /* clock-names = "clkin"; */ + clock-output-names = "clkout"; + vdd-supply = <&vdd_reg>; + }; + };Signature · 1 line
2.43.0
Antoniu Miclaus
maintainerPATCH v3 3/4 iio: adc: add ade9000 support
In reply to Antoniu MiclausAdd driver support for the ade9000. highly accurate, fully integrated, multiphase energy and power quality monitoring device.
---
changes in v3:
- add clk support for external clock input/output with clkout provider
- add dready interrupt handler and support
- use dev_err_probe for consistent error handling throughout
- simplify probe function and remove unnecessary channel setup
- update filter type from "sinc4+iir" to "sinc4+lp"
- remove dynamic channel setup and use static channel array
- simplify waveform buffer configuration (remove wf_mode field)
- add minmax.h and clk headers
- add waveform buffer configuration enum
- major code restructuring and cleanup
- remove ade9000_setup_iio_channels function
- update regulator handling for vref
- simplify SPI driver structure and formatting
- numerous register value updates and optimizations
drivers/iio/adc/Kconfig | 19 + drivers/iio/adc/Makefile | 2 + drivers/iio/adc/ade9000.c | 2033 +++++++++++++++++++++++++++++++++++++ 3 files changed, 2054 insertions(+) create mode 100644 drivers/iio/adc/ade9000.c
drivers/iio/adc/Kconfig +19 -0
@@ -507,6 +507,25 @@ config AD9467 To compile this driver as a module, choose M here: the module will be called ad9467. +config ADE9000 + tristate "Analog Devices ADE9000 Multiphase Energy, and Power Quality Monitoring IC Driver" + depends on SPI + select REGMAP_SPI + select IIO_BUFFER + select IIO_KFIFO_BUF + help + Say yes here to build support for the Analog Devices ADE9000, + a highly accurate, multiphase energy and power quality monitoring + integrated circuit. + + The device features high-precision analog-to-digital converters + and digital signal processing to compute RMS values, power factor, + frequency, and harmonic analysis. It supports SPI communication + and provides buffered data output through the IIO framework. + + To compile this driver as a module, choose M here: the module will + be called ade9000. + config ADI_AXI_ADC tristate "Analog Devices Generic AXI ADC IP core driver" depends on MICROBLAZE || NIOS2 || ARCH_ZYNQ || ARCH_ZYNQMP || ARCH_INTEL_SOCFPGA || COMPILE_TEST
drivers/iio/adc/Makefile +2 -0
@@ -20,6 +20,7 @@ obj-$(CONFIG_AD7091R5) += ad7091r5.o obj-$(CONFIG_AD7091R8) += ad7091r8.o obj-$(CONFIG_AD7124) += ad7124.o obj-$(CONFIG_AD7173) += ad7173.o +obj-$(CONFIG_ADE9000) += ade9000.o obj-$(CONFIG_AD7191) += ad7191.o obj-$(CONFIG_AD7192) += ad7192.o obj-$(CONFIG_AD7266) += ad7266.o
@@ -46,6 +47,7 @@ obj-$(CONFIG_AD7944) += ad7944.o obj-$(CONFIG_AD7949) += ad7949.o obj-$(CONFIG_AD799X) += ad799x.o obj-$(CONFIG_AD9467) += ad9467.o +obj-$(CONFIG_ADE9000) += ade9000.o obj-$(CONFIG_ADI_AXI_ADC) += adi-axi-adc.o obj-$(CONFIG_ASPEED_ADC) += aspeed_adc.o obj-$(CONFIG_AT91_ADC) += at91_adc.o
drivers/iio/adc/ade9000.c new file +2033 -0
2,033 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v3 3/4] iio: adc: add ade9000 support
Quoted 6 lines · Antoniu Miclaus
Add driver support for the ade9000. highly accurate, fully integrated, multiphase energy and power quality monitoring device.
Signed-off-by Antoniu Miclaus <[email protected]>---
Hi Antoniu,
Not an easy one. I guess it will take some iterations. Jumping in v3 so sorry if I'm asking something that was already discussed in previous versions.
Quoted 103 lines
changes in v3:
- add clk support for external clock input/output with clkout provider
- add dready interrupt handler and support
- usedev_err_probefor consistent error handling throughout
- simplify probe function and remove unnecessary channel setup
- update filter type from "sinc4+iir" to "sinc4+lp"
- remove dynamic channel setup and use static channel array
- simplify waveform buffer configuration (removewf_modefield)
- addminmax.hand clk headers
- add waveform buffer configuration enum
- major code restructuring and cleanup
- removeade9000_setup_iio_channelsfunction
- update regulator handling for vref
- simplify SPI driver structure and formatting
- numerous register value updates and optimizationsdrivers/iio/adc/Kconfig | 19 + drivers/iio/adc/Makefile | 2 + drivers/iio/adc/ade9000.c | 2033 +++++++++++++++++++++++++++++++++++++ 3 files changed, 2054 insertions(+) create mode 100644 drivers/iio/adc/ade9000.cdrivers/iio/adc/Kconfig +19
-0@@ -507,6 +507,25 @@ config AD9467 To compile this driver as a module, choose M here: the module will be called ad9467. +config ADE9000 + tristate "Analog Devices ADE9000 Multiphase Energy, and Power Quality Monitoring IC Driver" + depends on SPI + select REGMAP_SPI + select IIO_BUFFER + select IIO_KFIFO_BUF + help + Say yes here to build support for the Analog Devices ADE9000, + a highly accurate, multiphase energy and power quality monitoring + integrated circuit. + + The device features high-precision analog-to-digital converters + and digital signal processing to compute RMS values, power factor, + frequency, and harmonic analysis. It supports SPI communication + and provides buffered data output through the IIO framework. + + To compile this driver as a module, choose M here: the module will + be called ade9000. + config ADI_AXI_ADC tristate "Analog Devices Generic AXI ADC IP core driver" depends on MICROBLAZE || NIOS2 || ARCH_ZYNQ || ARCH_ZYNQMP || ARCH_INTEL_SOCFPGA || COMPILE_TESTdrivers/iio/adc/Makefile +2
-0@@ -20,6 +20,7 @@ obj-$(CONFIG_AD7091R5) += ad7091r5.o obj-$(CONFIG_AD7091R8) += ad7091r8.o obj-$(CONFIG_AD7124) += ad7124.o obj-$(CONFIG_AD7173) += ad7173.o +obj-$(CONFIG_ADE9000) += ade9000.o obj-$(CONFIG_AD7191) += ad7191.o obj-$(CONFIG_AD7192) += ad7192.o obj-$(CONFIG_AD7266) += ad7266.o@@ -46,6 +47,7 @@ obj-$(CONFIG_AD7944) += ad7944.o obj-$(CONFIG_AD7949) += ad7949.o obj-$(CONFIG_AD799X) += ad799x.o obj-$(CONFIG_AD9467) += ad9467.o +obj-$(CONFIG_ADE9000) += ade9000.o obj-$(CONFIG_ADI_AXI_ADC) += adi-axi-adc.o obj-$(CONFIG_ASPEED_ADC) += aspeed_adc.o obj-$(CONFIG_AT91_ADC) += at91_adc.odrivers/iio/adc/ade9000.c new file +26
-0@@ -0,0 +1,2033 @@ +// SPDX-License-Identifier: GPL-2.0-only +/** +* ADE9000 driver +* +* Copyright 2025 Analog Devices Inc. +*/ +#include <linux/bitfield.h> +#include <linux/clk.h> +#include <linux/clk-provider.h> +#include <linux/delay.h> +#include <linux/gpio/consumer.h> +#include <linux/interrupt.h> +#include <linux/iio/iio.h> +#include <linux/iio/buffer.h> +#include <linux/iio/kfifo_buf.h> +#include <linux/iio/events.h> +#include <linux/iio/sysfs.h> +#include <linux/interrupt.h> +#include <linux/minmax.h> +#include <linux/module.h> +#include <linux/property.h> +#include <linux/regmap.h> +#include <linux/regulator/consumer.h> +#include <linux/spi/spi.h> +#include <linux/unaligned.h> +
...
drivers/iio/adc/ade9000.c line 740
Quoted 7 lines
drivers/iio/adc/ade9000.c new file +7
-0+static int ade9000_spi_read_reg (void *context,unsigned int reg, +unsigned int *val) +{ +struct device *dev = context; +struct spi_device *spi =to_spi_device (dev); +struct ade9000_state *st =spi_get_drvdata (spi); +
Why the new line? Also, why not having 'struct ade9000_state' as your context?
Quoted 36 lines
drivers/iio/adc/ade9000.c new file +36
-0+u16 addr; +int ret =0 ; +struct spi_transfer xfer[] = { + { + .tx_buf = st->tx, + .len =2 , + }, + { + .rx_buf = st->rx, + }, + }; + + addr =FIELD_PREP (ADE9000_REG_ADDR_MASK , reg) | +ADE9000_REG_READ_BIT_MASK ; + +put_unaligned_be16 (addr, st->tx); + +if (reg >ADE9000_REG_RUN && reg <ADE9000_REG_VERSION ) + xfer[1 ].len =4 ; +else + xfer[1 ].len =6 ; + + ret =spi_sync_transfer (st->spi, xfer,ARRAY_SIZE (xfer)); +if (ret) { +dev_err (&st->spi->dev,"error reading register 0x%x" , + reg); +return ret; + } + +if (reg >ADE9000_REG_RUN && reg <ADE9000_REG_VERSION ) + *val =get_unaligned_be16 (st->rx); +else + *val =get_unaligned_be32 (st->rx); + +return 0 ; +}
...
drivers/iio/adc/ade9000.c line 888
Quoted 6 lines
drivers/iio/adc/ade9000.c new file +6
-0+ +static int ade9000_waveform_buffer_en (struct ade9000_state *st,bool state) +{ +return regmap_update_bits (st->regmap,ADE9000_REG_WFB_CFG ,BIT (4 ), + state ?BIT (4 ) :0 ); +}
I'm not a big fan of these wrapper helpers.
Quoted 34 lines
drivers/iio/adc/ade9000.c new file +34
-0+ +static irqreturn_t ade9000_irq0_thread (int irq,void *data) +{ +struct iio_dev *indio_dev = data; +struct ade9000_state *st =iio_priv (indio_dev); +s64 timestamp =iio_get_time_ns (indio_dev); +u32 handled_irq =0 ; +u32 interrupts; +u32 status; +int ret; + + ret =regmap_read (st->regmap,ADE9000_REG_STATUS0 , &status); +if (ret) { +dev_err (&st->spi->dev,"IRQ0 read status fail" ); +return IRQ_HANDLED ; + } + + ret =regmap_read (st->regmap,ADE9000_REG_MASK0 , &interrupts); +if (ret) { +dev_err (&st->spi->dev,"IRQ0 read status fail" ); +return IRQ_HANDLED ; + } + +if ((status &ADE9000_ST0_PAGE_FULL_BIT ) && + (interrupts &ADE9000_ST0_PAGE_FULL_BIT )) { +/* Always use streaming mode */ + ret =ade9000_iio_push_streaming (indio_dev); +if (ret) { +dev_err (&st->spi->dev,"IRQ0 IIO push fail" ); +return IRQ_HANDLED ; + } + + handled_irq |=ADE9000_ST0_PAGE_FULL_BIT ; + }
drivers/iio/adc/ade9000.c line 932
Quoted 10 lines
drivers/iio/adc/ade9000.c new file +10
-0+ +if ((status &ADE9000_ST0_WFB_TRIG_BIT ) && + (interrupts &ADE9000_ST0_WFB_TRIG_BIT )) { + ret =ade9000_waveform_buffer_en (st, false); +if (ret) { +dev_err (&st->spi->dev,"IRQ0 WFB fail" ); +return IRQ_HANDLED ; + } + + ret =ade9000_iio_push_buffer (indio_dev);
Don't we also need to make sure that buffering is enabled here?
Quoted 38 lines
drivers/iio/adc/ade9000.c new file +38
-0+if (ret) { +dev_err (&st->spi->dev,"IRQ0 IIO push fail @ WFB TRIG" ); +return IRQ_HANDLED ; + } + + handled_irq |=ADE9000_ST0_WFB_TRIG_BIT ; + } + +if ((status &ADE9000_ST0_EGYRDY ) && + (interrupts &ADE9000_ST0_EGYRDY )) { +iio_push_event (indio_dev, +IIO_UNMOD_EVENT_CODE (IIO_ENERGY , +ADE9000_ST0_EGYRDY , +IIO_EV_TYPE_MAG , +IIO_EV_DIR_NONE ), + timestamp); + + handled_irq |=ADE9000_ST0_EGYRDY ; + } + + ret =regmap_write (st->regmap,ADE9000_REG_STATUS0 , handled_irq); +if (ret) +dev_err (&st->spi->dev,"IRQ0 write status fail" ); + +return IRQ_HANDLED ; +} + +static irqreturn_t ade9000_irq1_thread (int irq,void *data) +{ +struct iio_dev *indio_dev = data; +struct ade9000_state *st =iio_priv (indio_dev); +unsigned int bit =ADE9000_ST1_CROSSING_FIRST ; +s64 timestamp =iio_get_time_ns (indio_dev); +u32 handled_irq =0 ; +u32 interrupts; +u32 result; +u32 status; +u32 tmp;
drivers/iio/adc/ade9000.c line 980
Quoted 10 lines
drivers/iio/adc/ade9000.c new file +10
-0+unsigned long interrupt_bits; +int ret; + +if (!st->rst_done) { + ret =regmap_read (st->regmap,ADE9000_REG_STATUS1 , &result); +if (ret) +return ret; + +if (result &ADE9000_ST1_RSTDONE_BIT ) + st->rst_done = true;
Might be sensible to use something like complete() instead of the flag so that you can have some timeout of the reset() API.
Quoted 174 lines
drivers/iio/adc/ade9000.c new file +174
-0+else +dev_err (&st->spi->dev,"Error testing reset done" ); + +return IRQ_HANDLED ; + } + + ret =regmap_read (st->regmap,ADE9000_REG_STATUS1 , &status); +if (ret) +return IRQ_HANDLED ; + + ret =regmap_read (st->regmap,ADE9000_REG_MASK1 , &interrupts); +if (ret) { +dev_err (&st->spi->dev,"IRQ1 read status fail" ); +return IRQ_HANDLED ; + } + + interrupt_bits = interrupts; +for_each_set_bit_from (bit, &interrupt_bits, +ADE9000_ST1_CROSSING_DEPTH ){ + tmp = status &BIT (bit); + +switch (tmp) { +case ADE9000_ST1_ZXVA_BIT : +iio_push_event (indio_dev, +IIO_UNMOD_EVENT_CODE (IIO_VOLTAGE , +ADE9000_ST1_ZXVA_BIT , +IIO_EV_TYPE_THRESH , +IIO_EV_DIR_EITHER ), + timestamp); + handled_irq |=ADE9000_ST1_ZXVA_BIT ; +break ; +case ADE9000_ST1_ZXTOVA_BIT : +iio_push_event (indio_dev, +IIO_UNMOD_EVENT_CODE (IIO_VOLTAGE , +ADE9000_ST1_ZXTOVA_BIT , +IIO_EV_TYPE_THRESH , +IIO_EV_DIR_EITHER ), + timestamp); + handled_irq |=ADE9000_ST1_ZXTOVA_BIT ; +break ; +case ADE9000_ST1_ZXIA_BIT : +iio_push_event (indio_dev, +IIO_UNMOD_EVENT_CODE (IIO_CURRENT , +ADE9000_ST1_ZXIA_BIT , +IIO_EV_TYPE_THRESH , +IIO_EV_DIR_EITHER ), + timestamp); + handled_irq |=ADE9000_ST1_ZXIA_BIT ; +break ; +case ADE9000_ST1_ZXVB_BIT : +iio_push_event (indio_dev, +IIO_UNMOD_EVENT_CODE (IIO_VOLTAGE , +ADE9000_ST1_ZXVB_BIT , +IIO_EV_TYPE_THRESH , +IIO_EV_DIR_EITHER ), + timestamp); + handled_irq |=ADE9000_ST1_ZXVB_BIT ; +break ; +case ADE9000_ST1_ZXTOVB_BIT : +iio_push_event (indio_dev, +IIO_UNMOD_EVENT_CODE (IIO_VOLTAGE , +ADE9000_ST1_ZXTOVB_BIT , +IIO_EV_TYPE_THRESH , +IIO_EV_DIR_EITHER ), + timestamp); + handled_irq |=ADE9000_ST1_ZXTOVB_BIT ; +break ; +case ADE9000_ST1_ZXIB_BIT : +iio_push_event (indio_dev, +IIO_UNMOD_EVENT_CODE (IIO_CURRENT , +ADE9000_ST1_ZXIB_BIT , +IIO_EV_TYPE_THRESH , +IIO_EV_DIR_EITHER ), + timestamp); + handled_irq |=ADE9000_ST1_ZXIB_BIT ; +break ; +case ADE9000_ST1_ZXVC_BIT : +iio_push_event (indio_dev, +IIO_UNMOD_EVENT_CODE (IIO_VOLTAGE , +ADE9000_ST1_ZXVC_BIT , +IIO_EV_TYPE_THRESH , +IIO_EV_DIR_EITHER ), + timestamp); + handled_irq |=ADE9000_ST1_ZXVC_BIT ; +break ; +case ADE9000_ST1_ZXTOVC_BIT : +iio_push_event (indio_dev, +IIO_UNMOD_EVENT_CODE (IIO_VOLTAGE , +ADE9000_ST1_ZXTOVC_BIT , +IIO_EV_TYPE_THRESH , +IIO_EV_DIR_EITHER ), + timestamp); + handled_irq |=ADE9000_ST1_ZXTOVC_BIT ; +break ; +case ADE9000_ST1_ZXIC_BIT : +iio_push_event (indio_dev, +IIO_UNMOD_EVENT_CODE (IIO_CURRENT , +ADE9000_ST1_ZXIC_BIT , +IIO_EV_TYPE_THRESH , +IIO_EV_DIR_EITHER ), + timestamp); + handled_irq |=ADE9000_ST1_ZXIC_BIT ; +break ; +case ADE9000_ST1_SWELLA_BIT : +iio_push_event (indio_dev, +IIO_UNMOD_EVENT_CODE (IIO_VOLTAGE , +ADE9000_ST1_SWELLA_BIT >>20 , +IIO_EV_TYPE_THRESH , +IIO_EV_DIR_RISING ), + timestamp); + handled_irq |=ADE9000_ST1_SWELLA_BIT ; +break ; +case ADE9000_ST1_SWELLB_BIT : +iio_push_event (indio_dev, +IIO_UNMOD_EVENT_CODE (IIO_VOLTAGE , +ADE9000_ST1_SWELLB_BIT >>20 , +IIO_EV_TYPE_THRESH , +IIO_EV_DIR_RISING ), + timestamp); + handled_irq |=ADE9000_ST1_SWELLB_BIT ; +break ; +case ADE9000_ST1_SWELLC_BIT : +iio_push_event (indio_dev, +IIO_UNMOD_EVENT_CODE (IIO_VOLTAGE , +ADE9000_ST1_SWELLC_BIT >>20 , +IIO_EV_TYPE_THRESH , +IIO_EV_DIR_RISING ), + timestamp); + handled_irq |=ADE9000_ST1_SWELLC_BIT ; +break ; +case ADE9000_ST1_DIPA_BIT : +iio_push_event (indio_dev, +IIO_UNMOD_EVENT_CODE (IIO_VOLTAGE , +ADE9000_ST1_DIPA_BIT >>20 , +IIO_EV_TYPE_THRESH , +IIO_EV_DIR_FALLING ), + timestamp); + handled_irq |=ADE9000_ST1_DIPA_BIT ; +break ; +case ADE9000_ST1_DIPB_BIT : +iio_push_event (indio_dev, +IIO_UNMOD_EVENT_CODE (IIO_VOLTAGE , +ADE9000_ST1_DIPB_BIT >>20 , +IIO_EV_TYPE_THRESH , +IIO_EV_DIR_FALLING ), + timestamp); + handled_irq |=ADE9000_ST1_DIPB_BIT ; +break ; +case ADE9000_ST1_DIPC_BIT : +iio_push_event (indio_dev, +IIO_UNMOD_EVENT_CODE (IIO_VOLTAGE , +ADE9000_ST1_DIPC_BIT >>20 , +IIO_EV_TYPE_THRESH , +IIO_EV_DIR_FALLING ), + timestamp); + handled_irq |=ADE9000_ST1_DIPC_BIT ; +break ; +case ADE9000_ST1_SEQERR_BIT : +iio_push_event (indio_dev, +IIO_UNMOD_EVENT_CODE (IIO_VOLTAGE , +ADE9000_ST1_SEQERR_BIT >>12 , +IIO_EV_TYPE_CHANGE , +IIO_EV_DIR_NONE ), + timestamp); + handled_irq |=ADE9000_ST1_SEQERR_BIT ; +break ; +default : +return IRQ_HANDLED ; + } + } + + ret =regmap_write (st->regmap,ADE9000_REG_STATUS1 , handled_irq); +if (ret) +return ret;
drivers/iio/adc/ade9000.c line 1164
Quoted 10 lines
drivers/iio/adc/ade9000.c new file +10
-0+ +return IRQ_HANDLED ; +} + +static irqreturn_t ade9000_dready_thread (int irq,void *data) +{ +struct iio_dev *indio_dev = data; + +/* Handle data ready interrupt from C4/EVENT/DREADY pin */ +if (iio_buffer_enabled (indio_dev))
I think the above is potentially racy... I see two options:
1) Either you enable/disable the interrupt on the buffer setup ops (assuming the interrupt does not need to be enabled on normal operation).
2) Or use iio_device_claim_buffer_mode()
I think 1) is preferred (if possible). BTW, can we do it at the device level? I mean, without having to rely on enable/disable_irq().
Quoted 4 lines
drivers/iio/adc/ade9000.c new file +4
-0+ade9000_iio_push_buffer (indio_dev); + +return IRQ_HANDLED ; +}
...
Quoted 64 lines
drivers/iio/adc/ade9000.c new file +64
-0+static int ade9000_write_raw (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan, +int val, +int val2, +long mask) +{ +struct ade9000_state *st =iio_priv (indio_dev); +u32 addr; +u32 tmp; + +switch (mask) { +case IIO_CHAN_INFO_SAMP_FREQ : +if (val !=50 && val !=60 ) +return -EINVAL ; +return regmap_write (st->regmap,ADE9000_REG_ACCMODE , + (val ==60 ) ?ADE9000_ACCMODE_60HZ :ADE9000_ACCMODE ); +case IIO_CHAN_INFO_OFFSET : +switch (chan->type) { +case IIO_CURRENT : +return regmap_write (st->regmap, +ADE9000_ADDR_ADJUST (ADE9000_REG_AIRMSOS , + chan->channel), val); +case IIO_VOLTAGE : +case IIO_ALTVOLTAGE : +return regmap_write (st->regmap, +ADE9000_ADDR_ADJUST (ADE9000_REG_AVRMSOS , + chan->channel), val); +case IIO_POWER : + tmp = chan->address; + tmp &= ~ADE9000_PHASE_B_POS_BIT ; + tmp &= ~ADE9000_PHASE_C_POS_BIT ; + +switch (tmp) { +case ADE9000_REG_AWATTOS : +return regmap_write (st->regmap, +ADE9000_ADDR_ADJUST (ADE9000_REG_AWATTOS , + chan->channel), val); +case ADE9000_REG_AVAR : +return regmap_write (st->regmap, +ADE9000_ADDR_ADJUST (ADE9000_REG_AVAROS , + chan->channel), val); +case ADE9000_REG_AFVAR : +return regmap_write (st->regmap, +ADE9000_ADDR_ADJUST (ADE9000_REG_AFVAROS , + chan->channel), val); +default : +return -EINVAL ; + } +default : +return -EINVAL ; + } +case IIO_CHAN_INFO_HARDWAREGAIN : +switch (chan->type) { +case IIO_CURRENT : +return regmap_write (st->regmap, +ADE9000_ADDR_ADJUST (ADE9000_REG_AIGAIN , + chan->channel), val); +case IIO_VOLTAGE : +return regmap_write (st->regmap, +ADE9000_ADDR_ADJUST (ADE9000_REG_AVGAIN , + chan->channel), val); +case IIO_POWER : + return regmap_write(st->regmap, + ADE9000_ADDR_ADJUST(ADE9000_REG_APGAIN,
drivers/iio/adc/ade9000.c line 1373
Quoted 10 lines
drivers/iio/adc/ade9000.c new file +10
-0+ chan->channel), val); +default : +return -EINVAL ; + } +case IIO_CHAN_INFO_CALIBSCALE : +if (val >4 || val <1 || val ==3 ) +return -EINVAL ; + addr =ADE9000_REG_PGA_GAIN ; + val =ilog2 (val) << (chan->channel *2 +8 ); + tmp =0x3 << (chan->channel *2 +8 );
I guess the above deserves a comment about what's going on.
drivers/iio/adc/ade9000.c line 1378
Quoted 5 lines
drivers/iio/adc/ade9000.c new file +5
-0+return regmap_update_bits (st->regmap, addr, tmp, val); +default : +return -EINVAL ; + } +}
Also have to ask: Will any of the above possible interfere with the device has events enabled or is buffering? Basically should we use the iio_claim_direct() functions?
Quoted 28 lines
drivers/iio/adc/ade9000.c new file +28
-0+ +static int ade9000_reg_access (struct iio_dev *indio_dev, +unsigned int reg, +unsigned int tx_val, +unsigned int *rx_val) +{ +struct ade9000_state *st =iio_priv (indio_dev); + +if (rx_val) +return regmap_read (st->regmap, reg, rx_val); + +return regmap_write (st->regmap, reg, tx_val); +} + +static int ade9000_read_event_config (struct iio_dev *indio_dev, +const struct iio_chan_spec *chan, +enum iio_event_type type, +enum iio_event_direction dir) +{ +struct ade9000_state *st =iio_priv (indio_dev); +u32 interrupts0, interrupts1, number; +int ret; + + ret =regmap_read (st->regmap,ADE9000_REG_MASK0 , &interrupts0); +if (ret) +return ret; + + ret =regmap_read (st->regmap,ADE9000_REG_MASK1 , &interrupts1);
drivers/iio/adc/ade9000.c line 1416
Quoted 10 lines
drivers/iio/adc/ade9000.c new file +10
-0+if (ret) +return ret; + +if (type ==IIO_EV_TYPE_MAG ) +return (interrupts0 &ADE9000_ST0_EGYRDY ); + +if (type ==IIO_EV_TYPE_CHANGE ) +return (interrupts1 &ADE9000_ST1_SEQERR_BIT ); + + number = chan->channel;
nit: new line
Quoted 94 lines
drivers/iio/adc/ade9000.c new file +94
-0+switch (number) { +case ADE9000_PHASE_A_NR : +if (chan->type ==IIO_VOLTAGE ) { +if (dir ==IIO_EV_DIR_EITHER ) +return (interrupts1 &ADE9000_ST1_ZXVA_BIT ); +if (dir ==IIO_EV_DIR_NONE ) +return (interrupts1 &ADE9000_ST1_ZXTOVA_BIT ); +if (dir ==IIO_EV_DIR_RISING ) +return (interrupts1 &ADE9000_ST1_SWELLA_BIT ); +if (dir ==IIO_EV_DIR_FALLING ) +return (interrupts1 &ADE9000_ST1_DIPA_BIT ); + }else if (chan->type ==IIO_CURRENT ) { +return (interrupts1 &ADE9000_ST1_ZXIA_BIT ); + } +break ; +case ADE9000_PHASE_B_NR : +if (chan->type ==IIO_VOLTAGE ) { +if (dir ==IIO_EV_DIR_EITHER ) +return (interrupts1 &ADE9000_ST1_ZXVB_BIT ); +if (dir ==IIO_EV_DIR_NONE ) +return (interrupts1 &ADE9000_ST1_ZXTOVB_BIT ); +if (dir ==IIO_EV_DIR_RISING ) +return (interrupts1 &ADE9000_ST1_SWELLB_BIT ); +if (dir ==IIO_EV_DIR_FALLING ) +return (interrupts1 &ADE9000_ST1_DIPB_BIT ); + }else if (chan->type ==IIO_CURRENT ) { +return (interrupts1 &ADE9000_ST1_ZXIB_BIT ); + } +break ; +case ADE9000_PHASE_C_NR : +if (chan->type ==IIO_VOLTAGE ) { +if (dir ==IIO_EV_DIR_EITHER ) +return (interrupts1 &ADE9000_ST1_ZXVC_BIT ); +if (dir ==IIO_EV_DIR_NONE ) +return (interrupts1 &ADE9000_ST1_ZXTOVC_BIT ); +if (dir ==IIO_EV_DIR_RISING ) +return (interrupts1 &ADE9000_ST1_SWELLC_BIT ); +if (dir ==IIO_EV_DIR_FALLING ) +return (interrupts1 &ADE9000_ST1_DIPC_BIT ); + }else if (chan->type ==IIO_CURRENT ) { +return (interrupts1 &ADE9000_ST1_ZXIC_BIT ); + } +break ; +default : +return -EINVAL ; + } + +return 0 ; +} + +static int ade9000_write_event_config (struct iio_dev *indio_dev, +const struct iio_chan_spec *chan, +enum iio_event_type type, +enum iio_event_direction dir, +bool state) +{ +struct ade9000_state *st =iio_priv (indio_dev); +u32 interrupts, tmp; +int ret; +struct irq_wfb_trig { +u32 irq; +u32 wfb_trg; + }; + + ret =regmap_write (st->regmap,ADE9000_REG_STATUS1 ,GENMASK (31 ,0 )); +if (ret) +return ret; + +if (type ==IIO_EV_TYPE_MAG ) { + ret =regmap_update_bits (st->regmap,ADE9000_REG_STATUS0 , +ADE9000_ST0_EGYRDY ,ADE9000_ST0_EGYRDY ); +if (ret) +return ret; +return regmap_update_bits (st->regmap,ADE9000_REG_MASK0 , +ADE9000_ST0_EGYRDY , + state ?ADE9000_ST1_SEQERR_BIT :0 ); + } + +if (type ==IIO_EV_TYPE_CHANGE ) +return regmap_update_bits (st->regmap,ADE9000_REG_MASK1 , +ADE9000_ST1_SEQERR_BIT , + state ?ADE9000_ST1_SEQERR_BIT :0 ); + +struct irq_wfb_trig trig_arr[6 ] = { + { + .irq =ADE9000_ST1_ZXVA_BIT , + .wfb_trg =ADE9000_WFB_TRG_ZXVA_BIT + }, { + .irq =ADE9000_ST1_ZXIA_BIT , + .wfb_trg =ADE9000_WFB_TRG_ZXIA_BIT + }, { + .irq =ADE9000_ST1_ZXVB_BIT , + .wfb_trg =ADE9000_WFB_TRG_ZXVB_BIT + }, {
drivers/iio/adc/ade9000.c line 1520
Quoted 10 lines
drivers/iio/adc/ade9000.c new file +10
-0+ .irq = ADE9000_ST1_ZXIB_BIT, + .wfb_trg = ADE9000_WFB_TRG_ZXIB_BIT + }, { + .irq = ADE9000_ST1_ZXVC_BIT, + .wfb_trg = ADE9000_WFB_TRG_ZXVC_BIT + }, { + .irq = ADE9000_ST1_ZXIC_BIT, + .wfb_trg = ADE9000_WFB_TRG_ZXIC_BIT + }, + };
Not seeing any reason for the mixed code declaration.
Quoted 19 lines
drivers/iio/adc/ade9000.c new file +19
-0+ +if (dir ==IIO_EV_DIR_EITHER ) { +if (state) { + interrupts |= trig_arr[chan->channel *2 + chan->type].irq; + st->wfb_trg |= trig_arr[chan->channel *2 + chan->type].wfb_trg; + }else { + interrupts &= ~trig_arr[chan->channel *2 + chan->type].irq; + st->wfb_trg &= ~trig_arr[chan->channel *2 + chan->type].wfb_trg; + } +if (dir ==IIO_EV_DIR_NONE ) { +switch (chan->channel) { +case ADE9000_PHASE_A_NR : + interrupts |=ADE9000_ST1_ZXTOVA_BIT ; +break ; +case ADE9000_PHASE_B_NR : + interrupts |=ADE9000_ST1_ZXTOVB_BIT ; +break ; +case ADE9000_PHASE_C_NR : + interrupts |=ADE9000_ST1_ZXTOVC_BIT ;
drivers/iio/adc/ade9000.c line 1549
Quoted 10 lines
drivers/iio/adc/ade9000.c new file +10
-0+break ; +default : +break ; + } + +if (state) + interrupts |= tmp; +else + interrupts &= ~tmp; + }
Yeah, something is odd with the above. I guess you missed to indent the above condition.
drivers/iio/adc/ade9000.c line 1550
Quoted 1 line
drivers/iio/adc/ade9000.c new file +1
-0+ }else if (dir ==IIO_EV_DIR_RISING ) {
ups...
Quoted 35 lines
drivers/iio/adc/ade9000.c new file +35
-0+switch (chan->channel) { +case ADE9000_PHASE_A_NR : + tmp |=ADE9000_ST1_SWELLA_BIT ; +break ; +case ADE9000_PHASE_B_NR : + tmp |=ADE9000_ST1_SWELLB_BIT ; +break ; +case ADE9000_PHASE_C_NR : + tmp |=ADE9000_ST1_SWELLC_BIT ; +break ; +default : +break ; + } + +if (state) { + interrupts |= tmp; + st->wfb_trg |=ADE9000_WFB_TRG_SWELL_BIT ; + }else { + interrupts &= ~tmp; + st->wfb_trg &= ~ADE9000_WFB_TRG_SWELL_BIT ; + } + + }else if (dir ==IIO_EV_DIR_FALLING ) { +switch (chan->channel) { +case ADE9000_PHASE_A_NR : + tmp |=ADE9000_ST1_DIPA_BIT ; +break ; +case ADE9000_PHASE_B_NR : + tmp |=ADE9000_ST1_DIPB_BIT ; +break ; +case ADE9000_PHASE_C_NR : + tmp |=ADE9000_ST1_DIPC_BIT ; +break ; +default : +break ;
drivers/iio/adc/ade9000.c line 1595
Quoted 10 lines
drivers/iio/adc/ade9000.c new file +10
-0+ } + +if (state) { + interrupts |= tmp; + st->wfb_trg |=ADE9000_WFB_TRG_DIP_BIT ; + }else { + interrupts &= ~tmp; + st->wfb_trg &= ~ADE9000_WFB_TRG_DIP_BIT ; + } + }
nit: new line
Quoted 4 lines
drivers/iio/adc/ade9000.c new file +4
-0+return regmap_update_bits (st->regmap,ADE9000_REG_MASK1 , interrupts, + interrupts); +} +
...
Quoted 12 lines
drivers/iio/adc/ade9000.c new file +12
-0+ +static int ade9000_waveform_buffer_interrupt_setup (struct ade9000_state *st) +{ +int ret; + + ret =regmap_write (st->regmap,ADE9000_REG_WFB_TRG_CFG ,0x0 ); +if (ret) +return ret; + +/* Always use streaming mode setup */ + ret =regmap_write (st->regmap,ADE9000_REG_WFB_PG_IRQEN , +ADE9000_MIDDLE_PAGE_BIT );
drivers/iio/adc/ade9000.c line 1746
Quoted 10 lines
drivers/iio/adc/ade9000.c new file +10
-0+if (ret) +return ret; + + ret =regmap_write (st->regmap,ADE9000_REG_STATUS0 ,GENMASK (31 ,0 )); +if (ret) +return ret; + +return regmap_update_bits (st->regmap,ADE9000_REG_MASK0 , +ADE9000_ST0_PAGE_FULL_BIT , +ADE9000_ST0_PAGE_FULL_BIT );
The above looks like regmap_set_bits()?
drivers/iio/adc/ade9000.c line 1759
Quoted 13 lines
drivers/iio/adc/ade9000.c new file +13
-0+} + +static int ade9000_buffer_preenable (struct iio_dev *indio_dev) +{ +struct ade9000_state *st =iio_priv (indio_dev); +int ret; + + ret =ade9000_waveform_buffer_config (indio_dev); +if (ret) +return ret; + + st->wfb_nr_samples =ADE9000_WFB_MAX_SAMPLES_CHAN * + st->wfb_nr_activ_chan;
If not passing the 101 limit, I would do this in one liner. To me this is one of those cases where readability is hurt.
Quoted 17 lines
drivers/iio/adc/ade9000.c new file +17
-0+ + ret =ade9000_configure_scan (indio_dev,ADE9000_REG_WF_BUFF ); +if (ret) +return ret; + + ret =ade9000_waveform_buffer_interrupt_setup (st); +if (ret) +return ret; + + ret =ade9000_waveform_buffer_en (st, true); +if (ret) { +dev_err (&st->spi->dev,"Post-enable waveform buffer enable fail" ); +return ret; + } + +return 0 ; +}
drivers/iio/adc/ade9000.c line 1786
Quoted 10 lines
drivers/iio/adc/ade9000.c new file +10
-0+ +static int ade9000_buffer_postdisable (struct iio_dev *indio_dev) +{ +struct ade9000_state *st =iio_priv (indio_dev); +u32 interrupts; +int ret; + + ret =ade9000_waveform_buffer_en (st, false); +if (ret) { +dev_err (&st->spi->dev,"Post-disable waveform buffer disable fail" );
Again (I started the review from bottom :)), I would log any meaningful error insinde the helper. Ditto for all other places.
Quoted 22 lines
drivers/iio/adc/ade9000.c new file +22
-0+return ret; + } + + ret =regmap_write (st->regmap,ADE9000_REG_WFB_TRG_CFG ,0x0 ); +if (ret) +return ret; + + interrupts =ADE9000_ST0_WFB_TRIG_BIT |ADE9000_ST0_PAGE_FULL_BIT ; + +return regmap_update_bits (st->regmap,ADE9000_REG_MASK0 , interrupts,0 ); +if (ret) { +dev_err (&st->spi->dev,"Post-disable update maks0 fail" ); +return ret; + } + +return regmap_write (st->regmap,ADE9000_REG_STATUS0 ,GENMASK (31 ,0 )); +} + +static int ade9000_reset (struct ade9000_state *st) +{ +struct gpio_desc *gpio_reset; +int ret;
drivers/iio/adc/ade9000.c line 1818
Quoted 10 lines
drivers/iio/adc/ade9000.c new file +10
-0+ + st->rst_done = false; + + gpio_reset =devm_gpiod_get_optional (&st->spi->dev,"reset" , +GPIOD_OUT_HIGH ); +if (IS_ERR (gpio_reset)) +return PTR_ERR (gpio_reset); + +if (gpio_reset) { +gpiod_set_value_cansleep (gpio_reset,1 );
No need for the above. You already request the gpio with GPIOD_OUT_HIGH which means you ask for it in the asserted state. Hence you just need to bring the device out of reset.
Quoted 17 lines
drivers/iio/adc/ade9000.c new file +17
-0+fsleep (10 ); +gpiod_set_value_cansleep (gpio_reset,0 ); +fsleep (50000 ); + }else { + ret =regmap_update_bits (st->regmap,ADE9000_REG_CONFIG1 , +ADE9000_SWRST_BIT ,ADE9000_SWRST_BIT ); +if (ret) +return ret; +fsleep (90 ); + } + +if (!st->rst_done) +return -EIO ; + +return 0 ; +} +
drivers/iio/adc/ade9000.c line 1845
Quoted 10 lines
drivers/iio/adc/ade9000.c new file +10
-0+static int ade9000_setup (struct ade9000_state *st) +{ +int ret; + + ret =regmap_multi_reg_write (st->regmap, ade9000_reg_sequence, +ARRAY_SIZE (ade9000_reg_sequence)); +if (ret) +return ret; + +msleep_interruptible (2 );
Why interruptible? I would also use fsleep(). 2ms is not really meant for msleep().
Quoted 36 lines
drivers/iio/adc/ade9000.c new file +36
-0+ +/* Clear all pending status bits by writing 1s */ + ret =regmap_write (st->regmap,ADE9000_REG_STATUS0 ,GENMASK (31 ,0 )); +if (ret) +return ret; + +return regmap_write (st->regmap,ADE9000_REG_STATUS1 ,GENMASK (31 ,0 )); +} + +static const struct iio_buffer_setup_ops ade9000_buffer_ops = { + .preenable = &ade9000_buffer_preenable, + .postdisable = &ade9000_buffer_postdisable, +}; + +static const struct iio_info ade9000_info = { + .read_raw = ade9000_read_raw, + .write_raw = ade9000_write_raw, + .debugfs_reg_access = ade9000_reg_access, + .write_event_config = ade9000_write_event_config, + .read_event_config = ade9000_read_event_config, + .write_event_value = ade9000_write_event_value, + .read_event_value = ade9000_read_event_value, +}; + +static const struct regmap_config ade9000_regmap_config = { + .reg_bits =16 , + .val_bits =32 , + .zero_flag_mask = true, + .cache_type =REGCACHE_RBTREE , + .reg_read = ade9000_spi_read_reg, + .reg_write = ade9000_spi_write_reg, + .volatile_reg = ade9000_is_volatile_reg, +}; + +static int ade9000_probe (struct spi_device *spi) +{
drivers/iio/adc/ade9000.c line 1891
Quoted 10 lines
drivers/iio/adc/ade9000.c new file +10
-0+struct device *dev = &spi->dev; +struct iio_dev *indio_dev; +struct ade9000_state *st; +struct regmap *regmap; +int irq; +int ret; + + indio_dev =devm_iio_device_alloc (dev,sizeof (*st)); +if (!indio_dev) +return dev_err_probe (dev, -ENOMEM ,"Unable to allocate ADE9000 IIO" );
nit: new line
drivers/iio/adc/ade9000.c line 1896
Quoted 5 lines
drivers/iio/adc/ade9000.c new file +5
-0+ st =iio_priv (indio_dev); + + regmap =devm_regmap_init (dev,NULL , spi, &ade9000_regmap_config); +if (IS_ERR (regmap)) +return dev_err_probe (dev,PTR_ERR (regmap),"Unable to allocate ADE9000 regmap" );
nit: new line
Quoted 24 lines
drivers/iio/adc/ade9000.c new file +24
-0+spi_set_drvdata (spi, st); + + irq =fwnode_irq_get_byname (dev_fwnode (dev),"irq0" ); +if (irq <0 ) +return dev_err_probe (dev, -EINVAL ,"Unable to find irq0" ); + + ret =devm_request_threaded_irq (dev, irq,NULL , + ade9000_irq0_thread, +IRQF_ONESHOT , +KBUILD_MODNAME , indio_dev); +if (ret) +return dev_err_probe (dev, ret,"Failed to request threaded irq: %d" , ret); + + irq =fwnode_irq_get_byname (dev_fwnode (dev),"irq1" ); +if (irq <0 ) +return dev_err_probe (dev, -EINVAL ,"Unable to find irq1" ); + + ret =devm_request_threaded_irq (dev, irq,NULL , + ade9000_irq1_thread, +IRQF_ONESHOT , +KBUILD_MODNAME , indio_dev); +if (ret) +return dev_err_probe (dev, ret,"Failed to request threaded irq: %d" , ret); +
drivers/iio/adc/ade9000.c line 1930
Quoted 10 lines
drivers/iio/adc/ade9000.c new file +10
-0+ irq =fwnode_irq_get_byname (dev_fwnode (dev),"dready" ); +if (irq <0 ) +return dev_err_probe (dev, -EINVAL ,"Unable to find dready" ); + + ret =devm_request_threaded_irq (dev, irq,NULL , + ade9000_dready_thread, +IRQF_ONESHOT , +KBUILD_MODNAME , indio_dev); +if (ret) +return dev_err_probe (dev, ret,"Failed to request threaded irq: %d" , ret);
You could likely have an helper function to handle the above. By passing the IRQ handler and the property string you would not need to do it three times (If I'm not missing nothing).
Quoted 8 lines
drivers/iio/adc/ade9000.c new file +8
-0+ + st->spi = spi; + +/* Optional external clock input */ + st->clkin =devm_clk_get_optional_enabled (dev,"clkin" ); +if (IS_ERR (st->clkin)) +return dev_err_probe (dev,PTR_ERR (st->clkin),"Failed to get and enable clkin: %ld" ,PTR_ERR (st->clkin)); +
drivers/iio/adc/ade9000.c line 1948
Quoted 10 lines
drivers/iio/adc/ade9000.c new file +10
-0+/* Register clock output provider */ +if (device_property_present (dev,"#clock-cells" )) { +struct clk_init_data clk_init = {}; +struct clk *clkout; +unsigned long parent_rate =24576000 ;/* Default crystal frequency */ + +if (st->clkin) + parent_rate =clk_get_rate (st->clkin); + + clk_init.name ="clkout" ;
If we have multiple devices the above might fail. I wooud the same as in:
https://elixir.bootlin.com/linux/v6.16/source/drivers/iio/frequency/adf4350.c#L467
Quoted 4 lines
drivers/iio/adc/ade9000.c new file +4
-0+ clk_init.ops = &ade9000_clkout_ops; + clk_init.flags =CLK_GET_RATE_NOCACHE ; + clk_init.num_parents =0 ; +
drivers/iio/adc/ade9000.c line 1962
Quoted 10 lines
drivers/iio/adc/ade9000.c new file +10
-0+ st->clkout_hw.init = &clk_init; + + clkout =devm_clk_register (dev, &st->clkout_hw); +if (IS_ERR (clkout)) +return dev_err_probe (dev,PTR_ERR (clkout),"Failed to register clkout: %ld" ,PTR_ERR (clkout)); + + ret =devm_of_clk_add_hw_provider (dev, of_clk_hw_simple_get, &st->clkout_hw); +if (ret) +return dev_err_probe (dev, ret,"Failed to add clock provider: %d" , ret); + }
nit: I would move the abov to it's own function.
drivers/iio/adc/ade9000.c line 1964
Quoted 2 lines
drivers/iio/adc/ade9000.c new file +2
-0+ + indio_dev->name =spi_get_device_id (spi)->name;
I tend to prefer a literal string here. And move it to something like chip_info->name if we add more chips to the driver.
drivers/iio/adc/ade9000.c line 1965
Quoted 1 line
drivers/iio/adc/ade9000.c new file +1
-0+ indio_dev->dev.parent = &st->spi->dev;
No need for the above line
Quoted 17 lines
drivers/iio/adc/ade9000.c new file +17
-0+ indio_dev->info = &ade9000_info; + indio_dev->modes =INDIO_DIRECT_MODE |INDIO_BUFFER_SOFTWARE ; + indio_dev->setup_ops = &ade9000_buffer_ops; + + st->regmap = regmap; + + ret =devm_regulator_get_enable (&spi->dev,"vdd" ); +if (ret) +return dev_err_probe (&spi->dev, ret, +"Failed to get and enable vdd regulator\n" ); + + ret =devm_regulator_get_enable_optional (dev,"vref" ); +if (ret <0 && ret != -ENODEV ) +return dev_err_probe (dev, ret, +"Failed to get and enable vref regulator\n" ); + +/* Configure reference selection based on vref regulator availability */
drivers/iio/adc/ade9000.c line 1992
Quoted 10 lines
drivers/iio/adc/ade9000.c new file +10
-0+if (ret != -ENODEV ) { + ret =regmap_update_bits (st->regmap,ADE9000_REG_CONFIG1 , +ADE9000_EXT_REF_MASK , +ADE9000_EXT_REF_MASK ); +if (ret) +return ret; + } + + indio_dev->channels = ade9000_channels; + indio_dev->num_channels =ARRAY_SIZE (ade9000_channels);
nit: new line
drivers/iio/adc/ade9000.c line 2000
Quoted 8 lines
drivers/iio/adc/ade9000.c new file +8
-0+ ret =devm_iio_kfifo_buffer_setup (dev, indio_dev, + &ade9000_buffer_ops); +if (ret) +return dev_err_probe (dev, ret,"Failed to setup IIO buffer" ); + + ret =ade9000_reset (st); +if (ret) +return dev_err_probe (dev, ret,"ADE9000 reset failed" );
I would very much prefer to do any logging inside ade9000_reset() and here just 'return ret'
drivers/iio/adc/ade9000.c line 2004
Quoted 4 lines
drivers/iio/adc/ade9000.c new file +4
-0+ + ret =ade9000_setup (st); +if (ret) +return dev_err_probe (dev, ret,"Unable to setup ADE9000" );
ditto.
- Nuno Sá
Re: [PATCH v3 3/4] iio: adc: add ade9000 support
In reply to Antoniu MiclausHi Antoniu,
Similarly to Nuno, I'm also getting into review only in v3 and may be missing remarks made in previous iterations. Also, this driver is extensive and for now, I only reviewed a few things (mostly register definitions and clock setup). I agree with somebody's suggestion I've seen about splitting the code into more patches according to the features that are going to be supported. More comments inline.
Quoted 6 lines · Antoniu Miclaus
Add driver support for the ade9000. highly accurate, fully integrated, multiphase energy and power quality monitoring device.
Signed-off-by Antoniu Miclaus <[email protected]>---
...
Quoted 13 lines
drivers/iio/adc/Makefile +1
-0@@ -20,6 +20,7 @@ obj-$(CONFIG_AD7091R5) += ad7091r5.o obj-$(CONFIG_AD7091R8) += ad7091r8.o obj-$(CONFIG_AD7124) += ad7124.o obj-$(CONFIG_AD7173) += ad7173.o +obj-$(CONFIG_ADE9000) += ade9000.o obj-$(CONFIG_AD7191) += ad7191.o obj-$(CONFIG_AD7192) += ad7192.o obj-$(CONFIG_AD7266) += ad7266.o
drivers/iio/adc/Makefile obj-$(CONFIG_AD7944) += ad7944.o · line 50
Quoted 4 lines
drivers/iio/adc/Makefile +1
-0@@ -46,6 +47,7 @@ obj-$(CONFIG_AD7944) += ad7944.o obj-$(CONFIG_AD7949) += ad7949.o obj-$(CONFIG_AD799X) += ad799x.o obj-$(CONFIG_AD9467) += ad9467.o +obj-$(CONFIG_ADE9000) += ade9000.o
Duplicated addition to into Makefile?
Quoted 9 lines
drivers/iio/adc/Makefile +0
-0obj-$(CONFIG_ADI_AXI_ADC) += adi-axi-adc.o obj-$(CONFIG_ASPEED_ADC) += aspeed_adc.o obj-$(CONFIG_AT91_ADC) += at91_adc.odrivers/iio/adc/ade9000.c new file +0
-0@@ -0,0 +1,2033 @@
...
Quoted 21 lines
drivers/iio/adc/ade9000.c new file +21
-0+ +/* Address of ADE9000 registers */ +#define ADE9000_REG_AIGAIN 0x000 +#define ADE9000_REG_AVGAIN 0x00B +#define ADE9000_REG_AIRMSOS 0x00C +#define ADE9000_REG_AVRMSOS 0x00D +#define ADE9000_REG_APGAIN 0x00E +#define ADE9000_REG_AWATTOS 0x00F +#define ADE9000_REG_AVAROS 0x010 +#define ADE9000_REG_AFVAROS 0x012 +#define ADE9000_REG_CONFIG0 0x060 +#define ADE9000_REG_DICOEFF 0x072 +#define ADE9000_REG_AI_PCF 0x20A +#define ADE9000_REG_AV_PCF 0x20B +#define ADE9000_REG_AIRMS 0x20C +#define ADE9000_REG_AVRMS 0x20D +#define ADE9000_REG_AWATT 0x210 +#define ADE9000_REG_AVAR 0x211 +#define ADE9000_REG_AVA 0x212 +#define ADE9000_REG_AFVAR 0x214 +#define ADE9000_REG_APF 0x216
drivers/iio/adc/ade9000.c line 57
Quoted 10 lines
drivers/iio/adc/ade9000.c new file +10
-0+#define ADE9000_REG_BI_PCF 0x22A +#define ADE9000_REG_BV_PCF 0x22B +#define ADE9000_REG_BIRMS 0x22C +#define ADE9000_REG_BVRMS 0x22D +#define ADE9000_REG_CI_PCF 0x24A +#define ADE9000_REG_CV_PCF 0x24B +#define ADE9000_REG_CIRMS 0x24C +#define ADE9000_REG_CVRMS 0x24D +#define ADE9000_REG_AWATT_ACC 0x2E5 +#define ADE9000_REG_AWATTHR_LO 0x2E6
Some defines are using tabs before the define name, others are using spaces. I would standardize to have one space after each '#define' but I guess it's also okay if you use all tabs.
Quoted 5 lines
drivers/iio/adc/ade9000.c new file +5
-0+#define ADE9000_REG_AVAHR_LO 0x2FA +#define ADE9000_REG_AFVARHR_LO 0x30E +#define ADE9000_REG_BWATTHR_LO 0x322 +#define ADE9000_REG_BVAHR_LO 0x336 +#define ADE9000_REG_BFVARHR_LO 0x34A
...
drivers/iio/adc/ade9000.c line 145
Quoted 3 lines
drivers/iio/adc/ade9000.c new file +3
-0+ +/* Disable all interrupts except EGYRDY */ +#define ADE9000_MASK0 0x00000001
Can we have more suggestive names for the masks?
If this disables all interrupts except EGYRDY, would it be reasonable to call it ADE9000_EGYRDY_INT_MASK or ADE9000_MASK0_EGYRDY_MASK (or something that hints about that interrupt)?
By the way, to me, this almost looks more like a constant than a mask. Does it become clearer that these are masks if we define them with BIT macro?
#define ADE9000_MASK0_EGYRDY_MASK BIT(0) ?
drivers/iio/adc/ade9000.c line 148
Quoted 3 lines
drivers/iio/adc/ade9000.c new file +3
-0+ +/* Disable all interrupts */ +#define ADE9000_MASK1 0x00000000
Same suggestion would apply here although this really looks like a constant. ADE9000_MASK1_INT_DISABLE maybe?
drivers/iio/adc/ade9000.c line 151
Quoted 3 lines
drivers/iio/adc/ade9000.c new file +3
-0+ +/* Events disabled */ +#define ADE9000_EVENT_MASK 0x00000000
I would call these predefined values just constants and drop the mask part of the name.
Quoted 7 lines
drivers/iio/adc/ade9000.c new file +7
-0+ +/* +* Assuming Vnom=1/2 of full scale. +* Refer to Technical reference manual for detailed calculations. +*/ +#define ADE9000_VLEVEL 0x0022EA28 +
...
drivers/iio/adc/ade9000.c line 336
Quoted 6 lines
drivers/iio/adc/ade9000.c new file +6
-0+ +static unsigned long ade9000_clkout_recalc_rate (struct clk_hw *hw, +unsigned long parent_rate) +{ +/* CLKOUT provides the same frequency as the crystal/external clock */ +return parent_rate ? parent_rate :24576000 ;/* Default 24.576 MHz */
This value is a data sheet constant and is used twice in the driver. Does it make sense to have a define for it?
#define ADE9000_DEFAULT_CLK_FREQ_HZ 24576000 ?
Quoted 7 lines
drivers/iio/adc/ade9000.c new file +7
-0+} + +static const struct clk_ops ade9000_clkout_ops = { + .prepare = ade9000_clkout_prepare, + .unprepare = ade9000_clkout_unprepare, + .recalc_rate = ade9000_clkout_recalc_rate, +};
...
Quoted 9 lines
drivers/iio/adc/ade9000.c new file +9
-0+ +static int ade9000_probe (struct spi_device *spi) +{ +struct device *dev = &spi->dev; +struct iio_dev *indio_dev; +struct ade9000_state *st; +struct regmap *regmap; +int irq; +int ret;
...
drivers/iio/adc/ade9000.c line 1939
Quoted 7 lines
drivers/iio/adc/ade9000.c new file +7
-0+ +/* Optional external clock input */ + st->clkin =devm_clk_get_optional_enabled (dev,"clkin" ); +if (IS_ERR (st->clkin)) +return dev_err_probe (dev,PTR_ERR (st->clkin),"Failed to get and enable clkin: %ld" ,PTR_ERR (st->clkin)); + +/* Register clock output provider */
If I'm correctly reading ADE9000 data sheet, CLKOUT will have one of the connections to the external crystal when a crystal is used. So, maybe condition the clock provider feature to the supply of an external CMOS clock (not crystal)? Also, the description of CLKIN pin gives me the impression that the device requires an external clock input (either crystal or CMOS clock) so clkin would not be optional.
Quoted 13 lines
drivers/iio/adc/ade9000.c new file +13
-0+if (device_property_present (dev,"#clock-cells" )) { +struct clk_init_data clk_init = {}; +struct clk *clkout; +unsigned long parent_rate =24576000 ;/* Default crystal frequency */ + +if (st->clkin) + parent_rate =clk_get_rate (st->clkin); + + clk_init.name ="clkout" ; + clk_init.ops = &ade9000_clkout_ops; + clk_init.flags =CLK_GET_RATE_NOCACHE ; + clk_init.num_parents =0 ; +
drivers/iio/adc/ade9000.c line 1962
Quoted 10 lines
drivers/iio/adc/ade9000.c new file +10
-0+ st->clkout_hw.init = &clk_init; + + clkout =devm_clk_register (dev, &st->clkout_hw); +if (IS_ERR (clkout)) +return dev_err_probe (dev,PTR_ERR (clkout),"Failed to register clkout: %ld" ,PTR_ERR (clkout)); + + ret =devm_of_clk_add_hw_provider (dev, of_clk_hw_simple_get, &st->clkout_hw); +if (ret) +return dev_err_probe (dev, ret,"Failed to add clock provider: %d" , ret); + }
Best regards,
Marcelo
Re: [PATCH v3 3/4] iio: adc: add ade9000 support
In reply to Antoniu MiclausLe 08/08/2025 à 16:10, Antoniu Miclaus a écrit :
Quoted 3 lines
Add driver support for the ade9000. highly accurate, fully integrated, multiphase energy and power quality monitoring device.
Hi,
...
drivers/iio/adc/ade9000.c line 330
Quoted 8 lines
drivers/iio/adc/ade9000.c new file +8
-0+static int ade9000_clkout_prepare (struct clk_hw *hw) +{ +return 0 ; +} + +static void ade9000_clkout_unprepare (struct clk_hw *hw) +{ +}
Is it really needed to have these 2 empty functions? clk core seems to accept NULL for .prepare and .unprepare.
Quoted 13 lines
drivers/iio/adc/ade9000.c new file +13
-0+ +static unsigned long ade9000_clkout_recalc_rate (struct clk_hw *hw, +unsigned long parent_rate) +{ +/* CLKOUT provides the same frequency as the crystal/external clock */ +return parent_rate ? parent_rate :24576000 ;/* Default 24.576 MHz */ +} + +static const struct clk_ops ade9000_clkout_ops = { + .prepare = ade9000_clkout_prepare, + .unprepare = ade9000_clkout_unprepare, + .recalc_rate = ade9000_clkout_recalc_rate, +};
...
Quoted 9 lines
drivers/iio/adc/ade9000.c new file +9
-0+static irqreturn_t ade9000_irq0_thread (int irq,void *data) +{ +struct iio_dev *indio_dev = data; +struct ade9000_state *st =iio_priv (indio_dev); +s64 timestamp =iio_get_time_ns (indio_dev); +u32 handled_irq =0 ; +u32 interrupts; +u32 status; +int ret;
drivers/iio/adc/ade9000.c line 908
Quoted 10 lines
drivers/iio/adc/ade9000.c new file +10
-0+ + ret =regmap_read (st->regmap,ADE9000_REG_STATUS0 , &status); +if (ret) { +dev_err (&st->spi->dev,"IRQ0 read status fail" ); +return IRQ_HANDLED ; + } + + ret =regmap_read (st->regmap,ADE9000_REG_MASK0 , &interrupts); +if (ret) { +dev_err (&st->spi->dev,"IRQ0 read status fail" );
s/status/interrupts/ maybe?
Quoted 50 lines
drivers/iio/adc/ade9000.c new file +50
-0+return IRQ_HANDLED ; + } + +if ((status &ADE9000_ST0_PAGE_FULL_BIT ) && + (interrupts &ADE9000_ST0_PAGE_FULL_BIT )) { +/* Always use streaming mode */ + ret =ade9000_iio_push_streaming (indio_dev); +if (ret) { +dev_err (&st->spi->dev,"IRQ0 IIO push fail" ); +return IRQ_HANDLED ; + } + + handled_irq |=ADE9000_ST0_PAGE_FULL_BIT ; + } + +if ((status &ADE9000_ST0_WFB_TRIG_BIT ) && + (interrupts &ADE9000_ST0_WFB_TRIG_BIT )) { + ret =ade9000_waveform_buffer_en (st, false); +if (ret) { +dev_err (&st->spi->dev,"IRQ0 WFB fail" ); +return IRQ_HANDLED ; + } + + ret =ade9000_iio_push_buffer (indio_dev); +if (ret) { +dev_err (&st->spi->dev,"IRQ0 IIO push fail @ WFB TRIG" ); +return IRQ_HANDLED ; + } + + handled_irq |=ADE9000_ST0_WFB_TRIG_BIT ; + } + +if ((status &ADE9000_ST0_EGYRDY ) && + (interrupts &ADE9000_ST0_EGYRDY )) { +iio_push_event (indio_dev, +IIO_UNMOD_EVENT_CODE (IIO_ENERGY , +ADE9000_ST0_EGYRDY , +IIO_EV_TYPE_MAG , +IIO_EV_DIR_NONE ), + timestamp); + + handled_irq |=ADE9000_ST0_EGYRDY ; + } + + ret =regmap_write (st->regmap,ADE9000_REG_STATUS0 , handled_irq); +if (ret) +dev_err (&st->spi->dev,"IRQ0 write status fail" ); + +return IRQ_HANDLED ; +}
...
Quoted 48 lines
drivers/iio/adc/ade9000.c new file +48
-0+static int ade9000_probe (struct spi_device *spi) +{ +struct device *dev = &spi->dev; +struct iio_dev *indio_dev; +struct ade9000_state *st; +struct regmap *regmap; +int irq; +int ret; + + indio_dev =devm_iio_device_alloc (dev,sizeof (*st)); +if (!indio_dev) +return dev_err_probe (dev, -ENOMEM ,"Unable to allocate ADE9000 IIO" ); + st =iio_priv (indio_dev); + + regmap =devm_regmap_init (dev,NULL , spi, &ade9000_regmap_config); +if (IS_ERR (regmap)) +return dev_err_probe (dev,PTR_ERR (regmap),"Unable to allocate ADE9000 regmap" ); +spi_set_drvdata (spi, st); + + irq =fwnode_irq_get_byname (dev_fwnode (dev),"irq0" ); +if (irq <0 ) +return dev_err_probe (dev, -EINVAL ,"Unable to find irq0" ); + + ret =devm_request_threaded_irq (dev, irq,NULL , + ade9000_irq0_thread, +IRQF_ONESHOT , +KBUILD_MODNAME , indio_dev); +if (ret) +return dev_err_probe (dev, ret,"Failed to request threaded irq: %d" , ret); + + irq =fwnode_irq_get_byname (dev_fwnode (dev),"irq1" ); +if (irq <0 ) +return dev_err_probe (dev, -EINVAL ,"Unable to find irq1" ); + + ret =devm_request_threaded_irq (dev, irq,NULL , + ade9000_irq1_thread, +IRQF_ONESHOT , +KBUILD_MODNAME , indio_dev); +if (ret) +return dev_err_probe (dev, ret,"Failed to request threaded irq: %d" , ret); + + irq =fwnode_irq_get_byname (dev_fwnode (dev),"dready" ); +if (irq <0 ) +return dev_err_probe (dev, -EINVAL ,"Unable to find dready" ); + + ret = devm_request_threaded_irq(dev, irq, NULL, + ade9000_dready_thread, + IRQF_ONESHOT,
drivers/iio/adc/ade9000.c line 1937
Quoted 10 lines
drivers/iio/adc/ade9000.c new file +10
-0+ KBUILD_MODNAME, indio_dev); +if (ret) +return dev_err_probe (dev, ret,"Failed to request threaded irq: %d" , ret); + + st->spi = spi; + +/* Optional external clock input */ + st->clkin =devm_clk_get_optional_enabled (dev,"clkin" ); +if (IS_ERR (st->clkin)) +return dev_err_probe (dev,PTR_ERR (st->clkin),"Failed to get and enable clkin: %ld" ,PTR_ERR (st->clkin));
No need to dupliacte PTR_ERR(clkin) in the error message. Same for several messages above.
drivers/iio/adc/ade9000.c line 1946
Quoted 9 lines
drivers/iio/adc/ade9000.c new file +9
-0+ +/* Register clock output provider */ +if (device_property_present (dev,"#clock-cells" )) { +struct clk_init_data clk_init = {}; +struct clk *clkout; +unsigned long parent_rate =24576000 ;/* Default crystal frequency */ + +if (st->clkin) + parent_rate =clk_get_rate (st->clkin);
parent_rate seems to be unused.
drivers/iio/adc/ade9000.c line 1951
Quoted 5 lines
drivers/iio/adc/ade9000.c new file +5
-0+ + clk_init.name ="clkout" ; + clk_init.ops = &ade9000_clkout_ops; + clk_init.flags =CLK_GET_RATE_NOCACHE ; + clk_init.num_parents =0 ;
Would it make sense to have clk_init a "static const struct clk_init_data" defined outside of the probe to have the code be less verbose?
drivers/iio/adc/ade9000.c line 1957
Quoted 6 lines
drivers/iio/adc/ade9000.c new file +6
-0+ + st->clkout_hw.init = &clk_init; + + clkout =devm_clk_register (dev, &st->clkout_hw); +if (IS_ERR (clkout)) +return dev_err_probe (dev,PTR_ERR (clkout),"Failed to register clkout: %ld" ,PTR_ERR (clkout));
No need to dupliacte PTR_ERR(clkout) in the error message.
drivers/iio/adc/ade9000.c line 1961
Quoted 4 lines
drivers/iio/adc/ade9000.c new file +4
-0+ + ret =devm_of_clk_add_hw_provider (dev, of_clk_hw_simple_get, &st->clkout_hw); +if (ret) +return dev_err_probe (dev, ret,"Failed to add clock provider: %d" , ret);
No need to dupliacte ret in the error message.
Quoted 33 lines
drivers/iio/adc/ade9000.c new file +33
-0+ } + + indio_dev->name =spi_get_device_id (spi)->name; + indio_dev->dev.parent = &st->spi->dev; + indio_dev->info = &ade9000_info; + indio_dev->modes =INDIO_DIRECT_MODE |INDIO_BUFFER_SOFTWARE ; + indio_dev->setup_ops = &ade9000_buffer_ops; + + st->regmap = regmap; + + ret =devm_regulator_get_enable (&spi->dev,"vdd" ); +if (ret) +return dev_err_probe (&spi->dev, ret, +"Failed to get and enable vdd regulator\n" ); + + ret =devm_regulator_get_enable_optional (dev,"vref" ); +if (ret <0 && ret != -ENODEV ) +return dev_err_probe (dev, ret, +"Failed to get and enable vref regulator\n" ); + +/* Configure reference selection based on vref regulator availability */ +if (ret != -ENODEV ) { + ret =regmap_update_bits (st->regmap,ADE9000_REG_CONFIG1 , +ADE9000_EXT_REF_MASK , +ADE9000_EXT_REF_MASK ); +if (ret) +return ret; + } + + indio_dev->channels = ade9000_channels; + indio_dev->num_channels =ARRAY_SIZE (ade9000_channels); + ret =devm_iio_kfifo_buffer_setup (dev, indio_dev, + &ade9000_buffer_ops);
drivers/iio/adc/ade9000.c line 2004
Quoted 10 lines
drivers/iio/adc/ade9000.c new file +10
-0+if (ret) +return dev_err_probe (dev, ret,"Failed to setup IIO buffer" ); + + ret =ade9000_reset (st); +if (ret) +return dev_err_probe (dev, ret,"ADE9000 reset failed" ); + + ret =ade9000_setup (st); +if (ret) +return dev_err_probe (dev, ret,"Unable to setup ADE9000" );
Missing \n at the end of the message. Same for some other messages above using dev_err_probe() or dev_err().
drivers/iio/adc/ade9000.c line 2007
Quoted 3 lines
drivers/iio/adc/ade9000.c new file +3
-0+ +return devm_iio_device_register (dev, indio_dev); +};
...
CJ
Antoniu Miclaus
maintainerPATCH v3 4/4 Documentation: ABI: iio: add sinc4+lp
In reply to Antoniu MiclausAdd new filter type to the sysfs-bus-iio ABI documentation:
- "sinc4+lp" for Sinc4 + Low Pass Filter
---
changes in v3:
- change from "sinc4+iir" to "sinc4+lp" for better generic naming
- remove "dsp" filter type
Documentation/ABI/testing/sysfs-bus-iio | 1 + 1 file changed, 1 insertion(+)
Documentation/ABI/testing/sysfs-bus-iio +1 -0
@@ -2309,6 +2309,7 @@ Description: * "sinc5+pf1" - Sinc5 + device specific Post Filter 1. * "wideband" - filter with wideband low ripple passband and sharp transition band. + * "sinc4+lp" - Sinc4 + Low Pass Filter. What: /sys/bus/iio/devices/iio:deviceX/filter_type What: /sys/bus/iio/devices/iio:deviceX/in_voltageY-voltageZ_filter_type
Signature · 1 line
2.43.0
Re: [PATCH v3 4/4] Documentation: ABI: iio: add sinc4+lp
Quoted 16 lines · Antoniu Miclaus
Add new filter type to the sysfs-bus-iio ABI documentation:
- "sinc4+lp" for Sinc4 + Low Pass FilterSigned-off-by Antoniu Miclaus <[email protected]>---
changes in v3:
- change from "sinc4+iir" to "sinc4+lp" for better generic naming
- remove "dsp" filter typeDocumentation/ABI/testing/sysfs-bus-iio | 1 + 1 file changed, 1 insertion(+)Documentation/ABI/testing/sysfs-bus-iio +0
-0
Documentation/ABI/testing/sysfs-bus-iio Description: · line 2313
Quoted 5 lines
Documentation/ABI/testing/sysfs-bus-iio +1
-0@@ -2309,6 +2309,7 @@ Description: * "sinc5+pf1" - Sinc5 + device specific Post Filter 1. * "wideband" - filter with wideband low ripple passband and sharp transition band. + * "sinc4+lp" - Sinc4 + Low Pass Filter.
Not entering the discussion about what would be a proper name for the filter but, since we had a patch putting filter ABI doc in alphabetical order.
https://lore.kernel.org/linux-iio/20250710-iio-adc-ad7137-add-filter-support-v1-4-acffe401c4d2@baylibre.com/
I guess we keep the docs organized by adding "sinc4+lp" between "sinc4" and "sinc4+sinc1" (if keeping that filter name of course).
Quoted 6 lines
Documentation/ABI/testing/sysfs-bus-iio +0
-0What: /sys/bus/iio/devices/iio:deviceX/filter_type What: /sys/bus/iio/devices/iio:deviceX/in_voltageY-voltageZ_filter_typeSignature · 1 line
2.43.0