Lines Matching +full:mic +full:- +full:pos
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>
22 * ---- PDMC Register map ----
35 * ---- Control Register (Write-only) ----
40 * ---- Mode Register (Read/Write) ----
68 * ---- Configuration Register (Read/Write) ----
77 * ---- Interrupt Enable/Disable/Mask/Status Registers ----
87 * ---- Version Register (Read-only) ----
140 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; in mchp_pdmc_sinc_order_get()
143 item = snd_soc_enum_val_to_item(e, dd->sinc_order); in mchp_pdmc_sinc_order_get()
144 uvalue->value.enumerated.item[0] = item; in mchp_pdmc_sinc_order_get()
154 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; in mchp_pdmc_sinc_order_put()
155 unsigned int *item = uvalue->value.enumerated.item; in mchp_pdmc_sinc_order_put()
158 if (item[0] >= e->items) in mchp_pdmc_sinc_order_put()
159 return -EINVAL; in mchp_pdmc_sinc_order_put()
161 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l; in mchp_pdmc_sinc_order_put()
162 if (val == dd->sinc_order) in mchp_pdmc_sinc_order_put()
165 dd->sinc_order = val; in mchp_pdmc_sinc_order_put()
176 uvalue->value.integer.value[0] = !!dd->audio_filter_en; in mchp_pdmc_af_get()
186 bool af = uvalue->value.integer.value[0] ? true : false; in mchp_pdmc_af_put()
188 if (dd->audio_filter_en == af) in mchp_pdmc_af_put()
191 dd->audio_filter_en = af; in mchp_pdmc_af_put()
200 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; in mchp_pdmc_chmap_ctl_info()
201 uinfo->count = info->dd->mic_no; in mchp_pdmc_chmap_ctl_info()
202 uinfo->value.integer.min = 0; in mchp_pdmc_chmap_ctl_info()
203 uinfo->value.integer.max = SNDRV_CHMAP_RR; /* maxmimum 4 channels */ in mchp_pdmc_chmap_ctl_info()
212 for (s = info->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; s; s = s->next) in mchp_pdmc_chmap_substream()
213 if (s->number == idx) in mchp_pdmc_chmap_substream()
223 for (map = ch_info->chmap; map->channels; map++) { in mchp_pdmc_chmap_get()
224 if (map->channels == substream->runtime->channels) in mchp_pdmc_chmap_get()
234 struct mchp_pdmc *dd = info->dd; in mchp_pdmc_chmap_ctl_get()
235 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); in mchp_pdmc_chmap_ctl_get()
241 if (!info->chmap) in mchp_pdmc_chmap_ctl_get()
242 return -EINVAL; in mchp_pdmc_chmap_ctl_get()
245 return -ENODEV; in mchp_pdmc_chmap_ctl_get()
246 memset(ucontrol->value.integer.value, 0, sizeof(long) * info->dd->mic_no); in mchp_pdmc_chmap_ctl_get()
247 if (!substream->runtime) in mchp_pdmc_chmap_ctl_get()
252 return -EINVAL; in mchp_pdmc_chmap_ctl_get()
254 for (i = 0; i < map->channels; i++) { in mchp_pdmc_chmap_ctl_get()
255 int map_idx = map->channels == 1 ? map->map[i] - SNDRV_CHMAP_MONO : in mchp_pdmc_chmap_ctl_get()
256 map->map[i] - SNDRV_CHMAP_FL; in mchp_pdmc_chmap_ctl_get()
259 if (dd->channel_mic_map[map_idx].ds_pos) in mchp_pdmc_chmap_ctl_get()
261 if (dd->channel_mic_map[map_idx].clk_edge) in mchp_pdmc_chmap_ctl_get()
264 ucontrol->value.integer.value[i] = map->map[i]; in mchp_pdmc_chmap_ctl_get()
267 regmap_write(dd->regmap, MCHP_PDMC_CFGR, cfgr_val); in mchp_pdmc_chmap_ctl_get()
276 struct mchp_pdmc *dd = info->dd; in mchp_pdmc_chmap_ctl_put()
277 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); in mchp_pdmc_chmap_ctl_put()
283 if (!info->chmap) in mchp_pdmc_chmap_ctl_put()
284 return -EINVAL; in mchp_pdmc_chmap_ctl_put()
287 return -ENODEV; in mchp_pdmc_chmap_ctl_put()
291 return -EINVAL; in mchp_pdmc_chmap_ctl_put()
293 for (i = 0; i < map->channels; i++) { in mchp_pdmc_chmap_ctl_put()
296 map->map[i] = ucontrol->value.integer.value[i]; in mchp_pdmc_chmap_ctl_put()
297 map_idx = map->channels == 1 ? map->map[i] - SNDRV_CHMAP_MONO : in mchp_pdmc_chmap_ctl_put()
298 map->map[i] - SNDRV_CHMAP_FL; in mchp_pdmc_chmap_ctl_put()
301 if (dd->channel_mic_map[map_idx].ds_pos) in mchp_pdmc_chmap_ctl_put()
303 if (dd->channel_mic_map[map_idx].clk_edge) in mchp_pdmc_chmap_ctl_put()
307 regmap_write(dd->regmap, MCHP_PDMC_CFGR, cfgr_val); in mchp_pdmc_chmap_ctl_put()
316 info->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].chmap_kctl = NULL; in mchp_pdmc_chmap_ctl_private_free()
328 if (!info->chmap) in mchp_pdmc_chmap_ctl_tlv()
329 return -EINVAL; in mchp_pdmc_chmap_ctl_tlv()
331 return -ENOMEM; in mchp_pdmc_chmap_ctl_tlv()
333 return -EFAULT; in mchp_pdmc_chmap_ctl_tlv()
334 size -= 8; in mchp_pdmc_chmap_ctl_tlv()
336 for (map = info->chmap; map->channels; map++) { in mchp_pdmc_chmap_ctl_tlv()
337 int chs_bytes = map->channels * 4; in mchp_pdmc_chmap_ctl_tlv()
340 return -ENOMEM; in mchp_pdmc_chmap_ctl_tlv()
343 return -EFAULT; in mchp_pdmc_chmap_ctl_tlv()
345 size -= 8; in mchp_pdmc_chmap_ctl_tlv()
348 return -ENOMEM; in mchp_pdmc_chmap_ctl_tlv()
349 size -= chs_bytes; in mchp_pdmc_chmap_ctl_tlv()
351 for (c = 0; c < map->channels; c++) { in mchp_pdmc_chmap_ctl_tlv()
352 if (put_user(map->map[c], dst)) in mchp_pdmc_chmap_ctl_tlv()
353 return -EFAULT; in mchp_pdmc_chmap_ctl_tlv()
358 return -EFAULT; in mchp_pdmc_chmap_ctl_tlv()
393 if (component->name_prefix) in mchp_pdmc_open()
394 snprintf(id.name, sizeof(id.name), "%s %s", component->name_prefix, in mchp_pdmc_open()
395 control->name); in mchp_pdmc_open()
397 strscpy(id.name, control->name, sizeof(id.name)); in mchp_pdmc_open()
400 id.iface = control->iface; in mchp_pdmc_open()
401 id.device = control->device; in mchp_pdmc_open()
402 id.subdevice = control->subdevice; in mchp_pdmc_open()
403 id.index = control->index; in mchp_pdmc_open()
404 kctl = snd_ctl_find_id(component->card->snd_card, &id); in mchp_pdmc_open()
406 dev_err(component->dev, "Failed to find %s\n", control->name); in mchp_pdmc_open()
409 err = snd_ctl_remove(component->card->snd_card, kctl); in mchp_pdmc_open()
411 dev_err(component->dev, "%d: Failed to remove %s\n", err, in mchp_pdmc_open()
412 control->name); in mchp_pdmc_open()
421 .name = "mchp-pdmc",
459 ret = clk_prepare_enable(dd->pclk); in mchp_pdmc_startup()
461 dev_err(dd->dev, "failed to enable the peripheral clock: %d\n", ret); in mchp_pdmc_startup()
465 regmap_write(dd->regmap, MCHP_PDMC_CR, MCHP_PDMC_CR_SWRST); in mchp_pdmc_startup()
467 snd_pcm_hw_constraint_list(substream->runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, in mchp_pdmc_startup()
468 &mchp_pdmc_chan_constr[dd->mic_no - 1]); in mchp_pdmc_startup()
478 clk_disable_unprepare(dd->pclk); in mchp_pdmc_shutdown()
485 snd_soc_dai_init_dma_data(dai, NULL, &dd->addr); in mchp_pdmc_dai_probe()
498 return -EINVAL; in mchp_pdmc_set_fmt()
502 return -EINVAL; in mchp_pdmc_set_fmt()
567 struct snd_soc_component *comp = dai->component; in mchp_pdmc_hw_params()
578 dev_dbg(comp->dev, "%s() rate=%u format=%#x width=%u channels=%u\n", in mchp_pdmc_hw_params()
582 if (channels > dd->mic_no) { in mchp_pdmc_hw_params()
583 dev_err(comp->dev, "more channels %u than microphones %d\n", in mchp_pdmc_hw_params()
584 channels, dd->mic_no); in mchp_pdmc_hw_params()
585 return -EINVAL; in mchp_pdmc_hw_params()
588 dd->pdmcen = 0; in mchp_pdmc_hw_params()
590 dd->pdmcen |= MCHP_PDMC_MR_PDMCEN(i); in mchp_pdmc_hw_params()
591 if (dd->channel_mic_map[i].ds_pos) in mchp_pdmc_hw_params()
593 if (dd->channel_mic_map[i].clk_edge) in mchp_pdmc_hw_params()
597 if (dd->gclk_enabled) { in mchp_pdmc_hw_params()
598 clk_disable_unprepare(dd->gclk); in mchp_pdmc_hw_params()
599 dd->gclk_enabled = 0; in mchp_pdmc_hw_params()
602 for (osr_start = dd->audio_filter_en ? 64 : 8; in mchp_pdmc_hw_params()
607 round_rate = clk_round_rate(dd->gclk, in mchp_pdmc_hw_params()
611 diff_rate = abs((fs * 16 * osr_start) - round_rate); in mchp_pdmc_hw_params()
619 dev_err(comp->dev, "invalid sampling rate: %u\n", fs); in mchp_pdmc_hw_params()
620 return -EINVAL; in mchp_pdmc_hw_params()
624 ret = clk_set_rate(dd->gclk, gclk_rate); in mchp_pdmc_hw_params()
626 dev_err(comp->dev, "unable to set rate %lu to GCLK: %d\n", in mchp_pdmc_hw_params()
631 mr_val |= mchp_pdmc_mr_set_osr(dd->audio_filter_en, osr); in mchp_pdmc_hw_params()
633 mr_val |= MCHP_PDMC_MR_SINCORDER(dd->sinc_order); in mchp_pdmc_hw_params()
635 dd->addr.maxburst = mchp_pdmc_period_to_maxburst(snd_pcm_lib_period_bytes(substream)); in mchp_pdmc_hw_params()
636 mr_val |= MCHP_PDMC_MR_CHUNK(dd->addr.maxburst); in mchp_pdmc_hw_params()
637 dev_dbg(comp->dev, "maxburst set to %d\n", dd->addr.maxburst); in mchp_pdmc_hw_params()
639 clk_prepare_enable(dd->gclk); in mchp_pdmc_hw_params()
640 dd->gclk_enabled = 1; in mchp_pdmc_hw_params()
658 if (dd->gclk_enabled) { in mchp_pdmc_hw_free()
659 clk_disable_unprepare(dd->gclk); in mchp_pdmc_hw_free()
660 dd->gclk_enabled = 0; in mchp_pdmc_hw_free()
670 struct snd_soc_component *cpu = dai->component; in mchp_pdmc_trigger()
680 regmap_write(dd->regmap, MCHP_PDMC_IER, in mchp_pdmc_trigger()
684 dd->pdmcen); in mchp_pdmc_trigger()
690 regmap_write(dd->regmap, MCHP_PDMC_IDR, in mchp_pdmc_trigger()
696 return -EINVAL; in mchp_pdmc_trigger()
700 regmap_read(dd->regmap, MCHP_PDMC_MR, &val); in mchp_pdmc_trigger()
701 dev_dbg(dd->dev, "MR (0x%02x): 0x%08x\n", MCHP_PDMC_MR, val); in mchp_pdmc_trigger()
702 regmap_read(dd->regmap, MCHP_PDMC_CFGR, &val); in mchp_pdmc_trigger()
703 dev_dbg(dd->dev, "CFGR (0x%02x): 0x%08x\n", MCHP_PDMC_CFGR, val); in mchp_pdmc_trigger()
704 regmap_read(dd->regmap, MCHP_PDMC_IMR, &val); in mchp_pdmc_trigger()
705 dev_dbg(dd->dev, "IMR (0x%02x): 0x%08x\n", MCHP_PDMC_IMR, val); in mchp_pdmc_trigger()
735 if (WARN_ON(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].chmap_kctl)) in mchp_pdmc_add_chmap_ctls()
736 return -EBUSY; in mchp_pdmc_add_chmap_ctls()
739 return -ENOMEM; in mchp_pdmc_add_chmap_ctls()
740 info->pcm = pcm; in mchp_pdmc_add_chmap_ctls()
741 info->dd = dd; in mchp_pdmc_add_chmap_ctls()
742 info->chmap = mchp_pdmc_std_chmaps; in mchp_pdmc_add_chmap_ctls()
744 knew.device = pcm->device; in mchp_pdmc_add_chmap_ctls()
745 knew.count = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count; in mchp_pdmc_add_chmap_ctls()
746 info->kctl = snd_ctl_new1(&knew, info); in mchp_pdmc_add_chmap_ctls()
747 if (!info->kctl) { in mchp_pdmc_add_chmap_ctls()
749 return -ENOMEM; in mchp_pdmc_add_chmap_ctls()
751 info->kctl->private_free = mchp_pdmc_chmap_ctl_private_free; in mchp_pdmc_add_chmap_ctls()
752 err = snd_ctl_add(pcm->card, info->kctl); in mchp_pdmc_add_chmap_ctls()
755 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].chmap_kctl = info->kctl; in mchp_pdmc_add_chmap_ctls()
765 ret = mchp_pdmc_add_chmap_ctls(rtd->pcm, dd); in mchp_pdmc_pcm_new()
767 dev_err(dd->dev, "failed to add channel map controls: %d\n", ret); in mchp_pdmc_pcm_new()
796 regmap_read(dd->regmap, MCHP_PDMC_ISR, &isr); in mchp_pdmc_interrupt()
797 regmap_read(dd->regmap, MCHP_PDMC_IMR, &msr); in mchp_pdmc_interrupt()
800 dev_dbg(dd->dev, "ISR (0x%02x): 0x%08x, IMR (0x%02x): 0x%08x, pending: 0x%08x\n", in mchp_pdmc_interrupt()
806 dev_warn(dd->dev, "underrun detected\n"); in mchp_pdmc_interrupt()
807 regmap_write(dd->regmap, MCHP_PDMC_IDR, MCHP_PDMC_IR_RXUDR); in mchp_pdmc_interrupt()
811 dev_warn(dd->dev, "overrun detected\n"); in mchp_pdmc_interrupt()
812 regmap_write(dd->regmap, MCHP_PDMC_IDR, MCHP_PDMC_IR_RXOVR); in mchp_pdmc_interrupt()
871 struct device_node *np = dd->dev->of_node; in mchp_pdmc_dt_init()
877 dev_err(dd->dev, "device node not found\n"); in mchp_pdmc_dt_init()
878 return -EINVAL; in mchp_pdmc_dt_init()
881 dd->mic_no = of_property_count_u32_elems(np, "microchip,mic-pos"); in mchp_pdmc_dt_init()
882 if (dd->mic_no < 0) { in mchp_pdmc_dt_init()
883 dev_err(dd->dev, "failed to get microchip,mic-pos: %d", in mchp_pdmc_dt_init()
884 dd->mic_no); in mchp_pdmc_dt_init()
885 return dd->mic_no; in mchp_pdmc_dt_init()
887 if (!dd->mic_no || dd->mic_no % 2 || in mchp_pdmc_dt_init()
888 dd->mic_no / 2 > MCHP_PDMC_MAX_CHANNELS) { in mchp_pdmc_dt_init()
889 dev_err(dd->dev, "invalid array length for microchip,mic-pos: %d", in mchp_pdmc_dt_init()
890 dd->mic_no); in mchp_pdmc_dt_init()
891 return -EINVAL; in mchp_pdmc_dt_init()
894 dd->mic_no /= 2; in mchp_pdmc_dt_init()
896 dev_info(dd->dev, "%d PDM microphones declared\n", dd->mic_no); in mchp_pdmc_dt_init()
900 * microchip,mic-pos to be the same with the channel mapping; in mchp_pdmc_dt_init()
903 for (i = 0; i < dd->mic_no; i++) { in mchp_pdmc_dt_init()
907 ret = of_property_read_u32_index(np, "microchip,mic-pos", i * 2, in mchp_pdmc_dt_init()
910 dev_err(dd->dev, in mchp_pdmc_dt_init()
911 "failed to get value no %d value from microchip,mic-pos: %d", in mchp_pdmc_dt_init()
916 dev_err(dd->dev, in mchp_pdmc_dt_init()
917 "invalid DS index in microchip,mic-pos array: %d", in mchp_pdmc_dt_init()
919 return -EINVAL; in mchp_pdmc_dt_init()
922 ret = of_property_read_u32_index(np, "microchip,mic-pos", i * 2 + 1, in mchp_pdmc_dt_init()
925 dev_err(dd->dev, in mchp_pdmc_dt_init()
926 "failed to get value no %d value from microchip,mic-pos: %d", in mchp_pdmc_dt_init()
933 dev_err(dd->dev, in mchp_pdmc_dt_init()
934 "invalid edge in microchip,mic-pos array: %d", edge); in mchp_pdmc_dt_init()
935 return -EINVAL; in mchp_pdmc_dt_init()
938 dev_err(dd->dev, in mchp_pdmc_dt_init()
939 "duplicated mic (DS %d, edge %d) in microchip,mic-pos array", in mchp_pdmc_dt_init()
941 return -EINVAL; in mchp_pdmc_dt_init()
944 dd->channel_mic_map[i].ds_pos = ds; in mchp_pdmc_dt_init()
945 dd->channel_mic_map[i].clk_edge = edge; in mchp_pdmc_dt_init()
956 struct snd_pcm_runtime *runtime = substream->runtime; in mchp_pdmc_process()
957 u8 *dma_ptr = runtime->dma_area + hwoff + in mchp_pdmc_process()
958 channel * (runtime->dma_bytes / runtime->channels); in mchp_pdmc_process()
975 struct device *dev = &pdev->dev; in mchp_pdmc_probe()
985 return -ENOMEM; in mchp_pdmc_probe()
987 dd->dev = &pdev->dev; in mchp_pdmc_probe()
996 dd->pclk = devm_clk_get(dev, "pclk"); in mchp_pdmc_probe()
997 if (IS_ERR(dd->pclk)) { in mchp_pdmc_probe()
998 ret = PTR_ERR(dd->pclk); in mchp_pdmc_probe()
1003 dd->gclk = devm_clk_get(dev, "gclk"); in mchp_pdmc_probe()
1004 if (IS_ERR(dd->gclk)) { in mchp_pdmc_probe()
1005 ret = PTR_ERR(dd->gclk); in mchp_pdmc_probe()
1017 dd->regmap = devm_regmap_init_mmio(dev, io_base, in mchp_pdmc_probe()
1019 if (IS_ERR(dd->regmap)) { in mchp_pdmc_probe()
1020 ret = PTR_ERR(dd->regmap); in mchp_pdmc_probe()
1026 dev_name(&pdev->dev), (void *)dd); in mchp_pdmc_probe()
1036 dd->audio_filter_en = true; in mchp_pdmc_probe()
1037 dd->sinc_order = 3; in mchp_pdmc_probe()
1039 dd->addr.addr = (dma_addr_t)res->start + MCHP_PDMC_RHR; in mchp_pdmc_probe()
1057 regmap_read(dd->regmap, MCHP_PDMC_VER, &version); in mchp_pdmc_probe()
1058 dev_info(dd->dev, "hw version: %#lx\n", in mchp_pdmc_probe()
1066 .compatible = "microchip,sama7g5-pdmc",
1075 .name = "mchp-pdmc",