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/clk.h>
12 #include <linux/module.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/regmap.h>
17 #include <sound/core.h>
18 #include <sound/dmaengine_pcm.h>
19 #include <sound/pcm_params.h>
20 #include <sound/tlv.h>
23 * ---- PDMC Register map ----
25 #define MCHP_PDMC_CR 0x00 /* Control Register */
26 #define MCHP_PDMC_MR 0x04 /* Mode Register */
27 #define MCHP_PDMC_CFGR 0x08 /* Configuration Register */
28 #define MCHP_PDMC_RHR 0x0C /* Receive Holding Register */
29 #define MCHP_PDMC_IER 0x14 /* Interrupt Enable Register */
30 #define MCHP_PDMC_IDR 0x18 /* Interrupt Disable Register */
31 #define MCHP_PDMC_IMR 0x1C /* Interrupt Mask Register */
32 #define MCHP_PDMC_ISR 0x20 /* Interrupt Status Register */
33 #define MCHP_PDMC_VER 0x50 /* Version Register */
36 * ---- Control Register (Write-only) ----
38 #define MCHP_PDMC_CR_SWRST BIT(0) /* Software Reset */
41 * ---- Mode Register (Read/Write) ----
43 #define MCHP_PDMC_MR_PDMCEN_MASK GENMASK(3, 0)
44 #define MCHP_PDMC_MR_PDMCEN(ch) (BIT(ch) & MCHP_PDMC_MR_PDMCEN_MASK)
46 #define MCHP_PDMC_MR_OSR_MASK GENMASK(17, 16)
47 #define MCHP_PDMC_MR_OSR64 (1 << 16)
48 #define MCHP_PDMC_MR_OSR128 (2 << 16)
49 #define MCHP_PDMC_MR_OSR256 (3 << 16)
51 #define MCHP_PDMC_MR_SINCORDER_MASK GENMASK(23, 20)
52 #define MCHP_PDMC_MR_SINCORDER(order) (((order) << 20) & \
53 MCHP_PDMC_MR_SINCORDER_MASK)
55 #define MCHP_PDMC_MR_SINC_OSR_MASK GENMASK(27, 24)
56 #define MCHP_PDMC_MR_SINC_OSR_DIS (0 << 24)
57 #define MCHP_PDMC_MR_SINC_OSR_8 (1 << 24)
58 #define MCHP_PDMC_MR_SINC_OSR_16 (2 << 24)
59 #define MCHP_PDMC_MR_SINC_OSR_32 (3 << 24)
60 #define MCHP_PDMC_MR_SINC_OSR_64 (4 << 24)
61 #define MCHP_PDMC_MR_SINC_OSR_128 (5 << 24)
62 #define MCHP_PDMC_MR_SINC_OSR_256 (6 << 24)
64 #define MCHP_PDMC_MR_CHUNK_MASK GENMASK(31, 28)
65 #define MCHP_PDMC_MR_CHUNK(chunk) (((chunk) << 28) & \
66 MCHP_PDMC_MR_CHUNK_MASK)
69 * ---- Configuration Register (Read/Write) ----
71 #define MCHP_PDMC_CFGR_BSSEL_MASK (BIT(0) | BIT(2) | BIT(4) | BIT(6))
72 #define MCHP_PDMC_CFGR_BSSEL(ch) BIT((ch) * 2)
74 #define MCHP_PDMC_CFGR_PDMSEL_MASK (BIT(16) | BIT(18) | BIT(20) | BIT(22))
75 #define MCHP_PDMC_CFGR_PDMSEL(ch) BIT((ch) * 2 + 16)
78 * ---- Interrupt Enable/Disable/Mask/Status Registers ----
80 #define MCHP_PDMC_IR_RXRDY BIT(0)
81 #define MCHP_PDMC_IR_RXEMPTY BIT(1)
82 #define MCHP_PDMC_IR_RXFULL BIT(2)
83 #define MCHP_PDMC_IR_RXCHUNK BIT(3)
84 #define MCHP_PDMC_IR_RXUDR BIT(4)
85 #define MCHP_PDMC_IR_RXOVR BIT(5)
88 * ---- Version Register (Read-only) ----
90 #define MCHP_PDMC_VER_VERSION GENMASK(11, 0)
92 #define MCHP_PDMC_MAX_CHANNELS 4
93 #define MCHP_PDMC_DS_NO 2
94 #define MCHP_PDMC_EDGE_NO 2
101 struct mchp_pdmc_chmap {
102 struct snd_pcm_chmap_elem *chmap;
103 struct mchp_pdmc *dd;
105 struct snd_kcontrol *kctl;
109 struct mic_map channel_mic_map[MCHP_PDMC_MAX_CHANNELS];
111 struct snd_dmaengine_dai_dma_data addr;
112 struct regmap *regmap;
119 bool audio_filter_en;
122 static const char *const mchp_pdmc_sinc_filter_order_text[] = {
123 "1", "2", "3", "4", "5"
126 static const unsigned int mchp_pdmc_sinc_filter_order_values[] = {
130 static const struct soc_enum mchp_pdmc_sinc_filter_order_enum = {
131 .items = ARRAY_SIZE(mchp_pdmc_sinc_filter_order_text),
132 .texts = mchp_pdmc_sinc_filter_order_text,
133 .values = mchp_pdmc_sinc_filter_order_values,
136 static int mchp_pdmc_sinc_order_get(struct snd_kcontrol *kcontrol,
137 struct snd_ctl_elem_value *uvalue)
139 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
140 struct mchp_pdmc *dd = snd_soc_component_get_drvdata(component);
141 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
144 item = snd_soc_enum_val_to_item(e, dd->sinc_order);
145 uvalue->value.enumerated.item[0] = item;
150 static int mchp_pdmc_sinc_order_put(struct snd_kcontrol *kcontrol,
151 struct snd_ctl_elem_value *uvalue)
153 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
154 struct mchp_pdmc *dd = snd_soc_component_get_drvdata(component);
155 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
156 unsigned int *item = uvalue->value.enumerated.item;
159 if (item[0] >= e->items)
162 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
163 if (val == dd->sinc_order)
166 dd->sinc_order = val;
171 static int mchp_pdmc_af_get(struct snd_kcontrol *kcontrol,
172 struct snd_ctl_elem_value *uvalue)
174 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
175 struct mchp_pdmc *dd = snd_soc_component_get_drvdata(component);
177 uvalue->value.integer.value[0] = !!dd->audio_filter_en;
182 static int mchp_pdmc_af_put(struct snd_kcontrol *kcontrol,
183 struct snd_ctl_elem_value *uvalue)
185 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
186 struct mchp_pdmc *dd = snd_soc_component_get_drvdata(component);
187 bool af = uvalue->value.integer.value[0] ? true : false;
189 if (dd->audio_filter_en == af)
192 dd->audio_filter_en = af;
197 static int mchp_pdmc_chmap_ctl_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
199 struct mchp_pdmc_chmap *info = snd_kcontrol_chip(kcontrol);
201 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
202 uinfo->count = info->dd->mic_no;
203 uinfo->value.integer.min = 0;
204 uinfo->value.integer.max = SNDRV_CHMAP_RR; /* maxmimum 4 channels */
208 static inline struct snd_pcm_substream *
209 mchp_pdmc_chmap_substream(struct mchp_pdmc_chmap *info, unsigned int idx)
211 struct snd_pcm_substream *s;
213 for (s = info->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; s; s = s->next)
214 if (s->number == idx)
219 static struct snd_pcm_chmap_elem *mchp_pdmc_chmap_get(struct snd_pcm_substream *substream,
220 struct mchp_pdmc_chmap *ch_info)
222 struct snd_pcm_chmap_elem *map;
224 for (map = ch_info->chmap; map->channels; map++) {
225 if (map->channels == substream->runtime->channels)
231 static int mchp_pdmc_chmap_ctl_get(struct snd_kcontrol *kcontrol,
232 struct snd_ctl_elem_value *ucontrol)
234 struct mchp_pdmc_chmap *info = snd_kcontrol_chip(kcontrol);
235 struct mchp_pdmc *dd = info->dd;
236 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
237 struct snd_pcm_substream *substream;
238 const struct snd_pcm_chmap_elem *map;
244 substream = mchp_pdmc_chmap_substream(info, idx);
247 memset(ucontrol->value.integer.value, 0, sizeof(long) * info->dd->mic_no);
248 if (!substream->runtime)
249 return 0; /* no channels set */
251 map = mchp_pdmc_chmap_get(substream, info);
255 for (i = 0; i < map->channels; i++) {
256 int map_idx = map->channels == 1 ? map->map[i] - SNDRV_CHMAP_MONO :
257 map->map[i] - SNDRV_CHMAP_FL;
259 /* make sure the reported channel map is the real one, so write the map */
260 if (dd->channel_mic_map[map_idx].ds_pos)
261 cfgr_val |= MCHP_PDMC_CFGR_PDMSEL(i);
262 if (dd->channel_mic_map[map_idx].clk_edge)
263 cfgr_val |= MCHP_PDMC_CFGR_BSSEL(i);
265 ucontrol->value.integer.value[i] = map->map[i];
268 regmap_write(dd->regmap, MCHP_PDMC_CFGR, cfgr_val);
273 static int mchp_pdmc_chmap_ctl_put(struct snd_kcontrol *kcontrol,
274 struct snd_ctl_elem_value *ucontrol)
276 struct mchp_pdmc_chmap *info = snd_kcontrol_chip(kcontrol);
277 struct mchp_pdmc *dd = info->dd;
278 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
279 struct snd_pcm_substream *substream;
280 struct snd_pcm_chmap_elem *map;
286 substream = mchp_pdmc_chmap_substream(info, idx);
290 map = mchp_pdmc_chmap_get(substream, info);
294 for (i = 0; i < map->channels; i++) {
297 map->map[i] = ucontrol->value.integer.value[i];
298 map_idx = map->channels == 1 ? map->map[i] - SNDRV_CHMAP_MONO :
299 map->map[i] - SNDRV_CHMAP_FL;
301 /* configure IP for the desired channel map */
302 if (dd->channel_mic_map[map_idx].ds_pos)
303 cfgr_val |= MCHP_PDMC_CFGR_PDMSEL(i);
304 if (dd->channel_mic_map[map_idx].clk_edge)
305 cfgr_val |= MCHP_PDMC_CFGR_BSSEL(i);
308 regmap_write(dd->regmap, MCHP_PDMC_CFGR, cfgr_val);
313 static void mchp_pdmc_chmap_ctl_private_free(struct snd_kcontrol *kcontrol)
315 struct mchp_pdmc_chmap *info = snd_kcontrol_chip(kcontrol);
317 info->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].chmap_kctl = NULL;
321 static int mchp_pdmc_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
322 unsigned int size, unsigned int __user *tlv)
324 struct mchp_pdmc_chmap *info = snd_kcontrol_chip(kcontrol);
325 const struct snd_pcm_chmap_elem *map;
326 unsigned int __user *dst;
333 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
337 for (map = info->chmap; map->channels; map++) {
338 int chs_bytes = map->channels * 4;
342 if (put_user(SNDRV_CTL_TLVT_CHMAP_VAR, dst) ||
343 put_user(chs_bytes, dst + 1))
348 if (size < chs_bytes)
352 for (c = 0; c < map->channels; c++) {
353 if (put_user(map->map[c], dst))
358 if (put_user(count, tlv + 1))
363 static const struct snd_kcontrol_new mchp_pdmc_snd_controls[] = {
364 SOC_SINGLE_BOOL_EXT("Audio Filter", 0, &mchp_pdmc_af_get, &mchp_pdmc_af_put),
366 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
367 .name = "SINC Filter Order",
368 .info = snd_soc_info_enum_double,
369 .get = mchp_pdmc_sinc_order_get,
370 .put = mchp_pdmc_sinc_order_put,
371 .private_value = (unsigned long)&mchp_pdmc_sinc_filter_order_enum,
375 static int mchp_pdmc_close(struct snd_soc_component *component,
376 struct snd_pcm_substream *substream)
378 return snd_soc_add_component_controls(component, mchp_pdmc_snd_controls,
379 ARRAY_SIZE(mchp_pdmc_snd_controls));
382 static int mchp_pdmc_open(struct snd_soc_component *component,
383 struct snd_pcm_substream *substream)
387 /* remove controls that can't be changed at runtime */
388 for (i = 0; i < ARRAY_SIZE(mchp_pdmc_snd_controls); i++) {
389 const struct snd_kcontrol_new *control = &mchp_pdmc_snd_controls[i];
390 struct snd_ctl_elem_id id;
391 struct snd_kcontrol *kctl;
394 if (component->name_prefix)
395 snprintf(id.name, sizeof(id.name), "%s %s", component->name_prefix,
398 strscpy(id.name, control->name, sizeof(id.name));
401 id.iface = control->iface;
402 id.device = control->device;
403 id.subdevice = control->subdevice;
404 id.index = control->index;
405 kctl = snd_ctl_find_id(component->card->snd_card, &id);
407 dev_err(component->dev, "Failed to find %s\n", control->name);
410 err = snd_ctl_remove(component->card->snd_card, kctl);
412 dev_err(component->dev, "%d: Failed to remove %s\n", err,
421 static const struct snd_soc_component_driver mchp_pdmc_dai_component = {
423 .controls = mchp_pdmc_snd_controls,
424 .num_controls = ARRAY_SIZE(mchp_pdmc_snd_controls),
425 .open = &mchp_pdmc_open,
426 .close = &mchp_pdmc_close,
427 .legacy_dai_naming = 1,
430 static const unsigned int mchp_pdmc_1mic[] = {1};
431 static const unsigned int mchp_pdmc_2mic[] = {1, 2};
432 static const unsigned int mchp_pdmc_3mic[] = {1, 2, 3};
433 static const unsigned int mchp_pdmc_4mic[] = {1, 2, 3, 4};
435 static const struct snd_pcm_hw_constraint_list mchp_pdmc_chan_constr[] = {
437 .list = mchp_pdmc_1mic,
438 .count = ARRAY_SIZE(mchp_pdmc_1mic),
441 .list = mchp_pdmc_2mic,
442 .count = ARRAY_SIZE(mchp_pdmc_2mic),
445 .list = mchp_pdmc_3mic,
446 .count = ARRAY_SIZE(mchp_pdmc_3mic),
449 .list = mchp_pdmc_4mic,
450 .count = ARRAY_SIZE(mchp_pdmc_4mic),
454 static int mchp_pdmc_startup(struct snd_pcm_substream *substream,
455 struct snd_soc_dai *dai)
457 struct mchp_pdmc *dd = snd_soc_dai_get_drvdata(dai);
459 regmap_write(dd->regmap, MCHP_PDMC_CR, MCHP_PDMC_CR_SWRST);
461 snd_pcm_hw_constraint_list(substream->runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
462 &mchp_pdmc_chan_constr[dd->mic_no - 1]);
467 static int mchp_pdmc_dai_probe(struct snd_soc_dai *dai)
469 struct mchp_pdmc *dd = snd_soc_dai_get_drvdata(dai);
471 snd_soc_dai_init_dma_data(dai, NULL, &dd->addr);
476 static int mchp_pdmc_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
478 unsigned int fmt_master = fmt & SND_SOC_DAIFMT_MASTER_MASK;
479 unsigned int fmt_format = fmt & SND_SOC_DAIFMT_FORMAT_MASK;
481 /* IP needs to be bitclock master */
482 if (fmt_master != SND_SOC_DAIFMT_BP_FP &&
483 fmt_master != SND_SOC_DAIFMT_BP_FC)
486 /* IP supports only PDM interface */
487 if (fmt_format != SND_SOC_DAIFMT_PDM)
493 static u32 mchp_pdmc_mr_set_osr(int audio_filter_en, unsigned int osr)
495 if (audio_filter_en) {
498 return MCHP_PDMC_MR_OSR64;
500 return MCHP_PDMC_MR_OSR128;
502 return MCHP_PDMC_MR_OSR256;
507 return MCHP_PDMC_MR_SINC_OSR_8;
509 return MCHP_PDMC_MR_SINC_OSR_16;
511 return MCHP_PDMC_MR_SINC_OSR_32;
513 return MCHP_PDMC_MR_SINC_OSR_64;
515 return MCHP_PDMC_MR_SINC_OSR_128;
517 return MCHP_PDMC_MR_SINC_OSR_256;
523 static inline int mchp_pdmc_period_to_maxburst(int period_size)
525 if (!(period_size % 8))
527 if (!(period_size % 4))
529 if (!(period_size % 2))
534 static struct snd_pcm_chmap_elem mchp_pdmc_std_chmaps[] = {
536 .map = { SNDRV_CHMAP_MONO } },
538 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
540 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
543 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
544 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
548 static int mchp_pdmc_hw_params(struct snd_pcm_substream *substream,
549 struct snd_pcm_hw_params *params,
550 struct snd_soc_dai *dai)
552 struct mchp_pdmc *dd = snd_soc_dai_get_drvdata(dai);
553 struct snd_soc_component *comp = dai->component;
554 unsigned long gclk_rate = 0;
555 unsigned long best_diff_rate = ~0UL;
556 unsigned int channels = params_channels(params);
557 unsigned int osr = 0, osr_start;
558 unsigned int fs = params_rate(params);
564 dev_dbg(comp->dev, "%s() rate=%u format=%#x width=%u channels=%u\n",
565 __func__, params_rate(params), params_format(params),
566 params_width(params), params_channels(params));
568 if (channels > dd->mic_no) {
569 dev_err(comp->dev, "more channels %u than microphones %d\n",
570 channels, dd->mic_no);
575 for (i = 0; i < channels; i++) {
576 dd->pdmcen |= MCHP_PDMC_MR_PDMCEN(i);
577 if (dd->channel_mic_map[i].ds_pos)
578 cfgr_val |= MCHP_PDMC_CFGR_PDMSEL(i);
579 if (dd->channel_mic_map[i].clk_edge)
580 cfgr_val |= MCHP_PDMC_CFGR_BSSEL(i);
583 for (osr_start = dd->audio_filter_en ? 64 : 8;
584 osr_start <= 256 && best_diff_rate; osr_start *= 2) {
586 unsigned long diff_rate;
588 round_rate = clk_round_rate(dd->gclk,
589 (unsigned long)fs * 16 * osr_start);
592 diff_rate = abs((fs * 16 * osr_start) - round_rate);
593 if (diff_rate < best_diff_rate) {
594 best_diff_rate = diff_rate;
596 gclk_rate = fs * 16 * osr;
600 dev_err(comp->dev, "invalid sampling rate: %u\n", fs);
604 /* CLK is enabled by runtime PM. */
605 clk_disable_unprepare(dd->gclk);
608 ret = clk_set_rate(dd->gclk, gclk_rate);
609 clk_prepare_enable(dd->gclk);
611 dev_err(comp->dev, "unable to set rate %lu to GCLK: %d\n",
616 mr_val |= mchp_pdmc_mr_set_osr(dd->audio_filter_en, osr);
618 mr_val |= MCHP_PDMC_MR_SINCORDER(dd->sinc_order);
620 dd->addr.maxburst = mchp_pdmc_period_to_maxburst(snd_pcm_lib_period_bytes(substream));
621 mr_val |= MCHP_PDMC_MR_CHUNK(dd->addr.maxburst);
622 dev_dbg(comp->dev, "maxburst set to %d\n", dd->addr.maxburst);
624 snd_soc_component_update_bits(comp, MCHP_PDMC_MR,
625 MCHP_PDMC_MR_OSR_MASK |
626 MCHP_PDMC_MR_SINCORDER_MASK |
627 MCHP_PDMC_MR_SINC_OSR_MASK |
628 MCHP_PDMC_MR_CHUNK_MASK, mr_val);
630 snd_soc_component_write(comp, MCHP_PDMC_CFGR, cfgr_val);
635 static int mchp_pdmc_trigger(struct snd_pcm_substream *substream,
636 int cmd, struct snd_soc_dai *dai)
638 struct mchp_pdmc *dd = snd_soc_dai_get_drvdata(dai);
639 struct snd_soc_component *cpu = dai->component;
645 case SNDRV_PCM_TRIGGER_RESUME:
646 case SNDRV_PCM_TRIGGER_START:
647 /* Enable overrun and underrun error interrupts */
648 regmap_write(dd->regmap, MCHP_PDMC_IER, dd->suspend_irq |
649 MCHP_PDMC_IR_RXOVR | MCHP_PDMC_IR_RXUDR);
652 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
653 snd_soc_component_update_bits(cpu, MCHP_PDMC_MR,
654 MCHP_PDMC_MR_PDMCEN_MASK,
657 case SNDRV_PCM_TRIGGER_SUSPEND:
658 regmap_read(dd->regmap, MCHP_PDMC_IMR, &dd->suspend_irq);
660 case SNDRV_PCM_TRIGGER_STOP:
661 /* Disable overrun and underrun error interrupts */
662 regmap_write(dd->regmap, MCHP_PDMC_IDR, dd->suspend_irq |
663 MCHP_PDMC_IR_RXOVR | MCHP_PDMC_IR_RXUDR);
665 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
666 snd_soc_component_update_bits(cpu, MCHP_PDMC_MR,
667 MCHP_PDMC_MR_PDMCEN_MASK, 0);
674 regmap_read(dd->regmap, MCHP_PDMC_MR, &val);
675 dev_dbg(dd->dev, "MR (0x%02x): 0x%08x\n", MCHP_PDMC_MR, val);
676 regmap_read(dd->regmap, MCHP_PDMC_CFGR, &val);
677 dev_dbg(dd->dev, "CFGR (0x%02x): 0x%08x\n", MCHP_PDMC_CFGR, val);
678 regmap_read(dd->regmap, MCHP_PDMC_IMR, &val);
679 dev_dbg(dd->dev, "IMR (0x%02x): 0x%08x\n", MCHP_PDMC_IMR, val);
685 static const struct snd_soc_dai_ops mchp_pdmc_dai_ops = {
686 .set_fmt = mchp_pdmc_set_fmt,
687 .startup = mchp_pdmc_startup,
688 .hw_params = mchp_pdmc_hw_params,
689 .trigger = mchp_pdmc_trigger,
692 static int mchp_pdmc_add_chmap_ctls(struct snd_pcm *pcm, struct mchp_pdmc *dd)
694 struct mchp_pdmc_chmap *info;
695 struct snd_kcontrol_new knew = {
696 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
697 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
698 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
699 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
700 .info = mchp_pdmc_chmap_ctl_info,
701 .get = mchp_pdmc_chmap_ctl_get,
702 .put = mchp_pdmc_chmap_ctl_put,
703 .tlv.c = mchp_pdmc_chmap_ctl_tlv,
707 if (WARN_ON(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].chmap_kctl))
709 info = kzalloc(sizeof(*info), GFP_KERNEL);
714 info->chmap = mchp_pdmc_std_chmaps;
715 knew.name = "Capture Channel Map";
716 knew.device = pcm->device;
717 knew.count = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
718 info->kctl = snd_ctl_new1(&knew, info);
723 info->kctl->private_free = mchp_pdmc_chmap_ctl_private_free;
724 err = snd_ctl_add(pcm->card, info->kctl);
727 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].chmap_kctl = info->kctl;
731 static int mchp_pdmc_pcm_new(struct snd_soc_pcm_runtime *rtd,
732 struct snd_soc_dai *dai)
734 struct mchp_pdmc *dd = snd_soc_dai_get_drvdata(dai);
737 ret = mchp_pdmc_add_chmap_ctls(rtd->pcm, dd);
739 dev_err(dd->dev, "failed to add channel map controls: %d\n", ret);
746 static struct snd_soc_dai_driver mchp_pdmc_dai = {
747 .probe = mchp_pdmc_dai_probe,
749 .stream_name = "Capture",
754 .rates = SNDRV_PCM_RATE_KNOT,
755 .formats = SNDRV_PCM_FMTBIT_S24_LE,
757 .ops = &mchp_pdmc_dai_ops,
758 .pcm_new = &mchp_pdmc_pcm_new,
761 /* PDMC interrupt handler */
762 static irqreturn_t mchp_pdmc_interrupt(int irq, void *dev_id)
764 struct mchp_pdmc *dd = (struct mchp_pdmc *)dev_id;
765 u32 isr, msr, pending;
766 irqreturn_t ret = IRQ_NONE;
768 regmap_read(dd->regmap, MCHP_PDMC_ISR, &isr);
769 regmap_read(dd->regmap, MCHP_PDMC_IMR, &msr);
772 dev_dbg(dd->dev, "ISR (0x%02x): 0x%08x, IMR (0x%02x): 0x%08x, pending: 0x%08x\n",
773 MCHP_PDMC_ISR, isr, MCHP_PDMC_IMR, msr, pending);
777 if (pending & MCHP_PDMC_IR_RXUDR) {
778 dev_warn(dd->dev, "underrun detected\n");
779 regmap_write(dd->regmap, MCHP_PDMC_IDR, MCHP_PDMC_IR_RXUDR);
782 if (pending & MCHP_PDMC_IR_RXOVR) {
783 dev_warn(dd->dev, "overrun detected\n");
784 regmap_write(dd->regmap, MCHP_PDMC_IDR, MCHP_PDMC_IR_RXOVR);
791 /* regmap configuration */
792 static bool mchp_pdmc_readable_reg(struct device *dev, unsigned int reg)
806 static bool mchp_pdmc_writeable_reg(struct device *dev, unsigned int reg)
820 static bool mchp_pdmc_precious_reg(struct device *dev, unsigned int reg)
831 static const struct regmap_config mchp_pdmc_regmap_config = {
835 .max_register = MCHP_PDMC_VER,
836 .readable_reg = mchp_pdmc_readable_reg,
837 .writeable_reg = mchp_pdmc_writeable_reg,
838 .precious_reg = mchp_pdmc_precious_reg,
839 .cache_type = REGCACHE_FLAT,
842 static int mchp_pdmc_dt_init(struct mchp_pdmc *dd)
844 struct device_node *np = dd->dev->of_node;
845 bool mic_ch[MCHP_PDMC_DS_NO][MCHP_PDMC_EDGE_NO] = {0};
850 dev_err(dd->dev, "device node not found\n");
854 dd->mic_no = of_property_count_u32_elems(np, "microchip,mic-pos");
855 if (dd->mic_no < 0) {
856 dev_err(dd->dev, "failed to get microchip,mic-pos: %d",
860 if (!dd->mic_no || dd->mic_no % 2 ||
861 dd->mic_no / 2 > MCHP_PDMC_MAX_CHANNELS) {
862 dev_err(dd->dev, "invalid array length for microchip,mic-pos: %d",
869 dev_info(dd->dev, "%d PDM microphones declared\n", dd->mic_no);
872 * by default, we consider the order of microphones in
873 * microchip,mic-pos to be the same with the channel mapping;
874 * 1st microphone channel 0, 2nd microphone channel 1, etc.
876 for (i = 0; i < dd->mic_no; i++) {
880 ret = of_property_read_u32_index(np, "microchip,mic-pos", i * 2,
884 "failed to get value no %d value from microchip,mic-pos: %d",
888 if (ds >= MCHP_PDMC_DS_NO) {
890 "invalid DS index in microchip,mic-pos array: %d",
895 ret = of_property_read_u32_index(np, "microchip,mic-pos", i * 2 + 1,
899 "failed to get value no %d value from microchip,mic-pos: %d",
904 if (edge != MCHP_PDMC_CLK_POSITIVE &&
905 edge != MCHP_PDMC_CLK_NEGATIVE) {
907 "invalid edge in microchip,mic-pos array: %d", edge);
910 if (mic_ch[ds][edge]) {
912 "duplicated mic (DS %d, edge %d) in microchip,mic-pos array",
916 mic_ch[ds][edge] = true;
917 dd->channel_mic_map[i].ds_pos = ds;
918 dd->channel_mic_map[i].clk_edge = edge;
924 /* used to clean the channel index found on RHR's MSB */
925 static int mchp_pdmc_process(struct snd_pcm_substream *substream,
926 int channel, unsigned long hwoff,
927 void *buf, unsigned long bytes)
929 struct snd_pcm_runtime *runtime = substream->runtime;
930 u8 *dma_ptr = runtime->dma_area + hwoff +
931 channel * (runtime->dma_bytes / runtime->channels);
932 u8 *dma_ptr_end = dma_ptr + bytes;
933 unsigned int sample_size = samples_to_bytes(runtime, 1);
935 for (; dma_ptr < dma_ptr_end; dma_ptr += sample_size)
941 static struct snd_dmaengine_pcm_config mchp_pdmc_config = {
942 .process = mchp_pdmc_process,
943 .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
946 static int mchp_pdmc_runtime_suspend(struct device *dev)
948 struct mchp_pdmc *dd = dev_get_drvdata(dev);
950 regcache_cache_only(dd->regmap, true);
952 clk_disable_unprepare(dd->gclk);
953 clk_disable_unprepare(dd->pclk);
958 static int mchp_pdmc_runtime_resume(struct device *dev)
960 struct mchp_pdmc *dd = dev_get_drvdata(dev);
963 ret = clk_prepare_enable(dd->pclk);
966 "failed to enable the peripheral clock: %d\n", ret);
969 ret = clk_prepare_enable(dd->gclk);
972 "failed to enable generic clock: %d\n", ret);
976 regcache_cache_only(dd->regmap, false);
977 regcache_mark_dirty(dd->regmap);
978 ret = regcache_sync(dd->regmap);
980 regcache_cache_only(dd->regmap, true);
981 clk_disable_unprepare(dd->gclk);
983 clk_disable_unprepare(dd->pclk);
989 static int mchp_pdmc_probe(struct platform_device *pdev)
991 struct device *dev = &pdev->dev;
992 struct mchp_pdmc *dd;
993 struct resource *res;
994 void __iomem *io_base;
999 dd = devm_kzalloc(dev, sizeof(*dd), GFP_KERNEL);
1003 dd->dev = &pdev->dev;
1004 ret = mchp_pdmc_dt_init(dd);
1008 irq = platform_get_irq(pdev, 0);
1012 dd->pclk = devm_clk_get(dev, "pclk");
1013 if (IS_ERR(dd->pclk)) {
1014 ret = PTR_ERR(dd->pclk);
1015 dev_err(dev, "failed to get peripheral clock: %d\n", ret);
1019 dd->gclk = devm_clk_get(dev, "gclk");
1020 if (IS_ERR(dd->gclk)) {
1021 ret = PTR_ERR(dd->gclk);
1022 dev_err(dev, "failed to get GCK: %d\n", ret);
1026 io_base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
1027 if (IS_ERR(io_base)) {
1028 ret = PTR_ERR(io_base);
1029 dev_err(dev, "failed to remap register memory: %d\n", ret);
1033 dd->regmap = devm_regmap_init_mmio(dev, io_base,
1034 &mchp_pdmc_regmap_config);
1035 if (IS_ERR(dd->regmap)) {
1036 ret = PTR_ERR(dd->regmap);
1037 dev_err(dev, "failed to init register map: %d\n", ret);
1041 ret = devm_request_irq(dev, irq, mchp_pdmc_interrupt, 0,
1042 dev_name(&pdev->dev), (void *)dd);
1044 dev_err(dev, "can't register ISR for IRQ %u (ret=%i)\n",
1049 /* by default audio filter is enabled and the SINC Filter order
1050 * will be set to the recommended value, 3
1052 dd->audio_filter_en = true;
1055 dd->addr.addr = (dma_addr_t)res->start + MCHP_PDMC_RHR;
1056 platform_set_drvdata(pdev, dd);
1058 pm_runtime_enable(dd->dev);
1059 if (!pm_runtime_enabled(dd->dev)) {
1060 ret = mchp_pdmc_runtime_resume(dd->dev);
1065 /* register platform */
1066 ret = devm_snd_dmaengine_pcm_register(dev, &mchp_pdmc_config, 0);
1068 dev_err(dev, "could not register platform: %d\n", ret);
1069 goto pm_runtime_suspend;
1072 ret = devm_snd_soc_register_component(dev, &mchp_pdmc_dai_component,
1075 dev_err(dev, "could not register CPU DAI: %d\n", ret);
1076 goto pm_runtime_suspend;
1079 /* print IP version */
1080 regmap_read(dd->regmap, MCHP_PDMC_VER, &version);
1081 dev_info(dd->dev, "hw version: %#lx\n",
1082 version & MCHP_PDMC_VER_VERSION);
1087 if (!pm_runtime_status_suspended(dd->dev))
1088 mchp_pdmc_runtime_suspend(dd->dev);
1089 pm_runtime_disable(dd->dev);
1094 static int mchp_pdmc_remove(struct platform_device *pdev)
1096 struct mchp_pdmc *dd = platform_get_drvdata(pdev);
1098 if (!pm_runtime_status_suspended(dd->dev))
1099 mchp_pdmc_runtime_suspend(dd->dev);
1101 pm_runtime_disable(dd->dev);
1106 static const struct of_device_id mchp_pdmc_of_match[] = {
1108 .compatible = "microchip,sama7g5-pdmc",
1113 MODULE_DEVICE_TABLE(of, mchp_pdmc_of_match);
1115 static const struct dev_pm_ops mchp_pdmc_pm_ops = {
1116 SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume)
1117 RUNTIME_PM_OPS(mchp_pdmc_runtime_suspend, mchp_pdmc_runtime_resume,
1121 static struct platform_driver mchp_pdmc_driver = {
1123 .name = "mchp-pdmc",
1124 .of_match_table = of_match_ptr(mchp_pdmc_of_match),
1125 .pm = pm_ptr(&mchp_pdmc_pm_ops),
1127 .probe = mchp_pdmc_probe,
1128 .remove = mchp_pdmc_remove,
1130 module_platform_driver(mchp_pdmc_driver);
1132 MODULE_DESCRIPTION("Microchip PDMC driver under ALSA SoC architecture");
1133 MODULE_AUTHOR("Codrin Ciubotariu <codrin.ciubotariu@microchip.com>");
1134 MODULE_LICENSE("GPL v2");