Lines Matching +full:oversampling +full:- +full:ratio

1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
10 #include <linux/dma-mapping.h>
11 #include <linux/iio/adc/stm32-dfsdm-adc.h>
13 #include <linux/iio/hw-consumer.h>
15 #include <linux/iio/timer/stm32-lptim-trigger.h>
16 #include <linux/iio/timer/stm32-timer-trigger.h>
27 #include "stm32-dfsdm.h"
35 /* Oversampling attribute default */
38 /* Oversampling max values */
42 /* Limit filter output resolution to 31 bits. (i.e. sample range is +/-2^30) */
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)
129 for (p = list; p && p->name; p++) in stm32_dfsdm_str2val()
130 if (!strcmp(p->name, str)) in stm32_dfsdm_str2val()
131 return p->val; in stm32_dfsdm_str2val()
133 return -EINVAL; in stm32_dfsdm_str2val()
137 * struct stm32_dfsdm_trig_info - DFSDM trigger info
183 !strcmp(stm32_dfsdm_trigs[i].name, trig->name)) { in stm32_dfsdm_get_jextsel()
188 return -EINVAL; in stm32_dfsdm_get_jextsel()
198 unsigned int p = fl->ford; /* filter order (ford) */ in stm32_dfsdm_compute_osrs()
199 struct stm32_dfsdm_filter_osr *flo = &fl->flo[fast]; in stm32_dfsdm_compute_osrs()
201 pr_debug("Requested oversampling: %d\n", oversamp); in stm32_dfsdm_compute_osrs()
203 * This function tries to compute filter oversampling and integrator in stm32_dfsdm_compute_osrs()
204 * oversampling, base on oversampling ratio requested by user. in stm32_dfsdm_compute_osrs()
206 * Decimation d depends on the filter order and the oversampling ratios. in stm32_dfsdm_compute_osrs()
208 * fosr: filter over sampling ratio in stm32_dfsdm_compute_osrs()
209 * iosr: integrator over sampling ratio in stm32_dfsdm_compute_osrs()
211 if (fl->ford == DFSDM_FASTSINC_ORDER) { in stm32_dfsdm_compute_osrs()
217 * Look for filter and integrator oversampling ratios which allows in stm32_dfsdm_compute_osrs()
224 else if (fl->ford == DFSDM_FASTSINC_ORDER) in stm32_dfsdm_compute_osrs()
227 d = fosr * (iosr - 1 + p) + p; in stm32_dfsdm_compute_osrs()
242 for (i = p - 1; i > 0; i--) { in stm32_dfsdm_compute_osrs()
254 if (res >= flo->res) { in stm32_dfsdm_compute_osrs()
255 flo->res = res; in stm32_dfsdm_compute_osrs()
256 flo->fosr = fosr; in stm32_dfsdm_compute_osrs()
257 flo->iosr = iosr; in stm32_dfsdm_compute_osrs()
259 bits = fls(flo->res); in stm32_dfsdm_compute_osrs()
261 max = flo->res << 8; in stm32_dfsdm_compute_osrs()
264 if (flo->res > BIT(bits - 1)) in stm32_dfsdm_compute_osrs()
267 max--; in stm32_dfsdm_compute_osrs()
269 shift = DFSDM_DATA_RES - bits; in stm32_dfsdm_compute_osrs()
278 flo->rshift = 0; in stm32_dfsdm_compute_osrs()
279 flo->lshift = shift; in stm32_dfsdm_compute_osrs()
291 flo->rshift = 1 - shift; in stm32_dfsdm_compute_osrs()
292 flo->lshift = 1; in stm32_dfsdm_compute_osrs()
293 max >>= flo->rshift; in stm32_dfsdm_compute_osrs()
295 flo->max = (s32)max; in stm32_dfsdm_compute_osrs()
296 flo->bits = bits; in stm32_dfsdm_compute_osrs()
299 fast, flo->fosr, flo->iosr, in stm32_dfsdm_compute_osrs()
300 flo->res, bits, flo->rshift, in stm32_dfsdm_compute_osrs()
301 flo->lshift); in stm32_dfsdm_compute_osrs()
306 if (!flo->res) in stm32_dfsdm_compute_osrs()
307 return -EINVAL; in stm32_dfsdm_compute_osrs()
316 struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[adc->fl_id]; in stm32_dfsdm_compute_all_osrs()
319 memset(&fl->flo[0], 0, sizeof(fl->flo[0])); in stm32_dfsdm_compute_all_osrs()
320 memset(&fl->flo[1], 0, sizeof(fl->flo[1])); in stm32_dfsdm_compute_all_osrs()
325 dev_err(&indio_dev->dev, in stm32_dfsdm_compute_all_osrs()
328 return -EINVAL; in stm32_dfsdm_compute_all_osrs()
337 struct regmap *regmap = adc->dfsdm->regmap; in stm32_dfsdm_start_channel()
342 for_each_set_bit(bit, &adc->smask, sizeof(adc->smask) * BITS_PER_BYTE) { in stm32_dfsdm_start_channel()
343 chan = indio_dev->channels + bit; in stm32_dfsdm_start_channel()
344 ret = regmap_update_bits(regmap, DFSDM_CHCFGR1(chan->channel), in stm32_dfsdm_start_channel()
357 struct regmap *regmap = adc->dfsdm->regmap; in stm32_dfsdm_stop_channel()
361 for_each_set_bit(bit, &adc->smask, sizeof(adc->smask) * BITS_PER_BYTE) { in stm32_dfsdm_stop_channel()
362 chan = indio_dev->channels + bit; in stm32_dfsdm_stop_channel()
363 regmap_update_bits(regmap, DFSDM_CHCFGR1(chan->channel), in stm32_dfsdm_stop_channel()
372 unsigned int id = ch->id; in stm32_dfsdm_chan_configure()
373 struct regmap *regmap = dfsdm->regmap; in stm32_dfsdm_chan_configure()
378 DFSDM_CHCFGR1_SITP(ch->type)); in stm32_dfsdm_chan_configure()
383 DFSDM_CHCFGR1_SPICKSEL(ch->src)); in stm32_dfsdm_chan_configure()
388 DFSDM_CHCFGR1_CHINSEL(ch->alt_si)); in stm32_dfsdm_chan_configure()
395 struct stm32_dfsdm *dfsdm = adc->dfsdm; in stm32_dfsdm_start_filter()
399 ret = regmap_update_bits(dfsdm->regmap, DFSDM_CR1(fl_id), in stm32_dfsdm_start_filter()
405 if (adc->nconv > 1 || trig) in stm32_dfsdm_start_filter()
409 return regmap_update_bits(dfsdm->regmap, DFSDM_CR1(fl_id), in stm32_dfsdm_start_filter()
418 regmap_update_bits(dfsdm->regmap, DFSDM_CR1(fl_id), in stm32_dfsdm_stop_filter()
427 struct regmap *regmap = adc->dfsdm->regmap; in stm32_dfsdm_filter_set_trig()
456 struct regmap *regmap = adc->dfsdm->regmap; in stm32_dfsdm_channels_configure()
457 struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[fl_id]; in stm32_dfsdm_channels_configure()
458 struct stm32_dfsdm_filter_osr *flo = &fl->flo[0]; in stm32_dfsdm_channels_configure()
463 fl->fast = 0; in stm32_dfsdm_channels_configure()
469 if (adc->nconv == 1 && !trig && iio_buffer_enabled(indio_dev)) { in stm32_dfsdm_channels_configure()
470 if (fl->flo[1].res >= fl->flo[0].res) { in stm32_dfsdm_channels_configure()
471 fl->fast = 1; in stm32_dfsdm_channels_configure()
472 flo = &fl->flo[1]; in stm32_dfsdm_channels_configure()
476 if (!flo->res) in stm32_dfsdm_channels_configure()
477 return -EINVAL; in stm32_dfsdm_channels_configure()
479 dev_dbg(&indio_dev->dev, "Samples actual resolution: %d bits", in stm32_dfsdm_channels_configure()
480 min(flo->bits, (u32)DFSDM_DATA_RES - 1)); in stm32_dfsdm_channels_configure()
482 for_each_set_bit(bit, &adc->smask, in stm32_dfsdm_channels_configure()
483 sizeof(adc->smask) * BITS_PER_BYTE) { in stm32_dfsdm_channels_configure()
484 chan = indio_dev->channels + bit; in stm32_dfsdm_channels_configure()
487 DFSDM_CHCFGR2(chan->channel), in stm32_dfsdm_channels_configure()
489 DFSDM_CHCFGR2_DTRBS(flo->rshift)); in stm32_dfsdm_channels_configure()
502 struct regmap *regmap = adc->dfsdm->regmap; in stm32_dfsdm_filter_configure()
503 struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[fl_id]; in stm32_dfsdm_filter_configure()
504 struct stm32_dfsdm_filter_osr *flo = &fl->flo[fl->fast]; in stm32_dfsdm_filter_configure()
510 /* Average integrator oversampling */ in stm32_dfsdm_filter_configure()
512 DFSDM_FCR_IOSR(flo->iosr - 1)); in stm32_dfsdm_filter_configure()
516 /* Filter order and Oversampling */ in stm32_dfsdm_filter_configure()
518 DFSDM_FCR_FOSR(flo->fosr - 1)); in stm32_dfsdm_filter_configure()
523 DFSDM_FCR_FORD(fl->ford)); in stm32_dfsdm_filter_configure()
533 DFSDM_CR1_FAST(fl->fast)); in stm32_dfsdm_filter_configure()
539 * ---------------------------------------------------------------- in stm32_dfsdm_filter_configure()
542 * --------------|---------|--------------|----------|------------| in stm32_dfsdm_filter_configure()
545 * --------------|---------|--------------|----------|------------| in stm32_dfsdm_filter_configure()
548 * --------------|---------|--------------|----------|------------| in stm32_dfsdm_filter_configure()
551 * --------------|---------|--------------|----------|------------| in stm32_dfsdm_filter_configure()
554 * ---------------------------------------------------------------- in stm32_dfsdm_filter_configure()
556 if (adc->nconv == 1 && !trig) { in stm32_dfsdm_filter_configure()
557 bit = __ffs(adc->smask); in stm32_dfsdm_filter_configure()
558 chan = indio_dev->channels + bit; in stm32_dfsdm_filter_configure()
561 cr1 = DFSDM_CR1_RCH(chan->channel); in stm32_dfsdm_filter_configure()
567 cr1 |= DFSDM_CR1_RSYNC(fl->sync_mode); in stm32_dfsdm_filter_configure()
570 for_each_set_bit(bit, &adc->smask, in stm32_dfsdm_filter_configure()
571 sizeof(adc->smask) * BITS_PER_BYTE) { in stm32_dfsdm_filter_configure()
572 chan = indio_dev->channels + bit; in stm32_dfsdm_filter_configure()
573 jchg |= BIT(chan->channel); in stm32_dfsdm_filter_configure()
580 cr1 = DFSDM_CR1_JSCAN((adc->nconv > 1) ? 1 : 0); in stm32_dfsdm_filter_configure()
585 * - conversions in sync with filter 0 in stm32_dfsdm_filter_configure()
586 * - triggered conversions in stm32_dfsdm_filter_configure()
588 if (!fl->sync_mode && !trig) in stm32_dfsdm_filter_configure()
589 return -EINVAL; in stm32_dfsdm_filter_configure()
590 cr1 |= DFSDM_CR1_JSYNC(fl->sync_mode); in stm32_dfsdm_filter_configure()
603 int chan_idx = ch->scan_index; in stm32_dfsdm_channel_parse_of()
606 ret = of_property_read_u32_index(indio_dev->dev.of_node, in stm32_dfsdm_channel_parse_of()
607 "st,adc-channels", chan_idx, in stm32_dfsdm_channel_parse_of()
608 &ch->channel); in stm32_dfsdm_channel_parse_of()
610 dev_err(&indio_dev->dev, in stm32_dfsdm_channel_parse_of()
611 " Error parsing 'st,adc-channels' for idx %d\n", in stm32_dfsdm_channel_parse_of()
615 if (ch->channel >= dfsdm->num_chs) { in stm32_dfsdm_channel_parse_of()
616 dev_err(&indio_dev->dev, in stm32_dfsdm_channel_parse_of()
618 ch->channel, dfsdm->num_chs); in stm32_dfsdm_channel_parse_of()
619 return -EINVAL; in stm32_dfsdm_channel_parse_of()
622 ret = of_property_read_string_index(indio_dev->dev.of_node, in stm32_dfsdm_channel_parse_of()
623 "st,adc-channel-names", chan_idx, in stm32_dfsdm_channel_parse_of()
624 &ch->datasheet_name); in stm32_dfsdm_channel_parse_of()
626 dev_err(&indio_dev->dev, in stm32_dfsdm_channel_parse_of()
627 " Error parsing 'st,adc-channel-names' for idx %d\n", in stm32_dfsdm_channel_parse_of()
632 df_ch = &dfsdm->ch_list[ch->channel]; in stm32_dfsdm_channel_parse_of()
633 df_ch->id = ch->channel; in stm32_dfsdm_channel_parse_of()
635 ret = of_property_read_string_index(indio_dev->dev.of_node, in stm32_dfsdm_channel_parse_of()
636 "st,adc-channel-types", chan_idx, in stm32_dfsdm_channel_parse_of()
645 df_ch->type = val; in stm32_dfsdm_channel_parse_of()
647 ret = of_property_read_string_index(indio_dev->dev.of_node, in stm32_dfsdm_channel_parse_of()
648 "st,adc-channel-clk-src", chan_idx, in stm32_dfsdm_channel_parse_of()
657 df_ch->src = val; in stm32_dfsdm_channel_parse_of()
659 ret = of_property_read_u32_index(indio_dev->dev.of_node, in stm32_dfsdm_channel_parse_of()
660 "st,adc-alt-channel", chan_idx, in stm32_dfsdm_channel_parse_of()
661 &df_ch->alt_si); in stm32_dfsdm_channel_parse_of()
663 df_ch->alt_si = 0; in stm32_dfsdm_channel_parse_of()
675 return snprintf(buf, PAGE_SIZE, "%d\n", adc->spi_freq); in dfsdm_adc_audio_get_spiclk()
688 dev_dbg(&indio_dev->dev, in dfsdm_adc_set_samp_freq()
696 adc->sample_freq = spi_freq / oversamp; in dfsdm_adc_set_samp_freq()
697 adc->oversamp = oversamp; in dfsdm_adc_set_samp_freq()
708 struct stm32_dfsdm_channel *ch = &adc->dfsdm->ch_list[chan->channel]; in dfsdm_adc_audio_set_spiclk()
709 unsigned int sample_freq = adc->sample_freq; in dfsdm_adc_audio_set_spiclk()
713 dev_err(&indio_dev->dev, "enter %s\n", __func__); in dfsdm_adc_audio_set_spiclk()
715 if (ch->src != DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL) in dfsdm_adc_audio_set_spiclk()
716 return -EPERM; in dfsdm_adc_audio_set_spiclk()
723 return -EINVAL; in dfsdm_adc_audio_set_spiclk()
730 adc->spi_freq = spi_freq; in dfsdm_adc_audio_set_spiclk()
739 struct regmap *regmap = adc->dfsdm->regmap; in stm32_dfsdm_start_conv()
742 ret = stm32_dfsdm_channels_configure(indio_dev, adc->fl_id, trig); in stm32_dfsdm_start_conv()
750 ret = stm32_dfsdm_filter_configure(indio_dev, adc->fl_id, trig); in stm32_dfsdm_start_conv()
754 ret = stm32_dfsdm_start_filter(adc, adc->fl_id, trig); in stm32_dfsdm_start_conv()
761 regmap_update_bits(regmap, DFSDM_CR1(adc->fl_id), in stm32_dfsdm_start_conv()
772 struct regmap *regmap = adc->dfsdm->regmap; in stm32_dfsdm_stop_conv()
774 stm32_dfsdm_stop_filter(adc->dfsdm, adc->fl_id); in stm32_dfsdm_stop_conv()
776 regmap_update_bits(regmap, DFSDM_CR1(adc->fl_id), in stm32_dfsdm_stop_conv()
792 * - always one buffer (period) DMA is working on in stm32_dfsdm_set_watermark()
793 * - one buffer (period) driver pushed to ASoC side. in stm32_dfsdm_set_watermark()
796 adc->buf_sz = min(rx_buf_sz, watermark * 2 * adc->nconv); in stm32_dfsdm_set_watermark()
806 status = dmaengine_tx_status(adc->dma_chan, in stm32_dfsdm_adc_dma_residue()
807 adc->dma_chan->cookie, in stm32_dfsdm_adc_dma_residue()
811 unsigned int i = adc->buf_sz - state.residue; in stm32_dfsdm_adc_dma_residue()
815 if (i >= adc->bufi) in stm32_dfsdm_adc_dma_residue()
816 size = i - adc->bufi; in stm32_dfsdm_adc_dma_residue()
818 size = adc->buf_sz + i - adc->bufi; in stm32_dfsdm_adc_dma_residue()
829 struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[adc->fl_id]; in stm32_dfsdm_process_data()
830 struct stm32_dfsdm_filter_osr *flo = &fl->flo[fl->fast]; in stm32_dfsdm_process_data()
831 unsigned int i = adc->nconv; in stm32_dfsdm_process_data()
834 while (i--) { in stm32_dfsdm_process_data()
837 /* Convert 2^(n-1) sample to 2^(n-1)-1 to avoid wrap-around */ in stm32_dfsdm_process_data()
838 if (*ptr > flo->max) in stm32_dfsdm_process_data()
839 *ptr -= 1; in stm32_dfsdm_process_data()
844 *ptr <<= flo->lshift; in stm32_dfsdm_process_data()
866 dev_dbg(&indio_dev->dev, "pos = %d, available = %d\n", in stm32_dfsdm_dma_buffer_done()
867 adc->bufi, available); in stm32_dfsdm_dma_buffer_done()
868 old_pos = adc->bufi; in stm32_dfsdm_dma_buffer_done()
870 while (available >= indio_dev->scan_bytes) { in stm32_dfsdm_dma_buffer_done()
871 s32 *buffer = (s32 *)&adc->rx_buf[adc->bufi]; in stm32_dfsdm_dma_buffer_done()
875 available -= indio_dev->scan_bytes; in stm32_dfsdm_dma_buffer_done()
876 adc->bufi += indio_dev->scan_bytes; in stm32_dfsdm_dma_buffer_done()
877 if (adc->bufi >= adc->buf_sz) { in stm32_dfsdm_dma_buffer_done()
878 if (adc->cb) in stm32_dfsdm_dma_buffer_done()
879 adc->cb(&adc->rx_buf[old_pos], in stm32_dfsdm_dma_buffer_done()
880 adc->buf_sz - old_pos, adc->cb_priv); in stm32_dfsdm_dma_buffer_done()
881 adc->bufi = 0; in stm32_dfsdm_dma_buffer_done()
893 if (adc->dev_data->type == DFSDM_IIO) in stm32_dfsdm_dma_buffer_done()
896 if (adc->cb) in stm32_dfsdm_dma_buffer_done()
897 adc->cb(&adc->rx_buf[old_pos], adc->bufi - old_pos, in stm32_dfsdm_dma_buffer_done()
898 adc->cb_priv); in stm32_dfsdm_dma_buffer_done()
905 * The DFSDM supports half-word transfers. However, for 16 bits record, in stm32_dfsdm_adc_dma_start()
910 .src_addr = (dma_addr_t)adc->dfsdm->phys_base, in stm32_dfsdm_adc_dma_start()
917 if (!adc->dma_chan) in stm32_dfsdm_adc_dma_start()
918 return -EINVAL; in stm32_dfsdm_adc_dma_start()
920 dev_dbg(&indio_dev->dev, "size=%d watermark=%d\n", in stm32_dfsdm_adc_dma_start()
921 adc->buf_sz, adc->buf_sz / 2); in stm32_dfsdm_adc_dma_start()
923 if (adc->nconv == 1 && !indio_dev->trig) in stm32_dfsdm_adc_dma_start()
924 config.src_addr += DFSDM_RDATAR(adc->fl_id); in stm32_dfsdm_adc_dma_start()
926 config.src_addr += DFSDM_JDATAR(adc->fl_id); in stm32_dfsdm_adc_dma_start()
927 ret = dmaengine_slave_config(adc->dma_chan, &config); in stm32_dfsdm_adc_dma_start()
932 desc = dmaengine_prep_dma_cyclic(adc->dma_chan, in stm32_dfsdm_adc_dma_start()
933 adc->dma_buf, in stm32_dfsdm_adc_dma_start()
934 adc->buf_sz, adc->buf_sz / 2, in stm32_dfsdm_adc_dma_start()
938 return -EBUSY; in stm32_dfsdm_adc_dma_start()
940 desc->callback = stm32_dfsdm_dma_buffer_done; in stm32_dfsdm_adc_dma_start()
941 desc->callback_param = indio_dev; in stm32_dfsdm_adc_dma_start()
949 dma_async_issue_pending(adc->dma_chan); in stm32_dfsdm_adc_dma_start()
951 if (adc->nconv == 1 && !indio_dev->trig) { in stm32_dfsdm_adc_dma_start()
953 ret = regmap_update_bits(adc->dfsdm->regmap, in stm32_dfsdm_adc_dma_start()
954 DFSDM_CR1(adc->fl_id), in stm32_dfsdm_adc_dma_start()
959 ret = regmap_update_bits(adc->dfsdm->regmap, in stm32_dfsdm_adc_dma_start()
960 DFSDM_CR1(adc->fl_id), in stm32_dfsdm_adc_dma_start()
971 dmaengine_terminate_all(adc->dma_chan); in stm32_dfsdm_adc_dma_start()
980 if (!adc->dma_chan) in stm32_dfsdm_adc_dma_stop()
983 regmap_update_bits(adc->dfsdm->regmap, DFSDM_CR1(adc->fl_id), in stm32_dfsdm_adc_dma_stop()
985 dmaengine_terminate_all(adc->dma_chan); in stm32_dfsdm_adc_dma_stop()
993 adc->nconv = bitmap_weight(scan_mask, indio_dev->masklength); in stm32_dfsdm_update_scan_mode()
994 adc->smask = *scan_mask; in stm32_dfsdm_update_scan_mode()
996 dev_dbg(&indio_dev->dev, "nconv=%d mask=%lx\n", adc->nconv, *scan_mask); in stm32_dfsdm_update_scan_mode()
1007 adc->bufi = 0; in stm32_dfsdm_postenable()
1009 if (adc->hwc) { in stm32_dfsdm_postenable()
1010 ret = iio_hw_consumer_enable(adc->hwc); in stm32_dfsdm_postenable()
1015 ret = stm32_dfsdm_start_dfsdm(adc->dfsdm); in stm32_dfsdm_postenable()
1021 dev_err(&indio_dev->dev, "Can't start DMA\n"); in stm32_dfsdm_postenable()
1025 ret = stm32_dfsdm_start_conv(indio_dev, indio_dev->trig); in stm32_dfsdm_postenable()
1027 dev_err(&indio_dev->dev, "Can't start conversion\n"); in stm32_dfsdm_postenable()
1036 stm32_dfsdm_stop_dfsdm(adc->dfsdm); in stm32_dfsdm_postenable()
1038 if (adc->hwc) in stm32_dfsdm_postenable()
1039 iio_hw_consumer_disable(adc->hwc); in stm32_dfsdm_postenable()
1052 stm32_dfsdm_stop_dfsdm(adc->dfsdm); in stm32_dfsdm_predisable()
1054 if (adc->hwc) in stm32_dfsdm_predisable()
1055 iio_hw_consumer_disable(adc->hwc); in stm32_dfsdm_predisable()
1066 * stm32_dfsdm_get_buff_cb() - register a callback that will be called when
1071 * - data: pointer to data buffer
1072 * - size: size in byte of the data buffer
1073 * - private: pointer to consumer private structure.
1084 return -EINVAL; in stm32_dfsdm_get_buff_cb()
1087 adc->cb = cb; in stm32_dfsdm_get_buff_cb()
1088 adc->cb_priv = private; in stm32_dfsdm_get_buff_cb()
1095 * stm32_dfsdm_release_buff_cb - unregister buffer callback
1104 return -EINVAL; in stm32_dfsdm_release_buff_cb()
1107 adc->cb = NULL; in stm32_dfsdm_release_buff_cb()
1108 adc->cb_priv = NULL; in stm32_dfsdm_release_buff_cb()
1121 reinit_completion(&adc->completion); in stm32_dfsdm_single_conv()
1123 adc->buffer = res; in stm32_dfsdm_single_conv()
1125 ret = stm32_dfsdm_start_dfsdm(adc->dfsdm); in stm32_dfsdm_single_conv()
1129 ret = regmap_update_bits(adc->dfsdm->regmap, DFSDM_CR2(adc->fl_id), in stm32_dfsdm_single_conv()
1134 adc->nconv = 1; in stm32_dfsdm_single_conv()
1135 adc->smask = BIT(chan->scan_index); in stm32_dfsdm_single_conv()
1138 regmap_update_bits(adc->dfsdm->regmap, DFSDM_CR2(adc->fl_id), in stm32_dfsdm_single_conv()
1143 timeout = wait_for_completion_interruptible_timeout(&adc->completion, in stm32_dfsdm_single_conv()
1147 regmap_update_bits(adc->dfsdm->regmap, DFSDM_CR2(adc->fl_id), in stm32_dfsdm_single_conv()
1151 ret = -ETIMEDOUT; in stm32_dfsdm_single_conv()
1162 stm32_dfsdm_stop_dfsdm(adc->dfsdm); in stm32_dfsdm_single_conv()
1172 struct stm32_dfsdm_channel *ch = &adc->dfsdm->ch_list[chan->channel]; in stm32_dfsdm_write_raw()
1174 int ret = -EINVAL; in stm32_dfsdm_write_raw()
1176 switch (ch->src) { in stm32_dfsdm_write_raw()
1178 spi_freq = adc->dfsdm->spi_master_freq; in stm32_dfsdm_write_raw()
1182 spi_freq = adc->dfsdm->spi_master_freq / 2; in stm32_dfsdm_write_raw()
1185 spi_freq = adc->spi_freq; in stm32_dfsdm_write_raw()
1196 dev_dbg(&indio_dev->dev, in stm32_dfsdm_write_raw()
1198 adc->sample_freq, spi_freq / val); in stm32_dfsdm_write_raw()
1199 adc->oversamp = val; in stm32_dfsdm_write_raw()
1200 adc->sample_freq = spi_freq / val; in stm32_dfsdm_write_raw()
1207 return -EINVAL; in stm32_dfsdm_write_raw()
1218 return -EINVAL; in stm32_dfsdm_write_raw()
1233 ret = iio_hw_consumer_enable(adc->hwc); in stm32_dfsdm_read_raw()
1235 dev_err(&indio_dev->dev, in stm32_dfsdm_read_raw()
1237 __func__, chan->channel); in stm32_dfsdm_read_raw()
1242 iio_hw_consumer_disable(adc->hwc); in stm32_dfsdm_read_raw()
1244 dev_err(&indio_dev->dev, in stm32_dfsdm_read_raw()
1246 __func__, chan->channel); in stm32_dfsdm_read_raw()
1254 *val = adc->oversamp; in stm32_dfsdm_read_raw()
1259 *val = adc->sample_freq; in stm32_dfsdm_read_raw()
1264 return -EINVAL; in stm32_dfsdm_read_raw()
1270 return stm32_dfsdm_get_jextsel(indio_dev, trig) < 0 ? -EINVAL : 0; in stm32_dfsdm_validate_trigger()
1292 struct regmap *regmap = adc->dfsdm->regmap; in stm32_dfsdm_irq()
1295 regmap_read(regmap, DFSDM_ISR(adc->fl_id), &status); in stm32_dfsdm_irq()
1296 regmap_read(regmap, DFSDM_CR2(adc->fl_id), &int_en); in stm32_dfsdm_irq()
1300 regmap_read(regmap, DFSDM_RDATAR(adc->fl_id), adc->buffer); in stm32_dfsdm_irq()
1301 complete(&adc->completion); in stm32_dfsdm_irq()
1306 dev_warn(&indio_dev->dev, "Overrun detected\n"); in stm32_dfsdm_irq()
1307 regmap_update_bits(regmap, DFSDM_ICR(adc->fl_id), in stm32_dfsdm_irq()
1334 if (adc->dma_chan) { in stm32_dfsdm_dma_release()
1335 dma_free_coherent(adc->dma_chan->device->dev, in stm32_dfsdm_dma_release()
1337 adc->rx_buf, adc->dma_buf); in stm32_dfsdm_dma_release()
1338 dma_release_channel(adc->dma_chan); in stm32_dfsdm_dma_release()
1347 adc->dma_chan = dma_request_chan(dev, "rx"); in stm32_dfsdm_dma_request()
1348 if (IS_ERR(adc->dma_chan)) { in stm32_dfsdm_dma_request()
1349 int ret = PTR_ERR(adc->dma_chan); in stm32_dfsdm_dma_request()
1351 adc->dma_chan = NULL; in stm32_dfsdm_dma_request()
1355 adc->rx_buf = dma_alloc_coherent(adc->dma_chan->device->dev, in stm32_dfsdm_dma_request()
1357 &adc->dma_buf, GFP_KERNEL); in stm32_dfsdm_dma_request()
1358 if (!adc->rx_buf) { in stm32_dfsdm_dma_request()
1359 dma_release_channel(adc->dma_chan); in stm32_dfsdm_dma_request()
1360 return -ENOMEM; in stm32_dfsdm_dma_request()
1363 indio_dev->modes |= INDIO_BUFFER_SOFTWARE; in stm32_dfsdm_dma_request()
1364 indio_dev->setup_ops = &stm32_dfsdm_buffer_setup_ops; in stm32_dfsdm_dma_request()
1375 ret = stm32_dfsdm_channel_parse_of(adc->dfsdm, indio_dev, ch); in stm32_dfsdm_adc_chan_init_one()
1379 ch->type = IIO_VOLTAGE; in stm32_dfsdm_adc_chan_init_one()
1380 ch->indexed = 1; in stm32_dfsdm_adc_chan_init_one()
1384 * IIO_CHAN_INFO_OVERSAMPLING_RATIO: used to set oversampling in stm32_dfsdm_adc_chan_init_one()
1386 ch->info_mask_separate = BIT(IIO_CHAN_INFO_RAW); in stm32_dfsdm_adc_chan_init_one()
1387 ch->info_mask_shared_by_all = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) | in stm32_dfsdm_adc_chan_init_one()
1390 if (adc->dev_data->type == DFSDM_AUDIO) { in stm32_dfsdm_adc_chan_init_one()
1391 ch->ext_info = dfsdm_adc_audio_ext_info; in stm32_dfsdm_adc_chan_init_one()
1393 ch->scan_type.shift = 8; in stm32_dfsdm_adc_chan_init_one()
1395 ch->scan_type.sign = 's'; in stm32_dfsdm_adc_chan_init_one()
1396 ch->scan_type.realbits = 24; in stm32_dfsdm_adc_chan_init_one()
1397 ch->scan_type.storagebits = 32; in stm32_dfsdm_adc_chan_init_one()
1399 return stm32_dfsdm_chan_configure(adc->dfsdm, in stm32_dfsdm_adc_chan_init_one()
1400 &adc->dfsdm->ch_list[ch->channel]); in stm32_dfsdm_adc_chan_init_one()
1410 ch = devm_kzalloc(&indio_dev->dev, sizeof(*ch), GFP_KERNEL); in stm32_dfsdm_audio_init()
1412 return -ENOMEM; in stm32_dfsdm_audio_init()
1414 ch->scan_index = 0; in stm32_dfsdm_audio_init()
1418 dev_err(&indio_dev->dev, "Channels init failed\n"); in stm32_dfsdm_audio_init()
1421 ch->info_mask_separate = BIT(IIO_CHAN_INFO_SAMP_FREQ); in stm32_dfsdm_audio_init()
1423 d_ch = &adc->dfsdm->ch_list[ch->channel]; in stm32_dfsdm_audio_init()
1424 if (d_ch->src != DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL) in stm32_dfsdm_audio_init()
1425 adc->spi_freq = adc->dfsdm->spi_master_freq; in stm32_dfsdm_audio_init()
1427 indio_dev->num_channels = 1; in stm32_dfsdm_audio_init()
1428 indio_dev->channels = ch; in stm32_dfsdm_audio_init()
1440 adc->oversamp = DFSDM_DEFAULT_OVERSAMPLING; in stm32_dfsdm_adc_init()
1441 ret = stm32_dfsdm_compute_all_osrs(indio_dev, adc->oversamp); in stm32_dfsdm_adc_init()
1445 num_ch = of_property_count_u32_elems(indio_dev->dev.of_node, in stm32_dfsdm_adc_init()
1446 "st,adc-channels"); in stm32_dfsdm_adc_init()
1447 if (num_ch < 0 || num_ch > adc->dfsdm->num_chs) { in stm32_dfsdm_adc_init()
1448 dev_err(&indio_dev->dev, "Bad st,adc-channels\n"); in stm32_dfsdm_adc_init()
1449 return num_ch < 0 ? num_ch : -EINVAL; in stm32_dfsdm_adc_init()
1453 adc->hwc = devm_iio_hw_consumer_alloc(&indio_dev->dev); in stm32_dfsdm_adc_init()
1454 if (IS_ERR(adc->hwc)) in stm32_dfsdm_adc_init()
1455 return -EPROBE_DEFER; in stm32_dfsdm_adc_init()
1457 ch = devm_kcalloc(&indio_dev->dev, num_ch, sizeof(*ch), in stm32_dfsdm_adc_init()
1460 return -ENOMEM; in stm32_dfsdm_adc_init()
1466 dev_err(&indio_dev->dev, "Channels init failed\n"); in stm32_dfsdm_adc_init()
1471 indio_dev->num_channels = num_ch; in stm32_dfsdm_adc_init()
1472 indio_dev->channels = ch; in stm32_dfsdm_adc_init()
1474 init_completion(&adc->completion); in stm32_dfsdm_adc_init()
1479 if (ret != -ENODEV) in stm32_dfsdm_adc_init()
1492 dev_err(&indio_dev->dev, "buffer setup failed\n"); in stm32_dfsdm_adc_init()
1497 indio_dev->modes |= INDIO_HARDWARE_TRIGGERED; in stm32_dfsdm_adc_init()
1514 .compatible = "st,stm32-dfsdm-adc",
1518 .compatible = "st,stm32-dfsdm-dmic",
1526 struct device *dev = &pdev->dev; in stm32_dfsdm_adc_probe()
1528 struct device_node *np = dev->of_node; in stm32_dfsdm_adc_probe()
1538 return -ENOMEM; in stm32_dfsdm_adc_probe()
1542 adc->dfsdm = dev_get_drvdata(dev->parent); in stm32_dfsdm_adc_probe()
1544 iio->dev.of_node = np; in stm32_dfsdm_adc_probe()
1545 iio->modes = INDIO_DIRECT_MODE; in stm32_dfsdm_adc_probe()
1549 ret = of_property_read_u32(dev->of_node, "reg", &adc->fl_id); in stm32_dfsdm_adc_probe()
1550 if (ret != 0 || adc->fl_id >= adc->dfsdm->num_fls) { in stm32_dfsdm_adc_probe()
1552 return -EINVAL; in stm32_dfsdm_adc_probe()
1555 name = devm_kzalloc(dev, sizeof("dfsdm-adc0"), GFP_KERNEL); in stm32_dfsdm_adc_probe()
1557 return -ENOMEM; in stm32_dfsdm_adc_probe()
1558 if (dev_data->type == DFSDM_AUDIO) { in stm32_dfsdm_adc_probe()
1559 iio->info = &stm32_dfsdm_info_audio; in stm32_dfsdm_adc_probe()
1560 snprintf(name, sizeof("dfsdm-pdm0"), "dfsdm-pdm%d", adc->fl_id); in stm32_dfsdm_adc_probe()
1562 iio->info = &stm32_dfsdm_info_adc; in stm32_dfsdm_adc_probe()
1563 snprintf(name, sizeof("dfsdm-adc0"), "dfsdm-adc%d", adc->fl_id); in stm32_dfsdm_adc_probe()
1565 iio->name = name; in stm32_dfsdm_adc_probe()
1576 0, pdev->name, iio); in stm32_dfsdm_adc_probe()
1582 ret = of_property_read_u32(dev->of_node, "st,filter-order", &val); in stm32_dfsdm_adc_probe()
1588 adc->dfsdm->fl_list[adc->fl_id].ford = val; in stm32_dfsdm_adc_probe()
1590 ret = of_property_read_u32(dev->of_node, "st,filter0-sync", &val); in stm32_dfsdm_adc_probe()
1592 adc->dfsdm->fl_list[adc->fl_id].sync_mode = val; in stm32_dfsdm_adc_probe()
1594 adc->dev_data = dev_data; in stm32_dfsdm_adc_probe()
1595 ret = dev_data->init(dev, iio); in stm32_dfsdm_adc_probe()
1603 if (dev_data->type == DFSDM_AUDIO) { in stm32_dfsdm_adc_probe()
1626 if (adc->dev_data->type == DFSDM_AUDIO) in stm32_dfsdm_adc_remove()
1627 of_platform_depopulate(&pdev->dev); in stm32_dfsdm_adc_remove()
1653 for (i = 0; i < indio_dev->num_channels; i++) { in stm32_dfsdm_adc_resume()
1654 chan = indio_dev->channels + i; in stm32_dfsdm_adc_resume()
1655 ch = &adc->dfsdm->ch_list[chan->channel]; in stm32_dfsdm_adc_resume()
1656 ret = stm32_dfsdm_chan_configure(adc->dfsdm, ch); in stm32_dfsdm_adc_resume()
1673 .name = "stm32-dfsdm-adc",