Lines Matching +full:vref +full:- +full:half
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>
14 #include <linux/iio/hw-consumer.h>
16 #include <linux/iio/timer/stm32-lptim-trigger.h>
17 #include <linux/iio/timer/stm32-timer-trigger.h>
29 #include "stm32-dfsdm.h"
44 /* Limit filter output resolution to 31 bits. (i.e. sample range is +/-2^30) */
48 * Data from filters are in the range +/-2^(n-1)
49 * 2^(n-1) maximum positive value cannot be coded in 2's complement n bits
50 * An extra bit is required to avoid wrap-around of the binary code for 2^(n-1)
132 for (p = list; p && p->name; p++) in stm32_dfsdm_str2val()
133 if (!strcmp(p->name, str)) in stm32_dfsdm_str2val()
134 return p->val; in stm32_dfsdm_str2val()
136 return -EINVAL; in stm32_dfsdm_str2val()
140 * struct stm32_dfsdm_trig_info - DFSDM trigger info
186 !strcmp(stm32_dfsdm_trigs[i].name, trig->name)) { in stm32_dfsdm_get_jextsel()
191 return -EINVAL; in stm32_dfsdm_get_jextsel()
201 unsigned int p = fl->ford; /* filter order (ford) */ in stm32_dfsdm_compute_osrs()
202 struct stm32_dfsdm_filter_osr *flo = &fl->flo[fast]; in stm32_dfsdm_compute_osrs()
214 if (fl->ford == DFSDM_FASTSINC_ORDER) { in stm32_dfsdm_compute_osrs()
227 else if (fl->ford == DFSDM_FASTSINC_ORDER) in stm32_dfsdm_compute_osrs()
230 d = fosr * (iosr - 1 + p) + p; in stm32_dfsdm_compute_osrs()
245 for (i = p - 1; i > 0; i--) { in stm32_dfsdm_compute_osrs()
257 if (res >= flo->res) { in stm32_dfsdm_compute_osrs()
258 flo->res = res; in stm32_dfsdm_compute_osrs()
259 flo->fosr = fosr; in stm32_dfsdm_compute_osrs()
260 flo->iosr = iosr; in stm32_dfsdm_compute_osrs()
262 bits = fls(flo->res); in stm32_dfsdm_compute_osrs()
264 max = flo->res << 8; in stm32_dfsdm_compute_osrs()
267 if (flo->res > BIT(bits - 1)) in stm32_dfsdm_compute_osrs()
270 max--; in stm32_dfsdm_compute_osrs()
272 shift = DFSDM_DATA_RES - bits; in stm32_dfsdm_compute_osrs()
281 flo->rshift = 0; in stm32_dfsdm_compute_osrs()
282 flo->lshift = shift; in stm32_dfsdm_compute_osrs()
294 flo->rshift = 1 - shift; in stm32_dfsdm_compute_osrs()
295 flo->lshift = 1; in stm32_dfsdm_compute_osrs()
296 max >>= flo->rshift; in stm32_dfsdm_compute_osrs()
298 flo->max = (s32)max; in stm32_dfsdm_compute_osrs()
299 flo->bits = bits; in stm32_dfsdm_compute_osrs()
302 fast, flo->fosr, flo->iosr, in stm32_dfsdm_compute_osrs()
303 flo->res, bits, flo->rshift, in stm32_dfsdm_compute_osrs()
304 flo->lshift); in stm32_dfsdm_compute_osrs()
309 if (!flo->res) in stm32_dfsdm_compute_osrs()
310 return -EINVAL; in stm32_dfsdm_compute_osrs()
319 struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[adc->fl_id]; in stm32_dfsdm_compute_all_osrs()
322 memset(&fl->flo[0], 0, sizeof(fl->flo[0])); in stm32_dfsdm_compute_all_osrs()
323 memset(&fl->flo[1], 0, sizeof(fl->flo[1])); in stm32_dfsdm_compute_all_osrs()
328 dev_err(&indio_dev->dev, in stm32_dfsdm_compute_all_osrs()
331 return -EINVAL; in stm32_dfsdm_compute_all_osrs()
340 struct regmap *regmap = adc->dfsdm->regmap; in stm32_dfsdm_start_channel()
345 for_each_set_bit(bit, &adc->smask, sizeof(adc->smask) * BITS_PER_BYTE) { in stm32_dfsdm_start_channel()
346 chan = indio_dev->channels + bit; in stm32_dfsdm_start_channel()
347 ret = regmap_update_bits(regmap, DFSDM_CHCFGR1(chan->channel), in stm32_dfsdm_start_channel()
360 struct regmap *regmap = adc->dfsdm->regmap; in stm32_dfsdm_stop_channel()
364 for_each_set_bit(bit, &adc->smask, sizeof(adc->smask) * BITS_PER_BYTE) { in stm32_dfsdm_stop_channel()
365 chan = indio_dev->channels + bit; in stm32_dfsdm_stop_channel()
366 regmap_update_bits(regmap, DFSDM_CHCFGR1(chan->channel), in stm32_dfsdm_stop_channel()
375 unsigned int id = ch->id; in stm32_dfsdm_chan_configure()
376 struct regmap *regmap = dfsdm->regmap; in stm32_dfsdm_chan_configure()
381 DFSDM_CHCFGR1_SITP(ch->type)); in stm32_dfsdm_chan_configure()
386 DFSDM_CHCFGR1_SPICKSEL(ch->src)); in stm32_dfsdm_chan_configure()
391 DFSDM_CHCFGR1_CHINSEL(ch->alt_si)); in stm32_dfsdm_chan_configure()
398 struct stm32_dfsdm *dfsdm = adc->dfsdm; in stm32_dfsdm_start_filter()
402 ret = regmap_update_bits(dfsdm->regmap, DFSDM_CR1(fl_id), in stm32_dfsdm_start_filter()
408 if (adc->nconv > 1 || trig) in stm32_dfsdm_start_filter()
412 return regmap_update_bits(dfsdm->regmap, DFSDM_CR1(fl_id), in stm32_dfsdm_start_filter()
421 regmap_update_bits(dfsdm->regmap, DFSDM_CR1(fl_id), in stm32_dfsdm_stop_filter()
430 struct regmap *regmap = adc->dfsdm->regmap; in stm32_dfsdm_filter_set_trig()
459 struct regmap *regmap = adc->dfsdm->regmap; in stm32_dfsdm_channels_configure()
460 struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[fl_id]; in stm32_dfsdm_channels_configure()
461 struct stm32_dfsdm_filter_osr *flo = &fl->flo[0]; in stm32_dfsdm_channels_configure()
466 fl->fast = 0; in stm32_dfsdm_channels_configure()
472 if (adc->nconv == 1 && !trig && iio_buffer_enabled(indio_dev)) { in stm32_dfsdm_channels_configure()
473 if (fl->flo[1].res >= fl->flo[0].res) { in stm32_dfsdm_channels_configure()
474 fl->fast = 1; in stm32_dfsdm_channels_configure()
475 flo = &fl->flo[1]; in stm32_dfsdm_channels_configure()
479 if (!flo->res) in stm32_dfsdm_channels_configure()
480 return -EINVAL; in stm32_dfsdm_channels_configure()
482 dev_dbg(&indio_dev->dev, "Samples actual resolution: %d bits", in stm32_dfsdm_channels_configure()
483 min(flo->bits, (u32)DFSDM_DATA_RES - 1)); in stm32_dfsdm_channels_configure()
485 for_each_set_bit(bit, &adc->smask, in stm32_dfsdm_channels_configure()
486 sizeof(adc->smask) * BITS_PER_BYTE) { in stm32_dfsdm_channels_configure()
487 chan = indio_dev->channels + bit; in stm32_dfsdm_channels_configure()
490 DFSDM_CHCFGR2(chan->channel), in stm32_dfsdm_channels_configure()
492 DFSDM_CHCFGR2_DTRBS(flo->rshift)); in stm32_dfsdm_channels_configure()
505 struct regmap *regmap = adc->dfsdm->regmap; in stm32_dfsdm_filter_configure()
506 struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[fl_id]; in stm32_dfsdm_filter_configure()
507 struct stm32_dfsdm_filter_osr *flo = &fl->flo[fl->fast]; in stm32_dfsdm_filter_configure()
515 DFSDM_FCR_IOSR(flo->iosr - 1)); in stm32_dfsdm_filter_configure()
521 DFSDM_FCR_FOSR(flo->fosr - 1)); in stm32_dfsdm_filter_configure()
526 DFSDM_FCR_FORD(fl->ford)); in stm32_dfsdm_filter_configure()
536 DFSDM_CR1_FAST(fl->fast)); 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()
557 * ---------------------------------------------------------------- in stm32_dfsdm_filter_configure()
559 if (adc->nconv == 1 && !trig) { in stm32_dfsdm_filter_configure()
560 bit = __ffs(adc->smask); in stm32_dfsdm_filter_configure()
561 chan = indio_dev->channels + bit; in stm32_dfsdm_filter_configure()
564 cr1 = DFSDM_CR1_RCH(chan->channel); in stm32_dfsdm_filter_configure()
570 cr1 |= DFSDM_CR1_RSYNC(fl->sync_mode); in stm32_dfsdm_filter_configure()
573 for_each_set_bit(bit, &adc->smask, in stm32_dfsdm_filter_configure()
574 sizeof(adc->smask) * BITS_PER_BYTE) { in stm32_dfsdm_filter_configure()
575 chan = indio_dev->channels + bit; in stm32_dfsdm_filter_configure()
576 jchg |= BIT(chan->channel); in stm32_dfsdm_filter_configure()
583 cr1 = DFSDM_CR1_JSCAN((adc->nconv > 1) ? 1 : 0); in stm32_dfsdm_filter_configure()
588 * - conversions in sync with filter 0 in stm32_dfsdm_filter_configure()
589 * - triggered conversions in stm32_dfsdm_filter_configure()
591 if (!fl->sync_mode && !trig) in stm32_dfsdm_filter_configure()
592 return -EINVAL; in stm32_dfsdm_filter_configure()
593 cr1 |= DFSDM_CR1_JSYNC(fl->sync_mode); in stm32_dfsdm_filter_configure()
606 int chan_idx = ch->scan_index; in stm32_dfsdm_channel_parse_of()
609 ret = of_property_read_u32_index(indio_dev->dev.of_node, in stm32_dfsdm_channel_parse_of()
610 "st,adc-channels", chan_idx, in stm32_dfsdm_channel_parse_of()
611 &ch->channel); in stm32_dfsdm_channel_parse_of()
613 dev_err(&indio_dev->dev, in stm32_dfsdm_channel_parse_of()
614 " Error parsing 'st,adc-channels' for idx %d\n", in stm32_dfsdm_channel_parse_of()
618 if (ch->channel >= dfsdm->num_chs) { in stm32_dfsdm_channel_parse_of()
619 dev_err(&indio_dev->dev, in stm32_dfsdm_channel_parse_of()
621 ch->channel, dfsdm->num_chs); in stm32_dfsdm_channel_parse_of()
622 return -EINVAL; in stm32_dfsdm_channel_parse_of()
625 ret = of_property_read_string_index(indio_dev->dev.of_node, in stm32_dfsdm_channel_parse_of()
626 "st,adc-channel-names", chan_idx, in stm32_dfsdm_channel_parse_of()
627 &ch->datasheet_name); in stm32_dfsdm_channel_parse_of()
629 dev_err(&indio_dev->dev, in stm32_dfsdm_channel_parse_of()
630 " Error parsing 'st,adc-channel-names' for idx %d\n", in stm32_dfsdm_channel_parse_of()
635 df_ch = &dfsdm->ch_list[ch->channel]; in stm32_dfsdm_channel_parse_of()
636 df_ch->id = ch->channel; in stm32_dfsdm_channel_parse_of()
638 ret = of_property_read_string_index(indio_dev->dev.of_node, in stm32_dfsdm_channel_parse_of()
639 "st,adc-channel-types", chan_idx, in stm32_dfsdm_channel_parse_of()
648 df_ch->type = val; in stm32_dfsdm_channel_parse_of()
650 ret = of_property_read_string_index(indio_dev->dev.of_node, in stm32_dfsdm_channel_parse_of()
651 "st,adc-channel-clk-src", chan_idx, in stm32_dfsdm_channel_parse_of()
660 df_ch->src = val; in stm32_dfsdm_channel_parse_of()
662 ret = of_property_read_u32_index(indio_dev->dev.of_node, in stm32_dfsdm_channel_parse_of()
663 "st,adc-alt-channel", chan_idx, in stm32_dfsdm_channel_parse_of()
664 &df_ch->alt_si); in stm32_dfsdm_channel_parse_of()
666 df_ch->alt_si = 0; in stm32_dfsdm_channel_parse_of()
682 ret = fwnode_property_read_u32(node, "reg", &ch->channel); in stm32_dfsdm_generic_channel_parse_of()
684 dev_err(&indio_dev->dev, "Missing channel index %d\n", ret); in stm32_dfsdm_generic_channel_parse_of()
688 if (ch->channel >= dfsdm->num_chs) { in stm32_dfsdm_generic_channel_parse_of()
689 dev_err(&indio_dev->dev, " Error bad channel number %d (max = %d)\n", in stm32_dfsdm_generic_channel_parse_of()
690 ch->channel, dfsdm->num_chs); in stm32_dfsdm_generic_channel_parse_of()
691 return -EINVAL; in stm32_dfsdm_generic_channel_parse_of()
694 ret = fwnode_property_read_string(node, "label", &ch->datasheet_name); in stm32_dfsdm_generic_channel_parse_of()
696 dev_err(&indio_dev->dev, in stm32_dfsdm_generic_channel_parse_of()
697 " Error parsing 'label' for idx %d\n", ch->channel); in stm32_dfsdm_generic_channel_parse_of()
701 df_ch = &dfsdm->ch_list[ch->channel]; in stm32_dfsdm_generic_channel_parse_of()
702 df_ch->id = ch->channel; in stm32_dfsdm_generic_channel_parse_of()
704 ret = fwnode_property_read_string(node, "st,adc-channel-type", &of_str); in stm32_dfsdm_generic_channel_parse_of()
712 df_ch->type = val; in stm32_dfsdm_generic_channel_parse_of()
714 ret = fwnode_property_read_string(node, "st,adc-channel-clk-src", &of_str); in stm32_dfsdm_generic_channel_parse_of()
722 df_ch->src = val; in stm32_dfsdm_generic_channel_parse_of()
724 ret = fwnode_property_read_u32(node, "st,adc-alt-channel", &df_ch->alt_si); in stm32_dfsdm_generic_channel_parse_of()
725 if (ret != -EINVAL) in stm32_dfsdm_generic_channel_parse_of()
726 df_ch->alt_si = 0; in stm32_dfsdm_generic_channel_parse_of()
728 if (adc->dev_data->type == DFSDM_IIO) { in stm32_dfsdm_generic_channel_parse_of()
729 backend = devm_iio_backend_fwnode_get(&indio_dev->dev, NULL, node); in stm32_dfsdm_generic_channel_parse_of()
731 return dev_err_probe(&indio_dev->dev, PTR_ERR(backend), in stm32_dfsdm_generic_channel_parse_of()
733 adc->backend[ch->scan_index] = backend; in stm32_dfsdm_generic_channel_parse_of()
746 return snprintf(buf, PAGE_SIZE, "%d\n", adc->spi_freq); in dfsdm_adc_audio_get_spiclk()
759 dev_dbg(&indio_dev->dev, in dfsdm_adc_set_samp_freq()
767 adc->sample_freq = spi_freq / oversamp; in dfsdm_adc_set_samp_freq()
768 adc->oversamp = oversamp; in dfsdm_adc_set_samp_freq()
779 struct stm32_dfsdm_channel *ch = &adc->dfsdm->ch_list[chan->channel]; in dfsdm_adc_audio_set_spiclk()
780 unsigned int sample_freq = adc->sample_freq; in dfsdm_adc_audio_set_spiclk()
784 dev_err(&indio_dev->dev, "enter %s\n", __func__); in dfsdm_adc_audio_set_spiclk()
786 if (ch->src != DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL) in dfsdm_adc_audio_set_spiclk()
787 return -EPERM; in dfsdm_adc_audio_set_spiclk()
794 return -EINVAL; in dfsdm_adc_audio_set_spiclk()
801 adc->spi_freq = spi_freq; in dfsdm_adc_audio_set_spiclk()
810 struct regmap *regmap = adc->dfsdm->regmap; in stm32_dfsdm_start_conv()
813 ret = stm32_dfsdm_channels_configure(indio_dev, adc->fl_id, trig); in stm32_dfsdm_start_conv()
821 ret = stm32_dfsdm_filter_configure(indio_dev, adc->fl_id, trig); in stm32_dfsdm_start_conv()
825 ret = stm32_dfsdm_start_filter(adc, adc->fl_id, trig); in stm32_dfsdm_start_conv()
832 regmap_clear_bits(regmap, DFSDM_CR1(adc->fl_id), DFSDM_CR1_CFG_MASK); in stm32_dfsdm_start_conv()
842 struct regmap *regmap = adc->dfsdm->regmap; in stm32_dfsdm_stop_conv()
844 stm32_dfsdm_stop_filter(adc->dfsdm, adc->fl_id); in stm32_dfsdm_stop_conv()
846 regmap_clear_bits(regmap, DFSDM_CR1(adc->fl_id), DFSDM_CR1_CFG_MASK); in stm32_dfsdm_stop_conv()
861 * - always one buffer (period) DMA is working on in stm32_dfsdm_set_watermark()
862 * - one buffer (period) driver pushed to ASoC side. in stm32_dfsdm_set_watermark()
865 adc->buf_sz = min(rx_buf_sz, watermark * 2 * adc->nconv); in stm32_dfsdm_set_watermark()
875 status = dmaengine_tx_status(adc->dma_chan, in stm32_dfsdm_adc_dma_residue()
876 adc->dma_chan->cookie, in stm32_dfsdm_adc_dma_residue()
880 unsigned int i = adc->buf_sz - state.residue; in stm32_dfsdm_adc_dma_residue()
884 if (i >= adc->bufi) in stm32_dfsdm_adc_dma_residue()
885 size = i - adc->bufi; in stm32_dfsdm_adc_dma_residue()
887 size = adc->buf_sz + i - adc->bufi; in stm32_dfsdm_adc_dma_residue()
898 struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[adc->fl_id]; in stm32_dfsdm_process_data()
899 struct stm32_dfsdm_filter_osr *flo = &fl->flo[fl->fast]; in stm32_dfsdm_process_data()
900 unsigned int i = adc->nconv; in stm32_dfsdm_process_data()
903 while (i--) { in stm32_dfsdm_process_data()
906 /* Convert 2^(n-1) sample to 2^(n-1)-1 to avoid wrap-around */ in stm32_dfsdm_process_data()
907 if (*ptr > flo->max) in stm32_dfsdm_process_data()
908 *ptr -= 1; in stm32_dfsdm_process_data()
913 *ptr <<= flo->lshift; in stm32_dfsdm_process_data()
935 dev_dbg(&indio_dev->dev, "pos = %d, available = %d\n", in stm32_dfsdm_dma_buffer_done()
936 adc->bufi, available); in stm32_dfsdm_dma_buffer_done()
937 old_pos = adc->bufi; in stm32_dfsdm_dma_buffer_done()
939 while (available >= indio_dev->scan_bytes) { in stm32_dfsdm_dma_buffer_done()
940 s32 *buffer = (s32 *)&adc->rx_buf[adc->bufi]; in stm32_dfsdm_dma_buffer_done()
944 available -= indio_dev->scan_bytes; in stm32_dfsdm_dma_buffer_done()
945 adc->bufi += indio_dev->scan_bytes; in stm32_dfsdm_dma_buffer_done()
946 if (adc->bufi >= adc->buf_sz) { in stm32_dfsdm_dma_buffer_done()
947 if (adc->cb) in stm32_dfsdm_dma_buffer_done()
948 adc->cb(&adc->rx_buf[old_pos], in stm32_dfsdm_dma_buffer_done()
949 adc->buf_sz - old_pos, adc->cb_priv); in stm32_dfsdm_dma_buffer_done()
950 adc->bufi = 0; in stm32_dfsdm_dma_buffer_done()
962 if (adc->dev_data->type == DFSDM_IIO) in stm32_dfsdm_dma_buffer_done()
965 if (adc->cb) in stm32_dfsdm_dma_buffer_done()
966 adc->cb(&adc->rx_buf[old_pos], adc->bufi - old_pos, in stm32_dfsdm_dma_buffer_done()
967 adc->cb_priv); in stm32_dfsdm_dma_buffer_done()
974 * The DFSDM supports half-word transfers. However, for 16 bits record, in stm32_dfsdm_adc_dma_start()
979 .src_addr = (dma_addr_t)adc->dfsdm->phys_base, in stm32_dfsdm_adc_dma_start()
986 if (!adc->dma_chan) in stm32_dfsdm_adc_dma_start()
987 return -EINVAL; in stm32_dfsdm_adc_dma_start()
989 dev_dbg(&indio_dev->dev, "size=%d watermark=%d\n", in stm32_dfsdm_adc_dma_start()
990 adc->buf_sz, adc->buf_sz / 2); in stm32_dfsdm_adc_dma_start()
992 if (adc->nconv == 1 && !indio_dev->trig) in stm32_dfsdm_adc_dma_start()
993 config.src_addr += DFSDM_RDATAR(adc->fl_id); in stm32_dfsdm_adc_dma_start()
995 config.src_addr += DFSDM_JDATAR(adc->fl_id); in stm32_dfsdm_adc_dma_start()
996 ret = dmaengine_slave_config(adc->dma_chan, &config); in stm32_dfsdm_adc_dma_start()
1001 desc = dmaengine_prep_dma_cyclic(adc->dma_chan, in stm32_dfsdm_adc_dma_start()
1002 adc->dma_buf, in stm32_dfsdm_adc_dma_start()
1003 adc->buf_sz, adc->buf_sz / 2, in stm32_dfsdm_adc_dma_start()
1007 return -EBUSY; in stm32_dfsdm_adc_dma_start()
1009 desc->callback = stm32_dfsdm_dma_buffer_done; in stm32_dfsdm_adc_dma_start()
1010 desc->callback_param = indio_dev; in stm32_dfsdm_adc_dma_start()
1018 dma_async_issue_pending(adc->dma_chan); in stm32_dfsdm_adc_dma_start()
1020 if (adc->nconv == 1 && !indio_dev->trig) { in stm32_dfsdm_adc_dma_start()
1022 ret = regmap_set_bits(adc->dfsdm->regmap, in stm32_dfsdm_adc_dma_start()
1023 DFSDM_CR1(adc->fl_id), in stm32_dfsdm_adc_dma_start()
1027 ret = regmap_set_bits(adc->dfsdm->regmap, in stm32_dfsdm_adc_dma_start()
1028 DFSDM_CR1(adc->fl_id), in stm32_dfsdm_adc_dma_start()
1038 dmaengine_terminate_all(adc->dma_chan); in stm32_dfsdm_adc_dma_start()
1047 if (!adc->dma_chan) in stm32_dfsdm_adc_dma_stop()
1050 regmap_clear_bits(adc->dfsdm->regmap, DFSDM_CR1(adc->fl_id), in stm32_dfsdm_adc_dma_stop()
1052 dmaengine_terminate_all(adc->dma_chan); in stm32_dfsdm_adc_dma_stop()
1060 adc->nconv = bitmap_weight(scan_mask, iio_get_masklength(indio_dev)); in stm32_dfsdm_update_scan_mode()
1061 adc->smask = *scan_mask; in stm32_dfsdm_update_scan_mode()
1063 dev_dbg(&indio_dev->dev, "nconv=%d mask=%lx\n", adc->nconv, *scan_mask); in stm32_dfsdm_update_scan_mode()
1075 adc->bufi = 0; in stm32_dfsdm_postenable()
1077 if (adc->hwc) { in stm32_dfsdm_postenable()
1078 ret = iio_hw_consumer_enable(adc->hwc); in stm32_dfsdm_postenable()
1083 if (adc->backend) { in stm32_dfsdm_postenable()
1084 while (adc->backend[i]) { in stm32_dfsdm_postenable()
1085 ret = iio_backend_enable(adc->backend[i]); in stm32_dfsdm_postenable()
1092 ret = stm32_dfsdm_start_dfsdm(adc->dfsdm); in stm32_dfsdm_postenable()
1098 dev_err(&indio_dev->dev, "Can't start DMA\n"); in stm32_dfsdm_postenable()
1102 ret = stm32_dfsdm_start_conv(indio_dev, indio_dev->trig); in stm32_dfsdm_postenable()
1104 dev_err(&indio_dev->dev, "Can't start conversion\n"); in stm32_dfsdm_postenable()
1113 stm32_dfsdm_stop_dfsdm(adc->dfsdm); in stm32_dfsdm_postenable()
1115 if (adc->hwc) in stm32_dfsdm_postenable()
1116 iio_hw_consumer_disable(adc->hwc); in stm32_dfsdm_postenable()
1130 stm32_dfsdm_stop_dfsdm(adc->dfsdm); in stm32_dfsdm_predisable()
1132 if (adc->backend) { in stm32_dfsdm_predisable()
1133 while (adc->backend[i]) { in stm32_dfsdm_predisable()
1134 iio_backend_disable(adc->backend[i]); in stm32_dfsdm_predisable()
1139 if (adc->hwc) in stm32_dfsdm_predisable()
1140 iio_hw_consumer_disable(adc->hwc); in stm32_dfsdm_predisable()
1151 * stm32_dfsdm_get_buff_cb() - register a callback that will be called when
1156 * - data: pointer to data buffer
1157 * - size: size in byte of the data buffer
1158 * - private: pointer to consumer private structure.
1169 return -EINVAL; in stm32_dfsdm_get_buff_cb()
1172 adc->cb = cb; in stm32_dfsdm_get_buff_cb()
1173 adc->cb_priv = private; in stm32_dfsdm_get_buff_cb()
1180 * stm32_dfsdm_release_buff_cb - unregister buffer callback
1189 return -EINVAL; in stm32_dfsdm_release_buff_cb()
1192 adc->cb = NULL; in stm32_dfsdm_release_buff_cb()
1193 adc->cb_priv = NULL; in stm32_dfsdm_release_buff_cb()
1206 reinit_completion(&adc->completion); in stm32_dfsdm_single_conv()
1208 adc->buffer = res; in stm32_dfsdm_single_conv()
1210 ret = stm32_dfsdm_start_dfsdm(adc->dfsdm); in stm32_dfsdm_single_conv()
1214 ret = regmap_update_bits(adc->dfsdm->regmap, DFSDM_CR2(adc->fl_id), in stm32_dfsdm_single_conv()
1219 adc->nconv = 1; in stm32_dfsdm_single_conv()
1220 adc->smask = BIT(chan->scan_index); in stm32_dfsdm_single_conv()
1223 regmap_update_bits(adc->dfsdm->regmap, DFSDM_CR2(adc->fl_id), in stm32_dfsdm_single_conv()
1228 time_left = wait_for_completion_interruptible_timeout(&adc->completion, in stm32_dfsdm_single_conv()
1232 regmap_update_bits(adc->dfsdm->regmap, DFSDM_CR2(adc->fl_id), in stm32_dfsdm_single_conv()
1236 ret = -ETIMEDOUT; in stm32_dfsdm_single_conv()
1247 stm32_dfsdm_stop_dfsdm(adc->dfsdm); in stm32_dfsdm_single_conv()
1257 struct stm32_dfsdm_channel *ch = &adc->dfsdm->ch_list[chan->channel]; in stm32_dfsdm_write_raw()
1259 int ret = -EINVAL; in stm32_dfsdm_write_raw()
1261 switch (ch->src) { in stm32_dfsdm_write_raw()
1263 spi_freq = adc->dfsdm->spi_master_freq; in stm32_dfsdm_write_raw()
1267 spi_freq = adc->dfsdm->spi_master_freq / 2; in stm32_dfsdm_write_raw()
1270 spi_freq = adc->spi_freq; in stm32_dfsdm_write_raw()
1281 dev_dbg(&indio_dev->dev, in stm32_dfsdm_write_raw()
1283 adc->sample_freq, spi_freq / val); in stm32_dfsdm_write_raw()
1284 adc->oversamp = val; in stm32_dfsdm_write_raw()
1285 adc->sample_freq = spi_freq / val; in stm32_dfsdm_write_raw()
1292 return -EINVAL; in stm32_dfsdm_write_raw()
1303 return -EINVAL; in stm32_dfsdm_write_raw()
1312 struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[adc->fl_id]; in stm32_dfsdm_read_raw()
1313 struct stm32_dfsdm_filter_osr *flo = &fl->flo[fl->fast]; in stm32_dfsdm_read_raw()
1314 u32 max = flo->max << (flo->lshift - chan->scan_type.shift); in stm32_dfsdm_read_raw()
1315 int idx = chan->scan_index; in stm32_dfsdm_read_raw()
1318 if (flo->lshift < chan->scan_type.shift) in stm32_dfsdm_read_raw()
1319 max = flo->max >> (chan->scan_type.shift - flo->lshift); in stm32_dfsdm_read_raw()
1326 if (adc->hwc) in stm32_dfsdm_read_raw()
1327 ret = iio_hw_consumer_enable(adc->hwc); in stm32_dfsdm_read_raw()
1328 if (adc->backend) in stm32_dfsdm_read_raw()
1329 ret = iio_backend_enable(adc->backend[idx]); in stm32_dfsdm_read_raw()
1331 dev_err(&indio_dev->dev, in stm32_dfsdm_read_raw()
1333 __func__, chan->channel); in stm32_dfsdm_read_raw()
1338 if (adc->hwc) in stm32_dfsdm_read_raw()
1339 iio_hw_consumer_disable(adc->hwc); in stm32_dfsdm_read_raw()
1340 if (adc->backend) in stm32_dfsdm_read_raw()
1341 iio_backend_disable(adc->backend[idx]); in stm32_dfsdm_read_raw()
1343 dev_err(&indio_dev->dev, in stm32_dfsdm_read_raw()
1345 __func__, chan->channel); in stm32_dfsdm_read_raw()
1353 *val = adc->oversamp; in stm32_dfsdm_read_raw()
1358 *val = adc->sample_freq; in stm32_dfsdm_read_raw()
1366 * than 2^n, where n = realbits - 1. in stm32_dfsdm_read_raw()
1371 if (adc->backend) { in stm32_dfsdm_read_raw()
1372 ret = iio_backend_read_scale(adc->backend[idx], chan, val, NULL); in stm32_dfsdm_read_raw()
1376 *val = div_u64((u64)*val * (u64)BIT(DFSDM_DATA_RES - 1), max); in stm32_dfsdm_read_raw()
1377 *val2 = chan->scan_type.realbits; in stm32_dfsdm_read_raw()
1378 if (chan->differential) in stm32_dfsdm_read_raw()
1385 * DFSDM output data are in the range [-2^n, 2^n], in stm32_dfsdm_read_raw()
1386 * with n = realbits - 1. in stm32_dfsdm_read_raw()
1387 * - Differential modulator: in stm32_dfsdm_read_raw()
1389 * - Single ended modulator: in stm32_dfsdm_read_raw()
1390 * Input is in [0V, Vref] range, in stm32_dfsdm_read_raw()
1391 * where 0V corresponds to -2^n, and Vref to 2^n. in stm32_dfsdm_read_raw()
1393 * offset = offset(sd) * vref / res(sd) * max / vref. in stm32_dfsdm_read_raw()
1395 if (adc->backend) { in stm32_dfsdm_read_raw()
1396 ret = iio_backend_read_offset(adc->backend[idx], chan, val, NULL); in stm32_dfsdm_read_raw()
1400 *val = div_u64((u64)max * *val, BIT(*val2 - 1)); in stm32_dfsdm_read_raw()
1401 if (!chan->differential) in stm32_dfsdm_read_raw()
1407 return -EINVAL; in stm32_dfsdm_read_raw()
1413 return stm32_dfsdm_get_jextsel(indio_dev, trig) < 0 ? -EINVAL : 0; in stm32_dfsdm_validate_trigger()
1435 struct regmap *regmap = adc->dfsdm->regmap; in stm32_dfsdm_irq()
1438 regmap_read(regmap, DFSDM_ISR(adc->fl_id), &status); in stm32_dfsdm_irq()
1439 regmap_read(regmap, DFSDM_CR2(adc->fl_id), &int_en); in stm32_dfsdm_irq()
1443 regmap_read(regmap, DFSDM_RDATAR(adc->fl_id), adc->buffer); in stm32_dfsdm_irq()
1444 complete(&adc->completion); in stm32_dfsdm_irq()
1449 dev_warn(&indio_dev->dev, "Overrun detected\n"); in stm32_dfsdm_irq()
1450 regmap_set_bits(regmap, DFSDM_ICR(adc->fl_id), in stm32_dfsdm_irq()
1476 if (adc->dma_chan) { in stm32_dfsdm_dma_release()
1477 dma_free_coherent(adc->dma_chan->device->dev, in stm32_dfsdm_dma_release()
1479 adc->rx_buf, adc->dma_buf); in stm32_dfsdm_dma_release()
1480 dma_release_channel(adc->dma_chan); in stm32_dfsdm_dma_release()
1489 adc->dma_chan = dma_request_chan(dev, "rx"); in stm32_dfsdm_dma_request()
1490 if (IS_ERR(adc->dma_chan)) { in stm32_dfsdm_dma_request()
1491 int ret = PTR_ERR(adc->dma_chan); in stm32_dfsdm_dma_request()
1493 adc->dma_chan = NULL; in stm32_dfsdm_dma_request()
1497 adc->rx_buf = dma_alloc_coherent(adc->dma_chan->device->dev, in stm32_dfsdm_dma_request()
1499 &adc->dma_buf, GFP_KERNEL); in stm32_dfsdm_dma_request()
1500 if (!adc->rx_buf) { in stm32_dfsdm_dma_request()
1501 dma_release_channel(adc->dma_chan); in stm32_dfsdm_dma_request()
1502 return -ENOMEM; in stm32_dfsdm_dma_request()
1505 indio_dev->modes |= INDIO_BUFFER_SOFTWARE; in stm32_dfsdm_dma_request()
1506 indio_dev->setup_ops = &stm32_dfsdm_buffer_setup_ops; in stm32_dfsdm_dma_request()
1518 ret = stm32_dfsdm_generic_channel_parse_of(adc->dfsdm, indio_dev, ch, child); in stm32_dfsdm_adc_chan_init_one()
1520 ret = stm32_dfsdm_channel_parse_of(adc->dfsdm, indio_dev, ch); in stm32_dfsdm_adc_chan_init_one()
1522 return dev_err_probe(&indio_dev->dev, ret, "Failed to parse channel\n"); in stm32_dfsdm_adc_chan_init_one()
1524 ch->type = IIO_VOLTAGE; in stm32_dfsdm_adc_chan_init_one()
1525 ch->indexed = 1; in stm32_dfsdm_adc_chan_init_one()
1532 ch->info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | in stm32_dfsdm_adc_chan_init_one()
1537 ch->info_mask_separate = BIT(IIO_CHAN_INFO_RAW); in stm32_dfsdm_adc_chan_init_one()
1540 ch->info_mask_shared_by_all = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) | in stm32_dfsdm_adc_chan_init_one()
1543 if (adc->dev_data->type == DFSDM_AUDIO) { in stm32_dfsdm_adc_chan_init_one()
1544 ch->ext_info = dfsdm_adc_audio_ext_info; in stm32_dfsdm_adc_chan_init_one()
1545 ch->scan_index = 0; in stm32_dfsdm_adc_chan_init_one()
1547 ch->scan_type.shift = 8; in stm32_dfsdm_adc_chan_init_one()
1549 ch->scan_type.sign = 's'; in stm32_dfsdm_adc_chan_init_one()
1550 ch->scan_type.realbits = 24; in stm32_dfsdm_adc_chan_init_one()
1551 ch->scan_type.storagebits = 32; in stm32_dfsdm_adc_chan_init_one()
1553 return stm32_dfsdm_chan_configure(adc->dfsdm, in stm32_dfsdm_adc_chan_init_one()
1554 &adc->dfsdm->ch_list[ch->channel]); in stm32_dfsdm_adc_chan_init_one()
1559 int num_ch = indio_dev->num_channels; in stm32_dfsdm_chan_init()
1567 return dev_err_probe(&indio_dev->dev, ret, "Channels init failed\n"); in stm32_dfsdm_chan_init()
1577 device_for_each_child_node_scoped(&indio_dev->dev, child) { in stm32_dfsdm_generic_chan_init()
1585 return dev_err_probe(&indio_dev->dev, ret, "Channels init failed\n"); in stm32_dfsdm_generic_chan_init()
1601 /* If st,adc-channels is defined legacy binding is used. Else assume generic binding. */ in stm32_dfsdm_audio_init()
1602 num_ch = of_property_count_u32_elems(indio_dev->dev.of_node, "st,adc-channels"); in stm32_dfsdm_audio_init()
1606 ch = devm_kzalloc(&indio_dev->dev, sizeof(*ch), GFP_KERNEL); in stm32_dfsdm_audio_init()
1608 return -ENOMEM; in stm32_dfsdm_audio_init()
1610 indio_dev->num_channels = 1; in stm32_dfsdm_audio_init()
1611 indio_dev->channels = ch; in stm32_dfsdm_audio_init()
1619 dev_err(&indio_dev->dev, "Channels init failed\n"); in stm32_dfsdm_audio_init()
1622 ch->info_mask_separate = BIT(IIO_CHAN_INFO_SAMP_FREQ); in stm32_dfsdm_audio_init()
1624 d_ch = &adc->dfsdm->ch_list[ch->channel]; in stm32_dfsdm_audio_init()
1625 if (d_ch->src != DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL) in stm32_dfsdm_audio_init()
1626 adc->spi_freq = adc->dfsdm->spi_master_freq; in stm32_dfsdm_audio_init()
1638 adc->oversamp = DFSDM_DEFAULT_OVERSAMPLING; in stm32_dfsdm_adc_init()
1639 ret = stm32_dfsdm_compute_all_osrs(indio_dev, adc->oversamp); in stm32_dfsdm_adc_init()
1643 num_ch = device_get_child_node_count(&indio_dev->dev); in stm32_dfsdm_adc_init()
1646 num_ch = of_property_count_u32_elems(indio_dev->dev.of_node, "st,adc-channels"); in stm32_dfsdm_adc_init()
1648 dev_err(&indio_dev->dev, "Bad st,adc-channels\n"); in stm32_dfsdm_adc_init()
1655 if (num_ch > adc->dfsdm->num_chs) { in stm32_dfsdm_adc_init()
1656 dev_err(&indio_dev->dev, "Number of channel [%d] exceeds [%d]\n", in stm32_dfsdm_adc_init()
1657 num_ch, adc->dfsdm->num_chs); in stm32_dfsdm_adc_init()
1658 return -EINVAL; in stm32_dfsdm_adc_init()
1660 indio_dev->num_channels = num_ch; in stm32_dfsdm_adc_init()
1664 adc->hwc = devm_iio_hw_consumer_alloc(&indio_dev->dev); in stm32_dfsdm_adc_init()
1665 if (IS_ERR(adc->hwc)) in stm32_dfsdm_adc_init()
1666 return dev_err_probe(&indio_dev->dev, -EPROBE_DEFER, in stm32_dfsdm_adc_init()
1670 adc->hwc = NULL; in stm32_dfsdm_adc_init()
1672 adc->backend = devm_kcalloc(&indio_dev->dev, num_ch, sizeof(*adc->backend), in stm32_dfsdm_adc_init()
1674 if (!adc->backend) in stm32_dfsdm_adc_init()
1675 return -ENOMEM; in stm32_dfsdm_adc_init()
1678 ch = devm_kcalloc(&indio_dev->dev, num_ch, sizeof(*ch), GFP_KERNEL); in stm32_dfsdm_adc_init()
1680 return -ENOMEM; in stm32_dfsdm_adc_init()
1681 indio_dev->channels = ch; in stm32_dfsdm_adc_init()
1690 init_completion(&adc->completion); in stm32_dfsdm_adc_init()
1695 if (ret != -ENODEV) in stm32_dfsdm_adc_init()
1708 dev_err(&indio_dev->dev, "buffer setup failed\n"); in stm32_dfsdm_adc_init()
1713 indio_dev->modes |= INDIO_HARDWARE_TRIGGERED; in stm32_dfsdm_adc_init()
1730 .compatible = "st,stm32-dfsdm-adc",
1734 .compatible = "st,stm32-dfsdm-dmic",
1743 struct device *dev = &pdev->dev; in stm32_dfsdm_adc_probe()
1745 struct device_node *np = dev->of_node; in stm32_dfsdm_adc_probe()
1755 return -ENOMEM; in stm32_dfsdm_adc_probe()
1759 adc->dfsdm = dev_get_drvdata(dev->parent); in stm32_dfsdm_adc_probe()
1761 iio->dev.of_node = np; in stm32_dfsdm_adc_probe()
1762 iio->modes = INDIO_DIRECT_MODE; in stm32_dfsdm_adc_probe()
1766 ret = of_property_read_u32(dev->of_node, "reg", &adc->fl_id); in stm32_dfsdm_adc_probe()
1767 if (ret != 0 || adc->fl_id >= adc->dfsdm->num_fls) { in stm32_dfsdm_adc_probe()
1769 return -EINVAL; in stm32_dfsdm_adc_probe()
1772 name = devm_kzalloc(dev, sizeof("dfsdm-adc0"), GFP_KERNEL); in stm32_dfsdm_adc_probe()
1774 return -ENOMEM; in stm32_dfsdm_adc_probe()
1775 if (dev_data->type == DFSDM_AUDIO) { in stm32_dfsdm_adc_probe()
1776 iio->info = &stm32_dfsdm_info_audio; in stm32_dfsdm_adc_probe()
1777 snprintf(name, sizeof("dfsdm-pdm0"), "dfsdm-pdm%d", adc->fl_id); in stm32_dfsdm_adc_probe()
1779 iio->info = &stm32_dfsdm_info_adc; in stm32_dfsdm_adc_probe()
1780 snprintf(name, sizeof("dfsdm-adc0"), "dfsdm-adc%d", adc->fl_id); in stm32_dfsdm_adc_probe()
1782 iio->name = name; in stm32_dfsdm_adc_probe()
1793 0, pdev->name, iio); in stm32_dfsdm_adc_probe()
1799 ret = of_property_read_u32(dev->of_node, "st,filter-order", &val); in stm32_dfsdm_adc_probe()
1805 adc->dfsdm->fl_list[adc->fl_id].ford = val; in stm32_dfsdm_adc_probe()
1807 ret = of_property_read_u32(dev->of_node, "st,filter0-sync", &val); in stm32_dfsdm_adc_probe()
1809 adc->dfsdm->fl_list[adc->fl_id].sync_mode = val; in stm32_dfsdm_adc_probe()
1811 adc->dev_data = dev_data; in stm32_dfsdm_adc_probe()
1812 ret = dev_data->init(dev, iio); in stm32_dfsdm_adc_probe()
1820 if (dev_data->type == DFSDM_AUDIO) { in stm32_dfsdm_adc_probe()
1843 if (adc->dev_data->type == DFSDM_AUDIO) in stm32_dfsdm_adc_remove()
1844 of_platform_depopulate(&pdev->dev); in stm32_dfsdm_adc_remove()
1868 for (i = 0; i < indio_dev->num_channels; i++) { in stm32_dfsdm_adc_resume()
1869 chan = indio_dev->channels + i; in stm32_dfsdm_adc_resume()
1870 ch = &adc->dfsdm->ch_list[chan->channel]; in stm32_dfsdm_adc_resume()
1871 ret = stm32_dfsdm_chan_configure(adc->dfsdm, ch); in stm32_dfsdm_adc_resume()
1888 .name = "stm32-dfsdm-adc",