Re: [PATCH v3 12/13] iio: adc: ad4134: Support high-speed data capture

From: Andy Shevchenko

Date: Wed Sep 30 2026 - 05:47:18 EST


On Tue, Sep 29, 2026 at 07:45:40PM -0300, Marcelo Schmitt wrote:
> Make use of SPI transfer offloading to speed up data capture, enabling data
> acquisition at faster sample rates (up to 1.496 MSPS).

...

> + odr_wf.period_length_ns = DIV_ROUND_UP_ULL(NSEC_PER_SEC, freq_Hz);
> + /*
> + * Set the PWM duty cycle to keep ODR high for at least minimum required
> + * time. If the rounded PWM's value is less than the minimum required,
> + * increase the target value by 10 and attempt to round the waveform
> + * again, until the minimum (or try count limit) is reached.
> + */
> + odr_high_time_ns = div64_ul(6ULL * NSEC_PER_SEC, st->sys_clk_hz);
> + target_ns = 0;
> + count = 100;
> + do {
> + target_ns += 10; /* Increment by PWM duty cycle period */
> + odr_wf.duty_length_ns = target_ns;
> + ret = pwm_round_waveform_might_sleep(st->odr_pwm, &odr_wf);
> + if (ret)
> + return ret;
> + } while (count-- && odr_wf.duty_length_ns < odr_high_time_ns);

In do {} while () cases the counter check should be --count, otherwise it's
off-by-one (non-critical) mistake, you will have 101 iterations instead of 100.

> + /* Check the minimum ODR high time is met */
> + if (odr_wf.duty_length_ns < odr_high_time_ns)
> + return -EDOM;
> +
> + if (odr_wf.period_length_ns < 2 * odr_high_time_ns)
> + return -EDOM;
> +
> + /*
> + * Configure SPI offload PWM trigger.
> + * For gated DCLK, the minimum required time between ODR rising edge
> + * and DCLK rising edge is the sum of ODR high time and ODR falling
> + * edge to DCLK rising edge time. Delay the offload trigger for at least
> + * that amount of time so the ADC sample data will be available when the
> + * SPI transfer begin.
> + *
> + * Use the same period as ODR PWM to avoid timing issues.
> + * Convert back from period to frequency for the SPI offload API.
> + */
> + offload_period_ns = odr_wf.period_length_ns;
> + config.periodic.frequency_hz = DIV_ROUND_UP_ULL(HZ_PER_GHZ, offload_period_ns);
> + offload_offset_ns = odr_high_time_ns + AD4134_DCLK_RISING_OFFSET_NS;
> + count = 100;
> + do {
> + config.periodic.offset_ns = offload_offset_ns;
> + ret = spi_offload_trigger_validate(st->offload_trigger, &config);
> + if (ret)
> + return ret;
> +
> + offload_offset_ns += 10;
> + } while (count-- && config.periodic.offset_ns < odr_high_time_ns +
> + AD4134_DCLK_RISING_OFFSET_NS);

Ditto.

...

> + if (!st->sys_clk_hz)
> + return dev_err_probe(dev, -EINVAL, "0 Hz external clock\n");

Perhaps -ERANGE?


...

Address the above only if you need a new version, otherwise they are not a big
deal.

--
With Best Regards,
Andy Shevchenko