Lines Matching +full:adc +full:- +full:channel +full:- +full:clk +full:- +full:src

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
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"
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)
77 /* ADC specific */
102 /* DFSDM channel serial interface type */
111 /* DFSDM channel clock source */
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()
211 if (fl->ford == DFSDM_FASTSINC_ORDER) { 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 __func__, 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()
315 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); in stm32_dfsdm_compute_all_osrs() local
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()
336 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); in stm32_dfsdm_start_channel() local
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()
356 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); in stm32_dfsdm_stop_channel() local
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()
391 static int stm32_dfsdm_start_filter(struct stm32_dfsdm_adc *adc, in stm32_dfsdm_start_filter() argument
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()
426 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); in stm32_dfsdm_filter_set_trig() local
427 struct regmap *regmap = adc->dfsdm->regmap; in stm32_dfsdm_filter_set_trig()
455 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); in stm32_dfsdm_channels_configure() local
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 && in stm32_dfsdm_channels_configure()
470 (indio_dev->currentmode & INDIO_BUFFER_SOFTWARE)) { in stm32_dfsdm_channels_configure()
471 if (fl->flo[1].res >= fl->flo[0].res) { in stm32_dfsdm_channels_configure()
472 fl->fast = 1; in stm32_dfsdm_channels_configure()
473 flo = &fl->flo[1]; in stm32_dfsdm_channels_configure()
477 if (!flo->res) in stm32_dfsdm_channels_configure()
478 return -EINVAL; in stm32_dfsdm_channels_configure()
480 dev_dbg(&indio_dev->dev, "Samples actual resolution: %d bits", in stm32_dfsdm_channels_configure()
481 min(flo->bits, (u32)DFSDM_DATA_RES - 1)); in stm32_dfsdm_channels_configure()
483 for_each_set_bit(bit, &adc->smask, in stm32_dfsdm_channels_configure()
484 sizeof(adc->smask) * BITS_PER_BYTE) { in stm32_dfsdm_channels_configure()
485 chan = indio_dev->channels + bit; in stm32_dfsdm_channels_configure()
488 DFSDM_CHCFGR2(chan->channel), in stm32_dfsdm_channels_configure()
490 DFSDM_CHCFGR2_DTRBS(flo->rshift)); in stm32_dfsdm_channels_configure()
502 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); in stm32_dfsdm_filter_configure() local
503 struct regmap *regmap = adc->dfsdm->regmap; in stm32_dfsdm_filter_configure()
504 struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[fl_id]; in stm32_dfsdm_filter_configure()
505 struct stm32_dfsdm_filter_osr *flo = &fl->flo[fl->fast]; in stm32_dfsdm_filter_configure()
513 DFSDM_FCR_IOSR(flo->iosr - 1)); in stm32_dfsdm_filter_configure()
519 DFSDM_FCR_FOSR(flo->fosr - 1)); in stm32_dfsdm_filter_configure()
524 DFSDM_FCR_FORD(fl->ford)); in stm32_dfsdm_filter_configure()
534 DFSDM_CR1_FAST(fl->fast)); in stm32_dfsdm_filter_configure()
540 * ---------------------------------------------------------------- in stm32_dfsdm_filter_configure()
543 * --------------|---------|--------------|----------|------------| in stm32_dfsdm_filter_configure()
546 * --------------|---------|--------------|----------|------------| in stm32_dfsdm_filter_configure()
549 * --------------|---------|--------------|----------|------------| in stm32_dfsdm_filter_configure()
552 * --------------|---------|--------------|----------|------------| in stm32_dfsdm_filter_configure()
555 * ---------------------------------------------------------------- in stm32_dfsdm_filter_configure()
557 if (adc->nconv == 1 && !trig) { in stm32_dfsdm_filter_configure()
558 bit = __ffs(adc->smask); in stm32_dfsdm_filter_configure()
559 chan = indio_dev->channels + bit; in stm32_dfsdm_filter_configure()
561 /* Use regular conversion for single channel without trigger */ in stm32_dfsdm_filter_configure()
562 cr1 = DFSDM_CR1_RCH(chan->channel); in stm32_dfsdm_filter_configure()
564 /* Continuous conversions triggered by SPI clk in buffer mode */ in stm32_dfsdm_filter_configure()
565 if (indio_dev->currentmode & INDIO_BUFFER_SOFTWARE) in stm32_dfsdm_filter_configure()
568 cr1 |= DFSDM_CR1_RSYNC(fl->sync_mode); in stm32_dfsdm_filter_configure()
571 for_each_set_bit(bit, &adc->smask, in stm32_dfsdm_filter_configure()
572 sizeof(adc->smask) * BITS_PER_BYTE) { in stm32_dfsdm_filter_configure()
573 chan = indio_dev->channels + bit; in stm32_dfsdm_filter_configure()
574 jchg |= BIT(chan->channel); in stm32_dfsdm_filter_configure()
581 cr1 = DFSDM_CR1_JSCAN((adc->nconv > 1) ? 1 : 0); in stm32_dfsdm_filter_configure()
586 * - conversions in sync with filter 0 in stm32_dfsdm_filter_configure()
587 * - triggered conversions in stm32_dfsdm_filter_configure()
589 if (!fl->sync_mode && !trig) in stm32_dfsdm_filter_configure()
590 return -EINVAL; in stm32_dfsdm_filter_configure()
591 cr1 |= DFSDM_CR1_JSYNC(fl->sync_mode); in stm32_dfsdm_filter_configure()
604 int chan_idx = ch->scan_index; in stm32_dfsdm_channel_parse_of()
607 ret = of_property_read_u32_index(indio_dev->dev.of_node, in stm32_dfsdm_channel_parse_of()
608 "st,adc-channels", chan_idx, in stm32_dfsdm_channel_parse_of()
609 &ch->channel); in stm32_dfsdm_channel_parse_of()
611 dev_err(&indio_dev->dev, in stm32_dfsdm_channel_parse_of()
612 " Error parsing 'st,adc-channels' for idx %d\n", in stm32_dfsdm_channel_parse_of()
616 if (ch->channel >= dfsdm->num_chs) { in stm32_dfsdm_channel_parse_of()
617 dev_err(&indio_dev->dev, in stm32_dfsdm_channel_parse_of()
618 " Error bad channel number %d (max = %d)\n", in stm32_dfsdm_channel_parse_of()
619 ch->channel, dfsdm->num_chs); in stm32_dfsdm_channel_parse_of()
620 return -EINVAL; in stm32_dfsdm_channel_parse_of()
623 ret = of_property_read_string_index(indio_dev->dev.of_node, in stm32_dfsdm_channel_parse_of()
624 "st,adc-channel-names", chan_idx, in stm32_dfsdm_channel_parse_of()
625 &ch->datasheet_name); in stm32_dfsdm_channel_parse_of()
627 dev_err(&indio_dev->dev, in stm32_dfsdm_channel_parse_of()
628 " Error parsing 'st,adc-channel-names' for idx %d\n", in stm32_dfsdm_channel_parse_of()
633 df_ch = &dfsdm->ch_list[ch->channel]; in stm32_dfsdm_channel_parse_of()
634 df_ch->id = ch->channel; in stm32_dfsdm_channel_parse_of()
636 ret = of_property_read_string_index(indio_dev->dev.of_node, in stm32_dfsdm_channel_parse_of()
637 "st,adc-channel-types", chan_idx, in stm32_dfsdm_channel_parse_of()
646 df_ch->type = val; in stm32_dfsdm_channel_parse_of()
648 ret = of_property_read_string_index(indio_dev->dev.of_node, in stm32_dfsdm_channel_parse_of()
649 "st,adc-channel-clk-src", chan_idx, in stm32_dfsdm_channel_parse_of()
658 df_ch->src = val; in stm32_dfsdm_channel_parse_of()
660 ret = of_property_read_u32_index(indio_dev->dev.of_node, in stm32_dfsdm_channel_parse_of()
661 "st,adc-alt-channel", chan_idx, in stm32_dfsdm_channel_parse_of()
662 &df_ch->alt_si); in stm32_dfsdm_channel_parse_of()
664 df_ch->alt_si = 0; in stm32_dfsdm_channel_parse_of()
674 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); in dfsdm_adc_audio_get_spiclk() local
676 return snprintf(buf, PAGE_SIZE, "%d\n", adc->spi_freq); in dfsdm_adc_audio_get_spiclk()
683 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); in dfsdm_adc_set_samp_freq() local
689 dev_dbg(&indio_dev->dev, in dfsdm_adc_set_samp_freq()
697 adc->sample_freq = spi_freq / oversamp; in dfsdm_adc_set_samp_freq()
698 adc->oversamp = oversamp; in dfsdm_adc_set_samp_freq()
708 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); in dfsdm_adc_audio_set_spiclk() local
709 struct stm32_dfsdm_channel *ch = &adc->dfsdm->ch_list[chan->channel]; in dfsdm_adc_audio_set_spiclk()
710 unsigned int sample_freq = adc->sample_freq; in dfsdm_adc_audio_set_spiclk()
714 dev_err(&indio_dev->dev, "enter %s\n", __func__); in dfsdm_adc_audio_set_spiclk()
716 if (ch->src != DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL) in dfsdm_adc_audio_set_spiclk()
717 return -EPERM; in dfsdm_adc_audio_set_spiclk()
724 return -EINVAL; in dfsdm_adc_audio_set_spiclk()
731 adc->spi_freq = spi_freq; in dfsdm_adc_audio_set_spiclk()
739 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); in stm32_dfsdm_start_conv() local
740 struct regmap *regmap = adc->dfsdm->regmap; in stm32_dfsdm_start_conv()
743 ret = stm32_dfsdm_channels_configure(indio_dev, adc->fl_id, trig); in stm32_dfsdm_start_conv()
751 ret = stm32_dfsdm_filter_configure(indio_dev, adc->fl_id, trig); in stm32_dfsdm_start_conv()
755 ret = stm32_dfsdm_start_filter(adc, adc->fl_id, trig); in stm32_dfsdm_start_conv()
762 regmap_update_bits(regmap, DFSDM_CR1(adc->fl_id), in stm32_dfsdm_start_conv()
772 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); in stm32_dfsdm_stop_conv() local
773 struct regmap *regmap = adc->dfsdm->regmap; in stm32_dfsdm_stop_conv()
775 stm32_dfsdm_stop_filter(adc->dfsdm, adc->fl_id); in stm32_dfsdm_stop_conv()
777 regmap_update_bits(regmap, DFSDM_CR1(adc->fl_id), in stm32_dfsdm_stop_conv()
786 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); in stm32_dfsdm_set_watermark() local
793 * - always one buffer (period) DMA is working on in stm32_dfsdm_set_watermark()
794 * - one buffer (period) driver pushed to ASoC side. in stm32_dfsdm_set_watermark()
797 adc->buf_sz = min(rx_buf_sz, watermark * 2 * adc->nconv); in stm32_dfsdm_set_watermark()
802 static unsigned int stm32_dfsdm_adc_dma_residue(struct stm32_dfsdm_adc *adc) in stm32_dfsdm_adc_dma_residue() argument
807 status = dmaengine_tx_status(adc->dma_chan, in stm32_dfsdm_adc_dma_residue()
808 adc->dma_chan->cookie, in stm32_dfsdm_adc_dma_residue()
812 unsigned int i = adc->buf_sz - state.residue; in stm32_dfsdm_adc_dma_residue()
816 if (i >= adc->bufi) in stm32_dfsdm_adc_dma_residue()
817 size = i - adc->bufi; in stm32_dfsdm_adc_dma_residue()
819 size = adc->buf_sz + i - adc->bufi; in stm32_dfsdm_adc_dma_residue()
827 static inline void stm32_dfsdm_process_data(struct stm32_dfsdm_adc *adc, in stm32_dfsdm_process_data() argument
830 struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[adc->fl_id]; in stm32_dfsdm_process_data()
831 struct stm32_dfsdm_filter_osr *flo = &fl->flo[fl->fast]; in stm32_dfsdm_process_data()
832 unsigned int i = adc->nconv; in stm32_dfsdm_process_data()
835 while (i--) { in stm32_dfsdm_process_data()
836 /* Mask 8 LSB that contains the channel ID */ in stm32_dfsdm_process_data()
838 /* Convert 2^(n-1) sample to 2^(n-1)-1 to avoid wrap-around */ in stm32_dfsdm_process_data()
839 if (*ptr > flo->max) in stm32_dfsdm_process_data()
840 *ptr -= 1; in stm32_dfsdm_process_data()
845 *ptr <<= flo->lshift; in stm32_dfsdm_process_data()
854 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); in stm32_dfsdm_dma_buffer_done() local
855 int available = stm32_dfsdm_adc_dma_residue(adc); in stm32_dfsdm_dma_buffer_done()
867 dev_dbg(&indio_dev->dev, "%s: pos = %d, available = %d\n", __func__, in stm32_dfsdm_dma_buffer_done()
868 adc->bufi, available); in stm32_dfsdm_dma_buffer_done()
869 old_pos = adc->bufi; in stm32_dfsdm_dma_buffer_done()
871 while (available >= indio_dev->scan_bytes) { in stm32_dfsdm_dma_buffer_done()
872 s32 *buffer = (s32 *)&adc->rx_buf[adc->bufi]; in stm32_dfsdm_dma_buffer_done()
874 stm32_dfsdm_process_data(adc, buffer); in stm32_dfsdm_dma_buffer_done()
876 available -= indio_dev->scan_bytes; in stm32_dfsdm_dma_buffer_done()
877 adc->bufi += indio_dev->scan_bytes; in stm32_dfsdm_dma_buffer_done()
878 if (adc->bufi >= adc->buf_sz) { in stm32_dfsdm_dma_buffer_done()
879 if (adc->cb) in stm32_dfsdm_dma_buffer_done()
880 adc->cb(&adc->rx_buf[old_pos], in stm32_dfsdm_dma_buffer_done()
881 adc->buf_sz - old_pos, adc->cb_priv); in stm32_dfsdm_dma_buffer_done()
882 adc->bufi = 0; in stm32_dfsdm_dma_buffer_done()
894 if (adc->dev_data->type == DFSDM_IIO) in stm32_dfsdm_dma_buffer_done()
897 if (adc->cb) in stm32_dfsdm_dma_buffer_done()
898 adc->cb(&adc->rx_buf[old_pos], adc->bufi - old_pos, in stm32_dfsdm_dma_buffer_done()
899 adc->cb_priv); in stm32_dfsdm_dma_buffer_done()
904 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); in stm32_dfsdm_adc_dma_start() local
906 * The DFSDM supports half-word transfers. However, for 16 bits record, in stm32_dfsdm_adc_dma_start()
911 .src_addr = (dma_addr_t)adc->dfsdm->phys_base, in stm32_dfsdm_adc_dma_start()
918 if (!adc->dma_chan) in stm32_dfsdm_adc_dma_start()
919 return -EINVAL; in stm32_dfsdm_adc_dma_start()
921 dev_dbg(&indio_dev->dev, "%s size=%d watermark=%d\n", __func__, in stm32_dfsdm_adc_dma_start()
922 adc->buf_sz, adc->buf_sz / 2); in stm32_dfsdm_adc_dma_start()
924 if (adc->nconv == 1 && !indio_dev->trig) in stm32_dfsdm_adc_dma_start()
925 config.src_addr += DFSDM_RDATAR(adc->fl_id); in stm32_dfsdm_adc_dma_start()
927 config.src_addr += DFSDM_JDATAR(adc->fl_id); in stm32_dfsdm_adc_dma_start()
928 ret = dmaengine_slave_config(adc->dma_chan, &config); in stm32_dfsdm_adc_dma_start()
933 desc = dmaengine_prep_dma_cyclic(adc->dma_chan, in stm32_dfsdm_adc_dma_start()
934 adc->dma_buf, in stm32_dfsdm_adc_dma_start()
935 adc->buf_sz, adc->buf_sz / 2, in stm32_dfsdm_adc_dma_start()
939 return -EBUSY; in stm32_dfsdm_adc_dma_start()
941 desc->callback = stm32_dfsdm_dma_buffer_done; in stm32_dfsdm_adc_dma_start()
942 desc->callback_param = indio_dev; in stm32_dfsdm_adc_dma_start()
950 dma_async_issue_pending(adc->dma_chan); in stm32_dfsdm_adc_dma_start()
952 if (adc->nconv == 1 && !indio_dev->trig) { in stm32_dfsdm_adc_dma_start()
954 ret = regmap_update_bits(adc->dfsdm->regmap, in stm32_dfsdm_adc_dma_start()
955 DFSDM_CR1(adc->fl_id), in stm32_dfsdm_adc_dma_start()
960 ret = regmap_update_bits(adc->dfsdm->regmap, in stm32_dfsdm_adc_dma_start()
961 DFSDM_CR1(adc->fl_id), in stm32_dfsdm_adc_dma_start()
972 dmaengine_terminate_all(adc->dma_chan); in stm32_dfsdm_adc_dma_start()
979 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); in stm32_dfsdm_adc_dma_stop() local
981 if (!adc->dma_chan) in stm32_dfsdm_adc_dma_stop()
984 regmap_update_bits(adc->dfsdm->regmap, DFSDM_CR1(adc->fl_id), in stm32_dfsdm_adc_dma_stop()
986 dmaengine_terminate_all(adc->dma_chan); in stm32_dfsdm_adc_dma_stop()
992 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); in stm32_dfsdm_update_scan_mode() local
994 adc->nconv = bitmap_weight(scan_mask, indio_dev->masklength); in stm32_dfsdm_update_scan_mode()
995 adc->smask = *scan_mask; in stm32_dfsdm_update_scan_mode()
997 dev_dbg(&indio_dev->dev, "nconv=%d mask=%lx\n", adc->nconv, *scan_mask); in stm32_dfsdm_update_scan_mode()
1004 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); in stm32_dfsdm_postenable() local
1007 /* Reset adc buffer index */ in stm32_dfsdm_postenable()
1008 adc->bufi = 0; in stm32_dfsdm_postenable()
1010 if (adc->hwc) { in stm32_dfsdm_postenable()
1011 ret = iio_hw_consumer_enable(adc->hwc); in stm32_dfsdm_postenable()
1016 ret = stm32_dfsdm_start_dfsdm(adc->dfsdm); in stm32_dfsdm_postenable()
1022 dev_err(&indio_dev->dev, "Can't start DMA\n"); in stm32_dfsdm_postenable()
1026 ret = stm32_dfsdm_start_conv(indio_dev, indio_dev->trig); in stm32_dfsdm_postenable()
1028 dev_err(&indio_dev->dev, "Can't start conversion\n"); in stm32_dfsdm_postenable()
1037 stm32_dfsdm_stop_dfsdm(adc->dfsdm); in stm32_dfsdm_postenable()
1039 if (adc->hwc) in stm32_dfsdm_postenable()
1040 iio_hw_consumer_disable(adc->hwc); in stm32_dfsdm_postenable()
1047 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); in stm32_dfsdm_predisable() local
1053 stm32_dfsdm_stop_dfsdm(adc->dfsdm); in stm32_dfsdm_predisable()
1055 if (adc->hwc) in stm32_dfsdm_predisable()
1056 iio_hw_consumer_disable(adc->hwc); in stm32_dfsdm_predisable()
1067 * stm32_dfsdm_get_buff_cb() - register a callback that will be called when
1072 * - data: pointer to data buffer
1073 * - size: size in byte of the data buffer
1074 * - private: pointer to consumer private structure.
1082 struct stm32_dfsdm_adc *adc; in stm32_dfsdm_get_buff_cb() local
1085 return -EINVAL; in stm32_dfsdm_get_buff_cb()
1086 adc = iio_priv(iio_dev); in stm32_dfsdm_get_buff_cb()
1088 adc->cb = cb; in stm32_dfsdm_get_buff_cb()
1089 adc->cb_priv = private; in stm32_dfsdm_get_buff_cb()
1096 * stm32_dfsdm_release_buff_cb - unregister buffer callback
1102 struct stm32_dfsdm_adc *adc; in stm32_dfsdm_release_buff_cb() local
1105 return -EINVAL; in stm32_dfsdm_release_buff_cb()
1106 adc = iio_priv(iio_dev); in stm32_dfsdm_release_buff_cb()
1108 adc->cb = NULL; in stm32_dfsdm_release_buff_cb()
1109 adc->cb_priv = NULL; in stm32_dfsdm_release_buff_cb()
1118 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); in stm32_dfsdm_single_conv() local
1122 reinit_completion(&adc->completion); in stm32_dfsdm_single_conv()
1124 adc->buffer = res; in stm32_dfsdm_single_conv()
1126 ret = stm32_dfsdm_start_dfsdm(adc->dfsdm); in stm32_dfsdm_single_conv()
1130 ret = regmap_update_bits(adc->dfsdm->regmap, DFSDM_CR2(adc->fl_id), in stm32_dfsdm_single_conv()
1135 adc->nconv = 1; in stm32_dfsdm_single_conv()
1136 adc->smask = BIT(chan->scan_index); in stm32_dfsdm_single_conv()
1139 regmap_update_bits(adc->dfsdm->regmap, DFSDM_CR2(adc->fl_id), in stm32_dfsdm_single_conv()
1144 timeout = wait_for_completion_interruptible_timeout(&adc->completion, in stm32_dfsdm_single_conv()
1148 regmap_update_bits(adc->dfsdm->regmap, DFSDM_CR2(adc->fl_id), in stm32_dfsdm_single_conv()
1152 ret = -ETIMEDOUT; in stm32_dfsdm_single_conv()
1160 stm32_dfsdm_process_data(adc, res); in stm32_dfsdm_single_conv()
1163 stm32_dfsdm_stop_dfsdm(adc->dfsdm); in stm32_dfsdm_single_conv()
1172 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); in stm32_dfsdm_write_raw() local
1173 struct stm32_dfsdm_channel *ch = &adc->dfsdm->ch_list[chan->channel]; in stm32_dfsdm_write_raw()
1175 int ret = -EINVAL; in stm32_dfsdm_write_raw()
1177 switch (ch->src) { in stm32_dfsdm_write_raw()
1179 spi_freq = adc->dfsdm->spi_master_freq; in stm32_dfsdm_write_raw()
1183 spi_freq = adc->dfsdm->spi_master_freq / 2; in stm32_dfsdm_write_raw()
1186 spi_freq = adc->spi_freq; in stm32_dfsdm_write_raw()
1197 dev_dbg(&indio_dev->dev, in stm32_dfsdm_write_raw()
1199 adc->sample_freq, spi_freq / val); in stm32_dfsdm_write_raw()
1200 adc->oversamp = val; in stm32_dfsdm_write_raw()
1201 adc->sample_freq = spi_freq / val; in stm32_dfsdm_write_raw()
1208 return -EINVAL; in stm32_dfsdm_write_raw()
1219 return -EINVAL; in stm32_dfsdm_write_raw()
1226 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); in stm32_dfsdm_read_raw() local
1234 ret = iio_hw_consumer_enable(adc->hwc); in stm32_dfsdm_read_raw()
1236 dev_err(&indio_dev->dev, in stm32_dfsdm_read_raw()
1237 "%s: IIO enable failed (channel %d)\n", in stm32_dfsdm_read_raw()
1238 __func__, chan->channel); in stm32_dfsdm_read_raw()
1243 iio_hw_consumer_disable(adc->hwc); in stm32_dfsdm_read_raw()
1245 dev_err(&indio_dev->dev, in stm32_dfsdm_read_raw()
1246 "%s: Conversion failed (channel %d)\n", in stm32_dfsdm_read_raw()
1247 __func__, chan->channel); in stm32_dfsdm_read_raw()
1255 *val = adc->oversamp; in stm32_dfsdm_read_raw()
1260 *val = adc->sample_freq; in stm32_dfsdm_read_raw()
1265 return -EINVAL; in stm32_dfsdm_read_raw()
1271 return stm32_dfsdm_get_jextsel(indio_dev, trig) < 0 ? -EINVAL : 0; in stm32_dfsdm_validate_trigger()
1292 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); in stm32_dfsdm_irq() local
1293 struct regmap *regmap = adc->dfsdm->regmap; in stm32_dfsdm_irq()
1296 regmap_read(regmap, DFSDM_ISR(adc->fl_id), &status); in stm32_dfsdm_irq()
1297 regmap_read(regmap, DFSDM_CR2(adc->fl_id), &int_en); in stm32_dfsdm_irq()
1301 regmap_read(regmap, DFSDM_RDATAR(adc->fl_id), adc->buffer); in stm32_dfsdm_irq()
1302 complete(&adc->completion); in stm32_dfsdm_irq()
1307 dev_warn(&indio_dev->dev, "Overrun detected\n"); in stm32_dfsdm_irq()
1308 regmap_update_bits(regmap, DFSDM_ICR(adc->fl_id), in stm32_dfsdm_irq()
1321 /* spi_clk_freq : clock freq on SPI/manchester bus used by channel */
1333 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); in stm32_dfsdm_dma_release() local
1335 if (adc->dma_chan) { in stm32_dfsdm_dma_release()
1336 dma_free_coherent(adc->dma_chan->device->dev, in stm32_dfsdm_dma_release()
1338 adc->rx_buf, adc->dma_buf); in stm32_dfsdm_dma_release()
1339 dma_release_channel(adc->dma_chan); in stm32_dfsdm_dma_release()
1346 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); in stm32_dfsdm_dma_request() local
1348 adc->dma_chan = dma_request_chan(dev, "rx"); in stm32_dfsdm_dma_request()
1349 if (IS_ERR(adc->dma_chan)) { in stm32_dfsdm_dma_request()
1350 int ret = PTR_ERR(adc->dma_chan); in stm32_dfsdm_dma_request()
1352 adc->dma_chan = NULL; in stm32_dfsdm_dma_request()
1356 adc->rx_buf = dma_alloc_coherent(adc->dma_chan->device->dev, in stm32_dfsdm_dma_request()
1358 &adc->dma_buf, GFP_KERNEL); in stm32_dfsdm_dma_request()
1359 if (!adc->rx_buf) { in stm32_dfsdm_dma_request()
1360 dma_release_channel(adc->dma_chan); in stm32_dfsdm_dma_request()
1361 return -ENOMEM; in stm32_dfsdm_dma_request()
1364 indio_dev->modes |= INDIO_BUFFER_SOFTWARE; in stm32_dfsdm_dma_request()
1365 indio_dev->setup_ops = &stm32_dfsdm_buffer_setup_ops; in stm32_dfsdm_dma_request()
1373 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); in stm32_dfsdm_adc_chan_init_one() local
1376 ret = stm32_dfsdm_channel_parse_of(adc->dfsdm, indio_dev, ch); in stm32_dfsdm_adc_chan_init_one()
1380 ch->type = IIO_VOLTAGE; in stm32_dfsdm_adc_chan_init_one()
1381 ch->indexed = 1; in stm32_dfsdm_adc_chan_init_one()
1387 ch->info_mask_separate = BIT(IIO_CHAN_INFO_RAW); in stm32_dfsdm_adc_chan_init_one()
1388 ch->info_mask_shared_by_all = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) | in stm32_dfsdm_adc_chan_init_one()
1391 if (adc->dev_data->type == DFSDM_AUDIO) { in stm32_dfsdm_adc_chan_init_one()
1392 ch->ext_info = dfsdm_adc_audio_ext_info; in stm32_dfsdm_adc_chan_init_one()
1394 ch->scan_type.shift = 8; in stm32_dfsdm_adc_chan_init_one()
1396 ch->scan_type.sign = 's'; in stm32_dfsdm_adc_chan_init_one()
1397 ch->scan_type.realbits = 24; in stm32_dfsdm_adc_chan_init_one()
1398 ch->scan_type.storagebits = 32; in stm32_dfsdm_adc_chan_init_one()
1400 return stm32_dfsdm_chan_configure(adc->dfsdm, in stm32_dfsdm_adc_chan_init_one()
1401 &adc->dfsdm->ch_list[ch->channel]); in stm32_dfsdm_adc_chan_init_one()
1407 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); in stm32_dfsdm_audio_init() local
1411 ch = devm_kzalloc(&indio_dev->dev, sizeof(*ch), GFP_KERNEL); in stm32_dfsdm_audio_init()
1413 return -ENOMEM; in stm32_dfsdm_audio_init()
1415 ch->scan_index = 0; in stm32_dfsdm_audio_init()
1419 dev_err(&indio_dev->dev, "Channels init failed\n"); in stm32_dfsdm_audio_init()
1422 ch->info_mask_separate = BIT(IIO_CHAN_INFO_SAMP_FREQ); in stm32_dfsdm_audio_init()
1424 d_ch = &adc->dfsdm->ch_list[ch->channel]; in stm32_dfsdm_audio_init()
1425 if (d_ch->src != DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL) in stm32_dfsdm_audio_init()
1426 adc->spi_freq = adc->dfsdm->spi_master_freq; in stm32_dfsdm_audio_init()
1428 indio_dev->num_channels = 1; in stm32_dfsdm_audio_init()
1429 indio_dev->channels = ch; in stm32_dfsdm_audio_init()
1437 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); in stm32_dfsdm_adc_init() local
1441 adc->oversamp = DFSDM_DEFAULT_OVERSAMPLING; in stm32_dfsdm_adc_init()
1442 ret = stm32_dfsdm_compute_all_osrs(indio_dev, adc->oversamp); in stm32_dfsdm_adc_init()
1446 num_ch = of_property_count_u32_elems(indio_dev->dev.of_node, in stm32_dfsdm_adc_init()
1447 "st,adc-channels"); in stm32_dfsdm_adc_init()
1448 if (num_ch < 0 || num_ch > adc->dfsdm->num_chs) { in stm32_dfsdm_adc_init()
1449 dev_err(&indio_dev->dev, "Bad st,adc-channels\n"); in stm32_dfsdm_adc_init()
1450 return num_ch < 0 ? num_ch : -EINVAL; in stm32_dfsdm_adc_init()
1454 adc->hwc = devm_iio_hw_consumer_alloc(&indio_dev->dev); in stm32_dfsdm_adc_init()
1455 if (IS_ERR(adc->hwc)) in stm32_dfsdm_adc_init()
1456 return -EPROBE_DEFER; in stm32_dfsdm_adc_init()
1458 ch = devm_kcalloc(&indio_dev->dev, num_ch, sizeof(*ch), in stm32_dfsdm_adc_init()
1461 return -ENOMEM; in stm32_dfsdm_adc_init()
1467 dev_err(&indio_dev->dev, "Channels init failed\n"); in stm32_dfsdm_adc_init()
1472 indio_dev->num_channels = num_ch; in stm32_dfsdm_adc_init()
1473 indio_dev->channels = ch; in stm32_dfsdm_adc_init()
1475 init_completion(&adc->completion); in stm32_dfsdm_adc_init()
1480 if (ret != -ENODEV) in stm32_dfsdm_adc_init()
1482 "DMA channel request failed with\n"); in stm32_dfsdm_adc_init()
1493 dev_err(&indio_dev->dev, "buffer setup failed\n"); in stm32_dfsdm_adc_init()
1498 indio_dev->modes |= INDIO_HARDWARE_TRIGGERED; in stm32_dfsdm_adc_init()
1515 .compatible = "st,stm32-dfsdm-adc",
1519 .compatible = "st,stm32-dfsdm-dmic",
1527 struct device *dev = &pdev->dev; in stm32_dfsdm_adc_probe()
1528 struct stm32_dfsdm_adc *adc; in stm32_dfsdm_adc_probe() local
1529 struct device_node *np = dev->of_node; in stm32_dfsdm_adc_probe()
1536 iio = devm_iio_device_alloc(dev, sizeof(*adc)); in stm32_dfsdm_adc_probe()
1539 return -ENOMEM; in stm32_dfsdm_adc_probe()
1542 adc = iio_priv(iio); in stm32_dfsdm_adc_probe()
1543 adc->dfsdm = dev_get_drvdata(dev->parent); in stm32_dfsdm_adc_probe()
1545 iio->dev.of_node = np; in stm32_dfsdm_adc_probe()
1546 iio->modes = INDIO_DIRECT_MODE; in stm32_dfsdm_adc_probe()
1550 ret = of_property_read_u32(dev->of_node, "reg", &adc->fl_id); in stm32_dfsdm_adc_probe()
1551 if (ret != 0 || adc->fl_id >= adc->dfsdm->num_fls) { in stm32_dfsdm_adc_probe()
1553 return -EINVAL; in stm32_dfsdm_adc_probe()
1556 name = devm_kzalloc(dev, sizeof("dfsdm-adc0"), GFP_KERNEL); in stm32_dfsdm_adc_probe()
1558 return -ENOMEM; in stm32_dfsdm_adc_probe()
1559 if (dev_data->type == DFSDM_AUDIO) { in stm32_dfsdm_adc_probe()
1560 iio->info = &stm32_dfsdm_info_audio; in stm32_dfsdm_adc_probe()
1561 snprintf(name, sizeof("dfsdm-pdm0"), "dfsdm-pdm%d", adc->fl_id); in stm32_dfsdm_adc_probe()
1563 iio->info = &stm32_dfsdm_info_adc; in stm32_dfsdm_adc_probe()
1564 snprintf(name, sizeof("dfsdm-adc0"), "dfsdm-adc%d", adc->fl_id); in stm32_dfsdm_adc_probe()
1566 iio->name = name; in stm32_dfsdm_adc_probe()
1577 0, pdev->name, iio); in stm32_dfsdm_adc_probe()
1583 ret = of_property_read_u32(dev->of_node, "st,filter-order", &val); in stm32_dfsdm_adc_probe()
1589 adc->dfsdm->fl_list[adc->fl_id].ford = val; in stm32_dfsdm_adc_probe()
1591 ret = of_property_read_u32(dev->of_node, "st,filter0-sync", &val); in stm32_dfsdm_adc_probe()
1593 adc->dfsdm->fl_list[adc->fl_id].sync_mode = val; in stm32_dfsdm_adc_probe()
1595 adc->dev_data = dev_data; in stm32_dfsdm_adc_probe()
1596 ret = dev_data->init(dev, iio); in stm32_dfsdm_adc_probe()
1604 if (dev_data->type == DFSDM_AUDIO) { in stm32_dfsdm_adc_probe()
1625 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); in stm32_dfsdm_adc_remove() local
1627 if (adc->dev_data->type == DFSDM_AUDIO) in stm32_dfsdm_adc_remove()
1628 of_platform_depopulate(&pdev->dev); in stm32_dfsdm_adc_remove()
1648 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); in stm32_dfsdm_adc_resume() local
1654 for (i = 0; i < indio_dev->num_channels; i++) { in stm32_dfsdm_adc_resume()
1655 chan = indio_dev->channels + i; in stm32_dfsdm_adc_resume()
1656 ch = &adc->dfsdm->ch_list[chan->channel]; in stm32_dfsdm_adc_resume()
1657 ret = stm32_dfsdm_chan_configure(adc->dfsdm, ch); in stm32_dfsdm_adc_resume()
1673 .name = "stm32-dfsdm-adc",
1682 MODULE_DESCRIPTION("STM32 sigma delta ADC");