xref: /openbmc/linux/sound/soc/fsl/fsl_micfil.c (revision 2c602c7ef9ef08835f2e3e0c438d10b7142d6959)
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>
1947a70e6fSCosmin Samoila #include <sound/dmaengine_pcm.h>
2047a70e6fSCosmin Samoila #include <sound/pcm.h>
2147a70e6fSCosmin Samoila #include <sound/soc.h>
2247a70e6fSCosmin Samoila #include <sound/tlv.h>
2347a70e6fSCosmin Samoila #include <sound/core.h>
2447a70e6fSCosmin Samoila 
2547a70e6fSCosmin Samoila #include "fsl_micfil.h"
2647a70e6fSCosmin Samoila #include "imx-pcm.h"
2747a70e6fSCosmin Samoila 
2847a70e6fSCosmin Samoila #define FSL_MICFIL_RATES		SNDRV_PCM_RATE_8000_48000
2947a70e6fSCosmin Samoila #define FSL_MICFIL_FORMATS		(SNDRV_PCM_FMTBIT_S16_LE)
3047a70e6fSCosmin Samoila 
3147a70e6fSCosmin Samoila struct fsl_micfil {
3247a70e6fSCosmin Samoila 	struct platform_device *pdev;
3347a70e6fSCosmin Samoila 	struct regmap *regmap;
3447a70e6fSCosmin Samoila 	const struct fsl_micfil_soc_data *soc;
35b5cf28f7SShengjiu Wang 	struct clk *busclk;
3647a70e6fSCosmin Samoila 	struct clk *mclk;
3747a70e6fSCosmin Samoila 	struct snd_dmaengine_dai_dma_data dma_params_rx;
3847a70e6fSCosmin Samoila 	unsigned int dataline;
3947a70e6fSCosmin Samoila 	char name[32];
4047a70e6fSCosmin Samoila 	int irq[MICFIL_IRQ_LINES];
4147a70e6fSCosmin Samoila 	unsigned int mclk_streams;
4247a70e6fSCosmin Samoila 	int quality;	/*QUALITY 2-0 bits */
4347a70e6fSCosmin Samoila 	bool slave_mode;
4447a70e6fSCosmin Samoila 	int channel_gain[8];
4547a70e6fSCosmin Samoila };
4647a70e6fSCosmin Samoila 
4747a70e6fSCosmin Samoila struct fsl_micfil_soc_data {
4847a70e6fSCosmin Samoila 	unsigned int fifos;
4947a70e6fSCosmin Samoila 	unsigned int fifo_depth;
5047a70e6fSCosmin Samoila 	unsigned int dataline;
5147a70e6fSCosmin Samoila 	bool imx;
5247a70e6fSCosmin Samoila };
5347a70e6fSCosmin Samoila 
5447a70e6fSCosmin Samoila static struct fsl_micfil_soc_data fsl_micfil_imx8mm = {
5547a70e6fSCosmin Samoila 	.imx = true,
5647a70e6fSCosmin Samoila 	.fifos = 8,
5747a70e6fSCosmin Samoila 	.fifo_depth = 8,
5847a70e6fSCosmin Samoila 	.dataline =  0xf,
5947a70e6fSCosmin Samoila };
6047a70e6fSCosmin Samoila 
6147a70e6fSCosmin Samoila static const struct of_device_id fsl_micfil_dt_ids[] = {
6247a70e6fSCosmin Samoila 	{ .compatible = "fsl,imx8mm-micfil", .data = &fsl_micfil_imx8mm },
6347a70e6fSCosmin Samoila 	{}
6447a70e6fSCosmin Samoila };
6547a70e6fSCosmin Samoila MODULE_DEVICE_TABLE(of, fsl_micfil_dt_ids);
6647a70e6fSCosmin Samoila 
6747a70e6fSCosmin Samoila /* Table 5. Quality Modes
6847a70e6fSCosmin Samoila  * Medium	0 0 0
6947a70e6fSCosmin Samoila  * High		0 0 1
7047a70e6fSCosmin Samoila  * Very Low 2	1 0 0
7147a70e6fSCosmin Samoila  * Very Low 1	1 0 1
7247a70e6fSCosmin Samoila  * Very Low 0	1 1 0
7347a70e6fSCosmin Samoila  * Low		1 1 1
7447a70e6fSCosmin Samoila  */
7547a70e6fSCosmin Samoila static const char * const micfil_quality_select_texts[] = {
7647a70e6fSCosmin Samoila 	"Medium", "High",
7747a70e6fSCosmin Samoila 	"N/A", "N/A",
7847a70e6fSCosmin Samoila 	"VLow2", "VLow1",
7947a70e6fSCosmin Samoila 	"VLow0", "Low",
8047a70e6fSCosmin Samoila };
8147a70e6fSCosmin Samoila 
8247a70e6fSCosmin Samoila static const struct soc_enum fsl_micfil_quality_enum =
8347a70e6fSCosmin Samoila 	SOC_ENUM_SINGLE(REG_MICFIL_CTRL2,
8447a70e6fSCosmin Samoila 			MICFIL_CTRL2_QSEL_SHIFT,
8547a70e6fSCosmin Samoila 			ARRAY_SIZE(micfil_quality_select_texts),
8647a70e6fSCosmin Samoila 			micfil_quality_select_texts);
8747a70e6fSCosmin Samoila 
8847a70e6fSCosmin Samoila static DECLARE_TLV_DB_SCALE(gain_tlv, 0, 100, 0);
8947a70e6fSCosmin Samoila 
9047a70e6fSCosmin Samoila static const struct snd_kcontrol_new fsl_micfil_snd_controls[] = {
9147a70e6fSCosmin Samoila 	SOC_SINGLE_SX_TLV("CH0 Volume", REG_MICFIL_OUT_CTRL,
9247a70e6fSCosmin Samoila 			  MICFIL_OUTGAIN_CHX_SHIFT(0), 0xF, 0x7, gain_tlv),
9347a70e6fSCosmin Samoila 	SOC_SINGLE_SX_TLV("CH1 Volume", REG_MICFIL_OUT_CTRL,
9447a70e6fSCosmin Samoila 			  MICFIL_OUTGAIN_CHX_SHIFT(1), 0xF, 0x7, gain_tlv),
9547a70e6fSCosmin Samoila 	SOC_SINGLE_SX_TLV("CH2 Volume", REG_MICFIL_OUT_CTRL,
9647a70e6fSCosmin Samoila 			  MICFIL_OUTGAIN_CHX_SHIFT(2), 0xF, 0x7, gain_tlv),
9747a70e6fSCosmin Samoila 	SOC_SINGLE_SX_TLV("CH3 Volume", REG_MICFIL_OUT_CTRL,
9847a70e6fSCosmin Samoila 			  MICFIL_OUTGAIN_CHX_SHIFT(3), 0xF, 0x7, gain_tlv),
9947a70e6fSCosmin Samoila 	SOC_SINGLE_SX_TLV("CH4 Volume", REG_MICFIL_OUT_CTRL,
10047a70e6fSCosmin Samoila 			  MICFIL_OUTGAIN_CHX_SHIFT(4), 0xF, 0x7, gain_tlv),
10147a70e6fSCosmin Samoila 	SOC_SINGLE_SX_TLV("CH5 Volume", REG_MICFIL_OUT_CTRL,
10247a70e6fSCosmin Samoila 			  MICFIL_OUTGAIN_CHX_SHIFT(5), 0xF, 0x7, gain_tlv),
10347a70e6fSCosmin Samoila 	SOC_SINGLE_SX_TLV("CH6 Volume", REG_MICFIL_OUT_CTRL,
10447a70e6fSCosmin Samoila 			  MICFIL_OUTGAIN_CHX_SHIFT(6), 0xF, 0x7, gain_tlv),
10547a70e6fSCosmin Samoila 	SOC_SINGLE_SX_TLV("CH7 Volume", REG_MICFIL_OUT_CTRL,
10647a70e6fSCosmin Samoila 			  MICFIL_OUTGAIN_CHX_SHIFT(7), 0xF, 0x7, gain_tlv),
10747a70e6fSCosmin Samoila 	SOC_ENUM_EXT("MICFIL Quality Select",
10847a70e6fSCosmin Samoila 		     fsl_micfil_quality_enum,
10947a70e6fSCosmin Samoila 		     snd_soc_get_enum_double, snd_soc_put_enum_double),
11047a70e6fSCosmin Samoila };
11147a70e6fSCosmin Samoila 
11247a70e6fSCosmin Samoila static inline int get_pdm_clk(struct fsl_micfil *micfil,
11347a70e6fSCosmin Samoila 			      unsigned int rate)
11447a70e6fSCosmin Samoila {
11547a70e6fSCosmin Samoila 	u32 ctrl2_reg;
11647a70e6fSCosmin Samoila 	int qsel, osr;
11747a70e6fSCosmin Samoila 	int bclk;
11847a70e6fSCosmin Samoila 
11947a70e6fSCosmin Samoila 	regmap_read(micfil->regmap, REG_MICFIL_CTRL2, &ctrl2_reg);
12017f2142bSSascha Hauer 	osr = 16 - FIELD_GET(MICFIL_CTRL2_CICOSR, ctrl2_reg);
12117f2142bSSascha Hauer 	qsel = FIELD_GET(MICFIL_CTRL2_QSEL, ctrl2_reg);
12247a70e6fSCosmin Samoila 
12347a70e6fSCosmin Samoila 	switch (qsel) {
12417f2142bSSascha Hauer 	case MICFIL_QSEL_HIGH_QUALITY:
12547a70e6fSCosmin Samoila 		bclk = rate * 8 * osr / 2; /* kfactor = 0.5 */
12647a70e6fSCosmin Samoila 		break;
12717f2142bSSascha Hauer 	case MICFIL_QSEL_MEDIUM_QUALITY:
12817f2142bSSascha Hauer 	case MICFIL_QSEL_VLOW0_QUALITY:
12947a70e6fSCosmin Samoila 		bclk = rate * 4 * osr * 1; /* kfactor = 1 */
13047a70e6fSCosmin Samoila 		break;
13117f2142bSSascha Hauer 	case MICFIL_QSEL_LOW_QUALITY:
13217f2142bSSascha Hauer 	case MICFIL_QSEL_VLOW1_QUALITY:
13347a70e6fSCosmin Samoila 		bclk = rate * 2 * osr * 2; /* kfactor = 2 */
13447a70e6fSCosmin Samoila 		break;
13517f2142bSSascha Hauer 	case MICFIL_QSEL_VLOW2_QUALITY:
13647a70e6fSCosmin Samoila 		bclk = rate * osr * 4; /* kfactor = 4 */
13747a70e6fSCosmin Samoila 		break;
13847a70e6fSCosmin Samoila 	default:
13947a70e6fSCosmin Samoila 		dev_err(&micfil->pdev->dev,
14047a70e6fSCosmin Samoila 			"Please make sure you select a valid quality.\n");
14147a70e6fSCosmin Samoila 		bclk = -1;
14247a70e6fSCosmin Samoila 		break;
14347a70e6fSCosmin Samoila 	}
14447a70e6fSCosmin Samoila 
14547a70e6fSCosmin Samoila 	return bclk;
14647a70e6fSCosmin Samoila }
14747a70e6fSCosmin Samoila 
14847a70e6fSCosmin Samoila static inline int get_clk_div(struct fsl_micfil *micfil,
14947a70e6fSCosmin Samoila 			      unsigned int rate)
15047a70e6fSCosmin Samoila {
15147a70e6fSCosmin Samoila 	long mclk_rate;
15247a70e6fSCosmin Samoila 	int clk_div;
15347a70e6fSCosmin Samoila 
15447a70e6fSCosmin Samoila 	mclk_rate = clk_get_rate(micfil->mclk);
15547a70e6fSCosmin Samoila 
15647a70e6fSCosmin Samoila 	clk_div = mclk_rate / (get_pdm_clk(micfil, rate) * 2);
15747a70e6fSCosmin Samoila 
15847a70e6fSCosmin Samoila 	return clk_div;
15947a70e6fSCosmin Samoila }
16047a70e6fSCosmin Samoila 
16147a70e6fSCosmin Samoila /* The SRES is a self-negated bit which provides the CPU with the
16247a70e6fSCosmin Samoila  * capability to initialize the PDM Interface module through the
16347a70e6fSCosmin Samoila  * slave-bus interface. This bit always reads as zero, and this
16447a70e6fSCosmin Samoila  * bit is only effective when MDIS is cleared
16547a70e6fSCosmin Samoila  */
16647a70e6fSCosmin Samoila static int fsl_micfil_reset(struct device *dev)
16747a70e6fSCosmin Samoila {
16847a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = dev_get_drvdata(dev);
16947a70e6fSCosmin Samoila 	int ret;
17047a70e6fSCosmin Samoila 
171d46c2127SSascha Hauer 	ret = regmap_clear_bits(micfil->regmap, REG_MICFIL_CTRL1,
172d46c2127SSascha Hauer 				MICFIL_CTRL1_MDIS);
173*2c602c7eSSascha Hauer 	if (ret)
17447a70e6fSCosmin Samoila 		return ret;
17547a70e6fSCosmin Samoila 
176d46c2127SSascha Hauer 	ret = regmap_set_bits(micfil->regmap, REG_MICFIL_CTRL1,
17747a70e6fSCosmin Samoila 			      MICFIL_CTRL1_SRES);
178*2c602c7eSSascha Hauer 	if (ret)
17947a70e6fSCosmin Samoila 		return ret;
18047a70e6fSCosmin Samoila 
18147a70e6fSCosmin Samoila 	return 0;
18247a70e6fSCosmin Samoila }
18347a70e6fSCosmin Samoila 
18447a70e6fSCosmin Samoila static int fsl_micfil_set_mclk_rate(struct fsl_micfil *micfil,
18547a70e6fSCosmin Samoila 				    unsigned int freq)
18647a70e6fSCosmin Samoila {
18747a70e6fSCosmin Samoila 	struct device *dev = &micfil->pdev->dev;
18847a70e6fSCosmin Samoila 	int ret;
18947a70e6fSCosmin Samoila 
19047a70e6fSCosmin Samoila 	clk_disable_unprepare(micfil->mclk);
19147a70e6fSCosmin Samoila 
19247a70e6fSCosmin Samoila 	ret = clk_set_rate(micfil->mclk, freq * 1024);
19347a70e6fSCosmin Samoila 	if (ret)
19447a70e6fSCosmin Samoila 		dev_warn(dev, "failed to set rate (%u): %d\n",
19547a70e6fSCosmin Samoila 			 freq * 1024, ret);
19647a70e6fSCosmin Samoila 
19747a70e6fSCosmin Samoila 	clk_prepare_enable(micfil->mclk);
19847a70e6fSCosmin Samoila 
19947a70e6fSCosmin Samoila 	return ret;
20047a70e6fSCosmin Samoila }
20147a70e6fSCosmin Samoila 
20247a70e6fSCosmin Samoila static int fsl_micfil_startup(struct snd_pcm_substream *substream,
20347a70e6fSCosmin Samoila 			      struct snd_soc_dai *dai)
20447a70e6fSCosmin Samoila {
20547a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = snd_soc_dai_get_drvdata(dai);
20647a70e6fSCosmin Samoila 
20747a70e6fSCosmin Samoila 	if (!micfil) {
20811106cb3STang Bin 		dev_err(dai->dev, "micfil dai priv_data not set\n");
20947a70e6fSCosmin Samoila 		return -EINVAL;
21047a70e6fSCosmin Samoila 	}
21147a70e6fSCosmin Samoila 
21247a70e6fSCosmin Samoila 	return 0;
21347a70e6fSCosmin Samoila }
21447a70e6fSCosmin Samoila 
21547a70e6fSCosmin Samoila static int fsl_micfil_trigger(struct snd_pcm_substream *substream, int cmd,
21647a70e6fSCosmin Samoila 			      struct snd_soc_dai *dai)
21747a70e6fSCosmin Samoila {
21847a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = snd_soc_dai_get_drvdata(dai);
21947a70e6fSCosmin Samoila 	struct device *dev = &micfil->pdev->dev;
22047a70e6fSCosmin Samoila 	int ret;
22147a70e6fSCosmin Samoila 
22247a70e6fSCosmin Samoila 	switch (cmd) {
22347a70e6fSCosmin Samoila 	case SNDRV_PCM_TRIGGER_START:
22447a70e6fSCosmin Samoila 	case SNDRV_PCM_TRIGGER_RESUME:
22547a70e6fSCosmin Samoila 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
22647a70e6fSCosmin Samoila 		ret = fsl_micfil_reset(dev);
22747a70e6fSCosmin Samoila 		if (ret) {
22847a70e6fSCosmin Samoila 			dev_err(dev, "failed to soft reset\n");
22947a70e6fSCosmin Samoila 			return ret;
23047a70e6fSCosmin Samoila 		}
23147a70e6fSCosmin Samoila 
23247a70e6fSCosmin Samoila 		/* DMA Interrupt Selection - DISEL bits
23347a70e6fSCosmin Samoila 		 * 00 - DMA and IRQ disabled
23447a70e6fSCosmin Samoila 		 * 01 - DMA req enabled
23547a70e6fSCosmin Samoila 		 * 10 - IRQ enabled
23647a70e6fSCosmin Samoila 		 * 11 - reserved
23747a70e6fSCosmin Samoila 		 */
23847a70e6fSCosmin Samoila 		ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL1,
23917f2142bSSascha Hauer 				MICFIL_CTRL1_DISEL,
24017f2142bSSascha Hauer 				FIELD_PREP(MICFIL_CTRL1_DISEL, MICFIL_CTRL1_DISEL_DMA));
241*2c602c7eSSascha Hauer 		if (ret)
24247a70e6fSCosmin Samoila 			return ret;
24347a70e6fSCosmin Samoila 
24447a70e6fSCosmin Samoila 		/* Enable the module */
245d46c2127SSascha Hauer 		ret = regmap_set_bits(micfil->regmap, REG_MICFIL_CTRL1,
24647a70e6fSCosmin Samoila 				      MICFIL_CTRL1_PDMIEN);
247*2c602c7eSSascha Hauer 		if (ret)
24847a70e6fSCosmin Samoila 			return ret;
24947a70e6fSCosmin Samoila 
25047a70e6fSCosmin Samoila 		break;
25147a70e6fSCosmin Samoila 	case SNDRV_PCM_TRIGGER_STOP:
25247a70e6fSCosmin Samoila 	case SNDRV_PCM_TRIGGER_SUSPEND:
25347a70e6fSCosmin Samoila 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
25447a70e6fSCosmin Samoila 		/* Disable the module */
255d46c2127SSascha Hauer 		ret = regmap_clear_bits(micfil->regmap, REG_MICFIL_CTRL1,
256d46c2127SSascha Hauer 					MICFIL_CTRL1_PDMIEN);
257*2c602c7eSSascha Hauer 		if (ret)
25847a70e6fSCosmin Samoila 			return ret;
25947a70e6fSCosmin Samoila 
26047a70e6fSCosmin Samoila 		ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL1,
26117f2142bSSascha Hauer 				MICFIL_CTRL1_DISEL,
26217f2142bSSascha Hauer 				FIELD_PREP(MICFIL_CTRL1_DISEL, MICFIL_CTRL1_DISEL_DISABLE));
263*2c602c7eSSascha Hauer 		if (ret)
26447a70e6fSCosmin Samoila 			return ret;
26547a70e6fSCosmin Samoila 		break;
26647a70e6fSCosmin Samoila 	default:
26747a70e6fSCosmin Samoila 		return -EINVAL;
26847a70e6fSCosmin Samoila 	}
26947a70e6fSCosmin Samoila 	return 0;
27047a70e6fSCosmin Samoila }
27147a70e6fSCosmin Samoila 
27247a70e6fSCosmin Samoila static int fsl_set_clock_params(struct device *dev, unsigned int rate)
27347a70e6fSCosmin Samoila {
27447a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = dev_get_drvdata(dev);
27547a70e6fSCosmin Samoila 	int clk_div;
27615b5c496STang Bin 	int ret;
27747a70e6fSCosmin Samoila 
27847a70e6fSCosmin Samoila 	ret = fsl_micfil_set_mclk_rate(micfil, rate);
27947a70e6fSCosmin Samoila 	if (ret < 0)
28047a70e6fSCosmin Samoila 		dev_err(dev, "failed to set mclk[%lu] to rate %u\n",
28147a70e6fSCosmin Samoila 			clk_get_rate(micfil->mclk), rate);
28247a70e6fSCosmin Samoila 
28347a70e6fSCosmin Samoila 	/* set CICOSR */
284*2c602c7eSSascha Hauer 	ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL2,
28517f2142bSSascha Hauer 				 MICFIL_CTRL2_CICOSR,
28617f2142bSSascha Hauer 				 FIELD_PREP(MICFIL_CTRL2_CICOSR, MICFIL_CTRL2_CICOSR_DEFAULT));
28747a70e6fSCosmin Samoila 	if (ret)
288*2c602c7eSSascha Hauer 		return ret;
28947a70e6fSCosmin Samoila 
29047a70e6fSCosmin Samoila 	/* set CLK_DIV */
29147a70e6fSCosmin Samoila 	clk_div = get_clk_div(micfil, rate);
29247a70e6fSCosmin Samoila 	if (clk_div < 0)
29347a70e6fSCosmin Samoila 		ret = -EINVAL;
29447a70e6fSCosmin Samoila 
295*2c602c7eSSascha Hauer 	ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL2,
29617f2142bSSascha Hauer 				 MICFIL_CTRL2_CLKDIV,
29717f2142bSSascha Hauer 				 FIELD_PREP(MICFIL_CTRL2_CLKDIV, clk_div));
29847a70e6fSCosmin Samoila 
29947a70e6fSCosmin Samoila 	return ret;
30047a70e6fSCosmin Samoila }
30147a70e6fSCosmin Samoila 
30247a70e6fSCosmin Samoila static int fsl_micfil_hw_params(struct snd_pcm_substream *substream,
30347a70e6fSCosmin Samoila 				struct snd_pcm_hw_params *params,
30447a70e6fSCosmin Samoila 				struct snd_soc_dai *dai)
30547a70e6fSCosmin Samoila {
30647a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = snd_soc_dai_get_drvdata(dai);
30747a70e6fSCosmin Samoila 	unsigned int channels = params_channels(params);
30847a70e6fSCosmin Samoila 	unsigned int rate = params_rate(params);
30947a70e6fSCosmin Samoila 	struct device *dev = &micfil->pdev->dev;
31047a70e6fSCosmin Samoila 	int ret;
31147a70e6fSCosmin Samoila 
31247a70e6fSCosmin Samoila 	/* 1. Disable the module */
313d46c2127SSascha Hauer 	ret = regmap_clear_bits(micfil->regmap, REG_MICFIL_CTRL1,
314d46c2127SSascha Hauer 				MICFIL_CTRL1_PDMIEN);
315*2c602c7eSSascha Hauer 	if (ret)
31647a70e6fSCosmin Samoila 		return ret;
31747a70e6fSCosmin Samoila 
31847a70e6fSCosmin Samoila 	/* enable channels */
31947a70e6fSCosmin Samoila 	ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL1,
32047a70e6fSCosmin Samoila 				 0xFF, ((1 << channels) - 1));
321*2c602c7eSSascha Hauer 	if (ret)
32247a70e6fSCosmin Samoila 		return ret;
32347a70e6fSCosmin Samoila 
32447a70e6fSCosmin Samoila 	ret = fsl_set_clock_params(dev, rate);
32547a70e6fSCosmin Samoila 	if (ret < 0) {
32647a70e6fSCosmin Samoila 		dev_err(dev, "Failed to set clock parameters [%d]\n", ret);
32747a70e6fSCosmin Samoila 		return ret;
32847a70e6fSCosmin Samoila 	}
32947a70e6fSCosmin Samoila 
33047a70e6fSCosmin Samoila 	micfil->dma_params_rx.maxburst = channels * MICFIL_DMA_MAXBURST_RX;
33147a70e6fSCosmin Samoila 
33247a70e6fSCosmin Samoila 	return 0;
33347a70e6fSCosmin Samoila }
33447a70e6fSCosmin Samoila 
33538d89a56SRikard Falkeborn static const struct snd_soc_dai_ops fsl_micfil_dai_ops = {
33647a70e6fSCosmin Samoila 	.startup = fsl_micfil_startup,
33747a70e6fSCosmin Samoila 	.trigger = fsl_micfil_trigger,
33847a70e6fSCosmin Samoila 	.hw_params = fsl_micfil_hw_params,
33947a70e6fSCosmin Samoila };
34047a70e6fSCosmin Samoila 
34147a70e6fSCosmin Samoila static int fsl_micfil_dai_probe(struct snd_soc_dai *cpu_dai)
34247a70e6fSCosmin Samoila {
34347a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = dev_get_drvdata(cpu_dai->dev);
34447a70e6fSCosmin Samoila 	int ret;
34547a70e6fSCosmin Samoila 	int i;
34647a70e6fSCosmin Samoila 
34747a70e6fSCosmin Samoila 	/* set qsel to medium */
34847a70e6fSCosmin Samoila 	ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL2,
34917f2142bSSascha Hauer 			MICFIL_CTRL2_QSEL,
35017f2142bSSascha Hauer 			FIELD_PREP(MICFIL_CTRL2_QSEL, MICFIL_QSEL_MEDIUM_QUALITY));
351*2c602c7eSSascha Hauer 	if (ret)
35247a70e6fSCosmin Samoila 		return ret;
35347a70e6fSCosmin Samoila 
35447a70e6fSCosmin Samoila 	/* set default gain to max_gain */
35547a70e6fSCosmin Samoila 	regmap_write(micfil->regmap, REG_MICFIL_OUT_CTRL, 0x77777777);
35647a70e6fSCosmin Samoila 	for (i = 0; i < 8; i++)
35747a70e6fSCosmin Samoila 		micfil->channel_gain[i] = 0xF;
35847a70e6fSCosmin Samoila 
35947a70e6fSCosmin Samoila 	snd_soc_dai_init_dma_data(cpu_dai, NULL,
36047a70e6fSCosmin Samoila 				  &micfil->dma_params_rx);
36147a70e6fSCosmin Samoila 
36247a70e6fSCosmin Samoila 	/* FIFO Watermark Control - FIFOWMK*/
36347a70e6fSCosmin Samoila 	ret = regmap_update_bits(micfil->regmap, REG_MICFIL_FIFO_CTRL,
36417f2142bSSascha Hauer 			MICFIL_FIFO_CTRL_FIFOWMK,
36517f2142bSSascha Hauer 			FIELD_PREP(MICFIL_FIFO_CTRL_FIFOWMK, micfil->soc->fifo_depth - 1));
366*2c602c7eSSascha Hauer 	if (ret)
36747a70e6fSCosmin Samoila 		return ret;
36847a70e6fSCosmin Samoila 
36947a70e6fSCosmin Samoila 	return 0;
37047a70e6fSCosmin Samoila }
37147a70e6fSCosmin Samoila 
37247a70e6fSCosmin Samoila static struct snd_soc_dai_driver fsl_micfil_dai = {
37347a70e6fSCosmin Samoila 	.probe = fsl_micfil_dai_probe,
37447a70e6fSCosmin Samoila 	.capture = {
37547a70e6fSCosmin Samoila 		.stream_name = "CPU-Capture",
37647a70e6fSCosmin Samoila 		.channels_min = 1,
37747a70e6fSCosmin Samoila 		.channels_max = 8,
37847a70e6fSCosmin Samoila 		.rates = FSL_MICFIL_RATES,
37947a70e6fSCosmin Samoila 		.formats = FSL_MICFIL_FORMATS,
38047a70e6fSCosmin Samoila 	},
38147a70e6fSCosmin Samoila 	.ops = &fsl_micfil_dai_ops,
38247a70e6fSCosmin Samoila };
38347a70e6fSCosmin Samoila 
38447a70e6fSCosmin Samoila static const struct snd_soc_component_driver fsl_micfil_component = {
38547a70e6fSCosmin Samoila 	.name		= "fsl-micfil-dai",
38647a70e6fSCosmin Samoila 	.controls       = fsl_micfil_snd_controls,
38747a70e6fSCosmin Samoila 	.num_controls   = ARRAY_SIZE(fsl_micfil_snd_controls),
38847a70e6fSCosmin Samoila 
38947a70e6fSCosmin Samoila };
39047a70e6fSCosmin Samoila 
39147a70e6fSCosmin Samoila /* REGMAP */
39247a70e6fSCosmin Samoila static const struct reg_default fsl_micfil_reg_defaults[] = {
39347a70e6fSCosmin Samoila 	{REG_MICFIL_CTRL1,		0x00000000},
39447a70e6fSCosmin Samoila 	{REG_MICFIL_CTRL2,		0x00000000},
39547a70e6fSCosmin Samoila 	{REG_MICFIL_STAT,		0x00000000},
39647a70e6fSCosmin Samoila 	{REG_MICFIL_FIFO_CTRL,		0x00000007},
39747a70e6fSCosmin Samoila 	{REG_MICFIL_FIFO_STAT,		0x00000000},
39847a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH0,		0x00000000},
39947a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH1,		0x00000000},
40047a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH2,		0x00000000},
40147a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH3,		0x00000000},
40247a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH4,		0x00000000},
40347a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH5,		0x00000000},
40447a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH6,		0x00000000},
40547a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH7,		0x00000000},
40647a70e6fSCosmin Samoila 	{REG_MICFIL_DC_CTRL,		0x00000000},
40747a70e6fSCosmin Samoila 	{REG_MICFIL_OUT_CTRL,		0x00000000},
40847a70e6fSCosmin Samoila 	{REG_MICFIL_OUT_STAT,		0x00000000},
40947a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_CTRL1,		0x00000000},
41047a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_CTRL2,		0x000A0000},
41147a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_STAT,		0x00000000},
41247a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_SCONFIG,	0x00000000},
41347a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_NCONFIG,	0x80000000},
41447a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_NDATA,		0x00000000},
41547a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_ZCD,		0x00000004},
41647a70e6fSCosmin Samoila };
41747a70e6fSCosmin Samoila 
41847a70e6fSCosmin Samoila static bool fsl_micfil_readable_reg(struct device *dev, unsigned int reg)
41947a70e6fSCosmin Samoila {
42047a70e6fSCosmin Samoila 	switch (reg) {
42147a70e6fSCosmin Samoila 	case REG_MICFIL_CTRL1:
42247a70e6fSCosmin Samoila 	case REG_MICFIL_CTRL2:
42347a70e6fSCosmin Samoila 	case REG_MICFIL_STAT:
42447a70e6fSCosmin Samoila 	case REG_MICFIL_FIFO_CTRL:
42547a70e6fSCosmin Samoila 	case REG_MICFIL_FIFO_STAT:
42647a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH0:
42747a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH1:
42847a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH2:
42947a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH3:
43047a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH4:
43147a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH5:
43247a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH6:
43347a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH7:
43447a70e6fSCosmin Samoila 	case REG_MICFIL_DC_CTRL:
43547a70e6fSCosmin Samoila 	case REG_MICFIL_OUT_CTRL:
43647a70e6fSCosmin Samoila 	case REG_MICFIL_OUT_STAT:
43747a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_CTRL1:
43847a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_CTRL2:
43947a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_STAT:
44047a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_SCONFIG:
44147a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_NCONFIG:
44247a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_NDATA:
44347a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_ZCD:
44447a70e6fSCosmin Samoila 		return true;
44547a70e6fSCosmin Samoila 	default:
44647a70e6fSCosmin Samoila 		return false;
44747a70e6fSCosmin Samoila 	}
44847a70e6fSCosmin Samoila }
44947a70e6fSCosmin Samoila 
45047a70e6fSCosmin Samoila static bool fsl_micfil_writeable_reg(struct device *dev, unsigned int reg)
45147a70e6fSCosmin Samoila {
45247a70e6fSCosmin Samoila 	switch (reg) {
45347a70e6fSCosmin Samoila 	case REG_MICFIL_CTRL1:
45447a70e6fSCosmin Samoila 	case REG_MICFIL_CTRL2:
45547a70e6fSCosmin Samoila 	case REG_MICFIL_STAT:		/* Write 1 to Clear */
45647a70e6fSCosmin Samoila 	case REG_MICFIL_FIFO_CTRL:
45747a70e6fSCosmin Samoila 	case REG_MICFIL_FIFO_STAT:	/* Write 1 to Clear */
45847a70e6fSCosmin Samoila 	case REG_MICFIL_DC_CTRL:
45947a70e6fSCosmin Samoila 	case REG_MICFIL_OUT_CTRL:
46047a70e6fSCosmin Samoila 	case REG_MICFIL_OUT_STAT:	/* Write 1 to Clear */
46147a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_CTRL1:
46247a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_CTRL2:
46347a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_STAT:	/* Write 1 to Clear */
46447a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_SCONFIG:
46547a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_NCONFIG:
46647a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_ZCD:
46747a70e6fSCosmin Samoila 		return true;
46847a70e6fSCosmin Samoila 	default:
46947a70e6fSCosmin Samoila 		return false;
47047a70e6fSCosmin Samoila 	}
47147a70e6fSCosmin Samoila }
47247a70e6fSCosmin Samoila 
47347a70e6fSCosmin Samoila static bool fsl_micfil_volatile_reg(struct device *dev, unsigned int reg)
47447a70e6fSCosmin Samoila {
47547a70e6fSCosmin Samoila 	switch (reg) {
47647a70e6fSCosmin Samoila 	case REG_MICFIL_STAT:
47747a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH0:
47847a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH1:
47947a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH2:
48047a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH3:
48147a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH4:
48247a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH5:
48347a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH6:
48447a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH7:
48547a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_STAT:
48647a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_NDATA:
48747a70e6fSCosmin Samoila 		return true;
48847a70e6fSCosmin Samoila 	default:
48947a70e6fSCosmin Samoila 		return false;
49047a70e6fSCosmin Samoila 	}
49147a70e6fSCosmin Samoila }
49247a70e6fSCosmin Samoila 
49347a70e6fSCosmin Samoila static const struct regmap_config fsl_micfil_regmap_config = {
49447a70e6fSCosmin Samoila 	.reg_bits = 32,
49547a70e6fSCosmin Samoila 	.reg_stride = 4,
49647a70e6fSCosmin Samoila 	.val_bits = 32,
49747a70e6fSCosmin Samoila 
49847a70e6fSCosmin Samoila 	.max_register = REG_MICFIL_VAD0_ZCD,
49947a70e6fSCosmin Samoila 	.reg_defaults = fsl_micfil_reg_defaults,
50047a70e6fSCosmin Samoila 	.num_reg_defaults = ARRAY_SIZE(fsl_micfil_reg_defaults),
50147a70e6fSCosmin Samoila 	.readable_reg = fsl_micfil_readable_reg,
50247a70e6fSCosmin Samoila 	.volatile_reg = fsl_micfil_volatile_reg,
50347a70e6fSCosmin Samoila 	.writeable_reg = fsl_micfil_writeable_reg,
50447a70e6fSCosmin Samoila 	.cache_type = REGCACHE_RBTREE,
50547a70e6fSCosmin Samoila };
50647a70e6fSCosmin Samoila 
50747a70e6fSCosmin Samoila /* END OF REGMAP */
50847a70e6fSCosmin Samoila 
50947a70e6fSCosmin Samoila static irqreturn_t micfil_isr(int irq, void *devid)
51047a70e6fSCosmin Samoila {
51147a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = (struct fsl_micfil *)devid;
51247a70e6fSCosmin Samoila 	struct platform_device *pdev = micfil->pdev;
51347a70e6fSCosmin Samoila 	u32 stat_reg;
51447a70e6fSCosmin Samoila 	u32 fifo_stat_reg;
51547a70e6fSCosmin Samoila 	u32 ctrl1_reg;
51647a70e6fSCosmin Samoila 	bool dma_enabled;
51747a70e6fSCosmin Samoila 	int i;
51847a70e6fSCosmin Samoila 
51947a70e6fSCosmin Samoila 	regmap_read(micfil->regmap, REG_MICFIL_STAT, &stat_reg);
52047a70e6fSCosmin Samoila 	regmap_read(micfil->regmap, REG_MICFIL_CTRL1, &ctrl1_reg);
52147a70e6fSCosmin Samoila 	regmap_read(micfil->regmap, REG_MICFIL_FIFO_STAT, &fifo_stat_reg);
52247a70e6fSCosmin Samoila 
52317f2142bSSascha Hauer 	dma_enabled = FIELD_GET(MICFIL_CTRL1_DISEL, ctrl1_reg) == MICFIL_CTRL1_DISEL_DMA;
52447a70e6fSCosmin Samoila 
52547a70e6fSCosmin Samoila 	/* Channel 0-7 Output Data Flags */
52647a70e6fSCosmin Samoila 	for (i = 0; i < MICFIL_OUTPUT_CHANNELS; i++) {
52717f2142bSSascha Hauer 		if (stat_reg & MICFIL_STAT_CHXF(i))
52847a70e6fSCosmin Samoila 			dev_dbg(&pdev->dev,
52947a70e6fSCosmin Samoila 				"Data available in Data Channel %d\n", i);
53047a70e6fSCosmin Samoila 		/* if DMA is not enabled, field must be written with 1
53147a70e6fSCosmin Samoila 		 * to clear
53247a70e6fSCosmin Samoila 		 */
53347a70e6fSCosmin Samoila 		if (!dma_enabled)
53447a70e6fSCosmin Samoila 			regmap_write_bits(micfil->regmap,
53547a70e6fSCosmin Samoila 					  REG_MICFIL_STAT,
53617f2142bSSascha Hauer 					  MICFIL_STAT_CHXF(i),
53747a70e6fSCosmin Samoila 					  1);
53847a70e6fSCosmin Samoila 	}
53947a70e6fSCosmin Samoila 
54047a70e6fSCosmin Samoila 	for (i = 0; i < MICFIL_FIFO_NUM; i++) {
54117f2142bSSascha Hauer 		if (fifo_stat_reg & MICFIL_FIFO_STAT_FIFOX_OVER(i))
54247a70e6fSCosmin Samoila 			dev_dbg(&pdev->dev,
54347a70e6fSCosmin Samoila 				"FIFO Overflow Exception flag for channel %d\n",
54447a70e6fSCosmin Samoila 				i);
54547a70e6fSCosmin Samoila 
54617f2142bSSascha Hauer 		if (fifo_stat_reg & MICFIL_FIFO_STAT_FIFOX_UNDER(i))
54747a70e6fSCosmin Samoila 			dev_dbg(&pdev->dev,
54847a70e6fSCosmin Samoila 				"FIFO Underflow Exception flag for channel %d\n",
54947a70e6fSCosmin Samoila 				i);
55047a70e6fSCosmin Samoila 	}
55147a70e6fSCosmin Samoila 
55247a70e6fSCosmin Samoila 	return IRQ_HANDLED;
55347a70e6fSCosmin Samoila }
55447a70e6fSCosmin Samoila 
55547a70e6fSCosmin Samoila static irqreturn_t micfil_err_isr(int irq, void *devid)
55647a70e6fSCosmin Samoila {
55747a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = (struct fsl_micfil *)devid;
55847a70e6fSCosmin Samoila 	struct platform_device *pdev = micfil->pdev;
55947a70e6fSCosmin Samoila 	u32 stat_reg;
56047a70e6fSCosmin Samoila 
56147a70e6fSCosmin Samoila 	regmap_read(micfil->regmap, REG_MICFIL_STAT, &stat_reg);
56247a70e6fSCosmin Samoila 
563bd2cffd1SSascha Hauer 	if (stat_reg & MICFIL_STAT_BSY_FIL)
56447a70e6fSCosmin Samoila 		dev_dbg(&pdev->dev, "isr: Decimation Filter is running\n");
56547a70e6fSCosmin Samoila 
566bd2cffd1SSascha Hauer 	if (stat_reg & MICFIL_STAT_FIR_RDY)
56747a70e6fSCosmin Samoila 		dev_dbg(&pdev->dev, "isr: FIR Filter Data ready\n");
56847a70e6fSCosmin Samoila 
569bd2cffd1SSascha Hauer 	if (stat_reg & MICFIL_STAT_LOWFREQF) {
57047a70e6fSCosmin Samoila 		dev_dbg(&pdev->dev, "isr: ipg_clk_app is too low\n");
57147a70e6fSCosmin Samoila 		regmap_write_bits(micfil->regmap, REG_MICFIL_STAT,
572bd2cffd1SSascha Hauer 				  MICFIL_STAT_LOWFREQF, 1);
57347a70e6fSCosmin Samoila 	}
57447a70e6fSCosmin Samoila 
57547a70e6fSCosmin Samoila 	return IRQ_HANDLED;
57647a70e6fSCosmin Samoila }
57747a70e6fSCosmin Samoila 
57847a70e6fSCosmin Samoila static int fsl_micfil_probe(struct platform_device *pdev)
57947a70e6fSCosmin Samoila {
58047a70e6fSCosmin Samoila 	struct device_node *np = pdev->dev.of_node;
58147a70e6fSCosmin Samoila 	struct fsl_micfil *micfil;
58247a70e6fSCosmin Samoila 	struct resource *res;
58347a70e6fSCosmin Samoila 	void __iomem *regs;
58447a70e6fSCosmin Samoila 	int ret, i;
58547a70e6fSCosmin Samoila 	unsigned long irqflag = 0;
58647a70e6fSCosmin Samoila 
58747a70e6fSCosmin Samoila 	micfil = devm_kzalloc(&pdev->dev, sizeof(*micfil), GFP_KERNEL);
58847a70e6fSCosmin Samoila 	if (!micfil)
58947a70e6fSCosmin Samoila 		return -ENOMEM;
59047a70e6fSCosmin Samoila 
59147a70e6fSCosmin Samoila 	micfil->pdev = pdev;
59247a70e6fSCosmin Samoila 	strncpy(micfil->name, np->name, sizeof(micfil->name) - 1);
59347a70e6fSCosmin Samoila 
594d7388718SFabio Estevam 	micfil->soc = of_device_get_match_data(&pdev->dev);
59547a70e6fSCosmin Samoila 
59647a70e6fSCosmin Samoila 	/* ipg_clk is used to control the registers
59747a70e6fSCosmin Samoila 	 * ipg_clk_app is used to operate the filter
59847a70e6fSCosmin Samoila 	 */
59947a70e6fSCosmin Samoila 	micfil->mclk = devm_clk_get(&pdev->dev, "ipg_clk_app");
60047a70e6fSCosmin Samoila 	if (IS_ERR(micfil->mclk)) {
60147a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "failed to get core clock: %ld\n",
60247a70e6fSCosmin Samoila 			PTR_ERR(micfil->mclk));
60347a70e6fSCosmin Samoila 		return PTR_ERR(micfil->mclk);
60447a70e6fSCosmin Samoila 	}
60547a70e6fSCosmin Samoila 
606b5cf28f7SShengjiu Wang 	micfil->busclk = devm_clk_get(&pdev->dev, "ipg_clk");
607b5cf28f7SShengjiu Wang 	if (IS_ERR(micfil->busclk)) {
608b5cf28f7SShengjiu Wang 		dev_err(&pdev->dev, "failed to get ipg clock: %ld\n",
609b5cf28f7SShengjiu Wang 			PTR_ERR(micfil->busclk));
610b5cf28f7SShengjiu Wang 		return PTR_ERR(micfil->busclk);
611b5cf28f7SShengjiu Wang 	}
612b5cf28f7SShengjiu Wang 
61347a70e6fSCosmin Samoila 	/* init regmap */
614d9bf1e79SYang Yingliang 	regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
61547a70e6fSCosmin Samoila 	if (IS_ERR(regs))
61647a70e6fSCosmin Samoila 		return PTR_ERR(regs);
61747a70e6fSCosmin Samoila 
618b5cf28f7SShengjiu Wang 	micfil->regmap = devm_regmap_init_mmio(&pdev->dev,
61947a70e6fSCosmin Samoila 					       regs,
62047a70e6fSCosmin Samoila 					       &fsl_micfil_regmap_config);
62147a70e6fSCosmin Samoila 	if (IS_ERR(micfil->regmap)) {
62247a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "failed to init MICFIL regmap: %ld\n",
62347a70e6fSCosmin Samoila 			PTR_ERR(micfil->regmap));
62447a70e6fSCosmin Samoila 		return PTR_ERR(micfil->regmap);
62547a70e6fSCosmin Samoila 	}
62647a70e6fSCosmin Samoila 
62747a70e6fSCosmin Samoila 	/* dataline mask for RX */
62847a70e6fSCosmin Samoila 	ret = of_property_read_u32_index(np,
62947a70e6fSCosmin Samoila 					 "fsl,dataline",
63047a70e6fSCosmin Samoila 					 0,
63147a70e6fSCosmin Samoila 					 &micfil->dataline);
63247a70e6fSCosmin Samoila 	if (ret)
63347a70e6fSCosmin Samoila 		micfil->dataline = 1;
63447a70e6fSCosmin Samoila 
63547a70e6fSCosmin Samoila 	if (micfil->dataline & ~micfil->soc->dataline) {
63647a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "dataline setting error, Mask is 0x%X\n",
63747a70e6fSCosmin Samoila 			micfil->soc->dataline);
63847a70e6fSCosmin Samoila 		return -EINVAL;
63947a70e6fSCosmin Samoila 	}
64047a70e6fSCosmin Samoila 
64147a70e6fSCosmin Samoila 	/* get IRQs */
64247a70e6fSCosmin Samoila 	for (i = 0; i < MICFIL_IRQ_LINES; i++) {
64347a70e6fSCosmin Samoila 		micfil->irq[i] = platform_get_irq(pdev, i);
64447a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "GET IRQ: %d\n", micfil->irq[i]);
64583b35f45STang Bin 		if (micfil->irq[i] < 0)
64647a70e6fSCosmin Samoila 			return micfil->irq[i];
64747a70e6fSCosmin Samoila 	}
64847a70e6fSCosmin Samoila 
64947a70e6fSCosmin Samoila 	if (of_property_read_bool(np, "fsl,shared-interrupt"))
65047a70e6fSCosmin Samoila 		irqflag = IRQF_SHARED;
65147a70e6fSCosmin Samoila 
652a62ed960SFabio Estevam 	/* Digital Microphone interface interrupt */
65347a70e6fSCosmin Samoila 	ret = devm_request_irq(&pdev->dev, micfil->irq[0],
65447a70e6fSCosmin Samoila 			       micfil_isr, irqflag,
65547a70e6fSCosmin Samoila 			       micfil->name, micfil);
65647a70e6fSCosmin Samoila 	if (ret) {
65747a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "failed to claim mic interface irq %u\n",
65847a70e6fSCosmin Samoila 			micfil->irq[0]);
65947a70e6fSCosmin Samoila 		return ret;
66047a70e6fSCosmin Samoila 	}
66147a70e6fSCosmin Samoila 
662a62ed960SFabio Estevam 	/* Digital Microphone interface error interrupt */
66347a70e6fSCosmin Samoila 	ret = devm_request_irq(&pdev->dev, micfil->irq[1],
66447a70e6fSCosmin Samoila 			       micfil_err_isr, irqflag,
66547a70e6fSCosmin Samoila 			       micfil->name, micfil);
66647a70e6fSCosmin Samoila 	if (ret) {
66747a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "failed to claim mic interface error irq %u\n",
66847a70e6fSCosmin Samoila 			micfil->irq[1]);
66947a70e6fSCosmin Samoila 		return ret;
67047a70e6fSCosmin Samoila 	}
67147a70e6fSCosmin Samoila 
67247a70e6fSCosmin Samoila 	micfil->dma_params_rx.chan_name = "rx";
67347a70e6fSCosmin Samoila 	micfil->dma_params_rx.addr = res->start + REG_MICFIL_DATACH0;
67447a70e6fSCosmin Samoila 	micfil->dma_params_rx.maxburst = MICFIL_DMA_MAXBURST_RX;
67547a70e6fSCosmin Samoila 
67647a70e6fSCosmin Samoila 
67747a70e6fSCosmin Samoila 	platform_set_drvdata(pdev, micfil);
67847a70e6fSCosmin Samoila 
67947a70e6fSCosmin Samoila 	pm_runtime_enable(&pdev->dev);
680b5cf28f7SShengjiu Wang 	regcache_cache_only(micfil->regmap, true);
68147a70e6fSCosmin Samoila 
6820adf2920SShengjiu Wang 	/*
6830adf2920SShengjiu Wang 	 * Register platform component before registering cpu dai for there
6840adf2920SShengjiu Wang 	 * is not defer probe for platform component in snd_soc_add_pcm_runtime().
6850adf2920SShengjiu Wang 	 */
6860adf2920SShengjiu Wang 	ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
6870adf2920SShengjiu Wang 	if (ret) {
6880adf2920SShengjiu Wang 		dev_err(&pdev->dev, "failed to pcm register\n");
6890adf2920SShengjiu Wang 		return ret;
6900adf2920SShengjiu Wang 	}
6910adf2920SShengjiu Wang 
69247a70e6fSCosmin Samoila 	ret = devm_snd_soc_register_component(&pdev->dev, &fsl_micfil_component,
69347a70e6fSCosmin Samoila 					      &fsl_micfil_dai, 1);
69447a70e6fSCosmin Samoila 	if (ret) {
69547a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "failed to register component %s\n",
69647a70e6fSCosmin Samoila 			fsl_micfil_component.name);
69747a70e6fSCosmin Samoila 	}
69847a70e6fSCosmin Samoila 
69947a70e6fSCosmin Samoila 	return ret;
70047a70e6fSCosmin Samoila }
70147a70e6fSCosmin Samoila 
70247a70e6fSCosmin Samoila static int __maybe_unused fsl_micfil_runtime_suspend(struct device *dev)
70347a70e6fSCosmin Samoila {
70447a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = dev_get_drvdata(dev);
70547a70e6fSCosmin Samoila 
70647a70e6fSCosmin Samoila 	regcache_cache_only(micfil->regmap, true);
70747a70e6fSCosmin Samoila 
70847a70e6fSCosmin Samoila 	clk_disable_unprepare(micfil->mclk);
709b5cf28f7SShengjiu Wang 	clk_disable_unprepare(micfil->busclk);
71047a70e6fSCosmin Samoila 
71147a70e6fSCosmin Samoila 	return 0;
71247a70e6fSCosmin Samoila }
71347a70e6fSCosmin Samoila 
71447a70e6fSCosmin Samoila static int __maybe_unused fsl_micfil_runtime_resume(struct device *dev)
71547a70e6fSCosmin Samoila {
71647a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = dev_get_drvdata(dev);
71747a70e6fSCosmin Samoila 	int ret;
71847a70e6fSCosmin Samoila 
719b5cf28f7SShengjiu Wang 	ret = clk_prepare_enable(micfil->busclk);
72047a70e6fSCosmin Samoila 	if (ret < 0)
72147a70e6fSCosmin Samoila 		return ret;
72247a70e6fSCosmin Samoila 
723b5cf28f7SShengjiu Wang 	ret = clk_prepare_enable(micfil->mclk);
724b5cf28f7SShengjiu Wang 	if (ret < 0) {
725b5cf28f7SShengjiu Wang 		clk_disable_unprepare(micfil->busclk);
726b5cf28f7SShengjiu Wang 		return ret;
727b5cf28f7SShengjiu Wang 	}
728b5cf28f7SShengjiu Wang 
72947a70e6fSCosmin Samoila 	regcache_cache_only(micfil->regmap, false);
73047a70e6fSCosmin Samoila 	regcache_mark_dirty(micfil->regmap);
73147a70e6fSCosmin Samoila 	regcache_sync(micfil->regmap);
73247a70e6fSCosmin Samoila 
73347a70e6fSCosmin Samoila 	return 0;
73447a70e6fSCosmin Samoila }
73547a70e6fSCosmin Samoila 
73647a70e6fSCosmin Samoila static int __maybe_unused fsl_micfil_suspend(struct device *dev)
73747a70e6fSCosmin Samoila {
73847a70e6fSCosmin Samoila 	pm_runtime_force_suspend(dev);
73947a70e6fSCosmin Samoila 
74047a70e6fSCosmin Samoila 	return 0;
74147a70e6fSCosmin Samoila }
74247a70e6fSCosmin Samoila 
74347a70e6fSCosmin Samoila static int __maybe_unused fsl_micfil_resume(struct device *dev)
74447a70e6fSCosmin Samoila {
74547a70e6fSCosmin Samoila 	pm_runtime_force_resume(dev);
74647a70e6fSCosmin Samoila 
74747a70e6fSCosmin Samoila 	return 0;
74847a70e6fSCosmin Samoila }
74947a70e6fSCosmin Samoila 
75047a70e6fSCosmin Samoila static const struct dev_pm_ops fsl_micfil_pm_ops = {
75147a70e6fSCosmin Samoila 	SET_RUNTIME_PM_OPS(fsl_micfil_runtime_suspend,
75247a70e6fSCosmin Samoila 			   fsl_micfil_runtime_resume,
75347a70e6fSCosmin Samoila 			   NULL)
75447a70e6fSCosmin Samoila 	SET_SYSTEM_SLEEP_PM_OPS(fsl_micfil_suspend,
75547a70e6fSCosmin Samoila 				fsl_micfil_resume)
75647a70e6fSCosmin Samoila };
75747a70e6fSCosmin Samoila 
75847a70e6fSCosmin Samoila static struct platform_driver fsl_micfil_driver = {
75947a70e6fSCosmin Samoila 	.probe = fsl_micfil_probe,
76047a70e6fSCosmin Samoila 	.driver = {
76147a70e6fSCosmin Samoila 		.name = "fsl-micfil-dai",
76247a70e6fSCosmin Samoila 		.pm = &fsl_micfil_pm_ops,
76347a70e6fSCosmin Samoila 		.of_match_table = fsl_micfil_dt_ids,
76447a70e6fSCosmin Samoila 	},
76547a70e6fSCosmin Samoila };
76647a70e6fSCosmin Samoila module_platform_driver(fsl_micfil_driver);
76747a70e6fSCosmin Samoila 
76847a70e6fSCosmin Samoila MODULE_AUTHOR("Cosmin-Gabriel Samoila <cosmin.samoila@nxp.com>");
76947a70e6fSCosmin Samoila MODULE_DESCRIPTION("NXP PDM Microphone Interface (MICFIL) driver");
77047a70e6fSCosmin Samoila MODULE_LICENSE("GPL v2");
771