xref: /openbmc/linux/sound/soc/fsl/fsl_esai.c (revision 5be6155b)
13b5af9f1SFabio Estevam // SPDX-License-Identifier: GPL-2.0
23b5af9f1SFabio Estevam //
33b5af9f1SFabio Estevam // Freescale ESAI ALSA SoC Digital Audio Interface (DAI) driver
43b5af9f1SFabio Estevam //
53b5af9f1SFabio Estevam // Copyright (C) 2014 Freescale Semiconductor, Inc.
643d24e76SNicolin Chen 
743d24e76SNicolin Chen #include <linux/clk.h>
843d24e76SNicolin Chen #include <linux/dmaengine.h>
943d24e76SNicolin Chen #include <linux/module.h>
1043d24e76SNicolin Chen #include <linux/of_irq.h>
1143d24e76SNicolin Chen #include <linux/of_platform.h>
12b2d337d8SS.j. Wang #include <linux/pm_runtime.h>
1343d24e76SNicolin Chen #include <sound/dmaengine_pcm.h>
1443d24e76SNicolin Chen #include <sound/pcm_params.h>
1543d24e76SNicolin Chen 
1643d24e76SNicolin Chen #include "fsl_esai.h"
1743d24e76SNicolin Chen #include "imx-pcm.h"
1843d24e76SNicolin Chen 
1943d24e76SNicolin Chen #define FSL_ESAI_FORMATS	(SNDRV_PCM_FMTBIT_S8 | \
2043d24e76SNicolin Chen 				SNDRV_PCM_FMTBIT_S16_LE | \
2143d24e76SNicolin Chen 				SNDRV_PCM_FMTBIT_S20_3LE | \
2243d24e76SNicolin Chen 				SNDRV_PCM_FMTBIT_S24_LE)
2343d24e76SNicolin Chen 
2443d24e76SNicolin Chen /**
2543d24e76SNicolin Chen  * fsl_esai: ESAI private data
2643d24e76SNicolin Chen  *
2743d24e76SNicolin Chen  * @dma_params_rx: DMA parameters for receive channel
2843d24e76SNicolin Chen  * @dma_params_tx: DMA parameters for transmit channel
2943d24e76SNicolin Chen  * @pdev: platform device pointer
3043d24e76SNicolin Chen  * @regmap: regmap handler
3143d24e76SNicolin Chen  * @coreclk: clock source to access register
3243d24e76SNicolin Chen  * @extalclk: esai clock source to derive HCK, SCK and FS
3343d24e76SNicolin Chen  * @fsysclk: system clock source to derive HCK, SCK and FS
34a2a4d604SShengjiu Wang  * @spbaclk: SPBA clock (optional, depending on SoC design)
3543d24e76SNicolin Chen  * @fifo_depth: depth of tx/rx FIFO
3643d24e76SNicolin Chen  * @slot_width: width of each DAI slot
37de0d712aSShengjiu Wang  * @slots: number of slots
385be6155bSShengjiu Wang  * @channels: channel num for tx or rx
3943d24e76SNicolin Chen  * @hck_rate: clock rate of desired HCKx clock
40f975ca46SNicolin Chen  * @sck_rate: clock rate of desired SCKx clock
41f975ca46SNicolin Chen  * @hck_dir: the direction of HCKx pads
4243d24e76SNicolin Chen  * @sck_div: if using PSR/PM dividers for SCKx clock
4343d24e76SNicolin Chen  * @slave_mode: if fully using DAI slave mode
4443d24e76SNicolin Chen  * @synchronous: if using tx/rx synchronous mode
4543d24e76SNicolin Chen  * @name: driver name
4643d24e76SNicolin Chen  */
4743d24e76SNicolin Chen struct fsl_esai {
4843d24e76SNicolin Chen 	struct snd_dmaengine_dai_dma_data dma_params_rx;
4943d24e76SNicolin Chen 	struct snd_dmaengine_dai_dma_data dma_params_tx;
5043d24e76SNicolin Chen 	struct platform_device *pdev;
5143d24e76SNicolin Chen 	struct regmap *regmap;
5243d24e76SNicolin Chen 	struct clk *coreclk;
5343d24e76SNicolin Chen 	struct clk *extalclk;
5443d24e76SNicolin Chen 	struct clk *fsysclk;
55a2a4d604SShengjiu Wang 	struct clk *spbaclk;
5643d24e76SNicolin Chen 	u32 fifo_depth;
5743d24e76SNicolin Chen 	u32 slot_width;
58de0d712aSShengjiu Wang 	u32 slots;
590ff4e8c6SS.j. Wang 	u32 tx_mask;
600ff4e8c6SS.j. Wang 	u32 rx_mask;
615be6155bSShengjiu Wang 	u32 channels[2];
6243d24e76SNicolin Chen 	u32 hck_rate[2];
63f975ca46SNicolin Chen 	u32 sck_rate[2];
64f975ca46SNicolin Chen 	bool hck_dir[2];
6543d24e76SNicolin Chen 	bool sck_div[2];
6643d24e76SNicolin Chen 	bool slave_mode;
6743d24e76SNicolin Chen 	bool synchronous;
6843d24e76SNicolin Chen 	char name[32];
6943d24e76SNicolin Chen };
7043d24e76SNicolin Chen 
7143d24e76SNicolin Chen static irqreturn_t esai_isr(int irq, void *devid)
7243d24e76SNicolin Chen {
7343d24e76SNicolin Chen 	struct fsl_esai *esai_priv = (struct fsl_esai *)devid;
7443d24e76SNicolin Chen 	struct platform_device *pdev = esai_priv->pdev;
7543d24e76SNicolin Chen 	u32 esr;
7643d24e76SNicolin Chen 
7743d24e76SNicolin Chen 	regmap_read(esai_priv->regmap, REG_ESAI_ESR, &esr);
7843d24e76SNicolin Chen 
7943d24e76SNicolin Chen 	if (esr & ESAI_ESR_TINIT_MASK)
803bcc8656SColin Ian King 		dev_dbg(&pdev->dev, "isr: Transmission Initialized\n");
8143d24e76SNicolin Chen 
8243d24e76SNicolin Chen 	if (esr & ESAI_ESR_RFF_MASK)
8343d24e76SNicolin Chen 		dev_warn(&pdev->dev, "isr: Receiving overrun\n");
8443d24e76SNicolin Chen 
8543d24e76SNicolin Chen 	if (esr & ESAI_ESR_TFE_MASK)
863bcc8656SColin Ian King 		dev_warn(&pdev->dev, "isr: Transmission underrun\n");
8743d24e76SNicolin Chen 
8843d24e76SNicolin Chen 	if (esr & ESAI_ESR_TLS_MASK)
8943d24e76SNicolin Chen 		dev_dbg(&pdev->dev, "isr: Just transmitted the last slot\n");
9043d24e76SNicolin Chen 
9143d24e76SNicolin Chen 	if (esr & ESAI_ESR_TDE_MASK)
923bcc8656SColin Ian King 		dev_dbg(&pdev->dev, "isr: Transmission data exception\n");
9343d24e76SNicolin Chen 
9443d24e76SNicolin Chen 	if (esr & ESAI_ESR_TED_MASK)
9543d24e76SNicolin Chen 		dev_dbg(&pdev->dev, "isr: Transmitting even slots\n");
9643d24e76SNicolin Chen 
9743d24e76SNicolin Chen 	if (esr & ESAI_ESR_TD_MASK)
9843d24e76SNicolin Chen 		dev_dbg(&pdev->dev, "isr: Transmitting data\n");
9943d24e76SNicolin Chen 
10043d24e76SNicolin Chen 	if (esr & ESAI_ESR_RLS_MASK)
10143d24e76SNicolin Chen 		dev_dbg(&pdev->dev, "isr: Just received the last slot\n");
10243d24e76SNicolin Chen 
10343d24e76SNicolin Chen 	if (esr & ESAI_ESR_RDE_MASK)
10443d24e76SNicolin Chen 		dev_dbg(&pdev->dev, "isr: Receiving data exception\n");
10543d24e76SNicolin Chen 
10643d24e76SNicolin Chen 	if (esr & ESAI_ESR_RED_MASK)
10743d24e76SNicolin Chen 		dev_dbg(&pdev->dev, "isr: Receiving even slots\n");
10843d24e76SNicolin Chen 
10943d24e76SNicolin Chen 	if (esr & ESAI_ESR_RD_MASK)
11043d24e76SNicolin Chen 		dev_dbg(&pdev->dev, "isr: Receiving data\n");
11143d24e76SNicolin Chen 
11243d24e76SNicolin Chen 	return IRQ_HANDLED;
11343d24e76SNicolin Chen }
11443d24e76SNicolin Chen 
11543d24e76SNicolin Chen /**
11643d24e76SNicolin Chen  * This function is used to calculate the divisors of psr, pm, fp and it is
11743d24e76SNicolin Chen  * supposed to be called in set_dai_sysclk() and set_bclk().
11843d24e76SNicolin Chen  *
11943d24e76SNicolin Chen  * @ratio: desired overall ratio for the paticipating dividers
12043d24e76SNicolin Chen  * @usefp: for HCK setting, there is no need to set fp divider
12143d24e76SNicolin Chen  * @fp: bypass other dividers by setting fp directly if fp != 0
12243d24e76SNicolin Chen  * @tx: current setting is for playback or capture
12343d24e76SNicolin Chen  */
12443d24e76SNicolin Chen static int fsl_esai_divisor_cal(struct snd_soc_dai *dai, bool tx, u32 ratio,
12543d24e76SNicolin Chen 				bool usefp, u32 fp)
12643d24e76SNicolin Chen {
12743d24e76SNicolin Chen 	struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
12843d24e76SNicolin Chen 	u32 psr, pm = 999, maxfp, prod, sub, savesub, i, j;
12943d24e76SNicolin Chen 
13043d24e76SNicolin Chen 	maxfp = usefp ? 16 : 1;
13143d24e76SNicolin Chen 
13243d24e76SNicolin Chen 	if (usefp && fp)
13343d24e76SNicolin Chen 		goto out_fp;
13443d24e76SNicolin Chen 
13543d24e76SNicolin Chen 	if (ratio > 2 * 8 * 256 * maxfp || ratio < 2) {
13643d24e76SNicolin Chen 		dev_err(dai->dev, "the ratio is out of range (2 ~ %d)\n",
13743d24e76SNicolin Chen 				2 * 8 * 256 * maxfp);
13843d24e76SNicolin Chen 		return -EINVAL;
13943d24e76SNicolin Chen 	} else if (ratio % 2) {
14043d24e76SNicolin Chen 		dev_err(dai->dev, "the raio must be even if using upper divider\n");
14143d24e76SNicolin Chen 		return -EINVAL;
14243d24e76SNicolin Chen 	}
14343d24e76SNicolin Chen 
14443d24e76SNicolin Chen 	ratio /= 2;
14543d24e76SNicolin Chen 
14643d24e76SNicolin Chen 	psr = ratio <= 256 * maxfp ? ESAI_xCCR_xPSR_BYPASS : ESAI_xCCR_xPSR_DIV8;
14743d24e76SNicolin Chen 
148c656941dSNicolin Chen 	/* Do not loop-search if PM (1 ~ 256) alone can serve the ratio */
149c656941dSNicolin Chen 	if (ratio <= 256) {
150c656941dSNicolin Chen 		pm = ratio;
151c656941dSNicolin Chen 		fp = 1;
152c656941dSNicolin Chen 		goto out;
153c656941dSNicolin Chen 	}
154c656941dSNicolin Chen 
15543d24e76SNicolin Chen 	/* Set the max fluctuation -- 0.1% of the max devisor */
15643d24e76SNicolin Chen 	savesub = (psr ? 1 : 8)  * 256 * maxfp / 1000;
15743d24e76SNicolin Chen 
15843d24e76SNicolin Chen 	/* Find the best value for PM */
15943d24e76SNicolin Chen 	for (i = 1; i <= 256; i++) {
16043d24e76SNicolin Chen 		for (j = 1; j <= maxfp; j++) {
16143d24e76SNicolin Chen 			/* PSR (1 or 8) * PM (1 ~ 256) * FP (1 ~ 16) */
16243d24e76SNicolin Chen 			prod = (psr ? 1 : 8) * i * j;
16343d24e76SNicolin Chen 
16443d24e76SNicolin Chen 			if (prod == ratio)
16543d24e76SNicolin Chen 				sub = 0;
16643d24e76SNicolin Chen 			else if (prod / ratio == 1)
16743d24e76SNicolin Chen 				sub = prod - ratio;
16843d24e76SNicolin Chen 			else if (ratio / prod == 1)
16943d24e76SNicolin Chen 				sub = ratio - prod;
17043d24e76SNicolin Chen 			else
17143d24e76SNicolin Chen 				continue;
17243d24e76SNicolin Chen 
17343d24e76SNicolin Chen 			/* Calculate the fraction */
17443d24e76SNicolin Chen 			sub = sub * 1000 / ratio;
17543d24e76SNicolin Chen 			if (sub < savesub) {
17643d24e76SNicolin Chen 				savesub = sub;
17743d24e76SNicolin Chen 				pm = i;
17843d24e76SNicolin Chen 				fp = j;
17943d24e76SNicolin Chen 			}
18043d24e76SNicolin Chen 
18143d24e76SNicolin Chen 			/* We are lucky */
18243d24e76SNicolin Chen 			if (savesub == 0)
18343d24e76SNicolin Chen 				goto out;
18443d24e76SNicolin Chen 		}
18543d24e76SNicolin Chen 	}
18643d24e76SNicolin Chen 
18743d24e76SNicolin Chen 	if (pm == 999) {
18843d24e76SNicolin Chen 		dev_err(dai->dev, "failed to calculate proper divisors\n");
18943d24e76SNicolin Chen 		return -EINVAL;
19043d24e76SNicolin Chen 	}
19143d24e76SNicolin Chen 
19243d24e76SNicolin Chen out:
19343d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xCCR(tx),
19443d24e76SNicolin Chen 			   ESAI_xCCR_xPSR_MASK | ESAI_xCCR_xPM_MASK,
19543d24e76SNicolin Chen 			   psr | ESAI_xCCR_xPM(pm));
19643d24e76SNicolin Chen 
19743d24e76SNicolin Chen out_fp:
19843d24e76SNicolin Chen 	/* Bypass fp if not being required */
19943d24e76SNicolin Chen 	if (maxfp <= 1)
20043d24e76SNicolin Chen 		return 0;
20143d24e76SNicolin Chen 
20243d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xCCR(tx),
20343d24e76SNicolin Chen 			   ESAI_xCCR_xFP_MASK, ESAI_xCCR_xFP(fp));
20443d24e76SNicolin Chen 
20543d24e76SNicolin Chen 	return 0;
20643d24e76SNicolin Chen }
20743d24e76SNicolin Chen 
20843d24e76SNicolin Chen /**
20943d24e76SNicolin Chen  * This function mainly configures the clock frequency of MCLK (HCKT/HCKR)
21043d24e76SNicolin Chen  *
21143d24e76SNicolin Chen  * @Parameters:
21243d24e76SNicolin Chen  * clk_id: The clock source of HCKT/HCKR
21343d24e76SNicolin Chen  *	  (Input from outside; output from inside, FSYS or EXTAL)
21443d24e76SNicolin Chen  * freq: The required clock rate of HCKT/HCKR
21543d24e76SNicolin Chen  * dir: The clock direction of HCKT/HCKR
21643d24e76SNicolin Chen  *
21743d24e76SNicolin Chen  * Note: If the direction is input, we do not care about clk_id.
21843d24e76SNicolin Chen  */
21943d24e76SNicolin Chen static int fsl_esai_set_dai_sysclk(struct snd_soc_dai *dai, int clk_id,
22043d24e76SNicolin Chen 				   unsigned int freq, int dir)
22143d24e76SNicolin Chen {
22243d24e76SNicolin Chen 	struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
22343d24e76SNicolin Chen 	struct clk *clksrc = esai_priv->extalclk;
2241997ee89SS.j. Wang 	bool tx = (clk_id <= ESAI_HCKT_EXTAL || esai_priv->synchronous);
22543d24e76SNicolin Chen 	bool in = dir == SND_SOC_CLOCK_IN;
2263e185238SXiubo Li 	u32 ratio, ecr = 0;
22743d24e76SNicolin Chen 	unsigned long clk_rate;
2283e185238SXiubo Li 	int ret;
22943d24e76SNicolin Chen 
2308a2278b7SNicolin Chen 	if (freq == 0) {
2318a2278b7SNicolin Chen 		dev_err(dai->dev, "%sput freq of HCK%c should not be 0Hz\n",
2328a2278b7SNicolin Chen 			in ? "in" : "out", tx ? 'T' : 'R');
2338a2278b7SNicolin Chen 		return -EINVAL;
2348a2278b7SNicolin Chen 	}
2358a2278b7SNicolin Chen 
236f975ca46SNicolin Chen 	/* Bypass divider settings if the requirement doesn't change */
237f975ca46SNicolin Chen 	if (freq == esai_priv->hck_rate[tx] && dir == esai_priv->hck_dir[tx])
238f975ca46SNicolin Chen 		return 0;
23943d24e76SNicolin Chen 
24043d24e76SNicolin Chen 	/* sck_div can be only bypassed if ETO/ERO=0 and SNC_SOC_CLOCK_OUT */
24143d24e76SNicolin Chen 	esai_priv->sck_div[tx] = true;
24243d24e76SNicolin Chen 
24343d24e76SNicolin Chen 	/* Set the direction of HCKT/HCKR pins */
24443d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xCCR(tx),
24543d24e76SNicolin Chen 			   ESAI_xCCR_xHCKD, in ? 0 : ESAI_xCCR_xHCKD);
24643d24e76SNicolin Chen 
24743d24e76SNicolin Chen 	if (in)
24843d24e76SNicolin Chen 		goto out;
24943d24e76SNicolin Chen 
25043d24e76SNicolin Chen 	switch (clk_id) {
25143d24e76SNicolin Chen 	case ESAI_HCKT_FSYS:
25243d24e76SNicolin Chen 	case ESAI_HCKR_FSYS:
25343d24e76SNicolin Chen 		clksrc = esai_priv->fsysclk;
25443d24e76SNicolin Chen 		break;
25543d24e76SNicolin Chen 	case ESAI_HCKT_EXTAL:
25643d24e76SNicolin Chen 		ecr |= ESAI_ECR_ETI;
257903c220bSS.j. Wang 		break;
25843d24e76SNicolin Chen 	case ESAI_HCKR_EXTAL:
2591997ee89SS.j. Wang 		ecr |= esai_priv->synchronous ? ESAI_ECR_ETI : ESAI_ECR_ERI;
26043d24e76SNicolin Chen 		break;
26143d24e76SNicolin Chen 	default:
26243d24e76SNicolin Chen 		return -EINVAL;
26343d24e76SNicolin Chen 	}
26443d24e76SNicolin Chen 
26543d24e76SNicolin Chen 	if (IS_ERR(clksrc)) {
26643d24e76SNicolin Chen 		dev_err(dai->dev, "no assigned %s clock\n",
26743d24e76SNicolin Chen 				clk_id % 2 ? "extal" : "fsys");
26843d24e76SNicolin Chen 		return PTR_ERR(clksrc);
26943d24e76SNicolin Chen 	}
27043d24e76SNicolin Chen 	clk_rate = clk_get_rate(clksrc);
27143d24e76SNicolin Chen 
27243d24e76SNicolin Chen 	ratio = clk_rate / freq;
27343d24e76SNicolin Chen 	if (ratio * freq > clk_rate)
27443d24e76SNicolin Chen 		ret = ratio * freq - clk_rate;
27543d24e76SNicolin Chen 	else if (ratio * freq < clk_rate)
27643d24e76SNicolin Chen 		ret = clk_rate - ratio * freq;
27743d24e76SNicolin Chen 	else
27843d24e76SNicolin Chen 		ret = 0;
27943d24e76SNicolin Chen 
28043d24e76SNicolin Chen 	/* Block if clock source can not be divided into the required rate */
28143d24e76SNicolin Chen 	if (ret != 0 && clk_rate / ret < 1000) {
28243d24e76SNicolin Chen 		dev_err(dai->dev, "failed to derive required HCK%c rate\n",
28343d24e76SNicolin Chen 				tx ? 'T' : 'R');
28443d24e76SNicolin Chen 		return -EINVAL;
28543d24e76SNicolin Chen 	}
28643d24e76SNicolin Chen 
28757ebbcafSNicolin Chen 	/* Only EXTAL source can be output directly without using PSR and PM */
28857ebbcafSNicolin Chen 	if (ratio == 1 && clksrc == esai_priv->extalclk) {
28943d24e76SNicolin Chen 		/* Bypass all the dividers if not being needed */
29043d24e76SNicolin Chen 		ecr |= tx ? ESAI_ECR_ETO : ESAI_ECR_ERO;
29143d24e76SNicolin Chen 		goto out;
29257ebbcafSNicolin Chen 	} else if (ratio < 2) {
29357ebbcafSNicolin Chen 		/* The ratio should be no less than 2 if using other sources */
29457ebbcafSNicolin Chen 		dev_err(dai->dev, "failed to derive required HCK%c rate\n",
29557ebbcafSNicolin Chen 				tx ? 'T' : 'R');
29657ebbcafSNicolin Chen 		return -EINVAL;
29743d24e76SNicolin Chen 	}
29843d24e76SNicolin Chen 
29943d24e76SNicolin Chen 	ret = fsl_esai_divisor_cal(dai, tx, ratio, false, 0);
30043d24e76SNicolin Chen 	if (ret)
30143d24e76SNicolin Chen 		return ret;
30243d24e76SNicolin Chen 
30343d24e76SNicolin Chen 	esai_priv->sck_div[tx] = false;
30443d24e76SNicolin Chen 
30543d24e76SNicolin Chen out:
306f975ca46SNicolin Chen 	esai_priv->hck_dir[tx] = dir;
30743d24e76SNicolin Chen 	esai_priv->hck_rate[tx] = freq;
30843d24e76SNicolin Chen 
30943d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_ECR,
31043d24e76SNicolin Chen 			   tx ? ESAI_ECR_ETI | ESAI_ECR_ETO :
31143d24e76SNicolin Chen 			   ESAI_ECR_ERI | ESAI_ECR_ERO, ecr);
31243d24e76SNicolin Chen 
31343d24e76SNicolin Chen 	return 0;
31443d24e76SNicolin Chen }
31543d24e76SNicolin Chen 
31643d24e76SNicolin Chen /**
31743d24e76SNicolin Chen  * This function configures the related dividers according to the bclk rate
31843d24e76SNicolin Chen  */
31943d24e76SNicolin Chen static int fsl_esai_set_bclk(struct snd_soc_dai *dai, bool tx, u32 freq)
32043d24e76SNicolin Chen {
32143d24e76SNicolin Chen 	struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
32243d24e76SNicolin Chen 	u32 hck_rate = esai_priv->hck_rate[tx];
32343d24e76SNicolin Chen 	u32 sub, ratio = hck_rate / freq;
324f975ca46SNicolin Chen 	int ret;
32543d24e76SNicolin Chen 
326f975ca46SNicolin Chen 	/* Don't apply for fully slave mode or unchanged bclk */
327f975ca46SNicolin Chen 	if (esai_priv->slave_mode || esai_priv->sck_rate[tx] == freq)
32843d24e76SNicolin Chen 		return 0;
32943d24e76SNicolin Chen 
33043d24e76SNicolin Chen 	if (ratio * freq > hck_rate)
33143d24e76SNicolin Chen 		sub = ratio * freq - hck_rate;
33243d24e76SNicolin Chen 	else if (ratio * freq < hck_rate)
33343d24e76SNicolin Chen 		sub = hck_rate - ratio * freq;
33443d24e76SNicolin Chen 	else
33543d24e76SNicolin Chen 		sub = 0;
33643d24e76SNicolin Chen 
33743d24e76SNicolin Chen 	/* Block if clock source can not be divided into the required rate */
33843d24e76SNicolin Chen 	if (sub != 0 && hck_rate / sub < 1000) {
33943d24e76SNicolin Chen 		dev_err(dai->dev, "failed to derive required SCK%c rate\n",
34043d24e76SNicolin Chen 				tx ? 'T' : 'R');
34143d24e76SNicolin Chen 		return -EINVAL;
34243d24e76SNicolin Chen 	}
34343d24e76SNicolin Chen 
34489e47f62SNicolin Chen 	/* The ratio should be contented by FP alone if bypassing PM and PSR */
34589e47f62SNicolin Chen 	if (!esai_priv->sck_div[tx] && (ratio > 16 || ratio == 0)) {
34643d24e76SNicolin Chen 		dev_err(dai->dev, "the ratio is out of range (1 ~ 16)\n");
34743d24e76SNicolin Chen 		return -EINVAL;
34843d24e76SNicolin Chen 	}
34943d24e76SNicolin Chen 
350f975ca46SNicolin Chen 	ret = fsl_esai_divisor_cal(dai, tx, ratio, true,
35143d24e76SNicolin Chen 			esai_priv->sck_div[tx] ? 0 : ratio);
352f975ca46SNicolin Chen 	if (ret)
353f975ca46SNicolin Chen 		return ret;
354f975ca46SNicolin Chen 
355f975ca46SNicolin Chen 	/* Save current bclk rate */
356f975ca46SNicolin Chen 	esai_priv->sck_rate[tx] = freq;
357f975ca46SNicolin Chen 
358f975ca46SNicolin Chen 	return 0;
35943d24e76SNicolin Chen }
36043d24e76SNicolin Chen 
36143d24e76SNicolin Chen static int fsl_esai_set_dai_tdm_slot(struct snd_soc_dai *dai, u32 tx_mask,
36243d24e76SNicolin Chen 				     u32 rx_mask, int slots, int slot_width)
36343d24e76SNicolin Chen {
36443d24e76SNicolin Chen 	struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
36543d24e76SNicolin Chen 
36643d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_TCCR,
36743d24e76SNicolin Chen 			   ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(slots));
36843d24e76SNicolin Chen 
36943d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_RCCR,
37043d24e76SNicolin Chen 			   ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(slots));
37143d24e76SNicolin Chen 
37243d24e76SNicolin Chen 	esai_priv->slot_width = slot_width;
373de0d712aSShengjiu Wang 	esai_priv->slots = slots;
3740ff4e8c6SS.j. Wang 	esai_priv->tx_mask = tx_mask;
3750ff4e8c6SS.j. Wang 	esai_priv->rx_mask = rx_mask;
37643d24e76SNicolin Chen 
37743d24e76SNicolin Chen 	return 0;
37843d24e76SNicolin Chen }
37943d24e76SNicolin Chen 
38043d24e76SNicolin Chen static int fsl_esai_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
38143d24e76SNicolin Chen {
38243d24e76SNicolin Chen 	struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
38343d24e76SNicolin Chen 	u32 xcr = 0, xccr = 0, mask;
38443d24e76SNicolin Chen 
38543d24e76SNicolin Chen 	/* DAI mode */
38643d24e76SNicolin Chen 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
38743d24e76SNicolin Chen 	case SND_SOC_DAIFMT_I2S:
38843d24e76SNicolin Chen 		/* Data on rising edge of bclk, frame low, 1clk before data */
38943d24e76SNicolin Chen 		xcr |= ESAI_xCR_xFSR;
39043d24e76SNicolin Chen 		xccr |= ESAI_xCCR_xFSP | ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP;
39143d24e76SNicolin Chen 		break;
39243d24e76SNicolin Chen 	case SND_SOC_DAIFMT_LEFT_J:
39343d24e76SNicolin Chen 		/* Data on rising edge of bclk, frame high */
39443d24e76SNicolin Chen 		xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP;
39543d24e76SNicolin Chen 		break;
39643d24e76SNicolin Chen 	case SND_SOC_DAIFMT_RIGHT_J:
39743d24e76SNicolin Chen 		/* Data on rising edge of bclk, frame high, right aligned */
398cc29ea00SS.j. Wang 		xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP;
399cc29ea00SS.j. Wang 		xcr  |= ESAI_xCR_xWA;
40043d24e76SNicolin Chen 		break;
40143d24e76SNicolin Chen 	case SND_SOC_DAIFMT_DSP_A:
40243d24e76SNicolin Chen 		/* Data on rising edge of bclk, frame high, 1clk before data */
40343d24e76SNicolin Chen 		xcr |= ESAI_xCR_xFSL | ESAI_xCR_xFSR;
40443d24e76SNicolin Chen 		xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP;
40543d24e76SNicolin Chen 		break;
40643d24e76SNicolin Chen 	case SND_SOC_DAIFMT_DSP_B:
40743d24e76SNicolin Chen 		/* Data on rising edge of bclk, frame high */
40843d24e76SNicolin Chen 		xcr |= ESAI_xCR_xFSL;
40943d24e76SNicolin Chen 		xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP;
41043d24e76SNicolin Chen 		break;
41143d24e76SNicolin Chen 	default:
41243d24e76SNicolin Chen 		return -EINVAL;
41343d24e76SNicolin Chen 	}
41443d24e76SNicolin Chen 
41543d24e76SNicolin Chen 	/* DAI clock inversion */
41643d24e76SNicolin Chen 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
41743d24e76SNicolin Chen 	case SND_SOC_DAIFMT_NB_NF:
41843d24e76SNicolin Chen 		/* Nothing to do for both normal cases */
41943d24e76SNicolin Chen 		break;
42043d24e76SNicolin Chen 	case SND_SOC_DAIFMT_IB_NF:
42143d24e76SNicolin Chen 		/* Invert bit clock */
42243d24e76SNicolin Chen 		xccr ^= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP;
42343d24e76SNicolin Chen 		break;
42443d24e76SNicolin Chen 	case SND_SOC_DAIFMT_NB_IF:
42543d24e76SNicolin Chen 		/* Invert frame clock */
42643d24e76SNicolin Chen 		xccr ^= ESAI_xCCR_xFSP;
42743d24e76SNicolin Chen 		break;
42843d24e76SNicolin Chen 	case SND_SOC_DAIFMT_IB_IF:
42943d24e76SNicolin Chen 		/* Invert both clocks */
43043d24e76SNicolin Chen 		xccr ^= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP | ESAI_xCCR_xFSP;
43143d24e76SNicolin Chen 		break;
43243d24e76SNicolin Chen 	default:
43343d24e76SNicolin Chen 		return -EINVAL;
43443d24e76SNicolin Chen 	}
43543d24e76SNicolin Chen 
43643d24e76SNicolin Chen 	esai_priv->slave_mode = false;
43743d24e76SNicolin Chen 
43843d24e76SNicolin Chen 	/* DAI clock master masks */
43943d24e76SNicolin Chen 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
44043d24e76SNicolin Chen 	case SND_SOC_DAIFMT_CBM_CFM:
44143d24e76SNicolin Chen 		esai_priv->slave_mode = true;
44243d24e76SNicolin Chen 		break;
44343d24e76SNicolin Chen 	case SND_SOC_DAIFMT_CBS_CFM:
44443d24e76SNicolin Chen 		xccr |= ESAI_xCCR_xCKD;
44543d24e76SNicolin Chen 		break;
44643d24e76SNicolin Chen 	case SND_SOC_DAIFMT_CBM_CFS:
44743d24e76SNicolin Chen 		xccr |= ESAI_xCCR_xFSD;
44843d24e76SNicolin Chen 		break;
44943d24e76SNicolin Chen 	case SND_SOC_DAIFMT_CBS_CFS:
45043d24e76SNicolin Chen 		xccr |= ESAI_xCCR_xFSD | ESAI_xCCR_xCKD;
45143d24e76SNicolin Chen 		break;
45243d24e76SNicolin Chen 	default:
45343d24e76SNicolin Chen 		return -EINVAL;
45443d24e76SNicolin Chen 	}
45543d24e76SNicolin Chen 
456cc29ea00SS.j. Wang 	mask = ESAI_xCR_xFSL | ESAI_xCR_xFSR | ESAI_xCR_xWA;
45743d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR, mask, xcr);
45843d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_RCR, mask, xcr);
45943d24e76SNicolin Chen 
46043d24e76SNicolin Chen 	mask = ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP | ESAI_xCCR_xFSP |
461cc29ea00SS.j. Wang 		ESAI_xCCR_xFSD | ESAI_xCCR_xCKD;
46243d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_TCCR, mask, xccr);
46343d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_RCCR, mask, xccr);
46443d24e76SNicolin Chen 
46543d24e76SNicolin Chen 	return 0;
46643d24e76SNicolin Chen }
46743d24e76SNicolin Chen 
46843d24e76SNicolin Chen static int fsl_esai_startup(struct snd_pcm_substream *substream,
46943d24e76SNicolin Chen 			    struct snd_soc_dai *dai)
47043d24e76SNicolin Chen {
47143d24e76SNicolin Chen 	struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
47243d24e76SNicolin Chen 
47343d24e76SNicolin Chen 	if (!dai->active) {
47443d24e76SNicolin Chen 		/* Set synchronous mode */
47543d24e76SNicolin Chen 		regmap_update_bits(esai_priv->regmap, REG_ESAI_SAICR,
47643d24e76SNicolin Chen 				   ESAI_SAICR_SYNC, esai_priv->synchronous ?
47743d24e76SNicolin Chen 				   ESAI_SAICR_SYNC : 0);
47843d24e76SNicolin Chen 
47943d24e76SNicolin Chen 		/* Set a default slot number -- 2 */
48043d24e76SNicolin Chen 		regmap_update_bits(esai_priv->regmap, REG_ESAI_TCCR,
48143d24e76SNicolin Chen 				   ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(2));
48243d24e76SNicolin Chen 		regmap_update_bits(esai_priv->regmap, REG_ESAI_RCCR,
48343d24e76SNicolin Chen 				   ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(2));
48443d24e76SNicolin Chen 	}
48543d24e76SNicolin Chen 
48643d24e76SNicolin Chen 	return 0;
48733529ec9SFabio Estevam 
48843d24e76SNicolin Chen }
48943d24e76SNicolin Chen 
49043d24e76SNicolin Chen static int fsl_esai_hw_params(struct snd_pcm_substream *substream,
49143d24e76SNicolin Chen 			      struct snd_pcm_hw_params *params,
49243d24e76SNicolin Chen 			      struct snd_soc_dai *dai)
49343d24e76SNicolin Chen {
49443d24e76SNicolin Chen 	struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
49543d24e76SNicolin Chen 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
4964ca73043SZidan Wang 	u32 width = params_width(params);
49743d24e76SNicolin Chen 	u32 channels = params_channels(params);
498de0d712aSShengjiu Wang 	u32 pins = DIV_ROUND_UP(channels, esai_priv->slots);
49986ea522bSNicolin Chen 	u32 slot_width = width;
5003e185238SXiubo Li 	u32 bclk, mask, val;
5013e185238SXiubo Li 	int ret;
50243d24e76SNicolin Chen 
503d8ffcf71SGeert Uytterhoeven 	/* Override slot_width if being specifically set */
50486ea522bSNicolin Chen 	if (esai_priv->slot_width)
50586ea522bSNicolin Chen 		slot_width = esai_priv->slot_width;
50686ea522bSNicolin Chen 
50786ea522bSNicolin Chen 	bclk = params_rate(params) * slot_width * esai_priv->slots;
50843d24e76SNicolin Chen 
5091997ee89SS.j. Wang 	ret = fsl_esai_set_bclk(dai, esai_priv->synchronous || tx, bclk);
51043d24e76SNicolin Chen 	if (ret)
51143d24e76SNicolin Chen 		return ret;
51243d24e76SNicolin Chen 
5131997ee89SS.j. Wang 	mask = ESAI_xCR_xSWS_MASK;
5141997ee89SS.j. Wang 	val = ESAI_xCR_xSWS(slot_width, width);
5151997ee89SS.j. Wang 
5161997ee89SS.j. Wang 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), mask, val);
5171997ee89SS.j. Wang 	/* Recording in synchronous mode needs to set TCR also */
5181997ee89SS.j. Wang 	if (!tx && esai_priv->synchronous)
5191997ee89SS.j. Wang 		regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR, mask, val);
5201997ee89SS.j. Wang 
52143d24e76SNicolin Chen 	/* Use Normal mode to support monaural audio */
52243d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx),
52343d24e76SNicolin Chen 			   ESAI_xCR_xMOD_MASK, params_channels(params) > 1 ?
52443d24e76SNicolin Chen 			   ESAI_xCR_xMOD_NETWORK : 0);
52543d24e76SNicolin Chen 
52643d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx),
52743d24e76SNicolin Chen 			   ESAI_xFCR_xFR_MASK, ESAI_xFCR_xFR);
52843d24e76SNicolin Chen 
52943d24e76SNicolin Chen 	mask = ESAI_xFCR_xFR_MASK | ESAI_xFCR_xWA_MASK | ESAI_xFCR_xFWM_MASK |
53043d24e76SNicolin Chen 	      (tx ? ESAI_xFCR_TE_MASK | ESAI_xFCR_TIEN : ESAI_xFCR_RE_MASK);
53143d24e76SNicolin Chen 	val = ESAI_xFCR_xWA(width) | ESAI_xFCR_xFWM(esai_priv->fifo_depth) |
532de0d712aSShengjiu Wang 	     (tx ? ESAI_xFCR_TE(pins) | ESAI_xFCR_TIEN : ESAI_xFCR_RE(pins));
53343d24e76SNicolin Chen 
53443d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), mask, val);
53543d24e76SNicolin Chen 
5361997ee89SS.j. Wang 	if (tx)
5371997ee89SS.j. Wang 		regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR,
5381997ee89SS.j. Wang 				ESAI_xCR_PADC, ESAI_xCR_PADC);
53943d24e76SNicolin Chen 
5404f8210f6SNicolin Chen 	/* Remove ESAI personal reset by configuring ESAI_PCRC and ESAI_PRRC */
5414f8210f6SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_PRRC,
5424f8210f6SNicolin Chen 			   ESAI_PRRC_PDC_MASK, ESAI_PRRC_PDC(ESAI_GPIO));
5434f8210f6SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_PCRC,
5444f8210f6SNicolin Chen 			   ESAI_PCRC_PC_MASK, ESAI_PCRC_PC(ESAI_GPIO));
54543d24e76SNicolin Chen 	return 0;
54643d24e76SNicolin Chen }
54743d24e76SNicolin Chen 
5485be6155bSShengjiu Wang static int fsl_esai_hw_init(struct fsl_esai *esai_priv)
54943d24e76SNicolin Chen {
5505be6155bSShengjiu Wang 	struct platform_device *pdev = esai_priv->pdev;
5515be6155bSShengjiu Wang 	int ret;
5525be6155bSShengjiu Wang 
5535be6155bSShengjiu Wang 	/* Reset ESAI unit */
5545be6155bSShengjiu Wang 	ret = regmap_update_bits(esai_priv->regmap, REG_ESAI_ECR,
5555be6155bSShengjiu Wang 				 ESAI_ECR_ESAIEN_MASK | ESAI_ECR_ERST_MASK,
5565be6155bSShengjiu Wang 				 ESAI_ECR_ESAIEN | ESAI_ECR_ERST);
5575be6155bSShengjiu Wang 	if (ret) {
5585be6155bSShengjiu Wang 		dev_err(&pdev->dev, "failed to reset ESAI: %d\n", ret);
5595be6155bSShengjiu Wang 		return ret;
5605be6155bSShengjiu Wang 	}
5615be6155bSShengjiu Wang 
5625be6155bSShengjiu Wang 	/*
5635be6155bSShengjiu Wang 	 * We need to enable ESAI so as to access some of its registers.
5645be6155bSShengjiu Wang 	 * Otherwise, we would fail to dump regmap from user space.
5655be6155bSShengjiu Wang 	 */
5665be6155bSShengjiu Wang 	ret = regmap_update_bits(esai_priv->regmap, REG_ESAI_ECR,
5675be6155bSShengjiu Wang 				 ESAI_ECR_ESAIEN_MASK | ESAI_ECR_ERST_MASK,
5685be6155bSShengjiu Wang 				 ESAI_ECR_ESAIEN);
5695be6155bSShengjiu Wang 	if (ret) {
5705be6155bSShengjiu Wang 		dev_err(&pdev->dev, "failed to enable ESAI: %d\n", ret);
5715be6155bSShengjiu Wang 		return ret;
5725be6155bSShengjiu Wang 	}
5735be6155bSShengjiu Wang 
5745be6155bSShengjiu Wang 	regmap_update_bits(esai_priv->regmap, REG_ESAI_PRRC,
5755be6155bSShengjiu Wang 			   ESAI_PRRC_PDC_MASK, 0);
5765be6155bSShengjiu Wang 	regmap_update_bits(esai_priv->regmap, REG_ESAI_PCRC,
5775be6155bSShengjiu Wang 			   ESAI_PCRC_PC_MASK, 0);
5785be6155bSShengjiu Wang 
5795be6155bSShengjiu Wang 	return 0;
5805be6155bSShengjiu Wang }
5815be6155bSShengjiu Wang 
5825be6155bSShengjiu Wang static int fsl_esai_register_restore(struct fsl_esai *esai_priv)
5835be6155bSShengjiu Wang {
5845be6155bSShengjiu Wang 	int ret;
5855be6155bSShengjiu Wang 
5865be6155bSShengjiu Wang 	/* FIFO reset for safety */
5875be6155bSShengjiu Wang 	regmap_update_bits(esai_priv->regmap, REG_ESAI_TFCR,
5885be6155bSShengjiu Wang 			   ESAI_xFCR_xFR, ESAI_xFCR_xFR);
5895be6155bSShengjiu Wang 	regmap_update_bits(esai_priv->regmap, REG_ESAI_RFCR,
5905be6155bSShengjiu Wang 			   ESAI_xFCR_xFR, ESAI_xFCR_xFR);
5915be6155bSShengjiu Wang 
5925be6155bSShengjiu Wang 	regcache_mark_dirty(esai_priv->regmap);
5935be6155bSShengjiu Wang 	ret = regcache_sync(esai_priv->regmap);
5945be6155bSShengjiu Wang 	if (ret)
5955be6155bSShengjiu Wang 		return ret;
5965be6155bSShengjiu Wang 
5975be6155bSShengjiu Wang 	/* FIFO reset done */
5985be6155bSShengjiu Wang 	regmap_update_bits(esai_priv->regmap, REG_ESAI_TFCR, ESAI_xFCR_xFR, 0);
5995be6155bSShengjiu Wang 	regmap_update_bits(esai_priv->regmap, REG_ESAI_RFCR, ESAI_xFCR_xFR, 0);
6005be6155bSShengjiu Wang 
6015be6155bSShengjiu Wang 	return 0;
6025be6155bSShengjiu Wang }
6035be6155bSShengjiu Wang 
6045be6155bSShengjiu Wang static void fsl_esai_trigger_start(struct fsl_esai *esai_priv, bool tx)
6055be6155bSShengjiu Wang {
6065be6155bSShengjiu Wang 	u8 i, channels = esai_priv->channels[tx];
607de0d712aSShengjiu Wang 	u32 pins = DIV_ROUND_UP(channels, esai_priv->slots);
6080ff4e8c6SS.j. Wang 	u32 mask;
60943d24e76SNicolin Chen 
61043d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx),
61143d24e76SNicolin Chen 			   ESAI_xFCR_xFEN_MASK, ESAI_xFCR_xFEN);
61243d24e76SNicolin Chen 
61343d24e76SNicolin Chen 	/* Write initial words reqiured by ESAI as normal procedure */
61443d24e76SNicolin Chen 	for (i = 0; tx && i < channels; i++)
61543d24e76SNicolin Chen 		regmap_write(esai_priv->regmap, REG_ESAI_ETDR, 0x0);
61643d24e76SNicolin Chen 
6170ff4e8c6SS.j. Wang 	/*
6180ff4e8c6SS.j. Wang 	 * When set the TE/RE in the end of enablement flow, there
6190ff4e8c6SS.j. Wang 	 * will be channel swap issue for multi data line case.
6200ff4e8c6SS.j. Wang 	 * In order to workaround this issue, we switch the bit
6210ff4e8c6SS.j. Wang 	 * enablement sequence to below sequence
6220ff4e8c6SS.j. Wang 	 * 1) clear the xSMB & xSMA: which is done in probe and
6230ff4e8c6SS.j. Wang 	 *                           stop state.
6240ff4e8c6SS.j. Wang 	 * 2) set TE/RE
6250ff4e8c6SS.j. Wang 	 * 3) set xSMB
6260ff4e8c6SS.j. Wang 	 * 4) set xSMA:  xSMA is the last one in this flow, which
6270ff4e8c6SS.j. Wang 	 *               will trigger esai to start.
6280ff4e8c6SS.j. Wang 	 */
62943d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx),
63043d24e76SNicolin Chen 			   tx ? ESAI_xCR_TE_MASK : ESAI_xCR_RE_MASK,
631de0d712aSShengjiu Wang 			   tx ? ESAI_xCR_TE(pins) : ESAI_xCR_RE(pins));
6320ff4e8c6SS.j. Wang 	mask = tx ? esai_priv->tx_mask : esai_priv->rx_mask;
6330ff4e8c6SS.j. Wang 
6340ff4e8c6SS.j. Wang 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xSMB(tx),
6350ff4e8c6SS.j. Wang 			   ESAI_xSMB_xS_MASK, ESAI_xSMB_xS(mask));
6360ff4e8c6SS.j. Wang 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xSMA(tx),
6370ff4e8c6SS.j. Wang 			   ESAI_xSMA_xS_MASK, ESAI_xSMA_xS(mask));
6385be6155bSShengjiu Wang }
6390ff4e8c6SS.j. Wang 
6405be6155bSShengjiu Wang static void fsl_esai_trigger_stop(struct fsl_esai *esai_priv, bool tx)
6415be6155bSShengjiu Wang {
64243d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx),
64343d24e76SNicolin Chen 			   tx ? ESAI_xCR_TE_MASK : ESAI_xCR_RE_MASK, 0);
6440ff4e8c6SS.j. Wang 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xSMA(tx),
6450ff4e8c6SS.j. Wang 			   ESAI_xSMA_xS_MASK, 0);
6460ff4e8c6SS.j. Wang 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xSMB(tx),
6470ff4e8c6SS.j. Wang 			   ESAI_xSMB_xS_MASK, 0);
64843d24e76SNicolin Chen 
64943d24e76SNicolin Chen 	/* Disable and reset FIFO */
65043d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx),
65143d24e76SNicolin Chen 			   ESAI_xFCR_xFR | ESAI_xFCR_xFEN, ESAI_xFCR_xFR);
65243d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx),
65343d24e76SNicolin Chen 			   ESAI_xFCR_xFR, 0);
6545be6155bSShengjiu Wang }
6555be6155bSShengjiu Wang 
6565be6155bSShengjiu Wang static int fsl_esai_trigger(struct snd_pcm_substream *substream, int cmd,
6575be6155bSShengjiu Wang 			    struct snd_soc_dai *dai)
6585be6155bSShengjiu Wang {
6595be6155bSShengjiu Wang 	struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
6605be6155bSShengjiu Wang 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
6615be6155bSShengjiu Wang 
6625be6155bSShengjiu Wang 	esai_priv->channels[tx] = substream->runtime->channels;
6635be6155bSShengjiu Wang 
6645be6155bSShengjiu Wang 	switch (cmd) {
6655be6155bSShengjiu Wang 	case SNDRV_PCM_TRIGGER_START:
6665be6155bSShengjiu Wang 	case SNDRV_PCM_TRIGGER_RESUME:
6675be6155bSShengjiu Wang 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
6685be6155bSShengjiu Wang 		fsl_esai_trigger_start(esai_priv, tx);
6695be6155bSShengjiu Wang 		break;
6705be6155bSShengjiu Wang 	case SNDRV_PCM_TRIGGER_SUSPEND:
6715be6155bSShengjiu Wang 	case SNDRV_PCM_TRIGGER_STOP:
6725be6155bSShengjiu Wang 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
6735be6155bSShengjiu Wang 		fsl_esai_trigger_stop(esai_priv, tx);
67443d24e76SNicolin Chen 		break;
67543d24e76SNicolin Chen 	default:
67643d24e76SNicolin Chen 		return -EINVAL;
67743d24e76SNicolin Chen 	}
67843d24e76SNicolin Chen 
67943d24e76SNicolin Chen 	return 0;
68043d24e76SNicolin Chen }
68143d24e76SNicolin Chen 
6825d29e95eSGustavo A. R. Silva static const struct snd_soc_dai_ops fsl_esai_dai_ops = {
68343d24e76SNicolin Chen 	.startup = fsl_esai_startup,
68443d24e76SNicolin Chen 	.trigger = fsl_esai_trigger,
68543d24e76SNicolin Chen 	.hw_params = fsl_esai_hw_params,
68643d24e76SNicolin Chen 	.set_sysclk = fsl_esai_set_dai_sysclk,
68743d24e76SNicolin Chen 	.set_fmt = fsl_esai_set_dai_fmt,
68843d24e76SNicolin Chen 	.set_tdm_slot = fsl_esai_set_dai_tdm_slot,
68943d24e76SNicolin Chen };
69043d24e76SNicolin Chen 
69143d24e76SNicolin Chen static int fsl_esai_dai_probe(struct snd_soc_dai *dai)
69243d24e76SNicolin Chen {
69343d24e76SNicolin Chen 	struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
69443d24e76SNicolin Chen 
69543d24e76SNicolin Chen 	snd_soc_dai_init_dma_data(dai, &esai_priv->dma_params_tx,
69643d24e76SNicolin Chen 				  &esai_priv->dma_params_rx);
69743d24e76SNicolin Chen 
69843d24e76SNicolin Chen 	return 0;
69943d24e76SNicolin Chen }
70043d24e76SNicolin Chen 
70143d24e76SNicolin Chen static struct snd_soc_dai_driver fsl_esai_dai = {
70243d24e76SNicolin Chen 	.probe = fsl_esai_dai_probe,
70343d24e76SNicolin Chen 	.playback = {
70474ccb27cSNicolin Chen 		.stream_name = "CPU-Playback",
70543d24e76SNicolin Chen 		.channels_min = 1,
70643d24e76SNicolin Chen 		.channels_max = 12,
707f2a3ee01SFabio Estevam 		.rates = SNDRV_PCM_RATE_8000_192000,
70843d24e76SNicolin Chen 		.formats = FSL_ESAI_FORMATS,
70943d24e76SNicolin Chen 	},
71043d24e76SNicolin Chen 	.capture = {
71174ccb27cSNicolin Chen 		.stream_name = "CPU-Capture",
71243d24e76SNicolin Chen 		.channels_min = 1,
71343d24e76SNicolin Chen 		.channels_max = 8,
714f2a3ee01SFabio Estevam 		.rates = SNDRV_PCM_RATE_8000_192000,
71543d24e76SNicolin Chen 		.formats = FSL_ESAI_FORMATS,
71643d24e76SNicolin Chen 	},
71743d24e76SNicolin Chen 	.ops = &fsl_esai_dai_ops,
71843d24e76SNicolin Chen };
71943d24e76SNicolin Chen 
72043d24e76SNicolin Chen static const struct snd_soc_component_driver fsl_esai_component = {
72143d24e76SNicolin Chen 	.name		= "fsl-esai",
72243d24e76SNicolin Chen };
72343d24e76SNicolin Chen 
724c64c6076SZidan Wang static const struct reg_default fsl_esai_reg_defaults[] = {
7258973112aSZidan Wang 	{REG_ESAI_ETDR,	 0x00000000},
7268973112aSZidan Wang 	{REG_ESAI_ECR,	 0x00000000},
7278973112aSZidan Wang 	{REG_ESAI_TFCR,	 0x00000000},
7288973112aSZidan Wang 	{REG_ESAI_RFCR,	 0x00000000},
7298973112aSZidan Wang 	{REG_ESAI_TX0,	 0x00000000},
7308973112aSZidan Wang 	{REG_ESAI_TX1,	 0x00000000},
7318973112aSZidan Wang 	{REG_ESAI_TX2,	 0x00000000},
7328973112aSZidan Wang 	{REG_ESAI_TX3,	 0x00000000},
7338973112aSZidan Wang 	{REG_ESAI_TX4,	 0x00000000},
7348973112aSZidan Wang 	{REG_ESAI_TX5,	 0x00000000},
7358973112aSZidan Wang 	{REG_ESAI_TSR,	 0x00000000},
7368973112aSZidan Wang 	{REG_ESAI_SAICR, 0x00000000},
7378973112aSZidan Wang 	{REG_ESAI_TCR,	 0x00000000},
7388973112aSZidan Wang 	{REG_ESAI_TCCR,	 0x00000000},
7398973112aSZidan Wang 	{REG_ESAI_RCR,	 0x00000000},
7408973112aSZidan Wang 	{REG_ESAI_RCCR,	 0x00000000},
7418973112aSZidan Wang 	{REG_ESAI_TSMA,  0x0000ffff},
7428973112aSZidan Wang 	{REG_ESAI_TSMB,  0x0000ffff},
7438973112aSZidan Wang 	{REG_ESAI_RSMA,  0x0000ffff},
7448973112aSZidan Wang 	{REG_ESAI_RSMB,  0x0000ffff},
7458973112aSZidan Wang 	{REG_ESAI_PRRC,  0x00000000},
7468973112aSZidan Wang 	{REG_ESAI_PCRC,  0x00000000},
747c64c6076SZidan Wang };
748c64c6076SZidan Wang 
74943d24e76SNicolin Chen static bool fsl_esai_readable_reg(struct device *dev, unsigned int reg)
75043d24e76SNicolin Chen {
75143d24e76SNicolin Chen 	switch (reg) {
75243d24e76SNicolin Chen 	case REG_ESAI_ERDR:
75343d24e76SNicolin Chen 	case REG_ESAI_ECR:
75443d24e76SNicolin Chen 	case REG_ESAI_ESR:
75543d24e76SNicolin Chen 	case REG_ESAI_TFCR:
75643d24e76SNicolin Chen 	case REG_ESAI_TFSR:
75743d24e76SNicolin Chen 	case REG_ESAI_RFCR:
75843d24e76SNicolin Chen 	case REG_ESAI_RFSR:
75943d24e76SNicolin Chen 	case REG_ESAI_RX0:
76043d24e76SNicolin Chen 	case REG_ESAI_RX1:
76143d24e76SNicolin Chen 	case REG_ESAI_RX2:
76243d24e76SNicolin Chen 	case REG_ESAI_RX3:
76343d24e76SNicolin Chen 	case REG_ESAI_SAISR:
76443d24e76SNicolin Chen 	case REG_ESAI_SAICR:
76543d24e76SNicolin Chen 	case REG_ESAI_TCR:
76643d24e76SNicolin Chen 	case REG_ESAI_TCCR:
76743d24e76SNicolin Chen 	case REG_ESAI_RCR:
76843d24e76SNicolin Chen 	case REG_ESAI_RCCR:
76943d24e76SNicolin Chen 	case REG_ESAI_TSMA:
77043d24e76SNicolin Chen 	case REG_ESAI_TSMB:
77143d24e76SNicolin Chen 	case REG_ESAI_RSMA:
77243d24e76SNicolin Chen 	case REG_ESAI_RSMB:
77343d24e76SNicolin Chen 	case REG_ESAI_PRRC:
77443d24e76SNicolin Chen 	case REG_ESAI_PCRC:
77543d24e76SNicolin Chen 		return true;
77643d24e76SNicolin Chen 	default:
77743d24e76SNicolin Chen 		return false;
77843d24e76SNicolin Chen 	}
77943d24e76SNicolin Chen }
78043d24e76SNicolin Chen 
781c64c6076SZidan Wang static bool fsl_esai_volatile_reg(struct device *dev, unsigned int reg)
782c64c6076SZidan Wang {
783c64c6076SZidan Wang 	switch (reg) {
784c64c6076SZidan Wang 	case REG_ESAI_ERDR:
785c64c6076SZidan Wang 	case REG_ESAI_ESR:
786c64c6076SZidan Wang 	case REG_ESAI_TFSR:
787c64c6076SZidan Wang 	case REG_ESAI_RFSR:
788c64c6076SZidan Wang 	case REG_ESAI_RX0:
789c64c6076SZidan Wang 	case REG_ESAI_RX1:
790c64c6076SZidan Wang 	case REG_ESAI_RX2:
791c64c6076SZidan Wang 	case REG_ESAI_RX3:
792c64c6076SZidan Wang 	case REG_ESAI_SAISR:
793c64c6076SZidan Wang 		return true;
794c64c6076SZidan Wang 	default:
795c64c6076SZidan Wang 		return false;
796c64c6076SZidan Wang 	}
797c64c6076SZidan Wang }
798c64c6076SZidan Wang 
79943d24e76SNicolin Chen static bool fsl_esai_writeable_reg(struct device *dev, unsigned int reg)
80043d24e76SNicolin Chen {
80143d24e76SNicolin Chen 	switch (reg) {
80243d24e76SNicolin Chen 	case REG_ESAI_ETDR:
80343d24e76SNicolin Chen 	case REG_ESAI_ECR:
80443d24e76SNicolin Chen 	case REG_ESAI_TFCR:
80543d24e76SNicolin Chen 	case REG_ESAI_RFCR:
80643d24e76SNicolin Chen 	case REG_ESAI_TX0:
80743d24e76SNicolin Chen 	case REG_ESAI_TX1:
80843d24e76SNicolin Chen 	case REG_ESAI_TX2:
80943d24e76SNicolin Chen 	case REG_ESAI_TX3:
81043d24e76SNicolin Chen 	case REG_ESAI_TX4:
81143d24e76SNicolin Chen 	case REG_ESAI_TX5:
81243d24e76SNicolin Chen 	case REG_ESAI_TSR:
81343d24e76SNicolin Chen 	case REG_ESAI_SAICR:
81443d24e76SNicolin Chen 	case REG_ESAI_TCR:
81543d24e76SNicolin Chen 	case REG_ESAI_TCCR:
81643d24e76SNicolin Chen 	case REG_ESAI_RCR:
81743d24e76SNicolin Chen 	case REG_ESAI_RCCR:
81843d24e76SNicolin Chen 	case REG_ESAI_TSMA:
81943d24e76SNicolin Chen 	case REG_ESAI_TSMB:
82043d24e76SNicolin Chen 	case REG_ESAI_RSMA:
82143d24e76SNicolin Chen 	case REG_ESAI_RSMB:
82243d24e76SNicolin Chen 	case REG_ESAI_PRRC:
82343d24e76SNicolin Chen 	case REG_ESAI_PCRC:
82443d24e76SNicolin Chen 		return true;
82543d24e76SNicolin Chen 	default:
82643d24e76SNicolin Chen 		return false;
82743d24e76SNicolin Chen 	}
82843d24e76SNicolin Chen }
82943d24e76SNicolin Chen 
83092bd0334SXiubo Li static const struct regmap_config fsl_esai_regmap_config = {
83143d24e76SNicolin Chen 	.reg_bits = 32,
83243d24e76SNicolin Chen 	.reg_stride = 4,
83343d24e76SNicolin Chen 	.val_bits = 32,
83443d24e76SNicolin Chen 
83543d24e76SNicolin Chen 	.max_register = REG_ESAI_PCRC,
836c64c6076SZidan Wang 	.reg_defaults = fsl_esai_reg_defaults,
837c64c6076SZidan Wang 	.num_reg_defaults = ARRAY_SIZE(fsl_esai_reg_defaults),
83843d24e76SNicolin Chen 	.readable_reg = fsl_esai_readable_reg,
839c64c6076SZidan Wang 	.volatile_reg = fsl_esai_volatile_reg,
84043d24e76SNicolin Chen 	.writeable_reg = fsl_esai_writeable_reg,
8410effb865SMarek Vasut 	.cache_type = REGCACHE_FLAT,
84243d24e76SNicolin Chen };
84343d24e76SNicolin Chen 
84443d24e76SNicolin Chen static int fsl_esai_probe(struct platform_device *pdev)
84543d24e76SNicolin Chen {
84643d24e76SNicolin Chen 	struct device_node *np = pdev->dev.of_node;
84743d24e76SNicolin Chen 	struct fsl_esai *esai_priv;
84843d24e76SNicolin Chen 	struct resource *res;
8490600b3e1SFabio Estevam 	const __be32 *iprop;
85043d24e76SNicolin Chen 	void __iomem *regs;
85143d24e76SNicolin Chen 	int irq, ret;
85243d24e76SNicolin Chen 
85343d24e76SNicolin Chen 	esai_priv = devm_kzalloc(&pdev->dev, sizeof(*esai_priv), GFP_KERNEL);
85443d24e76SNicolin Chen 	if (!esai_priv)
85543d24e76SNicolin Chen 		return -ENOMEM;
85643d24e76SNicolin Chen 
85743d24e76SNicolin Chen 	esai_priv->pdev = pdev;
8585d585e1eSRob Herring 	snprintf(esai_priv->name, sizeof(esai_priv->name), "%pOFn", np);
85943d24e76SNicolin Chen 
86043d24e76SNicolin Chen 	/* Get the addresses and IRQ */
86143d24e76SNicolin Chen 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
86243d24e76SNicolin Chen 	regs = devm_ioremap_resource(&pdev->dev, res);
86343d24e76SNicolin Chen 	if (IS_ERR(regs))
86443d24e76SNicolin Chen 		return PTR_ERR(regs);
86543d24e76SNicolin Chen 
86643d24e76SNicolin Chen 	esai_priv->regmap = devm_regmap_init_mmio_clk(&pdev->dev,
86743d24e76SNicolin Chen 			"core", regs, &fsl_esai_regmap_config);
86843d24e76SNicolin Chen 	if (IS_ERR(esai_priv->regmap)) {
86943d24e76SNicolin Chen 		dev_err(&pdev->dev, "failed to init regmap: %ld\n",
87043d24e76SNicolin Chen 				PTR_ERR(esai_priv->regmap));
87143d24e76SNicolin Chen 		return PTR_ERR(esai_priv->regmap);
87243d24e76SNicolin Chen 	}
87343d24e76SNicolin Chen 
87443d24e76SNicolin Chen 	esai_priv->coreclk = devm_clk_get(&pdev->dev, "core");
87543d24e76SNicolin Chen 	if (IS_ERR(esai_priv->coreclk)) {
87643d24e76SNicolin Chen 		dev_err(&pdev->dev, "failed to get core clock: %ld\n",
87743d24e76SNicolin Chen 				PTR_ERR(esai_priv->coreclk));
87843d24e76SNicolin Chen 		return PTR_ERR(esai_priv->coreclk);
87943d24e76SNicolin Chen 	}
88043d24e76SNicolin Chen 
88143d24e76SNicolin Chen 	esai_priv->extalclk = devm_clk_get(&pdev->dev, "extal");
88243d24e76SNicolin Chen 	if (IS_ERR(esai_priv->extalclk))
88343d24e76SNicolin Chen 		dev_warn(&pdev->dev, "failed to get extal clock: %ld\n",
88443d24e76SNicolin Chen 				PTR_ERR(esai_priv->extalclk));
88543d24e76SNicolin Chen 
88643d24e76SNicolin Chen 	esai_priv->fsysclk = devm_clk_get(&pdev->dev, "fsys");
88743d24e76SNicolin Chen 	if (IS_ERR(esai_priv->fsysclk))
88843d24e76SNicolin Chen 		dev_warn(&pdev->dev, "failed to get fsys clock: %ld\n",
88943d24e76SNicolin Chen 				PTR_ERR(esai_priv->fsysclk));
89043d24e76SNicolin Chen 
891a2a4d604SShengjiu Wang 	esai_priv->spbaclk = devm_clk_get(&pdev->dev, "spba");
892a2a4d604SShengjiu Wang 	if (IS_ERR(esai_priv->spbaclk))
893a2a4d604SShengjiu Wang 		dev_warn(&pdev->dev, "failed to get spba clock: %ld\n",
894a2a4d604SShengjiu Wang 				PTR_ERR(esai_priv->spbaclk));
895a2a4d604SShengjiu Wang 
89643d24e76SNicolin Chen 	irq = platform_get_irq(pdev, 0);
89743d24e76SNicolin Chen 	if (irq < 0) {
898da2d4524SFabio Estevam 		dev_err(&pdev->dev, "no irq for node %s\n", pdev->name);
89943d24e76SNicolin Chen 		return irq;
90043d24e76SNicolin Chen 	}
90143d24e76SNicolin Chen 
90243d24e76SNicolin Chen 	ret = devm_request_irq(&pdev->dev, irq, esai_isr, 0,
90343d24e76SNicolin Chen 			       esai_priv->name, esai_priv);
90443d24e76SNicolin Chen 	if (ret) {
90543d24e76SNicolin Chen 		dev_err(&pdev->dev, "failed to claim irq %u\n", irq);
90643d24e76SNicolin Chen 		return ret;
90743d24e76SNicolin Chen 	}
90843d24e76SNicolin Chen 
909de0d712aSShengjiu Wang 	/* Set a default slot number */
910de0d712aSShengjiu Wang 	esai_priv->slots = 2;
911de0d712aSShengjiu Wang 
91243d24e76SNicolin Chen 	/* Set a default master/slave state */
91343d24e76SNicolin Chen 	esai_priv->slave_mode = true;
91443d24e76SNicolin Chen 
91543d24e76SNicolin Chen 	/* Determine the FIFO depth */
91643d24e76SNicolin Chen 	iprop = of_get_property(np, "fsl,fifo-depth", NULL);
91743d24e76SNicolin Chen 	if (iprop)
91843d24e76SNicolin Chen 		esai_priv->fifo_depth = be32_to_cpup(iprop);
91943d24e76SNicolin Chen 	else
92043d24e76SNicolin Chen 		esai_priv->fifo_depth = 64;
92143d24e76SNicolin Chen 
92243d24e76SNicolin Chen 	esai_priv->dma_params_tx.maxburst = 16;
92343d24e76SNicolin Chen 	esai_priv->dma_params_rx.maxburst = 16;
92443d24e76SNicolin Chen 	esai_priv->dma_params_tx.addr = res->start + REG_ESAI_ETDR;
92543d24e76SNicolin Chen 	esai_priv->dma_params_rx.addr = res->start + REG_ESAI_ERDR;
92643d24e76SNicolin Chen 
92743d24e76SNicolin Chen 	esai_priv->synchronous =
92843d24e76SNicolin Chen 		of_property_read_bool(np, "fsl,esai-synchronous");
92943d24e76SNicolin Chen 
93043d24e76SNicolin Chen 	/* Implement full symmetry for synchronous mode */
93143d24e76SNicolin Chen 	if (esai_priv->synchronous) {
93243d24e76SNicolin Chen 		fsl_esai_dai.symmetric_rates = 1;
93343d24e76SNicolin Chen 		fsl_esai_dai.symmetric_channels = 1;
93443d24e76SNicolin Chen 		fsl_esai_dai.symmetric_samplebits = 1;
93543d24e76SNicolin Chen 	}
93643d24e76SNicolin Chen 
93743d24e76SNicolin Chen 	dev_set_drvdata(&pdev->dev, esai_priv);
93843d24e76SNicolin Chen 
9395be6155bSShengjiu Wang 	ret = fsl_esai_hw_init(esai_priv);
9405be6155bSShengjiu Wang 	if (ret)
94143d24e76SNicolin Chen 		return ret;
94243d24e76SNicolin Chen 
9430ff4e8c6SS.j. Wang 	esai_priv->tx_mask = 0xFFFFFFFF;
9440ff4e8c6SS.j. Wang 	esai_priv->rx_mask = 0xFFFFFFFF;
9450ff4e8c6SS.j. Wang 
9460ff4e8c6SS.j. Wang 	/* Clear the TSMA, TSMB, RSMA, RSMB */
9470ff4e8c6SS.j. Wang 	regmap_write(esai_priv->regmap, REG_ESAI_TSMA, 0);
9480ff4e8c6SS.j. Wang 	regmap_write(esai_priv->regmap, REG_ESAI_TSMB, 0);
9490ff4e8c6SS.j. Wang 	regmap_write(esai_priv->regmap, REG_ESAI_RSMA, 0);
9500ff4e8c6SS.j. Wang 	regmap_write(esai_priv->regmap, REG_ESAI_RSMB, 0);
9510ff4e8c6SS.j. Wang 
95243d24e76SNicolin Chen 	ret = devm_snd_soc_register_component(&pdev->dev, &fsl_esai_component,
95343d24e76SNicolin Chen 					      &fsl_esai_dai, 1);
95443d24e76SNicolin Chen 	if (ret) {
95543d24e76SNicolin Chen 		dev_err(&pdev->dev, "failed to register DAI: %d\n", ret);
95643d24e76SNicolin Chen 		return ret;
95743d24e76SNicolin Chen 	}
95843d24e76SNicolin Chen 
959b2d337d8SS.j. Wang 	pm_runtime_enable(&pdev->dev);
960b2d337d8SS.j. Wang 
961b2d337d8SS.j. Wang 	regcache_cache_only(esai_priv->regmap, true);
962b2d337d8SS.j. Wang 
9630d69e0ddSShengjiu Wang 	ret = imx_pcm_dma_init(pdev, IMX_ESAI_DMABUF_SIZE);
96443d24e76SNicolin Chen 	if (ret)
96543d24e76SNicolin Chen 		dev_err(&pdev->dev, "failed to init imx pcm dma: %d\n", ret);
96643d24e76SNicolin Chen 
96743d24e76SNicolin Chen 	return ret;
96843d24e76SNicolin Chen }
96943d24e76SNicolin Chen 
970b2d337d8SS.j. Wang static int fsl_esai_remove(struct platform_device *pdev)
971b2d337d8SS.j. Wang {
972b2d337d8SS.j. Wang 	pm_runtime_disable(&pdev->dev);
973b2d337d8SS.j. Wang 
974b2d337d8SS.j. Wang 	return 0;
975b2d337d8SS.j. Wang }
976b2d337d8SS.j. Wang 
97743d24e76SNicolin Chen static const struct of_device_id fsl_esai_dt_ids[] = {
97843d24e76SNicolin Chen 	{ .compatible = "fsl,imx35-esai", },
979b21cc2f5SXiubo Li 	{ .compatible = "fsl,vf610-esai", },
98043d24e76SNicolin Chen 	{}
98143d24e76SNicolin Chen };
98243d24e76SNicolin Chen MODULE_DEVICE_TABLE(of, fsl_esai_dt_ids);
98343d24e76SNicolin Chen 
984b2d337d8SS.j. Wang #ifdef CONFIG_PM
985b2d337d8SS.j. Wang static int fsl_esai_runtime_resume(struct device *dev)
986c64c6076SZidan Wang {
987c64c6076SZidan Wang 	struct fsl_esai *esai = dev_get_drvdata(dev);
988c64c6076SZidan Wang 	int ret;
989c64c6076SZidan Wang 
990b2d337d8SS.j. Wang 	/*
991b2d337d8SS.j. Wang 	 * Some platforms might use the same bit to gate all three or two of
992b2d337d8SS.j. Wang 	 * clocks, so keep all clocks open/close at the same time for safety
993b2d337d8SS.j. Wang 	 */
994b2d337d8SS.j. Wang 	ret = clk_prepare_enable(esai->coreclk);
995b2d337d8SS.j. Wang 	if (ret)
996b2d337d8SS.j. Wang 		return ret;
997b2d337d8SS.j. Wang 	if (!IS_ERR(esai->spbaclk)) {
998b2d337d8SS.j. Wang 		ret = clk_prepare_enable(esai->spbaclk);
999b2d337d8SS.j. Wang 		if (ret)
1000b2d337d8SS.j. Wang 			goto err_spbaclk;
1001b2d337d8SS.j. Wang 	}
1002b2d337d8SS.j. Wang 	if (!IS_ERR(esai->extalclk)) {
1003b2d337d8SS.j. Wang 		ret = clk_prepare_enable(esai->extalclk);
1004b2d337d8SS.j. Wang 		if (ret)
1005b2d337d8SS.j. Wang 			goto err_extalclk;
1006b2d337d8SS.j. Wang 	}
1007b2d337d8SS.j. Wang 	if (!IS_ERR(esai->fsysclk)) {
1008b2d337d8SS.j. Wang 		ret = clk_prepare_enable(esai->fsysclk);
1009b2d337d8SS.j. Wang 		if (ret)
1010b2d337d8SS.j. Wang 			goto err_fsysclk;
1011b2d337d8SS.j. Wang 	}
1012b2d337d8SS.j. Wang 
1013c64c6076SZidan Wang 	regcache_cache_only(esai->regmap, false);
1014c64c6076SZidan Wang 
10155be6155bSShengjiu Wang 	ret = fsl_esai_register_restore(esai);
1016c64c6076SZidan Wang 	if (ret)
1017b2d337d8SS.j. Wang 		goto err_regcache_sync;
1018c64c6076SZidan Wang 
1019c64c6076SZidan Wang 	return 0;
1020b2d337d8SS.j. Wang 
1021b2d337d8SS.j. Wang err_regcache_sync:
1022b2d337d8SS.j. Wang 	if (!IS_ERR(esai->fsysclk))
1023b2d337d8SS.j. Wang 		clk_disable_unprepare(esai->fsysclk);
1024b2d337d8SS.j. Wang err_fsysclk:
1025b2d337d8SS.j. Wang 	if (!IS_ERR(esai->extalclk))
1026b2d337d8SS.j. Wang 		clk_disable_unprepare(esai->extalclk);
1027b2d337d8SS.j. Wang err_extalclk:
1028b2d337d8SS.j. Wang 	if (!IS_ERR(esai->spbaclk))
1029b2d337d8SS.j. Wang 		clk_disable_unprepare(esai->spbaclk);
1030b2d337d8SS.j. Wang err_spbaclk:
1031b2d337d8SS.j. Wang 	clk_disable_unprepare(esai->coreclk);
1032b2d337d8SS.j. Wang 
1033b2d337d8SS.j. Wang 	return ret;
1034c64c6076SZidan Wang }
1035b2d337d8SS.j. Wang 
1036b2d337d8SS.j. Wang static int fsl_esai_runtime_suspend(struct device *dev)
1037b2d337d8SS.j. Wang {
1038b2d337d8SS.j. Wang 	struct fsl_esai *esai = dev_get_drvdata(dev);
1039b2d337d8SS.j. Wang 
1040b2d337d8SS.j. Wang 	regcache_cache_only(esai->regmap, true);
1041b2d337d8SS.j. Wang 
1042b2d337d8SS.j. Wang 	if (!IS_ERR(esai->fsysclk))
1043b2d337d8SS.j. Wang 		clk_disable_unprepare(esai->fsysclk);
1044b2d337d8SS.j. Wang 	if (!IS_ERR(esai->extalclk))
1045b2d337d8SS.j. Wang 		clk_disable_unprepare(esai->extalclk);
1046b2d337d8SS.j. Wang 	if (!IS_ERR(esai->spbaclk))
1047b2d337d8SS.j. Wang 		clk_disable_unprepare(esai->spbaclk);
1048b2d337d8SS.j. Wang 	clk_disable_unprepare(esai->coreclk);
1049b2d337d8SS.j. Wang 
1050b2d337d8SS.j. Wang 	return 0;
1051b2d337d8SS.j. Wang }
1052b2d337d8SS.j. Wang #endif /* CONFIG_PM */
1053c64c6076SZidan Wang 
1054c64c6076SZidan Wang static const struct dev_pm_ops fsl_esai_pm_ops = {
1055b2d337d8SS.j. Wang 	SET_RUNTIME_PM_OPS(fsl_esai_runtime_suspend,
1056b2d337d8SS.j. Wang 			   fsl_esai_runtime_resume,
1057b2d337d8SS.j. Wang 			   NULL)
1058b2d337d8SS.j. Wang 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1059b2d337d8SS.j. Wang 				pm_runtime_force_resume)
1060c64c6076SZidan Wang };
1061c64c6076SZidan Wang 
106243d24e76SNicolin Chen static struct platform_driver fsl_esai_driver = {
106343d24e76SNicolin Chen 	.probe = fsl_esai_probe,
1064b2d337d8SS.j. Wang 	.remove = fsl_esai_remove,
106543d24e76SNicolin Chen 	.driver = {
106643d24e76SNicolin Chen 		.name = "fsl-esai-dai",
1067c64c6076SZidan Wang 		.pm = &fsl_esai_pm_ops,
106843d24e76SNicolin Chen 		.of_match_table = fsl_esai_dt_ids,
106943d24e76SNicolin Chen 	},
107043d24e76SNicolin Chen };
107143d24e76SNicolin Chen 
107243d24e76SNicolin Chen module_platform_driver(fsl_esai_driver);
107343d24e76SNicolin Chen 
107443d24e76SNicolin Chen MODULE_AUTHOR("Freescale Semiconductor, Inc.");
107543d24e76SNicolin Chen MODULE_DESCRIPTION("Freescale ESAI CPU DAI driver");
107643d24e76SNicolin Chen MODULE_LICENSE("GPL v2");
107743d24e76SNicolin Chen MODULE_ALIAS("platform:fsl-esai-dai");
1078