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.
147 for (fosr
= 1; fosr
<= DFSDM_MAX_FL_OVERSAMPLING
; fosr
++) {
148 for (iosr
= 1; iosr
<= DFSDM_MAX_INT_OVERSAMPLING
; iosr
++) {
151 else if (fl
->ford
== DFSDM_FASTSINC_ORDER
)
152 d
= fosr
* (iosr
+ 3) + 2;
154 d
= fosr
* (iosr
- 1 + p
) + p
;
158 else if (d
!= oversamp
)
161 * Check resolution (limited to signed 32 bits)
164 * res = m * fosr^p x iosr (with m=1, p=ford)
166 * res = m * fosr^p x iosr (with m=2, p=2)
169 for (i
= p
- 1; i
> 0; i
--) {
170 res
= res
* (u64
)fosr
;
171 if (res
> DFSDM_MAX_RES
)
174 if (res
> DFSDM_MAX_RES
)
176 res
= res
* (u64
)m
* (u64
)iosr
;
177 if (res
> DFSDM_MAX_RES
)
180 delta
= res
- DFSDM_DATA_RES
;
182 if (res
>= fl
->res
) {
187 pr_debug("%s: fosr = %d, iosr = %d\n",
188 __func__
, fl
->fosr
, fl
->iosr
);
202 static int stm32_dfsdm_start_channel(struct stm32_dfsdm
*dfsdm
,
205 return regmap_update_bits(dfsdm
->regmap
, DFSDM_CHCFGR1(ch_id
),
206 DFSDM_CHCFGR1_CHEN_MASK
,
207 DFSDM_CHCFGR1_CHEN(1));
210 static void stm32_dfsdm_stop_channel(struct stm32_dfsdm
*dfsdm
,
213 regmap_update_bits(dfsdm
->regmap
, DFSDM_CHCFGR1(ch_id
),
214 DFSDM_CHCFGR1_CHEN_MASK
, DFSDM_CHCFGR1_CHEN(0));
217 static int stm32_dfsdm_chan_configure(struct stm32_dfsdm
*dfsdm
,
218 struct stm32_dfsdm_channel
*ch
)
220 unsigned int id
= ch
->id
;
221 struct regmap
*regmap
= dfsdm
->regmap
;
224 ret
= regmap_update_bits(regmap
, DFSDM_CHCFGR1(id
),
225 DFSDM_CHCFGR1_SITP_MASK
,
226 DFSDM_CHCFGR1_SITP(ch
->type
));
229 ret
= regmap_update_bits(regmap
, DFSDM_CHCFGR1(id
),
230 DFSDM_CHCFGR1_SPICKSEL_MASK
,
231 DFSDM_CHCFGR1_SPICKSEL(ch
->src
));
234 return regmap_update_bits(regmap
, DFSDM_CHCFGR1(id
),
235 DFSDM_CHCFGR1_CHINSEL_MASK
,
236 DFSDM_CHCFGR1_CHINSEL(ch
->alt_si
));
239 static int stm32_dfsdm_start_filter(struct stm32_dfsdm
*dfsdm
,
245 ret
= regmap_update_bits(dfsdm
->regmap
, DFSDM_CR1(fl_id
),
246 DFSDM_CR1_DFEN_MASK
, DFSDM_CR1_DFEN(1));
250 /* Start conversion */
251 return regmap_update_bits(dfsdm
->regmap
, DFSDM_CR1(fl_id
),
252 DFSDM_CR1_RSWSTART_MASK
,
253 DFSDM_CR1_RSWSTART(1));
256 static void stm32_dfsdm_stop_filter(struct stm32_dfsdm
*dfsdm
,
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
= adc
->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
:
788 if (ch
->src
!= DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL
)
789 spi_freq
= adc
->dfsdm
->spi_master_freq
;
792 dev_warn(&indio_dev
->dev
,
793 "Sampling rate not accurate (%d)\n",
794 spi_freq
/ (spi_freq
/ val
));
796 ret
= stm32_dfsdm_set_osrs(fl
, 0, (spi_freq
/ val
));
798 dev_err(&indio_dev
->dev
,
799 "Not able to find parameter that match!\n");
802 adc
->sample_freq
= val
;
810 static int stm32_dfsdm_read_raw(struct iio_dev
*indio_dev
,
811 struct iio_chan_spec
const *chan
, int *val
,
812 int *val2
, long mask
)
814 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
818 case IIO_CHAN_INFO_RAW
:
819 ret
= iio_hw_consumer_enable(adc
->hwc
);
821 dev_err(&indio_dev
->dev
,
822 "%s: IIO enable failed (channel %d)\n",
823 __func__
, chan
->channel
);
826 ret
= stm32_dfsdm_single_conv(indio_dev
, chan
, val
);
827 iio_hw_consumer_disable(adc
->hwc
);
829 dev_err(&indio_dev
->dev
,
830 "%s: Conversion failed (channel %d)\n",
831 __func__
, chan
->channel
);
836 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
837 *val
= adc
->oversamp
;
841 case IIO_CHAN_INFO_SAMP_FREQ
:
842 *val
= adc
->sample_freq
;
850 static const struct iio_info stm32_dfsdm_info_audio
= {
851 .hwfifo_set_watermark
= stm32_dfsdm_set_watermark
,
852 .read_raw
= stm32_dfsdm_read_raw
,
853 .write_raw
= stm32_dfsdm_write_raw
,
856 static const struct iio_info stm32_dfsdm_info_adc
= {
857 .read_raw
= stm32_dfsdm_read_raw
,
858 .write_raw
= stm32_dfsdm_write_raw
,
861 static irqreturn_t
stm32_dfsdm_irq(int irq
, void *arg
)
863 struct stm32_dfsdm_adc
*adc
= arg
;
864 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
865 struct regmap
*regmap
= adc
->dfsdm
->regmap
;
866 unsigned int status
, int_en
;
868 regmap_read(regmap
, DFSDM_ISR(adc
->fl_id
), &status
);
869 regmap_read(regmap
, DFSDM_CR2(adc
->fl_id
), &int_en
);
871 if (status
& DFSDM_ISR_REOCF_MASK
) {
872 /* Read the data register clean the IRQ status */
873 regmap_read(regmap
, DFSDM_RDATAR(adc
->fl_id
), adc
->buffer
);
874 complete(&adc
->completion
);
877 if (status
& DFSDM_ISR_ROVRF_MASK
) {
878 if (int_en
& DFSDM_CR2_ROVRIE_MASK
)
879 dev_warn(&indio_dev
->dev
, "Overrun detected\n");
880 regmap_update_bits(regmap
, DFSDM_ICR(adc
->fl_id
),
881 DFSDM_ICR_CLRROVRF_MASK
,
882 DFSDM_ICR_CLRROVRF_MASK
);
889 * Define external info for SPI Frequency and audio sampling rate that can be
890 * configured by ASoC driver through consumer.h API
892 static const struct iio_chan_spec_ext_info dfsdm_adc_audio_ext_info
[] = {
893 /* spi_clk_freq : clock freq on SPI/manchester bus used by channel */
895 .name
= "spi_clk_freq",
896 .shared
= IIO_SHARED_BY_TYPE
,
897 .read
= dfsdm_adc_audio_get_spiclk
,
898 .write
= dfsdm_adc_audio_set_spiclk
,
903 static void stm32_dfsdm_dma_release(struct iio_dev
*indio_dev
)
905 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
908 dma_free_coherent(adc
->dma_chan
->device
->dev
,
909 DFSDM_DMA_BUFFER_SIZE
,
910 adc
->rx_buf
, adc
->dma_buf
);
911 dma_release_channel(adc
->dma_chan
);
915 static int stm32_dfsdm_dma_request(struct iio_dev
*indio_dev
)
917 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
918 struct dma_slave_config config
= {
919 .src_addr
= (dma_addr_t
)adc
->dfsdm
->phys_base
+
920 DFSDM_RDATAR(adc
->fl_id
),
921 .src_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
,
925 adc
->dma_chan
= dma_request_slave_channel(&indio_dev
->dev
, "rx");
929 adc
->rx_buf
= dma_alloc_coherent(adc
->dma_chan
->device
->dev
,
930 DFSDM_DMA_BUFFER_SIZE
,
931 &adc
->dma_buf
, GFP_KERNEL
);
937 ret
= dmaengine_slave_config(adc
->dma_chan
, &config
);
944 dma_free_coherent(adc
->dma_chan
->device
->dev
, DFSDM_DMA_BUFFER_SIZE
,
945 adc
->rx_buf
, adc
->dma_buf
);
947 dma_release_channel(adc
->dma_chan
);
952 static int stm32_dfsdm_adc_chan_init_one(struct iio_dev
*indio_dev
,
953 struct iio_chan_spec
*ch
)
955 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
958 ret
= stm32_dfsdm_channel_parse_of(adc
->dfsdm
, indio_dev
, ch
);
962 ch
->type
= IIO_VOLTAGE
;
966 * IIO_CHAN_INFO_RAW: used to compute regular conversion
967 * IIO_CHAN_INFO_OVERSAMPLING_RATIO: used to set oversampling
969 ch
->info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
);
970 ch
->info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
);
972 if (adc
->dev_data
->type
== DFSDM_AUDIO
) {
973 ch
->scan_type
.sign
= 's';
974 ch
->ext_info
= dfsdm_adc_audio_ext_info
;
976 ch
->scan_type
.sign
= 'u';
978 ch
->scan_type
.realbits
= 24;
979 ch
->scan_type
.storagebits
= 32;
981 return stm32_dfsdm_chan_configure(adc
->dfsdm
,
982 &adc
->dfsdm
->ch_list
[ch
->channel
]);
985 static int stm32_dfsdm_audio_init(struct iio_dev
*indio_dev
)
987 struct iio_chan_spec
*ch
;
988 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
989 struct stm32_dfsdm_channel
*d_ch
;
992 indio_dev
->modes
|= INDIO_BUFFER_SOFTWARE
;
993 indio_dev
->setup_ops
= &stm32_dfsdm_buffer_setup_ops
;
995 ch
= devm_kzalloc(&indio_dev
->dev
, sizeof(*ch
), GFP_KERNEL
);
1001 ret
= stm32_dfsdm_adc_chan_init_one(indio_dev
, ch
);
1003 dev_err(&indio_dev
->dev
, "Channels init failed\n");
1006 ch
->info_mask_separate
= BIT(IIO_CHAN_INFO_SAMP_FREQ
);
1008 d_ch
= &adc
->dfsdm
->ch_list
[ch
->channel
];
1009 if (d_ch
->src
!= DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL
)
1010 adc
->spi_freq
= adc
->dfsdm
->spi_master_freq
;
1012 indio_dev
->num_channels
= 1;
1013 indio_dev
->channels
= ch
;
1015 return stm32_dfsdm_dma_request(indio_dev
);
1018 static int stm32_dfsdm_adc_init(struct iio_dev
*indio_dev
)
1020 struct iio_chan_spec
*ch
;
1021 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
1025 adc
->oversamp
= DFSDM_DEFAULT_OVERSAMPLING
;
1026 ret
= stm32_dfsdm_set_osrs(&adc
->dfsdm
->fl_list
[adc
->fl_id
], 0,
1031 num_ch
= of_property_count_u32_elems(indio_dev
->dev
.of_node
,
1033 if (num_ch
< 0 || num_ch
> adc
->dfsdm
->num_chs
) {
1034 dev_err(&indio_dev
->dev
, "Bad st,adc-channels\n");
1035 return num_ch
< 0 ? num_ch
: -EINVAL
;
1038 /* Bind to SD modulator IIO device */
1039 adc
->hwc
= devm_iio_hw_consumer_alloc(&indio_dev
->dev
);
1040 if (IS_ERR(adc
->hwc
))
1041 return -EPROBE_DEFER
;
1043 ch
= devm_kcalloc(&indio_dev
->dev
, num_ch
, sizeof(*ch
),
1048 for (chan_idx
= 0; chan_idx
< num_ch
; chan_idx
++) {
1049 ch
[chan_idx
].scan_index
= chan_idx
;
1050 ret
= stm32_dfsdm_adc_chan_init_one(indio_dev
, &ch
[chan_idx
]);
1052 dev_err(&indio_dev
->dev
, "Channels init failed\n");
1057 indio_dev
->num_channels
= num_ch
;
1058 indio_dev
->channels
= ch
;
1060 init_completion(&adc
->completion
);
1065 static const struct stm32_dfsdm_dev_data stm32h7_dfsdm_adc_data
= {
1067 .init
= stm32_dfsdm_adc_init
,
1070 static const struct stm32_dfsdm_dev_data stm32h7_dfsdm_audio_data
= {
1071 .type
= DFSDM_AUDIO
,
1072 .init
= stm32_dfsdm_audio_init
,
1075 static const struct of_device_id stm32_dfsdm_adc_match
[] = {
1077 .compatible
= "st,stm32-dfsdm-adc",
1078 .data
= &stm32h7_dfsdm_adc_data
,
1081 .compatible
= "st,stm32-dfsdm-dmic",
1082 .data
= &stm32h7_dfsdm_audio_data
,
1087 static int stm32_dfsdm_adc_probe(struct platform_device
*pdev
)
1089 struct device
*dev
= &pdev
->dev
;
1090 struct stm32_dfsdm_adc
*adc
;
1091 struct device_node
*np
= dev
->of_node
;
1092 const struct stm32_dfsdm_dev_data
*dev_data
;
1093 struct iio_dev
*iio
;
1097 dev_data
= of_device_get_match_data(dev
);
1098 iio
= devm_iio_device_alloc(dev
, sizeof(*adc
));
1100 dev_err(dev
, "%s: Failed to allocate IIO\n", __func__
);
1104 adc
= iio_priv(iio
);
1105 adc
->dfsdm
= dev_get_drvdata(dev
->parent
);
1107 iio
->dev
.parent
= dev
;
1108 iio
->dev
.of_node
= np
;
1109 iio
->modes
= INDIO_DIRECT_MODE
| INDIO_BUFFER_SOFTWARE
;
1111 platform_set_drvdata(pdev
, adc
);
1113 ret
= of_property_read_u32(dev
->of_node
, "reg", &adc
->fl_id
);
1114 if (ret
!= 0 || adc
->fl_id
>= adc
->dfsdm
->num_fls
) {
1115 dev_err(dev
, "Missing or bad reg property\n");
1119 name
= devm_kzalloc(dev
, sizeof("dfsdm-adc0"), GFP_KERNEL
);
1122 if (dev_data
->type
== DFSDM_AUDIO
) {
1123 iio
->info
= &stm32_dfsdm_info_audio
;
1124 snprintf(name
, sizeof("dfsdm-pdm0"), "dfsdm-pdm%d", adc
->fl_id
);
1126 iio
->info
= &stm32_dfsdm_info_adc
;
1127 snprintf(name
, sizeof("dfsdm-adc0"), "dfsdm-adc%d", adc
->fl_id
);
1132 * In a first step IRQs generated for channels are not treated.
1133 * So IRQ associated to filter instance 0 is dedicated to the Filter 0.
1135 irq
= platform_get_irq(pdev
, 0);
1136 ret
= devm_request_irq(dev
, irq
, stm32_dfsdm_irq
,
1137 0, pdev
->name
, adc
);
1139 dev_err(dev
, "Failed to request IRQ\n");
1143 ret
= of_property_read_u32(dev
->of_node
, "st,filter-order", &val
);
1145 dev_err(dev
, "Failed to set filter order\n");
1149 adc
->dfsdm
->fl_list
[adc
->fl_id
].ford
= val
;
1151 ret
= of_property_read_u32(dev
->of_node
, "st,filter0-sync", &val
);
1153 adc
->dfsdm
->fl_list
[adc
->fl_id
].sync_mode
= val
;
1155 adc
->dev_data
= dev_data
;
1156 ret
= dev_data
->init(iio
);
1160 ret
= iio_device_register(iio
);
1164 if (dev_data
->type
== DFSDM_AUDIO
) {
1165 ret
= of_platform_populate(np
, NULL
, NULL
, dev
);
1167 dev_err(dev
, "Failed to find an audio DAI\n");
1168 goto err_unregister
;
1175 iio_device_unregister(iio
);
1177 stm32_dfsdm_dma_release(iio
);
1182 static int stm32_dfsdm_adc_remove(struct platform_device
*pdev
)
1184 struct stm32_dfsdm_adc
*adc
= platform_get_drvdata(pdev
);
1185 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
1187 if (adc
->dev_data
->type
== DFSDM_AUDIO
)
1188 of_platform_depopulate(&pdev
->dev
);
1189 iio_device_unregister(indio_dev
);
1190 stm32_dfsdm_dma_release(indio_dev
);
1195 static struct platform_driver stm32_dfsdm_adc_driver
= {
1197 .name
= "stm32-dfsdm-adc",
1198 .of_match_table
= stm32_dfsdm_adc_match
,
1200 .probe
= stm32_dfsdm_adc_probe
,
1201 .remove
= stm32_dfsdm_adc_remove
,
1203 module_platform_driver(stm32_dfsdm_adc_driver
);
1205 MODULE_DESCRIPTION("STM32 sigma delta ADC");
1206 MODULE_AUTHOR("Arnaud Pouliquen <arnaud.pouliquen@st.com>");
1207 MODULE_LICENSE("GPL v2");