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
;
60 unsigned int oversamp
;
61 struct iio_hw_consumer
*hwc
;
62 struct completion completion
;
66 unsigned int spi_freq
; /* SPI bus clock frequency */
67 unsigned int sample_freq
; /* Sample frequency after filter decimation */
68 int (*cb
)(const void *data
, size_t size
, void *cb_priv
);
73 unsigned int bufi
; /* Buffer current position */
74 unsigned int buf_sz
; /* Buffer size */
75 struct dma_chan
*dma_chan
;
79 struct stm32_dfsdm_str2field
{
84 /* DFSDM channel serial interface type */
85 static const struct stm32_dfsdm_str2field stm32_dfsdm_chan_type
[] = {
86 { "SPI_R", 0 }, /* SPI with data on rising edge */
87 { "SPI_F", 1 }, /* SPI with data on falling edge */
88 { "MANCH_R", 2 }, /* Manchester codec, rising edge = logic 0 */
89 { "MANCH_F", 3 }, /* Manchester codec, falling edge = logic 1 */
93 /* DFSDM channel clock source */
94 static const struct stm32_dfsdm_str2field stm32_dfsdm_chan_src
[] = {
95 /* External SPI clock (CLKIN x) */
96 { "CLKIN", DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL
},
97 /* Internal SPI clock (CLKOUT) */
98 { "CLKOUT", DFSDM_CHANNEL_SPI_CLOCK_INTERNAL
},
99 /* Internal SPI clock divided by 2 (falling edge) */
100 { "CLKOUT_F", DFSDM_CHANNEL_SPI_CLOCK_INTERNAL_DIV2_FALLING
},
101 /* Internal SPI clock divided by 2 (falling edge) */
102 { "CLKOUT_R", DFSDM_CHANNEL_SPI_CLOCK_INTERNAL_DIV2_RISING
},
106 static int stm32_dfsdm_str2val(const char *str
,
107 const struct stm32_dfsdm_str2field
*list
)
109 const struct stm32_dfsdm_str2field
*p
= list
;
111 for (p
= list
; p
&& p
->name
; p
++)
112 if (!strcmp(p
->name
, str
))
118 static int stm32_dfsdm_set_osrs(struct stm32_dfsdm_filter
*fl
,
119 unsigned int fast
, unsigned int oversamp
)
121 unsigned int i
, d
, fosr
, iosr
;
124 unsigned int m
= 1; /* multiplication factor */
125 unsigned int p
= fl
->ford
; /* filter order (ford) */
127 pr_debug("%s: Requested oversampling: %d\n", __func__
, oversamp
);
129 * This function tries to compute filter oversampling and integrator
130 * oversampling, base on oversampling ratio requested by user.
132 * Decimation d depends on the filter order and the oversampling ratios.
134 * fosr: filter over sampling ratio
135 * iosr: integrator over sampling ratio
137 if (fl
->ford
== DFSDM_FASTSINC_ORDER
) {
143 * Look for filter and integrator oversampling ratios which allows
144 * to reach 24 bits data output resolution.
145 * Leave as soon as if exact resolution if reached.
146 * 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
[adc
->ch_id
];
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
, bool dma
)
424 struct regmap
*regmap
= adc
->dfsdm
->regmap
;
426 unsigned int dma_en
= 0, cont_en
= 0;
428 ret
= stm32_dfsdm_start_channel(adc
->dfsdm
, adc
->ch_id
);
432 ret
= stm32_dfsdm_filter_configure(adc
->dfsdm
, adc
->fl_id
,
438 /* Enable DMA transfer*/
439 dma_en
= DFSDM_CR1_RDMAEN(1);
440 /* Enable conversion triggered by SPI clock*/
441 cont_en
= DFSDM_CR1_RCONT(1);
443 /* Enable DMA transfer*/
444 ret
= regmap_update_bits(regmap
, DFSDM_CR1(adc
->fl_id
),
445 DFSDM_CR1_RDMAEN_MASK
, dma_en
);
449 /* Enable conversion triggered by SPI clock*/
450 ret
= regmap_update_bits(regmap
, DFSDM_CR1(adc
->fl_id
),
451 DFSDM_CR1_RCONT_MASK
, cont_en
);
455 ret
= stm32_dfsdm_start_filter(adc
->dfsdm
, adc
->fl_id
);
462 regmap_update_bits(regmap
, DFSDM_CR1(adc
->fl_id
),
463 DFSDM_CR1_RDMAEN_MASK
, 0);
465 regmap_update_bits(regmap
, DFSDM_CR1(adc
->fl_id
),
466 DFSDM_CR1_RCONT_MASK
, 0);
467 stm32_dfsdm_stop_channel(adc
->dfsdm
, adc
->fl_id
);
472 static void stm32_dfsdm_stop_conv(struct stm32_dfsdm_adc
*adc
)
474 struct regmap
*regmap
= adc
->dfsdm
->regmap
;
476 stm32_dfsdm_stop_filter(adc
->dfsdm
, adc
->fl_id
);
478 /* Clean conversion options */
479 regmap_update_bits(regmap
, DFSDM_CR1(adc
->fl_id
),
480 DFSDM_CR1_RDMAEN_MASK
, 0);
482 regmap_update_bits(regmap
, DFSDM_CR1(adc
->fl_id
),
483 DFSDM_CR1_RCONT_MASK
, 0);
485 stm32_dfsdm_stop_channel(adc
->dfsdm
, adc
->ch_id
);
488 static int stm32_dfsdm_set_watermark(struct iio_dev
*indio_dev
,
491 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
492 unsigned int watermark
= DFSDM_DMA_BUFFER_SIZE
/ 2;
495 * DMA cyclic transfers are used, buffer is split into two periods.
497 * - always one buffer (period) DMA is working on
498 * - one buffer (period) driver pushed to ASoC side.
500 watermark
= min(watermark
, val
* (unsigned int)(sizeof(u32
)));
501 adc
->buf_sz
= watermark
* 2;
506 static unsigned int stm32_dfsdm_adc_dma_residue(struct stm32_dfsdm_adc
*adc
)
508 struct dma_tx_state state
;
509 enum dma_status status
;
511 status
= dmaengine_tx_status(adc
->dma_chan
,
512 adc
->dma_chan
->cookie
,
514 if (status
== DMA_IN_PROGRESS
) {
515 /* Residue is size in bytes from end of buffer */
516 unsigned int i
= adc
->buf_sz
- state
.residue
;
519 /* Return available bytes */
521 size
= i
- adc
->bufi
;
523 size
= adc
->buf_sz
+ i
- adc
->bufi
;
531 static void stm32_dfsdm_audio_dma_buffer_done(void *data
)
533 struct iio_dev
*indio_dev
= data
;
534 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
535 int available
= stm32_dfsdm_adc_dma_residue(adc
);
539 * FIXME: In Kernel interface does not support cyclic DMA buffer,and
540 * offers only an interface to push data samples per samples.
541 * For this reason IIO buffer interface is not used and interface is
542 * bypassed using a private callback registered by ASoC.
543 * This should be a temporary solution waiting a cyclic DMA engine
547 dev_dbg(&indio_dev
->dev
, "%s: pos = %d, available = %d\n", __func__
,
548 adc
->bufi
, available
);
551 while (available
>= indio_dev
->scan_bytes
) {
552 u32
*buffer
= (u32
*)&adc
->rx_buf
[adc
->bufi
];
554 /* Mask 8 LSB that contains the channel ID */
555 *buffer
= (*buffer
& 0xFFFFFF00) << 8;
556 available
-= indio_dev
->scan_bytes
;
557 adc
->bufi
+= indio_dev
->scan_bytes
;
558 if (adc
->bufi
>= adc
->buf_sz
) {
560 adc
->cb(&adc
->rx_buf
[old_pos
],
561 adc
->buf_sz
- old_pos
, adc
->cb_priv
);
567 adc
->cb(&adc
->rx_buf
[old_pos
], adc
->bufi
- old_pos
,
571 static int stm32_dfsdm_adc_dma_start(struct iio_dev
*indio_dev
)
573 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
574 struct dma_async_tx_descriptor
*desc
;
581 dev_dbg(&indio_dev
->dev
, "%s size=%d watermark=%d\n", __func__
,
582 adc
->buf_sz
, adc
->buf_sz
/ 2);
584 /* Prepare a DMA cyclic transaction */
585 desc
= dmaengine_prep_dma_cyclic(adc
->dma_chan
,
587 adc
->buf_sz
, adc
->buf_sz
/ 2,
593 desc
->callback
= stm32_dfsdm_audio_dma_buffer_done
;
594 desc
->callback_param
= indio_dev
;
596 cookie
= dmaengine_submit(desc
);
597 ret
= dma_submit_error(cookie
);
599 dmaengine_terminate_all(adc
->dma_chan
);
603 /* Issue pending DMA requests */
604 dma_async_issue_pending(adc
->dma_chan
);
609 static int stm32_dfsdm_postenable(struct iio_dev
*indio_dev
)
611 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
614 /* Reset adc buffer index */
617 ret
= stm32_dfsdm_start_dfsdm(adc
->dfsdm
);
621 ret
= stm32_dfsdm_start_conv(adc
, true);
623 dev_err(&indio_dev
->dev
, "Can't start conversion\n");
628 ret
= stm32_dfsdm_adc_dma_start(indio_dev
);
630 dev_err(&indio_dev
->dev
, "Can't start DMA\n");
638 stm32_dfsdm_stop_conv(adc
);
640 stm32_dfsdm_stop_dfsdm(adc
->dfsdm
);
645 static int stm32_dfsdm_predisable(struct iio_dev
*indio_dev
)
647 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
650 dmaengine_terminate_all(adc
->dma_chan
);
652 stm32_dfsdm_stop_conv(adc
);
654 stm32_dfsdm_stop_dfsdm(adc
->dfsdm
);
659 static const struct iio_buffer_setup_ops stm32_dfsdm_buffer_setup_ops
= {
660 .postenable
= &stm32_dfsdm_postenable
,
661 .predisable
= &stm32_dfsdm_predisable
,
665 * stm32_dfsdm_get_buff_cb() - register a callback that will be called when
666 * DMA transfer period is achieved.
668 * @iio_dev: Handle to IIO device.
669 * @cb: Pointer to callback function:
670 * - data: pointer to data buffer
671 * - size: size in byte of the data buffer
672 * - private: pointer to consumer private structure.
673 * @private: Pointer to consumer private structure.
675 int stm32_dfsdm_get_buff_cb(struct iio_dev
*iio_dev
,
676 int (*cb
)(const void *data
, size_t size
,
680 struct stm32_dfsdm_adc
*adc
;
684 adc
= iio_priv(iio_dev
);
687 adc
->cb_priv
= private;
691 EXPORT_SYMBOL_GPL(stm32_dfsdm_get_buff_cb
);
694 * stm32_dfsdm_release_buff_cb - unregister buffer callback
696 * @iio_dev: Handle to IIO device.
698 int stm32_dfsdm_release_buff_cb(struct iio_dev
*iio_dev
)
700 struct stm32_dfsdm_adc
*adc
;
704 adc
= iio_priv(iio_dev
);
711 EXPORT_SYMBOL_GPL(stm32_dfsdm_release_buff_cb
);
713 static int stm32_dfsdm_single_conv(struct iio_dev
*indio_dev
,
714 const struct iio_chan_spec
*chan
, int *res
)
716 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
720 reinit_completion(&adc
->completion
);
724 ret
= stm32_dfsdm_start_dfsdm(adc
->dfsdm
);
728 ret
= regmap_update_bits(adc
->dfsdm
->regmap
, DFSDM_CR2(adc
->fl_id
),
729 DFSDM_CR2_REOCIE_MASK
, DFSDM_CR2_REOCIE(1));
733 ret
= stm32_dfsdm_start_conv(adc
, false);
735 regmap_update_bits(adc
->dfsdm
->regmap
, DFSDM_CR2(adc
->fl_id
),
736 DFSDM_CR2_REOCIE_MASK
, DFSDM_CR2_REOCIE(0));
740 timeout
= wait_for_completion_interruptible_timeout(&adc
->completion
,
743 /* Mask IRQ for regular conversion achievement*/
744 regmap_update_bits(adc
->dfsdm
->regmap
, DFSDM_CR2(adc
->fl_id
),
745 DFSDM_CR2_REOCIE_MASK
, DFSDM_CR2_REOCIE(0));
749 else if (timeout
< 0)
754 stm32_dfsdm_stop_conv(adc
);
757 stm32_dfsdm_stop_dfsdm(adc
->dfsdm
);
762 static int stm32_dfsdm_write_raw(struct iio_dev
*indio_dev
,
763 struct iio_chan_spec
const *chan
,
764 int val
, int val2
, long mask
)
766 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
767 struct stm32_dfsdm_filter
*fl
= &adc
->dfsdm
->fl_list
[adc
->fl_id
];
768 struct stm32_dfsdm_channel
*ch
= &adc
->dfsdm
->ch_list
[adc
->ch_id
];
769 unsigned int spi_freq
= adc
->spi_freq
;
773 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
774 ret
= stm32_dfsdm_set_osrs(fl
, 0, val
);
780 case IIO_CHAN_INFO_SAMP_FREQ
:
783 if (ch
->src
!= DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL
)
784 spi_freq
= adc
->dfsdm
->spi_master_freq
;
787 dev_warn(&indio_dev
->dev
,
788 "Sampling rate not accurate (%d)\n",
789 spi_freq
/ (spi_freq
/ val
));
791 ret
= stm32_dfsdm_set_osrs(fl
, 0, (spi_freq
/ val
));
793 dev_err(&indio_dev
->dev
,
794 "Not able to find parameter that match!\n");
797 adc
->sample_freq
= val
;
805 static int stm32_dfsdm_read_raw(struct iio_dev
*indio_dev
,
806 struct iio_chan_spec
const *chan
, int *val
,
807 int *val2
, long mask
)
809 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
813 case IIO_CHAN_INFO_RAW
:
814 ret
= iio_hw_consumer_enable(adc
->hwc
);
816 dev_err(&indio_dev
->dev
,
817 "%s: IIO enable failed (channel %d)\n",
818 __func__
, chan
->channel
);
821 ret
= stm32_dfsdm_single_conv(indio_dev
, chan
, val
);
822 iio_hw_consumer_disable(adc
->hwc
);
824 dev_err(&indio_dev
->dev
,
825 "%s: Conversion failed (channel %d)\n",
826 __func__
, chan
->channel
);
831 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
832 *val
= adc
->oversamp
;
836 case IIO_CHAN_INFO_SAMP_FREQ
:
837 *val
= adc
->sample_freq
;
845 static const struct iio_info stm32_dfsdm_info_audio
= {
846 .hwfifo_set_watermark
= stm32_dfsdm_set_watermark
,
847 .read_raw
= stm32_dfsdm_read_raw
,
848 .write_raw
= stm32_dfsdm_write_raw
,
851 static const struct iio_info stm32_dfsdm_info_adc
= {
852 .read_raw
= stm32_dfsdm_read_raw
,
853 .write_raw
= stm32_dfsdm_write_raw
,
856 static irqreturn_t
stm32_dfsdm_irq(int irq
, void *arg
)
858 struct stm32_dfsdm_adc
*adc
= arg
;
859 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
860 struct regmap
*regmap
= adc
->dfsdm
->regmap
;
861 unsigned int status
, int_en
;
863 regmap_read(regmap
, DFSDM_ISR(adc
->fl_id
), &status
);
864 regmap_read(regmap
, DFSDM_CR2(adc
->fl_id
), &int_en
);
866 if (status
& DFSDM_ISR_REOCF_MASK
) {
867 /* Read the data register clean the IRQ status */
868 regmap_read(regmap
, DFSDM_RDATAR(adc
->fl_id
), adc
->buffer
);
869 complete(&adc
->completion
);
872 if (status
& DFSDM_ISR_ROVRF_MASK
) {
873 if (int_en
& DFSDM_CR2_ROVRIE_MASK
)
874 dev_warn(&indio_dev
->dev
, "Overrun detected\n");
875 regmap_update_bits(regmap
, DFSDM_ICR(adc
->fl_id
),
876 DFSDM_ICR_CLRROVRF_MASK
,
877 DFSDM_ICR_CLRROVRF_MASK
);
884 * Define external info for SPI Frequency and audio sampling rate that can be
885 * configured by ASoC driver through consumer.h API
887 static const struct iio_chan_spec_ext_info dfsdm_adc_audio_ext_info
[] = {
888 /* spi_clk_freq : clock freq on SPI/manchester bus used by channel */
890 .name
= "spi_clk_freq",
891 .shared
= IIO_SHARED_BY_TYPE
,
892 .read
= dfsdm_adc_audio_get_spiclk
,
893 .write
= dfsdm_adc_audio_set_spiclk
,
898 static void stm32_dfsdm_dma_release(struct iio_dev
*indio_dev
)
900 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
903 dma_free_coherent(adc
->dma_chan
->device
->dev
,
904 DFSDM_DMA_BUFFER_SIZE
,
905 adc
->rx_buf
, adc
->dma_buf
);
906 dma_release_channel(adc
->dma_chan
);
910 static int stm32_dfsdm_dma_request(struct iio_dev
*indio_dev
)
912 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
913 struct dma_slave_config config
= {
914 .src_addr
= (dma_addr_t
)adc
->dfsdm
->phys_base
+
915 DFSDM_RDATAR(adc
->fl_id
),
916 .src_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
,
920 adc
->dma_chan
= dma_request_slave_channel(&indio_dev
->dev
, "rx");
924 adc
->rx_buf
= dma_alloc_coherent(adc
->dma_chan
->device
->dev
,
925 DFSDM_DMA_BUFFER_SIZE
,
926 &adc
->dma_buf
, GFP_KERNEL
);
932 ret
= dmaengine_slave_config(adc
->dma_chan
, &config
);
939 dma_free_coherent(adc
->dma_chan
->device
->dev
, DFSDM_DMA_BUFFER_SIZE
,
940 adc
->rx_buf
, adc
->dma_buf
);
942 dma_release_channel(adc
->dma_chan
);
947 static int stm32_dfsdm_adc_chan_init_one(struct iio_dev
*indio_dev
,
948 struct iio_chan_spec
*ch
)
950 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
953 ret
= stm32_dfsdm_channel_parse_of(adc
->dfsdm
, indio_dev
, ch
);
957 ch
->type
= IIO_VOLTAGE
;
961 * IIO_CHAN_INFO_RAW: used to compute regular conversion
962 * IIO_CHAN_INFO_OVERSAMPLING_RATIO: used to set oversampling
964 ch
->info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
);
965 ch
->info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
);
967 if (adc
->dev_data
->type
== DFSDM_AUDIO
) {
968 ch
->scan_type
.sign
= 's';
969 ch
->ext_info
= dfsdm_adc_audio_ext_info
;
971 ch
->scan_type
.sign
= 'u';
973 ch
->scan_type
.realbits
= 24;
974 ch
->scan_type
.storagebits
= 32;
975 adc
->ch_id
= ch
->channel
;
977 return stm32_dfsdm_chan_configure(adc
->dfsdm
,
978 &adc
->dfsdm
->ch_list
[ch
->channel
]);
981 static int stm32_dfsdm_audio_init(struct iio_dev
*indio_dev
)
983 struct iio_chan_spec
*ch
;
984 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
985 struct stm32_dfsdm_channel
*d_ch
;
988 indio_dev
->modes
|= INDIO_BUFFER_SOFTWARE
;
989 indio_dev
->setup_ops
= &stm32_dfsdm_buffer_setup_ops
;
991 ch
= devm_kzalloc(&indio_dev
->dev
, sizeof(*ch
), GFP_KERNEL
);
997 ret
= stm32_dfsdm_adc_chan_init_one(indio_dev
, ch
);
999 dev_err(&indio_dev
->dev
, "Channels init failed\n");
1002 ch
->info_mask_separate
= BIT(IIO_CHAN_INFO_SAMP_FREQ
);
1004 d_ch
= &adc
->dfsdm
->ch_list
[adc
->ch_id
];
1005 if (d_ch
->src
!= DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL
)
1006 adc
->spi_freq
= adc
->dfsdm
->spi_master_freq
;
1008 indio_dev
->num_channels
= 1;
1009 indio_dev
->channels
= ch
;
1011 return stm32_dfsdm_dma_request(indio_dev
);
1014 static int stm32_dfsdm_adc_init(struct iio_dev
*indio_dev
)
1016 struct iio_chan_spec
*ch
;
1017 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
1021 adc
->oversamp
= DFSDM_DEFAULT_OVERSAMPLING
;
1022 ret
= stm32_dfsdm_set_osrs(&adc
->dfsdm
->fl_list
[adc
->fl_id
], 0,
1027 num_ch
= of_property_count_u32_elems(indio_dev
->dev
.of_node
,
1029 if (num_ch
< 0 || num_ch
> adc
->dfsdm
->num_chs
) {
1030 dev_err(&indio_dev
->dev
, "Bad st,adc-channels\n");
1031 return num_ch
< 0 ? num_ch
: -EINVAL
;
1034 /* Bind to SD modulator IIO device */
1035 adc
->hwc
= devm_iio_hw_consumer_alloc(&indio_dev
->dev
);
1036 if (IS_ERR(adc
->hwc
))
1037 return -EPROBE_DEFER
;
1039 ch
= devm_kcalloc(&indio_dev
->dev
, num_ch
, sizeof(*ch
),
1044 for (chan_idx
= 0; chan_idx
< num_ch
; chan_idx
++) {
1045 ch
->scan_index
= chan_idx
;
1046 ret
= stm32_dfsdm_adc_chan_init_one(indio_dev
, ch
);
1048 dev_err(&indio_dev
->dev
, "Channels init failed\n");
1053 indio_dev
->num_channels
= num_ch
;
1054 indio_dev
->channels
= ch
;
1056 init_completion(&adc
->completion
);
1061 static const struct stm32_dfsdm_dev_data stm32h7_dfsdm_adc_data
= {
1063 .init
= stm32_dfsdm_adc_init
,
1066 static const struct stm32_dfsdm_dev_data stm32h7_dfsdm_audio_data
= {
1067 .type
= DFSDM_AUDIO
,
1068 .init
= stm32_dfsdm_audio_init
,
1071 static const struct of_device_id stm32_dfsdm_adc_match
[] = {
1073 .compatible
= "st,stm32-dfsdm-adc",
1074 .data
= &stm32h7_dfsdm_adc_data
,
1077 .compatible
= "st,stm32-dfsdm-dmic",
1078 .data
= &stm32h7_dfsdm_audio_data
,
1083 static int stm32_dfsdm_adc_probe(struct platform_device
*pdev
)
1085 struct device
*dev
= &pdev
->dev
;
1086 struct stm32_dfsdm_adc
*adc
;
1087 struct device_node
*np
= dev
->of_node
;
1088 const struct stm32_dfsdm_dev_data
*dev_data
;
1089 struct iio_dev
*iio
;
1094 dev_data
= of_device_get_match_data(dev
);
1095 iio
= devm_iio_device_alloc(dev
, sizeof(*adc
));
1097 dev_err(dev
, "%s: Failed to allocate IIO\n", __func__
);
1101 adc
= iio_priv(iio
);
1102 adc
->dfsdm
= dev_get_drvdata(dev
->parent
);
1104 iio
->dev
.parent
= dev
;
1105 iio
->dev
.of_node
= np
;
1106 iio
->modes
= INDIO_DIRECT_MODE
| INDIO_BUFFER_SOFTWARE
;
1108 platform_set_drvdata(pdev
, adc
);
1110 ret
= of_property_read_u32(dev
->of_node
, "reg", &adc
->fl_id
);
1112 dev_err(dev
, "Missing reg property\n");
1116 name
= devm_kzalloc(dev
, sizeof("dfsdm-adc0"), GFP_KERNEL
);
1119 if (dev_data
->type
== DFSDM_AUDIO
) {
1120 iio
->info
= &stm32_dfsdm_info_audio
;
1121 snprintf(name
, sizeof("dfsdm-pdm0"), "dfsdm-pdm%d", adc
->fl_id
);
1123 iio
->info
= &stm32_dfsdm_info_adc
;
1124 snprintf(name
, sizeof("dfsdm-adc0"), "dfsdm-adc%d", adc
->fl_id
);
1129 * In a first step IRQs generated for channels are not treated.
1130 * So IRQ associated to filter instance 0 is dedicated to the Filter 0.
1132 irq
= platform_get_irq(pdev
, 0);
1133 ret
= devm_request_irq(dev
, irq
, stm32_dfsdm_irq
,
1134 0, pdev
->name
, adc
);
1136 dev_err(dev
, "Failed to request IRQ\n");
1140 ret
= of_property_read_u32(dev
->of_node
, "st,filter-order", &val
);
1142 dev_err(dev
, "Failed to set filter order\n");
1146 adc
->dfsdm
->fl_list
[adc
->fl_id
].ford
= val
;
1148 ret
= of_property_read_u32(dev
->of_node
, "st,filter0-sync", &val
);
1150 adc
->dfsdm
->fl_list
[adc
->fl_id
].sync_mode
= val
;
1152 adc
->dev_data
= dev_data
;
1153 ret
= dev_data
->init(iio
);
1157 ret
= iio_device_register(iio
);
1161 dev_err(dev
, "of_platform_populate\n");
1162 if (dev_data
->type
== DFSDM_AUDIO
) {
1163 ret
= of_platform_populate(np
, NULL
, NULL
, dev
);
1165 dev_err(dev
, "Failed to find an audio DAI\n");
1166 goto err_unregister
;
1173 iio_device_unregister(iio
);
1175 stm32_dfsdm_dma_release(iio
);
1180 static int stm32_dfsdm_adc_remove(struct platform_device
*pdev
)
1182 struct stm32_dfsdm_adc
*adc
= platform_get_drvdata(pdev
);
1183 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
1185 if (adc
->dev_data
->type
== DFSDM_AUDIO
)
1186 of_platform_depopulate(&pdev
->dev
);
1187 iio_device_unregister(indio_dev
);
1188 stm32_dfsdm_dma_release(indio_dev
);
1193 static struct platform_driver stm32_dfsdm_adc_driver
= {
1195 .name
= "stm32-dfsdm-adc",
1196 .of_match_table
= stm32_dfsdm_adc_match
,
1198 .probe
= stm32_dfsdm_adc_probe
,
1199 .remove
= stm32_dfsdm_adc_remove
,
1201 module_platform_driver(stm32_dfsdm_adc_driver
);
1203 MODULE_DESCRIPTION("STM32 sigma delta ADC");
1204 MODULE_AUTHOR("Arnaud Pouliquen <arnaud.pouliquen@st.com>");
1205 MODULE_LICENSE("GPL v2");