xref: /openbmc/linux/sound/soc/fsl/fsl_micfil.c (revision bea1d61d5892083551f92818e964801baabd95a9)
147a70e6fSCosmin Samoila // SPDX-License-Identifier: GPL-2.0
247a70e6fSCosmin Samoila // Copyright 2018 NXP
347a70e6fSCosmin Samoila 
417f2142bSSascha Hauer #include <linux/bitfield.h>
547a70e6fSCosmin Samoila #include <linux/clk.h>
647a70e6fSCosmin Samoila #include <linux/device.h>
747a70e6fSCosmin Samoila #include <linux/interrupt.h>
847a70e6fSCosmin Samoila #include <linux/kobject.h>
947a70e6fSCosmin Samoila #include <linux/kernel.h>
1047a70e6fSCosmin Samoila #include <linux/module.h>
1147a70e6fSCosmin Samoila #include <linux/of.h>
1247a70e6fSCosmin Samoila #include <linux/of_address.h>
1347a70e6fSCosmin Samoila #include <linux/of_irq.h>
1447a70e6fSCosmin Samoila #include <linux/of_platform.h>
1547a70e6fSCosmin Samoila #include <linux/pm_runtime.h>
1647a70e6fSCosmin Samoila #include <linux/regmap.h>
1747a70e6fSCosmin Samoila #include <linux/sysfs.h>
1847a70e6fSCosmin Samoila #include <linux/types.h>
192495ba26SSascha Hauer #include <linux/dma/imx-dma.h>
2047a70e6fSCosmin Samoila #include <sound/dmaengine_pcm.h>
2147a70e6fSCosmin Samoila #include <sound/pcm.h>
2247a70e6fSCosmin Samoila #include <sound/soc.h>
2347a70e6fSCosmin Samoila #include <sound/tlv.h>
2447a70e6fSCosmin Samoila #include <sound/core.h>
2547a70e6fSCosmin Samoila 
2647a70e6fSCosmin Samoila #include "fsl_micfil.h"
2747a70e6fSCosmin Samoila #include "imx-pcm.h"
2847a70e6fSCosmin Samoila 
2947a70e6fSCosmin Samoila #define FSL_MICFIL_RATES		SNDRV_PCM_RATE_8000_48000
3047a70e6fSCosmin Samoila #define FSL_MICFIL_FORMATS		(SNDRV_PCM_FMTBIT_S16_LE)
3147a70e6fSCosmin Samoila 
32fb855b8dSSascha Hauer #define MICFIL_OSR_DEFAULT	16
33fb855b8dSSascha Hauer 
34*bea1d61dSSascha Hauer enum quality {
35*bea1d61dSSascha Hauer 	QUALITY_HIGH,
36*bea1d61dSSascha Hauer 	QUALITY_MEDIUM,
37*bea1d61dSSascha Hauer 	QUALITY_LOW,
38*bea1d61dSSascha Hauer 	QUALITY_VLOW0,
39*bea1d61dSSascha Hauer 	QUALITY_VLOW1,
40*bea1d61dSSascha Hauer 	QUALITY_VLOW2,
41*bea1d61dSSascha Hauer };
42*bea1d61dSSascha Hauer 
4347a70e6fSCosmin Samoila struct fsl_micfil {
4447a70e6fSCosmin Samoila 	struct platform_device *pdev;
4547a70e6fSCosmin Samoila 	struct regmap *regmap;
4647a70e6fSCosmin Samoila 	const struct fsl_micfil_soc_data *soc;
47b5cf28f7SShengjiu Wang 	struct clk *busclk;
4847a70e6fSCosmin Samoila 	struct clk *mclk;
4947a70e6fSCosmin Samoila 	struct snd_dmaengine_dai_dma_data dma_params_rx;
502495ba26SSascha Hauer 	struct sdma_peripheral_config sdmacfg;
5147a70e6fSCosmin Samoila 	unsigned int dataline;
5247a70e6fSCosmin Samoila 	char name[32];
5347a70e6fSCosmin Samoila 	int irq[MICFIL_IRQ_LINES];
54*bea1d61dSSascha Hauer 	enum quality quality;
5547a70e6fSCosmin Samoila };
5647a70e6fSCosmin Samoila 
5747a70e6fSCosmin Samoila struct fsl_micfil_soc_data {
5847a70e6fSCosmin Samoila 	unsigned int fifos;
5947a70e6fSCosmin Samoila 	unsigned int fifo_depth;
6047a70e6fSCosmin Samoila 	unsigned int dataline;
6147a70e6fSCosmin Samoila 	bool imx;
6247a70e6fSCosmin Samoila };
6347a70e6fSCosmin Samoila 
6447a70e6fSCosmin Samoila static struct fsl_micfil_soc_data fsl_micfil_imx8mm = {
6547a70e6fSCosmin Samoila 	.imx = true,
6647a70e6fSCosmin Samoila 	.fifos = 8,
6747a70e6fSCosmin Samoila 	.fifo_depth = 8,
6847a70e6fSCosmin Samoila 	.dataline =  0xf,
6947a70e6fSCosmin Samoila };
7047a70e6fSCosmin Samoila 
7147a70e6fSCosmin Samoila static const struct of_device_id fsl_micfil_dt_ids[] = {
7247a70e6fSCosmin Samoila 	{ .compatible = "fsl,imx8mm-micfil", .data = &fsl_micfil_imx8mm },
7347a70e6fSCosmin Samoila 	{}
7447a70e6fSCosmin Samoila };
7547a70e6fSCosmin Samoila MODULE_DEVICE_TABLE(of, fsl_micfil_dt_ids);
7647a70e6fSCosmin Samoila 
7747a70e6fSCosmin Samoila static const char * const micfil_quality_select_texts[] = {
78*bea1d61dSSascha Hauer 	[QUALITY_HIGH] = "High",
79*bea1d61dSSascha Hauer 	[QUALITY_MEDIUM] = "Medium",
80*bea1d61dSSascha Hauer 	[QUALITY_LOW] = "Low",
81*bea1d61dSSascha Hauer 	[QUALITY_VLOW0] = "VLow0",
82*bea1d61dSSascha Hauer 	[QUALITY_VLOW1] = "Vlow1",
83*bea1d61dSSascha Hauer 	[QUALITY_VLOW2] = "Vlow2",
8447a70e6fSCosmin Samoila };
8547a70e6fSCosmin Samoila 
8647a70e6fSCosmin Samoila static const struct soc_enum fsl_micfil_quality_enum =
87*bea1d61dSSascha Hauer 	SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(micfil_quality_select_texts),
8847a70e6fSCosmin Samoila 			    micfil_quality_select_texts);
8947a70e6fSCosmin Samoila 
9047a70e6fSCosmin Samoila static DECLARE_TLV_DB_SCALE(gain_tlv, 0, 100, 0);
9147a70e6fSCosmin Samoila 
92*bea1d61dSSascha Hauer static int micfil_set_quality(struct fsl_micfil *micfil)
93*bea1d61dSSascha Hauer {
94*bea1d61dSSascha Hauer 	u32 qsel;
95*bea1d61dSSascha Hauer 
96*bea1d61dSSascha Hauer 	switch (micfil->quality) {
97*bea1d61dSSascha Hauer 	case QUALITY_HIGH:
98*bea1d61dSSascha Hauer 		qsel = MICFIL_QSEL_HIGH_QUALITY;
99*bea1d61dSSascha Hauer 		break;
100*bea1d61dSSascha Hauer 	case QUALITY_MEDIUM:
101*bea1d61dSSascha Hauer 		qsel = MICFIL_QSEL_MEDIUM_QUALITY;
102*bea1d61dSSascha Hauer 		break;
103*bea1d61dSSascha Hauer 	case QUALITY_LOW:
104*bea1d61dSSascha Hauer 		qsel = MICFIL_QSEL_LOW_QUALITY;
105*bea1d61dSSascha Hauer 		break;
106*bea1d61dSSascha Hauer 	case QUALITY_VLOW0:
107*bea1d61dSSascha Hauer 		qsel = MICFIL_QSEL_VLOW0_QUALITY;
108*bea1d61dSSascha Hauer 		break;
109*bea1d61dSSascha Hauer 	case QUALITY_VLOW1:
110*bea1d61dSSascha Hauer 		qsel = MICFIL_QSEL_VLOW1_QUALITY;
111*bea1d61dSSascha Hauer 		break;
112*bea1d61dSSascha Hauer 	case QUALITY_VLOW2:
113*bea1d61dSSascha Hauer 		qsel = MICFIL_QSEL_VLOW2_QUALITY;
114*bea1d61dSSascha Hauer 		break;
115*bea1d61dSSascha Hauer 	}
116*bea1d61dSSascha Hauer 
117*bea1d61dSSascha Hauer 	return regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL2,
118*bea1d61dSSascha Hauer 				  MICFIL_CTRL2_QSEL,
119*bea1d61dSSascha Hauer 				  FIELD_PREP(MICFIL_CTRL2_QSEL, qsel));
120*bea1d61dSSascha Hauer }
121*bea1d61dSSascha Hauer 
122*bea1d61dSSascha Hauer static int micfil_quality_get(struct snd_kcontrol *kcontrol,
123*bea1d61dSSascha Hauer 			      struct snd_ctl_elem_value *ucontrol)
124*bea1d61dSSascha Hauer {
125*bea1d61dSSascha Hauer 	struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
126*bea1d61dSSascha Hauer 	struct fsl_micfil *micfil = snd_soc_component_get_drvdata(cmpnt);
127*bea1d61dSSascha Hauer 
128*bea1d61dSSascha Hauer 	ucontrol->value.integer.value[0] = micfil->quality;
129*bea1d61dSSascha Hauer 
130*bea1d61dSSascha Hauer 	return 0;
131*bea1d61dSSascha Hauer }
132*bea1d61dSSascha Hauer 
133*bea1d61dSSascha Hauer static int micfil_quality_set(struct snd_kcontrol *kcontrol,
134*bea1d61dSSascha Hauer 			      struct snd_ctl_elem_value *ucontrol)
135*bea1d61dSSascha Hauer {
136*bea1d61dSSascha Hauer 	struct snd_soc_component *cmpnt = snd_soc_kcontrol_component(kcontrol);
137*bea1d61dSSascha Hauer 	struct fsl_micfil *micfil = snd_soc_component_get_drvdata(cmpnt);
138*bea1d61dSSascha Hauer 
139*bea1d61dSSascha Hauer 	micfil->quality = ucontrol->value.integer.value[0];
140*bea1d61dSSascha Hauer 
141*bea1d61dSSascha Hauer 	return micfil_set_quality(micfil);
142*bea1d61dSSascha Hauer }
143*bea1d61dSSascha Hauer 
14447a70e6fSCosmin Samoila static const struct snd_kcontrol_new fsl_micfil_snd_controls[] = {
14547a70e6fSCosmin Samoila 	SOC_SINGLE_SX_TLV("CH0 Volume", REG_MICFIL_OUT_CTRL,
14647a70e6fSCosmin Samoila 			  MICFIL_OUTGAIN_CHX_SHIFT(0), 0xF, 0x7, gain_tlv),
14747a70e6fSCosmin Samoila 	SOC_SINGLE_SX_TLV("CH1 Volume", REG_MICFIL_OUT_CTRL,
14847a70e6fSCosmin Samoila 			  MICFIL_OUTGAIN_CHX_SHIFT(1), 0xF, 0x7, gain_tlv),
14947a70e6fSCosmin Samoila 	SOC_SINGLE_SX_TLV("CH2 Volume", REG_MICFIL_OUT_CTRL,
15047a70e6fSCosmin Samoila 			  MICFIL_OUTGAIN_CHX_SHIFT(2), 0xF, 0x7, gain_tlv),
15147a70e6fSCosmin Samoila 	SOC_SINGLE_SX_TLV("CH3 Volume", REG_MICFIL_OUT_CTRL,
15247a70e6fSCosmin Samoila 			  MICFIL_OUTGAIN_CHX_SHIFT(3), 0xF, 0x7, gain_tlv),
15347a70e6fSCosmin Samoila 	SOC_SINGLE_SX_TLV("CH4 Volume", REG_MICFIL_OUT_CTRL,
15447a70e6fSCosmin Samoila 			  MICFIL_OUTGAIN_CHX_SHIFT(4), 0xF, 0x7, gain_tlv),
15547a70e6fSCosmin Samoila 	SOC_SINGLE_SX_TLV("CH5 Volume", REG_MICFIL_OUT_CTRL,
15647a70e6fSCosmin Samoila 			  MICFIL_OUTGAIN_CHX_SHIFT(5), 0xF, 0x7, gain_tlv),
15747a70e6fSCosmin Samoila 	SOC_SINGLE_SX_TLV("CH6 Volume", REG_MICFIL_OUT_CTRL,
15847a70e6fSCosmin Samoila 			  MICFIL_OUTGAIN_CHX_SHIFT(6), 0xF, 0x7, gain_tlv),
15947a70e6fSCosmin Samoila 	SOC_SINGLE_SX_TLV("CH7 Volume", REG_MICFIL_OUT_CTRL,
16047a70e6fSCosmin Samoila 			  MICFIL_OUTGAIN_CHX_SHIFT(7), 0xF, 0x7, gain_tlv),
16147a70e6fSCosmin Samoila 	SOC_ENUM_EXT("MICFIL Quality Select",
16247a70e6fSCosmin Samoila 		     fsl_micfil_quality_enum,
163*bea1d61dSSascha Hauer 		     micfil_quality_get, micfil_quality_set),
16447a70e6fSCosmin Samoila };
16547a70e6fSCosmin Samoila 
16647a70e6fSCosmin Samoila /* The SRES is a self-negated bit which provides the CPU with the
16747a70e6fSCosmin Samoila  * capability to initialize the PDM Interface module through the
16847a70e6fSCosmin Samoila  * slave-bus interface. This bit always reads as zero, and this
16947a70e6fSCosmin Samoila  * bit is only effective when MDIS is cleared
17047a70e6fSCosmin Samoila  */
17147a70e6fSCosmin Samoila static int fsl_micfil_reset(struct device *dev)
17247a70e6fSCosmin Samoila {
17347a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = dev_get_drvdata(dev);
17447a70e6fSCosmin Samoila 	int ret;
17547a70e6fSCosmin Samoila 
176d46c2127SSascha Hauer 	ret = regmap_clear_bits(micfil->regmap, REG_MICFIL_CTRL1,
177d46c2127SSascha Hauer 				MICFIL_CTRL1_MDIS);
1782c602c7eSSascha Hauer 	if (ret)
17947a70e6fSCosmin Samoila 		return ret;
18047a70e6fSCosmin Samoila 
181d46c2127SSascha Hauer 	ret = regmap_set_bits(micfil->regmap, REG_MICFIL_CTRL1,
18247a70e6fSCosmin Samoila 			      MICFIL_CTRL1_SRES);
1832c602c7eSSascha Hauer 	if (ret)
18447a70e6fSCosmin Samoila 		return ret;
18547a70e6fSCosmin Samoila 
18647a70e6fSCosmin Samoila 	return 0;
18747a70e6fSCosmin Samoila }
18847a70e6fSCosmin Samoila 
18947a70e6fSCosmin Samoila static int fsl_micfil_startup(struct snd_pcm_substream *substream,
19047a70e6fSCosmin Samoila 			      struct snd_soc_dai *dai)
19147a70e6fSCosmin Samoila {
19247a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = snd_soc_dai_get_drvdata(dai);
19347a70e6fSCosmin Samoila 
19447a70e6fSCosmin Samoila 	if (!micfil) {
19511106cb3STang Bin 		dev_err(dai->dev, "micfil dai priv_data not set\n");
19647a70e6fSCosmin Samoila 		return -EINVAL;
19747a70e6fSCosmin Samoila 	}
19847a70e6fSCosmin Samoila 
19947a70e6fSCosmin Samoila 	return 0;
20047a70e6fSCosmin Samoila }
20147a70e6fSCosmin Samoila 
20247a70e6fSCosmin Samoila static int fsl_micfil_trigger(struct snd_pcm_substream *substream, int cmd,
20347a70e6fSCosmin Samoila 			      struct snd_soc_dai *dai)
20447a70e6fSCosmin Samoila {
20547a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = snd_soc_dai_get_drvdata(dai);
20647a70e6fSCosmin Samoila 	struct device *dev = &micfil->pdev->dev;
20747a70e6fSCosmin Samoila 	int ret;
20847a70e6fSCosmin Samoila 
20947a70e6fSCosmin Samoila 	switch (cmd) {
21047a70e6fSCosmin Samoila 	case SNDRV_PCM_TRIGGER_START:
21147a70e6fSCosmin Samoila 	case SNDRV_PCM_TRIGGER_RESUME:
21247a70e6fSCosmin Samoila 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
21347a70e6fSCosmin Samoila 		ret = fsl_micfil_reset(dev);
21447a70e6fSCosmin Samoila 		if (ret) {
21547a70e6fSCosmin Samoila 			dev_err(dev, "failed to soft reset\n");
21647a70e6fSCosmin Samoila 			return ret;
21747a70e6fSCosmin Samoila 		}
21847a70e6fSCosmin Samoila 
21947a70e6fSCosmin Samoila 		/* DMA Interrupt Selection - DISEL bits
22047a70e6fSCosmin Samoila 		 * 00 - DMA and IRQ disabled
22147a70e6fSCosmin Samoila 		 * 01 - DMA req enabled
22247a70e6fSCosmin Samoila 		 * 10 - IRQ enabled
22347a70e6fSCosmin Samoila 		 * 11 - reserved
22447a70e6fSCosmin Samoila 		 */
22547a70e6fSCosmin Samoila 		ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL1,
22617f2142bSSascha Hauer 				MICFIL_CTRL1_DISEL,
22717f2142bSSascha Hauer 				FIELD_PREP(MICFIL_CTRL1_DISEL, MICFIL_CTRL1_DISEL_DMA));
2282c602c7eSSascha Hauer 		if (ret)
22947a70e6fSCosmin Samoila 			return ret;
23047a70e6fSCosmin Samoila 
23147a70e6fSCosmin Samoila 		/* Enable the module */
232d46c2127SSascha Hauer 		ret = regmap_set_bits(micfil->regmap, REG_MICFIL_CTRL1,
23347a70e6fSCosmin Samoila 				      MICFIL_CTRL1_PDMIEN);
2342c602c7eSSascha Hauer 		if (ret)
23547a70e6fSCosmin Samoila 			return ret;
23647a70e6fSCosmin Samoila 
23747a70e6fSCosmin Samoila 		break;
23847a70e6fSCosmin Samoila 	case SNDRV_PCM_TRIGGER_STOP:
23947a70e6fSCosmin Samoila 	case SNDRV_PCM_TRIGGER_SUSPEND:
24047a70e6fSCosmin Samoila 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
24147a70e6fSCosmin Samoila 		/* Disable the module */
242d46c2127SSascha Hauer 		ret = regmap_clear_bits(micfil->regmap, REG_MICFIL_CTRL1,
243d46c2127SSascha Hauer 					MICFIL_CTRL1_PDMIEN);
2442c602c7eSSascha Hauer 		if (ret)
24547a70e6fSCosmin Samoila 			return ret;
24647a70e6fSCosmin Samoila 
24747a70e6fSCosmin Samoila 		ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL1,
24817f2142bSSascha Hauer 				MICFIL_CTRL1_DISEL,
24917f2142bSSascha Hauer 				FIELD_PREP(MICFIL_CTRL1_DISEL, MICFIL_CTRL1_DISEL_DISABLE));
2502c602c7eSSascha Hauer 		if (ret)
25147a70e6fSCosmin Samoila 			return ret;
25247a70e6fSCosmin Samoila 		break;
25347a70e6fSCosmin Samoila 	default:
25447a70e6fSCosmin Samoila 		return -EINVAL;
25547a70e6fSCosmin Samoila 	}
25647a70e6fSCosmin Samoila 	return 0;
25747a70e6fSCosmin Samoila }
25847a70e6fSCosmin Samoila 
25947a70e6fSCosmin Samoila static int fsl_set_clock_params(struct device *dev, unsigned int rate)
26047a70e6fSCosmin Samoila {
26147a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = dev_get_drvdata(dev);
262e8936f69SSascha Hauer 	int clk_div = 8;
263*bea1d61dSSascha Hauer 	int osr = MICFIL_OSR_DEFAULT;
26415b5c496STang Bin 	int ret;
26547a70e6fSCosmin Samoila 
266*bea1d61dSSascha Hauer 	ret = clk_set_rate(micfil->mclk, rate * clk_div * osr * 8);
267e8936f69SSascha Hauer 	if (ret)
268e8936f69SSascha Hauer 		return ret;
26947a70e6fSCosmin Samoila 
270*bea1d61dSSascha Hauer 	ret = micfil_set_quality(micfil);
27147a70e6fSCosmin Samoila 	if (ret)
2722c602c7eSSascha Hauer 		return ret;
27347a70e6fSCosmin Samoila 
2742c602c7eSSascha Hauer 	ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL2,
275*bea1d61dSSascha Hauer 				 MICFIL_CTRL2_CLKDIV | MICFIL_CTRL2_CICOSR,
276*bea1d61dSSascha Hauer 				 FIELD_PREP(MICFIL_CTRL2_CLKDIV, clk_div) |
277*bea1d61dSSascha Hauer 				 FIELD_PREP(MICFIL_CTRL2_CICOSR, 16 - osr));
27847a70e6fSCosmin Samoila 
27947a70e6fSCosmin Samoila 	return ret;
28047a70e6fSCosmin Samoila }
28147a70e6fSCosmin Samoila 
28247a70e6fSCosmin Samoila static int fsl_micfil_hw_params(struct snd_pcm_substream *substream,
28347a70e6fSCosmin Samoila 				struct snd_pcm_hw_params *params,
28447a70e6fSCosmin Samoila 				struct snd_soc_dai *dai)
28547a70e6fSCosmin Samoila {
28647a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = snd_soc_dai_get_drvdata(dai);
28747a70e6fSCosmin Samoila 	unsigned int channels = params_channels(params);
28847a70e6fSCosmin Samoila 	unsigned int rate = params_rate(params);
28947a70e6fSCosmin Samoila 	struct device *dev = &micfil->pdev->dev;
29047a70e6fSCosmin Samoila 	int ret;
29147a70e6fSCosmin Samoila 
29247a70e6fSCosmin Samoila 	/* 1. Disable the module */
293d46c2127SSascha Hauer 	ret = regmap_clear_bits(micfil->regmap, REG_MICFIL_CTRL1,
294d46c2127SSascha Hauer 				MICFIL_CTRL1_PDMIEN);
2952c602c7eSSascha Hauer 	if (ret)
29647a70e6fSCosmin Samoila 		return ret;
29747a70e6fSCosmin Samoila 
29847a70e6fSCosmin Samoila 	/* enable channels */
29947a70e6fSCosmin Samoila 	ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL1,
30047a70e6fSCosmin Samoila 				 0xFF, ((1 << channels) - 1));
3012c602c7eSSascha Hauer 	if (ret)
30247a70e6fSCosmin Samoila 		return ret;
30347a70e6fSCosmin Samoila 
30447a70e6fSCosmin Samoila 	ret = fsl_set_clock_params(dev, rate);
30547a70e6fSCosmin Samoila 	if (ret < 0) {
30647a70e6fSCosmin Samoila 		dev_err(dev, "Failed to set clock parameters [%d]\n", ret);
30747a70e6fSCosmin Samoila 		return ret;
30847a70e6fSCosmin Samoila 	}
30947a70e6fSCosmin Samoila 
3102495ba26SSascha Hauer 	micfil->dma_params_rx.peripheral_config = &micfil->sdmacfg;
3112495ba26SSascha Hauer 	micfil->dma_params_rx.peripheral_size = sizeof(micfil->sdmacfg);
3122495ba26SSascha Hauer 	micfil->sdmacfg.n_fifos_src = channels;
3132495ba26SSascha Hauer 	micfil->sdmacfg.sw_done = true;
31447a70e6fSCosmin Samoila 	micfil->dma_params_rx.maxburst = channels * MICFIL_DMA_MAXBURST_RX;
31547a70e6fSCosmin Samoila 
31647a70e6fSCosmin Samoila 	return 0;
31747a70e6fSCosmin Samoila }
31847a70e6fSCosmin Samoila 
31938d89a56SRikard Falkeborn static const struct snd_soc_dai_ops fsl_micfil_dai_ops = {
32047a70e6fSCosmin Samoila 	.startup = fsl_micfil_startup,
32147a70e6fSCosmin Samoila 	.trigger = fsl_micfil_trigger,
32247a70e6fSCosmin Samoila 	.hw_params = fsl_micfil_hw_params,
32347a70e6fSCosmin Samoila };
32447a70e6fSCosmin Samoila 
32547a70e6fSCosmin Samoila static int fsl_micfil_dai_probe(struct snd_soc_dai *cpu_dai)
32647a70e6fSCosmin Samoila {
32747a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = dev_get_drvdata(cpu_dai->dev);
32847a70e6fSCosmin Samoila 	int ret;
32947a70e6fSCosmin Samoila 
330*bea1d61dSSascha Hauer 	micfil->quality = QUALITY_MEDIUM;
33147a70e6fSCosmin Samoila 
33247a70e6fSCosmin Samoila 	/* set default gain to max_gain */
33347a70e6fSCosmin Samoila 	regmap_write(micfil->regmap, REG_MICFIL_OUT_CTRL, 0x77777777);
33447a70e6fSCosmin Samoila 
33547a70e6fSCosmin Samoila 	snd_soc_dai_init_dma_data(cpu_dai, NULL,
33647a70e6fSCosmin Samoila 				  &micfil->dma_params_rx);
33747a70e6fSCosmin Samoila 
33847a70e6fSCosmin Samoila 	/* FIFO Watermark Control - FIFOWMK*/
33947a70e6fSCosmin Samoila 	ret = regmap_update_bits(micfil->regmap, REG_MICFIL_FIFO_CTRL,
34017f2142bSSascha Hauer 			MICFIL_FIFO_CTRL_FIFOWMK,
34117f2142bSSascha Hauer 			FIELD_PREP(MICFIL_FIFO_CTRL_FIFOWMK, micfil->soc->fifo_depth - 1));
3422c602c7eSSascha Hauer 	if (ret)
34347a70e6fSCosmin Samoila 		return ret;
34447a70e6fSCosmin Samoila 
34547a70e6fSCosmin Samoila 	return 0;
34647a70e6fSCosmin Samoila }
34747a70e6fSCosmin Samoila 
34847a70e6fSCosmin Samoila static struct snd_soc_dai_driver fsl_micfil_dai = {
34947a70e6fSCosmin Samoila 	.probe = fsl_micfil_dai_probe,
35047a70e6fSCosmin Samoila 	.capture = {
35147a70e6fSCosmin Samoila 		.stream_name = "CPU-Capture",
35247a70e6fSCosmin Samoila 		.channels_min = 1,
35347a70e6fSCosmin Samoila 		.channels_max = 8,
35447a70e6fSCosmin Samoila 		.rates = FSL_MICFIL_RATES,
35547a70e6fSCosmin Samoila 		.formats = FSL_MICFIL_FORMATS,
35647a70e6fSCosmin Samoila 	},
35747a70e6fSCosmin Samoila 	.ops = &fsl_micfil_dai_ops,
35847a70e6fSCosmin Samoila };
35947a70e6fSCosmin Samoila 
36047a70e6fSCosmin Samoila static const struct snd_soc_component_driver fsl_micfil_component = {
36147a70e6fSCosmin Samoila 	.name		= "fsl-micfil-dai",
36247a70e6fSCosmin Samoila 	.controls       = fsl_micfil_snd_controls,
36347a70e6fSCosmin Samoila 	.num_controls   = ARRAY_SIZE(fsl_micfil_snd_controls),
36447a70e6fSCosmin Samoila 
36547a70e6fSCosmin Samoila };
36647a70e6fSCosmin Samoila 
36747a70e6fSCosmin Samoila /* REGMAP */
36847a70e6fSCosmin Samoila static const struct reg_default fsl_micfil_reg_defaults[] = {
36947a70e6fSCosmin Samoila 	{REG_MICFIL_CTRL1,		0x00000000},
37047a70e6fSCosmin Samoila 	{REG_MICFIL_CTRL2,		0x00000000},
37147a70e6fSCosmin Samoila 	{REG_MICFIL_STAT,		0x00000000},
37247a70e6fSCosmin Samoila 	{REG_MICFIL_FIFO_CTRL,		0x00000007},
37347a70e6fSCosmin Samoila 	{REG_MICFIL_FIFO_STAT,		0x00000000},
37447a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH0,		0x00000000},
37547a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH1,		0x00000000},
37647a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH2,		0x00000000},
37747a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH3,		0x00000000},
37847a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH4,		0x00000000},
37947a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH5,		0x00000000},
38047a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH6,		0x00000000},
38147a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH7,		0x00000000},
38247a70e6fSCosmin Samoila 	{REG_MICFIL_DC_CTRL,		0x00000000},
38347a70e6fSCosmin Samoila 	{REG_MICFIL_OUT_CTRL,		0x00000000},
38447a70e6fSCosmin Samoila 	{REG_MICFIL_OUT_STAT,		0x00000000},
38547a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_CTRL1,		0x00000000},
38647a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_CTRL2,		0x000A0000},
38747a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_STAT,		0x00000000},
38847a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_SCONFIG,	0x00000000},
38947a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_NCONFIG,	0x80000000},
39047a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_NDATA,		0x00000000},
39147a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_ZCD,		0x00000004},
39247a70e6fSCosmin Samoila };
39347a70e6fSCosmin Samoila 
39447a70e6fSCosmin Samoila static bool fsl_micfil_readable_reg(struct device *dev, unsigned int reg)
39547a70e6fSCosmin Samoila {
39647a70e6fSCosmin Samoila 	switch (reg) {
39747a70e6fSCosmin Samoila 	case REG_MICFIL_CTRL1:
39847a70e6fSCosmin Samoila 	case REG_MICFIL_CTRL2:
39947a70e6fSCosmin Samoila 	case REG_MICFIL_STAT:
40047a70e6fSCosmin Samoila 	case REG_MICFIL_FIFO_CTRL:
40147a70e6fSCosmin Samoila 	case REG_MICFIL_FIFO_STAT:
40247a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH0:
40347a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH1:
40447a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH2:
40547a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH3:
40647a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH4:
40747a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH5:
40847a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH6:
40947a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH7:
41047a70e6fSCosmin Samoila 	case REG_MICFIL_DC_CTRL:
41147a70e6fSCosmin Samoila 	case REG_MICFIL_OUT_CTRL:
41247a70e6fSCosmin Samoila 	case REG_MICFIL_OUT_STAT:
41347a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_CTRL1:
41447a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_CTRL2:
41547a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_STAT:
41647a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_SCONFIG:
41747a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_NCONFIG:
41847a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_NDATA:
41947a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_ZCD:
42047a70e6fSCosmin Samoila 		return true;
42147a70e6fSCosmin Samoila 	default:
42247a70e6fSCosmin Samoila 		return false;
42347a70e6fSCosmin Samoila 	}
42447a70e6fSCosmin Samoila }
42547a70e6fSCosmin Samoila 
42647a70e6fSCosmin Samoila static bool fsl_micfil_writeable_reg(struct device *dev, unsigned int reg)
42747a70e6fSCosmin Samoila {
42847a70e6fSCosmin Samoila 	switch (reg) {
42947a70e6fSCosmin Samoila 	case REG_MICFIL_CTRL1:
43047a70e6fSCosmin Samoila 	case REG_MICFIL_CTRL2:
43147a70e6fSCosmin Samoila 	case REG_MICFIL_STAT:		/* Write 1 to Clear */
43247a70e6fSCosmin Samoila 	case REG_MICFIL_FIFO_CTRL:
43347a70e6fSCosmin Samoila 	case REG_MICFIL_FIFO_STAT:	/* Write 1 to Clear */
43447a70e6fSCosmin Samoila 	case REG_MICFIL_DC_CTRL:
43547a70e6fSCosmin Samoila 	case REG_MICFIL_OUT_CTRL:
43647a70e6fSCosmin Samoila 	case REG_MICFIL_OUT_STAT:	/* Write 1 to Clear */
43747a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_CTRL1:
43847a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_CTRL2:
43947a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_STAT:	/* Write 1 to Clear */
44047a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_SCONFIG:
44147a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_NCONFIG:
44247a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_ZCD:
44347a70e6fSCosmin Samoila 		return true;
44447a70e6fSCosmin Samoila 	default:
44547a70e6fSCosmin Samoila 		return false;
44647a70e6fSCosmin Samoila 	}
44747a70e6fSCosmin Samoila }
44847a70e6fSCosmin Samoila 
44947a70e6fSCosmin Samoila static bool fsl_micfil_volatile_reg(struct device *dev, unsigned int reg)
45047a70e6fSCosmin Samoila {
45147a70e6fSCosmin Samoila 	switch (reg) {
45247a70e6fSCosmin Samoila 	case REG_MICFIL_STAT:
45347a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH0:
45447a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH1:
45547a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH2:
45647a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH3:
45747a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH4:
45847a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH5:
45947a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH6:
46047a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH7:
46147a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_STAT:
46247a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_NDATA:
46347a70e6fSCosmin Samoila 		return true;
46447a70e6fSCosmin Samoila 	default:
46547a70e6fSCosmin Samoila 		return false;
46647a70e6fSCosmin Samoila 	}
46747a70e6fSCosmin Samoila }
46847a70e6fSCosmin Samoila 
46947a70e6fSCosmin Samoila static const struct regmap_config fsl_micfil_regmap_config = {
47047a70e6fSCosmin Samoila 	.reg_bits = 32,
47147a70e6fSCosmin Samoila 	.reg_stride = 4,
47247a70e6fSCosmin Samoila 	.val_bits = 32,
47347a70e6fSCosmin Samoila 
47447a70e6fSCosmin Samoila 	.max_register = REG_MICFIL_VAD0_ZCD,
47547a70e6fSCosmin Samoila 	.reg_defaults = fsl_micfil_reg_defaults,
47647a70e6fSCosmin Samoila 	.num_reg_defaults = ARRAY_SIZE(fsl_micfil_reg_defaults),
47747a70e6fSCosmin Samoila 	.readable_reg = fsl_micfil_readable_reg,
47847a70e6fSCosmin Samoila 	.volatile_reg = fsl_micfil_volatile_reg,
47947a70e6fSCosmin Samoila 	.writeable_reg = fsl_micfil_writeable_reg,
48047a70e6fSCosmin Samoila 	.cache_type = REGCACHE_RBTREE,
48147a70e6fSCosmin Samoila };
48247a70e6fSCosmin Samoila 
48347a70e6fSCosmin Samoila /* END OF REGMAP */
48447a70e6fSCosmin Samoila 
48547a70e6fSCosmin Samoila static irqreturn_t micfil_isr(int irq, void *devid)
48647a70e6fSCosmin Samoila {
48747a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = (struct fsl_micfil *)devid;
48847a70e6fSCosmin Samoila 	struct platform_device *pdev = micfil->pdev;
48947a70e6fSCosmin Samoila 	u32 stat_reg;
49047a70e6fSCosmin Samoila 	u32 fifo_stat_reg;
49147a70e6fSCosmin Samoila 	u32 ctrl1_reg;
49247a70e6fSCosmin Samoila 	bool dma_enabled;
49347a70e6fSCosmin Samoila 	int i;
49447a70e6fSCosmin Samoila 
49547a70e6fSCosmin Samoila 	regmap_read(micfil->regmap, REG_MICFIL_STAT, &stat_reg);
49647a70e6fSCosmin Samoila 	regmap_read(micfil->regmap, REG_MICFIL_CTRL1, &ctrl1_reg);
49747a70e6fSCosmin Samoila 	regmap_read(micfil->regmap, REG_MICFIL_FIFO_STAT, &fifo_stat_reg);
49847a70e6fSCosmin Samoila 
49917f2142bSSascha Hauer 	dma_enabled = FIELD_GET(MICFIL_CTRL1_DISEL, ctrl1_reg) == MICFIL_CTRL1_DISEL_DMA;
50047a70e6fSCosmin Samoila 
50147a70e6fSCosmin Samoila 	/* Channel 0-7 Output Data Flags */
50247a70e6fSCosmin Samoila 	for (i = 0; i < MICFIL_OUTPUT_CHANNELS; i++) {
50317f2142bSSascha Hauer 		if (stat_reg & MICFIL_STAT_CHXF(i))
50447a70e6fSCosmin Samoila 			dev_dbg(&pdev->dev,
50547a70e6fSCosmin Samoila 				"Data available in Data Channel %d\n", i);
50647a70e6fSCosmin Samoila 		/* if DMA is not enabled, field must be written with 1
50747a70e6fSCosmin Samoila 		 * to clear
50847a70e6fSCosmin Samoila 		 */
50947a70e6fSCosmin Samoila 		if (!dma_enabled)
51047a70e6fSCosmin Samoila 			regmap_write_bits(micfil->regmap,
51147a70e6fSCosmin Samoila 					  REG_MICFIL_STAT,
51217f2142bSSascha Hauer 					  MICFIL_STAT_CHXF(i),
51347a70e6fSCosmin Samoila 					  1);
51447a70e6fSCosmin Samoila 	}
51547a70e6fSCosmin Samoila 
51647a70e6fSCosmin Samoila 	for (i = 0; i < MICFIL_FIFO_NUM; i++) {
51717f2142bSSascha Hauer 		if (fifo_stat_reg & MICFIL_FIFO_STAT_FIFOX_OVER(i))
51847a70e6fSCosmin Samoila 			dev_dbg(&pdev->dev,
51947a70e6fSCosmin Samoila 				"FIFO Overflow Exception flag for channel %d\n",
52047a70e6fSCosmin Samoila 				i);
52147a70e6fSCosmin Samoila 
52217f2142bSSascha Hauer 		if (fifo_stat_reg & MICFIL_FIFO_STAT_FIFOX_UNDER(i))
52347a70e6fSCosmin Samoila 			dev_dbg(&pdev->dev,
52447a70e6fSCosmin Samoila 				"FIFO Underflow Exception flag for channel %d\n",
52547a70e6fSCosmin Samoila 				i);
52647a70e6fSCosmin Samoila 	}
52747a70e6fSCosmin Samoila 
52847a70e6fSCosmin Samoila 	return IRQ_HANDLED;
52947a70e6fSCosmin Samoila }
53047a70e6fSCosmin Samoila 
53147a70e6fSCosmin Samoila static irqreturn_t micfil_err_isr(int irq, void *devid)
53247a70e6fSCosmin Samoila {
53347a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = (struct fsl_micfil *)devid;
53447a70e6fSCosmin Samoila 	struct platform_device *pdev = micfil->pdev;
53547a70e6fSCosmin Samoila 	u32 stat_reg;
53647a70e6fSCosmin Samoila 
53747a70e6fSCosmin Samoila 	regmap_read(micfil->regmap, REG_MICFIL_STAT, &stat_reg);
53847a70e6fSCosmin Samoila 
539bd2cffd1SSascha Hauer 	if (stat_reg & MICFIL_STAT_BSY_FIL)
54047a70e6fSCosmin Samoila 		dev_dbg(&pdev->dev, "isr: Decimation Filter is running\n");
54147a70e6fSCosmin Samoila 
542bd2cffd1SSascha Hauer 	if (stat_reg & MICFIL_STAT_FIR_RDY)
54347a70e6fSCosmin Samoila 		dev_dbg(&pdev->dev, "isr: FIR Filter Data ready\n");
54447a70e6fSCosmin Samoila 
545bd2cffd1SSascha Hauer 	if (stat_reg & MICFIL_STAT_LOWFREQF) {
54647a70e6fSCosmin Samoila 		dev_dbg(&pdev->dev, "isr: ipg_clk_app is too low\n");
54747a70e6fSCosmin Samoila 		regmap_write_bits(micfil->regmap, REG_MICFIL_STAT,
548bd2cffd1SSascha Hauer 				  MICFIL_STAT_LOWFREQF, 1);
54947a70e6fSCosmin Samoila 	}
55047a70e6fSCosmin Samoila 
55147a70e6fSCosmin Samoila 	return IRQ_HANDLED;
55247a70e6fSCosmin Samoila }
55347a70e6fSCosmin Samoila 
55447a70e6fSCosmin Samoila static int fsl_micfil_probe(struct platform_device *pdev)
55547a70e6fSCosmin Samoila {
55647a70e6fSCosmin Samoila 	struct device_node *np = pdev->dev.of_node;
55747a70e6fSCosmin Samoila 	struct fsl_micfil *micfil;
55847a70e6fSCosmin Samoila 	struct resource *res;
55947a70e6fSCosmin Samoila 	void __iomem *regs;
56047a70e6fSCosmin Samoila 	int ret, i;
56147a70e6fSCosmin Samoila 	unsigned long irqflag = 0;
56247a70e6fSCosmin Samoila 
56347a70e6fSCosmin Samoila 	micfil = devm_kzalloc(&pdev->dev, sizeof(*micfil), GFP_KERNEL);
56447a70e6fSCosmin Samoila 	if (!micfil)
56547a70e6fSCosmin Samoila 		return -ENOMEM;
56647a70e6fSCosmin Samoila 
56747a70e6fSCosmin Samoila 	micfil->pdev = pdev;
56847a70e6fSCosmin Samoila 	strncpy(micfil->name, np->name, sizeof(micfil->name) - 1);
56947a70e6fSCosmin Samoila 
570d7388718SFabio Estevam 	micfil->soc = of_device_get_match_data(&pdev->dev);
57147a70e6fSCosmin Samoila 
57247a70e6fSCosmin Samoila 	/* ipg_clk is used to control the registers
57347a70e6fSCosmin Samoila 	 * ipg_clk_app is used to operate the filter
57447a70e6fSCosmin Samoila 	 */
57547a70e6fSCosmin Samoila 	micfil->mclk = devm_clk_get(&pdev->dev, "ipg_clk_app");
57647a70e6fSCosmin Samoila 	if (IS_ERR(micfil->mclk)) {
57747a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "failed to get core clock: %ld\n",
57847a70e6fSCosmin Samoila 			PTR_ERR(micfil->mclk));
57947a70e6fSCosmin Samoila 		return PTR_ERR(micfil->mclk);
58047a70e6fSCosmin Samoila 	}
58147a70e6fSCosmin Samoila 
582b5cf28f7SShengjiu Wang 	micfil->busclk = devm_clk_get(&pdev->dev, "ipg_clk");
583b5cf28f7SShengjiu Wang 	if (IS_ERR(micfil->busclk)) {
584b5cf28f7SShengjiu Wang 		dev_err(&pdev->dev, "failed to get ipg clock: %ld\n",
585b5cf28f7SShengjiu Wang 			PTR_ERR(micfil->busclk));
586b5cf28f7SShengjiu Wang 		return PTR_ERR(micfil->busclk);
587b5cf28f7SShengjiu Wang 	}
588b5cf28f7SShengjiu Wang 
58947a70e6fSCosmin Samoila 	/* init regmap */
590d9bf1e79SYang Yingliang 	regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
59147a70e6fSCosmin Samoila 	if (IS_ERR(regs))
59247a70e6fSCosmin Samoila 		return PTR_ERR(regs);
59347a70e6fSCosmin Samoila 
594b5cf28f7SShengjiu Wang 	micfil->regmap = devm_regmap_init_mmio(&pdev->dev,
59547a70e6fSCosmin Samoila 					       regs,
59647a70e6fSCosmin Samoila 					       &fsl_micfil_regmap_config);
59747a70e6fSCosmin Samoila 	if (IS_ERR(micfil->regmap)) {
59847a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "failed to init MICFIL regmap: %ld\n",
59947a70e6fSCosmin Samoila 			PTR_ERR(micfil->regmap));
60047a70e6fSCosmin Samoila 		return PTR_ERR(micfil->regmap);
60147a70e6fSCosmin Samoila 	}
60247a70e6fSCosmin Samoila 
60347a70e6fSCosmin Samoila 	/* dataline mask for RX */
60447a70e6fSCosmin Samoila 	ret = of_property_read_u32_index(np,
60547a70e6fSCosmin Samoila 					 "fsl,dataline",
60647a70e6fSCosmin Samoila 					 0,
60747a70e6fSCosmin Samoila 					 &micfil->dataline);
60847a70e6fSCosmin Samoila 	if (ret)
60947a70e6fSCosmin Samoila 		micfil->dataline = 1;
61047a70e6fSCosmin Samoila 
61147a70e6fSCosmin Samoila 	if (micfil->dataline & ~micfil->soc->dataline) {
61247a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "dataline setting error, Mask is 0x%X\n",
61347a70e6fSCosmin Samoila 			micfil->soc->dataline);
61447a70e6fSCosmin Samoila 		return -EINVAL;
61547a70e6fSCosmin Samoila 	}
61647a70e6fSCosmin Samoila 
61747a70e6fSCosmin Samoila 	/* get IRQs */
61847a70e6fSCosmin Samoila 	for (i = 0; i < MICFIL_IRQ_LINES; i++) {
61947a70e6fSCosmin Samoila 		micfil->irq[i] = platform_get_irq(pdev, i);
62047a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "GET IRQ: %d\n", micfil->irq[i]);
62183b35f45STang Bin 		if (micfil->irq[i] < 0)
62247a70e6fSCosmin Samoila 			return micfil->irq[i];
62347a70e6fSCosmin Samoila 	}
62447a70e6fSCosmin Samoila 
62547a70e6fSCosmin Samoila 	if (of_property_read_bool(np, "fsl,shared-interrupt"))
62647a70e6fSCosmin Samoila 		irqflag = IRQF_SHARED;
62747a70e6fSCosmin Samoila 
628a62ed960SFabio Estevam 	/* Digital Microphone interface interrupt */
62947a70e6fSCosmin Samoila 	ret = devm_request_irq(&pdev->dev, micfil->irq[0],
63047a70e6fSCosmin Samoila 			       micfil_isr, irqflag,
63147a70e6fSCosmin Samoila 			       micfil->name, micfil);
63247a70e6fSCosmin Samoila 	if (ret) {
63347a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "failed to claim mic interface irq %u\n",
63447a70e6fSCosmin Samoila 			micfil->irq[0]);
63547a70e6fSCosmin Samoila 		return ret;
63647a70e6fSCosmin Samoila 	}
63747a70e6fSCosmin Samoila 
638a62ed960SFabio Estevam 	/* Digital Microphone interface error interrupt */
63947a70e6fSCosmin Samoila 	ret = devm_request_irq(&pdev->dev, micfil->irq[1],
64047a70e6fSCosmin Samoila 			       micfil_err_isr, irqflag,
64147a70e6fSCosmin Samoila 			       micfil->name, micfil);
64247a70e6fSCosmin Samoila 	if (ret) {
64347a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "failed to claim mic interface error irq %u\n",
64447a70e6fSCosmin Samoila 			micfil->irq[1]);
64547a70e6fSCosmin Samoila 		return ret;
64647a70e6fSCosmin Samoila 	}
64747a70e6fSCosmin Samoila 
64847a70e6fSCosmin Samoila 	micfil->dma_params_rx.chan_name = "rx";
64947a70e6fSCosmin Samoila 	micfil->dma_params_rx.addr = res->start + REG_MICFIL_DATACH0;
65047a70e6fSCosmin Samoila 	micfil->dma_params_rx.maxburst = MICFIL_DMA_MAXBURST_RX;
65147a70e6fSCosmin Samoila 
65247a70e6fSCosmin Samoila 	platform_set_drvdata(pdev, micfil);
65347a70e6fSCosmin Samoila 
65447a70e6fSCosmin Samoila 	pm_runtime_enable(&pdev->dev);
655b5cf28f7SShengjiu Wang 	regcache_cache_only(micfil->regmap, true);
65647a70e6fSCosmin Samoila 
6570adf2920SShengjiu Wang 	/*
6580adf2920SShengjiu Wang 	 * Register platform component before registering cpu dai for there
6590adf2920SShengjiu Wang 	 * is not defer probe for platform component in snd_soc_add_pcm_runtime().
6600adf2920SShengjiu Wang 	 */
6610adf2920SShengjiu Wang 	ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
6620adf2920SShengjiu Wang 	if (ret) {
6630adf2920SShengjiu Wang 		dev_err(&pdev->dev, "failed to pcm register\n");
6640adf2920SShengjiu Wang 		return ret;
6650adf2920SShengjiu Wang 	}
6660adf2920SShengjiu Wang 
66747a70e6fSCosmin Samoila 	ret = devm_snd_soc_register_component(&pdev->dev, &fsl_micfil_component,
66847a70e6fSCosmin Samoila 					      &fsl_micfil_dai, 1);
66947a70e6fSCosmin Samoila 	if (ret) {
67047a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "failed to register component %s\n",
67147a70e6fSCosmin Samoila 			fsl_micfil_component.name);
67247a70e6fSCosmin Samoila 	}
67347a70e6fSCosmin Samoila 
67447a70e6fSCosmin Samoila 	return ret;
67547a70e6fSCosmin Samoila }
67647a70e6fSCosmin Samoila 
67747a70e6fSCosmin Samoila static int __maybe_unused fsl_micfil_runtime_suspend(struct device *dev)
67847a70e6fSCosmin Samoila {
67947a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = dev_get_drvdata(dev);
68047a70e6fSCosmin Samoila 
68147a70e6fSCosmin Samoila 	regcache_cache_only(micfil->regmap, true);
68247a70e6fSCosmin Samoila 
68347a70e6fSCosmin Samoila 	clk_disable_unprepare(micfil->mclk);
684b5cf28f7SShengjiu Wang 	clk_disable_unprepare(micfil->busclk);
68547a70e6fSCosmin Samoila 
68647a70e6fSCosmin Samoila 	return 0;
68747a70e6fSCosmin Samoila }
68847a70e6fSCosmin Samoila 
68947a70e6fSCosmin Samoila static int __maybe_unused fsl_micfil_runtime_resume(struct device *dev)
69047a70e6fSCosmin Samoila {
69147a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = dev_get_drvdata(dev);
69247a70e6fSCosmin Samoila 	int ret;
69347a70e6fSCosmin Samoila 
694b5cf28f7SShengjiu Wang 	ret = clk_prepare_enable(micfil->busclk);
69547a70e6fSCosmin Samoila 	if (ret < 0)
69647a70e6fSCosmin Samoila 		return ret;
69747a70e6fSCosmin Samoila 
698b5cf28f7SShengjiu Wang 	ret = clk_prepare_enable(micfil->mclk);
699b5cf28f7SShengjiu Wang 	if (ret < 0) {
700b5cf28f7SShengjiu Wang 		clk_disable_unprepare(micfil->busclk);
701b5cf28f7SShengjiu Wang 		return ret;
702b5cf28f7SShengjiu Wang 	}
703b5cf28f7SShengjiu Wang 
70447a70e6fSCosmin Samoila 	regcache_cache_only(micfil->regmap, false);
70547a70e6fSCosmin Samoila 	regcache_mark_dirty(micfil->regmap);
70647a70e6fSCosmin Samoila 	regcache_sync(micfil->regmap);
70747a70e6fSCosmin Samoila 
70847a70e6fSCosmin Samoila 	return 0;
70947a70e6fSCosmin Samoila }
71047a70e6fSCosmin Samoila 
71147a70e6fSCosmin Samoila static int __maybe_unused fsl_micfil_suspend(struct device *dev)
71247a70e6fSCosmin Samoila {
71347a70e6fSCosmin Samoila 	pm_runtime_force_suspend(dev);
71447a70e6fSCosmin Samoila 
71547a70e6fSCosmin Samoila 	return 0;
71647a70e6fSCosmin Samoila }
71747a70e6fSCosmin Samoila 
71847a70e6fSCosmin Samoila static int __maybe_unused fsl_micfil_resume(struct device *dev)
71947a70e6fSCosmin Samoila {
72047a70e6fSCosmin Samoila 	pm_runtime_force_resume(dev);
72147a70e6fSCosmin Samoila 
72247a70e6fSCosmin Samoila 	return 0;
72347a70e6fSCosmin Samoila }
72447a70e6fSCosmin Samoila 
72547a70e6fSCosmin Samoila static const struct dev_pm_ops fsl_micfil_pm_ops = {
72647a70e6fSCosmin Samoila 	SET_RUNTIME_PM_OPS(fsl_micfil_runtime_suspend,
72747a70e6fSCosmin Samoila 			   fsl_micfil_runtime_resume,
72847a70e6fSCosmin Samoila 			   NULL)
72947a70e6fSCosmin Samoila 	SET_SYSTEM_SLEEP_PM_OPS(fsl_micfil_suspend,
73047a70e6fSCosmin Samoila 				fsl_micfil_resume)
73147a70e6fSCosmin Samoila };
73247a70e6fSCosmin Samoila 
73347a70e6fSCosmin Samoila static struct platform_driver fsl_micfil_driver = {
73447a70e6fSCosmin Samoila 	.probe = fsl_micfil_probe,
73547a70e6fSCosmin Samoila 	.driver = {
73647a70e6fSCosmin Samoila 		.name = "fsl-micfil-dai",
73747a70e6fSCosmin Samoila 		.pm = &fsl_micfil_pm_ops,
73847a70e6fSCosmin Samoila 		.of_match_table = fsl_micfil_dt_ids,
73947a70e6fSCosmin Samoila 	},
74047a70e6fSCosmin Samoila };
74147a70e6fSCosmin Samoila module_platform_driver(fsl_micfil_driver);
74247a70e6fSCosmin Samoila 
74347a70e6fSCosmin Samoila MODULE_AUTHOR("Cosmin-Gabriel Samoila <cosmin.samoila@nxp.com>");
74447a70e6fSCosmin Samoila MODULE_DESCRIPTION("NXP PDM Microphone Interface (MICFIL) driver");
74547a70e6fSCosmin Samoila MODULE_LICENSE("GPL v2");
746