Skip to contentSkip to conversation

iio: imu: Add support for the ADI ADIS16607

PATCH Superseded Radu Sabau 4 participants 14 messages Oct 8 – Oct 12

Subsystems (4)

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Radu Sabau

author
#0 lore raw

v2 cover iio: imu: Add support for the ADI ADIS16607

This series adds support for the Analog Devices ADIS16607, a precision MEMS IMU with triaxial gyroscope, triaxial accelerometer, and temperature sensor, communicating over a non-standard SPI Half-Duplex protocol.

Patch 1 adds two small adislib (drivers/iio/imu/adis.c) extensions this series needs:
- An optional self_test callback for devices whose self-test sequence doesn't fit the generic mask-write/sleep/check-status pattern.
- An optional post_reset_setup callback for devices that need extra device-specific bring-up between reset and self-test (SPI mode locking, status/bootloader checks, pin or burst-mode configuration, etc.) - not all ADIS devices need this, so it's only called when set, same as self_test.
Plus a fix to __adis_initial_startup()'s reset fallback, which was calling __adis_reset() directly instead of dispatching through adis->ops->reset(). That bug is invisible for every existing adislib driver (adis_default_ops.reset is __adis_reset() itself), but silently breaks a driver with fully custom ops - like this one - on any board without a reset-gpios property. Carries a Fixes tag for the commit that introduced the hardcoded call; no stable backport is needed since the bug only manifests for a custom-ops driver, and this is the first one.

Patch 2 adds the devicetree binding.

Patches 3-6 add the driver in functional groupings, as requested in v1 review:
- Patch 3: base driver - probe, raw channel reads, decimation/sample frequency, startup and self-test via adislib, debugfs identity entries. No buffered capture yet.
- Patch 4: buffered capture via the device's hardware FIFO.
- Patch 5: external clock sync support.
- Patch 6: calibration bias support for gyro/accel.

Notable points for reviewers:

- The device does not implement the standard ADIS SPI protocol used by other parts in the family; reads/writes are done over a custom Half-Duplex sequence (see adis16607_spi_read()/adis16607_spi_write() and the adis_ops overrides).

- Startup is split across the two new adislib callbacks rather than a driver-private initial_startup(): adis16607_post_reset_setup() does SPI Half-Duplex locking, dev_id verification, the bootloader-busy check, an optional reset-status GPIO output, and (once FIFO is wired up in patch 4) burst-mode and interrupt-pin configuration; adis16607_self_test() does only the actual per-axis delta-check.

- The optional reset-status GPIO output (RESET_GPIO_MASK, part of "optional hardware reset via GPIO") is configured in patch 3's post_reset_setup, independent of FIFO. Patch 4 later adds the DR/DEV_NOK pins to the same USER_GPIO_CFG register via a read-modify-write, so it cannot clobber what patch 3 already set.

- The hardware FIFO is the *only* buffered capture path - there is no single-sample direct burst-read trigger mode. The FIFO-pop command and the regular burst-read command are genuinely different protocols on this device (different payload shapes, no checksum on the FIFO side), so this stays driver-private rather than going through adislib's generic burst-read helpers.

- The FIFO-threshold interrupt is signaled via DEV_NOK on the device's one physically-wired interrupt pin, which stays asserted (level-triggered) until DIAG_STAT is read and cleared - this sets has_fifo on adis_data and requests the IRQ through adislib's threaded/level path, matching adislib's actual contract for that flag.

- An optional external clock (clocks property) selects between SYNC_DIRECT and SYNC_SCALED modes depending on the supplied frequency, with the internal 8 kHz clock used as the default when no clock is provided (adis16607_config_clk()).

- spi-max-frequency is corrected to 10 MHz (datasheet maximum), down from 15 MHz in v1.

This has been tested on a Raspberry Pi 4 (arm64) with the device wired over SPI, exercising the FIFO buffered-capture path with the updated DEV_NOK/level-triggered interrupt wiring.

To: Lars-Peter Clausen <[email protected]>
To: Michael Hennerich <[email protected]>
To: Jonathan Cameron <[email protected]>
To: David Lechner <[email protected]>
To: Nuno Sá <[email protected]>
To: Andy Shevchenko <[email protected]>
To: Rob Herring <[email protected]>
To: Krzysztof Kozlowski <[email protected]>
To: Conor Dooley <[email protected]>
To: Alexandru Ardelean <[email protected]>

Changes in v2:
- Split the driver commit into functional groupings per review:
base, FIFO, external clock sync, calibration bias (Nuno).
- Added adis_data.self_test and adis_data.post_reset_setup callbacks
to adislib instead of duplicating startup/self-test logic in the
driver (Nuno). post_reset_setup is a second, honestly-named hook
added after review feedback that overloading self_test to also mean "everything else the device needs at startup" was misleading.
- Fixed __adis_initial_startup()'s reset fallback to dispatch through adis->ops->reset() instead of a hardcoded __adis_reset() call -
found while wiring up this driver's custom reset op. Added a Fixes
tag for the originating commit.
- Removed the direct single-sample burst-read capture path entirely;
the hardware FIFO is now the only buffered capture mode. Dropped adi,fifo-enable from the binding accordingly.
- Fixed FIFO-threshold interrupt wiring: set has_fifo on adis_data and switch the DT interrupt type from edge to level, matching the
DEV_NOK signal this now uses (previously only the data-ready pulse
was wired, which never actually generated a FIFO interrupt).
- Moved the optional reset-status GPIO output into the base driver
patch (where it conceptually belongs) instead of bundling it into
the FIFO patch's USER_GPIO_CFG write; the FIFO patch now updates
that register with a read-modify-write instead of a blind write.
- Fixed cover letter: "Three compatible variants (-1, -2, -3)" was
wrong, there are only two (-2, -3); "configurable data-ready
interrupt pin (DIO1-4)" was wrong, there's a single fixed pin.
- Corrected spi-max-frequency to 10 MHz (datasheet maximum), from
15 MHz.
- Renamed ADIS16607_REG_SW_RES to ADIS16607_REG_SOFT_RESET to match
the datasheet's register name; renamed burst_buf to fifo_buf and dropped the redundant "fifo_" prefix from the buffer postenable/ postdisable/ops names, since FIFO is the only capture mode and
"burst" no longer described what that scratch buffer is used for.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Radu Sabau (6):
iio: imu: adis: Add optional self_test callback and fix custom reset dispatch
dt-bindings: iio: imu: Add bindings for ADI ADIS16607
iio: imu: Add driver for the ADI ADIS16607
iio: imu: adis16607: Add FIFO-based buffered/triggered capture
iio: imu: adis16607: Add external clock sync support
iio: imu: adis16607: Add calibration bias support for gyro/accel

 .../devicetree/bindings/iio/imu/adi,adis16607.yaml |   83 ++
 MAINTAINERS                                        |    8 +
 drivers/iio/imu/Kconfig                            |   13 +
 drivers/iio/imu/Makefile                           |    1 +
 drivers/iio/imu/adis.c                             |   30 +-
 drivers/iio/imu/adis16607.c                        | 1373 ++++++++++++++++++++
 include/linux/iio/imu/adis.h                       |   14 +
 7 files changed, 1515 insertions(+), 7 deletions(-)

---
base-commit: 9c87e61e3c5797277407ba5eae4eac8a52be3fa3
change-id: 20260617-adis16607-089ffc655ceb

Best regards,

Signature · 1 line
Radu Sabau <[email protected]>
author
#1 lore raw

PATCH v2 1/6 iio: imu: adis: Add optional self_test callback and fix custom reset dispatch

Add an optional self_test callback to struct adis_data, for devices whose self-test doesn't fit the generic mask-write/sleep/check-status sequence. Purely additive: defaults to NULL, no change for existing drivers.

Also fix __adis_initial_startup()'s no-reset-gpio path: it called __adis_reset() directly instead of adis->ops->reset(), silently ignoring any driver's custom reset implementation. Invisible until now since adis_default_ops.reset is __adis_reset() itself. Dispatch through adis->ops->reset() instead, adding the settle-time sleep at the call site for custom ops (documented not to sleep internally) while skipping it for the default op (which already sleeps internally).

Fixes ecb010d44108 ("iio: imu: adis: Refactor adis_initial_startup")
---
 drivers/iio/imu/adis.c       | 30 +++++++++++++++++++++++-------
 include/linux/iio/imu/adis.h | 14 ++++++++++++++
 2 files changed, 37 insertions(+), 7 deletions(-)
drivers/iio/imu/adis.c +23 -7
index a2bc1d14ed91..306777526a11 100644
@@ -355,11 +355,20 @@ int __adis_reset(struct adis *adis)
 }
 EXPORT_SYMBOL_NS_GPL(__adis_reset, "IIO_ADIS_LIB");
 
+static const struct adis_ops adis_default_ops = {
+	.read = __adis_read_reg,
+	.write = __adis_write_reg,
+	.reset = __adis_reset,
+};
+
 static int adis_self_test(struct adis *adis)
 {
 	int ret;
 	const struct adis_timeout *timeouts = adis->data->timeouts;
 
+	if (adis->data->self_test)
+		return adis->data->self_test(adis);
+
 	ret = __adis_write_reg_16(adis, adis->data->self_test_reg,
 				  adis->data->self_test_mask);
 	if (ret) {
@@ -413,7 +422,20 @@ int __adis_initial_startup(struct adis *adis)
 		gpiod_set_value_cansleep(gpio, 0);
 		msleep(timeouts->reset_ms);
 	} else {
-		ret = __adis_reset(adis);
+		ret = adis->ops->reset(adis);
+		if (ret)
+			return ret;
+		/*
+		 * The default reset op (__adis_reset()) already sleeps the
+		 * settle time internally. Custom reset ops are documented
+		 * (struct adis_ops::reset) not to, so sleep here for them.
+		 */
+		if (adis->ops != &adis_default_ops)
+			msleep(timeouts->sw_reset_ms);
+	}
+
+	if (adis->data->post_reset_setup) {
+		ret = adis->data->post_reset_setup(adis);
 		if (ret)
 			return ret;
 	}
@@ -491,12 +513,6 @@ int adis_single_conversion(struct iio_dev *indio_dev,
 }
 EXPORT_SYMBOL_NS_GPL(adis_single_conversion, "IIO_ADISLIB");
 
-static const struct adis_ops adis_default_ops = {
-	.read = __adis_read_reg,
-	.write = __adis_write_reg,
-	.reset = __adis_reset,
-};
-
 /**
  * adis_init() - Initialize adis device structure
  * @adis:	The adis device
include/linux/iio/imu/adis.h +14 -0
index bfb6df68e6c9..06058843e02b 100644
@@ -51,6 +51,18 @@ struct adis_timeout {
  * @self_test_mask: Bitmask of supported self-test operations
  * @self_test_reg: Register address to request self test command
  * @self_test_no_autoclear: True if device's self-test needs clear of ctrl reg
+ * @post_reset_setup: Optional device-specific hook called after reset,
+ *		before self-test. Not all ADIS devices need extra bring-up
+ *		between reset and self-test (SPI mode locking, status or
+ *		bootloader checks, pin or burst-mode configuration, etc.) -
+ *		only called when set. Must return 0 on success, a negative
+ *		error code otherwise.
+ * @self_test: Optional device-specific self-test override. When set,
+ *		it is called instead of the generic mask-write/sleep/
+ *		check-status sequence. The callback owns its own pass/fail
+ *		determination entirely (it should call __adis_check_status()
+ *		itself if it wants status-register checks). Must return 0 on
+ *		success, a negative error code otherwise.
  * @status_error_msgs: Array of error messages
  * @status_error_mask: Bitmask of errors supported by the device
  * @timeouts: Chip specific delays
@@ -81,6 +93,8 @@ struct adis_data {
 	unsigned int self_test_mask;
 	unsigned int self_test_reg;
 	bool self_test_no_autoclear;
+	int (*post_reset_setup)(struct adis *adis);
+	int (*self_test)(struct adis *adis);
 	const struct adis_timeout *timeouts;
 
 	const char * const *status_error_msgs;
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v2 1/6] iio: imu: adis: Add optional self_test callback and fix custom reset dispatch

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] iio: imu: adis: adis_reset() API bypasses custom reset operations --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] iio: imu: adis: adis_reset() API bypasses custom reset operations --

commit 94d1ab88cd933c9c52f4aca4a47015c8c3d32cd3
Author: Radu Sabau <[email protected]>

iio: imu: adis: Add optional self_test callback and fix custom reset dispatch

Add an optional self_test callback to struct adis_data, for devices whose self-test doesn't fit the generic mask-write/sleep/check-status sequence. Purely additive: defaults to NULL, no change for existing drivers.

Also fix __adis_initial_startup()'s no-reset-gpio path: it called __adis_reset() directly instead of adis->ops->reset(), silently ignoring any driver's custom reset implementation. Invisible until now since adis_default_ops.reset is __adis_reset() itself. Dispatch through adis->ops->reset() instead, adding the settle-time sleep at the call site for custom ops (documented not to sleep internally) while skipping it for the default op (which already sleeps internally).

Quoted 4 lines
drivers/iio/imu/adis.c +0 -0
index a2bc1d14ed91d..306777526a118 100644

[ ... ]

drivers/iio/imu/adis.c int __adis_initial_startup(struct adis *adis) · line 425

Quoted 6 lines
drivers/iio/imu/adis.c +1 -1
@@ -413,7 +422,20 @@ int __adis_initial_startup(struct adis *adis)
 		gpiod_set_value_cansleep(gpio, 0);
 		msleep(timeouts->reset_ms);
 	} else {
-		ret = __adis_reset(adis);
+		ret = adis->ops->reset(adis);

[Severity: Low]
While this correctly fixes the dispatch here, does the public adis_reset() API function in include/linux/iio/imu/adis.h also need to be updated?

It currently still bypasses the custom operations by hardcoding a direct call to __adis_reset(adis):

include/linux/iio/imu/adis.h:adis_reset() {
	guard(mutex)(&adis->state_lock);
	return __adis_reset(adis);
}

If a driver utilizing a custom reset callback attempts to use adis_reset() (e.g., during error recovery), it would still bypass the custom logic and write directly to the default global command register.

For a device with a custom reset, like adis16550, this issues an 8-bit SPI write (ADIS_GLOB_CMD_SW_RESET) which would fail because its custom SPI transfer handler expects 16-bit or 32-bit transfers.

While it appears no in-tree drivers with custom operations currently invoke this wrapper, it seems leaving it this way breaks the API contract established by this patch.

Quoted 10 lines
drivers/iio/imu/adis.c +10 -0
+		if (ret)
+			return ret;
+		/*
+		 * The default reset op (__adis_reset()) already sleeps the
+		 * settle time internally. Custom reset ops are documented
+		 * (struct adis_ops::reset) not to, so sleep here for them.
+		 */
+		if (adis->ops != &adis_default_ops)
+			msleep(timeouts->sw_reset_ms);
+	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#3 lore raw

PATCH v2 2/6 dt-bindings: iio: imu: Add bindings for ADI ADIS16607

In reply to Radu Sabau

Add device tree bindings for the Analog Devices ADIS16607 Precision MEMS Inertial Measurement Unit. The ADIS16607 integrates a triaxial gyroscope, triaxial accelerometer, and temperature sensor, and communicates over SPI Half-Duplex.

Two compatible variants are supported: adis16607-2 and adis16607-3.

The binding exposes optional properties for:
- An external clock for sync mode (falling back to the internal 8 kHz clock when absent)
- A hardware reset GPIO (active low)
- FIFO mode for buffered data acquisition with a configurable watermark

---
 .../devicetree/bindings/iio/imu/adi,adis16607.yaml | 83 ++++++++++++++++++++++
 MAINTAINERS                                        |  7 ++
 2 files changed, 90 insertions(+)
Documentation/devicetree/bindings/iio/imu/adi,adis16607.yaml new file +83 -0
new file mode 100644
index 000000000000..b08809e2f327
@@ -0,0 +1,83 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/imu/adi,adis16607.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Analog Devices ADIS16607 IMU
+
+maintainers:
+  - Radu Sabau <[email protected]>
+
+description: |
+  Analog Devices ADIS16607 Precision MEMS Inertial Measurement Unit (IMU)
+  with a triaxial gyroscope, triaxial accelerometer, and temperature sensor.
+  Uses SPI Half-Duplex communication protocol.
+
+properties:
+  compatible:
+    enum:
+      - adi,adis16607-2
+      - adi,adis16607-3
+
+  reg:
+    maxItems: 1
+
+  spi-cpha: true
+
+  spi-cpol: true
+
+  spi-max-frequency:
+    maximum: 10000000
+
+  vdd-supply: true
+
+  interrupts:
+    maxItems: 1
+
+  clocks:
+    description:
+      Optional external clock for sync mode. If not provided, the internal
+      8 kHz clock is used.
+    maxItems: 1
+
+  reset-gpios:
+    description:
+      Optional GPIO for hardware reset. If specified, it will be asserted
+      during driver probe. The line is active low.
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - spi-cpha
+  - spi-cpol
+  - vdd-supply
+
+allOf:
+  - $ref: /schemas/spi/spi-peripheral-props.yaml#
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+    #include <dt-bindings/gpio/gpio.h>
+    spi {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        imu@0 {
+            compatible = "adi,adis16607-2";
+            reg = <0>;
+            spi-cpha;
+            spi-cpol;
+            spi-max-frequency = <10000000>;
+            vdd-supply = <&vdd>;
+            interrupts = <4 IRQ_TYPE_LEVEL_HIGH>;
+            interrupt-parent = <&gpio>;
+            clocks = <&sync_clk>;
+            reset-gpios = <&gpio 10 GPIO_ACTIVE_LOW>;
+        };
+    };
MAINTAINERS +7 -0
index a7fd31320b38..f6ad73be5260 100644
@@ -1717,6 +1717,13 @@ S:	Supported
 W:	https://ez.analog.com/linux-software-drivers
 F:	Documentation/devicetree/bindings/iio/imu/adi,adis16550.yaml
 
+ANALOG DEVICES INC ADIS16607 DRIVER
+M:	Radu Sabau <[email protected]>
+L:	[email protected]
+S:	Supported
+W:	https://ez.analog.com/linux-software-drivers
+F:	Documentation/devicetree/bindings/iio/imu/adi,adis16607.yaml
+
 ANALOG DEVICES INC ADL8113 DRIVER
 M:	Antoniu Miclaus <[email protected]>
 L:	[email protected]
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH v2 2/6] dt-bindings: iio: imu: Add bindings for ADI ADIS16607

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] dt-bindings: commit message claims nonexistent FIFO watermark property --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] dt-bindings: commit message claims nonexistent FIFO watermark property --

commit 46f4e849c03631bf69981fbde007debcca23f628
Author: Radu Sabau <[email protected]>

dt-bindings: iio: imu: Add bindings for ADI ADIS16607

This commit adds device tree bindings for the Analog Devices ADIS16607 Precision MEMS Inertial Measurement Unit. It documents the supported compatible variants, required SPI properties, and optional properties for external clocks and reset GPIOs.

Quoted 5 lines

The binding exposes optional properties for:
- An external clock for sync mode (falling back to the internal 8 kHz clock when absent)
- A hardware reset GPIO (active low)
- FIFO mode for buffered data acquisition with a configurable watermark

[Severity: Medium]
Is this commit message accurate?

The message claims the binding exposes optional properties for a configurable FIFO watermark, but looking at the properties block in Documentation/devicetree/bindings/iio/imu/adi,adis16607.yaml, no such properties are defined.

Because unevaluatedProperties is set to false, if a developer attempts to configure a FIFO watermark in their device tree node based on this commit message, it will cause a schema validation failure during dt_binding_check.

Are FIFO watermarks managed dynamically via IIO sysfs rather than statically in the device tree for this hardware?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#5 lore raw

Re: [PATCH v2 2/6] dt-bindings: iio: imu: Add bindings for ADI ADIS16607

In reply to Radu Sabau
Quoted 38 lines · On Thu, Oct 08, 2026 at 11:31:54AM +0300, Radu Sabau via B4 Relay wrote:

From: Radu Sabau <[email protected]>

Add device tree bindings for the Analog Devices ADIS16607 Precision MEMS Inertial Measurement Unit. The ADIS16607 integrates a triaxial gyroscope, triaxial accelerometer, and temperature sensor, and communicates over SPI Half-Duplex.

Two compatible variants are supported: adis16607-2 and adis16607-3.

The binding exposes optional properties for:
- An external clock for sync mode (falling back to the internal 8 kHz clock when absent)
- A hardware reset GPIO (active low)
- FIFO mode for buffered data acquisition with a configurable watermark

---
 .../devicetree/bindings/iio/imu/adi,adis16607.yaml | 83 ++++++++++++++++++++++
 MAINTAINERS                                        |  7 ++
 2 files changed, 90 insertions(+)
Documentation/devicetree/bindings/iio/imu/adi,adis16607.yaml new file +11 -0
new file mode 100644
index 000000000000..b08809e2f327
@@ -0,0 +1,83 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/imu/adi,adis16607.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Analog Devices ADIS16607 IMU
+
+maintainers:
+  - Radu Sabau <[email protected]>
+

Documentation/devicetree/bindings/iio/imu/adi,adis16607.yaml line 21

Quoted 10 lines
Documentation/devicetree/bindings/iio/imu/adi,adis16607.yaml new file +10 -0
+description: |
+  Analog Devices ADIS16607 Precision MEMS Inertial Measurement Unit (IMU)
+  with a triaxial gyroscope, triaxial accelerometer, and temperature sensor.
+  Uses SPI Half-Duplex communication protocol.
+
+properties:
+  compatible:
+    enum:
+      - adi,adis16607-2
+      - adi,adis16607-3

Commit message says they're compatible, binding says they're not!

Quoted 16 lines
Documentation/devicetree/bindings/iio/imu/adi,adis16607.yaml new file +16 -0
+
+  reg:
+    maxItems: 1
+
+  spi-cpha: true
+
+  spi-cpol: true
+
+  spi-max-frequency:
+    maximum: 10000000
+
+  vdd-supply: true
+
+  interrupts:
+    maxItems: 1
+

Documentation/devicetree/bindings/iio/imu/adi,adis16607.yaml line 47

Quoted 10 lines
Documentation/devicetree/bindings/iio/imu/adi,adis16607.yaml new file +10 -0
+  clocks:
+    description:
+      Optional external clock for sync mode. If not provided, the internal
+      8 kHz clock is used.
+    maxItems: 1
+
+  reset-gpios:
+    description:
+      Optional GPIO for hardware reset. If specified, it will be asserted
+      during driver probe. The line is active low.

Drop the second sentence, since that's a linux driver specific detail.
pw-bot: changes-requested

Thanks,
COnor,

Quoted 59 lines
Documentation/devicetree/bindings/iio/imu/adi,adis16607.yaml new file +36 -0
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - spi-cpha
+  - spi-cpol
+  - vdd-supply
+
+allOf:
+  - $ref: /schemas/spi/spi-peripheral-props.yaml#
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+    #include <dt-bindings/gpio/gpio.h>
+    spi {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        imu@0 {
+            compatible = "adi,adis16607-2";
+            reg = <0>;
+            spi-cpha;
+            spi-cpol;
+            spi-max-frequency = <10000000>;
+            vdd-supply = <&vdd>;
+            interrupts = <4 IRQ_TYPE_LEVEL_HIGH>;
+            interrupt-parent = <&gpio>;
+            clocks = <&sync_clk>;
+            reset-gpios = <&gpio 10 GPIO_ACTIVE_LOW>;
+        };
+    };
MAINTAINERS +7 -0
index a7fd31320b38..f6ad73be5260 100644
@@ -1717,6 +1717,13 @@ S:	Supported
 W:	https://ez.analog.com/linux-software-drivers
 F:	Documentation/devicetree/bindings/iio/imu/adi,adis16550.yaml
 
+ANALOG DEVICES INC ADIS16607 DRIVER
+M:	Radu Sabau <[email protected]>
+L:	[email protected]
+S:	Supported
+W:	https://ez.analog.com/linux-software-drivers
+F:	Documentation/devicetree/bindings/iio/imu/adi,adis16607.yaml
+
 ANALOG DEVICES INC ADL8113 DRIVER
 M:	Antoniu Miclaus <[email protected]>
 L:	[email protected]
Signature · 2 lines
-- 
2.43.0
author
#6 lore raw

PATCH v2 3/6 iio: imu: Add driver for the ADI ADIS16607

In reply to Radu Sabau

The ADIS16607 is a Precision MEMS IMU from Analog Devices with triaxial gyroscope and accelerometer, delta angle/velocity, and temperature channels, communicating over a custom SPI Half-Duplex protocol. ADIS16607-2 has a ±450°/s gyroscope range, ADIS16607-3 ±2000°/s; both share a ±40g accelerometer range and an 8 kHz internal clock.

This is the base driver: probe, raw channel reads, sampling-frequency control, startup/self-test via adislib, and debugfs identity entries. No buffered capture, external clock sync, or calibration bias yet - those follow in later patches.

---
 MAINTAINERS                 |   1 +
 drivers/iio/imu/Kconfig     |  13 +
 drivers/iio/imu/Makefile    |   1 +
 drivers/iio/imu/adis16607.c | 856 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 871 insertions(+)
MAINTAINERS +1 -0
index f6ad73be5260..70b7a430c610 100644
@@ -1723,6 +1723,7 @@ L:	[email protected]
 S:	Supported
 W:	https://ez.analog.com/linux-software-drivers
 F:	Documentation/devicetree/bindings/iio/imu/adi,adis16607.yaml
+F:	drivers/iio/imu/adis16607.c
 
 ANALOG DEVICES INC ADL8113 DRIVER
 M:	Antoniu Miclaus <[email protected]>
drivers/iio/imu/Kconfig +13 -0
index 7e0181c27bb6..7781bd7081cd 100644
@@ -65,6 +65,19 @@ config ADIS16550
 	  To compile this driver as a module, choose M here: the module will be
 	  called adis16550.
 
+config ADIS16607
+	tristate "Analog Devices ADIS16607 and similar IMU driver"
+	depends on SPI
+	select IIO_ADIS_LIB
+	select IIO_ADIS_LIB_BUFFER if IIO_BUFFER
+	help
+	  Say yes here to build support for Analog Devices ADIS16607-2
+	  and ADIS16607-3 inertial sensors containing triaxis
+	  gyroscope and triaxis accelerometer.
+
+	  To compile this driver as a module, choose M here: the module will be
+	  called adis16607.
+
 source "drivers/iio/imu/bmi160/Kconfig"
 source "drivers/iio/imu/bmi270/Kconfig"
 source "drivers/iio/imu/bmi323/Kconfig"
drivers/iio/imu/Makefile +1 -0
index 13fb7846e9c9..a5e21687cb5d 100644
@@ -9,6 +9,7 @@ obj-$(CONFIG_ADIS16460) += adis16460.o
 obj-$(CONFIG_ADIS16475) += adis16475.o
 obj-$(CONFIG_ADIS16480) += adis16480.o
 obj-$(CONFIG_ADIS16550) += adis16550.o
+obj-$(CONFIG_ADIS16607) += adis16607.o
 
 adis_lib-y += adis.o
 adis_lib-$(CONFIG_IIO_ADIS_LIB_BUFFER) += adis_trigger.o
drivers/iio/imu/adis16607.c new file +856 -0

856 lines not on this page · raw message

Signature · 1 line
2.43.0
#7 lore raw

Re: [PATCH v2 3/6] iio: imu: Add driver for the ADI ADIS16607

Quoted 1 line · On Thu, Oct 08, 2026 at 11:31:55AM +0300, Radu Sabau via B4 Relay wrote:

+#include <linux/mod_devicetable.h>

Please drop this header. <linux/spi/spi.h> already provides the needed *_device_id structs.

drivers/iio/imu/adis16607.c line 831

Quoted 3 lines
drivers/iio/imu/adis16607.c new file +3 -0
+static const struct spi_device_id adis16607_id[] = {
+	{ "adis16607-2", (kernel_ulong_t)&adis16607_2_chip_info },
+	{ "adis16607-3", (kernel_ulong_t)&adis16607_3_chip_info },

Please use named initializers here (i.e.

	{ .name = "adis16607-2", .driver_data = (kernel_ulong_t)&adis16607_2_chip_info },
	...

)

Best regards
Uwe

author
#8 lore raw

PATCH v2 4/6 iio: imu: adis16607: Add FIFO-based buffered/triggered capture

In reply to Radu Sabau

Add buffered capture via the device's hardware FIFO - the only capture mode this driver supports. Popping the FIFO uses a dedicated command and payload format (FIFO_DATA_CMD, 52 bytes, no checksum), handled directly rather than through adislib's generic burst-read helpers, which assume one fixed-shape read per trigger rather than a variable-count drain loop.

The watermark-met condition is signaled via DEV_NOK on the device's one interrupt pin, which stays asserted (level-triggered) until DIAG_STAT is read and cleared - matching adislib's has_fifo contract. DEV_NOK fires on any DIAG_STAT flag, not just FIFO_THRESH_MET, so the trigger handler checks that specific bit before popping anything. DR_GPIO_MASK stays enabled unconditionally: DR pulses drive the FIFO controller's internal sample enqueue, independent of pin routing.

Watermark is configurable via the standard hwfifo_set_watermark callback and the usual hwfifo_* sysfs attributes.

---
 drivers/iio/imu/adis16607.c | 402 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 400 insertions(+), 2 deletions(-)
drivers/iio/imu/adis16607.c +400 -2
index 24eaccaddfa3..90688037cbd3 100644
@@ -11,6 +11,9 @@
 #include <linux/iio/buffer.h>
 #include <linux/iio/iio.h>
 #include <linux/iio/imu/adis.h>
+#include <linux/iio/sysfs.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/interrupt.h>
 #include <linux/kernel.h>
 #include <linux/math.h>
 #include <linux/units.h>
@@ -41,8 +44,11 @@
 #define ADIS16607_REG_Y_DELTANG		0x1A
 #define ADIS16607_REG_Z_DELTANG		0x1C
 #define ADIS16607_REG_TEMP		0x20
+#define ADIS16607_REG_FIFO_WORD_CNT	0x2B
 #define ADIS16607_REG_USER_GPIO_CFG	0x2F
 #define ADIS16607_REG_SPI_HALFDUPLEX	0x32
+#define ADIS16607_REG_USER_DATA_CFG	0x34
+#define ADIS16607_REG_USER_FIFO_CFG	0x35
 #define ADIS16607_REG_SOFT_RESET	0x36
 #define ADIS16607_REG_SELF_TEST		0x39
 #define ADIS16607_REG_DEC_RATE		0x3A
@@ -53,8 +59,26 @@
 
 /* Masks */
 #define ADIS16607_RESET_GPIO_MASK	GENMASK(2, 0)
+#define ADIS16607_DR_GPIO_MASK		GENMASK(11, 9)
+#define ADIS16607_DEV_NOK_MASK		GENMASK(14, 12)
+#define ADIS16607_DATA_CNTR_EN_MASK	BIT(14)
+#define ADIS16607_BURST32_MASK		BIT(15)
 #define ADIS16607_BOOTLOADER_BUSY_MASK	BIT(0)
 
+/* FIFO masks */
+#define ADIS16607_FIFO_THR_MASK		GENMASK(10, 0)
+#define ADIS16607_FIFO_FLUSH_MASK	BIT(15)
+#define ADIS16607_FIFO_THR_MET_MASK	BIT(0)
+#define ADIS16607_FIFO_DATA_CMD		0xA9
+
+/* FIFO limits */
+#define ADIS16607_MAX_FIFO_WM		38
+#define ADIS16607_FIFO_WORDS_PER_SAMPLE	26
+#define ADIS16607_FIFO_DATA_LEN		52
+
+/* Burst read - Half-Duplex mode (DATA_CNTR enabled) */
+#define ADIS16607_BURST_CMD_SIZE	4
+
 /* Self-test registers and masks */
 #define ADIS16607_REG_SELF_TEST_DATA(x)	((x) + 0x23)
 #define ADIS16607_SNSR_SELF_TEST_MASK	BIT(6)
@@ -85,6 +109,9 @@ struct adis16607 {
 	struct adis adis;
 	unsigned long clk_freq;	/* millihertz */
 	u64 sample_period_ns;
+	u16 fifo_watermark;
+	/* Scratch buffer used for FIFO pop operations, see adis16607_fifo_pop() */
+	u8 *fifo_buf;
 	/* IIO buffer for reordered data - sized for max channels */
 	u8 data[64] __aligned(IIO_DMA_MINALIGN);
 };
@@ -225,6 +252,334 @@ static const struct adis_ops adis16607_ops = {
 	.reset = adis16607_reset,
 };
 
+/*
+ * FIFO/burst data offsets for each channel (in bytes from the data start,
+ * i.e. after the 4-byte command prefix).
+ * Each 32-bit inertial channel occupies 4 bytes (MSW + LSW), TEMP is 2 bytes.
+ *
+ * FIFO data layout (52 bytes, DATA_CNTR enabled, no checksum):
+ *   Offset 0-3:   ACCEL_X  (32-bit: MSW, LSW)
+ *   Offset 4-7:   ACCEL_Y
+ *   Offset 8-11:  ACCEL_Z
+ *   Offset 12-15: GYRO_X
+ *   Offset 16-19: GYRO_Y
+ *   Offset 20-23: GYRO_Z
+ *   Offset 24-27: DELTVEL_X
+ *   Offset 28-31: DELTVEL_Y
+ *   Offset 32-35: DELTVEL_Z
+ *   Offset 36-39: DELTANG_X
+ *   Offset 40-43: DELTANG_Y
+ *   Offset 44-47: DELTANG_Z
+ *   Offset 48-49: TEMP (16-bit)
+ *   Offset 50-51: DATA_CNTR (16-bit)
+ *
+ * Each 32-bit value encodes 24-bit sensor data as:
+ *   value_24bit = (MSW << 8) | (LSW >> 8)
+ */
+static const u8 adis16607_burst_offsets[] = {
+	[ADIS16607_SCAN_ACCEL_X] = 0,
+	[ADIS16607_SCAN_ACCEL_Y] = 4,
+	[ADIS16607_SCAN_ACCEL_Z] = 8,
+	[ADIS16607_SCAN_GYRO_X] = 12,
+	[ADIS16607_SCAN_GYRO_Y] = 16,
+	[ADIS16607_SCAN_GYRO_Z] = 20,
+	[ADIS16607_SCAN_DELTVEL_X] = 24,
+	[ADIS16607_SCAN_DELTVEL_Y] = 28,
+	[ADIS16607_SCAN_DELTVEL_Z] = 32,
+	[ADIS16607_SCAN_DELTANG_X] = 36,
+	[ADIS16607_SCAN_DELTANG_Y] = 40,
+	[ADIS16607_SCAN_DELTANG_Z] = 44,
+	[ADIS16607_SCAN_TEMP] = 48,
+};
+
+static void adis16607_fifo_buf_cleanup(void *arg)
+{
+	struct adis16607 *st = arg;
+
+	kfree(st->fifo_buf);
+}
+
+/*
+ * Sets up the scratch buffer used for FIFO pop operations (see
+ * adis16607_fifo_pop()). The 32-bit burst mode and DATA_CNTR config bits
+ * are enabled once during startup (adis16607_post_reset_setup()), not
+ * here, since they don't depend on scan_mask.
+ */
+static int adis16607_update_scan_mode(struct iio_dev *indio_dev,
+				      const unsigned long *scan_mask)
+{
+	struct adis16607 *st = iio_priv(indio_dev);
+	unsigned int burst_len = ADIS16607_BURST_CMD_SIZE + ADIS16607_FIFO_DATA_LEN;
+
+	kfree(st->fifo_buf);
+
+	st->fifo_buf = kzalloc(burst_len, GFP_KERNEL);
+	if (!st->fifo_buf)
+		return -ENOMEM;
+
+	return 0;
+}
+
+/*
+ * Pop a single sample from FIFO using FIFO_DATA command (0xA9).
+ * Returns the raw FIFO data in the provided buffer.
+ */
+static int adis16607_fifo_pop(struct adis16607 *st, u8 *data)
+{
+	struct spi_message msg;
+	struct spi_transfer xfer = {
+		.tx_buf = st->fifo_buf,
+		.rx_buf = st->fifo_buf,
+		.len = ADIS16607_BURST_CMD_SIZE + ADIS16607_FIFO_DATA_LEN,
+		.bits_per_word = 8,
+	};
+	int ret;
+
+	/* FIFO data command */
+	st->fifo_buf[0] = ADIS16607_FIFO_DATA_CMD;
+	st->fifo_buf[1] = 0;
+	st->fifo_buf[2] = 0;
+	st->fifo_buf[3] = 0;
+
+	spi_message_init(&msg);
+	spi_message_add_tail(&xfer, &msg);
+
+	ret = spi_sync(st->adis.spi, &msg);
+	if (ret)
+		return ret;
+
+	/* Copy data starting after command bytes */
+	memcpy(data, st->fifo_buf + ADIS16607_BURST_CMD_SIZE, ADIS16607_FIFO_DATA_LEN);
+	return 0;
+}
+
+/*
+ * Process a single FIFO sample and push to IIO buffer.
+ * FIFO data format differs from regular burst - no DIAG_STAT, data starts immediately.
+ *
+ * FIFO data layout (52 bytes):
+ *   Offset 0-3:   ACCEL_X (32-bit)
+ *   Offset 4-7:   ACCEL_Y (32-bit)
+ *   Offset 8-11:  ACCEL_Z (32-bit)
+ *   Offset 12-15: GYRO_X (32-bit)
+ *   Offset 16-19: GYRO_Y (32-bit)
+ *   Offset 20-23: GYRO_Z (32-bit)
+ *   Offset 24-27: DELTVEL_X (32-bit)
+ *   Offset 28-31: DELTVEL_Y (32-bit)
+ *   Offset 32-35: DELTVEL_Z (32-bit)
+ *   Offset 36-39: DELTANG_X (32-bit)
+ *   Offset 40-43: DELTANG_Y (32-bit)
+ *   Offset 44-47: DELTANG_Z (32-bit)
+ *   Offset 48-49: TEMP (16-bit)
+ *   Offset 50-51: DATA_CNTR (16-bit)
+ */
+static void adis16607_fifo_push_sample(struct iio_dev *indio_dev,
+				       u8 *fifo_data, s64 timestamp)
+{
+	struct adis16607 *st = iio_priv(indio_dev);
+	u16 msw, lsw;
+	s32 val24;
+	int bit, offset = 0;
+
+	memset(st->data, 0, sizeof(st->data));
+
+	for_each_set_bit(bit, indio_dev->active_scan_mask, indio_dev->masklength) {
+		u8 burst_off;
+
+		/* Skip the soft timestamp channel - it has no burst offset */
+		if (bit >= ARRAY_SIZE(adis16607_burst_offsets))
+			continue;
+
+		burst_off = adis16607_burst_offsets[bit];
+
+		if (bit == ADIS16607_SCAN_TEMP) {
+			/* TEMP is 16-bit */
+			put_unaligned_be16(get_unaligned_be16(&fifo_data[burst_off]),
+					   &st->data[offset]);
+			offset += 2;
+		} else {
+			/* All other channels are 24-bit stored as 32-bit */
+			msw = get_unaligned_be16(&fifo_data[burst_off]);
+			lsw = get_unaligned_be16(&fifo_data[burst_off + 2]);
+			val24 = sign_extend32(((u32)msw << 8) | (lsw >> 8), 23);
+			put_unaligned_be32(val24, &st->data[offset]);
+			offset += 4;
+		}
+	}
+
+	iio_push_to_buffers_with_timestamp(indio_dev, st->data, timestamp);
+}
+
+/*
+ * Trigger handler - reads samples from the device FIFO. Triggered by the
+ * DEV_NOK interrupt, which fires on any DIAG_STAT flag, not just
+ * FIFO_THRESH_MET - the DIAG_STAT read below both checks which flag(s)
+ * raised the interrupt and clears the latched status, and must happen
+ * before any FIFO pop, or we'd pop for nothing on an unrelated flag.
+ */
+static irqreturn_t adis16607_trigger_handler(int irq, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *indio_dev = pf->indio_dev;
+	struct adis16607 *st = iio_priv(indio_dev);
+	u8 fifo_sample[ADIS16607_FIFO_DATA_LEN];
+	u16 diag_stat, word_cnt, sample_cnt, threshold;
+	s64 timestamp = pf->timestamp;
+	int ret, i;
+
+	mutex_lock(&st->adis.state_lock);
+
+	/* Check if FIFO threshold is met */
+	ret = __adis_read_reg_16(&st->adis, ADIS16607_REG_DIAG_STAT, &diag_stat);
+	if (ret)
+		goto unlock;
+
+	if (!(diag_stat & ADIS16607_FIFO_THR_MET_MASK))
+		goto unlock;
+
+	/* Temporarily disable FIFO before reading */
+	ret = __adis_write_reg_16(&st->adis, ADIS16607_REG_USER_FIFO_CFG, 0);
+	if (ret)
+		goto unlock;
+
+	/* Read FIFO word count */
+	ret = __adis_read_reg_16(&st->adis, ADIS16607_REG_FIFO_WORD_CNT, &word_cnt);
+	if (ret)
+		goto reenable_fifo;
+
+	/* Calculate number of complete samples */
+	sample_cnt = word_cnt / ADIS16607_FIFO_WORDS_PER_SAMPLE;
+	if (!sample_cnt)
+		goto reenable_fifo;
+
+	/* Pop and process each sample */
+	for (i = 0; i < sample_cnt; i++) {
+		ret = adis16607_fifo_pop(st, fifo_sample);
+		if (ret)
+			goto reenable_fifo;
+
+		adis16607_fifo_push_sample(indio_dev, fifo_sample, timestamp);
+		timestamp += st->sample_period_ns;
+	}
+
+reenable_fifo:
+	/* Flush FIFO before re-enabling */
+	ret = __adis_write_reg_16(&st->adis, ADIS16607_REG_USER_FIFO_CFG,
+				  ADIS16607_FIFO_FLUSH_MASK);
+	if (ret)
+		dev_warn_ratelimited(&st->adis.spi->dev,
+				     "failed to flush FIFO: %d\n", ret);
+
+	/* Re-enable FIFO with watermark threshold */
+	threshold = st->fifo_watermark * ADIS16607_FIFO_WORDS_PER_SAMPLE;
+	ret = __adis_write_reg_16(&st->adis, ADIS16607_REG_USER_FIFO_CFG,
+				  threshold & ADIS16607_FIFO_THR_MASK);
+	if (ret)
+		dev_warn_ratelimited(&st->adis.spi->dev,
+				     "failed to re-enable FIFO: %d\n", ret);
+
+unlock:
+	mutex_unlock(&st->adis.state_lock);
+	iio_trigger_notify_done(indio_dev->trig);
+	return IRQ_HANDLED;
+}
+
+/* FIFO sysfs attributes */
+IIO_STATIC_CONST_DEVICE_ATTR(hwfifo_watermark_min, "1");
+IIO_STATIC_CONST_DEVICE_ATTR(hwfifo_watermark_max, __stringify(ADIS16607_MAX_FIFO_WM));
+
+static ssize_t hwfifo_watermark_show(struct device *dev,
+				     struct device_attribute *attr,
+				     char *buf)
+{
+	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
+	struct adis16607 *st = iio_priv(indio_dev);
+
+	return sysfs_emit(buf, "%u\n", st->fifo_watermark);
+}
+
+static ssize_t hwfifo_enabled_show(struct device *dev,
+				   struct device_attribute *attr,
+				   char *buf)
+{
+	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
+	struct adis16607 *st = iio_priv(indio_dev);
+	u16 val;
+	int ret;
+
+	ret = adis_read_reg_16(&st->adis, ADIS16607_REG_USER_FIFO_CFG, &val);
+	if (ret)
+		return ret;
+
+	return sysfs_emit(buf, "%d\n", (val & ADIS16607_FIFO_THR_MASK) ? 1 : 0);
+}
+
+static IIO_DEVICE_ATTR_RO(hwfifo_watermark, 0);
+static IIO_DEVICE_ATTR_RO(hwfifo_enabled, 0);
+
+static const struct iio_dev_attr *adis16607_fifo_attributes[] = {
+	&iio_dev_attr_hwfifo_watermark_min,
+	&iio_dev_attr_hwfifo_watermark_max,
+	&iio_dev_attr_hwfifo_watermark,
+	&iio_dev_attr_hwfifo_enabled,
+	NULL
+};
+
+static int adis16607_buffer_postenable(struct iio_dev *indio_dev)
+{
+	struct adis16607 *st = iio_priv(indio_dev);
+	struct adis *adis = &st->adis;
+	u16 threshold;
+	int ret;
+
+	mutex_lock(&adis->state_lock);
+
+	/* Flush FIFO to start clean */
+	ret = __adis_write_reg_16(adis, ADIS16607_REG_USER_FIFO_CFG,
+				  ADIS16607_FIFO_FLUSH_MASK);
+	if (ret)
+		goto unlock;
+
+	/* Enable FIFO with watermark threshold (in word count) */
+	threshold = st->fifo_watermark * ADIS16607_FIFO_WORDS_PER_SAMPLE;
+	ret = __adis_write_reg_16(adis, ADIS16607_REG_USER_FIFO_CFG,
+				  threshold & ADIS16607_FIFO_THR_MASK);
+
+unlock:
+	mutex_unlock(&adis->state_lock);
+	return ret;
+}
+
+static int adis16607_buffer_postdisable(struct iio_dev *indio_dev)
+{
+	struct adis16607 *st = iio_priv(indio_dev);
+	struct adis *adis = &st->adis;
+	int ret;
+
+	mutex_lock(&adis->state_lock);
+
+	/* Disable FIFO */
+	ret = __adis_write_reg_16(adis, ADIS16607_REG_USER_FIFO_CFG, 0);
+
+	mutex_unlock(&adis->state_lock);
+	return ret;
+}
+
+static const struct iio_buffer_setup_ops adis16607_buffer_ops = {
+	.postenable = adis16607_buffer_postenable,
+	.postdisable = adis16607_buffer_postdisable,
+};
+
+static int adis16607_set_watermark(struct iio_dev *indio_dev, unsigned int val)
+{
+	struct adis16607 *st = iio_priv(indio_dev);
+
+	val = clamp_t(unsigned int, val, 1, ADIS16607_MAX_FIFO_WM);
+	st->fifo_watermark = val;
+
+	return 0;
+}
+
 /*
  * Check if the delta between two values is within specified range.
  */
@@ -308,7 +663,8 @@ static int adis16607_sensor_self_test(struct adis16607 *st)
  * before self-test - no mutex is taken here either, matching that
  * convention. Does the device-specific startup steps adislib has no
  * concept of: SPI Half-Duplex locking, dev_id verification, the
- * bootloader-busy check, and (if wired up) a reset-status GPIO output.
+ * bootloader-busy check, a reset-status GPIO output (if wired up),
+ * enabling 32-bit burst mode + DATA_CNTR, and the rest of USER_GPIO_CFG.
  */
 static int adis16607_post_reset_setup(struct adis *adis)
 {
@@ -368,7 +724,29 @@ static int adis16607_post_reset_setup(struct adis *adis)
 			return ret;
 	}
 
-	return 0;
+	/* Enable 32-bit burst mode and DATA_CNTR - one-time, used by FIFO pops */
+	ret = __adis_update_bits_base(adis, ADIS16607_REG_USER_DATA_CFG,
+				      ADIS16607_BURST32_MASK | ADIS16607_DATA_CNTR_EN_MASK,
+				      ADIS16607_BURST32_MASK | ADIS16607_DATA_CNTR_EN_MASK, 2);
+	if (ret)
+		return ret;
+
+	/*
+	 * DR_GPIO_MASK and DEV_NOK_MASK are updated here rather than written
+	 * blind, so this doesn't clobber the RESET_GPIO_MASK bit possibly
+	 * just set above. Per datasheet, DR pulses are what drive the FIFO
+	 * controller's internal enqueue logic. DR and DEV_NOK are separate
+	 * physical pins, so enabling DR_GPIO_MASK here does not put DR's
+	 * full-sample-rate pulses onto the interrupt line - DEV_NOK_MASK
+	 * routes the aggregate "any DIAG_STAT flag asserted" signal onto the
+	 * one physically-wired interrupt pin, which is what actually fires
+	 * on FIFO_THRESH_MET, at the (much lower) watermark rate, not the
+	 * sample rate.
+	 */
+	return __adis_update_bits_base(adis, ADIS16607_REG_USER_GPIO_CFG,
+				       ADIS16607_DR_GPIO_MASK | ADIS16607_DEV_NOK_MASK,
+				       FIELD_PREP(ADIS16607_DR_GPIO_MASK, 1) |
+				       FIELD_PREP(ADIS16607_DEV_NOK_MASK, 1), 2);
 }
 
 /*
@@ -706,7 +1084,9 @@ static const struct iio_chan_spec adis16607_channels[] = {
 static const struct iio_info adis16607_info = {
 	.read_raw = adis16607_read_raw,
 	.write_raw = adis16607_write_raw,
+	.update_scan_mode = adis16607_update_scan_mode,
 	.debugfs_reg_access = adis_debugfs_reg_access,
+	.hwfifo_set_watermark = adis16607_set_watermark,
 };
 
 static const char * const adis16607_status_error_msgs[] = {
@@ -728,6 +1108,7 @@ static const struct adis_data adis16607_data = {
 	.prod_id_reg = ADIS16607_REG_DEV_ID,
 	.prod_id = 0x6000,
 	.unmasked_drdy = true,
+	.has_fifo = true,
 	.post_reset_setup = adis16607_post_reset_setup,
 	.self_test = adis16607_self_test,
 	.status_error_msgs = adis16607_status_error_msgs,
@@ -799,6 +1180,8 @@ static int adis16607_probe(struct spi_device *spi)
 	indio_dev->info = &adis16607_info;
 	indio_dev->modes = INDIO_DIRECT_MODE;
 
+	st->fifo_watermark = ADIS16607_MAX_FIFO_WM;
+
 	st->adis.ops = &adis16607_ops;
 
 	ret = devm_regulator_get_enable(dev, "vdd");
@@ -817,6 +1200,21 @@ static int adis16607_probe(struct spi_device *spi)
 	if (ret)
 		return ret;
 
+	/*
+	 * Register fifo_buf cleanup before buffer/trigger setup so that on
+	 * devm teardown (LIFO order) the IRQ handler is removed first, then
+	 * fifo_buf is freed, avoiding a use-after-free in the handler.
+	 */
+	ret = devm_add_action_or_reset(dev, adis16607_fifo_buf_cleanup, st);
+	if (ret)
+		return ret;
+
+	ret = devm_adis_setup_buffer_and_trigger_with_attrs(&st->adis, indio_dev,
+		adis16607_trigger_handler, &adis16607_buffer_ops,
+		adis16607_fifo_attributes);
+	if (ret)
+		return ret;
+
 	ret = devm_iio_device_register(dev, indio_dev);
 	if (ret)
 		return ret;
Signature · 1 line
2.43.0
#9 lore raw

Re: [PATCH v2 4/6] iio: imu: adis16607: Add FIFO-based buffered/triggered capture

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] iio: imu: adis16607: Data loss due to FIFO flush in trigger handler - [Medium] iio: imu…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] iio: imu: adis16607: Data loss due to FIFO flush in trigger handler
- [Medium] iio: imu: adis16607: Incorrect FIFO timestamps projected into the future --

commit e39eeb314e616a23019e35eaf1a136dc6bdb17f7
Author: Radu Sabau <[email protected]>

iio: imu: adis16607: Add FIFO-based buffered/triggered capture

This commit adds buffered capture support using the hardware FIFO on the adis16607. It introduces a custom trigger handler that pops samples directly and adds configuration for the FIFO watermark and sysfs attributes.

Quoted 4 lines
drivers/iio/imu/adis16607.c +0 -0
index 24eaccaddfa30..90688037cbd31 100644

[ ... ]

Quoted 12 lines
drivers/iio/imu/adis16607.c +12 -0
+static irqreturn_t adis16607_trigger_handler(int irq, void *p)
+{
+	struct iio_poll_func *pf = p;
+	struct iio_dev *indio_dev = pf->indio_dev;
+	struct adis16607 *st = iio_priv(indio_dev);
+	u8 fifo_sample[ADIS16607_FIFO_DATA_LEN];
+	u16 diag_stat, word_cnt, sample_cnt, threshold;
+	s64 timestamp = pf->timestamp;
+	int ret, i;
+
+	mutex_lock(&st->adis.state_lock);
+

drivers/iio/imu/adis16607.c static const struct adis_ops adis16607_ops = { · line 441

Quoted 10 lines
drivers/iio/imu/adis16607.c +10 -0
+	/* Check if FIFO threshold is met */
+	ret = __adis_read_reg_16(&st->adis, ADIS16607_REG_DIAG_STAT, &diag_stat);
+	if (ret)
+		goto unlock;
+
+	if (!(diag_stat & ADIS16607_FIFO_THR_MET_MASK))
+		goto unlock;
+
+	/* Temporarily disable FIFO before reading */
+	ret = __adis_write_reg_16(&st->adis, ADIS16607_REG_USER_FIFO_CFG, 0);

[Severity: Medium]
Will disabling the hardware FIFO here halt the enqueuing of new samples from the sensor core? If new data is generated while the SPI read is in progress, it seems those samples could be lost.

Quoted 11 lines
drivers/iio/imu/adis16607.c +11 -0
+	if (ret)
+		goto unlock;
+
+	/* Read FIFO word count */
+	ret = __adis_read_reg_16(&st->adis, ADIS16607_REG_FIFO_WORD_CNT, &word_cnt);
+	if (ret)
+		goto reenable_fifo;
+
+	/* Calculate number of complete samples */
+	sample_cnt = word_cnt / ADIS16607_FIFO_WORDS_PER_SAMPLE;
+	if (!sample_cnt)

drivers/iio/imu/adis16607.c static const struct adis_ops adis16607_ops = { · line 462

Quoted 10 lines
drivers/iio/imu/adis16607.c +10 -0
+		goto reenable_fifo;
+
+	/* Pop and process each sample */
+	for (i = 0; i < sample_cnt; i++) {
+		ret = adis16607_fifo_pop(st, fifo_sample);
+		if (ret)
+			goto reenable_fifo;
+
+		adis16607_fifo_push_sample(indio_dev, fifo_sample, timestamp);
+		timestamp += st->sample_period_ns;

[Severity: Medium]
Does this logic project the timestamps of subsequent samples into the future? Since the interrupt fires when the FIFO reaches the watermark, pf->timestamp represents the arrival time of the newest sample in the batch. However, the FIFO outputs the oldest sample first.

By starting at pf->timestamp and adding st->sample_period_ns for each subsequent sample, the newest sample ends up with a timestamp in the future:
pf->timestamp + (sample_cnt - 1) * st->sample_period_ns.

Could the timestamp be backdated before the loop instead? For example:
timestamp = pf->timestamp - (sample_cnt - 1) * st->sample_period_ns;

drivers/iio/imu/adis16607.c static const struct adis_ops adis16607_ops = { · line 468

Quoted 6 lines
drivers/iio/imu/adis16607.c +6 -0
+	}
+
+reenable_fifo:
+	/* Flush FIFO before re-enabling */
+	ret = __adis_write_reg_16(&st->adis, ADIS16607_REG_USER_FIFO_CFG,
+				  ADIS16607_FIFO_FLUSH_MASK);

[Severity: Medium]
Does flushing the FIFO here drop partial sample remainders or new samples that may have accumulated? For continuous buffered capture, dropping data between reads seems like it would break the continuous data stream.

Quoted 3 lines
drivers/iio/imu/adis16607.c +3 -0
+	if (ret)
+		dev_warn_ratelimited(&st->adis.spi->dev,
+				     "failed to flush FIFO: %d\n", ret);

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
author
#10 lore raw

PATCH v2 5/6 iio: imu: adis16607: Add external clock sync support

In reply to Radu Sabau

Add support for an optional external clock (clocks property), selecting between SYNC_DIRECT and SYNC_SCALED modes depending on the supplied frequency, with the internal 8 kHz clock used as the default when no clock is provided.

---
 drivers/iio/imu/adis16607.c | 102 +++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 95 insertions(+), 7 deletions(-)
drivers/iio/imu/adis16607.c +95 -7
index 90688037cbd3..0147eba0b173 100644
@@ -6,6 +6,7 @@
  */
 #include <linux/bitfield.h>
 #include <linux/bitops.h>
+#include <linux/clk.h>
 #include <linux/debugfs.h>
 #include <linux/delay.h>
 #include <linux/iio/buffer.h>
@@ -47,6 +48,7 @@
 #define ADIS16607_REG_FIFO_WORD_CNT	0x2B
 #define ADIS16607_REG_USER_GPIO_CFG	0x2F
 #define ADIS16607_REG_SPI_HALFDUPLEX	0x32
+#define ADIS16607_REG_USER_SYNC		0x33
 #define ADIS16607_REG_USER_DATA_CFG	0x34
 #define ADIS16607_REG_USER_FIFO_CFG	0x35
 #define ADIS16607_REG_SOFT_RESET	0x36
@@ -59,10 +61,12 @@
 
 /* Masks */
 #define ADIS16607_RESET_GPIO_MASK	GENMASK(2, 0)
+#define ADIS16607_SYNC_GPIO_MASK	GENMASK(8, 6)
 #define ADIS16607_DR_GPIO_MASK		GENMASK(11, 9)
 #define ADIS16607_DEV_NOK_MASK		GENMASK(14, 12)
 #define ADIS16607_DATA_CNTR_EN_MASK	BIT(14)
 #define ADIS16607_BURST32_MASK		BIT(15)
+#define ADIS16607_SYNC_MODE_MASK	BIT(15)
 #define ADIS16607_BOOTLOADER_BUSY_MASK	BIT(0)
 
 /* FIFO masks */
@@ -89,8 +93,22 @@
 #define ADIS16607_ACCEL_Z_DELTA_MAX	4000
 #define ADIS16607_GYRO_DELTA_MAX	2600
 
+/* Sync modes */
+enum adis16607_sync_mode {
+	ADIS16607_SYNC_DEFAULT,
+	ADIS16607_SYNC_DIRECT,
+	ADIS16607_SYNC_SCALED,
+};
+
+struct adis16607_sync {
+	u16 sync_mode;
+	u16 min_rate;
+	u16 max_rate;
+};
+
 struct adis16607_chip_info {
 	const struct iio_chan_spec *channels;
+	const struct adis16607_sync *sync_mode;
 	const char *name;
 	u32 num_channels;
 	u32 gyro_max_val;
@@ -102,6 +120,7 @@ struct adis16607_chip_info {
 	u32 deltvel_max_val;
 	u32 int_clk;
 	u16 max_dec;
+	u8 num_sync;
 };
 
 struct adis16607 {
@@ -109,6 +128,7 @@ struct adis16607 {
 	struct adis adis;
 	unsigned long clk_freq;	/* millihertz */
 	u64 sample_period_ns;
+	u32 sync_mode;
 	u16 fifo_watermark;
 	/* Scratch buffer used for FIFO pop operations, see adis16607_fifo_pop() */
 	u8 *fifo_buf;
@@ -1089,6 +1109,12 @@ static const struct iio_info adis16607_info = {
 	.hwfifo_set_watermark = adis16607_set_watermark,
 };
 
+static const struct adis16607_sync adis16607_sync_modes[] = {
+	{ ADIS16607_SYNC_DEFAULT, 0, 0 },
+	{ ADIS16607_SYNC_DIRECT, 401, 8000 },
+	{ ADIS16607_SYNC_SCALED, 1, 400 },
+};
+
 static const char * const adis16607_status_error_msgs[] = {
 	[9] = "Boot Memory Failure",
 	[11] = "Power Supply Failure",
@@ -1128,7 +1154,9 @@ static const struct adis_data adis16607_data = {
 	.temp_scale	= 5,						\
 	.deltvel_max_val = IIO_G_TO_M_S_2(40),				\
 	.int_clk	= 8000,						\
-	.max_dec	= 65535
+	.max_dec	= 65535,					\
+	.sync_mode	= adis16607_sync_modes,				\
+	.num_sync	= ARRAY_SIZE(adis16607_sync_modes)
 
 static const struct adis16607_chip_info adis16607_2_chip_info = {
 	ADIS16607_CHIP_INFO_COMMON,
@@ -1146,15 +1174,75 @@ static const struct adis16607_chip_info adis16607_3_chip_info = {
 	.deltang_max_val = IIO_DEGREE_TO_RAD(2000),
 };
 
-/*
- * No external clock sync support yet: always run on the internal clock.
- * Default dec=0 after reset, so period = 1 / clk_freq.
- */
 static int adis16607_config_clk(struct adis16607 *st)
 {
-	st->clk_freq = (unsigned long)st->info->int_clk * 1000;
+	struct device *dev = &st->adis.spi->dev;
+	const struct adis16607_sync *sync_mode_data = NULL;
+	unsigned long ext_clk_hz;
+	struct clk *clk;
+	int i;
+
+	clk = devm_clk_get_optional_enabled(dev, NULL);
+	if (IS_ERR(clk))
+		return PTR_ERR(clk);
+
+	if (!clk) {
+		st->clk_freq = (unsigned long)st->info->int_clk * 1000;
+		st->sync_mode = ADIS16607_SYNC_DEFAULT;
+		st->sample_period_ns = DIV_ROUND_CLOSEST_ULL((u64)NSEC_PER_SEC * 1000ULL,
+							     st->clk_freq);
+		return 0;
+	}
+
+	ext_clk_hz = clk_get_rate(clk);
+
+	for (i = 1; i < st->info->num_sync; i++) {
+		if (ext_clk_hz >= st->info->sync_mode[i].min_rate &&
+		    ext_clk_hz <= st->info->sync_mode[i].max_rate) {
+			sync_mode_data = &st->info->sync_mode[i];
+			break;
+		}
+	}
+
+	if (!sync_mode_data)
+		return dev_err_probe(dev, -EINVAL,
+				     "Clk rate: %lu not in a valid range\n",
+				     ext_clk_hz);
+
+	st->sync_mode = sync_mode_data->sync_mode;
+	/* DIRECT: external clock is the effective internal rate */
+	st->clk_freq = ext_clk_hz * 1000;
+
+	/* Configure sync via USER_GPIO_CFG and USER_SYNC registers */
+	if (st->sync_mode != ADIS16607_SYNC_DEFAULT) {
+		int ret;
+
+		/* Enable sync GPIO function */
+		ret = adis_update_bits_base(&st->adis, ADIS16607_REG_USER_GPIO_CFG,
+					    ADIS16607_SYNC_GPIO_MASK,
+					    FIELD_PREP(ADIS16607_SYNC_GPIO_MASK, 1), 2);
+		if (ret)
+			return ret;
+
+		/* Set sync mode in USER_SYNC register */
+		if (st->sync_mode == ADIS16607_SYNC_SCALED) {
+			u16 sync_scale;
+
+			sync_scale = st->info->int_clk / ext_clk_hz;
+			ret = adis_write_reg_16(&st->adis, ADIS16607_REG_USER_SYNC,
+						ADIS16607_SYNC_MODE_MASK | sync_scale);
+			if (ret)
+				return ret;
+
+			/* SCALED: effective rate is int_clk (ext_clk × sync_scale ≈ int_clk) */
+			st->clk_freq = (unsigned long)st->info->int_clk * 1000;
+		}
+	}
+
+	/* Default dec=0 after reset: period = 1 / clk_freq */
 	st->sample_period_ns = DIV_ROUND_CLOSEST_ULL((u64)NSEC_PER_SEC * 1000ULL,
-						     st->clk_freq);
+						      st->clk_freq);
+
 	return 0;
 }
 
Signature · 1 line
2.43.0
#11 lore raw

Re: [PATCH v2 5/6] iio: imu: adis16607: Add external clock sync support

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] iio: adis16607: hardcoded clk_freq in SCALED mode causes timestamp drift - [Medium] iio: imu…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] iio: adis16607: hardcoded clk_freq in SCALED mode causes timestamp drift
- [Medium] iio: imu: adis16607: missing USER_SYNC clear leaves stale flash config --

commit c8cae2338b805eec99b0652c721e93ee18498a36
Author: Radu Sabau <[email protected]>

iio: imu: adis16607: Add external clock sync support

This patch adds support for an optional external clock through the "clocks" property. It selects between SYNC_DIRECT and SYNC_SCALED modes depending on the supplied frequency, and defaults to the internal 8 kHz clock when no clock is provided.

Quoted 3 lines
drivers/iio/imu/adis16607.c +0 -0

[ ... ]

Quoted 18 lines
drivers/iio/imu/adis16607.c +7 -5
@@ -1174,15 +1174,75 @@ static const struct adis16607_chip_info adis16607_3_chip_info = {
 	.deltang_max_val = IIO_DEGREE_TO_RAD(2000),
 };
 
-/*
- * No external clock sync support yet: always run on the internal clock.
- * Default dec=0 after reset, so period = 1 / clk_freq.
- */
 static int adis16607_config_clk(struct adis16607 *st)
 {
-	st->clk_freq = (unsigned long)st->info->int_clk * 1000;
+	struct device *dev = &st->adis.spi->dev;
+	const struct adis16607_sync *sync_mode_data = NULL;
+	unsigned long ext_clk_hz;
+	struct clk *clk;
+	int i;
+
+	clk = devm_clk_get_optional_enabled(dev, NULL);

drivers/iio/imu/adis16607.c static const struct adis16607_chip_info adis16607_3_chip_info = { · line 1195

Quoted 10 lines
drivers/iio/imu/adis16607.c +10 -0
+	if (IS_ERR(clk))
+		return PTR_ERR(clk);
+
+	if (!clk) {
+		st->clk_freq = (unsigned long)st->info->int_clk * 1000;
+		st->sync_mode = ADIS16607_SYNC_DEFAULT;
+		st->sample_period_ns = DIV_ROUND_CLOSEST_ULL((u64)NSEC_PER_SEC * 1000ULL,
+							     st->clk_freq);
+		return 0;
+	}

[Severity: Medium]
If a user previously configured SCALED mode and saved it to flash, does this early return leave stale flash sync settings active in the hardware?

Because soft resets restore non-volatile flash settings instead of clearing them to hardware zero, returning here without explicitly clearing the sync configuration might leave the device stuck waiting for an external clock that no longer exists.

Quoted 18 lines
drivers/iio/imu/adis16607.c +18 -0
+
+	ext_clk_hz = clk_get_rate(clk);
+
+	for (i = 1; i < st->info->num_sync; i++) {
+		if (ext_clk_hz >= st->info->sync_mode[i].min_rate &&
+		    ext_clk_hz <= st->info->sync_mode[i].max_rate) {
+			sync_mode_data = &st->info->sync_mode[i];
+			break;
+		}
+	}
+
+	if (!sync_mode_data)
+		return dev_err_probe(dev, -EINVAL,
+				     "Clk rate: %lu not in a valid range\n",
+				     ext_clk_hz);
+
+	st->sync_mode = sync_mode_data->sync_mode;
+	/* DIRECT: external clock is the effective internal rate */

drivers/iio/imu/adis16607.c static const struct adis16607_chip_info adis16607_3_chip_info = { · line 1223

Quoted 10 lines
drivers/iio/imu/adis16607.c +10 -0
+	st->clk_freq = ext_clk_hz * 1000;
+
+	/* Configure sync via USER_GPIO_CFG and USER_SYNC registers */
+	if (st->sync_mode != ADIS16607_SYNC_DEFAULT) {
+		int ret;
+
+		/* Enable sync GPIO function */
+		ret = adis_update_bits_base(&st->adis, ADIS16607_REG_USER_GPIO_CFG,
+					    ADIS16607_SYNC_GPIO_MASK,
+					    FIELD_PREP(ADIS16607_SYNC_GPIO_MASK, 1), 2);

[Severity: Medium]
In DIRECT mode, only the GPIO function is updated here. Does this rely entirely on the prior soft reset to have cleared ADIS16607_SYNC_MODE_MASK in the ADIS16607_REG_USER_SYNC register?

If flash settings were restored during the soft reset, the device might still be configured for SCALED mode and operate at the wrong rate. Should the driver explicitly overwrite ADIS16607_REG_USER_SYNC for all sync modes?

drivers/iio/imu/adis16607.c static const struct adis16607_chip_info adis16607_3_chip_info = { · line 1231

Quoted 8 lines
drivers/iio/imu/adis16607.c +8 -0
+		if (ret)
+			return ret;
+
+		/* Set sync mode in USER_SYNC register */
+		if (st->sync_mode == ADIS16607_SYNC_SCALED) {
+			u16 sync_scale;
+
+			sync_scale = st->info->int_clk / ext_clk_hz;

[Severity: Medium]
Does this truncating integer division lead to inaccurate hardware sync scales when ext_clk_hz does not perfectly divide int_clk?

drivers/iio/imu/adis16607.c static const struct adis16607_chip_info adis16607_3_chip_info = { · line 1238

Quoted 7 lines
drivers/iio/imu/adis16607.c +7 -0
+			ret = adis_write_reg_16(&st->adis, ADIS16607_REG_USER_SYNC,
+						ADIS16607_SYNC_MODE_MASK | sync_scale);
+			if (ret)
+				return ret;
+
+			/* SCALED: effective rate is int_clk (ext_clk × sync_scale ≈ int_clk) */
+			st->clk_freq = (unsigned long)st->info->int_clk * 1000;

[Severity: Medium]
Does hardcoding the assumed clock frequency instead of dynamically computing the true scaled frequency (ext_clk_hz * sync_scale * 1000) cause IIO timestamps to systemically drift?

If ext_clk_hz does not perfectly divide the internal clock (e.g. ext_clk_hz is 399 instead of 400), actual physical sampling will occur at a slightly different rate than the hardcoded 8000 Hz.

When the trigger handler later increments timestamps using the miscalculated st->sample_period_ns, does this introduce a systemic timestamp drift that worsens over time?

Quoted 10 lines
drivers/iio/imu/adis16607.c +6 -1
+		}
+	}
+
+	/* Default dec=0 after reset: period = 1 / clk_freq */
 	st->sample_period_ns = DIV_ROUND_CLOSEST_ULL((u64)NSEC_PER_SEC * 1000ULL,
-						     st->clk_freq);
+						      st->clk_freq);
+
 	return 0;
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
author
#12 lore raw

PATCH v2 6/6 iio: imu: adis16607: Add calibration bias support for gyro/accel

In reply to Radu Sabau

Add IIO_CHAN_INFO_CALIBBIAS support for the gyroscope and accelerometer axes, backed by the device's per-axis bias registers (XA/YA/ZA_BIAS, XG/YG/ZG_BIAS).

---
 drivers/iio/imu/adis16607.c | 33 ++++++++++++++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)
drivers/iio/imu/adis16607.c +32 -1
index 0147eba0b173..d4b3a8a9fd08 100644
@@ -54,6 +54,12 @@
 #define ADIS16607_REG_SOFT_RESET	0x36
 #define ADIS16607_REG_SELF_TEST		0x39
 #define ADIS16607_REG_DEC_RATE		0x3A
+#define ADIS16607_REG_XA_BIAS		0x3B
+#define ADIS16607_REG_YA_BIAS		0x3C
+#define ADIS16607_REG_ZA_BIAS		0x3D
+#define ADIS16607_REG_XG_BIAS		0x3E
+#define ADIS16607_REG_YG_BIAS		0x3F
+#define ADIS16607_REG_ZG_BIAS		0x40
 #define ADIS16607_REG_DIGITAL_STATUS	0x4E
 
 /* SPI Half-Duplex lock value */
@@ -152,6 +158,15 @@ enum {
 	ADIS16607_SCAN_TEMP,
 };
 
+static const u32 adis16607_calib_bias[] = {
+	[ADIS16607_SCAN_ACCEL_X] = ADIS16607_REG_XA_BIAS,
+	[ADIS16607_SCAN_ACCEL_Y] = ADIS16607_REG_YA_BIAS,
+	[ADIS16607_SCAN_ACCEL_Z] = ADIS16607_REG_ZA_BIAS,
+	[ADIS16607_SCAN_GYRO_X] = ADIS16607_REG_XG_BIAS,
+	[ADIS16607_SCAN_GYRO_Y] = ADIS16607_REG_YG_BIAS,
+	[ADIS16607_SCAN_GYRO_Z] = ADIS16607_REG_ZG_BIAS,
+};
+
 /*
  * SPI Half-Duplex read operation for ADIS16607.
  * Protocol:
@@ -945,6 +960,7 @@ static int adis16607_read_raw(struct iio_dev *indio_dev,
 			      int *val, int *val2, long info)
 {
 	struct adis16607 *st = iio_priv(indio_dev);
+	const int idx = chan->scan_index;
 	int ret;
 	u32 tmp;
 
@@ -982,6 +998,16 @@ static int adis16607_read_raw(struct iio_dev *indio_dev,
 		*val = 5000;
 		return IIO_VAL_INT;
 
+	case IIO_CHAN_INFO_CALIBBIAS: {
+		u16 tmp16;
+
+		ret = adis_read_reg_16(&st->adis, adis16607_calib_bias[idx], &tmp16);
+		if (ret)
+			return ret;
+		*val = sign_extend32(tmp16, 15);
+		return IIO_VAL_INT;
+	}
+
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		ret = adis16607_get_freq(st, &tmp);
 		if (ret)
@@ -1000,6 +1026,7 @@ static int adis16607_write_raw(struct iio_dev *indio_dev,
 			       int val, int val2, long info)
 {
 	struct adis16607 *st = iio_priv(indio_dev);
+	const int idx = chan->scan_index;
 	u32 tmp;
 
 	switch (info) {
@@ -1007,6 +1034,9 @@ static int adis16607_write_raw(struct iio_dev *indio_dev,
 		tmp = val * 1000 + val2 / 1000;
 		return adis16607_set_freq(st, tmp);
 
+	case IIO_CHAN_INFO_CALIBBIAS:
+		return adis_write_reg_16(&st->adis, adis16607_calib_bias[idx], val);
+
 	default:
 		return -EINVAL;
 	}
@@ -1016,7 +1046,8 @@ static int adis16607_write_raw(struct iio_dev *indio_dev,
 	.type = (_type), \
 	.modified = 1, \
 	.channel2 = (_mod), \
-	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
+	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
+			      BIT(IIO_CHAN_INFO_CALIBBIAS), \
 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
 	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
 	.address = (_address), \
Signature · 1 line
2.43.0
#13 lore raw

Re: [PATCH v2 6/6] iio: imu: adis16607: Add calibration bias support for gyro/accel

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] iio: missing bounds check in adis16607_write_raw() causes integer truncation --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] iio: missing bounds check in adis16607_write_raw() causes integer truncation --

commit e23feabbd136bcd6fd30e2609a83f094ba9326fc
Author: Radu Sabau <[email protected]>

iio: imu: adis16607: Add calibration bias support for gyro/accel

This commit adds IIO_CHAN_INFO_CALIBBIAS support for the gyroscope and accelerometer axes. It backs the new attribute using the device's per-axis bias registers.

Quoted 4 lines
drivers/iio/imu/adis16607.c +0 -0
index 0147eba0b1737..d4b3a8a9fd08a 100644

[ ... ]

Quoted 9 lines
drivers/iio/imu/adis16607.c +1 -0
@@ -1000,6 +1026,7 @@ static int adis16607_write_raw(struct iio_dev *indio_dev,
 			       int val, int val2, long info)
 {
 	struct adis16607 *st = iio_priv(indio_dev);
+	const int idx = chan->scan_index;
 	u32 tmp;
 
 	switch (info) {

drivers/iio/imu/adis16607.c static int adis16607_write_raw(struct iio_dev *indio_dev, · line 1038

Quoted 5 lines
drivers/iio/imu/adis16607.c +2 -0
@@ -1007,6 +1034,9 @@ static int adis16607_write_raw(struct iio_dev *indio_dev,
 		tmp = val * 1000 + val2 / 1000;
 		return adis16607_set_freq(st, tmp);
 
+	case IIO_CHAN_INFO_CALIBBIAS:
+		return adis_write_reg_16(&st->adis, adis16607_calib_bias[idx], val);

[Severity: Medium]
Does this code silently truncate out-of-bounds user inputs? The val parameter in adis16607_write_raw() is a 32-bit signed integer provided by userspace, but adis_write_reg_16() expects a 16-bit integer.

Because there is no bounds check on val before passing it down, out-of-range values (values outside the 16-bit signed range [-32768, 32767]) could be implicitly cast and wrapped. For example, 65536 would become 0, leading to unintended calibration bias being applied to the device instead of rejecting the write and returning an error to userspace.

Quoted 4 lines
drivers/iio/imu/adis16607.c +1 -0
+
 	default:
 		return -EINVAL;
 	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6