Lines Matching +full:sama7g5 +full:- +full:pdmc

1 // SPDX-License-Identifier: GPL-2.0
3 // Driver for Microchip Pulse Density Microphone Controller (PDMC) interfaces
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) ----
94 * ---- DMA chunk size allowed ----
149 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; in mchp_pdmc_sinc_order_get()
152 item = snd_soc_enum_val_to_item(e, dd->sinc_order); in mchp_pdmc_sinc_order_get()
153 uvalue->value.enumerated.item[0] = item; in mchp_pdmc_sinc_order_get()
163 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; in mchp_pdmc_sinc_order_put()
164 unsigned int *item = uvalue->value.enumerated.item; in mchp_pdmc_sinc_order_put()
167 if (item[0] >= e->items) in mchp_pdmc_sinc_order_put()
168 return -EINVAL; in mchp_pdmc_sinc_order_put()
170 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l; in mchp_pdmc_sinc_order_put()
172 if (atomic_read(&dd->busy_stream)) in mchp_pdmc_sinc_order_put()
173 return -EBUSY; in mchp_pdmc_sinc_order_put()
175 if (val == dd->sinc_order) in mchp_pdmc_sinc_order_put()
178 dd->sinc_order = val; in mchp_pdmc_sinc_order_put()
189 uvalue->value.integer.value[0] = !!dd->audio_filter_en; in mchp_pdmc_af_get()
199 bool af = uvalue->value.integer.value[0] ? true : false; in mchp_pdmc_af_put()
201 if (atomic_read(&dd->busy_stream)) in mchp_pdmc_af_put()
202 return -EBUSY; in mchp_pdmc_af_put()
204 if (dd->audio_filter_en == af) in mchp_pdmc_af_put()
207 dd->audio_filter_en = af; in mchp_pdmc_af_put()
216 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; in mchp_pdmc_chmap_ctl_info()
217 uinfo->count = info->dd->mic_no; in mchp_pdmc_chmap_ctl_info()
218 uinfo->value.integer.min = 0; in mchp_pdmc_chmap_ctl_info()
219 uinfo->value.integer.max = SNDRV_CHMAP_RR; /* maxmimum 4 channels */ in mchp_pdmc_chmap_ctl_info()
228 for (s = info->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; s; s = s->next) in mchp_pdmc_chmap_substream()
229 if (s->number == idx) in mchp_pdmc_chmap_substream()
239 for (map = ch_info->chmap; map->channels; map++) { in mchp_pdmc_chmap_get()
240 if (map->channels == substream->runtime->channels) in mchp_pdmc_chmap_get()
250 struct mchp_pdmc *dd = info->dd; in mchp_pdmc_chmap_ctl_get()
251 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); in mchp_pdmc_chmap_ctl_get()
257 if (!info->chmap) in mchp_pdmc_chmap_ctl_get()
258 return -EINVAL; in mchp_pdmc_chmap_ctl_get()
261 return -ENODEV; in mchp_pdmc_chmap_ctl_get()
262 memset(ucontrol->value.integer.value, 0, sizeof(long) * info->dd->mic_no); in mchp_pdmc_chmap_ctl_get()
263 if (!substream->runtime) in mchp_pdmc_chmap_ctl_get()
268 return -EINVAL; in mchp_pdmc_chmap_ctl_get()
270 for (i = 0; i < map->channels; i++) { in mchp_pdmc_chmap_ctl_get()
271 int map_idx = map->channels == 1 ? map->map[i] - SNDRV_CHMAP_MONO : in mchp_pdmc_chmap_ctl_get()
272 map->map[i] - SNDRV_CHMAP_FL; in mchp_pdmc_chmap_ctl_get()
275 if (dd->channel_mic_map[map_idx].ds_pos) in mchp_pdmc_chmap_ctl_get()
277 if (dd->channel_mic_map[map_idx].clk_edge) in mchp_pdmc_chmap_ctl_get()
280 ucontrol->value.integer.value[i] = map->map[i]; in mchp_pdmc_chmap_ctl_get()
283 regmap_write(dd->regmap, MCHP_PDMC_CFGR, cfgr_val); in mchp_pdmc_chmap_ctl_get()
292 struct mchp_pdmc *dd = info->dd; in mchp_pdmc_chmap_ctl_put()
293 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); in mchp_pdmc_chmap_ctl_put()
299 if (!info->chmap) in mchp_pdmc_chmap_ctl_put()
300 return -EINVAL; in mchp_pdmc_chmap_ctl_put()
303 return -ENODEV; in mchp_pdmc_chmap_ctl_put()
305 if (!substream->runtime) in mchp_pdmc_chmap_ctl_put()
310 return -EINVAL; in mchp_pdmc_chmap_ctl_put()
312 for (i = 0; i < map->channels; i++) { in mchp_pdmc_chmap_ctl_put()
315 map->map[i] = ucontrol->value.integer.value[i]; in mchp_pdmc_chmap_ctl_put()
316 map_idx = map->channels == 1 ? map->map[i] - SNDRV_CHMAP_MONO : in mchp_pdmc_chmap_ctl_put()
317 map->map[i] - SNDRV_CHMAP_FL; in mchp_pdmc_chmap_ctl_put()
320 if (dd->channel_mic_map[map_idx].ds_pos) in mchp_pdmc_chmap_ctl_put()
322 if (dd->channel_mic_map[map_idx].clk_edge) in mchp_pdmc_chmap_ctl_put()
326 regmap_write(dd->regmap, MCHP_PDMC_CFGR, cfgr_val); in mchp_pdmc_chmap_ctl_put()
335 info->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].chmap_kctl = NULL; in mchp_pdmc_chmap_ctl_private_free()
347 if (!info->chmap) in mchp_pdmc_chmap_ctl_tlv()
348 return -EINVAL; in mchp_pdmc_chmap_ctl_tlv()
350 return -ENOMEM; in mchp_pdmc_chmap_ctl_tlv()
352 return -EFAULT; in mchp_pdmc_chmap_ctl_tlv()
353 size -= 8; in mchp_pdmc_chmap_ctl_tlv()
355 for (map = info->chmap; map->channels; map++) { in mchp_pdmc_chmap_ctl_tlv()
356 int chs_bytes = map->channels * 4; in mchp_pdmc_chmap_ctl_tlv()
359 return -ENOMEM; in mchp_pdmc_chmap_ctl_tlv()
362 return -EFAULT; in mchp_pdmc_chmap_ctl_tlv()
364 size -= 8; in mchp_pdmc_chmap_ctl_tlv()
367 return -ENOMEM; in mchp_pdmc_chmap_ctl_tlv()
368 size -= chs_bytes; in mchp_pdmc_chmap_ctl_tlv()
370 for (c = 0; c < map->channels; c++) { in mchp_pdmc_chmap_ctl_tlv()
371 if (put_user(map->map[c], dst)) in mchp_pdmc_chmap_ctl_tlv()
372 return -EFAULT; in mchp_pdmc_chmap_ctl_tlv()
377 return -EFAULT; in mchp_pdmc_chmap_ctl_tlv()
394 .name = "mchp-pdmc",
428 regmap_write(dd->regmap, MCHP_PDMC_CR, MCHP_PDMC_CR_SWRST); in mchp_pdmc_startup()
430 snd_pcm_hw_constraint_list(substream->runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, in mchp_pdmc_startup()
431 &mchp_pdmc_chan_constr[dd->mic_no - 1]); in mchp_pdmc_startup()
440 snd_soc_dai_init_dma_data(dai, NULL, &dd->addr); in mchp_pdmc_dai_probe()
453 return -EINVAL; in mchp_pdmc_set_fmt()
457 return -EINVAL; in mchp_pdmc_set_fmt()
525 struct snd_soc_component *comp = dai->component; in mchp_pdmc_hw_params()
540 dev_dbg(comp->dev, "%s() rate=%u format=%#x width=%u channels=%u period_bytes=%d\n", in mchp_pdmc_hw_params()
544 if (channels > dd->mic_no) { in mchp_pdmc_hw_params()
545 dev_err(comp->dev, "more channels %u than microphones %d\n", in mchp_pdmc_hw_params()
546 channels, dd->mic_no); in mchp_pdmc_hw_params()
547 return -EINVAL; in mchp_pdmc_hw_params()
550 dd->pdmcen = 0; in mchp_pdmc_hw_params()
552 dd->pdmcen |= MCHP_PDMC_MR_PDMCEN(i); in mchp_pdmc_hw_params()
553 if (dd->channel_mic_map[i].ds_pos) in mchp_pdmc_hw_params()
555 if (dd->channel_mic_map[i].clk_edge) in mchp_pdmc_hw_params()
563 atomic_set(&dd->busy_stream, 1); in mchp_pdmc_hw_params()
564 for (osr_start = dd->audio_filter_en ? 64 : 8; in mchp_pdmc_hw_params()
569 round_rate = clk_round_rate(dd->gclk, in mchp_pdmc_hw_params()
573 diff_rate = abs((fs * 16 * osr_start) - round_rate); in mchp_pdmc_hw_params()
581 dev_err(comp->dev, "invalid sampling rate: %u\n", fs); in mchp_pdmc_hw_params()
582 return -EINVAL; in mchp_pdmc_hw_params()
586 clk_disable_unprepare(dd->gclk); in mchp_pdmc_hw_params()
589 ret = clk_set_rate(dd->gclk, gclk_rate); in mchp_pdmc_hw_params()
590 clk_prepare_enable(dd->gclk); in mchp_pdmc_hw_params()
592 dev_err(comp->dev, "unable to set rate %lu to GCLK: %d\n", in mchp_pdmc_hw_params()
597 mr_val |= mchp_pdmc_mr_set_osr(dd->audio_filter_en, osr); in mchp_pdmc_hw_params()
599 mr_val |= FIELD_PREP(MCHP_PDMC_MR_SINCORDER_MASK, dd->sinc_order); in mchp_pdmc_hw_params()
602 dd->addr.maxburst = maxburst; in mchp_pdmc_hw_params()
603 mr_val |= FIELD_PREP(MCHP_PDMC_MR_CHUNK_MASK, dd->addr.maxburst); in mchp_pdmc_hw_params()
604 dev_dbg(comp->dev, "maxburst set to %d\n", dd->addr.maxburst); in mchp_pdmc_hw_params()
622 * PDMC doesn't wait for microphones' startup time thus the acquisition in mchp_pdmc_noise_filter_workaround()
631 usleep_range(dd->startup_delay_us, dd->startup_delay_us + 5); in mchp_pdmc_noise_filter_workaround()
633 while (steps--) in mchp_pdmc_noise_filter_workaround()
634 regmap_read(dd->regmap, MCHP_PDMC_RHR, &tmp); in mchp_pdmc_noise_filter_workaround()
637 regmap_read(dd->regmap, MCHP_PDMC_ISR, &tmp); in mchp_pdmc_noise_filter_workaround()
644 struct snd_soc_component *cpu = dai->component; in mchp_pdmc_trigger()
655 dd->pdmcen); in mchp_pdmc_trigger()
660 regmap_write(dd->regmap, MCHP_PDMC_IER, dd->suspend_irq | in mchp_pdmc_trigger()
662 dd->suspend_irq = 0; in mchp_pdmc_trigger()
665 regmap_read(dd->regmap, MCHP_PDMC_IMR, &dd->suspend_irq); in mchp_pdmc_trigger()
669 regmap_write(dd->regmap, MCHP_PDMC_IDR, dd->suspend_irq | in mchp_pdmc_trigger()
677 return -EINVAL; in mchp_pdmc_trigger()
681 regmap_read(dd->regmap, MCHP_PDMC_MR, &val); in mchp_pdmc_trigger()
682 dev_dbg(dd->dev, "MR (0x%02x): 0x%08x\n", MCHP_PDMC_MR, val); in mchp_pdmc_trigger()
683 regmap_read(dd->regmap, MCHP_PDMC_CFGR, &val); in mchp_pdmc_trigger()
684 dev_dbg(dd->dev, "CFGR (0x%02x): 0x%08x\n", MCHP_PDMC_CFGR, val); in mchp_pdmc_trigger()
685 regmap_read(dd->regmap, MCHP_PDMC_IMR, &val); in mchp_pdmc_trigger()
686 dev_dbg(dd->dev, "IMR (0x%02x): 0x%08x\n", MCHP_PDMC_IMR, val); in mchp_pdmc_trigger()
707 if (WARN_ON(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].chmap_kctl)) in mchp_pdmc_add_chmap_ctls()
708 return -EBUSY; in mchp_pdmc_add_chmap_ctls()
711 return -ENOMEM; in mchp_pdmc_add_chmap_ctls()
712 info->pcm = pcm; in mchp_pdmc_add_chmap_ctls()
713 info->dd = dd; in mchp_pdmc_add_chmap_ctls()
714 info->chmap = mchp_pdmc_std_chmaps; in mchp_pdmc_add_chmap_ctls()
716 knew.device = pcm->device; in mchp_pdmc_add_chmap_ctls()
717 knew.count = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count; in mchp_pdmc_add_chmap_ctls()
718 info->kctl = snd_ctl_new1(&knew, info); in mchp_pdmc_add_chmap_ctls()
719 if (!info->kctl) { in mchp_pdmc_add_chmap_ctls()
721 return -ENOMEM; in mchp_pdmc_add_chmap_ctls()
723 info->kctl->private_free = mchp_pdmc_chmap_ctl_private_free; in mchp_pdmc_add_chmap_ctls()
724 err = snd_ctl_add(pcm->card, info->kctl); in mchp_pdmc_add_chmap_ctls()
727 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].chmap_kctl = info->kctl; in mchp_pdmc_add_chmap_ctls()
737 ret = mchp_pdmc_add_chmap_ctls(rtd->pcm, dd); in mchp_pdmc_pcm_new()
739 dev_err(dd->dev, "failed to add channel map controls: %d\n", ret); in mchp_pdmc_pcm_new()
754 .name = "mchp-pdmc",
767 /* PDMC interrupt handler */
774 regmap_read(dd->regmap, MCHP_PDMC_ISR, &isr); in mchp_pdmc_interrupt()
775 regmap_read(dd->regmap, MCHP_PDMC_IMR, &msr); in mchp_pdmc_interrupt()
778 dev_dbg(dd->dev, "ISR (0x%02x): 0x%08x, IMR (0x%02x): 0x%08x, pending: 0x%08x\n", in mchp_pdmc_interrupt()
784 dev_warn(dd->dev, "underrun detected\n"); in mchp_pdmc_interrupt()
785 regmap_write(dd->regmap, MCHP_PDMC_IDR, MCHP_PDMC_IR_RXUDR); in mchp_pdmc_interrupt()
789 dev_warn(dd->dev, "overrun detected\n"); in mchp_pdmc_interrupt()
790 regmap_write(dd->regmap, MCHP_PDMC_IDR, MCHP_PDMC_IR_RXOVR); in mchp_pdmc_interrupt()
863 struct device_node *np = dd->dev->of_node; in mchp_pdmc_dt_init()
869 dev_err(dd->dev, "device node not found\n"); in mchp_pdmc_dt_init()
870 return -EINVAL; in mchp_pdmc_dt_init()
873 dd->mic_no = of_property_count_u32_elems(np, "microchip,mic-pos"); in mchp_pdmc_dt_init()
874 if (dd->mic_no < 0) { in mchp_pdmc_dt_init()
875 dev_err(dd->dev, "failed to get microchip,mic-pos: %d", in mchp_pdmc_dt_init()
876 dd->mic_no); in mchp_pdmc_dt_init()
877 return dd->mic_no; in mchp_pdmc_dt_init()
879 if (!dd->mic_no || dd->mic_no % 2 || in mchp_pdmc_dt_init()
880 dd->mic_no / 2 > MCHP_PDMC_MAX_CHANNELS) { in mchp_pdmc_dt_init()
881 dev_err(dd->dev, "invalid array length for microchip,mic-pos: %d", in mchp_pdmc_dt_init()
882 dd->mic_no); in mchp_pdmc_dt_init()
883 return -EINVAL; in mchp_pdmc_dt_init()
886 dd->mic_no /= 2; in mchp_pdmc_dt_init()
888 dev_info(dd->dev, "%d PDM microphones declared\n", dd->mic_no); in mchp_pdmc_dt_init()
892 * microchip,mic-pos to be the same with the channel mapping; in mchp_pdmc_dt_init()
895 for (i = 0; i < dd->mic_no; i++) { in mchp_pdmc_dt_init()
899 ret = of_property_read_u32_index(np, "microchip,mic-pos", i * 2, in mchp_pdmc_dt_init()
902 dev_err(dd->dev, in mchp_pdmc_dt_init()
903 "failed to get value no %d value from microchip,mic-pos: %d", in mchp_pdmc_dt_init()
908 dev_err(dd->dev, in mchp_pdmc_dt_init()
909 "invalid DS index in microchip,mic-pos array: %d", in mchp_pdmc_dt_init()
911 return -EINVAL; in mchp_pdmc_dt_init()
914 ret = of_property_read_u32_index(np, "microchip,mic-pos", i * 2 + 1, in mchp_pdmc_dt_init()
917 dev_err(dd->dev, in mchp_pdmc_dt_init()
918 "failed to get value no %d value from microchip,mic-pos: %d", in mchp_pdmc_dt_init()
925 dev_err(dd->dev, in mchp_pdmc_dt_init()
926 "invalid edge in microchip,mic-pos array: %d", edge); in mchp_pdmc_dt_init()
927 return -EINVAL; in mchp_pdmc_dt_init()
930 dev_err(dd->dev, in mchp_pdmc_dt_init()
931 "duplicated mic (DS %d, edge %d) in microchip,mic-pos array", in mchp_pdmc_dt_init()
933 return -EINVAL; in mchp_pdmc_dt_init()
936 dd->channel_mic_map[i].ds_pos = ds; in mchp_pdmc_dt_init()
937 dd->channel_mic_map[i].clk_edge = edge; in mchp_pdmc_dt_init()
940 dd->startup_delay_us = 150000; in mchp_pdmc_dt_init()
941 of_property_read_u32(np, "microchip,startup-delay-us", &dd->startup_delay_us); in mchp_pdmc_dt_init()
951 struct snd_pcm_runtime *runtime = substream->runtime; in mchp_pdmc_process()
952 u8 *dma_ptr = runtime->dma_area + hwoff + in mchp_pdmc_process()
953 channel * (runtime->dma_bytes / runtime->channels); in mchp_pdmc_process()
972 regcache_cache_only(dd->regmap, true); in mchp_pdmc_runtime_suspend()
974 clk_disable_unprepare(dd->gclk); in mchp_pdmc_runtime_suspend()
975 clk_disable_unprepare(dd->pclk); in mchp_pdmc_runtime_suspend()
985 ret = clk_prepare_enable(dd->pclk); in mchp_pdmc_runtime_resume()
987 dev_err(dd->dev, in mchp_pdmc_runtime_resume()
991 ret = clk_prepare_enable(dd->gclk); in mchp_pdmc_runtime_resume()
993 dev_err(dd->dev, in mchp_pdmc_runtime_resume()
998 regcache_cache_only(dd->regmap, false); in mchp_pdmc_runtime_resume()
999 regcache_mark_dirty(dd->regmap); in mchp_pdmc_runtime_resume()
1000 ret = regcache_sync(dd->regmap); in mchp_pdmc_runtime_resume()
1002 regcache_cache_only(dd->regmap, true); in mchp_pdmc_runtime_resume()
1003 clk_disable_unprepare(dd->gclk); in mchp_pdmc_runtime_resume()
1005 clk_disable_unprepare(dd->pclk); in mchp_pdmc_runtime_resume()
1013 struct device *dev = &pdev->dev; in mchp_pdmc_probe()
1023 return -ENOMEM; in mchp_pdmc_probe()
1025 dd->dev = &pdev->dev; in mchp_pdmc_probe()
1034 dd->pclk = devm_clk_get(dev, "pclk"); in mchp_pdmc_probe()
1035 if (IS_ERR(dd->pclk)) { in mchp_pdmc_probe()
1036 ret = PTR_ERR(dd->pclk); in mchp_pdmc_probe()
1041 dd->gclk = devm_clk_get(dev, "gclk"); in mchp_pdmc_probe()
1042 if (IS_ERR(dd->gclk)) { in mchp_pdmc_probe()
1043 ret = PTR_ERR(dd->gclk); in mchp_pdmc_probe()
1055 dd->regmap = devm_regmap_init_mmio(dev, io_base, in mchp_pdmc_probe()
1057 if (IS_ERR(dd->regmap)) { in mchp_pdmc_probe()
1058 ret = PTR_ERR(dd->regmap); in mchp_pdmc_probe()
1064 dev_name(&pdev->dev), dd); in mchp_pdmc_probe()
1074 dd->audio_filter_en = true; in mchp_pdmc_probe()
1075 dd->sinc_order = 3; in mchp_pdmc_probe()
1077 dd->addr.addr = (dma_addr_t)res->start + MCHP_PDMC_RHR; in mchp_pdmc_probe()
1080 pm_runtime_enable(dd->dev); in mchp_pdmc_probe()
1081 if (!pm_runtime_enabled(dd->dev)) { in mchp_pdmc_probe()
1082 ret = mchp_pdmc_runtime_resume(dd->dev); in mchp_pdmc_probe()
1102 regmap_read(dd->regmap, MCHP_PDMC_VER, &version); in mchp_pdmc_probe()
1103 dev_info(dd->dev, "hw version: %#lx\n", in mchp_pdmc_probe()
1109 if (!pm_runtime_status_suspended(dd->dev)) in mchp_pdmc_probe()
1110 mchp_pdmc_runtime_suspend(dd->dev); in mchp_pdmc_probe()
1111 pm_runtime_disable(dd->dev); in mchp_pdmc_probe()
1120 atomic_set(&dd->busy_stream, 0); in mchp_pdmc_remove()
1122 if (!pm_runtime_status_suspended(dd->dev)) in mchp_pdmc_remove()
1123 mchp_pdmc_runtime_suspend(dd->dev); in mchp_pdmc_remove()
1125 pm_runtime_disable(dd->dev); in mchp_pdmc_remove()
1130 .compatible = "microchip,sama7g5-pdmc",
1145 .name = "mchp-pdmc",
1154 MODULE_DESCRIPTION("Microchip PDMC driver under ALSA SoC architecture");