treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / iio / adc / stm32-dfsdm-adc.c
blob2aad2cda6943e1bd1e17340b38c64ba20c1d195c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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>.
7 */
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 | \
56 DFSDM_CR1_JSCAN_MASK)
58 enum sd_converter_type {
59 DFSDM_AUDIO,
60 DFSDM_IIO,
63 struct stm32_dfsdm_dev_data {
64 int type;
65 int (*init)(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;
73 unsigned int fl_id;
74 unsigned int nconv;
75 unsigned long smask;
77 /* ADC specific */
78 unsigned int oversamp;
79 struct iio_hw_consumer *hwc;
80 struct completion completion;
81 u32 *buffer;
83 /* Audio specific */
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);
87 void *cb_priv;
89 /* DMA */
90 u8 *rx_buf;
91 unsigned int bufi; /* Buffer current position */
92 unsigned int buf_sz; /* Buffer size */
93 struct dma_chan *dma_chan;
94 dma_addr_t dma_buf;
97 struct stm32_dfsdm_str2field {
98 const char *name;
99 unsigned int val;
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))
131 return p->val;
133 return -EINVAL;
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 {
142 const char *name;
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[] = {
155 { TIM1_TRGO, 0 },
156 { TIM1_TRGO2, 1 },
157 { TIM8_TRGO, 2 },
158 { TIM8_TRGO2, 3 },
159 { TIM3_TRGO, 4 },
160 { TIM4_TRGO, 5 },
161 { TIM16_OC1, 6 },
162 { TIM6_TRGO, 7 },
163 { TIM7_TRGO, 8 },
164 { LPTIM1_OUT, 26 },
165 { LPTIM2_OUT, 27 },
166 { LPTIM3_OUT, 28 },
170 static int stm32_dfsdm_get_jextsel(struct iio_dev *indio_dev,
171 struct iio_trigger *trig)
173 int i;
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;
188 return -EINVAL;
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;
195 u64 res, max;
196 int bits, shift;
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.
207 * ford: filter order
208 * fosr: filter over sampling ratio
209 * iosr: integrator over sampling ratio
211 if (fl->ford == DFSDM_FASTSINC_ORDER) {
212 m = 2;
213 p = 2;
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++) {
222 if (fast)
223 d = fosr * iosr;
224 else if (fl->ford == DFSDM_FASTSINC_ORDER)
225 d = fosr * (iosr + 3) + 2;
226 else
227 d = fosr * (iosr - 1 + p) + p;
229 if (d > oversamp)
230 break;
231 else if (d != oversamp)
232 continue;
234 * Check resolution (limited to signed 32 bits)
235 * res <= 2^31
236 * Sincx filters:
237 * res = m * fosr^p x iosr (with m=1, p=ford)
238 * FastSinc filter
239 * res = m * fosr^p x iosr (with m=2, p=2)
241 res = fosr;
242 for (i = p - 1; i > 0; i--) {
243 res = res * (u64)fosr;
244 if (res > DFSDM_DATA_MAX)
245 break;
247 if (res > DFSDM_DATA_MAX)
248 continue;
250 res = res * (u64)m * (u64)iosr;
251 if (res > DFSDM_DATA_MAX)
252 continue;
254 if (res >= flo->res) {
255 flo->res = res;
256 flo->fosr = fosr;
257 flo->iosr = iosr;
259 bits = fls(flo->res);
260 /* 8 LBSs in data register contain chan info */
261 max = flo->res << 8;
263 /* if resolution is not a power of two */
264 if (flo->res > BIT(bits - 1))
265 bits++;
266 else
267 max--;
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
276 if (shift > 0) {
277 /* Resolution is lower than 24 bits */
278 flo->rshift = 0;
279 flo->lshift = shift;
280 } else {
282 * If resolution is 24 bits or more,
283 * max positive value may be ambiguous
284 * (equal to max negative value as sign
285 * bit is dropped).
286 * Reduce resolution to 23 bits (rshift)
287 * to keep the sign on bit 23 and treat
288 * saturation before rescaling on 24
289 * bits (lshift).
291 flo->rshift = 1 - shift;
292 flo->lshift = 1;
293 max >>= flo->rshift;
295 flo->max = (s32)max;
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,
300 flo->lshift);
305 if (!flo->res)
306 return -EINVAL;
308 return 0;
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];
316 int ret0, ret1;
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",
326 ret0, ret1);
327 return -EINVAL;
330 return 0;
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;
338 unsigned int bit;
339 int ret;
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));
346 if (ret < 0)
347 return ret;
350 return 0;
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;
358 unsigned int bit;
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;
373 int ret;
375 ret = regmap_update_bits(regmap, DFSDM_CHCFGR1(id),
376 DFSDM_CHCFGR1_SITP_MASK,
377 DFSDM_CHCFGR1_SITP(ch->type));
378 if (ret < 0)
379 return ret;
380 ret = regmap_update_bits(regmap, DFSDM_CHCFGR1(id),
381 DFSDM_CHCFGR1_SPICKSEL_MASK,
382 DFSDM_CHCFGR1_SPICKSEL(ch->src));
383 if (ret < 0)
384 return ret;
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,
391 unsigned int fl_id,
392 struct iio_trigger *trig)
394 struct stm32_dfsdm *dfsdm = adc->dfsdm;
395 int ret;
397 /* Enable filter */
398 ret = regmap_update_bits(dfsdm->regmap, DFSDM_CR1(fl_id),
399 DFSDM_CR1_DFEN_MASK, DFSDM_CR1_DFEN(1));
400 if (ret < 0)
401 return ret;
403 /* Nothing more to do for injected (scan mode/triggered) conversions */
404 if (adc->nconv > 1 || trig)
405 return 0;
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,
414 unsigned int fl_id)
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,
422 unsigned int fl_id,
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;
428 int ret;
430 if (trig) {
431 ret = stm32_dfsdm_get_jextsel(indio_dev, trig);
432 if (ret < 0)
433 return ret;
435 /* set trigger source and polarity (default to rising edge) */
436 jextsel = ret;
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));
444 if (ret < 0)
445 return ret;
447 return 0;
450 static int stm32_dfsdm_channels_configure(struct stm32_dfsdm_adc *adc,
451 unsigned int fl_id,
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;
459 unsigned int bit;
460 int ret;
462 fl->fast = 0;
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) {
471 fl->fast = 1;
472 flo = &fl->flo[1];
476 if (!flo->res)
477 return -EINVAL;
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));
487 if (ret)
488 return ret;
491 return 0;
494 static int stm32_dfsdm_filter_configure(struct stm32_dfsdm_adc *adc,
495 unsigned int fl_id,
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];
502 u32 cr1;
503 const struct iio_chan_spec *chan;
504 unsigned int bit, jchg = 0;
505 int ret;
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));
510 if (ret)
511 return ret;
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));
516 if (ret)
517 return ret;
519 ret = regmap_update_bits(regmap, DFSDM_FCR(fl_id), DFSDM_FCR_FORD_MASK,
520 DFSDM_FCR_FORD(fl->ford));
521 if (ret)
522 return ret;
524 ret = stm32_dfsdm_filter_set_trig(adc, fl_id, trig);
525 if (ret)
526 return ret;
528 ret = regmap_update_bits(regmap, DFSDM_CR1(fl_id),
529 DFSDM_CR1_FAST_MASK,
530 DFSDM_CR1_FAST(fl->fast));
531 if (ret)
532 return ret;
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 | | | |
541 * (1 chan) | | | | |
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);
565 } else {
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);
573 if (ret < 0)
574 return ret;
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,
581 * either use:
582 * - conversions in sync with filter 0
583 * - triggered conversions
585 if (!fl->sync_mode && !trig)
586 return -EINVAL;
587 cr1 |= DFSDM_CR1_JSYNC(fl->sync_mode);
590 return regmap_update_bits(regmap, DFSDM_CR1(fl_id), DFSDM_CR1_CFG_MASK,
591 cr1);
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;
599 const char *of_str;
600 int chan_idx = ch->scan_index;
601 int ret, val;
603 ret = of_property_read_u32_index(indio_dev->dev.of_node,
604 "st,adc-channels", chan_idx,
605 &ch->channel);
606 if (ret < 0) {
607 dev_err(&indio_dev->dev,
608 " Error parsing 'st,adc-channels' for idx %d\n",
609 chan_idx);
610 return ret;
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);
616 return -EINVAL;
619 ret = of_property_read_string_index(indio_dev->dev.of_node,
620 "st,adc-channel-names", chan_idx,
621 &ch->datasheet_name);
622 if (ret < 0) {
623 dev_err(&indio_dev->dev,
624 " Error parsing 'st,adc-channel-names' for idx %d\n",
625 chan_idx);
626 return ret;
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,
634 &of_str);
635 if (!ret) {
636 val = stm32_dfsdm_str2val(of_str, stm32_dfsdm_chan_type);
637 if (val < 0)
638 return val;
639 } else {
640 val = 0;
642 df_ch->type = val;
644 ret = of_property_read_string_index(indio_dev->dev.of_node,
645 "st,adc-channel-clk-src", chan_idx,
646 &of_str);
647 if (!ret) {
648 val = stm32_dfsdm_str2val(of_str, stm32_dfsdm_chan_src);
649 if (val < 0)
650 return val;
651 } else {
652 val = 0;
654 df_ch->src = val;
656 ret = of_property_read_u32_index(indio_dev->dev.of_node,
657 "st,adc-alt-channel", chan_idx,
658 &df_ch->alt_si);
659 if (ret < 0)
660 df_ch->alt_si = 0;
662 return 0;
665 static ssize_t dfsdm_adc_audio_get_spiclk(struct iio_dev *indio_dev,
666 uintptr_t priv,
667 const struct iio_chan_spec *chan,
668 char *buf)
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;
681 int ret;
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);
690 if (ret < 0)
691 return ret;
693 adc->sample_freq = spi_freq / oversamp;
694 adc->oversamp = oversamp;
696 return 0;
699 static ssize_t dfsdm_adc_audio_set_spiclk(struct iio_dev *indio_dev,
700 uintptr_t priv,
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;
708 int ret;
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)
713 return -EPERM;
715 ret = kstrtoint(buf, 0, &spi_freq);
716 if (ret)
717 return ret;
719 if (!spi_freq)
720 return -EINVAL;
722 if (sample_freq) {
723 ret = dfsdm_adc_set_samp_freq(indio_dev, sample_freq, spi_freq);
724 if (ret < 0)
725 return ret;
727 adc->spi_freq = spi_freq;
729 return len;
732 static int stm32_dfsdm_start_conv(struct stm32_dfsdm_adc *adc,
733 struct iio_trigger *trig)
735 struct regmap *regmap = adc->dfsdm->regmap;
736 int ret;
738 ret = stm32_dfsdm_channels_configure(adc, adc->fl_id, trig);
739 if (ret < 0)
740 return ret;
742 ret = stm32_dfsdm_start_channel(adc);
743 if (ret < 0)
744 return ret;
746 ret = stm32_dfsdm_filter_configure(adc, adc->fl_id, trig);
747 if (ret < 0)
748 goto stop_channels;
750 ret = stm32_dfsdm_start_filter(adc, adc->fl_id, trig);
751 if (ret < 0)
752 goto filter_unconfigure;
754 return 0;
756 filter_unconfigure:
757 regmap_update_bits(regmap, DFSDM_CR1(adc->fl_id),
758 DFSDM_CR1_CFG_MASK, 0);
759 stop_channels:
760 stm32_dfsdm_stop_channel(adc);
762 return ret;
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,
778 unsigned int val)
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.
786 * There should be :
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);
793 return 0;
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,
803 &state);
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;
807 unsigned int size;
809 /* Return available bytes */
810 if (i >= adc->bufi)
811 size = i - adc->bufi;
812 else
813 size = adc->buf_sz + i - adc->bufi;
815 return size;
818 return 0;
821 static inline void stm32_dfsdm_process_data(struct stm32_dfsdm_adc *adc,
822 s32 *buffer)
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;
827 s32 *ptr = buffer;
829 while (i--) {
830 /* Mask 8 LSB that contains the channel ID */
831 *ptr &= 0xFFFFFF00;
832 /* Convert 2^(n-1) sample to 2^(n-1)-1 to avoid wrap-around */
833 if (*ptr > flo->max)
834 *ptr -= 1;
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;
841 ptr++;
845 static irqreturn_t stm32_dfsdm_adc_trigger_handler(int irq, void *p)
847 struct iio_poll_func *pf = p;
848 struct iio_dev *indio_dev = pf->indio_dev;
849 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
850 int available = stm32_dfsdm_adc_dma_residue(adc);
852 while (available >= indio_dev->scan_bytes) {
853 s32 *buffer = (s32 *)&adc->rx_buf[adc->bufi];
855 stm32_dfsdm_process_data(adc, buffer);
857 iio_push_to_buffers_with_timestamp(indio_dev, buffer,
858 pf->timestamp);
859 available -= indio_dev->scan_bytes;
860 adc->bufi += indio_dev->scan_bytes;
861 if (adc->bufi >= adc->buf_sz)
862 adc->bufi = 0;
865 iio_trigger_notify_done(indio_dev->trig);
867 return IRQ_HANDLED;
870 static void stm32_dfsdm_dma_buffer_done(void *data)
872 struct iio_dev *indio_dev = data;
873 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
874 int available = stm32_dfsdm_adc_dma_residue(adc);
875 size_t old_pos;
877 if (indio_dev->currentmode & INDIO_BUFFER_TRIGGERED) {
878 iio_trigger_poll_chained(indio_dev->trig);
879 return;
883 * FIXME: In Kernel interface does not support cyclic DMA buffer,and
884 * offers only an interface to push data samples per samples.
885 * For this reason IIO buffer interface is not used and interface is
886 * bypassed using a private callback registered by ASoC.
887 * This should be a temporary solution waiting a cyclic DMA engine
888 * support in IIO.
891 dev_dbg(&indio_dev->dev, "%s: pos = %d, available = %d\n", __func__,
892 adc->bufi, available);
893 old_pos = adc->bufi;
895 while (available >= indio_dev->scan_bytes) {
896 s32 *buffer = (s32 *)&adc->rx_buf[adc->bufi];
898 stm32_dfsdm_process_data(adc, buffer);
900 available -= indio_dev->scan_bytes;
901 adc->bufi += indio_dev->scan_bytes;
902 if (adc->bufi >= adc->buf_sz) {
903 if (adc->cb)
904 adc->cb(&adc->rx_buf[old_pos],
905 adc->buf_sz - old_pos, adc->cb_priv);
906 adc->bufi = 0;
907 old_pos = 0;
909 /* regular iio buffer without trigger */
910 if (adc->dev_data->type == DFSDM_IIO)
911 iio_push_to_buffers(indio_dev, buffer);
913 if (adc->cb)
914 adc->cb(&adc->rx_buf[old_pos], adc->bufi - old_pos,
915 adc->cb_priv);
918 static int stm32_dfsdm_adc_dma_start(struct iio_dev *indio_dev)
920 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
922 * The DFSDM supports half-word transfers. However, for 16 bits record,
923 * 4 bytes buswidth is kept, to avoid losing samples LSBs when left
924 * shift is required.
926 struct dma_slave_config config = {
927 .src_addr = (dma_addr_t)adc->dfsdm->phys_base,
928 .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
930 struct dma_async_tx_descriptor *desc;
931 dma_cookie_t cookie;
932 int ret;
934 if (!adc->dma_chan)
935 return -EINVAL;
937 dev_dbg(&indio_dev->dev, "%s size=%d watermark=%d\n", __func__,
938 adc->buf_sz, adc->buf_sz / 2);
940 if (adc->nconv == 1 && !indio_dev->trig)
941 config.src_addr += DFSDM_RDATAR(adc->fl_id);
942 else
943 config.src_addr += DFSDM_JDATAR(adc->fl_id);
944 ret = dmaengine_slave_config(adc->dma_chan, &config);
945 if (ret)
946 return ret;
948 /* Prepare a DMA cyclic transaction */
949 desc = dmaengine_prep_dma_cyclic(adc->dma_chan,
950 adc->dma_buf,
951 adc->buf_sz, adc->buf_sz / 2,
952 DMA_DEV_TO_MEM,
953 DMA_PREP_INTERRUPT);
954 if (!desc)
955 return -EBUSY;
957 desc->callback = stm32_dfsdm_dma_buffer_done;
958 desc->callback_param = indio_dev;
960 cookie = dmaengine_submit(desc);
961 ret = dma_submit_error(cookie);
962 if (ret)
963 goto err_stop_dma;
965 /* Issue pending DMA requests */
966 dma_async_issue_pending(adc->dma_chan);
968 if (adc->nconv == 1 && !indio_dev->trig) {
969 /* Enable regular DMA transfer*/
970 ret = regmap_update_bits(adc->dfsdm->regmap,
971 DFSDM_CR1(adc->fl_id),
972 DFSDM_CR1_RDMAEN_MASK,
973 DFSDM_CR1_RDMAEN_MASK);
974 } else {
975 /* Enable injected DMA transfer*/
976 ret = regmap_update_bits(adc->dfsdm->regmap,
977 DFSDM_CR1(adc->fl_id),
978 DFSDM_CR1_JDMAEN_MASK,
979 DFSDM_CR1_JDMAEN_MASK);
982 if (ret < 0)
983 goto err_stop_dma;
985 return 0;
987 err_stop_dma:
988 dmaengine_terminate_all(adc->dma_chan);
990 return ret;
993 static void stm32_dfsdm_adc_dma_stop(struct iio_dev *indio_dev)
995 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
997 if (!adc->dma_chan)
998 return;
1000 regmap_update_bits(adc->dfsdm->regmap, DFSDM_CR1(adc->fl_id),
1001 DFSDM_CR1_RDMAEN_MASK | DFSDM_CR1_JDMAEN_MASK, 0);
1002 dmaengine_terminate_all(adc->dma_chan);
1005 static int stm32_dfsdm_update_scan_mode(struct iio_dev *indio_dev,
1006 const unsigned long *scan_mask)
1008 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
1010 adc->nconv = bitmap_weight(scan_mask, indio_dev->masklength);
1011 adc->smask = *scan_mask;
1013 dev_dbg(&indio_dev->dev, "nconv=%d mask=%lx\n", adc->nconv, *scan_mask);
1015 return 0;
1018 static int __stm32_dfsdm_postenable(struct iio_dev *indio_dev)
1020 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
1021 int ret;
1023 /* Reset adc buffer index */
1024 adc->bufi = 0;
1026 if (adc->hwc) {
1027 ret = iio_hw_consumer_enable(adc->hwc);
1028 if (ret < 0)
1029 return ret;
1032 ret = stm32_dfsdm_start_dfsdm(adc->dfsdm);
1033 if (ret < 0)
1034 goto err_stop_hwc;
1036 ret = stm32_dfsdm_adc_dma_start(indio_dev);
1037 if (ret) {
1038 dev_err(&indio_dev->dev, "Can't start DMA\n");
1039 goto stop_dfsdm;
1042 ret = stm32_dfsdm_start_conv(adc, indio_dev->trig);
1043 if (ret) {
1044 dev_err(&indio_dev->dev, "Can't start conversion\n");
1045 goto err_stop_dma;
1048 return 0;
1050 err_stop_dma:
1051 stm32_dfsdm_adc_dma_stop(indio_dev);
1052 stop_dfsdm:
1053 stm32_dfsdm_stop_dfsdm(adc->dfsdm);
1054 err_stop_hwc:
1055 if (adc->hwc)
1056 iio_hw_consumer_disable(adc->hwc);
1058 return ret;
1061 static int stm32_dfsdm_postenable(struct iio_dev *indio_dev)
1063 int ret;
1065 if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
1066 ret = iio_triggered_buffer_postenable(indio_dev);
1067 if (ret < 0)
1068 return ret;
1071 ret = __stm32_dfsdm_postenable(indio_dev);
1072 if (ret < 0)
1073 goto err_predisable;
1075 return 0;
1077 err_predisable:
1078 if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED)
1079 iio_triggered_buffer_predisable(indio_dev);
1081 return ret;
1084 static void __stm32_dfsdm_predisable(struct iio_dev *indio_dev)
1086 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
1088 stm32_dfsdm_stop_conv(adc);
1090 stm32_dfsdm_adc_dma_stop(indio_dev);
1092 stm32_dfsdm_stop_dfsdm(adc->dfsdm);
1094 if (adc->hwc)
1095 iio_hw_consumer_disable(adc->hwc);
1098 static int stm32_dfsdm_predisable(struct iio_dev *indio_dev)
1100 __stm32_dfsdm_predisable(indio_dev);
1102 if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED)
1103 iio_triggered_buffer_predisable(indio_dev);
1105 return 0;
1108 static const struct iio_buffer_setup_ops stm32_dfsdm_buffer_setup_ops = {
1109 .postenable = &stm32_dfsdm_postenable,
1110 .predisable = &stm32_dfsdm_predisable,
1114 * stm32_dfsdm_get_buff_cb() - register a callback that will be called when
1115 * DMA transfer period is achieved.
1117 * @iio_dev: Handle to IIO device.
1118 * @cb: Pointer to callback function:
1119 * - data: pointer to data buffer
1120 * - size: size in byte of the data buffer
1121 * - private: pointer to consumer private structure.
1122 * @private: Pointer to consumer private structure.
1124 int stm32_dfsdm_get_buff_cb(struct iio_dev *iio_dev,
1125 int (*cb)(const void *data, size_t size,
1126 void *private),
1127 void *private)
1129 struct stm32_dfsdm_adc *adc;
1131 if (!iio_dev)
1132 return -EINVAL;
1133 adc = iio_priv(iio_dev);
1135 adc->cb = cb;
1136 adc->cb_priv = private;
1138 return 0;
1140 EXPORT_SYMBOL_GPL(stm32_dfsdm_get_buff_cb);
1143 * stm32_dfsdm_release_buff_cb - unregister buffer callback
1145 * @iio_dev: Handle to IIO device.
1147 int stm32_dfsdm_release_buff_cb(struct iio_dev *iio_dev)
1149 struct stm32_dfsdm_adc *adc;
1151 if (!iio_dev)
1152 return -EINVAL;
1153 adc = iio_priv(iio_dev);
1155 adc->cb = NULL;
1156 adc->cb_priv = NULL;
1158 return 0;
1160 EXPORT_SYMBOL_GPL(stm32_dfsdm_release_buff_cb);
1162 static int stm32_dfsdm_single_conv(struct iio_dev *indio_dev,
1163 const struct iio_chan_spec *chan, int *res)
1165 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
1166 long timeout;
1167 int ret;
1169 reinit_completion(&adc->completion);
1171 adc->buffer = res;
1173 ret = stm32_dfsdm_start_dfsdm(adc->dfsdm);
1174 if (ret < 0)
1175 return ret;
1177 ret = regmap_update_bits(adc->dfsdm->regmap, DFSDM_CR2(adc->fl_id),
1178 DFSDM_CR2_REOCIE_MASK, DFSDM_CR2_REOCIE(1));
1179 if (ret < 0)
1180 goto stop_dfsdm;
1182 adc->nconv = 1;
1183 adc->smask = BIT(chan->scan_index);
1184 ret = stm32_dfsdm_start_conv(adc, NULL);
1185 if (ret < 0) {
1186 regmap_update_bits(adc->dfsdm->regmap, DFSDM_CR2(adc->fl_id),
1187 DFSDM_CR2_REOCIE_MASK, DFSDM_CR2_REOCIE(0));
1188 goto stop_dfsdm;
1191 timeout = wait_for_completion_interruptible_timeout(&adc->completion,
1192 DFSDM_TIMEOUT);
1194 /* Mask IRQ for regular conversion achievement*/
1195 regmap_update_bits(adc->dfsdm->regmap, DFSDM_CR2(adc->fl_id),
1196 DFSDM_CR2_REOCIE_MASK, DFSDM_CR2_REOCIE(0));
1198 if (timeout == 0)
1199 ret = -ETIMEDOUT;
1200 else if (timeout < 0)
1201 ret = timeout;
1202 else
1203 ret = IIO_VAL_INT;
1205 stm32_dfsdm_stop_conv(adc);
1207 stm32_dfsdm_process_data(adc, res);
1209 stop_dfsdm:
1210 stm32_dfsdm_stop_dfsdm(adc->dfsdm);
1212 return ret;
1215 static int stm32_dfsdm_write_raw(struct iio_dev *indio_dev,
1216 struct iio_chan_spec const *chan,
1217 int val, int val2, long mask)
1219 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
1220 struct stm32_dfsdm_channel *ch = &adc->dfsdm->ch_list[chan->channel];
1221 unsigned int spi_freq;
1222 int ret = -EINVAL;
1224 switch (ch->src) {
1225 case DFSDM_CHANNEL_SPI_CLOCK_INTERNAL:
1226 spi_freq = adc->dfsdm->spi_master_freq;
1227 break;
1228 case DFSDM_CHANNEL_SPI_CLOCK_INTERNAL_DIV2_FALLING:
1229 case DFSDM_CHANNEL_SPI_CLOCK_INTERNAL_DIV2_RISING:
1230 spi_freq = adc->dfsdm->spi_master_freq / 2;
1231 break;
1232 default:
1233 spi_freq = adc->spi_freq;
1236 switch (mask) {
1237 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
1238 ret = iio_device_claim_direct_mode(indio_dev);
1239 if (ret)
1240 return ret;
1242 ret = stm32_dfsdm_compute_all_osrs(indio_dev, val);
1243 if (!ret) {
1244 dev_dbg(&indio_dev->dev,
1245 "Sampling rate changed from (%u) to (%u)\n",
1246 adc->sample_freq, spi_freq / val);
1247 adc->oversamp = val;
1248 adc->sample_freq = spi_freq / val;
1250 iio_device_release_direct_mode(indio_dev);
1251 return ret;
1253 case IIO_CHAN_INFO_SAMP_FREQ:
1254 if (!val)
1255 return -EINVAL;
1257 ret = iio_device_claim_direct_mode(indio_dev);
1258 if (ret)
1259 return ret;
1261 ret = dfsdm_adc_set_samp_freq(indio_dev, val, spi_freq);
1262 iio_device_release_direct_mode(indio_dev);
1263 return ret;
1266 return -EINVAL;
1269 static int stm32_dfsdm_read_raw(struct iio_dev *indio_dev,
1270 struct iio_chan_spec const *chan, int *val,
1271 int *val2, long mask)
1273 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
1274 int ret;
1276 switch (mask) {
1277 case IIO_CHAN_INFO_RAW:
1278 ret = iio_device_claim_direct_mode(indio_dev);
1279 if (ret)
1280 return ret;
1281 ret = iio_hw_consumer_enable(adc->hwc);
1282 if (ret < 0) {
1283 dev_err(&indio_dev->dev,
1284 "%s: IIO enable failed (channel %d)\n",
1285 __func__, chan->channel);
1286 iio_device_release_direct_mode(indio_dev);
1287 return ret;
1289 ret = stm32_dfsdm_single_conv(indio_dev, chan, val);
1290 iio_hw_consumer_disable(adc->hwc);
1291 if (ret < 0) {
1292 dev_err(&indio_dev->dev,
1293 "%s: Conversion failed (channel %d)\n",
1294 __func__, chan->channel);
1295 iio_device_release_direct_mode(indio_dev);
1296 return ret;
1298 iio_device_release_direct_mode(indio_dev);
1299 return IIO_VAL_INT;
1301 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
1302 *val = adc->oversamp;
1304 return IIO_VAL_INT;
1306 case IIO_CHAN_INFO_SAMP_FREQ:
1307 *val = adc->sample_freq;
1309 return IIO_VAL_INT;
1312 return -EINVAL;
1315 static int stm32_dfsdm_validate_trigger(struct iio_dev *indio_dev,
1316 struct iio_trigger *trig)
1318 return stm32_dfsdm_get_jextsel(indio_dev, trig) < 0 ? -EINVAL : 0;
1321 static const struct iio_info stm32_dfsdm_info_audio = {
1322 .hwfifo_set_watermark = stm32_dfsdm_set_watermark,
1323 .read_raw = stm32_dfsdm_read_raw,
1324 .write_raw = stm32_dfsdm_write_raw,
1325 .update_scan_mode = stm32_dfsdm_update_scan_mode,
1328 static const struct iio_info stm32_dfsdm_info_adc = {
1329 .hwfifo_set_watermark = stm32_dfsdm_set_watermark,
1330 .read_raw = stm32_dfsdm_read_raw,
1331 .write_raw = stm32_dfsdm_write_raw,
1332 .update_scan_mode = stm32_dfsdm_update_scan_mode,
1333 .validate_trigger = stm32_dfsdm_validate_trigger,
1336 static irqreturn_t stm32_dfsdm_irq(int irq, void *arg)
1338 struct stm32_dfsdm_adc *adc = arg;
1339 struct iio_dev *indio_dev = iio_priv_to_dev(adc);
1340 struct regmap *regmap = adc->dfsdm->regmap;
1341 unsigned int status, int_en;
1343 regmap_read(regmap, DFSDM_ISR(adc->fl_id), &status);
1344 regmap_read(regmap, DFSDM_CR2(adc->fl_id), &int_en);
1346 if (status & DFSDM_ISR_REOCF_MASK) {
1347 /* Read the data register clean the IRQ status */
1348 regmap_read(regmap, DFSDM_RDATAR(adc->fl_id), adc->buffer);
1349 complete(&adc->completion);
1352 if (status & DFSDM_ISR_ROVRF_MASK) {
1353 if (int_en & DFSDM_CR2_ROVRIE_MASK)
1354 dev_warn(&indio_dev->dev, "Overrun detected\n");
1355 regmap_update_bits(regmap, DFSDM_ICR(adc->fl_id),
1356 DFSDM_ICR_CLRROVRF_MASK,
1357 DFSDM_ICR_CLRROVRF_MASK);
1360 return IRQ_HANDLED;
1364 * Define external info for SPI Frequency and audio sampling rate that can be
1365 * configured by ASoC driver through consumer.h API
1367 static const struct iio_chan_spec_ext_info dfsdm_adc_audio_ext_info[] = {
1368 /* spi_clk_freq : clock freq on SPI/manchester bus used by channel */
1370 .name = "spi_clk_freq",
1371 .shared = IIO_SHARED_BY_TYPE,
1372 .read = dfsdm_adc_audio_get_spiclk,
1373 .write = dfsdm_adc_audio_set_spiclk,
1378 static void stm32_dfsdm_dma_release(struct iio_dev *indio_dev)
1380 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
1382 if (adc->dma_chan) {
1383 dma_free_coherent(adc->dma_chan->device->dev,
1384 DFSDM_DMA_BUFFER_SIZE,
1385 adc->rx_buf, adc->dma_buf);
1386 dma_release_channel(adc->dma_chan);
1390 static int stm32_dfsdm_dma_request(struct iio_dev *indio_dev)
1392 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
1394 adc->dma_chan = dma_request_chan(&indio_dev->dev, "rx");
1395 if (IS_ERR(adc->dma_chan)) {
1396 int ret = PTR_ERR(adc->dma_chan);
1398 adc->dma_chan = NULL;
1399 return ret;
1402 adc->rx_buf = dma_alloc_coherent(adc->dma_chan->device->dev,
1403 DFSDM_DMA_BUFFER_SIZE,
1404 &adc->dma_buf, GFP_KERNEL);
1405 if (!adc->rx_buf) {
1406 dma_release_channel(adc->dma_chan);
1407 return -ENOMEM;
1410 indio_dev->modes |= INDIO_BUFFER_SOFTWARE;
1411 indio_dev->setup_ops = &stm32_dfsdm_buffer_setup_ops;
1413 return 0;
1416 static int stm32_dfsdm_adc_chan_init_one(struct iio_dev *indio_dev,
1417 struct iio_chan_spec *ch)
1419 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
1420 int ret;
1422 ret = stm32_dfsdm_channel_parse_of(adc->dfsdm, indio_dev, ch);
1423 if (ret < 0)
1424 return ret;
1426 ch->type = IIO_VOLTAGE;
1427 ch->indexed = 1;
1430 * IIO_CHAN_INFO_RAW: used to compute regular conversion
1431 * IIO_CHAN_INFO_OVERSAMPLING_RATIO: used to set oversampling
1433 ch->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
1434 ch->info_mask_shared_by_all = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) |
1435 BIT(IIO_CHAN_INFO_SAMP_FREQ);
1437 if (adc->dev_data->type == DFSDM_AUDIO) {
1438 ch->ext_info = dfsdm_adc_audio_ext_info;
1439 } else {
1440 ch->scan_type.shift = 8;
1442 ch->scan_type.sign = 's';
1443 ch->scan_type.realbits = 24;
1444 ch->scan_type.storagebits = 32;
1446 return stm32_dfsdm_chan_configure(adc->dfsdm,
1447 &adc->dfsdm->ch_list[ch->channel]);
1450 static int stm32_dfsdm_audio_init(struct iio_dev *indio_dev)
1452 struct iio_chan_spec *ch;
1453 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
1454 struct stm32_dfsdm_channel *d_ch;
1455 int ret;
1457 ch = devm_kzalloc(&indio_dev->dev, sizeof(*ch), GFP_KERNEL);
1458 if (!ch)
1459 return -ENOMEM;
1461 ch->scan_index = 0;
1463 ret = stm32_dfsdm_adc_chan_init_one(indio_dev, ch);
1464 if (ret < 0) {
1465 dev_err(&indio_dev->dev, "Channels init failed\n");
1466 return ret;
1468 ch->info_mask_separate = BIT(IIO_CHAN_INFO_SAMP_FREQ);
1470 d_ch = &adc->dfsdm->ch_list[ch->channel];
1471 if (d_ch->src != DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL)
1472 adc->spi_freq = adc->dfsdm->spi_master_freq;
1474 indio_dev->num_channels = 1;
1475 indio_dev->channels = ch;
1477 return stm32_dfsdm_dma_request(indio_dev);
1480 static int stm32_dfsdm_adc_init(struct iio_dev *indio_dev)
1482 struct iio_chan_spec *ch;
1483 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
1484 int num_ch;
1485 int ret, chan_idx;
1487 adc->oversamp = DFSDM_DEFAULT_OVERSAMPLING;
1488 ret = stm32_dfsdm_compute_all_osrs(indio_dev, adc->oversamp);
1489 if (ret < 0)
1490 return ret;
1492 num_ch = of_property_count_u32_elems(indio_dev->dev.of_node,
1493 "st,adc-channels");
1494 if (num_ch < 0 || num_ch > adc->dfsdm->num_chs) {
1495 dev_err(&indio_dev->dev, "Bad st,adc-channels\n");
1496 return num_ch < 0 ? num_ch : -EINVAL;
1499 /* Bind to SD modulator IIO device */
1500 adc->hwc = devm_iio_hw_consumer_alloc(&indio_dev->dev);
1501 if (IS_ERR(adc->hwc))
1502 return -EPROBE_DEFER;
1504 ch = devm_kcalloc(&indio_dev->dev, num_ch, sizeof(*ch),
1505 GFP_KERNEL);
1506 if (!ch)
1507 return -ENOMEM;
1509 for (chan_idx = 0; chan_idx < num_ch; chan_idx++) {
1510 ch[chan_idx].scan_index = chan_idx;
1511 ret = stm32_dfsdm_adc_chan_init_one(indio_dev, &ch[chan_idx]);
1512 if (ret < 0) {
1513 dev_err(&indio_dev->dev, "Channels init failed\n");
1514 return ret;
1518 indio_dev->num_channels = num_ch;
1519 indio_dev->channels = ch;
1521 init_completion(&adc->completion);
1523 /* Optionally request DMA */
1524 ret = stm32_dfsdm_dma_request(indio_dev);
1525 if (ret) {
1526 if (ret != -ENODEV) {
1527 if (ret != -EPROBE_DEFER)
1528 dev_err(&indio_dev->dev,
1529 "DMA channel request failed with %d\n",
1530 ret);
1531 return ret;
1534 dev_dbg(&indio_dev->dev, "No DMA support\n");
1535 return 0;
1538 ret = iio_triggered_buffer_setup(indio_dev,
1539 &iio_pollfunc_store_time,
1540 &stm32_dfsdm_adc_trigger_handler,
1541 &stm32_dfsdm_buffer_setup_ops);
1542 if (ret) {
1543 stm32_dfsdm_dma_release(indio_dev);
1544 dev_err(&indio_dev->dev, "buffer setup failed\n");
1545 return ret;
1548 /* lptimer/timer hardware triggers */
1549 indio_dev->modes |= INDIO_HARDWARE_TRIGGERED;
1551 return 0;
1554 static const struct stm32_dfsdm_dev_data stm32h7_dfsdm_adc_data = {
1555 .type = DFSDM_IIO,
1556 .init = stm32_dfsdm_adc_init,
1559 static const struct stm32_dfsdm_dev_data stm32h7_dfsdm_audio_data = {
1560 .type = DFSDM_AUDIO,
1561 .init = stm32_dfsdm_audio_init,
1564 static const struct of_device_id stm32_dfsdm_adc_match[] = {
1566 .compatible = "st,stm32-dfsdm-adc",
1567 .data = &stm32h7_dfsdm_adc_data,
1570 .compatible = "st,stm32-dfsdm-dmic",
1571 .data = &stm32h7_dfsdm_audio_data,
1576 static int stm32_dfsdm_adc_probe(struct platform_device *pdev)
1578 struct device *dev = &pdev->dev;
1579 struct stm32_dfsdm_adc *adc;
1580 struct device_node *np = dev->of_node;
1581 const struct stm32_dfsdm_dev_data *dev_data;
1582 struct iio_dev *iio;
1583 char *name;
1584 int ret, irq, val;
1586 dev_data = of_device_get_match_data(dev);
1587 iio = devm_iio_device_alloc(dev, sizeof(*adc));
1588 if (!iio) {
1589 dev_err(dev, "%s: Failed to allocate IIO\n", __func__);
1590 return -ENOMEM;
1593 adc = iio_priv(iio);
1594 adc->dfsdm = dev_get_drvdata(dev->parent);
1596 iio->dev.parent = dev;
1597 iio->dev.of_node = np;
1598 iio->modes = INDIO_DIRECT_MODE;
1600 platform_set_drvdata(pdev, adc);
1602 ret = of_property_read_u32(dev->of_node, "reg", &adc->fl_id);
1603 if (ret != 0 || adc->fl_id >= adc->dfsdm->num_fls) {
1604 dev_err(dev, "Missing or bad reg property\n");
1605 return -EINVAL;
1608 name = devm_kzalloc(dev, sizeof("dfsdm-adc0"), GFP_KERNEL);
1609 if (!name)
1610 return -ENOMEM;
1611 if (dev_data->type == DFSDM_AUDIO) {
1612 iio->info = &stm32_dfsdm_info_audio;
1613 snprintf(name, sizeof("dfsdm-pdm0"), "dfsdm-pdm%d", adc->fl_id);
1614 } else {
1615 iio->info = &stm32_dfsdm_info_adc;
1616 snprintf(name, sizeof("dfsdm-adc0"), "dfsdm-adc%d", adc->fl_id);
1618 iio->name = name;
1621 * In a first step IRQs generated for channels are not treated.
1622 * So IRQ associated to filter instance 0 is dedicated to the Filter 0.
1624 irq = platform_get_irq(pdev, 0);
1625 if (irq < 0)
1626 return irq;
1628 ret = devm_request_irq(dev, irq, stm32_dfsdm_irq,
1629 0, pdev->name, adc);
1630 if (ret < 0) {
1631 dev_err(dev, "Failed to request IRQ\n");
1632 return ret;
1635 ret = of_property_read_u32(dev->of_node, "st,filter-order", &val);
1636 if (ret < 0) {
1637 dev_err(dev, "Failed to set filter order\n");
1638 return ret;
1641 adc->dfsdm->fl_list[adc->fl_id].ford = val;
1643 ret = of_property_read_u32(dev->of_node, "st,filter0-sync", &val);
1644 if (!ret)
1645 adc->dfsdm->fl_list[adc->fl_id].sync_mode = val;
1647 adc->dev_data = dev_data;
1648 ret = dev_data->init(iio);
1649 if (ret < 0)
1650 return ret;
1652 ret = iio_device_register(iio);
1653 if (ret < 0)
1654 goto err_cleanup;
1656 if (dev_data->type == DFSDM_AUDIO) {
1657 ret = of_platform_populate(np, NULL, NULL, dev);
1658 if (ret < 0) {
1659 dev_err(dev, "Failed to find an audio DAI\n");
1660 goto err_unregister;
1664 return 0;
1666 err_unregister:
1667 iio_device_unregister(iio);
1668 err_cleanup:
1669 stm32_dfsdm_dma_release(iio);
1671 return ret;
1674 static int stm32_dfsdm_adc_remove(struct platform_device *pdev)
1676 struct stm32_dfsdm_adc *adc = platform_get_drvdata(pdev);
1677 struct iio_dev *indio_dev = iio_priv_to_dev(adc);
1679 if (adc->dev_data->type == DFSDM_AUDIO)
1680 of_platform_depopulate(&pdev->dev);
1681 iio_device_unregister(indio_dev);
1682 stm32_dfsdm_dma_release(indio_dev);
1684 return 0;
1687 static int __maybe_unused stm32_dfsdm_adc_suspend(struct device *dev)
1689 struct stm32_dfsdm_adc *adc = dev_get_drvdata(dev);
1690 struct iio_dev *indio_dev = iio_priv_to_dev(adc);
1692 if (iio_buffer_enabled(indio_dev))
1693 __stm32_dfsdm_predisable(indio_dev);
1695 return 0;
1698 static int __maybe_unused stm32_dfsdm_adc_resume(struct device *dev)
1700 struct stm32_dfsdm_adc *adc = dev_get_drvdata(dev);
1701 struct iio_dev *indio_dev = iio_priv_to_dev(adc);
1702 const struct iio_chan_spec *chan;
1703 struct stm32_dfsdm_channel *ch;
1704 int i, ret;
1706 /* restore channels configuration */
1707 for (i = 0; i < indio_dev->num_channels; i++) {
1708 chan = indio_dev->channels + i;
1709 ch = &adc->dfsdm->ch_list[chan->channel];
1710 ret = stm32_dfsdm_chan_configure(adc->dfsdm, ch);
1711 if (ret)
1712 return ret;
1715 if (iio_buffer_enabled(indio_dev))
1716 __stm32_dfsdm_postenable(indio_dev);
1718 return 0;
1721 static SIMPLE_DEV_PM_OPS(stm32_dfsdm_adc_pm_ops,
1722 stm32_dfsdm_adc_suspend, stm32_dfsdm_adc_resume);
1724 static struct platform_driver stm32_dfsdm_adc_driver = {
1725 .driver = {
1726 .name = "stm32-dfsdm-adc",
1727 .of_match_table = stm32_dfsdm_adc_match,
1728 .pm = &stm32_dfsdm_adc_pm_ops,
1730 .probe = stm32_dfsdm_adc_probe,
1731 .remove = stm32_dfsdm_adc_remove,
1733 module_platform_driver(stm32_dfsdm_adc_driver);
1735 MODULE_DESCRIPTION("STM32 sigma delta ADC");
1736 MODULE_AUTHOR("Arnaud Pouliquen <arnaud.pouliquen@st.com>");
1737 MODULE_LICENSE("GPL v2");