Re: [PATCH v3] iio: adc: stm32: add oversampling support
From: Andy Shevchenko
Date: Wed Apr 09 2025 - 03:38:16 EST
On Tue, Apr 08, 2025 at 07:30:53PM +0200, Olivier Moysan wrote:
> Add oversampling support for STM32H7, STM32MP15 & STM32MP13.
> STM32F4 ADC has no oversampling feature.
>
> The current support of the oversampling feature aims at increasing
> the data SNR, without changing the data resolution.
> As the oversampling by itself increases data resolution,
> a right shift is applied to keep initial resolution.
> Only the oversampling ratio corresponding to a power of two are
> supported here, to get a direct link between right shift and
> oversampling ratio. (2exp(n) ratio <=> n right shift)
>
> The oversampling ratio is shared by all channels, whatever channel type.
> (e.g. single ended or differential).
>
> Oversampling can be configured using IIO ABI:
> - oversampling_ratio_available
> - oversampling_ratio
...
> --- a/drivers/iio/adc/stm32-adc-core.h
> +++ b/drivers/iio/adc/stm32-adc-core.h
Does this include bitfield.h and bits.h already?
...
> +static const unsigned int stm32h7_adc_oversampling_avail[] = {
> + 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024
Leave trailing comma.
> +};
> +
> +static const unsigned int stm32mp13_adc_oversampling_avail[] = {
> + 1, 2, 4, 8, 16, 32, 64, 128, 256
As well.
> };
...
> + .oversampling = stm32h7_adc_oversampling_avail,
> .num_res = ARRAY_SIZE(stm32h7_adc_resolutions),
> + .num_ovs = ARRAY_SIZE(stm32h7_adc_oversampling_avail),
+ array_size.h
...
> +static void stm32h7_adc_set_ovs(struct iio_dev *indio_dev, u32 ovs_idx)
> +{
> + struct stm32_adc *adc = iio_priv(indio_dev);
> + u32 ovsr_bits, bits, msk;
> +
> + msk = STM32H7_ROVSE | STM32H7_OVSR_MASK | STM32H7_OVSS_MASK;
> + stm32_adc_clr_bits(adc, STM32H7_ADC_CFGR2, msk);
> +
> + if (!ovs_idx)
> + return;
> +
> + /*
> + * Only the oversampling ratios corresponding to 2*exp(ovs_idx) are exposed in sysfs.
> + * Oversampling ratios [2,3,...,1024] are mapped on OVSR register values [1,2,...,1023].
> + * OVSR = 2 exp(ovs_idx) - 1
> + * These ratio increase the resolution by ovs_idx bits. Apply a right shift to keep initial
> + * resolution given by "assigned-resolution-bits" property.
> + * OVSS = ovs_idx
> + */
> + ovsr_bits = (1 << ovs_idx) - 1;
Why not GENMASK(ovs_idx - 1, 0)?
> + bits = STM32H7_ROVSE | STM32H7_OVSS(ovs_idx) | STM32H7_OVSR(ovsr_bits);
> +
> + stm32_adc_set_bits(adc, STM32H7_ADC_CFGR2, bits & msk);
> +}
> +
> +static void stm32mp13_adc_set_ovs(struct iio_dev *indio_dev, u32 ovs_idx)
> +{
> + struct stm32_adc *adc = iio_priv(indio_dev);
> + u32 bits, msk;
> +
> + msk = STM32H7_ROVSE | STM32MP13_OVSR_MASK | STM32MP13_OVSS_MASK;
> + stm32_adc_clr_bits(adc, STM32H7_ADC_CFGR2, msk);
> +
> + if (!ovs_idx)
> + return;
> +
> + /*
> + * The oversampling ratios [2,4,8,..,256] are mapped on OVSR register values [0,1,...,7].
> + * OVSR = ovs_idx - 1
> + * These ratio increase the resolution by ovs_idx bits. Apply a right shift to keep initial
> + * resolution given by "assigned-resolution-bits" property.
> + * OVSS = ovs_idx
> + */
> + bits = STM32H7_ROVSE | STM32MP13_OVSS(ovs_idx);
> + if (ovs_idx - 1)
> + bits |= STM32MP13_OVSR(ovs_idx - 1);
> +
> + stm32_adc_set_bits(adc, STM32H7_ADC_CFGR2, bits & msk);
> +}
...
> +static int stm32_adc_write_raw(struct iio_dev *indio_dev,
> + struct iio_chan_spec const *chan,
> + int val, int val2, long mask)
> +{
> + struct stm32_adc *adc = iio_priv(indio_dev);
> + struct device *dev = indio_dev->dev.parent;
> + int nb = adc->cfg->adc_info->num_ovs;
> + u32 idx;
Why this strange type for loop iterator? Shouldn't be as simple as unsigned int?
> + int ret;
> +
> + switch (mask) {
> + case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
> + if (val2)
> + return -EINVAL;
> +
> + for (idx = 0; idx < nb; idx++)
> + if (adc->cfg->adc_info->oversampling[idx] == val)
> + break;
> +
Unneeded blank line as this two are coupled well together.
> + if (idx >= nb)
> + return -EINVAL;
> +
> + if (!iio_device_claim_direct(indio_dev))
> + return -EBUSY;
> +
> + ret = pm_runtime_resume_and_get(dev);
> + if (ret < 0)
> + goto err;
> +
> + adc->cfg->set_ovs(indio_dev, idx);
> +
> + pm_runtime_mark_last_busy(dev);
> + pm_runtime_put_autosuspend(dev);
> +
> + adc->ovs_idx = idx;
> +err:
> + iio_device_release_direct(indio_dev);
> +
> + return ret;
> + default:
> + return -EINVAL;
> + }
> +}
--
With Best Regards,
Andy Shevchenko