Add support for AD4062 device family
Subsystems (6)
Jorge Marques
maintainerv4 cover Add support for AD4062 device family
The AD4060/AD4062 are versatile, 16-bit/12-bit, successive approximation register (SAR) analog-to-digital converter (ADC).
The device uses a 2-wire I3C interface. The device simplifies acquisition by providing 4-bytes in the register map, signal-extending the sample reading accordingly.
The device has autonomous monitoring capabilities, that are exposed as IIO events. Since register access requires leaving the monitoring state and returning, any device access exits monitoring mode, disabling the IIO event.
The device contains two optional outputs:
- gp0: ADC conversion ready signal on the falling edge.
The user should either invert the signal or set the IRQ as falling edge.
- gp1: Threshold either event interrupt on the rising edge.
The devices utilizes PM to enter the low power mode.
The devices datasheet:
https://www.analog.com/media/en/technical-documentation/data-sheets/ad4060.pdf
https://www.analog.com/media/en/technical-documentation/data-sheets/ad4062.pdf
The monitoring capabilities, I3C protocol, and multiple GPIOs were the decision factor to have a standalone driver for this device family. The device is expected to work with any I3C Bus. I tested the device with with off-the-shelf I3C controllers STM32H7 (baremetal only) and the open-source ADI I3C Controller (with Linux driver):
https://analogdevicesinc.github.io/hdl/library/i3c_controller/index.html
ADI I3C Controller lore:
https://lore.kernel.org/linux-i3c/[email protected]/
The series is divided in 3 blocks, adding:
- The base driver.
- An software IIO trigger: captures samples continuously.
- IIO events support: exposes the device's threshold monitoring capability.
The device internal clock register is exposed twice, as sampling_frequency and events/sampling_frequency, storing in distinct state variables, since the usage (burst averaging mode and monitor mode) cannot be executed at the same time.
Non-implemented features:
- Averaging mode: Similar to burst averaging mode used in the oversampling, but requiring a sequence of CNV triggers for each conversion.
- Trigger mode: Similar to monitor mode used in the monitoring mode, but exits to configuration mode on event.
This device is almost identical to AD4052 family, but I decided to submit the AD4062 before re-submitting AD4052 to better contextualize the focus of the device family (high latency, medium-speed protocol, low-power autonomous monitoring rather than high-throughput acquisition).
Depending on the resolution of this driver, the AD4052 family may be added to it, by splitting into ad4062_i3c.c, ad4062_spi.c, ad4062_core.c, or as a standalone driver ad4052.c.
Depends on:
https://lore.kernel.org/linux-i3c/aRYLc%2F+KAD13g7T7@lizhi-Precision-Tower-5810/T/#t
(for devm ibi clean-up)
---
Changes in v4:
- dt-bindings:
* Simplification of interrupts description
* Add minItems to interrupt-names, now symmetrical to interrupts.
- docs/ad4052.rst:
* Drop "Unimplemented features" list (normally forgotten in the long term).
- Move scan_type implementation to iio trigger commit.
- Move #include sysfs.h to iio event commit (used by events/sampling_frequency)
- Fix avg_max type (u8 -> u16) to fit all variants.
- Assert val2 != 0, -EINVAL at oversampling_ratio, store bit position
instead of resolved value
- Use u32 instead of u64 arithmetics on all possible locations.
- Fix regression at ad4062_get_sampling_frequency missing IIO_VAL_INT return.
- Use be*_to_cpu/cpu_to_be* instead of get/put_unaligned_u*
- Use devm_add_action for IBI
- Refactor methods to use _dispatch methods to, for future usage with "ACQUIRE for claim direct mode".
- Fix regression on GPIO configuration, introduced by misuse of masks.
- Simplify calibscale by using IIO_VAL_FRACTIONAL_LOG2.
- Simplify read_chan_raw by using "un-optimized" option for sample
read, to not depend on scan_type (triggered buffer uses the optimized version).
- Fix non-compile constant ad4060_chip_info.prod_id at i3c_device_id by
using defines AD406*_PROD_ID (kernel ci caught).
- Fix scan-type realbits/storagebits.
- Introduce st->conv_sizeof, st->conv_addr for the optimize triggered
buffer readings (lest protocol overhead). Adds intuitive ad4062_sizeof_storagebits() and ad4062_get_conv_addr() helpers for the changing scan_type (due to oversampling).
- Use iio_push_to_buffers_with_ts
- Review chan_scale, add clearer units and move "signed" comment to the vref_uV multiplication, as in the datasheet (result is unchanged).
- Use devm for trigger_work.
- Use clearer fieldbit-dependent expression like
val != sign_extend32(val, AD4062_LIMIT_BITS - 1) instead of magic numbers val > 2047 || val < -2048
- Fix regression on range for threshold value.
- Fix regression on enter MONITOR_MODE by adding missing address point
write
- Note: false checkpatch positive at "iio: adc: ad4062: Add IIO Trigger support" due to macro change adding "bits", used by the ext_scan_type.
- Link to v3: https://lore.kernel.org/r/[email protected]
Changes in v3:
- dt-bidings:
* Add minItems to interrupt-names, to match interrupts.
* Reword descriptions.
- Add () to methods in commit messages.
- Group defines by context, adding blanking line between.
- Change ad4062_conversion_freqs from int to unsigned int
- Rename vref_uv to vref_uV
- Re-order state struct, to save some bytes.
- At oversampling_ratio(), use in_range()
- Add formulas, units where appropriate.
- Tune reset delay (data sheet value) and sleep mode resume delay (experimental).
- Rework st->oversamp_ratio to store the exponent, so 0 is ratio 1 (2**0).
- Rework get_chan_calibscale() to use IIO_VAL_FRACTIONAL_LOG2, simplifying the logic.
- Rework set_chan_calibscale() to not use 64-bits arithmetics, and provide formulas, and comments.
- Merge __ad4062_read_chan_raw() into ad4062_read_chan_raw(), since was the only consumer.
- Optimize readings with GPO set as DATA_READY, by using CONV_READ register (roughly doubles the effective sample rate).
- Use new ACQUIRE macros for pm.
- Use devm wrapper for INIT_WORK, cancelling on driver removal, resolving exception if the duffer was enabled during removal.
- When possible, use return ret ?:.
- Use IIO_DEVICE_ATTR_RW() instead of IIO_DEVICE_ATTR().
- Use AD4062_LIMIT_BITS - 1 or BIT(x) - 1 to indicate hw limit/field size.
- Explain in the commit message why gpio-regmap cannot be used, and gpio-controller is used instead.
- Return relational operator directly (has type int).
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- dt-bindings:
* add a short description of all mode that can be configured to during runtime.
* add gpio-controller, to expose GPs not listed in interrupt-names as
a GPO.
- sampling_frequency is the duration of a single sample (convert-start
high edge until RDY falling edge) ((n_avg - 1) / fosc + tconv)
- Remove .grade from chip_info, since the supported devices have a
single speed grade.
- Update state buffer to use dma-aligned union of __be32, __be16, u8 bytes[4].
- Use standard IIO_CHAN_INFO_SAMP_FREQ and _AVAIL
- Add defines to magic numbers.
- Ensure commits only contain code related to the particular commit.
- Use new ACQUIRE pm macros.
- Drop lock for debugfs, let user mess the state thorugh the debug
interface.
- Restructure vio, vdd, ref voltages, only read if needed.
- Have error handling on top.
- Drop unnecessary check_ids error message.
- Use devm for IBI remove (requires patch on i3c subystem).
- Use heap buffers for all i3c_priv_xfer.
- Use CONV_READ if GP1 is routed (less overhead), use CONV_TRIGGER for
IBI fallback.
- Drop usage pm_runtime_mark_last_busy, since it is now internal to pm_runtime_put_autosuspend
- Don't allow access if monitor mode is enabled, return -EBUSY.
- Implement gpio-controller to expose GPs not listed in interrupt-names
as a GPO.
- Value in mv as ``raw * _scale`` (embed caliscale).
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Jorge Marques (9):
dt-bindings: iio: adc: Add adi,ad4062
docs: iio: New docs for ad4062 driver
iio: adc: Add support for ad4062
docs: iio: ad4062: Add IIO Trigger support
iio: adc: ad4062: Add IIO Trigger support
docs: iio: ad4062: Add IIO Events support
iio: adc: ad4062: Add IIO Events support
docs: iio: ad4062: Add GPIO Controller support
iio: adc: ad4062: Add GPIO Controller support
.../devicetree/bindings/iio/adc/adi,ad4062.yaml | 120 ++ Documentation/iio/ad4062.rst | 148 ++ Documentation/iio/index.rst | 1 + MAINTAINERS | 8 + drivers/iio/adc/Kconfig | 13 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/ad4062.c | 1609 ++++++++++++++++++++ 7 files changed, 1900 insertions(+)
---
base-commit: f9e05791642810a0cf6237d39fafd6fec5e0b4bb
change-id: 20251011-staging-ad4062-20d897d33ab6
prerequisite-change-id: 20251112-ibi-unsafe-48f343e178b8:v1
prerequisite-patch-id: 5f04cbbca0fcc3657c7a4d254656b03e289ad222
prerequisite-message-id: aReN+P5UZTnS1Tww@lizhi-Precision-Tower-5810
Best regards,
Signature · 1 line
Jorge Marques <[email protected]>
Jorge Marques
maintainerPATCH v4 1/9 dt-bindings: iio: adc: Add adi,ad4062
Add dt-bindings for AD4062 family, devices AD4060/AD4062, low-power with monitor capabilities SAR ADCs. Each variant of the family differs in resolution. The device contains two outputs (gp0, gp1). The outputs can be configured for range of options, such as threshold and data ready. The device uses a 2-wire I3C interface.
--- .../devicetree/bindings/iio/adc/adi,ad4062.yaml | 120 +++++++++++++++++++++ MAINTAINERS | 6 ++ 2 files changed, 126 insertions(+)
Documentation/devicetree/bindings/iio/adc/adi,ad4062.yaml new file +120 -0
@@ -0,0 +1,120 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +# Copyright 2025 Analog Devices Inc. +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/adc/adi,ad4062.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Analog Devices AD4062 ADC family device driver + +maintainers: + - Jorge Marques <[email protected]> + +description: | + Analog Devices AD4062 Single Channel Precision SAR ADC family + + https://www.analog.com/media/en/technical-documentation/data-sheets/ad4060.pdf + https://www.analog.com/media/en/technical-documentation/data-sheets/ad4062.pdf + +properties: + compatible: + enum: + - adi,ad4060 + - adi,ad4062 + + reg: + maxItems: 1 + + interrupts: + description: + Two pins are available that can be configured as either a general purpose + digital output, device enable signal (used to synchronise other parts of + the signal chain with ADC sampling), device ready (GP1 only) or various + interrupt signals. If intended for use as a GPIO or device enable, will not + present here. + minItems: 1 + items: + - description: + GP0 pin, cannot be configured as DEV_RDY. + - description: + GP1 pin, can be configured to any setting. + + interrupt-names: + minItems: 1 + items: + - const: gp0 + - const: gp1 + + gpio-controller: + description: + Marks the device node as a GPIO controller. GPs not listed as interrupts + are exposed as a GPO. + + '#gpio-cells': + const: 2 + description: + The first cell is the GPIO number and the second cell specifies + GPIO flags, as defined in <dt-bindings/gpio/gpio.h>. + + vdd-supply: + description: Analog power supply. + + vio-supply: + description: Digital interface logic power supply. + + ref-supply: + description: + Reference voltage to set the ADC full-scale range. If not present, + vdd-supply is used as the reference voltage. + +required: + - compatible + - reg + - vdd-supply + - vio-supply + +allOf: + - $ref: /schemas/i3c/i3c.yaml# + +unevaluatedProperties: false + +examples: + - | + #include <dt-bindings/gpio/gpio.h> + #include <dt-bindings/interrupt-controller/irq.h> + + i3c { + #address-cells = <3>; + #size-cells = <0>; + + adc@0,2ee007c0000 { + reg = <0x0 0x2ee 0x7c0000>; + vdd-supply = <&vdd>; + vio-supply = <&vio>; + ref-supply = <&ref>; + + interrupt-parent = <&gpio>; + interrupts = <0 0 IRQ_TYPE_EDGE_RISING>, + <0 1 IRQ_TYPE_EDGE_FALLING>; + interrupt-names = "gp0", "gp1"; + }; + }; + + - | + #include <dt-bindings/gpio/gpio.h> + #include <dt-bindings/interrupt-controller/irq.h> + + i3c { + #address-cells = <3>; + #size-cells = <0>; + + adc@0,2ee007c0000 { + reg = <0x0 0x2ee 0x7c0000>; + vdd-supply = <&vdd>; + vio-supply = <&vio>; + ref-supply = <&ref>; + + gpio-controller; + #gpio-cells = <2>; + }; + };
MAINTAINERS +6 -0
@@ -1432,6 +1432,12 @@ F: Documentation/devicetree/bindings/iio/adc/adi,ad4030.yaml F: Documentation/iio/ad4030.rst F: drivers/iio/adc/ad4030.c +ANALOG DEVICES INC AD4062 DRIVER +M: Jorge Marques <[email protected]> +S: Supported +W: https://ez.analog.com/linux-software-drivers +F: Documentation/devicetree/bindings/iio/adc/adi,ad4062.yaml + ANALOG DEVICES INC AD4080 DRIVER M: Antoniu Miclaus <[email protected]> L: [email protected]
Signature · 1 line
2.51.1
Re: [PATCH v4 1/9] dt-bindings: iio: adc: Add adi,ad4062
Quoted 6 lines · Jorge Marques
Add dt-bindings for AD4062 family, devices AD4060/AD4062, low-power with monitor capabilities SAR ADCs. Each variant of the family differs in resolution. The device contains two outputs (gp0, gp1). The outputs can be configured for range of options, such as threshold and data ready. The device uses a 2-wire I3C interface.
Quoted 6 lines
Signed-off-by Jorge Marques <[email protected]>--- .../devicetree/bindings/iio/adc/adi,ad4062.yaml | 120 +++++++++++++++++++++ MAINTAINERS | 6 ++ 2 files changed, 126 insertions(+)
Jorge Marques
maintainerPATCH v4 2/9 docs: iio: New docs for ad4062 driver
In reply to Jorge MarquesThis adds a new page to document how to use the ad4062 ADC driver.
--- Documentation/iio/ad4062.rst | 86 ++++++++++++++++++++++++++++++++++++++++++++ Documentation/iio/index.rst | 1 + MAINTAINERS | 1 + 3 files changed, 88 insertions(+)
Documentation/iio/ad4062.rst new file +86 -0
@@ -0,0 +1,86 @@ +.. SPDX-License-Identifier: GPL-2.0-only + +============= +AD4062 driver +============= + +ADC driver for Analog Devices Inc. AD4060/AD4062 devices. The module name is +``ad4062``. + +Supported devices +================= + +The following chips are supported by this driver: + +* `AD4060 <https://www.analog.com/AD4060>`_ +* `AD4062 <https://www.analog.com/AD4062>`_ + +Wiring modes +============ + +The ADC is interfaced through an I3C bus, and contains two programmable GPIOs. + +The ADC convert-start happens on the SDA rising edge of the I3C stop (P) bit +at the end of the read command. + +The two programmable GPIOS are optional and have a role assigned if present in +the devicetree ``interrupt-names`` property: + +- GP1: Is assigned the role of Data Ready signal. + +Device attributes +================= + +The ADC contains only one channel with following attributes: + +.. list-table:: Channel attributes + :header-rows: 1 + + * - Attribute + - Description + * - ``in_voltage_calibscale`` + - Sets the gain scaling factor that the hardware applies to the sample, + to compensate for system gain error. + * - ``in_voltage_oversampling_ratio`` + - Sets device's burst averaging mode to over sample using the + internal sample rate. Value 1 disable the burst averaging mode. + * - ``in_voltage_oversampling_ratio_available`` + - List of available oversampling values. + * - ``in_voltage_raw`` + - Returns the raw ADC voltage value. + * - ``in_voltage_scale`` + - Returns the channel scale in reference to the reference voltage + ``ref-supply`` or ``vdd-supply`` if the former not present. + +Also contain the following device attributes: + +.. list-table:: Device attributes + :header-rows: 1 + + * - Attribute + - Description + * - ``sampling_frequency`` + - Sets the duration of a single scan, used in the burst averaging mode. + The duration is described by ``(n_avg - 1) / fosc + tconv``, where + ``n_avg`` is the oversampling ratio, ``fosc`` is the internal sample + rate and ``tconv`` is the ADC conversion time. + * - ``sampling_frequency_available`` + - Lists the available sampling frequencies, computed on the current + oversampling ratio. If the ratio is 1, the frequency is ``1/tconv``. + +Interrupts +========== + +The interrupts are mapped through the ``interrupt-names`` and ``interrupts`` +properties. + +The ``interrupt-names`` ``gp1`` entry sets the role of Data Ready signal. +If it is not present, the driver fallback to enabling the same role as an +I3C IBI. + +Low-power mode +============== + +The device enters low-power mode on idle to save power. Enabling an event puts +the device out of the low-power since the ADC autonomously samples to assert +the event condition.
Documentation/iio/index.rst +1 -0
@@ -22,6 +22,7 @@ Industrial I/O Kernel Drivers ad3552r ad4000 ad4030 + ad4062 ad4695 ad7191 ad7380
MAINTAINERS +1 -0
@@ -1437,6 +1437,7 @@ M: Jorge Marques <[email protected]> S: Supported W: https://ez.analog.com/linux-software-drivers F: Documentation/devicetree/bindings/iio/adc/adi,ad4062.yaml +F: Documentation/iio/ad4062.rst ANALOG DEVICES INC AD4080 DRIVER M: Antoniu Miclaus <[email protected]>
Signature · 1 line
2.51.1
Jorge Marques
maintainerPATCH v4 3/9 iio: adc: Add support for ad4062
In reply to Jorge MarquesThe AD4060/AD4062 are versatile, 16-bit/12-bit, successive approximation register (SAR) analog-to-digital converter (ADC) with low-power and threshold monitoring modes.
--- MAINTAINERS | 1 + drivers/iio/adc/Kconfig | 11 + drivers/iio/adc/Makefile | 1 + drivers/iio/adc/ad4062.c | 819 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 832 insertions(+)
MAINTAINERS +1 -0
@@ -1438,6 +1438,7 @@ S: Supported W: https://ez.analog.com/linux-software-drivers F: Documentation/devicetree/bindings/iio/adc/adi,ad4062.yaml F: Documentation/iio/ad4062.rst +F: drivers/iio/adc/ad4062.c ANALOG DEVICES INC AD4080 DRIVER M: Antoniu Miclaus <[email protected]>
drivers/iio/adc/Kconfig +11 -0
@@ -70,6 +70,17 @@ config AD4030 To compile this driver as a module, choose M here: the module will be called ad4030. +config AD4062 + tristate "Analog Devices AD4062 Driver" + depends on I3C + select REGMAP_I3C + help + Say yes here to build support for Analog Devices AD4062 I3C analog + to digital converters (ADC). + + To compile this driver as a module, choose M here: the module will be + called ad4062. + config AD4080 tristate "Analog Devices AD4080 high speed ADC" depends on SPI
drivers/iio/adc/Makefile +1 -0
@@ -11,6 +11,7 @@ obj-$(CONFIG_AB8500_GPADC) += ab8500-gpadc.o obj-$(CONFIG_AD_SIGMA_DELTA) += ad_sigma_delta.o obj-$(CONFIG_AD4000) += ad4000.o obj-$(CONFIG_AD4030) += ad4030.o +obj-$(CONFIG_AD4062) += ad4062.o obj-$(CONFIG_AD4080) += ad4080.o obj-$(CONFIG_AD4130) += ad4130.o obj-$(CONFIG_AD4170_4) += ad4170-4.o
drivers/iio/adc/ad4062.c new file +819 -0
819 lines not on this page · raw message
Signature · 1 line
2.51.1
Jorge Marques
maintainerPATCH v4 4/9 docs: iio: ad4062: Add IIO Trigger support
In reply to Jorge MarquesExplains the IIO Trigger support and timings involved.
--- Documentation/iio/ad4062.rst | 13 +++++++++++++ 1 file changed, 13 insertions(+)
Documentation/iio/ad4062.rst +13 -0
@@ -84,3 +84,16 @@ Low-power mode The device enters low-power mode on idle to save power. Enabling an event puts the device out of the low-power since the ADC autonomously samples to assert the event condition. + +IIO trigger support +=================== + +An IIO trigger ``ad4062-devX`` is registered by the driver to be used by the +same device, to capture samples to a software buffer. It is required to attach +the trigger to the device by setting the ``current_trigger`` before enabling +and reading the buffer. + +The acquisition is sequential and bounded by the protocol timings, software +latency and internal timings, the sample rate is not configurable. The burst +averaging mode does impact the effective sample rate, since it increases the +internal timing to output a single sample.
Signature · 1 line
2.51.1
Jorge Marques
maintainerPATCH v4 5/9 iio: adc: ad4062: Add IIO Trigger support
In reply to Jorge MarquesAdds support for IIO Trigger. Optionally, gp1 is assigned as Data Ready signal, if not present, fallback to an I3C IBI with the same role. The software trigger is allocated by the device, but must be attached by the user before enabling the buffer. The purpose is to not impede removing the driver due to the increased reference count when iio_trigger_set_immutable() or iio_trigger_get() is used.
--- drivers/iio/adc/Kconfig | 2 + drivers/iio/adc/ad4062.c | 272 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 268 insertions(+), 6 deletions(-)
drivers/iio/adc/Kconfig +2 -0
@@ -74,6 +74,8 @@ config AD4062 tristate "Analog Devices AD4062 Driver" depends on I3C select REGMAP_I3C + select IIO_BUFFER + select IIO_TRIGGERED_BUFFER help Say yes here to build support for Analog Devices AD4062 I3C analog to digital converters (ADC).
drivers/iio/adc/ad4062.c +266 -6
405 lines not on this page · raw message
Signature · 1 line
2.51.1
Jorge Marques
maintainerPATCH v4 6/9 docs: iio: ad4062: Add IIO Events support
In reply to Jorge MarquesExplains the IIO Events support.
--- Documentation/iio/ad4062.rst | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-)
Documentation/iio/ad4062.rst +39 -2
@@ -26,6 +26,7 @@ at the end of the read command. The two programmable GPIOS are optional and have a role assigned if present in the devicetree ``interrupt-names`` property: +- GP0: Is assigned the role of Threshold Either signal. - GP1: Is assigned the role of Data Ready signal. Device attributes
@@ -74,8 +75,10 @@ Interrupts The interrupts are mapped through the ``interrupt-names`` and ``interrupts`` properties. -The ``interrupt-names`` ``gp1`` entry sets the role of Data Ready signal. -If it is not present, the driver fallback to enabling the same role as an +The ``interrupt-names`` ``gp0`` entry sets the role of Threshold signal, and +entry ``gp1`` the role of Data Ready signal. + +If each is not present, the driver fallback to enabling the same role as an I3C IBI. Low-power mode
@@ -97,3 +100,37 @@ The acquisition is sequential and bounded by the protocol timings, software latency and internal timings, the sample rate is not configurable. The burst averaging mode does impact the effective sample rate, since it increases the internal timing to output a single sample. + +Threshold events +================ + +The ADC supports a monitoring mode to raise threshold events. The driver +supports a single interrupt for both rising and falling readings. + +The feature is enabled/disabled by setting ``thresh_either_en``. During monitor +mode, the device continuously operates in autonomous mode. Any register access +puts the device back in configuration mode, due to this, any access disables +monitor mode. + +The following event attributes are available: + +.. list-table:: Event attributes + :header-rows: 1 + + * - Attribute + - Description + * - ``sampling_frequency`` + - Frequency used in the monitoring mode, sets the device internal sample + rate when the mode is activated. + * - ``sampling_frequency_available`` + - List of available sample rates. + * - ``thresh_either_en`` + - Enable monitoring mode. + * - ``thresh_falling_hysteresis`` + - Set the hysteresis value for the minimum threshold. + * - ``thresh_falling_value`` + - Set the minimum threshold value. + * - ``thresh_rising_hysteresis`` + - Set the hysteresis value for the maximum threshold. + * - ``thresh_rising_value`` + - Set the maximum threshold value.
Signature · 1 line
2.51.1
Jorge Marques
maintainerPATCH v4 7/9 iio: adc: ad4062: Add IIO Events support
In reply to Jorge MarquesAdds support for IIO Events. Optionally, gp0 is assigned as Threshold Either signal, if not present, fallback to an I3C IBI with the same role.
--- drivers/iio/adc/ad4062.c | 407 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 406 insertions(+), 1 deletion(-)
drivers/iio/adc/ad4062.c +406 -1
@@ -14,7 +14,9 @@#include <linux/i3c/device.h> #include <linux/i3c/master.h> #include <linux/iio/buffer.h> +#include <linux/iio/events.h> #include <linux/iio/iio.h> +#include <linux/iio/sysfs.h> #include <linux/iio/trigger.h> #include <linux/iio/trigger_consumer.h> #include <linux/iio/triggered_buffer.h>
@@ -51,14 +53,22 @@#define AD4062_REG_ADC_CONFIG_SCALE_EN_MSK BIT(4)#define AD4062_REG_AVG_CONFIG 0x23#define AD4062_REG_GP_CONF 0x24 +#define AD4062_REG_GP_CONF_MODE_MSK_0 GENMASK(2, 0)#define AD4062_REG_GP_CONF_MODE_MSK_1 GENMASK(6, 4)#define AD4062_REG_INTR_CONF 0x25 +#define AD4062_REG_INTR_CONF_EN_MSK_0 GENMASK(1, 0)#define AD4062_REG_INTR_CONF_EN_MSK_1 GENMASK(5, 4)#define AD4062_REG_TIMER_CONFIG 0x27#define AD4062_REG_TIMER_CONFIG_FS_MASK GENMASK(7, 4) +#define AD4062_REG_MAX_LIMIT 0x29 +#define AD4062_REG_MIN_LIMIT 0x2B +#define AD4062_REG_MAX_HYST 0x2C +#define AD4062_REG_MIN_HYST 0x2D#define AD4062_REG_MON_VAL 0x2F#define AD4062_REG_ADC_IBI_EN 0x31#define AD4062_REG_ADC_IBI_EN_CONV_TRIGGER BIT(2) +#define AD4062_REG_ADC_IBI_EN_MAX BIT(1) +#define AD4062_REG_ADC_IBI_EN_MIN BIT(0)#define AD4062_REG_FUSE_CRC 0x40#define AD4062_REG_DEVICE_STATUS 0x41#define AD4062_REG_DEVICE_STATUS_DEVICE_RESET BIT(6)
@@ -78,9 +88,13 @@#define AD4060_PROD_ID 0x7A#define AD4062_PROD_ID 0x7C +#define AD4062_GP_INTR 0x1#define AD4062_GP_DRDY 0x2 +#define AD4062_LIMIT_BITS 12 +#define AD4062_INTR_EN_NEITHER 0x0 +#define AD4062_INTR_EN_EITHER 0x3#define AD4062_TCONV_NS 270
@@ -149,10 +163,12 @@ struct ad4062_state {struct iio_dev *indio_dev;struct i3c_device *i3cdev;struct regmap *regmap; +bool wait_event;int vref_uV;unsigned int samp_freqs[ARRAY_SIZE (ad4062_conversion_freqs)];bool gpo_irq[2 ];u16 sampling_frequency; +u16 events_frequency;u8 oversamp_ratio;u8 conv_sizeof;u8 conv_addr;
@@ -188,6 +204,26 @@ static const struct regmap_access_table ad4062_regmap_wr_table = { .n_yes_ranges = ARRAY_SIZE(ad4062_regmap_wr_ranges), }; +static const struct iio_event_spec ad4062_events[] = { + { + .type =IIO_EV_TYPE_THRESH , + .dir =IIO_EV_DIR_EITHER , + .mask_shared_by_all =BIT (IIO_EV_INFO_ENABLE ), + }, + { + .type =IIO_EV_TYPE_THRESH , + .dir =IIO_EV_DIR_RISING , + .mask_shared_by_all =BIT (IIO_EV_INFO_VALUE ) | +BIT (IIO_EV_INFO_HYSTERESIS ), + }, + { + .type =IIO_EV_TYPE_THRESH , + .dir =IIO_EV_DIR_FALLING , + .mask_shared_by_all =BIT (IIO_EV_INFO_VALUE ) | +BIT (IIO_EV_INFO_HYSTERESIS ), + }, +}; +#define AD4062_CHAN (bits) { \ .type = IIO_VOLTAGE, \ .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_RAW) | \
@@ -199,6 +235,8 @@ static const struct regmap_access_table ad4062_regmap_wr_table = { .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ .indexed = 1, \ .channel = 0, \ + .event_spec = ad4062_events, \ + .num_event_specs = ARRAY_SIZE(ad4062_events), \ .has_ext_scan_type = 1, \ .ext_scan_type = ad4062_scan_type_##bits##_s, \ .num_ext_scan_type = ARRAY_SIZE(ad4062_scan_type_##bits##_s), \
@@ -218,6 +256,73 @@ static const struct ad4062_chip_info ad4062_chip_info = { .avg_max = 4096, }; +static ssize_t sampling_frequency_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct ad4062_state *st =iio_priv (dev_to_iio_dev (dev)); + +return sysfs_emit (buf,"%d\n" , ad4062_conversion_freqs[st->events_frequency]); +} + +static int sampling_frequency_store_dispatch (struct iio_dev *indio_dev, +const char *buf) +{ +struct ad4062_state *st =iio_priv (indio_dev); +int val, ret; + +if (st->wait_event) +return -EBUSY ; + + ret =kstrtoint (buf,10 , &val); +if (ret) +return ret; + + st->events_frequency =find_closest_descending (val, ad4062_conversion_freqs, +ARRAY_SIZE (ad4062_conversion_freqs)); +return 0 ; +} + +static ssize_t sampling_frequency_store (struct device *dev, +struct device_attribute *attr, +const char *buf,size_t len) +{ +struct iio_dev *indio_dev =dev_to_iio_dev (dev); +int ret; + +if (!iio_device_claim_direct (indio_dev)) +return -EBUSY ; + + ret =sampling_frequency_store_dispatch (indio_dev, buf); +iio_device_release_direct (indio_dev); +return ret ?: len; +} + +static IIO_DEVICE_ATTR_RW(sampling_frequency, 0); + +static ssize_t sampling_frequency_available_show (struct device *dev, +struct device_attribute *attr, +char *buf) +{ +int ret =0 ; + +for (u8 i =0 ; i <ARRAY_SIZE (ad4062_conversion_freqs); i++) + ret +=sysfs_emit_at (buf, ret,"%d%s" , ad4062_conversion_freqs[i], + i != (ARRAY_SIZE (ad4062_conversion_freqs) -1 ) ?" " :"\n" ); +return ret; +} + +static IIO_DEVICE_ATTR_RO(sampling_frequency_available, 0); + +static struct attribute *ad4062_event_attributes[] = { + &iio_dev_attr_sampling_frequency.dev_attr.attr, + &iio_dev_attr_sampling_frequency_available.dev_attr.attr, +NULL +}; + +static const struct attribute_group ad4062_event_attribute_group = { + .attrs = ad4062_event_attributes, +}; +static int ad4062_set_oversampling_ratio (struct ad4062_state *st,int val,int val2) {const u32 _max = st->chip->avg_max;
@@ -344,9 +449,11 @@ static int ad4062_conversion_frequency_set(struct ad4062_state *st, u8 val)static int ad4062_set_operation_mode (struct ad4062_state *st,enum ad4062_operation_mode mode) { +const unsigned int samp_freq = mode ==AD4062_MONITOR_MODE ? + st->events_frequency : st->sampling_frequency;int ret; - ret =ad4062_conversion_frequency_set (st, st->sampling_frequency); + ret =ad4062_conversion_frequency_set (st, samp_freq);if (ret)return ret;
@@ -355,6 +462,17 @@ static int ad4062_set_operation_mode(struct ad4062_state *st,if (ret)return ret; +if (mode ==AD4062_MONITOR_MODE ) { +/* Change address pointer to enter monitor mode */ +struct i3c_priv_xfer xfer_trigger = { + .data.out = &st->conv_addr, + .len =sizeof (st->conv_addr), + .rnw = false, + }; + st->conv_addr =AD4062_REG_CONV_TRIGGER_32BITS ; +return i3c_device_do_priv_xfers (st->i3cdev, &xfer_trigger,1 ); + } +return regmap_write (st->regmap,AD4062_REG_MODE_SET ,AD4062_REG_MODE_SET_ENTER_ADC ); }
@@ -385,6 +503,13 @@ static int ad4062_setup(struct iio_dev *indio_dev, struct iio_chan_spec const *cif (IS_ERR (scan_type))return PTR_ERR (scan_type); + ret =regmap_update_bits (st->regmap,AD4062_REG_GP_CONF , +AD4062_REG_GP_CONF_MODE_MSK_0 , +FIELD_PREP (AD4062_REG_GP_CONF_MODE_MSK_0 , +AD4062_GP_INTR )); +if (ret) +return ret; + ret = regmap_update_bits(st->regmap, AD4062_REG_GP_CONF, AD4062_REG_GP_CONF_MODE_MSK_1, FIELD_PREP(AD4062_REG_GP_CONF_MODE_MSK_1,
@@ -404,6 +529,13 @@ static int ad4062_setup(struct iio_dev *indio_dev, struct iio_chan_spec const *cif (ret)return ret; + ret =regmap_update_bits (st->regmap,AD4062_REG_INTR_CONF , +AD4062_REG_INTR_CONF_EN_MSK_0 , +FIELD_PREP (AD4062_REG_INTR_CONF_EN_MSK_0 , +AD4062_INTR_EN_EITHER )); +if (ret) +return ret; + ret = regmap_update_bits(st->regmap, AD4062_REG_INTR_CONF, AD4062_REG_INTR_CONF_EN_MSK_1, FIELD_PREP(AD4062_REG_INTR_CONF_EN_MSK_1,
@@ -416,6 +548,19 @@ static int ad4062_setup(struct iio_dev *indio_dev, struct iio_chan_spec const *c &st->buf.be16, sizeof(st->buf.be16)); } +static irqreturn_t ad4062_irq_handler_thresh (int irq,void *private) +{ +struct iio_dev *indio_dev = private; + +iio_push_event (indio_dev, +IIO_UNMOD_EVENT_CODE (IIO_VOLTAGE ,0 , +IIO_EV_TYPE_THRESH , +IIO_EV_DIR_EITHER ), +iio_get_time_ns (indio_dev)); + +return IRQ_HANDLED ; +} +static irqreturn_t ad4062_irq_handler_drdy (int irq,void *private) {struct iio_dev *indio_dev = private;
@@ -434,6 +579,14 @@ static void ad4062_ibi_handler(struct i3c_device *i3cdev, {struct ad4062_state *st =i3cdev_get_drvdata (i3cdev); +if (st->wait_event) { +iio_push_event (st->indio_dev, +IIO_UNMOD_EVENT_CODE (IIO_VOLTAGE ,0 , +IIO_EV_TYPE_THRESH , +IIO_EV_DIR_EITHER ), +iio_get_time_ns (st->indio_dev)); +return ; + }if (iio_buffer_enabled (st->indio_dev))iio_trigger_poll_nested (st->trigger);else
@@ -529,6 +682,25 @@ static int ad4062_request_irq(struct iio_dev *indio_dev)struct device *dev = &st->i3cdev->dev;int ret; + ret =fwnode_irq_get_byname (dev_fwnode (&st->i3cdev->dev),"gp0" ); +if (ret == -EPROBE_DEFER ) +return ret; + +if (ret <0 ) { + ret =regmap_update_bits (st->regmap,AD4062_REG_ADC_IBI_EN , +AD4062_REG_ADC_IBI_EN_MAX |AD4062_REG_ADC_IBI_EN_MIN , +AD4062_REG_ADC_IBI_EN_MAX |AD4062_REG_ADC_IBI_EN_MIN ); +if (ret) +return ret; + }else { + ret =devm_request_threaded_irq (dev, ret,NULL , + ad4062_irq_handler_thresh, +IRQF_ONESHOT , indio_dev->name, + indio_dev); +if (ret) +return ret; + } + ret =fwnode_irq_get_byname (dev_fwnode (&st->i3cdev->dev),"gp1" );if (ret == -EPROBE_DEFER )return ret;
@@ -720,6 +892,9 @@ static int ad4062_read_chan_raw(struct ad4062_state *st, int *val)static int ad4062_read_raw_dispatch (struct ad4062_state *st,int *val,int *val2,long info) { +if (st->wait_event) +return -EBUSY ; +switch (info) {case IIO_CHAN_INFO_RAW :return ad4062_read_chan_raw (st, val);
@@ -761,6 +936,9 @@ static int ad4062_read_raw(struct iio_dev *indio_dev,static int ad4062_write_raw_dispatch (struct ad4062_state *st,int val,int val2,long info) { +if (st->wait_event) +return -EBUSY ; +switch (info) {case IIO_CHAN_INFO_OVERSAMPLING_RATIO :return ad4062_set_oversampling_ratio (st, val, val2);
@@ -789,7 +967,224 @@ static int ad4062_write_raw(struct iio_dev *indio_dev,return -EBUSY ; ret =ad4062_write_raw_dispatch (st, val, val2, info); +iio_device_release_direct (indio_dev); +return ret; +} + +static int pm_ad4062_monitor_mode_enable (struct ad4062_state *st) +{ +int ret; + +PM_RUNTIME_ACQUIRE (&st->i3cdev->dev, pm); + ret =PM_RUNTIME_ACQUIRE_ERR (&pm); +if (ret) +return ret; + +return ad4062_set_operation_mode (st,AD4062_MONITOR_MODE ); +} + +static int ad4062_monitor_mode_enable (struct ad4062_state *st) +{ +int ret; + + ret =pm_ad4062_monitor_mode_enable (st); +if (ret) +return ret; + +pm_runtime_get_noresume (&st->i3cdev->dev); +return 0 ; +} +static int ad4062_monitor_mode_disable (struct ad4062_state *st) +{ +pm_runtime_put_autosuspend (&st->i3cdev->dev); +return 0 ; +} + +static int ad4062_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 ad4062_state *st =iio_priv (indio_dev); + +return st->wait_event; +} + +static int ad4062_write_event_config_dispatch (struct iio_dev *indio_dev, +bool state) +{ +struct ad4062_state *st =iio_priv (indio_dev); +int ret; + +if (st->wait_event == state) + ret =0 ; +else if (state) + ret =ad4062_monitor_mode_enable (st); +else + ret =ad4062_monitor_mode_disable (st); +if (ret) +return ret; + + st->wait_event = state; +return 0 ; +} + +static int ad4062_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) +{ +int ret; + +if (!iio_device_claim_direct (indio_dev)) +return -EBUSY ; + + ret =ad4062_write_event_config_dispatch (indio_dev, state); +iio_device_release_direct (indio_dev); +return ret; +} + +static int __ad4062_read_event_info_value (struct ad4062_state *st, +enum iio_event_direction dir,int *val) +{ +int ret; +u8 reg; + +if (dir ==IIO_EV_DIR_RISING ) + reg =AD4062_REG_MAX_LIMIT ; +else + reg =AD4062_REG_MIN_LIMIT ; + + ret =regmap_bulk_read (st->regmap, reg, &st->buf.be16, +sizeof (st->buf.be16)); +if (ret) +return ret; + + *val =sign_extend32 (be16_to_cpu (st->buf.be16),AD4062_LIMIT_BITS -1 ); + +return 0 ; +} + +static int __ad4062_read_event_info_hysteresis (struct ad4062_state *st, +enum iio_event_direction dir,int *val) +{ +u8 reg; + +if (dir ==IIO_EV_DIR_RISING ) + reg =AD4062_REG_MAX_HYST ; +else + reg =AD4062_REG_MIN_HYST ; +return regmap_read (st->regmap, reg, val); +} + +static int ad4062_read_event_config_dispatch (struct iio_dev *indio_dev, +enum iio_event_direction dir, +enum iio_event_info info,int *val) +{ +struct ad4062_state *st =iio_priv (indio_dev); + +if (st->wait_event) +return -EBUSY ; + +switch (info) { +case IIO_EV_INFO_VALUE : +return __ad4062_read_event_info_value (st, dir, val); +case IIO_EV_INFO_HYSTERESIS : +return __ad4062_read_event_info_hysteresis (st, dir, val); +default : +return -EINVAL ; + } +} + +static int ad4062_read_event_value (struct iio_dev *indio_dev, +const struct iio_chan_spec *chan, +enum iio_event_type type, +enum iio_event_direction dir, +enum iio_event_info info,int *val, +int *val2) +{ +int ret; + +if (!iio_device_claim_direct (indio_dev)) +return -EBUSY ; + + ret =ad4062_read_event_config_dispatch (indio_dev, dir, info, val); +iio_device_release_direct (indio_dev); +return ret ?:IIO_VAL_INT ; +} + +static int __ad4062_write_event_info_value (struct ad4062_state *st, +enum iio_event_direction dir,int val) +{ +u8 reg; + +if (val !=sign_extend32 (val,AD4062_LIMIT_BITS -1 )) +return -EINVAL ; +if (dir ==IIO_EV_DIR_RISING ) + reg =AD4062_REG_MAX_LIMIT ; +else + reg =AD4062_REG_MIN_LIMIT ; + st->buf.be16 =cpu_to_be16 (val); + +return regmap_bulk_write (st->regmap, reg, &st->buf.be16, +sizeof (st->buf.be16)); +} + +static int __ad4062_write_event_info_hysteresis (struct ad4062_state *st, +enum iio_event_direction dir,int val) +{ +u8 reg; + +if (val >BIT (7 ) -1 ) +return -EINVAL ; +if (dir ==IIO_EV_DIR_RISING ) + reg =AD4062_REG_MAX_HYST ; +else + reg =AD4062_REG_MIN_HYST ; + +return regmap_write (st->regmap, reg, val); +} + +static int ad4062_write_event_value_dispatch (struct iio_dev *indio_dev, +enum iio_event_type type, +enum iio_event_direction dir, +enum iio_event_info info,int val) +{ +struct ad4062_state *st =iio_priv (indio_dev); + +if (st->wait_event) +return -EBUSY ; + +switch (type) { +case IIO_EV_TYPE_THRESH : +switch (info) { +case IIO_EV_INFO_VALUE : +return __ad4062_write_event_info_value (st, dir, val); +case IIO_EV_INFO_HYSTERESIS : +return __ad4062_write_event_info_hysteresis (st, dir, val); +default : +return -EINVAL ; + } +default : +return -EINVAL ; + } +} + +static int ad4062_write_event_value (struct iio_dev *indio_dev, +const struct iio_chan_spec *chan, +enum iio_event_type type, +enum iio_event_direction dir, +enum iio_event_info info,int val, +int val2) +{ +int ret; + +if (!iio_device_claim_direct (indio_dev)) +return -EBUSY ; + + ret =ad4062_write_event_value_dispatch (indio_dev, type, dir, info, val);iio_device_release_direct (indio_dev);return ret; }
@@ -825,6 +1220,9 @@ static int pm_ad4062_triggered_buffer_postenable(struct ad4062_state *st)if (ret)return ret; +if (st->wait_event) +return -EBUSY ; + ret =ad4062_set_operation_mode (st, st->mode);if (ret)return ret;
@@ -900,6 +1298,11 @@ static const struct iio_info ad4062_info = { .read_raw = ad4062_read_raw, .write_raw = ad4062_write_raw, .read_avail = ad4062_read_avail, + .read_event_config = ad4062_read_event_config, + .write_event_config = ad4062_write_event_config, + .read_event_value = ad4062_read_event_value, + .write_event_value = ad4062_write_event_value, + .event_attrs = &ad4062_event_attribute_group, .get_current_scan_type = ad4062_get_current_scan_type, .debugfs_reg_access = ad4062_debugfs_reg_access, };
@@ -980,8 +1383,10 @@ static int ad4062_probe(struct i3c_device *i3cdev) "Failed to initialize regmap\n"); st->mode =AD4062_SAMPLE_MODE ; + st->wait_event = false; st->chip = chip; st->sampling_frequency =0 ; + st->events_frequency =0 ; st->oversamp_ratio =0 ; st->indio_dev = indio_dev;
Signature · 1 line
2.51.1
Jorge Marques
maintainerPATCH v4 8/9 docs: iio: ad4062: Add GPIO Controller support
In reply to Jorge MarquesExplains the GPIO controller support with emphasis on the mask depending on which GPs are exposed.
--- Documentation/iio/ad4062.rst | 12 ++++++++++++ 1 file changed, 12 insertions(+)
Documentation/iio/ad4062.rst +12 -0
@@ -29,6 +29,9 @@ the devicetree ``interrupt-names`` property: - GP0: Is assigned the role of Threshold Either signal. - GP1: Is assigned the role of Data Ready signal. +If the property ``gpio-controller`` is present in the devicetree, then the GPO +not present in the ``interrupt-names`` is exposed as a GPO. + Device attributes =================
@@ -134,3 +137,12 @@ The following event attributes are available: - Set the hysteresis value for the maximum threshold. * - ``thresh_rising_value`` - Set the maximum threshold value. + +GPO controller support +====================== + +The device supports using GP0 and GP1 as GPOs. If the devicetree contains the +node ``gpio-controller```, the device is marked as a GPIO controller and the +GPs not listed in ``interrupt-names`` are exposed as a GPO. The GPIO index +matches the pin name, so if GP0 is not exposed but GP1 is, index 0 is masked +out and only index 1 can be set.
Signature · 1 line
2.51.1
Jorge Marques
maintainerPATCH v4 9/9 iio: adc: ad4062: Add GPIO Controller support
In reply to Jorge MarquesWhen gp0 or gp1 is not taken as an interrupt, expose them as GPO if gpio-contoller is set in the devicetree. gpio-regmap is not used because the GPO static low is 'b101 and static high is 0b110; low state requires setting bit 0, not fitting the abstraction of low=0 and high=mask.
--- drivers/iio/adc/ad4062.c | 125 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+)
drivers/iio/adc/ad4062.c +125 -0
@@ -11,6 +11,7 @@#include <linux/delay.h> #include <linux/devm-helpers.h> #include <linux/err.h> +#include <linux/gpio/driver.h> #include <linux/i3c/device.h> #include <linux/i3c/master.h> #include <linux/iio/buffer.h>
@@ -88,8 +89,11 @@#define AD4060_PROD_ID 0x7A#define AD4062_PROD_ID 0x7C +#define AD4062_GP_DISABLED 0x0#define AD4062_GP_INTR 0x1#define AD4062_GP_DRDY 0x2 +#define AD4062_GP_STATIC_LOW 0x5 +#define AD4062_GP_STATIC_HIGH 0x6#define AD4062_LIMIT_BITS 12
@@ -687,12 +691,14 @@ static int ad4062_request_irq(struct iio_dev *indio_dev)return ret;if (ret <0 ) { + st->gpo_irq[0 ] = false; ret =regmap_update_bits (st->regmap,AD4062_REG_ADC_IBI_EN ,AD4062_REG_ADC_IBI_EN_MAX |AD4062_REG_ADC_IBI_EN_MIN ,AD4062_REG_ADC_IBI_EN_MAX |AD4062_REG_ADC_IBI_EN_MIN );if (ret)return ret; }else { + st->gpo_irq[0 ] = true; ret = devm_request_threaded_irq(dev, ret, NULL, ad4062_irq_handler_thresh, IRQF_ONESHOT, indio_dev->name,
@@ -1347,6 +1353,121 @@ static int ad4062_regulators_get(struct ad4062_state *st, bool *ref_sel)return 0 ; } +static int ad4062_gpio_get_direction (struct gpio_chip *gc,unsigned int offset) +{ +return GPIO_LINE_DIRECTION_OUT ; +} + +static int ad4062_gpio_set (struct gpio_chip *gc,unsigned int offset,int value) +{ +struct ad4062_state *st =gpiochip_get_data (gc); +unsigned int reg_val = value ?AD4062_GP_STATIC_HIGH :AD4062_GP_STATIC_LOW ; + +if (offset) +return regmap_update_bits (st->regmap,AD4062_REG_GP_CONF , +AD4062_REG_GP_CONF_MODE_MSK_1 , +FIELD_PREP (AD4062_REG_GP_CONF_MODE_MSK_1 , reg_val)); +else +return regmap_update_bits (st->regmap,AD4062_REG_GP_CONF , +AD4062_REG_GP_CONF_MODE_MSK_0 , +FIELD_PREP (AD4062_REG_GP_CONF_MODE_MSK_0 , reg_val)); +} + +static int ad4062_gpio_get (struct gpio_chip *gc,unsigned int offset) +{ +struct ad4062_state *st =gpiochip_get_data (gc); +unsigned int reg_val; +int ret; + + ret =regmap_read (st->regmap,AD4062_REG_GP_CONF , ®_val); +if (ret) +return ret; + +if (offset) + reg_val =FIELD_GET (AD4062_REG_GP_CONF_MODE_MSK_1 , reg_val); +else + reg_val =FIELD_GET (AD4062_REG_GP_CONF_MODE_MSK_0 , reg_val); + +return reg_val ==AD4062_GP_STATIC_HIGH ; +} + +static void ad4062_gpio_disable (void *data) +{ +struct ad4062_state *st = data; +u8 val =FIELD_PREP (AD4062_REG_GP_CONF_MODE_MSK_0 ,AD4062_GP_DISABLED ) | +FIELD_PREP (AD4062_REG_GP_CONF_MODE_MSK_1 ,AD4062_GP_DISABLED ); + +regmap_update_bits (st->regmap,AD4062_REG_GP_CONF , +AD4062_REG_GP_CONF_MODE_MSK_1 |AD4062_REG_GP_CONF_MODE_MSK_0 , + val); +} + +static int ad4062_gpio_init_valid_mask (struct gpio_chip *gc, +unsigned long *valid_mask, +unsigned int ngpios) +{ +struct ad4062_state *st =gpiochip_get_data (gc); + +bitmap_zero (valid_mask, ngpios); + +for (unsigned int i =0 ; i <ARRAY_SIZE (st->gpo_irq); i++) +__assign_bit (i, valid_mask, !st->gpo_irq[i]); + +return 0 ; +} + +static int ad4062_gpio_init (struct ad4062_state *st) +{ +struct device *dev = &st->i3cdev->dev; +struct gpio_chip *gc; +u8 val, mask; +int ret; + +if (!device_property_read_bool (dev,"gpio-controller" )) +return 0 ; + + gc =devm_kzalloc (dev,sizeof (*gc),GFP_KERNEL ); +if (!gc) +return -ENOMEM ; + + val =0 ; + mask =0 ; +if (!st->gpo_irq[0 ]) { + mask |=AD4062_REG_GP_CONF_MODE_MSK_0 ; + val |=FIELD_PREP (AD4062_REG_GP_CONF_MODE_MSK_0 ,AD4062_GP_STATIC_LOW ); + } +if (!st->gpo_irq[1 ]) { + mask |=AD4062_REG_GP_CONF_MODE_MSK_1 ; + val |=FIELD_PREP (AD4062_REG_GP_CONF_MODE_MSK_1 ,AD4062_GP_STATIC_LOW ); + } + + ret =regmap_update_bits (st->regmap,AD4062_REG_GP_CONF , + mask, val); +if (ret) +return ret; + + ret =devm_add_action_or_reset (dev, ad4062_gpio_disable, st); +if (ret) +return ret; + + gc->parent = dev; + gc->label = st->chip->name; + gc->owner =THIS_MODULE ; + gc->base =-1 ; + gc->ngpio =2 ; + gc->init_valid_mask = ad4062_gpio_init_valid_mask; + gc->get_direction = ad4062_gpio_get_direction; + gc->set = ad4062_gpio_set; + gc->get = ad4062_gpio_get; + gc->can_sleep = true; + + ret =devm_gpiochip_add_data (dev, gc, st); +if (ret) +return dev_err_probe (dev, ret,"Unable to register GPIO chip\n" ); + +return 0 ; +} +static const struct i3c_device_id ad4062_id_table[] = {I3C_DEVICE (AD4062_I3C_VENDOR ,AD4060_PROD_ID , &ad4060_chip_info),I3C_DEVICE (AD4062_I3C_VENDOR ,AD4062_PROD_ID , &ad4062_chip_info),
@@ -1435,6 +1556,10 @@ static int ad4062_probe(struct i3c_device *i3cdev)if (ret)return dev_err_probe (dev, ret,"Failed to request i3c ibi\n" ); + ret =ad4062_gpio_init (st); +if (ret) +return ret; + ret =devm_work_autocancel (dev, &st->trig_conv, ad4062_trigger_work);if (ret)return ret;
Signature · 1 line
2.51.1
Re: [PATCH v4 9/9] iio: adc: ad4062: Add GPIO Controller support
Quoted 10 lines · Jorge Marques
When gp0 or gp1 is not taken as an interrupt, expose them as GPO if gpio-contoller is set in the devicetree. gpio-regmap is not used because the GPO static low is 'b101 and static high is 0b110; low state requires setting bit 0, not fitting the abstraction of low=0 and high=mask.
Signed-off-by Jorge Marques <[email protected]>Reviewed-by Linus Walleij <[email protected]>---
Quoted 12 lines · On Wed, 17 Dec 2025 13:13:23 +0100 Jorge Marques <[email protected]>…
The AD4060/AD4062 are versatile, 16-bit/12-bit, successive approximation register (SAR) analog-to-digital converter (ADC).
The device uses a 2-wire I3C interface. The device simplifies acquisition by providing 4-bytes in the register map, signal-extending the sample reading accordingly.
The device has autonomous monitoring capabilities, that are exposed as IIO events. Since register access requires leaving the monitoring state and returning, any device access exits monitoring mode, disabling the IIO event.
Applied to the togreg branch of iio.git, though initially only pushed out as testing to give 0-day a first opportunity to poke at it.
Thanks,
Jonathan
Hi Jonathan, Jorge,
Heads up: the AD4062 driver uses the deprecated I3C API which was removed in commit 9904232ae30bc ("i3c: drop i3c_priv_xfer and i3c_device_do_priv_xfers()"). This causes build failures when merged with trees containing that commit:
drivers/iio/adc/ad4062.c:471:24: error: variable 'xfer_trigger' has initializer but incomplete type
471 | struct i3c_priv_xfer xfer_trigger = {
| ^~~~~~~~~~~~~ drivers/iio/adc/ad4062.c:472:26: error: 'struct i3c_priv_xfer' has no member named 'data'
...
The fix is straightforward - migrate to the new API:
- struct i3c_priv_xfer -> struct i3c_xfer
- i3c_device_do_priv_xfers(dev, xfers, n) -> i3c_device_do_xfers(dev, xfers, n, I3C_SDR)
The struct fields are identical (i3c_priv_xfer was just a #define alias), and I3C_SDR is what the old wrapper used internally.
Signature · 2 lines
Thanks, Sasha
Jonathan Cameron
maintainerQuoted 13 lines · On Sat, 17 Jan 2026 10:12:32 -0500 Sasha Levin <[email protected]> wrote:
Hi Jonathan, Jorge,
Heads up: the AD4062 driver uses the deprecated I3C API which was removed in commit
9904232ae30bc("i3c: dropi3c_priv_xferandi3c_device_do_priv_xfers()"). This causes build failures when merged with trees containing that commit:
drivers/iio/adc/ad4062.c:471:24: error: variable 'xfer_trigger' has initializer but incomplete type471 | struct i3c_priv_xfer xfer_trigger = { | ^~~~~~~~~~~~~
drivers/iio/adc/ad4062.c:472:26: error: 'structi3c_priv_xfer' has no member named 'data'
...
Quoted 8 lines
The fix is straightforward - migrate to the new API:
- struct
i3c_priv_xfer-> structi3c_xfer
-i3c_device_do_priv_xfers(dev, xfers, n)->i3c_device_do_xfers(dev, xfers, n, I3C_SDR)The struct fields are identical (
i3c_priv_xferwas just a #define alias), andI3C_SDRis what the old wrapper used internally.
Thanks for the heads up! Given this driver was quite a way back in my tree and I'd rather avoid a rebase, I've sent out a patch on top that makes the changes you suggest. I'll carry it on the iio/togreg tree now just to avoid causing build mess in linux-next and just rebase the top the tree if any issues turn up in review (or to add tags)
Cheers
Jonathan