Skip to contentSkip to conversation

iio: adc: ad4695: Fix call ordering in offload buffer postenable

PATCH Superseded Radu Sabau 4 participants 10 messages Mar 30 – Mar 31

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Radu Sabau

author
#0 lore raw

PATCH 1/1 iio: adc: ad4695: Fix call ordering in offload buffer postenable

ad4695_enter_advanced_sequencer_mode() was called after spi_offload_trigger_enable(), meaning a regular SPI transfer could be in flight while the offload was already active. When the offload fires its completion interrupt concurrently with the regular transfer, the SPI engine interrupt handler is not designed to handle both at once, leading to a kernel panic.

Fix this by calling ad4695_enter_advanced_sequencer_mode() before spi_offload_trigger_enable(), ensuring all SPI bus accesses are complete before the offload becomes active. This is consistent with the same constraint that already applies to the BUSY_GP_EN write above it.

Update the error unwind labels accordingly: add err_exit_conversion_mode so that a failure of spi_offload_trigger_enable() correctly exits conversion mode before clearing BUSY_GP_EN.

Fixes f09f140e3ea8 ("iio: adc: ad4695: Add support for SPI offload")

---
When enabling the IIO buffer for SPI offload operation on AD4695/AD4696, ad4695_enter_advanced_sequencer_mode() was called after spi_offload_trigger_enable(), resulting in a regular SPI transfer being in flight while the offload was already active. This caused a kernel panic in the SPI engine interrupt handler.

This series fixes the call ordering so all SPI bus accesses complete before the offload becomes active, consistent with the constraint that was already documented for the BUSY_GP_EN write in the same function.

---
 drivers/iio/adc/ad4695.c | 31 ++++++++++++++-----------------
 1 file changed, 14 insertions(+), 17 deletions(-)
drivers/iio/adc/ad4695.c +14 -17
index cda419638d9a..c6721f5ac8af 100644
@@ -866,24 +866,24 @@ static int ad4695_offload_buffer_postenable(struct iio_dev *indio_dev)
 		return ret;
 
 	/*
-	 * NB: technically, this is part the SPI offload trigger enable, but it
-	 * doesn't work to call it from the offload trigger enable callback
-	 * because it requires accessing the SPI bus. Calling it from the
-	 * trigger enable callback could cause a deadlock.
+	 * NB: these are technically part of the SPI offload trigger enable, but
+	 * they can't be called from the offload trigger enable callback because
+	 * they require accessing the SPI bus. Calling them from the trigger
+	 * enable callback could cause a deadlock.
 	 */
 	ret = regmap_set_bits(st->regmap, AD4695_REG_GP_MODE,
 			      AD4695_REG_GP_MODE_BUSY_GP_EN);
 	if (ret)
 		goto err_unoptimize_message;
 
-	ret = spi_offload_trigger_enable(st->offload, st->offload_trigger,
-					 &config);
+	ret = ad4695_enter_advanced_sequencer_mode(st, num_slots);
 	if (ret)
 		goto err_disable_busy_output;
 
-	ret = ad4695_enter_advanced_sequencer_mode(st, num_slots);
+	ret = spi_offload_trigger_enable(st->offload, st->offload_trigger,
+					 &config);
 	if (ret)
-		goto err_offload_trigger_disable;
+		goto err_exit_conversion_mode;
 
 	mutex_lock(&st->cnv_pwm_lock);
 	pwm_get_state(st->cnv_pwm, &state);
@@ -895,22 +895,19 @@ static int ad4695_offload_buffer_postenable(struct iio_dev *indio_dev)
 	ret = pwm_apply_might_sleep(st->cnv_pwm, &state);
 	mutex_unlock(&st->cnv_pwm_lock);
 	if (ret)
-		goto err_offload_exit_conversion_mode;
+		goto err_trigger_disable;
 
 	return 0;
 
-err_offload_exit_conversion_mode:
+err_trigger_disable:
 	/*
-	 * We have to unwind in a different order to avoid triggering offload.
-	 * ad4695_exit_conversion_mode() triggers a conversion, so it has to be
-	 * done after spi_offload_trigger_disable().
+	 * ad4695_exit_conversion_mode() triggers a conversion, so the offload
+	 * must be disabled first to avoid capturing a spurious sample.
 	 */
 	spi_offload_trigger_disable(st->offload, st->offload_trigger);
-	ad4695_exit_conversion_mode(st);
-	goto err_disable_busy_output;
 
-err_offload_trigger_disable:
-	spi_offload_trigger_disable(st->offload, st->offload_trigger);
+err_exit_conversion_mode:
+	ad4695_exit_conversion_mode(st);
 
 err_disable_busy_output:
 	regmap_clear_bits(st->regmap, AD4695_REG_GP_MODE,

---
base-commit: 11439c4635edd669ae435eec308f4ab8a0804808
change-id: 20260330-ad4696-fix-186955a8c511

Best regards,

Signature · 1 line
Radu Sabau <[email protected]>
reviewer
#1 lore raw
Quoted 18 lines · On 3/30/26 8:34 AM, Radu Sabau via B4 Relay wrote:

From: Radu Sabau <[email protected]>

ad4695_enter_advanced_sequencer_mode() was called after spi_offload_trigger_enable(), meaning a regular SPI transfer could be in flight while the offload was already active. When the offload fires its completion interrupt concurrently with the regular transfer, the SPI engine interrupt handler is not designed to handle both at once, leading to a kernel panic.

Fix this by calling ad4695_enter_advanced_sequencer_mode() before spi_offload_trigger_enable(), ensuring all SPI bus accesses are complete before the offload becomes active. This is consistent with the same constraint that already applies to the BUSY_GP_EN write above it.

Update the error unwind labels accordingly: add err_exit_conversion_mode so that a failure of spi_offload_trigger_enable() correctly exits conversion mode before clearing BUSY_GP_EN.

Quoted 8 lines
Fixes f09f140e3ea8 ("iio: adc: ad4695: Add support for SPI offload")

---
When enabling the IIO buffer for SPI offload operation on AD4695/AD4696, ad4695_enter_advanced_sequencer_mode() was called after spi_offload_trigger_enable(), resulting in a regular SPI transfer being in flight while the offload was already active. This caused a kernel panic in the SPI engine interrupt handler.

This used to work (I spend many hours testing it to arrive at the current state). Are we sure there hasn't been a bug introduced in the AXI SPI Engine instead?

Quoted 29 lines

This series fixes the call ordering so all SPI bus accesses complete before the offload becomes active, consistent with the constraint that was already documented for the BUSY_GP_EN write in the same function.

---
 drivers/iio/adc/ad4695.c | 31 ++++++++++++++-----------------
 1 file changed, 14 insertions(+), 17 deletions(-)
drivers/iio/adc/ad4695.c +4 -4
index cda419638d9a..c6721f5ac8af 100644
@@ -866,24 +866,24 @@ static int ad4695_offload_buffer_postenable(struct iio_dev *indio_dev)
 		return ret;
 
 	/*
-	 * NB: technically, this is part the SPI offload trigger enable, but it
-	 * doesn't work to call it from the offload trigger enable callback
-	 * because it requires accessing the SPI bus. Calling it from the
-	 * trigger enable callback could cause a deadlock.
+	 * NB: these are technically part of the SPI offload trigger enable, but
+	 * they can't be called from the offload trigger enable callback because
+	 * they require accessing the SPI bus. Calling them from the trigger
+	 * enable callback could cause a deadlock.
 	 */
 	ret = regmap_set_bits(st->regmap, AD4695_REG_GP_MODE,
 			      AD4695_REG_GP_MODE_BUSY_GP_EN);
 	if (ret)
 		goto err_unoptimize_message;

drivers/iio/adc/ad4695.c static int ad4695_offload_buffer_postenable(struct iio_dev *indio_dev) · line 884

Quoted 10 lines
drivers/iio/adc/ad4695.c +3 -3
 
-	ret = spi_offload_trigger_enable(st->offload, st->offload_trigger,
-					 &config);
+	ret = ad4695_enter_advanced_sequencer_mode(st, num_slots);
 	if (ret)
 		goto err_disable_busy_output;
 
-	ret = ad4695_enter_advanced_sequencer_mode(st, num_slots);
+	ret = spi_offload_trigger_enable(st->offload, st->offload_trigger,
+					 &config);

Swapping the order here introduces a race condition.

ad4695_enter_advanced_sequencer_mode() starts sampling (triggered from the internal clock in the ADC) and the first sample could be completed before the SPI offload is enabled. If the first sample is missed, then all of the data will be off by one index on the buffer.

Quoted 41 lines
drivers/iio/adc/ad4695.c +7 -11
 	if (ret)
-		goto err_offload_trigger_disable;
+		goto err_exit_conversion_mode;
 
 	mutex_lock(&st->cnv_pwm_lock);
 	pwm_get_state(st->cnv_pwm, &state);
@@ -895,22 +895,19 @@ static int ad4695_offload_buffer_postenable(struct iio_dev *indio_dev)
 	ret = pwm_apply_might_sleep(st->cnv_pwm, &state);
 	mutex_unlock(&st->cnv_pwm_lock);
 	if (ret)
-		goto err_offload_exit_conversion_mode;
+		goto err_trigger_disable;
 
 	return 0;
 
-err_offload_exit_conversion_mode:
+err_trigger_disable:
 	/*
-	 * We have to unwind in a different order to avoid triggering offload.
-	 * ad4695_exit_conversion_mode() triggers a conversion, so it has to be
-	 * done after spi_offload_trigger_disable().
+	 * ad4695_exit_conversion_mode() triggers a conversion, so the offload
+	 * must be disabled first to avoid capturing a spurious sample.
 	 */
 	spi_offload_trigger_disable(st->offload, st->offload_trigger);
-	ad4695_exit_conversion_mode(st);
-	goto err_disable_busy_output;
 
-err_offload_trigger_disable:
-	spi_offload_trigger_disable(st->offload, st->offload_trigger);
+err_exit_conversion_mode:
+	ad4695_exit_conversion_mode(st);
 
 err_disable_busy_output:
 	regmap_clear_bits(st->regmap, AD4695_REG_GP_MODE,
 
---

base-commit: 11439c4635edd669ae435eec308f4ab8a0804808
change-id: 20260330-ad4696-fix-186955a8c511

Best regards,

#2 lore raw
Quoted 43 lines

-----Original Message-----
From: David Lechner <[email protected]>
Sent: Monday, March 30, 2026 5:11 PM
To: Sabau, Radu bogdan <[email protected]>; Lars-Peter Clausen <[email protected]>; Hennerich, Michael <[email protected]>; Sa, Nuno <[email protected]>; Jonathan Cameron <[email protected]>; Andy Shevchenko <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] iio: adc: ad4695: Fix call ordering in offload buffer postenable

[External]

Quoted 18 lines · On 3/30/26 8:34 AM, Radu Sabau via B4 Relay wrote:

From: Radu Sabau <[email protected]>

ad4695_enter_advanced_sequencer_mode() was called after spi_offload_trigger_enable(), meaning a regular SPI transfer could be in flight while the offload was already active. When the offload fires its completion interrupt concurrently with the regular transfer, the SPI engine interrupt handler is not designed to handle both at once, leading to a kernel panic.

Fix this by calling ad4695_enter_advanced_sequencer_mode() before spi_offload_trigger_enable(), ensuring all SPI bus accesses are complete before the offload becomes active. This is consistent with the same constraint that already applies to the BUSY_GP_EN write above it.

Update the error unwind labels accordingly: add err_exit_conversion_mode so that a failure of spi_offload_trigger_enable() correctly exits conversion mode before clearing BUSY_GP_EN.

Quoted 8 lines
Fixes f09f140e3ea8 ("iio: adc: ad4695: Add support for SPI offload")

---
When enabling the IIO buffer for SPI offload operation on AD4695/AD4696, ad4695_enter_advanced_sequencer_mode() was called after spi_offload_trigger_enable(), resulting in a regular SPI transfer being in flight while the offload was already active. This caused a kernel panic in the SPI engine interrupt handler.

Quoted 4 lines

This used to work (I spend many hours testing it to arrive at the current state). Are we sure there hasn't been a bug introduced in the AXI SPI Engine instead?

Hi David,

I also tried looking in the AXI SPI Engine and find something related first but couldn't find anything, though I am happy to be corrected.

Quoted 4 lines
Quoted 4 lines

This series fixes the call ordering so all SPI bus accesses complete before the offload becomes active, consistent with the constraint that was already documented for the BUSY_GP_EN write in the same function.

...

Quoted 12 lines

drivers/iio/adc/ad4695.c static int ad4695_offload_buffer_postenable(struct iio_dev *indio_dev) · line 884

Quoted 9 lines
drivers/iio/adc/ad4695.c +3 -3
-	ret = spi_offload_trigger_enable(st->offload, st->offload_trigger,
-					 &config);
+	ret = ad4695_enter_advanced_sequencer_mode(st, num_slots);
 	if (ret)
 		goto err_disable_busy_output;
 
-	ret = ad4695_enter_advanced_sequencer_mode(st, num_slots);
+	ret = spi_offload_trigger_enable(st->offload, st->offload_trigger,
+					 &config);

Swapping the order here introduces a race condition.

Quoted 6 lines

ad4695_enter_advanced_sequencer_mode() starts sampling (triggered from the
internal clock in the ADC) and the first sample could be completed before the SPI offload is enabled. If the first sample is missed, then all of the data will be off by one index on the buffer.

I thought this too at the first glance, but couldn’t be sure so I tried testing it anyway using the FMCZ evaluation board and a Zedboard, and everything worked as expected.
I have plotted buffered data and it showed exactly what the Signal Generator was outputting and on the right channel, so no index was off, if that's what you meant.

Best Regards,
Radu

reviewer
#3 lore raw
Quoted 48 lines · Radu bogdan
Quoted 43 lines

-----Original Message-----
From: David Lechner <[email protected]>
Sent: Monday, March 30, 2026 5:11 PM
To: Sabau, Radu bogdan <[email protected]>; Lars-Peter Clausen <[email protected]>; Hennerich, Michael <[email protected]>; Sa, Nuno <[email protected]>; Jonathan Cameron <[email protected]>; Andy Shevchenko <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH] iio: adc: ad4695: Fix call ordering in offload buffer postenable

[External]

Quoted 18 lines · On 3/30/26 8:34 AM, Radu Sabau via B4 Relay wrote:

From: Radu Sabau <[email protected]>

ad4695_enter_advanced_sequencer_mode() was called after spi_offload_trigger_enable(), meaning a regular SPI transfer could be in flight while the offload was already active. When the offload fires its completion interrupt concurrently with the regular transfer, the SPI engine interrupt handler is not designed to handle both at once, leading to a kernel panic.

Fix this by calling ad4695_enter_advanced_sequencer_mode() before spi_offload_trigger_enable(), ensuring all SPI bus accesses are complete before the offload becomes active. This is consistent with the same constraint that already applies to the BUSY_GP_EN write above it.

Update the error unwind labels accordingly: add err_exit_conversion_mode so that a failure of spi_offload_trigger_enable() correctly exits conversion mode before clearing BUSY_GP_EN.

Quoted 8 lines
Fixes f09f140e3ea8 ("iio: adc: ad4695: Add support for SPI offload")

---
When enabling the IIO buffer for SPI offload operation on AD4695/AD4696, ad4695_enter_advanced_sequencer_mode() was called after spi_offload_trigger_enable(), resulting in a regular SPI transfer being in flight while the offload was already active. This caused a kernel panic in the SPI engine interrupt handler.

Quoted 4 lines

This used to work (I spend many hours testing it to arrive at the current state). Are we sure there hasn't been a bug introduced in the AXI SPI Engine instead?

Quoted 5 lines

Hi David,

I also tried looking in the AXI SPI Engine and find something related first but couldn't find anything, though I am happy to be corrected.

Which commit is the HDL being built from? It could be that something changed in the FPGA implementation rather than the driver.

Sending a regular SPI message while offload is enabled should still work. If there was a design decision that changed that, then we should also fix the AXI SPI Engine driver to return an error instead of crashing in this case. (I hope we can avoid this though.)

Even if this one particular case can be worked around, I expect the same race condition could be a problem on other chips (or this chip in a different configuration). So it would be best if we could restore the AXI SPI Engine to the way it was working before.

Quoted 33 lines
Quoted 4 lines
Quoted 4 lines

This series fixes the call ordering so all SPI bus accesses complete before the offload becomes active, consistent with the constraint that was already documented for the BUSY_GP_EN write in the same function.

...

Quoted 12 lines

drivers/iio/adc/ad4695.c static int ad4695_offload_buffer_postenable(struct iio_dev *indio_dev) · line 884

Quoted 9 lines
drivers/iio/adc/ad4695.c +3 -3
-	ret = spi_offload_trigger_enable(st->offload, st->offload_trigger,
-					 &config);
+	ret = ad4695_enter_advanced_sequencer_mode(st, num_slots);
 	if (ret)
 		goto err_disable_busy_output;
 
-	ret = ad4695_enter_advanced_sequencer_mode(st, num_slots);
+	ret = spi_offload_trigger_enable(st->offload, st->offload_trigger,
+					 &config);

Swapping the order here introduces a race condition.

Quoted 6 lines

ad4695_enter_advanced_sequencer_mode() starts sampling (triggered from the
internal clock in the ADC) and the first sample could be completed before the SPI offload is enabled. If the first sample is missed, then all of the data will be off by one index on the buffer.

I thought this too at the first glance, but couldn’t be sure so I tried testing it anyway using the FMCZ evaluation board and a Zedboard, and everything worked as expected.
I have plotted buffered data and it showed exactly what the Signal Generator was outputting and on the right channel, so no index was off, if that's what you meant.

Quoted 3 lines

Best Regards,
Radu

Does it still work correctly if you add a msleep(100); after enabling the sequencer and before enabling the SPI offload? (to simulate a slow/busy machine).

It would also help to look at the SPI signals with a logic analyzer to see what the timing is.

I guess it could still be OK since we currently only support the case of the PWM acting as the CNV source.

#4 lore raw
Quoted 6 lines

-----Original Message-----
From: David Lechner <[email protected]>
Sent: Monday, March 30, 2026 7:46 PM
To: Sabau, Radu bogdan <[email protected]>; Lars-Peter Clausen

Hi David, Andy,

Quoted 4 lines

Which commit is the HDL being built from? It could be that something changed in the FPGA implementation rather than the driver.

Good call. The bitstream was built from commit 79446d8ee in the ADI HDL tree — "library/spi_engine: fix spi engine for SDI backpressure", authored by Carlos Souza, merged February 4 2026.

That commit made two relevant changes to spi_engine_offload.v:

1. cmd_valid was decoupled from spi_active. Previously:

     assign cmd_valid = spi_active;

Now cmd_valid is driven by a separate offload_cmd_valid register. The practical effect is cmd_valid is no longer asserted the instant spi_active goes high.

2. spi_active now stays asserted until the DMA has consumed all pending SDI data, not just until the last command is accepted. The old deassertion condition was:

     cmd_ready && (spi_cmd_rd_addr_next == ctrl_cmd_wr_addr)

The new condition holds spi_active high while offload_disable_pending && sdi_data_valid. Since interconnect_dir = spi_enable | spi_active, this means the interconnect stays in offload direction for the full duration of the SDI drain phase.

The February commit also changed the execution module's io_ready1 logic (now gated on pending_sdi_data_valid instead of sdi_data_valid directly), which shifts when the execution engine signals completion via SYNC relative to the last bit being clocked. Together these changes alter the timing of the mode boundary between regular SPI and offload. The panic — as shown below — is consistent with a race where SPI_ENGINE_INT_CMD_ALMOST_EMPTY is left armed in int_enable after host->cur_msg has already been cleared. The exact causal chain needs more instrumentation to confirm, but this commit is the most significant HDL delta between your test setup and mine.

So your instinct was right — something changed on the FPGA side, specifically the SDI backpressure fix. The driver ordering fix is still correct regardless (and I would say is the right thing to do), but I wanted to close the loop on the root cause.

Regarding the race you raised with the new ordering — I tested with msleep(100) inserted between ad4695_enter_advanced_sequencer_mode() and spi_offload_trigger_enable() to simulate a slow machine. Data came back correctly with no index shift. This confirms the expectation: with PWM as the CNV source the ADC only converts on an external CNV pulse, and the PWM isn't running until spi_offload_trigger_enable() enables the trigger, so there is nothing to miss during that gap.

And for the 5 most relevant panic lines, here they are:

  Unable to handle kernel NULL pointer dereference at virtual address 0000002c when read
  PC is at spi_engine_write_cmd_fifo+0x8/0x74
  LR is at spi_engine_irq+0x10c/0x290
  r1 : 00000000
   spi_engine_write_cmd_fifo from spi_engine_irq+0x10c/0x290

The sequence that leads there with the old call ordering:

1. spi_offload_trigger_enable() completes — offload is now active, interconnect_dir switches to 1 (offload direction).

2. ad4695_enter_advanced_sequencer_mode() submits a regular SPI transfer. spi_engine_transfer_one_message() sets host->cur_msg, writes commands to the CPU CMD FIFO, and arms SPI_ENGINE_INT_CMD_ALMOST_EMPTY in int_enable, then waits for completion.

3. With interconnect_dir=1, s1_cmd_ready=0 — the execution engine cannot consume the CPU commands. The regular transfer stalls.

4. The PWM fires a CNV pulse. The offload starts executing its pre-programmed command sequence and sends its SYNC through the s0 path (not s1), so the CPU transfer never gets its completion signal.

5. The regular SPI transfer times out or its completion path is disrupted. host->cur_msg gets cleared (set to NULL), but SPI_ENGINE_INT_CMD_ALMOST_EMPTY is still armed in int_enable.

6. SPI_ENGINE_INT_CMD_ALMOST_EMPTY fires. The IRQ handler reads host->cur_msg = NULL into msg (r1=0x00000000), then calls spi_engine_write_cmd_fifo(spi_engine, NULL). The function immediately dereferences msg at offset 0x2c — 8 bytes into the function — and the kernel panics.

Best regards,
Radu

reviewer
#5 lore raw
Quoted 54 lines · Radu bogdan
Quoted 6 lines

-----Original Message-----
From: David Lechner <[email protected]>
Sent: Monday, March 30, 2026 7:46 PM
To: Sabau, Radu bogdan <[email protected]>; Lars-Peter Clausen

Hi David, Andy,

Quoted 4 lines

Which commit is the HDL being built from? It could be that something changed in the FPGA implementation rather than the driver.

Good call. The bitstream was built from commit 79446d8ee in the ADI HDL tree — "library/spi_engine: fix spi engine for SDI backpressure", authored by Carlos Souza, merged February 4 2026.

That commit made two relevant changes to spi_engine_offload.v:

1. cmd_valid was decoupled from spi_active. Previously:

     assign cmd_valid = spi_active;

Now cmd_valid is driven by a separate offload_cmd_valid register. The practical effect is cmd_valid is no longer asserted the instant spi_active goes high.

2. spi_active now stays asserted until the DMA has consumed all pending SDI data, not just until the last command is accepted. The old deassertion condition was:

     cmd_ready && (spi_cmd_rd_addr_next == ctrl_cmd_wr_addr)

The new condition holds spi_active high while offload_disable_pending && sdi_data_valid. Since interconnect_dir = spi_enable | spi_active, this means the interconnect stays in offload direction for the full duration of the SDI drain phase.

The February commit also changed the execution module's io_ready1 logic (now gated on pending_sdi_data_valid instead of sdi_data_valid directly), which shifts when the execution engine signals completion via SYNC relative to the last bit being clocked. Together these changes alter the timing of the mode boundary between regular SPI and offload. The panic — as shown below — is consistent with a race where SPI_ENGINE_INT_CMD_ALMOST_EMPTY is left armed in int_enable after host->cur_msg has already been cleared. The exact causal chain needs more instrumentation to confirm, but this commit is the most significant HDL delta between your test setup and mine.

So your instinct was right — something changed on the FPGA side, specifically the SDI backpressure fix. The driver ordering fix is still correct regardless (and I would say is the right thing to do), but I wanted to close the loop on the root cause.

Quoted 7 lines

Regarding the race you raised with the new ordering — I tested with msleep(100) inserted between ad4695_enter_advanced_sequencer_mode() and spi_offload_trigger_enable() to simulate a slow machine. Data came back correctly with no index shift. This confirms the expectation: with PWM as the CNV source the ADC only converts on an external CNV pulse, and the PWM isn't running until spi_offload_trigger_enable() enables the trigger, so there is nothing to miss during that gap.

Thanks for clearing this up. I found my notes from when I was working on this (back in Oct. 2024). It looks like when you aren't using an external CNV trigger, then ad4695_enter_advanced_sequencer_mode() actually triggers the first conversion. But it looks like in the end, we ended up only supporting the case where PWM is connected to CNV.

So the ordering currently in the driver was probably from an earlier revision that supported more wiring options.

So yes, I'm convinced that changing the ordering for the PWM/CNV case should be OK.

We'll still want a v2 though with an updated commit message that gives the reason for the change independent of the AXI SPI Engine changes that triggered a bug.

Reviewed-by on v1 1/1
#6 lore raw
In reply to Radu Sabau
Quoted 25 lines · Radu Sabau

ad4695_enter_advanced_sequencer_mode() was called after spi_offload_trigger_enable(), meaning a regular SPI transfer could be in flight while the offload was already active. When the offload fires its completion interrupt concurrently with the regular transfer, the SPI engine interrupt handler is not designed to handle both at once, leading to a kernel panic.

Fix this by calling ad4695_enter_advanced_sequencer_mode() before spi_offload_trigger_enable(), ensuring all SPI bus accesses are complete before the offload becomes active. This is consistent with the same constraint that already applies to the BUSY_GP_EN write above it.

Update the error unwind labels accordingly: add err_exit_conversion_mode so that a failure of spi_offload_trigger_enable() correctly exits conversion mode before clearing BUSY_GP_EN.

Fixes f09f140e3ea8 ("iio: adc: ad4695: Add support for SPI offload")

---
When enabling the IIO buffer for SPI offload operation on AD4695/AD4696, ad4695_enter_advanced_sequencer_mode() was called after spi_offload_trigger_enable(), resulting in a regular SPI transfer being in flight while the offload was already active. This caused a kernel panic in the SPI engine interrupt handler.

Quoted 4 lines

This series fixes the call ordering so all SPI bus accesses complete before the offload becomes active, consistent with the constraint that was already documented for the BUSY_GP_EN write in the same function.
---

LGTM,

Quoted 78 lines
 drivers/iio/adc/ad4695.c | 31 ++++++++++++++-----------------
 1 file changed, 14 insertions(+), 17 deletions(-)
drivers/iio/adc/ad4695.c +14 -18
index cda419638d9a..c6721f5ac8af 100644
@@ -866,24 +866,24 @@ static int ad4695_offload_buffer_postenable(struct iio_dev *indio_dev)
 		return ret;
 
 	/*
-	 * NB: technically, this is part the SPI offload trigger enable, but it
-	 * doesn't work to call it from the offload trigger enable callback
-	 * because it requires accessing the SPI bus. Calling it from the
-	 * trigger enable callback could cause a deadlock.
+	 * NB: these are technically part of the SPI offload trigger enable, but
+	 * they can't be called from the offload trigger enable callback because
+	 * they require accessing the SPI bus. Calling them from the trigger
+	 * enable callback could cause a deadlock.
 	 */
 	ret = regmap_set_bits(st->regmap, AD4695_REG_GP_MODE,
 			      AD4695_REG_GP_MODE_BUSY_GP_EN);
 	if (ret)
 		goto err_unoptimize_message;
 
-	ret = spi_offload_trigger_enable(st->offload, st->offload_trigger,
-					 &config);
+	ret = ad4695_enter_advanced_sequencer_mode(st, num_slots);
 	if (ret)
 		goto err_disable_busy_output;
 
-	ret = ad4695_enter_advanced_sequencer_mode(st, num_slots);
+	ret = spi_offload_trigger_enable(st->offload, st->offload_trigger,
+					 &config);
 	if (ret)
-		goto err_offload_trigger_disable;
+		goto err_exit_conversion_mode;
 
 	mutex_lock(&st->cnv_pwm_lock);
 	pwm_get_state(st->cnv_pwm, &state);
@@ -895,22 +895,19 @@ static int ad4695_offload_buffer_postenable(struct iio_dev *indio_dev)
 	ret = pwm_apply_might_sleep(st->cnv_pwm, &state);
 	mutex_unlock(&st->cnv_pwm_lock);
 	if (ret)
-		goto err_offload_exit_conversion_mode;
+		goto err_trigger_disable;
 
 	return 0;
 
-err_offload_exit_conversion_mode:
+err_trigger_disable:
 	/*
-	 * We have to unwind in a different order to avoid triggering offload.
-	 * ad4695_exit_conversion_mode() triggers a conversion, so it has to be
-	 * done after spi_offload_trigger_disable().
+	 * ad4695_exit_conversion_mode() triggers a conversion, so the offload
+	 * must be disabled first to avoid capturing a spurious sample.
 	 */
 	spi_offload_trigger_disable(st->offload, st->offload_trigger);
-	ad4695_exit_conversion_mode(st);
-	goto err_disable_busy_output;
 
-err_offload_trigger_disable:
-	spi_offload_trigger_disable(st->offload, st->offload_trigger);
+err_exit_conversion_mode:
+	ad4695_exit_conversion_mode(st);
 
 err_disable_busy_output:
 	regmap_clear_bits(st->regmap, AD4695_REG_GP_MODE,
 
---

base-commit: 11439c4635edd669ae435eec308f4ab8a0804808
change-id: 20260330-ad4696-fix-186955a8c511

Best regards,

Signature · 1 line
Radu Sabau <[email protected]>
#7 lore raw
In reply to Radu Sabau
Quoted 6 lines · On Mon, Mar 30, 2026 at 04:34:39PM +0300, Radu Sabau via B4 Relay wrote:

ad4695_enter_advanced_sequencer_mode() was called after spi_offload_trigger_enable(), meaning a regular SPI transfer could be in flight while the offload was already active. When the offload fires its completion interrupt concurrently with the regular transfer, the SPI engine interrupt handler is not designed to handle both at once, leading to a kernel panic.

Please, provide ~3-5 (the most important) lines of that panic.

Quoted 8 lines

Fix this by calling ad4695_enter_advanced_sequencer_mode() before spi_offload_trigger_enable(), ensuring all SPI bus accesses are complete before the offload becomes active. This is consistent with the same constraint that already applies to the BUSY_GP_EN write above it.

Update the error unwind labels accordingly: add err_exit_conversion_mode so that a failure of spi_offload_trigger_enable() correctly exits conversion mode before clearing BUSY_GP_EN.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
reviewer
#8 lore raw
In reply to Radu Sabau
Quoted 40 lines · On 3/30/26 8:34 AM, Radu Sabau via B4 Relay wrote:

From: Radu Sabau <[email protected]>

ad4695_enter_advanced_sequencer_mode() was called after spi_offload_trigger_enable(), meaning a regular SPI transfer could be in flight while the offload was already active. When the offload fires its completion interrupt concurrently with the regular transfer, the SPI engine interrupt handler is not designed to handle both at once, leading to a kernel panic.

Fix this by calling ad4695_enter_advanced_sequencer_mode() before spi_offload_trigger_enable(), ensuring all SPI bus accesses are complete before the offload becomes active. This is consistent with the same constraint that already applies to the BUSY_GP_EN write above it.

Update the error unwind labels accordingly: add err_exit_conversion_mode so that a failure of spi_offload_trigger_enable() correctly exits conversion mode before clearing BUSY_GP_EN.

Fixes f09f140e3ea8 ("iio: adc: ad4695: Add support for SPI offload")

---
When enabling the IIO buffer for SPI offload operation on AD4695/AD4696, ad4695_enter_advanced_sequencer_mode() was called after spi_offload_trigger_enable(), resulting in a regular SPI transfer being in flight while the offload was already active. This caused a kernel panic in the SPI engine interrupt handler.

This series fixes the call ordering so all SPI bus accesses complete before the offload becomes active, consistent with the constraint that was already documented for the BUSY_GP_EN write in the same function.

---
 drivers/iio/adc/ad4695.c | 31 ++++++++++++++-----------------
 1 file changed, 14 insertions(+), 17 deletions(-)
drivers/iio/adc/ad4695.c +0 -0
index cda419638d9a..c6721f5ac8af 100644
@@ -866,24 +866,24 @@ static int ad4695_offload_buffer_postenable(struct iio_dev *indio_dev)
 		return ret;

drivers/iio/adc/ad4695.c static int ad4695_offload_buffer_postenable(struct iio_dev *indio_dev) · line 872

Quoted 10 lines
drivers/iio/adc/ad4695.c +4 -4
 
 	/*
-	 * NB: technically, this is part the SPI offload trigger enable, but it
-	 * doesn't work to call it from the offload trigger enable callback
-	 * because it requires accessing the SPI bus. Calling it from the
-	 * trigger enable callback could cause a deadlock.
+	 * NB: these are technically part of the SPI offload trigger enable, but
+	 * they can't be called from the offload trigger enable callback because
+	 * they require accessing the SPI bus. Calling them from the trigger
+	 * enable callback could cause a deadlock.

I didn't see a explanation for changing the comment here. I think "this" is still correct instead of "these" because only the BUSY pin is the trigger, so only the one regmap_set_bits() below would be in the trigger callback if it was possible.

If we want to improve the comment to make it more clear, I would save that for a separate patch and specifically mention the BUSY output.

Quoted 28 lines
drivers/iio/adc/ad4695.c +5 -5
 	 */
 	ret = regmap_set_bits(st->regmap, AD4695_REG_GP_MODE,
 			      AD4695_REG_GP_MODE_BUSY_GP_EN);
 	if (ret)
 		goto err_unoptimize_message;
 
-	ret = spi_offload_trigger_enable(st->offload, st->offload_trigger,
-					 &config);
+	ret = ad4695_enter_advanced_sequencer_mode(st, num_slots);
 	if (ret)
 		goto err_disable_busy_output;
 
-	ret = ad4695_enter_advanced_sequencer_mode(st, num_slots);
+	ret = spi_offload_trigger_enable(st->offload, st->offload_trigger,
+					 &config);
 	if (ret)
-		goto err_offload_trigger_disable;
+		goto err_exit_conversion_mode;
 
 	mutex_lock(&st->cnv_pwm_lock);
 	pwm_get_state(st->cnv_pwm, &state);
@@ -895,22 +895,19 @@ static int ad4695_offload_buffer_postenable(struct iio_dev *indio_dev)
 	ret = pwm_apply_might_sleep(st->cnv_pwm, &state);
 	mutex_unlock(&st->cnv_pwm_lock);
 	if (ret)
-		goto err_offload_exit_conversion_mode;
+		goto err_trigger_disable;
 

drivers/iio/adc/ad4695.c static int ad4695_offload_buffer_postenable(struct iio_dev *indio_dev) · line 905

Quoted 10 lines
drivers/iio/adc/ad4695.c +3 -4
 	return 0;
 
-err_offload_exit_conversion_mode:
+err_trigger_disable:
 	/*
-	 * We have to unwind in a different order to avoid triggering offload.
-	 * ad4695_exit_conversion_mode() triggers a conversion, so it has to be
-	 * done after spi_offload_trigger_disable().
+	 * ad4695_exit_conversion_mode() triggers a conversion, so the offload
+	 * must be disabled first to avoid capturing a spurious sample.

Since this becomes the natural unwinding order, I don't think we need the comment at all any more.

There is still a similar comment in ad4695_offload_buffer_predisable() so we aren't losing this information if we remove it here.

Quoted 18 lines
drivers/iio/adc/ad4695.c +2 -5
 	 */
 	spi_offload_trigger_disable(st->offload, st->offload_trigger);
-	ad4695_exit_conversion_mode(st);
-	goto err_disable_busy_output;
 
-err_offload_trigger_disable:
-	spi_offload_trigger_disable(st->offload, st->offload_trigger);
+err_exit_conversion_mode:
+	ad4695_exit_conversion_mode(st);
 
 err_disable_busy_output:
 	regmap_clear_bits(st->regmap, AD4695_REG_GP_MODE,
 
---

base-commit: 11439c4635edd669ae435eec308f4ab8a0804808
change-id: 20260330-ad4696-fix-186955a8c511

Best regards,

#9 lore raw
Quoted 5 lines

-----Original Message-----
From: David Lechner <[email protected]>
Sent: Tuesday, March 31, 2026 5:02 PM

...

Quoted 15 lines

drivers/iio/adc/ad4695.c static int ad4695_offload_buffer_postenable(struct iio_dev *indio_dev) · line 872

Quoted 10 lines
drivers/iio/adc/ad4695.c +4 -4
 	/*
-	 * NB: technically, this is part the SPI offload trigger enable, but it
-	 * doesn't work to call it from the offload trigger enable callback
-	 * because it requires accessing the SPI bus. Calling it from the
-	 * trigger enable callback could cause a deadlock.
+	 * NB: these are technically part of the SPI offload trigger enable, but
+	 * they can't be called from the offload trigger enable callback because
+	 * they require accessing the SPI bus. Calling them from the trigger
+	 * enable callback could cause a deadlock.

I didn't see a explanation for changing the comment here. I think "this" is still correct instead of "these" because only the BUSY pin is the trigger, so only the one regmap_set_bits() below would be in the trigger callback if it was possible.

I was thinking this comment could be left untouched as well, but couldn't decide on whether to modify it or not and thought is better to wait for a second opinion on it. Thanks for clarifying this!

Quoted 9 lines

If we want to improve the comment to make it more clear, I would save that for a separate patch and specifically mention the BUSY output.

Quoted 5 lines
drivers/iio/adc/ad4695.c +0 -0
 	 */
 	ret = regmap_set_bits(st->regmap, AD4695_REG_GP_MODE,
 			      AD4695_REG_GP_MODE_BUSY_GP_EN);
 	if (ret)
 		goto err_unoptimize_message;

...

Quoted 10 lines

drivers/iio/adc/ad4695.c static int ad4695_offload_buffer_postenable(struct iio_dev *indio_dev)

Quoted 6 lines
drivers/iio/adc/ad4695.c +1 -3
-err_offload_exit_conversion_mode:
+err_trigger_disable:
 	/*
-	 * We have to unwind in a different order to avoid triggering offload.
-	 * ad4695_exit_conversion_mode() triggers a conversion, so it has to

be

Review

Quoted 2 lines
(quoted diff) +1 -1
-	 * done after spi_offload_trigger_disable().
+	 * ad4695_exit_conversion_mode() triggers a conversion, so the

offload

Quoted 8 lines

drivers/iio/adc/ad4695.c static int ad4695_offload_buffer_postenable(struct iio_dev *indio_dev) · line 905

Quoted 1 line
drivers/iio/adc/ad4695.c +1 -0
+	 * must be disabled first to avoid capturing a spurious sample.

Since this becomes the natural unwinding order, I don't think we need the comment at all any more.

There is still a similar comment in ad4695_offload_buffer_predisable() so we aren't losing this information if we remove it here.

Yep, I agree on this, too. The comment in postenable could be removed.