xref: /openbmc/linux/sound/soc/atmel/mchp-pdmc.c (revision fac59652993f075d57860769c99045b3ca18780d)
150291652SCodrin Ciubotariu // SPDX-License-Identifier: GPL-2.0
250291652SCodrin Ciubotariu //
350291652SCodrin Ciubotariu // Driver for Microchip Pulse Density Microphone Controller (PDMC) interfaces
450291652SCodrin Ciubotariu //
550291652SCodrin Ciubotariu // Copyright (C) 2019-2022 Microchip Technology Inc. and its subsidiaries
650291652SCodrin Ciubotariu //
750291652SCodrin Ciubotariu // Author: Codrin Ciubotariu <codrin.ciubotariu@microchip.com>
850291652SCodrin Ciubotariu 
950291652SCodrin Ciubotariu #include <dt-bindings/sound/microchip,pdmc.h>
1050291652SCodrin Ciubotariu 
1112974257SClaudiu Beznea #include <linux/bitfield.h>
1250291652SCodrin Ciubotariu #include <linux/clk.h>
1350291652SCodrin Ciubotariu #include <linux/module.h>
1450291652SCodrin Ciubotariu #include <linux/of.h>
15e8c8e9deSClaudiu Beznea #include <linux/pm_runtime.h>
1650291652SCodrin Ciubotariu #include <linux/regmap.h>
1750291652SCodrin Ciubotariu 
1850291652SCodrin Ciubotariu #include <sound/core.h>
1950291652SCodrin Ciubotariu #include <sound/dmaengine_pcm.h>
2050291652SCodrin Ciubotariu #include <sound/pcm_params.h>
2150291652SCodrin Ciubotariu #include <sound/tlv.h>
2250291652SCodrin Ciubotariu 
2350291652SCodrin Ciubotariu /*
2450291652SCodrin Ciubotariu  * ---- PDMC Register map ----
2550291652SCodrin Ciubotariu  */
2650291652SCodrin Ciubotariu #define MCHP_PDMC_CR			0x00	/* Control Register */
2750291652SCodrin Ciubotariu #define MCHP_PDMC_MR			0x04	/* Mode Register */
2850291652SCodrin Ciubotariu #define MCHP_PDMC_CFGR			0x08	/* Configuration Register */
2950291652SCodrin Ciubotariu #define MCHP_PDMC_RHR			0x0C	/* Receive Holding Register */
3050291652SCodrin Ciubotariu #define MCHP_PDMC_IER			0x14	/* Interrupt Enable Register */
3150291652SCodrin Ciubotariu #define MCHP_PDMC_IDR			0x18	/* Interrupt Disable Register */
3250291652SCodrin Ciubotariu #define MCHP_PDMC_IMR			0x1C	/* Interrupt Mask Register */
3350291652SCodrin Ciubotariu #define MCHP_PDMC_ISR			0x20	/* Interrupt Status Register */
3450291652SCodrin Ciubotariu #define MCHP_PDMC_VER			0x50	/* Version Register */
3550291652SCodrin Ciubotariu 
3650291652SCodrin Ciubotariu /*
3750291652SCodrin Ciubotariu  * ---- Control Register (Write-only) ----
3850291652SCodrin Ciubotariu  */
3950291652SCodrin Ciubotariu #define MCHP_PDMC_CR_SWRST		BIT(0)	/* Software Reset */
4050291652SCodrin Ciubotariu 
4150291652SCodrin Ciubotariu /*
4250291652SCodrin Ciubotariu  * ---- Mode Register (Read/Write) ----
4350291652SCodrin Ciubotariu  */
4450291652SCodrin Ciubotariu #define MCHP_PDMC_MR_PDMCEN_MASK	GENMASK(3, 0)
4550291652SCodrin Ciubotariu #define MCHP_PDMC_MR_PDMCEN(ch)		(BIT(ch) & MCHP_PDMC_MR_PDMCEN_MASK)
4650291652SCodrin Ciubotariu 
4750291652SCodrin Ciubotariu #define MCHP_PDMC_MR_OSR_MASK		GENMASK(17, 16)
4850291652SCodrin Ciubotariu #define MCHP_PDMC_MR_OSR64		(1 << 16)
4950291652SCodrin Ciubotariu #define MCHP_PDMC_MR_OSR128		(2 << 16)
5050291652SCodrin Ciubotariu #define MCHP_PDMC_MR_OSR256		(3 << 16)
5150291652SCodrin Ciubotariu 
5250291652SCodrin Ciubotariu #define MCHP_PDMC_MR_SINCORDER_MASK	GENMASK(23, 20)
5350291652SCodrin Ciubotariu 
5450291652SCodrin Ciubotariu #define MCHP_PDMC_MR_SINC_OSR_MASK	GENMASK(27, 24)
5550291652SCodrin Ciubotariu #define MCHP_PDMC_MR_SINC_OSR_DIS	(0 << 24)
5650291652SCodrin Ciubotariu #define MCHP_PDMC_MR_SINC_OSR_8		(1 << 24)
5750291652SCodrin Ciubotariu #define MCHP_PDMC_MR_SINC_OSR_16	(2 << 24)
5850291652SCodrin Ciubotariu #define MCHP_PDMC_MR_SINC_OSR_32	(3 << 24)
5950291652SCodrin Ciubotariu #define MCHP_PDMC_MR_SINC_OSR_64	(4 << 24)
6050291652SCodrin Ciubotariu #define MCHP_PDMC_MR_SINC_OSR_128	(5 << 24)
6150291652SCodrin Ciubotariu #define MCHP_PDMC_MR_SINC_OSR_256	(6 << 24)
6250291652SCodrin Ciubotariu 
6350291652SCodrin Ciubotariu #define MCHP_PDMC_MR_CHUNK_MASK		GENMASK(31, 28)
6450291652SCodrin Ciubotariu 
6550291652SCodrin Ciubotariu /*
6650291652SCodrin Ciubotariu  * ---- Configuration Register (Read/Write) ----
6750291652SCodrin Ciubotariu  */
6850291652SCodrin Ciubotariu #define MCHP_PDMC_CFGR_BSSEL_MASK	(BIT(0) | BIT(2) | BIT(4) | BIT(6))
6950291652SCodrin Ciubotariu #define MCHP_PDMC_CFGR_BSSEL(ch)	BIT((ch) * 2)
7050291652SCodrin Ciubotariu 
7150291652SCodrin Ciubotariu #define MCHP_PDMC_CFGR_PDMSEL_MASK	(BIT(16) | BIT(18) | BIT(20) | BIT(22))
7250291652SCodrin Ciubotariu #define MCHP_PDMC_CFGR_PDMSEL(ch)	BIT((ch) * 2 + 16)
7350291652SCodrin Ciubotariu 
7450291652SCodrin Ciubotariu /*
7550291652SCodrin Ciubotariu  * ---- Interrupt Enable/Disable/Mask/Status Registers ----
7650291652SCodrin Ciubotariu  */
7750291652SCodrin Ciubotariu #define MCHP_PDMC_IR_RXRDY		BIT(0)
7850291652SCodrin Ciubotariu #define MCHP_PDMC_IR_RXEMPTY		BIT(1)
7950291652SCodrin Ciubotariu #define MCHP_PDMC_IR_RXFULL		BIT(2)
8050291652SCodrin Ciubotariu #define MCHP_PDMC_IR_RXCHUNK		BIT(3)
8150291652SCodrin Ciubotariu #define MCHP_PDMC_IR_RXUDR		BIT(4)
8250291652SCodrin Ciubotariu #define MCHP_PDMC_IR_RXOVR		BIT(5)
8350291652SCodrin Ciubotariu 
8450291652SCodrin Ciubotariu /*
8550291652SCodrin Ciubotariu  * ---- Version Register (Read-only) ----
8650291652SCodrin Ciubotariu  */
8750291652SCodrin Ciubotariu #define MCHP_PDMC_VER_VERSION		GENMASK(11, 0)
8850291652SCodrin Ciubotariu 
8950291652SCodrin Ciubotariu #define MCHP_PDMC_MAX_CHANNELS		4
9050291652SCodrin Ciubotariu #define MCHP_PDMC_DS_NO			2
9150291652SCodrin Ciubotariu #define MCHP_PDMC_EDGE_NO		2
9250291652SCodrin Ciubotariu 
9350291652SCodrin Ciubotariu struct mic_map {
9450291652SCodrin Ciubotariu 	int ds_pos;
9550291652SCodrin Ciubotariu 	int clk_edge;
9650291652SCodrin Ciubotariu };
9750291652SCodrin Ciubotariu 
9850291652SCodrin Ciubotariu struct mchp_pdmc_chmap {
9950291652SCodrin Ciubotariu 	struct snd_pcm_chmap_elem *chmap;
10050291652SCodrin Ciubotariu 	struct mchp_pdmc *dd;
10150291652SCodrin Ciubotariu 	struct snd_pcm *pcm;
10250291652SCodrin Ciubotariu 	struct snd_kcontrol *kctl;
10350291652SCodrin Ciubotariu };
10450291652SCodrin Ciubotariu 
10550291652SCodrin Ciubotariu struct mchp_pdmc {
10650291652SCodrin Ciubotariu 	struct mic_map channel_mic_map[MCHP_PDMC_MAX_CHANNELS];
10750291652SCodrin Ciubotariu 	struct device *dev;
10850291652SCodrin Ciubotariu 	struct snd_dmaengine_dai_dma_data addr;
10950291652SCodrin Ciubotariu 	struct regmap *regmap;
11050291652SCodrin Ciubotariu 	struct clk *pclk;
11150291652SCodrin Ciubotariu 	struct clk *gclk;
11250291652SCodrin Ciubotariu 	u32 pdmcen;
113404c61c4SClaudiu Beznea 	u32 suspend_irq;
114c5682e2bSClaudiu Beznea 	u32 startup_delay_us;
11550291652SCodrin Ciubotariu 	int mic_no;
11650291652SCodrin Ciubotariu 	int sinc_order;
11750291652SCodrin Ciubotariu 	bool audio_filter_en;
11850291652SCodrin Ciubotariu };
11950291652SCodrin Ciubotariu 
12050291652SCodrin Ciubotariu static const char *const mchp_pdmc_sinc_filter_order_text[] = {
12150291652SCodrin Ciubotariu 	"1", "2", "3", "4", "5"
12250291652SCodrin Ciubotariu };
12350291652SCodrin Ciubotariu 
12450291652SCodrin Ciubotariu static const unsigned int mchp_pdmc_sinc_filter_order_values[] = {
12550291652SCodrin Ciubotariu 	1, 2, 3, 4, 5,
12650291652SCodrin Ciubotariu };
12750291652SCodrin Ciubotariu 
12850291652SCodrin Ciubotariu static const struct soc_enum mchp_pdmc_sinc_filter_order_enum = {
12950291652SCodrin Ciubotariu 	.items = ARRAY_SIZE(mchp_pdmc_sinc_filter_order_text),
13050291652SCodrin Ciubotariu 	.texts = mchp_pdmc_sinc_filter_order_text,
13150291652SCodrin Ciubotariu 	.values = mchp_pdmc_sinc_filter_order_values,
13250291652SCodrin Ciubotariu };
13350291652SCodrin Ciubotariu 
mchp_pdmc_sinc_order_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * uvalue)13450291652SCodrin Ciubotariu static int mchp_pdmc_sinc_order_get(struct snd_kcontrol *kcontrol,
13550291652SCodrin Ciubotariu 				    struct snd_ctl_elem_value *uvalue)
13650291652SCodrin Ciubotariu {
13750291652SCodrin Ciubotariu 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
13850291652SCodrin Ciubotariu 	struct mchp_pdmc *dd = snd_soc_component_get_drvdata(component);
13950291652SCodrin Ciubotariu 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
14050291652SCodrin Ciubotariu 	unsigned int item;
14150291652SCodrin Ciubotariu 
14250291652SCodrin Ciubotariu 	item = snd_soc_enum_val_to_item(e, dd->sinc_order);
14350291652SCodrin Ciubotariu 	uvalue->value.enumerated.item[0] = item;
14450291652SCodrin Ciubotariu 
14550291652SCodrin Ciubotariu 	return 0;
14650291652SCodrin Ciubotariu }
14750291652SCodrin Ciubotariu 
mchp_pdmc_sinc_order_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * uvalue)14850291652SCodrin Ciubotariu static int mchp_pdmc_sinc_order_put(struct snd_kcontrol *kcontrol,
14950291652SCodrin Ciubotariu 				    struct snd_ctl_elem_value *uvalue)
15050291652SCodrin Ciubotariu {
15150291652SCodrin Ciubotariu 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
15250291652SCodrin Ciubotariu 	struct mchp_pdmc *dd = snd_soc_component_get_drvdata(component);
15350291652SCodrin Ciubotariu 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
15450291652SCodrin Ciubotariu 	unsigned int *item = uvalue->value.enumerated.item;
15550291652SCodrin Ciubotariu 	unsigned int val;
15650291652SCodrin Ciubotariu 
15750291652SCodrin Ciubotariu 	if (item[0] >= e->items)
15850291652SCodrin Ciubotariu 		return -EINVAL;
15950291652SCodrin Ciubotariu 
16050291652SCodrin Ciubotariu 	val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
16150291652SCodrin Ciubotariu 	if (val == dd->sinc_order)
16250291652SCodrin Ciubotariu 		return 0;
16350291652SCodrin Ciubotariu 
16450291652SCodrin Ciubotariu 	dd->sinc_order = val;
16550291652SCodrin Ciubotariu 
16650291652SCodrin Ciubotariu 	return 1;
16750291652SCodrin Ciubotariu }
16850291652SCodrin Ciubotariu 
mchp_pdmc_af_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * uvalue)16950291652SCodrin Ciubotariu static int mchp_pdmc_af_get(struct snd_kcontrol *kcontrol,
17050291652SCodrin Ciubotariu 			    struct snd_ctl_elem_value *uvalue)
17150291652SCodrin Ciubotariu {
17250291652SCodrin Ciubotariu 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
17350291652SCodrin Ciubotariu 	struct mchp_pdmc *dd = snd_soc_component_get_drvdata(component);
17450291652SCodrin Ciubotariu 
17550291652SCodrin Ciubotariu 	uvalue->value.integer.value[0] = !!dd->audio_filter_en;
17650291652SCodrin Ciubotariu 
17750291652SCodrin Ciubotariu 	return 0;
17850291652SCodrin Ciubotariu }
17950291652SCodrin Ciubotariu 
mchp_pdmc_af_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * uvalue)18050291652SCodrin Ciubotariu static int mchp_pdmc_af_put(struct snd_kcontrol *kcontrol,
18150291652SCodrin Ciubotariu 			    struct snd_ctl_elem_value *uvalue)
18250291652SCodrin Ciubotariu {
18350291652SCodrin Ciubotariu 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
18450291652SCodrin Ciubotariu 	struct mchp_pdmc *dd = snd_soc_component_get_drvdata(component);
185a8ae15eaSAlviro Iskandar Setiawan 	bool af = uvalue->value.integer.value[0] ? true : false;
18650291652SCodrin Ciubotariu 
18750291652SCodrin Ciubotariu 	if (dd->audio_filter_en == af)
18850291652SCodrin Ciubotariu 		return 0;
18950291652SCodrin Ciubotariu 
19050291652SCodrin Ciubotariu 	dd->audio_filter_en = af;
19150291652SCodrin Ciubotariu 
19250291652SCodrin Ciubotariu 	return 1;
19350291652SCodrin Ciubotariu }
19450291652SCodrin Ciubotariu 
mchp_pdmc_chmap_ctl_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)19550291652SCodrin Ciubotariu static int mchp_pdmc_chmap_ctl_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
19650291652SCodrin Ciubotariu {
19750291652SCodrin Ciubotariu 	struct mchp_pdmc_chmap *info = snd_kcontrol_chip(kcontrol);
19850291652SCodrin Ciubotariu 
19950291652SCodrin Ciubotariu 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
20050291652SCodrin Ciubotariu 	uinfo->count = info->dd->mic_no;
20150291652SCodrin Ciubotariu 	uinfo->value.integer.min = 0;
20250291652SCodrin Ciubotariu 	uinfo->value.integer.max = SNDRV_CHMAP_RR; /* maxmimum 4 channels */
20350291652SCodrin Ciubotariu 	return 0;
20450291652SCodrin Ciubotariu }
20550291652SCodrin Ciubotariu 
20650291652SCodrin Ciubotariu static inline struct snd_pcm_substream *
mchp_pdmc_chmap_substream(struct mchp_pdmc_chmap * info,unsigned int idx)20750291652SCodrin Ciubotariu mchp_pdmc_chmap_substream(struct mchp_pdmc_chmap *info, unsigned int idx)
20850291652SCodrin Ciubotariu {
20950291652SCodrin Ciubotariu 	struct snd_pcm_substream *s;
21050291652SCodrin Ciubotariu 
21150291652SCodrin Ciubotariu 	for (s = info->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; s; s = s->next)
21250291652SCodrin Ciubotariu 		if (s->number == idx)
21350291652SCodrin Ciubotariu 			return s;
21450291652SCodrin Ciubotariu 	return NULL;
21550291652SCodrin Ciubotariu }
21650291652SCodrin Ciubotariu 
mchp_pdmc_chmap_get(struct snd_pcm_substream * substream,struct mchp_pdmc_chmap * ch_info)21750291652SCodrin Ciubotariu static struct snd_pcm_chmap_elem *mchp_pdmc_chmap_get(struct snd_pcm_substream *substream,
21850291652SCodrin Ciubotariu 						      struct mchp_pdmc_chmap *ch_info)
21950291652SCodrin Ciubotariu {
22050291652SCodrin Ciubotariu 	struct snd_pcm_chmap_elem *map;
22150291652SCodrin Ciubotariu 
22250291652SCodrin Ciubotariu 	for (map = ch_info->chmap; map->channels; map++) {
22350291652SCodrin Ciubotariu 		if (map->channels == substream->runtime->channels)
22450291652SCodrin Ciubotariu 			return map;
22550291652SCodrin Ciubotariu 	}
22650291652SCodrin Ciubotariu 	return NULL;
22750291652SCodrin Ciubotariu }
22850291652SCodrin Ciubotariu 
mchp_pdmc_chmap_ctl_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)22950291652SCodrin Ciubotariu static int mchp_pdmc_chmap_ctl_get(struct snd_kcontrol *kcontrol,
23050291652SCodrin Ciubotariu 				   struct snd_ctl_elem_value *ucontrol)
23150291652SCodrin Ciubotariu {
23250291652SCodrin Ciubotariu 	struct mchp_pdmc_chmap *info = snd_kcontrol_chip(kcontrol);
23350291652SCodrin Ciubotariu 	struct mchp_pdmc *dd = info->dd;
23450291652SCodrin Ciubotariu 	unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
23550291652SCodrin Ciubotariu 	struct snd_pcm_substream *substream;
23650291652SCodrin Ciubotariu 	const struct snd_pcm_chmap_elem *map;
23750291652SCodrin Ciubotariu 	int i;
23850291652SCodrin Ciubotariu 	u32 cfgr_val = 0;
23950291652SCodrin Ciubotariu 
24050291652SCodrin Ciubotariu 	if (!info->chmap)
24150291652SCodrin Ciubotariu 		return -EINVAL;
24250291652SCodrin Ciubotariu 	substream = mchp_pdmc_chmap_substream(info, idx);
24350291652SCodrin Ciubotariu 	if (!substream)
24450291652SCodrin Ciubotariu 		return -ENODEV;
24550291652SCodrin Ciubotariu 	memset(ucontrol->value.integer.value, 0, sizeof(long) * info->dd->mic_no);
24650291652SCodrin Ciubotariu 	if (!substream->runtime)
24750291652SCodrin Ciubotariu 		return 0; /* no channels set */
24850291652SCodrin Ciubotariu 
24950291652SCodrin Ciubotariu 	map = mchp_pdmc_chmap_get(substream, info);
25050291652SCodrin Ciubotariu 	if (!map)
25150291652SCodrin Ciubotariu 		return -EINVAL;
25250291652SCodrin Ciubotariu 
25350291652SCodrin Ciubotariu 	for (i = 0; i < map->channels; i++) {
25450291652SCodrin Ciubotariu 		int map_idx = map->channels == 1 ? map->map[i] - SNDRV_CHMAP_MONO :
25550291652SCodrin Ciubotariu 						   map->map[i] - SNDRV_CHMAP_FL;
25650291652SCodrin Ciubotariu 
25750291652SCodrin Ciubotariu 		/* make sure the reported channel map is the real one, so write the map */
25850291652SCodrin Ciubotariu 		if (dd->channel_mic_map[map_idx].ds_pos)
25950291652SCodrin Ciubotariu 			cfgr_val |= MCHP_PDMC_CFGR_PDMSEL(i);
26050291652SCodrin Ciubotariu 		if (dd->channel_mic_map[map_idx].clk_edge)
26150291652SCodrin Ciubotariu 			cfgr_val |= MCHP_PDMC_CFGR_BSSEL(i);
26250291652SCodrin Ciubotariu 
26350291652SCodrin Ciubotariu 		ucontrol->value.integer.value[i] = map->map[i];
26450291652SCodrin Ciubotariu 	}
26550291652SCodrin Ciubotariu 
26650291652SCodrin Ciubotariu 	regmap_write(dd->regmap, MCHP_PDMC_CFGR, cfgr_val);
26750291652SCodrin Ciubotariu 
26850291652SCodrin Ciubotariu 	return 0;
26950291652SCodrin Ciubotariu }
27050291652SCodrin Ciubotariu 
mchp_pdmc_chmap_ctl_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)27150291652SCodrin Ciubotariu static int mchp_pdmc_chmap_ctl_put(struct snd_kcontrol *kcontrol,
27250291652SCodrin Ciubotariu 				   struct snd_ctl_elem_value *ucontrol)
27350291652SCodrin Ciubotariu {
27450291652SCodrin Ciubotariu 	struct mchp_pdmc_chmap *info = snd_kcontrol_chip(kcontrol);
27550291652SCodrin Ciubotariu 	struct mchp_pdmc *dd = info->dd;
27650291652SCodrin Ciubotariu 	unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
27750291652SCodrin Ciubotariu 	struct snd_pcm_substream *substream;
27850291652SCodrin Ciubotariu 	struct snd_pcm_chmap_elem *map;
27950291652SCodrin Ciubotariu 	u32 cfgr_val = 0;
28050291652SCodrin Ciubotariu 	int i;
28150291652SCodrin Ciubotariu 
28250291652SCodrin Ciubotariu 	if (!info->chmap)
28350291652SCodrin Ciubotariu 		return -EINVAL;
28450291652SCodrin Ciubotariu 	substream = mchp_pdmc_chmap_substream(info, idx);
28550291652SCodrin Ciubotariu 	if (!substream)
28650291652SCodrin Ciubotariu 		return -ENODEV;
28750291652SCodrin Ciubotariu 
288*0152c81fSAndrei Simion 	if (!substream->runtime)
289*0152c81fSAndrei Simion 		return 0; /* just for avoiding error from alsactl restore */
290*0152c81fSAndrei Simion 
29150291652SCodrin Ciubotariu 	map = mchp_pdmc_chmap_get(substream, info);
29250291652SCodrin Ciubotariu 	if (!map)
29350291652SCodrin Ciubotariu 		return -EINVAL;
29450291652SCodrin Ciubotariu 
29550291652SCodrin Ciubotariu 	for (i = 0; i < map->channels; i++) {
29650291652SCodrin Ciubotariu 		int map_idx;
29750291652SCodrin Ciubotariu 
29850291652SCodrin Ciubotariu 		map->map[i] = ucontrol->value.integer.value[i];
29950291652SCodrin Ciubotariu 		map_idx = map->channels == 1 ? map->map[i] - SNDRV_CHMAP_MONO :
30050291652SCodrin Ciubotariu 					       map->map[i] - SNDRV_CHMAP_FL;
30150291652SCodrin Ciubotariu 
30250291652SCodrin Ciubotariu 		/* configure IP for the desired channel map */
30350291652SCodrin Ciubotariu 		if (dd->channel_mic_map[map_idx].ds_pos)
30450291652SCodrin Ciubotariu 			cfgr_val |= MCHP_PDMC_CFGR_PDMSEL(i);
30550291652SCodrin Ciubotariu 		if (dd->channel_mic_map[map_idx].clk_edge)
30650291652SCodrin Ciubotariu 			cfgr_val |= MCHP_PDMC_CFGR_BSSEL(i);
30750291652SCodrin Ciubotariu 	}
30850291652SCodrin Ciubotariu 
30950291652SCodrin Ciubotariu 	regmap_write(dd->regmap, MCHP_PDMC_CFGR, cfgr_val);
31050291652SCodrin Ciubotariu 
31150291652SCodrin Ciubotariu 	return 0;
31250291652SCodrin Ciubotariu }
31350291652SCodrin Ciubotariu 
mchp_pdmc_chmap_ctl_private_free(struct snd_kcontrol * kcontrol)31450291652SCodrin Ciubotariu static void mchp_pdmc_chmap_ctl_private_free(struct snd_kcontrol *kcontrol)
31550291652SCodrin Ciubotariu {
31650291652SCodrin Ciubotariu 	struct mchp_pdmc_chmap *info = snd_kcontrol_chip(kcontrol);
31750291652SCodrin Ciubotariu 
31850291652SCodrin Ciubotariu 	info->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].chmap_kctl = NULL;
31950291652SCodrin Ciubotariu 	kfree(info);
32050291652SCodrin Ciubotariu }
32150291652SCodrin Ciubotariu 
mchp_pdmc_chmap_ctl_tlv(struct snd_kcontrol * kcontrol,int op_flag,unsigned int size,unsigned int __user * tlv)32250291652SCodrin Ciubotariu static int mchp_pdmc_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
32350291652SCodrin Ciubotariu 				   unsigned int size, unsigned int __user *tlv)
32450291652SCodrin Ciubotariu {
32550291652SCodrin Ciubotariu 	struct mchp_pdmc_chmap *info = snd_kcontrol_chip(kcontrol);
32650291652SCodrin Ciubotariu 	const struct snd_pcm_chmap_elem *map;
32750291652SCodrin Ciubotariu 	unsigned int __user *dst;
32850291652SCodrin Ciubotariu 	int c, count = 0;
32950291652SCodrin Ciubotariu 
33050291652SCodrin Ciubotariu 	if (!info->chmap)
33150291652SCodrin Ciubotariu 		return -EINVAL;
33250291652SCodrin Ciubotariu 	if (size < 8)
33350291652SCodrin Ciubotariu 		return -ENOMEM;
33450291652SCodrin Ciubotariu 	if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
33550291652SCodrin Ciubotariu 		return -EFAULT;
33650291652SCodrin Ciubotariu 	size -= 8;
33750291652SCodrin Ciubotariu 	dst = tlv + 2;
33850291652SCodrin Ciubotariu 	for (map = info->chmap; map->channels; map++) {
33950291652SCodrin Ciubotariu 		int chs_bytes = map->channels * 4;
34050291652SCodrin Ciubotariu 
34150291652SCodrin Ciubotariu 		if (size < 8)
34250291652SCodrin Ciubotariu 			return -ENOMEM;
34350291652SCodrin Ciubotariu 		if (put_user(SNDRV_CTL_TLVT_CHMAP_VAR, dst) ||
34450291652SCodrin Ciubotariu 		    put_user(chs_bytes, dst + 1))
34550291652SCodrin Ciubotariu 			return -EFAULT;
34650291652SCodrin Ciubotariu 		dst += 2;
34750291652SCodrin Ciubotariu 		size -= 8;
34850291652SCodrin Ciubotariu 		count += 8;
34950291652SCodrin Ciubotariu 		if (size < chs_bytes)
35050291652SCodrin Ciubotariu 			return -ENOMEM;
35150291652SCodrin Ciubotariu 		size -= chs_bytes;
35250291652SCodrin Ciubotariu 		count += chs_bytes;
35350291652SCodrin Ciubotariu 		for (c = 0; c < map->channels; c++) {
35450291652SCodrin Ciubotariu 			if (put_user(map->map[c], dst))
35550291652SCodrin Ciubotariu 				return -EFAULT;
35650291652SCodrin Ciubotariu 			dst++;
35750291652SCodrin Ciubotariu 		}
35850291652SCodrin Ciubotariu 	}
35950291652SCodrin Ciubotariu 	if (put_user(count, tlv + 1))
36050291652SCodrin Ciubotariu 		return -EFAULT;
36150291652SCodrin Ciubotariu 	return 0;
36250291652SCodrin Ciubotariu }
36350291652SCodrin Ciubotariu 
36450291652SCodrin Ciubotariu static const struct snd_kcontrol_new mchp_pdmc_snd_controls[] = {
36550291652SCodrin Ciubotariu 	SOC_SINGLE_BOOL_EXT("Audio Filter", 0, &mchp_pdmc_af_get, &mchp_pdmc_af_put),
36650291652SCodrin Ciubotariu 	{
36750291652SCodrin Ciubotariu 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
36850291652SCodrin Ciubotariu 		.name = "SINC Filter Order",
36950291652SCodrin Ciubotariu 		.info = snd_soc_info_enum_double,
37050291652SCodrin Ciubotariu 		.get = mchp_pdmc_sinc_order_get,
37150291652SCodrin Ciubotariu 		.put = mchp_pdmc_sinc_order_put,
37250291652SCodrin Ciubotariu 		.private_value = (unsigned long)&mchp_pdmc_sinc_filter_order_enum,
37350291652SCodrin Ciubotariu 	},
37450291652SCodrin Ciubotariu };
37550291652SCodrin Ciubotariu 
mchp_pdmc_close(struct snd_soc_component * component,struct snd_pcm_substream * substream)37650291652SCodrin Ciubotariu static int mchp_pdmc_close(struct snd_soc_component *component,
37750291652SCodrin Ciubotariu 			   struct snd_pcm_substream *substream)
37850291652SCodrin Ciubotariu {
37950291652SCodrin Ciubotariu 	return snd_soc_add_component_controls(component, mchp_pdmc_snd_controls,
38050291652SCodrin Ciubotariu 					      ARRAY_SIZE(mchp_pdmc_snd_controls));
38150291652SCodrin Ciubotariu }
38250291652SCodrin Ciubotariu 
mchp_pdmc_open(struct snd_soc_component * component,struct snd_pcm_substream * substream)38350291652SCodrin Ciubotariu static int mchp_pdmc_open(struct snd_soc_component *component,
38450291652SCodrin Ciubotariu 			  struct snd_pcm_substream *substream)
38550291652SCodrin Ciubotariu {
38650291652SCodrin Ciubotariu 	int i;
38750291652SCodrin Ciubotariu 
38850291652SCodrin Ciubotariu 	/* remove controls that can't be changed at runtime */
38950291652SCodrin Ciubotariu 	for (i = 0; i < ARRAY_SIZE(mchp_pdmc_snd_controls); i++) {
39050291652SCodrin Ciubotariu 		const struct snd_kcontrol_new *control = &mchp_pdmc_snd_controls[i];
39150291652SCodrin Ciubotariu 		struct snd_ctl_elem_id id;
39250291652SCodrin Ciubotariu 		int err;
39350291652SCodrin Ciubotariu 
39450291652SCodrin Ciubotariu 		if (component->name_prefix)
39550291652SCodrin Ciubotariu 			snprintf(id.name, sizeof(id.name), "%s %s", component->name_prefix,
39650291652SCodrin Ciubotariu 				 control->name);
39750291652SCodrin Ciubotariu 		else
39850291652SCodrin Ciubotariu 			strscpy(id.name, control->name, sizeof(id.name));
39950291652SCodrin Ciubotariu 
40050291652SCodrin Ciubotariu 		id.numid = 0;
40150291652SCodrin Ciubotariu 		id.iface = control->iface;
40250291652SCodrin Ciubotariu 		id.device = control->device;
40350291652SCodrin Ciubotariu 		id.subdevice = control->subdevice;
40450291652SCodrin Ciubotariu 		id.index = control->index;
405d8b366c4STakashi Iwai 		err = snd_ctl_remove_id(component->card->snd_card, &id);
406d8b366c4STakashi Iwai 		if (err < 0)
40750291652SCodrin Ciubotariu 			dev_err(component->dev, "%d: Failed to remove %s\n", err,
40850291652SCodrin Ciubotariu 				control->name);
40950291652SCodrin Ciubotariu 	}
41050291652SCodrin Ciubotariu 
41150291652SCodrin Ciubotariu 	return 0;
41250291652SCodrin Ciubotariu }
41350291652SCodrin Ciubotariu 
41450291652SCodrin Ciubotariu static const struct snd_soc_component_driver mchp_pdmc_dai_component = {
41550291652SCodrin Ciubotariu 	.name = "mchp-pdmc",
41650291652SCodrin Ciubotariu 	.controls = mchp_pdmc_snd_controls,
41750291652SCodrin Ciubotariu 	.num_controls = ARRAY_SIZE(mchp_pdmc_snd_controls),
41850291652SCodrin Ciubotariu 	.open = &mchp_pdmc_open,
41950291652SCodrin Ciubotariu 	.close = &mchp_pdmc_close,
4207593e008SCharles Keepax 	.legacy_dai_naming = 1,
42138cb2a36SKuninori Morimoto 	.trigger_start = SND_SOC_TRIGGER_ORDER_LDC,
42250291652SCodrin Ciubotariu };
42350291652SCodrin Ciubotariu 
42450291652SCodrin Ciubotariu static const unsigned int mchp_pdmc_1mic[] = {1};
42550291652SCodrin Ciubotariu static const unsigned int mchp_pdmc_2mic[] = {1, 2};
42650291652SCodrin Ciubotariu static const unsigned int mchp_pdmc_3mic[] = {1, 2, 3};
42750291652SCodrin Ciubotariu static const unsigned int mchp_pdmc_4mic[] = {1, 2, 3, 4};
42850291652SCodrin Ciubotariu 
42950291652SCodrin Ciubotariu static const struct snd_pcm_hw_constraint_list mchp_pdmc_chan_constr[] = {
43050291652SCodrin Ciubotariu 	{
43150291652SCodrin Ciubotariu 		.list = mchp_pdmc_1mic,
43250291652SCodrin Ciubotariu 		.count = ARRAY_SIZE(mchp_pdmc_1mic),
43350291652SCodrin Ciubotariu 	},
43450291652SCodrin Ciubotariu 	{
43550291652SCodrin Ciubotariu 		.list = mchp_pdmc_2mic,
43650291652SCodrin Ciubotariu 		.count = ARRAY_SIZE(mchp_pdmc_2mic),
43750291652SCodrin Ciubotariu 	},
43850291652SCodrin Ciubotariu 	{
43950291652SCodrin Ciubotariu 		.list = mchp_pdmc_3mic,
44050291652SCodrin Ciubotariu 		.count = ARRAY_SIZE(mchp_pdmc_3mic),
44150291652SCodrin Ciubotariu 	},
44250291652SCodrin Ciubotariu 	{
44350291652SCodrin Ciubotariu 		.list = mchp_pdmc_4mic,
44450291652SCodrin Ciubotariu 		.count = ARRAY_SIZE(mchp_pdmc_4mic),
44550291652SCodrin Ciubotariu 	},
44650291652SCodrin Ciubotariu };
44750291652SCodrin Ciubotariu 
mchp_pdmc_startup(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)44850291652SCodrin Ciubotariu static int mchp_pdmc_startup(struct snd_pcm_substream *substream,
44950291652SCodrin Ciubotariu 			     struct snd_soc_dai *dai)
45050291652SCodrin Ciubotariu {
45150291652SCodrin Ciubotariu 	struct mchp_pdmc *dd = snd_soc_dai_get_drvdata(dai);
45250291652SCodrin Ciubotariu 
45350291652SCodrin Ciubotariu 	regmap_write(dd->regmap, MCHP_PDMC_CR, MCHP_PDMC_CR_SWRST);
45450291652SCodrin Ciubotariu 
45550291652SCodrin Ciubotariu 	snd_pcm_hw_constraint_list(substream->runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
45650291652SCodrin Ciubotariu 				   &mchp_pdmc_chan_constr[dd->mic_no - 1]);
45750291652SCodrin Ciubotariu 
45850291652SCodrin Ciubotariu 	return 0;
45950291652SCodrin Ciubotariu }
46050291652SCodrin Ciubotariu 
mchp_pdmc_dai_probe(struct snd_soc_dai * dai)46150291652SCodrin Ciubotariu static int mchp_pdmc_dai_probe(struct snd_soc_dai *dai)
46250291652SCodrin Ciubotariu {
46350291652SCodrin Ciubotariu 	struct mchp_pdmc *dd = snd_soc_dai_get_drvdata(dai);
46450291652SCodrin Ciubotariu 
46550291652SCodrin Ciubotariu 	snd_soc_dai_init_dma_data(dai, NULL, &dd->addr);
46650291652SCodrin Ciubotariu 
46750291652SCodrin Ciubotariu 	return 0;
46850291652SCodrin Ciubotariu }
46950291652SCodrin Ciubotariu 
mchp_pdmc_set_fmt(struct snd_soc_dai * dai,unsigned int fmt)47050291652SCodrin Ciubotariu static int mchp_pdmc_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
47150291652SCodrin Ciubotariu {
47250291652SCodrin Ciubotariu 	unsigned int fmt_master = fmt & SND_SOC_DAIFMT_MASTER_MASK;
47350291652SCodrin Ciubotariu 	unsigned int fmt_format = fmt & SND_SOC_DAIFMT_FORMAT_MASK;
47450291652SCodrin Ciubotariu 
47550291652SCodrin Ciubotariu 	/* IP needs to be bitclock master */
4760fd054a5SCharles Keepax 	if (fmt_master != SND_SOC_DAIFMT_BP_FP &&
4770fd054a5SCharles Keepax 	    fmt_master != SND_SOC_DAIFMT_BP_FC)
47850291652SCodrin Ciubotariu 		return -EINVAL;
47950291652SCodrin Ciubotariu 
48050291652SCodrin Ciubotariu 	/* IP supports only PDM interface */
48150291652SCodrin Ciubotariu 	if (fmt_format != SND_SOC_DAIFMT_PDM)
48250291652SCodrin Ciubotariu 		return -EINVAL;
48350291652SCodrin Ciubotariu 
48450291652SCodrin Ciubotariu 	return 0;
48550291652SCodrin Ciubotariu }
48650291652SCodrin Ciubotariu 
mchp_pdmc_mr_set_osr(int audio_filter_en,unsigned int osr)48750291652SCodrin Ciubotariu static u32 mchp_pdmc_mr_set_osr(int audio_filter_en, unsigned int osr)
48850291652SCodrin Ciubotariu {
48950291652SCodrin Ciubotariu 	if (audio_filter_en) {
49050291652SCodrin Ciubotariu 		switch (osr) {
49150291652SCodrin Ciubotariu 		case 64:
49250291652SCodrin Ciubotariu 			return MCHP_PDMC_MR_OSR64;
49350291652SCodrin Ciubotariu 		case 128:
49450291652SCodrin Ciubotariu 			return MCHP_PDMC_MR_OSR128;
49550291652SCodrin Ciubotariu 		case 256:
49650291652SCodrin Ciubotariu 			return MCHP_PDMC_MR_OSR256;
49750291652SCodrin Ciubotariu 		}
49850291652SCodrin Ciubotariu 	} else {
49950291652SCodrin Ciubotariu 		switch (osr) {
50050291652SCodrin Ciubotariu 		case 8:
50150291652SCodrin Ciubotariu 			return MCHP_PDMC_MR_SINC_OSR_8;
50250291652SCodrin Ciubotariu 		case 16:
50350291652SCodrin Ciubotariu 			return MCHP_PDMC_MR_SINC_OSR_16;
50450291652SCodrin Ciubotariu 		case 32:
50550291652SCodrin Ciubotariu 			return MCHP_PDMC_MR_SINC_OSR_32;
50650291652SCodrin Ciubotariu 		case 64:
50750291652SCodrin Ciubotariu 			return MCHP_PDMC_MR_SINC_OSR_64;
50850291652SCodrin Ciubotariu 		case 128:
50950291652SCodrin Ciubotariu 			return MCHP_PDMC_MR_SINC_OSR_128;
51050291652SCodrin Ciubotariu 		case 256:
51150291652SCodrin Ciubotariu 			return MCHP_PDMC_MR_SINC_OSR_256;
51250291652SCodrin Ciubotariu 		}
51350291652SCodrin Ciubotariu 	}
51450291652SCodrin Ciubotariu 	return 0;
51550291652SCodrin Ciubotariu }
51650291652SCodrin Ciubotariu 
mchp_pdmc_period_to_maxburst(int period_size)51750291652SCodrin Ciubotariu static inline int mchp_pdmc_period_to_maxburst(int period_size)
51850291652SCodrin Ciubotariu {
51950291652SCodrin Ciubotariu 	if (!(period_size % 8))
52050291652SCodrin Ciubotariu 		return 8;
52150291652SCodrin Ciubotariu 	if (!(period_size % 4))
52250291652SCodrin Ciubotariu 		return 4;
52350291652SCodrin Ciubotariu 	if (!(period_size % 2))
52450291652SCodrin Ciubotariu 		return 2;
52550291652SCodrin Ciubotariu 	return 1;
52650291652SCodrin Ciubotariu }
52750291652SCodrin Ciubotariu 
52850291652SCodrin Ciubotariu static struct snd_pcm_chmap_elem mchp_pdmc_std_chmaps[] = {
52950291652SCodrin Ciubotariu 	{ .channels = 1,
53050291652SCodrin Ciubotariu 	  .map = { SNDRV_CHMAP_MONO } },
53150291652SCodrin Ciubotariu 	{ .channels = 2,
53250291652SCodrin Ciubotariu 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
53350291652SCodrin Ciubotariu 	{ .channels = 3,
53450291652SCodrin Ciubotariu 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
53550291652SCodrin Ciubotariu 		   SNDRV_CHMAP_RL } },
53650291652SCodrin Ciubotariu 	{ .channels = 4,
53750291652SCodrin Ciubotariu 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
53850291652SCodrin Ciubotariu 		   SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
53950291652SCodrin Ciubotariu 	{ }
54050291652SCodrin Ciubotariu };
54150291652SCodrin Ciubotariu 
mchp_pdmc_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)54250291652SCodrin Ciubotariu static int mchp_pdmc_hw_params(struct snd_pcm_substream *substream,
54350291652SCodrin Ciubotariu 			       struct snd_pcm_hw_params *params,
54450291652SCodrin Ciubotariu 			       struct snd_soc_dai *dai)
54550291652SCodrin Ciubotariu {
54650291652SCodrin Ciubotariu 	struct mchp_pdmc *dd = snd_soc_dai_get_drvdata(dai);
54750291652SCodrin Ciubotariu 	struct snd_soc_component *comp = dai->component;
54850291652SCodrin Ciubotariu 	unsigned long gclk_rate = 0;
54950291652SCodrin Ciubotariu 	unsigned long best_diff_rate = ~0UL;
55050291652SCodrin Ciubotariu 	unsigned int channels = params_channels(params);
55150291652SCodrin Ciubotariu 	unsigned int osr = 0, osr_start;
55250291652SCodrin Ciubotariu 	unsigned int fs = params_rate(params);
55350291652SCodrin Ciubotariu 	u32 mr_val = 0;
55450291652SCodrin Ciubotariu 	u32 cfgr_val = 0;
55550291652SCodrin Ciubotariu 	int i;
55650291652SCodrin Ciubotariu 	int ret;
55750291652SCodrin Ciubotariu 
55850291652SCodrin Ciubotariu 	dev_dbg(comp->dev, "%s() rate=%u format=%#x width=%u channels=%u\n",
55950291652SCodrin Ciubotariu 		__func__, params_rate(params), params_format(params),
56050291652SCodrin Ciubotariu 		params_width(params), params_channels(params));
56150291652SCodrin Ciubotariu 
56250291652SCodrin Ciubotariu 	if (channels > dd->mic_no) {
56350291652SCodrin Ciubotariu 		dev_err(comp->dev, "more channels %u than microphones %d\n",
56450291652SCodrin Ciubotariu 			channels, dd->mic_no);
56550291652SCodrin Ciubotariu 		return -EINVAL;
56650291652SCodrin Ciubotariu 	}
56750291652SCodrin Ciubotariu 
56850291652SCodrin Ciubotariu 	dd->pdmcen = 0;
56950291652SCodrin Ciubotariu 	for (i = 0; i < channels; i++) {
57050291652SCodrin Ciubotariu 		dd->pdmcen |= MCHP_PDMC_MR_PDMCEN(i);
57150291652SCodrin Ciubotariu 		if (dd->channel_mic_map[i].ds_pos)
57250291652SCodrin Ciubotariu 			cfgr_val |= MCHP_PDMC_CFGR_PDMSEL(i);
57350291652SCodrin Ciubotariu 		if (dd->channel_mic_map[i].clk_edge)
57450291652SCodrin Ciubotariu 			cfgr_val |= MCHP_PDMC_CFGR_BSSEL(i);
57550291652SCodrin Ciubotariu 	}
57650291652SCodrin Ciubotariu 
57750291652SCodrin Ciubotariu 	for (osr_start = dd->audio_filter_en ? 64 : 8;
57850291652SCodrin Ciubotariu 	     osr_start <= 256 && best_diff_rate; osr_start *= 2) {
57950291652SCodrin Ciubotariu 		long round_rate;
58050291652SCodrin Ciubotariu 		unsigned long diff_rate;
58150291652SCodrin Ciubotariu 
58250291652SCodrin Ciubotariu 		round_rate = clk_round_rate(dd->gclk,
58350291652SCodrin Ciubotariu 					    (unsigned long)fs * 16 * osr_start);
58450291652SCodrin Ciubotariu 		if (round_rate < 0)
58550291652SCodrin Ciubotariu 			continue;
58650291652SCodrin Ciubotariu 		diff_rate = abs((fs * 16 * osr_start) - round_rate);
58750291652SCodrin Ciubotariu 		if (diff_rate < best_diff_rate) {
58850291652SCodrin Ciubotariu 			best_diff_rate = diff_rate;
58950291652SCodrin Ciubotariu 			osr = osr_start;
59050291652SCodrin Ciubotariu 			gclk_rate = fs * 16 * osr;
59150291652SCodrin Ciubotariu 		}
59250291652SCodrin Ciubotariu 	}
59350291652SCodrin Ciubotariu 	if (!gclk_rate) {
59450291652SCodrin Ciubotariu 		dev_err(comp->dev, "invalid sampling rate: %u\n", fs);
59550291652SCodrin Ciubotariu 		return -EINVAL;
59650291652SCodrin Ciubotariu 	}
59750291652SCodrin Ciubotariu 
598e8c8e9deSClaudiu Beznea 	/* CLK is enabled by runtime PM. */
599e8c8e9deSClaudiu Beznea 	clk_disable_unprepare(dd->gclk);
600e8c8e9deSClaudiu Beznea 
60150291652SCodrin Ciubotariu 	/* set the rate */
60250291652SCodrin Ciubotariu 	ret = clk_set_rate(dd->gclk, gclk_rate);
603e8c8e9deSClaudiu Beznea 	clk_prepare_enable(dd->gclk);
60450291652SCodrin Ciubotariu 	if (ret) {
60550291652SCodrin Ciubotariu 		dev_err(comp->dev, "unable to set rate %lu to GCLK: %d\n",
60650291652SCodrin Ciubotariu 			gclk_rate, ret);
60750291652SCodrin Ciubotariu 		return ret;
60850291652SCodrin Ciubotariu 	}
60950291652SCodrin Ciubotariu 
61050291652SCodrin Ciubotariu 	mr_val |= mchp_pdmc_mr_set_osr(dd->audio_filter_en, osr);
61150291652SCodrin Ciubotariu 
61212974257SClaudiu Beznea 	mr_val |= FIELD_PREP(MCHP_PDMC_MR_SINCORDER_MASK, dd->sinc_order);
61350291652SCodrin Ciubotariu 
61450291652SCodrin Ciubotariu 	dd->addr.maxburst = mchp_pdmc_period_to_maxburst(snd_pcm_lib_period_bytes(substream));
61512974257SClaudiu Beznea 	mr_val |= FIELD_PREP(MCHP_PDMC_MR_CHUNK_MASK, dd->addr.maxburst);
61650291652SCodrin Ciubotariu 	dev_dbg(comp->dev, "maxburst set to %d\n", dd->addr.maxburst);
61750291652SCodrin Ciubotariu 
61850291652SCodrin Ciubotariu 	snd_soc_component_update_bits(comp, MCHP_PDMC_MR,
61950291652SCodrin Ciubotariu 				      MCHP_PDMC_MR_OSR_MASK |
62050291652SCodrin Ciubotariu 				      MCHP_PDMC_MR_SINCORDER_MASK |
62150291652SCodrin Ciubotariu 				      MCHP_PDMC_MR_SINC_OSR_MASK |
62250291652SCodrin Ciubotariu 				      MCHP_PDMC_MR_CHUNK_MASK, mr_val);
62350291652SCodrin Ciubotariu 
62450291652SCodrin Ciubotariu 	snd_soc_component_write(comp, MCHP_PDMC_CFGR, cfgr_val);
62550291652SCodrin Ciubotariu 
62650291652SCodrin Ciubotariu 	return 0;
62750291652SCodrin Ciubotariu }
62850291652SCodrin Ciubotariu 
mchp_pdmc_noise_filter_workaround(struct mchp_pdmc * dd)629c5682e2bSClaudiu Beznea static void mchp_pdmc_noise_filter_workaround(struct mchp_pdmc *dd)
630c5682e2bSClaudiu Beznea {
631c5682e2bSClaudiu Beznea 	u32 tmp, steps = 16;
632c5682e2bSClaudiu Beznea 
633c5682e2bSClaudiu Beznea 	/*
634c5682e2bSClaudiu Beznea 	 * PDMC doesn't wait for microphones' startup time thus the acquisition
635c5682e2bSClaudiu Beznea 	 * may start before the microphones are ready leading to poc noises at
636c5682e2bSClaudiu Beznea 	 * the beginning of capture. To avoid this, we need to wait 50ms (in
637c5682e2bSClaudiu Beznea 	 * normal startup procedure) or 150 ms (worst case after resume from sleep
638c5682e2bSClaudiu Beznea 	 * states) after microphones are enabled and then clear the FIFOs (by
639c5682e2bSClaudiu Beznea 	 * reading the RHR 16 times) and possible interrupts before continuing.
640c5682e2bSClaudiu Beznea 	 * Also, for this to work the DMA needs to be started after interrupts
641c5682e2bSClaudiu Beznea 	 * are enabled.
642c5682e2bSClaudiu Beznea 	 */
643c5682e2bSClaudiu Beznea 	usleep_range(dd->startup_delay_us, dd->startup_delay_us + 5);
644c5682e2bSClaudiu Beznea 
645c5682e2bSClaudiu Beznea 	while (steps--)
646c5682e2bSClaudiu Beznea 		regmap_read(dd->regmap, MCHP_PDMC_RHR, &tmp);
647c5682e2bSClaudiu Beznea 
648c5682e2bSClaudiu Beznea 	/* Clear interrupts. */
649c5682e2bSClaudiu Beznea 	regmap_read(dd->regmap, MCHP_PDMC_ISR, &tmp);
650c5682e2bSClaudiu Beznea }
651c5682e2bSClaudiu Beznea 
mchp_pdmc_trigger(struct snd_pcm_substream * substream,int cmd,struct snd_soc_dai * dai)65250291652SCodrin Ciubotariu static int mchp_pdmc_trigger(struct snd_pcm_substream *substream,
65350291652SCodrin Ciubotariu 			     int cmd, struct snd_soc_dai *dai)
65450291652SCodrin Ciubotariu {
65550291652SCodrin Ciubotariu 	struct mchp_pdmc *dd = snd_soc_dai_get_drvdata(dai);
65650291652SCodrin Ciubotariu 	struct snd_soc_component *cpu = dai->component;
65750291652SCodrin Ciubotariu #ifdef DEBUG
65850291652SCodrin Ciubotariu 	u32 val;
65950291652SCodrin Ciubotariu #endif
66050291652SCodrin Ciubotariu 
66150291652SCodrin Ciubotariu 	switch (cmd) {
66250291652SCodrin Ciubotariu 	case SNDRV_PCM_TRIGGER_RESUME:
663404c61c4SClaudiu Beznea 	case SNDRV_PCM_TRIGGER_START:
664404c61c4SClaudiu Beznea 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
66550291652SCodrin Ciubotariu 		snd_soc_component_update_bits(cpu, MCHP_PDMC_MR,
66650291652SCodrin Ciubotariu 					      MCHP_PDMC_MR_PDMCEN_MASK,
66750291652SCodrin Ciubotariu 					      dd->pdmcen);
668c5682e2bSClaudiu Beznea 
669c5682e2bSClaudiu Beznea 		mchp_pdmc_noise_filter_workaround(dd);
670c5682e2bSClaudiu Beznea 
671c5682e2bSClaudiu Beznea 		/* Enable interrupts. */
672c5682e2bSClaudiu Beznea 		regmap_write(dd->regmap, MCHP_PDMC_IER, dd->suspend_irq |
673c5682e2bSClaudiu Beznea 			     MCHP_PDMC_IR_RXOVR | MCHP_PDMC_IR_RXUDR);
674c5682e2bSClaudiu Beznea 		dd->suspend_irq = 0;
67550291652SCodrin Ciubotariu 		break;
67650291652SCodrin Ciubotariu 	case SNDRV_PCM_TRIGGER_SUSPEND:
677404c61c4SClaudiu Beznea 		regmap_read(dd->regmap, MCHP_PDMC_IMR, &dd->suspend_irq);
678404c61c4SClaudiu Beznea 		fallthrough;
679404c61c4SClaudiu Beznea 	case SNDRV_PCM_TRIGGER_STOP:
68050291652SCodrin Ciubotariu 		/* Disable overrun and underrun error interrupts */
681404c61c4SClaudiu Beznea 		regmap_write(dd->regmap, MCHP_PDMC_IDR, dd->suspend_irq |
68250291652SCodrin Ciubotariu 			     MCHP_PDMC_IR_RXOVR | MCHP_PDMC_IR_RXUDR);
683404c61c4SClaudiu Beznea 		fallthrough;
684404c61c4SClaudiu Beznea 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
68550291652SCodrin Ciubotariu 		snd_soc_component_update_bits(cpu, MCHP_PDMC_MR,
68650291652SCodrin Ciubotariu 					      MCHP_PDMC_MR_PDMCEN_MASK, 0);
68750291652SCodrin Ciubotariu 		break;
68850291652SCodrin Ciubotariu 	default:
68950291652SCodrin Ciubotariu 		return -EINVAL;
69050291652SCodrin Ciubotariu 	}
69150291652SCodrin Ciubotariu 
69250291652SCodrin Ciubotariu #ifdef DEBUG
69350291652SCodrin Ciubotariu 	regmap_read(dd->regmap, MCHP_PDMC_MR, &val);
69450291652SCodrin Ciubotariu 	dev_dbg(dd->dev, "MR (0x%02x): 0x%08x\n", MCHP_PDMC_MR, val);
69550291652SCodrin Ciubotariu 	regmap_read(dd->regmap, MCHP_PDMC_CFGR, &val);
69650291652SCodrin Ciubotariu 	dev_dbg(dd->dev, "CFGR (0x%02x): 0x%08x\n", MCHP_PDMC_CFGR, val);
69750291652SCodrin Ciubotariu 	regmap_read(dd->regmap, MCHP_PDMC_IMR, &val);
69850291652SCodrin Ciubotariu 	dev_dbg(dd->dev, "IMR (0x%02x): 0x%08x\n", MCHP_PDMC_IMR, val);
69950291652SCodrin Ciubotariu #endif
70050291652SCodrin Ciubotariu 
70150291652SCodrin Ciubotariu 	return 0;
70250291652SCodrin Ciubotariu }
70350291652SCodrin Ciubotariu 
mchp_pdmc_add_chmap_ctls(struct snd_pcm * pcm,struct mchp_pdmc * dd)70450291652SCodrin Ciubotariu static int mchp_pdmc_add_chmap_ctls(struct snd_pcm *pcm, struct mchp_pdmc *dd)
70550291652SCodrin Ciubotariu {
70650291652SCodrin Ciubotariu 	struct mchp_pdmc_chmap *info;
70750291652SCodrin Ciubotariu 	struct snd_kcontrol_new knew = {
70850291652SCodrin Ciubotariu 		.iface = SNDRV_CTL_ELEM_IFACE_PCM,
70950291652SCodrin Ciubotariu 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
71050291652SCodrin Ciubotariu 			SNDRV_CTL_ELEM_ACCESS_TLV_READ |
71150291652SCodrin Ciubotariu 			SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
71250291652SCodrin Ciubotariu 		.info = mchp_pdmc_chmap_ctl_info,
71350291652SCodrin Ciubotariu 		.get = mchp_pdmc_chmap_ctl_get,
71450291652SCodrin Ciubotariu 		.put = mchp_pdmc_chmap_ctl_put,
71550291652SCodrin Ciubotariu 		.tlv.c = mchp_pdmc_chmap_ctl_tlv,
71650291652SCodrin Ciubotariu 	};
71750291652SCodrin Ciubotariu 	int err;
71850291652SCodrin Ciubotariu 
71950291652SCodrin Ciubotariu 	if (WARN_ON(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].chmap_kctl))
72050291652SCodrin Ciubotariu 		return -EBUSY;
72150291652SCodrin Ciubotariu 	info = kzalloc(sizeof(*info), GFP_KERNEL);
72250291652SCodrin Ciubotariu 	if (!info)
72350291652SCodrin Ciubotariu 		return -ENOMEM;
72450291652SCodrin Ciubotariu 	info->pcm = pcm;
72550291652SCodrin Ciubotariu 	info->dd = dd;
72650291652SCodrin Ciubotariu 	info->chmap = mchp_pdmc_std_chmaps;
72750291652SCodrin Ciubotariu 	knew.name = "Capture Channel Map";
72850291652SCodrin Ciubotariu 	knew.device = pcm->device;
72950291652SCodrin Ciubotariu 	knew.count = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
73050291652SCodrin Ciubotariu 	info->kctl = snd_ctl_new1(&knew, info);
73150291652SCodrin Ciubotariu 	if (!info->kctl) {
73250291652SCodrin Ciubotariu 		kfree(info);
73350291652SCodrin Ciubotariu 		return -ENOMEM;
73450291652SCodrin Ciubotariu 	}
73550291652SCodrin Ciubotariu 	info->kctl->private_free = mchp_pdmc_chmap_ctl_private_free;
73650291652SCodrin Ciubotariu 	err = snd_ctl_add(pcm->card, info->kctl);
73750291652SCodrin Ciubotariu 	if (err < 0)
73850291652SCodrin Ciubotariu 		return err;
73950291652SCodrin Ciubotariu 	pcm->streams[SNDRV_PCM_STREAM_CAPTURE].chmap_kctl = info->kctl;
74050291652SCodrin Ciubotariu 	return 0;
74150291652SCodrin Ciubotariu }
74250291652SCodrin Ciubotariu 
mchp_pdmc_pcm_new(struct snd_soc_pcm_runtime * rtd,struct snd_soc_dai * dai)74350291652SCodrin Ciubotariu static int mchp_pdmc_pcm_new(struct snd_soc_pcm_runtime *rtd,
74450291652SCodrin Ciubotariu 			     struct snd_soc_dai *dai)
74550291652SCodrin Ciubotariu {
74650291652SCodrin Ciubotariu 	struct mchp_pdmc *dd = snd_soc_dai_get_drvdata(dai);
74750291652SCodrin Ciubotariu 	int ret;
74850291652SCodrin Ciubotariu 
74950291652SCodrin Ciubotariu 	ret = mchp_pdmc_add_chmap_ctls(rtd->pcm, dd);
75051124a30SClaudiu Beznea 	if (ret < 0)
75150291652SCodrin Ciubotariu 		dev_err(dd->dev, "failed to add channel map controls: %d\n", ret);
75250291652SCodrin Ciubotariu 
75351124a30SClaudiu Beznea 	return ret;
75450291652SCodrin Ciubotariu }
75550291652SCodrin Ciubotariu 
7562ff8a43dSKuninori Morimoto static const struct snd_soc_dai_ops mchp_pdmc_dai_ops = {
75750291652SCodrin Ciubotariu 	.probe		= mchp_pdmc_dai_probe,
7582ff8a43dSKuninori Morimoto 	.set_fmt	= mchp_pdmc_set_fmt,
7592ff8a43dSKuninori Morimoto 	.startup	= mchp_pdmc_startup,
7602ff8a43dSKuninori Morimoto 	.hw_params	= mchp_pdmc_hw_params,
7612ff8a43dSKuninori Morimoto 	.trigger	= mchp_pdmc_trigger,
7622ff8a43dSKuninori Morimoto 	.pcm_new	= &mchp_pdmc_pcm_new,
7632ff8a43dSKuninori Morimoto };
7642ff8a43dSKuninori Morimoto 
7652ff8a43dSKuninori Morimoto static struct snd_soc_dai_driver mchp_pdmc_dai = {
76650291652SCodrin Ciubotariu 	.capture = {
76750291652SCodrin Ciubotariu 		.stream_name	= "Capture",
76850291652SCodrin Ciubotariu 		.channels_min	= 1,
76950291652SCodrin Ciubotariu 		.channels_max	= 4,
77050291652SCodrin Ciubotariu 		.rate_min	= 8000,
77150291652SCodrin Ciubotariu 		.rate_max	= 192000,
77250291652SCodrin Ciubotariu 		.rates		= SNDRV_PCM_RATE_KNOT,
77350291652SCodrin Ciubotariu 		.formats	= SNDRV_PCM_FMTBIT_S24_LE,
77450291652SCodrin Ciubotariu 	},
77550291652SCodrin Ciubotariu 	.ops = &mchp_pdmc_dai_ops,
77650291652SCodrin Ciubotariu };
77750291652SCodrin Ciubotariu 
77850291652SCodrin Ciubotariu /* PDMC interrupt handler */
mchp_pdmc_interrupt(int irq,void * dev_id)77950291652SCodrin Ciubotariu static irqreturn_t mchp_pdmc_interrupt(int irq, void *dev_id)
78050291652SCodrin Ciubotariu {
781cb72b29cSClaudiu Beznea 	struct mchp_pdmc *dd = dev_id;
78250291652SCodrin Ciubotariu 	u32 isr, msr, pending;
78350291652SCodrin Ciubotariu 	irqreturn_t ret = IRQ_NONE;
78450291652SCodrin Ciubotariu 
78550291652SCodrin Ciubotariu 	regmap_read(dd->regmap, MCHP_PDMC_ISR, &isr);
78650291652SCodrin Ciubotariu 	regmap_read(dd->regmap, MCHP_PDMC_IMR, &msr);
78750291652SCodrin Ciubotariu 
78850291652SCodrin Ciubotariu 	pending = isr & msr;
78950291652SCodrin Ciubotariu 	dev_dbg(dd->dev, "ISR (0x%02x): 0x%08x, IMR (0x%02x): 0x%08x, pending: 0x%08x\n",
79050291652SCodrin Ciubotariu 		MCHP_PDMC_ISR, isr, MCHP_PDMC_IMR, msr, pending);
79150291652SCodrin Ciubotariu 	if (!pending)
79250291652SCodrin Ciubotariu 		return IRQ_NONE;
79350291652SCodrin Ciubotariu 
79450291652SCodrin Ciubotariu 	if (pending & MCHP_PDMC_IR_RXUDR) {
79550291652SCodrin Ciubotariu 		dev_warn(dd->dev, "underrun detected\n");
79650291652SCodrin Ciubotariu 		regmap_write(dd->regmap, MCHP_PDMC_IDR, MCHP_PDMC_IR_RXUDR);
79750291652SCodrin Ciubotariu 		ret = IRQ_HANDLED;
79850291652SCodrin Ciubotariu 	}
79950291652SCodrin Ciubotariu 	if (pending & MCHP_PDMC_IR_RXOVR) {
80050291652SCodrin Ciubotariu 		dev_warn(dd->dev, "overrun detected\n");
80150291652SCodrin Ciubotariu 		regmap_write(dd->regmap, MCHP_PDMC_IDR, MCHP_PDMC_IR_RXOVR);
80250291652SCodrin Ciubotariu 		ret = IRQ_HANDLED;
80350291652SCodrin Ciubotariu 	}
80450291652SCodrin Ciubotariu 
80550291652SCodrin Ciubotariu 	return ret;
80650291652SCodrin Ciubotariu }
80750291652SCodrin Ciubotariu 
80850291652SCodrin Ciubotariu /* regmap configuration */
mchp_pdmc_readable_reg(struct device * dev,unsigned int reg)80950291652SCodrin Ciubotariu static bool mchp_pdmc_readable_reg(struct device *dev, unsigned int reg)
81050291652SCodrin Ciubotariu {
81150291652SCodrin Ciubotariu 	switch (reg) {
81250291652SCodrin Ciubotariu 	case MCHP_PDMC_MR:
81350291652SCodrin Ciubotariu 	case MCHP_PDMC_CFGR:
81450291652SCodrin Ciubotariu 	case MCHP_PDMC_IMR:
81550291652SCodrin Ciubotariu 	case MCHP_PDMC_ISR:
816c5682e2bSClaudiu Beznea 	case MCHP_PDMC_RHR:
81750291652SCodrin Ciubotariu 	case MCHP_PDMC_VER:
81850291652SCodrin Ciubotariu 		return true;
81950291652SCodrin Ciubotariu 	default:
82050291652SCodrin Ciubotariu 		return false;
82150291652SCodrin Ciubotariu 	}
82250291652SCodrin Ciubotariu }
82350291652SCodrin Ciubotariu 
mchp_pdmc_writeable_reg(struct device * dev,unsigned int reg)82450291652SCodrin Ciubotariu static bool mchp_pdmc_writeable_reg(struct device *dev, unsigned int reg)
82550291652SCodrin Ciubotariu {
82650291652SCodrin Ciubotariu 	switch (reg) {
82750291652SCodrin Ciubotariu 	case MCHP_PDMC_CR:
82850291652SCodrin Ciubotariu 	case MCHP_PDMC_MR:
82950291652SCodrin Ciubotariu 	case MCHP_PDMC_CFGR:
83050291652SCodrin Ciubotariu 	case MCHP_PDMC_IER:
83150291652SCodrin Ciubotariu 	case MCHP_PDMC_IDR:
83250291652SCodrin Ciubotariu 		return true;
83350291652SCodrin Ciubotariu 	default:
83450291652SCodrin Ciubotariu 		return false;
83550291652SCodrin Ciubotariu 	}
83650291652SCodrin Ciubotariu }
83750291652SCodrin Ciubotariu 
mchp_pdmc_volatile_reg(struct device * dev,unsigned int reg)838c5682e2bSClaudiu Beznea static bool mchp_pdmc_volatile_reg(struct device *dev, unsigned int reg)
839c5682e2bSClaudiu Beznea {
840c5682e2bSClaudiu Beznea 	switch (reg) {
841c5682e2bSClaudiu Beznea 	case MCHP_PDMC_ISR:
842c5682e2bSClaudiu Beznea 	case MCHP_PDMC_RHR:
843c5682e2bSClaudiu Beznea 		return true;
844c5682e2bSClaudiu Beznea 	default:
845c5682e2bSClaudiu Beznea 		return false;
846c5682e2bSClaudiu Beznea 	}
847c5682e2bSClaudiu Beznea }
848c5682e2bSClaudiu Beznea 
mchp_pdmc_precious_reg(struct device * dev,unsigned int reg)84950291652SCodrin Ciubotariu static bool mchp_pdmc_precious_reg(struct device *dev, unsigned int reg)
85050291652SCodrin Ciubotariu {
85150291652SCodrin Ciubotariu 	switch (reg) {
85250291652SCodrin Ciubotariu 	case MCHP_PDMC_RHR:
85350291652SCodrin Ciubotariu 	case MCHP_PDMC_ISR:
85450291652SCodrin Ciubotariu 		return true;
85550291652SCodrin Ciubotariu 	default:
85650291652SCodrin Ciubotariu 		return false;
85750291652SCodrin Ciubotariu 	}
85850291652SCodrin Ciubotariu }
85950291652SCodrin Ciubotariu 
86050291652SCodrin Ciubotariu static const struct regmap_config mchp_pdmc_regmap_config = {
86150291652SCodrin Ciubotariu 	.reg_bits	= 32,
86250291652SCodrin Ciubotariu 	.reg_stride	= 4,
86350291652SCodrin Ciubotariu 	.val_bits	= 32,
86450291652SCodrin Ciubotariu 	.max_register	= MCHP_PDMC_VER,
86550291652SCodrin Ciubotariu 	.readable_reg	= mchp_pdmc_readable_reg,
86650291652SCodrin Ciubotariu 	.writeable_reg	= mchp_pdmc_writeable_reg,
86750291652SCodrin Ciubotariu 	.precious_reg	= mchp_pdmc_precious_reg,
868c5682e2bSClaudiu Beznea 	.volatile_reg	= mchp_pdmc_volatile_reg,
869e8c8e9deSClaudiu Beznea 	.cache_type	= REGCACHE_FLAT,
87050291652SCodrin Ciubotariu };
87150291652SCodrin Ciubotariu 
mchp_pdmc_dt_init(struct mchp_pdmc * dd)87250291652SCodrin Ciubotariu static int mchp_pdmc_dt_init(struct mchp_pdmc *dd)
87350291652SCodrin Ciubotariu {
87450291652SCodrin Ciubotariu 	struct device_node *np = dd->dev->of_node;
87550291652SCodrin Ciubotariu 	bool mic_ch[MCHP_PDMC_DS_NO][MCHP_PDMC_EDGE_NO] = {0};
87650291652SCodrin Ciubotariu 	int i;
87750291652SCodrin Ciubotariu 	int ret;
87850291652SCodrin Ciubotariu 
87950291652SCodrin Ciubotariu 	if (!np) {
88050291652SCodrin Ciubotariu 		dev_err(dd->dev, "device node not found\n");
88150291652SCodrin Ciubotariu 		return -EINVAL;
88250291652SCodrin Ciubotariu 	}
88350291652SCodrin Ciubotariu 
88450291652SCodrin Ciubotariu 	dd->mic_no = of_property_count_u32_elems(np, "microchip,mic-pos");
88550291652SCodrin Ciubotariu 	if (dd->mic_no < 0) {
886c639e85eSCodrin Ciubotariu 		dev_err(dd->dev, "failed to get microchip,mic-pos: %d",
88750291652SCodrin Ciubotariu 			dd->mic_no);
88850291652SCodrin Ciubotariu 		return dd->mic_no;
88950291652SCodrin Ciubotariu 	}
89050291652SCodrin Ciubotariu 	if (!dd->mic_no || dd->mic_no % 2 ||
89150291652SCodrin Ciubotariu 	    dd->mic_no / 2 > MCHP_PDMC_MAX_CHANNELS) {
892c639e85eSCodrin Ciubotariu 		dev_err(dd->dev, "invalid array length for microchip,mic-pos: %d",
89350291652SCodrin Ciubotariu 			dd->mic_no);
89450291652SCodrin Ciubotariu 		return -EINVAL;
89550291652SCodrin Ciubotariu 	}
89650291652SCodrin Ciubotariu 
89750291652SCodrin Ciubotariu 	dd->mic_no /= 2;
89850291652SCodrin Ciubotariu 
8996b6bb5e2SColin Ian King 	dev_info(dd->dev, "%d PDM microphones declared\n", dd->mic_no);
90050291652SCodrin Ciubotariu 
901c639e85eSCodrin Ciubotariu 	/*
902c639e85eSCodrin Ciubotariu 	 * by default, we consider the order of microphones in
903c639e85eSCodrin Ciubotariu 	 * microchip,mic-pos to be the same with the channel mapping;
904c639e85eSCodrin Ciubotariu 	 * 1st microphone channel 0, 2nd microphone channel 1, etc.
90550291652SCodrin Ciubotariu 	 */
90650291652SCodrin Ciubotariu 	for (i = 0; i < dd->mic_no; i++) {
90750291652SCodrin Ciubotariu 		int ds;
90850291652SCodrin Ciubotariu 		int edge;
90950291652SCodrin Ciubotariu 
91050291652SCodrin Ciubotariu 		ret = of_property_read_u32_index(np, "microchip,mic-pos", i * 2,
91150291652SCodrin Ciubotariu 						 &ds);
91250291652SCodrin Ciubotariu 		if (ret) {
91350291652SCodrin Ciubotariu 			dev_err(dd->dev,
91450291652SCodrin Ciubotariu 				"failed to get value no %d value from microchip,mic-pos: %d",
91550291652SCodrin Ciubotariu 				i * 2, ret);
91650291652SCodrin Ciubotariu 			return ret;
91750291652SCodrin Ciubotariu 		}
91850291652SCodrin Ciubotariu 		if (ds >= MCHP_PDMC_DS_NO) {
91950291652SCodrin Ciubotariu 			dev_err(dd->dev,
92050291652SCodrin Ciubotariu 				"invalid DS index in microchip,mic-pos array: %d",
92150291652SCodrin Ciubotariu 				ds);
92250291652SCodrin Ciubotariu 			return -EINVAL;
92350291652SCodrin Ciubotariu 		}
92450291652SCodrin Ciubotariu 
92550291652SCodrin Ciubotariu 		ret = of_property_read_u32_index(np, "microchip,mic-pos", i * 2 + 1,
92650291652SCodrin Ciubotariu 						 &edge);
92750291652SCodrin Ciubotariu 		if (ret) {
92850291652SCodrin Ciubotariu 			dev_err(dd->dev,
92950291652SCodrin Ciubotariu 				"failed to get value no %d value from microchip,mic-pos: %d",
93050291652SCodrin Ciubotariu 				i * 2 + 1, ret);
93150291652SCodrin Ciubotariu 			return ret;
93250291652SCodrin Ciubotariu 		}
93350291652SCodrin Ciubotariu 
93450291652SCodrin Ciubotariu 		if (edge != MCHP_PDMC_CLK_POSITIVE &&
93550291652SCodrin Ciubotariu 		    edge != MCHP_PDMC_CLK_NEGATIVE) {
93650291652SCodrin Ciubotariu 			dev_err(dd->dev,
93750291652SCodrin Ciubotariu 				"invalid edge in microchip,mic-pos array: %d", edge);
93850291652SCodrin Ciubotariu 			return -EINVAL;
93950291652SCodrin Ciubotariu 		}
94050291652SCodrin Ciubotariu 		if (mic_ch[ds][edge]) {
94150291652SCodrin Ciubotariu 			dev_err(dd->dev,
94250291652SCodrin Ciubotariu 				"duplicated mic (DS %d, edge %d) in microchip,mic-pos array",
94350291652SCodrin Ciubotariu 				ds, edge);
94450291652SCodrin Ciubotariu 			return -EINVAL;
94550291652SCodrin Ciubotariu 		}
94650291652SCodrin Ciubotariu 		mic_ch[ds][edge] = true;
94750291652SCodrin Ciubotariu 		dd->channel_mic_map[i].ds_pos = ds;
94850291652SCodrin Ciubotariu 		dd->channel_mic_map[i].clk_edge = edge;
94950291652SCodrin Ciubotariu 	}
95050291652SCodrin Ciubotariu 
951c5682e2bSClaudiu Beznea 	dd->startup_delay_us = 150000;
952c5682e2bSClaudiu Beznea 	of_property_read_u32(np, "microchip,startup-delay-us", &dd->startup_delay_us);
953c5682e2bSClaudiu Beznea 
95450291652SCodrin Ciubotariu 	return 0;
95550291652SCodrin Ciubotariu }
95650291652SCodrin Ciubotariu 
95750291652SCodrin Ciubotariu /* used to clean the channel index found on RHR's MSB */
mchp_pdmc_process(struct snd_pcm_substream * substream,int channel,unsigned long hwoff,unsigned long bytes)95850291652SCodrin Ciubotariu static int mchp_pdmc_process(struct snd_pcm_substream *substream,
95950291652SCodrin Ciubotariu 			     int channel, unsigned long hwoff,
96069d0fd34STakashi Iwai 			     unsigned long bytes)
96150291652SCodrin Ciubotariu {
96250291652SCodrin Ciubotariu 	struct snd_pcm_runtime *runtime = substream->runtime;
96350291652SCodrin Ciubotariu 	u8 *dma_ptr = runtime->dma_area + hwoff +
96450291652SCodrin Ciubotariu 		      channel * (runtime->dma_bytes / runtime->channels);
96550291652SCodrin Ciubotariu 	u8 *dma_ptr_end = dma_ptr + bytes;
96650291652SCodrin Ciubotariu 	unsigned int sample_size = samples_to_bytes(runtime, 1);
96750291652SCodrin Ciubotariu 
96850291652SCodrin Ciubotariu 	for (; dma_ptr < dma_ptr_end; dma_ptr += sample_size)
96950291652SCodrin Ciubotariu 		*dma_ptr = 0;
97050291652SCodrin Ciubotariu 
97150291652SCodrin Ciubotariu 	return 0;
97250291652SCodrin Ciubotariu }
97350291652SCodrin Ciubotariu 
97450291652SCodrin Ciubotariu static struct snd_dmaengine_pcm_config mchp_pdmc_config = {
97550291652SCodrin Ciubotariu 	.process = mchp_pdmc_process,
9762bde1985SCodrin Ciubotariu 	.prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
97750291652SCodrin Ciubotariu };
97850291652SCodrin Ciubotariu 
mchp_pdmc_runtime_suspend(struct device * dev)979e8c8e9deSClaudiu Beznea static int mchp_pdmc_runtime_suspend(struct device *dev)
980e8c8e9deSClaudiu Beznea {
981e8c8e9deSClaudiu Beznea 	struct mchp_pdmc *dd = dev_get_drvdata(dev);
982e8c8e9deSClaudiu Beznea 
983e8c8e9deSClaudiu Beznea 	regcache_cache_only(dd->regmap, true);
984e8c8e9deSClaudiu Beznea 
985e8c8e9deSClaudiu Beznea 	clk_disable_unprepare(dd->gclk);
986e8c8e9deSClaudiu Beznea 	clk_disable_unprepare(dd->pclk);
987e8c8e9deSClaudiu Beznea 
988e8c8e9deSClaudiu Beznea 	return 0;
989e8c8e9deSClaudiu Beznea }
990e8c8e9deSClaudiu Beznea 
mchp_pdmc_runtime_resume(struct device * dev)991e8c8e9deSClaudiu Beznea static int mchp_pdmc_runtime_resume(struct device *dev)
992e8c8e9deSClaudiu Beznea {
993e8c8e9deSClaudiu Beznea 	struct mchp_pdmc *dd = dev_get_drvdata(dev);
994e8c8e9deSClaudiu Beznea 	int ret;
995e8c8e9deSClaudiu Beznea 
996e8c8e9deSClaudiu Beznea 	ret = clk_prepare_enable(dd->pclk);
997e8c8e9deSClaudiu Beznea 	if (ret) {
998e8c8e9deSClaudiu Beznea 		dev_err(dd->dev,
999e8c8e9deSClaudiu Beznea 			"failed to enable the peripheral clock: %d\n", ret);
1000e8c8e9deSClaudiu Beznea 		return ret;
1001e8c8e9deSClaudiu Beznea 	}
1002e8c8e9deSClaudiu Beznea 	ret = clk_prepare_enable(dd->gclk);
1003e8c8e9deSClaudiu Beznea 	if (ret) {
1004e8c8e9deSClaudiu Beznea 		dev_err(dd->dev,
1005e8c8e9deSClaudiu Beznea 			"failed to enable generic clock: %d\n", ret);
1006e8c8e9deSClaudiu Beznea 		goto disable_pclk;
1007e8c8e9deSClaudiu Beznea 	}
1008e8c8e9deSClaudiu Beznea 
1009e8c8e9deSClaudiu Beznea 	regcache_cache_only(dd->regmap, false);
1010e8c8e9deSClaudiu Beznea 	regcache_mark_dirty(dd->regmap);
1011e8c8e9deSClaudiu Beznea 	ret = regcache_sync(dd->regmap);
1012e8c8e9deSClaudiu Beznea 	if (ret) {
1013e8c8e9deSClaudiu Beznea 		regcache_cache_only(dd->regmap, true);
1014e8c8e9deSClaudiu Beznea 		clk_disable_unprepare(dd->gclk);
1015e8c8e9deSClaudiu Beznea disable_pclk:
1016e8c8e9deSClaudiu Beznea 		clk_disable_unprepare(dd->pclk);
1017e8c8e9deSClaudiu Beznea 	}
1018e8c8e9deSClaudiu Beznea 
1019e8c8e9deSClaudiu Beznea 	return ret;
1020e8c8e9deSClaudiu Beznea }
1021e8c8e9deSClaudiu Beznea 
mchp_pdmc_probe(struct platform_device * pdev)102250291652SCodrin Ciubotariu static int mchp_pdmc_probe(struct platform_device *pdev)
102350291652SCodrin Ciubotariu {
102450291652SCodrin Ciubotariu 	struct device *dev = &pdev->dev;
102550291652SCodrin Ciubotariu 	struct mchp_pdmc *dd;
102650291652SCodrin Ciubotariu 	struct resource *res;
102750291652SCodrin Ciubotariu 	void __iomem *io_base;
102850291652SCodrin Ciubotariu 	u32 version;
102950291652SCodrin Ciubotariu 	int irq;
103050291652SCodrin Ciubotariu 	int ret;
103150291652SCodrin Ciubotariu 
103250291652SCodrin Ciubotariu 	dd = devm_kzalloc(dev, sizeof(*dd), GFP_KERNEL);
103350291652SCodrin Ciubotariu 	if (!dd)
103450291652SCodrin Ciubotariu 		return -ENOMEM;
103550291652SCodrin Ciubotariu 
103650291652SCodrin Ciubotariu 	dd->dev = &pdev->dev;
103750291652SCodrin Ciubotariu 	ret = mchp_pdmc_dt_init(dd);
103850291652SCodrin Ciubotariu 	if (ret < 0)
103950291652SCodrin Ciubotariu 		return ret;
104050291652SCodrin Ciubotariu 
104150291652SCodrin Ciubotariu 	irq = platform_get_irq(pdev, 0);
10422588a014SJiapeng Chong 	if (irq < 0)
104350291652SCodrin Ciubotariu 		return irq;
104450291652SCodrin Ciubotariu 
104550291652SCodrin Ciubotariu 	dd->pclk = devm_clk_get(dev, "pclk");
104650291652SCodrin Ciubotariu 	if (IS_ERR(dd->pclk)) {
104750291652SCodrin Ciubotariu 		ret = PTR_ERR(dd->pclk);
104850291652SCodrin Ciubotariu 		dev_err(dev, "failed to get peripheral clock: %d\n", ret);
104950291652SCodrin Ciubotariu 		return ret;
105050291652SCodrin Ciubotariu 	}
105150291652SCodrin Ciubotariu 
105250291652SCodrin Ciubotariu 	dd->gclk = devm_clk_get(dev, "gclk");
105350291652SCodrin Ciubotariu 	if (IS_ERR(dd->gclk)) {
105450291652SCodrin Ciubotariu 		ret = PTR_ERR(dd->gclk);
105550291652SCodrin Ciubotariu 		dev_err(dev, "failed to get GCK: %d\n", ret);
105650291652SCodrin Ciubotariu 		return ret;
105750291652SCodrin Ciubotariu 	}
105850291652SCodrin Ciubotariu 
105950291652SCodrin Ciubotariu 	io_base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
106050291652SCodrin Ciubotariu 	if (IS_ERR(io_base)) {
106150291652SCodrin Ciubotariu 		ret = PTR_ERR(io_base);
106250291652SCodrin Ciubotariu 		dev_err(dev, "failed to remap register memory: %d\n", ret);
106350291652SCodrin Ciubotariu 		return ret;
106450291652SCodrin Ciubotariu 	}
106550291652SCodrin Ciubotariu 
106650291652SCodrin Ciubotariu 	dd->regmap = devm_regmap_init_mmio(dev, io_base,
106750291652SCodrin Ciubotariu 					   &mchp_pdmc_regmap_config);
106850291652SCodrin Ciubotariu 	if (IS_ERR(dd->regmap)) {
106950291652SCodrin Ciubotariu 		ret = PTR_ERR(dd->regmap);
107050291652SCodrin Ciubotariu 		dev_err(dev, "failed to init register map: %d\n", ret);
107150291652SCodrin Ciubotariu 		return ret;
107250291652SCodrin Ciubotariu 	}
107350291652SCodrin Ciubotariu 
107450291652SCodrin Ciubotariu 	ret = devm_request_irq(dev, irq, mchp_pdmc_interrupt, 0,
1075cb72b29cSClaudiu Beznea 			       dev_name(&pdev->dev), dd);
107650291652SCodrin Ciubotariu 	if (ret < 0) {
107750291652SCodrin Ciubotariu 		dev_err(dev, "can't register ISR for IRQ %u (ret=%i)\n",
107850291652SCodrin Ciubotariu 			irq, ret);
107950291652SCodrin Ciubotariu 		return ret;
108050291652SCodrin Ciubotariu 	}
108150291652SCodrin Ciubotariu 
108250291652SCodrin Ciubotariu 	/* by default audio filter is enabled and the SINC Filter order
108350291652SCodrin Ciubotariu 	 * will be set to the recommended value, 3
108450291652SCodrin Ciubotariu 	 */
108550291652SCodrin Ciubotariu 	dd->audio_filter_en = true;
108650291652SCodrin Ciubotariu 	dd->sinc_order = 3;
108750291652SCodrin Ciubotariu 
108850291652SCodrin Ciubotariu 	dd->addr.addr = (dma_addr_t)res->start + MCHP_PDMC_RHR;
108950291652SCodrin Ciubotariu 	platform_set_drvdata(pdev, dd);
109050291652SCodrin Ciubotariu 
1091e8c8e9deSClaudiu Beznea 	pm_runtime_enable(dd->dev);
1092e8c8e9deSClaudiu Beznea 	if (!pm_runtime_enabled(dd->dev)) {
1093e8c8e9deSClaudiu Beznea 		ret = mchp_pdmc_runtime_resume(dd->dev);
1094e8c8e9deSClaudiu Beznea 		if (ret)
1095e8c8e9deSClaudiu Beznea 			return ret;
1096e8c8e9deSClaudiu Beznea 	}
1097e8c8e9deSClaudiu Beznea 
109850291652SCodrin Ciubotariu 	/* register platform */
109950291652SCodrin Ciubotariu 	ret = devm_snd_dmaengine_pcm_register(dev, &mchp_pdmc_config, 0);
110050291652SCodrin Ciubotariu 	if (ret) {
110150291652SCodrin Ciubotariu 		dev_err(dev, "could not register platform: %d\n", ret);
1102e8c8e9deSClaudiu Beznea 		goto pm_runtime_suspend;
110350291652SCodrin Ciubotariu 	}
110450291652SCodrin Ciubotariu 
110550291652SCodrin Ciubotariu 	ret = devm_snd_soc_register_component(dev, &mchp_pdmc_dai_component,
110650291652SCodrin Ciubotariu 					      &mchp_pdmc_dai, 1);
110750291652SCodrin Ciubotariu 	if (ret) {
110850291652SCodrin Ciubotariu 		dev_err(dev, "could not register CPU DAI: %d\n", ret);
1109e8c8e9deSClaudiu Beznea 		goto pm_runtime_suspend;
111050291652SCodrin Ciubotariu 	}
111150291652SCodrin Ciubotariu 
111250291652SCodrin Ciubotariu 	/* print IP version */
111350291652SCodrin Ciubotariu 	regmap_read(dd->regmap, MCHP_PDMC_VER, &version);
111450291652SCodrin Ciubotariu 	dev_info(dd->dev, "hw version: %#lx\n",
111550291652SCodrin Ciubotariu 		 version & MCHP_PDMC_VER_VERSION);
111650291652SCodrin Ciubotariu 
111750291652SCodrin Ciubotariu 	return 0;
1118e8c8e9deSClaudiu Beznea 
1119e8c8e9deSClaudiu Beznea pm_runtime_suspend:
1120e8c8e9deSClaudiu Beznea 	if (!pm_runtime_status_suspended(dd->dev))
1121e8c8e9deSClaudiu Beznea 		mchp_pdmc_runtime_suspend(dd->dev);
1122e8c8e9deSClaudiu Beznea 	pm_runtime_disable(dd->dev);
1123e8c8e9deSClaudiu Beznea 
1124e8c8e9deSClaudiu Beznea 	return ret;
1125e8c8e9deSClaudiu Beznea }
1126e8c8e9deSClaudiu Beznea 
mchp_pdmc_remove(struct platform_device * pdev)112786fdd482SUwe Kleine-König static void mchp_pdmc_remove(struct platform_device *pdev)
1128e8c8e9deSClaudiu Beznea {
1129e8c8e9deSClaudiu Beznea 	struct mchp_pdmc *dd = platform_get_drvdata(pdev);
1130e8c8e9deSClaudiu Beznea 
1131e8c8e9deSClaudiu Beznea 	if (!pm_runtime_status_suspended(dd->dev))
1132e8c8e9deSClaudiu Beznea 		mchp_pdmc_runtime_suspend(dd->dev);
1133e8c8e9deSClaudiu Beznea 
1134e8c8e9deSClaudiu Beznea 	pm_runtime_disable(dd->dev);
113550291652SCodrin Ciubotariu }
113650291652SCodrin Ciubotariu 
113750291652SCodrin Ciubotariu static const struct of_device_id mchp_pdmc_of_match[] = {
113850291652SCodrin Ciubotariu 	{
113950291652SCodrin Ciubotariu 		.compatible = "microchip,sama7g5-pdmc",
114050291652SCodrin Ciubotariu 	}, {
114150291652SCodrin Ciubotariu 		/* sentinel */
114250291652SCodrin Ciubotariu 	}
114350291652SCodrin Ciubotariu };
114450291652SCodrin Ciubotariu MODULE_DEVICE_TABLE(of, mchp_pdmc_of_match);
114550291652SCodrin Ciubotariu 
1146e8c8e9deSClaudiu Beznea static const struct dev_pm_ops mchp_pdmc_pm_ops = {
1147404c61c4SClaudiu Beznea 	SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume)
1148e8c8e9deSClaudiu Beznea 	RUNTIME_PM_OPS(mchp_pdmc_runtime_suspend, mchp_pdmc_runtime_resume,
1149e8c8e9deSClaudiu Beznea 		       NULL)
1150e8c8e9deSClaudiu Beznea };
1151e8c8e9deSClaudiu Beznea 
115250291652SCodrin Ciubotariu static struct platform_driver mchp_pdmc_driver = {
115350291652SCodrin Ciubotariu 	.driver	= {
115450291652SCodrin Ciubotariu 		.name		= "mchp-pdmc",
115550291652SCodrin Ciubotariu 		.of_match_table	= of_match_ptr(mchp_pdmc_of_match),
1156e8c8e9deSClaudiu Beznea 		.pm		= pm_ptr(&mchp_pdmc_pm_ops),
115750291652SCodrin Ciubotariu 	},
115850291652SCodrin Ciubotariu 	.probe	= mchp_pdmc_probe,
115986fdd482SUwe Kleine-König 	.remove_new = mchp_pdmc_remove,
116050291652SCodrin Ciubotariu };
116150291652SCodrin Ciubotariu module_platform_driver(mchp_pdmc_driver);
116250291652SCodrin Ciubotariu 
116350291652SCodrin Ciubotariu MODULE_DESCRIPTION("Microchip PDMC driver under ALSA SoC architecture");
116450291652SCodrin Ciubotariu MODULE_AUTHOR("Codrin Ciubotariu <codrin.ciubotariu@microchip.com>");
116550291652SCodrin Ciubotariu MODULE_LICENSE("GPL v2");
1166