From: Olivier Moysan <olivier.moysan@foss.st.com>
To: "Jonathan Cameron" <jic23@kernel.org>,
"David Lechner" <dlechner@baylibre.com>,
"Nuno Sá" <nuno.sa@analog.com>,
"Andy Shevchenko" <andy@kernel.org>,
"Rob Herring" <robh@kernel.org>,
"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
"Conor Dooley" <conor+dt@kernel.org>,
"Maxime Coquelin" <mcoquelin.stm32@gmail.com>,
"Alexandre Torgue" <alexandre.torgue@foss.st.com>,
"Philipp Zabel" <p.zabel@pengutronix.de>,
"Sumit Semwal" <sumit.semwal@linaro.org>,
"Christian König" <christian.koenig@amd.com>,
"Philipp Stanner" <phasta@kernel.org>,
"Arnaud Pouliquen" <arnaud.pouliquen@foss.st.com>,
"Liam Girdwood" <lgirdwood@gmail.com>,
"Mark Brown" <broonie@kernel.org>,
"Jaroslav Kysela" <perex@perex.cz>,
"Takashi Iwai" <tiwai@suse.com>,
"Fabrice Gasnier" <fabrice.gasnier@foss.st.com>
Cc: <linux-iio@vger.kernel.org>, <devicetree@vger.kernel.org>,
<linux-stm32@st-md-mailman.stormreply.com>,
<linux-arm-kernel@lists.infradead.org>,
<linux-kernel@vger.kernel.org>, <linux-media@vger.kernel.org>,
<dri-devel@lists.freedesktop.org>,
<linaro-mm-sig@lists.linaro.org>, <linux-sound@vger.kernel.org>,
"Olivier Moysan" <olivier.moysan@foss.st.com>
Subject: [PATCH v2 11/11] ASoC: stm32: add mdf dai support
Date: Wed, 18 Nov 2026 16:17:56 +0100 [thread overview]
Message-ID: <20261118-upstream_mdf_v2-v2-11-7939d3c3720a@foss.st.com> (raw)
In-Reply-To: <20261118-upstream_mdf_v2-v2-0-7939d3c3720a@foss.st.com>
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
prev parent reply other threads:[~2026-11-18 15:18 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 15:17 [PATCH v2 00/11] iio: adc: stm32: add mdf support for stm32mp2 Olivier Moysan
2026-11-18 15:17 ` [PATCH v2 01/11] dt-bindings: iio: adc: add stm32mp2 mdf filter Olivier Moysan
2026-11-18 15:17 ` [PATCH v2 02/11] iio: adc: stm32-mdf: add core registration Olivier Moysan
2026-11-18 15:17 ` [PATCH v2 03/11] iio: adc: stm32-mdf: add cck clocks provider Olivier Moysan
2026-11-18 15:17 ` [PATCH v2 04/11] iio: adc: stm32-mdf: add serial interfaces support Olivier Moysan
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 ` [PATCH v2 06/11] iio: adc: stm32-mdf: configure cic filters and sample rates Olivier Moysan
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 ` [PATCH v2 08/11] iio: adc: stm32-mdf: add dma buffered acquisition support Olivier Moysan
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 ` [PATCH v2 10/11] iio: adc: stm32-mdf: add dmic audio filter support Olivier Moysan
2026-11-18 15:17 ` Olivier Moysan [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261118-upstream_mdf_v2-v2-11-7939d3c3720a@foss.st.com \
--to=olivier.moysan@foss.st.com \
--cc=alexandre.torgue@foss.st.com \
--cc=andy@kernel.org \
--cc=arnaud.pouliquen@foss.st.com \
--cc=broonie@kernel.org \
--cc=christian.koenig@amd.com \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=dlechner@baylibre.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=fabrice.gasnier@foss.st.com \
--cc=jic23@kernel.org \
--cc=krzk+dt@kernel.org \
--cc=lgirdwood@gmail.com \
--cc=linaro-mm-sig@lists.linaro.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=linux-sound@vger.kernel.org \
--cc=linux-stm32@st-md-mailman.stormreply.com \
--cc=mcoquelin.stm32@gmail.com \
--cc=nuno.sa@analog.com \
--cc=p.zabel@pengutronix.de \
--cc=perex@perex.cz \
--cc=phasta@kernel.org \
--cc=robh@kernel.org \
--cc=sumit.semwal@linaro.org \
--cc=tiwai@suse.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®