From: sashiko-bot@kernel.org
To: "Olivier Moysan" <olivier.moysan@foss.st.com>
Cc: devicetree@vger.kernel.org, dri-devel@lists.freedesktop.org,
linux-sound@vger.kernel.org, robh@kernel.org,
conor+dt@kernel.org, media-ci@linuxtv.org
Subject: Re: [PATCH v2 06/11] iio: adc: stm32-mdf: configure cic filters and sample rates
Date: Wed, 18 Nov 2026 15:34:45 +0000 [thread overview]
Message-ID: <sashiko-outbox-221345@kernel.org> (raw)
In-Reply-To: <20261118-upstream_mdf_v2-v2-6-7939d3c3720a@foss.st.com>
Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [High] iio: adc: stm32-mdf: division by zero in
stm32_mdf_adc_check_clock_config()
- [High] iio: adc: stm32-mdf: division by zero when decim_ratio is 0
- [Medium] iio: adc: stm32-mdf: wrong DATSRC mask in
stm32_mdf_adc_apply_filters_config()
- [Medium] iio: adc: stm32-mdf: wrong FastSinc scale in
stm32_mdf_adc_set_filters_config()
- [Medium] iio: adc: stm32-mdf: missing rsflt_bypass copy for
interleaved filters
- [Medium] iio: adc: stm32-mdf: broken ratio checks in
stm32_mdf_adc_check_clock_config()
--
commit 76232b76e094892afbfa80eaf7eda8e245546546
Author: Olivier Moysan <olivier.moysan@foss.st.com>
iio: adc: stm32-mdf: configure cic filters and sample rates
This commit adds helper functions to compute CIC and reshape filter
decimation ratios, data scaling, and serial clock validation from a
requested sampling frequency.
> diff --git a/drivers/iio/adc/stm32-mdf-adc.c b/drivers/iio/adc/stm32-mdf-adc.c
> index 411af78911c91..eae7a4fa14851 100644
> --- a/drivers/iio/adc/stm32-mdf-adc.c
> +++ b/drivers/iio/adc/stm32-mdf-adc.c
[ ... ]
> @@ -271,6 +329,352 @@ static struct stm32_mdf_adc *stm32_mdf_get_filter_by_handle(struct stm32_mdf *md
> return NULL;
> }
>
> +static int stm32_mdf_adc_compute_scale(struct device *dev, unsigned int decim,
> + unsigned int order, unsigned int data_size)
> +{
[ ... ]
> + /* scale = 20 * ((DS - 1) * log10(2) - NF * log10(D)) */
> + scale = 20 * ((data_size - 1) * stm32_mdf_log_table[0].log - order * logd);
> +
> + return scale;
> +}
> +
> +static int stm32_mdf_adc_apply_filters_config(struct stm32_mdf_adc *adc, unsigned int scale)
> +{
> + struct stm32_mdf_adc *adc_inter;
> + u32 msk, val;
> + int ret, cnt = 0;
> +
> + /* Apply conf from filter0 to interleaved filters if any */
> + if (MDF_IS_FILTER0(adc) && adc->mdf->nb_interleave) {
> + list_for_each_entry(adc_inter, &adc->mdf->filter_list, entry) {
> + if (MDF_IS_INTERLEAVED_FILT_NOT_0(adc_inter)) {
> + adc_inter->datsrc = adc->datsrc;
> + adc_inter->cicmode = adc->cicmode;
> + adc_inter->decim_cic = adc->decim_cic;
> + adc_inter->nbdis = adc->nbdis;
> + adc_inter->hpf_cutoff = adc->hpf_cutoff;
> +
> + stm32_mdf_adc_apply_filters_config(adc_inter, scale);
> + cnt++;
> + }
> + }
[Severity: Medium]
Should this loop also copy adc->rsflt_bypass to adc_inter->rsflt_bypass
and check the return value of stm32_mdf_adc_apply_filters_config()?
Secondary interleaved filters skip filter property parsing during probe,
so adc_inter->rsflt_bypass stays false even when filter 0 bypasses the
reshape filter, leaving MDF_DFLTRSFR_RSFLTBYP unset on the secondary
filters and applying an unintended 4x decimation. Any register update
failure on the secondary filters is also ignored.
[ ... ]
> + /* Configure CICR */
> + msk = MDF_SITFCR_SCKSRC_MASK | MDF_DFLTCICR_CICMOD_MASK |
> + MDF_DFLTCICR_MCICD_MASK | MDF_DFLTCICR_SCALE_MASK;
> + val = FIELD_PREP(MDF_SITFCR_SCKSRC_MASK, adc->datsrc) |
> + FIELD_PREP(MDF_DFLTCICR_CICMOD_MASK, adc->cicmode) |
> + FIELD_PREP(MDF_DFLTCICR_MCICD_MASK, adc->decim_cic - 1) |
> + FIELD_PREP(MDF_DFLTCICR_SCALE_MASK, scale);
> +
> + ret = regmap_update_bits(adc->regmap, MDF_DFLTCICR_REG, msk, val);
[Severity: Medium]
Should this use MDF_DFLTCICR_DATSRC_MASK instead of
MDF_SITFCR_SCKSRC_MASK when programming adc->datsrc into
MDF_DFLTCICR_REG?
MDF_SITFCR_SCKSRC_MASK is GENMASK(2, 1) whereas MDF_DFLTCICR_DATSRC_MASK
is GENMASK(1, 0), so adc->datsrc is shifted into bits [2:1] (where bit 2
is reserved) and bit 0 of DATSRC is left unmasked.
[ ... ]
> +static int stm32_mdf_adc_set_filters_config(struct iio_dev *indio_dev, unsigned int decim)
> +{
> + struct stm32_mdf_adc *adc = iio_priv(indio_dev);
> + struct device *dev = &indio_dev->dev;
> + unsigned int decim_cic, decim_rsflt = 1;
> + unsigned int data_size = STM32_MDF_DATA_RES, order = adc->cicmode;
[ ... ]
> + if (is_power_of_2(decim_cic)) {
> + /*
> + * Decimation ratio is a power of 2: D = 2 exp n
> + * max scale = 20 * (DS - n * NF) * log10(2)
> + */
> + log = stm32_mdf_log_table[0].log;
> +
> + /* Compute max scale (dB) * 1000 */
> + max_scale = (20 * (data_size - 1 - (order * (fls(decim_cic) - 1))) * log);
> + } else {
> + /*
> + * Decimation ratio is not a power of 2
> + * max scale = 20 * ((DS - 1) * log10(2) - NF * log10(D))
> + */
> + max_scale = stm32_mdf_adc_compute_scale(dev, decim_cic, order, data_size);
> + }
[Severity: Medium]
Does the max_scale calculation handle FastSinc mode (adc->cicmode == 0)?
As noted in the dflt_max calculation below, FastSinc has a CIC gain of
2 * D^2, but both branches here (and stm32_mdf_adc_compute_scale() above)
multiply the decimation log term by order = 0. That treats the CIC gain
as 0 dB and always selects the maximum +72.2 dB scale (0x18), saturating
the filter output and overflowing the 32-bit adc->dflt_max when
decim_cic >= 12.
[ ... ]
> + adc->decim_cic = decim_cic;
[ ... ]
> +static int stm32_mdf_adc_check_clock_config(struct stm32_mdf_adc *adc, unsigned long *sck_freq)
> +{
> + unsigned long cck_expected_freq;
> + unsigned int ratio, cic_ratio;
> + int ret, error = 0;
[ ... ]
> + /*
> + * Mode SPI: check Fproc >= 4 * Fsck
> + * Mode LF SPI: check Fproc >= 2 * Fsck
> + */
> + ratio = DIV_ROUND_CLOSEST(adc->mdf->fproc, *sck_freq);
> + if ((adc->sitf->mode == STM32_MDF_MODE_SPI && ratio <= 4) ||
> + (adc->sitf->mode == STM32_MDF_MODE_LF_SPI && ratio <= 2)) {
> + dev_dbg(adc->dev, "Wrong Fproc/Fsck ratio [%d]. sitf mode [%d], RSFLT [%s]\n",
> + ratio, adc->sitf->mode, adc->rsflt_bypass ? "off" : "on");
> + error |= STM32_MDF_ERR_MODE_RATIO;
> + }
> +
> + /* RSFLT enabled: check Fproc > 24 * Fsck / (MCICD+1) */
> + cic_ratio = DIV_ROUND_CLOSEST(24, adc->decim_cic);
> + if (!adc->rsflt_bypass && ratio <= cic_ratio)
> + error |= STM32_MDF_ERR_DECIM_RATIO;
[Severity: High]
Can DIV_ROUND_CLOSEST(24, adc->decim_cic) divide by zero here?
mdf_adc_set_samp_freq() calls stm32_mdf_adc_check_clock_config() before
calling stm32_mdf_adc_set_filters_config(), which is the only place that
sets adc->decim_cic. On the first call adc->decim_cic is still 0,
causing a division by zero, and on later sample rate changes this check
uses the stale adc->decim_cic from the previous configuration.
> +
> + /* If we found an error, try to restore clocks initial settings */
> + if (error) {
> + ret = stm32_mdf_core_restore_cck(adc->mdf);
> + if (ret < 0)
> + goto err;
> +
> + *sck_freq = clk_get_rate(adc->sitf->sck);
> + if (!*sck_freq) {
> + ret = -EINVAL;
> + goto err;
> + }
> + }
> +
> + return 0;
[Severity: Medium]
Does stm32_mdf_adc_check_clock_config() accept invalid clock ratios and
reject valid boundary ratios?
The SPI mode checks above use ratio <= 4 and ratio <= 2 instead of
ratio < 4 and ratio < 2, rejecting the valid boundary ratios Fproc = 4 *
Fsck and Fproc = 2 * Fsck. In addition, when error is non-zero, this
function calls stm32_mdf_core_restore_cck() and returns 0 without
re-checking whether the restored clock satisfies the frequency and
Fproc/Fsck ratio constraints. Finally, mdf_adc_set_samp_freq() computes
the default sample_freq from sck_freq before calling
stm32_mdf_adc_check_clock_config(), so if *sck_freq is updated here,
sample_freq stays based on the old rate.
[ ... ]
> +static int __maybe_unused mdf_adc_set_samp_freq(struct iio_dev *indio_dev)
> +{
[ ... ]
> + if (!sample_freq)
> + sample_freq = sck_freq / MDF_DEFAULT_DECIM_RATIO;
> +
> + ret = stm32_mdf_adc_check_clock_config(adc, &sck_freq);
> + if (ret)
> + goto err;
> +
> + decim_ratio = DIV_ROUND_CLOSEST(sck_freq, sample_freq);
> +
> + delta = abs(sck_freq - (decim_ratio * sample_freq));
> + delta_ppm = (1000000 * delta) / sck_freq;
> + if (delta_ppm > 1000)
> + dev_warn(dev, "Sample rate deviation [%lu] ppm: [%lu] vs [%lu] Hz\n",
> + delta_ppm, sck_freq / decim_ratio, sample_freq);
[Severity: High]
Can sck_freq / decim_ratio divide by zero when a caller requests
sample_freq > 2 * sck_freq?
DIV_ROUND_CLOSEST(sck_freq, sample_freq) rounds decim_ratio down to 0
before stm32_mdf_adc_set_filters_config() validates the decimation range,
and delta_ppm then evaluates to 1000000, hitting sck_freq / decim_ratio
in dev_warn(). Similarly, if requested_sample_freq is 0 and
sck_freq < MDF_DEFAULT_DECIM_RATIO, sample_freq becomes 0 and
DIV_ROUND_CLOSEST(sck_freq, sample_freq) divides by zero.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118-upstream_mdf_v2-v2-0-7939d3c3720a@foss.st.com?part=6
next prev parent reply other threads:[~2026-11-18 15:34 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 15:17 [PATCH v2 00/11] iio: adc: stm32: add mdf support for stm32mp2 Olivier Moysan
2026-11-18 15:17 ` [PATCH v2 01/11] dt-bindings: iio: adc: add stm32mp2 mdf filter Olivier Moysan
2026-11-18 15:17 ` [PATCH v2 02/11] iio: adc: stm32-mdf: add core registration Olivier Moysan
2026-11-18 15:45 ` sashiko-bot
2026-11-18 15:17 ` [PATCH v2 03/11] iio: adc: stm32-mdf: add cck clocks provider Olivier Moysan
2026-11-18 15:50 ` sashiko-bot
2026-11-18 15:17 ` [PATCH v2 04/11] iio: adc: stm32-mdf: add serial interfaces support Olivier Moysan
2026-11-18 15:42 ` sashiko-bot
2026-11-18 15:17 ` [PATCH v2 05/11] iio: adc: stm32-mdf: add digital filter device support Olivier Moysan
2026-11-18 15:40 ` sashiko-bot
2026-11-18 15:17 ` [PATCH v2 06/11] iio: adc: stm32-mdf: configure cic filters and sample rates Olivier Moysan
2026-11-18 15:34 ` sashiko-bot [this message]
2026-11-18 15:17 ` [PATCH v2 07/11] iio: adc: stm32-mdf: add direct iio conversion support Olivier Moysan
2026-11-18 15:51 ` sashiko-bot
2026-11-18 15:17 ` [PATCH v2 08/11] iio: adc: stm32-mdf: add dma buffered acquisition support Olivier Moysan
2026-11-18 15:55 ` sashiko-bot
2026-11-18 15:17 ` [PATCH v2 09/11] iio: adc: stm32-mdf: add external timer trigger support Olivier Moysan
2026-11-18 15:37 ` sashiko-bot
2026-11-18 15:17 ` [PATCH v2 10/11] iio: adc: stm32-mdf: add dmic audio filter support Olivier Moysan
2026-11-18 16:06 ` sashiko-bot
2026-11-18 15:17 ` [PATCH v2 11/11] ASoC: stm32: add mdf dai support Olivier Moysan
2026-11-18 16:08 ` sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sashiko-outbox-221345@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=dri-devel@lists.freedesktop.org \
--cc=linux-sound@vger.kernel.org \
--cc=media-ci@linuxtv.org \
--cc=olivier.moysan@foss.st.com \
--cc=robh@kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox