xref: /openbmc/linux/sound/soc/fsl/fsl_micfil.c (revision b5cf28f7a890f3554ca15a43edbbb86dd1b9663c)
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;
34*b5cf28f7SShengjiu 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 
19447a70e6fSCosmin Samoila 	return 0;
19547a70e6fSCosmin Samoila }
19647a70e6fSCosmin Samoila 
19747a70e6fSCosmin Samoila static int fsl_micfil_set_mclk_rate(struct fsl_micfil *micfil,
19847a70e6fSCosmin Samoila 				    unsigned int freq)
19947a70e6fSCosmin Samoila {
20047a70e6fSCosmin Samoila 	struct device *dev = &micfil->pdev->dev;
20147a70e6fSCosmin Samoila 	int ret;
20247a70e6fSCosmin Samoila 
20347a70e6fSCosmin Samoila 	clk_disable_unprepare(micfil->mclk);
20447a70e6fSCosmin Samoila 
20547a70e6fSCosmin Samoila 	ret = clk_set_rate(micfil->mclk, freq * 1024);
20647a70e6fSCosmin Samoila 	if (ret)
20747a70e6fSCosmin Samoila 		dev_warn(dev, "failed to set rate (%u): %d\n",
20847a70e6fSCosmin Samoila 			 freq * 1024, ret);
20947a70e6fSCosmin Samoila 
21047a70e6fSCosmin Samoila 	clk_prepare_enable(micfil->mclk);
21147a70e6fSCosmin Samoila 
21247a70e6fSCosmin Samoila 	return ret;
21347a70e6fSCosmin Samoila }
21447a70e6fSCosmin Samoila 
21547a70e6fSCosmin Samoila static int fsl_micfil_startup(struct snd_pcm_substream *substream,
21647a70e6fSCosmin Samoila 			      struct snd_soc_dai *dai)
21747a70e6fSCosmin Samoila {
21847a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = snd_soc_dai_get_drvdata(dai);
21947a70e6fSCosmin Samoila 
22047a70e6fSCosmin Samoila 	if (!micfil) {
22111106cb3STang Bin 		dev_err(dai->dev, "micfil dai priv_data not set\n");
22247a70e6fSCosmin Samoila 		return -EINVAL;
22347a70e6fSCosmin Samoila 	}
22447a70e6fSCosmin Samoila 
22547a70e6fSCosmin Samoila 	return 0;
22647a70e6fSCosmin Samoila }
22747a70e6fSCosmin Samoila 
22847a70e6fSCosmin Samoila static int fsl_micfil_trigger(struct snd_pcm_substream *substream, int cmd,
22947a70e6fSCosmin Samoila 			      struct snd_soc_dai *dai)
23047a70e6fSCosmin Samoila {
23147a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = snd_soc_dai_get_drvdata(dai);
23247a70e6fSCosmin Samoila 	struct device *dev = &micfil->pdev->dev;
23347a70e6fSCosmin Samoila 	int ret;
23447a70e6fSCosmin Samoila 
23547a70e6fSCosmin Samoila 	switch (cmd) {
23647a70e6fSCosmin Samoila 	case SNDRV_PCM_TRIGGER_START:
23747a70e6fSCosmin Samoila 	case SNDRV_PCM_TRIGGER_RESUME:
23847a70e6fSCosmin Samoila 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
23947a70e6fSCosmin Samoila 		ret = fsl_micfil_reset(dev);
24047a70e6fSCosmin Samoila 		if (ret) {
24147a70e6fSCosmin Samoila 			dev_err(dev, "failed to soft reset\n");
24247a70e6fSCosmin Samoila 			return ret;
24347a70e6fSCosmin Samoila 		}
24447a70e6fSCosmin Samoila 
24547a70e6fSCosmin Samoila 		/* DMA Interrupt Selection - DISEL bits
24647a70e6fSCosmin Samoila 		 * 00 - DMA and IRQ disabled
24747a70e6fSCosmin Samoila 		 * 01 - DMA req enabled
24847a70e6fSCosmin Samoila 		 * 10 - IRQ enabled
24947a70e6fSCosmin Samoila 		 * 11 - reserved
25047a70e6fSCosmin Samoila 		 */
25147a70e6fSCosmin Samoila 		ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL1,
25247a70e6fSCosmin Samoila 					 MICFIL_CTRL1_DISEL_MASK,
25347a70e6fSCosmin Samoila 					 (1 << MICFIL_CTRL1_DISEL_SHIFT));
25447a70e6fSCosmin Samoila 		if (ret) {
25547a70e6fSCosmin Samoila 			dev_err(dev, "failed to update DISEL bits\n");
25647a70e6fSCosmin Samoila 			return ret;
25747a70e6fSCosmin Samoila 		}
25847a70e6fSCosmin Samoila 
25947a70e6fSCosmin Samoila 		/* Enable the module */
26047a70e6fSCosmin Samoila 		ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL1,
26147a70e6fSCosmin Samoila 					 MICFIL_CTRL1_PDMIEN_MASK,
26247a70e6fSCosmin Samoila 					 MICFIL_CTRL1_PDMIEN);
26347a70e6fSCosmin Samoila 		if (ret) {
26447a70e6fSCosmin Samoila 			dev_err(dev, "failed to enable the module\n");
26547a70e6fSCosmin Samoila 			return ret;
26647a70e6fSCosmin Samoila 		}
26747a70e6fSCosmin Samoila 
26847a70e6fSCosmin Samoila 		break;
26947a70e6fSCosmin Samoila 	case SNDRV_PCM_TRIGGER_STOP:
27047a70e6fSCosmin Samoila 	case SNDRV_PCM_TRIGGER_SUSPEND:
27147a70e6fSCosmin Samoila 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
27247a70e6fSCosmin Samoila 		/* Disable the module */
27347a70e6fSCosmin Samoila 		ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL1,
27447a70e6fSCosmin Samoila 					 MICFIL_CTRL1_PDMIEN_MASK,
27547a70e6fSCosmin Samoila 					 0);
27647a70e6fSCosmin Samoila 		if (ret) {
27747a70e6fSCosmin Samoila 			dev_err(dev, "failed to enable the module\n");
27847a70e6fSCosmin Samoila 			return ret;
27947a70e6fSCosmin Samoila 		}
28047a70e6fSCosmin Samoila 
28147a70e6fSCosmin Samoila 		ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL1,
28247a70e6fSCosmin Samoila 					 MICFIL_CTRL1_DISEL_MASK,
28347a70e6fSCosmin Samoila 					 (0 << MICFIL_CTRL1_DISEL_SHIFT));
28447a70e6fSCosmin Samoila 		if (ret) {
28547a70e6fSCosmin Samoila 			dev_err(dev, "failed to update DISEL bits\n");
28647a70e6fSCosmin Samoila 			return ret;
28747a70e6fSCosmin Samoila 		}
28847a70e6fSCosmin Samoila 		break;
28947a70e6fSCosmin Samoila 	default:
29047a70e6fSCosmin Samoila 		return -EINVAL;
29147a70e6fSCosmin Samoila 	}
29247a70e6fSCosmin Samoila 	return 0;
29347a70e6fSCosmin Samoila }
29447a70e6fSCosmin Samoila 
29547a70e6fSCosmin Samoila static int fsl_set_clock_params(struct device *dev, unsigned int rate)
29647a70e6fSCosmin Samoila {
29747a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = dev_get_drvdata(dev);
29847a70e6fSCosmin Samoila 	int clk_div;
29915b5c496STang Bin 	int ret;
30047a70e6fSCosmin Samoila 
30147a70e6fSCosmin Samoila 	ret = fsl_micfil_set_mclk_rate(micfil, rate);
30247a70e6fSCosmin Samoila 	if (ret < 0)
30347a70e6fSCosmin Samoila 		dev_err(dev, "failed to set mclk[%lu] to rate %u\n",
30447a70e6fSCosmin Samoila 			clk_get_rate(micfil->mclk), rate);
30547a70e6fSCosmin Samoila 
30647a70e6fSCosmin Samoila 	/* set CICOSR */
30747a70e6fSCosmin Samoila 	ret |= regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL2,
30847a70e6fSCosmin Samoila 				 MICFIL_CTRL2_CICOSR_MASK,
30947a70e6fSCosmin Samoila 				 MICFIL_CTRL2_OSR_DEFAULT);
31047a70e6fSCosmin Samoila 	if (ret)
31147a70e6fSCosmin Samoila 		dev_err(dev, "failed to set CICOSR in reg 0x%X\n",
31247a70e6fSCosmin Samoila 			REG_MICFIL_CTRL2);
31347a70e6fSCosmin Samoila 
31447a70e6fSCosmin Samoila 	/* set CLK_DIV */
31547a70e6fSCosmin Samoila 	clk_div = get_clk_div(micfil, rate);
31647a70e6fSCosmin Samoila 	if (clk_div < 0)
31747a70e6fSCosmin Samoila 		ret = -EINVAL;
31847a70e6fSCosmin Samoila 
31947a70e6fSCosmin Samoila 	ret |= regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL2,
32047a70e6fSCosmin Samoila 				 MICFIL_CTRL2_CLKDIV_MASK, clk_div);
32147a70e6fSCosmin Samoila 	if (ret)
32247a70e6fSCosmin Samoila 		dev_err(dev, "failed to set CLKDIV in reg 0x%X\n",
32347a70e6fSCosmin Samoila 			REG_MICFIL_CTRL2);
32447a70e6fSCosmin Samoila 
32547a70e6fSCosmin Samoila 	return ret;
32647a70e6fSCosmin Samoila }
32747a70e6fSCosmin Samoila 
32847a70e6fSCosmin Samoila static int fsl_micfil_hw_params(struct snd_pcm_substream *substream,
32947a70e6fSCosmin Samoila 				struct snd_pcm_hw_params *params,
33047a70e6fSCosmin Samoila 				struct snd_soc_dai *dai)
33147a70e6fSCosmin Samoila {
33247a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = snd_soc_dai_get_drvdata(dai);
33347a70e6fSCosmin Samoila 	unsigned int channels = params_channels(params);
33447a70e6fSCosmin Samoila 	unsigned int rate = params_rate(params);
33547a70e6fSCosmin Samoila 	struct device *dev = &micfil->pdev->dev;
33647a70e6fSCosmin Samoila 	int ret;
33747a70e6fSCosmin Samoila 
33847a70e6fSCosmin Samoila 	/* 1. Disable the module */
33947a70e6fSCosmin Samoila 	ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL1,
34047a70e6fSCosmin Samoila 				 MICFIL_CTRL1_PDMIEN_MASK, 0);
34147a70e6fSCosmin Samoila 	if (ret) {
34247a70e6fSCosmin Samoila 		dev_err(dev, "failed to disable the module\n");
34347a70e6fSCosmin Samoila 		return ret;
34447a70e6fSCosmin Samoila 	}
34547a70e6fSCosmin Samoila 
34647a70e6fSCosmin Samoila 	/* enable channels */
34747a70e6fSCosmin Samoila 	ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL1,
34847a70e6fSCosmin Samoila 				 0xFF, ((1 << channels) - 1));
34947a70e6fSCosmin Samoila 	if (ret) {
35047a70e6fSCosmin Samoila 		dev_err(dev, "failed to enable channels %d, reg 0x%X\n", ret,
35147a70e6fSCosmin Samoila 			REG_MICFIL_CTRL1);
35247a70e6fSCosmin Samoila 		return ret;
35347a70e6fSCosmin Samoila 	}
35447a70e6fSCosmin Samoila 
35547a70e6fSCosmin Samoila 	ret = fsl_set_clock_params(dev, rate);
35647a70e6fSCosmin Samoila 	if (ret < 0) {
35747a70e6fSCosmin Samoila 		dev_err(dev, "Failed to set clock parameters [%d]\n", ret);
35847a70e6fSCosmin Samoila 		return ret;
35947a70e6fSCosmin Samoila 	}
36047a70e6fSCosmin Samoila 
36147a70e6fSCosmin Samoila 	micfil->dma_params_rx.maxburst = channels * MICFIL_DMA_MAXBURST_RX;
36247a70e6fSCosmin Samoila 
36347a70e6fSCosmin Samoila 	return 0;
36447a70e6fSCosmin Samoila }
36547a70e6fSCosmin Samoila 
36647a70e6fSCosmin Samoila static int fsl_micfil_set_dai_sysclk(struct snd_soc_dai *dai, int clk_id,
36747a70e6fSCosmin Samoila 				     unsigned int freq, int dir)
36847a70e6fSCosmin Samoila {
36947a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = snd_soc_dai_get_drvdata(dai);
37047a70e6fSCosmin Samoila 	struct device *dev = &micfil->pdev->dev;
37147a70e6fSCosmin Samoila 
37247a70e6fSCosmin Samoila 	int ret;
37347a70e6fSCosmin Samoila 
37447a70e6fSCosmin Samoila 	if (!freq)
37547a70e6fSCosmin Samoila 		return 0;
37647a70e6fSCosmin Samoila 
37747a70e6fSCosmin Samoila 	ret = fsl_micfil_set_mclk_rate(micfil, freq);
37847a70e6fSCosmin Samoila 	if (ret < 0)
37947a70e6fSCosmin Samoila 		dev_err(dev, "failed to set mclk[%lu] to rate %u\n",
38047a70e6fSCosmin Samoila 			clk_get_rate(micfil->mclk), freq);
38147a70e6fSCosmin Samoila 
38247a70e6fSCosmin Samoila 	return ret;
38347a70e6fSCosmin Samoila }
38447a70e6fSCosmin Samoila 
38538d89a56SRikard Falkeborn static const struct snd_soc_dai_ops fsl_micfil_dai_ops = {
38647a70e6fSCosmin Samoila 	.startup = fsl_micfil_startup,
38747a70e6fSCosmin Samoila 	.trigger = fsl_micfil_trigger,
38847a70e6fSCosmin Samoila 	.hw_params = fsl_micfil_hw_params,
38947a70e6fSCosmin Samoila 	.set_sysclk = fsl_micfil_set_dai_sysclk,
39047a70e6fSCosmin Samoila };
39147a70e6fSCosmin Samoila 
39247a70e6fSCosmin Samoila static int fsl_micfil_dai_probe(struct snd_soc_dai *cpu_dai)
39347a70e6fSCosmin Samoila {
39447a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = dev_get_drvdata(cpu_dai->dev);
39547a70e6fSCosmin Samoila 	struct device *dev = cpu_dai->dev;
39647a70e6fSCosmin Samoila 	unsigned int val;
39747a70e6fSCosmin Samoila 	int ret;
39847a70e6fSCosmin Samoila 	int i;
39947a70e6fSCosmin Samoila 
40047a70e6fSCosmin Samoila 	/* set qsel to medium */
40147a70e6fSCosmin Samoila 	ret = regmap_update_bits(micfil->regmap, REG_MICFIL_CTRL2,
40247a70e6fSCosmin Samoila 				 MICFIL_CTRL2_QSEL_MASK, MICFIL_MEDIUM_QUALITY);
40347a70e6fSCosmin Samoila 	if (ret) {
40447a70e6fSCosmin Samoila 		dev_err(dev, "failed to set quality mode bits, reg 0x%X\n",
40547a70e6fSCosmin Samoila 			REG_MICFIL_CTRL2);
40647a70e6fSCosmin Samoila 		return ret;
40747a70e6fSCosmin Samoila 	}
40847a70e6fSCosmin Samoila 
40947a70e6fSCosmin Samoila 	/* set default gain to max_gain */
41047a70e6fSCosmin Samoila 	regmap_write(micfil->regmap, REG_MICFIL_OUT_CTRL, 0x77777777);
41147a70e6fSCosmin Samoila 	for (i = 0; i < 8; i++)
41247a70e6fSCosmin Samoila 		micfil->channel_gain[i] = 0xF;
41347a70e6fSCosmin Samoila 
41447a70e6fSCosmin Samoila 	snd_soc_dai_init_dma_data(cpu_dai, NULL,
41547a70e6fSCosmin Samoila 				  &micfil->dma_params_rx);
41647a70e6fSCosmin Samoila 
41747a70e6fSCosmin Samoila 	/* FIFO Watermark Control - FIFOWMK*/
41847a70e6fSCosmin Samoila 	val = MICFIL_FIFO_CTRL_FIFOWMK(micfil->soc->fifo_depth) - 1;
41947a70e6fSCosmin Samoila 	ret = regmap_update_bits(micfil->regmap, REG_MICFIL_FIFO_CTRL,
42047a70e6fSCosmin Samoila 				 MICFIL_FIFO_CTRL_FIFOWMK_MASK,
42147a70e6fSCosmin Samoila 				 val);
42247a70e6fSCosmin Samoila 	if (ret) {
42347a70e6fSCosmin Samoila 		dev_err(dev, "failed to set FIFOWMK\n");
42447a70e6fSCosmin Samoila 		return ret;
42547a70e6fSCosmin Samoila 	}
42647a70e6fSCosmin Samoila 
42747a70e6fSCosmin Samoila 	return 0;
42847a70e6fSCosmin Samoila }
42947a70e6fSCosmin Samoila 
43047a70e6fSCosmin Samoila static struct snd_soc_dai_driver fsl_micfil_dai = {
43147a70e6fSCosmin Samoila 	.probe = fsl_micfil_dai_probe,
43247a70e6fSCosmin Samoila 	.capture = {
43347a70e6fSCosmin Samoila 		.stream_name = "CPU-Capture",
43447a70e6fSCosmin Samoila 		.channels_min = 1,
43547a70e6fSCosmin Samoila 		.channels_max = 8,
43647a70e6fSCosmin Samoila 		.rates = FSL_MICFIL_RATES,
43747a70e6fSCosmin Samoila 		.formats = FSL_MICFIL_FORMATS,
43847a70e6fSCosmin Samoila 	},
43947a70e6fSCosmin Samoila 	.ops = &fsl_micfil_dai_ops,
44047a70e6fSCosmin Samoila };
44147a70e6fSCosmin Samoila 
44247a70e6fSCosmin Samoila static const struct snd_soc_component_driver fsl_micfil_component = {
44347a70e6fSCosmin Samoila 	.name		= "fsl-micfil-dai",
44447a70e6fSCosmin Samoila 	.controls       = fsl_micfil_snd_controls,
44547a70e6fSCosmin Samoila 	.num_controls   = ARRAY_SIZE(fsl_micfil_snd_controls),
44647a70e6fSCosmin Samoila 
44747a70e6fSCosmin Samoila };
44847a70e6fSCosmin Samoila 
44947a70e6fSCosmin Samoila /* REGMAP */
45047a70e6fSCosmin Samoila static const struct reg_default fsl_micfil_reg_defaults[] = {
45147a70e6fSCosmin Samoila 	{REG_MICFIL_CTRL1,		0x00000000},
45247a70e6fSCosmin Samoila 	{REG_MICFIL_CTRL2,		0x00000000},
45347a70e6fSCosmin Samoila 	{REG_MICFIL_STAT,		0x00000000},
45447a70e6fSCosmin Samoila 	{REG_MICFIL_FIFO_CTRL,		0x00000007},
45547a70e6fSCosmin Samoila 	{REG_MICFIL_FIFO_STAT,		0x00000000},
45647a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH0,		0x00000000},
45747a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH1,		0x00000000},
45847a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH2,		0x00000000},
45947a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH3,		0x00000000},
46047a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH4,		0x00000000},
46147a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH5,		0x00000000},
46247a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH6,		0x00000000},
46347a70e6fSCosmin Samoila 	{REG_MICFIL_DATACH7,		0x00000000},
46447a70e6fSCosmin Samoila 	{REG_MICFIL_DC_CTRL,		0x00000000},
46547a70e6fSCosmin Samoila 	{REG_MICFIL_OUT_CTRL,		0x00000000},
46647a70e6fSCosmin Samoila 	{REG_MICFIL_OUT_STAT,		0x00000000},
46747a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_CTRL1,		0x00000000},
46847a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_CTRL2,		0x000A0000},
46947a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_STAT,		0x00000000},
47047a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_SCONFIG,	0x00000000},
47147a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_NCONFIG,	0x80000000},
47247a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_NDATA,		0x00000000},
47347a70e6fSCosmin Samoila 	{REG_MICFIL_VAD0_ZCD,		0x00000004},
47447a70e6fSCosmin Samoila };
47547a70e6fSCosmin Samoila 
47647a70e6fSCosmin Samoila static bool fsl_micfil_readable_reg(struct device *dev, unsigned int reg)
47747a70e6fSCosmin Samoila {
47847a70e6fSCosmin Samoila 	switch (reg) {
47947a70e6fSCosmin Samoila 	case REG_MICFIL_CTRL1:
48047a70e6fSCosmin Samoila 	case REG_MICFIL_CTRL2:
48147a70e6fSCosmin Samoila 	case REG_MICFIL_STAT:
48247a70e6fSCosmin Samoila 	case REG_MICFIL_FIFO_CTRL:
48347a70e6fSCosmin Samoila 	case REG_MICFIL_FIFO_STAT:
48447a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH0:
48547a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH1:
48647a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH2:
48747a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH3:
48847a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH4:
48947a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH5:
49047a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH6:
49147a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH7:
49247a70e6fSCosmin Samoila 	case REG_MICFIL_DC_CTRL:
49347a70e6fSCosmin Samoila 	case REG_MICFIL_OUT_CTRL:
49447a70e6fSCosmin Samoila 	case REG_MICFIL_OUT_STAT:
49547a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_CTRL1:
49647a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_CTRL2:
49747a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_STAT:
49847a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_SCONFIG:
49947a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_NCONFIG:
50047a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_NDATA:
50147a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_ZCD:
50247a70e6fSCosmin Samoila 		return true;
50347a70e6fSCosmin Samoila 	default:
50447a70e6fSCosmin Samoila 		return false;
50547a70e6fSCosmin Samoila 	}
50647a70e6fSCosmin Samoila }
50747a70e6fSCosmin Samoila 
50847a70e6fSCosmin Samoila static bool fsl_micfil_writeable_reg(struct device *dev, unsigned int reg)
50947a70e6fSCosmin Samoila {
51047a70e6fSCosmin Samoila 	switch (reg) {
51147a70e6fSCosmin Samoila 	case REG_MICFIL_CTRL1:
51247a70e6fSCosmin Samoila 	case REG_MICFIL_CTRL2:
51347a70e6fSCosmin Samoila 	case REG_MICFIL_STAT:		/* Write 1 to Clear */
51447a70e6fSCosmin Samoila 	case REG_MICFIL_FIFO_CTRL:
51547a70e6fSCosmin Samoila 	case REG_MICFIL_FIFO_STAT:	/* Write 1 to Clear */
51647a70e6fSCosmin Samoila 	case REG_MICFIL_DC_CTRL:
51747a70e6fSCosmin Samoila 	case REG_MICFIL_OUT_CTRL:
51847a70e6fSCosmin Samoila 	case REG_MICFIL_OUT_STAT:	/* Write 1 to Clear */
51947a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_CTRL1:
52047a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_CTRL2:
52147a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_STAT:	/* Write 1 to Clear */
52247a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_SCONFIG:
52347a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_NCONFIG:
52447a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_ZCD:
52547a70e6fSCosmin Samoila 		return true;
52647a70e6fSCosmin Samoila 	default:
52747a70e6fSCosmin Samoila 		return false;
52847a70e6fSCosmin Samoila 	}
52947a70e6fSCosmin Samoila }
53047a70e6fSCosmin Samoila 
53147a70e6fSCosmin Samoila static bool fsl_micfil_volatile_reg(struct device *dev, unsigned int reg)
53247a70e6fSCosmin Samoila {
53347a70e6fSCosmin Samoila 	switch (reg) {
53447a70e6fSCosmin Samoila 	case REG_MICFIL_STAT:
53547a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH0:
53647a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH1:
53747a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH2:
53847a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH3:
53947a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH4:
54047a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH5:
54147a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH6:
54247a70e6fSCosmin Samoila 	case REG_MICFIL_DATACH7:
54347a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_STAT:
54447a70e6fSCosmin Samoila 	case REG_MICFIL_VAD0_NDATA:
54547a70e6fSCosmin Samoila 		return true;
54647a70e6fSCosmin Samoila 	default:
54747a70e6fSCosmin Samoila 		return false;
54847a70e6fSCosmin Samoila 	}
54947a70e6fSCosmin Samoila }
55047a70e6fSCosmin Samoila 
55147a70e6fSCosmin Samoila static const struct regmap_config fsl_micfil_regmap_config = {
55247a70e6fSCosmin Samoila 	.reg_bits = 32,
55347a70e6fSCosmin Samoila 	.reg_stride = 4,
55447a70e6fSCosmin Samoila 	.val_bits = 32,
55547a70e6fSCosmin Samoila 
55647a70e6fSCosmin Samoila 	.max_register = REG_MICFIL_VAD0_ZCD,
55747a70e6fSCosmin Samoila 	.reg_defaults = fsl_micfil_reg_defaults,
55847a70e6fSCosmin Samoila 	.num_reg_defaults = ARRAY_SIZE(fsl_micfil_reg_defaults),
55947a70e6fSCosmin Samoila 	.readable_reg = fsl_micfil_readable_reg,
56047a70e6fSCosmin Samoila 	.volatile_reg = fsl_micfil_volatile_reg,
56147a70e6fSCosmin Samoila 	.writeable_reg = fsl_micfil_writeable_reg,
56247a70e6fSCosmin Samoila 	.cache_type = REGCACHE_RBTREE,
56347a70e6fSCosmin Samoila };
56447a70e6fSCosmin Samoila 
56547a70e6fSCosmin Samoila /* END OF REGMAP */
56647a70e6fSCosmin Samoila 
56747a70e6fSCosmin Samoila static irqreturn_t micfil_isr(int irq, void *devid)
56847a70e6fSCosmin Samoila {
56947a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = (struct fsl_micfil *)devid;
57047a70e6fSCosmin Samoila 	struct platform_device *pdev = micfil->pdev;
57147a70e6fSCosmin Samoila 	u32 stat_reg;
57247a70e6fSCosmin Samoila 	u32 fifo_stat_reg;
57347a70e6fSCosmin Samoila 	u32 ctrl1_reg;
57447a70e6fSCosmin Samoila 	bool dma_enabled;
57547a70e6fSCosmin Samoila 	int i;
57647a70e6fSCosmin Samoila 
57747a70e6fSCosmin Samoila 	regmap_read(micfil->regmap, REG_MICFIL_STAT, &stat_reg);
57847a70e6fSCosmin Samoila 	regmap_read(micfil->regmap, REG_MICFIL_CTRL1, &ctrl1_reg);
57947a70e6fSCosmin Samoila 	regmap_read(micfil->regmap, REG_MICFIL_FIFO_STAT, &fifo_stat_reg);
58047a70e6fSCosmin Samoila 
58147a70e6fSCosmin Samoila 	dma_enabled = MICFIL_DMA_ENABLED(ctrl1_reg);
58247a70e6fSCosmin Samoila 
58347a70e6fSCosmin Samoila 	/* Channel 0-7 Output Data Flags */
58447a70e6fSCosmin Samoila 	for (i = 0; i < MICFIL_OUTPUT_CHANNELS; i++) {
58547a70e6fSCosmin Samoila 		if (stat_reg & MICFIL_STAT_CHXF_MASK(i))
58647a70e6fSCosmin Samoila 			dev_dbg(&pdev->dev,
58747a70e6fSCosmin Samoila 				"Data available in Data Channel %d\n", i);
58847a70e6fSCosmin Samoila 		/* if DMA is not enabled, field must be written with 1
58947a70e6fSCosmin Samoila 		 * to clear
59047a70e6fSCosmin Samoila 		 */
59147a70e6fSCosmin Samoila 		if (!dma_enabled)
59247a70e6fSCosmin Samoila 			regmap_write_bits(micfil->regmap,
59347a70e6fSCosmin Samoila 					  REG_MICFIL_STAT,
59447a70e6fSCosmin Samoila 					  MICFIL_STAT_CHXF_MASK(i),
59547a70e6fSCosmin Samoila 					  1);
59647a70e6fSCosmin Samoila 	}
59747a70e6fSCosmin Samoila 
59847a70e6fSCosmin Samoila 	for (i = 0; i < MICFIL_FIFO_NUM; i++) {
59947a70e6fSCosmin Samoila 		if (fifo_stat_reg & MICFIL_FIFO_STAT_FIFOX_OVER_MASK(i))
60047a70e6fSCosmin Samoila 			dev_dbg(&pdev->dev,
60147a70e6fSCosmin Samoila 				"FIFO Overflow Exception flag for channel %d\n",
60247a70e6fSCosmin Samoila 				i);
60347a70e6fSCosmin Samoila 
60447a70e6fSCosmin Samoila 		if (fifo_stat_reg & MICFIL_FIFO_STAT_FIFOX_UNDER_MASK(i))
60547a70e6fSCosmin Samoila 			dev_dbg(&pdev->dev,
60647a70e6fSCosmin Samoila 				"FIFO Underflow Exception flag for channel %d\n",
60747a70e6fSCosmin Samoila 				i);
60847a70e6fSCosmin Samoila 	}
60947a70e6fSCosmin Samoila 
61047a70e6fSCosmin Samoila 	return IRQ_HANDLED;
61147a70e6fSCosmin Samoila }
61247a70e6fSCosmin Samoila 
61347a70e6fSCosmin Samoila static irqreturn_t micfil_err_isr(int irq, void *devid)
61447a70e6fSCosmin Samoila {
61547a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = (struct fsl_micfil *)devid;
61647a70e6fSCosmin Samoila 	struct platform_device *pdev = micfil->pdev;
61747a70e6fSCosmin Samoila 	u32 stat_reg;
61847a70e6fSCosmin Samoila 
61947a70e6fSCosmin Samoila 	regmap_read(micfil->regmap, REG_MICFIL_STAT, &stat_reg);
62047a70e6fSCosmin Samoila 
62147a70e6fSCosmin Samoila 	if (stat_reg & MICFIL_STAT_BSY_FIL_MASK)
62247a70e6fSCosmin Samoila 		dev_dbg(&pdev->dev, "isr: Decimation Filter is running\n");
62347a70e6fSCosmin Samoila 
62447a70e6fSCosmin Samoila 	if (stat_reg & MICFIL_STAT_FIR_RDY_MASK)
62547a70e6fSCosmin Samoila 		dev_dbg(&pdev->dev, "isr: FIR Filter Data ready\n");
62647a70e6fSCosmin Samoila 
62747a70e6fSCosmin Samoila 	if (stat_reg & MICFIL_STAT_LOWFREQF_MASK) {
62847a70e6fSCosmin Samoila 		dev_dbg(&pdev->dev, "isr: ipg_clk_app is too low\n");
62947a70e6fSCosmin Samoila 		regmap_write_bits(micfil->regmap, REG_MICFIL_STAT,
63047a70e6fSCosmin Samoila 				  MICFIL_STAT_LOWFREQF_MASK, 1);
63147a70e6fSCosmin Samoila 	}
63247a70e6fSCosmin Samoila 
63347a70e6fSCosmin Samoila 	return IRQ_HANDLED;
63447a70e6fSCosmin Samoila }
63547a70e6fSCosmin Samoila 
63647a70e6fSCosmin Samoila static int fsl_micfil_probe(struct platform_device *pdev)
63747a70e6fSCosmin Samoila {
63847a70e6fSCosmin Samoila 	struct device_node *np = pdev->dev.of_node;
63947a70e6fSCosmin Samoila 	struct fsl_micfil *micfil;
64047a70e6fSCosmin Samoila 	struct resource *res;
64147a70e6fSCosmin Samoila 	void __iomem *regs;
64247a70e6fSCosmin Samoila 	int ret, i;
64347a70e6fSCosmin Samoila 	unsigned long irqflag = 0;
64447a70e6fSCosmin Samoila 
64547a70e6fSCosmin Samoila 	micfil = devm_kzalloc(&pdev->dev, sizeof(*micfil), GFP_KERNEL);
64647a70e6fSCosmin Samoila 	if (!micfil)
64747a70e6fSCosmin Samoila 		return -ENOMEM;
64847a70e6fSCosmin Samoila 
64947a70e6fSCosmin Samoila 	micfil->pdev = pdev;
65047a70e6fSCosmin Samoila 	strncpy(micfil->name, np->name, sizeof(micfil->name) - 1);
65147a70e6fSCosmin Samoila 
652d7388718SFabio Estevam 	micfil->soc = of_device_get_match_data(&pdev->dev);
65347a70e6fSCosmin Samoila 
65447a70e6fSCosmin Samoila 	/* ipg_clk is used to control the registers
65547a70e6fSCosmin Samoila 	 * ipg_clk_app is used to operate the filter
65647a70e6fSCosmin Samoila 	 */
65747a70e6fSCosmin Samoila 	micfil->mclk = devm_clk_get(&pdev->dev, "ipg_clk_app");
65847a70e6fSCosmin Samoila 	if (IS_ERR(micfil->mclk)) {
65947a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "failed to get core clock: %ld\n",
66047a70e6fSCosmin Samoila 			PTR_ERR(micfil->mclk));
66147a70e6fSCosmin Samoila 		return PTR_ERR(micfil->mclk);
66247a70e6fSCosmin Samoila 	}
66347a70e6fSCosmin Samoila 
664*b5cf28f7SShengjiu Wang 	micfil->busclk = devm_clk_get(&pdev->dev, "ipg_clk");
665*b5cf28f7SShengjiu Wang 	if (IS_ERR(micfil->busclk)) {
666*b5cf28f7SShengjiu Wang 		dev_err(&pdev->dev, "failed to get ipg clock: %ld\n",
667*b5cf28f7SShengjiu Wang 			PTR_ERR(micfil->busclk));
668*b5cf28f7SShengjiu Wang 		return PTR_ERR(micfil->busclk);
669*b5cf28f7SShengjiu Wang 	}
670*b5cf28f7SShengjiu Wang 
67147a70e6fSCosmin Samoila 	/* init regmap */
67247a70e6fSCosmin Samoila 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
67347a70e6fSCosmin Samoila 	regs = devm_ioremap_resource(&pdev->dev, res);
67447a70e6fSCosmin Samoila 	if (IS_ERR(regs))
67547a70e6fSCosmin Samoila 		return PTR_ERR(regs);
67647a70e6fSCosmin Samoila 
677*b5cf28f7SShengjiu Wang 	micfil->regmap = devm_regmap_init_mmio(&pdev->dev,
67847a70e6fSCosmin Samoila 					       regs,
67947a70e6fSCosmin Samoila 					       &fsl_micfil_regmap_config);
68047a70e6fSCosmin Samoila 	if (IS_ERR(micfil->regmap)) {
68147a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "failed to init MICFIL regmap: %ld\n",
68247a70e6fSCosmin Samoila 			PTR_ERR(micfil->regmap));
68347a70e6fSCosmin Samoila 		return PTR_ERR(micfil->regmap);
68447a70e6fSCosmin Samoila 	}
68547a70e6fSCosmin Samoila 
68647a70e6fSCosmin Samoila 	/* dataline mask for RX */
68747a70e6fSCosmin Samoila 	ret = of_property_read_u32_index(np,
68847a70e6fSCosmin Samoila 					 "fsl,dataline",
68947a70e6fSCosmin Samoila 					 0,
69047a70e6fSCosmin Samoila 					 &micfil->dataline);
69147a70e6fSCosmin Samoila 	if (ret)
69247a70e6fSCosmin Samoila 		micfil->dataline = 1;
69347a70e6fSCosmin Samoila 
69447a70e6fSCosmin Samoila 	if (micfil->dataline & ~micfil->soc->dataline) {
69547a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "dataline setting error, Mask is 0x%X\n",
69647a70e6fSCosmin Samoila 			micfil->soc->dataline);
69747a70e6fSCosmin Samoila 		return -EINVAL;
69847a70e6fSCosmin Samoila 	}
69947a70e6fSCosmin Samoila 
70047a70e6fSCosmin Samoila 	/* get IRQs */
70147a70e6fSCosmin Samoila 	for (i = 0; i < MICFIL_IRQ_LINES; i++) {
70247a70e6fSCosmin Samoila 		micfil->irq[i] = platform_get_irq(pdev, i);
70347a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "GET IRQ: %d\n", micfil->irq[i]);
70483b35f45STang Bin 		if (micfil->irq[i] < 0)
70547a70e6fSCosmin Samoila 			return micfil->irq[i];
70647a70e6fSCosmin Samoila 	}
70747a70e6fSCosmin Samoila 
70847a70e6fSCosmin Samoila 	if (of_property_read_bool(np, "fsl,shared-interrupt"))
70947a70e6fSCosmin Samoila 		irqflag = IRQF_SHARED;
71047a70e6fSCosmin Samoila 
711a62ed960SFabio Estevam 	/* Digital Microphone interface interrupt */
71247a70e6fSCosmin Samoila 	ret = devm_request_irq(&pdev->dev, micfil->irq[0],
71347a70e6fSCosmin Samoila 			       micfil_isr, irqflag,
71447a70e6fSCosmin Samoila 			       micfil->name, micfil);
71547a70e6fSCosmin Samoila 	if (ret) {
71647a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "failed to claim mic interface irq %u\n",
71747a70e6fSCosmin Samoila 			micfil->irq[0]);
71847a70e6fSCosmin Samoila 		return ret;
71947a70e6fSCosmin Samoila 	}
72047a70e6fSCosmin Samoila 
721a62ed960SFabio Estevam 	/* Digital Microphone interface error interrupt */
72247a70e6fSCosmin Samoila 	ret = devm_request_irq(&pdev->dev, micfil->irq[1],
72347a70e6fSCosmin Samoila 			       micfil_err_isr, irqflag,
72447a70e6fSCosmin Samoila 			       micfil->name, micfil);
72547a70e6fSCosmin Samoila 	if (ret) {
72647a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "failed to claim mic interface error irq %u\n",
72747a70e6fSCosmin Samoila 			micfil->irq[1]);
72847a70e6fSCosmin Samoila 		return ret;
72947a70e6fSCosmin Samoila 	}
73047a70e6fSCosmin Samoila 
73147a70e6fSCosmin Samoila 	micfil->dma_params_rx.chan_name = "rx";
73247a70e6fSCosmin Samoila 	micfil->dma_params_rx.addr = res->start + REG_MICFIL_DATACH0;
73347a70e6fSCosmin Samoila 	micfil->dma_params_rx.maxburst = MICFIL_DMA_MAXBURST_RX;
73447a70e6fSCosmin Samoila 
73547a70e6fSCosmin Samoila 
73647a70e6fSCosmin Samoila 	platform_set_drvdata(pdev, micfil);
73747a70e6fSCosmin Samoila 
73847a70e6fSCosmin Samoila 	pm_runtime_enable(&pdev->dev);
739*b5cf28f7SShengjiu Wang 	regcache_cache_only(micfil->regmap, true);
74047a70e6fSCosmin Samoila 
74147a70e6fSCosmin Samoila 	ret = devm_snd_soc_register_component(&pdev->dev, &fsl_micfil_component,
74247a70e6fSCosmin Samoila 					      &fsl_micfil_dai, 1);
74347a70e6fSCosmin Samoila 	if (ret) {
74447a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "failed to register component %s\n",
74547a70e6fSCosmin Samoila 			fsl_micfil_component.name);
74647a70e6fSCosmin Samoila 		return ret;
74747a70e6fSCosmin Samoila 	}
74847a70e6fSCosmin Samoila 
74947a70e6fSCosmin Samoila 	ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
75047a70e6fSCosmin Samoila 	if (ret)
75147a70e6fSCosmin Samoila 		dev_err(&pdev->dev, "failed to pcm register\n");
75247a70e6fSCosmin Samoila 
75347a70e6fSCosmin Samoila 	return ret;
75447a70e6fSCosmin Samoila }
75547a70e6fSCosmin Samoila 
75647a70e6fSCosmin Samoila static int __maybe_unused fsl_micfil_runtime_suspend(struct device *dev)
75747a70e6fSCosmin Samoila {
75847a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = dev_get_drvdata(dev);
75947a70e6fSCosmin Samoila 
76047a70e6fSCosmin Samoila 	regcache_cache_only(micfil->regmap, true);
76147a70e6fSCosmin Samoila 
76247a70e6fSCosmin Samoila 	clk_disable_unprepare(micfil->mclk);
763*b5cf28f7SShengjiu Wang 	clk_disable_unprepare(micfil->busclk);
76447a70e6fSCosmin Samoila 
76547a70e6fSCosmin Samoila 	return 0;
76647a70e6fSCosmin Samoila }
76747a70e6fSCosmin Samoila 
76847a70e6fSCosmin Samoila static int __maybe_unused fsl_micfil_runtime_resume(struct device *dev)
76947a70e6fSCosmin Samoila {
77047a70e6fSCosmin Samoila 	struct fsl_micfil *micfil = dev_get_drvdata(dev);
77147a70e6fSCosmin Samoila 	int ret;
77247a70e6fSCosmin Samoila 
773*b5cf28f7SShengjiu Wang 	ret = clk_prepare_enable(micfil->busclk);
77447a70e6fSCosmin Samoila 	if (ret < 0)
77547a70e6fSCosmin Samoila 		return ret;
77647a70e6fSCosmin Samoila 
777*b5cf28f7SShengjiu Wang 	ret = clk_prepare_enable(micfil->mclk);
778*b5cf28f7SShengjiu Wang 	if (ret < 0) {
779*b5cf28f7SShengjiu Wang 		clk_disable_unprepare(micfil->busclk);
780*b5cf28f7SShengjiu Wang 		return ret;
781*b5cf28f7SShengjiu Wang 	}
782*b5cf28f7SShengjiu Wang 
78347a70e6fSCosmin Samoila 	regcache_cache_only(micfil->regmap, false);
78447a70e6fSCosmin Samoila 	regcache_mark_dirty(micfil->regmap);
78547a70e6fSCosmin Samoila 	regcache_sync(micfil->regmap);
78647a70e6fSCosmin Samoila 
78747a70e6fSCosmin Samoila 	return 0;
78847a70e6fSCosmin Samoila }
78947a70e6fSCosmin Samoila 
79047a70e6fSCosmin Samoila static int __maybe_unused fsl_micfil_suspend(struct device *dev)
79147a70e6fSCosmin Samoila {
79247a70e6fSCosmin Samoila 	pm_runtime_force_suspend(dev);
79347a70e6fSCosmin Samoila 
79447a70e6fSCosmin Samoila 	return 0;
79547a70e6fSCosmin Samoila }
79647a70e6fSCosmin Samoila 
79747a70e6fSCosmin Samoila static int __maybe_unused fsl_micfil_resume(struct device *dev)
79847a70e6fSCosmin Samoila {
79947a70e6fSCosmin Samoila 	pm_runtime_force_resume(dev);
80047a70e6fSCosmin Samoila 
80147a70e6fSCosmin Samoila 	return 0;
80247a70e6fSCosmin Samoila }
80347a70e6fSCosmin Samoila 
80447a70e6fSCosmin Samoila static const struct dev_pm_ops fsl_micfil_pm_ops = {
80547a70e6fSCosmin Samoila 	SET_RUNTIME_PM_OPS(fsl_micfil_runtime_suspend,
80647a70e6fSCosmin Samoila 			   fsl_micfil_runtime_resume,
80747a70e6fSCosmin Samoila 			   NULL)
80847a70e6fSCosmin Samoila 	SET_SYSTEM_SLEEP_PM_OPS(fsl_micfil_suspend,
80947a70e6fSCosmin Samoila 				fsl_micfil_resume)
81047a70e6fSCosmin Samoila };
81147a70e6fSCosmin Samoila 
81247a70e6fSCosmin Samoila static struct platform_driver fsl_micfil_driver = {
81347a70e6fSCosmin Samoila 	.probe = fsl_micfil_probe,
81447a70e6fSCosmin Samoila 	.driver = {
81547a70e6fSCosmin Samoila 		.name = "fsl-micfil-dai",
81647a70e6fSCosmin Samoila 		.pm = &fsl_micfil_pm_ops,
81747a70e6fSCosmin Samoila 		.of_match_table = fsl_micfil_dt_ids,
81847a70e6fSCosmin Samoila 	},
81947a70e6fSCosmin Samoila };
82047a70e6fSCosmin Samoila module_platform_driver(fsl_micfil_driver);
82147a70e6fSCosmin Samoila 
82247a70e6fSCosmin Samoila MODULE_AUTHOR("Cosmin-Gabriel Samoila <cosmin.samoila@nxp.com>");
82347a70e6fSCosmin Samoila MODULE_DESCRIPTION("NXP PDM Microphone Interface (MICFIL) driver");
82447a70e6fSCosmin Samoila MODULE_LICENSE("GPL v2");
825