Lines Matching +full:pdm +full:- +full:fmt

1 // SPDX-License-Identifier: GPL-2.0
5 // Copyright (C) 2019-2022 Microchip Technology Inc. and its subsidiaries
9 #include <dt-bindings/sound/microchip,pdmc.h>
24 * ---- PDMC Register map ----
37 * ---- Control Register (Write-only) ----
42 * ---- Mode Register (Read/Write) ----
66 * ---- Configuration Register (Read/Write) ----
75 * ---- Interrupt Enable/Disable/Mask/Status Registers ----
85 * ---- Version Register (Read-only) ----
139 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; in mchp_pdmc_sinc_order_get()
142 item = snd_soc_enum_val_to_item(e, dd->sinc_order); in mchp_pdmc_sinc_order_get()
143 uvalue->value.enumerated.item[0] = item; in mchp_pdmc_sinc_order_get()
153 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; in mchp_pdmc_sinc_order_put()
154 unsigned int *item = uvalue->value.enumerated.item; in mchp_pdmc_sinc_order_put()
157 if (item[0] >= e->items) in mchp_pdmc_sinc_order_put()
158 return -EINVAL; in mchp_pdmc_sinc_order_put()
160 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l; in mchp_pdmc_sinc_order_put()
161 if (val == dd->sinc_order) in mchp_pdmc_sinc_order_put()
164 dd->sinc_order = val; in mchp_pdmc_sinc_order_put()
175 uvalue->value.integer.value[0] = !!dd->audio_filter_en; in mchp_pdmc_af_get()
185 bool af = uvalue->value.integer.value[0] ? true : false; in mchp_pdmc_af_put()
187 if (dd->audio_filter_en == af) in mchp_pdmc_af_put()
190 dd->audio_filter_en = af; in mchp_pdmc_af_put()
199 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; in mchp_pdmc_chmap_ctl_info()
200 uinfo->count = info->dd->mic_no; in mchp_pdmc_chmap_ctl_info()
201 uinfo->value.integer.min = 0; in mchp_pdmc_chmap_ctl_info()
202 uinfo->value.integer.max = SNDRV_CHMAP_RR; /* maxmimum 4 channels */ in mchp_pdmc_chmap_ctl_info()
211 for (s = info->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; s; s = s->next) in mchp_pdmc_chmap_substream()
212 if (s->number == idx) in mchp_pdmc_chmap_substream()
222 for (map = ch_info->chmap; map->channels; map++) { in mchp_pdmc_chmap_get()
223 if (map->channels == substream->runtime->channels) in mchp_pdmc_chmap_get()
233 struct mchp_pdmc *dd = info->dd; in mchp_pdmc_chmap_ctl_get()
234 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); in mchp_pdmc_chmap_ctl_get()
240 if (!info->chmap) in mchp_pdmc_chmap_ctl_get()
241 return -EINVAL; in mchp_pdmc_chmap_ctl_get()
244 return -ENODEV; in mchp_pdmc_chmap_ctl_get()
245 memset(ucontrol->value.integer.value, 0, sizeof(long) * info->dd->mic_no); in mchp_pdmc_chmap_ctl_get()
246 if (!substream->runtime) in mchp_pdmc_chmap_ctl_get()
251 return -EINVAL; in mchp_pdmc_chmap_ctl_get()
253 for (i = 0; i < map->channels; i++) { in mchp_pdmc_chmap_ctl_get()
254 int map_idx = map->channels == 1 ? map->map[i] - SNDRV_CHMAP_MONO : in mchp_pdmc_chmap_ctl_get()
255 map->map[i] - SNDRV_CHMAP_FL; in mchp_pdmc_chmap_ctl_get()
258 if (dd->channel_mic_map[map_idx].ds_pos) in mchp_pdmc_chmap_ctl_get()
260 if (dd->channel_mic_map[map_idx].clk_edge) in mchp_pdmc_chmap_ctl_get()
263 ucontrol->value.integer.value[i] = map->map[i]; in mchp_pdmc_chmap_ctl_get()
266 regmap_write(dd->regmap, MCHP_PDMC_CFGR, cfgr_val); in mchp_pdmc_chmap_ctl_get()
275 struct mchp_pdmc *dd = info->dd; in mchp_pdmc_chmap_ctl_put()
276 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); in mchp_pdmc_chmap_ctl_put()
282 if (!info->chmap) in mchp_pdmc_chmap_ctl_put()
283 return -EINVAL; in mchp_pdmc_chmap_ctl_put()
286 return -ENODEV; in mchp_pdmc_chmap_ctl_put()
288 if (!substream->runtime) in mchp_pdmc_chmap_ctl_put()
293 return -EINVAL; in mchp_pdmc_chmap_ctl_put()
295 for (i = 0; i < map->channels; i++) { in mchp_pdmc_chmap_ctl_put()
298 map->map[i] = ucontrol->value.integer.value[i]; in mchp_pdmc_chmap_ctl_put()
299 map_idx = map->channels == 1 ? map->map[i] - SNDRV_CHMAP_MONO : in mchp_pdmc_chmap_ctl_put()
300 map->map[i] - SNDRV_CHMAP_FL; in mchp_pdmc_chmap_ctl_put()
303 if (dd->channel_mic_map[map_idx].ds_pos) in mchp_pdmc_chmap_ctl_put()
305 if (dd->channel_mic_map[map_idx].clk_edge) in mchp_pdmc_chmap_ctl_put()
309 regmap_write(dd->regmap, MCHP_PDMC_CFGR, cfgr_val); in mchp_pdmc_chmap_ctl_put()
318 info->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].chmap_kctl = NULL; in mchp_pdmc_chmap_ctl_private_free()
330 if (!info->chmap) in mchp_pdmc_chmap_ctl_tlv()
331 return -EINVAL; in mchp_pdmc_chmap_ctl_tlv()
333 return -ENOMEM; in mchp_pdmc_chmap_ctl_tlv()
335 return -EFAULT; in mchp_pdmc_chmap_ctl_tlv()
336 size -= 8; in mchp_pdmc_chmap_ctl_tlv()
338 for (map = info->chmap; map->channels; map++) { in mchp_pdmc_chmap_ctl_tlv()
339 int chs_bytes = map->channels * 4; in mchp_pdmc_chmap_ctl_tlv()
342 return -ENOMEM; in mchp_pdmc_chmap_ctl_tlv()
345 return -EFAULT; in mchp_pdmc_chmap_ctl_tlv()
347 size -= 8; in mchp_pdmc_chmap_ctl_tlv()
350 return -ENOMEM; in mchp_pdmc_chmap_ctl_tlv()
351 size -= chs_bytes; in mchp_pdmc_chmap_ctl_tlv()
353 for (c = 0; c < map->channels; c++) { in mchp_pdmc_chmap_ctl_tlv()
354 if (put_user(map->map[c], dst)) in mchp_pdmc_chmap_ctl_tlv()
355 return -EFAULT; in mchp_pdmc_chmap_ctl_tlv()
360 return -EFAULT; in mchp_pdmc_chmap_ctl_tlv()
394 if (component->name_prefix) in mchp_pdmc_open()
395 snprintf(id.name, sizeof(id.name), "%s %s", component->name_prefix, in mchp_pdmc_open()
396 control->name); in mchp_pdmc_open()
398 strscpy(id.name, control->name, sizeof(id.name)); in mchp_pdmc_open()
401 id.iface = control->iface; in mchp_pdmc_open()
402 id.device = control->device; in mchp_pdmc_open()
403 id.subdevice = control->subdevice; in mchp_pdmc_open()
404 id.index = control->index; in mchp_pdmc_open()
405 err = snd_ctl_remove_id(component->card->snd_card, &id); in mchp_pdmc_open()
407 dev_err(component->dev, "%d: Failed to remove %s\n", err, in mchp_pdmc_open()
408 control->name); in mchp_pdmc_open()
415 .name = "mchp-pdmc",
453 regmap_write(dd->regmap, MCHP_PDMC_CR, MCHP_PDMC_CR_SWRST); in mchp_pdmc_startup()
455 snd_pcm_hw_constraint_list(substream->runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, in mchp_pdmc_startup()
456 &mchp_pdmc_chan_constr[dd->mic_no - 1]); in mchp_pdmc_startup()
465 snd_soc_dai_init_dma_data(dai, NULL, &dd->addr); in mchp_pdmc_dai_probe()
470 static int mchp_pdmc_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) in mchp_pdmc_set_fmt() argument
472 unsigned int fmt_master = fmt & SND_SOC_DAIFMT_MASTER_MASK; in mchp_pdmc_set_fmt()
473 unsigned int fmt_format = fmt & SND_SOC_DAIFMT_FORMAT_MASK; in mchp_pdmc_set_fmt()
478 return -EINVAL; in mchp_pdmc_set_fmt()
480 /* IP supports only PDM interface */ in mchp_pdmc_set_fmt()
482 return -EINVAL; in mchp_pdmc_set_fmt()
547 struct snd_soc_component *comp = dai->component; in mchp_pdmc_hw_params()
558 dev_dbg(comp->dev, "%s() rate=%u format=%#x width=%u channels=%u\n", in mchp_pdmc_hw_params()
562 if (channels > dd->mic_no) { in mchp_pdmc_hw_params()
563 dev_err(comp->dev, "more channels %u than microphones %d\n", in mchp_pdmc_hw_params()
564 channels, dd->mic_no); in mchp_pdmc_hw_params()
565 return -EINVAL; in mchp_pdmc_hw_params()
568 dd->pdmcen = 0; in mchp_pdmc_hw_params()
570 dd->pdmcen |= MCHP_PDMC_MR_PDMCEN(i); in mchp_pdmc_hw_params()
571 if (dd->channel_mic_map[i].ds_pos) in mchp_pdmc_hw_params()
573 if (dd->channel_mic_map[i].clk_edge) in mchp_pdmc_hw_params()
577 for (osr_start = dd->audio_filter_en ? 64 : 8; in mchp_pdmc_hw_params()
582 round_rate = clk_round_rate(dd->gclk, in mchp_pdmc_hw_params()
586 diff_rate = abs((fs * 16 * osr_start) - round_rate); in mchp_pdmc_hw_params()
594 dev_err(comp->dev, "invalid sampling rate: %u\n", fs); in mchp_pdmc_hw_params()
595 return -EINVAL; in mchp_pdmc_hw_params()
599 clk_disable_unprepare(dd->gclk); in mchp_pdmc_hw_params()
602 ret = clk_set_rate(dd->gclk, gclk_rate); in mchp_pdmc_hw_params()
603 clk_prepare_enable(dd->gclk); in mchp_pdmc_hw_params()
605 dev_err(comp->dev, "unable to set rate %lu to GCLK: %d\n", in mchp_pdmc_hw_params()
610 mr_val |= mchp_pdmc_mr_set_osr(dd->audio_filter_en, osr); in mchp_pdmc_hw_params()
612 mr_val |= FIELD_PREP(MCHP_PDMC_MR_SINCORDER_MASK, dd->sinc_order); in mchp_pdmc_hw_params()
614 dd->addr.maxburst = mchp_pdmc_period_to_maxburst(snd_pcm_lib_period_bytes(substream)); in mchp_pdmc_hw_params()
615 mr_val |= FIELD_PREP(MCHP_PDMC_MR_CHUNK_MASK, dd->addr.maxburst); in mchp_pdmc_hw_params()
616 dev_dbg(comp->dev, "maxburst set to %d\n", dd->addr.maxburst); in mchp_pdmc_hw_params()
643 usleep_range(dd->startup_delay_us, dd->startup_delay_us + 5); in mchp_pdmc_noise_filter_workaround()
645 while (steps--) in mchp_pdmc_noise_filter_workaround()
646 regmap_read(dd->regmap, MCHP_PDMC_RHR, &tmp); in mchp_pdmc_noise_filter_workaround()
649 regmap_read(dd->regmap, MCHP_PDMC_ISR, &tmp); in mchp_pdmc_noise_filter_workaround()
656 struct snd_soc_component *cpu = dai->component; in mchp_pdmc_trigger()
667 dd->pdmcen); in mchp_pdmc_trigger()
672 regmap_write(dd->regmap, MCHP_PDMC_IER, dd->suspend_irq | in mchp_pdmc_trigger()
674 dd->suspend_irq = 0; in mchp_pdmc_trigger()
677 regmap_read(dd->regmap, MCHP_PDMC_IMR, &dd->suspend_irq); in mchp_pdmc_trigger()
681 regmap_write(dd->regmap, MCHP_PDMC_IDR, dd->suspend_irq | in mchp_pdmc_trigger()
689 return -EINVAL; in mchp_pdmc_trigger()
693 regmap_read(dd->regmap, MCHP_PDMC_MR, &val); in mchp_pdmc_trigger()
694 dev_dbg(dd->dev, "MR (0x%02x): 0x%08x\n", MCHP_PDMC_MR, val); in mchp_pdmc_trigger()
695 regmap_read(dd->regmap, MCHP_PDMC_CFGR, &val); in mchp_pdmc_trigger()
696 dev_dbg(dd->dev, "CFGR (0x%02x): 0x%08x\n", MCHP_PDMC_CFGR, val); in mchp_pdmc_trigger()
697 regmap_read(dd->regmap, MCHP_PDMC_IMR, &val); in mchp_pdmc_trigger()
698 dev_dbg(dd->dev, "IMR (0x%02x): 0x%08x\n", MCHP_PDMC_IMR, val); in mchp_pdmc_trigger()
719 if (WARN_ON(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].chmap_kctl)) in mchp_pdmc_add_chmap_ctls()
720 return -EBUSY; in mchp_pdmc_add_chmap_ctls()
723 return -ENOMEM; in mchp_pdmc_add_chmap_ctls()
724 info->pcm = pcm; in mchp_pdmc_add_chmap_ctls()
725 info->dd = dd; in mchp_pdmc_add_chmap_ctls()
726 info->chmap = mchp_pdmc_std_chmaps; in mchp_pdmc_add_chmap_ctls()
728 knew.device = pcm->device; in mchp_pdmc_add_chmap_ctls()
729 knew.count = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count; in mchp_pdmc_add_chmap_ctls()
730 info->kctl = snd_ctl_new1(&knew, info); in mchp_pdmc_add_chmap_ctls()
731 if (!info->kctl) { in mchp_pdmc_add_chmap_ctls()
733 return -ENOMEM; in mchp_pdmc_add_chmap_ctls()
735 info->kctl->private_free = mchp_pdmc_chmap_ctl_private_free; in mchp_pdmc_add_chmap_ctls()
736 err = snd_ctl_add(pcm->card, info->kctl); in mchp_pdmc_add_chmap_ctls()
739 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].chmap_kctl = info->kctl; in mchp_pdmc_add_chmap_ctls()
749 ret = mchp_pdmc_add_chmap_ctls(rtd->pcm, dd); in mchp_pdmc_pcm_new()
751 dev_err(dd->dev, "failed to add channel map controls: %d\n", ret); in mchp_pdmc_pcm_new()
785 regmap_read(dd->regmap, MCHP_PDMC_ISR, &isr); in mchp_pdmc_interrupt()
786 regmap_read(dd->regmap, MCHP_PDMC_IMR, &msr); in mchp_pdmc_interrupt()
789 dev_dbg(dd->dev, "ISR (0x%02x): 0x%08x, IMR (0x%02x): 0x%08x, pending: 0x%08x\n", in mchp_pdmc_interrupt()
795 dev_warn(dd->dev, "underrun detected\n"); in mchp_pdmc_interrupt()
796 regmap_write(dd->regmap, MCHP_PDMC_IDR, MCHP_PDMC_IR_RXUDR); in mchp_pdmc_interrupt()
800 dev_warn(dd->dev, "overrun detected\n"); in mchp_pdmc_interrupt()
801 regmap_write(dd->regmap, MCHP_PDMC_IDR, MCHP_PDMC_IR_RXOVR); in mchp_pdmc_interrupt()
874 struct device_node *np = dd->dev->of_node; in mchp_pdmc_dt_init()
880 dev_err(dd->dev, "device node not found\n"); in mchp_pdmc_dt_init()
881 return -EINVAL; in mchp_pdmc_dt_init()
884 dd->mic_no = of_property_count_u32_elems(np, "microchip,mic-pos"); in mchp_pdmc_dt_init()
885 if (dd->mic_no < 0) { in mchp_pdmc_dt_init()
886 dev_err(dd->dev, "failed to get microchip,mic-pos: %d", in mchp_pdmc_dt_init()
887 dd->mic_no); in mchp_pdmc_dt_init()
888 return dd->mic_no; in mchp_pdmc_dt_init()
890 if (!dd->mic_no || dd->mic_no % 2 || in mchp_pdmc_dt_init()
891 dd->mic_no / 2 > MCHP_PDMC_MAX_CHANNELS) { in mchp_pdmc_dt_init()
892 dev_err(dd->dev, "invalid array length for microchip,mic-pos: %d", in mchp_pdmc_dt_init()
893 dd->mic_no); in mchp_pdmc_dt_init()
894 return -EINVAL; in mchp_pdmc_dt_init()
897 dd->mic_no /= 2; in mchp_pdmc_dt_init()
899 dev_info(dd->dev, "%d PDM microphones declared\n", dd->mic_no); in mchp_pdmc_dt_init()
903 * microchip,mic-pos to be the same with the channel mapping; in mchp_pdmc_dt_init()
906 for (i = 0; i < dd->mic_no; i++) { in mchp_pdmc_dt_init()
910 ret = of_property_read_u32_index(np, "microchip,mic-pos", i * 2, in mchp_pdmc_dt_init()
913 dev_err(dd->dev, in mchp_pdmc_dt_init()
914 "failed to get value no %d value from microchip,mic-pos: %d", in mchp_pdmc_dt_init()
919 dev_err(dd->dev, in mchp_pdmc_dt_init()
920 "invalid DS index in microchip,mic-pos array: %d", in mchp_pdmc_dt_init()
922 return -EINVAL; in mchp_pdmc_dt_init()
925 ret = of_property_read_u32_index(np, "microchip,mic-pos", i * 2 + 1, in mchp_pdmc_dt_init()
928 dev_err(dd->dev, in mchp_pdmc_dt_init()
929 "failed to get value no %d value from microchip,mic-pos: %d", in mchp_pdmc_dt_init()
936 dev_err(dd->dev, in mchp_pdmc_dt_init()
937 "invalid edge in microchip,mic-pos array: %d", edge); in mchp_pdmc_dt_init()
938 return -EINVAL; in mchp_pdmc_dt_init()
941 dev_err(dd->dev, in mchp_pdmc_dt_init()
942 "duplicated mic (DS %d, edge %d) in microchip,mic-pos array", in mchp_pdmc_dt_init()
944 return -EINVAL; in mchp_pdmc_dt_init()
947 dd->channel_mic_map[i].ds_pos = ds; in mchp_pdmc_dt_init()
948 dd->channel_mic_map[i].clk_edge = edge; in mchp_pdmc_dt_init()
951 dd->startup_delay_us = 150000; in mchp_pdmc_dt_init()
952 of_property_read_u32(np, "microchip,startup-delay-us", &dd->startup_delay_us); in mchp_pdmc_dt_init()
962 struct snd_pcm_runtime *runtime = substream->runtime; in mchp_pdmc_process()
963 u8 *dma_ptr = runtime->dma_area + hwoff + in mchp_pdmc_process()
964 channel * (runtime->dma_bytes / runtime->channels); in mchp_pdmc_process()
983 regcache_cache_only(dd->regmap, true); in mchp_pdmc_runtime_suspend()
985 clk_disable_unprepare(dd->gclk); in mchp_pdmc_runtime_suspend()
986 clk_disable_unprepare(dd->pclk); in mchp_pdmc_runtime_suspend()
996 ret = clk_prepare_enable(dd->pclk); in mchp_pdmc_runtime_resume()
998 dev_err(dd->dev, in mchp_pdmc_runtime_resume()
1002 ret = clk_prepare_enable(dd->gclk); in mchp_pdmc_runtime_resume()
1004 dev_err(dd->dev, in mchp_pdmc_runtime_resume()
1009 regcache_cache_only(dd->regmap, false); in mchp_pdmc_runtime_resume()
1010 regcache_mark_dirty(dd->regmap); in mchp_pdmc_runtime_resume()
1011 ret = regcache_sync(dd->regmap); in mchp_pdmc_runtime_resume()
1013 regcache_cache_only(dd->regmap, true); in mchp_pdmc_runtime_resume()
1014 clk_disable_unprepare(dd->gclk); in mchp_pdmc_runtime_resume()
1016 clk_disable_unprepare(dd->pclk); in mchp_pdmc_runtime_resume()
1024 struct device *dev = &pdev->dev; in mchp_pdmc_probe()
1034 return -ENOMEM; in mchp_pdmc_probe()
1036 dd->dev = &pdev->dev; in mchp_pdmc_probe()
1045 dd->pclk = devm_clk_get(dev, "pclk"); in mchp_pdmc_probe()
1046 if (IS_ERR(dd->pclk)) { in mchp_pdmc_probe()
1047 ret = PTR_ERR(dd->pclk); in mchp_pdmc_probe()
1052 dd->gclk = devm_clk_get(dev, "gclk"); in mchp_pdmc_probe()
1053 if (IS_ERR(dd->gclk)) { in mchp_pdmc_probe()
1054 ret = PTR_ERR(dd->gclk); in mchp_pdmc_probe()
1066 dd->regmap = devm_regmap_init_mmio(dev, io_base, in mchp_pdmc_probe()
1068 if (IS_ERR(dd->regmap)) { in mchp_pdmc_probe()
1069 ret = PTR_ERR(dd->regmap); in mchp_pdmc_probe()
1075 dev_name(&pdev->dev), dd); in mchp_pdmc_probe()
1085 dd->audio_filter_en = true; in mchp_pdmc_probe()
1086 dd->sinc_order = 3; in mchp_pdmc_probe()
1088 dd->addr.addr = (dma_addr_t)res->start + MCHP_PDMC_RHR; in mchp_pdmc_probe()
1091 pm_runtime_enable(dd->dev); in mchp_pdmc_probe()
1092 if (!pm_runtime_enabled(dd->dev)) { in mchp_pdmc_probe()
1093 ret = mchp_pdmc_runtime_resume(dd->dev); in mchp_pdmc_probe()
1113 regmap_read(dd->regmap, MCHP_PDMC_VER, &version); in mchp_pdmc_probe()
1114 dev_info(dd->dev, "hw version: %#lx\n", in mchp_pdmc_probe()
1120 if (!pm_runtime_status_suspended(dd->dev)) in mchp_pdmc_probe()
1121 mchp_pdmc_runtime_suspend(dd->dev); in mchp_pdmc_probe()
1122 pm_runtime_disable(dd->dev); in mchp_pdmc_probe()
1131 if (!pm_runtime_status_suspended(dd->dev)) in mchp_pdmc_remove()
1132 mchp_pdmc_runtime_suspend(dd->dev); in mchp_pdmc_remove()
1134 pm_runtime_disable(dd->dev); in mchp_pdmc_remove()
1139 .compatible = "microchip,sama7g5-pdmc",
1154 .name = "mchp-pdmc",