Lines Matching +full:codec +full:- +full:clkout
1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
10 #include <linux/dma-mapping.h>
12 #include <linux/iio/adc/stm32-dfsdm-adc.h>
15 #include <linux/iio/hw-consumer.h>
17 #include <linux/iio/timer/stm32-lptim-trigger.h>
18 #include <linux/iio/timer/stm32-timer-trigger.h>
30 #include "stm32-dfsdm.h"
45 /* Limit filter output resolution to 31 bits. (i.e. sample range is +/-2^30) */
49 * Data from filters are in the range +/-2^(n-1)
50 * 2^(n-1) maximum positive value cannot be coded in 2's complement n bits
51 * An extra bit is required to avoid wrap-around of the binary code for 2^(n-1)
110 { "MANCH_R", 2 }, /* Manchester codec, rising edge = logic 0 */
111 { "MANCH_F", 3 }, /* Manchester codec, falling edge = logic 1 */
119 /* Internal SPI clock (CLKOUT) */
120 { "CLKOUT", DFSDM_CHANNEL_SPI_CLOCK_INTERNAL },
133 for (p = list; p && p->name; p++) in stm32_dfsdm_str2val()
134 if (!strcmp(p->name, str)) in stm32_dfsdm_str2val()
135 return p->val; in stm32_dfsdm_str2val()
137 return -EINVAL; in stm32_dfsdm_str2val()
141 * struct stm32_dfsdm_trig_info - DFSDM trigger info
187 !strcmp(stm32_dfsdm_trigs[i].name, trig->name)) { in stm32_dfsdm_get_jextsel()
192 return -EINVAL; in stm32_dfsdm_get_jextsel()
202 unsigned int p = fl->ford; /* filter order (ford) */ in stm32_dfsdm_compute_osrs()
203 struct stm32_dfsdm_filter_osr *flo = &fl->flo[fast]; in stm32_dfsdm_compute_osrs()
215 if (fl->ford == DFSDM_FASTSINC_ORDER) { in stm32_dfsdm_compute_osrs()
228 else if (fl->ford == DFSDM_FASTSINC_ORDER) in stm32_dfsdm_compute_osrs()
231 d = fosr * (iosr - 1 + p) + p; in stm32_dfsdm_compute_osrs()
246 for (i = p - 1; i > 0; i--) { in stm32_dfsdm_compute_osrs()
258 if (res >= flo->res) { in stm32_dfsdm_compute_osrs()
259 flo->res = res; in stm32_dfsdm_compute_osrs()
260 flo->fosr = fosr; in stm32_dfsdm_compute_osrs()
261 flo->iosr = iosr; in stm32_dfsdm_compute_osrs()
263 bits = fls(flo->res); in stm32_dfsdm_compute_osrs()
265 max = flo->res << 8; in stm32_dfsdm_compute_osrs()
268 if (flo->res > BIT(bits - 1)) in stm32_dfsdm_compute_osrs()
271 max--; in stm32_dfsdm_compute_osrs()
273 shift = DFSDM_DATA_RES - bits; in stm32_dfsdm_compute_osrs()
282 flo->rshift = 0; in stm32_dfsdm_compute_osrs()
283 flo->lshift = shift; in stm32_dfsdm_compute_osrs()
295 flo->rshift = 1 - shift; in stm32_dfsdm_compute_osrs()
296 flo->lshift = 1; in stm32_dfsdm_compute_osrs()
297 max >>= flo->rshift; in stm32_dfsdm_compute_osrs()
299 flo->max = (s32)max; in stm32_dfsdm_compute_osrs()
300 flo->bits = bits; in stm32_dfsdm_compute_osrs()
303 fast, flo->fosr, flo->iosr, in stm32_dfsdm_compute_osrs()
304 flo->res, bits, flo->rshift, in stm32_dfsdm_compute_osrs()
305 flo->lshift); in stm32_dfsdm_compute_osrs()
310 if (!flo->res) in stm32_dfsdm_compute_osrs()
311 return -EINVAL; in stm32_dfsdm_compute_osrs()
320 struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[adc->fl_id]; in stm32_dfsdm_compute_all_osrs()
323 memset(&fl->flo[0], 0, sizeof(fl->flo[0])); in stm32_dfsdm_compute_all_osrs()
324 memset(&fl->flo[1], 0, sizeof(fl->flo[1])); in stm32_dfsdm_compute_all_osrs()
329 dev_err(&indio_dev->dev, in stm32_dfsdm_compute_all_osrs()
332 return -EINVAL; in stm32_dfsdm_compute_all_osrs()
341 struct regmap *regmap = adc->dfsdm->regmap; in stm32_dfsdm_start_channel()
346 for_each_set_bit(bit, &adc->smask, sizeof(adc->smask) * BITS_PER_BYTE) { in stm32_dfsdm_start_channel()
347 chan = indio_dev->channels + bit; in stm32_dfsdm_start_channel()
348 ret = regmap_update_bits(regmap, DFSDM_CHCFGR1(chan->channel), in stm32_dfsdm_start_channel()
361 struct regmap *regmap = adc->dfsdm->regmap; in stm32_dfsdm_stop_channel()
365 for_each_set_bit(bit, &adc->smask, sizeof(adc->smask) * BITS_PER_BYTE) { in stm32_dfsdm_stop_channel()
366 chan = indio_dev->channels + bit; in stm32_dfsdm_stop_channel()
367 regmap_update_bits(regmap, DFSDM_CHCFGR1(chan->channel), in stm32_dfsdm_stop_channel()
376 unsigned int id = ch->id; in stm32_dfsdm_chan_configure()
377 struct regmap *regmap = dfsdm->regmap; in stm32_dfsdm_chan_configure()
382 DFSDM_CHCFGR1_SITP(ch->type)); in stm32_dfsdm_chan_configure()
387 DFSDM_CHCFGR1_SPICKSEL(ch->src)); in stm32_dfsdm_chan_configure()
392 DFSDM_CHCFGR1_CHINSEL(ch->alt_si)); in stm32_dfsdm_chan_configure()
399 struct stm32_dfsdm *dfsdm = adc->dfsdm; in stm32_dfsdm_start_filter()
403 ret = regmap_update_bits(dfsdm->regmap, DFSDM_CR1(fl_id), in stm32_dfsdm_start_filter()
409 if (adc->nconv > 1 || trig) in stm32_dfsdm_start_filter()
413 return regmap_update_bits(dfsdm->regmap, DFSDM_CR1(fl_id), in stm32_dfsdm_start_filter()
422 regmap_update_bits(dfsdm->regmap, DFSDM_CR1(fl_id), in stm32_dfsdm_stop_filter()
431 struct regmap *regmap = adc->dfsdm->regmap; in stm32_dfsdm_filter_set_trig()
460 struct regmap *regmap = adc->dfsdm->regmap; in stm32_dfsdm_channels_configure()
461 struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[fl_id]; in stm32_dfsdm_channels_configure()
462 struct stm32_dfsdm_filter_osr *flo = &fl->flo[0]; in stm32_dfsdm_channels_configure()
467 fl->fast = 0; in stm32_dfsdm_channels_configure()
473 if (adc->nconv == 1 && !trig && iio_buffer_enabled(indio_dev)) { in stm32_dfsdm_channels_configure()
474 if (fl->flo[1].res >= fl->flo[0].res) { in stm32_dfsdm_channels_configure()
475 fl->fast = 1; in stm32_dfsdm_channels_configure()
476 flo = &fl->flo[1]; in stm32_dfsdm_channels_configure()
480 if (!flo->res) in stm32_dfsdm_channels_configure()
481 return -EINVAL; in stm32_dfsdm_channels_configure()
483 dev_dbg(&indio_dev->dev, "Samples actual resolution: %d bits", in stm32_dfsdm_channels_configure()
484 min(flo->bits, (u32)DFSDM_DATA_RES - 1)); in stm32_dfsdm_channels_configure()
486 for_each_set_bit(bit, &adc->smask, in stm32_dfsdm_channels_configure()
487 sizeof(adc->smask) * BITS_PER_BYTE) { in stm32_dfsdm_channels_configure()
488 chan = indio_dev->channels + bit; in stm32_dfsdm_channels_configure()
491 DFSDM_CHCFGR2(chan->channel), in stm32_dfsdm_channels_configure()
493 DFSDM_CHCFGR2_DTRBS(flo->rshift)); in stm32_dfsdm_channels_configure()
506 struct regmap *regmap = adc->dfsdm->regmap; in stm32_dfsdm_filter_configure()
507 struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[fl_id]; in stm32_dfsdm_filter_configure()
508 struct stm32_dfsdm_filter_osr *flo = &fl->flo[fl->fast]; in stm32_dfsdm_filter_configure()
516 DFSDM_FCR_IOSR(flo->iosr - 1)); in stm32_dfsdm_filter_configure()
522 DFSDM_FCR_FOSR(flo->fosr - 1)); in stm32_dfsdm_filter_configure()
527 DFSDM_FCR_FORD(fl->ford)); in stm32_dfsdm_filter_configure()
537 DFSDM_CR1_FAST(fl->fast)); 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()
558 * ---------------------------------------------------------------- in stm32_dfsdm_filter_configure()
560 if (adc->nconv == 1 && !trig) { in stm32_dfsdm_filter_configure()
561 bit = __ffs(adc->smask); in stm32_dfsdm_filter_configure()
562 chan = indio_dev->channels + bit; in stm32_dfsdm_filter_configure()
565 cr1 = DFSDM_CR1_RCH(chan->channel); in stm32_dfsdm_filter_configure()
571 cr1 |= DFSDM_CR1_RSYNC(fl->sync_mode); in stm32_dfsdm_filter_configure()
574 for_each_set_bit(bit, &adc->smask, in stm32_dfsdm_filter_configure()
575 sizeof(adc->smask) * BITS_PER_BYTE) { in stm32_dfsdm_filter_configure()
576 chan = indio_dev->channels + bit; in stm32_dfsdm_filter_configure()
577 jchg |= BIT(chan->channel); in stm32_dfsdm_filter_configure()
584 cr1 = DFSDM_CR1_JSCAN((adc->nconv > 1) ? 1 : 0); in stm32_dfsdm_filter_configure()
589 * - conversions in sync with filter 0 in stm32_dfsdm_filter_configure()
590 * - triggered conversions in stm32_dfsdm_filter_configure()
592 if (!fl->sync_mode && !trig) in stm32_dfsdm_filter_configure()
593 return -EINVAL; in stm32_dfsdm_filter_configure()
594 cr1 |= DFSDM_CR1_JSYNC(fl->sync_mode); in stm32_dfsdm_filter_configure()
607 int chan_idx = ch->scan_index; in stm32_dfsdm_channel_parse_of()
610 ret = of_property_read_u32_index(indio_dev->dev.of_node, in stm32_dfsdm_channel_parse_of()
611 "st,adc-channels", chan_idx, in stm32_dfsdm_channel_parse_of()
612 &ch->channel); in stm32_dfsdm_channel_parse_of()
614 dev_err(&indio_dev->dev, in stm32_dfsdm_channel_parse_of()
615 " Error parsing 'st,adc-channels' for idx %d\n", in stm32_dfsdm_channel_parse_of()
619 if (ch->channel >= dfsdm->num_chs) { in stm32_dfsdm_channel_parse_of()
620 dev_err(&indio_dev->dev, in stm32_dfsdm_channel_parse_of()
622 ch->channel, dfsdm->num_chs); in stm32_dfsdm_channel_parse_of()
623 return -EINVAL; in stm32_dfsdm_channel_parse_of()
626 ret = of_property_read_string_index(indio_dev->dev.of_node, in stm32_dfsdm_channel_parse_of()
627 "st,adc-channel-names", chan_idx, in stm32_dfsdm_channel_parse_of()
628 &ch->datasheet_name); in stm32_dfsdm_channel_parse_of()
630 dev_err(&indio_dev->dev, in stm32_dfsdm_channel_parse_of()
631 " Error parsing 'st,adc-channel-names' for idx %d\n", in stm32_dfsdm_channel_parse_of()
636 df_ch = &dfsdm->ch_list[ch->channel]; in stm32_dfsdm_channel_parse_of()
637 df_ch->id = ch->channel; in stm32_dfsdm_channel_parse_of()
639 ret = of_property_read_string_index(indio_dev->dev.of_node, in stm32_dfsdm_channel_parse_of()
640 "st,adc-channel-types", chan_idx, in stm32_dfsdm_channel_parse_of()
649 df_ch->type = val; in stm32_dfsdm_channel_parse_of()
651 ret = of_property_read_string_index(indio_dev->dev.of_node, in stm32_dfsdm_channel_parse_of()
652 "st,adc-channel-clk-src", chan_idx, in stm32_dfsdm_channel_parse_of()
661 df_ch->src = val; in stm32_dfsdm_channel_parse_of()
663 ret = of_property_read_u32_index(indio_dev->dev.of_node, in stm32_dfsdm_channel_parse_of()
664 "st,adc-alt-channel", chan_idx, in stm32_dfsdm_channel_parse_of()
665 &df_ch->alt_si); in stm32_dfsdm_channel_parse_of()
667 df_ch->alt_si = 0; in stm32_dfsdm_channel_parse_of()
683 ret = fwnode_property_read_u32(node, "reg", &ch->channel); in stm32_dfsdm_generic_channel_parse_of()
685 dev_err(&indio_dev->dev, "Missing channel index %d\n", ret); in stm32_dfsdm_generic_channel_parse_of()
689 if (ch->channel >= dfsdm->num_chs) { in stm32_dfsdm_generic_channel_parse_of()
690 dev_err(&indio_dev->dev, " Error bad channel number %d (max = %d)\n", in stm32_dfsdm_generic_channel_parse_of()
691 ch->channel, dfsdm->num_chs); in stm32_dfsdm_generic_channel_parse_of()
692 return -EINVAL; in stm32_dfsdm_generic_channel_parse_of()
697 ret = fwnode_property_read_string(node, "label", &ch->datasheet_name); in stm32_dfsdm_generic_channel_parse_of()
699 dev_err(&indio_dev->dev, in stm32_dfsdm_generic_channel_parse_of()
700 " Error parsing 'label' for idx %d\n", ch->channel); in stm32_dfsdm_generic_channel_parse_of()
705 df_ch = &dfsdm->ch_list[ch->channel]; in stm32_dfsdm_generic_channel_parse_of()
706 df_ch->id = ch->channel; in stm32_dfsdm_generic_channel_parse_of()
708 ret = fwnode_property_read_string(node, "st,adc-channel-type", &of_str); in stm32_dfsdm_generic_channel_parse_of()
716 df_ch->type = val; in stm32_dfsdm_generic_channel_parse_of()
718 ret = fwnode_property_read_string(node, "st,adc-channel-clk-src", &of_str); in stm32_dfsdm_generic_channel_parse_of()
726 df_ch->src = val; in stm32_dfsdm_generic_channel_parse_of()
728 ret = fwnode_property_read_u32(node, "st,adc-alt-channel", &df_ch->alt_si); in stm32_dfsdm_generic_channel_parse_of()
729 if (ret != -EINVAL) in stm32_dfsdm_generic_channel_parse_of()
730 df_ch->alt_si = 0; in stm32_dfsdm_generic_channel_parse_of()
732 if (adc->dev_data->type == DFSDM_IIO) { in stm32_dfsdm_generic_channel_parse_of()
733 backend = devm_iio_backend_fwnode_get(&indio_dev->dev, NULL, node); in stm32_dfsdm_generic_channel_parse_of()
735 return dev_err_probe(&indio_dev->dev, PTR_ERR(backend), in stm32_dfsdm_generic_channel_parse_of()
737 adc->backend[ch->scan_index] = backend; in stm32_dfsdm_generic_channel_parse_of()
750 return snprintf(buf, PAGE_SIZE, "%d\n", adc->spi_freq); in dfsdm_adc_audio_get_spiclk()
763 dev_dbg(&indio_dev->dev, in dfsdm_adc_set_samp_freq()
771 adc->sample_freq = spi_freq / oversamp; in dfsdm_adc_set_samp_freq()
772 adc->oversamp = oversamp; in dfsdm_adc_set_samp_freq()
783 struct stm32_dfsdm_channel *ch = &adc->dfsdm->ch_list[chan->channel]; in dfsdm_adc_audio_set_spiclk()
784 unsigned int sample_freq = adc->sample_freq; in dfsdm_adc_audio_set_spiclk()
788 dev_err(&indio_dev->dev, "enter %s\n", __func__); in dfsdm_adc_audio_set_spiclk()
790 if (ch->src != DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL) in dfsdm_adc_audio_set_spiclk()
791 return -EPERM; in dfsdm_adc_audio_set_spiclk()
798 return -EINVAL; in dfsdm_adc_audio_set_spiclk()
805 adc->spi_freq = spi_freq; in dfsdm_adc_audio_set_spiclk()
814 struct regmap *regmap = adc->dfsdm->regmap; in stm32_dfsdm_start_conv()
817 ret = stm32_dfsdm_channels_configure(indio_dev, adc->fl_id, trig); in stm32_dfsdm_start_conv()
825 ret = stm32_dfsdm_filter_configure(indio_dev, adc->fl_id, trig); in stm32_dfsdm_start_conv()
829 ret = stm32_dfsdm_start_filter(adc, adc->fl_id, trig); in stm32_dfsdm_start_conv()
836 regmap_clear_bits(regmap, DFSDM_CR1(adc->fl_id), DFSDM_CR1_CFG_MASK); in stm32_dfsdm_start_conv()
846 struct regmap *regmap = adc->dfsdm->regmap; in stm32_dfsdm_stop_conv()
848 stm32_dfsdm_stop_filter(adc->dfsdm, adc->fl_id); in stm32_dfsdm_stop_conv()
850 regmap_clear_bits(regmap, DFSDM_CR1(adc->fl_id), DFSDM_CR1_CFG_MASK); in stm32_dfsdm_stop_conv()
865 * - always one buffer (period) DMA is working on in stm32_dfsdm_set_watermark()
866 * - one buffer (period) driver pushed to ASoC side. in stm32_dfsdm_set_watermark()
869 adc->buf_sz = min(rx_buf_sz, watermark * 2 * adc->nconv); in stm32_dfsdm_set_watermark()
879 status = dmaengine_tx_status(adc->dma_chan, in stm32_dfsdm_adc_dma_residue()
880 adc->dma_chan->cookie, in stm32_dfsdm_adc_dma_residue()
884 unsigned int i = adc->buf_sz - state.residue; in stm32_dfsdm_adc_dma_residue()
888 if (i >= adc->bufi) in stm32_dfsdm_adc_dma_residue()
889 size = i - adc->bufi; in stm32_dfsdm_adc_dma_residue()
891 size = adc->buf_sz + i - adc->bufi; in stm32_dfsdm_adc_dma_residue()
902 struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[adc->fl_id]; in stm32_dfsdm_process_data()
903 struct stm32_dfsdm_filter_osr *flo = &fl->flo[fl->fast]; in stm32_dfsdm_process_data()
904 unsigned int i = adc->nconv; in stm32_dfsdm_process_data()
907 while (i--) { in stm32_dfsdm_process_data()
910 /* Convert 2^(n-1) sample to 2^(n-1)-1 to avoid wrap-around */ in stm32_dfsdm_process_data()
911 if (*ptr > flo->max) in stm32_dfsdm_process_data()
912 *ptr -= 1; in stm32_dfsdm_process_data()
917 *ptr <<= flo->lshift; in stm32_dfsdm_process_data()
939 dev_dbg(&indio_dev->dev, "pos = %d, available = %d\n", in stm32_dfsdm_dma_buffer_done()
940 adc->bufi, available); in stm32_dfsdm_dma_buffer_done()
941 old_pos = adc->bufi; in stm32_dfsdm_dma_buffer_done()
943 while (available >= indio_dev->scan_bytes) { in stm32_dfsdm_dma_buffer_done()
944 s32 *buffer = (s32 *)&adc->rx_buf[adc->bufi]; in stm32_dfsdm_dma_buffer_done()
948 available -= indio_dev->scan_bytes; in stm32_dfsdm_dma_buffer_done()
949 adc->bufi += indio_dev->scan_bytes; in stm32_dfsdm_dma_buffer_done()
950 if (adc->bufi >= adc->buf_sz) { in stm32_dfsdm_dma_buffer_done()
951 if (adc->cb) in stm32_dfsdm_dma_buffer_done()
952 adc->cb(&adc->rx_buf[old_pos], in stm32_dfsdm_dma_buffer_done()
953 adc->buf_sz - old_pos, adc->cb_priv); in stm32_dfsdm_dma_buffer_done()
954 adc->bufi = 0; in stm32_dfsdm_dma_buffer_done()
966 if (adc->dev_data->type == DFSDM_IIO) in stm32_dfsdm_dma_buffer_done()
969 if (adc->cb) in stm32_dfsdm_dma_buffer_done()
970 adc->cb(&adc->rx_buf[old_pos], adc->bufi - old_pos, in stm32_dfsdm_dma_buffer_done()
971 adc->cb_priv); in stm32_dfsdm_dma_buffer_done()
978 * The DFSDM supports half-word transfers. However, for 16 bits record, in stm32_dfsdm_adc_dma_start()
983 .src_addr = (dma_addr_t)adc->dfsdm->phys_base, in stm32_dfsdm_adc_dma_start()
990 if (!adc->dma_chan) in stm32_dfsdm_adc_dma_start()
991 return -EINVAL; in stm32_dfsdm_adc_dma_start()
993 dev_dbg(&indio_dev->dev, "size=%d watermark=%d\n", in stm32_dfsdm_adc_dma_start()
994 adc->buf_sz, adc->buf_sz / 2); in stm32_dfsdm_adc_dma_start()
996 if (adc->nconv == 1 && !indio_dev->trig) in stm32_dfsdm_adc_dma_start()
997 config.src_addr += DFSDM_RDATAR(adc->fl_id); in stm32_dfsdm_adc_dma_start()
999 config.src_addr += DFSDM_JDATAR(adc->fl_id); in stm32_dfsdm_adc_dma_start()
1000 ret = dmaengine_slave_config(adc->dma_chan, &config); in stm32_dfsdm_adc_dma_start()
1005 desc = dmaengine_prep_dma_cyclic(adc->dma_chan, in stm32_dfsdm_adc_dma_start()
1006 adc->dma_buf, in stm32_dfsdm_adc_dma_start()
1007 adc->buf_sz, adc->buf_sz / 2, in stm32_dfsdm_adc_dma_start()
1011 return -EBUSY; in stm32_dfsdm_adc_dma_start()
1013 desc->callback = stm32_dfsdm_dma_buffer_done; in stm32_dfsdm_adc_dma_start()
1014 desc->callback_param = indio_dev; in stm32_dfsdm_adc_dma_start()
1022 dma_async_issue_pending(adc->dma_chan); in stm32_dfsdm_adc_dma_start()
1024 if (adc->nconv == 1 && !indio_dev->trig) { in stm32_dfsdm_adc_dma_start()
1026 ret = regmap_set_bits(adc->dfsdm->regmap, in stm32_dfsdm_adc_dma_start()
1027 DFSDM_CR1(adc->fl_id), in stm32_dfsdm_adc_dma_start()
1031 ret = regmap_set_bits(adc->dfsdm->regmap, in stm32_dfsdm_adc_dma_start()
1032 DFSDM_CR1(adc->fl_id), in stm32_dfsdm_adc_dma_start()
1042 dmaengine_terminate_all(adc->dma_chan); in stm32_dfsdm_adc_dma_start()
1051 if (!adc->dma_chan) in stm32_dfsdm_adc_dma_stop()
1054 regmap_clear_bits(adc->dfsdm->regmap, DFSDM_CR1(adc->fl_id), in stm32_dfsdm_adc_dma_stop()
1056 dmaengine_terminate_all(adc->dma_chan); in stm32_dfsdm_adc_dma_stop()
1064 adc->nconv = bitmap_weight(scan_mask, iio_get_masklength(indio_dev)); in stm32_dfsdm_update_scan_mode()
1065 adc->smask = *scan_mask; in stm32_dfsdm_update_scan_mode()
1067 dev_dbg(&indio_dev->dev, "nconv=%d mask=%lx\n", adc->nconv, *scan_mask); in stm32_dfsdm_update_scan_mode()
1079 adc->bufi = 0; in stm32_dfsdm_postenable()
1081 if (adc->hwc) { in stm32_dfsdm_postenable()
1082 ret = iio_hw_consumer_enable(adc->hwc); in stm32_dfsdm_postenable()
1087 if (adc->backend) { in stm32_dfsdm_postenable()
1088 while (adc->backend[i]) { in stm32_dfsdm_postenable()
1089 ret = iio_backend_enable(adc->backend[i]); in stm32_dfsdm_postenable()
1096 ret = stm32_dfsdm_start_dfsdm(adc->dfsdm); in stm32_dfsdm_postenable()
1102 dev_err(&indio_dev->dev, "Can't start DMA\n"); in stm32_dfsdm_postenable()
1106 ret = stm32_dfsdm_start_conv(indio_dev, indio_dev->trig); in stm32_dfsdm_postenable()
1108 dev_err(&indio_dev->dev, "Can't start conversion\n"); in stm32_dfsdm_postenable()
1117 stm32_dfsdm_stop_dfsdm(adc->dfsdm); in stm32_dfsdm_postenable()
1119 if (adc->hwc) in stm32_dfsdm_postenable()
1120 iio_hw_consumer_disable(adc->hwc); in stm32_dfsdm_postenable()
1134 stm32_dfsdm_stop_dfsdm(adc->dfsdm); in stm32_dfsdm_predisable()
1136 if (adc->backend) { in stm32_dfsdm_predisable()
1137 while (adc->backend[i]) { in stm32_dfsdm_predisable()
1138 iio_backend_disable(adc->backend[i]); in stm32_dfsdm_predisable()
1143 if (adc->hwc) in stm32_dfsdm_predisable()
1144 iio_hw_consumer_disable(adc->hwc); in stm32_dfsdm_predisable()
1155 * stm32_dfsdm_get_buff_cb() - register a callback that will be called when
1160 * - data: pointer to data buffer
1161 * - size: size in byte of the data buffer
1162 * - private: pointer to consumer private structure.
1173 return -EINVAL; in stm32_dfsdm_get_buff_cb()
1176 adc->cb = cb; in stm32_dfsdm_get_buff_cb()
1177 adc->cb_priv = private; in stm32_dfsdm_get_buff_cb()
1184 * stm32_dfsdm_release_buff_cb - unregister buffer callback
1193 return -EINVAL; in stm32_dfsdm_release_buff_cb()
1196 adc->cb = NULL; in stm32_dfsdm_release_buff_cb()
1197 adc->cb_priv = NULL; in stm32_dfsdm_release_buff_cb()
1210 reinit_completion(&adc->completion); in stm32_dfsdm_single_conv()
1212 adc->buffer = res; in stm32_dfsdm_single_conv()
1214 ret = stm32_dfsdm_start_dfsdm(adc->dfsdm); in stm32_dfsdm_single_conv()
1218 ret = regmap_update_bits(adc->dfsdm->regmap, DFSDM_CR2(adc->fl_id), in stm32_dfsdm_single_conv()
1223 adc->nconv = 1; in stm32_dfsdm_single_conv()
1224 adc->smask = BIT(chan->scan_index); in stm32_dfsdm_single_conv()
1227 regmap_update_bits(adc->dfsdm->regmap, DFSDM_CR2(adc->fl_id), in stm32_dfsdm_single_conv()
1232 time_left = wait_for_completion_interruptible_timeout(&adc->completion, in stm32_dfsdm_single_conv()
1236 regmap_update_bits(adc->dfsdm->regmap, DFSDM_CR2(adc->fl_id), in stm32_dfsdm_single_conv()
1240 ret = -ETIMEDOUT; in stm32_dfsdm_single_conv()
1251 stm32_dfsdm_stop_dfsdm(adc->dfsdm); in stm32_dfsdm_single_conv()
1261 struct stm32_dfsdm_channel *ch = &adc->dfsdm->ch_list[chan->channel]; in stm32_dfsdm_write_raw()
1263 int ret = -EINVAL; in stm32_dfsdm_write_raw()
1265 switch (ch->src) { in stm32_dfsdm_write_raw()
1267 spi_freq = adc->dfsdm->spi_master_freq; in stm32_dfsdm_write_raw()
1271 spi_freq = adc->dfsdm->spi_master_freq / 2; in stm32_dfsdm_write_raw()
1274 spi_freq = adc->spi_freq; in stm32_dfsdm_write_raw()
1280 return -EBUSY; in stm32_dfsdm_write_raw()
1284 dev_dbg(&indio_dev->dev, in stm32_dfsdm_write_raw()
1286 adc->sample_freq, spi_freq / val); in stm32_dfsdm_write_raw()
1287 adc->oversamp = val; in stm32_dfsdm_write_raw()
1288 adc->sample_freq = spi_freq / val; in stm32_dfsdm_write_raw()
1295 return -EINVAL; in stm32_dfsdm_write_raw()
1298 return -EBUSY; in stm32_dfsdm_write_raw()
1305 return -EINVAL; in stm32_dfsdm_write_raw()
1315 if (adc->hwc) in __stm32_dfsdm_read_info_raw()
1316 ret = iio_hw_consumer_enable(adc->hwc); in __stm32_dfsdm_read_info_raw()
1317 if (adc->backend) in __stm32_dfsdm_read_info_raw()
1318 ret = iio_backend_enable(adc->backend[chan->scan_index]); in __stm32_dfsdm_read_info_raw()
1320 dev_err(&indio_dev->dev, in __stm32_dfsdm_read_info_raw()
1322 __func__, chan->channel); in __stm32_dfsdm_read_info_raw()
1326 if (adc->hwc) in __stm32_dfsdm_read_info_raw()
1327 iio_hw_consumer_disable(adc->hwc); in __stm32_dfsdm_read_info_raw()
1328 if (adc->backend) in __stm32_dfsdm_read_info_raw()
1329 iio_backend_disable(adc->backend[chan->scan_index]); in __stm32_dfsdm_read_info_raw()
1331 dev_err(&indio_dev->dev, in __stm32_dfsdm_read_info_raw()
1333 __func__, chan->channel); in __stm32_dfsdm_read_info_raw()
1346 struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[adc->fl_id]; in stm32_dfsdm_read_raw()
1347 struct stm32_dfsdm_filter_osr *flo = &fl->flo[fl->fast]; in stm32_dfsdm_read_raw()
1348 u32 max = flo->max << (flo->lshift - chan->scan_type.shift); in stm32_dfsdm_read_raw()
1349 int idx = chan->scan_index; in stm32_dfsdm_read_raw()
1352 if (flo->lshift < chan->scan_type.shift) in stm32_dfsdm_read_raw()
1353 max = flo->max >> (chan->scan_type.shift - flo->lshift); in stm32_dfsdm_read_raw()
1358 return -EBUSY; in stm32_dfsdm_read_raw()
1367 *val = adc->oversamp; in stm32_dfsdm_read_raw()
1372 *val = adc->sample_freq; in stm32_dfsdm_read_raw()
1380 * than 2^n, where n = realbits - 1. in stm32_dfsdm_read_raw()
1385 if (adc->backend) { in stm32_dfsdm_read_raw()
1386 ret = iio_backend_read_scale(adc->backend[idx], chan, val, NULL); in stm32_dfsdm_read_raw()
1390 *val = div_u64((u64)*val * (u64)BIT(DFSDM_DATA_RES - 1), max); in stm32_dfsdm_read_raw()
1391 *val2 = chan->scan_type.realbits; in stm32_dfsdm_read_raw()
1392 if (chan->differential) in stm32_dfsdm_read_raw()
1399 * DFSDM output data are in the range [-2^n, 2^n], in stm32_dfsdm_read_raw()
1400 * with n = realbits - 1. in stm32_dfsdm_read_raw()
1401 * - Differential modulator: in stm32_dfsdm_read_raw()
1403 * - Single ended modulator: in stm32_dfsdm_read_raw()
1405 * where 0V corresponds to -2^n, and Vref to 2^n. in stm32_dfsdm_read_raw()
1409 if (adc->backend) { in stm32_dfsdm_read_raw()
1410 ret = iio_backend_read_offset(adc->backend[idx], chan, val, NULL); in stm32_dfsdm_read_raw()
1414 *val = div_u64((u64)max * *val, BIT(*val2 - 1)); in stm32_dfsdm_read_raw()
1415 if (!chan->differential) in stm32_dfsdm_read_raw()
1421 return -EINVAL; in stm32_dfsdm_read_raw()
1427 return stm32_dfsdm_get_jextsel(indio_dev, trig) < 0 ? -EINVAL : 0; in stm32_dfsdm_validate_trigger()
1449 struct regmap *regmap = adc->dfsdm->regmap; in stm32_dfsdm_irq()
1452 regmap_read(regmap, DFSDM_ISR(adc->fl_id), &status); in stm32_dfsdm_irq()
1453 regmap_read(regmap, DFSDM_CR2(adc->fl_id), &int_en); in stm32_dfsdm_irq()
1457 regmap_read(regmap, DFSDM_RDATAR(adc->fl_id), adc->buffer); in stm32_dfsdm_irq()
1458 complete(&adc->completion); in stm32_dfsdm_irq()
1463 dev_warn(&indio_dev->dev, "Overrun detected\n"); in stm32_dfsdm_irq()
1464 regmap_set_bits(regmap, DFSDM_ICR(adc->fl_id), in stm32_dfsdm_irq()
1490 if (adc->dma_chan) { in stm32_dfsdm_dma_release()
1491 dma_free_coherent(adc->dma_chan->device->dev, in stm32_dfsdm_dma_release()
1493 adc->rx_buf, adc->dma_buf); in stm32_dfsdm_dma_release()
1494 dma_release_channel(adc->dma_chan); in stm32_dfsdm_dma_release()
1503 adc->dma_chan = dma_request_chan(dev, "rx"); in stm32_dfsdm_dma_request()
1504 if (IS_ERR(adc->dma_chan)) { in stm32_dfsdm_dma_request()
1505 int ret = PTR_ERR(adc->dma_chan); in stm32_dfsdm_dma_request()
1507 adc->dma_chan = NULL; in stm32_dfsdm_dma_request()
1511 adc->rx_buf = dma_alloc_coherent(adc->dma_chan->device->dev, in stm32_dfsdm_dma_request()
1513 &adc->dma_buf, GFP_KERNEL); in stm32_dfsdm_dma_request()
1514 if (!adc->rx_buf) { in stm32_dfsdm_dma_request()
1515 dma_release_channel(adc->dma_chan); in stm32_dfsdm_dma_request()
1516 return -ENOMEM; in stm32_dfsdm_dma_request()
1519 indio_dev->modes |= INDIO_BUFFER_SOFTWARE; in stm32_dfsdm_dma_request()
1520 indio_dev->setup_ops = &stm32_dfsdm_buffer_setup_ops; in stm32_dfsdm_dma_request()
1532 ret = stm32_dfsdm_generic_channel_parse_of(adc->dfsdm, indio_dev, ch, child); in stm32_dfsdm_adc_chan_init_one()
1534 ret = stm32_dfsdm_channel_parse_of(adc->dfsdm, indio_dev, ch); in stm32_dfsdm_adc_chan_init_one()
1536 return dev_err_probe(&indio_dev->dev, ret, "Failed to parse channel\n"); in stm32_dfsdm_adc_chan_init_one()
1538 ch->type = IIO_VOLTAGE; in stm32_dfsdm_adc_chan_init_one()
1539 ch->indexed = 1; in stm32_dfsdm_adc_chan_init_one()
1546 ch->info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | in stm32_dfsdm_adc_chan_init_one()
1551 ch->info_mask_separate = BIT(IIO_CHAN_INFO_RAW); in stm32_dfsdm_adc_chan_init_one()
1554 ch->info_mask_shared_by_all = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) | in stm32_dfsdm_adc_chan_init_one()
1557 if (adc->dev_data->type == DFSDM_AUDIO) { in stm32_dfsdm_adc_chan_init_one()
1558 ch->ext_info = dfsdm_adc_audio_ext_info; in stm32_dfsdm_adc_chan_init_one()
1559 ch->scan_index = 0; in stm32_dfsdm_adc_chan_init_one()
1561 ch->scan_type.shift = 8; in stm32_dfsdm_adc_chan_init_one()
1563 ch->scan_type.sign = 's'; in stm32_dfsdm_adc_chan_init_one()
1564 ch->scan_type.realbits = 24; in stm32_dfsdm_adc_chan_init_one()
1565 ch->scan_type.storagebits = 32; in stm32_dfsdm_adc_chan_init_one()
1567 return stm32_dfsdm_chan_configure(adc->dfsdm, in stm32_dfsdm_adc_chan_init_one()
1568 &adc->dfsdm->ch_list[ch->channel]); in stm32_dfsdm_adc_chan_init_one()
1573 int num_ch = indio_dev->num_channels; in stm32_dfsdm_chan_init()
1581 return dev_err_probe(&indio_dev->dev, ret, "Channels init failed\n"); in stm32_dfsdm_chan_init()
1591 device_for_each_child_node_scoped(&indio_dev->dev, child) { in stm32_dfsdm_generic_chan_init()
1599 return dev_err_probe(&indio_dev->dev, ret, "Channels init failed\n"); in stm32_dfsdm_generic_chan_init()
1615 /* If st,adc-channels is defined legacy binding is used. Else assume generic binding. */ in stm32_dfsdm_audio_init()
1616 num_ch = of_property_count_u32_elems(indio_dev->dev.of_node, "st,adc-channels"); in stm32_dfsdm_audio_init()
1620 ch = devm_kzalloc(&indio_dev->dev, sizeof(*ch), GFP_KERNEL); in stm32_dfsdm_audio_init()
1622 return -ENOMEM; in stm32_dfsdm_audio_init()
1624 indio_dev->num_channels = 1; in stm32_dfsdm_audio_init()
1625 indio_dev->channels = ch; in stm32_dfsdm_audio_init()
1633 dev_err(&indio_dev->dev, "Channels init failed\n"); in stm32_dfsdm_audio_init()
1636 ch->info_mask_separate = BIT(IIO_CHAN_INFO_SAMP_FREQ); in stm32_dfsdm_audio_init()
1638 d_ch = &adc->dfsdm->ch_list[ch->channel]; in stm32_dfsdm_audio_init()
1639 if (d_ch->src != DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL) in stm32_dfsdm_audio_init()
1640 adc->spi_freq = adc->dfsdm->spi_master_freq; in stm32_dfsdm_audio_init()
1652 adc->oversamp = DFSDM_DEFAULT_OVERSAMPLING; in stm32_dfsdm_adc_init()
1653 ret = stm32_dfsdm_compute_all_osrs(indio_dev, adc->oversamp); in stm32_dfsdm_adc_init()
1657 num_ch = device_get_child_node_count(&indio_dev->dev); in stm32_dfsdm_adc_init()
1660 num_ch = of_property_count_u32_elems(indio_dev->dev.of_node, "st,adc-channels"); in stm32_dfsdm_adc_init()
1662 dev_err(&indio_dev->dev, "Bad st,adc-channels\n"); in stm32_dfsdm_adc_init()
1669 if (num_ch > adc->dfsdm->num_chs) { in stm32_dfsdm_adc_init()
1670 dev_err(&indio_dev->dev, "Number of channel [%d] exceeds [%d]\n", in stm32_dfsdm_adc_init()
1671 num_ch, adc->dfsdm->num_chs); in stm32_dfsdm_adc_init()
1672 return -EINVAL; in stm32_dfsdm_adc_init()
1674 indio_dev->num_channels = num_ch; in stm32_dfsdm_adc_init()
1678 adc->hwc = devm_iio_hw_consumer_alloc(&indio_dev->dev); in stm32_dfsdm_adc_init()
1679 if (IS_ERR(adc->hwc)) in stm32_dfsdm_adc_init()
1680 return dev_err_probe(&indio_dev->dev, -EPROBE_DEFER, in stm32_dfsdm_adc_init()
1684 adc->hwc = NULL; in stm32_dfsdm_adc_init()
1686 adc->backend = devm_kcalloc(&indio_dev->dev, num_ch, sizeof(*adc->backend), in stm32_dfsdm_adc_init()
1688 if (!adc->backend) in stm32_dfsdm_adc_init()
1689 return -ENOMEM; in stm32_dfsdm_adc_init()
1692 ch = devm_kcalloc(&indio_dev->dev, num_ch, sizeof(*ch), GFP_KERNEL); in stm32_dfsdm_adc_init()
1694 return -ENOMEM; in stm32_dfsdm_adc_init()
1695 indio_dev->channels = ch; in stm32_dfsdm_adc_init()
1704 init_completion(&adc->completion); in stm32_dfsdm_adc_init()
1709 if (ret != -ENODEV) in stm32_dfsdm_adc_init()
1722 dev_err(&indio_dev->dev, "buffer setup failed\n"); in stm32_dfsdm_adc_init()
1727 indio_dev->modes |= INDIO_HARDWARE_TRIGGERED; in stm32_dfsdm_adc_init()
1744 .compatible = "st,stm32-dfsdm-adc",
1748 .compatible = "st,stm32-dfsdm-dmic",
1757 struct device *dev = &pdev->dev; in stm32_dfsdm_adc_probe()
1759 struct device_node *np = dev->of_node; in stm32_dfsdm_adc_probe()
1769 return -ENOMEM; in stm32_dfsdm_adc_probe()
1773 adc->dfsdm = dev_get_drvdata(dev->parent); in stm32_dfsdm_adc_probe()
1775 iio->dev.of_node = np; in stm32_dfsdm_adc_probe()
1776 iio->modes = INDIO_DIRECT_MODE; in stm32_dfsdm_adc_probe()
1780 ret = of_property_read_u32(dev->of_node, "reg", &adc->fl_id); in stm32_dfsdm_adc_probe()
1781 if (ret != 0 || adc->fl_id >= adc->dfsdm->num_fls) { in stm32_dfsdm_adc_probe()
1783 return -EINVAL; in stm32_dfsdm_adc_probe()
1786 name = devm_kzalloc(dev, sizeof("dfsdm-adc0"), GFP_KERNEL); in stm32_dfsdm_adc_probe()
1788 return -ENOMEM; in stm32_dfsdm_adc_probe()
1789 if (dev_data->type == DFSDM_AUDIO) { in stm32_dfsdm_adc_probe()
1790 iio->info = &stm32_dfsdm_info_audio; in stm32_dfsdm_adc_probe()
1791 snprintf(name, sizeof("dfsdm-pdm0"), "dfsdm-pdm%d", adc->fl_id); in stm32_dfsdm_adc_probe()
1793 iio->info = &stm32_dfsdm_info_adc; in stm32_dfsdm_adc_probe()
1794 snprintf(name, sizeof("dfsdm-adc0"), "dfsdm-adc%d", adc->fl_id); in stm32_dfsdm_adc_probe()
1796 iio->name = name; in stm32_dfsdm_adc_probe()
1807 0, pdev->name, iio); in stm32_dfsdm_adc_probe()
1813 ret = of_property_read_u32(dev->of_node, "st,filter-order", &val); in stm32_dfsdm_adc_probe()
1819 adc->dfsdm->fl_list[adc->fl_id].ford = val; in stm32_dfsdm_adc_probe()
1821 ret = of_property_read_u32(dev->of_node, "st,filter0-sync", &val); in stm32_dfsdm_adc_probe()
1823 adc->dfsdm->fl_list[adc->fl_id].sync_mode = val; in stm32_dfsdm_adc_probe()
1825 adc->dev_data = dev_data; in stm32_dfsdm_adc_probe()
1826 ret = dev_data->init(dev, iio); in stm32_dfsdm_adc_probe()
1834 if (dev_data->type == DFSDM_AUDIO) { in stm32_dfsdm_adc_probe()
1857 if (adc->dev_data->type == DFSDM_AUDIO) in stm32_dfsdm_adc_remove()
1858 of_platform_depopulate(&pdev->dev); in stm32_dfsdm_adc_remove()
1882 for (i = 0; i < indio_dev->num_channels; i++) { in stm32_dfsdm_adc_resume()
1883 chan = indio_dev->channels + i; in stm32_dfsdm_adc_resume()
1884 ch = &adc->dfsdm->ch_list[chan->channel]; in stm32_dfsdm_adc_resume()
1885 ret = stm32_dfsdm_chan_configure(adc->dfsdm, ch); in stm32_dfsdm_adc_resume()
1902 .name = "stm32-dfsdm-adc",