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/iio/timer/stm32-lptim-trigger.h>
16 #include <linux/iio/timer/stm32-timer-trigger.h>
17 #include <linux/iio/trigger.h>
18 #include <linux/iio/trigger_consumer.h>
19 #include <linux/iio/triggered_buffer.h>
20 #include <linux/interrupt.h>
21 #include <linux/module.h>
22 #include <linux/of_device.h>
23 #include <linux/platform_device.h>
24 #include <linux/regmap.h>
25 #include <linux/slab.h>
27 #include "stm32-dfsdm.h"
29 #define DFSDM_DMA_BUFFER_SIZE (4 * PAGE_SIZE)
31 /* Conversion timeout */
32 #define DFSDM_TIMEOUT_US 100000
33 #define DFSDM_TIMEOUT (msecs_to_jiffies(DFSDM_TIMEOUT_US / 1000))
35 /* Oversampling attribute default */
36 #define DFSDM_DEFAULT_OVERSAMPLING 100
38 /* Oversampling max values */
39 #define DFSDM_MAX_INT_OVERSAMPLING 256
40 #define DFSDM_MAX_FL_OVERSAMPLING 1024
42 /* Limit filter output resolution to 31 bits. (i.e. sample range is +/-2^30) */
43 #define DFSDM_DATA_MAX BIT(30)
45 * Data are output as two's complement data in a 24 bit field.
46 * Data from filters are in the range +/-2^(n-1)
47 * 2^(n-1) maximum positive value cannot be coded in 2's complement n bits
48 * An extra bit is required to avoid wrap-around of the binary code for 2^(n-1)
49 * So, the resolution of samples from filter is actually limited to 23 bits
51 #define DFSDM_DATA_RES 24
53 /* Filter configuration */
54 #define DFSDM_CR1_CFG_MASK (DFSDM_CR1_RCH_MASK | DFSDM_CR1_RCONT_MASK | \
55 DFSDM_CR1_RSYNC_MASK | DFSDM_CR1_JSYNC_MASK | \
58 enum sd_converter_type
{
63 struct stm32_dfsdm_dev_data
{
65 int (*init
)(struct device
*dev
, struct iio_dev
*indio_dev
);
66 unsigned int num_channels
;
67 const struct regmap_config
*regmap_cfg
;
70 struct stm32_dfsdm_adc
{
71 struct stm32_dfsdm
*dfsdm
;
72 const struct stm32_dfsdm_dev_data
*dev_data
;
78 unsigned int oversamp
;
79 struct iio_hw_consumer
*hwc
;
80 struct completion completion
;
84 unsigned int spi_freq
; /* SPI bus clock frequency */
85 unsigned int sample_freq
; /* Sample frequency after filter decimation */
86 int (*cb
)(const void *data
, size_t size
, void *cb_priv
);
91 unsigned int bufi
; /* Buffer current position */
92 unsigned int buf_sz
; /* Buffer size */
93 struct dma_chan
*dma_chan
;
97 struct stm32_dfsdm_str2field
{
102 /* DFSDM channel serial interface type */
103 static const struct stm32_dfsdm_str2field stm32_dfsdm_chan_type
[] = {
104 { "SPI_R", 0 }, /* SPI with data on rising edge */
105 { "SPI_F", 1 }, /* SPI with data on falling edge */
106 { "MANCH_R", 2 }, /* Manchester codec, rising edge = logic 0 */
107 { "MANCH_F", 3 }, /* Manchester codec, falling edge = logic 1 */
111 /* DFSDM channel clock source */
112 static const struct stm32_dfsdm_str2field stm32_dfsdm_chan_src
[] = {
113 /* External SPI clock (CLKIN x) */
114 { "CLKIN", DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL
},
115 /* Internal SPI clock (CLKOUT) */
116 { "CLKOUT", DFSDM_CHANNEL_SPI_CLOCK_INTERNAL
},
117 /* Internal SPI clock divided by 2 (falling edge) */
118 { "CLKOUT_F", DFSDM_CHANNEL_SPI_CLOCK_INTERNAL_DIV2_FALLING
},
119 /* Internal SPI clock divided by 2 (falling edge) */
120 { "CLKOUT_R", DFSDM_CHANNEL_SPI_CLOCK_INTERNAL_DIV2_RISING
},
124 static int stm32_dfsdm_str2val(const char *str
,
125 const struct stm32_dfsdm_str2field
*list
)
127 const struct stm32_dfsdm_str2field
*p
= list
;
129 for (p
= list
; p
&& p
->name
; p
++)
130 if (!strcmp(p
->name
, str
))
137 * struct stm32_dfsdm_trig_info - DFSDM trigger info
138 * @name: name of the trigger, corresponding to its source
139 * @jextsel: trigger signal selection
141 struct stm32_dfsdm_trig_info
{
143 unsigned int jextsel
;
146 /* hardware injected trigger enable, edge selection */
147 enum stm32_dfsdm_jexten
{
148 STM32_DFSDM_JEXTEN_DISABLED
,
149 STM32_DFSDM_JEXTEN_RISING_EDGE
,
150 STM32_DFSDM_JEXTEN_FALLING_EDGE
,
151 STM32_DFSDM_EXTEN_BOTH_EDGES
,
154 static const struct stm32_dfsdm_trig_info stm32_dfsdm_trigs
[] = {
170 static int stm32_dfsdm_get_jextsel(struct iio_dev
*indio_dev
,
171 struct iio_trigger
*trig
)
175 /* lookup triggers registered by stm32 timer trigger driver */
176 for (i
= 0; stm32_dfsdm_trigs
[i
].name
; i
++) {
178 * Checking both stm32 timer trigger type and trig name
179 * should be safe against arbitrary trigger names.
181 if ((is_stm32_timer_trigger(trig
) ||
182 is_stm32_lptim_trigger(trig
)) &&
183 !strcmp(stm32_dfsdm_trigs
[i
].name
, trig
->name
)) {
184 return stm32_dfsdm_trigs
[i
].jextsel
;
191 static int stm32_dfsdm_compute_osrs(struct stm32_dfsdm_filter
*fl
,
192 unsigned int fast
, unsigned int oversamp
)
194 unsigned int i
, d
, fosr
, iosr
;
197 unsigned int m
= 1; /* multiplication factor */
198 unsigned int p
= fl
->ford
; /* filter order (ford) */
199 struct stm32_dfsdm_filter_osr
*flo
= &fl
->flo
[fast
];
201 pr_debug("%s: Requested oversampling: %d\n", __func__
, oversamp
);
203 * This function tries to compute filter oversampling and integrator
204 * oversampling, base on oversampling ratio requested by user.
206 * Decimation d depends on the filter order and the oversampling ratios.
208 * fosr: filter over sampling ratio
209 * iosr: integrator over sampling ratio
211 if (fl
->ford
== DFSDM_FASTSINC_ORDER
) {
217 * Look for filter and integrator oversampling ratios which allows
218 * to maximize data output resolution.
220 for (fosr
= 1; fosr
<= DFSDM_MAX_FL_OVERSAMPLING
; fosr
++) {
221 for (iosr
= 1; iosr
<= DFSDM_MAX_INT_OVERSAMPLING
; iosr
++) {
224 else if (fl
->ford
== DFSDM_FASTSINC_ORDER
)
225 d
= fosr
* (iosr
+ 3) + 2;
227 d
= fosr
* (iosr
- 1 + p
) + p
;
231 else if (d
!= oversamp
)
234 * Check resolution (limited to signed 32 bits)
237 * res = m * fosr^p x iosr (with m=1, p=ford)
239 * res = m * fosr^p x iosr (with m=2, p=2)
242 for (i
= p
- 1; i
> 0; i
--) {
243 res
= res
* (u64
)fosr
;
244 if (res
> DFSDM_DATA_MAX
)
247 if (res
> DFSDM_DATA_MAX
)
250 res
= res
* (u64
)m
* (u64
)iosr
;
251 if (res
> DFSDM_DATA_MAX
)
254 if (res
>= flo
->res
) {
259 bits
= fls(flo
->res
);
260 /* 8 LBSs in data register contain chan info */
263 /* if resolution is not a power of two */
264 if (flo
->res
> BIT(bits
- 1))
269 shift
= DFSDM_DATA_RES
- bits
;
271 * Compute right/left shift
272 * Right shift is performed by hardware
273 * when transferring samples to data register.
274 * Left shift is done by software on buffer
277 /* Resolution is lower than 24 bits */
282 * If resolution is 24 bits or more,
283 * max positive value may be ambiguous
284 * (equal to max negative value as sign
286 * Reduce resolution to 23 bits (rshift)
287 * to keep the sign on bit 23 and treat
288 * saturation before rescaling on 24
291 flo
->rshift
= 1 - shift
;
297 pr_debug("%s: fast %d, fosr %d, iosr %d, res 0x%llx/%d bits, rshift %d, lshift %d\n",
298 __func__
, fast
, flo
->fosr
, flo
->iosr
,
299 flo
->res
, bits
, flo
->rshift
,
311 static int stm32_dfsdm_compute_all_osrs(struct iio_dev
*indio_dev
,
312 unsigned int oversamp
)
314 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
315 struct stm32_dfsdm_filter
*fl
= &adc
->dfsdm
->fl_list
[adc
->fl_id
];
318 memset(&fl
->flo
[0], 0, sizeof(fl
->flo
[0]));
319 memset(&fl
->flo
[1], 0, sizeof(fl
->flo
[1]));
321 ret0
= stm32_dfsdm_compute_osrs(fl
, 0, oversamp
);
322 ret1
= stm32_dfsdm_compute_osrs(fl
, 1, oversamp
);
323 if (ret0
< 0 && ret1
< 0) {
324 dev_err(&indio_dev
->dev
,
325 "Filter parameters not found: errors %d/%d\n",
333 static int stm32_dfsdm_start_channel(struct stm32_dfsdm_adc
*adc
)
335 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
336 struct regmap
*regmap
= adc
->dfsdm
->regmap
;
337 const struct iio_chan_spec
*chan
;
341 for_each_set_bit(bit
, &adc
->smask
, sizeof(adc
->smask
) * BITS_PER_BYTE
) {
342 chan
= indio_dev
->channels
+ bit
;
343 ret
= regmap_update_bits(regmap
, DFSDM_CHCFGR1(chan
->channel
),
344 DFSDM_CHCFGR1_CHEN_MASK
,
345 DFSDM_CHCFGR1_CHEN(1));
353 static void stm32_dfsdm_stop_channel(struct stm32_dfsdm_adc
*adc
)
355 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
356 struct regmap
*regmap
= adc
->dfsdm
->regmap
;
357 const struct iio_chan_spec
*chan
;
360 for_each_set_bit(bit
, &adc
->smask
, sizeof(adc
->smask
) * BITS_PER_BYTE
) {
361 chan
= indio_dev
->channels
+ bit
;
362 regmap_update_bits(regmap
, DFSDM_CHCFGR1(chan
->channel
),
363 DFSDM_CHCFGR1_CHEN_MASK
,
364 DFSDM_CHCFGR1_CHEN(0));
368 static int stm32_dfsdm_chan_configure(struct stm32_dfsdm
*dfsdm
,
369 struct stm32_dfsdm_channel
*ch
)
371 unsigned int id
= ch
->id
;
372 struct regmap
*regmap
= dfsdm
->regmap
;
375 ret
= regmap_update_bits(regmap
, DFSDM_CHCFGR1(id
),
376 DFSDM_CHCFGR1_SITP_MASK
,
377 DFSDM_CHCFGR1_SITP(ch
->type
));
380 ret
= regmap_update_bits(regmap
, DFSDM_CHCFGR1(id
),
381 DFSDM_CHCFGR1_SPICKSEL_MASK
,
382 DFSDM_CHCFGR1_SPICKSEL(ch
->src
));
385 return regmap_update_bits(regmap
, DFSDM_CHCFGR1(id
),
386 DFSDM_CHCFGR1_CHINSEL_MASK
,
387 DFSDM_CHCFGR1_CHINSEL(ch
->alt_si
));
390 static int stm32_dfsdm_start_filter(struct stm32_dfsdm_adc
*adc
,
392 struct iio_trigger
*trig
)
394 struct stm32_dfsdm
*dfsdm
= adc
->dfsdm
;
398 ret
= regmap_update_bits(dfsdm
->regmap
, DFSDM_CR1(fl_id
),
399 DFSDM_CR1_DFEN_MASK
, DFSDM_CR1_DFEN(1));
403 /* Nothing more to do for injected (scan mode/triggered) conversions */
404 if (adc
->nconv
> 1 || trig
)
407 /* Software start (single or continuous) regular conversion */
408 return regmap_update_bits(dfsdm
->regmap
, DFSDM_CR1(fl_id
),
409 DFSDM_CR1_RSWSTART_MASK
,
410 DFSDM_CR1_RSWSTART(1));
413 static void stm32_dfsdm_stop_filter(struct stm32_dfsdm
*dfsdm
,
416 /* Disable conversion */
417 regmap_update_bits(dfsdm
->regmap
, DFSDM_CR1(fl_id
),
418 DFSDM_CR1_DFEN_MASK
, DFSDM_CR1_DFEN(0));
421 static int stm32_dfsdm_filter_set_trig(struct stm32_dfsdm_adc
*adc
,
423 struct iio_trigger
*trig
)
425 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
426 struct regmap
*regmap
= adc
->dfsdm
->regmap
;
427 u32 jextsel
= 0, jexten
= STM32_DFSDM_JEXTEN_DISABLED
;
431 ret
= stm32_dfsdm_get_jextsel(indio_dev
, trig
);
435 /* set trigger source and polarity (default to rising edge) */
437 jexten
= STM32_DFSDM_JEXTEN_RISING_EDGE
;
440 ret
= regmap_update_bits(regmap
, DFSDM_CR1(fl_id
),
441 DFSDM_CR1_JEXTSEL_MASK
| DFSDM_CR1_JEXTEN_MASK
,
442 DFSDM_CR1_JEXTSEL(jextsel
) |
443 DFSDM_CR1_JEXTEN(jexten
));
450 static int stm32_dfsdm_channels_configure(struct stm32_dfsdm_adc
*adc
,
452 struct iio_trigger
*trig
)
454 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
455 struct regmap
*regmap
= adc
->dfsdm
->regmap
;
456 struct stm32_dfsdm_filter
*fl
= &adc
->dfsdm
->fl_list
[fl_id
];
457 struct stm32_dfsdm_filter_osr
*flo
= &fl
->flo
[0];
458 const struct iio_chan_spec
*chan
;
465 * In continuous mode, use fast mode configuration,
466 * if it provides a better resolution.
468 if (adc
->nconv
== 1 && !trig
&&
469 (indio_dev
->currentmode
& INDIO_BUFFER_SOFTWARE
)) {
470 if (fl
->flo
[1].res
>= fl
->flo
[0].res
) {
479 for_each_set_bit(bit
, &adc
->smask
,
480 sizeof(adc
->smask
) * BITS_PER_BYTE
) {
481 chan
= indio_dev
->channels
+ bit
;
483 ret
= regmap_update_bits(regmap
,
484 DFSDM_CHCFGR2(chan
->channel
),
485 DFSDM_CHCFGR2_DTRBS_MASK
,
486 DFSDM_CHCFGR2_DTRBS(flo
->rshift
));
494 static int stm32_dfsdm_filter_configure(struct stm32_dfsdm_adc
*adc
,
496 struct iio_trigger
*trig
)
498 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
499 struct regmap
*regmap
= adc
->dfsdm
->regmap
;
500 struct stm32_dfsdm_filter
*fl
= &adc
->dfsdm
->fl_list
[fl_id
];
501 struct stm32_dfsdm_filter_osr
*flo
= &fl
->flo
[fl
->fast
];
503 const struct iio_chan_spec
*chan
;
504 unsigned int bit
, jchg
= 0;
507 /* Average integrator oversampling */
508 ret
= regmap_update_bits(regmap
, DFSDM_FCR(fl_id
), DFSDM_FCR_IOSR_MASK
,
509 DFSDM_FCR_IOSR(flo
->iosr
- 1));
513 /* Filter order and Oversampling */
514 ret
= regmap_update_bits(regmap
, DFSDM_FCR(fl_id
), DFSDM_FCR_FOSR_MASK
,
515 DFSDM_FCR_FOSR(flo
->fosr
- 1));
519 ret
= regmap_update_bits(regmap
, DFSDM_FCR(fl_id
), DFSDM_FCR_FORD_MASK
,
520 DFSDM_FCR_FORD(fl
->ford
));
524 ret
= stm32_dfsdm_filter_set_trig(adc
, fl_id
, trig
);
528 ret
= regmap_update_bits(regmap
, DFSDM_CR1(fl_id
),
530 DFSDM_CR1_FAST(fl
->fast
));
535 * DFSDM modes configuration W.R.T audio/iio type modes
536 * ----------------------------------------------------------------
537 * Modes | regular | regular | injected | injected |
538 * | | continuous | | + scan |
539 * --------------|---------|--------------|----------|------------|
540 * single conv | x | | | |
542 * --------------|---------|--------------|----------|------------|
543 * 1 Audio chan | | sample freq | | |
544 * | | or sync_mode | | |
545 * --------------|---------|--------------|----------|------------|
546 * 1 IIO chan | | sample freq | trigger | |
547 * | | or sync_mode | | |
548 * --------------|---------|--------------|----------|------------|
549 * 2+ IIO chans | | | | trigger or |
550 * | | | | sync_mode |
551 * ----------------------------------------------------------------
553 if (adc
->nconv
== 1 && !trig
) {
554 bit
= __ffs(adc
->smask
);
555 chan
= indio_dev
->channels
+ bit
;
557 /* Use regular conversion for single channel without trigger */
558 cr1
= DFSDM_CR1_RCH(chan
->channel
);
560 /* Continuous conversions triggered by SPI clk in buffer mode */
561 if (indio_dev
->currentmode
& INDIO_BUFFER_SOFTWARE
)
562 cr1
|= DFSDM_CR1_RCONT(1);
564 cr1
|= DFSDM_CR1_RSYNC(fl
->sync_mode
);
566 /* Use injected conversion for multiple channels */
567 for_each_set_bit(bit
, &adc
->smask
,
568 sizeof(adc
->smask
) * BITS_PER_BYTE
) {
569 chan
= indio_dev
->channels
+ bit
;
570 jchg
|= BIT(chan
->channel
);
572 ret
= regmap_write(regmap
, DFSDM_JCHGR(fl_id
), jchg
);
576 /* Use scan mode for multiple channels */
577 cr1
= DFSDM_CR1_JSCAN((adc
->nconv
> 1) ? 1 : 0);
580 * Continuous conversions not supported in injected mode,
582 * - conversions in sync with filter 0
583 * - triggered conversions
585 if (!fl
->sync_mode
&& !trig
)
587 cr1
|= DFSDM_CR1_JSYNC(fl
->sync_mode
);
590 return regmap_update_bits(regmap
, DFSDM_CR1(fl_id
), DFSDM_CR1_CFG_MASK
,
594 static int stm32_dfsdm_channel_parse_of(struct stm32_dfsdm
*dfsdm
,
595 struct iio_dev
*indio_dev
,
596 struct iio_chan_spec
*ch
)
598 struct stm32_dfsdm_channel
*df_ch
;
600 int chan_idx
= ch
->scan_index
;
603 ret
= of_property_read_u32_index(indio_dev
->dev
.of_node
,
604 "st,adc-channels", chan_idx
,
607 dev_err(&indio_dev
->dev
,
608 " Error parsing 'st,adc-channels' for idx %d\n",
612 if (ch
->channel
>= dfsdm
->num_chs
) {
613 dev_err(&indio_dev
->dev
,
614 " Error bad channel number %d (max = %d)\n",
615 ch
->channel
, dfsdm
->num_chs
);
619 ret
= of_property_read_string_index(indio_dev
->dev
.of_node
,
620 "st,adc-channel-names", chan_idx
,
621 &ch
->datasheet_name
);
623 dev_err(&indio_dev
->dev
,
624 " Error parsing 'st,adc-channel-names' for idx %d\n",
629 df_ch
= &dfsdm
->ch_list
[ch
->channel
];
630 df_ch
->id
= ch
->channel
;
632 ret
= of_property_read_string_index(indio_dev
->dev
.of_node
,
633 "st,adc-channel-types", chan_idx
,
636 val
= stm32_dfsdm_str2val(of_str
, stm32_dfsdm_chan_type
);
644 ret
= of_property_read_string_index(indio_dev
->dev
.of_node
,
645 "st,adc-channel-clk-src", chan_idx
,
648 val
= stm32_dfsdm_str2val(of_str
, stm32_dfsdm_chan_src
);
656 ret
= of_property_read_u32_index(indio_dev
->dev
.of_node
,
657 "st,adc-alt-channel", chan_idx
,
665 static ssize_t
dfsdm_adc_audio_get_spiclk(struct iio_dev
*indio_dev
,
667 const struct iio_chan_spec
*chan
,
670 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
672 return snprintf(buf
, PAGE_SIZE
, "%d\n", adc
->spi_freq
);
675 static int dfsdm_adc_set_samp_freq(struct iio_dev
*indio_dev
,
676 unsigned int sample_freq
,
677 unsigned int spi_freq
)
679 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
680 unsigned int oversamp
;
683 oversamp
= DIV_ROUND_CLOSEST(spi_freq
, sample_freq
);
684 if (spi_freq
% sample_freq
)
685 dev_dbg(&indio_dev
->dev
,
686 "Rate not accurate. requested (%u), actual (%u)\n",
687 sample_freq
, spi_freq
/ oversamp
);
689 ret
= stm32_dfsdm_compute_all_osrs(indio_dev
, oversamp
);
693 adc
->sample_freq
= spi_freq
/ oversamp
;
694 adc
->oversamp
= oversamp
;
699 static ssize_t
dfsdm_adc_audio_set_spiclk(struct iio_dev
*indio_dev
,
701 const struct iio_chan_spec
*chan
,
702 const char *buf
, size_t len
)
704 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
705 struct stm32_dfsdm_channel
*ch
= &adc
->dfsdm
->ch_list
[chan
->channel
];
706 unsigned int sample_freq
= adc
->sample_freq
;
707 unsigned int spi_freq
;
710 dev_err(&indio_dev
->dev
, "enter %s\n", __func__
);
711 /* If DFSDM is master on SPI, SPI freq can not be updated */
712 if (ch
->src
!= DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL
)
715 ret
= kstrtoint(buf
, 0, &spi_freq
);
723 ret
= dfsdm_adc_set_samp_freq(indio_dev
, sample_freq
, spi_freq
);
727 adc
->spi_freq
= spi_freq
;
732 static int stm32_dfsdm_start_conv(struct stm32_dfsdm_adc
*adc
,
733 struct iio_trigger
*trig
)
735 struct regmap
*regmap
= adc
->dfsdm
->regmap
;
738 ret
= stm32_dfsdm_channels_configure(adc
, adc
->fl_id
, trig
);
742 ret
= stm32_dfsdm_start_channel(adc
);
746 ret
= stm32_dfsdm_filter_configure(adc
, adc
->fl_id
, trig
);
750 ret
= stm32_dfsdm_start_filter(adc
, adc
->fl_id
, trig
);
752 goto filter_unconfigure
;
757 regmap_update_bits(regmap
, DFSDM_CR1(adc
->fl_id
),
758 DFSDM_CR1_CFG_MASK
, 0);
760 stm32_dfsdm_stop_channel(adc
);
765 static void stm32_dfsdm_stop_conv(struct stm32_dfsdm_adc
*adc
)
767 struct regmap
*regmap
= adc
->dfsdm
->regmap
;
769 stm32_dfsdm_stop_filter(adc
->dfsdm
, adc
->fl_id
);
771 regmap_update_bits(regmap
, DFSDM_CR1(adc
->fl_id
),
772 DFSDM_CR1_CFG_MASK
, 0);
774 stm32_dfsdm_stop_channel(adc
);
777 static int stm32_dfsdm_set_watermark(struct iio_dev
*indio_dev
,
780 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
781 unsigned int watermark
= DFSDM_DMA_BUFFER_SIZE
/ 2;
782 unsigned int rx_buf_sz
= DFSDM_DMA_BUFFER_SIZE
;
785 * DMA cyclic transfers are used, buffer is split into two periods.
787 * - always one buffer (period) DMA is working on
788 * - one buffer (period) driver pushed to ASoC side.
790 watermark
= min(watermark
, val
* (unsigned int)(sizeof(u32
)));
791 adc
->buf_sz
= min(rx_buf_sz
, watermark
* 2 * adc
->nconv
);
796 static unsigned int stm32_dfsdm_adc_dma_residue(struct stm32_dfsdm_adc
*adc
)
798 struct dma_tx_state state
;
799 enum dma_status status
;
801 status
= dmaengine_tx_status(adc
->dma_chan
,
802 adc
->dma_chan
->cookie
,
804 if (status
== DMA_IN_PROGRESS
) {
805 /* Residue is size in bytes from end of buffer */
806 unsigned int i
= adc
->buf_sz
- state
.residue
;
809 /* Return available bytes */
811 size
= i
- adc
->bufi
;
813 size
= adc
->buf_sz
+ i
- adc
->bufi
;
821 static inline void stm32_dfsdm_process_data(struct stm32_dfsdm_adc
*adc
,
824 struct stm32_dfsdm_filter
*fl
= &adc
->dfsdm
->fl_list
[adc
->fl_id
];
825 struct stm32_dfsdm_filter_osr
*flo
= &fl
->flo
[fl
->fast
];
826 unsigned int i
= adc
->nconv
;
830 /* Mask 8 LSB that contains the channel ID */
832 /* Convert 2^(n-1) sample to 2^(n-1)-1 to avoid wrap-around */
836 * Samples from filter are retrieved with 23 bits resolution
837 * or less. Shift left to align MSB on 24 bits.
839 *ptr
<<= flo
->lshift
;
845 static void stm32_dfsdm_dma_buffer_done(void *data
)
847 struct iio_dev
*indio_dev
= data
;
848 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
849 int available
= stm32_dfsdm_adc_dma_residue(adc
);
853 * FIXME: In Kernel interface does not support cyclic DMA buffer,and
854 * offers only an interface to push data samples per samples.
855 * For this reason IIO buffer interface is not used and interface is
856 * bypassed using a private callback registered by ASoC.
857 * This should be a temporary solution waiting a cyclic DMA engine
861 dev_dbg(&indio_dev
->dev
, "%s: pos = %d, available = %d\n", __func__
,
862 adc
->bufi
, available
);
865 while (available
>= indio_dev
->scan_bytes
) {
866 s32
*buffer
= (s32
*)&adc
->rx_buf
[adc
->bufi
];
868 stm32_dfsdm_process_data(adc
, buffer
);
870 available
-= indio_dev
->scan_bytes
;
871 adc
->bufi
+= indio_dev
->scan_bytes
;
872 if (adc
->bufi
>= adc
->buf_sz
) {
874 adc
->cb(&adc
->rx_buf
[old_pos
],
875 adc
->buf_sz
- old_pos
, adc
->cb_priv
);
880 * In DMA mode the trigger services of IIO are not used
881 * (e.g. no call to iio_trigger_poll).
882 * Calling irq handler associated to the hardware trigger is not
883 * relevant as the conversions have already been done. Data
884 * transfers are performed directly in DMA callback instead.
885 * This implementation avoids to call trigger irq handler that
886 * may sleep, in an atomic context (DMA irq handler context).
888 if (adc
->dev_data
->type
== DFSDM_IIO
)
889 iio_push_to_buffers(indio_dev
, buffer
);
892 adc
->cb(&adc
->rx_buf
[old_pos
], adc
->bufi
- old_pos
,
896 static int stm32_dfsdm_adc_dma_start(struct iio_dev
*indio_dev
)
898 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
900 * The DFSDM supports half-word transfers. However, for 16 bits record,
901 * 4 bytes buswidth is kept, to avoid losing samples LSBs when left
904 struct dma_slave_config config
= {
905 .src_addr
= (dma_addr_t
)adc
->dfsdm
->phys_base
,
906 .src_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
,
908 struct dma_async_tx_descriptor
*desc
;
915 dev_dbg(&indio_dev
->dev
, "%s size=%d watermark=%d\n", __func__
,
916 adc
->buf_sz
, adc
->buf_sz
/ 2);
918 if (adc
->nconv
== 1 && !indio_dev
->trig
)
919 config
.src_addr
+= DFSDM_RDATAR(adc
->fl_id
);
921 config
.src_addr
+= DFSDM_JDATAR(adc
->fl_id
);
922 ret
= dmaengine_slave_config(adc
->dma_chan
, &config
);
926 /* Prepare a DMA cyclic transaction */
927 desc
= dmaengine_prep_dma_cyclic(adc
->dma_chan
,
929 adc
->buf_sz
, adc
->buf_sz
/ 2,
935 desc
->callback
= stm32_dfsdm_dma_buffer_done
;
936 desc
->callback_param
= indio_dev
;
938 cookie
= dmaengine_submit(desc
);
939 ret
= dma_submit_error(cookie
);
943 /* Issue pending DMA requests */
944 dma_async_issue_pending(adc
->dma_chan
);
946 if (adc
->nconv
== 1 && !indio_dev
->trig
) {
947 /* Enable regular DMA transfer*/
948 ret
= regmap_update_bits(adc
->dfsdm
->regmap
,
949 DFSDM_CR1(adc
->fl_id
),
950 DFSDM_CR1_RDMAEN_MASK
,
951 DFSDM_CR1_RDMAEN_MASK
);
953 /* Enable injected DMA transfer*/
954 ret
= regmap_update_bits(adc
->dfsdm
->regmap
,
955 DFSDM_CR1(adc
->fl_id
),
956 DFSDM_CR1_JDMAEN_MASK
,
957 DFSDM_CR1_JDMAEN_MASK
);
966 dmaengine_terminate_all(adc
->dma_chan
);
971 static void stm32_dfsdm_adc_dma_stop(struct iio_dev
*indio_dev
)
973 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
978 regmap_update_bits(adc
->dfsdm
->regmap
, DFSDM_CR1(adc
->fl_id
),
979 DFSDM_CR1_RDMAEN_MASK
| DFSDM_CR1_JDMAEN_MASK
, 0);
980 dmaengine_terminate_all(adc
->dma_chan
);
983 static int stm32_dfsdm_update_scan_mode(struct iio_dev
*indio_dev
,
984 const unsigned long *scan_mask
)
986 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
988 adc
->nconv
= bitmap_weight(scan_mask
, indio_dev
->masklength
);
989 adc
->smask
= *scan_mask
;
991 dev_dbg(&indio_dev
->dev
, "nconv=%d mask=%lx\n", adc
->nconv
, *scan_mask
);
996 static int __stm32_dfsdm_postenable(struct iio_dev
*indio_dev
)
998 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
1001 /* Reset adc buffer index */
1005 ret
= iio_hw_consumer_enable(adc
->hwc
);
1010 ret
= stm32_dfsdm_start_dfsdm(adc
->dfsdm
);
1014 ret
= stm32_dfsdm_adc_dma_start(indio_dev
);
1016 dev_err(&indio_dev
->dev
, "Can't start DMA\n");
1020 ret
= stm32_dfsdm_start_conv(adc
, indio_dev
->trig
);
1022 dev_err(&indio_dev
->dev
, "Can't start conversion\n");
1029 stm32_dfsdm_adc_dma_stop(indio_dev
);
1031 stm32_dfsdm_stop_dfsdm(adc
->dfsdm
);
1034 iio_hw_consumer_disable(adc
->hwc
);
1039 static int stm32_dfsdm_postenable(struct iio_dev
*indio_dev
)
1043 if (indio_dev
->currentmode
== INDIO_BUFFER_TRIGGERED
) {
1044 ret
= iio_triggered_buffer_postenable(indio_dev
);
1049 ret
= __stm32_dfsdm_postenable(indio_dev
);
1051 goto err_predisable
;
1056 if (indio_dev
->currentmode
== INDIO_BUFFER_TRIGGERED
)
1057 iio_triggered_buffer_predisable(indio_dev
);
1062 static void __stm32_dfsdm_predisable(struct iio_dev
*indio_dev
)
1064 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
1066 stm32_dfsdm_stop_conv(adc
);
1068 stm32_dfsdm_adc_dma_stop(indio_dev
);
1070 stm32_dfsdm_stop_dfsdm(adc
->dfsdm
);
1073 iio_hw_consumer_disable(adc
->hwc
);
1076 static int stm32_dfsdm_predisable(struct iio_dev
*indio_dev
)
1078 __stm32_dfsdm_predisable(indio_dev
);
1080 if (indio_dev
->currentmode
== INDIO_BUFFER_TRIGGERED
)
1081 iio_triggered_buffer_predisable(indio_dev
);
1086 static const struct iio_buffer_setup_ops stm32_dfsdm_buffer_setup_ops
= {
1087 .postenable
= &stm32_dfsdm_postenable
,
1088 .predisable
= &stm32_dfsdm_predisable
,
1092 * stm32_dfsdm_get_buff_cb() - register a callback that will be called when
1093 * DMA transfer period is achieved.
1095 * @iio_dev: Handle to IIO device.
1096 * @cb: Pointer to callback function:
1097 * - data: pointer to data buffer
1098 * - size: size in byte of the data buffer
1099 * - private: pointer to consumer private structure.
1100 * @private: Pointer to consumer private structure.
1102 int stm32_dfsdm_get_buff_cb(struct iio_dev
*iio_dev
,
1103 int (*cb
)(const void *data
, size_t size
,
1107 struct stm32_dfsdm_adc
*adc
;
1111 adc
= iio_priv(iio_dev
);
1114 adc
->cb_priv
= private;
1118 EXPORT_SYMBOL_GPL(stm32_dfsdm_get_buff_cb
);
1121 * stm32_dfsdm_release_buff_cb - unregister buffer callback
1123 * @iio_dev: Handle to IIO device.
1125 int stm32_dfsdm_release_buff_cb(struct iio_dev
*iio_dev
)
1127 struct stm32_dfsdm_adc
*adc
;
1131 adc
= iio_priv(iio_dev
);
1134 adc
->cb_priv
= NULL
;
1138 EXPORT_SYMBOL_GPL(stm32_dfsdm_release_buff_cb
);
1140 static int stm32_dfsdm_single_conv(struct iio_dev
*indio_dev
,
1141 const struct iio_chan_spec
*chan
, int *res
)
1143 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
1147 reinit_completion(&adc
->completion
);
1151 ret
= stm32_dfsdm_start_dfsdm(adc
->dfsdm
);
1155 ret
= regmap_update_bits(adc
->dfsdm
->regmap
, DFSDM_CR2(adc
->fl_id
),
1156 DFSDM_CR2_REOCIE_MASK
, DFSDM_CR2_REOCIE(1));
1161 adc
->smask
= BIT(chan
->scan_index
);
1162 ret
= stm32_dfsdm_start_conv(adc
, NULL
);
1164 regmap_update_bits(adc
->dfsdm
->regmap
, DFSDM_CR2(adc
->fl_id
),
1165 DFSDM_CR2_REOCIE_MASK
, DFSDM_CR2_REOCIE(0));
1169 timeout
= wait_for_completion_interruptible_timeout(&adc
->completion
,
1172 /* Mask IRQ for regular conversion achievement*/
1173 regmap_update_bits(adc
->dfsdm
->regmap
, DFSDM_CR2(adc
->fl_id
),
1174 DFSDM_CR2_REOCIE_MASK
, DFSDM_CR2_REOCIE(0));
1178 else if (timeout
< 0)
1183 stm32_dfsdm_stop_conv(adc
);
1185 stm32_dfsdm_process_data(adc
, res
);
1188 stm32_dfsdm_stop_dfsdm(adc
->dfsdm
);
1193 static int stm32_dfsdm_write_raw(struct iio_dev
*indio_dev
,
1194 struct iio_chan_spec
const *chan
,
1195 int val
, int val2
, long mask
)
1197 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
1198 struct stm32_dfsdm_channel
*ch
= &adc
->dfsdm
->ch_list
[chan
->channel
];
1199 unsigned int spi_freq
;
1203 case DFSDM_CHANNEL_SPI_CLOCK_INTERNAL
:
1204 spi_freq
= adc
->dfsdm
->spi_master_freq
;
1206 case DFSDM_CHANNEL_SPI_CLOCK_INTERNAL_DIV2_FALLING
:
1207 case DFSDM_CHANNEL_SPI_CLOCK_INTERNAL_DIV2_RISING
:
1208 spi_freq
= adc
->dfsdm
->spi_master_freq
/ 2;
1211 spi_freq
= adc
->spi_freq
;
1215 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
1216 ret
= iio_device_claim_direct_mode(indio_dev
);
1220 ret
= stm32_dfsdm_compute_all_osrs(indio_dev
, val
);
1222 dev_dbg(&indio_dev
->dev
,
1223 "Sampling rate changed from (%u) to (%u)\n",
1224 adc
->sample_freq
, spi_freq
/ val
);
1225 adc
->oversamp
= val
;
1226 adc
->sample_freq
= spi_freq
/ val
;
1228 iio_device_release_direct_mode(indio_dev
);
1231 case IIO_CHAN_INFO_SAMP_FREQ
:
1235 ret
= iio_device_claim_direct_mode(indio_dev
);
1239 ret
= dfsdm_adc_set_samp_freq(indio_dev
, val
, spi_freq
);
1240 iio_device_release_direct_mode(indio_dev
);
1247 static int stm32_dfsdm_read_raw(struct iio_dev
*indio_dev
,
1248 struct iio_chan_spec
const *chan
, int *val
,
1249 int *val2
, long mask
)
1251 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
1255 case IIO_CHAN_INFO_RAW
:
1256 ret
= iio_device_claim_direct_mode(indio_dev
);
1259 ret
= iio_hw_consumer_enable(adc
->hwc
);
1261 dev_err(&indio_dev
->dev
,
1262 "%s: IIO enable failed (channel %d)\n",
1263 __func__
, chan
->channel
);
1264 iio_device_release_direct_mode(indio_dev
);
1267 ret
= stm32_dfsdm_single_conv(indio_dev
, chan
, val
);
1268 iio_hw_consumer_disable(adc
->hwc
);
1270 dev_err(&indio_dev
->dev
,
1271 "%s: Conversion failed (channel %d)\n",
1272 __func__
, chan
->channel
);
1273 iio_device_release_direct_mode(indio_dev
);
1276 iio_device_release_direct_mode(indio_dev
);
1279 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
1280 *val
= adc
->oversamp
;
1284 case IIO_CHAN_INFO_SAMP_FREQ
:
1285 *val
= adc
->sample_freq
;
1293 static int stm32_dfsdm_validate_trigger(struct iio_dev
*indio_dev
,
1294 struct iio_trigger
*trig
)
1296 return stm32_dfsdm_get_jextsel(indio_dev
, trig
) < 0 ? -EINVAL
: 0;
1299 static const struct iio_info stm32_dfsdm_info_audio
= {
1300 .hwfifo_set_watermark
= stm32_dfsdm_set_watermark
,
1301 .read_raw
= stm32_dfsdm_read_raw
,
1302 .write_raw
= stm32_dfsdm_write_raw
,
1303 .update_scan_mode
= stm32_dfsdm_update_scan_mode
,
1306 static const struct iio_info stm32_dfsdm_info_adc
= {
1307 .hwfifo_set_watermark
= stm32_dfsdm_set_watermark
,
1308 .read_raw
= stm32_dfsdm_read_raw
,
1309 .write_raw
= stm32_dfsdm_write_raw
,
1310 .update_scan_mode
= stm32_dfsdm_update_scan_mode
,
1311 .validate_trigger
= stm32_dfsdm_validate_trigger
,
1314 static irqreturn_t
stm32_dfsdm_irq(int irq
, void *arg
)
1316 struct stm32_dfsdm_adc
*adc
= arg
;
1317 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
1318 struct regmap
*regmap
= adc
->dfsdm
->regmap
;
1319 unsigned int status
, int_en
;
1321 regmap_read(regmap
, DFSDM_ISR(adc
->fl_id
), &status
);
1322 regmap_read(regmap
, DFSDM_CR2(adc
->fl_id
), &int_en
);
1324 if (status
& DFSDM_ISR_REOCF_MASK
) {
1325 /* Read the data register clean the IRQ status */
1326 regmap_read(regmap
, DFSDM_RDATAR(adc
->fl_id
), adc
->buffer
);
1327 complete(&adc
->completion
);
1330 if (status
& DFSDM_ISR_ROVRF_MASK
) {
1331 if (int_en
& DFSDM_CR2_ROVRIE_MASK
)
1332 dev_warn(&indio_dev
->dev
, "Overrun detected\n");
1333 regmap_update_bits(regmap
, DFSDM_ICR(adc
->fl_id
),
1334 DFSDM_ICR_CLRROVRF_MASK
,
1335 DFSDM_ICR_CLRROVRF_MASK
);
1342 * Define external info for SPI Frequency and audio sampling rate that can be
1343 * configured by ASoC driver through consumer.h API
1345 static const struct iio_chan_spec_ext_info dfsdm_adc_audio_ext_info
[] = {
1346 /* spi_clk_freq : clock freq on SPI/manchester bus used by channel */
1348 .name
= "spi_clk_freq",
1349 .shared
= IIO_SHARED_BY_TYPE
,
1350 .read
= dfsdm_adc_audio_get_spiclk
,
1351 .write
= dfsdm_adc_audio_set_spiclk
,
1356 static void stm32_dfsdm_dma_release(struct iio_dev
*indio_dev
)
1358 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
1360 if (adc
->dma_chan
) {
1361 dma_free_coherent(adc
->dma_chan
->device
->dev
,
1362 DFSDM_DMA_BUFFER_SIZE
,
1363 adc
->rx_buf
, adc
->dma_buf
);
1364 dma_release_channel(adc
->dma_chan
);
1368 static int stm32_dfsdm_dma_request(struct device
*dev
,
1369 struct iio_dev
*indio_dev
)
1371 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
1373 adc
->dma_chan
= dma_request_chan(dev
, "rx");
1374 if (IS_ERR(adc
->dma_chan
)) {
1375 int ret
= PTR_ERR(adc
->dma_chan
);
1377 adc
->dma_chan
= NULL
;
1381 adc
->rx_buf
= dma_alloc_coherent(adc
->dma_chan
->device
->dev
,
1382 DFSDM_DMA_BUFFER_SIZE
,
1383 &adc
->dma_buf
, GFP_KERNEL
);
1385 dma_release_channel(adc
->dma_chan
);
1389 indio_dev
->modes
|= INDIO_BUFFER_SOFTWARE
;
1390 indio_dev
->setup_ops
= &stm32_dfsdm_buffer_setup_ops
;
1395 static int stm32_dfsdm_adc_chan_init_one(struct iio_dev
*indio_dev
,
1396 struct iio_chan_spec
*ch
)
1398 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
1401 ret
= stm32_dfsdm_channel_parse_of(adc
->dfsdm
, indio_dev
, ch
);
1405 ch
->type
= IIO_VOLTAGE
;
1409 * IIO_CHAN_INFO_RAW: used to compute regular conversion
1410 * IIO_CHAN_INFO_OVERSAMPLING_RATIO: used to set oversampling
1412 ch
->info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
);
1413 ch
->info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
) |
1414 BIT(IIO_CHAN_INFO_SAMP_FREQ
);
1416 if (adc
->dev_data
->type
== DFSDM_AUDIO
) {
1417 ch
->ext_info
= dfsdm_adc_audio_ext_info
;
1419 ch
->scan_type
.shift
= 8;
1421 ch
->scan_type
.sign
= 's';
1422 ch
->scan_type
.realbits
= 24;
1423 ch
->scan_type
.storagebits
= 32;
1425 return stm32_dfsdm_chan_configure(adc
->dfsdm
,
1426 &adc
->dfsdm
->ch_list
[ch
->channel
]);
1429 static int stm32_dfsdm_audio_init(struct device
*dev
, struct iio_dev
*indio_dev
)
1431 struct iio_chan_spec
*ch
;
1432 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
1433 struct stm32_dfsdm_channel
*d_ch
;
1436 ch
= devm_kzalloc(&indio_dev
->dev
, sizeof(*ch
), GFP_KERNEL
);
1442 ret
= stm32_dfsdm_adc_chan_init_one(indio_dev
, ch
);
1444 dev_err(&indio_dev
->dev
, "Channels init failed\n");
1447 ch
->info_mask_separate
= BIT(IIO_CHAN_INFO_SAMP_FREQ
);
1449 d_ch
= &adc
->dfsdm
->ch_list
[ch
->channel
];
1450 if (d_ch
->src
!= DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL
)
1451 adc
->spi_freq
= adc
->dfsdm
->spi_master_freq
;
1453 indio_dev
->num_channels
= 1;
1454 indio_dev
->channels
= ch
;
1456 return stm32_dfsdm_dma_request(dev
, indio_dev
);
1459 static int stm32_dfsdm_adc_init(struct device
*dev
, struct iio_dev
*indio_dev
)
1461 struct iio_chan_spec
*ch
;
1462 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
1466 adc
->oversamp
= DFSDM_DEFAULT_OVERSAMPLING
;
1467 ret
= stm32_dfsdm_compute_all_osrs(indio_dev
, adc
->oversamp
);
1471 num_ch
= of_property_count_u32_elems(indio_dev
->dev
.of_node
,
1473 if (num_ch
< 0 || num_ch
> adc
->dfsdm
->num_chs
) {
1474 dev_err(&indio_dev
->dev
, "Bad st,adc-channels\n");
1475 return num_ch
< 0 ? num_ch
: -EINVAL
;
1478 /* Bind to SD modulator IIO device */
1479 adc
->hwc
= devm_iio_hw_consumer_alloc(&indio_dev
->dev
);
1480 if (IS_ERR(adc
->hwc
))
1481 return -EPROBE_DEFER
;
1483 ch
= devm_kcalloc(&indio_dev
->dev
, num_ch
, sizeof(*ch
),
1488 for (chan_idx
= 0; chan_idx
< num_ch
; chan_idx
++) {
1489 ch
[chan_idx
].scan_index
= chan_idx
;
1490 ret
= stm32_dfsdm_adc_chan_init_one(indio_dev
, &ch
[chan_idx
]);
1492 dev_err(&indio_dev
->dev
, "Channels init failed\n");
1497 indio_dev
->num_channels
= num_ch
;
1498 indio_dev
->channels
= ch
;
1500 init_completion(&adc
->completion
);
1502 /* Optionally request DMA */
1503 ret
= stm32_dfsdm_dma_request(dev
, indio_dev
);
1505 if (ret
!= -ENODEV
) {
1506 if (ret
!= -EPROBE_DEFER
)
1508 "DMA channel request failed with %d\n",
1513 dev_dbg(dev
, "No DMA support\n");
1517 ret
= iio_triggered_buffer_setup(indio_dev
,
1518 &iio_pollfunc_store_time
, NULL
,
1519 &stm32_dfsdm_buffer_setup_ops
);
1521 stm32_dfsdm_dma_release(indio_dev
);
1522 dev_err(&indio_dev
->dev
, "buffer setup failed\n");
1526 /* lptimer/timer hardware triggers */
1527 indio_dev
->modes
|= INDIO_HARDWARE_TRIGGERED
;
1532 static const struct stm32_dfsdm_dev_data stm32h7_dfsdm_adc_data
= {
1534 .init
= stm32_dfsdm_adc_init
,
1537 static const struct stm32_dfsdm_dev_data stm32h7_dfsdm_audio_data
= {
1538 .type
= DFSDM_AUDIO
,
1539 .init
= stm32_dfsdm_audio_init
,
1542 static const struct of_device_id stm32_dfsdm_adc_match
[] = {
1544 .compatible
= "st,stm32-dfsdm-adc",
1545 .data
= &stm32h7_dfsdm_adc_data
,
1548 .compatible
= "st,stm32-dfsdm-dmic",
1549 .data
= &stm32h7_dfsdm_audio_data
,
1554 static int stm32_dfsdm_adc_probe(struct platform_device
*pdev
)
1556 struct device
*dev
= &pdev
->dev
;
1557 struct stm32_dfsdm_adc
*adc
;
1558 struct device_node
*np
= dev
->of_node
;
1559 const struct stm32_dfsdm_dev_data
*dev_data
;
1560 struct iio_dev
*iio
;
1564 dev_data
= of_device_get_match_data(dev
);
1565 iio
= devm_iio_device_alloc(dev
, sizeof(*adc
));
1567 dev_err(dev
, "%s: Failed to allocate IIO\n", __func__
);
1571 adc
= iio_priv(iio
);
1572 adc
->dfsdm
= dev_get_drvdata(dev
->parent
);
1574 iio
->dev
.parent
= dev
;
1575 iio
->dev
.of_node
= np
;
1576 iio
->modes
= INDIO_DIRECT_MODE
;
1578 platform_set_drvdata(pdev
, adc
);
1580 ret
= of_property_read_u32(dev
->of_node
, "reg", &adc
->fl_id
);
1581 if (ret
!= 0 || adc
->fl_id
>= adc
->dfsdm
->num_fls
) {
1582 dev_err(dev
, "Missing or bad reg property\n");
1586 name
= devm_kzalloc(dev
, sizeof("dfsdm-adc0"), GFP_KERNEL
);
1589 if (dev_data
->type
== DFSDM_AUDIO
) {
1590 iio
->info
= &stm32_dfsdm_info_audio
;
1591 snprintf(name
, sizeof("dfsdm-pdm0"), "dfsdm-pdm%d", adc
->fl_id
);
1593 iio
->info
= &stm32_dfsdm_info_adc
;
1594 snprintf(name
, sizeof("dfsdm-adc0"), "dfsdm-adc%d", adc
->fl_id
);
1599 * In a first step IRQs generated for channels are not treated.
1600 * So IRQ associated to filter instance 0 is dedicated to the Filter 0.
1602 irq
= platform_get_irq(pdev
, 0);
1606 ret
= devm_request_irq(dev
, irq
, stm32_dfsdm_irq
,
1607 0, pdev
->name
, adc
);
1609 dev_err(dev
, "Failed to request IRQ\n");
1613 ret
= of_property_read_u32(dev
->of_node
, "st,filter-order", &val
);
1615 dev_err(dev
, "Failed to set filter order\n");
1619 adc
->dfsdm
->fl_list
[adc
->fl_id
].ford
= val
;
1621 ret
= of_property_read_u32(dev
->of_node
, "st,filter0-sync", &val
);
1623 adc
->dfsdm
->fl_list
[adc
->fl_id
].sync_mode
= val
;
1625 adc
->dev_data
= dev_data
;
1626 ret
= dev_data
->init(dev
, iio
);
1630 ret
= iio_device_register(iio
);
1634 if (dev_data
->type
== DFSDM_AUDIO
) {
1635 ret
= of_platform_populate(np
, NULL
, NULL
, dev
);
1637 dev_err(dev
, "Failed to find an audio DAI\n");
1638 goto err_unregister
;
1645 iio_device_unregister(iio
);
1647 stm32_dfsdm_dma_release(iio
);
1652 static int stm32_dfsdm_adc_remove(struct platform_device
*pdev
)
1654 struct stm32_dfsdm_adc
*adc
= platform_get_drvdata(pdev
);
1655 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
1657 if (adc
->dev_data
->type
== DFSDM_AUDIO
)
1658 of_platform_depopulate(&pdev
->dev
);
1659 iio_device_unregister(indio_dev
);
1660 stm32_dfsdm_dma_release(indio_dev
);
1665 static int __maybe_unused
stm32_dfsdm_adc_suspend(struct device
*dev
)
1667 struct stm32_dfsdm_adc
*adc
= dev_get_drvdata(dev
);
1668 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
1670 if (iio_buffer_enabled(indio_dev
))
1671 __stm32_dfsdm_predisable(indio_dev
);
1676 static int __maybe_unused
stm32_dfsdm_adc_resume(struct device
*dev
)
1678 struct stm32_dfsdm_adc
*adc
= dev_get_drvdata(dev
);
1679 struct iio_dev
*indio_dev
= iio_priv_to_dev(adc
);
1680 const struct iio_chan_spec
*chan
;
1681 struct stm32_dfsdm_channel
*ch
;
1684 /* restore channels configuration */
1685 for (i
= 0; i
< indio_dev
->num_channels
; i
++) {
1686 chan
= indio_dev
->channels
+ i
;
1687 ch
= &adc
->dfsdm
->ch_list
[chan
->channel
];
1688 ret
= stm32_dfsdm_chan_configure(adc
->dfsdm
, ch
);
1693 if (iio_buffer_enabled(indio_dev
))
1694 __stm32_dfsdm_postenable(indio_dev
);
1699 static SIMPLE_DEV_PM_OPS(stm32_dfsdm_adc_pm_ops
,
1700 stm32_dfsdm_adc_suspend
, stm32_dfsdm_adc_resume
);
1702 static struct platform_driver stm32_dfsdm_adc_driver
= {
1704 .name
= "stm32-dfsdm-adc",
1705 .of_match_table
= stm32_dfsdm_adc_match
,
1706 .pm
= &stm32_dfsdm_adc_pm_ops
,
1708 .probe
= stm32_dfsdm_adc_probe
,
1709 .remove
= stm32_dfsdm_adc_remove
,
1711 module_platform_driver(stm32_dfsdm_adc_driver
);
1713 MODULE_DESCRIPTION("STM32 sigma delta ADC");
1714 MODULE_AUTHOR("Arnaud Pouliquen <arnaud.pouliquen@st.com>");
1715 MODULE_LICENSE("GPL v2");