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
94 * ---- DMA chunk size allowed ----
96 #define MCHP_PDMC_DMA_8_WORD_CHUNK 8
97 #define MCHP_PDMC_DMA_4_WORD_CHUNK 4
98 #define MCHP_PDMC_DMA_2_WORD_CHUNK 2
99 #define MCHP_PDMC_DMA_1_WORD_CHUNK 1
100 #define DMA_BURST_ALIGNED(_p, _s, _w) !(_p % (_s * _w))
107 struct mchp_pdmc_chmap
{
108 struct snd_pcm_chmap_elem
*chmap
;
109 struct mchp_pdmc
*dd
;
111 struct snd_kcontrol
*kctl
;
115 struct mic_map channel_mic_map
[MCHP_PDMC_MAX_CHANNELS
];
117 struct snd_dmaengine_dai_dma_data addr
;
118 struct regmap
*regmap
;
123 u32 startup_delay_us
;
126 bool audio_filter_en
;
127 atomic_t busy_stream
;
130 static const char *const mchp_pdmc_sinc_filter_order_text
[] = {
131 "1", "2", "3", "4", "5"
134 static const unsigned int mchp_pdmc_sinc_filter_order_values
[] = {
138 static const struct soc_enum mchp_pdmc_sinc_filter_order_enum
= {
139 .items
= ARRAY_SIZE(mchp_pdmc_sinc_filter_order_text
),
140 .texts
= mchp_pdmc_sinc_filter_order_text
,
141 .values
= mchp_pdmc_sinc_filter_order_values
,
144 static int mchp_pdmc_sinc_order_get(struct snd_kcontrol
*kcontrol
,
145 struct snd_ctl_elem_value
*uvalue
)
147 struct snd_soc_component
*component
= snd_kcontrol_chip(kcontrol
);
148 struct mchp_pdmc
*dd
= snd_soc_component_get_drvdata(component
);
149 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
152 item
= snd_soc_enum_val_to_item(e
, dd
->sinc_order
);
153 uvalue
->value
.enumerated
.item
[0] = item
;
158 static int mchp_pdmc_sinc_order_put(struct snd_kcontrol
*kcontrol
,
159 struct snd_ctl_elem_value
*uvalue
)
161 struct snd_soc_component
*component
= snd_kcontrol_chip(kcontrol
);
162 struct mchp_pdmc
*dd
= snd_soc_component_get_drvdata(component
);
163 struct soc_enum
*e
= (struct soc_enum
*)kcontrol
->private_value
;
164 unsigned int *item
= uvalue
->value
.enumerated
.item
;
167 if (item
[0] >= e
->items
)
170 val
= snd_soc_enum_item_to_val(e
, item
[0]) << e
->shift_l
;
172 if (atomic_read(&dd
->busy_stream
))
175 if (val
== dd
->sinc_order
)
178 dd
->sinc_order
= val
;
183 static int mchp_pdmc_af_get(struct snd_kcontrol
*kcontrol
,
184 struct snd_ctl_elem_value
*uvalue
)
186 struct snd_soc_component
*component
= snd_kcontrol_chip(kcontrol
);
187 struct mchp_pdmc
*dd
= snd_soc_component_get_drvdata(component
);
189 uvalue
->value
.integer
.value
[0] = !!dd
->audio_filter_en
;
194 static int mchp_pdmc_af_put(struct snd_kcontrol
*kcontrol
,
195 struct snd_ctl_elem_value
*uvalue
)
197 struct snd_soc_component
*component
= snd_kcontrol_chip(kcontrol
);
198 struct mchp_pdmc
*dd
= snd_soc_component_get_drvdata(component
);
199 bool af
= uvalue
->value
.integer
.value
[0] ? true : false;
201 if (atomic_read(&dd
->busy_stream
))
204 if (dd
->audio_filter_en
== af
)
207 dd
->audio_filter_en
= af
;
212 static int mchp_pdmc_chmap_ctl_info(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_info
*uinfo
)
214 struct mchp_pdmc_chmap
*info
= snd_kcontrol_chip(kcontrol
);
216 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
217 uinfo
->count
= info
->dd
->mic_no
;
218 uinfo
->value
.integer
.min
= 0;
219 uinfo
->value
.integer
.max
= SNDRV_CHMAP_RR
; /* maxmimum 4 channels */
223 static inline struct snd_pcm_substream
*
224 mchp_pdmc_chmap_substream(struct mchp_pdmc_chmap
*info
, unsigned int idx
)
226 struct snd_pcm_substream
*s
;
228 for (s
= info
->pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].substream
; s
; s
= s
->next
)
229 if (s
->number
== idx
)
234 static struct snd_pcm_chmap_elem
*mchp_pdmc_chmap_get(struct snd_pcm_substream
*substream
,
235 struct mchp_pdmc_chmap
*ch_info
)
237 struct snd_pcm_chmap_elem
*map
;
239 for (map
= ch_info
->chmap
; map
->channels
; map
++) {
240 if (map
->channels
== substream
->runtime
->channels
)
246 static int mchp_pdmc_chmap_ctl_get(struct snd_kcontrol
*kcontrol
,
247 struct snd_ctl_elem_value
*ucontrol
)
249 struct mchp_pdmc_chmap
*info
= snd_kcontrol_chip(kcontrol
);
250 struct mchp_pdmc
*dd
= info
->dd
;
251 unsigned int idx
= snd_ctl_get_ioffidx(kcontrol
, &ucontrol
->id
);
252 struct snd_pcm_substream
*substream
;
253 const struct snd_pcm_chmap_elem
*map
;
259 substream
= mchp_pdmc_chmap_substream(info
, idx
);
262 memset(ucontrol
->value
.integer
.value
, 0, sizeof(long) * info
->dd
->mic_no
);
263 if (!substream
->runtime
)
264 return 0; /* no channels set */
266 map
= mchp_pdmc_chmap_get(substream
, info
);
270 for (i
= 0; i
< map
->channels
; i
++) {
271 int map_idx
= map
->channels
== 1 ? map
->map
[i
] - SNDRV_CHMAP_MONO
:
272 map
->map
[i
] - SNDRV_CHMAP_FL
;
274 /* make sure the reported channel map is the real one, so write the map */
275 if (dd
->channel_mic_map
[map_idx
].ds_pos
)
276 cfgr_val
|= MCHP_PDMC_CFGR_PDMSEL(i
);
277 if (dd
->channel_mic_map
[map_idx
].clk_edge
)
278 cfgr_val
|= MCHP_PDMC_CFGR_BSSEL(i
);
280 ucontrol
->value
.integer
.value
[i
] = map
->map
[i
];
283 regmap_write(dd
->regmap
, MCHP_PDMC_CFGR
, cfgr_val
);
288 static int mchp_pdmc_chmap_ctl_put(struct snd_kcontrol
*kcontrol
,
289 struct snd_ctl_elem_value
*ucontrol
)
291 struct mchp_pdmc_chmap
*info
= snd_kcontrol_chip(kcontrol
);
292 struct mchp_pdmc
*dd
= info
->dd
;
293 unsigned int idx
= snd_ctl_get_ioffidx(kcontrol
, &ucontrol
->id
);
294 struct snd_pcm_substream
*substream
;
295 struct snd_pcm_chmap_elem
*map
;
301 substream
= mchp_pdmc_chmap_substream(info
, idx
);
305 if (!substream
->runtime
)
306 return 0; /* just for avoiding error from alsactl restore */
308 map
= mchp_pdmc_chmap_get(substream
, info
);
312 for (i
= 0; i
< map
->channels
; i
++) {
315 map
->map
[i
] = ucontrol
->value
.integer
.value
[i
];
316 map_idx
= map
->channels
== 1 ? map
->map
[i
] - SNDRV_CHMAP_MONO
:
317 map
->map
[i
] - SNDRV_CHMAP_FL
;
319 /* configure IP for the desired channel map */
320 if (dd
->channel_mic_map
[map_idx
].ds_pos
)
321 cfgr_val
|= MCHP_PDMC_CFGR_PDMSEL(i
);
322 if (dd
->channel_mic_map
[map_idx
].clk_edge
)
323 cfgr_val
|= MCHP_PDMC_CFGR_BSSEL(i
);
326 regmap_write(dd
->regmap
, MCHP_PDMC_CFGR
, cfgr_val
);
331 static void mchp_pdmc_chmap_ctl_private_free(struct snd_kcontrol
*kcontrol
)
333 struct mchp_pdmc_chmap
*info
= snd_kcontrol_chip(kcontrol
);
335 info
->pcm
->streams
[SNDRV_PCM_STREAM_CAPTURE
].chmap_kctl
= NULL
;
339 static int mchp_pdmc_chmap_ctl_tlv(struct snd_kcontrol
*kcontrol
, int op_flag
,
340 unsigned int size
, unsigned int __user
*tlv
)
342 struct mchp_pdmc_chmap
*info
= snd_kcontrol_chip(kcontrol
);
343 const struct snd_pcm_chmap_elem
*map
;
344 unsigned int __user
*dst
;
351 if (put_user(SNDRV_CTL_TLVT_CONTAINER
, tlv
))
355 for (map
= info
->chmap
; map
->channels
; map
++) {
356 int chs_bytes
= map
->channels
* 4;
360 if (put_user(SNDRV_CTL_TLVT_CHMAP_VAR
, dst
) ||
361 put_user(chs_bytes
, dst
+ 1))
366 if (size
< chs_bytes
)
370 for (c
= 0; c
< map
->channels
; c
++) {
371 if (put_user(map
->map
[c
], dst
))
376 if (put_user(count
, tlv
+ 1))
381 static const struct snd_kcontrol_new mchp_pdmc_snd_controls
[] = {
382 SOC_SINGLE_BOOL_EXT("Audio Filter", 0, &mchp_pdmc_af_get
, &mchp_pdmc_af_put
),
384 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
385 .name
= "SINC Filter Order",
386 .info
= snd_soc_info_enum_double
,
387 .get
= mchp_pdmc_sinc_order_get
,
388 .put
= mchp_pdmc_sinc_order_put
,
389 .private_value
= (unsigned long)&mchp_pdmc_sinc_filter_order_enum
,
393 static const struct snd_soc_component_driver mchp_pdmc_dai_component
= {
395 .controls
= mchp_pdmc_snd_controls
,
396 .num_controls
= ARRAY_SIZE(mchp_pdmc_snd_controls
),
399 static const unsigned int mchp_pdmc_1mic
[] = {1};
400 static const unsigned int mchp_pdmc_2mic
[] = {1, 2};
401 static const unsigned int mchp_pdmc_3mic
[] = {1, 2, 3};
402 static const unsigned int mchp_pdmc_4mic
[] = {1, 2, 3, 4};
404 static const struct snd_pcm_hw_constraint_list mchp_pdmc_chan_constr
[] = {
406 .list
= mchp_pdmc_1mic
,
407 .count
= ARRAY_SIZE(mchp_pdmc_1mic
),
410 .list
= mchp_pdmc_2mic
,
411 .count
= ARRAY_SIZE(mchp_pdmc_2mic
),
414 .list
= mchp_pdmc_3mic
,
415 .count
= ARRAY_SIZE(mchp_pdmc_3mic
),
418 .list
= mchp_pdmc_4mic
,
419 .count
= ARRAY_SIZE(mchp_pdmc_4mic
),
423 static int mchp_pdmc_startup(struct snd_pcm_substream
*substream
,
424 struct snd_soc_dai
*dai
)
426 struct mchp_pdmc
*dd
= snd_soc_dai_get_drvdata(dai
);
428 regmap_write(dd
->regmap
, MCHP_PDMC_CR
, MCHP_PDMC_CR_SWRST
);
430 snd_pcm_hw_constraint_list(substream
->runtime
, 0, SNDRV_PCM_HW_PARAM_CHANNELS
,
431 &mchp_pdmc_chan_constr
[dd
->mic_no
- 1]);
436 static int mchp_pdmc_dai_probe(struct snd_soc_dai
*dai
)
438 struct mchp_pdmc
*dd
= snd_soc_dai_get_drvdata(dai
);
440 snd_soc_dai_init_dma_data(dai
, NULL
, &dd
->addr
);
445 static int mchp_pdmc_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
447 unsigned int fmt_master
= fmt
& SND_SOC_DAIFMT_MASTER_MASK
;
448 unsigned int fmt_format
= fmt
& SND_SOC_DAIFMT_FORMAT_MASK
;
450 /* IP needs to be bitclock master */
451 if (fmt_master
!= SND_SOC_DAIFMT_BP_FP
&&
452 fmt_master
!= SND_SOC_DAIFMT_BP_FC
)
455 /* IP supports only PDM interface */
456 if (fmt_format
!= SND_SOC_DAIFMT_PDM
)
462 static u32
mchp_pdmc_mr_set_osr(int audio_filter_en
, unsigned int osr
)
464 if (audio_filter_en
) {
467 return MCHP_PDMC_MR_OSR64
;
469 return MCHP_PDMC_MR_OSR128
;
471 return MCHP_PDMC_MR_OSR256
;
476 return MCHP_PDMC_MR_SINC_OSR_8
;
478 return MCHP_PDMC_MR_SINC_OSR_16
;
480 return MCHP_PDMC_MR_SINC_OSR_32
;
482 return MCHP_PDMC_MR_SINC_OSR_64
;
484 return MCHP_PDMC_MR_SINC_OSR_128
;
486 return MCHP_PDMC_MR_SINC_OSR_256
;
492 static inline int mchp_pdmc_period_to_maxburst(int period_size
, int sample_size
)
494 int p_size
= period_size
;
495 int s_size
= sample_size
;
497 if (DMA_BURST_ALIGNED(p_size
, s_size
, MCHP_PDMC_DMA_8_WORD_CHUNK
))
498 return MCHP_PDMC_DMA_8_WORD_CHUNK
;
499 if (DMA_BURST_ALIGNED(p_size
, s_size
, MCHP_PDMC_DMA_4_WORD_CHUNK
))
500 return MCHP_PDMC_DMA_4_WORD_CHUNK
;
501 if (DMA_BURST_ALIGNED(p_size
, s_size
, MCHP_PDMC_DMA_2_WORD_CHUNK
))
502 return MCHP_PDMC_DMA_2_WORD_CHUNK
;
503 return MCHP_PDMC_DMA_1_WORD_CHUNK
;
506 static struct snd_pcm_chmap_elem mchp_pdmc_std_chmaps
[] = {
508 .map
= { SNDRV_CHMAP_MONO
} },
510 .map
= { SNDRV_CHMAP_FL
, SNDRV_CHMAP_FR
} },
512 .map
= { SNDRV_CHMAP_FL
, SNDRV_CHMAP_FR
,
515 .map
= { SNDRV_CHMAP_FL
, SNDRV_CHMAP_FR
,
516 SNDRV_CHMAP_RL
, SNDRV_CHMAP_RR
} },
520 static int mchp_pdmc_hw_params(struct snd_pcm_substream
*substream
,
521 struct snd_pcm_hw_params
*params
,
522 struct snd_soc_dai
*dai
)
524 struct mchp_pdmc
*dd
= snd_soc_dai_get_drvdata(dai
);
525 struct snd_soc_component
*comp
= dai
->component
;
526 unsigned long gclk_rate
= 0;
527 unsigned long best_diff_rate
= ~0UL;
528 unsigned int channels
= params_channels(params
);
529 unsigned int osr
= 0, osr_start
;
530 unsigned int fs
= params_rate(params
);
531 int sample_bytes
= params_physical_width(params
) / 8;
532 int period_bytes
= params_period_size(params
) *
533 params_channels(params
) * sample_bytes
;
540 dev_dbg(comp
->dev
, "%s() rate=%u format=%#x width=%u channels=%u period_bytes=%d\n",
541 __func__
, params_rate(params
), params_format(params
),
542 params_width(params
), params_channels(params
), period_bytes
);
544 if (channels
> dd
->mic_no
) {
545 dev_err(comp
->dev
, "more channels %u than microphones %d\n",
546 channels
, dd
->mic_no
);
551 for (i
= 0; i
< channels
; i
++) {
552 dd
->pdmcen
|= MCHP_PDMC_MR_PDMCEN(i
);
553 if (dd
->channel_mic_map
[i
].ds_pos
)
554 cfgr_val
|= MCHP_PDMC_CFGR_PDMSEL(i
);
555 if (dd
->channel_mic_map
[i
].clk_edge
)
556 cfgr_val
|= MCHP_PDMC_CFGR_BSSEL(i
);
560 * from these point forward, we consider the controller busy, so the
561 * audio filter and SINC order can't be changed
563 atomic_set(&dd
->busy_stream
, 1);
564 for (osr_start
= dd
->audio_filter_en
? 64 : 8;
565 osr_start
<= 256 && best_diff_rate
; osr_start
*= 2) {
567 unsigned long diff_rate
;
569 round_rate
= clk_round_rate(dd
->gclk
,
570 (unsigned long)fs
* 16 * osr_start
);
573 diff_rate
= abs((fs
* 16 * osr_start
) - round_rate
);
574 if (diff_rate
< best_diff_rate
) {
575 best_diff_rate
= diff_rate
;
577 gclk_rate
= fs
* 16 * osr
;
581 dev_err(comp
->dev
, "invalid sampling rate: %u\n", fs
);
585 /* CLK is enabled by runtime PM. */
586 clk_disable_unprepare(dd
->gclk
);
589 ret
= clk_set_rate(dd
->gclk
, gclk_rate
);
590 clk_prepare_enable(dd
->gclk
);
592 dev_err(comp
->dev
, "unable to set rate %lu to GCLK: %d\n",
597 mr_val
|= mchp_pdmc_mr_set_osr(dd
->audio_filter_en
, osr
);
599 mr_val
|= FIELD_PREP(MCHP_PDMC_MR_SINCORDER_MASK
, dd
->sinc_order
);
601 maxburst
= mchp_pdmc_period_to_maxburst(period_bytes
, sample_bytes
);
602 dd
->addr
.maxburst
= maxburst
;
603 mr_val
|= FIELD_PREP(MCHP_PDMC_MR_CHUNK_MASK
, dd
->addr
.maxburst
);
604 dev_dbg(comp
->dev
, "maxburst set to %d\n", dd
->addr
.maxburst
);
606 snd_soc_component_update_bits(comp
, MCHP_PDMC_MR
,
607 MCHP_PDMC_MR_OSR_MASK
|
608 MCHP_PDMC_MR_SINCORDER_MASK
|
609 MCHP_PDMC_MR_SINC_OSR_MASK
|
610 MCHP_PDMC_MR_CHUNK_MASK
, mr_val
);
612 snd_soc_component_write(comp
, MCHP_PDMC_CFGR
, cfgr_val
);
617 static void mchp_pdmc_noise_filter_workaround(struct mchp_pdmc
*dd
)
622 * PDMC doesn't wait for microphones' startup time thus the acquisition
623 * may start before the microphones are ready leading to poc noises at
624 * the beginning of capture. To avoid this, we need to wait 50ms (in
625 * normal startup procedure) or 150 ms (worst case after resume from sleep
626 * states) after microphones are enabled and then clear the FIFOs (by
627 * reading the RHR 16 times) and possible interrupts before continuing.
628 * Also, for this to work the DMA needs to be started after interrupts
631 usleep_range(dd
->startup_delay_us
, dd
->startup_delay_us
+ 5);
634 regmap_read(dd
->regmap
, MCHP_PDMC_RHR
, &tmp
);
636 /* Clear interrupts. */
637 regmap_read(dd
->regmap
, MCHP_PDMC_ISR
, &tmp
);
640 static int mchp_pdmc_trigger(struct snd_pcm_substream
*substream
,
641 int cmd
, struct snd_soc_dai
*dai
)
643 struct mchp_pdmc
*dd
= snd_soc_dai_get_drvdata(dai
);
644 struct snd_soc_component
*cpu
= dai
->component
;
650 case SNDRV_PCM_TRIGGER_RESUME
:
651 case SNDRV_PCM_TRIGGER_START
:
652 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
653 snd_soc_component_update_bits(cpu
, MCHP_PDMC_MR
,
654 MCHP_PDMC_MR_PDMCEN_MASK
,
657 mchp_pdmc_noise_filter_workaround(dd
);
659 /* Enable interrupts. */
660 regmap_write(dd
->regmap
, MCHP_PDMC_IER
, dd
->suspend_irq
|
661 MCHP_PDMC_IR_RXOVR
| MCHP_PDMC_IR_RXUDR
);
664 case SNDRV_PCM_TRIGGER_SUSPEND
:
665 regmap_read(dd
->regmap
, MCHP_PDMC_IMR
, &dd
->suspend_irq
);
667 case SNDRV_PCM_TRIGGER_STOP
:
668 /* Disable overrun and underrun error interrupts */
669 regmap_write(dd
->regmap
, MCHP_PDMC_IDR
, dd
->suspend_irq
|
670 MCHP_PDMC_IR_RXOVR
| MCHP_PDMC_IR_RXUDR
);
672 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
673 snd_soc_component_update_bits(cpu
, MCHP_PDMC_MR
,
674 MCHP_PDMC_MR_PDMCEN_MASK
, 0);
681 regmap_read(dd
->regmap
, MCHP_PDMC_MR
, &val
);
682 dev_dbg(dd
->dev
, "MR (0x%02x): 0x%08x\n", MCHP_PDMC_MR
, val
);
683 regmap_read(dd
->regmap
, MCHP_PDMC_CFGR
, &val
);
684 dev_dbg(dd
->dev
, "CFGR (0x%02x): 0x%08x\n", MCHP_PDMC_CFGR
, val
);
685 regmap_read(dd
->regmap
, MCHP_PDMC_IMR
, &val
);
686 dev_dbg(dd
->dev
, "IMR (0x%02x): 0x%08x\n", MCHP_PDMC_IMR
, val
);
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
);
744 static const struct snd_soc_dai_ops mchp_pdmc_dai_ops
= {
745 .probe
= mchp_pdmc_dai_probe
,
746 .set_fmt
= mchp_pdmc_set_fmt
,
747 .startup
= mchp_pdmc_startup
,
748 .hw_params
= mchp_pdmc_hw_params
,
749 .trigger
= mchp_pdmc_trigger
,
750 .pcm_new
= &mchp_pdmc_pcm_new
,
753 static struct snd_soc_dai_driver mchp_pdmc_dai
= {
756 .stream_name
= "Capture",
761 .rates
= SNDRV_PCM_RATE_KNOT
,
762 .formats
= SNDRV_PCM_FMTBIT_S24_LE
,
764 .ops
= &mchp_pdmc_dai_ops
,
767 /* PDMC interrupt handler */
768 static irqreturn_t
mchp_pdmc_interrupt(int irq
, void *dev_id
)
770 struct mchp_pdmc
*dd
= dev_id
;
771 u32 isr
, msr
, pending
;
772 irqreturn_t ret
= IRQ_NONE
;
774 regmap_read(dd
->regmap
, MCHP_PDMC_ISR
, &isr
);
775 regmap_read(dd
->regmap
, MCHP_PDMC_IMR
, &msr
);
778 dev_dbg(dd
->dev
, "ISR (0x%02x): 0x%08x, IMR (0x%02x): 0x%08x, pending: 0x%08x\n",
779 MCHP_PDMC_ISR
, isr
, MCHP_PDMC_IMR
, msr
, pending
);
783 if (pending
& MCHP_PDMC_IR_RXUDR
) {
784 dev_warn(dd
->dev
, "underrun detected\n");
785 regmap_write(dd
->regmap
, MCHP_PDMC_IDR
, MCHP_PDMC_IR_RXUDR
);
788 if (pending
& MCHP_PDMC_IR_RXOVR
) {
789 dev_warn(dd
->dev
, "overrun detected\n");
790 regmap_write(dd
->regmap
, MCHP_PDMC_IDR
, MCHP_PDMC_IR_RXOVR
);
797 /* regmap configuration */
798 static bool mchp_pdmc_readable_reg(struct device
*dev
, unsigned int reg
)
813 static bool mchp_pdmc_writeable_reg(struct device
*dev
, unsigned int reg
)
827 static bool mchp_pdmc_volatile_reg(struct device
*dev
, unsigned int reg
)
838 static bool mchp_pdmc_precious_reg(struct device
*dev
, unsigned int reg
)
849 static const struct regmap_config mchp_pdmc_regmap_config
= {
853 .max_register
= MCHP_PDMC_VER
,
854 .readable_reg
= mchp_pdmc_readable_reg
,
855 .writeable_reg
= mchp_pdmc_writeable_reg
,
856 .precious_reg
= mchp_pdmc_precious_reg
,
857 .volatile_reg
= mchp_pdmc_volatile_reg
,
858 .cache_type
= REGCACHE_FLAT
,
861 static int mchp_pdmc_dt_init(struct mchp_pdmc
*dd
)
863 struct device_node
*np
= dd
->dev
->of_node
;
864 bool mic_ch
[MCHP_PDMC_DS_NO
][MCHP_PDMC_EDGE_NO
] = {0};
869 dev_err(dd
->dev
, "device node not found\n");
873 dd
->mic_no
= of_property_count_u32_elems(np
, "microchip,mic-pos");
874 if (dd
->mic_no
< 0) {
875 dev_err(dd
->dev
, "failed to get microchip,mic-pos: %d",
879 if (!dd
->mic_no
|| dd
->mic_no
% 2 ||
880 dd
->mic_no
/ 2 > MCHP_PDMC_MAX_CHANNELS
) {
881 dev_err(dd
->dev
, "invalid array length for microchip,mic-pos: %d",
888 dev_info(dd
->dev
, "%d PDM microphones declared\n", dd
->mic_no
);
891 * by default, we consider the order of microphones in
892 * microchip,mic-pos to be the same with the channel mapping;
893 * 1st microphone channel 0, 2nd microphone channel 1, etc.
895 for (i
= 0; i
< dd
->mic_no
; i
++) {
899 ret
= of_property_read_u32_index(np
, "microchip,mic-pos", i
* 2,
903 "failed to get value no %d value from microchip,mic-pos: %d",
907 if (ds
>= MCHP_PDMC_DS_NO
) {
909 "invalid DS index in microchip,mic-pos array: %d",
914 ret
= of_property_read_u32_index(np
, "microchip,mic-pos", i
* 2 + 1,
918 "failed to get value no %d value from microchip,mic-pos: %d",
923 if (edge
!= MCHP_PDMC_CLK_POSITIVE
&&
924 edge
!= MCHP_PDMC_CLK_NEGATIVE
) {
926 "invalid edge in microchip,mic-pos array: %d", edge
);
929 if (mic_ch
[ds
][edge
]) {
931 "duplicated mic (DS %d, edge %d) in microchip,mic-pos array",
935 mic_ch
[ds
][edge
] = true;
936 dd
->channel_mic_map
[i
].ds_pos
= ds
;
937 dd
->channel_mic_map
[i
].clk_edge
= edge
;
940 dd
->startup_delay_us
= 150000;
941 of_property_read_u32(np
, "microchip,startup-delay-us", &dd
->startup_delay_us
);
946 /* used to clean the channel index found on RHR's MSB */
947 static int mchp_pdmc_process(struct snd_pcm_substream
*substream
,
948 int channel
, unsigned long hwoff
,
951 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
952 u8
*dma_ptr
= runtime
->dma_area
+ hwoff
+
953 channel
* (runtime
->dma_bytes
/ runtime
->channels
);
954 u8
*dma_ptr_end
= dma_ptr
+ bytes
;
955 unsigned int sample_size
= samples_to_bytes(runtime
, 1);
957 for (; dma_ptr
< dma_ptr_end
; dma_ptr
+= sample_size
)
963 static struct snd_dmaengine_pcm_config mchp_pdmc_config
= {
964 .process
= mchp_pdmc_process
,
965 .prepare_slave_config
= snd_dmaengine_pcm_prepare_slave_config
,
968 static int mchp_pdmc_runtime_suspend(struct device
*dev
)
970 struct mchp_pdmc
*dd
= dev_get_drvdata(dev
);
972 regcache_cache_only(dd
->regmap
, true);
974 clk_disable_unprepare(dd
->gclk
);
975 clk_disable_unprepare(dd
->pclk
);
980 static int mchp_pdmc_runtime_resume(struct device
*dev
)
982 struct mchp_pdmc
*dd
= dev_get_drvdata(dev
);
985 ret
= clk_prepare_enable(dd
->pclk
);
988 "failed to enable the peripheral clock: %d\n", ret
);
991 ret
= clk_prepare_enable(dd
->gclk
);
994 "failed to enable generic clock: %d\n", ret
);
998 regcache_cache_only(dd
->regmap
, false);
999 regcache_mark_dirty(dd
->regmap
);
1000 ret
= regcache_sync(dd
->regmap
);
1002 regcache_cache_only(dd
->regmap
, true);
1003 clk_disable_unprepare(dd
->gclk
);
1005 clk_disable_unprepare(dd
->pclk
);
1011 static int mchp_pdmc_probe(struct platform_device
*pdev
)
1013 struct device
*dev
= &pdev
->dev
;
1014 struct mchp_pdmc
*dd
;
1015 struct resource
*res
;
1016 void __iomem
*io_base
;
1021 dd
= devm_kzalloc(dev
, sizeof(*dd
), GFP_KERNEL
);
1025 dd
->dev
= &pdev
->dev
;
1026 ret
= mchp_pdmc_dt_init(dd
);
1030 irq
= platform_get_irq(pdev
, 0);
1034 dd
->pclk
= devm_clk_get(dev
, "pclk");
1035 if (IS_ERR(dd
->pclk
)) {
1036 ret
= PTR_ERR(dd
->pclk
);
1037 dev_err(dev
, "failed to get peripheral clock: %d\n", ret
);
1041 dd
->gclk
= devm_clk_get(dev
, "gclk");
1042 if (IS_ERR(dd
->gclk
)) {
1043 ret
= PTR_ERR(dd
->gclk
);
1044 dev_err(dev
, "failed to get GCK: %d\n", ret
);
1048 io_base
= devm_platform_get_and_ioremap_resource(pdev
, 0, &res
);
1049 if (IS_ERR(io_base
)) {
1050 ret
= PTR_ERR(io_base
);
1051 dev_err(dev
, "failed to remap register memory: %d\n", ret
);
1055 dd
->regmap
= devm_regmap_init_mmio(dev
, io_base
,
1056 &mchp_pdmc_regmap_config
);
1057 if (IS_ERR(dd
->regmap
)) {
1058 ret
= PTR_ERR(dd
->regmap
);
1059 dev_err(dev
, "failed to init register map: %d\n", ret
);
1063 ret
= devm_request_irq(dev
, irq
, mchp_pdmc_interrupt
, 0,
1064 dev_name(&pdev
->dev
), dd
);
1066 dev_err(dev
, "can't register ISR for IRQ %u (ret=%i)\n",
1071 /* by default audio filter is enabled and the SINC Filter order
1072 * will be set to the recommended value, 3
1074 dd
->audio_filter_en
= true;
1077 dd
->addr
.addr
= (dma_addr_t
)res
->start
+ MCHP_PDMC_RHR
;
1078 platform_set_drvdata(pdev
, dd
);
1080 pm_runtime_enable(dd
->dev
);
1081 if (!pm_runtime_enabled(dd
->dev
)) {
1082 ret
= mchp_pdmc_runtime_resume(dd
->dev
);
1087 /* register platform */
1088 ret
= devm_snd_dmaengine_pcm_register(dev
, &mchp_pdmc_config
, 0);
1090 dev_err(dev
, "could not register platform: %d\n", ret
);
1091 goto pm_runtime_suspend
;
1094 ret
= devm_snd_soc_register_component(dev
, &mchp_pdmc_dai_component
,
1097 dev_err(dev
, "could not register CPU DAI: %d\n", ret
);
1098 goto pm_runtime_suspend
;
1101 /* print IP version */
1102 regmap_read(dd
->regmap
, MCHP_PDMC_VER
, &version
);
1103 dev_info(dd
->dev
, "hw version: %#lx\n",
1104 version
& MCHP_PDMC_VER_VERSION
);
1109 if (!pm_runtime_status_suspended(dd
->dev
))
1110 mchp_pdmc_runtime_suspend(dd
->dev
);
1111 pm_runtime_disable(dd
->dev
);
1116 static void mchp_pdmc_remove(struct platform_device
*pdev
)
1118 struct mchp_pdmc
*dd
= platform_get_drvdata(pdev
);
1120 atomic_set(&dd
->busy_stream
, 0);
1122 if (!pm_runtime_status_suspended(dd
->dev
))
1123 mchp_pdmc_runtime_suspend(dd
->dev
);
1125 pm_runtime_disable(dd
->dev
);
1128 static const struct of_device_id mchp_pdmc_of_match
[] = {
1130 .compatible
= "microchip,sama7g5-pdmc",
1135 MODULE_DEVICE_TABLE(of
, mchp_pdmc_of_match
);
1137 static const struct dev_pm_ops mchp_pdmc_pm_ops
= {
1138 SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
, pm_runtime_force_resume
)
1139 RUNTIME_PM_OPS(mchp_pdmc_runtime_suspend
, mchp_pdmc_runtime_resume
,
1143 static struct platform_driver mchp_pdmc_driver
= {
1145 .name
= "mchp-pdmc",
1146 .of_match_table
= of_match_ptr(mchp_pdmc_of_match
),
1147 .pm
= pm_ptr(&mchp_pdmc_pm_ops
),
1149 .probe
= mchp_pdmc_probe
,
1150 .remove
= mchp_pdmc_remove
,
1152 module_platform_driver(mchp_pdmc_driver
);
1154 MODULE_DESCRIPTION("Microchip PDMC driver under ALSA SoC architecture");
1155 MODULE_AUTHOR("Codrin Ciubotariu <codrin.ciubotariu@microchip.com>");
1156 MODULE_LICENSE("GPL v2");