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>
11 #include <linux/bitfield.h>
12 #include <linux/clk.h>
13 #include <linux/module.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/regmap.h>
18 #include <sound/core.h>
19 #include <sound/dmaengine_pcm.h>
20 #include <sound/pcm_params.h>
21 #include <sound/tlv.h>
24 * ---- PDMC Register map ----
26 #define MCHP_PDMC_CR 0x00 /* Control Register */
27 #define MCHP_PDMC_MR 0x04 /* Mode Register */
28 #define MCHP_PDMC_CFGR 0x08 /* Configuration Register */
29 #define MCHP_PDMC_RHR 0x0C /* Receive Holding Register */
30 #define MCHP_PDMC_IER 0x14 /* Interrupt Enable Register */
31 #define MCHP_PDMC_IDR 0x18 /* Interrupt Disable Register */
32 #define MCHP_PDMC_IMR 0x1C /* Interrupt Mask Register */
33 #define MCHP_PDMC_ISR 0x20 /* Interrupt Status Register */
34 #define MCHP_PDMC_VER 0x50 /* Version Register */
37 * ---- Control Register (Write-only) ----
39 #define MCHP_PDMC_CR_SWRST BIT(0) /* Software Reset */
42 * ---- Mode Register (Read/Write) ----
44 #define MCHP_PDMC_MR_PDMCEN_MASK GENMASK(3, 0)
45 #define MCHP_PDMC_MR_PDMCEN(ch) (BIT(ch) & MCHP_PDMC_MR_PDMCEN_MASK)
47 #define MCHP_PDMC_MR_OSR_MASK GENMASK(17, 16)
48 #define MCHP_PDMC_MR_OSR64 (1 << 16)
49 #define MCHP_PDMC_MR_OSR128 (2 << 16)
50 #define MCHP_PDMC_MR_OSR256 (3 << 16)
52 #define MCHP_PDMC_MR_SINCORDER_MASK GENMASK(23, 20)
54 #define MCHP_PDMC_MR_SINC_OSR_MASK GENMASK(27, 24)
55 #define MCHP_PDMC_MR_SINC_OSR_DIS (0 << 24)
56 #define MCHP_PDMC_MR_SINC_OSR_8 (1 << 24)
57 #define MCHP_PDMC_MR_SINC_OSR_16 (2 << 24)
58 #define MCHP_PDMC_MR_SINC_OSR_32 (3 << 24)
59 #define MCHP_PDMC_MR_SINC_OSR_64 (4 << 24)
60 #define MCHP_PDMC_MR_SINC_OSR_128 (5 << 24)
61 #define MCHP_PDMC_MR_SINC_OSR_256 (6 << 24)
63 #define MCHP_PDMC_MR_CHUNK_MASK GENMASK(31, 28)
66 * ---- Configuration Register (Read/Write) ----
68 #define MCHP_PDMC_CFGR_BSSEL_MASK (BIT(0) | BIT(2) | BIT(4) | BIT(6))
69 #define MCHP_PDMC_CFGR_BSSEL(ch) BIT((ch) * 2)
71 #define MCHP_PDMC_CFGR_PDMSEL_MASK (BIT(16) | BIT(18) | BIT(20) | BIT(22))
72 #define MCHP_PDMC_CFGR_PDMSEL(ch) BIT((ch) * 2 + 16)
75 * ---- Interrupt Enable/Disable/Mask/Status Registers ----
77 #define MCHP_PDMC_IR_RXRDY BIT(0)
78 #define MCHP_PDMC_IR_RXEMPTY BIT(1)
79 #define MCHP_PDMC_IR_RXFULL BIT(2)
80 #define MCHP_PDMC_IR_RXCHUNK BIT(3)
81 #define MCHP_PDMC_IR_RXUDR BIT(4)
82 #define MCHP_PDMC_IR_RXOVR BIT(5)
85 * ---- Version Register (Read-only) ----
87 #define MCHP_PDMC_VER_VERSION GENMASK(11, 0)
89 #define MCHP_PDMC_MAX_CHANNELS 4
90 #define MCHP_PDMC_DS_NO 2
91 #define MCHP_PDMC_EDGE_NO 2
98 struct mchp_pdmc_chmap {
99 struct snd_pcm_chmap_elem *chmap;
100 struct mchp_pdmc *dd;
102 struct snd_kcontrol *kctl;
106 struct mic_map channel_mic_map[MCHP_PDMC_MAX_CHANNELS];
108 struct snd_dmaengine_dai_dma_data addr;
109 struct regmap *regmap;
114 u32 startup_delay_us;
117 bool audio_filter_en;
120 static const char *const mchp_pdmc_sinc_filter_order_text[] = {
121 "1", "2", "3", "4", "5"
124 static const unsigned int mchp_pdmc_sinc_filter_order_values[] = {
128 static const struct soc_enum mchp_pdmc_sinc_filter_order_enum = {
129 .items = ARRAY_SIZE(mchp_pdmc_sinc_filter_order_text),
130 .texts = mchp_pdmc_sinc_filter_order_text,
131 .values = mchp_pdmc_sinc_filter_order_values,
134 static int mchp_pdmc_sinc_order_get(struct snd_kcontrol *kcontrol,
135 struct snd_ctl_elem_value *uvalue)
137 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
138 struct mchp_pdmc *dd = snd_soc_component_get_drvdata(component);
139 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
142 item = snd_soc_enum_val_to_item(e, dd->sinc_order);
143 uvalue->value.enumerated.item[0] = item;
148 static int mchp_pdmc_sinc_order_put(struct snd_kcontrol *kcontrol,
149 struct snd_ctl_elem_value *uvalue)
151 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
152 struct mchp_pdmc *dd = snd_soc_component_get_drvdata(component);
153 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
154 unsigned int *item = uvalue->value.enumerated.item;
157 if (item[0] >= e->items)
160 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
161 if (val == dd->sinc_order)
164 dd->sinc_order = val;
169 static int mchp_pdmc_af_get(struct snd_kcontrol *kcontrol,
170 struct snd_ctl_elem_value *uvalue)
172 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
173 struct mchp_pdmc *dd = snd_soc_component_get_drvdata(component);
175 uvalue->value.integer.value[0] = !!dd->audio_filter_en;
180 static int mchp_pdmc_af_put(struct snd_kcontrol *kcontrol,
181 struct snd_ctl_elem_value *uvalue)
183 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
184 struct mchp_pdmc *dd = snd_soc_component_get_drvdata(component);
185 bool af = uvalue->value.integer.value[0] ? true : false;
187 if (dd->audio_filter_en == af)
190 dd->audio_filter_en = af;
195 static int mchp_pdmc_chmap_ctl_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
197 struct mchp_pdmc_chmap *info = snd_kcontrol_chip(kcontrol);
199 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
200 uinfo->count = info->dd->mic_no;
201 uinfo->value.integer.min = 0;
202 uinfo->value.integer.max = SNDRV_CHMAP_RR; /* maxmimum 4 channels */
206 static inline struct snd_pcm_substream *
207 mchp_pdmc_chmap_substream(struct mchp_pdmc_chmap *info, unsigned int idx)
209 struct snd_pcm_substream *s;
211 for (s = info->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; s; s = s->next)
212 if (s->number == idx)
217 static struct snd_pcm_chmap_elem *mchp_pdmc_chmap_get(struct snd_pcm_substream *substream,
218 struct mchp_pdmc_chmap *ch_info)
220 struct snd_pcm_chmap_elem *map;
222 for (map = ch_info->chmap; map->channels; map++) {
223 if (map->channels == substream->runtime->channels)
229 static int mchp_pdmc_chmap_ctl_get(struct snd_kcontrol *kcontrol,
230 struct snd_ctl_elem_value *ucontrol)
232 struct mchp_pdmc_chmap *info = snd_kcontrol_chip(kcontrol);
233 struct mchp_pdmc *dd = info->dd;
234 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
235 struct snd_pcm_substream *substream;
236 const struct snd_pcm_chmap_elem *map;
242 substream = mchp_pdmc_chmap_substream(info, idx);
245 memset(ucontrol->value.integer.value, 0, sizeof(long) * info->dd->mic_no);
246 if (!substream->runtime)
247 return 0; /* no channels set */
249 map = mchp_pdmc_chmap_get(substream, info);
253 for (i = 0; i < map->channels; i++) {
254 int map_idx = map->channels == 1 ? map->map[i] - SNDRV_CHMAP_MONO :
255 map->map[i] - SNDRV_CHMAP_FL;
257 /* make sure the reported channel map is the real one, so write the map */
258 if (dd->channel_mic_map[map_idx].ds_pos)
259 cfgr_val |= MCHP_PDMC_CFGR_PDMSEL(i);
260 if (dd->channel_mic_map[map_idx].clk_edge)
261 cfgr_val |= MCHP_PDMC_CFGR_BSSEL(i);
263 ucontrol->value.integer.value[i] = map->map[i];
266 regmap_write(dd->regmap, MCHP_PDMC_CFGR, cfgr_val);
271 static int mchp_pdmc_chmap_ctl_put(struct snd_kcontrol *kcontrol,
272 struct snd_ctl_elem_value *ucontrol)
274 struct mchp_pdmc_chmap *info = snd_kcontrol_chip(kcontrol);
275 struct mchp_pdmc *dd = info->dd;
276 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
277 struct snd_pcm_substream *substream;
278 struct snd_pcm_chmap_elem *map;
284 substream = mchp_pdmc_chmap_substream(info, idx);
288 map = mchp_pdmc_chmap_get(substream, info);
292 for (i = 0; i < map->channels; i++) {
295 map->map[i] = ucontrol->value.integer.value[i];
296 map_idx = map->channels == 1 ? map->map[i] - SNDRV_CHMAP_MONO :
297 map->map[i] - SNDRV_CHMAP_FL;
299 /* configure IP for the desired channel map */
300 if (dd->channel_mic_map[map_idx].ds_pos)
301 cfgr_val |= MCHP_PDMC_CFGR_PDMSEL(i);
302 if (dd->channel_mic_map[map_idx].clk_edge)
303 cfgr_val |= MCHP_PDMC_CFGR_BSSEL(i);
306 regmap_write(dd->regmap, MCHP_PDMC_CFGR, cfgr_val);
311 static void mchp_pdmc_chmap_ctl_private_free(struct snd_kcontrol *kcontrol)
313 struct mchp_pdmc_chmap *info = snd_kcontrol_chip(kcontrol);
315 info->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].chmap_kctl = NULL;
319 static int mchp_pdmc_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
320 unsigned int size, unsigned int __user *tlv)
322 struct mchp_pdmc_chmap *info = snd_kcontrol_chip(kcontrol);
323 const struct snd_pcm_chmap_elem *map;
324 unsigned int __user *dst;
331 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
335 for (map = info->chmap; map->channels; map++) {
336 int chs_bytes = map->channels * 4;
340 if (put_user(SNDRV_CTL_TLVT_CHMAP_VAR, dst) ||
341 put_user(chs_bytes, dst + 1))
346 if (size < chs_bytes)
350 for (c = 0; c < map->channels; c++) {
351 if (put_user(map->map[c], dst))
356 if (put_user(count, tlv + 1))
361 static const struct snd_kcontrol_new mchp_pdmc_snd_controls[] = {
362 SOC_SINGLE_BOOL_EXT("Audio Filter", 0, &mchp_pdmc_af_get, &mchp_pdmc_af_put),
364 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
365 .name = "SINC Filter Order",
366 .info = snd_soc_info_enum_double,
367 .get = mchp_pdmc_sinc_order_get,
368 .put = mchp_pdmc_sinc_order_put,
369 .private_value = (unsigned long)&mchp_pdmc_sinc_filter_order_enum,
373 static int mchp_pdmc_close(struct snd_soc_component *component,
374 struct snd_pcm_substream *substream)
376 return snd_soc_add_component_controls(component, mchp_pdmc_snd_controls,
377 ARRAY_SIZE(mchp_pdmc_snd_controls));
380 static int mchp_pdmc_open(struct snd_soc_component *component,
381 struct snd_pcm_substream *substream)
385 /* remove controls that can't be changed at runtime */
386 for (i = 0; i < ARRAY_SIZE(mchp_pdmc_snd_controls); i++) {
387 const struct snd_kcontrol_new *control = &mchp_pdmc_snd_controls[i];
388 struct snd_ctl_elem_id id;
389 struct snd_kcontrol *kctl;
392 if (component->name_prefix)
393 snprintf(id.name, sizeof(id.name), "%s %s", component->name_prefix,
396 strscpy(id.name, control->name, sizeof(id.name));
399 id.iface = control->iface;
400 id.device = control->device;
401 id.subdevice = control->subdevice;
402 id.index = control->index;
403 kctl = snd_ctl_find_id(component->card->snd_card, &id);
405 dev_err(component->dev, "Failed to find %s\n", control->name);
408 err = snd_ctl_remove(component->card->snd_card, kctl);
410 dev_err(component->dev, "%d: Failed to remove %s\n", err,
419 static const struct snd_soc_component_driver mchp_pdmc_dai_component = {
421 .controls = mchp_pdmc_snd_controls,
422 .num_controls = ARRAY_SIZE(mchp_pdmc_snd_controls),
423 .open = &mchp_pdmc_open,
424 .close = &mchp_pdmc_close,
425 .legacy_dai_naming = 1,
429 static const unsigned int mchp_pdmc_1mic[] = {1};
430 static const unsigned int mchp_pdmc_2mic[] = {1, 2};
431 static const unsigned int mchp_pdmc_3mic[] = {1, 2, 3};
432 static const unsigned int mchp_pdmc_4mic[] = {1, 2, 3, 4};
434 static const struct snd_pcm_hw_constraint_list mchp_pdmc_chan_constr[] = {
436 .list = mchp_pdmc_1mic,
437 .count = ARRAY_SIZE(mchp_pdmc_1mic),
440 .list = mchp_pdmc_2mic,
441 .count = ARRAY_SIZE(mchp_pdmc_2mic),
444 .list = mchp_pdmc_3mic,
445 .count = ARRAY_SIZE(mchp_pdmc_3mic),
448 .list = mchp_pdmc_4mic,
449 .count = ARRAY_SIZE(mchp_pdmc_4mic),
453 static int mchp_pdmc_startup(struct snd_pcm_substream *substream,
454 struct snd_soc_dai *dai)
456 struct mchp_pdmc *dd = snd_soc_dai_get_drvdata(dai);
458 regmap_write(dd->regmap, MCHP_PDMC_CR, MCHP_PDMC_CR_SWRST);
460 snd_pcm_hw_constraint_list(substream->runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
461 &mchp_pdmc_chan_constr[dd->mic_no - 1]);
466 static int mchp_pdmc_dai_probe(struct snd_soc_dai *dai)
468 struct mchp_pdmc *dd = snd_soc_dai_get_drvdata(dai);
470 snd_soc_dai_init_dma_data(dai, NULL, &dd->addr);
475 static int mchp_pdmc_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
477 unsigned int fmt_master = fmt & SND_SOC_DAIFMT_MASTER_MASK;
478 unsigned int fmt_format = fmt & SND_SOC_DAIFMT_FORMAT_MASK;
480 /* IP needs to be bitclock master */
481 if (fmt_master != SND_SOC_DAIFMT_BP_FP &&
482 fmt_master != SND_SOC_DAIFMT_BP_FC)
485 /* IP supports only PDM interface */
486 if (fmt_format != SND_SOC_DAIFMT_PDM)
492 static u32 mchp_pdmc_mr_set_osr(int audio_filter_en, unsigned int osr)
494 if (audio_filter_en) {
497 return MCHP_PDMC_MR_OSR64;
499 return MCHP_PDMC_MR_OSR128;
501 return MCHP_PDMC_MR_OSR256;
506 return MCHP_PDMC_MR_SINC_OSR_8;
508 return MCHP_PDMC_MR_SINC_OSR_16;
510 return MCHP_PDMC_MR_SINC_OSR_32;
512 return MCHP_PDMC_MR_SINC_OSR_64;
514 return MCHP_PDMC_MR_SINC_OSR_128;
516 return MCHP_PDMC_MR_SINC_OSR_256;
522 static inline int mchp_pdmc_period_to_maxburst(int period_size)
524 if (!(period_size % 8))
526 if (!(period_size % 4))
528 if (!(period_size % 2))
533 static struct snd_pcm_chmap_elem mchp_pdmc_std_chmaps[] = {
535 .map = { SNDRV_CHMAP_MONO } },
537 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
539 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
542 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
543 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
547 static int mchp_pdmc_hw_params(struct snd_pcm_substream *substream,
548 struct snd_pcm_hw_params *params,
549 struct snd_soc_dai *dai)
551 struct mchp_pdmc *dd = snd_soc_dai_get_drvdata(dai);
552 struct snd_soc_component *comp = dai->component;
553 unsigned long gclk_rate = 0;
554 unsigned long best_diff_rate = ~0UL;
555 unsigned int channels = params_channels(params);
556 unsigned int osr = 0, osr_start;
557 unsigned int fs = params_rate(params);
563 dev_dbg(comp->dev, "%s() rate=%u format=%#x width=%u channels=%u\n",
564 __func__, params_rate(params), params_format(params),
565 params_width(params), params_channels(params));
567 if (channels > dd->mic_no) {
568 dev_err(comp->dev, "more channels %u than microphones %d\n",
569 channels, dd->mic_no);
574 for (i = 0; i < channels; i++) {
575 dd->pdmcen |= MCHP_PDMC_MR_PDMCEN(i);
576 if (dd->channel_mic_map[i].ds_pos)
577 cfgr_val |= MCHP_PDMC_CFGR_PDMSEL(i);
578 if (dd->channel_mic_map[i].clk_edge)
579 cfgr_val |= MCHP_PDMC_CFGR_BSSEL(i);
582 for (osr_start = dd->audio_filter_en ? 64 : 8;
583 osr_start <= 256 && best_diff_rate; osr_start *= 2) {
585 unsigned long diff_rate;
587 round_rate = clk_round_rate(dd->gclk,
588 (unsigned long)fs * 16 * osr_start);
591 diff_rate = abs((fs * 16 * osr_start) - round_rate);
592 if (diff_rate < best_diff_rate) {
593 best_diff_rate = diff_rate;
595 gclk_rate = fs * 16 * osr;
599 dev_err(comp->dev, "invalid sampling rate: %u\n", fs);
603 /* CLK is enabled by runtime PM. */
604 clk_disable_unprepare(dd->gclk);
607 ret = clk_set_rate(dd->gclk, gclk_rate);
608 clk_prepare_enable(dd->gclk);
610 dev_err(comp->dev, "unable to set rate %lu to GCLK: %d\n",
615 mr_val |= mchp_pdmc_mr_set_osr(dd->audio_filter_en, osr);
617 mr_val |= FIELD_PREP(MCHP_PDMC_MR_SINCORDER_MASK, dd->sinc_order);
619 dd->addr.maxburst = mchp_pdmc_period_to_maxburst(snd_pcm_lib_period_bytes(substream));
620 mr_val |= FIELD_PREP(MCHP_PDMC_MR_CHUNK_MASK, dd->addr.maxburst);
621 dev_dbg(comp->dev, "maxburst set to %d\n", dd->addr.maxburst);
623 snd_soc_component_update_bits(comp, MCHP_PDMC_MR,
624 MCHP_PDMC_MR_OSR_MASK |
625 MCHP_PDMC_MR_SINCORDER_MASK |
626 MCHP_PDMC_MR_SINC_OSR_MASK |
627 MCHP_PDMC_MR_CHUNK_MASK, mr_val);
629 snd_soc_component_write(comp, MCHP_PDMC_CFGR, cfgr_val);
634 static void mchp_pdmc_noise_filter_workaround(struct mchp_pdmc *dd)
639 * PDMC doesn't wait for microphones' startup time thus the acquisition
640 * may start before the microphones are ready leading to poc noises at
641 * the beginning of capture. To avoid this, we need to wait 50ms (in
642 * normal startup procedure) or 150 ms (worst case after resume from sleep
643 * states) after microphones are enabled and then clear the FIFOs (by
644 * reading the RHR 16 times) and possible interrupts before continuing.
645 * Also, for this to work the DMA needs to be started after interrupts
648 usleep_range(dd->startup_delay_us, dd->startup_delay_us + 5);
651 regmap_read(dd->regmap, MCHP_PDMC_RHR, &tmp);
653 /* Clear interrupts. */
654 regmap_read(dd->regmap, MCHP_PDMC_ISR, &tmp);
657 static int mchp_pdmc_trigger(struct snd_pcm_substream *substream,
658 int cmd, struct snd_soc_dai *dai)
660 struct mchp_pdmc *dd = snd_soc_dai_get_drvdata(dai);
661 struct snd_soc_component *cpu = dai->component;
667 case SNDRV_PCM_TRIGGER_RESUME:
668 case SNDRV_PCM_TRIGGER_START:
669 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
670 snd_soc_component_update_bits(cpu, MCHP_PDMC_MR,
671 MCHP_PDMC_MR_PDMCEN_MASK,
674 mchp_pdmc_noise_filter_workaround(dd);
676 /* Enable interrupts. */
677 regmap_write(dd->regmap, MCHP_PDMC_IER, dd->suspend_irq |
678 MCHP_PDMC_IR_RXOVR | MCHP_PDMC_IR_RXUDR);
681 case SNDRV_PCM_TRIGGER_SUSPEND:
682 regmap_read(dd->regmap, MCHP_PDMC_IMR, &dd->suspend_irq);
684 case SNDRV_PCM_TRIGGER_STOP:
685 /* Disable overrun and underrun error interrupts */
686 regmap_write(dd->regmap, MCHP_PDMC_IDR, dd->suspend_irq |
687 MCHP_PDMC_IR_RXOVR | MCHP_PDMC_IR_RXUDR);
689 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
690 snd_soc_component_update_bits(cpu, MCHP_PDMC_MR,
691 MCHP_PDMC_MR_PDMCEN_MASK, 0);
698 regmap_read(dd->regmap, MCHP_PDMC_MR, &val);
699 dev_dbg(dd->dev, "MR (0x%02x): 0x%08x\n", MCHP_PDMC_MR, val);
700 regmap_read(dd->regmap, MCHP_PDMC_CFGR, &val);
701 dev_dbg(dd->dev, "CFGR (0x%02x): 0x%08x\n", MCHP_PDMC_CFGR, val);
702 regmap_read(dd->regmap, MCHP_PDMC_IMR, &val);
703 dev_dbg(dd->dev, "IMR (0x%02x): 0x%08x\n", MCHP_PDMC_IMR, val);
709 static const struct snd_soc_dai_ops mchp_pdmc_dai_ops = {
710 .set_fmt = mchp_pdmc_set_fmt,
711 .startup = mchp_pdmc_startup,
712 .hw_params = mchp_pdmc_hw_params,
713 .trigger = mchp_pdmc_trigger,
716 static int mchp_pdmc_add_chmap_ctls(struct snd_pcm *pcm, struct mchp_pdmc *dd)
718 struct mchp_pdmc_chmap *info;
719 struct snd_kcontrol_new knew = {
720 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
721 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
722 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
723 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
724 .info = mchp_pdmc_chmap_ctl_info,
725 .get = mchp_pdmc_chmap_ctl_get,
726 .put = mchp_pdmc_chmap_ctl_put,
727 .tlv.c = mchp_pdmc_chmap_ctl_tlv,
731 if (WARN_ON(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].chmap_kctl))
733 info = kzalloc(sizeof(*info), GFP_KERNEL);
738 info->chmap = mchp_pdmc_std_chmaps;
739 knew.name = "Capture Channel Map";
740 knew.device = pcm->device;
741 knew.count = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
742 info->kctl = snd_ctl_new1(&knew, info);
747 info->kctl->private_free = mchp_pdmc_chmap_ctl_private_free;
748 err = snd_ctl_add(pcm->card, info->kctl);
751 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].chmap_kctl = info->kctl;
755 static int mchp_pdmc_pcm_new(struct snd_soc_pcm_runtime *rtd,
756 struct snd_soc_dai *dai)
758 struct mchp_pdmc *dd = snd_soc_dai_get_drvdata(dai);
761 ret = mchp_pdmc_add_chmap_ctls(rtd->pcm, dd);
763 dev_err(dd->dev, "failed to add channel map controls: %d\n", ret);
768 static struct snd_soc_dai_driver mchp_pdmc_dai = {
769 .probe = mchp_pdmc_dai_probe,
771 .stream_name = "Capture",
776 .rates = SNDRV_PCM_RATE_KNOT,
777 .formats = SNDRV_PCM_FMTBIT_S24_LE,
779 .ops = &mchp_pdmc_dai_ops,
780 .pcm_new = &mchp_pdmc_pcm_new,
783 /* PDMC interrupt handler */
784 static irqreturn_t mchp_pdmc_interrupt(int irq, void *dev_id)
786 struct mchp_pdmc *dd = dev_id;
787 u32 isr, msr, pending;
788 irqreturn_t ret = IRQ_NONE;
790 regmap_read(dd->regmap, MCHP_PDMC_ISR, &isr);
791 regmap_read(dd->regmap, MCHP_PDMC_IMR, &msr);
794 dev_dbg(dd->dev, "ISR (0x%02x): 0x%08x, IMR (0x%02x): 0x%08x, pending: 0x%08x\n",
795 MCHP_PDMC_ISR, isr, MCHP_PDMC_IMR, msr, pending);
799 if (pending & MCHP_PDMC_IR_RXUDR) {
800 dev_warn(dd->dev, "underrun detected\n");
801 regmap_write(dd->regmap, MCHP_PDMC_IDR, MCHP_PDMC_IR_RXUDR);
804 if (pending & MCHP_PDMC_IR_RXOVR) {
805 dev_warn(dd->dev, "overrun detected\n");
806 regmap_write(dd->regmap, MCHP_PDMC_IDR, MCHP_PDMC_IR_RXOVR);
813 /* regmap configuration */
814 static bool mchp_pdmc_readable_reg(struct device *dev, unsigned int reg)
829 static bool mchp_pdmc_writeable_reg(struct device *dev, unsigned int reg)
843 static bool mchp_pdmc_volatile_reg(struct device *dev, unsigned int reg)
854 static bool mchp_pdmc_precious_reg(struct device *dev, unsigned int reg)
865 static const struct regmap_config mchp_pdmc_regmap_config = {
869 .max_register = MCHP_PDMC_VER,
870 .readable_reg = mchp_pdmc_readable_reg,
871 .writeable_reg = mchp_pdmc_writeable_reg,
872 .precious_reg = mchp_pdmc_precious_reg,
873 .volatile_reg = mchp_pdmc_volatile_reg,
874 .cache_type = REGCACHE_FLAT,
877 static int mchp_pdmc_dt_init(struct mchp_pdmc *dd)
879 struct device_node *np = dd->dev->of_node;
880 bool mic_ch[MCHP_PDMC_DS_NO][MCHP_PDMC_EDGE_NO] = {0};
885 dev_err(dd->dev, "device node not found\n");
889 dd->mic_no = of_property_count_u32_elems(np, "microchip,mic-pos");
890 if (dd->mic_no < 0) {
891 dev_err(dd->dev, "failed to get microchip,mic-pos: %d",
895 if (!dd->mic_no || dd->mic_no % 2 ||
896 dd->mic_no / 2 > MCHP_PDMC_MAX_CHANNELS) {
897 dev_err(dd->dev, "invalid array length for microchip,mic-pos: %d",
904 dev_info(dd->dev, "%d PDM microphones declared\n", dd->mic_no);
907 * by default, we consider the order of microphones in
908 * microchip,mic-pos to be the same with the channel mapping;
909 * 1st microphone channel 0, 2nd microphone channel 1, etc.
911 for (i = 0; i < dd->mic_no; i++) {
915 ret = of_property_read_u32_index(np, "microchip,mic-pos", i * 2,
919 "failed to get value no %d value from microchip,mic-pos: %d",
923 if (ds >= MCHP_PDMC_DS_NO) {
925 "invalid DS index in microchip,mic-pos array: %d",
930 ret = of_property_read_u32_index(np, "microchip,mic-pos", i * 2 + 1,
934 "failed to get value no %d value from microchip,mic-pos: %d",
939 if (edge != MCHP_PDMC_CLK_POSITIVE &&
940 edge != MCHP_PDMC_CLK_NEGATIVE) {
942 "invalid edge in microchip,mic-pos array: %d", edge);
945 if (mic_ch[ds][edge]) {
947 "duplicated mic (DS %d, edge %d) in microchip,mic-pos array",
951 mic_ch[ds][edge] = true;
952 dd->channel_mic_map[i].ds_pos = ds;
953 dd->channel_mic_map[i].clk_edge = edge;
956 dd->startup_delay_us = 150000;
957 of_property_read_u32(np, "microchip,startup-delay-us", &dd->startup_delay_us);
962 /* used to clean the channel index found on RHR's MSB */
963 static int mchp_pdmc_process(struct snd_pcm_substream *substream,
964 int channel, unsigned long hwoff,
965 void *buf, unsigned long bytes)
967 struct snd_pcm_runtime *runtime = substream->runtime;
968 u8 *dma_ptr = runtime->dma_area + hwoff +
969 channel * (runtime->dma_bytes / runtime->channels);
970 u8 *dma_ptr_end = dma_ptr + bytes;
971 unsigned int sample_size = samples_to_bytes(runtime, 1);
973 for (; dma_ptr < dma_ptr_end; dma_ptr += sample_size)
979 static struct snd_dmaengine_pcm_config mchp_pdmc_config = {
980 .process = mchp_pdmc_process,
981 .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
984 static int mchp_pdmc_runtime_suspend(struct device *dev)
986 struct mchp_pdmc *dd = dev_get_drvdata(dev);
988 regcache_cache_only(dd->regmap, true);
990 clk_disable_unprepare(dd->gclk);
991 clk_disable_unprepare(dd->pclk);
996 static int mchp_pdmc_runtime_resume(struct device *dev)
998 struct mchp_pdmc *dd = dev_get_drvdata(dev);
1001 ret = clk_prepare_enable(dd->pclk);
1004 "failed to enable the peripheral clock: %d\n", ret);
1007 ret = clk_prepare_enable(dd->gclk);
1010 "failed to enable generic clock: %d\n", ret);
1014 regcache_cache_only(dd->regmap, false);
1015 regcache_mark_dirty(dd->regmap);
1016 ret = regcache_sync(dd->regmap);
1018 regcache_cache_only(dd->regmap, true);
1019 clk_disable_unprepare(dd->gclk);
1021 clk_disable_unprepare(dd->pclk);
1027 static int mchp_pdmc_probe(struct platform_device *pdev)
1029 struct device *dev = &pdev->dev;
1030 struct mchp_pdmc *dd;
1031 struct resource *res;
1032 void __iomem *io_base;
1037 dd = devm_kzalloc(dev, sizeof(*dd), GFP_KERNEL);
1041 dd->dev = &pdev->dev;
1042 ret = mchp_pdmc_dt_init(dd);
1046 irq = platform_get_irq(pdev, 0);
1050 dd->pclk = devm_clk_get(dev, "pclk");
1051 if (IS_ERR(dd->pclk)) {
1052 ret = PTR_ERR(dd->pclk);
1053 dev_err(dev, "failed to get peripheral clock: %d\n", ret);
1057 dd->gclk = devm_clk_get(dev, "gclk");
1058 if (IS_ERR(dd->gclk)) {
1059 ret = PTR_ERR(dd->gclk);
1060 dev_err(dev, "failed to get GCK: %d\n", ret);
1064 io_base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
1065 if (IS_ERR(io_base)) {
1066 ret = PTR_ERR(io_base);
1067 dev_err(dev, "failed to remap register memory: %d\n", ret);
1071 dd->regmap = devm_regmap_init_mmio(dev, io_base,
1072 &mchp_pdmc_regmap_config);
1073 if (IS_ERR(dd->regmap)) {
1074 ret = PTR_ERR(dd->regmap);
1075 dev_err(dev, "failed to init register map: %d\n", ret);
1079 ret = devm_request_irq(dev, irq, mchp_pdmc_interrupt, 0,
1080 dev_name(&pdev->dev), dd);
1082 dev_err(dev, "can't register ISR for IRQ %u (ret=%i)\n",
1087 /* by default audio filter is enabled and the SINC Filter order
1088 * will be set to the recommended value, 3
1090 dd->audio_filter_en = true;
1093 dd->addr.addr = (dma_addr_t)res->start + MCHP_PDMC_RHR;
1094 platform_set_drvdata(pdev, dd);
1096 pm_runtime_enable(dd->dev);
1097 if (!pm_runtime_enabled(dd->dev)) {
1098 ret = mchp_pdmc_runtime_resume(dd->dev);
1103 /* register platform */
1104 ret = devm_snd_dmaengine_pcm_register(dev, &mchp_pdmc_config, 0);
1106 dev_err(dev, "could not register platform: %d\n", ret);
1107 goto pm_runtime_suspend;
1110 ret = devm_snd_soc_register_component(dev, &mchp_pdmc_dai_component,
1113 dev_err(dev, "could not register CPU DAI: %d\n", ret);
1114 goto pm_runtime_suspend;
1117 /* print IP version */
1118 regmap_read(dd->regmap, MCHP_PDMC_VER, &version);
1119 dev_info(dd->dev, "hw version: %#lx\n",
1120 version & MCHP_PDMC_VER_VERSION);
1125 if (!pm_runtime_status_suspended(dd->dev))
1126 mchp_pdmc_runtime_suspend(dd->dev);
1127 pm_runtime_disable(dd->dev);
1132 static void mchp_pdmc_remove(struct platform_device *pdev)
1134 struct mchp_pdmc *dd = platform_get_drvdata(pdev);
1136 if (!pm_runtime_status_suspended(dd->dev))
1137 mchp_pdmc_runtime_suspend(dd->dev);
1139 pm_runtime_disable(dd->dev);
1142 static const struct of_device_id mchp_pdmc_of_match[] = {
1144 .compatible = "microchip,sama7g5-pdmc",
1149 MODULE_DEVICE_TABLE(of, mchp_pdmc_of_match);
1151 static const struct dev_pm_ops mchp_pdmc_pm_ops = {
1152 SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume)
1153 RUNTIME_PM_OPS(mchp_pdmc_runtime_suspend, mchp_pdmc_runtime_resume,
1157 static struct platform_driver mchp_pdmc_driver = {
1159 .name = "mchp-pdmc",
1160 .of_match_table = of_match_ptr(mchp_pdmc_of_match),
1161 .pm = pm_ptr(&mchp_pdmc_pm_ops),
1163 .probe = mchp_pdmc_probe,
1164 .remove_new = mchp_pdmc_remove,
1166 module_platform_driver(mchp_pdmc_driver);
1168 MODULE_DESCRIPTION("Microchip PDMC driver under ALSA SoC architecture");
1170 MODULE_LICENSE("GPL v2");