1 // SPDX-License-Identifier: GPL-2.0
3 * This file is the ADC part of the STM32 DFSDM driver
5 * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
6 * Author: Arnaud Pouliquen <arnaud.pouliquen@st.com>.
9 #include <linux/dmaengine.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/interrupt.h>
12 #include <linux/iio/buffer.h>
13 #include <linux/iio/hw-consumer.h>
14 #include <linux/iio/iio.h>
15 #include <linux/iio/sysfs.h>
16 #include <linux/module.h>
17 #include <linux/of_device.h>
18 #include <linux/platform_device.h>
19 #include <linux/regmap.h>
20 #include <linux/slab.h>
22 #include "stm32-dfsdm.h"
24 #define DFSDM_DMA_BUFFER_SIZE (4 * PAGE_SIZE)
26 /* Conversion timeout */
27 #define DFSDM_TIMEOUT_US 100000
28 #define DFSDM_TIMEOUT (msecs_to_jiffies(DFSDM_TIMEOUT_US / 1000))
30 /* Oversampling attribute default */
31 #define DFSDM_DEFAULT_OVERSAMPLING 100
33 /* Oversampling max values */
34 #define DFSDM_MAX_INT_OVERSAMPLING 256
35 #define DFSDM_MAX_FL_OVERSAMPLING 1024
37 /* Max sample resolutions */
38 #define DFSDM_MAX_RES BIT(31)
39 #define DFSDM_DATA_RES BIT(23)
41 enum sd_converter_type {
46 struct stm32_dfsdm_dev_data {
48 int (*init)(struct iio_dev *indio_dev);
49 unsigned int num_channels;
50 const struct regmap_config *regmap_cfg;
53 struct stm32_dfsdm_adc {
54 struct stm32_dfsdm *dfsdm;
55 const struct stm32_dfsdm_dev_data *dev_data;
59 unsigned int oversamp;
60 struct iio_hw_consumer *hwc;
61 struct completion completion;
65 unsigned int spi_freq; /* SPI bus clock frequency */
66 unsigned int sample_freq; /* Sample frequency after filter decimation */
67 int (*cb)(const void *data, size_t size, void *cb_priv);
72 unsigned int bufi; /* Buffer current position */
73 unsigned int buf_sz; /* Buffer size */
74 struct dma_chan *dma_chan;
78 struct stm32_dfsdm_str2field {
83 /* DFSDM channel serial interface type */
84 static const struct stm32_dfsdm_str2field stm32_dfsdm_chan_type[] = {
85 { "SPI_R", 0 }, /* SPI with data on rising edge */
86 { "SPI_F", 1 }, /* SPI with data on falling edge */
87 { "MANCH_R", 2 }, /* Manchester codec, rising edge = logic 0 */
88 { "MANCH_F", 3 }, /* Manchester codec, falling edge = logic 1 */
92 /* DFSDM channel clock source */
93 static const struct stm32_dfsdm_str2field stm32_dfsdm_chan_src[] = {
94 /* External SPI clock (CLKIN x) */
95 { "CLKIN", DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL },
96 /* Internal SPI clock (CLKOUT) */
97 { "CLKOUT", DFSDM_CHANNEL_SPI_CLOCK_INTERNAL },
98 /* Internal SPI clock divided by 2 (falling edge) */
99 { "CLKOUT_F", DFSDM_CHANNEL_SPI_CLOCK_INTERNAL_DIV2_FALLING },
100 /* Internal SPI clock divided by 2 (falling edge) */
101 { "CLKOUT_R", DFSDM_CHANNEL_SPI_CLOCK_INTERNAL_DIV2_RISING },
105 static int stm32_dfsdm_str2val(const char *str,
106 const struct stm32_dfsdm_str2field *list)
108 const struct stm32_dfsdm_str2field *p = list;
110 for (p = list; p && p->name; p++)
111 if (!strcmp(p->name, str))
117 static int stm32_dfsdm_set_osrs(struct stm32_dfsdm_filter *fl,
118 unsigned int fast, unsigned int oversamp)
120 unsigned int i, d, fosr, iosr;
123 unsigned int m = 1; /* multiplication factor */
124 unsigned int p = fl->ford; /* filter order (ford) */
126 pr_debug("%s: Requested oversampling: %d\n", __func__, oversamp);
128 * This function tries to compute filter oversampling and integrator
129 * oversampling, base on oversampling ratio requested by user.
131 * Decimation d depends on the filter order and the oversampling ratios.
133 * fosr: filter over sampling ratio
134 * iosr: integrator over sampling ratio
136 if (fl->ford == DFSDM_FASTSINC_ORDER) {
142 * Look for filter and integrator oversampling ratios which allows
143 * to reach 24 bits data output resolution.
144 * Leave as soon as if exact resolution if reached.
145 * Otherwise the higher resolution below 32 bits is kept.
148 for (fosr = 1; fosr <= DFSDM_MAX_FL_OVERSAMPLING; fosr++) {
149 for (iosr = 1; iosr <= DFSDM_MAX_INT_OVERSAMPLING; iosr++) {
152 else if (fl->ford == DFSDM_FASTSINC_ORDER)
153 d = fosr * (iosr + 3) + 2;
155 d = fosr * (iosr - 1 + p) + p;
159 else if (d != oversamp)
162 * Check resolution (limited to signed 32 bits)
165 * res = m * fosr^p x iosr (with m=1, p=ford)
167 * res = m * fosr^p x iosr (with m=2, p=2)
170 for (i = p - 1; i > 0; i--) {
171 res = res * (u64)fosr;
172 if (res > DFSDM_MAX_RES)
175 if (res > DFSDM_MAX_RES)
177 res = res * (u64)m * (u64)iosr;
178 if (res > DFSDM_MAX_RES)
181 delta = res - DFSDM_DATA_RES;
183 if (res >= fl->res) {
188 pr_debug("%s: fosr = %d, iosr = %d\n",
189 __func__, fl->fosr, fl->iosr);
203 static int stm32_dfsdm_start_channel(struct stm32_dfsdm *dfsdm,
206 return regmap_update_bits(dfsdm->regmap, DFSDM_CHCFGR1(ch_id),
207 DFSDM_CHCFGR1_CHEN_MASK,
208 DFSDM_CHCFGR1_CHEN(1));
211 static void stm32_dfsdm_stop_channel(struct stm32_dfsdm *dfsdm,
214 regmap_update_bits(dfsdm->regmap, DFSDM_CHCFGR1(ch_id),
215 DFSDM_CHCFGR1_CHEN_MASK, DFSDM_CHCFGR1_CHEN(0));
218 static int stm32_dfsdm_chan_configure(struct stm32_dfsdm *dfsdm,
219 struct stm32_dfsdm_channel *ch)
221 unsigned int id = ch->id;
222 struct regmap *regmap = dfsdm->regmap;
225 ret = regmap_update_bits(regmap, DFSDM_CHCFGR1(id),
226 DFSDM_CHCFGR1_SITP_MASK,
227 DFSDM_CHCFGR1_SITP(ch->type));
230 ret = regmap_update_bits(regmap, DFSDM_CHCFGR1(id),
231 DFSDM_CHCFGR1_SPICKSEL_MASK,
232 DFSDM_CHCFGR1_SPICKSEL(ch->src));
235 return regmap_update_bits(regmap, DFSDM_CHCFGR1(id),
236 DFSDM_CHCFGR1_CHINSEL_MASK,
237 DFSDM_CHCFGR1_CHINSEL(ch->alt_si));
240 static int stm32_dfsdm_start_filter(struct stm32_dfsdm *dfsdm,
246 ret = regmap_update_bits(dfsdm->regmap, DFSDM_CR1(fl_id),
247 DFSDM_CR1_DFEN_MASK, DFSDM_CR1_DFEN(1));
251 /* Start conversion */
252 return regmap_update_bits(dfsdm->regmap, DFSDM_CR1(fl_id),
253 DFSDM_CR1_RSWSTART_MASK,
254 DFSDM_CR1_RSWSTART(1));
257 static void stm32_dfsdm_stop_filter(struct stm32_dfsdm *dfsdm, unsigned int fl_id)
259 /* Disable conversion */
260 regmap_update_bits(dfsdm->regmap, DFSDM_CR1(fl_id),
261 DFSDM_CR1_DFEN_MASK, DFSDM_CR1_DFEN(0));
264 static int stm32_dfsdm_filter_configure(struct stm32_dfsdm *dfsdm,
265 unsigned int fl_id, unsigned int ch_id)
267 struct regmap *regmap = dfsdm->regmap;
268 struct stm32_dfsdm_filter *fl = &dfsdm->fl_list[fl_id];
271 /* Average integrator oversampling */
272 ret = regmap_update_bits(regmap, DFSDM_FCR(fl_id), DFSDM_FCR_IOSR_MASK,
273 DFSDM_FCR_IOSR(fl->iosr - 1));
277 /* Filter order and Oversampling */
278 ret = regmap_update_bits(regmap, DFSDM_FCR(fl_id), DFSDM_FCR_FOSR_MASK,
279 DFSDM_FCR_FOSR(fl->fosr - 1));
283 ret = regmap_update_bits(regmap, DFSDM_FCR(fl_id), DFSDM_FCR_FORD_MASK,
284 DFSDM_FCR_FORD(fl->ford));
288 /* No scan mode supported for the moment */
289 ret = regmap_update_bits(regmap, DFSDM_CR1(fl_id), DFSDM_CR1_RCH_MASK,
290 DFSDM_CR1_RCH(ch_id));
294 return regmap_update_bits(regmap, DFSDM_CR1(fl_id),
295 DFSDM_CR1_RSYNC_MASK,
296 DFSDM_CR1_RSYNC(fl->sync_mode));
299 static int stm32_dfsdm_channel_parse_of(struct stm32_dfsdm *dfsdm,
300 struct iio_dev *indio_dev,
301 struct iio_chan_spec *ch)
303 struct stm32_dfsdm_channel *df_ch;
305 int chan_idx = ch->scan_index;
308 ret = of_property_read_u32_index(indio_dev->dev.of_node,
309 "st,adc-channels", chan_idx,
312 dev_err(&indio_dev->dev,
313 " Error parsing 'st,adc-channels' for idx %d\n",
317 if (ch->channel >= dfsdm->num_chs) {
318 dev_err(&indio_dev->dev,
319 " Error bad channel number %d (max = %d)\n",
320 ch->channel, dfsdm->num_chs);
324 ret = of_property_read_string_index(indio_dev->dev.of_node,
325 "st,adc-channel-names", chan_idx,
326 &ch->datasheet_name);
328 dev_err(&indio_dev->dev,
329 " Error parsing 'st,adc-channel-names' for idx %d\n",
334 df_ch = &dfsdm->ch_list[ch->channel];
335 df_ch->id = ch->channel;
337 ret = of_property_read_string_index(indio_dev->dev.of_node,
338 "st,adc-channel-types", chan_idx,
341 val = stm32_dfsdm_str2val(of_str, stm32_dfsdm_chan_type);
349 ret = of_property_read_string_index(indio_dev->dev.of_node,
350 "st,adc-channel-clk-src", chan_idx,
353 val = stm32_dfsdm_str2val(of_str, stm32_dfsdm_chan_src);
361 ret = of_property_read_u32_index(indio_dev->dev.of_node,
362 "st,adc-alt-channel", chan_idx,
370 static ssize_t dfsdm_adc_audio_get_spiclk(struct iio_dev *indio_dev,
372 const struct iio_chan_spec *chan,
375 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
377 return snprintf(buf, PAGE_SIZE, "%d\n", adc->spi_freq);
380 static ssize_t dfsdm_adc_audio_set_spiclk(struct iio_dev *indio_dev,
382 const struct iio_chan_spec *chan,
383 const char *buf, size_t len)
385 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
386 struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[adc->fl_id];
387 struct stm32_dfsdm_channel *ch = &adc->dfsdm->ch_list[chan->channel];
388 unsigned int sample_freq = adc->sample_freq;
389 unsigned int spi_freq;
392 dev_err(&indio_dev->dev, "enter %s\n", __func__);
393 /* If DFSDM is master on SPI, SPI freq can not be updated */
394 if (ch->src != DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL)
397 ret = kstrtoint(buf, 0, &spi_freq);
405 if (spi_freq % sample_freq)
406 dev_warn(&indio_dev->dev,
407 "Sampling rate not accurate (%d)\n",
408 spi_freq / (spi_freq / sample_freq));
410 ret = stm32_dfsdm_set_osrs(fl, 0, (spi_freq / sample_freq));
412 dev_err(&indio_dev->dev,
413 "No filter parameters that match!\n");
417 adc->spi_freq = spi_freq;
422 static int stm32_dfsdm_start_conv(struct stm32_dfsdm_adc *adc,
423 const struct iio_chan_spec *chan,
426 struct regmap *regmap = adc->dfsdm->regmap;
428 unsigned int dma_en = 0, cont_en = 0;
430 ret = stm32_dfsdm_start_channel(adc->dfsdm, chan->channel);
434 ret = stm32_dfsdm_filter_configure(adc->dfsdm, adc->fl_id,
440 /* Enable DMA transfer*/
441 dma_en = DFSDM_CR1_RDMAEN(1);
442 /* Enable conversion triggered by SPI clock*/
443 cont_en = DFSDM_CR1_RCONT(1);
445 /* Enable DMA transfer*/
446 ret = regmap_update_bits(regmap, DFSDM_CR1(adc->fl_id),
447 DFSDM_CR1_RDMAEN_MASK, dma_en);
451 /* Enable conversion triggered by SPI clock*/
452 ret = regmap_update_bits(regmap, DFSDM_CR1(adc->fl_id),
453 DFSDM_CR1_RCONT_MASK, cont_en);
457 ret = stm32_dfsdm_start_filter(adc->dfsdm, adc->fl_id);
464 regmap_update_bits(regmap, DFSDM_CR1(adc->fl_id),
465 DFSDM_CR1_RDMAEN_MASK, 0);
467 regmap_update_bits(regmap, DFSDM_CR1(adc->fl_id),
468 DFSDM_CR1_RCONT_MASK, 0);
469 stm32_dfsdm_stop_channel(adc->dfsdm, chan->channel);
474 static void stm32_dfsdm_stop_conv(struct stm32_dfsdm_adc *adc,
475 const struct iio_chan_spec *chan)
477 struct regmap *regmap = adc->dfsdm->regmap;
479 stm32_dfsdm_stop_filter(adc->dfsdm, adc->fl_id);
481 /* Clean conversion options */
482 regmap_update_bits(regmap, DFSDM_CR1(adc->fl_id),
483 DFSDM_CR1_RDMAEN_MASK, 0);
485 regmap_update_bits(regmap, DFSDM_CR1(adc->fl_id),
486 DFSDM_CR1_RCONT_MASK, 0);
488 stm32_dfsdm_stop_channel(adc->dfsdm, chan->channel);
491 static int stm32_dfsdm_set_watermark(struct iio_dev *indio_dev,
494 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
495 unsigned int watermark = DFSDM_DMA_BUFFER_SIZE / 2;
498 * DMA cyclic transfers are used, buffer is split into two periods.
500 * - always one buffer (period) DMA is working on
501 * - one buffer (period) driver pushed to ASoC side.
503 watermark = min(watermark, val * (unsigned int)(sizeof(u32)));
504 adc->buf_sz = watermark * 2;
509 static unsigned int stm32_dfsdm_adc_dma_residue(struct stm32_dfsdm_adc *adc)
511 struct dma_tx_state state;
512 enum dma_status status;
514 status = dmaengine_tx_status(adc->dma_chan,
515 adc->dma_chan->cookie,
517 if (status == DMA_IN_PROGRESS) {
518 /* Residue is size in bytes from end of buffer */
519 unsigned int i = adc->buf_sz - state.residue;
522 /* Return available bytes */
524 size = i - adc->bufi;
526 size = adc->buf_sz + i - adc->bufi;
534 static void stm32_dfsdm_audio_dma_buffer_done(void *data)
536 struct iio_dev *indio_dev = data;
537 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
538 int available = stm32_dfsdm_adc_dma_residue(adc);
542 * FIXME: In Kernel interface does not support cyclic DMA buffer,and
543 * offers only an interface to push data samples per samples.
544 * For this reason IIO buffer interface is not used and interface is
545 * bypassed using a private callback registered by ASoC.
546 * This should be a temporary solution waiting a cyclic DMA engine
550 dev_dbg(&indio_dev->dev, "%s: pos = %d, available = %d\n", __func__,
551 adc->bufi, available);
554 while (available >= indio_dev->scan_bytes) {
555 u32 *buffer = (u32 *)&adc->rx_buf[adc->bufi];
557 /* Mask 8 LSB that contains the channel ID */
558 *buffer = (*buffer & 0xFFFFFF00) << 8;
559 available -= indio_dev->scan_bytes;
560 adc->bufi += indio_dev->scan_bytes;
561 if (adc->bufi >= adc->buf_sz) {
563 adc->cb(&adc->rx_buf[old_pos],
564 adc->buf_sz - old_pos, adc->cb_priv);
570 adc->cb(&adc->rx_buf[old_pos], adc->bufi - old_pos,
574 static int stm32_dfsdm_adc_dma_start(struct iio_dev *indio_dev)
576 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
577 struct dma_async_tx_descriptor *desc;
584 dev_dbg(&indio_dev->dev, "%s size=%d watermark=%d\n", __func__,
585 adc->buf_sz, adc->buf_sz / 2);
587 /* Prepare a DMA cyclic transaction */
588 desc = dmaengine_prep_dma_cyclic(adc->dma_chan,
590 adc->buf_sz, adc->buf_sz / 2,
596 desc->callback = stm32_dfsdm_audio_dma_buffer_done;
597 desc->callback_param = indio_dev;
599 cookie = dmaengine_submit(desc);
600 ret = dma_submit_error(cookie);
602 dmaengine_terminate_all(adc->dma_chan);
606 /* Issue pending DMA requests */
607 dma_async_issue_pending(adc->dma_chan);
612 static int stm32_dfsdm_postenable(struct iio_dev *indio_dev)
614 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
615 const struct iio_chan_spec *chan = &indio_dev->channels[0];
618 /* Reset adc buffer index */
621 ret = stm32_dfsdm_start_dfsdm(adc->dfsdm);
625 ret = stm32_dfsdm_start_conv(adc, chan, true);
627 dev_err(&indio_dev->dev, "Can't start conversion\n");
632 ret = stm32_dfsdm_adc_dma_start(indio_dev);
634 dev_err(&indio_dev->dev, "Can't start DMA\n");
642 stm32_dfsdm_stop_conv(adc, chan);
644 stm32_dfsdm_stop_dfsdm(adc->dfsdm);
649 static int stm32_dfsdm_predisable(struct iio_dev *indio_dev)
651 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
652 const struct iio_chan_spec *chan = &indio_dev->channels[0];
655 dmaengine_terminate_all(adc->dma_chan);
657 stm32_dfsdm_stop_conv(adc, chan);
659 stm32_dfsdm_stop_dfsdm(adc->dfsdm);
664 static const struct iio_buffer_setup_ops stm32_dfsdm_buffer_setup_ops = {
665 .postenable = &stm32_dfsdm_postenable,
666 .predisable = &stm32_dfsdm_predisable,
670 * stm32_dfsdm_get_buff_cb() - register a callback that will be called when
671 * DMA transfer period is achieved.
673 * @iio_dev: Handle to IIO device.
674 * @cb: Pointer to callback function:
675 * - data: pointer to data buffer
676 * - size: size in byte of the data buffer
677 * - private: pointer to consumer private structure.
678 * @private: Pointer to consumer private structure.
680 int stm32_dfsdm_get_buff_cb(struct iio_dev *iio_dev,
681 int (*cb)(const void *data, size_t size,
685 struct stm32_dfsdm_adc *adc;
689 adc = iio_priv(iio_dev);
692 adc->cb_priv = private;
696 EXPORT_SYMBOL_GPL(stm32_dfsdm_get_buff_cb);
699 * stm32_dfsdm_release_buff_cb - unregister buffer callback
701 * @iio_dev: Handle to IIO device.
703 int stm32_dfsdm_release_buff_cb(struct iio_dev *iio_dev)
705 struct stm32_dfsdm_adc *adc;
709 adc = iio_priv(iio_dev);
716 EXPORT_SYMBOL_GPL(stm32_dfsdm_release_buff_cb);
718 static int stm32_dfsdm_single_conv(struct iio_dev *indio_dev,
719 const struct iio_chan_spec *chan, int *res)
721 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
725 reinit_completion(&adc->completion);
729 ret = stm32_dfsdm_start_dfsdm(adc->dfsdm);
733 ret = regmap_update_bits(adc->dfsdm->regmap, DFSDM_CR2(adc->fl_id),
734 DFSDM_CR2_REOCIE_MASK, DFSDM_CR2_REOCIE(1));
738 ret = stm32_dfsdm_start_conv(adc, chan, false);
740 regmap_update_bits(adc->dfsdm->regmap, DFSDM_CR2(adc->fl_id),
741 DFSDM_CR2_REOCIE_MASK, DFSDM_CR2_REOCIE(0));
745 timeout = wait_for_completion_interruptible_timeout(&adc->completion,
748 /* Mask IRQ for regular conversion achievement*/
749 regmap_update_bits(adc->dfsdm->regmap, DFSDM_CR2(adc->fl_id),
750 DFSDM_CR2_REOCIE_MASK, DFSDM_CR2_REOCIE(0));
754 else if (timeout < 0)
759 stm32_dfsdm_stop_conv(adc, chan);
762 stm32_dfsdm_stop_dfsdm(adc->dfsdm);
767 static int stm32_dfsdm_write_raw(struct iio_dev *indio_dev,
768 struct iio_chan_spec const *chan,
769 int val, int val2, long mask)
771 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
772 struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[adc->fl_id];
773 struct stm32_dfsdm_channel *ch = &adc->dfsdm->ch_list[chan->channel];
774 unsigned int spi_freq;
778 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
779 ret = stm32_dfsdm_set_osrs(fl, 0, val);
785 case IIO_CHAN_INFO_SAMP_FREQ:
790 case DFSDM_CHANNEL_SPI_CLOCK_INTERNAL:
791 spi_freq = adc->dfsdm->spi_master_freq;
793 case DFSDM_CHANNEL_SPI_CLOCK_INTERNAL_DIV2_FALLING:
794 case DFSDM_CHANNEL_SPI_CLOCK_INTERNAL_DIV2_RISING:
795 spi_freq = adc->dfsdm->spi_master_freq / 2;
798 spi_freq = adc->spi_freq;
802 dev_warn(&indio_dev->dev,
803 "Sampling rate not accurate (%d)\n",
804 spi_freq / (spi_freq / val));
806 ret = stm32_dfsdm_set_osrs(fl, 0, (spi_freq / val));
808 dev_err(&indio_dev->dev,
809 "Not able to find parameter that match!\n");
812 adc->sample_freq = val;
820 static int stm32_dfsdm_read_raw(struct iio_dev *indio_dev,
821 struct iio_chan_spec const *chan, int *val,
822 int *val2, long mask)
824 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
828 case IIO_CHAN_INFO_RAW:
829 ret = iio_hw_consumer_enable(adc->hwc);
831 dev_err(&indio_dev->dev,
832 "%s: IIO enable failed (channel %d)\n",
833 __func__, chan->channel);
836 ret = stm32_dfsdm_single_conv(indio_dev, chan, val);
837 iio_hw_consumer_disable(adc->hwc);
839 dev_err(&indio_dev->dev,
840 "%s: Conversion failed (channel %d)\n",
841 __func__, chan->channel);
846 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
847 *val = adc->oversamp;
851 case IIO_CHAN_INFO_SAMP_FREQ:
852 *val = adc->sample_freq;
860 static const struct iio_info stm32_dfsdm_info_audio = {
861 .hwfifo_set_watermark = stm32_dfsdm_set_watermark,
862 .read_raw = stm32_dfsdm_read_raw,
863 .write_raw = stm32_dfsdm_write_raw,
866 static const struct iio_info stm32_dfsdm_info_adc = {
867 .read_raw = stm32_dfsdm_read_raw,
868 .write_raw = stm32_dfsdm_write_raw,
871 static irqreturn_t stm32_dfsdm_irq(int irq, void *arg)
873 struct stm32_dfsdm_adc *adc = arg;
874 struct iio_dev *indio_dev = iio_priv_to_dev(adc);
875 struct regmap *regmap = adc->dfsdm->regmap;
876 unsigned int status, int_en;
878 regmap_read(regmap, DFSDM_ISR(adc->fl_id), &status);
879 regmap_read(regmap, DFSDM_CR2(adc->fl_id), &int_en);
881 if (status & DFSDM_ISR_REOCF_MASK) {
882 /* Read the data register clean the IRQ status */
883 regmap_read(regmap, DFSDM_RDATAR(adc->fl_id), adc->buffer);
884 complete(&adc->completion);
887 if (status & DFSDM_ISR_ROVRF_MASK) {
888 if (int_en & DFSDM_CR2_ROVRIE_MASK)
889 dev_warn(&indio_dev->dev, "Overrun detected\n");
890 regmap_update_bits(regmap, DFSDM_ICR(adc->fl_id),
891 DFSDM_ICR_CLRROVRF_MASK,
892 DFSDM_ICR_CLRROVRF_MASK);
899 * Define external info for SPI Frequency and audio sampling rate that can be
900 * configured by ASoC driver through consumer.h API
902 static const struct iio_chan_spec_ext_info dfsdm_adc_audio_ext_info[] = {
903 /* spi_clk_freq : clock freq on SPI/manchester bus used by channel */
905 .name = "spi_clk_freq",
906 .shared = IIO_SHARED_BY_TYPE,
907 .read = dfsdm_adc_audio_get_spiclk,
908 .write = dfsdm_adc_audio_set_spiclk,
913 static void stm32_dfsdm_dma_release(struct iio_dev *indio_dev)
915 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
918 dma_free_coherent(adc->dma_chan->device->dev,
919 DFSDM_DMA_BUFFER_SIZE,
920 adc->rx_buf, adc->dma_buf);
921 dma_release_channel(adc->dma_chan);
925 static int stm32_dfsdm_dma_request(struct iio_dev *indio_dev)
927 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
928 struct dma_slave_config config = {
929 .src_addr = (dma_addr_t)adc->dfsdm->phys_base +
930 DFSDM_RDATAR(adc->fl_id),
931 .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
935 adc->dma_chan = dma_request_slave_channel(&indio_dev->dev, "rx");
939 adc->rx_buf = dma_alloc_coherent(adc->dma_chan->device->dev,
940 DFSDM_DMA_BUFFER_SIZE,
941 &adc->dma_buf, GFP_KERNEL);
947 ret = dmaengine_slave_config(adc->dma_chan, &config);
954 dma_free_coherent(adc->dma_chan->device->dev, DFSDM_DMA_BUFFER_SIZE,
955 adc->rx_buf, adc->dma_buf);
957 dma_release_channel(adc->dma_chan);
962 static int stm32_dfsdm_adc_chan_init_one(struct iio_dev *indio_dev,
963 struct iio_chan_spec *ch)
965 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
968 ret = stm32_dfsdm_channel_parse_of(adc->dfsdm, indio_dev, ch);
972 ch->type = IIO_VOLTAGE;
976 * IIO_CHAN_INFO_RAW: used to compute regular conversion
977 * IIO_CHAN_INFO_OVERSAMPLING_RATIO: used to set oversampling
979 ch->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
980 ch->info_mask_shared_by_all = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO);
982 if (adc->dev_data->type == DFSDM_AUDIO) {
983 ch->scan_type.sign = 's';
984 ch->ext_info = dfsdm_adc_audio_ext_info;
986 ch->scan_type.sign = 'u';
988 ch->scan_type.realbits = 24;
989 ch->scan_type.storagebits = 32;
991 return stm32_dfsdm_chan_configure(adc->dfsdm,
992 &adc->dfsdm->ch_list[ch->channel]);
995 static int stm32_dfsdm_audio_init(struct iio_dev *indio_dev)
997 struct iio_chan_spec *ch;
998 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
999 struct stm32_dfsdm_channel *d_ch;
1002 indio_dev->modes |= INDIO_BUFFER_SOFTWARE;
1003 indio_dev->setup_ops = &stm32_dfsdm_buffer_setup_ops;
1005 ch = devm_kzalloc(&indio_dev->dev, sizeof(*ch), GFP_KERNEL);
1011 ret = stm32_dfsdm_adc_chan_init_one(indio_dev, ch);
1013 dev_err(&indio_dev->dev, "Channels init failed\n");
1016 ch->info_mask_separate = BIT(IIO_CHAN_INFO_SAMP_FREQ);
1018 d_ch = &adc->dfsdm->ch_list[ch->channel];
1019 if (d_ch->src != DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL)
1020 adc->spi_freq = adc->dfsdm->spi_master_freq;
1022 indio_dev->num_channels = 1;
1023 indio_dev->channels = ch;
1025 return stm32_dfsdm_dma_request(indio_dev);
1028 static int stm32_dfsdm_adc_init(struct iio_dev *indio_dev)
1030 struct iio_chan_spec *ch;
1031 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
1035 adc->oversamp = DFSDM_DEFAULT_OVERSAMPLING;
1036 ret = stm32_dfsdm_set_osrs(&adc->dfsdm->fl_list[adc->fl_id], 0,
1041 num_ch = of_property_count_u32_elems(indio_dev->dev.of_node,
1043 if (num_ch < 0 || num_ch > adc->dfsdm->num_chs) {
1044 dev_err(&indio_dev->dev, "Bad st,adc-channels\n");
1045 return num_ch < 0 ? num_ch : -EINVAL;
1048 /* Bind to SD modulator IIO device */
1049 adc->hwc = devm_iio_hw_consumer_alloc(&indio_dev->dev);
1050 if (IS_ERR(adc->hwc))
1051 return -EPROBE_DEFER;
1053 ch = devm_kcalloc(&indio_dev->dev, num_ch, sizeof(*ch),
1058 for (chan_idx = 0; chan_idx < num_ch; chan_idx++) {
1059 ch[chan_idx].scan_index = chan_idx;
1060 ret = stm32_dfsdm_adc_chan_init_one(indio_dev, &ch[chan_idx]);
1062 dev_err(&indio_dev->dev, "Channels init failed\n");
1067 indio_dev->num_channels = num_ch;
1068 indio_dev->channels = ch;
1070 init_completion(&adc->completion);
1075 static const struct stm32_dfsdm_dev_data stm32h7_dfsdm_adc_data = {
1077 .init = stm32_dfsdm_adc_init,
1080 static const struct stm32_dfsdm_dev_data stm32h7_dfsdm_audio_data = {
1081 .type = DFSDM_AUDIO,
1082 .init = stm32_dfsdm_audio_init,
1085 static const struct of_device_id stm32_dfsdm_adc_match[] = {
1087 .compatible = "st,stm32-dfsdm-adc",
1088 .data = &stm32h7_dfsdm_adc_data,
1091 .compatible = "st,stm32-dfsdm-dmic",
1092 .data = &stm32h7_dfsdm_audio_data,
1097 static int stm32_dfsdm_adc_probe(struct platform_device *pdev)
1099 struct device *dev = &pdev->dev;
1100 struct stm32_dfsdm_adc *adc;
1101 struct device_node *np = dev->of_node;
1102 const struct stm32_dfsdm_dev_data *dev_data;
1103 struct iio_dev *iio;
1108 dev_data = of_device_get_match_data(dev);
1109 iio = devm_iio_device_alloc(dev, sizeof(*adc));
1111 dev_err(dev, "%s: Failed to allocate IIO\n", __func__);
1115 adc = iio_priv(iio);
1116 adc->dfsdm = dev_get_drvdata(dev->parent);
1118 iio->dev.parent = dev;
1119 iio->dev.of_node = np;
1120 iio->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
1122 platform_set_drvdata(pdev, adc);
1124 ret = of_property_read_u32(dev->of_node, "reg", &adc->fl_id);
1126 dev_err(dev, "Missing reg property\n");
1130 name = devm_kzalloc(dev, sizeof("dfsdm-adc0"), GFP_KERNEL);
1133 if (dev_data->type == DFSDM_AUDIO) {
1134 iio->info = &stm32_dfsdm_info_audio;
1135 snprintf(name, sizeof("dfsdm-pdm0"), "dfsdm-pdm%d", adc->fl_id);
1137 iio->info = &stm32_dfsdm_info_adc;
1138 snprintf(name, sizeof("dfsdm-adc0"), "dfsdm-adc%d", adc->fl_id);
1143 * In a first step IRQs generated for channels are not treated.
1144 * So IRQ associated to filter instance 0 is dedicated to the Filter 0.
1146 irq = platform_get_irq(pdev, 0);
1147 ret = devm_request_irq(dev, irq, stm32_dfsdm_irq,
1148 0, pdev->name, adc);
1150 dev_err(dev, "Failed to request IRQ\n");
1154 ret = of_property_read_u32(dev->of_node, "st,filter-order", &val);
1156 dev_err(dev, "Failed to set filter order\n");
1160 adc->dfsdm->fl_list[adc->fl_id].ford = val;
1162 ret = of_property_read_u32(dev->of_node, "st,filter0-sync", &val);
1164 adc->dfsdm->fl_list[adc->fl_id].sync_mode = val;
1166 adc->dev_data = dev_data;
1167 ret = dev_data->init(iio);
1171 ret = iio_device_register(iio);
1175 dev_err(dev, "of_platform_populate\n");
1176 if (dev_data->type == DFSDM_AUDIO) {
1177 ret = of_platform_populate(np, NULL, NULL, dev);
1179 dev_err(dev, "Failed to find an audio DAI\n");
1180 goto err_unregister;
1187 iio_device_unregister(iio);
1189 stm32_dfsdm_dma_release(iio);
1194 static int stm32_dfsdm_adc_remove(struct platform_device *pdev)
1196 struct stm32_dfsdm_adc *adc = platform_get_drvdata(pdev);
1197 struct iio_dev *indio_dev = iio_priv_to_dev(adc);
1199 if (adc->dev_data->type == DFSDM_AUDIO)
1200 of_platform_depopulate(&pdev->dev);
1201 iio_device_unregister(indio_dev);
1202 stm32_dfsdm_dma_release(indio_dev);
1207 static struct platform_driver stm32_dfsdm_adc_driver = {
1209 .name = "stm32-dfsdm-adc",
1210 .of_match_table = stm32_dfsdm_adc_match,
1212 .probe = stm32_dfsdm_adc_probe,
1213 .remove = stm32_dfsdm_adc_remove,
1215 module_platform_driver(stm32_dfsdm_adc_driver);
1217 MODULE_DESCRIPTION("STM32 sigma delta ADC");
1218 MODULE_AUTHOR("Arnaud Pouliquen <arnaud.pouliquen@st.com>");
1219 MODULE_LICENSE("GPL v2");