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/iio/adc/stm32-dfsdm-adc.h>
12 #include <linux/iio/buffer.h>
13 #include <linux/iio/hw-consumer.h>
14 #include <linux/iio/sysfs.h>
15 #include <linux/interrupt.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
,
260 /* Disable conversion */
261 regmap_update_bits(dfsdm
->regmap
, DFSDM_CR1(fl_id
),
262 DFSDM_CR1_DFEN_MASK
, DFSDM_CR1_DFEN(0));
265 static int stm32_dfsdm_filter_configure(struct stm32_dfsdm
*dfsdm
,
266 unsigned int fl_id
, unsigned int ch_id
)
268 struct regmap
*regmap
= dfsdm
->regmap
;
269 struct stm32_dfsdm_filter
*fl
= &dfsdm
->fl_list
[fl_id
];
272 /* Average integrator oversampling */
273 ret
= regmap_update_bits(regmap
, DFSDM_FCR(fl_id
), DFSDM_FCR_IOSR_MASK
,
274 DFSDM_FCR_IOSR(fl
->iosr
- 1));
278 /* Filter order and Oversampling */
279 ret
= regmap_update_bits(regmap
, DFSDM_FCR(fl_id
), DFSDM_FCR_FOSR_MASK
,
280 DFSDM_FCR_FOSR(fl
->fosr
- 1));
284 ret
= regmap_update_bits(regmap
, DFSDM_FCR(fl_id
), DFSDM_FCR_FORD_MASK
,
285 DFSDM_FCR_FORD(fl
->ford
));
289 /* No scan mode supported for the moment */
290 ret
= regmap_update_bits(regmap
, DFSDM_CR1(fl_id
), DFSDM_CR1_RCH_MASK
,
291 DFSDM_CR1_RCH(ch_id
));
295 return regmap_update_bits(regmap
, DFSDM_CR1(fl_id
),
296 DFSDM_CR1_RSYNC_MASK
,
297 DFSDM_CR1_RSYNC(fl
->sync_mode
));
300 static int stm32_dfsdm_channel_parse_of(struct stm32_dfsdm
*dfsdm
,
301 struct iio_dev
*indio_dev
,
302 struct iio_chan_spec
*ch
)
304 struct stm32_dfsdm_channel
*df_ch
;
306 int chan_idx
= ch
->scan_index
;
309 ret
= of_property_read_u32_index(indio_dev
->dev
.of_node
,
310 "st,adc-channels", chan_idx
,
313 dev_err(&indio_dev
->dev
,
314 " Error parsing 'st,adc-channels' for idx %d\n",
318 if (ch
->channel
>= dfsdm
->num_chs
) {
319 dev_err(&indio_dev
->dev
,
320 " Error bad channel number %d (max = %d)\n",
321 ch
->channel
, dfsdm
->num_chs
);
325 ret
= of_property_read_string_index(indio_dev
->dev
.of_node
,
326 "st,adc-channel-names", chan_idx
,
327 &ch
->datasheet_name
);
329 dev_err(&indio_dev
->dev
,
330 " Error parsing 'st,adc-channel-names' for idx %d\n",
335 df_ch
= &dfsdm
->ch_list
[ch
->channel
];
336 df_ch
->id
= ch
->channel
;
338 ret
= of_property_read_string_index(indio_dev
->dev
.of_node
,
339 "st,adc-channel-types", chan_idx
,
342 val
= stm32_dfsdm_str2val(of_str
, stm32_dfsdm_chan_type
);
350 ret
= of_property_read_string_index(indio_dev
->dev
.of_node
,
351 "st,adc-channel-clk-src", chan_idx
,
354 val
= stm32_dfsdm_str2val(of_str
, stm32_dfsdm_chan_src
);
362 ret
= of_property_read_u32_index(indio_dev
->dev
.of_node
,
363 "st,adc-alt-channel", chan_idx
,
371 static ssize_t
dfsdm_adc_audio_get_spiclk(struct iio_dev
*indio_dev
,
373 const struct iio_chan_spec
*chan
,
376 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
378 return snprintf(buf
, PAGE_SIZE
, "%d\n", adc
->spi_freq
);
381 static ssize_t
dfsdm_adc_audio_set_spiclk(struct iio_dev
*indio_dev
,
383 const struct iio_chan_spec
*chan
,
384 const char *buf
, size_t len
)
386 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
387 struct stm32_dfsdm_filter
*fl
= &adc
->dfsdm
->fl_list
[adc
->fl_id
];
388 struct stm32_dfsdm_channel
*ch
= &adc
->dfsdm
->ch_list
[chan
->channel
];
389 unsigned int sample_freq
= adc
->sample_freq
;
390 unsigned int spi_freq
;
393 dev_err(&indio_dev
->dev
, "enter %s\n", __func__
);
394 /* If DFSDM is master on SPI, SPI freq can not be updated */
395 if (ch
->src
!= DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL
)
398 ret
= kstrtoint(buf
, 0, &spi_freq
);
406 if (spi_freq
% sample_freq
)
407 dev_warn(&indio_dev
->dev
,
408 "Sampling rate not accurate (%d)\n",
409 spi_freq
/ (spi_freq
/ sample_freq
));
411 ret
= stm32_dfsdm_set_osrs(fl
, 0, (spi_freq
/ sample_freq
));
413 dev_err(&indio_dev
->dev
,
414 "No filter parameters that match!\n");
418 adc
->spi_freq
= spi_freq
;
423 static int stm32_dfsdm_start_conv(struct stm32_dfsdm_adc
*adc
,
424 const struct iio_chan_spec
*chan
,
427 struct regmap
*regmap
= adc
->dfsdm
->regmap
;
429 unsigned int dma_en
= 0, cont_en
= 0;
431 ret
= stm32_dfsdm_start_channel(adc
->dfsdm
, chan
->channel
);
435 ret
= stm32_dfsdm_filter_configure(adc
->dfsdm
, adc
->fl_id
,
441 /* Enable DMA transfer*/
442 dma_en
= DFSDM_CR1_RDMAEN(1);
443 /* Enable conversion triggered by SPI clock*/
444 cont_en
= DFSDM_CR1_RCONT(1);
446 /* Enable DMA transfer*/
447 ret
= regmap_update_bits(regmap
, DFSDM_CR1(adc
->fl_id
),
448 DFSDM_CR1_RDMAEN_MASK
, dma_en
);
452 /* Enable conversion triggered by SPI clock*/
453 ret
= regmap_update_bits(regmap
, DFSDM_CR1(adc
->fl_id
),
454 DFSDM_CR1_RCONT_MASK
, cont_en
);
458 ret
= stm32_dfsdm_start_filter(adc
->dfsdm
, adc
->fl_id
);
465 regmap_update_bits(regmap
, DFSDM_CR1(adc
->fl_id
),
466 DFSDM_CR1_RDMAEN_MASK
, 0);
468 regmap_update_bits(regmap
, DFSDM_CR1(adc
->fl_id
),
469 DFSDM_CR1_RCONT_MASK
, 0);
470 stm32_dfsdm_stop_channel(adc
->dfsdm
, chan
->channel
);
475 static void stm32_dfsdm_stop_conv(struct stm32_dfsdm_adc
*adc
,
476 const struct iio_chan_spec
*chan
)
478 struct regmap
*regmap
= adc
->dfsdm
->regmap
;
480 stm32_dfsdm_stop_filter(adc
->dfsdm
, adc
->fl_id
);
482 /* Clean conversion options */
483 regmap_update_bits(regmap
, DFSDM_CR1(adc
->fl_id
),
484 DFSDM_CR1_RDMAEN_MASK
, 0);
486 regmap_update_bits(regmap
, DFSDM_CR1(adc
->fl_id
),
487 DFSDM_CR1_RCONT_MASK
, 0);
489 stm32_dfsdm_stop_channel(adc
->dfsdm
, chan
->channel
);
492 static int stm32_dfsdm_set_watermark(struct iio_dev
*indio_dev
,
495 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
496 unsigned int watermark
= DFSDM_DMA_BUFFER_SIZE
/ 2;
499 * DMA cyclic transfers are used, buffer is split into two periods.
501 * - always one buffer (period) DMA is working on
502 * - one buffer (period) driver pushed to ASoC side.
504 watermark
= min(watermark
, val
* (unsigned int)(sizeof(u32
)));
505 adc
->buf_sz
= watermark
* 2;
510 static unsigned int stm32_dfsdm_adc_dma_residue(struct stm32_dfsdm_adc
*adc
)
512 struct dma_tx_state state
;
513 enum dma_status status
;
515 status
= dmaengine_tx_status(adc
->dma_chan
,
516 adc
->dma_chan
->cookie
,
518 if (status
== DMA_IN_PROGRESS
) {
519 /* Residue is size in bytes from end of buffer */
520 unsigned int i
= adc
->buf_sz
- state
.residue
;
523 /* Return available bytes */
525 size
= i
- adc
->bufi
;
527 size
= adc
->buf_sz
+ i
- adc
->bufi
;
535 static void stm32_dfsdm_audio_dma_buffer_done(void *data
)
537 struct iio_dev
*indio_dev
= data
;
538 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
539 int available
= stm32_dfsdm_adc_dma_residue(adc
);
543 * FIXME: In Kernel interface does not support cyclic DMA buffer,and
544 * offers only an interface to push data samples per samples.
545 * For this reason IIO buffer interface is not used and interface is
546 * bypassed using a private callback registered by ASoC.
547 * This should be a temporary solution waiting a cyclic DMA engine
551 dev_dbg(&indio_dev
->dev
, "%s: pos = %d, available = %d\n", __func__
,
552 adc
->bufi
, available
);
555 while (available
>= indio_dev
->scan_bytes
) {
556 u32
*buffer
= (u32
*)&adc
->rx_buf
[adc
->bufi
];
558 /* Mask 8 LSB that contains the channel ID */
559 *buffer
= (*buffer
& 0xFFFFFF00) << 8;
560 available
-= indio_dev
->scan_bytes
;
561 adc
->bufi
+= indio_dev
->scan_bytes
;
562 if (adc
->bufi
>= adc
->buf_sz
) {
564 adc
->cb(&adc
->rx_buf
[old_pos
],
565 adc
->buf_sz
- old_pos
, adc
->cb_priv
);
571 adc
->cb(&adc
->rx_buf
[old_pos
], adc
->bufi
- old_pos
,
575 static int stm32_dfsdm_adc_dma_start(struct iio_dev
*indio_dev
)
577 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
578 struct dma_async_tx_descriptor
*desc
;
585 dev_dbg(&indio_dev
->dev
, "%s size=%d watermark=%d\n", __func__
,
586 adc
->buf_sz
, adc
->buf_sz
/ 2);
588 /* Prepare a DMA cyclic transaction */
589 desc
= dmaengine_prep_dma_cyclic(adc
->dma_chan
,
591 adc
->buf_sz
, adc
->buf_sz
/ 2,
597 desc
->callback
= stm32_dfsdm_audio_dma_buffer_done
;
598 desc
->callback_param
= indio_dev
;
600 cookie
= dmaengine_submit(desc
);
601 ret
= dma_submit_error(cookie
);
603 dmaengine_terminate_all(adc
->dma_chan
);
607 /* Issue pending DMA requests */
608 dma_async_issue_pending(adc
->dma_chan
);
613 static int stm32_dfsdm_postenable(struct iio_dev
*indio_dev
)
615 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
616 const struct iio_chan_spec
*chan
= &indio_dev
->channels
[0];
619 /* Reset adc buffer index */
622 ret
= stm32_dfsdm_start_dfsdm(adc
->dfsdm
);
626 ret
= stm32_dfsdm_start_conv(adc
, chan
, true);
628 dev_err(&indio_dev
->dev
, "Can't start conversion\n");
633 ret
= stm32_dfsdm_adc_dma_start(indio_dev
);
635 dev_err(&indio_dev
->dev
, "Can't start DMA\n");
643 stm32_dfsdm_stop_conv(adc
, chan
);
645 stm32_dfsdm_stop_dfsdm(adc
->dfsdm
);
650 static int stm32_dfsdm_predisable(struct iio_dev
*indio_dev
)
652 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
653 const struct iio_chan_spec
*chan
= &indio_dev
->channels
[0];
656 dmaengine_terminate_all(adc
->dma_chan
);
658 stm32_dfsdm_stop_conv(adc
, chan
);
660 stm32_dfsdm_stop_dfsdm(adc
->dfsdm
);
665 static const struct iio_buffer_setup_ops stm32_dfsdm_buffer_setup_ops
= {
666 .postenable
= &stm32_dfsdm_postenable
,
667 .predisable
= &stm32_dfsdm_predisable
,
671 * stm32_dfsdm_get_buff_cb() - register a callback that will be called when
672 * DMA transfer period is achieved.
674 * @iio_dev: Handle to IIO device.
675 * @cb: Pointer to callback function:
676 * - data: pointer to data buffer
677 * - size: size in byte of the data buffer
678 * - private: pointer to consumer private structure.
679 * @private: Pointer to consumer private structure.
681 int stm32_dfsdm_get_buff_cb(struct iio_dev
*iio_dev
,
682 int (*cb
)(const void *data
, size_t size
,
686 struct stm32_dfsdm_adc
*adc
;
690 adc
= iio_priv(iio_dev
);
693 adc
->cb_priv
= private;
697 EXPORT_SYMBOL_GPL(stm32_dfsdm_get_buff_cb
);
700 * stm32_dfsdm_release_buff_cb - unregister buffer callback
702 * @iio_dev: Handle to IIO device.
704 int stm32_dfsdm_release_buff_cb(struct iio_dev
*iio_dev
)
706 struct stm32_dfsdm_adc
*adc
;
710 adc
= iio_priv(iio_dev
);
717 EXPORT_SYMBOL_GPL(stm32_dfsdm_release_buff_cb
);
719 static int stm32_dfsdm_single_conv(struct iio_dev
*indio_dev
,
720 const struct iio_chan_spec
*chan
, int *res
)
722 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
726 reinit_completion(&adc
->completion
);
730 ret
= stm32_dfsdm_start_dfsdm(adc
->dfsdm
);
734 ret
= regmap_update_bits(adc
->dfsdm
->regmap
, DFSDM_CR2(adc
->fl_id
),
735 DFSDM_CR2_REOCIE_MASK
, DFSDM_CR2_REOCIE(1));
739 ret
= stm32_dfsdm_start_conv(adc
, chan
, false);
741 regmap_update_bits(adc
->dfsdm
->regmap
, DFSDM_CR2(adc
->fl_id
),
742 DFSDM_CR2_REOCIE_MASK
, DFSDM_CR2_REOCIE(0));
746 timeout
= wait_for_completion_interruptible_timeout(&adc
->completion
,
749 /* Mask IRQ for regular conversion achievement*/
750 regmap_update_bits(adc
->dfsdm
->regmap
, DFSDM_CR2(adc
->fl_id
),
751 DFSDM_CR2_REOCIE_MASK
, DFSDM_CR2_REOCIE(0));
755 else if (timeout
< 0)
760 stm32_dfsdm_stop_conv(adc
, chan
);
763 stm32_dfsdm_stop_dfsdm(adc
->dfsdm
);
768 static int stm32_dfsdm_write_raw(struct iio_dev
*indio_dev
,
769 struct iio_chan_spec
const *chan
,
770 int val
, int val2
, long mask
)
772 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
773 struct stm32_dfsdm_filter
*fl
= &adc
->dfsdm
->fl_list
[adc
->fl_id
];
774 struct stm32_dfsdm_channel
*ch
= &adc
->dfsdm
->ch_list
[chan
->channel
];
775 unsigned int spi_freq
;
779 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
780 ret
= stm32_dfsdm_set_osrs(fl
, 0, val
);
786 case IIO_CHAN_INFO_SAMP_FREQ
:
791 case DFSDM_CHANNEL_SPI_CLOCK_INTERNAL
:
792 spi_freq
= adc
->dfsdm
->spi_master_freq
;
794 case DFSDM_CHANNEL_SPI_CLOCK_INTERNAL_DIV2_FALLING
:
795 case DFSDM_CHANNEL_SPI_CLOCK_INTERNAL_DIV2_RISING
:
796 spi_freq
= adc
->dfsdm
->spi_master_freq
/ 2;
799 spi_freq
= adc
->spi_freq
;
803 dev_warn(&indio_dev
->dev
,
804 "Sampling rate not accurate (%d)\n",
805 spi_freq
/ (spi_freq
/ val
));
807 ret
= stm32_dfsdm_set_osrs(fl
, 0, (spi_freq
/ val
));
809 dev_err(&indio_dev
->dev
,
810 "Not able to find parameter that match!\n");
813 adc
->sample_freq
= val
;
821 static int stm32_dfsdm_read_raw(struct iio_dev
*indio_dev
,
822 struct iio_chan_spec
const *chan
, int *val
,
823 int *val2
, long mask
)
825 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
829 case IIO_CHAN_INFO_RAW
:
830 ret
= iio_hw_consumer_enable(adc
->hwc
);
832 dev_err(&indio_dev
->dev
,
833 "%s: IIO enable failed (channel %d)\n",
834 __func__
, chan
->channel
);
837 ret
= stm32_dfsdm_single_conv(indio_dev
, chan
, val
);
838 iio_hw_consumer_disable(adc
->hwc
);
840 dev_err(&indio_dev
->dev
,
841 "%s: Conversion failed (channel %d)\n",
842 __func__
, chan
->channel
);
847 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
848 *val
= adc
->oversamp
;
852 case IIO_CHAN_INFO_SAMP_FREQ
:
853 *val
= adc
->sample_freq
;
861 static const struct iio_info stm32_dfsdm_info_audio
= {
862 .hwfifo_set_watermark
= stm32_dfsdm_set_watermark
,
863 .read_raw
= stm32_dfsdm_read_raw
,
864 .write_raw
= stm32_dfsdm_write_raw
,
867 static const struct iio_info stm32_dfsdm_info_adc
= {
868 .read_raw
= stm32_dfsdm_read_raw
,
869 .write_raw
= stm32_dfsdm_write_raw
,
872 static irqreturn_t
stm32_dfsdm_irq(int irq
, void *arg
)
874 struct stm32_dfsdm_adc
*adc
= arg
;
875 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
876 struct regmap
*regmap
= adc
->dfsdm
->regmap
;
877 unsigned int status
, int_en
;
879 regmap_read(regmap
, DFSDM_ISR(adc
->fl_id
), &status
);
880 regmap_read(regmap
, DFSDM_CR2(adc
->fl_id
), &int_en
);
882 if (status
& DFSDM_ISR_REOCF_MASK
) {
883 /* Read the data register clean the IRQ status */
884 regmap_read(regmap
, DFSDM_RDATAR(adc
->fl_id
), adc
->buffer
);
885 complete(&adc
->completion
);
888 if (status
& DFSDM_ISR_ROVRF_MASK
) {
889 if (int_en
& DFSDM_CR2_ROVRIE_MASK
)
890 dev_warn(&indio_dev
->dev
, "Overrun detected\n");
891 regmap_update_bits(regmap
, DFSDM_ICR(adc
->fl_id
),
892 DFSDM_ICR_CLRROVRF_MASK
,
893 DFSDM_ICR_CLRROVRF_MASK
);
900 * Define external info for SPI Frequency and audio sampling rate that can be
901 * configured by ASoC driver through consumer.h API
903 static const struct iio_chan_spec_ext_info dfsdm_adc_audio_ext_info
[] = {
904 /* spi_clk_freq : clock freq on SPI/manchester bus used by channel */
906 .name
= "spi_clk_freq",
907 .shared
= IIO_SHARED_BY_TYPE
,
908 .read
= dfsdm_adc_audio_get_spiclk
,
909 .write
= dfsdm_adc_audio_set_spiclk
,
914 static void stm32_dfsdm_dma_release(struct iio_dev
*indio_dev
)
916 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
919 dma_free_coherent(adc
->dma_chan
->device
->dev
,
920 DFSDM_DMA_BUFFER_SIZE
,
921 adc
->rx_buf
, adc
->dma_buf
);
922 dma_release_channel(adc
->dma_chan
);
926 static int stm32_dfsdm_dma_request(struct iio_dev
*indio_dev
)
928 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
929 struct dma_slave_config config
= {
930 .src_addr
= (dma_addr_t
)adc
->dfsdm
->phys_base
+
931 DFSDM_RDATAR(adc
->fl_id
),
932 .src_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
,
936 adc
->dma_chan
= dma_request_slave_channel(&indio_dev
->dev
, "rx");
940 adc
->rx_buf
= dma_alloc_coherent(adc
->dma_chan
->device
->dev
,
941 DFSDM_DMA_BUFFER_SIZE
,
942 &adc
->dma_buf
, GFP_KERNEL
);
948 ret
= dmaengine_slave_config(adc
->dma_chan
, &config
);
955 dma_free_coherent(adc
->dma_chan
->device
->dev
, DFSDM_DMA_BUFFER_SIZE
,
956 adc
->rx_buf
, adc
->dma_buf
);
958 dma_release_channel(adc
->dma_chan
);
963 static int stm32_dfsdm_adc_chan_init_one(struct iio_dev
*indio_dev
,
964 struct iio_chan_spec
*ch
)
966 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
969 ret
= stm32_dfsdm_channel_parse_of(adc
->dfsdm
, indio_dev
, ch
);
973 ch
->type
= IIO_VOLTAGE
;
977 * IIO_CHAN_INFO_RAW: used to compute regular conversion
978 * IIO_CHAN_INFO_OVERSAMPLING_RATIO: used to set oversampling
980 ch
->info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
);
981 ch
->info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
);
983 if (adc
->dev_data
->type
== DFSDM_AUDIO
) {
984 ch
->scan_type
.sign
= 's';
985 ch
->ext_info
= dfsdm_adc_audio_ext_info
;
987 ch
->scan_type
.sign
= 'u';
989 ch
->scan_type
.realbits
= 24;
990 ch
->scan_type
.storagebits
= 32;
992 return stm32_dfsdm_chan_configure(adc
->dfsdm
,
993 &adc
->dfsdm
->ch_list
[ch
->channel
]);
996 static int stm32_dfsdm_audio_init(struct iio_dev
*indio_dev
)
998 struct iio_chan_spec
*ch
;
999 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
1000 struct stm32_dfsdm_channel
*d_ch
;
1003 indio_dev
->modes
|= INDIO_BUFFER_SOFTWARE
;
1004 indio_dev
->setup_ops
= &stm32_dfsdm_buffer_setup_ops
;
1006 ch
= devm_kzalloc(&indio_dev
->dev
, sizeof(*ch
), GFP_KERNEL
);
1012 ret
= stm32_dfsdm_adc_chan_init_one(indio_dev
, ch
);
1014 dev_err(&indio_dev
->dev
, "Channels init failed\n");
1017 ch
->info_mask_separate
= BIT(IIO_CHAN_INFO_SAMP_FREQ
);
1019 d_ch
= &adc
->dfsdm
->ch_list
[ch
->channel
];
1020 if (d_ch
->src
!= DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL
)
1021 adc
->spi_freq
= adc
->dfsdm
->spi_master_freq
;
1023 indio_dev
->num_channels
= 1;
1024 indio_dev
->channels
= ch
;
1026 return stm32_dfsdm_dma_request(indio_dev
);
1029 static int stm32_dfsdm_adc_init(struct iio_dev
*indio_dev
)
1031 struct iio_chan_spec
*ch
;
1032 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
1036 adc
->oversamp
= DFSDM_DEFAULT_OVERSAMPLING
;
1037 ret
= stm32_dfsdm_set_osrs(&adc
->dfsdm
->fl_list
[adc
->fl_id
], 0,
1042 num_ch
= of_property_count_u32_elems(indio_dev
->dev
.of_node
,
1044 if (num_ch
< 0 || num_ch
> adc
->dfsdm
->num_chs
) {
1045 dev_err(&indio_dev
->dev
, "Bad st,adc-channels\n");
1046 return num_ch
< 0 ? num_ch
: -EINVAL
;
1049 /* Bind to SD modulator IIO device */
1050 adc
->hwc
= devm_iio_hw_consumer_alloc(&indio_dev
->dev
);
1051 if (IS_ERR(adc
->hwc
))
1052 return -EPROBE_DEFER
;
1054 ch
= devm_kcalloc(&indio_dev
->dev
, num_ch
, sizeof(*ch
),
1059 for (chan_idx
= 0; chan_idx
< num_ch
; chan_idx
++) {
1060 ch
[chan_idx
].scan_index
= chan_idx
;
1061 ret
= stm32_dfsdm_adc_chan_init_one(indio_dev
, &ch
[chan_idx
]);
1063 dev_err(&indio_dev
->dev
, "Channels init failed\n");
1068 indio_dev
->num_channels
= num_ch
;
1069 indio_dev
->channels
= ch
;
1071 init_completion(&adc
->completion
);
1076 static const struct stm32_dfsdm_dev_data stm32h7_dfsdm_adc_data
= {
1078 .init
= stm32_dfsdm_adc_init
,
1081 static const struct stm32_dfsdm_dev_data stm32h7_dfsdm_audio_data
= {
1082 .type
= DFSDM_AUDIO
,
1083 .init
= stm32_dfsdm_audio_init
,
1086 static const struct of_device_id stm32_dfsdm_adc_match
[] = {
1088 .compatible
= "st,stm32-dfsdm-adc",
1089 .data
= &stm32h7_dfsdm_adc_data
,
1092 .compatible
= "st,stm32-dfsdm-dmic",
1093 .data
= &stm32h7_dfsdm_audio_data
,
1098 static int stm32_dfsdm_adc_probe(struct platform_device
*pdev
)
1100 struct device
*dev
= &pdev
->dev
;
1101 struct stm32_dfsdm_adc
*adc
;
1102 struct device_node
*np
= dev
->of_node
;
1103 const struct stm32_dfsdm_dev_data
*dev_data
;
1104 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
);
1125 if (ret
!= 0 || adc
->fl_id
>= adc
->dfsdm
->num_fls
) {
1126 dev_err(dev
, "Missing or bad 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 if (dev_data
->type
== DFSDM_AUDIO
) {
1176 ret
= of_platform_populate(np
, NULL
, NULL
, dev
);
1178 dev_err(dev
, "Failed to find an audio DAI\n");
1179 goto err_unregister
;
1186 iio_device_unregister(iio
);
1188 stm32_dfsdm_dma_release(iio
);
1193 static int stm32_dfsdm_adc_remove(struct platform_device
*pdev
)
1195 struct stm32_dfsdm_adc
*adc
= platform_get_drvdata(pdev
);
1196 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
1198 if (adc
->dev_data
->type
== DFSDM_AUDIO
)
1199 of_platform_depopulate(&pdev
->dev
);
1200 iio_device_unregister(indio_dev
);
1201 stm32_dfsdm_dma_release(indio_dev
);
1206 static struct platform_driver stm32_dfsdm_adc_driver
= {
1208 .name
= "stm32-dfsdm-adc",
1209 .of_match_table
= stm32_dfsdm_adc_match
,
1211 .probe
= stm32_dfsdm_adc_probe
,
1212 .remove
= stm32_dfsdm_adc_remove
,
1214 module_platform_driver(stm32_dfsdm_adc_driver
);
1216 MODULE_DESCRIPTION("STM32 sigma delta ADC");
1217 MODULE_AUTHOR("Arnaud Pouliquen <arnaud.pouliquen@st.com>");
1218 MODULE_LICENSE("GPL v2");