xref: /openbmc/linux/sound/soc/fsl/fsl_micfil.c (revision 292709b9cf3ba470af94b62c9bb60284cc581b79)
147a70e6fSCosmin Samoila // SPDX-License-Identifier: GPL-2.0
247a70e6fSCosmin Samoila // Copyright 2018 NXP
347a70e6fSCosmin Samoila 
447a70e6fSCosmin Samoila #include <linux/clk.h>
547a70e6fSCosmin Samoila #include <linux/device.h>
647a70e6fSCosmin Samoila #include <linux/interrupt.h>
747a70e6fSCosmin Samoila #include <linux/kobject.h>
847a70e6fSCosmin Samoila #include <linux/kernel.h>
947a70e6fSCosmin Samoila #include <linux/module.h>
1047a70e6fSCosmin Samoila #include <linux/of.h>
1147a70e6fSCosmin Samoila #include <linux/of_address.h>
1247a70e6fSCosmin Samoila #include <linux/of_irq.h>
1347a70e6fSCosmin Samoila #include <linux/of_platform.h>
1447a70e6fSCosmin Samoila #include <linux/pm_runtime.h>
1547a70e6fSCosmin Samoila #include <linux/regmap.h>
1647a70e6fSCosmin Samoila #include <linux/sysfs.h>
1747a70e6fSCosmin Samoila #include <linux/types.h>
1847a70e6fSCosmin Samoila #include <sound/dmaengine_pcm.h>
1947a70e6fSCosmin Samoila #include <sound/pcm.h>
2047a70e6fSCosmin Samoila #include <sound/soc.h>
2147a70e6fSCosmin Samoila #include <sound/tlv.h>
2247a70e6fSCosmin Samoila #include <sound/core.h>
2347a70e6fSCosmin Samoila 
2447a70e6fSCosmin Samoila #include "fsl_micfil.h"
2547a70e6fSCosmin Samoila #include "imx-pcm.h"
2647a70e6fSCosmin Samoila 
2747a70e6fSCosmin Samoila #define FSL_MICFIL_RATES		SNDRV_PCM_RATE_8000_48000
2847a70e6fSCosmin Samoila #define FSL_MICFIL_FORMATS		(SNDRV_PCM_FMTBIT_S16_LE)
2947a70e6fSCosmin Samoila 
3047a70e6fSCosmin Samoila struct fsl_micfil {
3147a70e6fSCosmin Samoila 	struct platform_device *pdev;
3247a70e6fSCosmin Samoila 	struct regmap *regmap;
3347a70e6fSCosmin Samoila 	const struct fsl_micfil_soc_data *soc;
34b5cf28f7SShengjiu Wang 	struct clk *busclk;
3547a70e6fSCosmin Samoila 	struct clk *mclk;
3647a70e6fSCosmin Samoila 	struct snd_dmaengine_dai_dma_data dma_params_rx;
3747a70e6fSCosmin Samoila 	unsigned int dataline;
3847a70e6fSCosmin Samoila 	char name[32];
3947a70e6fSCosmin Samoila 	int irq[MICFIL_IRQ_LINES];
4047a70e6fSCosmin Samoila 	unsigned int mclk_streams;
4147a70e6fSCosmin Samoila 	int quality;	/*QUALITY 2-0 bits */
4247a70e6fSCosmin Samoila 	bool slave_mode;
4347a70e6fSCosmin Samoila 	int channel_gain[8];
4447a70e6fSCosmin Samoila };
4547a70e6fSCosmin Samoila 
4647a70e6fSCosmin Samoila struct fsl_micfil_soc_data {
4747a70e6fSCosmin Samoila 	unsigned int fifos;
4847a70e6fSCosmin Samoila 	unsigned int fifo_depth;
4947a70e6fSCosmin Samoila 	unsigned int dataline;
5047a70e6fSCosmin Samoila 	bool imx;
5147a70e6fSCosmin Samoila };
5247a70e6fSCosmin Samoila 
5347a70e6fSCosmin Samoila static struct fsl_micfil_soc_data fsl_micfil_imx8mm = {
5447a70e6fSCosmin Samoila 	.imx = true,
5547a70e6fSCosmin Samoila 	.fifos = 8,
5647a70e6fSCosmin Samoila 	.fifo_depth = 8,
5747a70e6fSCosmin Samoila 	.dataline =  0xf,
5847a70e6fSCosmin Samoila };
5947a70e6fSCosmin Samoila 
6047a70e6fSCosmin Samoila static const struct of_device_id fsl_micfil_dt_ids[] = {
6147a70e6fSCosmin Samoila 	{ .compatible = "fsl,imx8mm-micfil", .data = &fsl_micfil_imx8mm },
6247a70e6fSCosmin Samoila 	{}
6347a70e6fSCosmin Samoila };
6447a70e6fSCosmin Samoila MODULE_DEVICE_TABLE(of, fsl_micfil_dt_ids);
6547a70e6fSCosmin Samoila 
6647a70e6fSCosmin Samoila /* Table 5. Quality Modes
6747a70e6fSCosmin Samoila  * Medium	0 0 0
6847a70e6fSCosmin Samoila  * High		0 0 1
6947a70e6fSCosmin Samoila  * Very Low 2	1 0 0
7047a70e6fSCosmin Samoila  * Very Low 1	1 0 1
7147a70e6fSCosmin Samoila  * Very Low 0	1 1 0
7247a70e6fSCosmin Samoila  * Low		1 1 1
7347a70e6fSCosmin Samoila  */
7447a70e6fSCosmin Samoila static const char * const micfil_quality_select_texts[] = {
7547a70e6fSCosmin Samoila 	"Medium", "High",
7647a70e6fSCosmin Samoila 	"N/A", "N/A",
7747a70e6fSCosmin Samoila 	"VLow2", "VLow1",
7847a70e6fSCosmin Samoila 	"VLow0", "Low",
7947a70e6fSCosmin Samoila };
8047a70e6fSCosmin Samoila 
8147a70e6fSCosmin Samoila static const struct soc_enum fsl_micfil_quality_enum =
8247a70e6fSCosmin Samoila 	SOC_ENUM_SINGLE(REG_MICFIL_CTRL2,
8347a70e6fSCosmin Samoila 			MICFIL_CTRL2_QSEL_SHIFT,
8447a70e6fSCosmin Samoila 			ARRAY_SIZE(micfil_quality_select_texts),
8547a70e6fSCosmin Samoila 			micfil_quality_select_texts);
8647a70e6fSCosmin Samoila 
8747a70e6fSCosmin Samoila static DECLARE_TLV_DB_SCALE(gain_tlv, 0, 100, 0);
8847a70e6fSCosmin Samoila 
8947a70e6fSCosmin Samoila static const struct snd_kcontrol_new fsl_micfil_snd_controls[] = {
9047a70e6fSCosmin Samoila 	SOC_SINGLE_SX_TLV("CH0 Volume", REG_MICFIL_OUT_CTRL,
9147a70e6fSCosmin Samoila 			  MICFIL_OUTGAIN_CHX_SHIFT(0), 0xF, 0x7, gain_tlv),
9247a70e6fSCosmin Samoila 	SOC_SINGLE_SX_TLV("CH1 Volume", REG_MICFIL_OUT_CTRL,
9347a70e6fSCosmin Samoila 			  MICFIL_OUTGAIN_CHX_SHIFT(1), 0xF, 0x7, gain_tlv),
9447a70e6fSCosmin Samoila 	SOC_SINGLE_SX_TLV("CH2 Volume", REG_MICFIL_OUT_CTRL,
9547a70e6fSCosmin Samoila 			  MICFIL_OUTGAIN_CHX_SHIFT(2), 0xF, 0x7, gain_tlv),
9647a70e6fSCosmin Samoila 	SOC_SINGLE_SX_TLV("CH3 Volume", REG_MICFIL_OUT_CTRL,
9747a70e6fSCosmin Samoila 			  MICFIL_OUTGAIN_CHX_SHIFT(3), 0xF, 0x7, gain_tlv),
9847a70e6fSCosmin Samoila 	SOC_SINGLE_SX_TLV("CH4 Volume", REG_MICFIL_OUT_CTRL,
9947a70e6fSCosmin Samoila 			  MICFIL_OUTGAIN_CHX_SHIFT(4), 0xF, 0x7, gain_tlv),
10047a70e6fSCosmin Samoila 	SOC_SINGLE_SX_TLV("CH5 Volume", REG_MICFIL_OUT_CTRL,
10147a70e6fSCosmin Samoila 			  MICFIL_OUTGAIN_CHX_SHIFT(5), 0xF, 0x7, gain_tlv),
10247a70e6fSCosmin Samoila 	SOC_SINGLE_SX_TLV("CH6 Volume", REG_MICFIL_OUT_CTRL,
10347a70e6fSCosmin Samoila 			  MICFIL_OUTGAIN_CHX_SHIFT(6), 0xF, 0x7, gain_tlv),
10447a70e6fSCosmin Samoila 	SOC_SINGLE_SX_TLV("CH7 Volume", REG_MICFIL_OUT_CTRL,
10547a70e6fSCosmin Samoila 			  MICFIL_OUTGAIN_CHX_SHIFT(7), 0xF, 0x7, gain_tlv),
10647a70e6fSCosmin Samoila 	SOC_ENUM_EXT("MICFIL Quality Select",
10747a70e6fSCosmin Samoila 		     fsl_micfil_quality_enum,
10847a70e6fSCosmin Samoila 		     snd_soc_get_enum_double, snd_soc_put_enum_double),
10947a70e6fSCosmin Samoila };
11047a70e6fSCosmin Samoila 
11147a70e6fSCosmin Samoila static inline int get_pdm_clk(struct fsl_micfil *micfil,
11247a70e6fSCosmin Samoila 			      unsigned int rate)
11347a70e6fSCosmin Samoila {
11447a70e6fSCosmin Samoila 	u32 ctrl2_reg;
11547a70e6fSCosmin Samoila 	int qsel, osr;
11647a70e6fSCosmin Samoila 	int bclk;
11747a70e6fSCosmin Samoila 
11847a70e6fSCosmin Samoila 	regmap_read(micfil->regmap, REG_MICFIL_CTRL2, &ctrl2_reg);
11947a70e6fSCosmin Samoila 	osr = 16 - ((ctrl2_reg & MICFIL_CTRL2_CICOSR_MASK)
12047a70e6fSCosmin Samoila 		    >> MICFIL_CTRL2_CICOSR_SHIFT);
12147a70e6fSCosmin Samoila 
12247a70e6fSCosmin Samoila 	regmap_read(micfil->regmap, REG_MICFIL_CTRL2, &ctrl2_reg);
12347a70e6fSCosmin Samoila 	qsel = ctrl2_reg & MICFIL_CTRL2_QSEL_MASK;
12447a70e6fSCosmin Samoila 
12547a70e6fSCosmin Samoila 	switch (qsel) {
12647a70e6fSCosmin Samoila 	case MICFIL_HIGH_QUALITY:
12747a70e6fSCosmin Samoila 		bclk = rate * 8 * osr / 2; /* kfactor = 0.5 */
12847a70e6fSCosmin Samoila 		break;
12947a70e6fSCosmin Samoila 	case MICFIL_MEDIUM_QUALITY:
13047a70e6fSCosmin Samoila 	case MICFIL_VLOW0_QUALITY:
13147a70e6fSCosmin Samoila 		bclk = rate * 4 * osr * 1; /* kfactor = 1 */
13247a70e6fSCosmin Samoila 		break;
13347a70e6fSCosmin Samoila 	case MICFIL_LOW_QUALITY:
13447a70e6fSCosmin Samoila 	case MICFIL_VLOW1_QUALITY:
13547a70e6fSCosmin Samoila 		bclk = rate * 2 * osr * 2; /* kfactor = 2 */
13647a70e6fSCosmin Samoila 		break;
13747a70e6fSCosmin Samoila 	case MICFIL_VLOW2_QUALITY:
13847a70e6fSCosmin Samoila 		bclk = rate * osr * 4; /* kfactor = 4 */
13947a70e6fSCosmin Samoila 		break;
14047a70e6fSCosmin Samoila 	default:
14147a70e6fSCosmin Samoila 		dev_err(&micfil->pdev->dev,
14247a70e6fSCosmin Samoila 			"Please make sure you select a valid quality.\n");
14347a70e6fSCosmin Samoila 		bclk = -1;
14447a70e6fSCosmin Samoila 		break;
14547a70e6fSCosmin Samoila 	}
14647a70e6fSCosmin Samoila 
14747a70e6fSCosmin Samoila 	return bclk;
14847a70e6fSCosmin Samoila }
14947a70e6fSCosmin Samoila 
15047a70e6fSCosmin Samoila static inline int get_clk_div(struct fsl_micfil *micfil,
15147a70e6fSCosmin Samoila 			      unsigned int rate)
15247a70e6fSCosmin Samoila {
15347a70e6fSCosmin Samoila 	u32 ctrl2_reg;
15447a70e6fSCosmin Samoila 	long mclk_rate;
15547a70e6fSCosmin Samoila 	int clk_div;
15647a70e6fSCosmin Samoila 
15747a70e6fSCosmin Samoila 	regmap_read(micfil->regmap, REG_MICFIL_CTRL2, &ctrl2_reg);
15847a70e6fSCosmin Samoila 
15947a70e6fSCosmin Samoila 	mclk_rate = clk_get_rate(micfil->mclk);
16047a70e6fSCosmin Samoila 
16147a70e6fSCosmin Samoila 	clk_div = mclk_rate / (get_pdm_clk(micfil, rate) * 2);
16247a70e6fSCosmin Samoila 
16347a70e6fSCosmin Samoila 	return clk_div;
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 
17647a70e6fSCosmin Samoila 	ret = regmap_update_bits(micfil->regmap,
17747a70e6fSCosmin Samoila 				 REG_MICFIL_CTRL1,
17847a70e6fSCosmin Samoila 				 MICFIL_CTRL1_MDIS_MASK,
17947a70e6fSCosmin Samoila 				 0);
18047a70e6fSCosmin Samoila 	if (ret) {
18147a70e6fSCosmin Samoila 		dev_err(dev, "failed to clear MDIS bit %d\n", ret);
18247a70e6fSCosmin Samoila 		return ret;
18347a70e6fSCosmin Samoila 	}
18447a70e6fSCosmin Samoila 
18547a70e6fSCosmin Samoila 	ret = regmap_update_bits(micfil->regmap,
18647a70e6fSCosmin Samoila 				 REG_MICFIL_CTRL1,
18747a70e6fSCosmin Samoila 				 MICFIL_CTRL1_SRES_MASK,
18847a70e6fSCosmin Samoila 				 MICFIL_CTRL1_SRES);
18947a70e6fSCosmin Samoila 	if (ret) {
19047a70e6fSCosmin Samoila 		dev_err(dev, "failed to reset MICFIL: %d\n", ret);
19147a70e6fSCosmin Samoila 		return ret;
19247a70e6fSCosmin Samoila 	}
19347a70e6fSCosmin Samoila 
194*292709b9SShengjiu Wang 	/*
195*292709b9SShengjiu Wang 	 * SRES is self-cleared bit, but REG_MICFIL_CTRL1 is defined
196*292709b9SShengjiu Wang 	 * as non-volatile register, so SRES still remain in regmap
197*292709b9SShengjiu Wang 	 * cache after set, that every update of REG_MICFIL_CTRL1,
198*292709b9SShengjiu Wang 	 * software reset happens. so clear it explicitly.
199*292709b9SShengjiu Wang 	 */
200*292709b9SShengjiu Wang 	ret = regmap_clear_bits(micfil->regmap, REG_MICFIL_CTRL1,
201*292709b9SShengjiu Wang 				MICFIL_CTRL1_SRES);
202*292709b9SShengjiu Wang 	if (ret)
203*292709b9SShengjiu Wang 		return ret;
204*292709b9SShengjiu Wang 
20547a70e6fSCosmin Samoila 	return 0;
20647a70e6fSCosmin Samoila }
20747a70e6fSCosmin Samoila 
20847a70e6fSCosmin Samoila static int fsl_micfil_set_mclk_rate(struct fsl_micfil *micfil,
20947a70e6fSCosmin Samoila 				    unsigned int freq)
21047a70e6fSCosmin Samoila {
21147a70e6fSCosmin Samoila 	struct device *dev = &micfil->pdev->dev;
21247a70e6fSCosmin Samoila 	int ret;
21347a70e6fSCosmin Samoila 
21447a70e6fSCosmin Samoila 	clk_disable_unprepare(micfil->mclk);
21547a70e6fSCosmin Samoila 
21647a70e6fSCosmin Samoila 	ret = clk_set_rate(micfil->mclk, freq * 1024);
21747a70e6fSCosmin Samoila 	if (ret)
21847a70e6fSCosmin Samoila 		dev_warn(dev, "failed to set rate (%u): %d\n",
21947a70e6fSCosmin Samoila 			 freq * 1024, ret);
22047a70e6fSCosmin Samoila 
22147a70e6fSCosmin Samoila 	clk_prepare_enable(micfil->mclk);
22247a70e6fSCosmin Samoila 
22347a70e6fSCosmin Samoila 	return ret;
22447a70e6fSCosmin Samoila }
22547a70e6fSCosmin Samoila 
22647a70e6fSCosmin Samoila static int fsl_micfil_startup(struct snd_pcm_substream *substream,
22747a70e6fSCosmin Samoila 			      struct snd_soc_dai *dai)
22847a70e6fSCosmin Samoila {
22947a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = snd_soc_dai_get_drvdata(dai);
23047a70e6fSCosmin Samoila 
23147a70e6fSCosmin Samoila 	if (!micfil) {
23211106cb3STang Bin 		dev_err(dai->dev, "micfil dai priv_data not set\n");
23347a70e6fSCosmin Samoila 		return -EINVAL;
23447a70e6fSCosmin Samoila 	}
23547a70e6fSCosmin Samoila 
23647a70e6fSCosmin Samoila 	return 0;
23747a70e6fSCosmin Samoila }
23847a70e6fSCosmin Samoila 
23947a70e6fSCosmin Samoila static int fsl_micfil_trigger(struct snd_pcm_substream *substream, int cmd,
24047a70e6fSCosmin Samoila 			      struct snd_soc_dai *dai)
24147a70e6fSCosmin Samoila {
24247a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = snd_soc_dai_get_drvdata(dai);
24347a70e6fSCosmin Samoila 	struct device *dev = &micfil->pdev->dev;
24447a70e6fSCosmin Samoila 	int ret;
24547a70e6fSCosmin Samoila 
24647a70e6fSCosmin Samoila 	switch (cmd) {
24747a70e6fSCosmin Samoila 	case SNDRV_PCM_TRIGGER_START:
24847a70e6fSCosmin Samoila 	case SNDRV_PCM_TRIGGER_RESUME:
24947a70e6fSCosmin Samoila 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
25047a70e6fSCosmin Samoila 		ret = fsl_micfil_reset(dev);
25147a70e6fSCosmin Samoila 		if (ret) {
25247a70e6fSCosmin Samoila 			dev_err(dev, "failed to soft reset\n");
25347a70e6fSCosmin Samoila 			return ret;
25447a70e6fSCosmin Samoila 		}
25547a70e6fSCosmin Samoila 
25647a70e6fSCosmin Samoila 		/* DMA Interrupt Selection - DISEL bits
25747a70e6fSCosmin Samoila 		 * 00 - DMA and IRQ disabled
25847a70e6fSCosmin Samoila 		 * 01 - DMA req enabled
25947a70e6fSCosmin Samoila 		 * 10 - IRQ enabled
26047a70e6fSCosmin Samoila 		 * 11 - reserved
26147a70e6fSCosmin Samoila 		 */
26247a70e6fSCosmin Samoila 		ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL1,
26347a70e6fSCosmin Samoila 					 MICFIL_CTRL1_DISEL_MASK,
26447a70e6fSCosmin Samoila 					 (1 << MICFIL_CTRL1_DISEL_SHIFT));
26547a70e6fSCosmin Samoila 		if (ret) {
26647a70e6fSCosmin Samoila 			dev_err(dev, "failed to update DISEL bits\n");
26747a70e6fSCosmin Samoila 			return ret;
26847a70e6fSCosmin Samoila 		}
26947a70e6fSCosmin Samoila 
27047a70e6fSCosmin Samoila 		/* Enable the module */
27147a70e6fSCosmin Samoila 		ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL1,
27247a70e6fSCosmin Samoila 					 MICFIL_CTRL1_PDMIEN_MASK,
27347a70e6fSCosmin Samoila 					 MICFIL_CTRL1_PDMIEN);
27447a70e6fSCosmin Samoila 		if (ret) {
27547a70e6fSCosmin Samoila 			dev_err(dev, "failed to enable the module\n");
27647a70e6fSCosmin Samoila 			return ret;
27747a70e6fSCosmin Samoila 		}
27847a70e6fSCosmin Samoila 
27947a70e6fSCosmin Samoila 		break;
28047a70e6fSCosmin Samoila 	case SNDRV_PCM_TRIGGER_STOP:
28147a70e6fSCosmin Samoila 	case SNDRV_PCM_TRIGGER_SUSPEND:
28247a70e6fSCosmin Samoila 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
28347a70e6fSCosmin Samoila 		/* Disable the module */
28447a70e6fSCosmin Samoila 		ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL1,
28547a70e6fSCosmin Samoila 					 MICFIL_CTRL1_PDMIEN_MASK,
28647a70e6fSCosmin Samoila 					 0);
28747a70e6fSCosmin Samoila 		if (ret) {
28847a70e6fSCosmin Samoila 			dev_err(dev, "failed to enable the module\n");
28947a70e6fSCosmin Samoila 			return ret;
29047a70e6fSCosmin Samoila 		}
29147a70e6fSCosmin Samoila 
29247a70e6fSCosmin Samoila 		ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL1,
29347a70e6fSCosmin Samoila 					 MICFIL_CTRL1_DISEL_MASK,
29447a70e6fSCosmin Samoila 					 (0 << MICFIL_CTRL1_DISEL_SHIFT));
29547a70e6fSCosmin Samoila 		if (ret) {
29647a70e6fSCosmin Samoila 			dev_err(dev, "failed to update DISEL bits\n");
29747a70e6fSCosmin Samoila 			return ret;
29847a70e6fSCosmin Samoila 		}
29947a70e6fSCosmin Samoila 		break;
30047a70e6fSCosmin Samoila 	default:
30147a70e6fSCosmin Samoila 		return -EINVAL;
30247a70e6fSCosmin Samoila 	}
30347a70e6fSCosmin Samoila 	return 0;
30447a70e6fSCosmin Samoila }
30547a70e6fSCosmin Samoila 
30647a70e6fSCosmin Samoila static int fsl_set_clock_params(struct device *dev, unsigned int rate)
30747a70e6fSCosmin Samoila {
30847a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = dev_get_drvdata(dev);
30947a70e6fSCosmin Samoila 	int clk_div;
31015b5c496STang Bin 	int ret;
31147a70e6fSCosmin Samoila 
31247a70e6fSCosmin Samoila 	ret = fsl_micfil_set_mclk_rate(micfil, rate);
31347a70e6fSCosmin Samoila 	if (ret < 0)
31447a70e6fSCosmin Samoila 		dev_err(dev, "failed to set mclk[%lu] to rate %u\n",
31547a70e6fSCosmin Samoila 			clk_get_rate(micfil->mclk), rate);
31647a70e6fSCosmin Samoila 
31747a70e6fSCosmin Samoila 	/* set CICOSR */
31847a70e6fSCosmin Samoila 	ret |= regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL2,
31947a70e6fSCosmin Samoila 				 MICFIL_CTRL2_CICOSR_MASK,
32047a70e6fSCosmin Samoila 				 MICFIL_CTRL2_OSR_DEFAULT);
32147a70e6fSCosmin Samoila 	if (ret)
32247a70e6fSCosmin Samoila 		dev_err(dev, "failed to set CICOSR in reg 0x%X\n",
32347a70e6fSCosmin Samoila 			REG_MICFIL_CTRL2);
32447a70e6fSCosmin Samoila 
32547a70e6fSCosmin Samoila 	/* set CLK_DIV */
32647a70e6fSCosmin Samoila 	clk_div = get_clk_div(micfil, rate);
32747a70e6fSCosmin Samoila 	if (clk_div < 0)
32847a70e6fSCosmin Samoila 		ret = -EINVAL;
32947a70e6fSCosmin Samoila 
33047a70e6fSCosmin Samoila 	ret |= regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL2,
33147a70e6fSCosmin Samoila 				 MICFIL_CTRL2_CLKDIV_MASK, clk_div);
33247a70e6fSCosmin Samoila 	if (ret)
33347a70e6fSCosmin Samoila 		dev_err(dev, "failed to set CLKDIV in reg 0x%X\n",
33447a70e6fSCosmin Samoila 			REG_MICFIL_CTRL2);
33547a70e6fSCosmin Samoila 
33647a70e6fSCosmin Samoila 	return ret;
33747a70e6fSCosmin Samoila }
33847a70e6fSCosmin Samoila 
33947a70e6fSCosmin Samoila static int fsl_micfil_hw_params(struct snd_pcm_substream *substream,
34047a70e6fSCosmin Samoila 				struct snd_pcm_hw_params *params,
34147a70e6fSCosmin Samoila 				struct snd_soc_dai *dai)
34247a70e6fSCosmin Samoila {
34347a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = snd_soc_dai_get_drvdata(dai);
34447a70e6fSCosmin Samoila 	unsigned int channels = params_channels(params);
34547a70e6fSCosmin Samoila 	unsigned int rate = params_rate(params);
34647a70e6fSCosmin Samoila 	struct device *dev = &micfil->pdev->dev;
34747a70e6fSCosmin Samoila 	int ret;
34847a70e6fSCosmin Samoila 
34947a70e6fSCosmin Samoila 	/* 1. Disable the module */
35047a70e6fSCosmin Samoila 	ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL1,
35147a70e6fSCosmin Samoila 				 MICFIL_CTRL1_PDMIEN_MASK, 0);
35247a70e6fSCosmin Samoila 	if (ret) {
35347a70e6fSCosmin Samoila 		dev_err(dev, "failed to disable the module\n");
35447a70e6fSCosmin Samoila 		return ret;
35547a70e6fSCosmin Samoila 	}
35647a70e6fSCosmin Samoila 
35747a70e6fSCosmin Samoila 	/* enable channels */
35847a70e6fSCosmin Samoila 	ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL1,
35947a70e6fSCosmin Samoila 				 0xFF, ((1 << channels) - 1));
36047a70e6fSCosmin Samoila 	if (ret) {
36147a70e6fSCosmin Samoila 		dev_err(dev, "failed to enable channels %d, reg 0x%X\n", ret,
36247a70e6fSCosmin Samoila 			REG_MICFIL_CTRL1);
36347a70e6fSCosmin Samoila 		return ret;
36447a70e6fSCosmin Samoila 	}
36547a70e6fSCosmin Samoila 
36647a70e6fSCosmin Samoila 	ret = fsl_set_clock_params(dev, rate);
36747a70e6fSCosmin Samoila 	if (ret < 0) {
36847a70e6fSCosmin Samoila 		dev_err(dev, "Failed to set clock parameters [%d]\n", ret);
36947a70e6fSCosmin Samoila 		return ret;
37047a70e6fSCosmin Samoila 	}
37147a70e6fSCosmin Samoila 
37247a70e6fSCosmin Samoila 	micfil->dma_params_rx.maxburst = channels * MICFIL_DMA_MAXBURST_RX;
37347a70e6fSCosmin Samoila 
37447a70e6fSCosmin Samoila 	return 0;
37547a70e6fSCosmin Samoila }
37647a70e6fSCosmin Samoila 
37747a70e6fSCosmin Samoila static int fsl_micfil_set_dai_sysclk(struct snd_soc_dai *dai, int clk_id,
37847a70e6fSCosmin Samoila 				     unsigned int freq, int dir)
37947a70e6fSCosmin Samoila {
38047a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = snd_soc_dai_get_drvdata(dai);
38147a70e6fSCosmin Samoila 	struct device *dev = &micfil->pdev->dev;
38247a70e6fSCosmin Samoila 
38347a70e6fSCosmin Samoila 	int ret;
38447a70e6fSCosmin Samoila 
38547a70e6fSCosmin Samoila 	if (!freq)
38647a70e6fSCosmin Samoila 		return 0;
38747a70e6fSCosmin Samoila 
38847a70e6fSCosmin Samoila 	ret = fsl_micfil_set_mclk_rate(micfil, freq);
38947a70e6fSCosmin Samoila 	if (ret < 0)
39047a70e6fSCosmin Samoila 		dev_err(dev, "failed to set mclk[%lu] to rate %u\n",
39147a70e6fSCosmin Samoila 			clk_get_rate(micfil->mclk), freq);
39247a70e6fSCosmin Samoila 
39347a70e6fSCosmin Samoila 	return ret;
39447a70e6fSCosmin Samoila }
39547a70e6fSCosmin Samoila 
39638d89a56SRikard Falkeborn static const struct snd_soc_dai_ops fsl_micfil_dai_ops = {
39747a70e6fSCosmin Samoila 	.startup = fsl_micfil_startup,
39847a70e6fSCosmin Samoila 	.trigger = fsl_micfil_trigger,
39947a70e6fSCosmin Samoila 	.hw_params = fsl_micfil_hw_params,
40047a70e6fSCosmin Samoila 	.set_sysclk = fsl_micfil_set_dai_sysclk,
40147a70e6fSCosmin Samoila };
40247a70e6fSCosmin Samoila 
40347a70e6fSCosmin Samoila static int fsl_micfil_dai_probe(struct snd_soc_dai *cpu_dai)
40447a70e6fSCosmin Samoila {
40547a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = dev_get_drvdata(cpu_dai->dev);
40647a70e6fSCosmin Samoila 	struct device *dev = cpu_dai->dev;
40747a70e6fSCosmin Samoila 	unsigned int val;
40847a70e6fSCosmin Samoila 	int ret;
40947a70e6fSCosmin Samoila 	int i;
41047a70e6fSCosmin Samoila 
41147a70e6fSCosmin Samoila 	/* set qsel to medium */
41247a70e6fSCosmin Samoila 	ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL2,
41347a70e6fSCosmin Samoila 				 MICFIL_CTRL2_QSEL_MASK, MICFIL_MEDIUM_QUALITY);
41447a70e6fSCosmin Samoila 	if (ret) {
41547a70e6fSCosmin Samoila 		dev_err(dev, "failed to set quality mode bits, reg 0x%X\n",
41647a70e6fSCosmin Samoila 			REG_MICFIL_CTRL2);
41747a70e6fSCosmin Samoila 		return ret;
41847a70e6fSCosmin Samoila 	}
41947a70e6fSCosmin Samoila 
42047a70e6fSCosmin Samoila 	/* set default gain to max_gain */
42147a70e6fSCosmin Samoila 	regmap_write(micfil->regmap, REG_MICFIL_OUT_CTRL, 0x77777777);
42247a70e6fSCosmin Samoila 	for (i = 0; i < 8; i++)
42347a70e6fSCosmin Samoila 		micfil->channel_gain[i] = 0xF;
42447a70e6fSCosmin Samoila 
42547a70e6fSCosmin Samoila 	snd_soc_dai_init_dma_data(cpu_dai, NULL,
42647a70e6fSCosmin Samoila 				  &micfil->dma_params_rx);
42747a70e6fSCosmin Samoila 
42847a70e6fSCosmin Samoila 	/* FIFO Watermark Control - FIFOWMK*/
42947a70e6fSCosmin Samoila 	val = MICFIL_FIFO_CTRL_FIFOWMK(micfil->soc->fifo_depth) - 1;
43047a70e6fSCosmin Samoila 	ret = regmap_update_bits(micfil->regmap, REG_MICFIL_FIFO_CTRL,
43147a70e6fSCosmin Samoila 				 MICFIL_FIFO_CTRL_FIFOWMK_MASK,
43247a70e6fSCosmin Samoila 				 val);
43347a70e6fSCosmin Samoila 	if (ret) {
43447a70e6fSCosmin Samoila 		dev_err(dev, "failed to set FIFOWMK\n");
43547a70e6fSCosmin Samoila 		return ret;
43647a70e6fSCosmin Samoila 	}
43747a70e6fSCosmin Samoila 
43847a70e6fSCosmin Samoila 	return 0;
43947a70e6fSCosmin Samoila }
44047a70e6fSCosmin Samoila 
44147a70e6fSCosmin Samoila static struct snd_soc_dai_driver fsl_micfil_dai = {
44247a70e6fSCosmin Samoila 	.probe = fsl_micfil_dai_probe,
44347a70e6fSCosmin Samoila 	.capture = {
44447a70e6fSCosmin Samoila 		.stream_name = "CPU-Capture",
44547a70e6fSCosmin Samoila 		.channels_min = 1,
44647a70e6fSCosmin Samoila 		.channels_max = 8,
44747a70e6fSCosmin Samoila 		.rates = FSL_MICFIL_RATES,
44847a70e6fSCosmin Samoila 		.formats = FSL_MICFIL_FORMATS,
44947a70e6fSCosmin Samoila 	},
45047a70e6fSCosmin Samoila 	.ops = &fsl_micfil_dai_ops,
45147a70e6fSCosmin Samoila };
45247a70e6fSCosmin Samoila 
45347a70e6fSCosmin Samoila static const struct snd_soc_component_driver fsl_micfil_component = {
45447a70e6fSCosmin Samoila 	.name		= "fsl-micfil-dai",
45547a70e6fSCosmin Samoila 	.controls       = fsl_micfil_snd_controls,
45647a70e6fSCosmin Samoila 	.num_controls   = ARRAY_SIZE(fsl_micfil_snd_controls),
45747a70e6fSCosmin Samoila 
45847a70e6fSCosmin Samoila };
45947a70e6fSCosmin Samoila 
46047a70e6fSCosmin Samoila /* REGMAP */
46147a70e6fSCosmin Samoila static const struct reg_default fsl_micfil_reg_defaults[] = {
46247a70e6fSCosmin Samoila 	{REG_MICFIL_CTRL1,		0x00000000},
46347a70e6fSCosmin Samoila 	{REG_MICFIL_CTRL2,		0x00000000},
46447a70e6fSCosmin Samoila 	{REG_MICFIL_STAT,		0x00000000},
46547a70e6fSCosmin Samoila 	{REG_MICFIL_FIFO_CTRL,		0x00000007},
46647a70e6fSCosmin Samoila 	{REG_MICFIL_FIFO_STAT,		0x00000000},
46747a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH0,		0x00000000},
46847a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH1,		0x00000000},
46947a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH2,		0x00000000},
47047a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH3,		0x00000000},
47147a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH4,		0x00000000},
47247a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH5,		0x00000000},
47347a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH6,		0x00000000},
47447a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH7,		0x00000000},
47547a70e6fSCosmin Samoila 	{REG_MICFIL_DC_CTRL,		0x00000000},
47647a70e6fSCosmin Samoila 	{REG_MICFIL_OUT_CTRL,		0x00000000},
47747a70e6fSCosmin Samoila 	{REG_MICFIL_OUT_STAT,		0x00000000},
47847a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_CTRL1,		0x00000000},
47947a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_CTRL2,		0x000A0000},
48047a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_STAT,		0x00000000},
48147a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_SCONFIG,	0x00000000},
48247a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_NCONFIG,	0x80000000},
48347a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_NDATA,		0x00000000},
48447a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_ZCD,		0x00000004},
48547a70e6fSCosmin Samoila };
48647a70e6fSCosmin Samoila 
48747a70e6fSCosmin Samoila static bool fsl_micfil_readable_reg(struct device *dev, unsigned int reg)
48847a70e6fSCosmin Samoila {
48947a70e6fSCosmin Samoila 	switch (reg) {
49047a70e6fSCosmin Samoila 	case REG_MICFIL_CTRL1:
49147a70e6fSCosmin Samoila 	case REG_MICFIL_CTRL2:
49247a70e6fSCosmin Samoila 	case REG_MICFIL_STAT:
49347a70e6fSCosmin Samoila 	case REG_MICFIL_FIFO_CTRL:
49447a70e6fSCosmin Samoila 	case REG_MICFIL_FIFO_STAT:
49547a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH0:
49647a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH1:
49747a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH2:
49847a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH3:
49947a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH4:
50047a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH5:
50147a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH6:
50247a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH7:
50347a70e6fSCosmin Samoila 	case REG_MICFIL_DC_CTRL:
50447a70e6fSCosmin Samoila 	case REG_MICFIL_OUT_CTRL:
50547a70e6fSCosmin Samoila 	case REG_MICFIL_OUT_STAT:
50647a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_CTRL1:
50747a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_CTRL2:
50847a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_STAT:
50947a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_SCONFIG:
51047a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_NCONFIG:
51147a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_NDATA:
51247a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_ZCD:
51347a70e6fSCosmin Samoila 		return true;
51447a70e6fSCosmin Samoila 	default:
51547a70e6fSCosmin Samoila 		return false;
51647a70e6fSCosmin Samoila 	}
51747a70e6fSCosmin Samoila }
51847a70e6fSCosmin Samoila 
51947a70e6fSCosmin Samoila static bool fsl_micfil_writeable_reg(struct device *dev, unsigned int reg)
52047a70e6fSCosmin Samoila {
52147a70e6fSCosmin Samoila 	switch (reg) {
52247a70e6fSCosmin Samoila 	case REG_MICFIL_CTRL1:
52347a70e6fSCosmin Samoila 	case REG_MICFIL_CTRL2:
52447a70e6fSCosmin Samoila 	case REG_MICFIL_STAT:		/* Write 1 to Clear */
52547a70e6fSCosmin Samoila 	case REG_MICFIL_FIFO_CTRL:
52647a70e6fSCosmin Samoila 	case REG_MICFIL_FIFO_STAT:	/* Write 1 to Clear */
52747a70e6fSCosmin Samoila 	case REG_MICFIL_DC_CTRL:
52847a70e6fSCosmin Samoila 	case REG_MICFIL_OUT_CTRL:
52947a70e6fSCosmin Samoila 	case REG_MICFIL_OUT_STAT:	/* Write 1 to Clear */
53047a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_CTRL1:
53147a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_CTRL2:
53247a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_STAT:	/* Write 1 to Clear */
53347a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_SCONFIG:
53447a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_NCONFIG:
53547a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_ZCD:
53647a70e6fSCosmin Samoila 		return true;
53747a70e6fSCosmin Samoila 	default:
53847a70e6fSCosmin Samoila 		return false;
53947a70e6fSCosmin Samoila 	}
54047a70e6fSCosmin Samoila }
54147a70e6fSCosmin Samoila 
54247a70e6fSCosmin Samoila static bool fsl_micfil_volatile_reg(struct device *dev, unsigned int reg)
54347a70e6fSCosmin Samoila {
54447a70e6fSCosmin Samoila 	switch (reg) {
54547a70e6fSCosmin Samoila 	case REG_MICFIL_STAT:
54647a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH0:
54747a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH1:
54847a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH2:
54947a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH3:
55047a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH4:
55147a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH5:
55247a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH6:
55347a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH7:
55447a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_STAT:
55547a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_NDATA:
55647a70e6fSCosmin Samoila 		return true;
55747a70e6fSCosmin Samoila 	default:
55847a70e6fSCosmin Samoila 		return false;
55947a70e6fSCosmin Samoila 	}
56047a70e6fSCosmin Samoila }
56147a70e6fSCosmin Samoila 
56247a70e6fSCosmin Samoila static const struct regmap_config fsl_micfil_regmap_config = {
56347a70e6fSCosmin Samoila 	.reg_bits = 32,
56447a70e6fSCosmin Samoila 	.reg_stride = 4,
56547a70e6fSCosmin Samoila 	.val_bits = 32,
56647a70e6fSCosmin Samoila 
56747a70e6fSCosmin Samoila 	.max_register = REG_MICFIL_VAD0_ZCD,
56847a70e6fSCosmin Samoila 	.reg_defaults = fsl_micfil_reg_defaults,
56947a70e6fSCosmin Samoila 	.num_reg_defaults = ARRAY_SIZE(fsl_micfil_reg_defaults),
57047a70e6fSCosmin Samoila 	.readable_reg = fsl_micfil_readable_reg,
57147a70e6fSCosmin Samoila 	.volatile_reg = fsl_micfil_volatile_reg,
57247a70e6fSCosmin Samoila 	.writeable_reg = fsl_micfil_writeable_reg,
57347a70e6fSCosmin Samoila 	.cache_type = REGCACHE_RBTREE,
57447a70e6fSCosmin Samoila };
57547a70e6fSCosmin Samoila 
57647a70e6fSCosmin Samoila /* END OF REGMAP */
57747a70e6fSCosmin Samoila 
57847a70e6fSCosmin Samoila static irqreturn_t micfil_isr(int irq, void *devid)
57947a70e6fSCosmin Samoila {
58047a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = (struct fsl_micfil *)devid;
58147a70e6fSCosmin Samoila 	struct platform_device *pdev = micfil->pdev;
58247a70e6fSCosmin Samoila 	u32 stat_reg;
58347a70e6fSCosmin Samoila 	u32 fifo_stat_reg;
58447a70e6fSCosmin Samoila 	u32 ctrl1_reg;
58547a70e6fSCosmin Samoila 	bool dma_enabled;
58647a70e6fSCosmin Samoila 	int i;
58747a70e6fSCosmin Samoila 
58847a70e6fSCosmin Samoila 	regmap_read(micfil->regmap, REG_MICFIL_STAT, &stat_reg);
58947a70e6fSCosmin Samoila 	regmap_read(micfil->regmap, REG_MICFIL_CTRL1, &ctrl1_reg);
59047a70e6fSCosmin Samoila 	regmap_read(micfil->regmap, REG_MICFIL_FIFO_STAT, &fifo_stat_reg);
59147a70e6fSCosmin Samoila 
59247a70e6fSCosmin Samoila 	dma_enabled = MICFIL_DMA_ENABLED(ctrl1_reg);
59347a70e6fSCosmin Samoila 
59447a70e6fSCosmin Samoila 	/* Channel 0-7 Output Data Flags */
59547a70e6fSCosmin Samoila 	for (i = 0; i < MICFIL_OUTPUT_CHANNELS; i++) {
59647a70e6fSCosmin Samoila 		if (stat_reg & MICFIL_STAT_CHXF_MASK(i))
59747a70e6fSCosmin Samoila 			dev_dbg(&pdev->dev,
59847a70e6fSCosmin Samoila 				"Data available in Data Channel %d\n", i);
59947a70e6fSCosmin Samoila 		/* if DMA is not enabled, field must be written with 1
60047a70e6fSCosmin Samoila 		 * to clear
60147a70e6fSCosmin Samoila 		 */
60247a70e6fSCosmin Samoila 		if (!dma_enabled)
60347a70e6fSCosmin Samoila 			regmap_write_bits(micfil->regmap,
60447a70e6fSCosmin Samoila 					  REG_MICFIL_STAT,
60547a70e6fSCosmin Samoila 					  MICFIL_STAT_CHXF_MASK(i),
60647a70e6fSCosmin Samoila 					  1);
60747a70e6fSCosmin Samoila 	}
60847a70e6fSCosmin Samoila 
60947a70e6fSCosmin Samoila 	for (i = 0; i < MICFIL_FIFO_NUM; i++) {
61047a70e6fSCosmin Samoila 		if (fifo_stat_reg & MICFIL_FIFO_STAT_FIFOX_OVER_MASK(i))
61147a70e6fSCosmin Samoila 			dev_dbg(&pdev->dev,
61247a70e6fSCosmin Samoila 				"FIFO Overflow Exception flag for channel %d\n",
61347a70e6fSCosmin Samoila 				i);
61447a70e6fSCosmin Samoila 
61547a70e6fSCosmin Samoila 		if (fifo_stat_reg & MICFIL_FIFO_STAT_FIFOX_UNDER_MASK(i))
61647a70e6fSCosmin Samoila 			dev_dbg(&pdev->dev,
61747a70e6fSCosmin Samoila 				"FIFO Underflow Exception flag for channel %d\n",
61847a70e6fSCosmin Samoila 				i);
61947a70e6fSCosmin Samoila 	}
62047a70e6fSCosmin Samoila 
62147a70e6fSCosmin Samoila 	return IRQ_HANDLED;
62247a70e6fSCosmin Samoila }
62347a70e6fSCosmin Samoila 
62447a70e6fSCosmin Samoila static irqreturn_t micfil_err_isr(int irq, void *devid)
62547a70e6fSCosmin Samoila {
62647a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = (struct fsl_micfil *)devid;
62747a70e6fSCosmin Samoila 	struct platform_device *pdev = micfil->pdev;
62847a70e6fSCosmin Samoila 	u32 stat_reg;
62947a70e6fSCosmin Samoila 
63047a70e6fSCosmin Samoila 	regmap_read(micfil->regmap, REG_MICFIL_STAT, &stat_reg);
63147a70e6fSCosmin Samoila 
63247a70e6fSCosmin Samoila 	if (stat_reg & MICFIL_STAT_BSY_FIL_MASK)
63347a70e6fSCosmin Samoila 		dev_dbg(&pdev->dev, "isr: Decimation Filter is running\n");
63447a70e6fSCosmin Samoila 
63547a70e6fSCosmin Samoila 	if (stat_reg & MICFIL_STAT_FIR_RDY_MASK)
63647a70e6fSCosmin Samoila 		dev_dbg(&pdev->dev, "isr: FIR Filter Data ready\n");
63747a70e6fSCosmin Samoila 
63847a70e6fSCosmin Samoila 	if (stat_reg & MICFIL_STAT_LOWFREQF_MASK) {
63947a70e6fSCosmin Samoila 		dev_dbg(&pdev->dev, "isr: ipg_clk_app is too low\n");
64047a70e6fSCosmin Samoila 		regmap_write_bits(micfil->regmap, REG_MICFIL_STAT,
64147a70e6fSCosmin Samoila 				  MICFIL_STAT_LOWFREQF_MASK, 1);
64247a70e6fSCosmin Samoila 	}
64347a70e6fSCosmin Samoila 
64447a70e6fSCosmin Samoila 	return IRQ_HANDLED;
64547a70e6fSCosmin Samoila }
64647a70e6fSCosmin Samoila 
64747a70e6fSCosmin Samoila static int fsl_micfil_probe(struct platform_device *pdev)
64847a70e6fSCosmin Samoila {
64947a70e6fSCosmin Samoila 	struct device_node *np = pdev->dev.of_node;
65047a70e6fSCosmin Samoila 	struct fsl_micfil *micfil;
65147a70e6fSCosmin Samoila 	struct resource *res;
65247a70e6fSCosmin Samoila 	void __iomem *regs;
65347a70e6fSCosmin Samoila 	int ret, i;
65447a70e6fSCosmin Samoila 	unsigned long irqflag = 0;
65547a70e6fSCosmin Samoila 
65647a70e6fSCosmin Samoila 	micfil = devm_kzalloc(&pdev->dev, sizeof(*micfil), GFP_KERNEL);
65747a70e6fSCosmin Samoila 	if (!micfil)
65847a70e6fSCosmin Samoila 		return -ENOMEM;
65947a70e6fSCosmin Samoila 
66047a70e6fSCosmin Samoila 	micfil->pdev = pdev;
66147a70e6fSCosmin Samoila 	strncpy(micfil->name, np->name, sizeof(micfil->name) - 1);
66247a70e6fSCosmin Samoila 
663d7388718SFabio Estevam 	micfil->soc = of_device_get_match_data(&pdev->dev);
66447a70e6fSCosmin Samoila 
66547a70e6fSCosmin Samoila 	/* ipg_clk is used to control the registers
66647a70e6fSCosmin Samoila 	 * ipg_clk_app is used to operate the filter
66747a70e6fSCosmin Samoila 	 */
66847a70e6fSCosmin Samoila 	micfil->mclk = devm_clk_get(&pdev->dev, "ipg_clk_app");
66947a70e6fSCosmin Samoila 	if (IS_ERR(micfil->mclk)) {
67047a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "failed to get core clock: %ld\n",
67147a70e6fSCosmin Samoila 			PTR_ERR(micfil->mclk));
67247a70e6fSCosmin Samoila 		return PTR_ERR(micfil->mclk);
67347a70e6fSCosmin Samoila 	}
67447a70e6fSCosmin Samoila 
675b5cf28f7SShengjiu Wang 	micfil->busclk = devm_clk_get(&pdev->dev, "ipg_clk");
676b5cf28f7SShengjiu Wang 	if (IS_ERR(micfil->busclk)) {
677b5cf28f7SShengjiu Wang 		dev_err(&pdev->dev, "failed to get ipg clock: %ld\n",
678b5cf28f7SShengjiu Wang 			PTR_ERR(micfil->busclk));
679b5cf28f7SShengjiu Wang 		return PTR_ERR(micfil->busclk);
680b5cf28f7SShengjiu Wang 	}
681b5cf28f7SShengjiu Wang 
68247a70e6fSCosmin Samoila 	/* init regmap */
683d9bf1e79SYang Yingliang 	regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
68447a70e6fSCosmin Samoila 	if (IS_ERR(regs))
68547a70e6fSCosmin Samoila 		return PTR_ERR(regs);
68647a70e6fSCosmin Samoila 
687b5cf28f7SShengjiu Wang 	micfil->regmap = devm_regmap_init_mmio(&pdev->dev,
68847a70e6fSCosmin Samoila 					       regs,
68947a70e6fSCosmin Samoila 					       &fsl_micfil_regmap_config);
69047a70e6fSCosmin Samoila 	if (IS_ERR(micfil->regmap)) {
69147a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "failed to init MICFIL regmap: %ld\n",
69247a70e6fSCosmin Samoila 			PTR_ERR(micfil->regmap));
69347a70e6fSCosmin Samoila 		return PTR_ERR(micfil->regmap);
69447a70e6fSCosmin Samoila 	}
69547a70e6fSCosmin Samoila 
69647a70e6fSCosmin Samoila 	/* dataline mask for RX */
69747a70e6fSCosmin Samoila 	ret = of_property_read_u32_index(np,
69847a70e6fSCosmin Samoila 					 "fsl,dataline",
69947a70e6fSCosmin Samoila 					 0,
70047a70e6fSCosmin Samoila 					 &micfil->dataline);
70147a70e6fSCosmin Samoila 	if (ret)
70247a70e6fSCosmin Samoila 		micfil->dataline = 1;
70347a70e6fSCosmin Samoila 
70447a70e6fSCosmin Samoila 	if (micfil->dataline & ~micfil->soc->dataline) {
70547a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "dataline setting error, Mask is 0x%X\n",
70647a70e6fSCosmin Samoila 			micfil->soc->dataline);
70747a70e6fSCosmin Samoila 		return -EINVAL;
70847a70e6fSCosmin Samoila 	}
70947a70e6fSCosmin Samoila 
71047a70e6fSCosmin Samoila 	/* get IRQs */
71147a70e6fSCosmin Samoila 	for (i = 0; i < MICFIL_IRQ_LINES; i++) {
71247a70e6fSCosmin Samoila 		micfil->irq[i] = platform_get_irq(pdev, i);
71347a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "GET IRQ: %d\n", micfil->irq[i]);
71483b35f45STang Bin 		if (micfil->irq[i] < 0)
71547a70e6fSCosmin Samoila 			return micfil->irq[i];
71647a70e6fSCosmin Samoila 	}
71747a70e6fSCosmin Samoila 
71847a70e6fSCosmin Samoila 	if (of_property_read_bool(np, "fsl,shared-interrupt"))
71947a70e6fSCosmin Samoila 		irqflag = IRQF_SHARED;
72047a70e6fSCosmin Samoila 
721a62ed960SFabio Estevam 	/* Digital Microphone interface interrupt */
72247a70e6fSCosmin Samoila 	ret = devm_request_irq(&pdev->dev, micfil->irq[0],
72347a70e6fSCosmin Samoila 			       micfil_isr, irqflag,
72447a70e6fSCosmin Samoila 			       micfil->name, micfil);
72547a70e6fSCosmin Samoila 	if (ret) {
72647a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "failed to claim mic interface irq %u\n",
72747a70e6fSCosmin Samoila 			micfil->irq[0]);
72847a70e6fSCosmin Samoila 		return ret;
72947a70e6fSCosmin Samoila 	}
73047a70e6fSCosmin Samoila 
731a62ed960SFabio Estevam 	/* Digital Microphone interface error interrupt */
73247a70e6fSCosmin Samoila 	ret = devm_request_irq(&pdev->dev, micfil->irq[1],
73347a70e6fSCosmin Samoila 			       micfil_err_isr, irqflag,
73447a70e6fSCosmin Samoila 			       micfil->name, micfil);
73547a70e6fSCosmin Samoila 	if (ret) {
73647a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "failed to claim mic interface error irq %u\n",
73747a70e6fSCosmin Samoila 			micfil->irq[1]);
73847a70e6fSCosmin Samoila 		return ret;
73947a70e6fSCosmin Samoila 	}
74047a70e6fSCosmin Samoila 
74147a70e6fSCosmin Samoila 	micfil->dma_params_rx.chan_name = "rx";
74247a70e6fSCosmin Samoila 	micfil->dma_params_rx.addr = res->start + REG_MICFIL_DATACH0;
74347a70e6fSCosmin Samoila 	micfil->dma_params_rx.maxburst = MICFIL_DMA_MAXBURST_RX;
74447a70e6fSCosmin Samoila 
74547a70e6fSCosmin Samoila 
74647a70e6fSCosmin Samoila 	platform_set_drvdata(pdev, micfil);
74747a70e6fSCosmin Samoila 
74847a70e6fSCosmin Samoila 	pm_runtime_enable(&pdev->dev);
749b5cf28f7SShengjiu Wang 	regcache_cache_only(micfil->regmap, true);
75047a70e6fSCosmin Samoila 
7510adf2920SShengjiu Wang 	/*
7520adf2920SShengjiu Wang 	 * Register platform component before registering cpu dai for there
7530adf2920SShengjiu Wang 	 * is not defer probe for platform component in snd_soc_add_pcm_runtime().
7540adf2920SShengjiu Wang 	 */
7550adf2920SShengjiu Wang 	ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
7560adf2920SShengjiu Wang 	if (ret) {
7570adf2920SShengjiu Wang 		dev_err(&pdev->dev, "failed to pcm register\n");
7580adf2920SShengjiu Wang 		return ret;
7590adf2920SShengjiu Wang 	}
7600adf2920SShengjiu Wang 
76147a70e6fSCosmin Samoila 	ret = devm_snd_soc_register_component(&pdev->dev, &fsl_micfil_component,
76247a70e6fSCosmin Samoila 					      &fsl_micfil_dai, 1);
76347a70e6fSCosmin Samoila 	if (ret) {
76447a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "failed to register component %s\n",
76547a70e6fSCosmin Samoila 			fsl_micfil_component.name);
76647a70e6fSCosmin Samoila 	}
76747a70e6fSCosmin Samoila 
76847a70e6fSCosmin Samoila 	return ret;
76947a70e6fSCosmin Samoila }
77047a70e6fSCosmin Samoila 
77147a70e6fSCosmin Samoila static int __maybe_unused fsl_micfil_runtime_suspend(struct device *dev)
77247a70e6fSCosmin Samoila {
77347a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = dev_get_drvdata(dev);
77447a70e6fSCosmin Samoila 
77547a70e6fSCosmin Samoila 	regcache_cache_only(micfil->regmap, true);
77647a70e6fSCosmin Samoila 
77747a70e6fSCosmin Samoila 	clk_disable_unprepare(micfil->mclk);
778b5cf28f7SShengjiu Wang 	clk_disable_unprepare(micfil->busclk);
77947a70e6fSCosmin Samoila 
78047a70e6fSCosmin Samoila 	return 0;
78147a70e6fSCosmin Samoila }
78247a70e6fSCosmin Samoila 
78347a70e6fSCosmin Samoila static int __maybe_unused fsl_micfil_runtime_resume(struct device *dev)
78447a70e6fSCosmin Samoila {
78547a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = dev_get_drvdata(dev);
78647a70e6fSCosmin Samoila 	int ret;
78747a70e6fSCosmin Samoila 
788b5cf28f7SShengjiu Wang 	ret = clk_prepare_enable(micfil->busclk);
78947a70e6fSCosmin Samoila 	if (ret < 0)
79047a70e6fSCosmin Samoila 		return ret;
79147a70e6fSCosmin Samoila 
792b5cf28f7SShengjiu Wang 	ret = clk_prepare_enable(micfil->mclk);
793b5cf28f7SShengjiu Wang 	if (ret < 0) {
794b5cf28f7SShengjiu Wang 		clk_disable_unprepare(micfil->busclk);
795b5cf28f7SShengjiu Wang 		return ret;
796b5cf28f7SShengjiu Wang 	}
797b5cf28f7SShengjiu Wang 
79847a70e6fSCosmin Samoila 	regcache_cache_only(micfil->regmap, false);
79947a70e6fSCosmin Samoila 	regcache_mark_dirty(micfil->regmap);
80047a70e6fSCosmin Samoila 	regcache_sync(micfil->regmap);
80147a70e6fSCosmin Samoila 
80247a70e6fSCosmin Samoila 	return 0;
80347a70e6fSCosmin Samoila }
80447a70e6fSCosmin Samoila 
80547a70e6fSCosmin Samoila static int __maybe_unused fsl_micfil_suspend(struct device *dev)
80647a70e6fSCosmin Samoila {
80747a70e6fSCosmin Samoila 	pm_runtime_force_suspend(dev);
80847a70e6fSCosmin Samoila 
80947a70e6fSCosmin Samoila 	return 0;
81047a70e6fSCosmin Samoila }
81147a70e6fSCosmin Samoila 
81247a70e6fSCosmin Samoila static int __maybe_unused fsl_micfil_resume(struct device *dev)
81347a70e6fSCosmin Samoila {
81447a70e6fSCosmin Samoila 	pm_runtime_force_resume(dev);
81547a70e6fSCosmin Samoila 
81647a70e6fSCosmin Samoila 	return 0;
81747a70e6fSCosmin Samoila }
81847a70e6fSCosmin Samoila 
81947a70e6fSCosmin Samoila static const struct dev_pm_ops fsl_micfil_pm_ops = {
82047a70e6fSCosmin Samoila 	SET_RUNTIME_PM_OPS(fsl_micfil_runtime_suspend,
82147a70e6fSCosmin Samoila 			   fsl_micfil_runtime_resume,
82247a70e6fSCosmin Samoila 			   NULL)
82347a70e6fSCosmin Samoila 	SET_SYSTEM_SLEEP_PM_OPS(fsl_micfil_suspend,
82447a70e6fSCosmin Samoila 				fsl_micfil_resume)
82547a70e6fSCosmin Samoila };
82647a70e6fSCosmin Samoila 
82747a70e6fSCosmin Samoila static struct platform_driver fsl_micfil_driver = {
82847a70e6fSCosmin Samoila 	.probe = fsl_micfil_probe,
82947a70e6fSCosmin Samoila 	.driver = {
83047a70e6fSCosmin Samoila 		.name = "fsl-micfil-dai",
83147a70e6fSCosmin Samoila 		.pm = &fsl_micfil_pm_ops,
83247a70e6fSCosmin Samoila 		.of_match_table = fsl_micfil_dt_ids,
83347a70e6fSCosmin Samoila 	},
83447a70e6fSCosmin Samoila };
83547a70e6fSCosmin Samoila module_platform_driver(fsl_micfil_driver);
83647a70e6fSCosmin Samoila 
83747a70e6fSCosmin Samoila MODULE_AUTHOR("Cosmin-Gabriel Samoila <cosmin.samoila@nxp.com>");
83847a70e6fSCosmin Samoila MODULE_DESCRIPTION("NXP PDM Microphone Interface (MICFIL) driver");
83947a70e6fSCosmin Samoila MODULE_LICENSE("GPL v2");
840