xref: /openbmc/linux/sound/soc/fsl/fsl_micfil.c (revision 17f2142bae4b6f2e27f19ce57d79fc42ba5ef659)
147a70e6fSCosmin Samoila // SPDX-License-Identifier: GPL-2.0
247a70e6fSCosmin Samoila // Copyright 2018 NXP
347a70e6fSCosmin Samoila 
4*17f2142bSSascha 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);
120*17f2142bSSascha Hauer 	osr = 16 - FIELD_GET(MICFIL_CTRL2_CICOSR, ctrl2_reg);
121*17f2142bSSascha Hauer 	qsel = FIELD_GET(MICFIL_CTRL2_QSEL, ctrl2_reg);
12247a70e6fSCosmin Samoila 
12347a70e6fSCosmin Samoila 	switch (qsel) {
124*17f2142bSSascha Hauer 	case MICFIL_QSEL_HIGH_QUALITY:
12547a70e6fSCosmin Samoila 		bclk = rate * 8 * osr / 2; /* kfactor = 0.5 */
12647a70e6fSCosmin Samoila 		break;
127*17f2142bSSascha Hauer 	case MICFIL_QSEL_MEDIUM_QUALITY:
128*17f2142bSSascha Hauer 	case MICFIL_QSEL_VLOW0_QUALITY:
12947a70e6fSCosmin Samoila 		bclk = rate * 4 * osr * 1; /* kfactor = 1 */
13047a70e6fSCosmin Samoila 		break;
131*17f2142bSSascha Hauer 	case MICFIL_QSEL_LOW_QUALITY:
132*17f2142bSSascha Hauer 	case MICFIL_QSEL_VLOW1_QUALITY:
13347a70e6fSCosmin Samoila 		bclk = rate * 2 * osr * 2; /* kfactor = 2 */
13447a70e6fSCosmin Samoila 		break;
135*17f2142bSSascha 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 
17147a70e6fSCosmin Samoila 	ret = regmap_update_bits(micfil->regmap,
17247a70e6fSCosmin Samoila 				 REG_MICFIL_CTRL1,
173bd2cffd1SSascha Hauer 				 MICFIL_CTRL1_MDIS,
17447a70e6fSCosmin Samoila 				 0);
17547a70e6fSCosmin Samoila 	if (ret) {
17647a70e6fSCosmin Samoila 		dev_err(dev, "failed to clear MDIS bit %d\n", ret);
17747a70e6fSCosmin Samoila 		return ret;
17847a70e6fSCosmin Samoila 	}
17947a70e6fSCosmin Samoila 
18047a70e6fSCosmin Samoila 	ret = regmap_update_bits(micfil->regmap,
18147a70e6fSCosmin Samoila 				 REG_MICFIL_CTRL1,
182bd2cffd1SSascha Hauer 				 MICFIL_CTRL1_SRES,
18347a70e6fSCosmin Samoila 				 MICFIL_CTRL1_SRES);
18447a70e6fSCosmin Samoila 	if (ret) {
18547a70e6fSCosmin Samoila 		dev_err(dev, "failed to reset MICFIL: %d\n", ret);
18647a70e6fSCosmin Samoila 		return ret;
18747a70e6fSCosmin Samoila 	}
18847a70e6fSCosmin Samoila 
18947a70e6fSCosmin Samoila 	return 0;
19047a70e6fSCosmin Samoila }
19147a70e6fSCosmin Samoila 
19247a70e6fSCosmin Samoila static int fsl_micfil_set_mclk_rate(struct fsl_micfil *micfil,
19347a70e6fSCosmin Samoila 				    unsigned int freq)
19447a70e6fSCosmin Samoila {
19547a70e6fSCosmin Samoila 	struct device *dev = &micfil->pdev->dev;
19647a70e6fSCosmin Samoila 	int ret;
19747a70e6fSCosmin Samoila 
19847a70e6fSCosmin Samoila 	clk_disable_unprepare(micfil->mclk);
19947a70e6fSCosmin Samoila 
20047a70e6fSCosmin Samoila 	ret = clk_set_rate(micfil->mclk, freq * 1024);
20147a70e6fSCosmin Samoila 	if (ret)
20247a70e6fSCosmin Samoila 		dev_warn(dev, "failed to set rate (%u): %d\n",
20347a70e6fSCosmin Samoila 			 freq * 1024, ret);
20447a70e6fSCosmin Samoila 
20547a70e6fSCosmin Samoila 	clk_prepare_enable(micfil->mclk);
20647a70e6fSCosmin Samoila 
20747a70e6fSCosmin Samoila 	return ret;
20847a70e6fSCosmin Samoila }
20947a70e6fSCosmin Samoila 
21047a70e6fSCosmin Samoila static int fsl_micfil_startup(struct snd_pcm_substream *substream,
21147a70e6fSCosmin Samoila 			      struct snd_soc_dai *dai)
21247a70e6fSCosmin Samoila {
21347a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = snd_soc_dai_get_drvdata(dai);
21447a70e6fSCosmin Samoila 
21547a70e6fSCosmin Samoila 	if (!micfil) {
21611106cb3STang Bin 		dev_err(dai->dev, "micfil dai priv_data not set\n");
21747a70e6fSCosmin Samoila 		return -EINVAL;
21847a70e6fSCosmin Samoila 	}
21947a70e6fSCosmin Samoila 
22047a70e6fSCosmin Samoila 	return 0;
22147a70e6fSCosmin Samoila }
22247a70e6fSCosmin Samoila 
22347a70e6fSCosmin Samoila static int fsl_micfil_trigger(struct snd_pcm_substream *substream, int cmd,
22447a70e6fSCosmin Samoila 			      struct snd_soc_dai *dai)
22547a70e6fSCosmin Samoila {
22647a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = snd_soc_dai_get_drvdata(dai);
22747a70e6fSCosmin Samoila 	struct device *dev = &micfil->pdev->dev;
22847a70e6fSCosmin Samoila 	int ret;
22947a70e6fSCosmin Samoila 
23047a70e6fSCosmin Samoila 	switch (cmd) {
23147a70e6fSCosmin Samoila 	case SNDRV_PCM_TRIGGER_START:
23247a70e6fSCosmin Samoila 	case SNDRV_PCM_TRIGGER_RESUME:
23347a70e6fSCosmin Samoila 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
23447a70e6fSCosmin Samoila 		ret = fsl_micfil_reset(dev);
23547a70e6fSCosmin Samoila 		if (ret) {
23647a70e6fSCosmin Samoila 			dev_err(dev, "failed to soft reset\n");
23747a70e6fSCosmin Samoila 			return ret;
23847a70e6fSCosmin Samoila 		}
23947a70e6fSCosmin Samoila 
24047a70e6fSCosmin Samoila 		/* DMA Interrupt Selection - DISEL bits
24147a70e6fSCosmin Samoila 		 * 00 - DMA and IRQ disabled
24247a70e6fSCosmin Samoila 		 * 01 - DMA req enabled
24347a70e6fSCosmin Samoila 		 * 10 - IRQ enabled
24447a70e6fSCosmin Samoila 		 * 11 - reserved
24547a70e6fSCosmin Samoila 		 */
24647a70e6fSCosmin Samoila 		ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL1,
247*17f2142bSSascha Hauer 				MICFIL_CTRL1_DISEL,
248*17f2142bSSascha Hauer 				FIELD_PREP(MICFIL_CTRL1_DISEL, MICFIL_CTRL1_DISEL_DMA));
24947a70e6fSCosmin Samoila 		if (ret) {
25047a70e6fSCosmin Samoila 			dev_err(dev, "failed to update DISEL bits\n");
25147a70e6fSCosmin Samoila 			return ret;
25247a70e6fSCosmin Samoila 		}
25347a70e6fSCosmin Samoila 
25447a70e6fSCosmin Samoila 		/* Enable the module */
25547a70e6fSCosmin Samoila 		ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL1,
256bd2cffd1SSascha Hauer 					 MICFIL_CTRL1_PDMIEN,
25747a70e6fSCosmin Samoila 					 MICFIL_CTRL1_PDMIEN);
25847a70e6fSCosmin Samoila 		if (ret) {
25947a70e6fSCosmin Samoila 			dev_err(dev, "failed to enable the module\n");
26047a70e6fSCosmin Samoila 			return ret;
26147a70e6fSCosmin Samoila 		}
26247a70e6fSCosmin Samoila 
26347a70e6fSCosmin Samoila 		break;
26447a70e6fSCosmin Samoila 	case SNDRV_PCM_TRIGGER_STOP:
26547a70e6fSCosmin Samoila 	case SNDRV_PCM_TRIGGER_SUSPEND:
26647a70e6fSCosmin Samoila 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
26747a70e6fSCosmin Samoila 		/* Disable the module */
26847a70e6fSCosmin Samoila 		ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL1,
269bd2cffd1SSascha Hauer 					 MICFIL_CTRL1_PDMIEN,
27047a70e6fSCosmin Samoila 					 0);
27147a70e6fSCosmin Samoila 		if (ret) {
27247a70e6fSCosmin Samoila 			dev_err(dev, "failed to enable the module\n");
27347a70e6fSCosmin Samoila 			return ret;
27447a70e6fSCosmin Samoila 		}
27547a70e6fSCosmin Samoila 
27647a70e6fSCosmin Samoila 		ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL1,
277*17f2142bSSascha Hauer 				MICFIL_CTRL1_DISEL,
278*17f2142bSSascha Hauer 				FIELD_PREP(MICFIL_CTRL1_DISEL, MICFIL_CTRL1_DISEL_DISABLE));
27947a70e6fSCosmin Samoila 		if (ret) {
28047a70e6fSCosmin Samoila 			dev_err(dev, "failed to update DISEL bits\n");
28147a70e6fSCosmin Samoila 			return ret;
28247a70e6fSCosmin Samoila 		}
28347a70e6fSCosmin Samoila 		break;
28447a70e6fSCosmin Samoila 	default:
28547a70e6fSCosmin Samoila 		return -EINVAL;
28647a70e6fSCosmin Samoila 	}
28747a70e6fSCosmin Samoila 	return 0;
28847a70e6fSCosmin Samoila }
28947a70e6fSCosmin Samoila 
29047a70e6fSCosmin Samoila static int fsl_set_clock_params(struct device *dev, unsigned int rate)
29147a70e6fSCosmin Samoila {
29247a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = dev_get_drvdata(dev);
29347a70e6fSCosmin Samoila 	int clk_div;
29415b5c496STang Bin 	int ret;
29547a70e6fSCosmin Samoila 
29647a70e6fSCosmin Samoila 	ret = fsl_micfil_set_mclk_rate(micfil, rate);
29747a70e6fSCosmin Samoila 	if (ret < 0)
29847a70e6fSCosmin Samoila 		dev_err(dev, "failed to set mclk[%lu] to rate %u\n",
29947a70e6fSCosmin Samoila 			clk_get_rate(micfil->mclk), rate);
30047a70e6fSCosmin Samoila 
30147a70e6fSCosmin Samoila 	/* set CICOSR */
30247a70e6fSCosmin Samoila 	ret |= regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL2,
303*17f2142bSSascha Hauer 				 MICFIL_CTRL2_CICOSR,
304*17f2142bSSascha Hauer 				 FIELD_PREP(MICFIL_CTRL2_CICOSR, MICFIL_CTRL2_CICOSR_DEFAULT));
30547a70e6fSCosmin Samoila 	if (ret)
30647a70e6fSCosmin Samoila 		dev_err(dev, "failed to set CICOSR in reg 0x%X\n",
30747a70e6fSCosmin Samoila 			REG_MICFIL_CTRL2);
30847a70e6fSCosmin Samoila 
30947a70e6fSCosmin Samoila 	/* set CLK_DIV */
31047a70e6fSCosmin Samoila 	clk_div = get_clk_div(micfil, rate);
31147a70e6fSCosmin Samoila 	if (clk_div < 0)
31247a70e6fSCosmin Samoila 		ret = -EINVAL;
31347a70e6fSCosmin Samoila 
31447a70e6fSCosmin Samoila 	ret |= regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL2,
315*17f2142bSSascha Hauer 				 MICFIL_CTRL2_CLKDIV,
316*17f2142bSSascha Hauer 				 FIELD_PREP(MICFIL_CTRL2_CLKDIV, clk_div));
31747a70e6fSCosmin Samoila 	if (ret)
31847a70e6fSCosmin Samoila 		dev_err(dev, "failed to set CLKDIV in reg 0x%X\n",
31947a70e6fSCosmin Samoila 			REG_MICFIL_CTRL2);
32047a70e6fSCosmin Samoila 
32147a70e6fSCosmin Samoila 	return ret;
32247a70e6fSCosmin Samoila }
32347a70e6fSCosmin Samoila 
32447a70e6fSCosmin Samoila static int fsl_micfil_hw_params(struct snd_pcm_substream *substream,
32547a70e6fSCosmin Samoila 				struct snd_pcm_hw_params *params,
32647a70e6fSCosmin Samoila 				struct snd_soc_dai *dai)
32747a70e6fSCosmin Samoila {
32847a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = snd_soc_dai_get_drvdata(dai);
32947a70e6fSCosmin Samoila 	unsigned int channels = params_channels(params);
33047a70e6fSCosmin Samoila 	unsigned int rate = params_rate(params);
33147a70e6fSCosmin Samoila 	struct device *dev = &micfil->pdev->dev;
33247a70e6fSCosmin Samoila 	int ret;
33347a70e6fSCosmin Samoila 
33447a70e6fSCosmin Samoila 	/* 1. Disable the module */
33547a70e6fSCosmin Samoila 	ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL1,
336bd2cffd1SSascha Hauer 				 MICFIL_CTRL1_PDMIEN, 0);
33747a70e6fSCosmin Samoila 	if (ret) {
33847a70e6fSCosmin Samoila 		dev_err(dev, "failed to disable the module\n");
33947a70e6fSCosmin Samoila 		return ret;
34047a70e6fSCosmin Samoila 	}
34147a70e6fSCosmin Samoila 
34247a70e6fSCosmin Samoila 	/* enable channels */
34347a70e6fSCosmin Samoila 	ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL1,
34447a70e6fSCosmin Samoila 				 0xFF, ((1 << channels) - 1));
34547a70e6fSCosmin Samoila 	if (ret) {
34647a70e6fSCosmin Samoila 		dev_err(dev, "failed to enable channels %d, reg 0x%X\n", ret,
34747a70e6fSCosmin Samoila 			REG_MICFIL_CTRL1);
34847a70e6fSCosmin Samoila 		return ret;
34947a70e6fSCosmin Samoila 	}
35047a70e6fSCosmin Samoila 
35147a70e6fSCosmin Samoila 	ret = fsl_set_clock_params(dev, rate);
35247a70e6fSCosmin Samoila 	if (ret < 0) {
35347a70e6fSCosmin Samoila 		dev_err(dev, "Failed to set clock parameters [%d]\n", ret);
35447a70e6fSCosmin Samoila 		return ret;
35547a70e6fSCosmin Samoila 	}
35647a70e6fSCosmin Samoila 
35747a70e6fSCosmin Samoila 	micfil->dma_params_rx.maxburst = channels * MICFIL_DMA_MAXBURST_RX;
35847a70e6fSCosmin Samoila 
35947a70e6fSCosmin Samoila 	return 0;
36047a70e6fSCosmin Samoila }
36147a70e6fSCosmin Samoila 
36238d89a56SRikard Falkeborn static const struct snd_soc_dai_ops fsl_micfil_dai_ops = {
36347a70e6fSCosmin Samoila 	.startup = fsl_micfil_startup,
36447a70e6fSCosmin Samoila 	.trigger = fsl_micfil_trigger,
36547a70e6fSCosmin Samoila 	.hw_params = fsl_micfil_hw_params,
36647a70e6fSCosmin Samoila };
36747a70e6fSCosmin Samoila 
36847a70e6fSCosmin Samoila static int fsl_micfil_dai_probe(struct snd_soc_dai *cpu_dai)
36947a70e6fSCosmin Samoila {
37047a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = dev_get_drvdata(cpu_dai->dev);
37147a70e6fSCosmin Samoila 	struct device *dev = cpu_dai->dev;
37247a70e6fSCosmin Samoila 	int ret;
37347a70e6fSCosmin Samoila 	int i;
37447a70e6fSCosmin Samoila 
37547a70e6fSCosmin Samoila 	/* set qsel to medium */
37647a70e6fSCosmin Samoila 	ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL2,
377*17f2142bSSascha Hauer 			MICFIL_CTRL2_QSEL,
378*17f2142bSSascha Hauer 			FIELD_PREP(MICFIL_CTRL2_QSEL, MICFIL_QSEL_MEDIUM_QUALITY));
37947a70e6fSCosmin Samoila 	if (ret) {
38047a70e6fSCosmin Samoila 		dev_err(dev, "failed to set quality mode bits, reg 0x%X\n",
38147a70e6fSCosmin Samoila 			REG_MICFIL_CTRL2);
38247a70e6fSCosmin Samoila 		return ret;
38347a70e6fSCosmin Samoila 	}
38447a70e6fSCosmin Samoila 
38547a70e6fSCosmin Samoila 	/* set default gain to max_gain */
38647a70e6fSCosmin Samoila 	regmap_write(micfil->regmap, REG_MICFIL_OUT_CTRL, 0x77777777);
38747a70e6fSCosmin Samoila 	for (i = 0; i < 8; i++)
38847a70e6fSCosmin Samoila 		micfil->channel_gain[i] = 0xF;
38947a70e6fSCosmin Samoila 
39047a70e6fSCosmin Samoila 	snd_soc_dai_init_dma_data(cpu_dai, NULL,
39147a70e6fSCosmin Samoila 				  &micfil->dma_params_rx);
39247a70e6fSCosmin Samoila 
39347a70e6fSCosmin Samoila 	/* FIFO Watermark Control - FIFOWMK*/
39447a70e6fSCosmin Samoila 	ret = regmap_update_bits(micfil->regmap, REG_MICFIL_FIFO_CTRL,
395*17f2142bSSascha Hauer 			MICFIL_FIFO_CTRL_FIFOWMK,
396*17f2142bSSascha Hauer 			FIELD_PREP(MICFIL_FIFO_CTRL_FIFOWMK, micfil->soc->fifo_depth - 1));
39747a70e6fSCosmin Samoila 	if (ret) {
39847a70e6fSCosmin Samoila 		dev_err(dev, "failed to set FIFOWMK\n");
39947a70e6fSCosmin Samoila 		return ret;
40047a70e6fSCosmin Samoila 	}
40147a70e6fSCosmin Samoila 
40247a70e6fSCosmin Samoila 	return 0;
40347a70e6fSCosmin Samoila }
40447a70e6fSCosmin Samoila 
40547a70e6fSCosmin Samoila static struct snd_soc_dai_driver fsl_micfil_dai = {
40647a70e6fSCosmin Samoila 	.probe = fsl_micfil_dai_probe,
40747a70e6fSCosmin Samoila 	.capture = {
40847a70e6fSCosmin Samoila 		.stream_name = "CPU-Capture",
40947a70e6fSCosmin Samoila 		.channels_min = 1,
41047a70e6fSCosmin Samoila 		.channels_max = 8,
41147a70e6fSCosmin Samoila 		.rates = FSL_MICFIL_RATES,
41247a70e6fSCosmin Samoila 		.formats = FSL_MICFIL_FORMATS,
41347a70e6fSCosmin Samoila 	},
41447a70e6fSCosmin Samoila 	.ops = &fsl_micfil_dai_ops,
41547a70e6fSCosmin Samoila };
41647a70e6fSCosmin Samoila 
41747a70e6fSCosmin Samoila static const struct snd_soc_component_driver fsl_micfil_component = {
41847a70e6fSCosmin Samoila 	.name		= "fsl-micfil-dai",
41947a70e6fSCosmin Samoila 	.controls       = fsl_micfil_snd_controls,
42047a70e6fSCosmin Samoila 	.num_controls   = ARRAY_SIZE(fsl_micfil_snd_controls),
42147a70e6fSCosmin Samoila 
42247a70e6fSCosmin Samoila };
42347a70e6fSCosmin Samoila 
42447a70e6fSCosmin Samoila /* REGMAP */
42547a70e6fSCosmin Samoila static const struct reg_default fsl_micfil_reg_defaults[] = {
42647a70e6fSCosmin Samoila 	{REG_MICFIL_CTRL1,		0x00000000},
42747a70e6fSCosmin Samoila 	{REG_MICFIL_CTRL2,		0x00000000},
42847a70e6fSCosmin Samoila 	{REG_MICFIL_STAT,		0x00000000},
42947a70e6fSCosmin Samoila 	{REG_MICFIL_FIFO_CTRL,		0x00000007},
43047a70e6fSCosmin Samoila 	{REG_MICFIL_FIFO_STAT,		0x00000000},
43147a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH0,		0x00000000},
43247a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH1,		0x00000000},
43347a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH2,		0x00000000},
43447a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH3,		0x00000000},
43547a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH4,		0x00000000},
43647a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH5,		0x00000000},
43747a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH6,		0x00000000},
43847a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH7,		0x00000000},
43947a70e6fSCosmin Samoila 	{REG_MICFIL_DC_CTRL,		0x00000000},
44047a70e6fSCosmin Samoila 	{REG_MICFIL_OUT_CTRL,		0x00000000},
44147a70e6fSCosmin Samoila 	{REG_MICFIL_OUT_STAT,		0x00000000},
44247a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_CTRL1,		0x00000000},
44347a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_CTRL2,		0x000A0000},
44447a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_STAT,		0x00000000},
44547a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_SCONFIG,	0x00000000},
44647a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_NCONFIG,	0x80000000},
44747a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_NDATA,		0x00000000},
44847a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_ZCD,		0x00000004},
44947a70e6fSCosmin Samoila };
45047a70e6fSCosmin Samoila 
45147a70e6fSCosmin Samoila static bool fsl_micfil_readable_reg(struct device *dev, unsigned int reg)
45247a70e6fSCosmin Samoila {
45347a70e6fSCosmin Samoila 	switch (reg) {
45447a70e6fSCosmin Samoila 	case REG_MICFIL_CTRL1:
45547a70e6fSCosmin Samoila 	case REG_MICFIL_CTRL2:
45647a70e6fSCosmin Samoila 	case REG_MICFIL_STAT:
45747a70e6fSCosmin Samoila 	case REG_MICFIL_FIFO_CTRL:
45847a70e6fSCosmin Samoila 	case REG_MICFIL_FIFO_STAT:
45947a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH0:
46047a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH1:
46147a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH2:
46247a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH3:
46347a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH4:
46447a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH5:
46547a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH6:
46647a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH7:
46747a70e6fSCosmin Samoila 	case REG_MICFIL_DC_CTRL:
46847a70e6fSCosmin Samoila 	case REG_MICFIL_OUT_CTRL:
46947a70e6fSCosmin Samoila 	case REG_MICFIL_OUT_STAT:
47047a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_CTRL1:
47147a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_CTRL2:
47247a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_STAT:
47347a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_SCONFIG:
47447a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_NCONFIG:
47547a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_NDATA:
47647a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_ZCD:
47747a70e6fSCosmin Samoila 		return true;
47847a70e6fSCosmin Samoila 	default:
47947a70e6fSCosmin Samoila 		return false;
48047a70e6fSCosmin Samoila 	}
48147a70e6fSCosmin Samoila }
48247a70e6fSCosmin Samoila 
48347a70e6fSCosmin Samoila static bool fsl_micfil_writeable_reg(struct device *dev, unsigned int reg)
48447a70e6fSCosmin Samoila {
48547a70e6fSCosmin Samoila 	switch (reg) {
48647a70e6fSCosmin Samoila 	case REG_MICFIL_CTRL1:
48747a70e6fSCosmin Samoila 	case REG_MICFIL_CTRL2:
48847a70e6fSCosmin Samoila 	case REG_MICFIL_STAT:		/* Write 1 to Clear */
48947a70e6fSCosmin Samoila 	case REG_MICFIL_FIFO_CTRL:
49047a70e6fSCosmin Samoila 	case REG_MICFIL_FIFO_STAT:	/* Write 1 to Clear */
49147a70e6fSCosmin Samoila 	case REG_MICFIL_DC_CTRL:
49247a70e6fSCosmin Samoila 	case REG_MICFIL_OUT_CTRL:
49347a70e6fSCosmin Samoila 	case REG_MICFIL_OUT_STAT:	/* Write 1 to Clear */
49447a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_CTRL1:
49547a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_CTRL2:
49647a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_STAT:	/* Write 1 to Clear */
49747a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_SCONFIG:
49847a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_NCONFIG:
49947a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_ZCD:
50047a70e6fSCosmin Samoila 		return true;
50147a70e6fSCosmin Samoila 	default:
50247a70e6fSCosmin Samoila 		return false;
50347a70e6fSCosmin Samoila 	}
50447a70e6fSCosmin Samoila }
50547a70e6fSCosmin Samoila 
50647a70e6fSCosmin Samoila static bool fsl_micfil_volatile_reg(struct device *dev, unsigned int reg)
50747a70e6fSCosmin Samoila {
50847a70e6fSCosmin Samoila 	switch (reg) {
50947a70e6fSCosmin Samoila 	case REG_MICFIL_STAT:
51047a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH0:
51147a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH1:
51247a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH2:
51347a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH3:
51447a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH4:
51547a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH5:
51647a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH6:
51747a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH7:
51847a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_STAT:
51947a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_NDATA:
52047a70e6fSCosmin Samoila 		return true;
52147a70e6fSCosmin Samoila 	default:
52247a70e6fSCosmin Samoila 		return false;
52347a70e6fSCosmin Samoila 	}
52447a70e6fSCosmin Samoila }
52547a70e6fSCosmin Samoila 
52647a70e6fSCosmin Samoila static const struct regmap_config fsl_micfil_regmap_config = {
52747a70e6fSCosmin Samoila 	.reg_bits = 32,
52847a70e6fSCosmin Samoila 	.reg_stride = 4,
52947a70e6fSCosmin Samoila 	.val_bits = 32,
53047a70e6fSCosmin Samoila 
53147a70e6fSCosmin Samoila 	.max_register = REG_MICFIL_VAD0_ZCD,
53247a70e6fSCosmin Samoila 	.reg_defaults = fsl_micfil_reg_defaults,
53347a70e6fSCosmin Samoila 	.num_reg_defaults = ARRAY_SIZE(fsl_micfil_reg_defaults),
53447a70e6fSCosmin Samoila 	.readable_reg = fsl_micfil_readable_reg,
53547a70e6fSCosmin Samoila 	.volatile_reg = fsl_micfil_volatile_reg,
53647a70e6fSCosmin Samoila 	.writeable_reg = fsl_micfil_writeable_reg,
53747a70e6fSCosmin Samoila 	.cache_type = REGCACHE_RBTREE,
53847a70e6fSCosmin Samoila };
53947a70e6fSCosmin Samoila 
54047a70e6fSCosmin Samoila /* END OF REGMAP */
54147a70e6fSCosmin Samoila 
54247a70e6fSCosmin Samoila static irqreturn_t micfil_isr(int irq, void *devid)
54347a70e6fSCosmin Samoila {
54447a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = (struct fsl_micfil *)devid;
54547a70e6fSCosmin Samoila 	struct platform_device *pdev = micfil->pdev;
54647a70e6fSCosmin Samoila 	u32 stat_reg;
54747a70e6fSCosmin Samoila 	u32 fifo_stat_reg;
54847a70e6fSCosmin Samoila 	u32 ctrl1_reg;
54947a70e6fSCosmin Samoila 	bool dma_enabled;
55047a70e6fSCosmin Samoila 	int i;
55147a70e6fSCosmin Samoila 
55247a70e6fSCosmin Samoila 	regmap_read(micfil->regmap, REG_MICFIL_STAT, &stat_reg);
55347a70e6fSCosmin Samoila 	regmap_read(micfil->regmap, REG_MICFIL_CTRL1, &ctrl1_reg);
55447a70e6fSCosmin Samoila 	regmap_read(micfil->regmap, REG_MICFIL_FIFO_STAT, &fifo_stat_reg);
55547a70e6fSCosmin Samoila 
556*17f2142bSSascha Hauer 	dma_enabled = FIELD_GET(MICFIL_CTRL1_DISEL, ctrl1_reg) == MICFIL_CTRL1_DISEL_DMA;
55747a70e6fSCosmin Samoila 
55847a70e6fSCosmin Samoila 	/* Channel 0-7 Output Data Flags */
55947a70e6fSCosmin Samoila 	for (i = 0; i < MICFIL_OUTPUT_CHANNELS; i++) {
560*17f2142bSSascha Hauer 		if (stat_reg & MICFIL_STAT_CHXF(i))
56147a70e6fSCosmin Samoila 			dev_dbg(&pdev->dev,
56247a70e6fSCosmin Samoila 				"Data available in Data Channel %d\n", i);
56347a70e6fSCosmin Samoila 		/* if DMA is not enabled, field must be written with 1
56447a70e6fSCosmin Samoila 		 * to clear
56547a70e6fSCosmin Samoila 		 */
56647a70e6fSCosmin Samoila 		if (!dma_enabled)
56747a70e6fSCosmin Samoila 			regmap_write_bits(micfil->regmap,
56847a70e6fSCosmin Samoila 					  REG_MICFIL_STAT,
569*17f2142bSSascha Hauer 					  MICFIL_STAT_CHXF(i),
57047a70e6fSCosmin Samoila 					  1);
57147a70e6fSCosmin Samoila 	}
57247a70e6fSCosmin Samoila 
57347a70e6fSCosmin Samoila 	for (i = 0; i < MICFIL_FIFO_NUM; i++) {
574*17f2142bSSascha Hauer 		if (fifo_stat_reg & MICFIL_FIFO_STAT_FIFOX_OVER(i))
57547a70e6fSCosmin Samoila 			dev_dbg(&pdev->dev,
57647a70e6fSCosmin Samoila 				"FIFO Overflow Exception flag for channel %d\n",
57747a70e6fSCosmin Samoila 				i);
57847a70e6fSCosmin Samoila 
579*17f2142bSSascha Hauer 		if (fifo_stat_reg & MICFIL_FIFO_STAT_FIFOX_UNDER(i))
58047a70e6fSCosmin Samoila 			dev_dbg(&pdev->dev,
58147a70e6fSCosmin Samoila 				"FIFO Underflow Exception flag for channel %d\n",
58247a70e6fSCosmin Samoila 				i);
58347a70e6fSCosmin Samoila 	}
58447a70e6fSCosmin Samoila 
58547a70e6fSCosmin Samoila 	return IRQ_HANDLED;
58647a70e6fSCosmin Samoila }
58747a70e6fSCosmin Samoila 
58847a70e6fSCosmin Samoila static irqreturn_t micfil_err_isr(int irq, void *devid)
58947a70e6fSCosmin Samoila {
59047a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = (struct fsl_micfil *)devid;
59147a70e6fSCosmin Samoila 	struct platform_device *pdev = micfil->pdev;
59247a70e6fSCosmin Samoila 	u32 stat_reg;
59347a70e6fSCosmin Samoila 
59447a70e6fSCosmin Samoila 	regmap_read(micfil->regmap, REG_MICFIL_STAT, &stat_reg);
59547a70e6fSCosmin Samoila 
596bd2cffd1SSascha Hauer 	if (stat_reg & MICFIL_STAT_BSY_FIL)
59747a70e6fSCosmin Samoila 		dev_dbg(&pdev->dev, "isr: Decimation Filter is running\n");
59847a70e6fSCosmin Samoila 
599bd2cffd1SSascha Hauer 	if (stat_reg & MICFIL_STAT_FIR_RDY)
60047a70e6fSCosmin Samoila 		dev_dbg(&pdev->dev, "isr: FIR Filter Data ready\n");
60147a70e6fSCosmin Samoila 
602bd2cffd1SSascha Hauer 	if (stat_reg & MICFIL_STAT_LOWFREQF) {
60347a70e6fSCosmin Samoila 		dev_dbg(&pdev->dev, "isr: ipg_clk_app is too low\n");
60447a70e6fSCosmin Samoila 		regmap_write_bits(micfil->regmap, REG_MICFIL_STAT,
605bd2cffd1SSascha Hauer 				  MICFIL_STAT_LOWFREQF, 1);
60647a70e6fSCosmin Samoila 	}
60747a70e6fSCosmin Samoila 
60847a70e6fSCosmin Samoila 	return IRQ_HANDLED;
60947a70e6fSCosmin Samoila }
61047a70e6fSCosmin Samoila 
61147a70e6fSCosmin Samoila static int fsl_micfil_probe(struct platform_device *pdev)
61247a70e6fSCosmin Samoila {
61347a70e6fSCosmin Samoila 	struct device_node *np = pdev->dev.of_node;
61447a70e6fSCosmin Samoila 	struct fsl_micfil *micfil;
61547a70e6fSCosmin Samoila 	struct resource *res;
61647a70e6fSCosmin Samoila 	void __iomem *regs;
61747a70e6fSCosmin Samoila 	int ret, i;
61847a70e6fSCosmin Samoila 	unsigned long irqflag = 0;
61947a70e6fSCosmin Samoila 
62047a70e6fSCosmin Samoila 	micfil = devm_kzalloc(&pdev->dev, sizeof(*micfil), GFP_KERNEL);
62147a70e6fSCosmin Samoila 	if (!micfil)
62247a70e6fSCosmin Samoila 		return -ENOMEM;
62347a70e6fSCosmin Samoila 
62447a70e6fSCosmin Samoila 	micfil->pdev = pdev;
62547a70e6fSCosmin Samoila 	strncpy(micfil->name, np->name, sizeof(micfil->name) - 1);
62647a70e6fSCosmin Samoila 
627d7388718SFabio Estevam 	micfil->soc = of_device_get_match_data(&pdev->dev);
62847a70e6fSCosmin Samoila 
62947a70e6fSCosmin Samoila 	/* ipg_clk is used to control the registers
63047a70e6fSCosmin Samoila 	 * ipg_clk_app is used to operate the filter
63147a70e6fSCosmin Samoila 	 */
63247a70e6fSCosmin Samoila 	micfil->mclk = devm_clk_get(&pdev->dev, "ipg_clk_app");
63347a70e6fSCosmin Samoila 	if (IS_ERR(micfil->mclk)) {
63447a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "failed to get core clock: %ld\n",
63547a70e6fSCosmin Samoila 			PTR_ERR(micfil->mclk));
63647a70e6fSCosmin Samoila 		return PTR_ERR(micfil->mclk);
63747a70e6fSCosmin Samoila 	}
63847a70e6fSCosmin Samoila 
639b5cf28f7SShengjiu Wang 	micfil->busclk = devm_clk_get(&pdev->dev, "ipg_clk");
640b5cf28f7SShengjiu Wang 	if (IS_ERR(micfil->busclk)) {
641b5cf28f7SShengjiu Wang 		dev_err(&pdev->dev, "failed to get ipg clock: %ld\n",
642b5cf28f7SShengjiu Wang 			PTR_ERR(micfil->busclk));
643b5cf28f7SShengjiu Wang 		return PTR_ERR(micfil->busclk);
644b5cf28f7SShengjiu Wang 	}
645b5cf28f7SShengjiu Wang 
64647a70e6fSCosmin Samoila 	/* init regmap */
647d9bf1e79SYang Yingliang 	regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
64847a70e6fSCosmin Samoila 	if (IS_ERR(regs))
64947a70e6fSCosmin Samoila 		return PTR_ERR(regs);
65047a70e6fSCosmin Samoila 
651b5cf28f7SShengjiu Wang 	micfil->regmap = devm_regmap_init_mmio(&pdev->dev,
65247a70e6fSCosmin Samoila 					       regs,
65347a70e6fSCosmin Samoila 					       &fsl_micfil_regmap_config);
65447a70e6fSCosmin Samoila 	if (IS_ERR(micfil->regmap)) {
65547a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "failed to init MICFIL regmap: %ld\n",
65647a70e6fSCosmin Samoila 			PTR_ERR(micfil->regmap));
65747a70e6fSCosmin Samoila 		return PTR_ERR(micfil->regmap);
65847a70e6fSCosmin Samoila 	}
65947a70e6fSCosmin Samoila 
66047a70e6fSCosmin Samoila 	/* dataline mask for RX */
66147a70e6fSCosmin Samoila 	ret = of_property_read_u32_index(np,
66247a70e6fSCosmin Samoila 					 "fsl,dataline",
66347a70e6fSCosmin Samoila 					 0,
66447a70e6fSCosmin Samoila 					 &micfil->dataline);
66547a70e6fSCosmin Samoila 	if (ret)
66647a70e6fSCosmin Samoila 		micfil->dataline = 1;
66747a70e6fSCosmin Samoila 
66847a70e6fSCosmin Samoila 	if (micfil->dataline & ~micfil->soc->dataline) {
66947a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "dataline setting error, Mask is 0x%X\n",
67047a70e6fSCosmin Samoila 			micfil->soc->dataline);
67147a70e6fSCosmin Samoila 		return -EINVAL;
67247a70e6fSCosmin Samoila 	}
67347a70e6fSCosmin Samoila 
67447a70e6fSCosmin Samoila 	/* get IRQs */
67547a70e6fSCosmin Samoila 	for (i = 0; i < MICFIL_IRQ_LINES; i++) {
67647a70e6fSCosmin Samoila 		micfil->irq[i] = platform_get_irq(pdev, i);
67747a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "GET IRQ: %d\n", micfil->irq[i]);
67883b35f45STang Bin 		if (micfil->irq[i] < 0)
67947a70e6fSCosmin Samoila 			return micfil->irq[i];
68047a70e6fSCosmin Samoila 	}
68147a70e6fSCosmin Samoila 
68247a70e6fSCosmin Samoila 	if (of_property_read_bool(np, "fsl,shared-interrupt"))
68347a70e6fSCosmin Samoila 		irqflag = IRQF_SHARED;
68447a70e6fSCosmin Samoila 
685a62ed960SFabio Estevam 	/* Digital Microphone interface interrupt */
68647a70e6fSCosmin Samoila 	ret = devm_request_irq(&pdev->dev, micfil->irq[0],
68747a70e6fSCosmin Samoila 			       micfil_isr, irqflag,
68847a70e6fSCosmin Samoila 			       micfil->name, micfil);
68947a70e6fSCosmin Samoila 	if (ret) {
69047a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "failed to claim mic interface irq %u\n",
69147a70e6fSCosmin Samoila 			micfil->irq[0]);
69247a70e6fSCosmin Samoila 		return ret;
69347a70e6fSCosmin Samoila 	}
69447a70e6fSCosmin Samoila 
695a62ed960SFabio Estevam 	/* Digital Microphone interface error interrupt */
69647a70e6fSCosmin Samoila 	ret = devm_request_irq(&pdev->dev, micfil->irq[1],
69747a70e6fSCosmin Samoila 			       micfil_err_isr, irqflag,
69847a70e6fSCosmin Samoila 			       micfil->name, micfil);
69947a70e6fSCosmin Samoila 	if (ret) {
70047a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "failed to claim mic interface error irq %u\n",
70147a70e6fSCosmin Samoila 			micfil->irq[1]);
70247a70e6fSCosmin Samoila 		return ret;
70347a70e6fSCosmin Samoila 	}
70447a70e6fSCosmin Samoila 
70547a70e6fSCosmin Samoila 	micfil->dma_params_rx.chan_name = "rx";
70647a70e6fSCosmin Samoila 	micfil->dma_params_rx.addr = res->start + REG_MICFIL_DATACH0;
70747a70e6fSCosmin Samoila 	micfil->dma_params_rx.maxburst = MICFIL_DMA_MAXBURST_RX;
70847a70e6fSCosmin Samoila 
70947a70e6fSCosmin Samoila 
71047a70e6fSCosmin Samoila 	platform_set_drvdata(pdev, micfil);
71147a70e6fSCosmin Samoila 
71247a70e6fSCosmin Samoila 	pm_runtime_enable(&pdev->dev);
713b5cf28f7SShengjiu Wang 	regcache_cache_only(micfil->regmap, true);
71447a70e6fSCosmin Samoila 
7150adf2920SShengjiu Wang 	/*
7160adf2920SShengjiu Wang 	 * Register platform component before registering cpu dai for there
7170adf2920SShengjiu Wang 	 * is not defer probe for platform component in snd_soc_add_pcm_runtime().
7180adf2920SShengjiu Wang 	 */
7190adf2920SShengjiu Wang 	ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
7200adf2920SShengjiu Wang 	if (ret) {
7210adf2920SShengjiu Wang 		dev_err(&pdev->dev, "failed to pcm register\n");
7220adf2920SShengjiu Wang 		return ret;
7230adf2920SShengjiu Wang 	}
7240adf2920SShengjiu Wang 
72547a70e6fSCosmin Samoila 	ret = devm_snd_soc_register_component(&pdev->dev, &fsl_micfil_component,
72647a70e6fSCosmin Samoila 					      &fsl_micfil_dai, 1);
72747a70e6fSCosmin Samoila 	if (ret) {
72847a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "failed to register component %s\n",
72947a70e6fSCosmin Samoila 			fsl_micfil_component.name);
73047a70e6fSCosmin Samoila 	}
73147a70e6fSCosmin Samoila 
73247a70e6fSCosmin Samoila 	return ret;
73347a70e6fSCosmin Samoila }
73447a70e6fSCosmin Samoila 
73547a70e6fSCosmin Samoila static int __maybe_unused fsl_micfil_runtime_suspend(struct device *dev)
73647a70e6fSCosmin Samoila {
73747a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = dev_get_drvdata(dev);
73847a70e6fSCosmin Samoila 
73947a70e6fSCosmin Samoila 	regcache_cache_only(micfil->regmap, true);
74047a70e6fSCosmin Samoila 
74147a70e6fSCosmin Samoila 	clk_disable_unprepare(micfil->mclk);
742b5cf28f7SShengjiu Wang 	clk_disable_unprepare(micfil->busclk);
74347a70e6fSCosmin Samoila 
74447a70e6fSCosmin Samoila 	return 0;
74547a70e6fSCosmin Samoila }
74647a70e6fSCosmin Samoila 
74747a70e6fSCosmin Samoila static int __maybe_unused fsl_micfil_runtime_resume(struct device *dev)
74847a70e6fSCosmin Samoila {
74947a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = dev_get_drvdata(dev);
75047a70e6fSCosmin Samoila 	int ret;
75147a70e6fSCosmin Samoila 
752b5cf28f7SShengjiu Wang 	ret = clk_prepare_enable(micfil->busclk);
75347a70e6fSCosmin Samoila 	if (ret < 0)
75447a70e6fSCosmin Samoila 		return ret;
75547a70e6fSCosmin Samoila 
756b5cf28f7SShengjiu Wang 	ret = clk_prepare_enable(micfil->mclk);
757b5cf28f7SShengjiu Wang 	if (ret < 0) {
758b5cf28f7SShengjiu Wang 		clk_disable_unprepare(micfil->busclk);
759b5cf28f7SShengjiu Wang 		return ret;
760b5cf28f7SShengjiu Wang 	}
761b5cf28f7SShengjiu Wang 
76247a70e6fSCosmin Samoila 	regcache_cache_only(micfil->regmap, false);
76347a70e6fSCosmin Samoila 	regcache_mark_dirty(micfil->regmap);
76447a70e6fSCosmin Samoila 	regcache_sync(micfil->regmap);
76547a70e6fSCosmin Samoila 
76647a70e6fSCosmin Samoila 	return 0;
76747a70e6fSCosmin Samoila }
76847a70e6fSCosmin Samoila 
76947a70e6fSCosmin Samoila static int __maybe_unused fsl_micfil_suspend(struct device *dev)
77047a70e6fSCosmin Samoila {
77147a70e6fSCosmin Samoila 	pm_runtime_force_suspend(dev);
77247a70e6fSCosmin Samoila 
77347a70e6fSCosmin Samoila 	return 0;
77447a70e6fSCosmin Samoila }
77547a70e6fSCosmin Samoila 
77647a70e6fSCosmin Samoila static int __maybe_unused fsl_micfil_resume(struct device *dev)
77747a70e6fSCosmin Samoila {
77847a70e6fSCosmin Samoila 	pm_runtime_force_resume(dev);
77947a70e6fSCosmin Samoila 
78047a70e6fSCosmin Samoila 	return 0;
78147a70e6fSCosmin Samoila }
78247a70e6fSCosmin Samoila 
78347a70e6fSCosmin Samoila static const struct dev_pm_ops fsl_micfil_pm_ops = {
78447a70e6fSCosmin Samoila 	SET_RUNTIME_PM_OPS(fsl_micfil_runtime_suspend,
78547a70e6fSCosmin Samoila 			   fsl_micfil_runtime_resume,
78647a70e6fSCosmin Samoila 			   NULL)
78747a70e6fSCosmin Samoila 	SET_SYSTEM_SLEEP_PM_OPS(fsl_micfil_suspend,
78847a70e6fSCosmin Samoila 				fsl_micfil_resume)
78947a70e6fSCosmin Samoila };
79047a70e6fSCosmin Samoila 
79147a70e6fSCosmin Samoila static struct platform_driver fsl_micfil_driver = {
79247a70e6fSCosmin Samoila 	.probe = fsl_micfil_probe,
79347a70e6fSCosmin Samoila 	.driver = {
79447a70e6fSCosmin Samoila 		.name = "fsl-micfil-dai",
79547a70e6fSCosmin Samoila 		.pm = &fsl_micfil_pm_ops,
79647a70e6fSCosmin Samoila 		.of_match_table = fsl_micfil_dt_ids,
79747a70e6fSCosmin Samoila 	},
79847a70e6fSCosmin Samoila };
79947a70e6fSCosmin Samoila module_platform_driver(fsl_micfil_driver);
80047a70e6fSCosmin Samoila 
80147a70e6fSCosmin Samoila MODULE_AUTHOR("Cosmin-Gabriel Samoila <cosmin.samoila@nxp.com>");
80247a70e6fSCosmin Samoila MODULE_DESCRIPTION("NXP PDM Microphone Interface (MICFIL) driver");
80347a70e6fSCosmin Samoila MODULE_LICENSE("GPL v2");
804