xref: /openbmc/linux/sound/soc/atmel/mchp-pdmc.c (revision 69d0fd34)
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 
28850291652SCodrin Ciubotariu 	map = mchp_pdmc_chmap_get(substream, info);
28950291652SCodrin Ciubotariu 	if (!map)
29050291652SCodrin Ciubotariu 		return -EINVAL;
29150291652SCodrin Ciubotariu 
29250291652SCodrin Ciubotariu 	for (i = 0; i < map->channels; i++) {
29350291652SCodrin Ciubotariu 		int map_idx;
29450291652SCodrin Ciubotariu 
29550291652SCodrin Ciubotariu 		map->map[i] = ucontrol->value.integer.value[i];
29650291652SCodrin Ciubotariu 		map_idx = map->channels == 1 ? map->map[i] - SNDRV_CHMAP_MONO :
29750291652SCodrin Ciubotariu 					       map->map[i] - SNDRV_CHMAP_FL;
29850291652SCodrin Ciubotariu 
29950291652SCodrin Ciubotariu 		/* configure IP for the desired channel map */
30050291652SCodrin Ciubotariu 		if (dd->channel_mic_map[map_idx].ds_pos)
30150291652SCodrin Ciubotariu 			cfgr_val |= MCHP_PDMC_CFGR_PDMSEL(i);
30250291652SCodrin Ciubotariu 		if (dd->channel_mic_map[map_idx].clk_edge)
30350291652SCodrin Ciubotariu 			cfgr_val |= MCHP_PDMC_CFGR_BSSEL(i);
30450291652SCodrin Ciubotariu 	}
30550291652SCodrin Ciubotariu 
30650291652SCodrin Ciubotariu 	regmap_write(dd->regmap, MCHP_PDMC_CFGR, cfgr_val);
30750291652SCodrin Ciubotariu 
30850291652SCodrin Ciubotariu 	return 0;
30950291652SCodrin Ciubotariu }
31050291652SCodrin Ciubotariu 
mchp_pdmc_chmap_ctl_private_free(struct snd_kcontrol * kcontrol)31150291652SCodrin Ciubotariu static void mchp_pdmc_chmap_ctl_private_free(struct snd_kcontrol *kcontrol)
31250291652SCodrin Ciubotariu {
31350291652SCodrin Ciubotariu 	struct mchp_pdmc_chmap *info = snd_kcontrol_chip(kcontrol);
31450291652SCodrin Ciubotariu 
31550291652SCodrin Ciubotariu 	info->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].chmap_kctl = NULL;
31650291652SCodrin Ciubotariu 	kfree(info);
31750291652SCodrin Ciubotariu }
31850291652SCodrin Ciubotariu 
mchp_pdmc_chmap_ctl_tlv(struct snd_kcontrol * kcontrol,int op_flag,unsigned int size,unsigned int __user * tlv)31950291652SCodrin Ciubotariu static int mchp_pdmc_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
32050291652SCodrin Ciubotariu 				   unsigned int size, unsigned int __user *tlv)
32150291652SCodrin Ciubotariu {
32250291652SCodrin Ciubotariu 	struct mchp_pdmc_chmap *info = snd_kcontrol_chip(kcontrol);
32350291652SCodrin Ciubotariu 	const struct snd_pcm_chmap_elem *map;
32450291652SCodrin Ciubotariu 	unsigned int __user *dst;
32550291652SCodrin Ciubotariu 	int c, count = 0;
32650291652SCodrin Ciubotariu 
32750291652SCodrin Ciubotariu 	if (!info->chmap)
32850291652SCodrin Ciubotariu 		return -EINVAL;
32950291652SCodrin Ciubotariu 	if (size < 8)
33050291652SCodrin Ciubotariu 		return -ENOMEM;
33150291652SCodrin Ciubotariu 	if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
33250291652SCodrin Ciubotariu 		return -EFAULT;
33350291652SCodrin Ciubotariu 	size -= 8;
33450291652SCodrin Ciubotariu 	dst = tlv + 2;
33550291652SCodrin Ciubotariu 	for (map = info->chmap; map->channels; map++) {
33650291652SCodrin Ciubotariu 		int chs_bytes = map->channels * 4;
33750291652SCodrin Ciubotariu 
33850291652SCodrin Ciubotariu 		if (size < 8)
33950291652SCodrin Ciubotariu 			return -ENOMEM;
34050291652SCodrin Ciubotariu 		if (put_user(SNDRV_CTL_TLVT_CHMAP_VAR, dst) ||
34150291652SCodrin Ciubotariu 		    put_user(chs_bytes, dst + 1))
34250291652SCodrin Ciubotariu 			return -EFAULT;
34350291652SCodrin Ciubotariu 		dst += 2;
34450291652SCodrin Ciubotariu 		size -= 8;
34550291652SCodrin Ciubotariu 		count += 8;
34650291652SCodrin Ciubotariu 		if (size < chs_bytes)
34750291652SCodrin Ciubotariu 			return -ENOMEM;
34850291652SCodrin Ciubotariu 		size -= chs_bytes;
34950291652SCodrin Ciubotariu 		count += chs_bytes;
35050291652SCodrin Ciubotariu 		for (c = 0; c < map->channels; c++) {
35150291652SCodrin Ciubotariu 			if (put_user(map->map[c], dst))
35250291652SCodrin Ciubotariu 				return -EFAULT;
35350291652SCodrin Ciubotariu 			dst++;
35450291652SCodrin Ciubotariu 		}
35550291652SCodrin Ciubotariu 	}
35650291652SCodrin Ciubotariu 	if (put_user(count, tlv + 1))
35750291652SCodrin Ciubotariu 		return -EFAULT;
35850291652SCodrin Ciubotariu 	return 0;
35950291652SCodrin Ciubotariu }
36050291652SCodrin Ciubotariu 
36150291652SCodrin Ciubotariu static const struct snd_kcontrol_new mchp_pdmc_snd_controls[] = {
36250291652SCodrin Ciubotariu 	SOC_SINGLE_BOOL_EXT("Audio Filter", 0, &mchp_pdmc_af_get, &mchp_pdmc_af_put),
36350291652SCodrin Ciubotariu 	{
36450291652SCodrin Ciubotariu 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
36550291652SCodrin Ciubotariu 		.name = "SINC Filter Order",
36650291652SCodrin Ciubotariu 		.info = snd_soc_info_enum_double,
36750291652SCodrin Ciubotariu 		.get = mchp_pdmc_sinc_order_get,
36850291652SCodrin Ciubotariu 		.put = mchp_pdmc_sinc_order_put,
36950291652SCodrin Ciubotariu 		.private_value = (unsigned long)&mchp_pdmc_sinc_filter_order_enum,
37050291652SCodrin Ciubotariu 	},
37150291652SCodrin Ciubotariu };
37250291652SCodrin Ciubotariu 
mchp_pdmc_close(struct snd_soc_component * component,struct snd_pcm_substream * substream)37350291652SCodrin Ciubotariu static int mchp_pdmc_close(struct snd_soc_component *component,
37450291652SCodrin Ciubotariu 			   struct snd_pcm_substream *substream)
37550291652SCodrin Ciubotariu {
37650291652SCodrin Ciubotariu 	return snd_soc_add_component_controls(component, mchp_pdmc_snd_controls,
37750291652SCodrin Ciubotariu 					      ARRAY_SIZE(mchp_pdmc_snd_controls));
37850291652SCodrin Ciubotariu }
37950291652SCodrin Ciubotariu 
mchp_pdmc_open(struct snd_soc_component * component,struct snd_pcm_substream * substream)38050291652SCodrin Ciubotariu static int mchp_pdmc_open(struct snd_soc_component *component,
38150291652SCodrin Ciubotariu 			  struct snd_pcm_substream *substream)
38250291652SCodrin Ciubotariu {
38350291652SCodrin Ciubotariu 	int i;
38450291652SCodrin Ciubotariu 
38550291652SCodrin Ciubotariu 	/* remove controls that can't be changed at runtime */
38650291652SCodrin Ciubotariu 	for (i = 0; i < ARRAY_SIZE(mchp_pdmc_snd_controls); i++) {
38750291652SCodrin Ciubotariu 		const struct snd_kcontrol_new *control = &mchp_pdmc_snd_controls[i];
38850291652SCodrin Ciubotariu 		struct snd_ctl_elem_id id;
38950291652SCodrin Ciubotariu 		int err;
39050291652SCodrin Ciubotariu 
39150291652SCodrin Ciubotariu 		if (component->name_prefix)
39250291652SCodrin Ciubotariu 			snprintf(id.name, sizeof(id.name), "%s %s", component->name_prefix,
39350291652SCodrin Ciubotariu 				 control->name);
39450291652SCodrin Ciubotariu 		else
39550291652SCodrin Ciubotariu 			strscpy(id.name, control->name, sizeof(id.name));
39650291652SCodrin Ciubotariu 
39750291652SCodrin Ciubotariu 		id.numid = 0;
39850291652SCodrin Ciubotariu 		id.iface = control->iface;
39950291652SCodrin Ciubotariu 		id.device = control->device;
40050291652SCodrin Ciubotariu 		id.subdevice = control->subdevice;
40150291652SCodrin Ciubotariu 		id.index = control->index;
402d8b366c4STakashi Iwai 		err = snd_ctl_remove_id(component->card->snd_card, &id);
403d8b366c4STakashi Iwai 		if (err < 0)
40450291652SCodrin Ciubotariu 			dev_err(component->dev, "%d: Failed to remove %s\n", err,
40550291652SCodrin Ciubotariu 				control->name);
40650291652SCodrin Ciubotariu 	}
40750291652SCodrin Ciubotariu 
40850291652SCodrin Ciubotariu 	return 0;
40950291652SCodrin Ciubotariu }
41050291652SCodrin Ciubotariu 
41150291652SCodrin Ciubotariu static const struct snd_soc_component_driver mchp_pdmc_dai_component = {
41250291652SCodrin Ciubotariu 	.name = "mchp-pdmc",
41350291652SCodrin Ciubotariu 	.controls = mchp_pdmc_snd_controls,
41450291652SCodrin Ciubotariu 	.num_controls = ARRAY_SIZE(mchp_pdmc_snd_controls),
41550291652SCodrin Ciubotariu 	.open = &mchp_pdmc_open,
41650291652SCodrin Ciubotariu 	.close = &mchp_pdmc_close,
4177593e008SCharles Keepax 	.legacy_dai_naming = 1,
41838cb2a36SKuninori Morimoto 	.trigger_start = SND_SOC_TRIGGER_ORDER_LDC,
41950291652SCodrin Ciubotariu };
42050291652SCodrin Ciubotariu 
42150291652SCodrin Ciubotariu static const unsigned int mchp_pdmc_1mic[] = {1};
42250291652SCodrin Ciubotariu static const unsigned int mchp_pdmc_2mic[] = {1, 2};
42350291652SCodrin Ciubotariu static const unsigned int mchp_pdmc_3mic[] = {1, 2, 3};
42450291652SCodrin Ciubotariu static const unsigned int mchp_pdmc_4mic[] = {1, 2, 3, 4};
42550291652SCodrin Ciubotariu 
42650291652SCodrin Ciubotariu static const struct snd_pcm_hw_constraint_list mchp_pdmc_chan_constr[] = {
42750291652SCodrin Ciubotariu 	{
42850291652SCodrin Ciubotariu 		.list = mchp_pdmc_1mic,
42950291652SCodrin Ciubotariu 		.count = ARRAY_SIZE(mchp_pdmc_1mic),
43050291652SCodrin Ciubotariu 	},
43150291652SCodrin Ciubotariu 	{
43250291652SCodrin Ciubotariu 		.list = mchp_pdmc_2mic,
43350291652SCodrin Ciubotariu 		.count = ARRAY_SIZE(mchp_pdmc_2mic),
43450291652SCodrin Ciubotariu 	},
43550291652SCodrin Ciubotariu 	{
43650291652SCodrin Ciubotariu 		.list = mchp_pdmc_3mic,
43750291652SCodrin Ciubotariu 		.count = ARRAY_SIZE(mchp_pdmc_3mic),
43850291652SCodrin Ciubotariu 	},
43950291652SCodrin Ciubotariu 	{
44050291652SCodrin Ciubotariu 		.list = mchp_pdmc_4mic,
44150291652SCodrin Ciubotariu 		.count = ARRAY_SIZE(mchp_pdmc_4mic),
44250291652SCodrin Ciubotariu 	},
44350291652SCodrin Ciubotariu };
44450291652SCodrin Ciubotariu 
mchp_pdmc_startup(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)44550291652SCodrin Ciubotariu static int mchp_pdmc_startup(struct snd_pcm_substream *substream,
44650291652SCodrin Ciubotariu 			     struct snd_soc_dai *dai)
44750291652SCodrin Ciubotariu {
44850291652SCodrin Ciubotariu 	struct mchp_pdmc *dd = snd_soc_dai_get_drvdata(dai);
44950291652SCodrin Ciubotariu 
45050291652SCodrin Ciubotariu 	regmap_write(dd->regmap, MCHP_PDMC_CR, MCHP_PDMC_CR_SWRST);
45150291652SCodrin Ciubotariu 
45250291652SCodrin Ciubotariu 	snd_pcm_hw_constraint_list(substream->runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
45350291652SCodrin Ciubotariu 				   &mchp_pdmc_chan_constr[dd->mic_no - 1]);
45450291652SCodrin Ciubotariu 
45550291652SCodrin Ciubotariu 	return 0;
45650291652SCodrin Ciubotariu }
45750291652SCodrin Ciubotariu 
mchp_pdmc_dai_probe(struct snd_soc_dai * dai)45850291652SCodrin Ciubotariu static int mchp_pdmc_dai_probe(struct snd_soc_dai *dai)
45950291652SCodrin Ciubotariu {
46050291652SCodrin Ciubotariu 	struct mchp_pdmc *dd = snd_soc_dai_get_drvdata(dai);
46150291652SCodrin Ciubotariu 
46250291652SCodrin Ciubotariu 	snd_soc_dai_init_dma_data(dai, NULL, &dd->addr);
46350291652SCodrin Ciubotariu 
46450291652SCodrin Ciubotariu 	return 0;
46550291652SCodrin Ciubotariu }
46650291652SCodrin Ciubotariu 
mchp_pdmc_set_fmt(struct snd_soc_dai * dai,unsigned int fmt)46750291652SCodrin Ciubotariu static int mchp_pdmc_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
46850291652SCodrin Ciubotariu {
46950291652SCodrin Ciubotariu 	unsigned int fmt_master = fmt & SND_SOC_DAIFMT_MASTER_MASK;
47050291652SCodrin Ciubotariu 	unsigned int fmt_format = fmt & SND_SOC_DAIFMT_FORMAT_MASK;
47150291652SCodrin Ciubotariu 
47250291652SCodrin Ciubotariu 	/* IP needs to be bitclock master */
4730fd054a5SCharles Keepax 	if (fmt_master != SND_SOC_DAIFMT_BP_FP &&
4740fd054a5SCharles Keepax 	    fmt_master != SND_SOC_DAIFMT_BP_FC)
47550291652SCodrin Ciubotariu 		return -EINVAL;
47650291652SCodrin Ciubotariu 
47750291652SCodrin Ciubotariu 	/* IP supports only PDM interface */
47850291652SCodrin Ciubotariu 	if (fmt_format != SND_SOC_DAIFMT_PDM)
47950291652SCodrin Ciubotariu 		return -EINVAL;
48050291652SCodrin Ciubotariu 
48150291652SCodrin Ciubotariu 	return 0;
48250291652SCodrin Ciubotariu }
48350291652SCodrin Ciubotariu 
mchp_pdmc_mr_set_osr(int audio_filter_en,unsigned int osr)48450291652SCodrin Ciubotariu static u32 mchp_pdmc_mr_set_osr(int audio_filter_en, unsigned int osr)
48550291652SCodrin Ciubotariu {
48650291652SCodrin Ciubotariu 	if (audio_filter_en) {
48750291652SCodrin Ciubotariu 		switch (osr) {
48850291652SCodrin Ciubotariu 		case 64:
48950291652SCodrin Ciubotariu 			return MCHP_PDMC_MR_OSR64;
49050291652SCodrin Ciubotariu 		case 128:
49150291652SCodrin Ciubotariu 			return MCHP_PDMC_MR_OSR128;
49250291652SCodrin Ciubotariu 		case 256:
49350291652SCodrin Ciubotariu 			return MCHP_PDMC_MR_OSR256;
49450291652SCodrin Ciubotariu 		}
49550291652SCodrin Ciubotariu 	} else {
49650291652SCodrin Ciubotariu 		switch (osr) {
49750291652SCodrin Ciubotariu 		case 8:
49850291652SCodrin Ciubotariu 			return MCHP_PDMC_MR_SINC_OSR_8;
49950291652SCodrin Ciubotariu 		case 16:
50050291652SCodrin Ciubotariu 			return MCHP_PDMC_MR_SINC_OSR_16;
50150291652SCodrin Ciubotariu 		case 32:
50250291652SCodrin Ciubotariu 			return MCHP_PDMC_MR_SINC_OSR_32;
50350291652SCodrin Ciubotariu 		case 64:
50450291652SCodrin Ciubotariu 			return MCHP_PDMC_MR_SINC_OSR_64;
50550291652SCodrin Ciubotariu 		case 128:
50650291652SCodrin Ciubotariu 			return MCHP_PDMC_MR_SINC_OSR_128;
50750291652SCodrin Ciubotariu 		case 256:
50850291652SCodrin Ciubotariu 			return MCHP_PDMC_MR_SINC_OSR_256;
50950291652SCodrin Ciubotariu 		}
51050291652SCodrin Ciubotariu 	}
51150291652SCodrin Ciubotariu 	return 0;
51250291652SCodrin Ciubotariu }
51350291652SCodrin Ciubotariu 
mchp_pdmc_period_to_maxburst(int period_size)51450291652SCodrin Ciubotariu static inline int mchp_pdmc_period_to_maxburst(int period_size)
51550291652SCodrin Ciubotariu {
51650291652SCodrin Ciubotariu 	if (!(period_size % 8))
51750291652SCodrin Ciubotariu 		return 8;
51850291652SCodrin Ciubotariu 	if (!(period_size % 4))
51950291652SCodrin Ciubotariu 		return 4;
52050291652SCodrin Ciubotariu 	if (!(period_size % 2))
52150291652SCodrin Ciubotariu 		return 2;
52250291652SCodrin Ciubotariu 	return 1;
52350291652SCodrin Ciubotariu }
52450291652SCodrin Ciubotariu 
52550291652SCodrin Ciubotariu static struct snd_pcm_chmap_elem mchp_pdmc_std_chmaps[] = {
52650291652SCodrin Ciubotariu 	{ .channels = 1,
52750291652SCodrin Ciubotariu 	  .map = { SNDRV_CHMAP_MONO } },
52850291652SCodrin Ciubotariu 	{ .channels = 2,
52950291652SCodrin Ciubotariu 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
53050291652SCodrin Ciubotariu 	{ .channels = 3,
53150291652SCodrin Ciubotariu 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
53250291652SCodrin Ciubotariu 		   SNDRV_CHMAP_RL } },
53350291652SCodrin Ciubotariu 	{ .channels = 4,
53450291652SCodrin Ciubotariu 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
53550291652SCodrin Ciubotariu 		   SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
53650291652SCodrin Ciubotariu 	{ }
53750291652SCodrin Ciubotariu };
53850291652SCodrin Ciubotariu 
mchp_pdmc_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)53950291652SCodrin Ciubotariu static int mchp_pdmc_hw_params(struct snd_pcm_substream *substream,
54050291652SCodrin Ciubotariu 			       struct snd_pcm_hw_params *params,
54150291652SCodrin Ciubotariu 			       struct snd_soc_dai *dai)
54250291652SCodrin Ciubotariu {
54350291652SCodrin Ciubotariu 	struct mchp_pdmc *dd = snd_soc_dai_get_drvdata(dai);
54450291652SCodrin Ciubotariu 	struct snd_soc_component *comp = dai->component;
54550291652SCodrin Ciubotariu 	unsigned long gclk_rate = 0;
54650291652SCodrin Ciubotariu 	unsigned long best_diff_rate = ~0UL;
54750291652SCodrin Ciubotariu 	unsigned int channels = params_channels(params);
54850291652SCodrin Ciubotariu 	unsigned int osr = 0, osr_start;
54950291652SCodrin Ciubotariu 	unsigned int fs = params_rate(params);
55050291652SCodrin Ciubotariu 	u32 mr_val = 0;
55150291652SCodrin Ciubotariu 	u32 cfgr_val = 0;
55250291652SCodrin Ciubotariu 	int i;
55350291652SCodrin Ciubotariu 	int ret;
55450291652SCodrin Ciubotariu 
55550291652SCodrin Ciubotariu 	dev_dbg(comp->dev, "%s() rate=%u format=%#x width=%u channels=%u\n",
55650291652SCodrin Ciubotariu 		__func__, params_rate(params), params_format(params),
55750291652SCodrin Ciubotariu 		params_width(params), params_channels(params));
55850291652SCodrin Ciubotariu 
55950291652SCodrin Ciubotariu 	if (channels > dd->mic_no) {
56050291652SCodrin Ciubotariu 		dev_err(comp->dev, "more channels %u than microphones %d\n",
56150291652SCodrin Ciubotariu 			channels, dd->mic_no);
56250291652SCodrin Ciubotariu 		return -EINVAL;
56350291652SCodrin Ciubotariu 	}
56450291652SCodrin Ciubotariu 
56550291652SCodrin Ciubotariu 	dd->pdmcen = 0;
56650291652SCodrin Ciubotariu 	for (i = 0; i < channels; i++) {
56750291652SCodrin Ciubotariu 		dd->pdmcen |= MCHP_PDMC_MR_PDMCEN(i);
56850291652SCodrin Ciubotariu 		if (dd->channel_mic_map[i].ds_pos)
56950291652SCodrin Ciubotariu 			cfgr_val |= MCHP_PDMC_CFGR_PDMSEL(i);
57050291652SCodrin Ciubotariu 		if (dd->channel_mic_map[i].clk_edge)
57150291652SCodrin Ciubotariu 			cfgr_val |= MCHP_PDMC_CFGR_BSSEL(i);
57250291652SCodrin Ciubotariu 	}
57350291652SCodrin Ciubotariu 
57450291652SCodrin Ciubotariu 	for (osr_start = dd->audio_filter_en ? 64 : 8;
57550291652SCodrin Ciubotariu 	     osr_start <= 256 && best_diff_rate; osr_start *= 2) {
57650291652SCodrin Ciubotariu 		long round_rate;
57750291652SCodrin Ciubotariu 		unsigned long diff_rate;
57850291652SCodrin Ciubotariu 
57950291652SCodrin Ciubotariu 		round_rate = clk_round_rate(dd->gclk,
58050291652SCodrin Ciubotariu 					    (unsigned long)fs * 16 * osr_start);
58150291652SCodrin Ciubotariu 		if (round_rate < 0)
58250291652SCodrin Ciubotariu 			continue;
58350291652SCodrin Ciubotariu 		diff_rate = abs((fs * 16 * osr_start) - round_rate);
58450291652SCodrin Ciubotariu 		if (diff_rate < best_diff_rate) {
58550291652SCodrin Ciubotariu 			best_diff_rate = diff_rate;
58650291652SCodrin Ciubotariu 			osr = osr_start;
58750291652SCodrin Ciubotariu 			gclk_rate = fs * 16 * osr;
58850291652SCodrin Ciubotariu 		}
58950291652SCodrin Ciubotariu 	}
59050291652SCodrin Ciubotariu 	if (!gclk_rate) {
59150291652SCodrin Ciubotariu 		dev_err(comp->dev, "invalid sampling rate: %u\n", fs);
59250291652SCodrin Ciubotariu 		return -EINVAL;
59350291652SCodrin Ciubotariu 	}
59450291652SCodrin Ciubotariu 
595e8c8e9deSClaudiu Beznea 	/* CLK is enabled by runtime PM. */
596e8c8e9deSClaudiu Beznea 	clk_disable_unprepare(dd->gclk);
597e8c8e9deSClaudiu Beznea 
59850291652SCodrin Ciubotariu 	/* set the rate */
59950291652SCodrin Ciubotariu 	ret = clk_set_rate(dd->gclk, gclk_rate);
600e8c8e9deSClaudiu Beznea 	clk_prepare_enable(dd->gclk);
60150291652SCodrin Ciubotariu 	if (ret) {
60250291652SCodrin Ciubotariu 		dev_err(comp->dev, "unable to set rate %lu to GCLK: %d\n",
60350291652SCodrin Ciubotariu 			gclk_rate, ret);
60450291652SCodrin Ciubotariu 		return ret;
60550291652SCodrin Ciubotariu 	}
60650291652SCodrin Ciubotariu 
60750291652SCodrin Ciubotariu 	mr_val |= mchp_pdmc_mr_set_osr(dd->audio_filter_en, osr);
60850291652SCodrin Ciubotariu 
60912974257SClaudiu Beznea 	mr_val |= FIELD_PREP(MCHP_PDMC_MR_SINCORDER_MASK, dd->sinc_order);
61050291652SCodrin Ciubotariu 
61150291652SCodrin Ciubotariu 	dd->addr.maxburst = mchp_pdmc_period_to_maxburst(snd_pcm_lib_period_bytes(substream));
61212974257SClaudiu Beznea 	mr_val |= FIELD_PREP(MCHP_PDMC_MR_CHUNK_MASK, dd->addr.maxburst);
61350291652SCodrin Ciubotariu 	dev_dbg(comp->dev, "maxburst set to %d\n", dd->addr.maxburst);
61450291652SCodrin Ciubotariu 
61550291652SCodrin Ciubotariu 	snd_soc_component_update_bits(comp, MCHP_PDMC_MR,
61650291652SCodrin Ciubotariu 				      MCHP_PDMC_MR_OSR_MASK |
61750291652SCodrin Ciubotariu 				      MCHP_PDMC_MR_SINCORDER_MASK |
61850291652SCodrin Ciubotariu 				      MCHP_PDMC_MR_SINC_OSR_MASK |
61950291652SCodrin Ciubotariu 				      MCHP_PDMC_MR_CHUNK_MASK, mr_val);
62050291652SCodrin Ciubotariu 
62150291652SCodrin Ciubotariu 	snd_soc_component_write(comp, MCHP_PDMC_CFGR, cfgr_val);
62250291652SCodrin Ciubotariu 
62350291652SCodrin Ciubotariu 	return 0;
62450291652SCodrin Ciubotariu }
62550291652SCodrin Ciubotariu 
mchp_pdmc_noise_filter_workaround(struct mchp_pdmc * dd)626c5682e2bSClaudiu Beznea static void mchp_pdmc_noise_filter_workaround(struct mchp_pdmc *dd)
627c5682e2bSClaudiu Beznea {
628c5682e2bSClaudiu Beznea 	u32 tmp, steps = 16;
629c5682e2bSClaudiu Beznea 
630c5682e2bSClaudiu Beznea 	/*
631c5682e2bSClaudiu Beznea 	 * PDMC doesn't wait for microphones' startup time thus the acquisition
632c5682e2bSClaudiu Beznea 	 * may start before the microphones are ready leading to poc noises at
633c5682e2bSClaudiu Beznea 	 * the beginning of capture. To avoid this, we need to wait 50ms (in
634c5682e2bSClaudiu Beznea 	 * normal startup procedure) or 150 ms (worst case after resume from sleep
635c5682e2bSClaudiu Beznea 	 * states) after microphones are enabled and then clear the FIFOs (by
636c5682e2bSClaudiu Beznea 	 * reading the RHR 16 times) and possible interrupts before continuing.
637c5682e2bSClaudiu Beznea 	 * Also, for this to work the DMA needs to be started after interrupts
638c5682e2bSClaudiu Beznea 	 * are enabled.
639c5682e2bSClaudiu Beznea 	 */
640c5682e2bSClaudiu Beznea 	usleep_range(dd->startup_delay_us, dd->startup_delay_us + 5);
641c5682e2bSClaudiu Beznea 
642c5682e2bSClaudiu Beznea 	while (steps--)
643c5682e2bSClaudiu Beznea 		regmap_read(dd->regmap, MCHP_PDMC_RHR, &tmp);
644c5682e2bSClaudiu Beznea 
645c5682e2bSClaudiu Beznea 	/* Clear interrupts. */
646c5682e2bSClaudiu Beznea 	regmap_read(dd->regmap, MCHP_PDMC_ISR, &tmp);
647c5682e2bSClaudiu Beznea }
648c5682e2bSClaudiu Beznea 
mchp_pdmc_trigger(struct snd_pcm_substream * substream,int cmd,struct snd_soc_dai * dai)64950291652SCodrin Ciubotariu static int mchp_pdmc_trigger(struct snd_pcm_substream *substream,
65050291652SCodrin Ciubotariu 			     int cmd, struct snd_soc_dai *dai)
65150291652SCodrin Ciubotariu {
65250291652SCodrin Ciubotariu 	struct mchp_pdmc *dd = snd_soc_dai_get_drvdata(dai);
65350291652SCodrin Ciubotariu 	struct snd_soc_component *cpu = dai->component;
65450291652SCodrin Ciubotariu #ifdef DEBUG
65550291652SCodrin Ciubotariu 	u32 val;
65650291652SCodrin Ciubotariu #endif
65750291652SCodrin Ciubotariu 
65850291652SCodrin Ciubotariu 	switch (cmd) {
65950291652SCodrin Ciubotariu 	case SNDRV_PCM_TRIGGER_RESUME:
660404c61c4SClaudiu Beznea 	case SNDRV_PCM_TRIGGER_START:
661404c61c4SClaudiu Beznea 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
66250291652SCodrin Ciubotariu 		snd_soc_component_update_bits(cpu, MCHP_PDMC_MR,
66350291652SCodrin Ciubotariu 					      MCHP_PDMC_MR_PDMCEN_MASK,
66450291652SCodrin Ciubotariu 					      dd->pdmcen);
665c5682e2bSClaudiu Beznea 
666c5682e2bSClaudiu Beznea 		mchp_pdmc_noise_filter_workaround(dd);
667c5682e2bSClaudiu Beznea 
668c5682e2bSClaudiu Beznea 		/* Enable interrupts. */
669c5682e2bSClaudiu Beznea 		regmap_write(dd->regmap, MCHP_PDMC_IER, dd->suspend_irq |
670c5682e2bSClaudiu Beznea 			     MCHP_PDMC_IR_RXOVR | MCHP_PDMC_IR_RXUDR);
671c5682e2bSClaudiu Beznea 		dd->suspend_irq = 0;
67250291652SCodrin Ciubotariu 		break;
67350291652SCodrin Ciubotariu 	case SNDRV_PCM_TRIGGER_SUSPEND:
674404c61c4SClaudiu Beznea 		regmap_read(dd->regmap, MCHP_PDMC_IMR, &dd->suspend_irq);
675404c61c4SClaudiu Beznea 		fallthrough;
676404c61c4SClaudiu Beznea 	case SNDRV_PCM_TRIGGER_STOP:
67750291652SCodrin Ciubotariu 		/* Disable overrun and underrun error interrupts */
678404c61c4SClaudiu Beznea 		regmap_write(dd->regmap, MCHP_PDMC_IDR, dd->suspend_irq |
67950291652SCodrin Ciubotariu 			     MCHP_PDMC_IR_RXOVR | MCHP_PDMC_IR_RXUDR);
680404c61c4SClaudiu Beznea 		fallthrough;
681404c61c4SClaudiu Beznea 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
68250291652SCodrin Ciubotariu 		snd_soc_component_update_bits(cpu, MCHP_PDMC_MR,
68350291652SCodrin Ciubotariu 					      MCHP_PDMC_MR_PDMCEN_MASK, 0);
68450291652SCodrin Ciubotariu 		break;
68550291652SCodrin Ciubotariu 	default:
68650291652SCodrin Ciubotariu 		return -EINVAL;
68750291652SCodrin Ciubotariu 	}
68850291652SCodrin Ciubotariu 
68950291652SCodrin Ciubotariu #ifdef DEBUG
69050291652SCodrin Ciubotariu 	regmap_read(dd->regmap, MCHP_PDMC_MR, &val);
69150291652SCodrin Ciubotariu 	dev_dbg(dd->dev, "MR (0x%02x): 0x%08x\n", MCHP_PDMC_MR, val);
69250291652SCodrin Ciubotariu 	regmap_read(dd->regmap, MCHP_PDMC_CFGR, &val);
69350291652SCodrin Ciubotariu 	dev_dbg(dd->dev, "CFGR (0x%02x): 0x%08x\n", MCHP_PDMC_CFGR, val);
69450291652SCodrin Ciubotariu 	regmap_read(dd->regmap, MCHP_PDMC_IMR, &val);
69550291652SCodrin Ciubotariu 	dev_dbg(dd->dev, "IMR (0x%02x): 0x%08x\n", MCHP_PDMC_IMR, val);
69650291652SCodrin Ciubotariu #endif
69750291652SCodrin Ciubotariu 
69850291652SCodrin Ciubotariu 	return 0;
69950291652SCodrin Ciubotariu }
70050291652SCodrin Ciubotariu 
mchp_pdmc_add_chmap_ctls(struct snd_pcm * pcm,struct mchp_pdmc * dd)70150291652SCodrin Ciubotariu static int mchp_pdmc_add_chmap_ctls(struct snd_pcm *pcm, struct mchp_pdmc *dd)
70250291652SCodrin Ciubotariu {
70350291652SCodrin Ciubotariu 	struct mchp_pdmc_chmap *info;
70450291652SCodrin Ciubotariu 	struct snd_kcontrol_new knew = {
70550291652SCodrin Ciubotariu 		.iface = SNDRV_CTL_ELEM_IFACE_PCM,
70650291652SCodrin Ciubotariu 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
70750291652SCodrin Ciubotariu 			SNDRV_CTL_ELEM_ACCESS_TLV_READ |
70850291652SCodrin Ciubotariu 			SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
70950291652SCodrin Ciubotariu 		.info = mchp_pdmc_chmap_ctl_info,
71050291652SCodrin Ciubotariu 		.get = mchp_pdmc_chmap_ctl_get,
71150291652SCodrin Ciubotariu 		.put = mchp_pdmc_chmap_ctl_put,
71250291652SCodrin Ciubotariu 		.tlv.c = mchp_pdmc_chmap_ctl_tlv,
71350291652SCodrin Ciubotariu 	};
71450291652SCodrin Ciubotariu 	int err;
71550291652SCodrin Ciubotariu 
71650291652SCodrin Ciubotariu 	if (WARN_ON(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].chmap_kctl))
71750291652SCodrin Ciubotariu 		return -EBUSY;
71850291652SCodrin Ciubotariu 	info = kzalloc(sizeof(*info), GFP_KERNEL);
71950291652SCodrin Ciubotariu 	if (!info)
72050291652SCodrin Ciubotariu 		return -ENOMEM;
72150291652SCodrin Ciubotariu 	info->pcm = pcm;
72250291652SCodrin Ciubotariu 	info->dd = dd;
72350291652SCodrin Ciubotariu 	info->chmap = mchp_pdmc_std_chmaps;
72450291652SCodrin Ciubotariu 	knew.name = "Capture Channel Map";
72550291652SCodrin Ciubotariu 	knew.device = pcm->device;
72650291652SCodrin Ciubotariu 	knew.count = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count;
72750291652SCodrin Ciubotariu 	info->kctl = snd_ctl_new1(&knew, info);
72850291652SCodrin Ciubotariu 	if (!info->kctl) {
72950291652SCodrin Ciubotariu 		kfree(info);
73050291652SCodrin Ciubotariu 		return -ENOMEM;
73150291652SCodrin Ciubotariu 	}
73250291652SCodrin Ciubotariu 	info->kctl->private_free = mchp_pdmc_chmap_ctl_private_free;
73350291652SCodrin Ciubotariu 	err = snd_ctl_add(pcm->card, info->kctl);
73450291652SCodrin Ciubotariu 	if (err < 0)
73550291652SCodrin Ciubotariu 		return err;
73650291652SCodrin Ciubotariu 	pcm->streams[SNDRV_PCM_STREAM_CAPTURE].chmap_kctl = info->kctl;
73750291652SCodrin Ciubotariu 	return 0;
73850291652SCodrin Ciubotariu }
73950291652SCodrin Ciubotariu 
mchp_pdmc_pcm_new(struct snd_soc_pcm_runtime * rtd,struct snd_soc_dai * dai)74050291652SCodrin Ciubotariu static int mchp_pdmc_pcm_new(struct snd_soc_pcm_runtime *rtd,
74150291652SCodrin Ciubotariu 			     struct snd_soc_dai *dai)
74250291652SCodrin Ciubotariu {
74350291652SCodrin Ciubotariu 	struct mchp_pdmc *dd = snd_soc_dai_get_drvdata(dai);
74450291652SCodrin Ciubotariu 	int ret;
74550291652SCodrin Ciubotariu 
74650291652SCodrin Ciubotariu 	ret = mchp_pdmc_add_chmap_ctls(rtd->pcm, dd);
74751124a30SClaudiu Beznea 	if (ret < 0)
74850291652SCodrin Ciubotariu 		dev_err(dd->dev, "failed to add channel map controls: %d\n", ret);
74950291652SCodrin Ciubotariu 
75051124a30SClaudiu Beznea 	return ret;
75150291652SCodrin Ciubotariu }
75250291652SCodrin Ciubotariu 
7532ff8a43dSKuninori Morimoto static const struct snd_soc_dai_ops mchp_pdmc_dai_ops = {
75450291652SCodrin Ciubotariu 	.probe		= mchp_pdmc_dai_probe,
7552ff8a43dSKuninori Morimoto 	.set_fmt	= mchp_pdmc_set_fmt,
7562ff8a43dSKuninori Morimoto 	.startup	= mchp_pdmc_startup,
7572ff8a43dSKuninori Morimoto 	.hw_params	= mchp_pdmc_hw_params,
7582ff8a43dSKuninori Morimoto 	.trigger	= mchp_pdmc_trigger,
7592ff8a43dSKuninori Morimoto 	.pcm_new	= &mchp_pdmc_pcm_new,
7602ff8a43dSKuninori Morimoto };
7612ff8a43dSKuninori Morimoto 
7622ff8a43dSKuninori Morimoto static struct snd_soc_dai_driver mchp_pdmc_dai = {
76350291652SCodrin Ciubotariu 	.capture = {
76450291652SCodrin Ciubotariu 		.stream_name	= "Capture",
76550291652SCodrin Ciubotariu 		.channels_min	= 1,
76650291652SCodrin Ciubotariu 		.channels_max	= 4,
76750291652SCodrin Ciubotariu 		.rate_min	= 8000,
76850291652SCodrin Ciubotariu 		.rate_max	= 192000,
76950291652SCodrin Ciubotariu 		.rates		= SNDRV_PCM_RATE_KNOT,
77050291652SCodrin Ciubotariu 		.formats	= SNDRV_PCM_FMTBIT_S24_LE,
77150291652SCodrin Ciubotariu 	},
77250291652SCodrin Ciubotariu 	.ops = &mchp_pdmc_dai_ops,
77350291652SCodrin Ciubotariu };
77450291652SCodrin Ciubotariu 
77550291652SCodrin Ciubotariu /* PDMC interrupt handler */
mchp_pdmc_interrupt(int irq,void * dev_id)77650291652SCodrin Ciubotariu static irqreturn_t mchp_pdmc_interrupt(int irq, void *dev_id)
77750291652SCodrin Ciubotariu {
778cb72b29cSClaudiu Beznea 	struct mchp_pdmc *dd = dev_id;
77950291652SCodrin Ciubotariu 	u32 isr, msr, pending;
78050291652SCodrin Ciubotariu 	irqreturn_t ret = IRQ_NONE;
78150291652SCodrin Ciubotariu 
78250291652SCodrin Ciubotariu 	regmap_read(dd->regmap, MCHP_PDMC_ISR, &isr);
78350291652SCodrin Ciubotariu 	regmap_read(dd->regmap, MCHP_PDMC_IMR, &msr);
78450291652SCodrin Ciubotariu 
78550291652SCodrin Ciubotariu 	pending = isr & msr;
78650291652SCodrin Ciubotariu 	dev_dbg(dd->dev, "ISR (0x%02x): 0x%08x, IMR (0x%02x): 0x%08x, pending: 0x%08x\n",
78750291652SCodrin Ciubotariu 		MCHP_PDMC_ISR, isr, MCHP_PDMC_IMR, msr, pending);
78850291652SCodrin Ciubotariu 	if (!pending)
78950291652SCodrin Ciubotariu 		return IRQ_NONE;
79050291652SCodrin Ciubotariu 
79150291652SCodrin Ciubotariu 	if (pending & MCHP_PDMC_IR_RXUDR) {
79250291652SCodrin Ciubotariu 		dev_warn(dd->dev, "underrun detected\n");
79350291652SCodrin Ciubotariu 		regmap_write(dd->regmap, MCHP_PDMC_IDR, MCHP_PDMC_IR_RXUDR);
79450291652SCodrin Ciubotariu 		ret = IRQ_HANDLED;
79550291652SCodrin Ciubotariu 	}
79650291652SCodrin Ciubotariu 	if (pending & MCHP_PDMC_IR_RXOVR) {
79750291652SCodrin Ciubotariu 		dev_warn(dd->dev, "overrun detected\n");
79850291652SCodrin Ciubotariu 		regmap_write(dd->regmap, MCHP_PDMC_IDR, MCHP_PDMC_IR_RXOVR);
79950291652SCodrin Ciubotariu 		ret = IRQ_HANDLED;
80050291652SCodrin Ciubotariu 	}
80150291652SCodrin Ciubotariu 
80250291652SCodrin Ciubotariu 	return ret;
80350291652SCodrin Ciubotariu }
80450291652SCodrin Ciubotariu 
80550291652SCodrin Ciubotariu /* regmap configuration */
mchp_pdmc_readable_reg(struct device * dev,unsigned int reg)80650291652SCodrin Ciubotariu static bool mchp_pdmc_readable_reg(struct device *dev, unsigned int reg)
80750291652SCodrin Ciubotariu {
80850291652SCodrin Ciubotariu 	switch (reg) {
80950291652SCodrin Ciubotariu 	case MCHP_PDMC_MR:
81050291652SCodrin Ciubotariu 	case MCHP_PDMC_CFGR:
81150291652SCodrin Ciubotariu 	case MCHP_PDMC_IMR:
81250291652SCodrin Ciubotariu 	case MCHP_PDMC_ISR:
813c5682e2bSClaudiu Beznea 	case MCHP_PDMC_RHR:
81450291652SCodrin Ciubotariu 	case MCHP_PDMC_VER:
81550291652SCodrin Ciubotariu 		return true;
81650291652SCodrin Ciubotariu 	default:
81750291652SCodrin Ciubotariu 		return false;
81850291652SCodrin Ciubotariu 	}
81950291652SCodrin Ciubotariu }
82050291652SCodrin Ciubotariu 
mchp_pdmc_writeable_reg(struct device * dev,unsigned int reg)82150291652SCodrin Ciubotariu static bool mchp_pdmc_writeable_reg(struct device *dev, unsigned int reg)
82250291652SCodrin Ciubotariu {
82350291652SCodrin Ciubotariu 	switch (reg) {
82450291652SCodrin Ciubotariu 	case MCHP_PDMC_CR:
82550291652SCodrin Ciubotariu 	case MCHP_PDMC_MR:
82650291652SCodrin Ciubotariu 	case MCHP_PDMC_CFGR:
82750291652SCodrin Ciubotariu 	case MCHP_PDMC_IER:
82850291652SCodrin Ciubotariu 	case MCHP_PDMC_IDR:
82950291652SCodrin Ciubotariu 		return true;
83050291652SCodrin Ciubotariu 	default:
83150291652SCodrin Ciubotariu 		return false;
83250291652SCodrin Ciubotariu 	}
83350291652SCodrin Ciubotariu }
83450291652SCodrin Ciubotariu 
mchp_pdmc_volatile_reg(struct device * dev,unsigned int reg)835c5682e2bSClaudiu Beznea static bool mchp_pdmc_volatile_reg(struct device *dev, unsigned int reg)
836c5682e2bSClaudiu Beznea {
837c5682e2bSClaudiu Beznea 	switch (reg) {
838c5682e2bSClaudiu Beznea 	case MCHP_PDMC_ISR:
839c5682e2bSClaudiu Beznea 	case MCHP_PDMC_RHR:
840c5682e2bSClaudiu Beznea 		return true;
841c5682e2bSClaudiu Beznea 	default:
842c5682e2bSClaudiu Beznea 		return false;
843c5682e2bSClaudiu Beznea 	}
844c5682e2bSClaudiu Beznea }
845c5682e2bSClaudiu Beznea 
mchp_pdmc_precious_reg(struct device * dev,unsigned int reg)84650291652SCodrin Ciubotariu static bool mchp_pdmc_precious_reg(struct device *dev, unsigned int reg)
84750291652SCodrin Ciubotariu {
84850291652SCodrin Ciubotariu 	switch (reg) {
84950291652SCodrin Ciubotariu 	case MCHP_PDMC_RHR:
85050291652SCodrin Ciubotariu 	case MCHP_PDMC_ISR:
85150291652SCodrin Ciubotariu 		return true;
85250291652SCodrin Ciubotariu 	default:
85350291652SCodrin Ciubotariu 		return false;
85450291652SCodrin Ciubotariu 	}
85550291652SCodrin Ciubotariu }
85650291652SCodrin Ciubotariu 
85750291652SCodrin Ciubotariu static const struct regmap_config mchp_pdmc_regmap_config = {
85850291652SCodrin Ciubotariu 	.reg_bits	= 32,
85950291652SCodrin Ciubotariu 	.reg_stride	= 4,
86050291652SCodrin Ciubotariu 	.val_bits	= 32,
86150291652SCodrin Ciubotariu 	.max_register	= MCHP_PDMC_VER,
86250291652SCodrin Ciubotariu 	.readable_reg	= mchp_pdmc_readable_reg,
86350291652SCodrin Ciubotariu 	.writeable_reg	= mchp_pdmc_writeable_reg,
86450291652SCodrin Ciubotariu 	.precious_reg	= mchp_pdmc_precious_reg,
865c5682e2bSClaudiu Beznea 	.volatile_reg	= mchp_pdmc_volatile_reg,
866e8c8e9deSClaudiu Beznea 	.cache_type	= REGCACHE_FLAT,
86750291652SCodrin Ciubotariu };
86850291652SCodrin Ciubotariu 
mchp_pdmc_dt_init(struct mchp_pdmc * dd)86950291652SCodrin Ciubotariu static int mchp_pdmc_dt_init(struct mchp_pdmc *dd)
87050291652SCodrin Ciubotariu {
87150291652SCodrin Ciubotariu 	struct device_node *np = dd->dev->of_node;
87250291652SCodrin Ciubotariu 	bool mic_ch[MCHP_PDMC_DS_NO][MCHP_PDMC_EDGE_NO] = {0};
87350291652SCodrin Ciubotariu 	int i;
87450291652SCodrin Ciubotariu 	int ret;
87550291652SCodrin Ciubotariu 
87650291652SCodrin Ciubotariu 	if (!np) {
87750291652SCodrin Ciubotariu 		dev_err(dd->dev, "device node not found\n");
87850291652SCodrin Ciubotariu 		return -EINVAL;
87950291652SCodrin Ciubotariu 	}
88050291652SCodrin Ciubotariu 
88150291652SCodrin Ciubotariu 	dd->mic_no = of_property_count_u32_elems(np, "microchip,mic-pos");
88250291652SCodrin Ciubotariu 	if (dd->mic_no < 0) {
883c639e85eSCodrin Ciubotariu 		dev_err(dd->dev, "failed to get microchip,mic-pos: %d",
88450291652SCodrin Ciubotariu 			dd->mic_no);
88550291652SCodrin Ciubotariu 		return dd->mic_no;
88650291652SCodrin Ciubotariu 	}
88750291652SCodrin Ciubotariu 	if (!dd->mic_no || dd->mic_no % 2 ||
88850291652SCodrin Ciubotariu 	    dd->mic_no / 2 > MCHP_PDMC_MAX_CHANNELS) {
889c639e85eSCodrin Ciubotariu 		dev_err(dd->dev, "invalid array length for microchip,mic-pos: %d",
89050291652SCodrin Ciubotariu 			dd->mic_no);
89150291652SCodrin Ciubotariu 		return -EINVAL;
89250291652SCodrin Ciubotariu 	}
89350291652SCodrin Ciubotariu 
89450291652SCodrin Ciubotariu 	dd->mic_no /= 2;
89550291652SCodrin Ciubotariu 
8966b6bb5e2SColin Ian King 	dev_info(dd->dev, "%d PDM microphones declared\n", dd->mic_no);
89750291652SCodrin Ciubotariu 
898c639e85eSCodrin Ciubotariu 	/*
899c639e85eSCodrin Ciubotariu 	 * by default, we consider the order of microphones in
900c639e85eSCodrin Ciubotariu 	 * microchip,mic-pos to be the same with the channel mapping;
901c639e85eSCodrin Ciubotariu 	 * 1st microphone channel 0, 2nd microphone channel 1, etc.
90250291652SCodrin Ciubotariu 	 */
90350291652SCodrin Ciubotariu 	for (i = 0; i < dd->mic_no; i++) {
90450291652SCodrin Ciubotariu 		int ds;
90550291652SCodrin Ciubotariu 		int edge;
90650291652SCodrin Ciubotariu 
90750291652SCodrin Ciubotariu 		ret = of_property_read_u32_index(np, "microchip,mic-pos", i * 2,
90850291652SCodrin Ciubotariu 						 &ds);
90950291652SCodrin Ciubotariu 		if (ret) {
91050291652SCodrin Ciubotariu 			dev_err(dd->dev,
91150291652SCodrin Ciubotariu 				"failed to get value no %d value from microchip,mic-pos: %d",
91250291652SCodrin Ciubotariu 				i * 2, ret);
91350291652SCodrin Ciubotariu 			return ret;
91450291652SCodrin Ciubotariu 		}
91550291652SCodrin Ciubotariu 		if (ds >= MCHP_PDMC_DS_NO) {
91650291652SCodrin Ciubotariu 			dev_err(dd->dev,
91750291652SCodrin Ciubotariu 				"invalid DS index in microchip,mic-pos array: %d",
91850291652SCodrin Ciubotariu 				ds);
91950291652SCodrin Ciubotariu 			return -EINVAL;
92050291652SCodrin Ciubotariu 		}
92150291652SCodrin Ciubotariu 
92250291652SCodrin Ciubotariu 		ret = of_property_read_u32_index(np, "microchip,mic-pos", i * 2 + 1,
92350291652SCodrin Ciubotariu 						 &edge);
92450291652SCodrin Ciubotariu 		if (ret) {
92550291652SCodrin Ciubotariu 			dev_err(dd->dev,
92650291652SCodrin Ciubotariu 				"failed to get value no %d value from microchip,mic-pos: %d",
92750291652SCodrin Ciubotariu 				i * 2 + 1, ret);
92850291652SCodrin Ciubotariu 			return ret;
92950291652SCodrin Ciubotariu 		}
93050291652SCodrin Ciubotariu 
93150291652SCodrin Ciubotariu 		if (edge != MCHP_PDMC_CLK_POSITIVE &&
93250291652SCodrin Ciubotariu 		    edge != MCHP_PDMC_CLK_NEGATIVE) {
93350291652SCodrin Ciubotariu 			dev_err(dd->dev,
93450291652SCodrin Ciubotariu 				"invalid edge in microchip,mic-pos array: %d", edge);
93550291652SCodrin Ciubotariu 			return -EINVAL;
93650291652SCodrin Ciubotariu 		}
93750291652SCodrin Ciubotariu 		if (mic_ch[ds][edge]) {
93850291652SCodrin Ciubotariu 			dev_err(dd->dev,
93950291652SCodrin Ciubotariu 				"duplicated mic (DS %d, edge %d) in microchip,mic-pos array",
94050291652SCodrin Ciubotariu 				ds, edge);
94150291652SCodrin Ciubotariu 			return -EINVAL;
94250291652SCodrin Ciubotariu 		}
94350291652SCodrin Ciubotariu 		mic_ch[ds][edge] = true;
94450291652SCodrin Ciubotariu 		dd->channel_mic_map[i].ds_pos = ds;
94550291652SCodrin Ciubotariu 		dd->channel_mic_map[i].clk_edge = edge;
94650291652SCodrin Ciubotariu 	}
94750291652SCodrin Ciubotariu 
948c5682e2bSClaudiu Beznea 	dd->startup_delay_us = 150000;
949c5682e2bSClaudiu Beznea 	of_property_read_u32(np, "microchip,startup-delay-us", &dd->startup_delay_us);
950c5682e2bSClaudiu Beznea 
95150291652SCodrin Ciubotariu 	return 0;
95250291652SCodrin Ciubotariu }
95350291652SCodrin Ciubotariu 
95450291652SCodrin 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)95550291652SCodrin Ciubotariu static int mchp_pdmc_process(struct snd_pcm_substream *substream,
95650291652SCodrin Ciubotariu 			     int channel, unsigned long hwoff,
957*69d0fd34STakashi Iwai 			     unsigned long bytes)
95850291652SCodrin Ciubotariu {
95950291652SCodrin Ciubotariu 	struct snd_pcm_runtime *runtime = substream->runtime;
96050291652SCodrin Ciubotariu 	u8 *dma_ptr = runtime->dma_area + hwoff +
96150291652SCodrin Ciubotariu 		      channel * (runtime->dma_bytes / runtime->channels);
96250291652SCodrin Ciubotariu 	u8 *dma_ptr_end = dma_ptr + bytes;
96350291652SCodrin Ciubotariu 	unsigned int sample_size = samples_to_bytes(runtime, 1);
96450291652SCodrin Ciubotariu 
96550291652SCodrin Ciubotariu 	for (; dma_ptr < dma_ptr_end; dma_ptr += sample_size)
96650291652SCodrin Ciubotariu 		*dma_ptr = 0;
96750291652SCodrin Ciubotariu 
96850291652SCodrin Ciubotariu 	return 0;
96950291652SCodrin Ciubotariu }
97050291652SCodrin Ciubotariu 
97150291652SCodrin Ciubotariu static struct snd_dmaengine_pcm_config mchp_pdmc_config = {
97250291652SCodrin Ciubotariu 	.process = mchp_pdmc_process,
9732bde1985SCodrin Ciubotariu 	.prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
97450291652SCodrin Ciubotariu };
97550291652SCodrin Ciubotariu 
mchp_pdmc_runtime_suspend(struct device * dev)976e8c8e9deSClaudiu Beznea static int mchp_pdmc_runtime_suspend(struct device *dev)
977e8c8e9deSClaudiu Beznea {
978e8c8e9deSClaudiu Beznea 	struct mchp_pdmc *dd = dev_get_drvdata(dev);
979e8c8e9deSClaudiu Beznea 
980e8c8e9deSClaudiu Beznea 	regcache_cache_only(dd->regmap, true);
981e8c8e9deSClaudiu Beznea 
982e8c8e9deSClaudiu Beznea 	clk_disable_unprepare(dd->gclk);
983e8c8e9deSClaudiu Beznea 	clk_disable_unprepare(dd->pclk);
984e8c8e9deSClaudiu Beznea 
985e8c8e9deSClaudiu Beznea 	return 0;
986e8c8e9deSClaudiu Beznea }
987e8c8e9deSClaudiu Beznea 
mchp_pdmc_runtime_resume(struct device * dev)988e8c8e9deSClaudiu Beznea static int mchp_pdmc_runtime_resume(struct device *dev)
989e8c8e9deSClaudiu Beznea {
990e8c8e9deSClaudiu Beznea 	struct mchp_pdmc *dd = dev_get_drvdata(dev);
991e8c8e9deSClaudiu Beznea 	int ret;
992e8c8e9deSClaudiu Beznea 
993e8c8e9deSClaudiu Beznea 	ret = clk_prepare_enable(dd->pclk);
994e8c8e9deSClaudiu Beznea 	if (ret) {
995e8c8e9deSClaudiu Beznea 		dev_err(dd->dev,
996e8c8e9deSClaudiu Beznea 			"failed to enable the peripheral clock: %d\n", ret);
997e8c8e9deSClaudiu Beznea 		return ret;
998e8c8e9deSClaudiu Beznea 	}
999e8c8e9deSClaudiu Beznea 	ret = clk_prepare_enable(dd->gclk);
1000e8c8e9deSClaudiu Beznea 	if (ret) {
1001e8c8e9deSClaudiu Beznea 		dev_err(dd->dev,
1002e8c8e9deSClaudiu Beznea 			"failed to enable generic clock: %d\n", ret);
1003e8c8e9deSClaudiu Beznea 		goto disable_pclk;
1004e8c8e9deSClaudiu Beznea 	}
1005e8c8e9deSClaudiu Beznea 
1006e8c8e9deSClaudiu Beznea 	regcache_cache_only(dd->regmap, false);
1007e8c8e9deSClaudiu Beznea 	regcache_mark_dirty(dd->regmap);
1008e8c8e9deSClaudiu Beznea 	ret = regcache_sync(dd->regmap);
1009e8c8e9deSClaudiu Beznea 	if (ret) {
1010e8c8e9deSClaudiu Beznea 		regcache_cache_only(dd->regmap, true);
1011e8c8e9deSClaudiu Beznea 		clk_disable_unprepare(dd->gclk);
1012e8c8e9deSClaudiu Beznea disable_pclk:
1013e8c8e9deSClaudiu Beznea 		clk_disable_unprepare(dd->pclk);
1014e8c8e9deSClaudiu Beznea 	}
1015e8c8e9deSClaudiu Beznea 
1016e8c8e9deSClaudiu Beznea 	return ret;
1017e8c8e9deSClaudiu Beznea }
1018e8c8e9deSClaudiu Beznea 
mchp_pdmc_probe(struct platform_device * pdev)101950291652SCodrin Ciubotariu static int mchp_pdmc_probe(struct platform_device *pdev)
102050291652SCodrin Ciubotariu {
102150291652SCodrin Ciubotariu 	struct device *dev = &pdev->dev;
102250291652SCodrin Ciubotariu 	struct mchp_pdmc *dd;
102350291652SCodrin Ciubotariu 	struct resource *res;
102450291652SCodrin Ciubotariu 	void __iomem *io_base;
102550291652SCodrin Ciubotariu 	u32 version;
102650291652SCodrin Ciubotariu 	int irq;
102750291652SCodrin Ciubotariu 	int ret;
102850291652SCodrin Ciubotariu 
102950291652SCodrin Ciubotariu 	dd = devm_kzalloc(dev, sizeof(*dd), GFP_KERNEL);
103050291652SCodrin Ciubotariu 	if (!dd)
103150291652SCodrin Ciubotariu 		return -ENOMEM;
103250291652SCodrin Ciubotariu 
103350291652SCodrin Ciubotariu 	dd->dev = &pdev->dev;
103450291652SCodrin Ciubotariu 	ret = mchp_pdmc_dt_init(dd);
103550291652SCodrin Ciubotariu 	if (ret < 0)
103650291652SCodrin Ciubotariu 		return ret;
103750291652SCodrin Ciubotariu 
103850291652SCodrin Ciubotariu 	irq = platform_get_irq(pdev, 0);
10392588a014SJiapeng Chong 	if (irq < 0)
104050291652SCodrin Ciubotariu 		return irq;
104150291652SCodrin Ciubotariu 
104250291652SCodrin Ciubotariu 	dd->pclk = devm_clk_get(dev, "pclk");
104350291652SCodrin Ciubotariu 	if (IS_ERR(dd->pclk)) {
104450291652SCodrin Ciubotariu 		ret = PTR_ERR(dd->pclk);
104550291652SCodrin Ciubotariu 		dev_err(dev, "failed to get peripheral clock: %d\n", ret);
104650291652SCodrin Ciubotariu 		return ret;
104750291652SCodrin Ciubotariu 	}
104850291652SCodrin Ciubotariu 
104950291652SCodrin Ciubotariu 	dd->gclk = devm_clk_get(dev, "gclk");
105050291652SCodrin Ciubotariu 	if (IS_ERR(dd->gclk)) {
105150291652SCodrin Ciubotariu 		ret = PTR_ERR(dd->gclk);
105250291652SCodrin Ciubotariu 		dev_err(dev, "failed to get GCK: %d\n", ret);
105350291652SCodrin Ciubotariu 		return ret;
105450291652SCodrin Ciubotariu 	}
105550291652SCodrin Ciubotariu 
105650291652SCodrin Ciubotariu 	io_base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
105750291652SCodrin Ciubotariu 	if (IS_ERR(io_base)) {
105850291652SCodrin Ciubotariu 		ret = PTR_ERR(io_base);
105950291652SCodrin Ciubotariu 		dev_err(dev, "failed to remap register memory: %d\n", ret);
106050291652SCodrin Ciubotariu 		return ret;
106150291652SCodrin Ciubotariu 	}
106250291652SCodrin Ciubotariu 
106350291652SCodrin Ciubotariu 	dd->regmap = devm_regmap_init_mmio(dev, io_base,
106450291652SCodrin Ciubotariu 					   &mchp_pdmc_regmap_config);
106550291652SCodrin Ciubotariu 	if (IS_ERR(dd->regmap)) {
106650291652SCodrin Ciubotariu 		ret = PTR_ERR(dd->regmap);
106750291652SCodrin Ciubotariu 		dev_err(dev, "failed to init register map: %d\n", ret);
106850291652SCodrin Ciubotariu 		return ret;
106950291652SCodrin Ciubotariu 	}
107050291652SCodrin Ciubotariu 
107150291652SCodrin Ciubotariu 	ret = devm_request_irq(dev, irq, mchp_pdmc_interrupt, 0,
1072cb72b29cSClaudiu Beznea 			       dev_name(&pdev->dev), dd);
107350291652SCodrin Ciubotariu 	if (ret < 0) {
107450291652SCodrin Ciubotariu 		dev_err(dev, "can't register ISR for IRQ %u (ret=%i)\n",
107550291652SCodrin Ciubotariu 			irq, ret);
107650291652SCodrin Ciubotariu 		return ret;
107750291652SCodrin Ciubotariu 	}
107850291652SCodrin Ciubotariu 
107950291652SCodrin Ciubotariu 	/* by default audio filter is enabled and the SINC Filter order
108050291652SCodrin Ciubotariu 	 * will be set to the recommended value, 3
108150291652SCodrin Ciubotariu 	 */
108250291652SCodrin Ciubotariu 	dd->audio_filter_en = true;
108350291652SCodrin Ciubotariu 	dd->sinc_order = 3;
108450291652SCodrin Ciubotariu 
108550291652SCodrin Ciubotariu 	dd->addr.addr = (dma_addr_t)res->start + MCHP_PDMC_RHR;
108650291652SCodrin Ciubotariu 	platform_set_drvdata(pdev, dd);
108750291652SCodrin Ciubotariu 
1088e8c8e9deSClaudiu Beznea 	pm_runtime_enable(dd->dev);
1089e8c8e9deSClaudiu Beznea 	if (!pm_runtime_enabled(dd->dev)) {
1090e8c8e9deSClaudiu Beznea 		ret = mchp_pdmc_runtime_resume(dd->dev);
1091e8c8e9deSClaudiu Beznea 		if (ret)
1092e8c8e9deSClaudiu Beznea 			return ret;
1093e8c8e9deSClaudiu Beznea 	}
1094e8c8e9deSClaudiu Beznea 
109550291652SCodrin Ciubotariu 	/* register platform */
109650291652SCodrin Ciubotariu 	ret = devm_snd_dmaengine_pcm_register(dev, &mchp_pdmc_config, 0);
109750291652SCodrin Ciubotariu 	if (ret) {
109850291652SCodrin Ciubotariu 		dev_err(dev, "could not register platform: %d\n", ret);
1099e8c8e9deSClaudiu Beznea 		goto pm_runtime_suspend;
110050291652SCodrin Ciubotariu 	}
110150291652SCodrin Ciubotariu 
110250291652SCodrin Ciubotariu 	ret = devm_snd_soc_register_component(dev, &mchp_pdmc_dai_component,
110350291652SCodrin Ciubotariu 					      &mchp_pdmc_dai, 1);
110450291652SCodrin Ciubotariu 	if (ret) {
110550291652SCodrin Ciubotariu 		dev_err(dev, "could not register CPU DAI: %d\n", ret);
1106e8c8e9deSClaudiu Beznea 		goto pm_runtime_suspend;
110750291652SCodrin Ciubotariu 	}
110850291652SCodrin Ciubotariu 
110950291652SCodrin Ciubotariu 	/* print IP version */
111050291652SCodrin Ciubotariu 	regmap_read(dd->regmap, MCHP_PDMC_VER, &version);
111150291652SCodrin Ciubotariu 	dev_info(dd->dev, "hw version: %#lx\n",
111250291652SCodrin Ciubotariu 		 version & MCHP_PDMC_VER_VERSION);
111350291652SCodrin Ciubotariu 
111450291652SCodrin Ciubotariu 	return 0;
1115e8c8e9deSClaudiu Beznea 
1116e8c8e9deSClaudiu Beznea pm_runtime_suspend:
1117e8c8e9deSClaudiu Beznea 	if (!pm_runtime_status_suspended(dd->dev))
1118e8c8e9deSClaudiu Beznea 		mchp_pdmc_runtime_suspend(dd->dev);
1119e8c8e9deSClaudiu Beznea 	pm_runtime_disable(dd->dev);
1120e8c8e9deSClaudiu Beznea 
1121e8c8e9deSClaudiu Beznea 	return ret;
1122e8c8e9deSClaudiu Beznea }
1123e8c8e9deSClaudiu Beznea 
mchp_pdmc_remove(struct platform_device * pdev)112486fdd482SUwe Kleine-König static void mchp_pdmc_remove(struct platform_device *pdev)
1125e8c8e9deSClaudiu Beznea {
1126e8c8e9deSClaudiu Beznea 	struct mchp_pdmc *dd = platform_get_drvdata(pdev);
1127e8c8e9deSClaudiu Beznea 
1128e8c8e9deSClaudiu Beznea 	if (!pm_runtime_status_suspended(dd->dev))
1129e8c8e9deSClaudiu Beznea 		mchp_pdmc_runtime_suspend(dd->dev);
1130e8c8e9deSClaudiu Beznea 
1131e8c8e9deSClaudiu Beznea 	pm_runtime_disable(dd->dev);
113250291652SCodrin Ciubotariu }
113350291652SCodrin Ciubotariu 
113450291652SCodrin Ciubotariu static const struct of_device_id mchp_pdmc_of_match[] = {
113550291652SCodrin Ciubotariu 	{
113650291652SCodrin Ciubotariu 		.compatible = "microchip,sama7g5-pdmc",
113750291652SCodrin Ciubotariu 	}, {
113850291652SCodrin Ciubotariu 		/* sentinel */
113950291652SCodrin Ciubotariu 	}
114050291652SCodrin Ciubotariu };
114150291652SCodrin Ciubotariu MODULE_DEVICE_TABLE(of, mchp_pdmc_of_match);
114250291652SCodrin Ciubotariu 
1143e8c8e9deSClaudiu Beznea static const struct dev_pm_ops mchp_pdmc_pm_ops = {
1144404c61c4SClaudiu Beznea 	SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume)
1145e8c8e9deSClaudiu Beznea 	RUNTIME_PM_OPS(mchp_pdmc_runtime_suspend, mchp_pdmc_runtime_resume,
1146e8c8e9deSClaudiu Beznea 		       NULL)
1147e8c8e9deSClaudiu Beznea };
1148e8c8e9deSClaudiu Beznea 
114950291652SCodrin Ciubotariu static struct platform_driver mchp_pdmc_driver = {
115050291652SCodrin Ciubotariu 	.driver	= {
115150291652SCodrin Ciubotariu 		.name		= "mchp-pdmc",
115250291652SCodrin Ciubotariu 		.of_match_table	= of_match_ptr(mchp_pdmc_of_match),
1153e8c8e9deSClaudiu Beznea 		.pm		= pm_ptr(&mchp_pdmc_pm_ops),
115450291652SCodrin Ciubotariu 	},
115550291652SCodrin Ciubotariu 	.probe	= mchp_pdmc_probe,
115686fdd482SUwe Kleine-König 	.remove_new = mchp_pdmc_remove,
115750291652SCodrin Ciubotariu };
115850291652SCodrin Ciubotariu module_platform_driver(mchp_pdmc_driver);
115950291652SCodrin Ciubotariu 
116050291652SCodrin Ciubotariu MODULE_DESCRIPTION("Microchip PDMC driver under ALSA SoC architecture");
116150291652SCodrin Ciubotariu MODULE_AUTHOR("Codrin Ciubotariu <codrin.ciubotariu@microchip.com>");
116250291652SCodrin Ciubotariu MODULE_LICENSE("GPL v2");
1163