Re: [PATCH v7 11/12] iio: adc: ad7768-1: add filter type and oversampling ratio attributes

From: Andy Shevchenko
Date: Thu May 08 2025 - 15:39:52 EST


On Thu, May 08, 2025 at 02:05:26PM -0300, Jonathan Santos wrote:
> Separate filter type and decimation rate from the sampling frequency
> attribute. The new filter type attribute enables sinc3, sinc3+rej60
> and wideband filters, which were previously unavailable.
>
> Previously, combining decimation and MCLK divider in the sampling
> frequency obscured performance trade-offs. Lower MCLK divider
> settings increase power usage, while lower decimation rates reduce
> precision by decreasing averaging. By creating an oversampling
> attribute, which controls the decimation, users gain finer control
> over performance.
>
> The addition of those attributes allows a wider range of sampling
> frequencies and more access to the device features. Sampling frequency
> table is updated after every digital filter parameter change.
>
> Changes in the sampling frequency are not allowed anymore while in
> buffered mode.

...

> static void ad7768_fill_samp_freq_tbl(struct ad7768_state *st)
> {
> - unsigned int i;
> + unsigned int i, freq_filtered, len = 0;
> +
> + freq_filtered = DIV_ROUND_CLOSEST(st->mclk_freq, st->oversampling_ratio);
> + for (i = 0; i < ARRAY_SIZE(ad7768_mclk_div_rates); i++) {

> + st->samp_freq_avail[len] = DIV_ROUND_CLOSEST(freq_filtered,
> + ad7768_mclk_div_rates[i]);

Same comment as per previous patch.

> + /* Sampling frequency cannot be lower than the minimum of 50 SPS */
> + if (st->samp_freq_avail[len] >= 50)
> + len++;

Actually I would rather see the assignment once.

... samp_freq_avail;

for (i = 0; i < ARRAY_SIZE(ad7768_mclk_div_rates); i++) {
/* Sampling frequency cannot be lower than the minimum of 50 SPS */
samp_freq_avail = DIV_ROUND_CLOSEST(freq_filtered, ad7768_mclk_div_rates[i]);
if (samp_freq_avail < 50)
continue;

st->samp_freq_avail[len++] = samp_freq_avail;
}

> + }
> +
> + st->samp_freq_avail_len = len;
> +}

...

> + res = DIV_ROUND_CLOSEST(st->mclk_freq, freq * st->oversampling_ratio);
>
> /* Find the closest match for the desired sampling frequency */
> + for (i = 0; i < ARRAY_SIZE(ad7768_mclk_div_rates); i++) {
> + diff_new = abs(res - ad7768_mclk_div_rates[i]);
> if (diff_new < diff_old) {
> diff_old = diff_new;
> idx = i;
> }
> }

Hmm... Wasn't the point to include util_macros.h to replace the above?

--
With Best Regards,
Andy Shevchenko