* [PATCH v2 00/11] iio: adc: stm32: add mdf support for stm32mp2
@ 2026-11-18 15:17 Olivier Moysan
2026-11-18 15:17 ` [PATCH v2 01/11] dt-bindings: iio: adc: add stm32mp2 mdf filter Olivier Moysan
` (10 more replies)
0 siblings, 11 replies; 22+ messages in thread
From: Olivier Moysan @ 2026-11-18 15:17 UTC (permalink / raw)
To: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko,
Rob Herring, Krzysztof Kozlowski, Conor Dooley, Maxime Coquelin,
Alexandre Torgue, Philipp Zabel, Sumit Semwal,
Christian König, Philipp Stanner, Arnaud Pouliquen,
Liam Girdwood, Mark Brown, Jaroslav Kysela, Takashi Iwai,
Fabrice Gasnier
Cc: linux-iio, devicetree, linux-stm32, linux-arm-kernel,
linux-kernel, linux-media, dri-devel, linaro-mm-sig, linux-sound,
Olivier Moysan
This series adds support for the STM32 Multi-function Digital Filter
(MDF) found on STM32MP2 SoCs. MDF converts bitstreams from external
sigma-delta modulators and PDM microphones into samples for IIO and
ASoC consumers.
The drivers support direct IIO conversions, cyclic DMA acquisition,
CIC configuration and scaling, synchronized and interleaved filters,
and external timer triggers. An ASoC DAI provides PDM microphone capture.
This revision addresses the v1 review comments listed below and splits
the driver patch into smaller functional patches for readability.
It retains the existing IIO backend interface for external sigma-delta
modulators, pending agreement on the preferred abstraction.
These changes do not resolve all v1 review comments. In particular, the
frontend/backend architecture, DT child-node model and complete clock
topology remain open discussion points.
* Open questions: MDF topology (backend/frontend)
In Analog Devices use of the IIO backend framework, the AXI ADC is a
hardware backend, while the AD9467 is a hardware frontend. The external
sigma-delta modulator is actually a hardware frontend in the MDF signal
chain. The difference from the AD9467 use case is which driver owns the
aggregate IIO device.
Option 1:
One option would be to generalize the existing backend abstraction to
represent components contributing to an aggregate IIO device,
independently of their position in the signal chain.
The existing framework appears to provide the required mechanisms. None
of these mechanisms inherently requires the provider to be a hardware backend.
A generic component/provider model would support both topologies without
duplicating interfaces or requiring reciprocal dependencies. The key
point here is to preserve the compatibility of APIs and DT bindings.
Option 2:
Alternatively, if the backend abstraction should retain its hardware
meaning, we could introduce an explicit frontend-provider abstraction
sharing the existing infrastructure. Frontend operations would cover
analog-side control and conversion parameters.
Would generalizing the existing abstraction be acceptable, or would a
separate frontend interface built on shared infrastructure be preferred?
Option 3:
An aggregate IIO device based on the component framework, as discussed
in previous threads, could also be considered. I currently regard this
as a fallback solution:
https://lore.kernel.org/all/ec411432-4b77-4410-b0aa-b29f73b82c84@foss.st.com/
* Driver implementation
Serial interfaces (SITF) are shared resources managed directly by the
MDF core and no longer have separate platform devices. This revision
retains their DT nodes to describe configuration and clock connections,
and to allow filters to reference their input bitstreams without
duplicating shared settings.
This revision also retains a platform device for each MDF filter, as
each filter manages its own registers, interrupt, DMA channel and IIO
acquisition state, with a separate suspend/resume lifecycle.
* Clock management
Several clock-management review comments are addressed in this revision:
- The provider-side clock-frequency property has been removed. Consumers
request CCK rates through assigned-clocks and assigned-clock-rates.
- The clock-names property has been removed. The core acquires its single
input clock by index, and CCK gating uses regmap-based clock operations.
- Remove the unused clock-ranges property, as the core now explicitly
shares the kernel clock with the serial interfaces and filters instead
of relying on parent clock lookup.
Clock topology point remains open, awaiting clarification of the MDF topology.
Changes since v1:
- Split the large IIO driver patch into smaller functional patches.
- Replace provider-side clock-frequency with consumer-assigned CCK rates
through the common clock framework.
- Remove the platform devices for serial interfaces.
- Replace the interleave phandle list with a filter count, covering
consecutive filters starting at filter 0.
- Simplify clock-output-names, remove clock-names, reset-names and
filter-level clock properties, and move mdf-dai into an explicit
property outside the conditional schema.
- Rename the binding to match the STM32MP25 compatible, sort compatible
entries, and simplify the binding patch subject.
- Use unsigned long for match data, improve probe error reporting and
wrapping, remove a probe-success debug message, and add kernel-doc for
exported core functions.
- Use scoped spinlock guards.
- Remove empty serial-interface runtime-PM callbacks.
- Remove the redundant ASoC platform module alias.
Additional fixes since v1 (following Sashiko review):
- Compute the settling-time discard count with 64-bit arithmetic.
- Size cyclic DMA periods in complete scans and validate buffer and
period alignment before starting DMA.
- Fix backend-enable rollback so backend zero is disabled as well.
- Use managed allocation for the conversion buffer and managed IIO
triggered-buffer setup.
- Release temporary synchronization and serial-interface firmware-node
references. Replacing interleave phandles also removes the associated
stored node references.
- Avoid unregistering secondary interleaved filters that were never
registered as IIO devices.
- Use managed acquisition of the optional serial clock.
- Replace the GCD-based CCK divider selection with a search for the
largest valid divisor, allowing non-power-of-two divider values.
- Remove the selection of the undefined COMMON_CLK_REGMAP symbol.
Previous discussion:
https://lore.kernel.org/all/20261001145702.2628429-1-olivier.moysan@foss.st.com/
Signed-off-by: Olivier Moysan <olivier.moysan@foss.st.com>
---
Olivier Moysan (11):
dt-bindings: iio: adc: add stm32mp2 mdf filter
iio: adc: stm32-mdf: add core registration
iio: adc: stm32-mdf: add cck clocks provider
iio: adc: stm32-mdf: add serial interfaces support
iio: adc: stm32-mdf: add digital filter device support
iio: adc: stm32-mdf: configure cic filters and sample rates
iio: adc: stm32-mdf: add direct iio conversion support
iio: adc: stm32-mdf: add dma buffered acquisition support
iio: adc: stm32-mdf: add external timer trigger support
iio: adc: stm32-mdf: add dmic audio filter support
ASoC: stm32: add mdf dai support
.../bindings/iio/adc/st,stm32mp25-mdf.yaml | 341 ++++
drivers/iio/adc/Kconfig | 29 +
drivers/iio/adc/Makefile | 3 +
drivers/iio/adc/stm32-mdf-adc.c | 2080 ++++++++++++++++++++
drivers/iio/adc/stm32-mdf-core.c | 899 +++++++++
drivers/iio/adc/stm32-mdf-serial.c | 223 +++
drivers/iio/adc/stm32-mdf.h | 275 +++
include/linux/iio/adc/stm32-mdf-adc.h | 19 +
sound/soc/stm/Kconfig | 15 +
sound/soc/stm/Makefile | 3 +
sound/soc/stm/stm32_amdf.c | 375 ++++
11 files changed, 4262 insertions(+)
---
base-commit: d922ed738439e1d5b4701df7f4cdd49a09925624
change-id: 20261112-upstream_mdf_v2-76040d99138e
Best regards,
--
Olivier Moysan <olivier.moysan@foss.st.com>
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH v2 01/11] dt-bindings: iio: adc: add stm32mp2 mdf filter
2026-11-18 15:17 [PATCH v2 00/11] iio: adc: stm32: add mdf support for stm32mp2 Olivier Moysan
@ 2026-11-18 15:17 ` Olivier Moysan
2026-11-18 15:17 ` [PATCH v2 02/11] iio: adc: stm32-mdf: add core registration Olivier Moysan
` (9 subsequent siblings)
10 siblings, 0 replies; 22+ messages in thread
From: Olivier Moysan @ 2026-11-18 15:17 UTC (permalink / raw)
To: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko,
Rob Herring, Krzysztof Kozlowski, Conor Dooley, Maxime Coquelin,
Alexandre Torgue, Philipp Zabel, Sumit Semwal,
Christian König, Philipp Stanner, Arnaud Pouliquen,
Liam Girdwood, Mark Brown, Jaroslav Kysela, Takashi Iwai,
Fabrice Gasnier
Cc: linux-iio, devicetree, linux-stm32, linux-arm-kernel,
linux-kernel, linux-media, dri-devel, linaro-mm-sig, linux-sound,
Olivier Moysan
Add bindings that describes STM32 MDF settings to support
digital filtering for Pulse Density Modulation (PDM) microphones
and analog sigma delta modulators, on STM32MP2 SoCs.
Signed-off-by: Olivier Moysan <olivier.moysan@foss.st.com>
---
.../bindings/iio/adc/st,stm32mp25-mdf.yaml | 341 +++++++++++++++++++++
1 file changed, 341 insertions(+)
diff --git a/Documentation/devicetree/bindings/iio/adc/st,stm32mp25-mdf.yaml b/Documentation/devicetree/bindings/iio/adc/st,stm32mp25-mdf.yaml
new file mode 100644
index 000000000000..e4925a38012d
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/adc/st,stm32mp25-mdf.yaml
@@ -0,0 +1,341 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/adc/st,stm32mp25-mdf.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: STMicroelectronics STM32 Multi-function Digital Filter (MDF) ADC
+
+maintainers:
+ - Olivier Moysan <olivier.moysan@foss.st.com>
+
+description: |
+ STM32 MDF ADC is a sigma delta analog-to-digital converter dedicated to
+ interface external sigma delta modulators to STM32 micro controllers.
+
+properties:
+ compatible:
+ enum:
+ - st,stm32mp23-mdf
+ - st,stm32mp25-mdf
+
+ reg:
+ minItems: 1
+ maxItems: 2
+
+ clocks:
+ maxItems: 1
+
+ "#clock-cells":
+ enum: [0, 1]
+
+ clock-output-names:
+ description: |
+ CCK0 and CCK1 are optional output clocks, which share the same clock frequency,
+ but can be gated independently to save power.
+ minItems: 1
+ items:
+ - enum: [cck0, cck1]
+ - const: cck1
+
+ "#address-cells":
+ const: 1
+
+ "#size-cells":
+ const: 1
+
+ ranges: true
+
+ resets:
+ maxItems: 1
+
+ access-controllers:
+ maxItems: 1
+
+ power-domains:
+ maxItems: 1
+
+ st,interleave:
+ description: |
+ Number of interleaved filters, starting from filter 0. For a value N,
+ filters 0 through N - 1 participate in interleaving. Their samples are
+ multiplexed into a single channel and retrieved through the device
+ associated with filter 0. Filters 1 through N - 1 must be enabled and
+ inherit their configuration from filter 0. If absent, interleaving is
+ disabled.
+ $ref: /schemas/types.yaml#/definitions/uint32
+ minimum: 2
+ maximum: 8
+
+required:
+ - compatible
+ - reg
+ - ranges
+ - clocks
+ - "#address-cells"
+ - "#size-cells"
+
+additionalProperties: false
+
+patternProperties:
+ "^sitf@[0-9]+$":
+ type: object
+ description: |
+ Serial interface configuration, managed by the MDF core. Pin control
+ states for the serial interface pins belong to the MDF parent node.
+
+ properties:
+ reg:
+ description: Specify the SITF serial interface instance
+ maxItems: 1
+
+ clocks:
+ description: |
+ Serial interface clock (optional depending on interface mode)
+ maxItems: 1
+
+ st,sitf-mode:
+ description: |
+ Select serial interface protocol
+ - spi: SPI mode
+ - lf_spi: low frequency SPI mode for low power applications
+ $ref: /schemas/types.yaml#/definitions/string
+ enum:
+ - spi
+ - lf_spi
+
+ required:
+ - reg
+ - st,sitf-mode
+
+ additionalProperties: false
+
+ "^filter@[0-9]+$":
+ type: object
+ description: Digital filter path child node
+
+ properties:
+ compatible:
+ enum:
+ - st,stm32mp25-mdf-dmic
+ - st,stm32mp25-mdf-adc
+
+ reg:
+ description: Specify the MDF filter instance
+ maxItems: 1
+
+ interrupts:
+ maxItems: 1
+
+ dmas:
+ maxItems: 1
+
+ dma-names:
+ items:
+ - const: rx
+
+ "#io-channel-cells":
+ const: 1
+
+ '#address-cells':
+ const: 1
+
+ '#size-cells':
+ const: 0
+
+ st,cic-mode:
+ description: |
+ Cascaded-integrator-comb (CIC) filter configuration
+ - 0: MCIC & ACIC filters in FastSinc mode
+ - [1-3]: MCIC & ACIC filters in Sinc mode order 1 to 3
+ - [4-5]: Single CIC filter in Sinc mode order 4 to 5
+ For audio purpose it is recommended to use CIC Sinc4 or Sinc5
+ This property is mandatory for filter 0 or filters not used in interleave mode.
+ $ref: /schemas/types.yaml#/definitions/uint32
+ minimum: 0
+ maximum: 5
+
+ st,delay:
+ description: Filter delay in samples
+ $ref: /schemas/types.yaml#/definitions/uint32
+ maximum: 127
+
+ st,rs-filter-bypass:
+ description: Bypass RSFLT reshaping filter.
+ $ref: /schemas/types.yaml#/definitions/flag
+
+ st,hpf-filter-cutoff-bp:
+ description: |
+ High Pass Filter (HPF) cut-off frequency expressed as a fraction of the PCM sampling rate.
+ Cut-off frequency = st,hpf-filter-cutoff-bp x Fpcm / 10000.
+ If this property is not defined the HPF is disabled.
+ enum: [625, 1250, 2500, 9500]
+
+ st,sync:
+ description:
+ Synchronize to another filter.
+ Must contain the phandle of the filter providing the synchronization.
+ allOf:
+ - $ref: /schemas/types.yaml#/definitions/phandle-array
+ - maxItems: 1
+
+ st,sitf:
+ $ref: /schemas/types.yaml#/definitions/phandle-array
+ items:
+ - items:
+ - description: Phandle of the serial interface connected to the digital filter
+ - description: |
+ The phandle's argument selects the bitstream on the falling or rising edge
+ of the serial interface clock:
+ - 0: rising edge
+ - 1: falling edge
+ enum: [0, 1]
+ default: 0
+ description:
+ Should be phandle/bitstream pair.
+
+ mdf-dai:
+ type: object
+ description: Audio interface for the MDF filter
+
+ properties:
+ compatible:
+ const: st,stm32mp25-mdf-dai
+
+ "#sound-dai-cells":
+ const: 0
+
+ io-channels:
+ description:
+ From common IIO binding. Used to pipe external sigma delta
+ modulator or internal ADC output to MDF channel.
+
+ power-domains:
+ maxItems: 1
+
+ port:
+ $ref: /schemas/sound/audio-graph-port.yaml#
+ unevaluatedProperties: false
+
+ required:
+ - compatible
+ - "#sound-dai-cells"
+ - io-channels
+
+ additionalProperties: false
+
+ required:
+ - compatible
+ - reg
+ - interrupts
+ - dmas
+ - dma-names
+ - "#io-channel-cells"
+ - "#address-cells"
+ - "#size-cells"
+ - st,sitf
+
+ patternProperties:
+ "^channel@([0-7])$":
+ type: object
+ $ref: adc.yaml
+ description: Represents the external channel which is connected to the MDF.
+
+ properties:
+ reg:
+ maximum: 7
+
+ io-backends:
+ description:
+ Used to pipe external sigma delta modulator or internal ADC backend to MDF
+ channel.
+ maxItems: 1
+
+ required:
+ - reg
+
+ unevaluatedProperties: false
+
+ allOf:
+ - if:
+ properties:
+ compatible:
+ contains:
+ const: st,stm32mp25-mdf-adc
+
+ then:
+ patternProperties:
+ "^channel@[0-7]$":
+ required:
+ - io-backends
+ properties:
+ mdf-dai: false
+
+ unevaluatedProperties: false
+
+examples:
+ - |
+ #include <dt-bindings/clock/st,stm32mp25-rcc.h>
+ #include <dt-bindings/interrupt-controller/arm-gic.h>
+ mdf1: filter@504d0000 {
+ compatible = "st,stm32mp25-mdf";
+ ranges = <0 0x504d0000 0x1000>;
+ reg = <0x504d0000 0x8>, <0x504d0ff0 0x10>;
+ #address-cells = <1>;
+ #size-cells = <1>;
+ clocks = <&rcc CK_KER_MDF1>;
+ #clock-cells = <1>;
+ clock-output-names = "cck0", "cck1";
+
+ sitf5: sitf@300 {
+ reg = <0x300 0x4>;
+ st,sitf-mode = "spi";
+ clocks = <&mdf1 0>;
+ assigned-clocks = <&mdf1 0>;
+ assigned-clock-rates = <2048000>;
+ };
+
+ filter0: filter@84 {
+ compatible = "st,stm32mp25-mdf-dmic";
+ reg = <0x84 0x70>;
+ #io-channel-cells = <1>;
+ interrupts = <GIC_SPI 184 IRQ_TYPE_LEVEL_HIGH>;
+ dmas = <&hpdma 63 0x63 0x12 0>;
+ dma-names = "rx";
+ st,cic-mode = <5>;
+ st,sitf = <&sitf5 0>;
+ #address-cells = <1>;
+ #size-cells = <0>;
+
+ channel@0 {
+ reg = <0>;
+ };
+
+ mdf-dai {
+ compatible = "st,stm32mp25-mdf-dai";
+ #sound-dai-cells = <0>;
+ io-channels = <&filter0 0>;
+ };
+ };
+
+ filter@104 {
+ compatible = "st,stm32mp25-mdf-adc";
+ reg = <0x104 0x70>;
+ #io-channel-cells = <1>;
+ interrupts = <GIC_SPI 185 IRQ_TYPE_LEVEL_HIGH>;
+ dmas = <&hpdma 64 0x63 0x12 0>;
+ dma-names = "rx";
+ st,cic-mode = <2>;
+ st,sitf = <&sitf5 1>;
+ #address-cells = <1>;
+ #size-cells = <0>;
+
+ channel@1 {
+ reg = <1>;
+ settling-time-us = <1000>;
+ io-backends = <&sd_adc1>;
+ };
+ };
+ };
+
+...
--
2.43.0
^ permalink raw reply related [flat|nested] 22+ messages in thread
* [PATCH v2 02/11] iio: adc: stm32-mdf: add core registration
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 ` 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
` (8 subsequent siblings)
10 siblings, 1 reply; 22+ messages in thread
From: Olivier Moysan @ 2026-11-18 15:17 UTC (permalink / raw)
To: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko,
Rob Herring, Krzysztof Kozlowski, Conor Dooley, Maxime Coquelin,
Alexandre Torgue, Philipp Zabel, Sumit Semwal,
Christian König, Philipp Stanner, Arnaud Pouliquen,
Liam Girdwood, Mark Brown, Jaroslav Kysela, Takashi Iwai,
Fabrice Gasnier
Cc: linux-iio, devicetree, linux-stm32, linux-arm-kernel,
linux-kernel, linux-media, dri-devel, linaro-mm-sig, linux-sound,
Olivier Moysan
The STM32 multifunction digital filter (MDF) shares its register bank,
kernel clock and power resources between serial interfaces and digital
filters. Add a core driver to manage these common resources on STM32MP2.
Check the number of filter actually supported from identification
registers.
Signed-off-by: Olivier Moysan <olivier.moysan@foss.st.com>
---
drivers/iio/adc/Kconfig | 13 ++
drivers/iio/adc/Makefile | 2 +
drivers/iio/adc/stm32-mdf-core.c | 471 +++++++++++++++++++++++++++++++++++++++
drivers/iio/adc/stm32-mdf.h | 221 ++++++++++++++++++
4 files changed, 707 insertions(+)
diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
index 1d6ed49041ab..6bd46c79d6d6 100644
--- a/drivers/iio/adc/Kconfig
+++ b/drivers/iio/adc/Kconfig
@@ -1740,6 +1740,19 @@ config STM32_DFSDM_ADC
This driver can also be built as a module. If so, the module
will be called stm32-dfsdm-adc.
+config STM32_MDF_CORE
+ tristate "STMicroelectronics STM32 MDF core"
+ depends on (ARCH_STM32 && OF) || COMPILE_TEST
+ depends on COMMON_CLK
+ select REGMAP
+ select REGMAP_MMIO
+ help
+ Select this option to enable the driver for STMicroelectronics
+ STM32 Multi-function Digital Filter for sigma delta converter.
+
+ This driver can also be built as a module. If so, the module
+ will be called stm32-mdf.
+
config STMPE_ADC
tristate "STMicroelectronics STMPE ADC driver"
depends on OF && MFD_STMPE
diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile
index 769965295dcc..d5fc809c33ca 100644
--- a/drivers/iio/adc/Makefile
+++ b/drivers/iio/adc/Makefile
@@ -146,6 +146,8 @@ obj-$(CONFIG_STM32_ADC_CORE) += stm32-adc-core.o
obj-$(CONFIG_STM32_ADC) += stm32-adc.o
obj-$(CONFIG_STM32_DFSDM_ADC) += stm32-dfsdm-adc.o
obj-$(CONFIG_STM32_DFSDM_CORE) += stm32-dfsdm-core.o
+obj-$(CONFIG_STM32_MDF_CORE) += stm32-mdf.o
+stm32-mdf-y := stm32-mdf-core.o
obj-$(CONFIG_STMPE_ADC) += stmpe-adc.o
obj-$(CONFIG_SUN20I_GPADC) += sun20i-gpadc-iio.o
obj-$(CONFIG_SUN4I_GPADC) += sun4i-gpadc-iio.o
diff --git a/drivers/iio/adc/stm32-mdf-core.c b/drivers/iio/adc/stm32-mdf-core.c
new file mode 100644
index 000000000000..ca50ce566a43
--- /dev/null
+++ b/drivers/iio/adc/stm32-mdf-core.c
@@ -0,0 +1,471 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * This file is part of STM32 MDF driver
+ *
+ * Copyright (C) 2023-2026, STMicroelectronics - All Rights Reserved
+ * Author: Olivier Moysan <olivier.moysan@foss.st.com>.
+ */
+
+#include <linux/cleanup.h>
+#include <linux/clk.h>
+#include <linux/device.h>
+#include <linux/list.h>
+#include <linux/module.h>
+#include <linux/of_address.h>
+#include <linux/of_device.h>
+#include <linux/of_platform.h>
+#include <linux/pinctrl/consumer.h>
+#include <linux/pm_runtime.h>
+#include <linux/platform_device.h>
+#include <linux/regmap.h>
+#include <linux/reset.h>
+
+#include "stm32-mdf.h"
+
+static bool stm32_mdf_core_readable_reg(struct device *dev, unsigned int reg)
+{
+ switch (reg) {
+ case MDF_GCR_REG:
+ case MDF_CKGCR_REG:
+ case MDF_HWCFGR_REG:
+ case MDF_VERR_REG:
+ case MDF_IPIDR_REG:
+ case MDF_SIDR_REG:
+ return true;
+ default:
+ return false;
+ }
+}
+
+static bool stm32_mdf_core_volatile_reg(struct device *dev, unsigned int reg)
+{
+ /*
+ * In MDF_CKGCR_REG register only CKGACTIVE bit is volatile. MDF_CKGCR_REG is not marked
+ * as volatible to ease the suspend/resume case, and benefit from the regcache API.
+ * Access to CKGACTIVE bit is managed specifically instead.
+ */
+ return false;
+}
+
+static bool stm32_mdf_core_writeable_reg(struct device *dev, unsigned int reg)
+{
+ switch (reg) {
+ case MDF_GCR_REG:
+ case MDF_CKGCR_REG:
+ return true;
+ default:
+ return false;
+ }
+}
+
+static const struct regmap_config stm32_mdf_regmap_cfg = {
+ .reg_bits = 32,
+ .val_bits = 32,
+ .reg_stride = sizeof(u32),
+ .max_register = MDF_SIDR_REG,
+ .readable_reg = stm32_mdf_core_readable_reg,
+ .volatile_reg = stm32_mdf_core_volatile_reg,
+ .writeable_reg = stm32_mdf_core_writeable_reg,
+ .num_reg_defaults_raw = MDF_SIDR_REG / sizeof(u32) + 1,
+ .fast_io = true,
+ .cache_type = REGCACHE_FLAT,
+};
+
+/*
+ * struct stm32_mdf_priv - STM32 MDF private data
+ * @mdf: mdf common data
+ * @pdev: platform device pointer
+ * @regmap: regmap for register read/write
+ * @kclk: mdf kernel clock handle
+ * @base: mdf registers base cpu address
+ * @phys_base: mdf registers base physical address
+ * @n_active_ch: number of active channels
+ * @lock: lock to manage common resources
+ * @kclk_rate: save kclk rate at probe
+ * @exclusive_flg: exclusive rate request flag
+ */
+struct stm32_mdf_priv {
+ struct stm32_mdf mdf;
+ struct platform_device *pdev;
+ struct regmap *regmap;
+ struct clk *kclk;
+ void __iomem *base;
+ phys_addr_t phys_base;
+ atomic_t n_active_ch;
+ spinlock_t lock; /* Manage common resources race conditions */
+ unsigned long kclk_rate;
+ bool exclusive_flg;
+};
+
+#define STM32_MDF_MP23_FILTER_NB 4
+
+static inline struct stm32_mdf_priv *to_stm32_mdf_priv(struct stm32_mdf *mdf)
+{
+ return container_of(mdf, struct stm32_mdf_priv, mdf);
+}
+
+/**
+ * stm32_mdf_core_start_mdf() - Start the MDF clock generator
+ * @mdf: MDF instance
+ *
+ * Enable the MDF clocks on first active channel. The kernel clock
+ * rate is made exclusive while channels are active.
+ *
+ * Return: 0 on success, or a negative error code on failure.
+ */
+int stm32_mdf_core_start_mdf(struct stm32_mdf *mdf)
+{
+ struct stm32_mdf_priv *priv = to_stm32_mdf_priv(mdf);
+ struct device *dev = &priv->pdev->dev;
+ int ret;
+ u32 val;
+
+ if (atomic_inc_return(&priv->n_active_ch) == 1) {
+ ret = pm_runtime_resume_and_get(dev);
+ if (ret < 0)
+ goto err;
+
+ /* Enable PROCDIV and CCKDIV clock dividers */
+ ret = regmap_set_bits(priv->regmap, MDF_CKGCR_REG, MDF_CKG_CKGDEN);
+ if (ret < 0)
+ goto pm_put;
+
+ /* Check clock status. Bypass cache to access volatile CKGACTIVE active bit */
+ regcache_cache_bypass(priv->regmap, true);
+ regmap_read(priv->regmap, MDF_CKGCR_REG, &val);
+ regcache_cache_bypass(priv->regmap, false);
+ if (!(val & MDF_CKG_ACTIVE)) {
+ ret = -EINVAL;
+ dev_err(dev, "MDF clock not active\n");
+ goto pm_put;
+ }
+
+ if (!priv->exclusive_flg) {
+ clk_rate_exclusive_get(priv->kclk);
+ priv->exclusive_flg = true;
+ }
+ }
+
+ return 0;
+
+pm_put:
+ pm_runtime_put_sync(dev);
+err:
+ atomic_dec(&priv->n_active_ch);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(stm32_mdf_core_start_mdf);
+
+/**
+ * stm32_mdf_core_trigger() - Trigger MDF filters
+ * @mdf: MDF instance
+ *
+ * Trigger MDF filters via TRGO trigger.
+ *
+ * Return: 0 on success, or a negative error code on failure.
+ */
+int stm32_mdf_core_trigger(struct stm32_mdf *mdf)
+{
+ struct stm32_mdf_priv *priv = to_stm32_mdf_priv(mdf);
+ int ret;
+ u32 val;
+
+ guard(spinlock)(&priv->lock);
+
+ /* Bypass cache to access TRGO volatile bit */
+ regcache_cache_bypass(priv->regmap, true);
+
+ ret = regmap_read(priv->regmap, MDF_GCR_REG, &val);
+ if (ret < 0)
+ goto err;
+
+ if (val & MDF_GCR_TRGO) {
+ ret = -EBUSY;
+ goto err;
+ }
+
+ ret = regmap_set_bits(priv->regmap, MDF_GCR_REG, MDF_GCR_TRGO);
+
+err:
+ regcache_cache_bypass(priv->regmap, false);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(stm32_mdf_core_trigger);
+
+/**
+ * stm32_mdf_core_stop_mdf() - Stop an MDF channel
+ * @mdf: MDF instance
+ *
+ * Drop one active-channel reference. When the last channel stops, release the
+ * exclusive kernel clock rate and disable the MDF clock.
+ *
+ * Return: 0 on success, or a negative error code on failure.
+ */
+int stm32_mdf_core_stop_mdf(struct stm32_mdf *mdf)
+{
+ struct stm32_mdf_priv *priv = to_stm32_mdf_priv(mdf);
+ int ret = 0;
+
+ if (atomic_dec_and_test(&priv->n_active_ch)) {
+ if (priv->exclusive_flg) {
+ clk_rate_exclusive_put(priv->kclk);
+ priv->exclusive_flg = false;
+ }
+
+ ret = regmap_clear_bits(priv->regmap, MDF_CKGCR_REG, MDF_CKG_CKGDEN);
+
+ pm_runtime_put_sync(&priv->pdev->dev);
+ }
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(stm32_mdf_core_stop_mdf);
+
+static int stm32_mdf_core_parse_clocks(struct platform_device *pdev, struct stm32_mdf_priv *priv)
+{
+ priv->kclk_rate = clk_get_rate(priv->kclk);
+
+ return 0;
+}
+
+static int stm32_mdf_core_parse_of(struct platform_device *pdev, struct stm32_mdf_priv *priv)
+{
+ struct device_node *node = pdev->dev.of_node;
+ struct reset_control *rst;
+ struct device *dev = &pdev->dev;
+ u32 count;
+ int ret;
+
+ if (!node)
+ return -EINVAL;
+
+ rst = devm_reset_control_get_optional_exclusive(&pdev->dev, NULL);
+ if (IS_ERR(rst))
+ return dev_err_probe(dev, PTR_ERR(rst), "Failed to get reset controller\n");
+
+ ret = reset_control_reset(rst);
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to reset MDF\n");
+
+ ret = stm32_mdf_core_parse_clocks(pdev, priv);
+ if (ret < 0)
+ return ret;
+
+ if (device_property_present(&pdev->dev, "st,interleave")) {
+ ret = device_property_read_u32(dev, "st,interleave", &count);
+ if (ret)
+ return dev_err_probe(dev, ret,
+ "Failed to read interleaved filter count\n");
+
+ if (count < 2 || count > 8 || count > priv->mdf.nbf)
+ return dev_err_probe(dev, -EINVAL,
+ "Invalid interleaved filter count [%u]\n", count);
+
+ priv->mdf.nb_interleave = count;
+
+ /* Configure GCR */
+ ret = regmap_update_bits(priv->regmap, MDF_GCR_REG,
+ MDF_GCR_ILVNB_MASK,
+ FIELD_PREP(MDF_GCR_ILVNB_MASK, count - 1));
+ }
+
+ return ret;
+}
+
+static const struct of_device_id stm32_mdf_of_match[] = {
+ { .compatible = "st,stm32mp23-mdf", .data = (void *)STM32_MDF_MP23_FILTER_NB },
+ { .compatible = "st,stm32mp25-mdf" },
+ {}
+};
+MODULE_DEVICE_TABLE(of, stm32_mdf_of_match);
+
+static int stm32_mdf_core_identification(struct platform_device *pdev, struct stm32_mdf_priv *priv)
+{
+ struct stm32_mdf *mdf = &priv->mdf;
+ u32 val;
+ int ret;
+
+ /* If filter number is explicitly defined, don't check identification registers */
+ mdf->nbf = ((unsigned long)device_get_match_data(&pdev->dev));
+ if (mdf->nbf)
+ return 0;
+
+ ret = regmap_read(priv->regmap, MDF_IPIDR_REG, &val);
+ if (ret)
+ return ret;
+
+ if (val == STM32MP25_MDF_IPIDR_NUMBER) {
+ ret = regmap_read(priv->regmap, MDF_HWCFGR_REG, &val);
+ if (ret)
+ return ret;
+
+ mdf->nbf = FIELD_GET(MDF_HWCFGR_NBF_MASK, val);
+
+ ret = regmap_read(priv->regmap, MDF_VERR_REG, &val);
+
+ dev_dbg(&pdev->dev, "MDF version: %lu.%lu\n", FIELD_GET(MDF_VERR_MAJREV_MASK, val),
+ FIELD_GET(MDF_VERR_MINREV_MASK, val));
+ } else {
+ dev_err(&pdev->dev, "Unexpected ID number: 0x%x\n", val);
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+static int stm32_mdf_core_probe(struct platform_device *pdev)
+{
+ struct stm32_mdf_priv *priv;
+ struct resource *res;
+ int ret;
+
+ priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
+ if (!priv)
+ return -ENOMEM;
+ priv->pdev = pdev;
+ spin_lock_init(&priv->lock);
+
+ priv->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
+ if (IS_ERR(priv->base))
+ return PTR_ERR(priv->base);
+ priv->phys_base = res->start;
+
+ priv->kclk = devm_clk_get(&pdev->dev, NULL);
+ if (IS_ERR(priv->kclk))
+ return dev_err_probe(&pdev->dev, PTR_ERR(priv->kclk),
+ "Failed to get kernel clock\n");
+ priv->mdf.kclk = priv->kclk;
+
+ priv->regmap = stm32_mdf_regmap_init(&pdev->dev, priv->kclk, priv->base,
+ &stm32_mdf_regmap_cfg);
+ if (IS_ERR(priv->regmap))
+ return dev_err_probe(&pdev->dev, PTR_ERR(priv->regmap),
+ "Regmap init error\n");
+
+ ret = stm32_mdf_core_identification(pdev, priv);
+ if (ret < 0)
+ return ret;
+
+ ret = stm32_mdf_core_parse_of(pdev, priv);
+ if (ret < 0)
+ return ret;
+
+ INIT_LIST_HEAD(&priv->mdf.filter_list);
+
+ platform_set_drvdata(pdev, priv);
+
+ pm_runtime_get_noresume(&pdev->dev);
+ pm_runtime_set_active(&pdev->dev);
+ pm_runtime_enable(&pdev->dev);
+
+ ret = of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
+ if (ret)
+ goto pm_put;
+
+ pm_runtime_put(&pdev->dev);
+
+ return 0;
+
+pm_put:
+ pm_runtime_disable(&pdev->dev);
+ pm_runtime_set_suspended(&pdev->dev);
+ pm_runtime_put_noidle(&pdev->dev);
+
+ return ret;
+}
+
+static void stm32_mdf_core_remove(struct platform_device *pdev)
+{
+ pm_runtime_get_sync(&pdev->dev);
+ of_platform_depopulate(&pdev->dev);
+ pm_runtime_disable(&pdev->dev);
+ pm_runtime_set_suspended(&pdev->dev);
+ pm_runtime_put_noidle(&pdev->dev);
+}
+
+static int stm32_mdf_core_suspend(struct device *dev)
+{
+ struct stm32_mdf *mdf = dev_get_drvdata(dev);
+ struct stm32_mdf_priv *priv = to_stm32_mdf_priv(mdf);
+ int ret;
+
+ ret = pm_runtime_force_suspend(dev);
+ if (ret)
+ return ret;
+
+ regcache_cache_only(priv->regmap, true);
+ regcache_mark_dirty(priv->regmap);
+
+ /* Balance regmap_mmio_attach_clk() clk_prepare() */
+ clk_unprepare(priv->kclk);
+
+ return pinctrl_pm_select_sleep_state(dev);
+}
+
+static int stm32_mdf_core_resume(struct device *dev)
+{
+ struct stm32_mdf *mdf = dev_get_drvdata(dev);
+ struct stm32_mdf_priv *priv = to_stm32_mdf_priv(mdf);
+ int ret;
+
+ ret = pinctrl_pm_select_default_state(dev);
+ if (ret) {
+ dev_err(dev, "Failed to set pins default state: %d\n", ret);
+ return ret;
+ }
+
+ ret = clk_prepare(priv->kclk);
+ if (ret) {
+ dev_err(dev, "Failed to prepare kernel clock: %d\n", ret);
+ goto err_clk;
+ }
+
+ regcache_cache_only(priv->regmap, false);
+ ret = regcache_sync(priv->regmap);
+ if (ret) {
+ dev_err(dev, "Failed to sync cache: %d\n", ret);
+ goto err_cache;
+ }
+
+ return pm_runtime_force_resume(dev);
+
+err_cache:
+ clk_unprepare(priv->kclk);
+err_clk:
+ pinctrl_pm_select_sleep_state(dev);
+
+ return ret;
+}
+
+static int stm32_mdf_core_runtime_suspend(struct device *dev)
+{
+ return 0;
+}
+
+static int stm32_mdf_core_runtime_resume(struct device *dev)
+{
+ return 0;
+}
+
+static const struct dev_pm_ops stm32_mdf_core_pm_ops = {
+ SET_SYSTEM_SLEEP_PM_OPS(stm32_mdf_core_suspend, stm32_mdf_core_resume)
+ SET_RUNTIME_PM_OPS(stm32_mdf_core_runtime_suspend, stm32_mdf_core_runtime_resume, NULL)
+};
+
+static struct platform_driver stm32_mdf_driver = {
+ .probe = stm32_mdf_core_probe,
+ .remove = stm32_mdf_core_remove,
+ .driver = {
+ .name = "stm32-mdf",
+ .of_match_table = stm32_mdf_of_match,
+ .pm = &stm32_mdf_core_pm_ops,
+ },
+};
+
+module_platform_driver(stm32_mdf_driver);
+
+MODULE_AUTHOR("Olivier Moysan <olivier.moysan@foss.st.com>");
+MODULE_DESCRIPTION("STMicroelectronics STM32 MDF driver");
+MODULE_LICENSE("GPL");
diff --git a/drivers/iio/adc/stm32-mdf.h b/drivers/iio/adc/stm32-mdf.h
new file mode 100644
index 000000000000..b334769f8b17
--- /dev/null
+++ b/drivers/iio/adc/stm32-mdf.h
@@ -0,0 +1,221 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * This file is part of STM32 MDF driver
+ *
+ * Copyright (C) 2023, STMicroelectronics - All Rights Reserved
+ * Author: Olivier Moysan <olivier.moysan@foss.st.com>.
+ */
+
+#include <linux/bitfield.h>
+#include <linux/clk.h>
+#include <linux/err.h>
+#include <linux/regmap.h>
+
+/* Register definitions */
+#define MDF_GCR_REG 0x00
+#define MDF_CKGCR_REG 0x04
+#define MDF_HWCFGR_REG 0xff0
+#define MDF_VERR_REG 0xff4
+#define MDF_IPIDR_REG 0xff8
+#define MDF_SIDR_REG 0xffc
+
+/* Registers offsets relative to filter base address */
+#define MDF_BSMXCR_REG 0x00 /* 0x84 * x + offset */
+#define MDF_DFLTCR_REG 0x04
+#define MDF_DFLTCICR_REG 0x08
+#define MDF_DFLTRSFR_REG 0x0c
+#define MDF_DFLTINTR_REG 0x10
+#define MDF_OLDCR_REG 0x14
+#define MDF_OLDTHLR_REG 0x18
+#define MDF_OLDTHHR_REG 0x1c
+#define MDF_DLYCR_REG 0x20
+#define MDF_SCDCR_REG 0x24
+#define MDF_DFLTIER_REG 0x28
+#define MDF_DFLTISR_REG 0x2C
+#define MDF_OECCR_REG 0x30
+#define MDF_SNPSDR 0x64
+#define MDF_DFLTDR_REG 0x6c
+
+/* MDF_GCR: Global Control Register */
+#define MDF_GCR_TRGO BIT(0)
+#define MDF_GCR_ILVNB_MASK GENMASK(7, 4)
+
+/* MDF_CKGCR: Clock Generator Control Register */
+#define MDF_CKG_CKGDEN BIT(0)
+#define MDF_CKG_CCK0EN BIT(1)
+#define MDF_CKG_CCK1EN BIT(2)
+#define MDF_CKG_CKGMOD BIT(4)
+#define MDF_CKG_CCK0DIR BIT(5)
+#define MDF_CKG_CCK1DIR BIT(6)
+#define MDF_CKG_TRGSENS BIT(8)
+#define MDF_CKG_TRGSRC_MASK GENMASK(15, 12)
+#define MDF_CKG_CCKDIV_MASK GENMASK(19, 16)
+#define MDF_CKG_CCKDIV_MAX (FIELD_MAX(MDF_CKG_CCKDIV_MASK) + 1)
+#define MDF_CKG_PROCDIV_MASK GENMASK(30, 24)
+#define MDF_PROCDIV_MAX (FIELD_MAX(MDF_CKG_PROCDIV_MASK) + 1)
+#define MDF_CKG_ACTIVE BIT(31)
+
+/* MDF_OR: Option Register */
+#define MDF_OR_OPTION_MASK OPTION(31, 0)
+
+/* MDF_SITFCR: Serial Interface Control Register */
+#define MDF_SITFCR_SITFEN BIT(0)
+#define MDF_SITFCR_SCKSRC_MASK GENMASK(2, 1)
+#define MDF_SITFCR_SITFMOD_MASK GENMASK(5, 4)
+#define MDF_SITFCR_STH_MASK GENMASK(12, 8)
+#define MDF_SITFCR_ACTIVE BIT(31)
+
+/* MDF_BSMXCR: Bitstream Matrix Control Register */
+#define MDF_BSMXCR_BSSEL_MASK GENMASK(4, 0)
+#define MDF_BSMXCR_ACTIVE BIT(31)
+
+/* MDF_DFLTCR: Digital Filter Control Register */
+#define MDF_DFLTCR_DFLTEN BIT(0)
+#define MDF_DFLTCR_DMAEN BIT(1)
+#define MDF_DFLTCR_FTH BIT(2)
+#define MDF_DFLTCR_ACQMOD_MASK GENMASK(6, 4)
+#define MDF_DFLTCR_TRGSENS BIT(8)
+#define MDF_DFLTCR_TRGSRC_MASK GENMASK(15, 12)
+#define MDF_DFLTCR_SNPSFMT BIT(16)
+#define MDF_DFLTCR_NBDIS_MASK GENMASK(27, 20)
+#define MDF_DFLTCR_NBDIS_MAX FIELD_MAX(MDF_DFLTCR_NBDIS_MASK)
+#define MDF_DFLTCR_DFLTRUN BIT(30)
+#define MDF_DFLTCR_ACTIVE BIT(31)
+
+/* MDF_DFLTCICR: Digital Filter Configuration Register */
+#define MDF_DFLTCICR_DATSRC_MASK GENMASK(1, 0)
+#define MDF_DFLTCICR_CICMOD_MASK GENMASK(6, 4)
+#define MDF_DFLTCICR_MCICD_MASK GENMASK(16, 8)
+#define MDF_DFLTCICR_MCICD_MIN 2
+#define MDF_DFLTCICR_SCALE_MASK GENMASK(25, 20)
+
+/* MDF_DFLTRSFR: Reshape Filter Configuration Register */
+#define MDF_DFLTRSFR_RSFLTBYP BIT(0)
+#define MDF_DFLTRSFR_RSFLTD BIT(4)
+#define MDF_DFLTRSFR_HPFBYP BIT(7)
+#define MDF_DFLTRSFR_HPFC_MASK GENMASK(9, 8)
+
+/* MDF_DFLTINTR: Integrator Configuration Register */
+#define MDF_DFLTINTR_INTDIV_MASK GENMASK(1, 0)
+#define MDF_DFLTINTR_INTVAL_MASK GENMASK(10, 4)
+
+/* MDF_OLDCR: Out-Of Limit Detector Control Register */
+#define MDF_OLDCR_OLDEN_MASK BIT(0)
+#define MDF_OLDCR_THINB_MASK BIT(1)
+#define MDF_OLDCR_BKOLD_MASK GENMASK(7, 4)
+#define MDF_OLDCR_ACICN_MASK GENMASK(13, 12)
+#define MDF_OLDCR_ACICD_MASK GENMASK(21, 17)
+#define MDF_OLDCR_ACTIVE_MASK BIT(31)
+
+/* MDF_OLDxTHLR: OLD Threshold Low Register */
+#define MDF_OLDTHLR_OLDTHL_MASK GENMASK(25, 0)
+
+/* MDF_OLDxTHHR: OLD Threshold High Register */
+#define MDF_OLDTHHR_OLDTHH_MASK GENMASK(25, 0)
+
+/* MDF_DLYCR: Delay control Register */
+#define MDF_DLYCR_SKPDLY_MASK GENMASK(6, 0)
+
+/* MDF_SCDCR: Short circuit detector control Register */
+#define MDF_SCDCR_CDEN_MASK BIT(0)
+#define MDF_SCDCR_BKSCD_MASK GENMASK(7, 4)
+#define MDF_SCDCR_SCDT_MASK GENMASK(19, 12)
+#define MDF_SCDCR_ACTIVE_MASK BIT(31)
+
+/* MDF_DFLTIER: DFLT Interrupt enable Register */
+#define MDF_DFLTIER_FTHIE_MASK BIT(0)
+#define MDF_DFLTIER_DOVRIE_MASK BIT(1)
+#define MDF_DFLTIER_SSDRIE_MASK BIT(2)
+#define MDF_DFLTIER_OLDIE_MASK BIT(4)
+#define MDF_DFLTIER_SSOVRIE_MASK BIT(7)
+#define MDF_DFLTIER_SCDIE_MASK BIT(8)
+#define MDF_DFLTIER_SATIE_MASK BIT(9)
+#define MDF_DFLTIER_CKABIE_MASK BIT(10)
+#define MDF_DFLTIER_RFOVRIE_MASK BIT(11)
+
+/* MDF_DFLTISR: DFLT Interrupt status Register */
+#define MDF_DFLTISR_FTHF_MASK BIT(0)
+#define MDF_DFLTISR_DOVRF_MASK BIT(1)
+#define MDF_DFLTISR_SSDRF_MASK BIT(2)
+#define MDF_DFLTISR_OLDF_MASK BIT(4)
+#define MDF_DFLTISR_SSOVRF_MASK BIT(7)
+#define MDF_DFLTISR_SCDF_MASK BIT(8)
+#define MDF_DFLTISR_SATF_MASK BIT(9)
+#define MDF_DFLTISR_CKABF_MASK BIT(10)
+#define MDF_DFLTISR_RFOVRF_MASK BIT(11)
+
+/* MDF_OECCR: Offset Error Compensation Control Register */
+#define MDF_OECCR_OFFSET_MASK GENMASK(25, 0)
+
+/* MDF_SNPSDR: Snapshot Data Register */
+#define MDF_SNPSDR_MCICDC_MASK GENMASK(8, 0)
+#define MDF_SNPSDR_EXTSDR_MASK GENMASK(15, 9)
+#define MDF_SNPSDR_SDR_MASK GENMASK(31, 16)
+
+/* MDF_DFLTDR: Digital Filter Data Register */
+#define MDF_DFLTDR_DR_MASK GENMASK(31, 8)
+
+/* MDF_HWCFGR: Hardware configuration register */
+#define MDF_HWCFGR_FIFO_SIZE_MASK GENMASK(7, 0)
+#define MDF_HWCFGR_NBF_MASK GENMASK(15, 8)
+#define MDF_HWCFGR_FLT_CFG_MASK GENMASK(19, 16)
+#define MDF_HWCFGR_SAD_MASK GENMASK(23, 20)
+#define MDF_HWCFGR_OPTION_MASK GENMASK(31, 24)
+
+/* MDF_VERR: Version Register */
+#define MDF_VERR_MINREV_MASK GENMASK(3, 0)
+#define MDF_VERR_MAJREV_MASK GENMASK(7, 4)
+
+/* MDF_IPIDR: Identification Register */
+#define MDF_IPIDR_ID_MASK GENMASK(31, 0)
+
+/* MDF_SIDR: Size Identification Register */
+#define MDF_SIDR_SID_MASK GENMASK(31, 0)
+
+#define STM32MP25_MDF_IPIDR_NUMBER 0x00110032
+
+/*
+ * struct stm32_mdf - STM32 MDF driver common data (for all instances)
+ * @filter_list: pointer to filter interfaces list
+ * @kclk: kernel clock shared by the core and its children
+ * @fproc: processing clock frequency
+ * @nbf: total number of digital filters
+ * @nb_interleave: number of interleaved filters
+ */
+struct stm32_mdf {
+ struct list_head filter_list;
+ struct clk *kclk;
+ unsigned long fproc;
+ unsigned int nbf;
+ unsigned int nb_interleave;
+};
+
+static inline struct regmap *stm32_mdf_regmap_init(struct device *dev, struct clk *kclk,
+ void __iomem *base,
+ const struct regmap_config *config)
+{
+ struct regmap *map;
+ int ret;
+
+ /* Regcache initialization may read registers before the clock is attached. */
+ ret = clk_prepare_enable(kclk);
+ if (ret)
+ return ERR_PTR(ret);
+
+ map = devm_regmap_init_mmio(dev, base, config);
+ if (!IS_ERR(map)) {
+ ret = regmap_mmio_attach_clk(map, kclk);
+ if (ret)
+ map = ERR_PTR(ret);
+ }
+ clk_disable_unprepare(kclk);
+
+ return map;
+}
+
+int stm32_mdf_core_start_mdf(struct stm32_mdf *mdf);
+int stm32_mdf_core_stop_mdf(struct stm32_mdf *mdf);
+int stm32_mdf_core_trigger(struct stm32_mdf *mdf);
+int stm32_mdf_core_lock_kclk_rate(struct stm32_mdf *mdf);
+void stm32_mdf_core_unlock_kclk_rate(struct stm32_mdf *mdf);
+
--
2.43.0
^ permalink raw reply related [flat|nested] 22+ messages in thread
* [PATCH v2 03/11] iio: adc: stm32-mdf: add cck clocks provider
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:17 ` 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
` (7 subsequent siblings)
10 siblings, 1 reply; 22+ messages in thread
From: Olivier Moysan @ 2026-11-18 15:17 UTC (permalink / raw)
To: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko,
Rob Herring, Krzysztof Kozlowski, Conor Dooley, Maxime Coquelin,
Alexandre Torgue, Philipp Zabel, Sumit Semwal,
Christian König, Philipp Stanner, Arnaud Pouliquen,
Liam Girdwood, Mark Brown, Jaroslav Kysela, Takashi Iwai,
Fabrice Gasnier
Cc: linux-iio, devicetree, linux-stm32, linux-arm-kernel,
linux-kernel, linux-media, dri-devel, linaro-mm-sig, linux-sound,
Olivier Moysan
MDF clock outputs can supply the serial interfaces and their external
converters. Register CCK0 and CCK1 with the common clock framework so that
consumers can configure and gate these outputs through the clock API.
Implement rate selection using the processing and CCK dividers, checking
the hardware limits and clock accuracy.
Expose helpers for child drivers to retrieve the CCK rate and restore its
configuration.
Signed-off-by: Olivier Moysan <olivier.moysan@foss.st.com>
---
drivers/iio/adc/stm32-mdf-core.c | 398 ++++++++++++++++++++++++++++++++++++++-
drivers/iio/adc/stm32-mdf.h | 5 +
2 files changed, 402 insertions(+), 1 deletion(-)
diff --git a/drivers/iio/adc/stm32-mdf-core.c b/drivers/iio/adc/stm32-mdf-core.c
index ca50ce566a43..4e462c6c42bc 100644
--- a/drivers/iio/adc/stm32-mdf-core.c
+++ b/drivers/iio/adc/stm32-mdf-core.c
@@ -8,6 +8,7 @@
#include <linux/cleanup.h>
#include <linux/clk.h>
+#include <linux/clk-provider.h>
#include <linux/device.h>
#include <linux/list.h>
#include <linux/module.h>
@@ -82,6 +83,7 @@ static const struct regmap_config stm32_mdf_regmap_cfg = {
* @n_active_ch: number of active channels
* @lock: lock to manage common resources
* @kclk_rate: save kclk rate at probe
+ * @cck_freq_actual: actual output cck clocks frequency
* @exclusive_flg: exclusive rate request flag
*/
struct stm32_mdf_priv {
@@ -94,10 +96,24 @@ struct stm32_mdf_priv {
atomic_t n_active_ch;
spinlock_t lock; /* Manage common resources race conditions */
unsigned long kclk_rate;
+ unsigned long cck_freq_actual;
bool exclusive_flg;
};
+#define STM32_MDF_MAX_CCK 2
#define STM32_MDF_MP23_FILTER_NB 4
+#define STM32_MDF_MAX_CCK_RATE 25000000
+
+struct clk_mdf_cck {
+ struct clk_hw hw;
+ struct stm32_mdf_priv *priv;
+};
+
+static const struct clk_parent_data clk_cck_parent_data[] = {
+ { .index = 0 },
+};
+
+#define to_clk_mdf_cck(_hw) container_of(_hw, struct clk_mdf_cck, hw)
static inline struct stm32_mdf_priv *to_stm32_mdf_priv(struct stm32_mdf *mdf)
{
@@ -194,6 +210,96 @@ int stm32_mdf_core_trigger(struct stm32_mdf *mdf)
}
EXPORT_SYMBOL_GPL(stm32_mdf_core_trigger);
+/**
+ * stm32_mdf_core_get_cck() - Get the configured CCK frequency
+ * @mdf: MDF instance
+ *
+ * Return: The actual CCK frequency, in Hz.
+ */
+unsigned long stm32_mdf_core_get_cck(struct stm32_mdf *mdf)
+{
+ struct stm32_mdf_priv *priv = to_stm32_mdf_priv(mdf);
+
+ return priv->cck_freq_actual;
+}
+EXPORT_SYMBOL_GPL(stm32_mdf_core_get_cck);
+
+/**
+ * stm32_mdf_core_lock_kclk_rate() - Prevent changes to the MDF kernel clock rate
+ * @mdf: MDF instance
+ *
+ * Request an exclusive rate on the kernel clock when no MDF channels are
+ * active. While channels are active, the rate is already held exclusive by
+ * the start operation.
+ *
+ * Return: 0 on success, or a negative error code on failure.
+ */
+int stm32_mdf_core_lock_kclk_rate(struct stm32_mdf *mdf)
+{
+ struct stm32_mdf_priv *priv = to_stm32_mdf_priv(mdf);
+ struct device *dev = &priv->pdev->dev;
+ int ret;
+
+ if (!atomic_read(&priv->n_active_ch)) {
+ /* Request rate exclusivity on kernel clock right now */
+ ret = clk_rate_exclusive_get(priv->kclk);
+ if (ret < 0) {
+ dev_err(dev, "Failed to get exclusive rate on kernel clock: [%d]\n", ret);
+ return ret;
+ }
+
+ priv->exclusive_flg = true;
+ }
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(stm32_mdf_core_lock_kclk_rate);
+
+/**
+ * stm32_mdf_core_unlock_kclk_rate() - Release the MDF kernel clock rate lock
+ * @mdf: MDF instance
+ *
+ * Release an exclusive rate acquired by stm32_mdf_core_lock_kclk_rate() when
+ * no MDF channels are active. The lock is retained while channels are active.
+ */
+void stm32_mdf_core_unlock_kclk_rate(struct stm32_mdf *mdf)
+{
+ struct stm32_mdf_priv *priv = to_stm32_mdf_priv(mdf);
+
+ if (!atomic_read(&priv->n_active_ch)) {
+ clk_rate_exclusive_put(priv->kclk);
+ priv->exclusive_flg = false;
+ }
+}
+EXPORT_SYMBOL_GPL(stm32_mdf_core_unlock_kclk_rate);
+
+/**
+ * stm32_mdf_core_restore_cck() - Restore the MDF kernel clock rate
+ * @mdf: MDF instance
+ *
+ * Set the kernel clock to the rate recorded when the MDF core was probed.
+ *
+ * Return: 0 on success, or a negative error code on failure.
+ */
+int stm32_mdf_core_restore_cck(struct stm32_mdf *mdf)
+{
+ struct stm32_mdf_priv *priv = to_stm32_mdf_priv(mdf);
+ struct device *dev = &priv->pdev->dev;
+ int ret;
+
+ ret = clk_set_rate(priv->kclk, priv->kclk_rate);
+ if (ret) {
+ dev_err(dev, "Failed to change kernel clock rate from [%lu]Hz to [%lu]Hz: [%d]\n",
+ clk_get_rate(priv->kclk), priv->kclk_rate, ret);
+ return ret;
+ }
+
+ dev_dbg(dev, "MDF kernel clock rate changed to [%lu]Hz.\n", priv->kclk_rate);
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(stm32_mdf_core_restore_cck);
+
/**
* stm32_mdf_core_stop_mdf() - Stop an MDF channel
* @mdf: MDF instance
@@ -223,11 +329,301 @@ int stm32_mdf_core_stop_mdf(struct stm32_mdf *mdf)
}
EXPORT_SYMBOL_GPL(stm32_mdf_core_stop_mdf);
+static int stm32_mdf_core_cck_divider_set_rate(struct platform_device *pdev,
+ struct stm32_mdf_priv *priv,
+ unsigned long rate,
+ unsigned long parent_rate)
+{
+ struct device *dev = &pdev->dev;
+ unsigned long ratio, delta, delta_ppm, procdiv;
+ unsigned int cckdiv;
+ int ret;
+
+ if (!rate || rate > STM32_MDF_MAX_CCK_RATE || !parent_rate)
+ return -EINVAL;
+
+ ratio = DIV_ROUND_CLOSEST(parent_rate, rate);
+ if (!ratio) {
+ dev_err(dev, "CCK frequency above kernel frequency\n");
+ return -EINVAL;
+ }
+
+ delta = abs(parent_rate - (ratio * rate));
+
+ delta_ppm = (1000000 * delta) / parent_rate;
+ if (delta_ppm > 1000) {
+ /* If frequency deviation is higher than 1000 ppm return error */
+ dev_dbg(dev, "CCK clock deviation [%lu] ppm too large: [%lu] vs [%lu] Hz\n",
+ delta_ppm, DIV_ROUND_CLOSEST(parent_rate, ratio), rate);
+ return -EINVAL;
+ } else if (delta_ppm) {
+ dev_dbg(dev, "CCK clock frequency not accurate: [%lu] ppm deviation\n", delta_ppm);
+ }
+
+ /*
+ * The total divider ratio must be split between proc divider and
+ * cck divider. Try to maximize cck divider first, to help fulfilling
+ * frequency ratio requirements between fproc and fcck.
+ */
+ cckdiv = min_t(unsigned long, ratio, MDF_CKG_CCKDIV_MAX);
+ while (ratio % cckdiv)
+ cckdiv--;
+ procdiv = ratio / cckdiv;
+
+ if (procdiv > MDF_PROCDIV_MAX) {
+ dev_err(dev, "Proc divider out of range: %lu > %lu\n", procdiv,
+ MDF_PROCDIV_MAX);
+ return -EINVAL;
+ }
+
+ priv->mdf.fproc = DIV_ROUND_CLOSEST(parent_rate, procdiv);
+
+ /* Configure CKGCR register */
+ dev_dbg(dev, "Set MDF dividers: CCKDIV [%u], PROCDIV [%lu]\n", cckdiv, procdiv);
+ ret = regmap_update_bits(priv->regmap, MDF_CKGCR_REG,
+ MDF_CKG_CCKDIV_MASK | MDF_CKG_PROCDIV_MASK,
+ FIELD_PREP(MDF_CKG_CCKDIV_MASK, cckdiv - 1) |
+ FIELD_PREP(MDF_CKG_PROCDIV_MASK, procdiv - 1));
+ if (!ret)
+ priv->cck_freq_actual = DIV_ROUND_CLOSEST(parent_rate, ratio);
+
+ return ret;
+}
+
+static int stm32_mdf_core_cck_set_rate(struct clk_hw *hw, unsigned long rate,
+ unsigned long parent_rate)
+{
+ struct clk_mdf_cck *clk = to_clk_mdf_cck(hw);
+ struct stm32_mdf_priv *priv = clk->priv;
+
+ return stm32_mdf_core_cck_divider_set_rate(priv->pdev, priv, rate, parent_rate);
+}
+
+static int stm32_mdf_core_cck_determine_rate(struct clk_hw *hw,
+ struct clk_rate_request *req)
+{
+ struct clk_mdf_cck *clk = to_clk_mdf_cck(hw);
+ struct stm32_mdf_priv *priv = clk->priv;
+ struct platform_device *pdev = priv->pdev;
+ unsigned long ratio;
+
+ if (!req->rate || req->rate > STM32_MDF_MAX_CCK_RATE || !req->best_parent_rate)
+ return -EINVAL;
+
+ ratio = DIV_ROUND_CLOSEST(req->best_parent_rate, req->rate);
+ if (!ratio) {
+ dev_err(&pdev->dev, "CCK frequency above kernel frequency\n");
+ return -EINVAL;
+ }
+
+ req->rate = DIV_ROUND_CLOSEST(req->best_parent_rate, ratio);
+
+ return 0;
+}
+
+static unsigned long stm32_mdf_core_cck_divider_recalc_rate(struct clk_hw *hw,
+ unsigned long parent_rate)
+{
+ struct clk_mdf_cck *clk = to_clk_mdf_cck(hw);
+ struct stm32_mdf_priv *priv = clk->priv;
+ unsigned int val;
+ unsigned int ratio;
+ int ret;
+
+ ret = regmap_read(priv->regmap, MDF_CKGCR_REG, &val);
+ if (ret)
+ /* Gives a hint that something is wrong */
+ return 0;
+
+ ratio = (FIELD_GET(MDF_CKG_PROCDIV_MASK, val) + 1) *
+ (FIELD_GET(MDF_CKG_CCKDIV_MASK, val) + 1);
+
+ return DIV_ROUND_CLOSEST_ULL((u64)parent_rate, ratio);
+};
+
+static const struct clk_ops clk_cck_ops = {
+ .recalc_rate = stm32_mdf_core_cck_divider_recalc_rate,
+ .set_rate = stm32_mdf_core_cck_set_rate,
+ .determine_rate = stm32_mdf_core_cck_determine_rate,
+};
+
+static struct clk_mdf_cck clk_cck;
+
+struct clk_cckx_gate {
+ struct clk_hw hw;
+ struct regmap *map;
+ u32 map_offset;
+ u8 bit_idx;
+ u8 flags;
+ spinlock_t *lock; /* Protect cckx gating */
+};
+
+#define to_clk_cckx_gate(_hw) container_of(_hw, struct clk_cckx_gate, hw)
+
+static inline u32 clk_cckx_gate_readl(struct clk_cckx_gate *gate)
+{
+ u32 val;
+
+ regmap_read(gate->map, gate->map_offset, &val);
+
+ return val;
+}
+
+static inline void clk_cckx_gate_writel(struct clk_cckx_gate *gate, u32 val)
+{
+ regmap_write(gate->map, gate->map_offset, val);
+}
+
+static void clk_cckx_gate_endisable(struct clk_hw *hw, int enable)
+{
+ struct clk_cckx_gate *gate = to_clk_cckx_gate(hw);
+ unsigned long flags = 0;
+ u32 reg;
+
+ if (gate->lock)
+ spin_lock_irqsave(gate->lock, flags);
+ else
+ __acquire(gate->lock);
+
+ reg = clk_cckx_gate_readl(gate);
+
+ if (enable)
+ reg |= BIT(gate->bit_idx);
+ else
+ reg &= ~BIT(gate->bit_idx);
+
+ clk_cckx_gate_writel(gate, reg);
+
+ if (gate->lock)
+ spin_unlock_irqrestore(gate->lock, flags);
+ else
+ __release(gate->lock);
+}
+
+static int clk_cckx_gate_enable(struct clk_hw *hw)
+{
+ clk_cckx_gate_endisable(hw, 1);
+
+ return 0;
+}
+
+static void clk_cckx_gate_disable(struct clk_hw *hw)
+{
+ clk_cckx_gate_endisable(hw, 0);
+}
+
+static int clk_cckx_gate_is_enabled(struct clk_hw *hw)
+{
+ struct clk_cckx_gate *gate = to_clk_cckx_gate(hw);
+ u32 reg;
+
+ reg = clk_cckx_gate_readl(gate);
+
+ reg &= BIT(gate->bit_idx);
+
+ return reg ? 1 : 0;
+}
+
+static const struct clk_ops clk_cckx_gate_ops = {
+ .enable = clk_cckx_gate_enable,
+ .disable = clk_cckx_gate_disable,
+ .is_enabled = clk_cckx_gate_is_enabled,
+};
+
+static struct clk_cckx_gate clk_cck0 = {
+ .map_offset = MDF_CKGCR_REG,
+ .bit_idx = 1,
+};
+
+static struct clk_cckx_gate clk_cck1 = {
+ .map_offset = MDF_CKGCR_REG,
+ .bit_idx = 2,
+};
+
+static int stm32_mdf_core_register_clock_provider(struct platform_device *pdev,
+ struct stm32_mdf_priv *priv)
+{
+ struct device_node *node = pdev->dev.of_node;
+ struct device *dev = &pdev->dev;
+ struct clk_hw_onecell_data *clk_data;
+ struct property *prop;
+ const char *clk_name;
+ u32 ckgcr = 0;
+ int index = 0, ret, clk_id;
+ struct clk_hw *mdf_hw[] = { &clk_cck0.hw, &clk_cck1.hw };
+ struct clk_cckx_gate *gate;
+
+ clk_cck.hw.init = CLK_HW_INIT_PARENTS_DATA("clk_cck", clk_cck_parent_data,
+ &clk_cck_ops, 0);
+ clk_cck0.hw.init = CLK_HW_INIT_HW(STM32_MDF_CCK0, &clk_cck.hw, &clk_cckx_gate_ops,
+ CLK_SET_RATE_PARENT | CLK_IGNORE_UNUSED);
+ clk_cck1.hw.init = CLK_HW_INIT_HW(STM32_MDF_CCK1, &clk_cck.hw, &clk_cckx_gate_ops,
+ CLK_SET_RATE_PARENT | CLK_IGNORE_UNUSED);
+
+ clk_cck.priv = priv;
+
+ ret = devm_clk_hw_register(dev, &clk_cck.hw);
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to register clk_cck");
+
+ clk_data = devm_kzalloc(dev, struct_size(clk_data, hws, STM32_MDF_MAX_CCK), GFP_KERNEL);
+ if (!clk_data)
+ return -ENOMEM;
+
+ of_property_for_each_string(node, "clock-output-names", prop, clk_name) {
+ if (index >= STM32_MDF_MAX_CCK) {
+ dev_err(dev, "Too many cck providers defined\n");
+ return -EINVAL;
+ }
+
+ if (!strncmp(clk_name, "cck0", 4)) {
+ clk_id = 0;
+ } else if (!strncmp(clk_name, "cck1", 4)) {
+ clk_id = 1;
+ } else {
+ dev_err(dev, "Unexpected cck clock provider name [%s]\n", clk_name);
+ return -EINVAL;
+ }
+
+ gate = to_clk_cckx_gate(mdf_hw[clk_id]);
+ gate->map = priv->regmap;
+
+ ret = devm_clk_hw_register(dev, mdf_hw[clk_id]);
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to register %s clock provider",
+ clk_name);
+
+ clk_data->hws[index] = mdf_hw[clk_id];
+
+ /* Configure the CCKx clock as output */
+ ckgcr |= clk_id ? MDF_CKG_CCK1DIR : MDF_CKG_CCK0DIR;
+
+ index++;
+ }
+
+ /*
+ * Set num to max number of clock. This allows any clocks indice below maximum
+ * in of_clk_hw_onecell_get, whatever the actual number of providers.
+ */
+ clk_data->num = STM32_MDF_MAX_CCK;
+ ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, clk_data);
+ if (ret) {
+ dev_err(dev, "Failed to add %s clock provider: %d\n",
+ clk_name ? clk_name : "", ret);
+ return ret;
+ }
+
+ /* Configure CKGCR register */
+ return regmap_update_bits(priv->regmap, MDF_CKGCR_REG,
+ MDF_CKG_CCK1DIR | MDF_CKG_CCK0DIR, ckgcr);
+}
+
static int stm32_mdf_core_parse_clocks(struct platform_device *pdev, struct stm32_mdf_priv *priv)
{
priv->kclk_rate = clk_get_rate(priv->kclk);
- return 0;
+ /* The consumer assigns the CCK rate after the provider is registered. */
+ return stm32_mdf_core_register_clock_provider(pdev, priv);
}
static int stm32_mdf_core_parse_of(struct platform_device *pdev, struct stm32_mdf_priv *priv)
diff --git a/drivers/iio/adc/stm32-mdf.h b/drivers/iio/adc/stm32-mdf.h
index b334769f8b17..e3617b181e28 100644
--- a/drivers/iio/adc/stm32-mdf.h
+++ b/drivers/iio/adc/stm32-mdf.h
@@ -174,6 +174,9 @@
#define STM32MP25_MDF_IPIDR_NUMBER 0x00110032
+#define STM32_MDF_CCK0 "clk_cck0"
+#define STM32_MDF_CCK1 "clk_cck1"
+
/*
* struct stm32_mdf - STM32 MDF driver common data (for all instances)
* @filter_list: pointer to filter interfaces list
@@ -216,6 +219,8 @@ static inline struct regmap *stm32_mdf_regmap_init(struct device *dev, struct cl
int stm32_mdf_core_start_mdf(struct stm32_mdf *mdf);
int stm32_mdf_core_stop_mdf(struct stm32_mdf *mdf);
int stm32_mdf_core_trigger(struct stm32_mdf *mdf);
+int stm32_mdf_core_restore_cck(struct stm32_mdf *mdf);
int stm32_mdf_core_lock_kclk_rate(struct stm32_mdf *mdf);
void stm32_mdf_core_unlock_kclk_rate(struct stm32_mdf *mdf);
+unsigned long stm32_mdf_core_get_cck(struct stm32_mdf *mdf);
--
2.43.0
^ permalink raw reply related [flat|nested] 22+ messages in thread
* [PATCH v2 04/11] iio: adc: stm32-mdf: add serial interfaces support
2026-11-18 15:17 [PATCH v2 00/11] iio: adc: stm32: add mdf support for stm32mp2 Olivier Moysan
` (2 preceding siblings ...)
2026-11-18 15:17 ` [PATCH v2 03/11] iio: adc: stm32-mdf: add cck clocks provider Olivier Moysan
@ 2026-11-18 15:17 ` 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
` (6 subsequent siblings)
10 siblings, 1 reply; 22+ messages in thread
From: Olivier Moysan @ 2026-11-18 15:17 UTC (permalink / raw)
To: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko,
Rob Herring, Krzysztof Kozlowski, Conor Dooley, Maxime Coquelin,
Alexandre Torgue, Philipp Zabel, Sumit Semwal,
Christian König, Philipp Stanner, Arnaud Pouliquen,
Liam Girdwood, Mark Brown, Jaroslav Kysela, Takashi Iwai,
Fabrice Gasnier
Cc: linux-iio, devicetree, linux-stm32, linux-arm-kernel,
linux-kernel, linux-media, dri-devel, linaro-mm-sig, linux-sound,
Olivier Moysan
The MDF serial interfaces receive the bitstreams from external sigma delta
modulators supporting SPI and Manchester protocols.
Select an external serial clock or an MDF CCK output, validate the clock
requirements of each mode and program the serial interface.
Expose reference-counted start and stop helpers so that multiple filters
can share an interface.
Signed-off-by: Olivier Moysan <olivier.moysan@foss.st.com>
---
drivers/iio/adc/Makefile | 2 +-
drivers/iio/adc/stm32-mdf-core.c | 34 +++++-
drivers/iio/adc/stm32-mdf-serial.c | 223 +++++++++++++++++++++++++++++++++++++
drivers/iio/adc/stm32-mdf.h | 49 ++++++++
4 files changed, 306 insertions(+), 2 deletions(-)
diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile
index d5fc809c33ca..811ef85a88fb 100644
--- a/drivers/iio/adc/Makefile
+++ b/drivers/iio/adc/Makefile
@@ -147,7 +147,7 @@ obj-$(CONFIG_STM32_ADC) += stm32-adc.o
obj-$(CONFIG_STM32_DFSDM_ADC) += stm32-dfsdm-adc.o
obj-$(CONFIG_STM32_DFSDM_CORE) += stm32-dfsdm-core.o
obj-$(CONFIG_STM32_MDF_CORE) += stm32-mdf.o
-stm32-mdf-y := stm32-mdf-core.o
+stm32-mdf-y := stm32-mdf-core.o stm32-mdf-serial.o
obj-$(CONFIG_STMPE_ADC) += stmpe-adc.o
obj-$(CONFIG_SUN20I_GPADC) += sun20i-gpadc-iio.o
obj-$(CONFIG_SUN4I_GPADC) += sun4i-gpadc-iio.o
diff --git a/drivers/iio/adc/stm32-mdf-core.c b/drivers/iio/adc/stm32-mdf-core.c
index 4e462c6c42bc..5b5d2d4cbb5c 100644
--- a/drivers/iio/adc/stm32-mdf-core.c
+++ b/drivers/iio/adc/stm32-mdf-core.c
@@ -748,8 +748,13 @@ static int stm32_mdf_core_probe(struct platform_device *pdev)
if (ret < 0)
return ret;
+ INIT_LIST_HEAD(&priv->mdf.sitf_list);
INIT_LIST_HEAD(&priv->mdf.filter_list);
+ ret = stm32_mdf_sitf_init(&pdev->dev, &priv->mdf);
+ if (ret)
+ return ret;
+
platform_set_drvdata(pdev, priv);
pm_runtime_get_noresume(&pdev->dev);
@@ -785,8 +790,15 @@ static int stm32_mdf_core_suspend(struct device *dev)
{
struct stm32_mdf *mdf = dev_get_drvdata(dev);
struct stm32_mdf_priv *priv = to_stm32_mdf_priv(mdf);
+ struct stm32_mdf_sitf *sitf;
int ret;
+ list_for_each_entry(sitf, &mdf->sitf_list, entry) {
+ ret = regmap_read(sitf->regmap, MDF_SITFCR_REG, &sitf->sitfcr);
+ if (ret)
+ return ret;
+ }
+
ret = pm_runtime_force_suspend(dev);
if (ret)
return ret;
@@ -794,7 +806,9 @@ static int stm32_mdf_core_suspend(struct device *dev)
regcache_cache_only(priv->regmap, true);
regcache_mark_dirty(priv->regmap);
- /* Balance regmap_mmio_attach_clk() clk_prepare() */
+ /* Balance regmap_mmio_attach_clk() for the SITFs and the core. */
+ list_for_each_entry(sitf, &mdf->sitf_list, entry)
+ clk_unprepare(priv->kclk);
clk_unprepare(priv->kclk);
return pinctrl_pm_select_sleep_state(dev);
@@ -804,6 +818,8 @@ static int stm32_mdf_core_resume(struct device *dev)
{
struct stm32_mdf *mdf = dev_get_drvdata(dev);
struct stm32_mdf_priv *priv = to_stm32_mdf_priv(mdf);
+ struct stm32_mdf_sitf *sitf;
+ unsigned int prepared = 0;
int ret;
ret = pinctrl_pm_select_default_state(dev);
@@ -825,8 +841,24 @@ static int stm32_mdf_core_resume(struct device *dev)
goto err_cache;
}
+ list_for_each_entry(sitf, &mdf->sitf_list, entry) {
+ ret = clk_prepare(priv->kclk);
+ if (ret)
+ goto err_sitf;
+ prepared++;
+
+ ret = regmap_write(sitf->regmap, MDF_SITFCR_REG, sitf->sitfcr);
+ if (ret)
+ goto err_sitf;
+ }
+
return pm_runtime_force_resume(dev);
+err_sitf:
+ while (prepared) {
+ clk_unprepare(priv->kclk);
+ prepared--;
+ }
err_cache:
clk_unprepare(priv->kclk);
err_clk:
diff --git a/drivers/iio/adc/stm32-mdf-serial.c b/drivers/iio/adc/stm32-mdf-serial.c
new file mode 100644
index 000000000000..6ba3be019847
--- /dev/null
+++ b/drivers/iio/adc/stm32-mdf-serial.c
@@ -0,0 +1,223 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * This file is part of STM32 MDF driver
+ *
+ * Copyright (C) 2023-2026, STMicroelectronics - All Rights Reserved
+ */
+
+#include <linux/cleanup.h>
+#include <linux/clk.h>
+#include <linux/clk/clk-conf.h>
+#include <linux/clk-provider.h>
+#include <linux/device.h>
+#include <linux/export.h>
+#include <linux/list.h>
+#include <linux/of.h>
+#include <linux/regmap.h>
+#include <linux/slab.h>
+#include <linux/spinlock.h>
+#include <linux/string.h>
+
+#include "stm32-mdf.h"
+
+enum {
+ STM32_MDF_SCKSRC_CCK0,
+ STM32_MDF_SCKSRC_CCK1,
+ STM32_MDF_SCKSRC_CLK,
+ STM32_MDF_SCKSRC_NONE,
+};
+
+static const char * const stm32_mdf_sitf_modes[] = {
+ [STM32_MDF_MODE_SPI] = "spi",
+ [STM32_MDF_MODE_LF_SPI] = "lf_spi",
+ [STM32_MDF_MODE_MANCHESTER_R] = "manchester_r",
+ [STM32_MDF_MODE_MANCHESTER_F] = "manchester_f",
+};
+
+static const struct regmap_config stm32_sitf_regmap_cfg = {
+ .reg_bits = 32,
+ .val_bits = 32,
+ .reg_stride = sizeof(u32),
+ .max_register = MDF_SITFCR_REG,
+ .fast_io = true,
+ /* Do not use regcache as it does not support single register map */
+};
+
+/**
+ * stm32_mdf_sitf_start() - Enable an MDF serial interface
+ * @sitf: Serial interface to enable
+ *
+ * Enable the serial interface and increment its active-user count on success.
+ * Each successful call must be balanced by stm32_mdf_sitf_stop().
+ *
+ * Return: 0 on success, or a negative error code on failure.
+ */
+int stm32_mdf_sitf_start(struct stm32_mdf_sitf *sitf)
+{
+ int ret;
+
+ guard(spinlock)(&sitf->lock);
+
+ ret = regmap_set_bits(sitf->regmap, MDF_SITFCR_REG, MDF_SITFCR_SITFEN);
+ if (!ret)
+ sitf->refcnt++;
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(stm32_mdf_sitf_start);
+
+/**
+ * stm32_mdf_sitf_stop() - Release an MDF serial interface user
+ * @sitf: Serial interface to release
+ *
+ * Decrement the active-user count and disable the serial interface when the
+ * last user releases it.
+ *
+ * Return: 0 on success, -EPERM if there are no active users, or a negative
+ * error code if disabling the interface fails.
+ */
+int stm32_mdf_sitf_stop(struct stm32_mdf_sitf *sitf)
+{
+ int ret = 0;
+
+ guard(spinlock)(&sitf->lock);
+
+ if (!sitf->refcnt) {
+ dev_err(sitf->dev, "Unbalanced serial interface stop ?\n");
+ return -EPERM;
+ }
+
+ sitf->refcnt--;
+
+ if (!sitf->refcnt)
+ ret = regmap_clear_bits(sitf->regmap, MDF_SITFCR_REG, MDF_SITFCR_SITFEN);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(stm32_mdf_sitf_stop);
+
+static int stm32_mdf_sitf_get_clk(struct device *dev, struct device_node *np,
+ struct stm32_mdf_sitf *sitf)
+{
+ struct clk *sck;
+ int ret;
+
+ /* SITF nodes have no device to apply their assigned clock configuration. */
+ ret = of_clk_set_defaults(np, false);
+ if (ret)
+ return dev_err_probe(dev, ret, "Can't configure serial clock for %pOF\n", np);
+
+ sitf->scksrc = STM32_MDF_SCKSRC_NONE;
+ /* Optional clock. Clock not needed in Manchester mode. */
+ if (!of_property_present(np, "clocks"))
+ return 0;
+
+ sck = devm_get_clk_from_child(dev, np, NULL);
+ if (IS_ERR(sck))
+ return dev_err_probe(dev, PTR_ERR(sck), "Can't get serial clock for %pOF\n", np);
+
+ sitf->sck = sck;
+ if (!strncmp(__clk_get_name(sck), STM32_MDF_CCK0, sizeof(STM32_MDF_CCK0)))
+ sitf->scksrc = STM32_MDF_SCKSRC_CCK0;
+ else if (!strncmp(__clk_get_name(sck), STM32_MDF_CCK1, sizeof(STM32_MDF_CCK1)))
+ sitf->scksrc = STM32_MDF_SCKSRC_CCK1;
+ else
+ sitf->scksrc = STM32_MDF_SCKSRC_CLK;
+
+ return 0;
+}
+
+static int stm32_mdf_sitf_parse(struct device *dev, struct device_node *np,
+ struct stm32_mdf_sitf *sitf)
+{
+ const char *str;
+ int ret, mode;
+ u32 idx, sitfcr;
+
+ ret = of_property_read_u32(np, "reg", &idx);
+ if (ret)
+ return dev_err_probe(dev, ret, "Could not get interface index for %pOF\n", np);
+
+ if (idx % 0x80) {
+ dev_err(dev, "Unexpected reg property value [%x]\n", idx);
+ return -EINVAL;
+ }
+
+ idx = (idx >> 7) - 1;
+ if (idx > sitf->mdf->nbf) {
+ dev_err(dev, "Interface index [%d] exceeds maximum [%d]\n", idx, sitf->mdf->nbf);
+ return -EINVAL;
+ }
+
+ ret = of_property_read_string(np, "st,sitf-mode", &str);
+ if (ret)
+ return dev_err_probe(dev, ret, "Could not get interface mode for %pOF\n", np);
+
+ mode = match_string(stm32_mdf_sitf_modes, ARRAY_SIZE(stm32_mdf_sitf_modes), str);
+ if (mode < 0) {
+ dev_err(dev, "Unknown serial interface mode [%s]\n", str);
+ return mode;
+ }
+
+ sitf->mode = mode;
+ sitfcr = FIELD_PREP(MDF_SITFCR_SITFMOD_MASK, mode);
+
+ ret = stm32_mdf_sitf_get_clk(dev, np, sitf);
+ if (ret)
+ return ret;
+
+ if (mode == STM32_MDF_MODE_SPI && sitf->scksrc == STM32_MDF_SCKSRC_NONE) {
+ dev_err(dev, "Missing clock for serial interface [%d] in SPI mode\n", idx);
+ return -EINVAL;
+ }
+
+ if (mode == STM32_MDF_MODE_LF_SPI && (sitf->scksrc != STM32_MDF_SCKSRC_CCK0 &&
+ sitf->scksrc != STM32_MDF_SCKSRC_CCK1)) {
+ dev_err(dev, "Missing CCKx clock for serial interface [%d] in LF_SPI mode\n", idx);
+ return -EINVAL;
+ }
+
+ sitf->id = idx;
+ sitf->node = of_fwnode_handle(np);
+ spin_lock_init(&sitf->lock);
+
+ sitfcr |= FIELD_PREP(MDF_SITFCR_SCKSRC_MASK, sitf->scksrc);
+
+ return regmap_set_bits(sitf->regmap, MDF_SITFCR_REG, sitfcr);
+}
+
+int stm32_mdf_sitf_init(struct device *dev, struct stm32_mdf *mdf)
+{
+ struct stm32_mdf_sitf *sitf;
+ void __iomem *base;
+ int ret;
+
+ for_each_available_child_of_node_scoped(dev->of_node, child) {
+ if (!of_node_name_eq(child, "sitf"))
+ continue;
+
+ sitf = devm_kzalloc(dev, sizeof(*sitf), GFP_KERNEL);
+ if (!sitf)
+ return -ENOMEM;
+ sitf->dev = dev;
+ sitf->mdf = mdf;
+
+ base = devm_of_iomap(dev, child, 0, NULL);
+ if (IS_ERR(base))
+ return PTR_ERR(base);
+
+ sitf->regmap = stm32_mdf_regmap_init(dev, mdf->kclk, base,
+ &stm32_sitf_regmap_cfg);
+ if (IS_ERR(sitf->regmap))
+ return dev_err_probe(dev, PTR_ERR(sitf->regmap),
+ "Failed to init SITF regmap\n");
+
+ ret = stm32_mdf_sitf_parse(dev, child, sitf);
+ if (ret)
+ return ret;
+
+ list_add(&sitf->entry, &mdf->sitf_list);
+ }
+
+ return 0;
+}
diff --git a/drivers/iio/adc/stm32-mdf.h b/drivers/iio/adc/stm32-mdf.h
index e3617b181e28..fa689954d7c1 100644
--- a/drivers/iio/adc/stm32-mdf.h
+++ b/drivers/iio/adc/stm32-mdf.h
@@ -19,6 +19,9 @@
#define MDF_IPIDR_REG 0xff8
#define MDF_SIDR_REG 0xffc
+/* Registers offsets relative to sitf base address */
+#define MDF_SITFCR_REG 0x00 /* 0x80 * x + offset */
+
/* Registers offsets relative to filter base address */
#define MDF_BSMXCR_REG 0x00 /* 0x84 * x + offset */
#define MDF_DFLTCR_REG 0x04
@@ -177,8 +180,49 @@
#define STM32_MDF_CCK0 "clk_cck0"
#define STM32_MDF_CCK1 "clk_cck1"
+enum stm32_sitf_mode {
+ STM32_MDF_MODE_LF_SPI,
+ STM32_MDF_MODE_SPI,
+ STM32_MDF_MODE_MANCHESTER_R,
+ STM32_MDF_MODE_MANCHESTER_F,
+ STM32_MDF_MODE_NB,
+};
+
+/*
+ * struct stm32_mdf_sitf - STM32 MDF serial interface data
+ * @entry: serial interface list head
+ * @dev: pointer to parent device
+ * @regmap: regmap for register read/write
+ * @mdf: mdf common data pointer
+ * @node: serial interface node handle
+ * @sck: sitf clock handle
+ * @base: sitf registers base cpu addr
+ * @lock: sitf state manegement lock
+ * @refcnt: sitf usage reference counter
+ * @scksrc: sitf clock source
+ * @id: serial interface index
+ * @mode: sitf mode
+ * @sitfcr: sitf configuration register backup
+ */
+struct stm32_mdf_sitf {
+ struct list_head entry;
+ struct device *dev;
+ struct regmap *regmap;
+ struct stm32_mdf *mdf;
+ struct fwnode_handle *node;
+ struct clk *sck;
+ void __iomem *base;
+ spinlock_t lock; /* Manage race conditions on sitf state */
+ unsigned int refcnt;
+ unsigned int scksrc;
+ u32 id;
+ u32 mode;
+ u32 sitfcr;
+};
+
/*
* struct stm32_mdf - STM32 MDF driver common data (for all instances)
+ * @sitf_list: pointer to serial interfaces list
* @filter_list: pointer to filter interfaces list
* @kclk: kernel clock shared by the core and its children
* @fproc: processing clock frequency
@@ -186,6 +230,7 @@
* @nb_interleave: number of interleaved filters
*/
struct stm32_mdf {
+ struct list_head sitf_list;
struct list_head filter_list;
struct clk *kclk;
unsigned long fproc;
@@ -224,3 +269,7 @@ int stm32_mdf_core_lock_kclk_rate(struct stm32_mdf *mdf);
void stm32_mdf_core_unlock_kclk_rate(struct stm32_mdf *mdf);
unsigned long stm32_mdf_core_get_cck(struct stm32_mdf *mdf);
+int stm32_mdf_sitf_init(struct device *dev, struct stm32_mdf *mdf);
+int stm32_mdf_sitf_start(struct stm32_mdf_sitf *sitf);
+int stm32_mdf_sitf_stop(struct stm32_mdf_sitf *sitf);
+
--
2.43.0
^ permalink raw reply related [flat|nested] 22+ messages in thread
* [PATCH v2 05/11] iio: adc: stm32-mdf: add digital filter device support
2026-11-18 15:17 [PATCH v2 00/11] iio: adc: stm32: add mdf support for stm32mp2 Olivier Moysan
` (3 preceding siblings ...)
2026-11-18 15:17 ` [PATCH v2 04/11] iio: adc: stm32-mdf: add serial interfaces support Olivier Moysan
@ 2026-11-18 15:17 ` 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
` (5 subsequent siblings)
10 siblings, 1 reply; 22+ messages in thread
From: Olivier Moysan @ 2026-11-18 15:17 UTC (permalink / raw)
To: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko,
Rob Herring, Krzysztof Kozlowski, Conor Dooley, Maxime Coquelin,
Alexandre Torgue, Philipp Zabel, Sumit Semwal,
Christian König, Philipp Stanner, Arnaud Pouliquen,
Liam Girdwood, Mark Brown, Jaroslav Kysela, Takashi Iwai,
Fabrice Gasnier
Cc: linux-iio, devicetree, linux-stm32, linux-arm-kernel,
linux-kernel, linux-media, dri-devel, linaro-mm-sig, linux-sound,
Olivier Moysan
The digital filters ensure the decimation of the bitstream signal
received from serial interfaces, through a bitstream matrix (BSMX).
Introduce the digital filter driver structures, register map and platform
driver as the foundation for analog conversion and digital microphone
support.
Manage configuration of digital filters such as the CIC mode, reshape
filter bypass, high-pass cutoff, synchronization, interleave and
acquisition delay.
Manage also connection beteween filters and serial interfaces through
BSMX.
Signed-off-by: Olivier Moysan <olivier.moysan@foss.st.com>
---
drivers/iio/adc/Kconfig | 15 ++
drivers/iio/adc/Makefile | 1 +
drivers/iio/adc/stm32-mdf-adc.c | 514 ++++++++++++++++++++++++++++++++++++++++
3 files changed, 530 insertions(+)
diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
index 6bd46c79d6d6..c3e9885af22d 100644
--- a/drivers/iio/adc/Kconfig
+++ b/drivers/iio/adc/Kconfig
@@ -1753,6 +1753,21 @@ config STM32_MDF_CORE
This driver can also be built as a module. If so, the module
will be called stm32-mdf.
+config STM32_MDF_ADC
+ tristate "STMicroelectronics STM32 MDF adc"
+ depends on (ARCH_STM32 && OF) || COMPILE_TEST
+ depends on COMMON_CLK
+ select STM32_MDF_CORE
+ select IIO_BUFFER
+ select IIO_BUFFER_HW_CONSUMER
+ select IIO_BACKEND
+ help
+ Select this option to support ADCSigma delta modulator for
+ STMicroelectronics STM32 Multi-function digital filter.
+
+ This driver can also be built as a module. If so, the module
+ will be called stm32-mdf-adc.
+
config STMPE_ADC
tristate "STMicroelectronics STMPE ADC driver"
depends on OF && MFD_STMPE
diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile
index 811ef85a88fb..028f33f3d15a 100644
--- a/drivers/iio/adc/Makefile
+++ b/drivers/iio/adc/Makefile
@@ -146,6 +146,7 @@ obj-$(CONFIG_STM32_ADC_CORE) += stm32-adc-core.o
obj-$(CONFIG_STM32_ADC) += stm32-adc.o
obj-$(CONFIG_STM32_DFSDM_ADC) += stm32-dfsdm-adc.o
obj-$(CONFIG_STM32_DFSDM_CORE) += stm32-dfsdm-core.o
+obj-$(CONFIG_STM32_MDF_ADC) += stm32-mdf-adc.o
obj-$(CONFIG_STM32_MDF_CORE) += stm32-mdf.o
stm32-mdf-y := stm32-mdf-core.o stm32-mdf-serial.o
obj-$(CONFIG_STMPE_ADC) += stmpe-adc.o
diff --git a/drivers/iio/adc/stm32-mdf-adc.c b/drivers/iio/adc/stm32-mdf-adc.c
new file mode 100644
index 000000000000..411af78911c9
--- /dev/null
+++ b/drivers/iio/adc/stm32-mdf-adc.c
@@ -0,0 +1,514 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * This file is part of STM32 MDF driver
+ *
+ * Copyright (C) 2023-2026, STMicroelectronics - All Rights Reserved
+ */
+
+#include <linux/clk.h>
+#include <linux/clk-provider.h>
+#include <linux/dmaengine.h>
+#include <linux/dma-mapping.h>
+#include <linux/iio/backend.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/hw-consumer.h>
+#include <linux/iio/sysfs.h>
+#include <linux/iio/trigger_consumer.h>
+#include <linux/iio/triggered_buffer.h>
+#include <linux/interrupt.h>
+#include <linux/jiffies.h>
+#include <linux/module.h>
+#include <linux/of_device.h>
+#include <linux/of_platform.h>
+#include <linux/platform_device.h>
+#include <linux/regmap.h>
+
+#include "stm32-mdf.h"
+
+#define MDF_DMA_BUFFER_SIZE (4 * PAGE_SIZE)
+#define STM32_MDF_ITF_MAX 8
+#define STM32_MDF_DATA_RES 24
+#define STM32_MDF_HPF_BYPASS -1
+#define STM32_MDF_TIMEOUT_MS msecs_to_jiffies(100)
+/*
+ * Choose a default sampling ratio supported for all filter orders with RSFLT active.
+ * 32 is the maximum decimation ratio for filter order 5, with RSFLT active.
+ */
+#define MDF_DEFAULT_DECIM_RATIO 32
+
+#define MDF_IS_FILTER0(adc) (!((adc)->fl_id))
+#define MDF_IS_INTERLEAVED_FILT(adc) ((adc)->interleaved)
+#define MDF_IS_INTERLEAVED_FILT_NOT_0(adc) ({ typeof(adc) x = (adc);\
+ MDF_IS_INTERLEAVED_FILT(x) && !MDF_IS_FILTER0(x); })
+
+#define STM32_MDF_ERR_SCK_FREQ BIT(1)
+#define STM32_MDF_ERR_MODE_RATIO BIT(2)
+#define STM32_MDF_ERR_DECIM_RATIO BIT(3)
+
+struct stm32_mdf_dev_data {
+ int type;
+ int (*init)(struct device *dev, struct iio_dev *indio_dev);
+};
+
+struct stm32_mdf_adc_chan {
+ const char *channel_name;
+ struct iio_backend *backend;
+};
+
+/*
+ * struct stm32_mdf_adc - STM32 MDF ADC private data
+ * @entry: pointer to serial interfaces list
+ * @dev: pointer to filter device
+ * @mdf: pointer to mdf common data
+ * @regmap: regmap pointer for register read/write
+ * @node: pointer to filter node
+ * @dma_chan: filter dma channel pointer
+ * @channels: pointer to channel descriptors array
+ * @dev_data: mdf device data pointer
+ * @sitf: pointer to serial interface feeding the filter
+ * @completion: completion for conversion
+ * @dma_buf: physical dma address
+ * @phys_addr: mdf physical address
+ * @cb: iio consumer callback function pointer
+ * @cb_priv: pointer to consumer private structure
+ * @sck_freq: serial interface frequency
+ * @requested_sample_freq: requested sample frequency from IIO sysfs or from audio stream
+ * @sample_freq: actual sampling frequency
+ * @fl_id: filter index
+ * @decim_ratio: total decimation ratio
+ * @decim_cic: CIC filter decimation ratio
+ * @stu: settling time in micro seconds
+ * @nbdis: number of samples to discard
+ * @bufi: dma buffer current position
+ * @buf_sz: dma buffer size
+ * @buffer: buffer pointer for raw conversion
+ * @dflt_max: dflt maximum output
+ * @cicmode: cic filter order
+ * @hpf_cutoff: high pass filter cut-off frequency
+ * @delay: microphone delay
+ * @datsrc: data source path
+ * @rx_buf: dma buffer pointer
+ * @rsflt_bypass: reshape filter bypass flag
+ * @synced: synchronous flag
+ * @trgo: TRGO trigger flag
+ * @interleaved: interleave flag
+ */
+struct stm32_mdf_adc {
+ struct list_head entry;
+ struct device *dev;
+ struct stm32_mdf *mdf;
+ struct regmap *regmap;
+ struct fwnode_handle *node;
+ struct dma_chan *dma_chan;
+ struct stm32_mdf_adc_chan *channels;
+ const struct stm32_mdf_dev_data *dev_data;
+ struct stm32_mdf_sitf *sitf;
+ struct completion completion;
+ dma_addr_t dma_buf;
+ phys_addr_t phys_addr;
+ int (*cb)(const void *data, size_t size, void *cb_priv);
+ void *cb_priv;
+ unsigned long sck_freq;
+ unsigned long requested_sample_freq;
+ unsigned long sample_freq;
+ unsigned int fl_id;
+ unsigned int decim_ratio;
+ unsigned int decim_cic;
+ unsigned int stu;
+ unsigned int nbdis;
+ unsigned int bufi;
+ unsigned int buf_sz;
+ unsigned int dflt_max;
+ u32 *buffer;
+ u32 cicmode;
+ u32 hpf_cutoff;
+ u32 delay;
+ u32 datsrc;
+ u8 *rx_buf;
+ bool rsflt_bypass;
+ bool synced;
+ bool trgo;
+ bool interleaved;
+};
+
+struct stm32_mdf_scales {
+ unsigned int scale;
+ int gain_db;
+ int gain_lin;
+};
+
+struct stm32_mdf_log10 {
+ unsigned int raw;
+ unsigned int log;
+};
+
+enum stm32_mdf_converter_type {
+ STM32_MDF_AUDIO,
+ STM32_MDF_IIO,
+};
+
+enum stm32_mdf_data_src_type {
+ STM32_MDF_DATSRC_BSMX,
+ STM32_MDF_DATSRC_UNSUPPORTED,
+ STM32_MDF_DATSRC_ADCITF1,
+ STM32_MDF_DATSRC_ADCITF2,
+ STM32_MDF_DATSRC_NB,
+};
+
+enum stm32_mdf_acq_mode {
+ STM32_MDF_ACQ_MODE_ASYNC_CONT,
+ STM32_MDF_ACQ_MODE_ASYNC_SINGLE_SHOT,
+ STM32_MDF_ACQ_MODE_SYNC_CONT,
+ STM32_MDF_ACQ_MODE_SYNC_SINGLE_SHOT,
+ STM32_MDF_ACQ_MODE_WINDOW_CONT,
+ STM32_MDF_ACQ_MODE_SYNC_SNAPSHOT,
+ STM32_MDF_ACQ_MODE_NB,
+};
+
+enum stm32_trig_type {
+ STM32_MDF_TRGSRC_TRGO,
+ STM32_MDF_TRGSRC_OLD,
+ STM32_MDF_TRGSRC_EXT,
+ STM32_MDF_TRGSRC_NB,
+};
+
+static const unsigned int stm32_mdf_hpf_cutoff_ratio[] = {
+ 625, 1250, 2500, 9500
+};
+
+/*
+ * The CIC output data resolution cannot exceed 26 bits.
+ * Output data resolution: D = N * ln(D) / ln(2) + 1 (for serial interface data),
+ * where N is filter order and D the CIC decimation factor.
+ * Following table gives the maximum decimation ratio for filter order [0..5].
+ */
+static bool stm32_mdf_adc_readable_reg(struct device *dev, unsigned int reg)
+{
+ switch (reg) {
+ case MDF_BSMXCR_REG:
+ case MDF_DFLTCR_REG:
+ case MDF_DFLTCICR_REG:
+ case MDF_DFLTRSFR_REG:
+ case MDF_DFLTINTR_REG:
+ case MDF_OLDCR_REG:
+ case MDF_OLDTHLR_REG:
+ case MDF_OLDTHHR_REG:
+ case MDF_DLYCR_REG:
+ case MDF_SCDCR_REG:
+ case MDF_DFLTIER_REG:
+ case MDF_DFLTISR_REG:
+ case MDF_OECCR_REG:
+ case MDF_SNPSDR:
+ case MDF_DFLTDR_REG:
+ return true;
+ default:
+ return false;
+ }
+}
+
+static bool stm32_mdf_adc_volatile_reg(struct device *dev, unsigned int reg)
+{
+ /*
+ * In MDF_DFLTCR_REG register only DFLTACTIVE & DFLTRUN bits are volatile.
+ * MDF_DFLTCR_REG is not marked as volatible to ease the suspend/resume case, and benefit
+ * from the regcache API. Access to volatile bits is managed specifically instead.
+ */
+ switch (reg) {
+ case MDF_DFLTISR_REG:
+ case MDF_SNPSDR:
+ case MDF_DFLTDR_REG:
+ return true;
+ default:
+ return false;
+ }
+}
+
+static bool stm32_mdf_adc_writeable_reg(struct device *dev, unsigned int reg)
+{
+ switch (reg) {
+ case MDF_BSMXCR_REG:
+ case MDF_DFLTCR_REG:
+ case MDF_DFLTCICR_REG:
+ case MDF_DFLTRSFR_REG:
+ case MDF_DFLTINTR_REG:
+ case MDF_OLDCR_REG:
+ case MDF_OLDTHLR_REG:
+ case MDF_OLDTHHR_REG:
+ case MDF_DLYCR_REG:
+ case MDF_SCDCR_REG:
+ case MDF_DFLTIER_REG:
+ case MDF_DFLTISR_REG:
+ case MDF_OECCR_REG:
+ return true;
+ default:
+ return false;
+ }
+}
+
+static const struct regmap_config stm32_mdf_regmap_cfg = {
+ .reg_bits = 32,
+ .val_bits = 32,
+ .reg_stride = sizeof(u32),
+ .max_register = MDF_DFLTDR_REG,
+ .readable_reg = stm32_mdf_adc_readable_reg,
+ .volatile_reg = stm32_mdf_adc_volatile_reg,
+ .writeable_reg = stm32_mdf_adc_writeable_reg,
+ .num_reg_defaults_raw = MDF_DFLTDR_REG / sizeof(u32) + 1,
+ .cache_type = REGCACHE_FLAT,
+ .fast_io = true,
+};
+
+static struct stm32_mdf_adc *stm32_mdf_get_filter_by_handle(struct stm32_mdf *mdf,
+ struct fwnode_handle *node)
+{
+ struct stm32_mdf_adc *adc;
+
+ /* Look for filter data from filter node handle */
+ list_for_each_entry(adc, &mdf->filter_list, entry) {
+ if (adc->node == node)
+ return adc;
+ }
+ return NULL;
+}
+
+static int stm32_mdf_get_sitf(struct device *dev, struct stm32_mdf_adc *adc,
+ struct fwnode_handle *sitf_node)
+{
+ struct stm32_mdf_sitf *sitf;
+
+ /* Look for sitf interface from node handle */
+ list_for_each_entry(sitf, &adc->mdf->sitf_list, entry) {
+ if (sitf->node == sitf_node) {
+ adc->sitf = sitf;
+ break;
+ }
+ }
+
+ if (!adc->sitf) {
+ dev_err(dev, "Serial interface not configured\n");
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+static int stm32_mdf_get_filters_config(struct device *dev, struct stm32_mdf_adc *adc)
+{
+ int i, ret;
+ u32 val;
+
+ ret = device_property_read_u32(dev, "st,cic-mode", &val);
+ if (ret) {
+ dev_err(dev, "Could not get cic filter mode: %d\n", ret);
+ return ret;
+ }
+ adc->cicmode = val;
+
+ adc->rsflt_bypass = device_property_present(dev, "st,rs-filter-bypass");
+
+ adc->hpf_cutoff = STM32_MDF_HPF_BYPASS;
+ if (device_property_present(dev, "st,hpf-filter-cutoff-bp")) {
+ ret = device_property_read_u32(dev, "st,hpf-filter-cutoff-bp", &val);
+ if (ret) {
+ dev_err(dev, "Could not read HPF cut-off frequency: %d\n", ret);
+ return ret;
+ }
+
+ for (i = 0; i < ARRAY_SIZE(stm32_mdf_hpf_cutoff_ratio); i++) {
+ if (stm32_mdf_hpf_cutoff_ratio[i] == val) {
+ adc->hpf_cutoff = i;
+ break;
+ }
+ }
+
+ if (adc->hpf_cutoff == STM32_MDF_HPF_BYPASS) {
+ dev_err(dev, "Unknown HPF cut-off frequency ratio: %d\n", val);
+ return -EINVAL;
+ }
+ }
+
+ dev_dbg(dev, "Filter [%d] config: cic mode [%d], rsflt [%s], hpf [%s]\n", adc->fl_id,
+ adc->cicmode, adc->rsflt_bypass ? "off" : "on",
+ adc->hpf_cutoff == STM32_MDF_HPF_BYPASS ? "off" : "on");
+
+ return 0;
+}
+
+static int stm32_mdf_adc_parse_of(struct platform_device *pdev, struct stm32_mdf_adc *adc)
+{
+ struct device *dev = &pdev->dev;
+ struct stm32_mdf_adc *adcm;
+ struct fwnode_handle *sitf_node;
+ struct fwnode_handle *filt_node;
+ struct fwnode_reference_args args;
+ int ret, stream, buf_size = 1;
+ u32 idx, bsmx, val;
+
+ ret = device_property_read_u32(dev, "reg", &idx);
+ if (ret) {
+ dev_err(dev, "Could not get filter index: %d\n", ret);
+ return ret;
+ }
+
+ if ((idx - 4) % 0x80) {
+ dev_err(dev, "Unexpected reg property value [%x]\n", idx);
+ return -EINVAL;
+ }
+
+ adc->fl_id = (idx >> 7) - 1;
+ if (adc->fl_id >= adc->mdf->nbf) {
+ dev_err(dev, "Wrong filter index [%d]\n", adc->fl_id);
+ return -EINVAL;
+ }
+ adc->node = dev_fwnode(dev);
+
+ if (device_property_present(&pdev->dev, "st,sync")) {
+ filt_node = fwnode_find_reference(dev_fwnode(dev), "st,sync", 0);
+ if (IS_ERR(filt_node)) {
+ dev_err(dev, "Failed to get filter sync handle %ld\n", PTR_ERR(filt_node));
+ return PTR_ERR(filt_node);
+ }
+ adc->synced = true;
+
+ adcm = stm32_mdf_get_filter_by_handle(adc->mdf, filt_node);
+ fwnode_handle_put(filt_node);
+ if (!adcm)
+ return dev_err_probe(dev, -EPROBE_DEFER, "Failed to get filter synchro\n");
+
+ /* The Synchronized master filter is the TRGO trigger source */
+ adcm->trgo = true;
+ }
+
+ if (device_property_present(&pdev->dev, "st,delay")) {
+ ret = device_property_read_u32(dev, "st,delay", &val);
+ if (ret) {
+ dev_err(dev, "Could not get filter delay: %d\n", ret);
+ return ret;
+ }
+ adc->delay = val;
+ }
+
+ /*
+ * In interleave mode the filters in range [1..n] share their configuration with filter 0.
+ * In this case, use config from filter 0, instead of parsing DT.
+ */
+ if (!MDF_IS_FILTER0(adc) && adc->fl_id < adc->mdf->nb_interleave) {
+ adc->interleaved = true;
+ } else {
+ ret = stm32_mdf_get_filters_config(dev, adc);
+ if (ret)
+ return ret;
+
+ if (MDF_IS_FILTER0(adc) && adc->mdf->nb_interleave) {
+ /* Filter 0 is the TRGO trigger source in interleave mode */
+ adc->trgo = true;
+ adc->interleaved = true;
+ buf_size = adc->mdf->nb_interleave;
+ }
+ }
+
+ adc->buffer = devm_kcalloc(dev, buf_size, sizeof(u32), GFP_KERNEL);
+ if (!adc->buffer)
+ return -ENOMEM;
+
+ /* Retrieve serial interface */
+ ret = fwnode_property_get_reference_args(dev_fwnode(dev), "st,sitf", NULL, 1, 0, &args);
+ if (ret) {
+ dev_err(dev, "Serial interface node not found: %d\n", ret);
+ return ret;
+ }
+ sitf_node = args.fwnode;
+
+ /* Get stream index */
+ if (args.nargs != 1) {
+ dev_err(dev, "Failed to get stream index in st,sitf property\n");
+ fwnode_handle_put(sitf_node);
+ return -EINVAL;
+ }
+ stream = args.args[0];
+
+ /* Retrieve sitf data from sitf node phanle */
+ ret = stm32_mdf_get_sitf(dev, adc, sitf_node);
+ fwnode_handle_put(sitf_node);
+ if (ret)
+ return ret;
+
+ bsmx = adc->sitf->id * 2 + stream;
+
+ dev_dbg(dev, "Digital filter [%d] linked to sitf [%d]\n", adc->fl_id, adc->sitf->id);
+
+ /* Only support BSMX filter source right now */
+ adc->datsrc = STM32_MDF_DATSRC_BSMX;
+
+ list_add(&adc->entry, &adc->mdf->filter_list);
+
+ /* Configure BSMXCR */
+ regmap_update_bits(adc->regmap, MDF_BSMXCR_REG,
+ MDF_BSMXCR_BSSEL_MASK, FIELD_PREP(MDF_BSMXCR_BSSEL_MASK, bsmx));
+
+ return 0;
+}
+
+static int stm32_mdf_adc_probe(struct platform_device *pdev)
+{
+ struct device *dev = &pdev->dev;
+ struct stm32_mdf_adc *adc;
+ struct iio_dev *iio;
+ struct resource *res;
+ void __iomem *base;
+ int ret;
+
+ iio = devm_iio_device_alloc(dev, sizeof(*adc));
+ if (!iio) {
+ dev_err(dev, "Failed to allocate IIO device\n");
+ return -ENOMEM;
+ }
+ iio->modes = INDIO_DIRECT_MODE;
+
+ adc = iio_priv(iio);
+ adc->mdf = dev_get_drvdata(dev->parent);
+
+ platform_set_drvdata(pdev, iio);
+
+ base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
+ if (IS_ERR(base))
+ return dev_err_probe(dev, PTR_ERR(base), "Failed to get resource\n");
+ adc->phys_addr = res->start;
+
+ adc->regmap = stm32_mdf_regmap_init(dev, adc->mdf->kclk, base, &stm32_mdf_regmap_cfg);
+ if (IS_ERR(adc->regmap))
+ return dev_err_probe(dev, PTR_ERR(adc->regmap), "Failed to init regmap\n");
+
+ ret = stm32_mdf_adc_parse_of(pdev, adc);
+ if (ret < 0)
+ return ret;
+
+ adc->dev = dev;
+
+ return 0;
+}
+
+static void stm32_mdf_adc_remove(struct platform_device *pdev)
+{
+ struct iio_dev *indio_dev = platform_get_drvdata(pdev);
+ struct stm32_mdf_adc *adc = iio_priv(indio_dev);
+
+ if (!MDF_IS_INTERLEAVED_FILT_NOT_0(adc))
+ iio_device_unregister(indio_dev);
+
+ list_del(&adc->entry);
+}
+
+static struct platform_driver stm32_mdf_adc_driver = {
+ .driver = {
+ .name = "stm32-mdf-adc",
+ },
+ .probe = stm32_mdf_adc_probe,
+ .remove = stm32_mdf_adc_remove,
+};
+module_platform_driver(stm32_mdf_adc_driver);
+
+MODULE_DESCRIPTION("STM32 MDF sigma delta ADC");
+MODULE_AUTHOR("Olivier Moysan <olivier.moysan@foss.st.com>");
+MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS("IIO_BACKEND");
--
2.43.0
^ permalink raw reply related [flat|nested] 22+ messages in thread
* [PATCH v2 06/11] iio: adc: stm32-mdf: configure cic filters and sample rates
2026-11-18 15:17 [PATCH v2 00/11] iio: adc: stm32: add mdf support for stm32mp2 Olivier Moysan
` (4 preceding siblings ...)
2026-11-18 15:17 ` [PATCH v2 05/11] iio: adc: stm32-mdf: add digital filter device support Olivier Moysan
@ 2026-11-18 15:17 ` Olivier Moysan
2026-11-18 15:34 ` sashiko-bot
2026-11-18 15:17 ` [PATCH v2 07/11] iio: adc: stm32-mdf: add direct iio conversion support Olivier Moysan
` (4 subsequent siblings)
10 siblings, 1 reply; 22+ messages in thread
From: Olivier Moysan @ 2026-11-18 15:17 UTC (permalink / raw)
To: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko,
Rob Herring, Krzysztof Kozlowski, Conor Dooley, Maxime Coquelin,
Alexandre Torgue, Philipp Zabel, Sumit Semwal,
Christian König, Philipp Stanner, Arnaud Pouliquen,
Liam Girdwood, Mark Brown, Jaroslav Kysela, Takashi Iwai,
Fabrice Gasnier
Cc: linux-iio, devicetree, linux-stm32, linux-arm-kernel,
linux-kernel, linux-media, dri-devel, linaro-mm-sig, linux-sound,
Olivier Moysan
MDF output sample rates depend on the serial clock and the decimation
applied by the CIC and reshape filters. Add the calculations needed to
derive these settings from a requested sampling frequency.
Compute CIC gain and data scaling for the selected filter order and
decimation to maximize output signal dynamic.
Check serial and processing clock constraints, and configure the CCK
rate when needed.
Signed-off-by: Olivier Moysan <olivier.moysan@foss.st.com>
---
drivers/iio/adc/stm32-mdf-adc.c | 404 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 404 insertions(+)
diff --git a/drivers/iio/adc/stm32-mdf-adc.c b/drivers/iio/adc/stm32-mdf-adc.c
index 411af78911c9..eae7a4fa1485 100644
--- a/drivers/iio/adc/stm32-mdf-adc.c
+++ b/drivers/iio/adc/stm32-mdf-adc.c
@@ -182,6 +182,64 @@ static const unsigned int stm32_mdf_hpf_cutoff_ratio[] = {
* where N is filter order and D the CIC decimation factor.
* Following table gives the maximum decimation ratio for filter order [0..5].
*/
+static const unsigned int stm32_mdf_cic_max_decim_sitf[] = {
+512, 512, 512, 322, 76, 32
+};
+
+/* Gain (dB) x 10 according to scale value in hex */
+static const struct stm32_mdf_scales stm32_mdf_scale_table[] = {
+ {0x20, -482, -2558},
+ {0x21, -446, -1706},
+ {0x22, -421, -1280},
+ {0x23, -386, -853},
+ {0x24, -361, -640},
+ {0x25, -326, -427},
+ {0x26, -301, -320},
+ {0x27, -266, -213},
+ {0x28, -241, -160},
+ {0x29, -206, -107},
+ {0x2A, -181, -80},
+ {0x2B, -145, -53},
+ {0x2C, -120, -40},
+ {0x2D, -85, -27},
+ {0x2E, -60, -20},
+ {0x2F, -25, -13},
+ {0x00, 0, 10},
+ {0x01, 35, 15},
+ {0x02, 60, 20},
+ {0x03, 95, 30},
+ {0x04, 120, 40},
+ {0x05, 156, 60},
+ {0x06, 181, 80},
+ {0x07, 216, 120},
+ {0x08, 241, 160},
+ {0x09, 276, 240},
+ {0x0A, 301, 320},
+ {0x0B, 336, 480},
+ {0x0C, 361, 640},
+ {0x0D, 396, 960},
+ {0x0E, 421, 1280},
+ {0x0F, 457, 1920},
+ {0x10, 482, 2560},
+ {0x11, 517, 3840},
+ {0x12, 542, 5120},
+ {0x13, 577, 7680},
+ {0x14, 602, 10240},
+ {0x15, 637, 15360},
+ {0x16, 662, 20480},
+ {0x17, 697, 30720},
+ {0x18, 722, 40960},
+};
+
+/* Prime number 1000 x log10 table */
+static const struct stm32_mdf_log10 stm32_mdf_log_table[] = {
+ {2, 301}, {3, 477}, {5, 699}, {7, 845}, {11, 1041}, {13, 1114}, {17, 1230}, {19, 1279},
+ {23, 1362}, {29, 1462}, {31, 1491}, {37, 1568}, {41, 1613}, {43, 1633}, {47, 1672},
+ {53, 1724}, {59, 1771}, {61, 1785}, {67, 1826}, {71, 1851}, {73, 1863}, {79, 1898},
+ {83, 1919}, {89, 1949}, {97, 1987}, {101, 2004}, {103, 2013}, {107, 2029}, {109, 2037},
+ {113, 2053}, {127, 2104}
+};
+
static bool stm32_mdf_adc_readable_reg(struct device *dev, unsigned int reg)
{
switch (reg) {
@@ -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)
+{
+ unsigned long max = ARRAY_SIZE(stm32_mdf_log_table);
+ unsigned int prime_factors[16];
+ unsigned int num, div, logd = 0;
+ int i, j, scale;
+
+ /* Decompose decimation ratio D, as prime number factors, to compute log10(D) */
+ j = 0;
+ num = decim;
+ while (num > 1) {
+ i = 0;
+ while (i < max) {
+ div = stm32_mdf_log_table[i].raw;
+ if (!(num % div)) {
+ prime_factors[j] = stm32_mdf_log_table[i].log;
+ num = num / div;
+ j++;
+ break;
+ }
+ i++;
+ }
+ if (i == max) {
+ dev_warn(dev, "Failed to set scale. Output signal may saturate.\n");
+ return 0;
+ }
+ }
+
+ for (i = 0; i < j; i++)
+ logd += prime_factors[i];
+
+ /* 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++;
+ }
+ }
+ if (cnt != adc->mdf->nb_interleave - 1) {
+ dev_err(adc->dev, "Interleaved filter number [%d] / expected [%d]\n",
+ cnt, adc->mdf->nb_interleave - 1);
+ return -EINVAL;
+ }
+ }
+
+ /* Configure delay */
+ ret = regmap_update_bits(adc->regmap, MDF_DLYCR_REG, MDF_DLYCR_SKPDLY_MASK, adc->delay);
+ if (ret)
+ return ret;
+
+ /* Configure NBDIS */
+ if (adc->nbdis) {
+ ret = regmap_update_bits(adc->regmap, MDF_DFLTCR_REG, MDF_DFLTCR_NBDIS_MASK,
+ FIELD_PREP(MDF_DFLTCR_NBDIS_MASK, adc->nbdis));
+ if (ret)
+ return ret;
+ }
+
+ /* 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);
+ if (ret)
+ return ret;
+
+ /* Configure RSFR & HPF */
+ if (adc->hpf_cutoff == STM32_MDF_HPF_BYPASS)
+ val = MDF_DFLTRSFR_HPFBYP;
+ else
+ val = FIELD_PREP(MDF_DFLTRSFR_HPFC_MASK, adc->hpf_cutoff);
+ val |= adc->rsflt_bypass ? MDF_DFLTRSFR_RSFLTBYP : 0;
+ msk = MDF_DFLTRSFR_RSFLTBYP | MDF_DFLTRSFR_HPFBYP | MDF_DFLTRSFR_HPFC_MASK;
+
+ return regmap_update_bits(adc->regmap, MDF_DFLTRSFR_REG, msk, val);
+}
+
+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;
+ u64 max;
+ int i, d, log, scale, max_scale, gain_lin;
+
+ if (!adc->rsflt_bypass) {
+ decim_rsflt = 4;
+ data_size -= 2;
+
+ /* Check if total decimation factor is a multiple of reshape filter decimation */
+ if (decim % decim_rsflt) {
+ dev_err(dev, "Total decimation factor [%d] not multiple of [%d]\n",
+ decim, decim_rsflt);
+ return -EINVAL;
+ }
+ }
+
+ decim_cic = DIV_ROUND_CLOSEST(decim, decim_rsflt);
+ if (decim_cic < MDF_DFLTCICR_MCICD_MIN ||
+ decim_cic > stm32_mdf_cic_max_decim_sitf[order]) {
+ dev_err(dev, "Decimation factor [%d] out of range for CIC filter order [%d]\n",
+ decim_cic, adc->cicmode);
+ return -EINVAL;
+ }
+
+ /*
+ * Compute scaling:
+ * max scale = 20 * log10( 2 exp DS / D exp NF )
+ * - DS = max data size at scale output (RSFLT on: DS = 22 / RSFLT off: DS = 24)
+ * - NF = Main CIC filter order
+ */
+ 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);
+ }
+
+ dev_dbg(dev, "Filter order [%d], decimation [%d], data size [%d], max scale [%d]\n",
+ order, decim_cic, data_size, max_scale / 1000);
+
+ /*
+ * Find scale register setting.
+ * Limit max_scale accuracy to first decimal for comparison with scale table values.
+ */
+ max_scale = DIV_ROUND_CLOSEST(max_scale, 100);
+ i = ARRAY_SIZE(stm32_mdf_scale_table) - 1;
+ while (i > 0) {
+ if (stm32_mdf_scale_table[i].gain_db < max_scale)
+ break;
+ i--;
+ };
+ scale = stm32_mdf_scale_table[i].scale;
+ gain_lin = stm32_mdf_scale_table[i].gain_lin;
+
+ dev_dbg(dev, "Set scale to [%d]dB: [0x%x]\n", stm32_mdf_scale_table[i].gain_db / 10, scale);
+
+ adc->decim_cic = decim_cic;
+
+ /*
+ * Calculate maximum DFLT output filter
+ * max = K * G
+ * - Fastsinc (order 0): G = 2 * d^2 * gain_lin
+ * - Sinc order 1 to 5: G = d^N * gain_lin
+ * - RSFLT off: K = 1, d = decim
+ * - RSFLT on: K = 2.98, d = decim_cic
+ * N = CIC filter order, decim = total decimation ratio, decim_cic = CIC decimation ratio
+ * gain_lin is multiplied by a 10 factor in stm32_mdf_scale_table, and K with a 100 factor.
+ */
+ if (adc->rsflt_bypass) {
+ d = decim;
+ max = 100;
+ } else {
+ d = decim_cic;
+ max = 298;
+ }
+
+ if (order) {
+ i = 0;
+ while (i < order) {
+ max *= d;
+ i++;
+ }
+ } else {
+ max *= 2 * d * d;
+ }
+
+ if (gain_lin > 0) {
+ max *= gain_lin;
+ max = DIV_ROUND_CLOSEST_ULL(max, 1000);
+ }
+ if (gain_lin < 0) {
+ max = DIV_ROUND_CLOSEST_ULL(max, -gain_lin);
+ max = DIV_ROUND_CLOSEST_ULL(max, 10);
+ }
+
+ adc->dflt_max = max;
+
+ dev_dbg(dev, "DFLT maximum output [%d]\n", adc->dflt_max);
+
+ return stm32_mdf_adc_apply_filters_config(adc, scale);
+}
+
+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;
+
+ /*
+ * The CCK rate is requested by its consumer through the clock framework.
+ * The serial interface clock sck (aka cck) is derived from MDF kernel clock frequency.
+ * The kernel clock rate may have been changed by another consumer, or MDF dividers
+ * may have been updated through clk_get_rate() call.
+ * Ensure that kernel clock rate and MDF dividers, still provide the right sck/cck rate
+ */
+ cck_expected_freq = stm32_mdf_core_get_cck(adc->mdf);
+ if (*sck_freq != cck_expected_freq) {
+ dev_dbg(adc->dev, "Wrong sck_freq [%lu]Hz. Expected [%lu]Hz.\n",
+ *sck_freq, cck_expected_freq);
+ error |= STM32_MDF_ERR_SCK_FREQ;
+ }
+
+ /*
+ * 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;
+
+ /* 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;
+
+err:
+ dev_err(adc->dev, "Wrong clock configuration [0x%x]. Clock recovering returned [%d]\n",
+ error, ret);
+
+ return ret;
+}
+
+static int __maybe_unused mdf_adc_set_samp_freq(struct iio_dev *indio_dev)
+{
+ struct stm32_mdf_adc *adc = iio_priv(indio_dev);
+ struct device *dev = &indio_dev->dev;
+ unsigned int decim_ratio;
+ unsigned long delta, delta_ppm, sck_freq, sample_freq = adc->requested_sample_freq;
+ u64 nbdis;
+ int ret;
+
+ sck_freq = clk_get_rate(adc->sitf->sck);
+ if (!sck_freq) {
+ dev_err(dev, "Unexpected serial clock frequency: 0Hz\n");
+ ret = -EINVAL;
+ goto err;
+ }
+
+ /*
+ * In analog use cases, the sampling frequency may not be already set in IIO sysfs.
+ * (In audio use cases, the sampling frequency is always provided on stream startup)
+ * If requested sampling frequency is 0, set a default frequency.
+ * The default frequency is computed from the default decimation ratio.
+ * This ensures that a filter configuration can be found whatever the selected filter
+ * order. (Most constrained case is order 5)
+ */
+ 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);
+ else if (delta)
+ dev_dbg(dev, "Sample rate deviation [%lu] ppm: [%lu] vs [%lu] Hz\n",
+ delta_ppm, sck_freq / decim_ratio, sample_freq);
+
+ /*
+ * Convert settling time into a minimum number of output sample to discard before releasing
+ * the data. This allows to drop the samples affected by the impulse response of the
+ * filter and wait for stable data.
+ */
+ nbdis = DIV_ROUND_UP_ULL((u64)adc->stu * sample_freq, 1000000);
+ if (nbdis > MDF_DFLTCR_NBDIS_MAX) {
+ dev_warn(dev, "NBDIS [%llu] too large. Force to [%lu]\n",
+ nbdis, MDF_DFLTCR_NBDIS_MAX);
+ adc->nbdis = MDF_DFLTCR_NBDIS_MAX;
+ } else {
+ adc->nbdis = nbdis;
+ dev_dbg(dev, "Settling time [%u] us. NBDIS set to [%u] samples\n",
+ adc->stu, adc->nbdis);
+ }
+
+ ret = stm32_mdf_adc_set_filters_config(indio_dev, decim_ratio);
+ if (ret < 0)
+ goto err;
+
+ adc->sample_freq = DIV_ROUND_CLOSEST(sck_freq, decim_ratio);
+ adc->decim_ratio = decim_ratio;
+
+err:
+ return ret;
+}
+
static int stm32_mdf_get_sitf(struct device *dev, struct stm32_mdf_adc *adc,
struct fwnode_handle *sitf_node)
{
--
2.43.0
^ permalink raw reply related [flat|nested] 22+ messages in thread
* [PATCH v2 07/11] iio: adc: stm32-mdf: add direct iio conversion support
2026-11-18 15:17 [PATCH v2 00/11] iio: adc: stm32: add mdf support for stm32mp2 Olivier Moysan
` (5 preceding siblings ...)
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:17 ` 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
` (3 subsequent siblings)
10 siblings, 1 reply; 22+ messages in thread
From: Olivier Moysan @ 2026-11-18 15:17 UTC (permalink / raw)
To: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko,
Rob Herring, Krzysztof Kozlowski, Conor Dooley, Maxime Coquelin,
Alexandre Torgue, Philipp Zabel, Sumit Semwal,
Christian König, Philipp Stanner, Arnaud Pouliquen,
Liam Girdwood, Mark Brown, Jaroslav Kysela, Takashi Iwai,
Fabrice Gasnier
Cc: linux-iio, devicetree, linux-stm32, linux-arm-kernel,
linux-kernel, linux-media, dri-devel, linaro-mm-sig, linux-sound,
Olivier Moysan
Add single-conversion acquisition and the IIO accessors for raw samples,
scale, offset, sampling frequency and channel labels. Obtain analog scale
and offset information from the associated IIO backend.
Coordinate the MDF core, serial interface and digital filters when starting
and stopping conversions.
Initialize channel and analog IIO backends.
Signed-off-by: Olivier Moysan <olivier.moysan@foss.st.com>
---
drivers/iio/adc/stm32-mdf-adc.c | 585 +++++++++++++++++++++++++++++++++++++++-
1 file changed, 583 insertions(+), 2 deletions(-)
diff --git a/drivers/iio/adc/stm32-mdf-adc.c b/drivers/iio/adc/stm32-mdf-adc.c
index eae7a4fa1485..7361ce6182be 100644
--- a/drivers/iio/adc/stm32-mdf-adc.c
+++ b/drivers/iio/adc/stm32-mdf-adc.c
@@ -329,6 +329,79 @@ static struct stm32_mdf_adc *stm32_mdf_get_filter_by_handle(struct stm32_mdf *md
return NULL;
}
+static int stm32_mdf_adc_start_filter(struct stm32_mdf_adc *adc)
+{
+ struct stm32_mdf_adc *adc_inter;
+ struct stm32_mdf *mdf = adc->mdf;
+ u32 val;
+
+ if (MDF_IS_FILTER0(adc))
+ list_for_each_entry(adc_inter, &mdf->filter_list, entry)
+ if (MDF_IS_INTERLEAVED_FILT_NOT_0(adc_inter))
+ stm32_mdf_adc_start_filter(adc_inter);
+
+ /* Check filter status. Bypass cache to access volatile MDF_DFLTCR_ACTIVE bit */
+ regcache_cache_bypass(adc->regmap, true);
+ regmap_read(adc->regmap, MDF_DFLTCR_REG, &val);
+ regcache_cache_bypass(adc->regmap, false);
+ if (val & MDF_DFLTCR_ACTIVE) {
+ dev_err(adc->dev, "Filter [%d] is already running\n", adc->fl_id);
+ return -EBUSY;
+ }
+
+ return regmap_set_bits(adc->regmap, MDF_DFLTCR_REG, MDF_DFLTCR_DFLTEN);
+}
+
+static void stm32_mdf_adc_stop_filter(struct stm32_mdf_adc *adc)
+{
+ struct stm32_mdf_adc *adc_inter;
+ struct stm32_mdf *mdf = adc->mdf;
+
+ regmap_clear_bits(adc->regmap, MDF_DFLTCR_REG, MDF_DFLTCR_DFLTEN);
+
+ if (MDF_IS_FILTER0(adc))
+ list_for_each_entry(adc_inter, &mdf->filter_list, entry)
+ if (MDF_IS_INTERLEAVED_FILT_NOT_0(adc_inter))
+ stm32_mdf_adc_stop_filter(adc_inter);
+}
+
+static int stm32_mdf_adc_filter_set_mode(struct stm32_mdf_adc *adc, bool cont)
+{
+ struct stm32_mdf_adc *adc_inter;
+ u32 mode;
+
+ if (MDF_IS_FILTER0(adc)) {
+ list_for_each_entry(adc_inter, &adc->mdf->filter_list, entry) {
+ if (MDF_IS_INTERLEAVED_FILT_NOT_0(adc_inter))
+ stm32_mdf_adc_filter_set_mode(adc_inter, cont);
+ }
+ }
+
+ if (adc->synced || MDF_IS_INTERLEAVED_FILT(adc)) {
+ if (cont)
+ mode = STM32_MDF_ACQ_MODE_SYNC_CONT;
+ else
+ mode = STM32_MDF_ACQ_MODE_SYNC_SINGLE_SHOT;
+ } else {
+ if (cont)
+ mode = STM32_MDF_ACQ_MODE_ASYNC_CONT;
+ else
+ /*
+ * The expected mode would be STM32_MDF_ACQ_MODE_ASYNC_SINGLE_SHOT here.
+ * However, there is an instability depending on "bus/kernel clock ratio"
+ * in this mode.
+ * Use STM32_MDF_ACQ_MODE_SYNC_SINGLE_SHOT mode along with TRGO trigger,
+ * as a workaround.
+ */
+ mode = STM32_MDF_ACQ_MODE_SYNC_SINGLE_SHOT;
+ }
+
+ dev_dbg(adc->dev, "Set mode [0x%x] on filter [%d]\n", mode, adc->fl_id);
+
+ return regmap_update_bits(adc->regmap, MDF_DFLTCR_REG,
+ MDF_DFLTCR_ACQMOD_MASK, FIELD_PREP(MDF_DFLTCR_ACQMOD_MASK, mode));
+}
+
static int stm32_mdf_adc_compute_scale(struct device *dev, unsigned int decim,
unsigned int order, unsigned int data_size)
{
@@ -606,7 +679,7 @@ static int stm32_mdf_adc_check_clock_config(struct stm32_mdf_adc *adc, unsigned
return ret;
}
-static int __maybe_unused mdf_adc_set_samp_freq(struct iio_dev *indio_dev)
+static int mdf_adc_set_samp_freq(struct iio_dev *indio_dev)
{
struct stm32_mdf_adc *adc = iio_priv(indio_dev);
struct device *dev = &indio_dev->dev;
@@ -675,6 +748,483 @@ static int __maybe_unused mdf_adc_set_samp_freq(struct iio_dev *indio_dev)
return ret;
}
+static int stm32_mdf_adc_start_mdf(struct iio_dev *indio_dev)
+{
+ struct stm32_mdf_adc *adc = iio_priv(indio_dev);
+ int ret;
+
+ ret = clk_prepare_enable(adc->sitf->sck);
+ if (ret < 0) {
+ dev_err(&indio_dev->dev, "Failed to enable clock %s\n",
+ __clk_get_name(adc->sitf->sck));
+ return ret;
+ }
+
+ ret = mdf_adc_set_samp_freq(indio_dev);
+ if (ret < 0)
+ goto err;
+
+ ret = stm32_mdf_core_start_mdf(adc->mdf);
+ if (ret < 0)
+ goto err;
+
+ return 0;
+
+err:
+ clk_disable_unprepare(adc->sitf->sck);
+
+ return ret;
+}
+
+static void stm32_mdf_adc_stop_mdf(struct iio_dev *indio_dev)
+{
+ struct stm32_mdf_adc *adc = iio_priv(indio_dev);
+
+ stm32_mdf_core_stop_mdf(adc->mdf);
+
+ clk_disable_unprepare(adc->sitf->sck);
+}
+
+static int stm32_mdf_adc_start_conv(struct iio_dev *indio_dev)
+{
+ struct stm32_mdf_adc *adc = iio_priv(indio_dev);
+ int ret;
+
+ ret = stm32_mdf_sitf_start(adc->sitf);
+ if (ret < 0)
+ return ret;
+
+ ret = stm32_mdf_adc_start_filter(adc);
+ if (ret < 0)
+ goto stop_sitf;
+
+ if (adc->trgo) {
+ ret = stm32_mdf_core_trigger(adc->mdf);
+ if (ret < 0)
+ goto stop_filter;
+ }
+
+ return 0;
+
+stop_filter:
+ stm32_mdf_adc_stop_filter(adc);
+stop_sitf:
+ stm32_mdf_sitf_stop(adc->sitf);
+
+ return ret;
+}
+
+static void stm32_mdf_adc_stop_conv(struct iio_dev *indio_dev)
+{
+ struct stm32_mdf_adc *adc = iio_priv(indio_dev);
+
+ stm32_mdf_adc_stop_filter(adc);
+
+ stm32_mdf_sitf_stop(adc->sitf);
+}
+
+static int stm32_mdf_channel_parse_of(struct iio_dev *indio_dev, struct fwnode_handle *node,
+ struct iio_chan_spec *ch)
+{
+ struct stm32_mdf_adc *adc = iio_priv(indio_dev);
+ struct iio_backend *backend;
+ const char *label = fwnode_get_name(node);
+ int ret;
+ u32 stu = 0;
+
+ ret = fwnode_property_read_u32(node, "reg", &ch->channel);
+ if (ret < 0) {
+ dev_err(&indio_dev->dev, "Failed to read channel index: [%d]\n", ret);
+ return ret;
+ }
+
+ if (fwnode_property_present(node, "label")) {
+ /* label is optional */
+ ret = fwnode_property_read_string(node, "label", &label);
+ if (ret < 0) {
+ dev_err(&indio_dev->dev,
+ " Error parsing 'label' for idx %d\n", ch->channel);
+ return ret;
+ }
+ }
+ adc->channels[ch->scan_index].channel_name = label;
+
+ /* settling-time-us is optional */
+ if (fwnode_property_present(node, "settling-time-us")) {
+ /*
+ * The settling time is relevant only for the first channel. In case of interleaved
+ * channels, the settling time of the first configured channel, is applied to other
+ * channels.
+ */
+ if (!ch->scan_index) {
+ ret = fwnode_property_read_u32(node, "settling-time-us", &stu);
+ if (ret < 0) {
+ dev_err(&indio_dev->dev, "Failed to read settling time: [%d]\n",
+ ret);
+ return ret;
+ }
+
+ adc->stu = stu;
+ }
+ }
+
+ if (adc->dev_data->type == STM32_MDF_IIO) {
+ backend = devm_iio_backend_fwnode_get(&indio_dev->dev, NULL, node);
+ if (IS_ERR(backend))
+ return dev_err_probe(&indio_dev->dev, PTR_ERR(backend),
+ "Failed to get backend\n");
+ adc->channels[ch->scan_index].backend = backend;
+ }
+
+ return ret;
+}
+
+static int stm32_mdf_adc_chan_init_one(struct iio_dev *indio_dev, struct fwnode_handle *node,
+ struct iio_chan_spec *ch, int idx)
+{
+ struct stm32_mdf_adc *adc = iio_priv(indio_dev);
+ int ret;
+
+ ch->type = IIO_VOLTAGE;
+ ch->indexed = 1;
+ ch->scan_index = idx;
+
+ ret = stm32_mdf_channel_parse_of(indio_dev, node, ch);
+ if (ret < 0) {
+ dev_err(&indio_dev->dev, "Failed to parse channel [%d]\n", idx);
+ return ret;
+ }
+
+ if (adc->dev_data->type == STM32_MDF_IIO) {
+ ch->info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE) |
+ BIT(IIO_CHAN_INFO_OFFSET);
+ ch->scan_type.shift = 8;
+ }
+
+ ch->info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ);
+ ch->scan_type.sign = 's';
+ ch->scan_type.realbits = STM32_MDF_DATA_RES;
+ ch->scan_type.storagebits = 32;
+
+ return 0;
+}
+
+static int stm32_mdf_adc_chan_init(struct iio_dev *indio_dev, struct iio_chan_spec *channels)
+{
+ struct fwnode_handle *child;
+ int chan_idx = 0, ret;
+
+ device_for_each_child_node(indio_dev->dev.parent, child) {
+ /* Skip the child nodes with a compatible. (e.g. DAI node for audio) */
+ if (fwnode_property_present(child, "compatible"))
+ continue;
+
+ ret = stm32_mdf_adc_chan_init_one(indio_dev, child, &channels[chan_idx], chan_idx);
+ if (ret < 0) {
+ fwnode_handle_put(child);
+ return dev_err_probe(&indio_dev->dev, ret, "Channels [%d] init failed\n",
+ chan_idx);
+ }
+
+ chan_idx++;
+ }
+
+ return chan_idx;
+}
+
+static int stm32_mdf_adc_single_conv(struct iio_dev *indio_dev,
+ const struct iio_chan_spec *chan, int *res)
+{
+ struct stm32_mdf_adc *adc = iio_priv(indio_dev);
+ long timeout;
+ bool save_trgo = adc->trgo;
+ int ret;
+
+ reinit_completion(&adc->completion);
+
+ ret = stm32_mdf_adc_start_mdf(indio_dev);
+ if (ret < 0)
+ return ret;
+
+ ret = regmap_set_bits(adc->regmap, MDF_DFLTIER_REG, MDF_DFLTIER_FTHIE_MASK);
+ if (ret < 0)
+ goto err_conv;
+
+ stm32_mdf_adc_filter_set_mode(adc, false);
+
+ /* Force trgo as a workaround for STM32_MDF_ACQ_MODE_ASYNC_SINGLE_SHOT mode */
+ adc->trgo = true;
+
+ ret = stm32_mdf_adc_start_conv(indio_dev);
+ if (ret < 0) {
+ regmap_update_bits(adc->regmap, MDF_DFLTIER_REG, MDF_DFLTIER_FTHIE_MASK, 0);
+ goto err_conv;
+ }
+
+ timeout = wait_for_completion_interruptible_timeout(&adc->completion, STM32_MDF_TIMEOUT_MS);
+
+ regmap_clear_bits(adc->regmap, MDF_DFLTIER_REG, MDF_DFLTIER_FTHIE_MASK);
+
+ if (timeout == 0) {
+ dev_err(&indio_dev->dev, "Timeout reached on channel [%d]", chan->channel);
+ ret = -ETIMEDOUT;
+ goto stop_conv;
+ } else if (timeout < 0) {
+ ret = timeout;
+ goto stop_conv;
+ } else {
+ ret = IIO_VAL_INT;
+ }
+
+ if (MDF_IS_INTERLEAVED_FILT(adc))
+ *res = adc->buffer[chan->channel];
+ else
+ *res = adc->buffer[0];
+
+stop_conv:
+ stm32_mdf_adc_stop_conv(indio_dev);
+
+err_conv:
+ adc->trgo = save_trgo;
+
+ stm32_mdf_adc_stop_mdf(indio_dev);
+
+ return ret;
+}
+
+static int stm32_mdf_adc_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
+ int val, int val2, long mask)
+{
+ struct stm32_mdf_adc *adc = iio_priv(indio_dev);
+ int ret = 0;
+
+ if (mask != IIO_CHAN_INFO_SAMP_FREQ || !val)
+ return -EINVAL;
+
+ {
+ IIO_DEV_ACQUIRE_DIRECT_MODE(indio_dev, claim);
+ if (IIO_DEV_ACQUIRE_FAILED(claim))
+ return -EBUSY;
+
+ /* Set requested_sample_freq right now, as used by mdf_adc_set_samp_freq() */
+ adc->requested_sample_freq = val;
+
+ if (adc->dev_data->type == STM32_MDF_IIO) {
+ ret = stm32_mdf_core_lock_kclk_rate(adc->mdf);
+ if (!ret) {
+ ret = mdf_adc_set_samp_freq(indio_dev);
+
+ stm32_mdf_core_unlock_kclk_rate(adc->mdf);
+ }
+ }
+ }
+
+ if (ret)
+ adc->requested_sample_freq = 0;
+
+ return ret;
+}
+
+static int stm32_mdf_adc_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
+ int *val, int *val2, long mask)
+{
+ struct stm32_mdf_adc *adc = iio_priv(indio_dev);
+ int idx = chan->scan_index;
+ int max = BIT(STM32_MDF_DATA_RES - 1) - 1;
+ int scale, offset;
+ int ret;
+
+ switch (mask) {
+ case IIO_CHAN_INFO_RAW: {
+ IIO_DEV_ACQUIRE_DIRECT_MODE(indio_dev, claim);
+ if (IIO_DEV_ACQUIRE_FAILED(claim))
+ return -EBUSY;
+
+ if (adc->channels[idx].backend) {
+ ret = iio_backend_enable(adc->channels[idx].backend);
+ if (ret)
+ return ret;
+ }
+
+ ret = stm32_mdf_adc_single_conv(indio_dev, chan, val);
+
+ if (adc->channels[idx].backend)
+ iio_backend_disable(adc->channels[idx].backend);
+
+ return ret ? ret : IIO_VAL_INT;
+ }
+ case IIO_CHAN_INFO_SAMP_FREQ:
+ *val = adc->sample_freq;
+
+ return IIO_VAL_INT;
+
+ case IIO_CHAN_INFO_SCALE:
+ /*
+ * Vconv = (raw>>shift + offset) * scale.
+ * scale = Vref * k / 2^res (denominator is managed through FRACTIONAL_LOG2 type)
+ * k correspond to the ratio between max resolution and actual filter maximum
+ * k = max / dflt_max
+ * max = 2^(res - 1) - 1
+ * max_dflt = D^N * gain_lin * gain_rsflt
+ * scale = Vref * max / dflt_max
+ */
+ if (adc->channels[idx].backend) {
+ ret = iio_backend_read_scale(adc->channels[idx].backend, chan,
+ &scale, NULL);
+ if (ret < 0)
+ return ret;
+
+ *val = div_u64((u64)scale * max, adc->dflt_max);
+
+ *val2 = chan->scan_type.realbits;
+ if (chan->differential)
+ *val *= 2;
+ } else {
+ return -EPERM;
+ }
+
+ return IIO_VAL_FRACTIONAL_LOG2;
+
+ case IIO_CHAN_INFO_OFFSET:
+ if (adc->channels[idx].backend) {
+ ret = iio_backend_read_offset(adc->channels[idx].backend, chan,
+ &offset, NULL);
+ if (ret < 0)
+ return ret;
+
+ *val = offset;
+ if (!chan->differential)
+ *val += adc->dflt_max;
+ } else {
+ return -EPERM;
+ }
+
+ return IIO_VAL_INT;
+ }
+
+ return -EINVAL;
+}
+
+static int stm32_mdf_adc_read_label(struct iio_dev *indio_dev, const struct iio_chan_spec *chan,
+ char *label)
+{
+ struct stm32_mdf_adc *adc = iio_priv(indio_dev);
+ const char *name = adc->channels[chan->scan_index].channel_name;
+
+ return sysfs_emit(label, "%s\n", name);
+}
+
+static const struct iio_info stm32_mdf_info_adc = {
+ .write_raw = stm32_mdf_adc_write_raw,
+ .read_raw = stm32_mdf_adc_read_raw,
+ .read_label = stm32_mdf_adc_read_label,
+};
+
+static irqreturn_t stm32_mdf_irq(int irq, void *arg)
+{
+ struct iio_dev *indio_dev = arg;
+ struct stm32_mdf_adc *adc = iio_priv(indio_dev), *adc_inter;
+ struct regmap *regmap = adc->regmap;
+ u32 *ptr = adc->buffer;
+ u32 isr, ier, flags;
+
+ regmap_read(regmap, MDF_DFLTISR_REG, &isr);
+ regmap_read(regmap, MDF_DFLTIER_REG, &ier);
+
+ flags = isr & ier;
+ if (!flags)
+ return IRQ_NONE;
+
+ if (flags & MDF_DFLTISR_FTHF_MASK) {
+ /* Reading the data register clear the IRQ status */
+ regmap_read(regmap, MDF_DFLTDR_REG, ptr++);
+
+ if (MDF_IS_FILTER0(adc))
+ list_for_each_entry(adc_inter, &adc->mdf->filter_list, entry)
+ if (MDF_IS_INTERLEAVED_FILT_NOT_0(adc_inter))
+ regmap_read(regmap, MDF_DFLTDR_REG, ptr++);
+
+ complete(&adc->completion);
+ }
+
+ if (flags & MDF_DFLTISR_DOVRF_MASK) {
+ dev_warn(&indio_dev->dev, "Data overflow detected\n");
+ regmap_set_bits(regmap, MDF_DFLTISR_REG, MDF_DFLTISR_DOVRF_MASK);
+ }
+
+ if (flags & MDF_DFLTISR_RFOVRF_MASK) {
+ dev_warn(&indio_dev->dev, "Reshape filter overrun detected\n");
+ regmap_set_bits(regmap, MDF_DFLTISR_REG, MDF_DFLTISR_RFOVRF_MASK);
+ }
+
+ if (flags & MDF_DFLTISR_SATF_MASK) {
+ dev_warn(&indio_dev->dev, "Saturation detected\n");
+ regmap_set_bits(regmap, MDF_DFLTISR_REG, MDF_DFLTISR_SATF_MASK);
+
+ /* Notify only once */
+ regmap_clear_bits(adc->regmap, MDF_DFLTIER_REG, MDF_DFLTIER_SATIE_MASK);
+ }
+
+ return IRQ_HANDLED;
+}
+
+static int stm32_mdf_adc_init(struct device *dev, struct iio_dev *indio_dev)
+{
+ struct stm32_mdf_adc *adc = iio_priv(indio_dev);
+ struct iio_chan_spec *ch = NULL;
+ unsigned int num_ch;
+ int ret;
+
+ num_ch = device_get_child_node_count(indio_dev->dev.parent);
+ if (num_ch) {
+ /* Filter0 may have several channels in interleaved mode */
+ if (num_ch > 1) {
+ if (!MDF_IS_FILTER0(adc)) {
+ dev_err(dev, "Too many channels for filter [%d]\n", adc->fl_id);
+ return -EINVAL;
+ } else if (num_ch != adc->mdf->nb_interleave) {
+ dev_err(dev, "Unexpected channels number for filter0: [%d]\n",
+ num_ch);
+ return -EINVAL;
+ }
+ }
+
+ ch = devm_kcalloc(&indio_dev->dev, num_ch, sizeof(*ch), GFP_KERNEL);
+ if (!ch)
+ return -ENOMEM;
+
+ adc->channels = devm_kcalloc(&indio_dev->dev, num_ch, sizeof(*adc->channels),
+ GFP_KERNEL);
+ if (!adc->channels)
+ return -ENOMEM;
+
+ ret = stm32_mdf_adc_chan_init(indio_dev, ch);
+ if (ret < 0)
+ return dev_err_probe(dev, ret, "Channels init failed\n");
+ }
+
+ indio_dev->num_channels = num_ch;
+ indio_dev->channels = ch;
+
+ init_completion(&adc->completion);
+
+ return 0;
+}
+
+static const struct stm32_mdf_dev_data stm32h7_mdf_adc_data = {
+ .type = STM32_MDF_IIO,
+ .init = stm32_mdf_adc_init,
+};
+
+static const struct of_device_id stm32_mdf_adc_match[] = {
+ {
+ .compatible = "st,stm32mp25-mdf-adc",
+ .data = &stm32h7_mdf_adc_data,
+ },
+ {}
+};
+MODULE_DEVICE_TABLE(of, stm32_mdf_adc_match);
+
static int stm32_mdf_get_sitf(struct device *dev, struct stm32_mdf_adc *adc,
struct fwnode_handle *sitf_node)
{
@@ -857,11 +1407,13 @@ static int stm32_mdf_adc_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct stm32_mdf_adc *adc;
+ const struct stm32_mdf_dev_data *dev_data;
struct iio_dev *iio;
struct resource *res;
void __iomem *base;
- int ret;
+ int ret, irq;
+ dev_data = device_get_match_data(dev);
iio = devm_iio_device_alloc(dev, sizeof(*adc));
if (!iio) {
dev_err(dev, "Failed to allocate IIO device\n");
@@ -883,13 +1435,41 @@ static int stm32_mdf_adc_probe(struct platform_device *pdev)
if (IS_ERR(adc->regmap))
return dev_err_probe(dev, PTR_ERR(adc->regmap), "Failed to init regmap\n");
+ irq = platform_get_irq(pdev, 0);
+ if (irq < 0)
+ return dev_err_probe(dev, irq, "Failed to get IRQ\n");
+
+ ret = devm_request_irq(dev, irq, stm32_mdf_irq, 0, pdev->name, iio);
+ if (ret < 0)
+ return dev_err_probe(dev, ret, "Failed to request IRQ\n");
+
ret = stm32_mdf_adc_parse_of(pdev, adc);
if (ret < 0)
return ret;
+ iio->info = &stm32_mdf_info_adc;
+ iio->name = dev_name(&pdev->dev);
+
adc->dev = dev;
+ adc->dev_data = dev_data;
+ ret = dev_data->init(dev, iio);
+ if (ret < 0)
+ goto err_clean_list;
+
+ if (!MDF_IS_INTERLEAVED_FILT_NOT_0(adc)) {
+ ret = iio_device_register(iio);
+ if (ret < 0) {
+ dev_err_probe(dev, ret, "Failed to register IIO device: %d\n", ret);
+ goto err_clean_list;
+ }
+ }
return 0;
+
+err_clean_list:
+ list_del(&adc->entry);
+
+ return ret;
}
static void stm32_mdf_adc_remove(struct platform_device *pdev)
@@ -906,6 +1486,7 @@ static void stm32_mdf_adc_remove(struct platform_device *pdev)
static struct platform_driver stm32_mdf_adc_driver = {
.driver = {
.name = "stm32-mdf-adc",
+ .of_match_table = stm32_mdf_adc_match,
},
.probe = stm32_mdf_adc_probe,
.remove = stm32_mdf_adc_remove,
--
2.43.0
^ permalink raw reply related [flat|nested] 22+ messages in thread
* [PATCH v2 08/11] iio: adc: stm32-mdf: add dma buffered acquisition support
2026-11-18 15:17 [PATCH v2 00/11] iio: adc: stm32: add mdf support for stm32mp2 Olivier Moysan
` (6 preceding siblings ...)
2026-11-18 15:17 ` [PATCH v2 07/11] iio: adc: stm32-mdf: add direct iio conversion support Olivier Moysan
@ 2026-11-18 15:17 ` 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
` (2 subsequent siblings)
10 siblings, 1 reply; 22+ messages in thread
From: Olivier Moysan @ 2026-11-18 15:17 UTC (permalink / raw)
To: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko,
Rob Herring, Krzysztof Kozlowski, Conor Dooley, Maxime Coquelin,
Alexandre Torgue, Philipp Zabel, Sumit Semwal,
Christian König, Philipp Stanner, Arnaud Pouliquen,
Liam Girdwood, Mark Brown, Jaroslav Kysela, Takashi Iwai,
Fabrice Gasnier
Cc: linux-iio, devicetree, linux-stm32, linux-arm-kernel,
linux-kernel, linux-media, dri-devel, linaro-mm-sig, linux-sound,
Olivier Moysan
Add optional cyclic DMA acquisition with buffer enable and disable
operations. Push completed DMA scans to the IIO buffer or deliver periods
to a registered consumer callback.
Add the buffer watermark callback to size a cyclic DMA buffer with two
periods. Limit the requested watermark to the available buffer capacity
and reject invalid scan sizes.
Set up analog IIO buffers when DMA is available, retaining direct-only
operation without DMA. Keep internal TRGO triggering for synchronized
and interleaved acquisition.
Stop active buffers during system suspend and restore their configuration
and acquisition on resume. External timer trigger support follows in a
separate commit.
Signed-off-by: Olivier Moysan <olivier.moysan@foss.st.com>
---
drivers/iio/adc/Kconfig | 1 +
drivers/iio/adc/stm32-mdf-adc.c | 315 +++++++++++++++++++++++++++++++++++++++-
2 files changed, 315 insertions(+), 1 deletion(-)
diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
index c3e9885af22d..f9fce54f8627 100644
--- a/drivers/iio/adc/Kconfig
+++ b/drivers/iio/adc/Kconfig
@@ -1760,6 +1760,7 @@ config STM32_MDF_ADC
select STM32_MDF_CORE
select IIO_BUFFER
select IIO_BUFFER_HW_CONSUMER
+ select IIO_TRIGGERED_BUFFER
select IIO_BACKEND
help
Select this option to support ADCSigma delta modulator for
diff --git a/drivers/iio/adc/stm32-mdf-adc.c b/drivers/iio/adc/stm32-mdf-adc.c
index 7361ce6182be..046ca3943a39 100644
--- a/drivers/iio/adc/stm32-mdf-adc.c
+++ b/drivers/iio/adc/stm32-mdf-adc.c
@@ -823,6 +823,243 @@ static void stm32_mdf_adc_stop_conv(struct iio_dev *indio_dev)
stm32_mdf_sitf_stop(adc->sitf);
}
+static unsigned int stm32_mdf_adc_dma_residue(struct stm32_mdf_adc *adc)
+{
+ struct dma_tx_state state;
+ enum dma_status status;
+
+ status = dmaengine_tx_status(adc->dma_chan, adc->dma_chan->cookie, &state);
+ if (status == DMA_IN_PROGRESS) {
+ /* Residue is size in bytes from end of buffer */
+ unsigned int i = adc->buf_sz - state.residue;
+ unsigned int size;
+
+ /* Return available bytes */
+ if (i >= adc->bufi)
+ size = i - adc->bufi;
+ else
+ size = adc->buf_sz + i - adc->bufi;
+
+ return size;
+ }
+
+ return 0;
+}
+
+static void stm32_mdf_adc_dma_buffer_done(void *data)
+{
+ struct iio_dev *indio_dev = data;
+ struct stm32_mdf_adc *adc = iio_priv(indio_dev);
+ int available = stm32_mdf_adc_dma_residue(adc);
+ size_t old_pos;
+
+ dev_dbg(&indio_dev->dev, "pos = %d, available = %d\n", adc->bufi, available);
+ old_pos = adc->bufi;
+
+ while (available >= indio_dev->scan_bytes) {
+ s32 *buffer = (s32 *)&adc->rx_buf[adc->bufi];
+
+ adc->bufi += indio_dev->scan_bytes;
+ if (adc->bufi >= adc->buf_sz) {
+ if (adc->cb)
+ adc->cb(&adc->rx_buf[old_pos], adc->buf_sz - old_pos, adc->cb_priv);
+ adc->bufi = 0;
+ old_pos = 0;
+ }
+ if (adc->dev_data->type == STM32_MDF_IIO)
+ iio_push_to_buffers(indio_dev, buffer);
+ available -= indio_dev->scan_bytes;
+ }
+ if (adc->cb)
+ adc->cb(&adc->rx_buf[old_pos], adc->bufi - old_pos, adc->cb_priv);
+}
+
+static int stm32_mdf_adc_dma_start(struct iio_dev *indio_dev)
+{
+ struct stm32_mdf_adc *adc = iio_priv(indio_dev);
+ struct dma_slave_config config = {
+ .src_addr = (dma_addr_t)adc->phys_addr + MDF_DFLTDR_REG,
+ .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
+ };
+ struct dma_async_tx_descriptor *desc;
+ int ret;
+
+ if (!adc->dma_chan)
+ return -EINVAL;
+
+ if (!indio_dev->scan_bytes || !adc->buf_sz ||
+ (adc->buf_sz / 2) % indio_dev->scan_bytes)
+ return -EINVAL;
+
+ dev_dbg(&indio_dev->dev, "size=%d watermark=%d\n", adc->buf_sz, adc->buf_sz / 2);
+
+ ret = dmaengine_slave_config(adc->dma_chan, &config);
+ if (ret)
+ return ret;
+
+ /* Prepare a DMA cyclic transaction */
+ desc = dmaengine_prep_dma_cyclic(adc->dma_chan, adc->dma_buf,
+ adc->buf_sz, adc->buf_sz / 2,
+ DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
+ if (!desc)
+ return -EBUSY;
+
+ desc->callback = stm32_mdf_adc_dma_buffer_done;
+ desc->callback_param = indio_dev;
+
+ ret = dma_submit_error(dmaengine_submit(desc));
+ if (ret)
+ goto err_stop_dma;
+
+ /* Issue pending DMA requests */
+ dma_async_issue_pending(adc->dma_chan);
+
+ /* Enable regular DMA transfer*/
+ ret = regmap_set_bits(adc->regmap, MDF_DFLTCR_REG, MDF_DFLTCR_DMAEN);
+ if (ret < 0)
+ goto err_stop_dma;
+
+ return 0;
+
+err_stop_dma:
+ dmaengine_terminate_sync(adc->dma_chan);
+
+ return ret;
+}
+
+static void stm32_mdf_adc_dma_stop(struct iio_dev *indio_dev)
+{
+ struct stm32_mdf_adc *adc = iio_priv(indio_dev);
+
+ if (!adc->dma_chan)
+ return;
+
+ regmap_clear_bits(adc->regmap, MDF_DFLTCR_REG, MDF_DFLTCR_DMAEN);
+
+ dmaengine_terminate_sync(adc->dma_chan);
+}
+
+static int stm32_mdf_adc_postenable(struct iio_dev *indio_dev)
+{
+ struct stm32_mdf_adc *adc = iio_priv(indio_dev);
+ int i = 0;
+ int ret;
+
+ /* Reset adc buffer index */
+ adc->bufi = 0;
+
+ while (i < indio_dev->num_channels && adc->channels[i].backend) {
+ ret = iio_backend_enable(adc->channels[i].backend);
+ if (ret < 0) {
+ while (i > 0)
+ iio_backend_disable(adc->channels[--i].backend);
+
+ return ret;
+ }
+ i++;
+ }
+
+ ret = stm32_mdf_adc_start_mdf(indio_dev);
+ if (ret < 0)
+ goto err_start;
+
+ stm32_mdf_adc_filter_set_mode(adc, true);
+
+ regmap_clear_bits(adc->regmap, MDF_DFLTISR_REG,
+ MDF_DFLTISR_DOVRF_MASK | MDF_DFLTISR_SATF_MASK);
+
+ regmap_set_bits(adc->regmap, MDF_DFLTIER_REG,
+ MDF_DFLTIER_DOVRIE_MASK | MDF_DFLTIER_SATIE_MASK);
+
+ ret = stm32_mdf_adc_dma_start(indio_dev);
+ if (ret) {
+ dev_err(&indio_dev->dev, "Can't start DMA\n");
+ goto err_dma;
+ }
+
+ ret = stm32_mdf_adc_start_conv(indio_dev);
+ if (ret) {
+ dev_err(&indio_dev->dev, "Can't start conversion\n");
+ goto err_conv;
+ }
+
+ return 0;
+
+err_conv:
+ stm32_mdf_adc_dma_stop(indio_dev);
+err_dma:
+ stm32_mdf_adc_stop_mdf(indio_dev);
+err_start:
+ i = 0;
+ while (i < indio_dev->num_channels && adc->channels[i].backend) {
+ iio_backend_disable(adc->channels[i].backend);
+ i++;
+ }
+
+ return ret;
+}
+
+static int stm32_mdf_adc_predisable(struct iio_dev *indio_dev)
+{
+ struct stm32_mdf_adc *adc = iio_priv(indio_dev);
+ int i = 0;
+
+ stm32_mdf_adc_stop_conv(indio_dev);
+
+ stm32_mdf_adc_dma_stop(indio_dev);
+
+ regmap_clear_bits(adc->regmap, MDF_DFLTIER_REG,
+ MDF_DFLTIER_DOVRIE_MASK | MDF_DFLTIER_SATIE_MASK);
+
+ stm32_mdf_adc_stop_mdf(indio_dev);
+
+ while (i < indio_dev->num_channels && adc->channels[i].backend) {
+ iio_backend_disable(adc->channels[i].backend);
+ i++;
+ }
+
+ return 0;
+}
+
+static const struct iio_buffer_setup_ops stm32_mdf_buffer_setup_ops = {
+ .postenable = &stm32_mdf_adc_postenable,
+ .predisable = &stm32_mdf_adc_predisable,
+};
+
+static void stm32_mdf_dma_release(struct iio_dev *indio_dev)
+{
+ struct stm32_mdf_adc *adc = iio_priv(indio_dev);
+
+ if (adc && adc->dma_chan) {
+ dma_free_coherent(adc->dma_chan->device->dev,
+ MDF_DMA_BUFFER_SIZE, adc->rx_buf, adc->dma_buf);
+ dma_release_channel(adc->dma_chan);
+ }
+}
+
+static int stm32_mdf_dma_request(struct device *dev, struct iio_dev *indio_dev)
+{
+ struct stm32_mdf_adc *adc = iio_priv(indio_dev);
+ struct dma_chan *dma_chan;
+
+ dma_chan = dma_request_chan(dev, "rx");
+ if (IS_ERR(dma_chan))
+ return PTR_ERR(dma_chan) ? PTR_ERR(dma_chan) : -ENODEV;
+ adc->dma_chan = dma_chan;
+
+ adc->rx_buf = dma_alloc_coherent(adc->dma_chan->device->dev,
+ MDF_DMA_BUFFER_SIZE, &adc->dma_buf, GFP_KERNEL);
+ if (!adc->rx_buf) {
+ dma_release_channel(adc->dma_chan);
+ return -ENOMEM;
+ }
+
+ indio_dev->modes |= INDIO_BUFFER_SOFTWARE;
+ indio_dev->setup_ops = &stm32_mdf_buffer_setup_ops;
+
+ return 0;
+}
+
static int stm32_mdf_channel_parse_of(struct iio_dev *indio_dev, struct fwnode_handle *node,
struct iio_chan_spec *ch)
{
@@ -932,6 +1169,27 @@ static int stm32_mdf_adc_chan_init(struct iio_dev *indio_dev, struct iio_chan_sp
return chan_idx;
}
+static int stm32_mdf_set_watermark(struct iio_dev *indio_dev, unsigned int val)
+{
+ struct stm32_mdf_adc *adc = iio_priv(indio_dev);
+ unsigned int scan_bytes = indio_dev->scan_bytes;
+ unsigned int watermark;
+
+ /*
+ * DMA cyclic transfers are used, buffer is split into two periods.
+ * There should be :
+ * - always one buffer (period) DMA is working on
+ * - one buffer (period) driver pushed to ASoC side.
+ */
+ if (!val || !scan_bytes || scan_bytes > MDF_DMA_BUFFER_SIZE / 2)
+ return -EINVAL;
+
+ watermark = min(val, MDF_DMA_BUFFER_SIZE / (2 * scan_bytes));
+ adc->buf_sz = 2 * watermark * scan_bytes;
+
+ return 0;
+}
+
static int stm32_mdf_adc_single_conv(struct iio_dev *indio_dev,
const struct iio_chan_spec *chan, int *res)
{
@@ -1115,6 +1373,7 @@ static int stm32_mdf_adc_read_label(struct iio_dev *indio_dev, const struct iio_
}
static const struct iio_info stm32_mdf_info_adc = {
+ .hwfifo_set_watermark = stm32_mdf_set_watermark,
.write_raw = stm32_mdf_adc_write_raw,
.read_raw = stm32_mdf_adc_read_raw,
.read_label = stm32_mdf_adc_read_label,
@@ -1208,6 +1467,24 @@ static int stm32_mdf_adc_init(struct device *dev, struct iio_dev *indio_dev)
init_completion(&adc->completion);
+ /* Optionally request DMA */
+ ret = stm32_mdf_dma_request(dev, indio_dev);
+ if (ret) {
+ if (ret != -ENODEV)
+ return dev_err_probe(dev, ret, "DMA channel request failed with\n");
+
+ dev_dbg(dev, "No DMA support\n");
+ return 0;
+ }
+
+ ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
+ &iio_pollfunc_store_time, NULL,
+ &stm32_mdf_buffer_setup_ops);
+ if (ret) {
+ stm32_mdf_dma_release(indio_dev);
+ return dev_err_probe(dev, ret, "Buffer setup failed\n");
+ }
+
return 0;
}
@@ -1460,12 +1737,14 @@ static int stm32_mdf_adc_probe(struct platform_device *pdev)
ret = iio_device_register(iio);
if (ret < 0) {
dev_err_probe(dev, ret, "Failed to register IIO device: %d\n", ret);
- goto err_clean_list;
+ goto err_cleanup;
}
}
return 0;
+err_cleanup:
+ stm32_mdf_dma_release(iio);
err_clean_list:
list_del(&adc->entry);
@@ -1479,14 +1758,48 @@ static void stm32_mdf_adc_remove(struct platform_device *pdev)
if (!MDF_IS_INTERLEAVED_FILT_NOT_0(adc))
iio_device_unregister(indio_dev);
+ stm32_mdf_dma_release(indio_dev);
list_del(&adc->entry);
}
+static int stm32_mdf_adc_suspend(struct device *dev)
+{
+ struct iio_dev *indio_dev = dev_get_drvdata(dev);
+ struct stm32_mdf_adc *adc = iio_priv(indio_dev);
+ int ret = 0;
+
+ if (iio_buffer_enabled(indio_dev))
+ ret = stm32_mdf_adc_predisable(indio_dev);
+
+ regcache_cache_only(adc->regmap, true);
+ regcache_mark_dirty(adc->regmap);
+
+ return ret;
+}
+
+static int stm32_mdf_adc_resume(struct device *dev)
+{
+ struct iio_dev *indio_dev = dev_get_drvdata(dev);
+ struct stm32_mdf_adc *adc = iio_priv(indio_dev);
+ int ret;
+
+ regcache_cache_only(adc->regmap, false);
+ ret = regcache_sync(adc->regmap);
+
+ if (!ret && iio_buffer_enabled(indio_dev))
+ ret = stm32_mdf_adc_postenable(indio_dev);
+
+ return ret;
+}
+
+static SIMPLE_DEV_PM_OPS(stm32_mdf_adc_pm_ops, stm32_mdf_adc_suspend, stm32_mdf_adc_resume);
+
static struct platform_driver stm32_mdf_adc_driver = {
.driver = {
.name = "stm32-mdf-adc",
.of_match_table = stm32_mdf_adc_match,
+ .pm = &stm32_mdf_adc_pm_ops,
},
.probe = stm32_mdf_adc_probe,
.remove = stm32_mdf_adc_remove,
--
2.43.0
^ permalink raw reply related [flat|nested] 22+ messages in thread
* [PATCH v2 09/11] iio: adc: stm32-mdf: add external timer trigger support
2026-11-18 15:17 [PATCH v2 00/11] iio: adc: stm32: add mdf support for stm32mp2 Olivier Moysan
` (7 preceding siblings ...)
2026-11-18 15:17 ` [PATCH v2 08/11] iio: adc: stm32-mdf: add dma buffered acquisition support Olivier Moysan
@ 2026-11-18 15:17 ` 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 15:17 ` [PATCH v2 11/11] ASoC: stm32: add mdf dai support Olivier Moysan
10 siblings, 1 reply; 22+ messages in thread
From: Olivier Moysan @ 2026-11-18 15:17 UTC (permalink / raw)
To: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko,
Rob Herring, Krzysztof Kozlowski, Conor Dooley, Maxime Coquelin,
Alexandre Torgue, Philipp Zabel, Sumit Semwal,
Christian König, Philipp Stanner, Arnaud Pouliquen,
Liam Girdwood, Mark Brown, Jaroslav Kysela, Takashi Iwai,
Fabrice Gasnier
Cc: linux-iio, devicetree, linux-stm32, linux-arm-kernel,
linux-kernel, linux-media, dri-devel, linaro-mm-sig, linux-sound,
Olivier Moysan
Introduce the STM32 timer and low-power timer trigger source definitions
and lookup table. Add the IIO trigger validation hook and filter trigger
source and polarity programming.
Signed-off-by: Olivier Moysan <olivier.moysan@foss.st.com>
---
drivers/iio/adc/stm32-mdf-adc.c | 108 +++++++++++++++++++++++++++++++++++++++-
1 file changed, 107 insertions(+), 1 deletion(-)
diff --git a/drivers/iio/adc/stm32-mdf-adc.c b/drivers/iio/adc/stm32-mdf-adc.c
index 046ca3943a39..97099b4adffc 100644
--- a/drivers/iio/adc/stm32-mdf-adc.c
+++ b/drivers/iio/adc/stm32-mdf-adc.c
@@ -13,6 +13,9 @@
#include <linux/iio/buffer.h>
#include <linux/iio/hw-consumer.h>
#include <linux/iio/sysfs.h>
+#include <linux/iio/timer/stm32-lptim-trigger.h>
+#include <linux/iio/timer/stm32-timer-trigger.h>
+#include <linux/iio/trigger.h>
#include <linux/iio/trigger_consumer.h>
#include <linux/iio/triggered_buffer.h>
#include <linux/interrupt.h>
@@ -172,6 +175,43 @@ enum stm32_trig_type {
STM32_MDF_TRGSRC_NB,
};
+enum stm32_trig_sens {
+ STM32_MDF_TRGSENS_RISING_EDGE,
+ STM32_MDF_TRGSENS_FALLING_EDGE,
+};
+
+enum stm32_trig_src {
+ STM32_MDF_TRGSRC_TIM1_TRGO2 = 0x2,
+ STM32_MDF_TRGSRC_TIM8_TRGO2,
+ STM32_MDF_TRGSRC_TIM20_TRGO2,
+ STM32_MDF_TRGSRC_TIM16_OC1,
+ STM32_MDF_TRGSRC_TIM6_TRGO,
+ STM32_MDF_TRGSRC_TIM7_TRGO,
+ STM32_MDF_TRGSRC_EXTI11,
+ STM32_MDF_TRGSRC_EXTI15,
+ STM32_MDF_TRGSRC_LPTIM1_CH1,
+ STM32_MDF_TRGSRC_LPTIM2_CH1,
+ STM32_MDF_TRGSRC_LPTIM3_CH1,
+};
+
+struct stm32_mdf_ext_trig_src {
+ const char *name;
+ unsigned int trgsrc;
+};
+
+static const struct stm32_mdf_ext_trig_src stm32_mdf_trigs[] = {
+ { TIM1_TRGO2, STM32_MDF_TRGSRC_TIM1_TRGO2 },
+ { TIM8_TRGO2, STM32_MDF_TRGSRC_TIM8_TRGO2 },
+ { TIM20_TRGO2, STM32_MDF_TRGSRC_TIM20_TRGO2 },
+ { TIM16_OC1, STM32_MDF_TRGSRC_TIM16_OC1 },
+ { TIM6_TRGO, STM32_MDF_TRGSRC_TIM6_TRGO },
+ { TIM7_TRGO, STM32_MDF_TRGSRC_TIM7_TRGO },
+ { LPTIM1_CH1, STM32_MDF_TRGSRC_LPTIM1_CH1 },
+ { LPTIM2_CH1, STM32_MDF_TRGSRC_LPTIM2_CH1 },
+ { LPTIM3_CH1, STM32_MDF_TRGSRC_LPTIM3_CH1 },
+ {},
+};
+
static const unsigned int stm32_mdf_hpf_cutoff_ratio[] = {
625, 1250, 2500, 9500
};
@@ -365,9 +405,63 @@ static void stm32_mdf_adc_stop_filter(struct stm32_mdf_adc *adc)
stm32_mdf_adc_stop_filter(adc_inter);
}
+static int stm32_mdf_adc_get_trig(struct iio_dev *indio_dev, struct iio_trigger *trig)
+{
+ int i;
+
+ /* lookup triggers registered by stm32 timer trigger driver */
+ for (i = 0; stm32_mdf_trigs[i].name; i++) {
+ /**
+ * Checking both stm32 timer trigger type and trig name
+ * should be safe against arbitrary trigger names.
+ */
+ if ((is_stm32_timer_trigger(trig) ||
+ is_stm32_lptim_trigger(trig)) &&
+ !strcmp(stm32_mdf_trigs[i].name, trig->name)) {
+ dev_dbg(&indio_dev->dev, "Trigger [%d] found\n", i);
+ return stm32_mdf_trigs[i].trgsrc;
+ }
+ }
+
+ return -EINVAL;
+}
+
+static int stm32_mdf_adc_filter_set_trig(struct iio_dev *indio_dev)
+{
+ struct stm32_mdf_adc *adc = iio_priv(indio_dev);
+ struct iio_trigger *trig = indio_dev->trig;
+ u32 trgsrc = 0;
+ /* set trigger polarity to rising edge by default */
+ u32 trgsens = STM32_MDF_TRGSENS_RISING_EDGE;
+ int ret;
+
+ if (trig) {
+ ret = stm32_mdf_adc_get_trig(indio_dev, trig);
+ if (ret < 0)
+ return ret;
+ }
+
+ dev_dbg(adc->dev, "Set trigger source [%d] on filter [%d]\n", trgsrc, adc->fl_id);
+
+ return regmap_update_bits(adc->regmap, MDF_DFLTCR_REG,
+ MDF_DFLTCR_TRGSRC_MASK | MDF_DFLTCR_TRGSENS,
+ FIELD_PREP(MDF_DFLTCR_TRGSRC_MASK, trgsrc) |
+ FIELD_PREP(MDF_DFLTCR_TRGSENS, trgsens));
+}
+
+static void stm32_mdf_adc_filter_clear_trig(struct iio_dev *indio_dev)
+{
+ struct stm32_mdf_adc *adc = iio_priv(indio_dev);
+
+ regmap_update_bits(adc->regmap, MDF_DFLTCR_REG,
+ MDF_DFLTCR_TRGSRC_MASK | MDF_DFLTCR_TRGSENS, 0);
+}
+
static int stm32_mdf_adc_filter_set_mode(struct stm32_mdf_adc *adc, bool cont)
{
struct stm32_mdf_adc *adc_inter;
+ struct iio_dev *indio_dev = dev_get_drvdata(adc->dev);
+ struct iio_trigger *trig = indio_dev->trig;
u32 mode;
if (MDF_IS_FILTER0(adc)) {
@@ -377,7 +471,7 @@ static int stm32_mdf_adc_filter_set_mode(struct stm32_mdf_adc *adc, bool cont)
}
}
- if (adc->synced || MDF_IS_INTERLEAVED_FILT(adc)) {
+ if (adc->synced || MDF_IS_INTERLEAVED_FILT(adc) || trig) {
if (cont)
mode = STM32_MDF_ACQ_MODE_SYNC_CONT;
else
@@ -977,6 +1071,10 @@ static int stm32_mdf_adc_postenable(struct iio_dev *indio_dev)
goto err_dma;
}
+ ret = stm32_mdf_adc_filter_set_trig(indio_dev);
+ if (ret < 0)
+ goto err_trig;
+
ret = stm32_mdf_adc_start_conv(indio_dev);
if (ret) {
dev_err(&indio_dev->dev, "Can't start conversion\n");
@@ -986,6 +1084,8 @@ static int stm32_mdf_adc_postenable(struct iio_dev *indio_dev)
return 0;
err_conv:
+ stm32_mdf_adc_filter_clear_trig(indio_dev);
+err_trig:
stm32_mdf_adc_dma_stop(indio_dev);
err_dma:
stm32_mdf_adc_stop_mdf(indio_dev);
@@ -1006,6 +1106,8 @@ static int stm32_mdf_adc_predisable(struct iio_dev *indio_dev)
stm32_mdf_adc_stop_conv(indio_dev);
+ stm32_mdf_adc_filter_clear_trig(indio_dev);
+
stm32_mdf_adc_dma_stop(indio_dev);
regmap_clear_bits(adc->regmap, MDF_DFLTIER_REG,
@@ -1377,6 +1479,7 @@ static const struct iio_info stm32_mdf_info_adc = {
.write_raw = stm32_mdf_adc_write_raw,
.read_raw = stm32_mdf_adc_read_raw,
.read_label = stm32_mdf_adc_read_label,
+ .validate_trigger = stm32_mdf_adc_get_trig,
};
static irqreturn_t stm32_mdf_irq(int irq, void *arg)
@@ -1485,6 +1588,9 @@ static int stm32_mdf_adc_init(struct device *dev, struct iio_dev *indio_dev)
return dev_err_probe(dev, ret, "Buffer setup failed\n");
}
+ /* lptimer/timer hardware triggers */
+ indio_dev->modes |= INDIO_HARDWARE_TRIGGERED;
+
return 0;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 22+ messages in thread
* [PATCH v2 10/11] iio: adc: stm32-mdf: add dmic audio filter support
2026-11-18 15:17 [PATCH v2 00/11] iio: adc: stm32: add mdf support for stm32mp2 Olivier Moysan
` (8 preceding siblings ...)
2026-11-18 15:17 ` [PATCH v2 09/11] iio: adc: stm32-mdf: add external timer trigger support Olivier Moysan
@ 2026-11-18 15:17 ` 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
10 siblings, 1 reply; 22+ messages in thread
From: Olivier Moysan @ 2026-11-18 15:17 UTC (permalink / raw)
To: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko,
Rob Herring, Krzysztof Kozlowski, Conor Dooley, Maxime Coquelin,
Alexandre Torgue, Philipp Zabel, Sumit Semwal,
Christian König, Philipp Stanner, Arnaud Pouliquen,
Liam Girdwood, Mark Brown, Jaroslav Kysela, Takashi Iwai,
Fabrice Gasnier
Cc: linux-iio, devicetree, linux-stm32, linux-arm-kernel,
linux-kernel, linux-media, dri-devel, linaro-mm-sig, linux-sound,
Olivier Moysan
Extend the MDF filter driver to support digital microphones and provide
the interface required by an audio consumer.
Signed-off-by: Olivier Moysan <olivier.moysan@foss.st.com>
---
drivers/iio/adc/stm32-mdf-adc.c | 164 +++++++++++++++++++++++++++++++++-
include/linux/iio/adc/stm32-mdf-adc.h | 19 ++++
2 files changed, 182 insertions(+), 1 deletion(-)
diff --git a/drivers/iio/adc/stm32-mdf-adc.c b/drivers/iio/adc/stm32-mdf-adc.c
index 97099b4adffc..27c47bef71a4 100644
--- a/drivers/iio/adc/stm32-mdf-adc.c
+++ b/drivers/iio/adc/stm32-mdf-adc.c
@@ -9,6 +9,7 @@
#include <linux/clk-provider.h>
#include <linux/dmaengine.h>
#include <linux/dma-mapping.h>
+#include <linux/iio/adc/stm32-mdf-adc.h>
#include <linux/iio/backend.h>
#include <linux/iio/buffer.h>
#include <linux/iio/hw-consumer.h>
@@ -1128,6 +1129,42 @@ static const struct iio_buffer_setup_ops stm32_mdf_buffer_setup_ops = {
.predisable = &stm32_mdf_adc_predisable,
};
+/**
+ * stm32_mdf_get_sub_channels_nb() - Get the number of MDF sub-channels
+ * @indio_dev: MDF IIO device
+ *
+ * Return: 0 on success, or a negative error code on failure.
+ */
+unsigned int stm32_mdf_get_sub_channels_nb(struct iio_dev *indio_dev)
+{
+ struct stm32_mdf_adc *adc = iio_priv(indio_dev);
+
+ if (MDF_IS_FILTER0(adc) && adc->mdf->nb_interleave)
+ return adc->mdf->nb_interleave;
+
+ return 1;
+}
+EXPORT_SYMBOL_GPL(stm32_mdf_get_sub_channels_nb);
+
+static ssize_t stm32_mdf_adc_audio_get_channels(struct iio_dev *indio_dev, uintptr_t priv,
+ const struct iio_chan_spec *chan, char *buf)
+{
+ return sysfs_emit(buf, "%u\n", stm32_mdf_get_sub_channels_nb(indio_dev));
+}
+
+/*
+ * IIO channel extended info used by the audio device IIO channel consumer.
+ * sub_channels_nb: provides the number of audio channels associated to the IIO channel.
+ */
+static const struct iio_chan_spec_ext_info stm32_mdf_adc_audio_ext_info[] = {
+ {
+ .name = "sub_channels_nb",
+ .shared = IIO_SHARED_BY_TYPE,
+ .read = stm32_mdf_adc_audio_get_channels,
+ },
+ {},
+};
+
static void stm32_mdf_dma_release(struct iio_dev *indio_dev)
{
struct stm32_mdf_adc *adc = iio_priv(indio_dev);
@@ -1240,6 +1277,9 @@ static int stm32_mdf_adc_chan_init_one(struct iio_dev *indio_dev, struct fwnode_
ch->scan_type.shift = 8;
}
+ if (adc->dev_data->type == STM32_MDF_AUDIO)
+ ch->ext_info = stm32_mdf_adc_audio_ext_info;
+
ch->info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ);
ch->scan_type.sign = 's';
ch->scan_type.realbits = STM32_MDF_DATA_RES;
@@ -1474,6 +1514,13 @@ static int stm32_mdf_adc_read_label(struct iio_dev *indio_dev, const struct iio_
return sysfs_emit(label, "%s\n", name);
}
+static const struct iio_info stm32_mdf_info_audio = {
+ .hwfifo_set_watermark = stm32_mdf_set_watermark,
+ .write_raw = stm32_mdf_adc_write_raw,
+ .read_raw = stm32_mdf_adc_read_raw,
+ .read_label = stm32_mdf_adc_read_label,
+};
+
static const struct iio_info stm32_mdf_info_adc = {
.hwfifo_set_watermark = stm32_mdf_set_watermark,
.write_raw = stm32_mdf_adc_write_raw,
@@ -1530,6 +1577,38 @@ static irqreturn_t stm32_mdf_irq(int irq, void *arg)
return IRQ_HANDLED;
}
+static int stm32_mdf_audio_init(struct device *dev, struct iio_dev *indio_dev)
+{
+ struct stm32_mdf_adc *adc = iio_priv(indio_dev);
+ struct iio_chan_spec *ch;
+ int ret;
+
+ ch = devm_kzalloc(&indio_dev->dev, sizeof(*ch), GFP_KERNEL);
+ if (!ch)
+ return -ENOMEM;
+
+ adc->channels = devm_kzalloc(&indio_dev->dev, sizeof(*adc->channels), GFP_KERNEL);
+ if (!adc->channels)
+ return -ENOMEM;
+
+ ret = stm32_mdf_adc_chan_init(indio_dev, ch);
+ if (ret < 0)
+ return dev_err_probe(dev, ret, "Channels init failed\n");
+
+ indio_dev->num_channels = 1;
+ indio_dev->channels = ch;
+
+ ret = stm32_mdf_dma_request(dev, indio_dev);
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to get DMA\n");
+
+ ret = stm32_mdf_adc_filter_set_mode(adc, true);
+ if (ret)
+ stm32_mdf_dma_release(indio_dev);
+
+ return ret;
+}
+
static int stm32_mdf_adc_init(struct device *dev, struct iio_dev *indio_dev)
{
struct stm32_mdf_adc *adc = iio_priv(indio_dev);
@@ -1599,11 +1678,77 @@ static const struct stm32_mdf_dev_data stm32h7_mdf_adc_data = {
.init = stm32_mdf_adc_init,
};
+static const struct stm32_mdf_dev_data stm32_mdf_audio_data = {
+ .type = STM32_MDF_AUDIO,
+ .init = stm32_mdf_audio_init,
+};
+
+/**
+ * stm32_mdf_get_buff_cb() - register a callback that will be called when
+ * DMA transfer period is achieved.
+ *
+ * @iio_dev: Handle to IIO device.
+ * @cb: Pointer to callback function:
+ * - data: pointer to data buffer
+ * - size: size in byte of the data buffer
+ * - private: pointer to consumer private structure.
+ * @private: Pointer to consumer private structure.
+ *
+ * Return: 0 on success, or a negative error code on failure.
+ */
+int stm32_mdf_get_buff_cb(struct iio_dev *iio_dev,
+ int (*cb)(const void *data, size_t size, void *private), void *private)
+{
+ struct stm32_mdf_adc *adc;
+
+ if (!iio_dev)
+ return -EINVAL;
+ adc = iio_priv(iio_dev);
+
+ if (!adc)
+ return -EINVAL;
+
+ adc->cb = cb;
+ adc->cb_priv = private;
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(stm32_mdf_get_buff_cb);
+
+/**
+ * stm32_mdf_release_buff_cb() - Unregister the buffer callback
+ *
+ * @iio_dev: Handle to IIO device.
+ *
+ * Return: 0 on success, or a negative error code on failure.
+ */
+int stm32_mdf_release_buff_cb(struct iio_dev *iio_dev)
+{
+ struct stm32_mdf_adc *adc;
+
+ if (!iio_dev)
+ return -EINVAL;
+ adc = iio_priv(iio_dev);
+
+ if (!adc)
+ return -EINVAL;
+
+ adc->cb = NULL;
+ adc->cb_priv = NULL;
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(stm32_mdf_release_buff_cb);
+
static const struct of_device_id stm32_mdf_adc_match[] = {
{
.compatible = "st,stm32mp25-mdf-adc",
.data = &stm32h7_mdf_adc_data,
},
+ {
+ .compatible = "st,stm32mp25-mdf-dmic",
+ .data = &stm32_mdf_audio_data,
+ },
{}
};
MODULE_DEVICE_TABLE(of, stm32_mdf_adc_match);
@@ -1788,6 +1933,7 @@ static int stm32_mdf_adc_parse_of(struct platform_device *pdev, struct stm32_mdf
static int stm32_mdf_adc_probe(struct platform_device *pdev)
{
+ struct device_node *node = pdev->dev.of_node;
struct device *dev = &pdev->dev;
struct stm32_mdf_adc *adc;
const struct stm32_mdf_dev_data *dev_data;
@@ -1830,7 +1976,10 @@ static int stm32_mdf_adc_probe(struct platform_device *pdev)
if (ret < 0)
return ret;
- iio->info = &stm32_mdf_info_adc;
+ if (dev_data->type == STM32_MDF_AUDIO)
+ iio->info = &stm32_mdf_info_audio;
+ else
+ iio->info = &stm32_mdf_info_adc;
iio->name = dev_name(&pdev->dev);
adc->dev = dev;
@@ -1847,8 +1996,19 @@ static int stm32_mdf_adc_probe(struct platform_device *pdev)
}
}
+ if (dev_data->type == STM32_MDF_AUDIO) {
+ ret = of_platform_populate(node, NULL, NULL, dev);
+ if (ret < 0) {
+ dev_err_probe(dev, ret, "Failed to find an audio DAI\n");
+ goto err_unregister;
+ }
+ }
+
return 0;
+err_unregister:
+ if (!MDF_IS_INTERLEAVED_FILT_NOT_0(adc))
+ iio_device_unregister(iio);
err_cleanup:
stm32_mdf_dma_release(iio);
err_clean_list:
@@ -1862,6 +2022,8 @@ static void stm32_mdf_adc_remove(struct platform_device *pdev)
struct iio_dev *indio_dev = platform_get_drvdata(pdev);
struct stm32_mdf_adc *adc = iio_priv(indio_dev);
+ if (adc->dev_data->type == STM32_MDF_AUDIO)
+ of_platform_depopulate(&pdev->dev);
if (!MDF_IS_INTERLEAVED_FILT_NOT_0(adc))
iio_device_unregister(indio_dev);
stm32_mdf_dma_release(indio_dev);
diff --git a/include/linux/iio/adc/stm32-mdf-adc.h b/include/linux/iio/adc/stm32-mdf-adc.h
new file mode 100644
index 000000000000..95e3f4afdb78
--- /dev/null
+++ b/include/linux/iio/adc/stm32-mdf-adc.h
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * This file describe the STM32 MDF IIO driver API for audio part
+ *
+ * Copyright (C) 2023, STMicroelectronics.
+ * Author(s): Olivier Moysan <olivier.moysan@foss.st.com>.
+ */
+
+#ifndef STM32_MDF_ADC_H
+#define STM32_MDF_ADC_H
+
+#include <linux/iio/iio.h>
+
+int stm32_mdf_get_buff_cb(struct iio_dev *iio_dev,
+ int (*cb)(const void *data, size_t size, void *private), void *private);
+int stm32_mdf_release_buff_cb(struct iio_dev *iio_dev);
+unsigned int stm32_mdf_get_sub_channels_nb(struct iio_dev *iio_dev);
+
+#endif
--
2.43.0
^ permalink raw reply related [flat|nested] 22+ messages in thread
* [PATCH v2 11/11] ASoC: stm32: add mdf dai support
2026-11-18 15:17 [PATCH v2 00/11] iio: adc: stm32: add mdf support for stm32mp2 Olivier Moysan
` (9 preceding siblings ...)
2026-11-18 15:17 ` [PATCH v2 10/11] iio: adc: stm32-mdf: add dmic audio filter support Olivier Moysan
@ 2026-11-18 15:17 ` Olivier Moysan
2026-11-18 16:08 ` sashiko-bot
10 siblings, 1 reply; 22+ messages in thread
From: Olivier Moysan @ 2026-11-18 15:17 UTC (permalink / raw)
To: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko,
Rob Herring, Krzysztof Kozlowski, Conor Dooley, Maxime Coquelin,
Alexandre Torgue, Philipp Zabel, Sumit Semwal,
Christian König, Philipp Stanner, Arnaud Pouliquen,
Liam Girdwood, Mark Brown, Jaroslav Kysela, Takashi Iwai,
Fabrice Gasnier
Cc: linux-iio, devicetree, linux-stm32, linux-arm-kernel,
linux-kernel, linux-media, dri-devel, linaro-mm-sig, linux-sound,
Olivier Moysan
Add driver to handle DAI interface for PDM microphones connected
to MDF STM32 IP.
Signed-off-by: Olivier Moysan <olivier.moysan@foss.st.com>
---
sound/soc/stm/Kconfig | 15 ++
sound/soc/stm/Makefile | 3 +
sound/soc/stm/stm32_amdf.c | 375 +++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 393 insertions(+)
diff --git a/sound/soc/stm/Kconfig b/sound/soc/stm/Kconfig
index 2753d6c2a826..6661b3a8e4f8 100644
--- a/sound/soc/stm/Kconfig
+++ b/sound/soc/stm/Kconfig
@@ -44,4 +44,19 @@ config SND_SOC_STM32_DFSDM
Select this option to enable the STM32 Digital Filter
for Sigma Delta Modulators (DFSDM) driver used
in various STM32 series for digital microphone capture.
+
+config SND_SOC_STM32_MDF
+ tristate "SoC Audio support for STM32 MDF"
+ depends on ARCH_STM32 || COMPILE_TEST
+ depends on SND_SOC
+ depends on STM32_MDF_ADC
+ select SND_SOC_GENERIC_DMAENGINE_PCM
+ select SND_SOC_DMIC
+ select IIO_BUFFER_CB
+ help
+ Select this option to enable the STM32 Multi-function
+ Digital Filter (MDF) driver used in STM32MP2 series for
+ digital microphone capture.
+
+ Module name is stm32_amdf when the driver is built as a module.
endmenu
diff --git a/sound/soc/stm/Makefile b/sound/soc/stm/Makefile
index 3372432faa09..4e1b3f943a44 100644
--- a/sound/soc/stm/Makefile
+++ b/sound/soc/stm/Makefile
@@ -16,3 +16,6 @@ obj-$(CONFIG_SND_SOC_STM32_SPDIFRX) += snd-soc-stm32-spdifrx.o
#DFSDM
obj-$(CONFIG_SND_SOC_STM32_DFSDM) += stm32_adfsdm.o
+
+#MDF
+obj-$(CONFIG_SND_SOC_STM32_MDF) += stm32_amdf.o
diff --git a/sound/soc/stm/stm32_amdf.c b/sound/soc/stm/stm32_amdf.c
new file mode 100644
index 000000000000..a733a7a2997f
--- /dev/null
+++ b/sound/soc/stm/stm32_amdf.c
@@ -0,0 +1,375 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * This file is part of STM32 MDF ASoC DAI driver
+ *
+ * Copyright (C) 2023, STMicroelectronics.
+ * Author: Olivier Moysan <olivier.moysan@foss.st.com>.
+ */
+
+#include <linux/clk.h>
+#include <linux/iio/adc/stm32-mdf-adc.h>
+#include <linux/iio/consumer.h>
+#include <linux/iio/iio.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/platform_device.h>
+#include <linux/pm_runtime.h>
+#include <linux/slab.h>
+
+#include <sound/pcm.h>
+#include <sound/soc.h>
+
+#define STM32_AMDF_DRV_NAME "stm32-amdf"
+
+#define MDF_MAX_PERIOD_SIZE (PAGE_SIZE / 2)
+#define MDF_MAX_PERIODS 6
+
+struct stm32_amdf_priv {
+ struct snd_soc_dai_driver dai_drv;
+ struct snd_pcm_substream *substream;
+ struct device *dev;
+
+ /* IIO */
+ struct iio_channel *iio_ch;
+ struct iio_cb_buffer *iio_cb;
+ bool iio_active;
+
+ /* PCM buffer */
+ unsigned char *pcm_buff;
+ unsigned int pos;
+
+ struct mutex lock; /* protect against race condition on iio state */
+};
+
+static const struct snd_pcm_hardware stm32_amdf_pcm_hw = {
+ .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
+ SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_PAUSE,
+ .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
+
+ .channels_min = 1,
+ .channels_max = 1,
+
+ .periods_min = 2,
+ .periods_max = MDF_MAX_PERIODS,
+
+ .period_bytes_max = MDF_MAX_PERIOD_SIZE,
+ .buffer_bytes_max = MDF_MAX_PERIODS * MDF_MAX_PERIOD_SIZE
+};
+
+static void stm32_amdf_shutdown(struct snd_pcm_substream *substream,
+ struct snd_soc_dai *dai)
+{
+ struct stm32_amdf_priv *priv = snd_soc_dai_get_drvdata(dai);
+
+ mutex_lock(&priv->lock);
+ if (priv->iio_active) {
+ iio_channel_stop_all_cb(priv->iio_cb);
+ priv->iio_active = false;
+ }
+ mutex_unlock(&priv->lock);
+}
+
+static int stm32_amdf_dai_prepare(struct snd_pcm_substream *substream,
+ struct snd_soc_dai *dai)
+{
+ struct stm32_amdf_priv *priv = snd_soc_dai_get_drvdata(dai);
+ int ret;
+
+ mutex_lock(&priv->lock);
+ if (priv->iio_active) {
+ iio_channel_stop_all_cb(priv->iio_cb);
+ priv->iio_active = false;
+ }
+
+ ret = iio_write_channel_attribute(priv->iio_ch,
+ substream->runtime->rate, 0,
+ IIO_CHAN_INFO_SAMP_FREQ);
+ if (ret < 0) {
+ dev_err(dai->dev, "%s: Failed to set %d sampling rate\n",
+ __func__, substream->runtime->rate);
+ goto out;
+ }
+
+ if (!priv->iio_active) {
+ ret = iio_channel_start_all_cb(priv->iio_cb);
+ if (!ret)
+ priv->iio_active = true;
+ else
+ dev_err(dai->dev, "%s: IIO channel start failed (%d)\n",
+ __func__, ret);
+ }
+
+out:
+ mutex_unlock(&priv->lock);
+
+ return ret;
+}
+
+static const struct snd_soc_dai_ops stm32_amdf_dai_ops = {
+ .shutdown = stm32_amdf_shutdown,
+ .prepare = stm32_amdf_dai_prepare,
+};
+
+static const struct snd_soc_dai_driver stm32_amdf_dai = {
+ .capture = {
+ .channels_min = 1,
+ .channels_max = 1,
+ .formats = SNDRV_PCM_FMTBIT_S16_LE |
+ SNDRV_PCM_FMTBIT_S32_LE,
+ .rates = SNDRV_PCM_RATE_CONTINUOUS,
+ .rate_min = 8000,
+ .rate_max = 48000,
+ },
+ .ops = &stm32_amdf_dai_ops,
+};
+
+static const struct snd_soc_component_driver stm32_amdf_dai_component = {
+ .name = "stm32_mdf_audio",
+};
+
+static void stm32_memcpy_32to16(void *dest, const void *src, size_t n)
+{
+ unsigned int i = 0;
+ u16 *d = (u16 *)dest, *s = (u16 *)src;
+
+ s++;
+ for (i = n >> 1; i > 0; i--) {
+ *d++ = *s++;
+ s++;
+ }
+}
+
+static int stm32_afsdm_pcm_cb(const void *data, size_t size, void *private)
+{
+ struct stm32_amdf_priv *priv = private;
+ struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(priv->substream);
+ u8 *pcm_buff = priv->pcm_buff;
+ u8 *src_buff = (u8 *)data;
+ unsigned int old_pos = priv->pos;
+ size_t buff_size = snd_pcm_lib_buffer_bytes(priv->substream);
+ size_t period_size = snd_pcm_lib_period_bytes(priv->substream);
+ size_t cur_size, src_size = size;
+ snd_pcm_format_t format = priv->substream->runtime->format;
+
+ if (format == SNDRV_PCM_FORMAT_S16_LE)
+ src_size >>= 1;
+ cur_size = src_size;
+
+ dev_dbg(rtd->dev, "%s: buff_add :%pK, pos = %d, size = %zu\n",
+ __func__, &pcm_buff[priv->pos], priv->pos, src_size);
+
+ if ((priv->pos + src_size) > buff_size) {
+ if (format == SNDRV_PCM_FORMAT_S16_LE)
+ stm32_memcpy_32to16(&pcm_buff[priv->pos], src_buff, buff_size - priv->pos);
+ else
+ memcpy(&pcm_buff[priv->pos], src_buff, buff_size - priv->pos);
+ cur_size -= buff_size - priv->pos;
+ priv->pos = 0;
+ }
+
+ if (format == SNDRV_PCM_FORMAT_S16_LE)
+ stm32_memcpy_32to16(&pcm_buff[priv->pos], &src_buff[src_size - cur_size], cur_size);
+ else
+ memcpy(&pcm_buff[priv->pos], &src_buff[src_size - cur_size], cur_size);
+
+ priv->pos = (priv->pos + cur_size) % buff_size;
+
+ if (cur_size != src_size || (old_pos && (old_pos % period_size < size)))
+ snd_pcm_period_elapsed(priv->substream);
+
+ return 0;
+}
+
+static int stm32_amdf_trigger(struct snd_soc_component *component,
+ struct snd_pcm_substream *substream, int cmd)
+{
+ struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
+ struct stm32_amdf_priv *priv =
+ snd_soc_dai_get_drvdata(snd_soc_rtd_to_cpu(rtd, 0));
+
+ switch (cmd) {
+ case SNDRV_PCM_TRIGGER_START:
+ case SNDRV_PCM_TRIGGER_RESUME:
+ priv->pos = 0;
+ return stm32_mdf_get_buff_cb(priv->iio_ch->indio_dev, stm32_afsdm_pcm_cb, priv);
+ case SNDRV_PCM_TRIGGER_SUSPEND:
+ case SNDRV_PCM_TRIGGER_STOP:
+ return stm32_mdf_release_buff_cb(priv->iio_ch->indio_dev);
+ }
+
+ return -EINVAL;
+}
+
+static int stm32_amdf_pcm_open(struct snd_soc_component *component,
+ struct snd_pcm_substream *substream)
+{
+ struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
+ struct stm32_amdf_priv *priv = snd_soc_dai_get_drvdata(snd_soc_rtd_to_cpu(rtd, 0));
+ int ret;
+
+ ret = snd_soc_set_runtime_hwparams(substream, &stm32_amdf_pcm_hw);
+ if (!ret)
+ priv->substream = substream;
+
+ return ret;
+}
+
+static int stm32_amdf_pcm_close(struct snd_soc_component *component,
+ struct snd_pcm_substream *substream)
+{
+ struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
+ struct stm32_amdf_priv *priv =
+ snd_soc_dai_get_drvdata(snd_soc_rtd_to_cpu(rtd, 0));
+
+ priv->substream = NULL;
+
+ return 0;
+}
+
+static snd_pcm_uframes_t stm32_amdf_pcm_pointer(struct snd_soc_component *component,
+ struct snd_pcm_substream *substream)
+{
+ struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
+ struct stm32_amdf_priv *priv =
+ snd_soc_dai_get_drvdata(snd_soc_rtd_to_cpu(rtd, 0));
+
+ return bytes_to_frames(substream->runtime, priv->pos);
+}
+
+static int stm32_amdf_pcm_hw_params(struct snd_soc_component *component,
+ struct snd_pcm_substream *substream,
+ struct snd_pcm_hw_params *params)
+{
+ struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
+ struct stm32_amdf_priv *priv =
+ snd_soc_dai_get_drvdata(snd_soc_rtd_to_cpu(rtd, 0));
+
+ priv->pcm_buff = substream->runtime->dma_area;
+
+ return iio_channel_cb_set_buffer_watermark(priv->iio_cb,
+ params_period_size(params));
+}
+
+static int stm32_amdf_pcm_new(struct snd_soc_component *component,
+ struct snd_soc_pcm_runtime *rtd)
+{
+ struct snd_pcm *pcm = rtd->pcm;
+ struct stm32_amdf_priv *priv =
+ snd_soc_dai_get_drvdata(snd_soc_rtd_to_cpu(rtd, 0));
+ unsigned int size = MDF_MAX_PERIODS * MDF_MAX_PERIOD_SIZE;
+
+ snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
+ priv->dev, size, size);
+
+ return 0;
+}
+
+static int stm32_amdf_dummy_cb(const void *data, void *private)
+{
+ /*
+ * This dummy callback is requested by iio_channel_get_all_cb() API,
+ * but the stm32_mdf_get_buff_cb() API is used instead, to optimize
+ * DMA transfers.
+ */
+ return 0;
+}
+
+static void stm32_amdf_cleanup(void *data)
+{
+ iio_channel_release_all_cb(data);
+}
+
+static const struct snd_soc_component_driver stm32_amdf_soc_platform = {
+ .open = stm32_amdf_pcm_open,
+ .close = stm32_amdf_pcm_close,
+ .hw_params = stm32_amdf_pcm_hw_params,
+ .trigger = stm32_amdf_trigger,
+ .pointer = stm32_amdf_pcm_pointer,
+ .pcm_new = stm32_amdf_pcm_new,
+ .debugfs_prefix = "pcm",
+};
+
+static const struct of_device_id stm32_amdf_of_match[] = {
+ {.compatible = "st,stm32mp25-mdf-dai"},
+ {}
+};
+MODULE_DEVICE_TABLE(of, stm32_amdf_of_match);
+
+static int stm32_amdf_probe(struct platform_device *pdev)
+{
+ struct stm32_amdf_priv *priv;
+ unsigned int channels_max;
+ int ret;
+
+ priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
+ if (!priv)
+ return -ENOMEM;
+
+ priv->dev = &pdev->dev;
+ priv->dai_drv = stm32_amdf_dai;
+ priv->dai_drv.name = dev_name(&pdev->dev);
+ mutex_init(&priv->lock);
+
+ dev_set_drvdata(&pdev->dev, priv);
+
+ /* Associate iio channel */
+ priv->iio_ch = devm_iio_channel_get_all(&pdev->dev);
+ if (IS_ERR(priv->iio_ch)) {
+ dev_err(&pdev->dev, "Failed to get IIO channels %ld\n", PTR_ERR(priv->iio_ch));
+ return PTR_ERR(priv->iio_ch);
+ }
+
+ priv->iio_cb = iio_channel_get_all_cb(&pdev->dev, &stm32_amdf_dummy_cb, NULL);
+ if (IS_ERR(priv->iio_cb)) {
+ dev_err(&pdev->dev, "Failed to get IIO callbacks %ld\n", PTR_ERR(priv->iio_cb));
+ return PTR_ERR(priv->iio_cb);
+ }
+
+ ret = devm_add_action_or_reset(&pdev->dev, stm32_amdf_cleanup, priv->iio_cb);
+ if (ret < 0) {
+ dev_err(&pdev->dev, "Unable to add action\n");
+ return ret;
+ }
+
+ channels_max = stm32_mdf_get_sub_channels_nb(priv->iio_ch->indio_dev);
+ priv->dai_drv.capture.channels_max = channels_max;
+
+ ret = devm_snd_soc_register_component(&pdev->dev, &stm32_amdf_dai_component,
+ &priv->dai_drv, 1);
+ if (ret < 0) {
+ dev_err(&pdev->dev, "Failed to register %s\n", stm32_amdf_dai_component.name);
+ return ret;
+ }
+
+ ret = devm_snd_soc_register_component(&pdev->dev, &stm32_amdf_soc_platform,
+ NULL, 0);
+ if (ret < 0) {
+ dev_err(&pdev->dev, "Failed to register PCM platform\n");
+ return ret;
+ }
+
+ pm_runtime_enable(&pdev->dev);
+
+ return ret;
+}
+
+static void stm32_amdf_remove(struct platform_device *pdev)
+{
+ pm_runtime_disable(&pdev->dev);
+}
+
+static struct platform_driver stm32_amdf_driver = {
+ .driver = {
+ .name = STM32_AMDF_DRV_NAME,
+ .of_match_table = stm32_amdf_of_match,
+ },
+ .probe = stm32_amdf_probe,
+ .remove = stm32_amdf_remove,
+};
+
+module_platform_driver(stm32_amdf_driver);
+
+MODULE_DESCRIPTION("stm32 MDF DAI driver");
+MODULE_AUTHOR("Olivier Moysan <olivier.moysan@foss.st.com>");
+MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS("IIO_CONSUMER");
--
2.43.0
^ permalink raw reply related [flat|nested] 22+ messages in thread
* Re: [PATCH v2 06/11] iio: adc: stm32-mdf: configure cic filters and sample rates
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
0 siblings, 0 replies; 22+ messages in thread
From: sashiko-bot @ 2026-11-18 15:34 UTC (permalink / raw)
To: Olivier Moysan
Cc: devicetree, dri-devel, linux-sound, robh, conor+dt, media-ci
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
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH v2 09/11] iio: adc: stm32-mdf: add external timer trigger support
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
0 siblings, 0 replies; 22+ messages in thread
From: sashiko-bot @ 2026-11-18 15:37 UTC (permalink / raw)
To: Olivier Moysan
Cc: devicetree, dri-devel, linux-sound, robh, conor+dt, media-ci
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] iio: adc: stm32-mdf: non-zero return in
stm32_mdf_adc_get_trig() rejects trig
- [High] iio: adc: stm32-mdf: unassigned trgsrc in
stm32_mdf_adc_filter_set_trig()
- [Medium] iio: adc: stm32-mdf: unpropagated trigger in
stm32_mdf_adc_filter_set_trig()
--
commit 2310fd550deab336f6cec980952473a0c65d6371
Author: Olivier Moysan <olivier.moysan@foss.st.com>
iio: adc: stm32-mdf: add external timer trigger support
This commit adds STM32 timer and low-power timer trigger definitions, a
trigger validation callback, and filter trigger source and polarity
configuration for the STM32 MDF ADC driver.
> diff --git a/drivers/iio/adc/stm32-mdf-adc.c b/drivers/iio/adc/stm32-mdf-adc.c
> index 046ca3943a397..97099b4adffc6 100644
> --- a/drivers/iio/adc/stm32-mdf-adc.c
> +++ b/drivers/iio/adc/stm32-mdf-adc.c
[ ... ]
> @@ -365,9 +405,63 @@ static void stm32_mdf_adc_stop_filter(struct stm32_mdf_adc *adc)
> stm32_mdf_adc_stop_filter(adc_inter);
> }
>
> +static int stm32_mdf_adc_get_trig(struct iio_dev *indio_dev, struct iio_trigger *trig)
> +{
> + int i;
> +
> + /* lookup triggers registered by stm32 timer trigger driver */
> + for (i = 0; stm32_mdf_trigs[i].name; i++) {
> + /**
> + * Checking both stm32 timer trigger type and trig name
> + * should be safe against arbitrary trigger names.
> + */
> + if ((is_stm32_timer_trigger(trig) ||
> + is_stm32_lptim_trigger(trig)) &&
> + !strcmp(stm32_mdf_trigs[i].name, trig->name)) {
> + dev_dbg(&indio_dev->dev, "Trigger [%d] found\n", i);
> + return stm32_mdf_trigs[i].trgsrc;
> + }
> + }
> +
> + return -EINVAL;
> +}
> +
> +static int stm32_mdf_adc_filter_set_trig(struct iio_dev *indio_dev)
> +{
> + struct stm32_mdf_adc *adc = iio_priv(indio_dev);
> + struct iio_trigger *trig = indio_dev->trig;
> + u32 trgsrc = 0;
> + /* set trigger polarity to rising edge by default */
> + u32 trgsens = STM32_MDF_TRGSENS_RISING_EDGE;
> + int ret;
> +
> + if (trig) {
> + ret = stm32_mdf_adc_get_trig(indio_dev, trig);
> + if (ret < 0)
> + return ret;
> + }
> +
> + dev_dbg(adc->dev, "Set trigger source [%d] on filter [%d]\n", trgsrc, adc->fl_id);
> +
> + return regmap_update_bits(adc->regmap, MDF_DFLTCR_REG,
> + MDF_DFLTCR_TRGSRC_MASK | MDF_DFLTCR_TRGSENS,
> + FIELD_PREP(MDF_DFLTCR_TRGSRC_MASK, trgsrc) |
> + FIELD_PREP(MDF_DFLTCR_TRGSENS, trgsens));
> +}
[Severity: High]
Does stm32_mdf_adc_filter_set_trig() leave trgsrc at 0 when a trigger is
attached? The return value of stm32_mdf_adc_get_trig() is checked in ret
but never assigned to trgsrc, so MDF_DFLTCR_REG is always programmed with
trigger source 0 (TRGO) instead of the selected external timer trigger.
> +
> +static void stm32_mdf_adc_filter_clear_trig(struct iio_dev *indio_dev)
> +{
> + struct stm32_mdf_adc *adc = iio_priv(indio_dev);
> +
> + regmap_update_bits(adc->regmap, MDF_DFLTCR_REG,
> + MDF_DFLTCR_TRGSRC_MASK | MDF_DFLTCR_TRGSENS, 0);
> +}
[Severity: Medium]
In interleaved mode, should stm32_mdf_adc_filter_set_trig() and
stm32_mdf_adc_filter_clear_trig() also update the secondary interleaved
filters in adc->mdf->filter_list, and should stm32_mdf_adc_start_conv()
skip pulsing MDF_GCR_TRGO when indio_dev->trig is set?
Unlike stm32_mdf_adc_filter_set_mode(), these helpers only write filter 0's
MDF_DFLTCR_REG, leaving secondary interleaved filters configured for
TRGSRC = 0 (TRGO). Because adc->trgo is set at probe time for interleaved
and synchronized master filters, stm32_mdf_adc_start_conv() still calls
stm32_mdf_core_trigger(adc->mdf) during buffer enable, starting the
TRGO-triggered filters immediately while filter 0 waits for the external
timer edge.
[ ... ]
> @@ -1377,6 +1479,7 @@ static const struct iio_info stm32_mdf_info_adc = {
> .write_raw = stm32_mdf_adc_write_raw,
> .read_raw = stm32_mdf_adc_read_raw,
> .read_label = stm32_mdf_adc_read_label,
> + .validate_trigger = stm32_mdf_adc_get_trig,
> };
[Severity: High]
Will wiring stm32_mdf_adc_get_trig() directly as .validate_trigger reject
every valid trigger? stm32_mdf_adc_get_trig() returns a positive trgsrc
value (0x2..0xc) on match, whereas current_trigger_store() in
drivers/iio/industrialio-trigger.c treats any non-zero return from
validate_trigger() as an error and aborts assigning indio_dev->trig.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118-upstream_mdf_v2-v2-0-7939d3c3720a@foss.st.com?part=9
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH v2 05/11] iio: adc: stm32-mdf: add digital filter device support
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
0 siblings, 0 replies; 22+ messages in thread
From: sashiko-bot @ 2026-11-18 15:40 UTC (permalink / raw)
To: Olivier Moysan
Cc: devicetree, dri-devel, linux-sound, robh, conor+dt, media-ci
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] iio: adc: stm32-mdf: unpropagated rsflt_bypass in
stm32_mdf_adc_parse_of()
- [Medium] iio: adc: stm32-mdf: unbalanced kclk prepare count for
filters in suspend
--
commit 7cf0176f9896de83d6274c225f454821ace01160
Author: Olivier Moysan <olivier.moysan@foss.st.com>
iio: adc: stm32-mdf: add digital filter device support
This commit introduces the STM32 MDF digital filter platform driver, regmap
configuration, and device tree parsing for filter parameters and BSMX serial
interface routing.
> diff --git a/drivers/iio/adc/stm32-mdf-adc.c b/drivers/iio/adc/stm32-mdf-adc.c
> new file mode 100644
> index 0000000000000..411af78911c91
> --- /dev/null
> +++ b/drivers/iio/adc/stm32-mdf-adc.c
[ ... ]
> +static int stm32_mdf_adc_parse_of(struct platform_device *pdev, struct stm32_mdf_adc *adc)
> +{
[ ... ]
> + /*
> + * In interleave mode the filters in range [1..n] share their configuration with filter 0.
> + * In this case, use config from filter 0, instead of parsing DT.
> + */
> + if (!MDF_IS_FILTER0(adc) && adc->fl_id < adc->mdf->nb_interleave) {
> + adc->interleaved = true;
> + } else {
> + ret = stm32_mdf_get_filters_config(dev, adc);
> + if (ret)
> + return ret;
> +
> + if (MDF_IS_FILTER0(adc) && adc->mdf->nb_interleave) {
> + /* Filter 0 is the TRGO trigger source in interleave mode */
> + adc->trgo = true;
> + adc->interleaved = true;
> + buf_size = adc->mdf->nb_interleave;
> + }
> + }
[Severity: Medium]
Does skipping stm32_mdf_get_filters_config() here leave adc->rsflt_bypass
unset on interleaved filters 1..n?
In commit "iio: adc: stm32-mdf: configure cic filters and sample rates",
stm32_mdf_adc_apply_filters_config() copies datsrc, cicmode, decim_cic,
nbdis, and hpf_cutoff from filter 0 to each interleaved filter, but never
copies adc->rsflt_bypass. When st,rs-filter-bypass is set on filter 0,
interleaved filters 1..n will leave MDF_DFLTRSFR_RSFLTBYP cleared while
filter 0 bypasses the reshape filter, resulting in mismatched decimation
ratios and gain across the interleaved channels.
[ ... ]
> +static int stm32_mdf_adc_probe(struct platform_device *pdev)
> +{
> + struct device *dev = &pdev->dev;
> + struct stm32_mdf_adc *adc;
> + struct iio_dev *iio;
> + struct resource *res;
> + void __iomem *base;
> + int ret;
> +
> + iio = devm_iio_device_alloc(dev, sizeof(*adc));
> + if (!iio) {
> + dev_err(dev, "Failed to allocate IIO device\n");
> + return -ENOMEM;
> + }
> + iio->modes = INDIO_DIRECT_MODE;
> +
> + adc = iio_priv(iio);
> + adc->mdf = dev_get_drvdata(dev->parent);
> +
> + platform_set_drvdata(pdev, iio);
> +
> + base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
> + if (IS_ERR(base))
> + return dev_err_probe(dev, PTR_ERR(base), "Failed to get resource\n");
> + adc->phys_addr = res->start;
> +
> + adc->regmap = stm32_mdf_regmap_init(dev, adc->mdf->kclk, base, &stm32_mdf_regmap_cfg);
[Severity: Medium]
Does attaching adc->mdf->kclk to adc->regmap here leave kclk prepared
across system suspend?
stm32_mdf_regmap_init() calls regmap_mmio_attach_clk(), which prepares
mdf->kclk once for each probed filter. However, stm32_mdf_core_suspend()
and stm32_mdf_core_resume() only unprepare and re-prepare kclk for the
core regmap and each entry in mdf->sitf_list (and stm32_mdf_adc_suspend()
in commit "iio: adc: stm32-mdf: add dma buffered acquisition support"
does not unprepare it either), preventing mdf->kclk from being unprepared
during system sleep whenever any filter is probed.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118-upstream_mdf_v2-v2-0-7939d3c3720a@foss.st.com?part=5
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH v2 04/11] iio: adc: stm32-mdf: add serial interfaces support
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
0 siblings, 0 replies; 22+ messages in thread
From: sashiko-bot @ 2026-11-18 15:42 UTC (permalink / raw)
To: Olivier Moysan
Cc: devicetree, dri-devel, linux-sound, robh, conor+dt, media-ci
Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [High] iio: adc: stm32-mdf: NULL pointer dereference in
stm32_mdf_sitf_get_clk()
- [Medium] iio: adc: stm32-mdf: off-by-one and underflow in
stm32_mdf_sitf_parse()
- [Medium] iio: adc: stm32-mdf: invalid SCKSRC and bit update in
stm32_mdf_sitf_parse()
- [Medium] iio: adc: stm32-mdf: missing max_register_is_0 and name in
sitf regmap_config
- [Medium] iio: adc: stm32-mdf: unbalanced kclk prepare count on
suspend/resume errors
- [Low] iio: adc: stm32-mdf: module self-pinning in
stm32_mdf_sitf_get_clk()
- [Low] iio: adc: stm32-mdf: unused base member in struct stm32_mdf_sitf
--
commit a885a42b631244af337425ef9681fd1c4db1960e
Author: Olivier Moysan <olivier.moysan@foss.st.com>
iio: adc: stm32-mdf: add serial interfaces support
This commit adds support for the STM32 MDF serial interfaces (SPI and
Manchester modes), including Device Tree parsing, clock selection,
suspend/resume handling, and reference-counted start and stop helpers.
> diff --git a/drivers/iio/adc/stm32-mdf-core.c b/drivers/iio/adc/stm32-mdf-core.c
> index 4e462c6c42bc8..5b5d2d4cbb5c5 100644
> --- a/drivers/iio/adc/stm32-mdf-core.c
> +++ b/drivers/iio/adc/stm32-mdf-core.c
[ ... ]
> @@ -794,7 +806,9 @@ static int stm32_mdf_core_suspend(struct device *dev)
> regcache_cache_only(priv->regmap, true);
> regcache_mark_dirty(priv->regmap);
>
> - /* Balance regmap_mmio_attach_clk() clk_prepare() */
> + /* Balance regmap_mmio_attach_clk() for the SITFs and the core. */
> + list_for_each_entry(sitf, &mdf->sitf_list, entry)
> + clk_unprepare(priv->kclk);
> clk_unprepare(priv->kclk);
>
> return pinctrl_pm_select_sleep_state(dev);
> @@ -804,6 +818,8 @@ static int stm32_mdf_core_resume(struct device *dev)
> {
> struct stm32_mdf *mdf = dev_get_drvdata(dev);
> struct stm32_mdf_priv *priv = to_stm32_mdf_priv(mdf);
> + struct stm32_mdf_sitf *sitf;
> + unsigned int prepared = 0;
> int ret;
>
> ret = pinctrl_pm_select_default_state(dev);
> @@ -825,8 +841,24 @@ static int stm32_mdf_core_resume(struct device *dev)
> goto err_cache;
> }
>
> + list_for_each_entry(sitf, &mdf->sitf_list, entry) {
> + ret = clk_prepare(priv->kclk);
> + if (ret)
> + goto err_sitf;
> + prepared++;
> +
> + ret = regmap_write(sitf->regmap, MDF_SITFCR_REG, sitf->sitfcr);
> + if (ret)
> + goto err_sitf;
> + }
> +
> return pm_runtime_force_resume(dev);
>
> +err_sitf:
> + while (prepared) {
> + clk_unprepare(priv->kclk);
> + prepared--;
> + }
> err_cache:
> clk_unprepare(priv->kclk);
> err_clk:
[Severity: Medium]
Does an error in pinctrl_pm_select_sleep_state() or in
stm32_mdf_core_resume() leave priv->kclk unprepared while the regmap_mmio
instances remain active?
Each regmap_mmio_attach_clk() call at probe holds a clk_prepare() reference
that regmap_mmio_free_context() releases on teardown. If
pinctrl_pm_select_sleep_state() fails in stm32_mdf_core_suspend(), or if
stm32_mdf_core_resume() fails and unwinds clk_prepare(priv->kclk) (while
also leaving priv->regmap out of cache-only mode), later register accesses
call clk_enable() on an unprepared clock and driver removal triggers
clk_unprepare() underflow warnings.
> diff --git a/drivers/iio/adc/stm32-mdf-serial.c b/drivers/iio/adc/stm32-mdf-serial.c
> new file mode 100644
> index 0000000000000..6ba3be0198478
> --- /dev/null
> +++ b/drivers/iio/adc/stm32-mdf-serial.c
[ ... ]
> +static const struct regmap_config stm32_sitf_regmap_cfg = {
> + .reg_bits = 32,
> + .val_bits = 32,
> + .reg_stride = sizeof(u32),
> + .max_register = MDF_SITFCR_REG,
> + .fast_io = true,
> + /* Do not use regcache as it does not support single register map */
> +};
[Severity: Medium]
Should stm32_sitf_regmap_cfg set .max_register_is_0 = true and give each
serial interface regmap a distinct .name?
Because MDF_SITFCR_REG is 0, __regmap_init() treats .max_register as
unspecified unless .max_register_is_0 = true is set, which disables upper
bounds checking on the 4-byte iomapped region (and is also why regcache
rejected the single-register map). In addition, stm32_mdf_sitf_init()
registers every sitf->regmap on the parent device with .name == NULL, so
their debugfs entries collide with priv->regmap and with each other.
[ ... ]
> +static int stm32_mdf_sitf_get_clk(struct device *dev, struct device_node *np,
> + struct stm32_mdf_sitf *sitf)
> +{
> + struct clk *sck;
> + int ret;
> +
> + /* SITF nodes have no device to apply their assigned clock configuration. */
> + ret = of_clk_set_defaults(np, false);
> + if (ret)
> + return dev_err_probe(dev, ret, "Can't configure serial clock for %pOF\n", np);
> +
> + sitf->scksrc = STM32_MDF_SCKSRC_NONE;
> + /* Optional clock. Clock not needed in Manchester mode. */
> + if (!of_property_present(np, "clocks"))
> + return 0;
> +
> + sck = devm_get_clk_from_child(dev, np, NULL);
> + if (IS_ERR(sck))
> + return dev_err_probe(dev, PTR_ERR(sck), "Can't get serial clock for %pOF\n", np);
[Severity: Low]
When the SITF clock is one of the internal CCK0 or CCK1 clocks registered
by the same driver in stm32_mdf_core_register_clock_provider(), does
devm_get_clk_from_child() pin stm32-mdf.ko to itself?
devm_get_clk_from_child() resolves the clock through OF with a NULL
consumer owner, so clk_hw_create_clk() sees owner != core->owner and calls
try_module_get() on stm32-mdf.ko, preventing rmmod until the device is
manually unbound.
> +
> + sitf->sck = sck;
> + if (!strncmp(__clk_get_name(sck), STM32_MDF_CCK0, sizeof(STM32_MDF_CCK0)))
> + sitf->scksrc = STM32_MDF_SCKSRC_CCK0;
> + else if (!strncmp(__clk_get_name(sck), STM32_MDF_CCK1, sizeof(STM32_MDF_CCK1)))
> + sitf->scksrc = STM32_MDF_SCKSRC_CCK1;
> + else
> + sitf->scksrc = STM32_MDF_SCKSRC_CLK;
> +
> + return 0;
> +}
[Severity: High]
Can sck be NULL here, causing a NULL pointer dereference in strncmp()?
In stm32_mdf_core_register_clock_provider(), clk_data->num is set to
STM32_MDF_MAX_CCK while only clocks listed in clock-output-names are
populated in clk_data->hws[]:
drivers/iio/adc/stm32-mdf-core.c:stm32_mdf_core_register_clock_provider() {
...
clk_data->hws[index] = mdf_hw[clk_id];
...
index++;
}
clk_data->num = STM32_MDF_MAX_CCK;
}
If a sitf node references an unpopulated CCK index, of_clk_hw_onecell_get()
returns NULL and devm_get_clk_from_child() returns sck = NULL. IS_ERR(sck)
does not catch NULL, so __clk_get_name(sck) returns NULL and strncmp()
dereferences it.
> +
> +static int stm32_mdf_sitf_parse(struct device *dev, struct device_node *np,
> + struct stm32_mdf_sitf *sitf)
> +{
> + const char *str;
> + int ret, mode;
> + u32 idx, sitfcr;
> +
> + ret = of_property_read_u32(np, "reg", &idx);
> + if (ret)
> + return dev_err_probe(dev, ret, "Could not get interface index for %pOF\n", np);
> +
> + if (idx % 0x80) {
> + dev_err(dev, "Unexpected reg property value [%x]\n", idx);
> + return -EINVAL;
> + }
> +
> + idx = (idx >> 7) - 1;
> + if (idx > sitf->mdf->nbf) {
> + dev_err(dev, "Interface index [%d] exceeds maximum [%d]\n", idx, sitf->mdf->nbf);
> + return -EINVAL;
> + }
[Severity: Medium]
Should the bounds check on the 0-based idx be idx >= sitf->mdf->nbf?
With nbf serial interfaces at offsets 0x80 .. 0x80 * nbf, valid 0-based
indices are 0 .. sitf->mdf->nbf - 1, so idx > sitf->mdf->nbf is off by one
and accepts idx == sitf->mdf->nbf. Also, if reg is 0, idx % 0x80 is 0 and
(idx >> 7) - 1 underflows u32 idx to U32_MAX, which %d prints as -1.
[ ... ]
> + sitf->id = idx;
> + sitf->node = of_fwnode_handle(np);
> + spin_lock_init(&sitf->lock);
> +
> + sitfcr |= FIELD_PREP(MDF_SITFCR_SCKSRC_MASK, sitf->scksrc);
> +
> + return regmap_set_bits(sitf->regmap, MDF_SITFCR_REG, sitfcr);
> +}
[Severity: Medium]
Does this write the software sentinel STM32_MDF_SCKSRC_NONE into
MDF_SITFCR_SCKSRC_MASK when no clock is provided in Manchester mode, and
leave stale bits set in MDF_SITFCR_REG?
When the optional clocks property is absent, sitf->scksrc is
STM32_MDF_SCKSRC_NONE (3), which FIELD_PREP() encodes as reserved value
0b11 in SCKSRC[1:0]. Furthermore, regmap_set_bits() only sets 1-bits in
sitfcr and never clears 0-bits in MDF_SITFCR_SITFMOD_MASK |
MDF_SITFCR_SCKSRC_MASK (and performs no write at all when sitfcr is 0);
should this use regmap_update_bits() with that mask instead?
> diff --git a/drivers/iio/adc/stm32-mdf.h b/drivers/iio/adc/stm32-mdf.h
> index e3617b181e28a..fa689954d7c15 100644
> --- a/drivers/iio/adc/stm32-mdf.h
> +++ b/drivers/iio/adc/stm32-mdf.h
[ ... ]
> +struct stm32_mdf_sitf {
> + struct list_head entry;
> + struct device *dev;
> + struct regmap *regmap;
> + struct stm32_mdf *mdf;
> + struct fwnode_handle *node;
> + struct clk *sck;
> + void __iomem *base;
> + spinlock_t lock; /* Manage race conditions on sitf state */
[Severity: Low]
This isn't a bug, but base in struct stm32_mdf_sitf is never read or
written and can be dropped.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118-upstream_mdf_v2-v2-0-7939d3c3720a@foss.st.com?part=4
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH v2 02/11] iio: adc: stm32-mdf: add core registration
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
0 siblings, 0 replies; 22+ messages in thread
From: sashiko-bot @ 2026-11-18 15:45 UTC (permalink / raw)
To: Olivier Moysan
Cc: devicetree, dri-devel, linux-sound, robh, conor+dt, media-ci
Thank you for your contribution! Sashiko AI review found 12 potential
issues to consider:
- [High] iio: adc: stm32-mdf: reset in stm32_mdf_core_parse_of() breaks
regcache/probe
- [Medium] iio: adc: stm32-mdf: unchecked read and missing rollback in
start_mdf()
- [Medium] iio: adc: stm32-mdf: lockless n_active_ch races in
start_mdf() and stop_mdf()
- [Medium] iio: adc: stm32-mdf: unlocked regcache_cache_bypass() races
with trigger()
- [Medium] iio: adc: stm32-mdf: missing state rollback on suspend and
resume failure
- [Medium] iio: adc: stm32-mdf: unmapped resource 1 read in
stm32_mdf_core_probe()
- [Low] iio: adc: stm32-mdf: unused PM callbacks when CONFIG_PM_SLEEP or
CONFIG_PM=n
- [Low] iio: adc: stm32-mdf: drvdata pointer type mismatch in
stm32_mdf_core_probe()
- [Low] iio: adc: stm32-mdf: unchecked regmap_read() for MDF_VERR_REG
- [Low] iio: adc: stm32-mdf: guard() mixed with goto in
stm32_mdf_core_trigger()
- [Low] iio: adc: stm32-mdf: invalid OPTION() macro in
MDF_OR_OPTION_MASK
- [Low] iio: adc: stm32-mdf: missing header include guards in
stm32-mdf.h
--
commit 016595fa648050f874751e37d47a5cfa33a78bde
Author: Olivier Moysan <olivier.moysan@foss.st.com>
iio: adc: stm32-mdf: add core registration
This commit adds the core driver for the STM32 multifunction digital
filter (MDF) to manage shared registers, clocks, and power resources on
STM32MP2.
> diff --git a/drivers/iio/adc/stm32-mdf-core.c b/drivers/iio/adc/stm32-mdf-core.c
> new file mode 100644
> index 0000000000000..ca50ce566a435
> --- /dev/null
> +++ b/drivers/iio/adc/stm32-mdf-core.c
[ ... ]
> +int stm32_mdf_core_start_mdf(struct stm32_mdf *mdf)
> +{
> + struct stm32_mdf_priv *priv = to_stm32_mdf_priv(mdf);
> + struct device *dev = &priv->pdev->dev;
> + int ret;
> + u32 val;
> +
> + if (atomic_inc_return(&priv->n_active_ch) == 1) {
> + ret = pm_runtime_resume_and_get(dev);
> + if (ret < 0)
> + goto err;
> +
> + /* Enable PROCDIV and CCKDIV clock dividers */
> + ret = regmap_set_bits(priv->regmap, MDF_CKGCR_REG, MDF_CKG_CKGDEN);
> + if (ret < 0)
> + goto pm_put;
> +
> + /* Check clock status. Bypass cache to access volatile CKGACTIVE active bit */
> + regcache_cache_bypass(priv->regmap, true);
> + regmap_read(priv->regmap, MDF_CKGCR_REG, &val);
> + regcache_cache_bypass(priv->regmap, false);
> + if (!(val & MDF_CKG_ACTIVE)) {
> + ret = -EINVAL;
> + dev_err(dev, "MDF clock not active\n");
> + goto pm_put;
> + }
[Severity: Medium]
What happens if regmap_read() fails here, or if MDF_CKG_ACTIVE is not set?
The return value of regmap_read() is not checked, so a read failure tests
an uninitialized val on the stack.
In addition, jumping to pm_put when MDF_CKG_ACTIVE is not set leaves
MDF_CKG_CKGDEN set in hardware and in REGCACHE_FLAT, so subsequent
non-forced regmap_set_bits() calls will skip writing MDF_CKG_CKGDEN.
> +
> + if (!priv->exclusive_flg) {
> + clk_rate_exclusive_get(priv->kclk);
> + priv->exclusive_flg = true;
> + }
> + }
> +
> + return 0;
> +
> +pm_put:
> + pm_runtime_put_sync(dev);
> +err:
> + atomic_dec(&priv->n_active_ch);
> +
> + return ret;
> +}
> +EXPORT_SYMBOL_GPL(stm32_mdf_core_start_mdf);
> +
> +/**
> + * stm32_mdf_core_trigger() - Trigger MDF filters
> + * @mdf: MDF instance
> + *
> + * Trigger MDF filters via TRGO trigger.
> + *
> + * Return: 0 on success, or a negative error code on failure.
> + */
> +int stm32_mdf_core_trigger(struct stm32_mdf *mdf)
> +{
> + struct stm32_mdf_priv *priv = to_stm32_mdf_priv(mdf);
> + int ret;
> + u32 val;
> +
> + guard(spinlock)(&priv->lock);
> +
> + /* Bypass cache to access TRGO volatile bit */
> + regcache_cache_bypass(priv->regmap, true);
> +
> + ret = regmap_read(priv->regmap, MDF_GCR_REG, &val);
> + if (ret < 0)
> + goto err;
> +
> + if (val & MDF_GCR_TRGO) {
> + ret = -EBUSY;
> + goto err;
> + }
> +
> + ret = regmap_set_bits(priv->regmap, MDF_GCR_REG, MDF_GCR_TRGO);
> +
> +err:
> + regcache_cache_bypass(priv->regmap, false);
> +
> + return ret;
> +}
[Severity: Medium]
Can regcache_cache_bypass() in stm32_mdf_core_start_mdf() race with
stm32_mdf_core_trigger()?
stm32_mdf_core_trigger() takes priv->lock before toggling map-wide
cache_bypass on priv->regmap, but stm32_mdf_core_start_mdf() and
stm32_mdf_core_stop_mdf() access priv->regmap and toggle cache_bypass
without holding priv->lock:
CPU0 CPU1
stm32_mdf_core_trigger() stm32_mdf_core_start_mdf()
guard(spinlock)(&priv->lock)
regcache_cache_bypass(..., true)
regmap_read(..., MDF_GCR_REG, ...)
regcache_cache_bypass(..., true)
regmap_read(..., MDF_CKGCR_REG, ...)
regcache_cache_bypass(..., false)
regmap_set_bits(..., MDF_GCR_TRGO)
<-- cached in REGCACHE_FLAT
With MDF_GCR_TRGO cached in REGCACHE_FLAT, later regcache_sync() calls on
resume will re-trigger the filters.
[Severity: Low]
This isn't a bug, but stm32_mdf_core_trigger() mixes guard(spinlock) with
goto err error handling in the same function.
> +EXPORT_SYMBOL_GPL(stm32_mdf_core_trigger);
> +
> +/**
> + * stm32_mdf_core_stop_mdf() - Stop an MDF channel
> + * @mdf: MDF instance
> + *
> + * Drop one active-channel reference. When the last channel stops, release the
> + * exclusive kernel clock rate and disable the MDF clock.
> + *
> + * Return: 0 on success, or a negative error code on failure.
> + */
> +int stm32_mdf_core_stop_mdf(struct stm32_mdf *mdf)
> +{
> + struct stm32_mdf_priv *priv = to_stm32_mdf_priv(mdf);
> + int ret = 0;
> +
> + if (atomic_dec_and_test(&priv->n_active_ch)) {
> + if (priv->exclusive_flg) {
> + clk_rate_exclusive_put(priv->kclk);
> + priv->exclusive_flg = false;
> + }
> +
> + ret = regmap_clear_bits(priv->regmap, MDF_CKGCR_REG, MDF_CKG_CKGDEN);
> +
> + pm_runtime_put_sync(&priv->pdev->dev);
> + }
> +
> + return ret;
> +}
[Severity: Medium]
Can concurrent calls to stm32_mdf_core_start_mdf() and
stm32_mdf_core_stop_mdf() race on priv->n_active_ch?
Only the atomic counter update is serialized, not the hardware startup and
teardown sequences gated by the 0->1 and 1->0 transitions:
CPU0 CPU1
stm32_mdf_core_start_mdf() stm32_mdf_core_start_mdf()
atomic_inc_return() == 1
atomic_inc_return() == 2
return 0 <-- clocks not enabled yet
pm_runtime_resume_and_get()
...
A similar race occurs when one channel calls stm32_mdf_core_stop_mdf()
(decrementing n_active_ch from 1 to 0) concurrently with another channel
calling stm32_mdf_core_start_mdf() (incrementing n_active_ch from 0 to 1),
allowing stm32_mdf_core_stop_mdf() to clear MDF_CKG_CKGDEN and call
pm_runtime_put_sync() after the new channel has started. Using a mutex to
serialize start and stop would avoid both races.
[ ... ]
> +static int stm32_mdf_core_parse_of(struct platform_device *pdev, struct stm32_mdf_priv *priv)
> +{
> + struct device_node *node = pdev->dev.of_node;
> + struct reset_control *rst;
> + struct device *dev = &pdev->dev;
> + u32 count;
> + int ret;
> +
> + if (!node)
> + return -EINVAL;
> +
> + rst = devm_reset_control_get_optional_exclusive(&pdev->dev, NULL);
> + if (IS_ERR(rst))
> + return dev_err_probe(dev, PTR_ERR(rst), "Failed to get reset controller\n");
> +
> + ret = reset_control_reset(rst);
> + if (ret)
> + return dev_err_probe(dev, ret, "Failed to reset MDF\n");
[Severity: High]
Does reset_control_reset() fail with -ENOTSUPP on STM32MP25 when resets is
present in the device tree?
The STM32 RCC reset controller driver only implements .assert, .deassert,
and .status:
drivers/clk/stm32/reset-stm32.c:
static const struct reset_control_ops stm32_reset_ops = {
.assert = stm32_reset_assert,
.deassert = stm32_reset_deassert,
.status = stm32_reset_status,
};
Because .reset is NULL, reset_control_reset() returns -ENOTSUPP and aborts
probe.
In addition, stm32_mdf_core_probe() calls stm32_mdf_regmap_init() and
stm32_mdf_core_identification() before stm32_mdf_core_parse_of() resets
the hardware. Reading identification registers and populating
REGCACHE_FLAT before deasserting reset reads registers while the block may
still be in reset, and resetting afterward clears hardware registers behind
the initialized cache.
[ ... ]
> +static int stm32_mdf_core_identification(struct platform_device *pdev, struct stm32_mdf_priv *priv)
> +{
> + struct stm32_mdf *mdf = &priv->mdf;
> + u32 val;
> + int ret;
> +
> + /* If filter number is explicitly defined, don't check identification registers */
> + mdf->nbf = ((unsigned long)device_get_match_data(&pdev->dev));
> + if (mdf->nbf)
> + return 0;
> +
> + ret = regmap_read(priv->regmap, MDF_IPIDR_REG, &val);
> + if (ret)
> + return ret;
> +
> + if (val == STM32MP25_MDF_IPIDR_NUMBER) {
> + ret = regmap_read(priv->regmap, MDF_HWCFGR_REG, &val);
> + if (ret)
> + return ret;
> +
> + mdf->nbf = FIELD_GET(MDF_HWCFGR_NBF_MASK, val);
> +
> + ret = regmap_read(priv->regmap, MDF_VERR_REG, &val);
> +
> + dev_dbg(&pdev->dev, "MDF version: %lu.%lu\n", FIELD_GET(MDF_VERR_MAJREV_MASK, val),
> + FIELD_GET(MDF_VERR_MINREV_MASK, val));
[Severity: Low]
Should ret be checked after reading MDF_VERR_REG? Right now ret is
assigned and ignored, so a failed read logs stale bits from MDF_HWCFGR_REG
and returns 0.
[ ... ]
> +static int stm32_mdf_core_probe(struct platform_device *pdev)
> +{
> + struct stm32_mdf_priv *priv;
> + struct resource *res;
> + int ret;
> +
> + priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
> + if (!priv)
> + return -ENOMEM;
> + priv->pdev = pdev;
> + spin_lock_init(&priv->lock);
> +
> + priv->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
> + if (IS_ERR(priv->base))
> + return PTR_ERR(priv->base);
> + priv->phys_base = res->start;
> +
> + priv->kclk = devm_clk_get(&pdev->dev, NULL);
> + if (IS_ERR(priv->kclk))
> + return dev_err_probe(&pdev->dev, PTR_ERR(priv->kclk),
> + "Failed to get kernel clock\n");
> + priv->mdf.kclk = priv->kclk;
> +
> + priv->regmap = stm32_mdf_regmap_init(&pdev->dev, priv->kclk, priv->base,
> + &stm32_mdf_regmap_cfg);
[Severity: Medium]
Does stm32_mdf_regmap_init() read past the end of the ioremapped resource
0 region?
In the DT binding (Documentation/devicetree/bindings/iio/adc/st,stm32mp25-mdf.yaml),
resource 0 is only 8 bytes (reg = <0x504d0000 0x8>, <0x504d0ff0 0x10>),
while the identification registers at 0xff0..0xffc are in resource 1.
stm32_mdf_core_probe() only maps resource 0, yet stm32_mdf_regmap_cfg sets
max_register = MDF_SIDR_REG (0xffc) with REGCACHE_FLAT, causing both
regcache_hw_init() and stm32_mdf_core_identification() to read offsets
0xff0..0xffc from priv->base without requesting or mapping resource 1.
[ ... ]
> + INIT_LIST_HEAD(&priv->mdf.filter_list);
> +
> + platform_set_drvdata(pdev, priv);
[Severity: Low]
Should platform_set_drvdata() pass &priv->mdf instead of priv?
stm32_mdf_core_suspend(), stm32_mdf_core_resume(), and child filter
drivers treat dev_get_drvdata() as a struct stm32_mdf * pointer and pass
it to to_stm32_mdf_priv(), which only works today because mdf happens to
be the first member of struct stm32_mdf_priv.
[ ... ]
> +static int stm32_mdf_core_suspend(struct device *dev)
> +{
> + struct stm32_mdf *mdf = dev_get_drvdata(dev);
> + struct stm32_mdf_priv *priv = to_stm32_mdf_priv(mdf);
> + int ret;
> +
> + ret = pm_runtime_force_suspend(dev);
> + if (ret)
> + return ret;
> +
> + regcache_cache_only(priv->regmap, true);
> + regcache_mark_dirty(priv->regmap);
> +
> + /* Balance regmap_mmio_attach_clk() clk_prepare() */
> + clk_unprepare(priv->kclk);
> +
> + return pinctrl_pm_select_sleep_state(dev);
> +}
> +
> +static int stm32_mdf_core_resume(struct device *dev)
> +{
> + struct stm32_mdf *mdf = dev_get_drvdata(dev);
> + struct stm32_mdf_priv *priv = to_stm32_mdf_priv(mdf);
> + int ret;
> +
> + ret = pinctrl_pm_select_default_state(dev);
> + if (ret) {
> + dev_err(dev, "Failed to set pins default state: %d\n", ret);
> + return ret;
> + }
> +
> + ret = clk_prepare(priv->kclk);
> + if (ret) {
> + dev_err(dev, "Failed to prepare kernel clock: %d\n", ret);
> + goto err_clk;
> + }
> +
> + regcache_cache_only(priv->regmap, false);
> + ret = regcache_sync(priv->regmap);
> + if (ret) {
> + dev_err(dev, "Failed to sync cache: %d\n", ret);
> + goto err_cache;
> + }
> +
> + return pm_runtime_force_resume(dev);
> +
> +err_cache:
> + clk_unprepare(priv->kclk);
> +err_clk:
> + pinctrl_pm_select_sleep_state(dev);
> +
> + return ret;
> +}
[Severity: Medium]
If pinctrl_pm_select_sleep_state() fails in stm32_mdf_core_suspend(), does
the device stay in cache-only mode with priv->kclk unprepared? When
.suspend returns an error, .resume is not called, so failing without
re-preparing priv->kclk and restoring regcache_cache_only(false) and
pm_runtime_force_resume() leaves the clock prepare count unbalanced on
teardown.
Similarly, in stm32_mdf_core_resume(), if regcache_sync() fails, err_cache
unprepares priv->kclk while leaving regcache_cache_only(priv->regmap,
false) set.
> +
> +static int stm32_mdf_core_runtime_suspend(struct device *dev)
> +{
> + return 0;
> +}
> +
> +static int stm32_mdf_core_runtime_resume(struct device *dev)
> +{
> + return 0;
> +}
> +
> +static const struct dev_pm_ops stm32_mdf_core_pm_ops = {
> + SET_SYSTEM_SLEEP_PM_OPS(stm32_mdf_core_suspend, stm32_mdf_core_resume)
> + SET_RUNTIME_PM_OPS(stm32_mdf_core_runtime_suspend, stm32_mdf_core_runtime_resume, NULL)
> +};
> +
> +static struct platform_driver stm32_mdf_driver = {
> + .probe = stm32_mdf_core_probe,
> + .remove = stm32_mdf_core_remove,
> + .driver = {
> + .name = "stm32-mdf",
> + .of_match_table = stm32_mdf_of_match,
> + .pm = &stm32_mdf_core_pm_ops,
> + },
> +};
[Severity: Low]
Building with CONFIG_PM_SLEEP=n or CONFIG_PM=n (for example under
COMPILE_TEST) will warn about unused static PM callbacks because
SET_SYSTEM_SLEEP_PM_OPS() and SET_RUNTIME_PM_OPS() compile out the
references. Using SYSTEM_SLEEP_PM_OPS(), RUNTIME_PM_OPS(), and
pm_ptr(&stm32_mdf_core_pm_ops) allows the compiler to drop them cleanly
when PM is disabled.
> diff --git a/drivers/iio/adc/stm32-mdf.h b/drivers/iio/adc/stm32-mdf.h
> new file mode 100644
> index 0000000000000..b334769f8b175
> --- /dev/null
> +++ b/drivers/iio/adc/stm32-mdf.h
[ ... ]
> +/* MDF_OR: Option Register */
> +#define MDF_OR_OPTION_MASK OPTION(31, 0)
[Severity: Low]
Should OPTION(31, 0) be GENMASK(31, 0)? OPTION() is not defined anywhere,
so any use of MDF_OR_OPTION_MASK will fail to build.
[ ... ]
> +struct stm32_mdf {
> + struct list_head filter_list;
> + struct clk *kclk;
> + unsigned long fproc;
> + unsigned int nbf;
> + unsigned int nb_interleave;
> +};
[Severity: Low]
stm32-mdf.h has no #ifndef/#define/#endif include guards, so including it
more than once in a translation unit will cause redefinition errors for
struct stm32_mdf and stm32_mdf_regmap_init().
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118-upstream_mdf_v2-v2-0-7939d3c3720a@foss.st.com?part=2
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH v2 03/11] iio: adc: stm32-mdf: add cck clocks provider
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
0 siblings, 0 replies; 22+ messages in thread
From: sashiko-bot @ 2026-11-18 15:50 UTC (permalink / raw)
To: Olivier Moysan
Cc: devicetree, dri-devel, linux-sound, robh, conor+dt, media-ci
Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [High] iio: adc: stm32-mdf: premature rounding in
stm32_mdf_core_cck_determine_rate()
- [High] iio: adc: stm32-mdf: ABBA deadlock between CCF enable_lock and
regmap spinlock
- [High] iio: adc: stm32-mdf: broken onecell indexing and __counted_by
in clock provider
- [Medium] iio: adc: stm32-mdf: unlocked RMW and unchecked read in
clk_cckx_gate_ops
- [Medium] iio: adc: stm32-mdf: static global clk_hw structs and stack
init data in probe
- [Medium] iio: adc: stm32-mdf: unbalanced kclk exclusivity in
lock/unlock_kclk_rate()
--
commit aeca8d3955d2508e74c0c672d9e5746542fb1914
Author: Olivier Moysan <olivier.moysan@foss.st.com>
iio: adc: stm32-mdf: add cck clocks provider
This commit registers the MDF CCK0 and CCK1 clock outputs with the common
clock framework and adds helpers to lock, unlock, query and restore the
clock rate.
> diff --git a/drivers/iio/adc/stm32-mdf-core.c b/drivers/iio/adc/stm32-mdf-core.c
> index ca50ce566a435..4e462c6c42bc8 100644
> --- a/drivers/iio/adc/stm32-mdf-core.c
> +++ b/drivers/iio/adc/stm32-mdf-core.c
[ ... ]
> @@ -194,6 +210,96 @@ int stm32_mdf_core_trigger(struct stm32_mdf *mdf)
[ ... ]
> +int stm32_mdf_core_lock_kclk_rate(struct stm32_mdf *mdf)
> +{
> + struct stm32_mdf_priv *priv = to_stm32_mdf_priv(mdf);
> + struct device *dev = &priv->pdev->dev;
> + int ret;
> +
> + if (!atomic_read(&priv->n_active_ch)) {
> + /* Request rate exclusivity on kernel clock right now */
> + ret = clk_rate_exclusive_get(priv->kclk);
> + if (ret < 0) {
> + dev_err(dev, "Failed to get exclusive rate on kernel clock: [%d]\n", ret);
> + return ret;
> + }
> +
> + priv->exclusive_flg = true;
> + }
> +
> + return 0;
> +}
> +EXPORT_SYMBOL_GPL(stm32_mdf_core_lock_kclk_rate);
> +
> +/**
> + * stm32_mdf_core_unlock_kclk_rate() - Release the MDF kernel clock rate lock
> + * @mdf: MDF instance
> + *
> + * Release an exclusive rate acquired by stm32_mdf_core_lock_kclk_rate() when
> + * no MDF channels are active. The lock is retained while channels are active.
> + */
> +void stm32_mdf_core_unlock_kclk_rate(struct stm32_mdf *mdf)
> +{
> + struct stm32_mdf_priv *priv = to_stm32_mdf_priv(mdf);
> +
> + if (!atomic_read(&priv->n_active_ch)) {
> + clk_rate_exclusive_put(priv->kclk);
> + priv->exclusive_flg = false;
> + }
> +}
> +EXPORT_SYMBOL_GPL(stm32_mdf_core_unlock_kclk_rate);
[Severity: Medium]
Can concurrent calls across sibling MDF filter devices unbalance
clk_rate_exclusive_get() and clk_rate_exclusive_put() on priv->kclk?
Neither stm32_mdf_core_lock_kclk_rate() nor
stm32_mdf_core_unlock_kclk_rate() takes a lock or checks
priv->exclusive_flg:
CPU0 (Filter A) CPU1 (Filter B)
stm32_mdf_core_start_mdf()
n_active_ch becomes 1
clk_rate_exclusive_get()
priv->exclusive_flg = true
stm32_mdf_core_lock_kclk_rate()
/* n_active_ch != 0: does nothing */
stm32_mdf_core_stop_mdf()
n_active_ch becomes 0
clk_rate_exclusive_put()
priv->exclusive_flg = false
stm32_mdf_core_unlock_kclk_rate()
/* n_active_ch == 0 */
clk_rate_exclusive_put() <-- underflow
That extra put triggers WARN_ON(clk->exclusive_count <= 0) in
clk_rate_exclusive_put(). Conversely, if two idle filter devices call
stm32_mdf_core_lock_kclk_rate() while n_active_ch is 0,
clk_rate_exclusive_get() runs twice while priv->exclusive_flg only records
a single boolean; if a channel starts before they unlock,
stm32_mdf_core_stop_mdf() only puts once and leaks an exclusive reference,
causing later clk_set_rate() calls in stm32_mdf_core_restore_cck() to fail.
[ ... ]
> @@ -223,11 +329,301 @@ int stm32_mdf_core_stop_mdf(struct stm32_mdf *mdf)
> }
> EXPORT_SYMBOL_GPL(stm32_mdf_core_stop_mdf);
>
> +static int stm32_mdf_core_cck_divider_set_rate(struct platform_device *pdev,
> + struct stm32_mdf_priv *priv,
> + unsigned long rate,
> + unsigned long parent_rate)
> +{
> + struct device *dev = &pdev->dev;
> + unsigned long ratio, delta, delta_ppm, procdiv;
> + unsigned int cckdiv;
> + int ret;
> +
> + if (!rate || rate > STM32_MDF_MAX_CCK_RATE || !parent_rate)
> + return -EINVAL;
> +
> + ratio = DIV_ROUND_CLOSEST(parent_rate, rate);
> + if (!ratio) {
> + dev_err(dev, "CCK frequency above kernel frequency\n");
> + return -EINVAL;
> + }
> +
> + delta = abs(parent_rate - (ratio * rate));
> +
> + delta_ppm = (1000000 * delta) / parent_rate;
> + if (delta_ppm > 1000) {
> + /* If frequency deviation is higher than 1000 ppm return error */
> + dev_dbg(dev, "CCK clock deviation [%lu] ppm too large: [%lu] vs [%lu] Hz\n",
> + delta_ppm, DIV_ROUND_CLOSEST(parent_rate, ratio), rate);
> + return -EINVAL;
> + } else if (delta_ppm) {
> + dev_dbg(dev, "CCK clock frequency not accurate: [%lu] ppm deviation\n", delta_ppm);
> + }
> +
> + /*
> + * The total divider ratio must be split between proc divider and
> + * cck divider. Try to maximize cck divider first, to help fulfilling
> + * frequency ratio requirements between fproc and fcck.
> + */
> + cckdiv = min_t(unsigned long, ratio, MDF_CKG_CCKDIV_MAX);
> + while (ratio % cckdiv)
> + cckdiv--;
> + procdiv = ratio / cckdiv;
> +
> + if (procdiv > MDF_PROCDIV_MAX) {
> + dev_err(dev, "Proc divider out of range: %lu > %lu\n", procdiv,
> + MDF_PROCDIV_MAX);
> + return -EINVAL;
> + }
> +
> + priv->mdf.fproc = DIV_ROUND_CLOSEST(parent_rate, procdiv);
> +
> + /* Configure CKGCR register */
> + dev_dbg(dev, "Set MDF dividers: CCKDIV [%u], PROCDIV [%lu]\n", cckdiv, procdiv);
> + ret = regmap_update_bits(priv->regmap, MDF_CKGCR_REG,
> + MDF_CKG_CCKDIV_MASK | MDF_CKG_PROCDIV_MASK,
> + FIELD_PREP(MDF_CKG_CCKDIV_MASK, cckdiv - 1) |
> + FIELD_PREP(MDF_CKG_PROCDIV_MASK, procdiv - 1));
> + if (!ret)
> + priv->cck_freq_actual = DIV_ROUND_CLOSEST(parent_rate, ratio);
> +
> + return ret;
> +}
> +
> +static int stm32_mdf_core_cck_set_rate(struct clk_hw *hw, unsigned long rate,
> + unsigned long parent_rate)
> +{
> + struct clk_mdf_cck *clk = to_clk_mdf_cck(hw);
> + struct stm32_mdf_priv *priv = clk->priv;
> +
> + return stm32_mdf_core_cck_divider_set_rate(priv->pdev, priv, rate, parent_rate);
> +}
> +
> +static int stm32_mdf_core_cck_determine_rate(struct clk_hw *hw,
> + struct clk_rate_request *req)
> +{
> + struct clk_mdf_cck *clk = to_clk_mdf_cck(hw);
> + struct stm32_mdf_priv *priv = clk->priv;
> + struct platform_device *pdev = priv->pdev;
> + unsigned long ratio;
> +
> + if (!req->rate || req->rate > STM32_MDF_MAX_CCK_RATE || !req->best_parent_rate)
> + return -EINVAL;
> +
> + ratio = DIV_ROUND_CLOSEST(req->best_parent_rate, req->rate);
> + if (!ratio) {
> + dev_err(&pdev->dev, "CCK frequency above kernel frequency\n");
> + return -EINVAL;
> + }
> +
> + req->rate = DIV_ROUND_CLOSEST(req->best_parent_rate, ratio);
> +
> + return 0;
> +}
[Severity: High]
Does rounding req->rate in stm32_mdf_core_cck_determine_rate() bypass the
1000 ppm accuracy check and divider validation in
stm32_mdf_core_cck_divider_set_rate()?
During clk_set_rate(), clk_calc_new_rates() calls
stm32_mdf_core_cck_determine_rate() first and stores the rounded req->rate
in core->new_rate. clk_change_rate() then passes that already-rounded
core->new_rate (not the caller's requested rate) to
stm32_mdf_core_cck_set_rate(), and ignores the return value of
core->ops->set_rate(). As a result:
- Inside stm32_mdf_core_cck_divider_set_rate(), rate is already
DIV_ROUND_CLOSEST(parent_rate, ratio), so delta only measures the integer
division remainder rather than the deviation from the requested frequency,
defeating the 1000 ppm check.
- If procdiv > MDF_PROCDIV_MAX, stm32_mdf_core_cck_divider_set_rate()
returns -EINVAL, which clk_change_rate() ignores; clk_set_rate() returns
0 while MDF_CKGCR_REG and priv->cck_freq_actual remain unconfigured.
Also, in stm32_mdf_core_cck_divider_set_rate(), 1000000 * delta is computed
in unsigned long and overflows on 32-bit builds when delta >= 4295 Hz, and
priv->mdf.fproc is updated before checking whether regmap_update_bits()
succeeded. Should the 1000 ppm and procdiv <= MDF_PROCDIV_MAX checks be
performed in stm32_mdf_core_cck_determine_rate() using 64-bit arithmetic?
[ ... ]
> +static inline u32 clk_cckx_gate_readl(struct clk_cckx_gate *gate)
> +{
> + u32 val;
> +
> + regmap_read(gate->map, gate->map_offset, &val);
> +
> + return val;
> +}
> +
> +static inline void clk_cckx_gate_writel(struct clk_cckx_gate *gate, u32 val)
> +{
> + regmap_write(gate->map, gate->map_offset, val);
> +}
> +
> +static void clk_cckx_gate_endisable(struct clk_hw *hw, int enable)
> +{
> + struct clk_cckx_gate *gate = to_clk_cckx_gate(hw);
> + unsigned long flags = 0;
> + u32 reg;
> +
> + if (gate->lock)
> + spin_lock_irqsave(gate->lock, flags);
> + else
> + __acquire(gate->lock);
> +
> + reg = clk_cckx_gate_readl(gate);
> +
> + if (enable)
> + reg |= BIT(gate->bit_idx);
> + else
> + reg &= ~BIT(gate->bit_idx);
> +
> + clk_cckx_gate_writel(gate, reg);
> +
> + if (gate->lock)
> + spin_unlock_irqrestore(gate->lock, flags);
> + else
> + __release(gate->lock);
> +}
> +
> +static int clk_cckx_gate_enable(struct clk_hw *hw)
> +{
> + clk_cckx_gate_endisable(hw, 1);
> +
> + return 0;
> +}
> +
> +static void clk_cckx_gate_disable(struct clk_hw *hw)
> +{
> + clk_cckx_gate_endisable(hw, 0);
> +}
> +
> +static int clk_cckx_gate_is_enabled(struct clk_hw *hw)
> +{
> + struct clk_cckx_gate *gate = to_clk_cckx_gate(hw);
> + u32 reg;
> +
> + reg = clk_cckx_gate_readl(gate);
> +
> + reg &= BIT(gate->bit_idx);
> +
> + return reg ? 1 : 0;
> +}
[Severity: High]
Can calling regmap_read() and regmap_write() on priv->regmap inside
clk_cckx_gate_ops cause an ABBA deadlock with the clock framework's
enable_lock?
priv->regmap uses .fast_io = true (map->spinlock) and attaches priv->kclk
via regmap_mmio_attach_clk() in stm32_mdf_regmap_init(). Every hardware
access on priv->regmap acquires map->spinlock first and then calls
clk_enable(ctx->clk) in regmap_mmio_read()/regmap_mmio_write(), which takes
the CCF global enable_lock. Conversely, CCF calls .enable, .disable and
.is_enabled in clk_cckx_gate_ops while already holding enable_lock:
CPU0 CPU1
---- ----
lock(&map->spinlock)
lock(&enable_lock)
lock(&map->spinlock)
lock(&enable_lock)
*** DEADLOCK ***
CPU0: stm32_mdf_core_trigger() -> regmap_read() -> regmap_mmio_read() ->
clk_enable()
CPU1: clk_enable() -> clk_cckx_gate_enable() -> clk_cckx_gate_endisable() ->
regmap_read()
> +static const struct clk_ops clk_cckx_gate_ops = {
> + .enable = clk_cckx_gate_enable,
> + .disable = clk_cckx_gate_disable,
> + .is_enabled = clk_cckx_gate_is_enabled,
> +};
> +
> +static struct clk_cckx_gate clk_cck0 = {
> + .map_offset = MDF_CKGCR_REG,
> + .bit_idx = 1,
> +};
> +
> +static struct clk_cckx_gate clk_cck1 = {
> + .map_offset = MDF_CKGCR_REG,
> + .bit_idx = 2,
> +};
[Severity: Medium]
Does clk_cckx_gate_endisable() perform an unlocked read-modify-write on
MDF_CKGCR_REG using an unchecked regmap_read()?
clk_cck0.lock and clk_cck1.lock are never initialized, so gate->lock is
always NULL and no lock is held between clk_cckx_gate_readl() and
clk_cckx_gate_writel(). A concurrent update to MDF_CKGCR_REG in
stm32_mdf_core_start_mdf(), stm32_mdf_core_stop_mdf() or
stm32_mdf_core_cck_divider_set_rate() can be overwritten with stale bits.
In addition, clk_cckx_gate_readl() leaves val uninitialized on the stack and
ignores the return value of regmap_read(), which would write uninitialized
stack data to MDF_CKGCR_REG if the read fails.
> +static int stm32_mdf_core_register_clock_provider(struct platform_device *pdev,
> + struct stm32_mdf_priv *priv)
> +{
> + struct device_node *node = pdev->dev.of_node;
> + struct device *dev = &pdev->dev;
> + struct clk_hw_onecell_data *clk_data;
> + struct property *prop;
> + const char *clk_name;
> + u32 ckgcr = 0;
> + int index = 0, ret, clk_id;
> + struct clk_hw *mdf_hw[] = { &clk_cck0.hw, &clk_cck1.hw };
> + struct clk_cckx_gate *gate;
> +
> + clk_cck.hw.init = CLK_HW_INIT_PARENTS_DATA("clk_cck", clk_cck_parent_data,
> + &clk_cck_ops, 0);
> + clk_cck0.hw.init = CLK_HW_INIT_HW(STM32_MDF_CCK0, &clk_cck.hw, &clk_cckx_gate_ops,
> + CLK_SET_RATE_PARENT | CLK_IGNORE_UNUSED);
> + clk_cck1.hw.init = CLK_HW_INIT_HW(STM32_MDF_CCK1, &clk_cck.hw, &clk_cckx_gate_ops,
> + CLK_SET_RATE_PARENT | CLK_IGNORE_UNUSED);
> +
> + clk_cck.priv = priv;
> +
> + ret = devm_clk_hw_register(dev, &clk_cck.hw);
> + if (ret)
> + return dev_err_probe(dev, ret, "Failed to register clk_cck");
> +
> + clk_data = devm_kzalloc(dev, struct_size(clk_data, hws, STM32_MDF_MAX_CCK), GFP_KERNEL);
> + if (!clk_data)
> + return -ENOMEM;
> +
> + of_property_for_each_string(node, "clock-output-names", prop, clk_name) {
> + if (index >= STM32_MDF_MAX_CCK) {
> + dev_err(dev, "Too many cck providers defined\n");
> + return -EINVAL;
> + }
> +
> + if (!strncmp(clk_name, "cck0", 4)) {
> + clk_id = 0;
> + } else if (!strncmp(clk_name, "cck1", 4)) {
> + clk_id = 1;
> + } else {
> + dev_err(dev, "Unexpected cck clock provider name [%s]\n", clk_name);
> + return -EINVAL;
> + }
> +
> + gate = to_clk_cckx_gate(mdf_hw[clk_id]);
> + gate->map = priv->regmap;
> +
> + ret = devm_clk_hw_register(dev, mdf_hw[clk_id]);
[Severity: Medium]
Can static file-scope clk_cck, clk_cck0 and clk_cck1 structures and stack
compound literals in CLK_HW_INIT_* corrupt state or dereference NULL?
- CLK_HW_INIT_PARENTS_DATA() and CLK_HW_INIT_HW() create compound literal
struct clk_init_data objects on stm32_mdf_core_register_clock_provider()'s
stack. __clk_register() only clears hw->init = NULL for clocks that are
registered, so when clock-output-names lists fewer than two clocks, the
unregistered global clk_cck0.hw.init or clk_cck1.hw.init pointer is left
dangling into expired stack memory after this function returns.
- Conversely, if clock-output-names contains duplicate names (such as
"cck1", "cck1"), the first devm_clk_hw_register() clears
mdf_hw[clk_id]->init = NULL and the second call dereferences NULL at
hw->init->name in __clk_register().
- Because clk_cck, clk_cck0 and clk_cck1 are file-scope static globals with
fixed clock names, probing a second MDF instance overwrites clk_cck.priv
and gate->map of the first instance and fails registration with -EEXIST.
> + if (ret)
> + return dev_err_probe(dev, ret, "Failed to register %s clock provider",
> + clk_name);
> +
> + clk_data->hws[index] = mdf_hw[clk_id];
> +
> + /* Configure the CCKx clock as output */
> + ckgcr |= clk_id ? MDF_CKG_CCK1DIR : MDF_CKG_CCK0DIR;
> +
> + index++;
> + }
> +
> + /*
> + * Set num to max number of clock. This allows any clocks indice below maximum
> + * in of_clk_hw_onecell_get, whatever the actual number of providers.
> + */
> + clk_data->num = STM32_MDF_MAX_CCK;
> + ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, clk_data);
> + if (ret) {
> + dev_err(dev, "Failed to add %s clock provider: %d\n",
> + clk_name ? clk_name : "", ret);
> + return ret;
> + }
[Severity: High]
Does writing clk_data->hws[index] before setting clk_data->num violate the
__counted_by(num) annotation on struct clk_hw_onecell_data and mis-index
single-clock configurations?
struct clk_hw_onecell_data declares hws[] with __counted_by(num), so
writing to clk_data->hws[index] while clk_data->num is still 0 triggers a
UBSAN bounds violation when CONFIG_UBSAN_BOUNDS is enabled; clk_data->num
must be set before accessing clk_data->hws[].
In addition:
- Indexing clk_data->hws[] by sequential loop counter index instead of
clk_id places clk_cck1 at hws[0] when clock-output-names only contains
"cck1", and leaves unpopulated slots in hws[] as NULL (which
of_clk_hw_onecell_get() returns as a dummy NULL clock rather than
ERR_PTR(-ENOENT)).
- The DT binding allows #clock-cells = <0> when a single CCK output is
used, in which case of_parse_clkspec() parses 0 arguments and
of_clk_hw_onecell_get() reads clkspec->args[0] from uninitialized stack
memory in of_clk_get_hw().
- After of_property_for_each_string() finishes, clk_name is always NULL, so
the dev_err() message on devm_of_clk_add_hw_provider() failure always
prints an empty name.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118-upstream_mdf_v2-v2-0-7939d3c3720a@foss.st.com?part=3
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH v2 07/11] iio: adc: stm32-mdf: add direct iio conversion support
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
0 siblings, 0 replies; 22+ messages in thread
From: sashiko-bot @ 2026-11-18 15:51 UTC (permalink / raw)
To: Olivier Moysan
Cc: devicetree, dri-devel, linux-sound, robh, conor+dt, media-ci
Thank you for your contribution! Sashiko AI review found 15 potential
issues to consider:
- [High] iio: adc: stm32-mdf: divide by zero in stm32_mdf_adc_read_raw()
on scale read
- [High] iio: adc: stm32-mdf: unshifted raw sample in
stm32_mdf_adc_single_conv()
- [High] iio: adc: stm32-mdf: divide by zero in
stm32_mdf_adc_check_clock_config()
- [High] iio: adc: stm32-mdf: wrong regmap and filter order in
stm32_mdf_irq()
- [High] iio: adc: stm32-mdf: wrong consumer device in
stm32_mdf_channel_parse_of()
- [Medium] iio: adc: stm32-mdf: W1C status bits not cleared in
stm32_mdf_irq()
- [Medium] iio: adc: stm32-mdf: divide by zero in
mdf_adc_set_samp_freq() on bad rate
- [Medium] iio: adc: stm32-mdf: out-of-bounds buffer read in
stm32_mdf_adc_single_conv()
- [Medium] iio: adc: stm32-mdf: premature IRQ registration in
stm32_mdf_adc_probe()
- [Medium] iio: adc: stm32-mdf: missing error handling in
stm32_mdf_adc_start_filter()
- [Medium] iio: adc: stm32-mdf: channel count mismatch in
stm32_mdf_adc_init()
- [Medium] iio: adc: stm32-mdf: unlocked requested_sample_freq reset in
write_raw()
- [Medium] iio: adc: stm32-mdf: kclk exclusivity and n_active_ch race
across filters
- [Medium] iio: adc: stm32-mdf: unsynchronized mdf->filter_list access
and removal
- [Medium] iio: adc: stm32-mdf: wrong bitmask for datsrc in
apply_filters_config()
--
commit 2d5ac177e0b4a7ad327bdcde3015713c0361e50e
Author: Olivier Moysan <olivier.moysan@foss.st.com>
iio: adc: stm32-mdf: add direct iio conversion support
This commit adds single-shot IIO direct conversion support, channel and
backend initialization, interrupt handling, and raw/scale/offset/frequency
sysfs accessors to the STM32 MDF ADC driver.
> diff --git a/drivers/iio/adc/stm32-mdf-adc.c b/drivers/iio/adc/stm32-mdf-adc.c
> index eae7a4fa14851..7361ce6182bec 100644
> --- a/drivers/iio/adc/stm32-mdf-adc.c
> +++ b/drivers/iio/adc/stm32-mdf-adc.c
> @@ -329,6 +329,79 @@ static struct stm32_mdf_adc *stm32_mdf_get_filter_by_handle(struct stm32_mdf *md
> return NULL;
> }
>
> +static int stm32_mdf_adc_start_filter(struct stm32_mdf_adc *adc)
> +{
> + struct stm32_mdf_adc *adc_inter;
> + struct stm32_mdf *mdf = adc->mdf;
> + u32 val;
> +
> + if (MDF_IS_FILTER0(adc))
> + list_for_each_entry(adc_inter, &mdf->filter_list, entry)
> + if (MDF_IS_INTERLEAVED_FILT_NOT_0(adc_inter))
> + stm32_mdf_adc_start_filter(adc_inter);
> +
> + /* Check filter status. Bypass cache to access volatile MDF_DFLTCR_ACTIVE bit */
> + regcache_cache_bypass(adc->regmap, true);
> + regmap_read(adc->regmap, MDF_DFLTCR_REG, &val);
> + regcache_cache_bypass(adc->regmap, false);
> + if (val & MDF_DFLTCR_ACTIVE) {
> + dev_err(adc->dev, "Filter [%d] is already running\n", adc->fl_id);
> + return -EBUSY;
> + }
> +
> + return regmap_set_bits(adc->regmap, MDF_DFLTCR_REG, MDF_DFLTCR_DFLTEN);
> +}
[Severity: Medium]
Does stm32_mdf_adc_start_filter() ignore errors from starting interleaved
filters and leave already-started sibling filters enabled if filter 0 is
busy?
When stm32_mdf_adc_start_conv() sees an error from
stm32_mdf_adc_start_filter(), it jumps to stop_sitf without stopping any
secondary interleaved filters that were just enabled.
stm32_mdf_adc_filter_set_mode() and its caller stm32_mdf_adc_single_conv()
similarly ignore errors when configuring the filter acquisition mode.
> @@ -606,7 +679,7 @@ static int stm32_mdf_adc_check_clock_config(struct stm32_mdf_adc *adc, unsigned
> return ret;
> }
>
> -static int __maybe_unused mdf_adc_set_samp_freq(struct iio_dev *indio_dev)
> +static int mdf_adc_set_samp_freq(struct iio_dev *indio_dev)
> {
> struct stm32_mdf_adc *adc = iio_priv(indio_dev);
> struct device *dev = &indio_dev->dev;
> @@ -675,6 +748,483 @@ static int __maybe_unused mdf_adc_set_samp_freq(struct iio_dev *indio_dev)
> return ret;
> }
>
> +static int stm32_mdf_adc_start_mdf(struct iio_dev *indio_dev)
> +{
> + struct stm32_mdf_adc *adc = iio_priv(indio_dev);
> + int ret;
> +
> + ret = clk_prepare_enable(adc->sitf->sck);
> + if (ret < 0) {
> + dev_err(&indio_dev->dev, "Failed to enable clock %s\n",
> + __clk_get_name(adc->sitf->sck));
> + return ret;
> + }
> +
> + ret = mdf_adc_set_samp_freq(indio_dev);
[Severity: High]
Does calling mdf_adc_set_samp_freq() divide by zero in
stm32_mdf_adc_check_clock_config() on the first conversion or sampling
frequency write?
mdf_adc_set_samp_freq() calls stm32_mdf_adc_check_clock_config() before
stm32_mdf_adc_set_filters_config() initializes adc->decim_cic:
drivers/iio/adc/stm32-mdf-adc.c:stm32_mdf_adc_check_clock_config() {
...
/* RSFLT enabled: check Fproc > 24 * Fsck / (MCICD+1) */
cic_ratio = DIV_ROUND_CLOSEST(24, adc->decim_cic);
...
}
On the first call after probe, adc->decim_cic is still 0, triggering a
divide by zero; on later rate changes, stm32_mdf_adc_check_clock_config()
checks against the previous stale adc->decim_cic value.
In addition, stm32_mdf_adc_apply_filters_config() (called via
mdf_adc_set_samp_freq() -> stm32_mdf_adc_set_filters_config()) uses
MDF_SITFCR_SCKSRC_MASK (GENMASK(2, 1)) instead of MDF_DFLTCICR_DATSRC_MASK
(GENMASK(1, 0)) when programming adc->datsrc into MDF_DFLTCICR_REG:
drivers/iio/adc/stm32-mdf-adc.c:stm32_mdf_adc_apply_filters_config() {
...
/* 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) |
...
}
This shifts datsrc into bits [2:1] and leaves bit 0 unmasked.
[ ... ]
> +static int stm32_mdf_channel_parse_of(struct iio_dev *indio_dev, struct fwnode_handle *node,
> + struct iio_chan_spec *ch)
> +{
> + struct stm32_mdf_adc *adc = iio_priv(indio_dev);
> + struct iio_backend *backend;
> + const char *label = fwnode_get_name(node);
> + int ret;
> + u32 stu = 0;
> +
> + ret = fwnode_property_read_u32(node, "reg", &ch->channel);
> + if (ret < 0) {
> + dev_err(&indio_dev->dev, "Failed to read channel index: [%d]\n", ret);
> + return ret;
> + }
[ ... ]
> + if (adc->dev_data->type == STM32_MDF_IIO) {
> + backend = devm_iio_backend_fwnode_get(&indio_dev->dev, NULL, node);
> + if (IS_ERR(backend))
> + return dev_err_probe(&indio_dev->dev, PTR_ERR(backend),
> + "Failed to get backend\n");
> + adc->channels[ch->scan_index].backend = backend;
> + }
> +
> + return ret;
> +}
[Severity: High]
Should devm_iio_backend_fwnode_get() (and dev_err_probe(), as well as
devm_kcalloc() in stm32_mdf_adc_init()) use adc->dev (&pdev->dev) instead
of &indio_dev->dev?
Passing &indio_dev->dev attaches the DL_FLAG_AUTOREMOVE_CONSUMER
device_link and the devres release action to the unregistered, driverless
IIO struct device rather than the platform device. If a later channel's
backend defers or probe fails before iio_device_register(),
device_links_purge(&indio_dev->dev) never runs, so the reference taken by
device_link_add() pins &indio_dev->dev at refcount 1 and leaks the IIO
device, its devres allocations, the device_link, and the backend module
reference. Even when probe succeeds, &indio_dev->dev has no bound
device_driver, so the device link stays in DL_STATE_AVAILABLE and
unbinding the backend supplier driver fails to unbind stm32-mdf-adc and
triggers a WARN_ON in device_links_driver_cleanup().
[ ... ]
> +static int stm32_mdf_adc_single_conv(struct iio_dev *indio_dev,
> + const struct iio_chan_spec *chan, int *res)
> +{
[ ... ]
> + if (MDF_IS_INTERLEAVED_FILT(adc))
> + *res = adc->buffer[chan->channel];
> + else
> + *res = adc->buffer[0];
[Severity: High]
Does stm32_mdf_adc_single_conv() return an unshifted 32-bit MDF_DFLTDR_REG
value instead of the 24-bit signed sample?
The 24-bit sample sits in bits [31:8] of MDF_DFLTDR_REG
(MDF_DFLTDR_DR_MASK), and IIO_CHAN_INFO_SCALE and IIO_CHAN_INFO_OFFSET in
stm32_mdf_adc_read_raw() are computed for a 24-bit sample (realbits = 24,
dflt_max). In the IIO ABI, scan_type.shift (8) is only applied to
buffered scan elements, never to IIO_CHAN_INFO_RAW, so *res is 256 times
too large (and retains bits [7:0]) unless it is arithmetic right-shifted
by 8.
[Severity: Medium]
Can adc->buffer[chan->channel] also read out of bounds in interleaved
mode?
adc->buffer is allocated with adc->mdf->nb_interleave elements (which can
be 2), whereas chan->channel comes directly from the unvalidated DT "reg"
property (0..7) rather than chan->scan_index. Also, passing &ch->channel
(int *) to fwnode_property_read_u32() in stm32_mdf_channel_parse_of()
passes a signed pointer where u32 * is expected.
> +
> +stop_conv:
> + stm32_mdf_adc_stop_conv(indio_dev);
> +
> +err_conv:
> + adc->trgo = save_trgo;
> +
> + stm32_mdf_adc_stop_mdf(indio_dev);
> +
> + return ret;
> +}
> +
> +static int stm32_mdf_adc_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
> + int val, int val2, long mask)
> +{
> + struct stm32_mdf_adc *adc = iio_priv(indio_dev);
> + int ret = 0;
> +
> + if (mask != IIO_CHAN_INFO_SAMP_FREQ || !val)
> + return -EINVAL;
[Severity: Medium]
Can writing a negative value or a frequency greater than 2 * sck_freq to
in_voltage_sampling_frequency trigger a divide by zero in
mdf_adc_set_samp_freq()?
Checking !val allows negative signed int values through, which sign-extend
when assigned to unsigned long adc->requested_sample_freq. Whenever
sample_freq > 2 * sck_freq, DIV_ROUND_CLOSEST(sck_freq, sample_freq) in
mdf_adc_set_samp_freq() evaluates to 0, and the immediately following
dev_warn() divides by decim_ratio:
drivers/iio/adc/stm32-mdf-adc.c:mdf_adc_set_samp_freq() {
...
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);
...
}
> +
> + {
> + IIO_DEV_ACQUIRE_DIRECT_MODE(indio_dev, claim);
> + if (IIO_DEV_ACQUIRE_FAILED(claim))
> + return -EBUSY;
> +
> + /* Set requested_sample_freq right now, as used by mdf_adc_set_samp_freq() */
> + adc->requested_sample_freq = val;
> +
> + if (adc->dev_data->type == STM32_MDF_IIO) {
> + ret = stm32_mdf_core_lock_kclk_rate(adc->mdf);
> + if (!ret) {
> + ret = mdf_adc_set_samp_freq(indio_dev);
> +
> + stm32_mdf_core_unlock_kclk_rate(adc->mdf);
> + }
> + }
> + }
> +
> + if (ret)
> + adc->requested_sample_freq = 0;
[Severity: Medium]
Does resetting adc->requested_sample_freq = 0 outside the
IIO_DEV_ACQUIRE_DIRECT_MODE scope race with concurrent direct-mode callers
and discard any previously configured sampling frequency on error?
The inner block releases the direct-mode claim before if (ret) runs, so a
concurrent reader or writer can observe the failed rate or have its new
rate overwritten with 0, and any prior valid requested_sample_freq is not
restored.
[Severity: Medium]
Can concurrent operations across two filter IIO devices sharing adc->mdf
cause an unbalanced clk_rate_exclusive_put() on priv->kclk?
Each filter has its own IIO direct-mode lock, while
stm32_mdf_core_lock_kclk_rate(), stm32_mdf_core_unlock_kclk_rate(),
stm32_mdf_core_start_mdf(), and stm32_mdf_core_stop_mdf() share
priv->n_active_ch and priv->exclusive_flg without locking:
CPU0 (Filter A write_raw) CPU1 (Filter B raw conversion)
stm32_mdf_core_lock_kclk_rate()
/* n_active_ch == 1: skips get */
stm32_mdf_core_stop_mdf()
atomic_dec_and_test(&n_active_ch)
clk_rate_exclusive_put(kclk)
exclusive_flg = false
stm32_mdf_core_unlock_kclk_rate()
/* !atomic_read(&n_active_ch) */
clk_rate_exclusive_put(kclk) <-- unbalanced put
Similarly, if two filters call stm32_mdf_core_start_mdf() concurrently from
0, the second filter sees atomic_inc_return(&priv->n_active_ch) == 2 and
returns 0 before the first filter finishes pm_runtime_resume_and_get() and
sets MDF_CKG_CKGDEN.
> +
> + return ret;
> +}
> +
> +static int stm32_mdf_adc_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan,
> + int *val, int *val2, long mask)
> +{
[ ... ]
> + case IIO_CHAN_INFO_SAMP_FREQ:
> + *val = adc->sample_freq;
> +
> + return IIO_VAL_INT;
> +
> + case IIO_CHAN_INFO_SCALE:
> + /*
> + * Vconv = (raw>>shift + offset) * scale.
> + * scale = Vref * k / 2^res (denominator is managed through FRACTIONAL_LOG2 type)
> + * k correspond to the ratio between max resolution and actual filter maximum
> + * k = max / dflt_max
> + * max = 2^(res - 1) - 1
> + * max_dflt = D^N * gain_lin * gain_rsflt
> + * scale = Vref * max / dflt_max
> + */
> + if (adc->channels[idx].backend) {
> + ret = iio_backend_read_scale(adc->channels[idx].backend, chan,
> + &scale, NULL);
> + if (ret < 0)
> + return ret;
> +
> + *val = div_u64((u64)scale * max, adc->dflt_max);
> +
> + *val2 = chan->scan_type.realbits;
> + if (chan->differential)
> + *val *= 2;
> + } else {
> + return -EPERM;
> + }
> +
> + return IIO_VAL_FRACTIONAL_LOG2;
> +
> + case IIO_CHAN_INFO_OFFSET:
> + if (adc->channels[idx].backend) {
> + ret = iio_backend_read_offset(adc->channels[idx].backend, chan,
> + &offset, NULL);
> + if (ret < 0)
> + return ret;
> +
> + *val = offset;
> + if (!chan->differential)
> + *val += adc->dflt_max;
[Severity: High]
Does reading IIO_CHAN_INFO_SCALE before the first raw conversion or
sampling_frequency write divide by zero because adc->dflt_max is still 0?
Neither stm32_mdf_adc_probe() nor stm32_mdf_adc_init() calls
mdf_adc_set_samp_freq() to compute the initial filter parameters, so
adc->dflt_max and adc->sample_freq remain 0 until a conversion is started
or IIO_CHAN_INFO_SAMP_FREQ is written. Reading in_voltageX_scale right
after probe divides by adc->dflt_max == 0, while reading
in_voltageX_offset or in_voltage_sampling_frequency uses uninitialized 0
values.
[ ... ]
> +static irqreturn_t stm32_mdf_irq(int irq, void *arg)
> +{
> + struct iio_dev *indio_dev = arg;
> + struct stm32_mdf_adc *adc = iio_priv(indio_dev), *adc_inter;
> + struct regmap *regmap = adc->regmap;
> + u32 *ptr = adc->buffer;
> + u32 isr, ier, flags;
> +
> + regmap_read(regmap, MDF_DFLTISR_REG, &isr);
> + regmap_read(regmap, MDF_DFLTIER_REG, &ier);
> +
> + flags = isr & ier;
> + if (!flags)
> + return IRQ_NONE;
> +
> + if (flags & MDF_DFLTISR_FTHF_MASK) {
> + /* Reading the data register clear the IRQ status */
> + regmap_read(regmap, MDF_DFLTDR_REG, ptr++);
> +
> + if (MDF_IS_FILTER0(adc))
> + list_for_each_entry(adc_inter, &adc->mdf->filter_list, entry)
> + if (MDF_IS_INTERLEAVED_FILT_NOT_0(adc_inter))
> + regmap_read(regmap, MDF_DFLTDR_REG, ptr++);
> +
> + complete(&adc->completion);
> + }
[Severity: High]
In the interleaved filter loop, shouldn't regmap_read() read from
adc_inter->regmap instead of Filter 0's regmap?
Passing regmap reads Filter 0's MDF_DFLTDR_REG repeatedly and leaves the
interleaved filters' data registers unread. Also, because
stm32_mdf_adc_parse_of() inserts filters at the head of mdf->filter_list
with list_add(), iterating mdf->filter_list with ptr++ stores the
interleaved samples into adc->buffer in reverse probe order.
> +
> + if (flags & MDF_DFLTISR_DOVRF_MASK) {
> + dev_warn(&indio_dev->dev, "Data overflow detected\n");
> + regmap_set_bits(regmap, MDF_DFLTISR_REG, MDF_DFLTISR_DOVRF_MASK);
> + }
> +
> + if (flags & MDF_DFLTISR_RFOVRF_MASK) {
> + dev_warn(&indio_dev->dev, "Reshape filter overrun detected\n");
> + regmap_set_bits(regmap, MDF_DFLTISR_REG, MDF_DFLTISR_RFOVRF_MASK);
> + }
> +
> + if (flags & MDF_DFLTISR_SATF_MASK) {
> + dev_warn(&indio_dev->dev, "Saturation detected\n");
> + regmap_set_bits(regmap, MDF_DFLTISR_REG, MDF_DFLTISR_SATF_MASK);
> +
> + /* Notify only once */
> + regmap_clear_bits(adc->regmap, MDF_DFLTIER_REG, MDF_DFLTIER_SATIE_MASK);
> + }
[Severity: Medium]
Does regmap_set_bits() fail to clear the write-1-to-clear status flags in
MDF_DFLTISR_REG?
MDF_DFLTISR_REG is marked volatile in stm32_mdf_adc_volatile_reg(), and
regmap_set_bits() calls _regmap_update_bits() with force = false. When an
interrupt status bit is already 1 in hardware, regmap reads MDF_DFLTISR_REG,
sees that the bit is already set, and skips the register write, leaving the
interrupt asserted. Using regmap_write() with the flag mask will clear the
W1C bit without accidentally clearing other pending W1C bits via a
read-modify-write.
> +
> + return IRQ_HANDLED;
> +}
> +
> +static int stm32_mdf_adc_init(struct device *dev, struct iio_dev *indio_dev)
> +{
> + struct stm32_mdf_adc *adc = iio_priv(indio_dev);
> + struct iio_chan_spec *ch = NULL;
> + unsigned int num_ch;
> + int ret;
> +
> + num_ch = device_get_child_node_count(indio_dev->dev.parent);
> + if (num_ch) {
> + /* Filter0 may have several channels in interleaved mode */
> + if (num_ch > 1) {
> + if (!MDF_IS_FILTER0(adc)) {
> + dev_err(dev, "Too many channels for filter [%d]\n", adc->fl_id);
> + return -EINVAL;
> + } else if (num_ch != adc->mdf->nb_interleave) {
> + dev_err(dev, "Unexpected channels number for filter0: [%d]\n",
> + num_ch);
> + return -EINVAL;
> + }
> + }
> +
> + ch = devm_kcalloc(&indio_dev->dev, num_ch, sizeof(*ch), GFP_KERNEL);
> + if (!ch)
> + return -ENOMEM;
> +
> + adc->channels = devm_kcalloc(&indio_dev->dev, num_ch, sizeof(*adc->channels),
> + GFP_KERNEL);
> + if (!adc->channels)
> + return -ENOMEM;
> +
> + ret = stm32_mdf_adc_chan_init(indio_dev, ch);
> + if (ret < 0)
> + return dev_err_probe(dev, ret, "Channels init failed\n");
> + }
> +
> + indio_dev->num_channels = num_ch;
[Severity: Medium]
If any child node has a "compatible" property (which
stm32_mdf_adc_chan_init() skips), does assigning indio_dev->num_channels =
num_ch instead of the initialized channel count returned in ret leave
uninitialized trailing entries in indio_dev->channels and adc->channels?
Also, because num_ch != adc->mdf->nb_interleave is only checked when
num_ch > 1, a Filter 0 configured for interleaved mode
(adc->mdf->nb_interleave >= 2) with only 1 channel node is not rejected.
[ ... ]
> @@ -883,13 +1435,41 @@ static int stm32_mdf_adc_probe(struct platform_device *pdev)
> if (IS_ERR(adc->regmap))
> return dev_err_probe(dev, PTR_ERR(adc->regmap), "Failed to init regmap\n");
>
> + irq = platform_get_irq(pdev, 0);
> + if (irq < 0)
> + return dev_err_probe(dev, irq, "Failed to get IRQ\n");
> +
> + ret = devm_request_irq(dev, irq, stm32_mdf_irq, 0, pdev->name, iio);
> + if (ret < 0)
> + return dev_err_probe(dev, ret, "Failed to request IRQ\n");
> +
> ret = stm32_mdf_adc_parse_of(pdev, adc);
> if (ret < 0)
> return ret;
>
> + iio->info = &stm32_mdf_info_adc;
> + iio->name = dev_name(&pdev->dev);
> +
> adc->dev = dev;
> + adc->dev_data = dev_data;
> + ret = dev_data->init(dev, iio);
[Severity: Medium]
Can an interrupt firing right after devm_request_irq() dereference a NULL
adc->buffer or an uninitialized adc->completion in stm32_mdf_irq()?
adc->buffer is only allocated later in stm32_mdf_adc_parse_of(), and
init_completion(&adc->completion) is only called in dev_data->init().
Because devres releases resources in reverse order of registration,
adc->buffer is also freed before the IRQ is freed on teardown.
> + if (ret < 0)
> + goto err_clean_list;
> +
> + if (!MDF_IS_INTERLEAVED_FILT_NOT_0(adc)) {
> + ret = iio_device_register(iio);
> + if (ret < 0) {
> + dev_err_probe(dev, ret, "Failed to register IIO device: %d\n", ret);
> + goto err_clean_list;
> + }
> + }
>
> return 0;
> +
> +err_clean_list:
> + list_del(&adc->entry);
> +
> + return ret;
> }
[Severity: Medium]
What serializes mdf->filter_list modifications in probe/remove against
Filter 0 traversing mdf->filter_list in process and hardirq contexts?
Filter 0 registers its IIO device as soon as it probes, so conversions and
stm32_mdf_irq() on Filter 0 can traverse mdf->filter_list while a sibling
filter device is still probing (calling list_add() in
stm32_mdf_adc_parse_of() or list_del() on error) or being unbound in
stm32_mdf_adc_remove(), where the sibling's struct stm32_mdf_adc is also
freed while Filter 0 remains registered.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118-upstream_mdf_v2-v2-0-7939d3c3720a@foss.st.com?part=7
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH v2 08/11] iio: adc: stm32-mdf: add dma buffered acquisition support
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
0 siblings, 0 replies; 22+ messages in thread
From: sashiko-bot @ 2026-11-18 15:55 UTC (permalink / raw)
To: Olivier Moysan
Cc: devicetree, dri-devel, linux-sound, robh, conor+dt, media-ci
Thank you for your contribution! Sashiko AI review found 9 potential
issues to consider:
- [High] iio: adc: stm32-mdf: no-op W1C clear in stm32_mdf_irq() causes
IRQ storm
- [High] iio: adc: stm32-mdf: broken interleaved suspend and resume
ordering
- [High] iio: adc: stm32-mdf: NULL thread_fn in
devm_iio_triggered_buffer_setup()
- [Medium] iio: adc: stm32-mdf: wrong W1C clear in
stm32_mdf_adc_postenable()
- [Medium] iio: adc: stm32-mdf: uncleaned IRQs on error in
stm32_mdf_adc_postenable()
- [Medium] iio: adc: stm32-mdf: unbalanced kclk prepare in
stm32_mdf_adc_suspend()
- [Medium] iio: adc: stm32-mdf: zero-size cb on wrap in
stm32_mdf_adc_dma_buffer_done()
- [Medium] iio: adc: stm32-mdf: missing available_scan_masks for
interleaved mode
- [Low] iio: adc: stm32-mdf: unused PM callbacks when CONFIG_PM_SLEEP is
disabled
--
commit abc251af1764611faf8f05bc9f73b60d34b2ef36
Author: Olivier Moysan <olivier.moysan@foss.st.com>
iio: adc: stm32-mdf: add dma buffered acquisition support
This commit adds cyclic DMA buffered acquisition, watermark configuration,
and system suspend/resume support to the STM32 MDF ADC driver.
> diff --git a/drivers/iio/adc/stm32-mdf-adc.c b/drivers/iio/adc/stm32-mdf-adc.c
> index 7361ce6182bec..046ca3943a397 100644
> --- a/drivers/iio/adc/stm32-mdf-adc.c
> +++ b/drivers/iio/adc/stm32-mdf-adc.c
> @@ -823,6 +823,243 @@ static void stm32_mdf_adc_stop_conv(struct iio_dev *indio_dev)
[ ... ]
> +static void stm32_mdf_adc_dma_buffer_done(void *data)
> +{
> + struct iio_dev *indio_dev = data;
> + struct stm32_mdf_adc *adc = iio_priv(indio_dev);
> + int available = stm32_mdf_adc_dma_residue(adc);
> + size_t old_pos;
> +
> + dev_dbg(&indio_dev->dev, "pos = %d, available = %d\n", adc->bufi, available);
> + old_pos = adc->bufi;
> +
> + while (available >= indio_dev->scan_bytes) {
> + s32 *buffer = (s32 *)&adc->rx_buf[adc->bufi];
> +
> + adc->bufi += indio_dev->scan_bytes;
> + if (adc->bufi >= adc->buf_sz) {
> + if (adc->cb)
> + adc->cb(&adc->rx_buf[old_pos], adc->buf_sz - old_pos, adc->cb_priv);
> + adc->bufi = 0;
> + old_pos = 0;
> + }
> + if (adc->dev_data->type == STM32_MDF_IIO)
> + iio_push_to_buffers(indio_dev, buffer);
> + available -= indio_dev->scan_bytes;
> + }
[Severity: Medium]
In multi-channel interleaved mode on Filter 0, hardware and DMA always
transfer all num_ch 32-bit samples per scan into adc->rx_buf, but
stm32_mdf_adc_init() never sets indio_dev->available_scan_masks. If a
user enables only a subset of channels in scan_elements, does stepping
adc->bufi by the smaller indio_dev->scan_bytes without an IIO demux table
push samples from disabled interleaved filters as if they belonged to the
enabled channel?
> + if (adc->cb)
> + adc->cb(&adc->rx_buf[old_pos], adc->bufi - old_pos, adc->cb_priv);
> +}
[Severity: Medium]
When adc->bufi wraps around to 0 at adc->buf_sz inside the loop, both
adc->bufi and old_pos are reset to 0 after invoking adc->cb(). Does this
trailing check then call adc->cb() a second time with a size of 0 on every
buffer wrap-around (and whenever available < indio_dev->scan_bytes)?
[ ... ]
> +static int stm32_mdf_adc_postenable(struct iio_dev *indio_dev)
> +{
[ ... ]
> + stm32_mdf_adc_filter_set_mode(adc, true);
> +
> + regmap_clear_bits(adc->regmap, MDF_DFLTISR_REG,
> + MDF_DFLTISR_DOVRF_MASK | MDF_DFLTISR_SATF_MASK);
[Severity: Medium]
Status flags in MDF_DFLTISR_REG are write-1-to-clear (rc_w1). Because
MDF_DFLTISR_REG is volatile, regmap_clear_bits() reads the register and
writes back the value with MDF_DFLTISR_DOVRF_MASK | MDF_DFLTISR_SATF_MASK
masked to 0. Does this leave pending DOVRF and SATF flags asserted right
before enabling their interrupts, while inadvertently writing 1 to clear
any other asserted status flags in MDF_DFLTISR_REG? Clearing DOVRF and
SATF requires regmap_write().
> +
> + regmap_set_bits(adc->regmap, MDF_DFLTIER_REG,
> + MDF_DFLTIER_DOVRIE_MASK | MDF_DFLTIER_SATIE_MASK);
[Severity: High]
Enabling MDF_DFLTIER_DOVRIE_MASK and MDF_DFLTIER_SATIE_MASK here makes the
overflow and saturation handlers in stm32_mdf_irq() reachable:
drivers/iio/adc/stm32-mdf-adc.c:stm32_mdf_irq() {
...
if (flags & MDF_DFLTISR_DOVRF_MASK) {
dev_warn(&indio_dev->dev, "Data overflow detected\n");
regmap_set_bits(regmap, MDF_DFLTISR_REG, MDF_DFLTISR_DOVRF_MASK);
}
...
if (flags & MDF_DFLTISR_SATF_MASK) {
dev_warn(&indio_dev->dev, "Saturation detected\n");
regmap_set_bits(regmap, MDF_DFLTISR_REG, MDF_DFLTISR_SATF_MASK);
...
}
Because MDF_DFLTISR_REG is volatile and regmap_set_bits() passes
force = false, _regmap_update_bits() reads MDF_DFLTISR_REG, sees the
status bit is already 1, and skips the bus write. If a data overflow
occurs during DMA acquisition, will MDF_DFLTISR_DOVRF_MASK remain set
while MDF_DFLTIER_DOVRIE_MASK stays enabled, causing an infinite hardirq
storm?
> +
> + ret = stm32_mdf_adc_dma_start(indio_dev);
> + if (ret) {
> + dev_err(&indio_dev->dev, "Can't start DMA\n");
> + goto err_dma;
> + }
> +
> + ret = stm32_mdf_adc_start_conv(indio_dev);
> + if (ret) {
> + dev_err(&indio_dev->dev, "Can't start conversion\n");
> + goto err_conv;
> + }
> +
> + return 0;
> +
> +err_conv:
> + stm32_mdf_adc_dma_stop(indio_dev);
> +err_dma:
> + stm32_mdf_adc_stop_mdf(indio_dev);
[Severity: Medium]
If stm32_mdf_adc_dma_start() or stm32_mdf_adc_start_conv() fails (for
example, when adc->buf_sz is 0), does the err_dma unwind path leave
MDF_DFLTIER_DOVRIE_MASK | MDF_DFLTIER_SATIE_MASK enabled in
MDF_DFLTIER_REG?
[ ... ]
> @@ -1208,6 +1467,24 @@ static int stm32_mdf_adc_init(struct device *dev, struct iio_dev *indio_dev)
>
> init_completion(&adc->completion);
>
> + /* Optionally request DMA */
> + ret = stm32_mdf_dma_request(dev, indio_dev);
> + if (ret) {
> + if (ret != -ENODEV)
> + return dev_err_probe(dev, ret, "DMA channel request failed with\n");
> +
> + dev_dbg(dev, "No DMA support\n");
> + return 0;
> + }
> +
> + ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
> + &iio_pollfunc_store_time, NULL,
> + &stm32_mdf_buffer_setup_ops);
[Severity: High]
Can attaching an IIO trigger and enabling the buffer crash or wedge the
kernel here?
devm_iio_triggered_buffer_setup() sets INDIO_BUFFER_TRIGGERED and
allocates indio_dev->pollfunc with h = iio_pollfunc_store_time,
thread = NULL, and type = IRQF_ONESHOT. When a trigger is attached and
the buffer is enabled, iio_verify_update() selects INDIO_BUFFER_TRIGGERED
over INDIO_BUFFER_SOFTWARE and iio_trigger_attach_poll_func() calls
request_threaded_irq() with IRQF_ONESHOT and thread_fn = NULL, hitting
WARN_ON_ONCE(new->flags & IRQF_ONESHOT && !new->thread_fn) in
__setup_irq(). When the trigger fires, a nested trigger such as
iio-trig-loop dereferences NULL action->thread_fn in handle_nested_irq(),
while a non-nested trigger such as iio-trig-sysfs or iio-trig-hrtimer
returns IRQ_WAKE_THREAD with no thread_fn, hitting warn_no_thread() and
never calling iio_trigger_notify_done().
The follow-up commit "iio: adc: stm32-mdf: add external timer trigger
support" adds INDIO_HARDWARE_TRIGGERED and .validate_trigger, but keeps
devm_iio_triggered_buffer_setup() with a NULL thread_fn, so
INDIO_BUFFER_TRIGGERED is still selected whenever indio_dev->trig is set.
Since samples are pushed from the DMA callback and external triggers are
routed in hardware, should this use devm_iio_kfifo_buffer_setup() (and
select IIO_KFIFO_BUF in Kconfig) instead of
devm_iio_triggered_buffer_setup()?
[ ... ]
> @@ -1479,14 +1758,48 @@ static void stm32_mdf_adc_remove(struct platform_device *pdev)
[ ... ]
> +static int stm32_mdf_adc_suspend(struct device *dev)
> +{
> + struct iio_dev *indio_dev = dev_get_drvdata(dev);
> + struct stm32_mdf_adc *adc = iio_priv(indio_dev);
> + int ret = 0;
> +
> + if (iio_buffer_enabled(indio_dev))
> + ret = stm32_mdf_adc_predisable(indio_dev);
> +
> + regcache_cache_only(adc->regmap, true);
> + regcache_mark_dirty(adc->regmap);\n+
> + return ret;
> +}
[Severity: Medium]
stm32_mdf_adc_probe() attaches adc->mdf->kclk to adc->regmap via
stm32_mdf_regmap_init() -> regmap_mmio_attach_clk(), which holds a
clk_prepare() reference on kclk. In stm32_mdf_core_suspend(), the parent
driver unprepares kclk only for sitf_list and the core regmap, not for
filter regmaps. Should stm32_mdf_adc_suspend() and stm32_mdf_adc_resume()
also unprepare and re-prepare adc->mdf->kclk so kclk does not stay
prepared across system suspend?
> +
> +static int stm32_mdf_adc_resume(struct device *dev)
> +{
> + struct iio_dev *indio_dev = dev_get_drvdata(dev);
> + struct stm32_mdf_adc *adc = iio_priv(indio_dev);
> + int ret;
> +
> + regcache_cache_only(adc->regmap, false);
> + ret = regcache_sync(adc->regmap);
> +
> + if (!ret && iio_buffer_enabled(indio_dev))
> + ret = stm32_mdf_adc_postenable(indio_dev);
> +
> + return ret;
> +}
[Severity: High]
How does suspend and resume work when interleaved (or synchronized)
filters are active?
In interleaved mode, only Filter 0 registers an IIO device, so
iio_buffer_enabled() is only ever true on Filter 0, while Filter 0 starts
and stops subordinate filters (Filters 1..N-1) by iterating
mdf->filter_list. Because child devices suspend in reverse probe order
and resume in forward probe order:
1. On suspend, Filters N-1..1 run stm32_mdf_adc_suspend() before Filter 0
and set regcache_cache_only(adc_inter->regmap, true). When Filter 0
then calls stm32_mdf_adc_predisable() -> stm32_mdf_adc_stop_filter(),
clearing MDF_DFLTCR_DFLTEN on adc_inter->regmap only updates the cache
and does not stop Filters 1..N-1 in hardware.
2. On resume, Filter 0 runs stm32_mdf_adc_resume() ->
stm32_mdf_adc_postenable() before Filters 1..N-1 have cleared
cache_only on their regmaps:
drivers/iio/adc/stm32-mdf-adc.c:stm32_mdf_adc_start_filter() {
...
if (MDF_IS_FILTER0(adc))
list_for_each_entry(adc_inter, &mdf->filter_list, entry)
if (MDF_IS_INTERLEAVED_FILT_NOT_0(adc_inter))
stm32_mdf_adc_start_filter(adc_inter);
/* Check filter status. Bypass cache to access volatile MDF_DFLTCR_ACTIVE bit */
regcache_cache_bypass(adc->regmap, true);
regmap_read(adc->regmap, MDF_DFLTCR_REG, &val);
regcache_cache_bypass(adc->regmap, false);
...
}
Calling stm32_mdf_adc_start_filter(adc_inter) while adc_inter->regmap
is still cache-only triggers WARN_ON(map->cache_only && enable) in
regcache_cache_bypass(), causes regmap_read() to fail with -EBUSY
leaving val uninitialized on the stack, traps filter configuration and
MDF_DFLTCR_DFLTEN writes in cache, and pulses MDF_GCR_TRGO before
subordinate interleaved (or st,sync slave) filters have resumed in
hardware.
In addition, stm32_mdf_adc_init() requests a DMA channel, 16 KB coherent
buffer, and triggered IIO buffer even for MDF_IS_INTERLEAVED_FILT_NOT_0()
filters that never register an IIO device.
> +
> +static SIMPLE_DEV_PM_OPS(stm32_mdf_adc_pm_ops, stm32_mdf_adc_suspend, stm32_mdf_adc_resume);
> +
> static struct platform_driver stm32_mdf_adc_driver = {
> .driver = {
> .name = "stm32-mdf-adc",
> .of_match_table = stm32_mdf_adc_match,
> + .pm = &stm32_mdf_adc_pm_ops,
> },
[Severity: Low]
When CONFIG_PM_SLEEP is disabled, SIMPLE_DEV_PM_OPS() leaves
stm32_mdf_adc_suspend() and stm32_mdf_adc_resume() unused, producing
-Wunused-function warnings. Could this use DEFINE_SIMPLE_DEV_PM_OPS() and
pm_sleep_ptr(&stm32_mdf_adc_pm_ops) instead?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118-upstream_mdf_v2-v2-0-7939d3c3720a@foss.st.com?part=8
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH v2 10/11] iio: adc: stm32-mdf: add dmic audio filter support
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
0 siblings, 0 replies; 22+ messages in thread
From: sashiko-bot @ 2026-11-18 16:06 UTC (permalink / raw)
To: Olivier Moysan
Cc: devicetree, dri-devel, linux-sound, robh, conor+dt, media-ci
Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] iio: adc: stm32-mdf: race updating cb and cb_priv in
stm32_mdf_get_buff_cb()
- [Medium] iio: adc: stm32-mdf: out-of-bounds or uninitialized channel
in stm32_mdf_audio_init()
- [Medium] iio: adc: stm32-mdf: unlocked freq reset in
stm32_mdf_adc_write_raw()
- [Medium] iio: stm32-mdf: bad mask and bypass in
stm32_mdf_adc_apply_filters_config()
- [Low] iio: adc: stm32-mdf: inaccurate kernel-doc Return for
stm32_mdf_get_sub_channels_nb()
--
commit dd4cd2a4f3dd0be22589f1468dca26c3807bd280
Author: Olivier Moysan <olivier.moysan@foss.st.com>
iio: adc: stm32-mdf: add dmic audio filter support
This commit extends the STM32 MDF filter driver to support digital
microphone audio filters and adds callback and sub-channel helpers for
audio consumers.
> diff --git a/drivers/iio/adc/stm32-mdf-adc.c b/drivers/iio/adc/stm32-mdf-adc.c
> index 97099b4adffc6..27c47bef71a42 100644
> --- a/drivers/iio/adc/stm32-mdf-adc.c
> +++ b/drivers/iio/adc/stm32-mdf-adc.c
[ ... ]
> @@ -1128,6 +1129,42 @@ static const struct iio_buffer_setup_ops stm32_mdf_buffer_setup_ops = {
> .predisable = &stm32_mdf_adc_predisable,
> };
>
> +/**
> + * stm32_mdf_get_sub_channels_nb() - Get the number of MDF sub-channels
> + * @indio_dev: MDF IIO device
> + *
> + * Return: 0 on success, or a negative error code on failure.
> + */
[Severity: Low]
The Return description in the kernel-doc says this returns 0 on success or
a negative error code on failure, whereas stm32_mdf_get_sub_channels_nb()
returns an unsigned int sub-channel count (nb_interleave or 1).
> +unsigned int stm32_mdf_get_sub_channels_nb(struct iio_dev *indio_dev)
> +{
> + struct stm32_mdf_adc *adc = iio_priv(indio_dev);
> +
> + if (MDF_IS_FILTER0(adc) && adc->mdf->nb_interleave)
> + return adc->mdf->nb_interleave;
> +
> + return 1;
> +}
> +EXPORT_SYMBOL_GPL(stm32_mdf_get_sub_channels_nb);
[Severity: Medium]
Does stm32_mdf_adc_apply_filters_config() misconfigure MDF_DFLTCICR_REG and
drop rsflt_bypass on secondary interleaved filters?
drivers/iio/adc/stm32-mdf-adc.c:stm32_mdf_adc_apply_filters_config() {
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++;
}
}
...
/* 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);
}
When programming MDF_DFLTCICR_REG, msk and val use MDF_SITFCR_SCKSRC_MASK
(GENMASK(2, 1)) instead of MDF_DFLTCICR_DATSRC_MASK (GENMASK(1, 0)), which
shifts adc->datsrc into bits [2:1] and leaves bit 0 untouched. In
addition, the interleaved filter loop does not copy adc->rsflt_bypass to
adc_inter->rsflt_bypass, leaving the reshape filter enabled on secondary
interleaved filters when filter 0 has st,rs-filter-bypass set.
[ ... ]
> @@ -1474,6 +1514,13 @@ static int stm32_mdf_adc_read_label(struct iio_dev *indio_dev, const struct iio_
> return sysfs_emit(label, "%s\n", name);
> }
>
> +static const struct iio_info stm32_mdf_info_audio = {
> + .hwfifo_set_watermark = stm32_mdf_set_watermark,
> + .write_raw = stm32_mdf_adc_write_raw,
> + .read_raw = stm32_mdf_adc_read_raw,
> + .read_label = stm32_mdf_adc_read_label,
> +};
[Severity: Medium]
Does stm32_mdf_adc_write_raw() reset adc->requested_sample_freq outside
the direct-mode lock on error?
drivers/iio/adc/stm32-mdf-adc.c:stm32_mdf_adc_write_raw() {
...
{
IIO_DEV_ACQUIRE_DIRECT_MODE(indio_dev, claim);
if (IIO_DEV_ACQUIRE_FAILED(claim))
return -EBUSY;
/* Set requested_sample_freq right now, as used by mdf_adc_set_samp_freq() */
adc->requested_sample_freq = val;
if (adc->dev_data->type == STM32_MDF_IIO) {
ret = stm32_mdf_core_lock_kclk_rate(adc->mdf);
if (!ret) {
ret = mdf_adc_set_samp_freq(indio_dev);
stm32_mdf_core_unlock_kclk_rate(adc->mdf);
}
}
}
if (ret)
adc->requested_sample_freq = 0;
...
}
The IIO_DEV_ACQUIRE_DIRECT_MODE() guard releases direct mode at the end of
the inner block, before adc->requested_sample_freq is cleared. If
stm32_mdf_core_lock_kclk_rate() or mdf_adc_set_samp_freq() fails, a
concurrent direct-mode writer or buffer enable can observe the failed
frequency or have its own valid update overwritten by 0.
> @@ -1530,6 +1577,38 @@ static irqreturn_t stm32_mdf_irq(int irq, void *arg)
> return IRQ_HANDLED;
> }
>
> +static int stm32_mdf_audio_init(struct device *dev, struct iio_dev *indio_dev)
> +{
> + struct stm32_mdf_adc *adc = iio_priv(indio_dev);
> + struct iio_chan_spec *ch;
> + int ret;
> +
> + ch = devm_kzalloc(&indio_dev->dev, sizeof(*ch), GFP_KERNEL);
> + if (!ch)
> + return -ENOMEM;
> +
> + adc->channels = devm_kzalloc(&indio_dev->dev, sizeof(*adc->channels), GFP_KERNEL);
> + if (!adc->channels)
> + return -ENOMEM;
> +
> + ret = stm32_mdf_adc_chan_init(indio_dev, ch);
> + if (ret < 0)
> + return dev_err_probe(dev, ret, "Channels init failed\n");
> +
> + indio_dev->num_channels = 1;
> + indio_dev->channels = ch;
[Severity: Medium]
Can stm32_mdf_adc_chan_init() write past ch and adc->channels or leave them
uninitialized here?
stm32_mdf_audio_init() allocates a single element for ch and adc->channels,
but stm32_mdf_adc_chan_init() loops over every child node without a
compatible property and indexes channels[chan_idx] and
adc->channels[chan_idx] without checking the count. If a dmic node has
more than one channel child node, stm32_mdf_adc_chan_init_one() writes out
of bounds on both allocations. If there are no channel child nodes (such
as on a secondary interleaved filter), stm32_mdf_adc_chan_init() returns 0,
leaving indio_dev->num_channels set to 1 with ch[0] and adc->channels[0]
uninitialized.
[ ... ]
> @@ -1599,11 +1678,77 @@ static const struct stm32_mdf_dev_data stm32h7_mdf_adc_data = {
> .init = stm32_mdf_adc_init,
> };
>
> +static const struct stm32_mdf_dev_data stm32_mdf_audio_data = {
> + .type = STM32_MDF_AUDIO,
> + .init = stm32_mdf_audio_init,
> +};
> +
> +/**
> + * stm32_mdf_get_buff_cb() - register a callback that will be called when
> + * DMA transfer period is achieved.
> + *
> + * @iio_dev: Handle to IIO device.
> + * @cb: Pointer to callback function:
> + * - data: pointer to data buffer
> + * - size: size in byte of the data buffer
> + * - private: pointer to consumer private structure.
> + * @private: Pointer to consumer private structure.
> + *
> + * Return: 0 on success, or a negative error code on failure.
> + */
> +int stm32_mdf_get_buff_cb(struct iio_dev *iio_dev,
> + int (*cb)(const void *data, size_t size, void *private), void *private)
> +{
> + struct stm32_mdf_adc *adc;
> +
> + if (!iio_dev)
> + return -EINVAL;
> + adc = iio_priv(iio_dev);
> +
> + if (!adc)
> + return -EINVAL;
> +
> + adc->cb = cb;
> + adc->cb_priv = private;
> +
> + return 0;
> +}
> +EXPORT_SYMBOL_GPL(stm32_mdf_get_buff_cb);
> +
> +/**
> + * stm32_mdf_release_buff_cb() - Unregister the buffer callback
> + *
> + * @iio_dev: Handle to IIO device.
> + *
> + * Return: 0 on success, or a negative error code on failure.
> + */
> +int stm32_mdf_release_buff_cb(struct iio_dev *iio_dev)
> +{
> + struct stm32_mdf_adc *adc;
> +
> + if (!iio_dev)
> + return -EINVAL;
> + adc = iio_priv(iio_dev);
> +
> + if (!adc)
> + return -EINVAL;
> +
> + adc->cb = NULL;
> + adc->cb_priv = NULL;
> +
> + return 0;
> +}
> +EXPORT_SYMBOL_GPL(stm32_mdf_release_buff_cb);
[Severity: High]
Can stm32_mdf_get_buff_cb() and stm32_mdf_release_buff_cb() race with
stm32_mdf_adc_dma_buffer_done() and cause a NULL pointer dereference?
In the ASoC consumer driver, iio_channel_start_all_cb() is called during
DAI prepare, which runs stm32_mdf_adc_postenable() and starts cyclic DMA
before PCM trigger start, keeping it running across trigger stop. As a
result, the DMA completion callback stm32_mdf_adc_dma_buffer_done() is
already running when stm32_mdf_get_buff_cb() and
stm32_mdf_release_buff_cb() update adc->cb and adc->cb_priv without locking
or ordering:
CPU0 CPU1
stm32_mdf_get_buff_cb()
adc->cb = cb;
stm32_mdf_adc_dma_buffer_done()
if (adc->cb)
adc->cb(..., adc->cb_priv);
<-- cb_priv is still NULL
adc->cb_priv = private;
Similarly, stm32_mdf_release_buff_cb() clears adc->cb and adc->cb_priv
without synchronizing with stm32_mdf_adc_dma_buffer_done(), so CPU1 can
pass a cleared adc->cb_priv or jump through a NULL adc->cb after the
if (adc->cb) check.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118-upstream_mdf_v2-v2-0-7939d3c3720a@foss.st.com?part=10
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH v2 11/11] ASoC: stm32: add mdf dai support
2026-11-18 15:17 ` [PATCH v2 11/11] ASoC: stm32: add mdf dai support Olivier Moysan
@ 2026-11-18 16:08 ` sashiko-bot
0 siblings, 0 replies; 22+ messages in thread
From: sashiko-bot @ 2026-11-18 16:08 UTC (permalink / raw)
To: Olivier Moysan
Cc: devicetree, dri-devel, linux-sound, robh, conor+dt, media-ci
Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [High] ASoC: stm32: amdf: wrong S16_LE source offset on wrap in
stm32_afsdm_pcm_cb()
- [High] ASoC: stm32: amdf: broken period elapsed condition in
stm32_afsdm_pcm_cb()
- [High] ASoC: stm32: amdf: broken watermark and channel limits in
interleaved mode
- [High] ASoC: stm32: amdf: unsynchronized DMA callback registration,
stop, and pos update
- [Medium] ASoC: stm32: amdf: SNDRV_PCM_INFO_PAUSE set without pause
trigger handling
- [Low] ASoC: stm32: amdf: invalid %pK format in dev_dbg() in
stm32_afsdm_pcm_cb()
- [Low] ASoC: stm32: amdf: missing period_bytes_min in stm32_amdf_pcm_hw
--
commit 2a312f0d863bc42aba2f6fceb749066af9dab645
Author: Olivier Moysan <olivier.moysan@foss.st.com>
ASoC: stm32: add mdf dai support
This commit adds an ASoC DAI and platform driver for PDM digital
microphones connected to the STM32 Multi-function Digital Filter (MDF)
IP via the IIO consumer buffer interface.
> diff --git a/sound/soc/stm/stm32_amdf.c b/sound/soc/stm/stm32_amdf.c
> new file mode 100644
> index 0000000000000..a733a7a2997fa
> --- /dev/null
> +++ b/sound/soc/stm/stm32_amdf.c
> @@ -0,0 +1,375 @@
[ ... ]
> +static const struct snd_pcm_hardware stm32_amdf_pcm_hw = {
> + .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
> + SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_PAUSE,
> + .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE,
> +
> + .channels_min = 1,
> + .channels_max = 1,
> +
> + .periods_min = 2,
> + .periods_max = MDF_MAX_PERIODS,
> +
> + .period_bytes_max = MDF_MAX_PERIOD_SIZE,
> + .buffer_bytes_max = MDF_MAX_PERIODS * MDF_MAX_PERIOD_SIZE
> +};
[Severity: Low]
Should stm32_amdf_pcm_hw define .period_bytes_min?
Without .period_bytes_min, SNDRV_PCM_HW_PARAM_PERIOD_BYTES has a minimum
of 0. An unconstrained hw_params request can then fail during interval
refinement with -EINVAL, or userspace can configure a 1-frame period and
set the IIO watermark to 1, generating a DMA interrupt on every frame.
[ ... ]
> +static void stm32_memcpy_32to16(void *dest, const void *src, size_t n)
> +{
> + unsigned int i = 0;
> + u16 *d = (u16 *)dest, *s = (u16 *)src;
> +
> + s++;
> + for (i = n >> 1; i > 0; i--) {
> + *d++ = *s++;
> + s++;
> + }
> +}
> +
> +static int stm32_afsdm_pcm_cb(const void *data, size_t size, void *private)
> +{
> + struct stm32_amdf_priv *priv = private;
> + struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(priv->substream);
> + u8 *pcm_buff = priv->pcm_buff;
> + u8 *src_buff = (u8 *)data;
> + unsigned int old_pos = priv->pos;
> + size_t buff_size = snd_pcm_lib_buffer_bytes(priv->substream);
> + size_t period_size = snd_pcm_lib_period_bytes(priv->substream);
> + size_t cur_size, src_size = size;
> + snd_pcm_format_t format = priv->substream->runtime->format;
> +
> + if (format == SNDRV_PCM_FORMAT_S16_LE)
> + src_size >>= 1;
> + cur_size = src_size;
> +
> + dev_dbg(rtd->dev, "%s: buff_add :%pK, pos = %d, size = %zu\n",
> + __func__, &pcm_buff[priv->pos], priv->pos, src_size);
[Severity: Low]
Does %pK work in stm32_afsdm_pcm_cb()?
This callback runs from the DMA completion handler in softirq or hardirq
context, where %pK prints "pK-error" when kptr_restrict is 1; commit
4db4ce15706d ("ASoC: stm32: dfsdm: don't use %pK through printk")
switched the same debug print in stm32_adfsdm.c to %p.
> +
> + if ((priv->pos + src_size) > buff_size) {
> + if (format == SNDRV_PCM_FORMAT_S16_LE)
> + stm32_memcpy_32to16(&pcm_buff[priv->pos], src_buff, buff_size - priv->pos);
> + else
> + memcpy(&pcm_buff[priv->pos], src_buff, buff_size - priv->pos);
> + cur_size -= buff_size - priv->pos;
> + priv->pos = 0;
> + }
> +
> + if (format == SNDRV_PCM_FORMAT_S16_LE)
> + stm32_memcpy_32to16(&pcm_buff[priv->pos], &src_buff[src_size - cur_size], cur_size);
> + else
> + memcpy(&pcm_buff[priv->pos], &src_buff[src_size - cur_size], cur_size);
[Severity: High]
Does the second stm32_memcpy_32to16() call read from the wrong offset in
src_buff when the ring buffer wraps in SNDRV_PCM_FORMAT_S16_LE mode?
When format is SNDRV_PCM_FORMAT_S16_LE, src_size and cur_size are halved
to measure 16-bit destination bytes, while src_buff holds 32-bit samples
and stm32_memcpy_32to16() consumes 2 * n source bytes to write n
destination bytes. After the first copy writes buff_size - priv->pos
destination bytes, indexing &src_buff[src_size - cur_size] advances
src_buff by only half the consumed source bytes instead of
2 * (src_size - cur_size), re-copying earlier samples and misaligning
the 16-bit word offset whenever an odd number of samples was copied
before the wrap.
> +
> + priv->pos = (priv->pos + cur_size) % buff_size;
> +
> + if (cur_size != src_size || (old_pos && (old_pos % period_size < size)))
> + snd_pcm_period_elapsed(priv->substream);
> +
> + return 0;
> +}
[Severity: High]
Does this skip snd_pcm_period_elapsed() whenever old_pos is 0?
At stream start (and at the start of every new buffer cycle when
buff_size is a multiple of period_size), old_pos is 0 and cur_size
equals src_size, so the condition evaluates to false and drops the first
period notification of every buffer cycle. When old_pos is non-zero,
old_pos % period_size < size also compares the pre-copy PCM byte offset
against the unscaled 32-bit DMA size (which is twice the PCM byte count
in S16_LE mode) rather than checking whether advancing by src_size bytes
crossed a period boundary.
> +
> +static int stm32_amdf_trigger(struct snd_soc_component *component,
> + struct snd_pcm_substream *substream, int cmd)
> +{
> + struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
> + struct stm32_amdf_priv *priv =
> + snd_soc_dai_get_drvdata(snd_soc_rtd_to_cpu(rtd, 0));
> +
> + switch (cmd) {
> + case SNDRV_PCM_TRIGGER_START:
> + case SNDRV_PCM_TRIGGER_RESUME:
> + priv->pos = 0;
> + return stm32_mdf_get_buff_cb(priv->iio_ch->indio_dev, stm32_afsdm_pcm_cb, priv);
> + case SNDRV_PCM_TRIGGER_SUSPEND:
> + case SNDRV_PCM_TRIGGER_STOP:
> + return stm32_mdf_release_buff_cb(priv->iio_ch->indio_dev);
> + }
> +
> + return -EINVAL;
> +}
[Severity: Medium]
Does stm32_amdf_trigger() fail with -EINVAL on pause requests despite
stm32_amdf_pcm_hw advertising SNDRV_PCM_INFO_PAUSE?
SNDRV_PCM_TRIGGER_PAUSE_PUSH and SNDRV_PCM_TRIGGER_PAUSE_RELEASE are not
handled in the switch and fall through to return -EINVAL. In addition,
SNDRV_PCM_TRIGGER_RESUME resets priv->pos to 0 instead of preserving the
buffer position across suspend/resume.
[Severity: High]
Can stm32_amdf_trigger() race with an active DMA completion callback in
stm32_mdf_adc_dma_buffer_done()?
stm32_amdf_dai_prepare() starts cyclic IIO DMA via
iio_channel_start_all_cb() before trigger() is called, and DMA continues
running until stm32_amdf_shutdown(). While DMA callbacks are firing,
stm32_mdf_get_buff_cb() and stm32_mdf_release_buff_cb() set or clear
adc->cb and adc->cb_priv without locking or stopping DMA:
drivers/iio/adc/stm32-mdf-adc.c:stm32_mdf_adc_dma_buffer_done() {
...
if (adc->bufi >= adc->buf_sz) {
if (adc->cb)
adc->cb(&adc->rx_buf[old_pos], adc->buf_sz - old_pos, adc->cb_priv);
adc->bufi = 0;
old_pos = 0;
}
if (adc->dev_data->type == STM32_MDF_IIO)
iio_push_to_buffers(indio_dev, buffer);
available -= indio_dev->scan_bytes;
}
if (adc->cb)
adc->cb(&adc->rx_buf[old_pos], adc->bufi - old_pos, adc->cb_priv);
}
CPU0 CPU1 (<softirq>)
stm32_amdf_trigger(..., STOP)
stm32_mdf_release_buff_cb()
stm32_mdf_adc_dma_buffer_done()
if (adc->cb)
adc->cb = NULL;
adc->cb_priv = NULL;
adc->cb(..., adc->cb_priv)
<-- NULL deref
The same NULL dereference can happen on a single CPU if
snd_pcm_period_elapsed() during the first adc->cb() call in
stm32_mdf_adc_dma_buffer_done() triggers an XRUN stop before the second
adc->cb() call, or on START if CPU1 observes adc->cb before adc->cb_priv
is written. Because STOP does not wait for in-flight callbacks to
finish, stm32_afsdm_pcm_cb() can also keep writing to priv->pcm_buff
after hw_free, and setting priv->pos = 0 mid-copy in stm32_afsdm_pcm_cb()
exposes a transient 0 offset to concurrent lockless
stm32_amdf_pcm_pointer() readers.
[ ... ]
> +static int stm32_amdf_pcm_hw_params(struct snd_soc_component *component,
> + struct snd_pcm_substream *substream,
> + struct snd_pcm_hw_params *params)
> +{
> + struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
> + struct stm32_amdf_priv *priv =
> + snd_soc_dai_get_drvdata(snd_soc_rtd_to_cpu(rtd, 0));
> +
> + priv->pcm_buff = substream->runtime->dma_area;
> +
> + return iio_channel_cb_set_buffer_watermark(priv->iio_cb,
> + params_period_size(params));
> +}
[ ... ]
> +static int stm32_amdf_probe(struct platform_device *pdev)
> +{
[ ... ]
> + channels_max = stm32_mdf_get_sub_channels_nb(priv->iio_ch->indio_dev);
> + priv->dai_drv.capture.channels_max = channels_max;
[Severity: High]
Does multi-channel interleaved capture misconfigure the IIO watermark and
channel constraints when channels_max > 1?
In stm32_mdf_audio_init(), the audio IIO device always sets
indio_dev->num_channels = 1 with 32-bit storage, so indio_dev->scan_bytes
is 4 bytes (one sample) rather than a multi-channel frame:
drivers/iio/adc/stm32-mdf-adc.c:stm32_mdf_set_watermark() {
...
watermark = min(val, MDF_DMA_BUFFER_SIZE / (2 * scan_bytes));
adc->buf_sz = 2 * watermark * scan_bytes;
}
Passing params_period_size(params) (in frames) instead of
params_period_size(params) * params_channels(params) sizes each DMA
period to 1 / channels of the ALSA period. Also, stm32_amdf_pcm_hw
keeps .channels_max = 1 (overriding the DAI's channels_max in
stm32_amdf_pcm_open()), and priv->dai_drv.capture.channels_min is left
at 1 even though the MDF hardware always interleaves all channels_max
filters into the DMA stream.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118-upstream_mdf_v2-v2-0-7939d3c3720a@foss.st.com?part=11
^ permalink raw reply [flat|nested] 22+ messages in thread
end of thread, other threads:[~2026-11-18 16:08 UTC | newest]
Thread overview: 22+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox