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 08/11] iio: adc: stm32-mdf: add dma buffered acquisition support
Date: Wed, 18 Nov 2026 16:17:53 +0100 [thread overview]
Message-ID: <20261118-upstream_mdf_v2-v2-8-7939d3c3720a@foss.st.com> (raw)
In-Reply-To: <20261118-upstream_mdf_v2-v2-0-7939d3c3720a@foss.st.com>
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
next 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 ` Olivier Moysan [this message]
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 ` [PATCH v2 11/11] ASoC: stm32: add mdf dai support Olivier Moysan
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-8-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®