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/backend.h>
13 #include <linux/iio/buffer.h>
14 #include <linux/iio/hw-consumer.h>
15 #include <linux/iio/sysfs.h>
16 #include <linux/iio/timer/stm32-lptim-trigger.h>
17 #include <linux/iio/timer/stm32-timer-trigger.h>
18 #include <linux/iio/trigger.h>
19 #include <linux/iio/trigger_consumer.h>
20 #include <linux/iio/triggered_buffer.h>
21 #include <linux/interrupt.h>
22 #include <linux/module.h>
24 #include <linux/of_platform.h>
25 #include <linux/platform_device.h>
26 #include <linux/regmap.h>
27 #include <linux/slab.h>
29 #include "stm32-dfsdm.h"
31 #define DFSDM_DMA_BUFFER_SIZE (4 * PAGE_SIZE)
33 /* Conversion timeout */
34 #define DFSDM_TIMEOUT_US 100000
35 #define DFSDM_TIMEOUT (msecs_to_jiffies(DFSDM_TIMEOUT_US / 1000))
37 /* Oversampling attribute default */
38 #define DFSDM_DEFAULT_OVERSAMPLING 100
40 /* Oversampling max values */
41 #define DFSDM_MAX_INT_OVERSAMPLING 256
42 #define DFSDM_MAX_FL_OVERSAMPLING 1024
44 /* Limit filter output resolution to 31 bits. (i.e. sample range is +/-2^30) */
45 #define DFSDM_DATA_MAX BIT(30)
47 * Data are output as two's complement data in a 24 bit field.
48 * Data from filters are in the range +/-2^(n-1)
49 * 2^(n-1) maximum positive value cannot be coded in 2's complement n bits
50 * An extra bit is required to avoid wrap-around of the binary code for 2^(n-1)
51 * So, the resolution of samples from filter is actually limited to 23 bits
53 #define DFSDM_DATA_RES 24
55 /* Filter configuration */
56 #define DFSDM_CR1_CFG_MASK (DFSDM_CR1_RCH_MASK | DFSDM_CR1_RCONT_MASK | \
57 DFSDM_CR1_RSYNC_MASK | DFSDM_CR1_JSYNC_MASK | \
60 enum sd_converter_type
{
65 struct stm32_dfsdm_dev_data
{
67 int (*init
)(struct device
*dev
, struct iio_dev
*indio_dev
);
68 unsigned int num_channels
;
69 const struct regmap_config
*regmap_cfg
;
72 struct stm32_dfsdm_adc
{
73 struct stm32_dfsdm
*dfsdm
;
74 const struct stm32_dfsdm_dev_data
*dev_data
;
80 unsigned int oversamp
;
81 struct iio_hw_consumer
*hwc
;
82 struct iio_backend
**backend
;
83 struct completion completion
;
87 unsigned int spi_freq
; /* SPI bus clock frequency */
88 unsigned int sample_freq
; /* Sample frequency after filter decimation */
89 int (*cb
)(const void *data
, size_t size
, void *cb_priv
);
94 unsigned int bufi
; /* Buffer current position */
95 unsigned int buf_sz
; /* Buffer size */
96 struct dma_chan
*dma_chan
;
100 struct stm32_dfsdm_str2field
{
105 /* DFSDM channel serial interface type */
106 static const struct stm32_dfsdm_str2field stm32_dfsdm_chan_type
[] = {
107 { "SPI_R", 0 }, /* SPI with data on rising edge */
108 { "SPI_F", 1 }, /* SPI with data on falling edge */
109 { "MANCH_R", 2 }, /* Manchester codec, rising edge = logic 0 */
110 { "MANCH_F", 3 }, /* Manchester codec, falling edge = logic 1 */
114 /* DFSDM channel clock source */
115 static const struct stm32_dfsdm_str2field stm32_dfsdm_chan_src
[] = {
116 /* External SPI clock (CLKIN x) */
117 { "CLKIN", DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL
},
118 /* Internal SPI clock (CLKOUT) */
119 { "CLKOUT", DFSDM_CHANNEL_SPI_CLOCK_INTERNAL
},
120 /* Internal SPI clock divided by 2 (falling edge) */
121 { "CLKOUT_F", DFSDM_CHANNEL_SPI_CLOCK_INTERNAL_DIV2_FALLING
},
122 /* Internal SPI clock divided by 2 (falling edge) */
123 { "CLKOUT_R", DFSDM_CHANNEL_SPI_CLOCK_INTERNAL_DIV2_RISING
},
127 static int stm32_dfsdm_str2val(const char *str
,
128 const struct stm32_dfsdm_str2field
*list
)
130 const struct stm32_dfsdm_str2field
*p
= list
;
132 for (p
= list
; p
&& p
->name
; p
++)
133 if (!strcmp(p
->name
, str
))
140 * struct stm32_dfsdm_trig_info - DFSDM trigger info
141 * @name: name of the trigger, corresponding to its source
142 * @jextsel: trigger signal selection
144 struct stm32_dfsdm_trig_info
{
146 unsigned int jextsel
;
149 /* hardware injected trigger enable, edge selection */
150 enum stm32_dfsdm_jexten
{
151 STM32_DFSDM_JEXTEN_DISABLED
,
152 STM32_DFSDM_JEXTEN_RISING_EDGE
,
153 STM32_DFSDM_JEXTEN_FALLING_EDGE
,
154 STM32_DFSDM_EXTEN_BOTH_EDGES
,
157 static const struct stm32_dfsdm_trig_info stm32_dfsdm_trigs
[] = {
173 static int stm32_dfsdm_get_jextsel(struct iio_dev
*indio_dev
,
174 struct iio_trigger
*trig
)
178 /* lookup triggers registered by stm32 timer trigger driver */
179 for (i
= 0; stm32_dfsdm_trigs
[i
].name
; i
++) {
181 * Checking both stm32 timer trigger type and trig name
182 * should be safe against arbitrary trigger names.
184 if ((is_stm32_timer_trigger(trig
) ||
185 is_stm32_lptim_trigger(trig
)) &&
186 !strcmp(stm32_dfsdm_trigs
[i
].name
, trig
->name
)) {
187 return stm32_dfsdm_trigs
[i
].jextsel
;
194 static int stm32_dfsdm_compute_osrs(struct stm32_dfsdm_filter
*fl
,
195 unsigned int fast
, unsigned int oversamp
)
197 unsigned int i
, d
, fosr
, iosr
;
200 unsigned int m
= 1; /* multiplication factor */
201 unsigned int p
= fl
->ford
; /* filter order (ford) */
202 struct stm32_dfsdm_filter_osr
*flo
= &fl
->flo
[fast
];
204 pr_debug("Requested oversampling: %d\n", oversamp
);
206 * This function tries to compute filter oversampling and integrator
207 * oversampling, base on oversampling ratio requested by user.
209 * Decimation d depends on the filter order and the oversampling ratios.
211 * fosr: filter over sampling ratio
212 * iosr: integrator over sampling ratio
214 if (fl
->ford
== DFSDM_FASTSINC_ORDER
) {
220 * Look for filter and integrator oversampling ratios which allows
221 * to maximize data output resolution.
223 for (fosr
= 1; fosr
<= DFSDM_MAX_FL_OVERSAMPLING
; fosr
++) {
224 for (iosr
= 1; iosr
<= DFSDM_MAX_INT_OVERSAMPLING
; iosr
++) {
227 else if (fl
->ford
== DFSDM_FASTSINC_ORDER
)
228 d
= fosr
* (iosr
+ 3) + 2;
230 d
= fosr
* (iosr
- 1 + p
) + p
;
234 else if (d
!= oversamp
)
237 * Check resolution (limited to signed 32 bits)
240 * res = m * fosr^p x iosr (with m=1, p=ford)
242 * res = m * fosr^p x iosr (with m=2, p=2)
245 for (i
= p
- 1; i
> 0; i
--) {
246 res
= res
* (u64
)fosr
;
247 if (res
> DFSDM_DATA_MAX
)
250 if (res
> DFSDM_DATA_MAX
)
253 res
= res
* (u64
)m
* (u64
)iosr
;
254 if (res
> DFSDM_DATA_MAX
)
257 if (res
>= flo
->res
) {
262 bits
= fls(flo
->res
);
263 /* 8 LBSs in data register contain chan info */
266 /* if resolution is not a power of two */
267 if (flo
->res
> BIT(bits
- 1))
272 shift
= DFSDM_DATA_RES
- bits
;
274 * Compute right/left shift
275 * Right shift is performed by hardware
276 * when transferring samples to data register.
277 * Left shift is done by software on buffer
280 /* Resolution is lower than 24 bits */
285 * If resolution is 24 bits or more,
286 * max positive value may be ambiguous
287 * (equal to max negative value as sign
289 * Reduce resolution to 23 bits (rshift)
290 * to keep the sign on bit 23 and treat
291 * saturation before rescaling on 24
294 flo
->rshift
= 1 - shift
;
301 pr_debug("fast %d, fosr %d, iosr %d, res 0x%llx/%d bits, rshift %d, lshift %d\n",
302 fast
, flo
->fosr
, flo
->iosr
,
303 flo
->res
, bits
, flo
->rshift
,
315 static int stm32_dfsdm_compute_all_osrs(struct iio_dev
*indio_dev
,
316 unsigned int oversamp
)
318 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
319 struct stm32_dfsdm_filter
*fl
= &adc
->dfsdm
->fl_list
[adc
->fl_id
];
322 memset(&fl
->flo
[0], 0, sizeof(fl
->flo
[0]));
323 memset(&fl
->flo
[1], 0, sizeof(fl
->flo
[1]));
325 ret0
= stm32_dfsdm_compute_osrs(fl
, 0, oversamp
);
326 ret1
= stm32_dfsdm_compute_osrs(fl
, 1, oversamp
);
327 if (ret0
< 0 && ret1
< 0) {
328 dev_err(&indio_dev
->dev
,
329 "Filter parameters not found: errors %d/%d\n",
337 static int stm32_dfsdm_start_channel(struct iio_dev
*indio_dev
)
339 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
340 struct regmap
*regmap
= adc
->dfsdm
->regmap
;
341 const struct iio_chan_spec
*chan
;
345 for_each_set_bit(bit
, &adc
->smask
, sizeof(adc
->smask
) * BITS_PER_BYTE
) {
346 chan
= indio_dev
->channels
+ bit
;
347 ret
= regmap_update_bits(regmap
, DFSDM_CHCFGR1(chan
->channel
),
348 DFSDM_CHCFGR1_CHEN_MASK
,
349 DFSDM_CHCFGR1_CHEN(1));
357 static void stm32_dfsdm_stop_channel(struct iio_dev
*indio_dev
)
359 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
360 struct regmap
*regmap
= adc
->dfsdm
->regmap
;
361 const struct iio_chan_spec
*chan
;
364 for_each_set_bit(bit
, &adc
->smask
, sizeof(adc
->smask
) * BITS_PER_BYTE
) {
365 chan
= indio_dev
->channels
+ bit
;
366 regmap_update_bits(regmap
, DFSDM_CHCFGR1(chan
->channel
),
367 DFSDM_CHCFGR1_CHEN_MASK
,
368 DFSDM_CHCFGR1_CHEN(0));
372 static int stm32_dfsdm_chan_configure(struct stm32_dfsdm
*dfsdm
,
373 struct stm32_dfsdm_channel
*ch
)
375 unsigned int id
= ch
->id
;
376 struct regmap
*regmap
= dfsdm
->regmap
;
379 ret
= regmap_update_bits(regmap
, DFSDM_CHCFGR1(id
),
380 DFSDM_CHCFGR1_SITP_MASK
,
381 DFSDM_CHCFGR1_SITP(ch
->type
));
384 ret
= regmap_update_bits(regmap
, DFSDM_CHCFGR1(id
),
385 DFSDM_CHCFGR1_SPICKSEL_MASK
,
386 DFSDM_CHCFGR1_SPICKSEL(ch
->src
));
389 return regmap_update_bits(regmap
, DFSDM_CHCFGR1(id
),
390 DFSDM_CHCFGR1_CHINSEL_MASK
,
391 DFSDM_CHCFGR1_CHINSEL(ch
->alt_si
));
394 static int stm32_dfsdm_start_filter(struct stm32_dfsdm_adc
*adc
,
396 struct iio_trigger
*trig
)
398 struct stm32_dfsdm
*dfsdm
= adc
->dfsdm
;
402 ret
= regmap_update_bits(dfsdm
->regmap
, DFSDM_CR1(fl_id
),
403 DFSDM_CR1_DFEN_MASK
, DFSDM_CR1_DFEN(1));
407 /* Nothing more to do for injected (scan mode/triggered) conversions */
408 if (adc
->nconv
> 1 || trig
)
411 /* Software start (single or continuous) regular conversion */
412 return regmap_update_bits(dfsdm
->regmap
, DFSDM_CR1(fl_id
),
413 DFSDM_CR1_RSWSTART_MASK
,
414 DFSDM_CR1_RSWSTART(1));
417 static void stm32_dfsdm_stop_filter(struct stm32_dfsdm
*dfsdm
,
420 /* Disable conversion */
421 regmap_update_bits(dfsdm
->regmap
, DFSDM_CR1(fl_id
),
422 DFSDM_CR1_DFEN_MASK
, DFSDM_CR1_DFEN(0));
425 static int stm32_dfsdm_filter_set_trig(struct iio_dev
*indio_dev
,
427 struct iio_trigger
*trig
)
429 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
430 struct regmap
*regmap
= adc
->dfsdm
->regmap
;
431 u32 jextsel
= 0, jexten
= STM32_DFSDM_JEXTEN_DISABLED
;
435 ret
= stm32_dfsdm_get_jextsel(indio_dev
, trig
);
439 /* set trigger source and polarity (default to rising edge) */
441 jexten
= STM32_DFSDM_JEXTEN_RISING_EDGE
;
444 ret
= regmap_update_bits(regmap
, DFSDM_CR1(fl_id
),
445 DFSDM_CR1_JEXTSEL_MASK
| DFSDM_CR1_JEXTEN_MASK
,
446 DFSDM_CR1_JEXTSEL(jextsel
) |
447 DFSDM_CR1_JEXTEN(jexten
));
454 static int stm32_dfsdm_channels_configure(struct iio_dev
*indio_dev
,
456 struct iio_trigger
*trig
)
458 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
459 struct regmap
*regmap
= adc
->dfsdm
->regmap
;
460 struct stm32_dfsdm_filter
*fl
= &adc
->dfsdm
->fl_list
[fl_id
];
461 struct stm32_dfsdm_filter_osr
*flo
= &fl
->flo
[0];
462 const struct iio_chan_spec
*chan
;
469 * In continuous mode, use fast mode configuration,
470 * if it provides a better resolution.
472 if (adc
->nconv
== 1 && !trig
&& iio_buffer_enabled(indio_dev
)) {
473 if (fl
->flo
[1].res
>= fl
->flo
[0].res
) {
482 dev_dbg(&indio_dev
->dev
, "Samples actual resolution: %d bits",
483 min(flo
->bits
, (u32
)DFSDM_DATA_RES
- 1));
485 for_each_set_bit(bit
, &adc
->smask
,
486 sizeof(adc
->smask
) * BITS_PER_BYTE
) {
487 chan
= indio_dev
->channels
+ bit
;
489 ret
= regmap_update_bits(regmap
,
490 DFSDM_CHCFGR2(chan
->channel
),
491 DFSDM_CHCFGR2_DTRBS_MASK
,
492 DFSDM_CHCFGR2_DTRBS(flo
->rshift
));
500 static int stm32_dfsdm_filter_configure(struct iio_dev
*indio_dev
,
502 struct iio_trigger
*trig
)
504 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
505 struct regmap
*regmap
= adc
->dfsdm
->regmap
;
506 struct stm32_dfsdm_filter
*fl
= &adc
->dfsdm
->fl_list
[fl_id
];
507 struct stm32_dfsdm_filter_osr
*flo
= &fl
->flo
[fl
->fast
];
509 const struct iio_chan_spec
*chan
;
510 unsigned int bit
, jchg
= 0;
513 /* Average integrator oversampling */
514 ret
= regmap_update_bits(regmap
, DFSDM_FCR(fl_id
), DFSDM_FCR_IOSR_MASK
,
515 DFSDM_FCR_IOSR(flo
->iosr
- 1));
519 /* Filter order and Oversampling */
520 ret
= regmap_update_bits(regmap
, DFSDM_FCR(fl_id
), DFSDM_FCR_FOSR_MASK
,
521 DFSDM_FCR_FOSR(flo
->fosr
- 1));
525 ret
= regmap_update_bits(regmap
, DFSDM_FCR(fl_id
), DFSDM_FCR_FORD_MASK
,
526 DFSDM_FCR_FORD(fl
->ford
));
530 ret
= stm32_dfsdm_filter_set_trig(indio_dev
, fl_id
, trig
);
534 ret
= regmap_update_bits(regmap
, DFSDM_CR1(fl_id
),
536 DFSDM_CR1_FAST(fl
->fast
));
541 * DFSDM modes configuration W.R.T audio/iio type modes
542 * ----------------------------------------------------------------
543 * Modes | regular | regular | injected | injected |
544 * | | continuous | | + scan |
545 * --------------|---------|--------------|----------|------------|
546 * single conv | x | | | |
548 * --------------|---------|--------------|----------|------------|
549 * 1 Audio chan | | sample freq | | |
550 * | | or sync_mode | | |
551 * --------------|---------|--------------|----------|------------|
552 * 1 IIO chan | | sample freq | trigger | |
553 * | | or sync_mode | | |
554 * --------------|---------|--------------|----------|------------|
555 * 2+ IIO chans | | | | trigger or |
556 * | | | | sync_mode |
557 * ----------------------------------------------------------------
559 if (adc
->nconv
== 1 && !trig
) {
560 bit
= __ffs(adc
->smask
);
561 chan
= indio_dev
->channels
+ bit
;
563 /* Use regular conversion for single channel without trigger */
564 cr1
= DFSDM_CR1_RCH(chan
->channel
);
566 /* Continuous conversions triggered by SPI clk in buffer mode */
567 if (iio_buffer_enabled(indio_dev
))
568 cr1
|= DFSDM_CR1_RCONT(1);
570 cr1
|= DFSDM_CR1_RSYNC(fl
->sync_mode
);
572 /* Use injected conversion for multiple channels */
573 for_each_set_bit(bit
, &adc
->smask
,
574 sizeof(adc
->smask
) * BITS_PER_BYTE
) {
575 chan
= indio_dev
->channels
+ bit
;
576 jchg
|= BIT(chan
->channel
);
578 ret
= regmap_write(regmap
, DFSDM_JCHGR(fl_id
), jchg
);
582 /* Use scan mode for multiple channels */
583 cr1
= DFSDM_CR1_JSCAN((adc
->nconv
> 1) ? 1 : 0);
586 * Continuous conversions not supported in injected mode,
588 * - conversions in sync with filter 0
589 * - triggered conversions
591 if (!fl
->sync_mode
&& !trig
)
593 cr1
|= DFSDM_CR1_JSYNC(fl
->sync_mode
);
596 return regmap_update_bits(regmap
, DFSDM_CR1(fl_id
), DFSDM_CR1_CFG_MASK
,
600 static int stm32_dfsdm_channel_parse_of(struct stm32_dfsdm
*dfsdm
,
601 struct iio_dev
*indio_dev
,
602 struct iio_chan_spec
*ch
)
604 struct stm32_dfsdm_channel
*df_ch
;
606 int chan_idx
= ch
->scan_index
;
609 ret
= of_property_read_u32_index(indio_dev
->dev
.of_node
,
610 "st,adc-channels", chan_idx
,
613 dev_err(&indio_dev
->dev
,
614 " Error parsing 'st,adc-channels' for idx %d\n",
618 if (ch
->channel
>= dfsdm
->num_chs
) {
619 dev_err(&indio_dev
->dev
,
620 " Error bad channel number %d (max = %d)\n",
621 ch
->channel
, dfsdm
->num_chs
);
625 ret
= of_property_read_string_index(indio_dev
->dev
.of_node
,
626 "st,adc-channel-names", chan_idx
,
627 &ch
->datasheet_name
);
629 dev_err(&indio_dev
->dev
,
630 " Error parsing 'st,adc-channel-names' for idx %d\n",
635 df_ch
= &dfsdm
->ch_list
[ch
->channel
];
636 df_ch
->id
= ch
->channel
;
638 ret
= of_property_read_string_index(indio_dev
->dev
.of_node
,
639 "st,adc-channel-types", chan_idx
,
642 val
= stm32_dfsdm_str2val(of_str
, stm32_dfsdm_chan_type
);
650 ret
= of_property_read_string_index(indio_dev
->dev
.of_node
,
651 "st,adc-channel-clk-src", chan_idx
,
654 val
= stm32_dfsdm_str2val(of_str
, stm32_dfsdm_chan_src
);
662 ret
= of_property_read_u32_index(indio_dev
->dev
.of_node
,
663 "st,adc-alt-channel", chan_idx
,
671 static int stm32_dfsdm_generic_channel_parse_of(struct stm32_dfsdm
*dfsdm
,
672 struct iio_dev
*indio_dev
,
673 struct iio_chan_spec
*ch
,
674 struct fwnode_handle
*node
)
676 struct stm32_dfsdm_channel
*df_ch
;
677 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
678 struct iio_backend
*backend
;
682 ret
= fwnode_property_read_u32(node
, "reg", &ch
->channel
);
684 dev_err(&indio_dev
->dev
, "Missing channel index %d\n", ret
);
688 if (ch
->channel
>= dfsdm
->num_chs
) {
689 dev_err(&indio_dev
->dev
, " Error bad channel number %d (max = %d)\n",
690 ch
->channel
, dfsdm
->num_chs
);
694 ret
= fwnode_property_read_string(node
, "label", &ch
->datasheet_name
);
696 dev_err(&indio_dev
->dev
,
697 " Error parsing 'label' for idx %d\n", ch
->channel
);
701 df_ch
= &dfsdm
->ch_list
[ch
->channel
];
702 df_ch
->id
= ch
->channel
;
704 ret
= fwnode_property_read_string(node
, "st,adc-channel-type", &of_str
);
706 val
= stm32_dfsdm_str2val(of_str
, stm32_dfsdm_chan_type
);
714 ret
= fwnode_property_read_string(node
, "st,adc-channel-clk-src", &of_str
);
716 val
= stm32_dfsdm_str2val(of_str
, stm32_dfsdm_chan_src
);
724 ret
= fwnode_property_read_u32(node
, "st,adc-alt-channel", &df_ch
->alt_si
);
728 if (adc
->dev_data
->type
== DFSDM_IIO
) {
729 backend
= devm_iio_backend_fwnode_get(&indio_dev
->dev
, NULL
, node
);
731 return dev_err_probe(&indio_dev
->dev
, PTR_ERR(backend
),
732 "Failed to get backend\n");
733 adc
->backend
[ch
->scan_index
] = backend
;
739 static ssize_t
dfsdm_adc_audio_get_spiclk(struct iio_dev
*indio_dev
,
741 const struct iio_chan_spec
*chan
,
744 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
746 return snprintf(buf
, PAGE_SIZE
, "%d\n", adc
->spi_freq
);
749 static int dfsdm_adc_set_samp_freq(struct iio_dev
*indio_dev
,
750 unsigned int sample_freq
,
751 unsigned int spi_freq
)
753 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
754 unsigned int oversamp
;
757 oversamp
= DIV_ROUND_CLOSEST(spi_freq
, sample_freq
);
758 if (spi_freq
% sample_freq
)
759 dev_dbg(&indio_dev
->dev
,
760 "Rate not accurate. requested (%u), actual (%u)\n",
761 sample_freq
, spi_freq
/ oversamp
);
763 ret
= stm32_dfsdm_compute_all_osrs(indio_dev
, oversamp
);
767 adc
->sample_freq
= spi_freq
/ oversamp
;
768 adc
->oversamp
= oversamp
;
773 static ssize_t
dfsdm_adc_audio_set_spiclk(struct iio_dev
*indio_dev
,
775 const struct iio_chan_spec
*chan
,
776 const char *buf
, size_t len
)
778 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
779 struct stm32_dfsdm_channel
*ch
= &adc
->dfsdm
->ch_list
[chan
->channel
];
780 unsigned int sample_freq
= adc
->sample_freq
;
781 unsigned int spi_freq
;
784 dev_err(&indio_dev
->dev
, "enter %s\n", __func__
);
785 /* If DFSDM is master on SPI, SPI freq can not be updated */
786 if (ch
->src
!= DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL
)
789 ret
= kstrtoint(buf
, 0, &spi_freq
);
797 ret
= dfsdm_adc_set_samp_freq(indio_dev
, sample_freq
, spi_freq
);
801 adc
->spi_freq
= spi_freq
;
806 static int stm32_dfsdm_start_conv(struct iio_dev
*indio_dev
,
807 struct iio_trigger
*trig
)
809 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
810 struct regmap
*regmap
= adc
->dfsdm
->regmap
;
813 ret
= stm32_dfsdm_channels_configure(indio_dev
, adc
->fl_id
, trig
);
817 ret
= stm32_dfsdm_start_channel(indio_dev
);
821 ret
= stm32_dfsdm_filter_configure(indio_dev
, adc
->fl_id
, trig
);
825 ret
= stm32_dfsdm_start_filter(adc
, adc
->fl_id
, trig
);
827 goto filter_unconfigure
;
832 regmap_clear_bits(regmap
, DFSDM_CR1(adc
->fl_id
), DFSDM_CR1_CFG_MASK
);
834 stm32_dfsdm_stop_channel(indio_dev
);
839 static void stm32_dfsdm_stop_conv(struct iio_dev
*indio_dev
)
841 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
842 struct regmap
*regmap
= adc
->dfsdm
->regmap
;
844 stm32_dfsdm_stop_filter(adc
->dfsdm
, adc
->fl_id
);
846 regmap_clear_bits(regmap
, DFSDM_CR1(adc
->fl_id
), DFSDM_CR1_CFG_MASK
);
848 stm32_dfsdm_stop_channel(indio_dev
);
851 static int stm32_dfsdm_set_watermark(struct iio_dev
*indio_dev
,
854 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
855 unsigned int watermark
= DFSDM_DMA_BUFFER_SIZE
/ 2;
856 unsigned int rx_buf_sz
= DFSDM_DMA_BUFFER_SIZE
;
859 * DMA cyclic transfers are used, buffer is split into two periods.
861 * - always one buffer (period) DMA is working on
862 * - one buffer (period) driver pushed to ASoC side.
864 watermark
= min(watermark
, val
* (unsigned int)(sizeof(u32
)));
865 adc
->buf_sz
= min(rx_buf_sz
, watermark
* 2 * adc
->nconv
);
870 static unsigned int stm32_dfsdm_adc_dma_residue(struct stm32_dfsdm_adc
*adc
)
872 struct dma_tx_state state
;
873 enum dma_status status
;
875 status
= dmaengine_tx_status(adc
->dma_chan
,
876 adc
->dma_chan
->cookie
,
878 if (status
== DMA_IN_PROGRESS
) {
879 /* Residue is size in bytes from end of buffer */
880 unsigned int i
= adc
->buf_sz
- state
.residue
;
883 /* Return available bytes */
885 size
= i
- adc
->bufi
;
887 size
= adc
->buf_sz
+ i
- adc
->bufi
;
895 static inline void stm32_dfsdm_process_data(struct stm32_dfsdm_adc
*adc
,
898 struct stm32_dfsdm_filter
*fl
= &adc
->dfsdm
->fl_list
[adc
->fl_id
];
899 struct stm32_dfsdm_filter_osr
*flo
= &fl
->flo
[fl
->fast
];
900 unsigned int i
= adc
->nconv
;
904 /* Mask 8 LSB that contains the channel ID */
906 /* Convert 2^(n-1) sample to 2^(n-1)-1 to avoid wrap-around */
910 * Samples from filter are retrieved with 23 bits resolution
911 * or less. Shift left to align MSB on 24 bits.
913 *ptr
<<= flo
->lshift
;
919 static void stm32_dfsdm_dma_buffer_done(void *data
)
921 struct iio_dev
*indio_dev
= data
;
922 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
923 int available
= stm32_dfsdm_adc_dma_residue(adc
);
927 * FIXME: In Kernel interface does not support cyclic DMA buffer,and
928 * offers only an interface to push data samples per samples.
929 * For this reason IIO buffer interface is not used and interface is
930 * bypassed using a private callback registered by ASoC.
931 * This should be a temporary solution waiting a cyclic DMA engine
935 dev_dbg(&indio_dev
->dev
, "pos = %d, available = %d\n",
936 adc
->bufi
, available
);
939 while (available
>= indio_dev
->scan_bytes
) {
940 s32
*buffer
= (s32
*)&adc
->rx_buf
[adc
->bufi
];
942 stm32_dfsdm_process_data(adc
, buffer
);
944 available
-= indio_dev
->scan_bytes
;
945 adc
->bufi
+= indio_dev
->scan_bytes
;
946 if (adc
->bufi
>= adc
->buf_sz
) {
948 adc
->cb(&adc
->rx_buf
[old_pos
],
949 adc
->buf_sz
- old_pos
, adc
->cb_priv
);
954 * In DMA mode the trigger services of IIO are not used
955 * (e.g. no call to iio_trigger_poll).
956 * Calling irq handler associated to the hardware trigger is not
957 * relevant as the conversions have already been done. Data
958 * transfers are performed directly in DMA callback instead.
959 * This implementation avoids to call trigger irq handler that
960 * may sleep, in an atomic context (DMA irq handler context).
962 if (adc
->dev_data
->type
== DFSDM_IIO
)
963 iio_push_to_buffers(indio_dev
, buffer
);
966 adc
->cb(&adc
->rx_buf
[old_pos
], adc
->bufi
- old_pos
,
970 static int stm32_dfsdm_adc_dma_start(struct iio_dev
*indio_dev
)
972 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
974 * The DFSDM supports half-word transfers. However, for 16 bits record,
975 * 4 bytes buswidth is kept, to avoid losing samples LSBs when left
978 struct dma_slave_config config
= {
979 .src_addr
= (dma_addr_t
)adc
->dfsdm
->phys_base
,
980 .src_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
,
982 struct dma_async_tx_descriptor
*desc
;
989 dev_dbg(&indio_dev
->dev
, "size=%d watermark=%d\n",
990 adc
->buf_sz
, adc
->buf_sz
/ 2);
992 if (adc
->nconv
== 1 && !indio_dev
->trig
)
993 config
.src_addr
+= DFSDM_RDATAR(adc
->fl_id
);
995 config
.src_addr
+= DFSDM_JDATAR(adc
->fl_id
);
996 ret
= dmaengine_slave_config(adc
->dma_chan
, &config
);
1000 /* Prepare a DMA cyclic transaction */
1001 desc
= dmaengine_prep_dma_cyclic(adc
->dma_chan
,
1003 adc
->buf_sz
, adc
->buf_sz
/ 2,
1005 DMA_PREP_INTERRUPT
);
1009 desc
->callback
= stm32_dfsdm_dma_buffer_done
;
1010 desc
->callback_param
= indio_dev
;
1012 cookie
= dmaengine_submit(desc
);
1013 ret
= dma_submit_error(cookie
);
1017 /* Issue pending DMA requests */
1018 dma_async_issue_pending(adc
->dma_chan
);
1020 if (adc
->nconv
== 1 && !indio_dev
->trig
) {
1021 /* Enable regular DMA transfer*/
1022 ret
= regmap_set_bits(adc
->dfsdm
->regmap
,
1023 DFSDM_CR1(adc
->fl_id
),
1024 DFSDM_CR1_RDMAEN_MASK
);
1026 /* Enable injected DMA transfer*/
1027 ret
= regmap_set_bits(adc
->dfsdm
->regmap
,
1028 DFSDM_CR1(adc
->fl_id
),
1029 DFSDM_CR1_JDMAEN_MASK
);
1038 dmaengine_terminate_all(adc
->dma_chan
);
1043 static void stm32_dfsdm_adc_dma_stop(struct iio_dev
*indio_dev
)
1045 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
1050 regmap_clear_bits(adc
->dfsdm
->regmap
, DFSDM_CR1(adc
->fl_id
),
1051 DFSDM_CR1_RDMAEN_MASK
| DFSDM_CR1_JDMAEN_MASK
);
1052 dmaengine_terminate_all(adc
->dma_chan
);
1055 static int stm32_dfsdm_update_scan_mode(struct iio_dev
*indio_dev
,
1056 const unsigned long *scan_mask
)
1058 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
1060 adc
->nconv
= bitmap_weight(scan_mask
, iio_get_masklength(indio_dev
));
1061 adc
->smask
= *scan_mask
;
1063 dev_dbg(&indio_dev
->dev
, "nconv=%d mask=%lx\n", adc
->nconv
, *scan_mask
);
1068 static int stm32_dfsdm_postenable(struct iio_dev
*indio_dev
)
1070 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
1074 /* Reset adc buffer index */
1078 ret
= iio_hw_consumer_enable(adc
->hwc
);
1084 while (adc
->backend
[i
]) {
1085 ret
= iio_backend_enable(adc
->backend
[i
]);
1092 ret
= stm32_dfsdm_start_dfsdm(adc
->dfsdm
);
1096 ret
= stm32_dfsdm_adc_dma_start(indio_dev
);
1098 dev_err(&indio_dev
->dev
, "Can't start DMA\n");
1102 ret
= stm32_dfsdm_start_conv(indio_dev
, indio_dev
->trig
);
1104 dev_err(&indio_dev
->dev
, "Can't start conversion\n");
1111 stm32_dfsdm_adc_dma_stop(indio_dev
);
1113 stm32_dfsdm_stop_dfsdm(adc
->dfsdm
);
1116 iio_hw_consumer_disable(adc
->hwc
);
1121 static int stm32_dfsdm_predisable(struct iio_dev
*indio_dev
)
1123 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
1126 stm32_dfsdm_stop_conv(indio_dev
);
1128 stm32_dfsdm_adc_dma_stop(indio_dev
);
1130 stm32_dfsdm_stop_dfsdm(adc
->dfsdm
);
1133 while (adc
->backend
[i
]) {
1134 iio_backend_disable(adc
->backend
[i
]);
1140 iio_hw_consumer_disable(adc
->hwc
);
1145 static const struct iio_buffer_setup_ops stm32_dfsdm_buffer_setup_ops
= {
1146 .postenable
= &stm32_dfsdm_postenable
,
1147 .predisable
= &stm32_dfsdm_predisable
,
1151 * stm32_dfsdm_get_buff_cb() - register a callback that will be called when
1152 * DMA transfer period is achieved.
1154 * @iio_dev: Handle to IIO device.
1155 * @cb: Pointer to callback function:
1156 * - data: pointer to data buffer
1157 * - size: size in byte of the data buffer
1158 * - private: pointer to consumer private structure.
1159 * @private: Pointer to consumer private structure.
1161 int stm32_dfsdm_get_buff_cb(struct iio_dev
*iio_dev
,
1162 int (*cb
)(const void *data
, size_t size
,
1166 struct stm32_dfsdm_adc
*adc
;
1170 adc
= iio_priv(iio_dev
);
1173 adc
->cb_priv
= private;
1177 EXPORT_SYMBOL_GPL(stm32_dfsdm_get_buff_cb
);
1180 * stm32_dfsdm_release_buff_cb - unregister buffer callback
1182 * @iio_dev: Handle to IIO device.
1184 int stm32_dfsdm_release_buff_cb(struct iio_dev
*iio_dev
)
1186 struct stm32_dfsdm_adc
*adc
;
1190 adc
= iio_priv(iio_dev
);
1193 adc
->cb_priv
= NULL
;
1197 EXPORT_SYMBOL_GPL(stm32_dfsdm_release_buff_cb
);
1199 static int stm32_dfsdm_single_conv(struct iio_dev
*indio_dev
,
1200 const struct iio_chan_spec
*chan
, int *res
)
1202 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
1206 reinit_completion(&adc
->completion
);
1210 ret
= stm32_dfsdm_start_dfsdm(adc
->dfsdm
);
1214 ret
= regmap_update_bits(adc
->dfsdm
->regmap
, DFSDM_CR2(adc
->fl_id
),
1215 DFSDM_CR2_REOCIE_MASK
, DFSDM_CR2_REOCIE(1));
1220 adc
->smask
= BIT(chan
->scan_index
);
1221 ret
= stm32_dfsdm_start_conv(indio_dev
, NULL
);
1223 regmap_update_bits(adc
->dfsdm
->regmap
, DFSDM_CR2(adc
->fl_id
),
1224 DFSDM_CR2_REOCIE_MASK
, DFSDM_CR2_REOCIE(0));
1228 time_left
= wait_for_completion_interruptible_timeout(&adc
->completion
,
1231 /* Mask IRQ for regular conversion achievement*/
1232 regmap_update_bits(adc
->dfsdm
->regmap
, DFSDM_CR2(adc
->fl_id
),
1233 DFSDM_CR2_REOCIE_MASK
, DFSDM_CR2_REOCIE(0));
1237 else if (time_left
< 0)
1242 stm32_dfsdm_stop_conv(indio_dev
);
1244 stm32_dfsdm_process_data(adc
, res
);
1247 stm32_dfsdm_stop_dfsdm(adc
->dfsdm
);
1252 static int stm32_dfsdm_write_raw(struct iio_dev
*indio_dev
,
1253 struct iio_chan_spec
const *chan
,
1254 int val
, int val2
, long mask
)
1256 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
1257 struct stm32_dfsdm_channel
*ch
= &adc
->dfsdm
->ch_list
[chan
->channel
];
1258 unsigned int spi_freq
;
1262 case DFSDM_CHANNEL_SPI_CLOCK_INTERNAL
:
1263 spi_freq
= adc
->dfsdm
->spi_master_freq
;
1265 case DFSDM_CHANNEL_SPI_CLOCK_INTERNAL_DIV2_FALLING
:
1266 case DFSDM_CHANNEL_SPI_CLOCK_INTERNAL_DIV2_RISING
:
1267 spi_freq
= adc
->dfsdm
->spi_master_freq
/ 2;
1270 spi_freq
= adc
->spi_freq
;
1274 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
1275 ret
= iio_device_claim_direct_mode(indio_dev
);
1279 ret
= stm32_dfsdm_compute_all_osrs(indio_dev
, val
);
1281 dev_dbg(&indio_dev
->dev
,
1282 "Sampling rate changed from (%u) to (%u)\n",
1283 adc
->sample_freq
, spi_freq
/ val
);
1284 adc
->oversamp
= val
;
1285 adc
->sample_freq
= spi_freq
/ val
;
1287 iio_device_release_direct_mode(indio_dev
);
1290 case IIO_CHAN_INFO_SAMP_FREQ
:
1294 ret
= iio_device_claim_direct_mode(indio_dev
);
1298 ret
= dfsdm_adc_set_samp_freq(indio_dev
, val
, spi_freq
);
1299 iio_device_release_direct_mode(indio_dev
);
1306 static int stm32_dfsdm_read_raw(struct iio_dev
*indio_dev
,
1307 struct iio_chan_spec
const *chan
, int *val
,
1308 int *val2
, long mask
)
1310 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
1312 struct stm32_dfsdm_filter
*fl
= &adc
->dfsdm
->fl_list
[adc
->fl_id
];
1313 struct stm32_dfsdm_filter_osr
*flo
= &fl
->flo
[fl
->fast
];
1314 u32 max
= flo
->max
<< (flo
->lshift
- chan
->scan_type
.shift
);
1315 int idx
= chan
->scan_index
;
1318 if (flo
->lshift
< chan
->scan_type
.shift
)
1319 max
= flo
->max
>> (chan
->scan_type
.shift
- flo
->lshift
);
1322 case IIO_CHAN_INFO_RAW
:
1323 ret
= iio_device_claim_direct_mode(indio_dev
);
1327 ret
= iio_hw_consumer_enable(adc
->hwc
);
1329 ret
= iio_backend_enable(adc
->backend
[idx
]);
1331 dev_err(&indio_dev
->dev
,
1332 "%s: IIO enable failed (channel %d)\n",
1333 __func__
, chan
->channel
);
1334 iio_device_release_direct_mode(indio_dev
);
1337 ret
= stm32_dfsdm_single_conv(indio_dev
, chan
, val
);
1339 iio_hw_consumer_disable(adc
->hwc
);
1341 iio_backend_disable(adc
->backend
[idx
]);
1343 dev_err(&indio_dev
->dev
,
1344 "%s: Conversion failed (channel %d)\n",
1345 __func__
, chan
->channel
);
1346 iio_device_release_direct_mode(indio_dev
);
1349 iio_device_release_direct_mode(indio_dev
);
1352 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
1353 *val
= adc
->oversamp
;
1357 case IIO_CHAN_INFO_SAMP_FREQ
:
1358 *val
= adc
->sample_freq
;
1362 case IIO_CHAN_INFO_SCALE
:
1364 * Scale is expressed in mV.
1365 * When fast mode is disabled, actual resolution may be lower
1366 * than 2^n, where n = realbits - 1.
1367 * This leads to underestimating the input voltage.
1368 * To compensate this deviation, the voltage reference can be
1369 * corrected with a factor = realbits resolution / actual max
1372 ret
= iio_backend_read_scale(adc
->backend
[idx
], chan
, val
, NULL
);
1376 *val
= div_u64((u64
)*val
* (u64
)BIT(DFSDM_DATA_RES
- 1), max
);
1377 *val2
= chan
->scan_type
.realbits
;
1378 if (chan
->differential
)
1381 return IIO_VAL_FRACTIONAL_LOG2
;
1383 case IIO_CHAN_INFO_OFFSET
:
1385 * DFSDM output data are in the range [-2^n, 2^n],
1386 * with n = realbits - 1.
1387 * - Differential modulator:
1388 * Offset correspond to SD modulator offset.
1389 * - Single ended modulator:
1390 * Input is in [0V, Vref] range,
1391 * where 0V corresponds to -2^n, and Vref to 2^n.
1392 * Add 2^n to offset. (i.e. middle of input range)
1393 * offset = offset(sd) * vref / res(sd) * max / vref.
1396 ret
= iio_backend_read_offset(adc
->backend
[idx
], chan
, val
, NULL
);
1400 *val
= div_u64((u64
)max
* *val
, BIT(*val2
- 1));
1401 if (!chan
->differential
)
1410 static int stm32_dfsdm_validate_trigger(struct iio_dev
*indio_dev
,
1411 struct iio_trigger
*trig
)
1413 return stm32_dfsdm_get_jextsel(indio_dev
, trig
) < 0 ? -EINVAL
: 0;
1416 static const struct iio_info stm32_dfsdm_info_audio
= {
1417 .hwfifo_set_watermark
= stm32_dfsdm_set_watermark
,
1418 .read_raw
= stm32_dfsdm_read_raw
,
1419 .write_raw
= stm32_dfsdm_write_raw
,
1420 .update_scan_mode
= stm32_dfsdm_update_scan_mode
,
1423 static const struct iio_info stm32_dfsdm_info_adc
= {
1424 .hwfifo_set_watermark
= stm32_dfsdm_set_watermark
,
1425 .read_raw
= stm32_dfsdm_read_raw
,
1426 .write_raw
= stm32_dfsdm_write_raw
,
1427 .update_scan_mode
= stm32_dfsdm_update_scan_mode
,
1428 .validate_trigger
= stm32_dfsdm_validate_trigger
,
1431 static irqreturn_t
stm32_dfsdm_irq(int irq
, void *arg
)
1433 struct iio_dev
*indio_dev
= arg
;
1434 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
1435 struct regmap
*regmap
= adc
->dfsdm
->regmap
;
1436 unsigned int status
, int_en
;
1438 regmap_read(regmap
, DFSDM_ISR(adc
->fl_id
), &status
);
1439 regmap_read(regmap
, DFSDM_CR2(adc
->fl_id
), &int_en
);
1441 if (status
& DFSDM_ISR_REOCF_MASK
) {
1442 /* Read the data register clean the IRQ status */
1443 regmap_read(regmap
, DFSDM_RDATAR(adc
->fl_id
), adc
->buffer
);
1444 complete(&adc
->completion
);
1447 if (status
& DFSDM_ISR_ROVRF_MASK
) {
1448 if (int_en
& DFSDM_CR2_ROVRIE_MASK
)
1449 dev_warn(&indio_dev
->dev
, "Overrun detected\n");
1450 regmap_set_bits(regmap
, DFSDM_ICR(adc
->fl_id
),
1451 DFSDM_ICR_CLRROVRF_MASK
);
1458 * Define external info for SPI Frequency and audio sampling rate that can be
1459 * configured by ASoC driver through consumer.h API
1461 static const struct iio_chan_spec_ext_info dfsdm_adc_audio_ext_info
[] = {
1462 /* spi_clk_freq : clock freq on SPI/manchester bus used by channel */
1464 .name
= "spi_clk_freq",
1465 .shared
= IIO_SHARED_BY_TYPE
,
1466 .read
= dfsdm_adc_audio_get_spiclk
,
1467 .write
= dfsdm_adc_audio_set_spiclk
,
1472 static void stm32_dfsdm_dma_release(struct iio_dev
*indio_dev
)
1474 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
1476 if (adc
->dma_chan
) {
1477 dma_free_coherent(adc
->dma_chan
->device
->dev
,
1478 DFSDM_DMA_BUFFER_SIZE
,
1479 adc
->rx_buf
, adc
->dma_buf
);
1480 dma_release_channel(adc
->dma_chan
);
1484 static int stm32_dfsdm_dma_request(struct device
*dev
,
1485 struct iio_dev
*indio_dev
)
1487 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
1489 adc
->dma_chan
= dma_request_chan(dev
, "rx");
1490 if (IS_ERR(adc
->dma_chan
)) {
1491 int ret
= PTR_ERR(adc
->dma_chan
);
1493 adc
->dma_chan
= NULL
;
1497 adc
->rx_buf
= dma_alloc_coherent(adc
->dma_chan
->device
->dev
,
1498 DFSDM_DMA_BUFFER_SIZE
,
1499 &adc
->dma_buf
, GFP_KERNEL
);
1501 dma_release_channel(adc
->dma_chan
);
1505 indio_dev
->modes
|= INDIO_BUFFER_SOFTWARE
;
1506 indio_dev
->setup_ops
= &stm32_dfsdm_buffer_setup_ops
;
1511 static int stm32_dfsdm_adc_chan_init_one(struct iio_dev
*indio_dev
, struct iio_chan_spec
*ch
,
1512 struct fwnode_handle
*child
)
1514 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
1518 ret
= stm32_dfsdm_generic_channel_parse_of(adc
->dfsdm
, indio_dev
, ch
, child
);
1519 else /* Legacy binding */
1520 ret
= stm32_dfsdm_channel_parse_of(adc
->dfsdm
, indio_dev
, ch
);
1522 return dev_err_probe(&indio_dev
->dev
, ret
, "Failed to parse channel\n");
1524 ch
->type
= IIO_VOLTAGE
;
1528 * IIO_CHAN_INFO_RAW: used to compute regular conversion
1529 * IIO_CHAN_INFO_OVERSAMPLING_RATIO: used to set oversampling
1532 ch
->info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1533 BIT(IIO_CHAN_INFO_SCALE
) |
1534 BIT(IIO_CHAN_INFO_OFFSET
);
1536 /* Legacy. Scaling not supported */
1537 ch
->info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
);
1540 ch
->info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
) |
1541 BIT(IIO_CHAN_INFO_SAMP_FREQ
);
1543 if (adc
->dev_data
->type
== DFSDM_AUDIO
) {
1544 ch
->ext_info
= dfsdm_adc_audio_ext_info
;
1547 ch
->scan_type
.shift
= 8;
1549 ch
->scan_type
.sign
= 's';
1550 ch
->scan_type
.realbits
= 24;
1551 ch
->scan_type
.storagebits
= 32;
1553 return stm32_dfsdm_chan_configure(adc
->dfsdm
,
1554 &adc
->dfsdm
->ch_list
[ch
->channel
]);
1557 static int stm32_dfsdm_chan_init(struct iio_dev
*indio_dev
, struct iio_chan_spec
*channels
)
1559 int num_ch
= indio_dev
->num_channels
;
1563 for (chan_idx
= 0; chan_idx
< num_ch
; chan_idx
++) {
1564 channels
[chan_idx
].scan_index
= chan_idx
;
1565 ret
= stm32_dfsdm_adc_chan_init_one(indio_dev
, &channels
[chan_idx
], NULL
);
1567 return dev_err_probe(&indio_dev
->dev
, ret
, "Channels init failed\n");
1573 static int stm32_dfsdm_generic_chan_init(struct iio_dev
*indio_dev
, struct iio_chan_spec
*channels
)
1575 int chan_idx
= 0, ret
;
1577 device_for_each_child_node_scoped(&indio_dev
->dev
, child
) {
1578 /* Skip DAI node in DFSDM audio nodes */
1579 if (fwnode_property_present(child
, "compatible"))
1582 channels
[chan_idx
].scan_index
= chan_idx
;
1583 ret
= stm32_dfsdm_adc_chan_init_one(indio_dev
, &channels
[chan_idx
], child
);
1585 return dev_err_probe(&indio_dev
->dev
, ret
, "Channels init failed\n");
1593 static int stm32_dfsdm_audio_init(struct device
*dev
, struct iio_dev
*indio_dev
)
1595 struct iio_chan_spec
*ch
;
1596 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
1597 struct stm32_dfsdm_channel
*d_ch
;
1598 bool legacy
= false;
1601 /* If st,adc-channels is defined legacy binding is used. Else assume generic binding. */
1602 num_ch
= of_property_count_u32_elems(indio_dev
->dev
.of_node
, "st,adc-channels");
1606 ch
= devm_kzalloc(&indio_dev
->dev
, sizeof(*ch
), GFP_KERNEL
);
1610 indio_dev
->num_channels
= 1;
1611 indio_dev
->channels
= ch
;
1614 ret
= stm32_dfsdm_chan_init(indio_dev
, ch
);
1616 ret
= stm32_dfsdm_generic_chan_init(indio_dev
, ch
);
1619 dev_err(&indio_dev
->dev
, "Channels init failed\n");
1622 ch
->info_mask_separate
= BIT(IIO_CHAN_INFO_SAMP_FREQ
);
1624 d_ch
= &adc
->dfsdm
->ch_list
[ch
->channel
];
1625 if (d_ch
->src
!= DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL
)
1626 adc
->spi_freq
= adc
->dfsdm
->spi_master_freq
;
1628 return stm32_dfsdm_dma_request(dev
, indio_dev
);
1631 static int stm32_dfsdm_adc_init(struct device
*dev
, struct iio_dev
*indio_dev
)
1633 struct iio_chan_spec
*ch
;
1634 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
1636 bool legacy
= false;
1638 adc
->oversamp
= DFSDM_DEFAULT_OVERSAMPLING
;
1639 ret
= stm32_dfsdm_compute_all_osrs(indio_dev
, adc
->oversamp
);
1643 num_ch
= device_get_child_node_count(&indio_dev
->dev
);
1645 /* No channels nodes found. Assume legacy binding */
1646 num_ch
= of_property_count_u32_elems(indio_dev
->dev
.of_node
, "st,adc-channels");
1648 dev_err(&indio_dev
->dev
, "Bad st,adc-channels\n");
1655 if (num_ch
> adc
->dfsdm
->num_chs
) {
1656 dev_err(&indio_dev
->dev
, "Number of channel [%d] exceeds [%d]\n",
1657 num_ch
, adc
->dfsdm
->num_chs
);
1660 indio_dev
->num_channels
= num_ch
;
1663 /* Bind to SD modulator IIO device. */
1664 adc
->hwc
= devm_iio_hw_consumer_alloc(&indio_dev
->dev
);
1665 if (IS_ERR(adc
->hwc
))
1666 return dev_err_probe(&indio_dev
->dev
, -EPROBE_DEFER
,
1667 "waiting for SD modulator\n");
1669 /* Generic binding. SD modulator IIO device not used. Use SD modulator backend. */
1672 adc
->backend
= devm_kcalloc(&indio_dev
->dev
, num_ch
, sizeof(*adc
->backend
),
1678 ch
= devm_kcalloc(&indio_dev
->dev
, num_ch
, sizeof(*ch
), GFP_KERNEL
);
1681 indio_dev
->channels
= ch
;
1684 ret
= stm32_dfsdm_chan_init(indio_dev
, ch
);
1686 ret
= stm32_dfsdm_generic_chan_init(indio_dev
, ch
);
1690 init_completion(&adc
->completion
);
1692 /* Optionally request DMA */
1693 ret
= stm32_dfsdm_dma_request(dev
, indio_dev
);
1696 return dev_err_probe(dev
, ret
,
1697 "DMA channel request failed with\n");
1699 dev_dbg(dev
, "No DMA support\n");
1703 ret
= iio_triggered_buffer_setup(indio_dev
,
1704 &iio_pollfunc_store_time
, NULL
,
1705 &stm32_dfsdm_buffer_setup_ops
);
1707 stm32_dfsdm_dma_release(indio_dev
);
1708 dev_err(&indio_dev
->dev
, "buffer setup failed\n");
1712 /* lptimer/timer hardware triggers */
1713 indio_dev
->modes
|= INDIO_HARDWARE_TRIGGERED
;
1718 static const struct stm32_dfsdm_dev_data stm32h7_dfsdm_adc_data
= {
1720 .init
= stm32_dfsdm_adc_init
,
1723 static const struct stm32_dfsdm_dev_data stm32h7_dfsdm_audio_data
= {
1724 .type
= DFSDM_AUDIO
,
1725 .init
= stm32_dfsdm_audio_init
,
1728 static const struct of_device_id stm32_dfsdm_adc_match
[] = {
1730 .compatible
= "st,stm32-dfsdm-adc",
1731 .data
= &stm32h7_dfsdm_adc_data
,
1734 .compatible
= "st,stm32-dfsdm-dmic",
1735 .data
= &stm32h7_dfsdm_audio_data
,
1739 MODULE_DEVICE_TABLE(of
, stm32_dfsdm_adc_match
);
1741 static int stm32_dfsdm_adc_probe(struct platform_device
*pdev
)
1743 struct device
*dev
= &pdev
->dev
;
1744 struct stm32_dfsdm_adc
*adc
;
1745 struct device_node
*np
= dev
->of_node
;
1746 const struct stm32_dfsdm_dev_data
*dev_data
;
1747 struct iio_dev
*iio
;
1751 dev_data
= of_device_get_match_data(dev
);
1752 iio
= devm_iio_device_alloc(dev
, sizeof(*adc
));
1754 dev_err(dev
, "%s: Failed to allocate IIO\n", __func__
);
1758 adc
= iio_priv(iio
);
1759 adc
->dfsdm
= dev_get_drvdata(dev
->parent
);
1761 iio
->dev
.of_node
= np
;
1762 iio
->modes
= INDIO_DIRECT_MODE
;
1764 platform_set_drvdata(pdev
, iio
);
1766 ret
= of_property_read_u32(dev
->of_node
, "reg", &adc
->fl_id
);
1767 if (ret
!= 0 || adc
->fl_id
>= adc
->dfsdm
->num_fls
) {
1768 dev_err(dev
, "Missing or bad reg property\n");
1772 name
= devm_kzalloc(dev
, sizeof("dfsdm-adc0"), GFP_KERNEL
);
1775 if (dev_data
->type
== DFSDM_AUDIO
) {
1776 iio
->info
= &stm32_dfsdm_info_audio
;
1777 snprintf(name
, sizeof("dfsdm-pdm0"), "dfsdm-pdm%d", adc
->fl_id
);
1779 iio
->info
= &stm32_dfsdm_info_adc
;
1780 snprintf(name
, sizeof("dfsdm-adc0"), "dfsdm-adc%d", adc
->fl_id
);
1785 * In a first step IRQs generated for channels are not treated.
1786 * So IRQ associated to filter instance 0 is dedicated to the Filter 0.
1788 irq
= platform_get_irq(pdev
, 0);
1792 ret
= devm_request_irq(dev
, irq
, stm32_dfsdm_irq
,
1793 0, pdev
->name
, iio
);
1795 dev_err(dev
, "Failed to request IRQ\n");
1799 ret
= of_property_read_u32(dev
->of_node
, "st,filter-order", &val
);
1801 dev_err(dev
, "Failed to set filter order\n");
1805 adc
->dfsdm
->fl_list
[adc
->fl_id
].ford
= val
;
1807 ret
= of_property_read_u32(dev
->of_node
, "st,filter0-sync", &val
);
1809 adc
->dfsdm
->fl_list
[adc
->fl_id
].sync_mode
= val
;
1811 adc
->dev_data
= dev_data
;
1812 ret
= dev_data
->init(dev
, iio
);
1816 ret
= iio_device_register(iio
);
1820 if (dev_data
->type
== DFSDM_AUDIO
) {
1821 ret
= of_platform_populate(np
, NULL
, NULL
, dev
);
1823 dev_err(dev
, "Failed to find an audio DAI\n");
1824 goto err_unregister
;
1831 iio_device_unregister(iio
);
1833 stm32_dfsdm_dma_release(iio
);
1838 static void stm32_dfsdm_adc_remove(struct platform_device
*pdev
)
1840 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
1841 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
1843 if (adc
->dev_data
->type
== DFSDM_AUDIO
)
1844 of_platform_depopulate(&pdev
->dev
);
1845 iio_device_unregister(indio_dev
);
1846 stm32_dfsdm_dma_release(indio_dev
);
1849 static int stm32_dfsdm_adc_suspend(struct device
*dev
)
1851 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1853 if (iio_buffer_enabled(indio_dev
))
1854 stm32_dfsdm_predisable(indio_dev
);
1859 static int stm32_dfsdm_adc_resume(struct device
*dev
)
1861 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1862 struct stm32_dfsdm_adc
*adc
= iio_priv(indio_dev
);
1863 const struct iio_chan_spec
*chan
;
1864 struct stm32_dfsdm_channel
*ch
;
1867 /* restore channels configuration */
1868 for (i
= 0; i
< indio_dev
->num_channels
; i
++) {
1869 chan
= indio_dev
->channels
+ i
;
1870 ch
= &adc
->dfsdm
->ch_list
[chan
->channel
];
1871 ret
= stm32_dfsdm_chan_configure(adc
->dfsdm
, ch
);
1876 if (iio_buffer_enabled(indio_dev
))
1877 stm32_dfsdm_postenable(indio_dev
);
1882 static DEFINE_SIMPLE_DEV_PM_OPS(stm32_dfsdm_adc_pm_ops
,
1883 stm32_dfsdm_adc_suspend
,
1884 stm32_dfsdm_adc_resume
);
1886 static struct platform_driver stm32_dfsdm_adc_driver
= {
1888 .name
= "stm32-dfsdm-adc",
1889 .of_match_table
= stm32_dfsdm_adc_match
,
1890 .pm
= pm_sleep_ptr(&stm32_dfsdm_adc_pm_ops
),
1892 .probe
= stm32_dfsdm_adc_probe
,
1893 .remove
= stm32_dfsdm_adc_remove
,
1895 module_platform_driver(stm32_dfsdm_adc_driver
);
1897 MODULE_DESCRIPTION("STM32 sigma delta ADC");
1898 MODULE_AUTHOR("Arnaud Pouliquen <arnaud.pouliquen@st.com>");
1899 MODULE_LICENSE("GPL v2");
1900 MODULE_IMPORT_NS("IIO_BACKEND");