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