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
7 // Author: Codrin Ciubotariu <codrin.ciubotariu@microchip.com>
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;
391 if (component->name_prefix)
392 snprintf(id.name, sizeof(id.name), "%s %s", component->name_prefix,
395 strscpy(id.name, control->name, sizeof(id.name));
398 id.iface = control->iface;
399 id.device = control->device;
400 id.subdevice = control->subdevice;
401 id.index = control->index;
402 err = snd_ctl_remove_id(component->card->snd_card, &id);
404 dev_err(component->dev, "%d: Failed to remove %s\n", err,
411 static const struct snd_soc_component_driver mchp_pdmc_dai_component = {
413 .controls = mchp_pdmc_snd_controls,
414 .num_controls = ARRAY_SIZE(mchp_pdmc_snd_controls),
415 .open = &mchp_pdmc_open,
416 .close = &mchp_pdmc_close,
417 .legacy_dai_naming = 1,
418 .trigger_start = SND_SOC_TRIGGER_ORDER_LDC,
421 static const unsigned int mchp_pdmc_1mic[] = {1};
422 static const unsigned int mchp_pdmc_2mic[] = {1, 2};
423 static const unsigned int mchp_pdmc_3mic[] = {1, 2, 3};
424 static const unsigned int mchp_pdmc_4mic[] = {1, 2, 3, 4};
426 static const struct snd_pcm_hw_constraint_list mchp_pdmc_chan_constr[] = {
428 .list = mchp_pdmc_1mic,
429 .count = ARRAY_SIZE(mchp_pdmc_1mic),
432 .list = mchp_pdmc_2mic,
433 .count = ARRAY_SIZE(mchp_pdmc_2mic),
436 .list = mchp_pdmc_3mic,
437 .count = ARRAY_SIZE(mchp_pdmc_3mic),
440 .list = mchp_pdmc_4mic,
441 .count = ARRAY_SIZE(mchp_pdmc_4mic),
445 static int mchp_pdmc_startup(struct snd_pcm_substream *substream,
446 struct snd_soc_dai *dai)
448 struct mchp_pdmc *dd = snd_soc_dai_get_drvdata(dai);
450 regmap_write(dd->regmap, MCHP_PDMC_CR, MCHP_PDMC_CR_SWRST);
452 snd_pcm_hw_constraint_list(substream->runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
453 &mchp_pdmc_chan_constr[dd->mic_no - 1]);
458 static int mchp_pdmc_dai_probe(struct snd_soc_dai *dai)
460 struct mchp_pdmc *dd = snd_soc_dai_get_drvdata(dai);
462 snd_soc_dai_init_dma_data(dai, NULL, &dd->addr);
467 static int mchp_pdmc_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
469 unsigned int fmt_master = fmt & SND_SOC_DAIFMT_MASTER_MASK;
470 unsigned int fmt_format = fmt & SND_SOC_DAIFMT_FORMAT_MASK;
472 /* IP needs to be bitclock master */
473 if (fmt_master != SND_SOC_DAIFMT_BP_FP &&
474 fmt_master != SND_SOC_DAIFMT_BP_FC)
477 /* IP supports only PDM interface */
478 if (fmt_format != SND_SOC_DAIFMT_PDM)
484 static u32 mchp_pdmc_mr_set_osr(int audio_filter_en, unsigned int osr)
486 if (audio_filter_en) {
489 return MCHP_PDMC_MR_OSR64;
491 return MCHP_PDMC_MR_OSR128;
493 return MCHP_PDMC_MR_OSR256;
498 return MCHP_PDMC_MR_SINC_OSR_8;
500 return MCHP_PDMC_MR_SINC_OSR_16;
502 return MCHP_PDMC_MR_SINC_OSR_32;
504 return MCHP_PDMC_MR_SINC_OSR_64;
506 return MCHP_PDMC_MR_SINC_OSR_128;
508 return MCHP_PDMC_MR_SINC_OSR_256;
514 static inline int mchp_pdmc_period_to_maxburst(int period_size)
516 if (!(period_size % 8))
518 if (!(period_size % 4))
520 if (!(period_size % 2))
525 static struct snd_pcm_chmap_elem mchp_pdmc_std_chmaps[] = {
527 .map = { SNDRV_CHMAP_MONO } },
529 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
531 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
534 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
535 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
539 static int mchp_pdmc_hw_params(struct snd_pcm_substream *substream,
540 struct snd_pcm_hw_params *params,
541 struct snd_soc_dai *dai)
543 struct mchp_pdmc *dd = snd_soc_dai_get_drvdata(dai);
544 struct snd_soc_component *comp = dai->component;
545 unsigned long gclk_rate = 0;
546 unsigned long best_diff_rate = ~0UL;
547 unsigned int channels = params_channels(params);
548 unsigned int osr = 0, osr_start;
549 unsigned int fs = params_rate(params);
555 dev_dbg(comp->dev, "%s() rate=%u format=%#x width=%u channels=%u\n",
556 __func__, params_rate(params), params_format(params),
557 params_width(params), params_channels(params));
559 if (channels > dd->mic_no) {
560 dev_err(comp->dev, "more channels %u than microphones %d\n",
561 channels, dd->mic_no);
566 for (i = 0; i < channels; i++) {
567 dd->pdmcen |= MCHP_PDMC_MR_PDMCEN(i);
568 if (dd->channel_mic_map[i].ds_pos)
569 cfgr_val |= MCHP_PDMC_CFGR_PDMSEL(i);
570 if (dd->channel_mic_map[i].clk_edge)
571 cfgr_val |= MCHP_PDMC_CFGR_BSSEL(i);
574 for (osr_start = dd->audio_filter_en ? 64 : 8;
575 osr_start <= 256 && best_diff_rate; osr_start *= 2) {
577 unsigned long diff_rate;
579 round_rate = clk_round_rate(dd->gclk,
580 (unsigned long)fs * 16 * osr_start);
583 diff_rate = abs((fs * 16 * osr_start) - round_rate);
584 if (diff_rate < best_diff_rate) {
585 best_diff_rate = diff_rate;
587 gclk_rate = fs * 16 * osr;
591 dev_err(comp->dev, "invalid sampling rate: %u\n", fs);
595 /* CLK is enabled by runtime PM. */
596 clk_disable_unprepare(dd->gclk);
599 ret = clk_set_rate(dd->gclk, gclk_rate);
600 clk_prepare_enable(dd->gclk);
602 dev_err(comp->dev, "unable to set rate %lu to GCLK: %d\n",
607 mr_val |= mchp_pdmc_mr_set_osr(dd->audio_filter_en, osr);
609 mr_val |= FIELD_PREP(MCHP_PDMC_MR_SINCORDER_MASK, dd->sinc_order);
611 dd->addr.maxburst = mchp_pdmc_period_to_maxburst(snd_pcm_lib_period_bytes(substream));
612 mr_val |= FIELD_PREP(MCHP_PDMC_MR_CHUNK_MASK, dd->addr.maxburst);
613 dev_dbg(comp->dev, "maxburst set to %d\n", dd->addr.maxburst);
615 snd_soc_component_update_bits(comp, MCHP_PDMC_MR,
616 MCHP_PDMC_MR_OSR_MASK |
617 MCHP_PDMC_MR_SINCORDER_MASK |
618 MCHP_PDMC_MR_SINC_OSR_MASK |
619 MCHP_PDMC_MR_CHUNK_MASK, mr_val);
621 snd_soc_component_write(comp, MCHP_PDMC_CFGR, cfgr_val);
626 static void mchp_pdmc_noise_filter_workaround(struct mchp_pdmc *dd)
631 * PDMC doesn't wait for microphones' startup time thus the acquisition
632 * may start before the microphones are ready leading to poc noises at
633 * the beginning of capture. To avoid this, we need to wait 50ms (in
634 * normal startup procedure) or 150 ms (worst case after resume from sleep
635 * states) after microphones are enabled and then clear the FIFOs (by
636 * reading the RHR 16 times) and possible interrupts before continuing.
637 * Also, for this to work the DMA needs to be started after interrupts
640 usleep_range(dd->startup_delay_us, dd->startup_delay_us + 5);
643 regmap_read(dd->regmap, MCHP_PDMC_RHR, &tmp);
645 /* Clear interrupts. */
646 regmap_read(dd->regmap, MCHP_PDMC_ISR, &tmp);
649 static int mchp_pdmc_trigger(struct snd_pcm_substream *substream,
650 int cmd, struct snd_soc_dai *dai)
652 struct mchp_pdmc *dd = snd_soc_dai_get_drvdata(dai);
653 struct snd_soc_component *cpu = dai->component;
659 case SNDRV_PCM_TRIGGER_RESUME:
660 case SNDRV_PCM_TRIGGER_START:
661 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
662 snd_soc_component_update_bits(cpu, MCHP_PDMC_MR,
663 MCHP_PDMC_MR_PDMCEN_MASK,
666 mchp_pdmc_noise_filter_workaround(dd);
668 /* Enable interrupts. */
669 regmap_write(dd->regmap, MCHP_PDMC_IER, dd->suspend_irq |
670 MCHP_PDMC_IR_RXOVR | MCHP_PDMC_IR_RXUDR);
673 case SNDRV_PCM_TRIGGER_SUSPEND:
674 regmap_read(dd->regmap, MCHP_PDMC_IMR, &dd->suspend_irq);
676 case SNDRV_PCM_TRIGGER_STOP:
677 /* Disable overrun and underrun error interrupts */
678 regmap_write(dd->regmap, MCHP_PDMC_IDR, dd->suspend_irq |
679 MCHP_PDMC_IR_RXOVR | MCHP_PDMC_IR_RXUDR);
681 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
682 snd_soc_component_update_bits(cpu, MCHP_PDMC_MR,
683 MCHP_PDMC_MR_PDMCEN_MASK, 0);
690 regmap_read(dd->regmap, MCHP_PDMC_MR, &val);
691 dev_dbg(dd->dev, "MR (0x%02x): 0x%08x\n", MCHP_PDMC_MR, val);
692 regmap_read(dd->regmap, MCHP_PDMC_CFGR, &val);
693 dev_dbg(dd->dev, "CFGR (0x%02x): 0x%08x\n", MCHP_PDMC_CFGR, val);
694 regmap_read(dd->regmap, MCHP_PDMC_IMR, &val);
695 dev_dbg(dd->dev, "IMR (0x%02x): 0x%08x\n", MCHP_PDMC_IMR, val);
701 static int mchp_pdmc_add_chmap_ctls(struct snd_pcm *pcm, struct mchp_pdmc *dd)
703 struct mchp_pdmc_chmap *info;
704 struct snd_kcontrol_new knew = {
705 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
706 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
707 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
708 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
709 .info = mchp_pdmc_chmap_ctl_info,
710 .get = mchp_pdmc_chmap_ctl_get,
711 .put = mchp_pdmc_chmap_ctl_put,
712 .tlv.c = mchp_pdmc_chmap_ctl_tlv,
716 if (WARN_ON(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].chmap_kctl))
718 info = kzalloc(sizeof(*info), GFP_KERNEL);
723 info->chmap = mchp_pdmc_std_chmaps;
724 knew.name = "Capture Channel Map";
725 knew.device = pcm->device;
726 knew.count = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
727 info->kctl = snd_ctl_new1(&knew, info);
732 info->kctl->private_free = mchp_pdmc_chmap_ctl_private_free;
733 err = snd_ctl_add(pcm->card, info->kctl);
736 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].chmap_kctl = info->kctl;
740 static int mchp_pdmc_pcm_new(struct snd_soc_pcm_runtime *rtd,
741 struct snd_soc_dai *dai)
743 struct mchp_pdmc *dd = snd_soc_dai_get_drvdata(dai);
746 ret = mchp_pdmc_add_chmap_ctls(rtd->pcm, dd);
748 dev_err(dd->dev, "failed to add channel map controls: %d\n", ret);
753 static const struct snd_soc_dai_ops mchp_pdmc_dai_ops = {
754 .probe = mchp_pdmc_dai_probe,
755 .set_fmt = mchp_pdmc_set_fmt,
756 .startup = mchp_pdmc_startup,
757 .hw_params = mchp_pdmc_hw_params,
758 .trigger = mchp_pdmc_trigger,
759 .pcm_new = &mchp_pdmc_pcm_new,
762 static struct snd_soc_dai_driver mchp_pdmc_dai = {
764 .stream_name = "Capture",
769 .rates = SNDRV_PCM_RATE_KNOT,
770 .formats = SNDRV_PCM_FMTBIT_S24_LE,
772 .ops = &mchp_pdmc_dai_ops,
775 /* PDMC interrupt handler */
776 static irqreturn_t mchp_pdmc_interrupt(int irq, void *dev_id)
778 struct mchp_pdmc *dd = dev_id;
779 u32 isr, msr, pending;
780 irqreturn_t ret = IRQ_NONE;
782 regmap_read(dd->regmap, MCHP_PDMC_ISR, &isr);
783 regmap_read(dd->regmap, MCHP_PDMC_IMR, &msr);
786 dev_dbg(dd->dev, "ISR (0x%02x): 0x%08x, IMR (0x%02x): 0x%08x, pending: 0x%08x\n",
787 MCHP_PDMC_ISR, isr, MCHP_PDMC_IMR, msr, pending);
791 if (pending & MCHP_PDMC_IR_RXUDR) {
792 dev_warn(dd->dev, "underrun detected\n");
793 regmap_write(dd->regmap, MCHP_PDMC_IDR, MCHP_PDMC_IR_RXUDR);
796 if (pending & MCHP_PDMC_IR_RXOVR) {
797 dev_warn(dd->dev, "overrun detected\n");
798 regmap_write(dd->regmap, MCHP_PDMC_IDR, MCHP_PDMC_IR_RXOVR);
805 /* regmap configuration */
806 static bool mchp_pdmc_readable_reg(struct device *dev, unsigned int reg)
821 static bool mchp_pdmc_writeable_reg(struct device *dev, unsigned int reg)
835 static bool mchp_pdmc_volatile_reg(struct device *dev, unsigned int reg)
846 static bool mchp_pdmc_precious_reg(struct device *dev, unsigned int reg)
857 static const struct regmap_config mchp_pdmc_regmap_config = {
861 .max_register = MCHP_PDMC_VER,
862 .readable_reg = mchp_pdmc_readable_reg,
863 .writeable_reg = mchp_pdmc_writeable_reg,
864 .precious_reg = mchp_pdmc_precious_reg,
865 .volatile_reg = mchp_pdmc_volatile_reg,
866 .cache_type = REGCACHE_FLAT,
869 static int mchp_pdmc_dt_init(struct mchp_pdmc *dd)
871 struct device_node *np = dd->dev->of_node;
872 bool mic_ch[MCHP_PDMC_DS_NO][MCHP_PDMC_EDGE_NO] = {0};
877 dev_err(dd->dev, "device node not found\n");
881 dd->mic_no = of_property_count_u32_elems(np, "microchip,mic-pos");
882 if (dd->mic_no < 0) {
883 dev_err(dd->dev, "failed to get microchip,mic-pos: %d",
887 if (!dd->mic_no || dd->mic_no % 2 ||
888 dd->mic_no / 2 > MCHP_PDMC_MAX_CHANNELS) {
889 dev_err(dd->dev, "invalid array length for microchip,mic-pos: %d",
896 dev_info(dd->dev, "%d PDM microphones declared\n", dd->mic_no);
899 * by default, we consider the order of microphones in
900 * microchip,mic-pos to be the same with the channel mapping;
901 * 1st microphone channel 0, 2nd microphone channel 1, etc.
903 for (i = 0; i < dd->mic_no; i++) {
907 ret = of_property_read_u32_index(np, "microchip,mic-pos", i * 2,
911 "failed to get value no %d value from microchip,mic-pos: %d",
915 if (ds >= MCHP_PDMC_DS_NO) {
917 "invalid DS index in microchip,mic-pos array: %d",
922 ret = of_property_read_u32_index(np, "microchip,mic-pos", i * 2 + 1,
926 "failed to get value no %d value from microchip,mic-pos: %d",
931 if (edge != MCHP_PDMC_CLK_POSITIVE &&
932 edge != MCHP_PDMC_CLK_NEGATIVE) {
934 "invalid edge in microchip,mic-pos array: %d", edge);
937 if (mic_ch[ds][edge]) {
939 "duplicated mic (DS %d, edge %d) in microchip,mic-pos array",
943 mic_ch[ds][edge] = true;
944 dd->channel_mic_map[i].ds_pos = ds;
945 dd->channel_mic_map[i].clk_edge = edge;
948 dd->startup_delay_us = 150000;
949 of_property_read_u32(np, "microchip,startup-delay-us", &dd->startup_delay_us);
954 /* used to clean the channel index found on RHR's MSB */
955 static int mchp_pdmc_process(struct snd_pcm_substream *substream,
956 int channel, unsigned long hwoff,
959 struct snd_pcm_runtime *runtime = substream->runtime;
960 u8 *dma_ptr = runtime->dma_area + hwoff +
961 channel * (runtime->dma_bytes / runtime->channels);
962 u8 *dma_ptr_end = dma_ptr + bytes;
963 unsigned int sample_size = samples_to_bytes(runtime, 1);
965 for (; dma_ptr < dma_ptr_end; dma_ptr += sample_size)
971 static struct snd_dmaengine_pcm_config mchp_pdmc_config = {
972 .process = mchp_pdmc_process,
973 .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
976 static int mchp_pdmc_runtime_suspend(struct device *dev)
978 struct mchp_pdmc *dd = dev_get_drvdata(dev);
980 regcache_cache_only(dd->regmap, true);
982 clk_disable_unprepare(dd->gclk);
983 clk_disable_unprepare(dd->pclk);
988 static int mchp_pdmc_runtime_resume(struct device *dev)
990 struct mchp_pdmc *dd = dev_get_drvdata(dev);
993 ret = clk_prepare_enable(dd->pclk);
996 "failed to enable the peripheral clock: %d\n", ret);
999 ret = clk_prepare_enable(dd->gclk);
1002 "failed to enable generic clock: %d\n", ret);
1006 regcache_cache_only(dd->regmap, false);
1007 regcache_mark_dirty(dd->regmap);
1008 ret = regcache_sync(dd->regmap);
1010 regcache_cache_only(dd->regmap, true);
1011 clk_disable_unprepare(dd->gclk);
1013 clk_disable_unprepare(dd->pclk);
1019 static int mchp_pdmc_probe(struct platform_device *pdev)
1021 struct device *dev = &pdev->dev;
1022 struct mchp_pdmc *dd;
1023 struct resource *res;
1024 void __iomem *io_base;
1029 dd = devm_kzalloc(dev, sizeof(*dd), GFP_KERNEL);
1033 dd->dev = &pdev->dev;
1034 ret = mchp_pdmc_dt_init(dd);
1038 irq = platform_get_irq(pdev, 0);
1042 dd->pclk = devm_clk_get(dev, "pclk");
1043 if (IS_ERR(dd->pclk)) {
1044 ret = PTR_ERR(dd->pclk);
1045 dev_err(dev, "failed to get peripheral clock: %d\n", ret);
1049 dd->gclk = devm_clk_get(dev, "gclk");
1050 if (IS_ERR(dd->gclk)) {
1051 ret = PTR_ERR(dd->gclk);
1052 dev_err(dev, "failed to get GCK: %d\n", ret);
1056 io_base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
1057 if (IS_ERR(io_base)) {
1058 ret = PTR_ERR(io_base);
1059 dev_err(dev, "failed to remap register memory: %d\n", ret);
1063 dd->regmap = devm_regmap_init_mmio(dev, io_base,
1064 &mchp_pdmc_regmap_config);
1065 if (IS_ERR(dd->regmap)) {
1066 ret = PTR_ERR(dd->regmap);
1067 dev_err(dev, "failed to init register map: %d\n", ret);
1071 ret = devm_request_irq(dev, irq, mchp_pdmc_interrupt, 0,
1072 dev_name(&pdev->dev), dd);
1074 dev_err(dev, "can't register ISR for IRQ %u (ret=%i)\n",
1079 /* by default audio filter is enabled and the SINC Filter order
1080 * will be set to the recommended value, 3
1082 dd->audio_filter_en = true;
1085 dd->addr.addr = (dma_addr_t)res->start + MCHP_PDMC_RHR;
1086 platform_set_drvdata(pdev, dd);
1088 pm_runtime_enable(dd->dev);
1089 if (!pm_runtime_enabled(dd->dev)) {
1090 ret = mchp_pdmc_runtime_resume(dd->dev);
1095 /* register platform */
1096 ret = devm_snd_dmaengine_pcm_register(dev, &mchp_pdmc_config, 0);
1098 dev_err(dev, "could not register platform: %d\n", ret);
1099 goto pm_runtime_suspend;
1102 ret = devm_snd_soc_register_component(dev, &mchp_pdmc_dai_component,
1105 dev_err(dev, "could not register CPU DAI: %d\n", ret);
1106 goto pm_runtime_suspend;
1109 /* print IP version */
1110 regmap_read(dd->regmap, MCHP_PDMC_VER, &version);
1111 dev_info(dd->dev, "hw version: %#lx\n",
1112 version & MCHP_PDMC_VER_VERSION);
1117 if (!pm_runtime_status_suspended(dd->dev))
1118 mchp_pdmc_runtime_suspend(dd->dev);
1119 pm_runtime_disable(dd->dev);
1124 static void mchp_pdmc_remove(struct platform_device *pdev)
1126 struct mchp_pdmc *dd = platform_get_drvdata(pdev);
1128 if (!pm_runtime_status_suspended(dd->dev))
1129 mchp_pdmc_runtime_suspend(dd->dev);
1131 pm_runtime_disable(dd->dev);
1134 static const struct of_device_id mchp_pdmc_of_match[] = {
1136 .compatible = "microchip,sama7g5-pdmc",
1141 MODULE_DEVICE_TABLE(of, mchp_pdmc_of_match);
1143 static const struct dev_pm_ops mchp_pdmc_pm_ops = {
1144 SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume)
1145 RUNTIME_PM_OPS(mchp_pdmc_runtime_suspend, mchp_pdmc_runtime_resume,
1149 static struct platform_driver mchp_pdmc_driver = {
1151 .name = "mchp-pdmc",
1152 .of_match_table = of_match_ptr(mchp_pdmc_of_match),
1153 .pm = pm_ptr(&mchp_pdmc_pm_ops),
1155 .probe = mchp_pdmc_probe,
1156 .remove_new = mchp_pdmc_remove,
1158 module_platform_driver(mchp_pdmc_driver);
1160 MODULE_DESCRIPTION("Microchip PDMC driver under ALSA SoC architecture");
1161 MODULE_AUTHOR("Codrin Ciubotariu <codrin.ciubotariu@microchip.com>");
1162 MODULE_LICENSE("GPL v2");