xref: /openbmc/linux/sound/soc/fsl/fsl_esai.c (revision cf9441adb1a35506d7606866c382b9d8614169b5)
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)
357ccafa2bSShengjiu Wang  * @task: tasklet to handle the reset operation
3643d24e76SNicolin Chen  * @fifo_depth: depth of tx/rx FIFO
3743d24e76SNicolin Chen  * @slot_width: width of each DAI slot
38de0d712aSShengjiu Wang  * @slots: number of slots
395be6155bSShengjiu Wang  * @channels: channel num for tx or rx
4043d24e76SNicolin Chen  * @hck_rate: clock rate of desired HCKx clock
41f975ca46SNicolin Chen  * @sck_rate: clock rate of desired SCKx clock
42f975ca46SNicolin Chen  * @hck_dir: the direction of HCKx pads
4343d24e76SNicolin Chen  * @sck_div: if using PSR/PM dividers for SCKx clock
4443d24e76SNicolin Chen  * @slave_mode: if fully using DAI slave mode
4543d24e76SNicolin Chen  * @synchronous: if using tx/rx synchronous mode
467ccafa2bSShengjiu Wang  * @reset_at_xrun: flags for enable reset operaton
4743d24e76SNicolin Chen  * @name: driver name
4843d24e76SNicolin Chen  */
4943d24e76SNicolin Chen struct fsl_esai {
5043d24e76SNicolin Chen 	struct snd_dmaengine_dai_dma_data dma_params_rx;
5143d24e76SNicolin Chen 	struct snd_dmaengine_dai_dma_data dma_params_tx;
5243d24e76SNicolin Chen 	struct platform_device *pdev;
5343d24e76SNicolin Chen 	struct regmap *regmap;
5443d24e76SNicolin Chen 	struct clk *coreclk;
5543d24e76SNicolin Chen 	struct clk *extalclk;
5643d24e76SNicolin Chen 	struct clk *fsysclk;
57a2a4d604SShengjiu Wang 	struct clk *spbaclk;
587ccafa2bSShengjiu Wang 	struct tasklet_struct task;
5943d24e76SNicolin Chen 	u32 fifo_depth;
6043d24e76SNicolin Chen 	u32 slot_width;
61de0d712aSShengjiu Wang 	u32 slots;
620ff4e8c6SS.j. Wang 	u32 tx_mask;
630ff4e8c6SS.j. Wang 	u32 rx_mask;
645be6155bSShengjiu Wang 	u32 channels[2];
6543d24e76SNicolin Chen 	u32 hck_rate[2];
66f975ca46SNicolin Chen 	u32 sck_rate[2];
67f975ca46SNicolin Chen 	bool hck_dir[2];
6843d24e76SNicolin Chen 	bool sck_div[2];
6943d24e76SNicolin Chen 	bool slave_mode;
7043d24e76SNicolin Chen 	bool synchronous;
717ccafa2bSShengjiu Wang 	bool reset_at_xrun;
7243d24e76SNicolin Chen 	char name[32];
7343d24e76SNicolin Chen };
7443d24e76SNicolin Chen 
7543d24e76SNicolin Chen static irqreturn_t esai_isr(int irq, void *devid)
7643d24e76SNicolin Chen {
7743d24e76SNicolin Chen 	struct fsl_esai *esai_priv = (struct fsl_esai *)devid;
7843d24e76SNicolin Chen 	struct platform_device *pdev = esai_priv->pdev;
7943d24e76SNicolin Chen 	u32 esr;
807ccafa2bSShengjiu Wang 	u32 saisr;
8143d24e76SNicolin Chen 
8243d24e76SNicolin Chen 	regmap_read(esai_priv->regmap, REG_ESAI_ESR, &esr);
837ccafa2bSShengjiu Wang 	regmap_read(esai_priv->regmap, REG_ESAI_SAISR, &saisr);
847ccafa2bSShengjiu Wang 
857ccafa2bSShengjiu Wang 	if ((saisr & (ESAI_SAISR_TUE | ESAI_SAISR_ROE)) &&
867ccafa2bSShengjiu Wang 	    esai_priv->reset_at_xrun) {
877ccafa2bSShengjiu Wang 		dev_dbg(&pdev->dev, "reset module for xrun\n");
887ccafa2bSShengjiu Wang 		tasklet_schedule(&esai_priv->task);
897ccafa2bSShengjiu Wang 	}
9043d24e76SNicolin Chen 
9143d24e76SNicolin Chen 	if (esr & ESAI_ESR_TINIT_MASK)
923bcc8656SColin Ian King 		dev_dbg(&pdev->dev, "isr: Transmission Initialized\n");
9343d24e76SNicolin Chen 
9443d24e76SNicolin Chen 	if (esr & ESAI_ESR_RFF_MASK)
9543d24e76SNicolin Chen 		dev_warn(&pdev->dev, "isr: Receiving overrun\n");
9643d24e76SNicolin Chen 
9743d24e76SNicolin Chen 	if (esr & ESAI_ESR_TFE_MASK)
983bcc8656SColin Ian King 		dev_warn(&pdev->dev, "isr: Transmission underrun\n");
9943d24e76SNicolin Chen 
10043d24e76SNicolin Chen 	if (esr & ESAI_ESR_TLS_MASK)
10143d24e76SNicolin Chen 		dev_dbg(&pdev->dev, "isr: Just transmitted the last slot\n");
10243d24e76SNicolin Chen 
10343d24e76SNicolin Chen 	if (esr & ESAI_ESR_TDE_MASK)
1043bcc8656SColin Ian King 		dev_dbg(&pdev->dev, "isr: Transmission data exception\n");
10543d24e76SNicolin Chen 
10643d24e76SNicolin Chen 	if (esr & ESAI_ESR_TED_MASK)
10743d24e76SNicolin Chen 		dev_dbg(&pdev->dev, "isr: Transmitting even slots\n");
10843d24e76SNicolin Chen 
10943d24e76SNicolin Chen 	if (esr & ESAI_ESR_TD_MASK)
11043d24e76SNicolin Chen 		dev_dbg(&pdev->dev, "isr: Transmitting data\n");
11143d24e76SNicolin Chen 
11243d24e76SNicolin Chen 	if (esr & ESAI_ESR_RLS_MASK)
11343d24e76SNicolin Chen 		dev_dbg(&pdev->dev, "isr: Just received the last slot\n");
11443d24e76SNicolin Chen 
11543d24e76SNicolin Chen 	if (esr & ESAI_ESR_RDE_MASK)
11643d24e76SNicolin Chen 		dev_dbg(&pdev->dev, "isr: Receiving data exception\n");
11743d24e76SNicolin Chen 
11843d24e76SNicolin Chen 	if (esr & ESAI_ESR_RED_MASK)
11943d24e76SNicolin Chen 		dev_dbg(&pdev->dev, "isr: Receiving even slots\n");
12043d24e76SNicolin Chen 
12143d24e76SNicolin Chen 	if (esr & ESAI_ESR_RD_MASK)
12243d24e76SNicolin Chen 		dev_dbg(&pdev->dev, "isr: Receiving data\n");
12343d24e76SNicolin Chen 
12443d24e76SNicolin Chen 	return IRQ_HANDLED;
12543d24e76SNicolin Chen }
12643d24e76SNicolin Chen 
12743d24e76SNicolin Chen /**
12843d24e76SNicolin Chen  * This function is used to calculate the divisors of psr, pm, fp and it is
12943d24e76SNicolin Chen  * supposed to be called in set_dai_sysclk() and set_bclk().
13043d24e76SNicolin Chen  *
13143d24e76SNicolin Chen  * @ratio: desired overall ratio for the paticipating dividers
13243d24e76SNicolin Chen  * @usefp: for HCK setting, there is no need to set fp divider
13343d24e76SNicolin Chen  * @fp: bypass other dividers by setting fp directly if fp != 0
13443d24e76SNicolin Chen  * @tx: current setting is for playback or capture
13543d24e76SNicolin Chen  */
13643d24e76SNicolin Chen static int fsl_esai_divisor_cal(struct snd_soc_dai *dai, bool tx, u32 ratio,
13743d24e76SNicolin Chen 				bool usefp, u32 fp)
13843d24e76SNicolin Chen {
13943d24e76SNicolin Chen 	struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
14043d24e76SNicolin Chen 	u32 psr, pm = 999, maxfp, prod, sub, savesub, i, j;
14143d24e76SNicolin Chen 
14243d24e76SNicolin Chen 	maxfp = usefp ? 16 : 1;
14343d24e76SNicolin Chen 
14443d24e76SNicolin Chen 	if (usefp && fp)
14543d24e76SNicolin Chen 		goto out_fp;
14643d24e76SNicolin Chen 
14743d24e76SNicolin Chen 	if (ratio > 2 * 8 * 256 * maxfp || ratio < 2) {
14843d24e76SNicolin Chen 		dev_err(dai->dev, "the ratio is out of range (2 ~ %d)\n",
14943d24e76SNicolin Chen 				2 * 8 * 256 * maxfp);
15043d24e76SNicolin Chen 		return -EINVAL;
15143d24e76SNicolin Chen 	} else if (ratio % 2) {
15243d24e76SNicolin Chen 		dev_err(dai->dev, "the raio must be even if using upper divider\n");
15343d24e76SNicolin Chen 		return -EINVAL;
15443d24e76SNicolin Chen 	}
15543d24e76SNicolin Chen 
15643d24e76SNicolin Chen 	ratio /= 2;
15743d24e76SNicolin Chen 
15843d24e76SNicolin Chen 	psr = ratio <= 256 * maxfp ? ESAI_xCCR_xPSR_BYPASS : ESAI_xCCR_xPSR_DIV8;
15943d24e76SNicolin Chen 
160c656941dSNicolin Chen 	/* Do not loop-search if PM (1 ~ 256) alone can serve the ratio */
161c656941dSNicolin Chen 	if (ratio <= 256) {
162c656941dSNicolin Chen 		pm = ratio;
163c656941dSNicolin Chen 		fp = 1;
164c656941dSNicolin Chen 		goto out;
165c656941dSNicolin Chen 	}
166c656941dSNicolin Chen 
16743d24e76SNicolin Chen 	/* Set the max fluctuation -- 0.1% of the max devisor */
16843d24e76SNicolin Chen 	savesub = (psr ? 1 : 8)  * 256 * maxfp / 1000;
16943d24e76SNicolin Chen 
17043d24e76SNicolin Chen 	/* Find the best value for PM */
17143d24e76SNicolin Chen 	for (i = 1; i <= 256; i++) {
17243d24e76SNicolin Chen 		for (j = 1; j <= maxfp; j++) {
17343d24e76SNicolin Chen 			/* PSR (1 or 8) * PM (1 ~ 256) * FP (1 ~ 16) */
17443d24e76SNicolin Chen 			prod = (psr ? 1 : 8) * i * j;
17543d24e76SNicolin Chen 
17643d24e76SNicolin Chen 			if (prod == ratio)
17743d24e76SNicolin Chen 				sub = 0;
17843d24e76SNicolin Chen 			else if (prod / ratio == 1)
17943d24e76SNicolin Chen 				sub = prod - ratio;
18043d24e76SNicolin Chen 			else if (ratio / prod == 1)
18143d24e76SNicolin Chen 				sub = ratio - prod;
18243d24e76SNicolin Chen 			else
18343d24e76SNicolin Chen 				continue;
18443d24e76SNicolin Chen 
18543d24e76SNicolin Chen 			/* Calculate the fraction */
18643d24e76SNicolin Chen 			sub = sub * 1000 / ratio;
18743d24e76SNicolin Chen 			if (sub < savesub) {
18843d24e76SNicolin Chen 				savesub = sub;
18943d24e76SNicolin Chen 				pm = i;
19043d24e76SNicolin Chen 				fp = j;
19143d24e76SNicolin Chen 			}
19243d24e76SNicolin Chen 
19343d24e76SNicolin Chen 			/* We are lucky */
19443d24e76SNicolin Chen 			if (savesub == 0)
19543d24e76SNicolin Chen 				goto out;
19643d24e76SNicolin Chen 		}
19743d24e76SNicolin Chen 	}
19843d24e76SNicolin Chen 
19943d24e76SNicolin Chen 	if (pm == 999) {
20043d24e76SNicolin Chen 		dev_err(dai->dev, "failed to calculate proper divisors\n");
20143d24e76SNicolin Chen 		return -EINVAL;
20243d24e76SNicolin Chen 	}
20343d24e76SNicolin Chen 
20443d24e76SNicolin Chen out:
20543d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xCCR(tx),
20643d24e76SNicolin Chen 			   ESAI_xCCR_xPSR_MASK | ESAI_xCCR_xPM_MASK,
20743d24e76SNicolin Chen 			   psr | ESAI_xCCR_xPM(pm));
20843d24e76SNicolin Chen 
20943d24e76SNicolin Chen out_fp:
21043d24e76SNicolin Chen 	/* Bypass fp if not being required */
21143d24e76SNicolin Chen 	if (maxfp <= 1)
21243d24e76SNicolin Chen 		return 0;
21343d24e76SNicolin Chen 
21443d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xCCR(tx),
21543d24e76SNicolin Chen 			   ESAI_xCCR_xFP_MASK, ESAI_xCCR_xFP(fp));
21643d24e76SNicolin Chen 
21743d24e76SNicolin Chen 	return 0;
21843d24e76SNicolin Chen }
21943d24e76SNicolin Chen 
22043d24e76SNicolin Chen /**
22143d24e76SNicolin Chen  * This function mainly configures the clock frequency of MCLK (HCKT/HCKR)
22243d24e76SNicolin Chen  *
22343d24e76SNicolin Chen  * @Parameters:
22443d24e76SNicolin Chen  * clk_id: The clock source of HCKT/HCKR
22543d24e76SNicolin Chen  *	  (Input from outside; output from inside, FSYS or EXTAL)
22643d24e76SNicolin Chen  * freq: The required clock rate of HCKT/HCKR
22743d24e76SNicolin Chen  * dir: The clock direction of HCKT/HCKR
22843d24e76SNicolin Chen  *
22943d24e76SNicolin Chen  * Note: If the direction is input, we do not care about clk_id.
23043d24e76SNicolin Chen  */
23143d24e76SNicolin Chen static int fsl_esai_set_dai_sysclk(struct snd_soc_dai *dai, int clk_id,
23243d24e76SNicolin Chen 				   unsigned int freq, int dir)
23343d24e76SNicolin Chen {
23443d24e76SNicolin Chen 	struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
23543d24e76SNicolin Chen 	struct clk *clksrc = esai_priv->extalclk;
2361997ee89SS.j. Wang 	bool tx = (clk_id <= ESAI_HCKT_EXTAL || esai_priv->synchronous);
23743d24e76SNicolin Chen 	bool in = dir == SND_SOC_CLOCK_IN;
2383e185238SXiubo Li 	u32 ratio, ecr = 0;
23943d24e76SNicolin Chen 	unsigned long clk_rate;
2403e185238SXiubo Li 	int ret;
24143d24e76SNicolin Chen 
2428a2278b7SNicolin Chen 	if (freq == 0) {
2438a2278b7SNicolin Chen 		dev_err(dai->dev, "%sput freq of HCK%c should not be 0Hz\n",
2448a2278b7SNicolin Chen 			in ? "in" : "out", tx ? 'T' : 'R');
2458a2278b7SNicolin Chen 		return -EINVAL;
2468a2278b7SNicolin Chen 	}
2478a2278b7SNicolin Chen 
248f975ca46SNicolin Chen 	/* Bypass divider settings if the requirement doesn't change */
249f975ca46SNicolin Chen 	if (freq == esai_priv->hck_rate[tx] && dir == esai_priv->hck_dir[tx])
250f975ca46SNicolin Chen 		return 0;
25143d24e76SNicolin Chen 
25243d24e76SNicolin Chen 	/* sck_div can be only bypassed if ETO/ERO=0 and SNC_SOC_CLOCK_OUT */
25343d24e76SNicolin Chen 	esai_priv->sck_div[tx] = true;
25443d24e76SNicolin Chen 
25543d24e76SNicolin Chen 	/* Set the direction of HCKT/HCKR pins */
25643d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xCCR(tx),
25743d24e76SNicolin Chen 			   ESAI_xCCR_xHCKD, in ? 0 : ESAI_xCCR_xHCKD);
25843d24e76SNicolin Chen 
25943d24e76SNicolin Chen 	if (in)
26043d24e76SNicolin Chen 		goto out;
26143d24e76SNicolin Chen 
26243d24e76SNicolin Chen 	switch (clk_id) {
26343d24e76SNicolin Chen 	case ESAI_HCKT_FSYS:
26443d24e76SNicolin Chen 	case ESAI_HCKR_FSYS:
26543d24e76SNicolin Chen 		clksrc = esai_priv->fsysclk;
26643d24e76SNicolin Chen 		break;
26743d24e76SNicolin Chen 	case ESAI_HCKT_EXTAL:
26843d24e76SNicolin Chen 		ecr |= ESAI_ECR_ETI;
269903c220bSS.j. Wang 		break;
27043d24e76SNicolin Chen 	case ESAI_HCKR_EXTAL:
2711997ee89SS.j. Wang 		ecr |= esai_priv->synchronous ? ESAI_ECR_ETI : ESAI_ECR_ERI;
27243d24e76SNicolin Chen 		break;
27343d24e76SNicolin Chen 	default:
27443d24e76SNicolin Chen 		return -EINVAL;
27543d24e76SNicolin Chen 	}
27643d24e76SNicolin Chen 
27743d24e76SNicolin Chen 	if (IS_ERR(clksrc)) {
27843d24e76SNicolin Chen 		dev_err(dai->dev, "no assigned %s clock\n",
27943d24e76SNicolin Chen 				clk_id % 2 ? "extal" : "fsys");
28043d24e76SNicolin Chen 		return PTR_ERR(clksrc);
28143d24e76SNicolin Chen 	}
28243d24e76SNicolin Chen 	clk_rate = clk_get_rate(clksrc);
28343d24e76SNicolin Chen 
28443d24e76SNicolin Chen 	ratio = clk_rate / freq;
28543d24e76SNicolin Chen 	if (ratio * freq > clk_rate)
28643d24e76SNicolin Chen 		ret = ratio * freq - clk_rate;
28743d24e76SNicolin Chen 	else if (ratio * freq < clk_rate)
28843d24e76SNicolin Chen 		ret = clk_rate - ratio * freq;
28943d24e76SNicolin Chen 	else
29043d24e76SNicolin Chen 		ret = 0;
29143d24e76SNicolin Chen 
29243d24e76SNicolin Chen 	/* Block if clock source can not be divided into the required rate */
29343d24e76SNicolin Chen 	if (ret != 0 && clk_rate / ret < 1000) {
29443d24e76SNicolin Chen 		dev_err(dai->dev, "failed to derive required HCK%c rate\n",
29543d24e76SNicolin Chen 				tx ? 'T' : 'R');
29643d24e76SNicolin Chen 		return -EINVAL;
29743d24e76SNicolin Chen 	}
29843d24e76SNicolin Chen 
29957ebbcafSNicolin Chen 	/* Only EXTAL source can be output directly without using PSR and PM */
30057ebbcafSNicolin Chen 	if (ratio == 1 && clksrc == esai_priv->extalclk) {
30143d24e76SNicolin Chen 		/* Bypass all the dividers if not being needed */
30243d24e76SNicolin Chen 		ecr |= tx ? ESAI_ECR_ETO : ESAI_ECR_ERO;
30343d24e76SNicolin Chen 		goto out;
30457ebbcafSNicolin Chen 	} else if (ratio < 2) {
30557ebbcafSNicolin Chen 		/* The ratio should be no less than 2 if using other sources */
30657ebbcafSNicolin Chen 		dev_err(dai->dev, "failed to derive required HCK%c rate\n",
30757ebbcafSNicolin Chen 				tx ? 'T' : 'R');
30857ebbcafSNicolin Chen 		return -EINVAL;
30943d24e76SNicolin Chen 	}
31043d24e76SNicolin Chen 
31143d24e76SNicolin Chen 	ret = fsl_esai_divisor_cal(dai, tx, ratio, false, 0);
31243d24e76SNicolin Chen 	if (ret)
31343d24e76SNicolin Chen 		return ret;
31443d24e76SNicolin Chen 
31543d24e76SNicolin Chen 	esai_priv->sck_div[tx] = false;
31643d24e76SNicolin Chen 
31743d24e76SNicolin Chen out:
318f975ca46SNicolin Chen 	esai_priv->hck_dir[tx] = dir;
31943d24e76SNicolin Chen 	esai_priv->hck_rate[tx] = freq;
32043d24e76SNicolin Chen 
32143d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_ECR,
32243d24e76SNicolin Chen 			   tx ? ESAI_ECR_ETI | ESAI_ECR_ETO :
32343d24e76SNicolin Chen 			   ESAI_ECR_ERI | ESAI_ECR_ERO, ecr);
32443d24e76SNicolin Chen 
32543d24e76SNicolin Chen 	return 0;
32643d24e76SNicolin Chen }
32743d24e76SNicolin Chen 
32843d24e76SNicolin Chen /**
32943d24e76SNicolin Chen  * This function configures the related dividers according to the bclk rate
33043d24e76SNicolin Chen  */
33143d24e76SNicolin Chen static int fsl_esai_set_bclk(struct snd_soc_dai *dai, bool tx, u32 freq)
33243d24e76SNicolin Chen {
33343d24e76SNicolin Chen 	struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
33443d24e76SNicolin Chen 	u32 hck_rate = esai_priv->hck_rate[tx];
33543d24e76SNicolin Chen 	u32 sub, ratio = hck_rate / freq;
336f975ca46SNicolin Chen 	int ret;
33743d24e76SNicolin Chen 
338f975ca46SNicolin Chen 	/* Don't apply for fully slave mode or unchanged bclk */
339f975ca46SNicolin Chen 	if (esai_priv->slave_mode || esai_priv->sck_rate[tx] == freq)
34043d24e76SNicolin Chen 		return 0;
34143d24e76SNicolin Chen 
34243d24e76SNicolin Chen 	if (ratio * freq > hck_rate)
34343d24e76SNicolin Chen 		sub = ratio * freq - hck_rate;
34443d24e76SNicolin Chen 	else if (ratio * freq < hck_rate)
34543d24e76SNicolin Chen 		sub = hck_rate - ratio * freq;
34643d24e76SNicolin Chen 	else
34743d24e76SNicolin Chen 		sub = 0;
34843d24e76SNicolin Chen 
34943d24e76SNicolin Chen 	/* Block if clock source can not be divided into the required rate */
35043d24e76SNicolin Chen 	if (sub != 0 && hck_rate / sub < 1000) {
35143d24e76SNicolin Chen 		dev_err(dai->dev, "failed to derive required SCK%c rate\n",
35243d24e76SNicolin Chen 				tx ? 'T' : 'R');
35343d24e76SNicolin Chen 		return -EINVAL;
35443d24e76SNicolin Chen 	}
35543d24e76SNicolin Chen 
35689e47f62SNicolin Chen 	/* The ratio should be contented by FP alone if bypassing PM and PSR */
35789e47f62SNicolin Chen 	if (!esai_priv->sck_div[tx] && (ratio > 16 || ratio == 0)) {
35843d24e76SNicolin Chen 		dev_err(dai->dev, "the ratio is out of range (1 ~ 16)\n");
35943d24e76SNicolin Chen 		return -EINVAL;
36043d24e76SNicolin Chen 	}
36143d24e76SNicolin Chen 
362f975ca46SNicolin Chen 	ret = fsl_esai_divisor_cal(dai, tx, ratio, true,
36343d24e76SNicolin Chen 			esai_priv->sck_div[tx] ? 0 : ratio);
364f975ca46SNicolin Chen 	if (ret)
365f975ca46SNicolin Chen 		return ret;
366f975ca46SNicolin Chen 
367f975ca46SNicolin Chen 	/* Save current bclk rate */
368f975ca46SNicolin Chen 	esai_priv->sck_rate[tx] = freq;
369f975ca46SNicolin Chen 
370f975ca46SNicolin Chen 	return 0;
37143d24e76SNicolin Chen }
37243d24e76SNicolin Chen 
37343d24e76SNicolin Chen static int fsl_esai_set_dai_tdm_slot(struct snd_soc_dai *dai, u32 tx_mask,
37443d24e76SNicolin Chen 				     u32 rx_mask, int slots, int slot_width)
37543d24e76SNicolin Chen {
37643d24e76SNicolin Chen 	struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
37743d24e76SNicolin Chen 
37843d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_TCCR,
37943d24e76SNicolin Chen 			   ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(slots));
38043d24e76SNicolin Chen 
38143d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_RCCR,
38243d24e76SNicolin Chen 			   ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(slots));
38343d24e76SNicolin Chen 
38443d24e76SNicolin Chen 	esai_priv->slot_width = slot_width;
385de0d712aSShengjiu Wang 	esai_priv->slots = slots;
3860ff4e8c6SS.j. Wang 	esai_priv->tx_mask = tx_mask;
3870ff4e8c6SS.j. Wang 	esai_priv->rx_mask = rx_mask;
38843d24e76SNicolin Chen 
38943d24e76SNicolin Chen 	return 0;
39043d24e76SNicolin Chen }
39143d24e76SNicolin Chen 
39243d24e76SNicolin Chen static int fsl_esai_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
39343d24e76SNicolin Chen {
39443d24e76SNicolin Chen 	struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
39543d24e76SNicolin Chen 	u32 xcr = 0, xccr = 0, mask;
39643d24e76SNicolin Chen 
39743d24e76SNicolin Chen 	/* DAI mode */
39843d24e76SNicolin Chen 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
39943d24e76SNicolin Chen 	case SND_SOC_DAIFMT_I2S:
40043d24e76SNicolin Chen 		/* Data on rising edge of bclk, frame low, 1clk before data */
40143d24e76SNicolin Chen 		xcr |= ESAI_xCR_xFSR;
40243d24e76SNicolin Chen 		xccr |= ESAI_xCCR_xFSP | ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP;
40343d24e76SNicolin Chen 		break;
40443d24e76SNicolin Chen 	case SND_SOC_DAIFMT_LEFT_J:
40543d24e76SNicolin Chen 		/* Data on rising edge of bclk, frame high */
40643d24e76SNicolin Chen 		xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP;
40743d24e76SNicolin Chen 		break;
40843d24e76SNicolin Chen 	case SND_SOC_DAIFMT_RIGHT_J:
40943d24e76SNicolin Chen 		/* Data on rising edge of bclk, frame high, right aligned */
410cc29ea00SS.j. Wang 		xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP;
411cc29ea00SS.j. Wang 		xcr  |= ESAI_xCR_xWA;
41243d24e76SNicolin Chen 		break;
41343d24e76SNicolin Chen 	case SND_SOC_DAIFMT_DSP_A:
41443d24e76SNicolin Chen 		/* Data on rising edge of bclk, frame high, 1clk before data */
41543d24e76SNicolin Chen 		xcr |= ESAI_xCR_xFSL | ESAI_xCR_xFSR;
41643d24e76SNicolin Chen 		xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP;
41743d24e76SNicolin Chen 		break;
41843d24e76SNicolin Chen 	case SND_SOC_DAIFMT_DSP_B:
41943d24e76SNicolin Chen 		/* Data on rising edge of bclk, frame high */
42043d24e76SNicolin Chen 		xcr |= ESAI_xCR_xFSL;
42143d24e76SNicolin Chen 		xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP;
42243d24e76SNicolin Chen 		break;
42343d24e76SNicolin Chen 	default:
42443d24e76SNicolin Chen 		return -EINVAL;
42543d24e76SNicolin Chen 	}
42643d24e76SNicolin Chen 
42743d24e76SNicolin Chen 	/* DAI clock inversion */
42843d24e76SNicolin Chen 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
42943d24e76SNicolin Chen 	case SND_SOC_DAIFMT_NB_NF:
43043d24e76SNicolin Chen 		/* Nothing to do for both normal cases */
43143d24e76SNicolin Chen 		break;
43243d24e76SNicolin Chen 	case SND_SOC_DAIFMT_IB_NF:
43343d24e76SNicolin Chen 		/* Invert bit clock */
43443d24e76SNicolin Chen 		xccr ^= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP;
43543d24e76SNicolin Chen 		break;
43643d24e76SNicolin Chen 	case SND_SOC_DAIFMT_NB_IF:
43743d24e76SNicolin Chen 		/* Invert frame clock */
43843d24e76SNicolin Chen 		xccr ^= ESAI_xCCR_xFSP;
43943d24e76SNicolin Chen 		break;
44043d24e76SNicolin Chen 	case SND_SOC_DAIFMT_IB_IF:
44143d24e76SNicolin Chen 		/* Invert both clocks */
44243d24e76SNicolin Chen 		xccr ^= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP | ESAI_xCCR_xFSP;
44343d24e76SNicolin Chen 		break;
44443d24e76SNicolin Chen 	default:
44543d24e76SNicolin Chen 		return -EINVAL;
44643d24e76SNicolin Chen 	}
44743d24e76SNicolin Chen 
44843d24e76SNicolin Chen 	esai_priv->slave_mode = false;
44943d24e76SNicolin Chen 
45043d24e76SNicolin Chen 	/* DAI clock master masks */
45143d24e76SNicolin Chen 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
45243d24e76SNicolin Chen 	case SND_SOC_DAIFMT_CBM_CFM:
45343d24e76SNicolin Chen 		esai_priv->slave_mode = true;
45443d24e76SNicolin Chen 		break;
45543d24e76SNicolin Chen 	case SND_SOC_DAIFMT_CBS_CFM:
45643d24e76SNicolin Chen 		xccr |= ESAI_xCCR_xCKD;
45743d24e76SNicolin Chen 		break;
45843d24e76SNicolin Chen 	case SND_SOC_DAIFMT_CBM_CFS:
45943d24e76SNicolin Chen 		xccr |= ESAI_xCCR_xFSD;
46043d24e76SNicolin Chen 		break;
46143d24e76SNicolin Chen 	case SND_SOC_DAIFMT_CBS_CFS:
46243d24e76SNicolin Chen 		xccr |= ESAI_xCCR_xFSD | ESAI_xCCR_xCKD;
46343d24e76SNicolin Chen 		break;
46443d24e76SNicolin Chen 	default:
46543d24e76SNicolin Chen 		return -EINVAL;
46643d24e76SNicolin Chen 	}
46743d24e76SNicolin Chen 
468cc29ea00SS.j. Wang 	mask = ESAI_xCR_xFSL | ESAI_xCR_xFSR | ESAI_xCR_xWA;
46943d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR, mask, xcr);
47043d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_RCR, mask, xcr);
47143d24e76SNicolin Chen 
47243d24e76SNicolin Chen 	mask = ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP | ESAI_xCCR_xFSP |
473cc29ea00SS.j. Wang 		ESAI_xCCR_xFSD | ESAI_xCCR_xCKD;
47443d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_TCCR, mask, xccr);
47543d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_RCCR, mask, xccr);
47643d24e76SNicolin Chen 
47743d24e76SNicolin Chen 	return 0;
47843d24e76SNicolin Chen }
47943d24e76SNicolin Chen 
48043d24e76SNicolin Chen static int fsl_esai_startup(struct snd_pcm_substream *substream,
48143d24e76SNicolin Chen 			    struct snd_soc_dai *dai)
48243d24e76SNicolin Chen {
48343d24e76SNicolin Chen 	struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
48443d24e76SNicolin Chen 
48543d24e76SNicolin Chen 	if (!dai->active) {
48643d24e76SNicolin Chen 		/* Set synchronous mode */
48743d24e76SNicolin Chen 		regmap_update_bits(esai_priv->regmap, REG_ESAI_SAICR,
48843d24e76SNicolin Chen 				   ESAI_SAICR_SYNC, esai_priv->synchronous ?
48943d24e76SNicolin Chen 				   ESAI_SAICR_SYNC : 0);
49043d24e76SNicolin Chen 
49143d24e76SNicolin Chen 		/* Set a default slot number -- 2 */
49243d24e76SNicolin Chen 		regmap_update_bits(esai_priv->regmap, REG_ESAI_TCCR,
49343d24e76SNicolin Chen 				   ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(2));
49443d24e76SNicolin Chen 		regmap_update_bits(esai_priv->regmap, REG_ESAI_RCCR,
49543d24e76SNicolin Chen 				   ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(2));
49643d24e76SNicolin Chen 	}
49743d24e76SNicolin Chen 
49843d24e76SNicolin Chen 	return 0;
49933529ec9SFabio Estevam 
50043d24e76SNicolin Chen }
50143d24e76SNicolin Chen 
50243d24e76SNicolin Chen static int fsl_esai_hw_params(struct snd_pcm_substream *substream,
50343d24e76SNicolin Chen 			      struct snd_pcm_hw_params *params,
50443d24e76SNicolin Chen 			      struct snd_soc_dai *dai)
50543d24e76SNicolin Chen {
50643d24e76SNicolin Chen 	struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
50743d24e76SNicolin Chen 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
5084ca73043SZidan Wang 	u32 width = params_width(params);
50943d24e76SNicolin Chen 	u32 channels = params_channels(params);
510de0d712aSShengjiu Wang 	u32 pins = DIV_ROUND_UP(channels, esai_priv->slots);
51186ea522bSNicolin Chen 	u32 slot_width = width;
5123e185238SXiubo Li 	u32 bclk, mask, val;
5133e185238SXiubo Li 	int ret;
51443d24e76SNicolin Chen 
515d8ffcf71SGeert Uytterhoeven 	/* Override slot_width if being specifically set */
51686ea522bSNicolin Chen 	if (esai_priv->slot_width)
51786ea522bSNicolin Chen 		slot_width = esai_priv->slot_width;
51886ea522bSNicolin Chen 
51986ea522bSNicolin Chen 	bclk = params_rate(params) * slot_width * esai_priv->slots;
52043d24e76SNicolin Chen 
5211997ee89SS.j. Wang 	ret = fsl_esai_set_bclk(dai, esai_priv->synchronous || tx, bclk);
52243d24e76SNicolin Chen 	if (ret)
52343d24e76SNicolin Chen 		return ret;
52443d24e76SNicolin Chen 
5251997ee89SS.j. Wang 	mask = ESAI_xCR_xSWS_MASK;
5261997ee89SS.j. Wang 	val = ESAI_xCR_xSWS(slot_width, width);
5271997ee89SS.j. Wang 
5281997ee89SS.j. Wang 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), mask, val);
5291997ee89SS.j. Wang 	/* Recording in synchronous mode needs to set TCR also */
5301997ee89SS.j. Wang 	if (!tx && esai_priv->synchronous)
5311997ee89SS.j. Wang 		regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR, mask, val);
5321997ee89SS.j. Wang 
53343d24e76SNicolin Chen 	/* Use Normal mode to support monaural audio */
53443d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx),
53543d24e76SNicolin Chen 			   ESAI_xCR_xMOD_MASK, params_channels(params) > 1 ?
53643d24e76SNicolin Chen 			   ESAI_xCR_xMOD_NETWORK : 0);
53743d24e76SNicolin Chen 
53843d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx),
53943d24e76SNicolin Chen 			   ESAI_xFCR_xFR_MASK, ESAI_xFCR_xFR);
54043d24e76SNicolin Chen 
54143d24e76SNicolin Chen 	mask = ESAI_xFCR_xFR_MASK | ESAI_xFCR_xWA_MASK | ESAI_xFCR_xFWM_MASK |
54243d24e76SNicolin Chen 	      (tx ? ESAI_xFCR_TE_MASK | ESAI_xFCR_TIEN : ESAI_xFCR_RE_MASK);
54343d24e76SNicolin Chen 	val = ESAI_xFCR_xWA(width) | ESAI_xFCR_xFWM(esai_priv->fifo_depth) |
544de0d712aSShengjiu Wang 	     (tx ? ESAI_xFCR_TE(pins) | ESAI_xFCR_TIEN : ESAI_xFCR_RE(pins));
54543d24e76SNicolin Chen 
54643d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), mask, val);
54743d24e76SNicolin Chen 
5481997ee89SS.j. Wang 	if (tx)
5491997ee89SS.j. Wang 		regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR,
5501997ee89SS.j. Wang 				ESAI_xCR_PADC, ESAI_xCR_PADC);
55143d24e76SNicolin Chen 
5524f8210f6SNicolin Chen 	/* Remove ESAI personal reset by configuring ESAI_PCRC and ESAI_PRRC */
5534f8210f6SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_PRRC,
5544f8210f6SNicolin Chen 			   ESAI_PRRC_PDC_MASK, ESAI_PRRC_PDC(ESAI_GPIO));
5554f8210f6SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_PCRC,
5564f8210f6SNicolin Chen 			   ESAI_PCRC_PC_MASK, ESAI_PCRC_PC(ESAI_GPIO));
55743d24e76SNicolin Chen 	return 0;
55843d24e76SNicolin Chen }
55943d24e76SNicolin Chen 
5605be6155bSShengjiu Wang static int fsl_esai_hw_init(struct fsl_esai *esai_priv)
56143d24e76SNicolin Chen {
5625be6155bSShengjiu Wang 	struct platform_device *pdev = esai_priv->pdev;
5635be6155bSShengjiu Wang 	int ret;
5645be6155bSShengjiu Wang 
5655be6155bSShengjiu Wang 	/* Reset ESAI unit */
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 | ESAI_ECR_ERST);
5695be6155bSShengjiu Wang 	if (ret) {
5705be6155bSShengjiu Wang 		dev_err(&pdev->dev, "failed to reset ESAI: %d\n", ret);
5715be6155bSShengjiu Wang 		return ret;
5725be6155bSShengjiu Wang 	}
5735be6155bSShengjiu Wang 
5745be6155bSShengjiu Wang 	/*
5755be6155bSShengjiu Wang 	 * We need to enable ESAI so as to access some of its registers.
5765be6155bSShengjiu Wang 	 * Otherwise, we would fail to dump regmap from user space.
5775be6155bSShengjiu Wang 	 */
5785be6155bSShengjiu Wang 	ret = regmap_update_bits(esai_priv->regmap, REG_ESAI_ECR,
5795be6155bSShengjiu Wang 				 ESAI_ECR_ESAIEN_MASK | ESAI_ECR_ERST_MASK,
5805be6155bSShengjiu Wang 				 ESAI_ECR_ESAIEN);
5815be6155bSShengjiu Wang 	if (ret) {
5825be6155bSShengjiu Wang 		dev_err(&pdev->dev, "failed to enable ESAI: %d\n", ret);
5835be6155bSShengjiu Wang 		return ret;
5845be6155bSShengjiu Wang 	}
5855be6155bSShengjiu Wang 
5865be6155bSShengjiu Wang 	regmap_update_bits(esai_priv->regmap, REG_ESAI_PRRC,
5875be6155bSShengjiu Wang 			   ESAI_PRRC_PDC_MASK, 0);
5885be6155bSShengjiu Wang 	regmap_update_bits(esai_priv->regmap, REG_ESAI_PCRC,
5895be6155bSShengjiu Wang 			   ESAI_PCRC_PC_MASK, 0);
5905be6155bSShengjiu Wang 
5915be6155bSShengjiu Wang 	return 0;
5925be6155bSShengjiu Wang }
5935be6155bSShengjiu Wang 
5945be6155bSShengjiu Wang static int fsl_esai_register_restore(struct fsl_esai *esai_priv)
5955be6155bSShengjiu Wang {
5965be6155bSShengjiu Wang 	int ret;
5975be6155bSShengjiu Wang 
5985be6155bSShengjiu Wang 	/* FIFO reset for safety */
5995be6155bSShengjiu Wang 	regmap_update_bits(esai_priv->regmap, REG_ESAI_TFCR,
6005be6155bSShengjiu Wang 			   ESAI_xFCR_xFR, ESAI_xFCR_xFR);
6015be6155bSShengjiu Wang 	regmap_update_bits(esai_priv->regmap, REG_ESAI_RFCR,
6025be6155bSShengjiu Wang 			   ESAI_xFCR_xFR, ESAI_xFCR_xFR);
6035be6155bSShengjiu Wang 
6045be6155bSShengjiu Wang 	regcache_mark_dirty(esai_priv->regmap);
6055be6155bSShengjiu Wang 	ret = regcache_sync(esai_priv->regmap);
6065be6155bSShengjiu Wang 	if (ret)
6075be6155bSShengjiu Wang 		return ret;
6085be6155bSShengjiu Wang 
6095be6155bSShengjiu Wang 	/* FIFO reset done */
6105be6155bSShengjiu Wang 	regmap_update_bits(esai_priv->regmap, REG_ESAI_TFCR, ESAI_xFCR_xFR, 0);
6115be6155bSShengjiu Wang 	regmap_update_bits(esai_priv->regmap, REG_ESAI_RFCR, ESAI_xFCR_xFR, 0);
6125be6155bSShengjiu Wang 
6135be6155bSShengjiu Wang 	return 0;
6145be6155bSShengjiu Wang }
6155be6155bSShengjiu Wang 
6165be6155bSShengjiu Wang static void fsl_esai_trigger_start(struct fsl_esai *esai_priv, bool tx)
6175be6155bSShengjiu Wang {
6185be6155bSShengjiu Wang 	u8 i, channels = esai_priv->channels[tx];
619de0d712aSShengjiu Wang 	u32 pins = DIV_ROUND_UP(channels, esai_priv->slots);
6200ff4e8c6SS.j. Wang 	u32 mask;
62143d24e76SNicolin Chen 
62243d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx),
62343d24e76SNicolin Chen 			   ESAI_xFCR_xFEN_MASK, ESAI_xFCR_xFEN);
62443d24e76SNicolin Chen 
62543d24e76SNicolin Chen 	/* Write initial words reqiured by ESAI as normal procedure */
62643d24e76SNicolin Chen 	for (i = 0; tx && i < channels; i++)
62743d24e76SNicolin Chen 		regmap_write(esai_priv->regmap, REG_ESAI_ETDR, 0x0);
62843d24e76SNicolin Chen 
6290ff4e8c6SS.j. Wang 	/*
6300ff4e8c6SS.j. Wang 	 * When set the TE/RE in the end of enablement flow, there
6310ff4e8c6SS.j. Wang 	 * will be channel swap issue for multi data line case.
6320ff4e8c6SS.j. Wang 	 * In order to workaround this issue, we switch the bit
6330ff4e8c6SS.j. Wang 	 * enablement sequence to below sequence
6340ff4e8c6SS.j. Wang 	 * 1) clear the xSMB & xSMA: which is done in probe and
6350ff4e8c6SS.j. Wang 	 *                           stop state.
6360ff4e8c6SS.j. Wang 	 * 2) set TE/RE
6370ff4e8c6SS.j. Wang 	 * 3) set xSMB
6380ff4e8c6SS.j. Wang 	 * 4) set xSMA:  xSMA is the last one in this flow, which
6390ff4e8c6SS.j. Wang 	 *               will trigger esai to start.
6400ff4e8c6SS.j. Wang 	 */
64143d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx),
64243d24e76SNicolin Chen 			   tx ? ESAI_xCR_TE_MASK : ESAI_xCR_RE_MASK,
643de0d712aSShengjiu Wang 			   tx ? ESAI_xCR_TE(pins) : ESAI_xCR_RE(pins));
6440ff4e8c6SS.j. Wang 	mask = tx ? esai_priv->tx_mask : esai_priv->rx_mask;
6450ff4e8c6SS.j. Wang 
6460ff4e8c6SS.j. Wang 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xSMB(tx),
6470ff4e8c6SS.j. Wang 			   ESAI_xSMB_xS_MASK, ESAI_xSMB_xS(mask));
6480ff4e8c6SS.j. Wang 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xSMA(tx),
6490ff4e8c6SS.j. Wang 			   ESAI_xSMA_xS_MASK, ESAI_xSMA_xS(mask));
6507ccafa2bSShengjiu Wang 
6517ccafa2bSShengjiu Wang 	/* Enable Exception interrupt */
6527ccafa2bSShengjiu Wang 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx),
6537ccafa2bSShengjiu Wang 			   ESAI_xCR_xEIE_MASK, ESAI_xCR_xEIE);
6545be6155bSShengjiu Wang }
6550ff4e8c6SS.j. Wang 
6565be6155bSShengjiu Wang static void fsl_esai_trigger_stop(struct fsl_esai *esai_priv, bool tx)
6575be6155bSShengjiu Wang {
65843d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx),
6597ccafa2bSShengjiu Wang 			   ESAI_xCR_xEIE_MASK, 0);
6607ccafa2bSShengjiu Wang 
6617ccafa2bSShengjiu Wang 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx),
66243d24e76SNicolin Chen 			   tx ? ESAI_xCR_TE_MASK : ESAI_xCR_RE_MASK, 0);
6630ff4e8c6SS.j. Wang 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xSMA(tx),
6640ff4e8c6SS.j. Wang 			   ESAI_xSMA_xS_MASK, 0);
6650ff4e8c6SS.j. Wang 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xSMB(tx),
6660ff4e8c6SS.j. Wang 			   ESAI_xSMB_xS_MASK, 0);
66743d24e76SNicolin Chen 
66843d24e76SNicolin Chen 	/* Disable and reset FIFO */
66943d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx),
67043d24e76SNicolin Chen 			   ESAI_xFCR_xFR | ESAI_xFCR_xFEN, ESAI_xFCR_xFR);
67143d24e76SNicolin Chen 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx),
67243d24e76SNicolin Chen 			   ESAI_xFCR_xFR, 0);
6735be6155bSShengjiu Wang }
6745be6155bSShengjiu Wang 
6757ccafa2bSShengjiu Wang static void fsl_esai_hw_reset(unsigned long arg)
6767ccafa2bSShengjiu Wang {
6777ccafa2bSShengjiu Wang 	struct fsl_esai *esai_priv = (struct fsl_esai *)arg;
6787ccafa2bSShengjiu Wang 	bool tx = true, rx = false, enabled[2];
6797ccafa2bSShengjiu Wang 	u32 tfcr, rfcr;
6807ccafa2bSShengjiu Wang 
6817ccafa2bSShengjiu Wang 	/* Save the registers */
6827ccafa2bSShengjiu Wang 	regmap_read(esai_priv->regmap, REG_ESAI_TFCR, &tfcr);
6837ccafa2bSShengjiu Wang 	regmap_read(esai_priv->regmap, REG_ESAI_RFCR, &rfcr);
6847ccafa2bSShengjiu Wang 	enabled[tx] = tfcr & ESAI_xFCR_xFEN;
6857ccafa2bSShengjiu Wang 	enabled[rx] = rfcr & ESAI_xFCR_xFEN;
6867ccafa2bSShengjiu Wang 
6877ccafa2bSShengjiu Wang 	/* Stop the tx & rx */
6887ccafa2bSShengjiu Wang 	fsl_esai_trigger_stop(esai_priv, tx);
6897ccafa2bSShengjiu Wang 	fsl_esai_trigger_stop(esai_priv, rx);
6907ccafa2bSShengjiu Wang 
6917ccafa2bSShengjiu Wang 	/* Reset the esai, and ignore return value */
6927ccafa2bSShengjiu Wang 	fsl_esai_hw_init(esai_priv);
6937ccafa2bSShengjiu Wang 
6947ccafa2bSShengjiu Wang 	/* Enforce ESAI personal resets for both TX and RX */
6957ccafa2bSShengjiu Wang 	regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR,
6967ccafa2bSShengjiu Wang 			   ESAI_xCR_xPR_MASK, ESAI_xCR_xPR);
6977ccafa2bSShengjiu Wang 	regmap_update_bits(esai_priv->regmap, REG_ESAI_RCR,
6987ccafa2bSShengjiu Wang 			   ESAI_xCR_xPR_MASK, ESAI_xCR_xPR);
6997ccafa2bSShengjiu Wang 
7007ccafa2bSShengjiu Wang 	/* Restore registers by regcache_sync, and ignore return value */
7017ccafa2bSShengjiu Wang 	fsl_esai_register_restore(esai_priv);
7027ccafa2bSShengjiu Wang 
7037ccafa2bSShengjiu Wang 	/* Remove ESAI personal resets by configuring PCRC and PRRC also */
7047ccafa2bSShengjiu Wang 	regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR,
7057ccafa2bSShengjiu Wang 			   ESAI_xCR_xPR_MASK, 0);
7067ccafa2bSShengjiu Wang 	regmap_update_bits(esai_priv->regmap, REG_ESAI_RCR,
7077ccafa2bSShengjiu Wang 			   ESAI_xCR_xPR_MASK, 0);
7087ccafa2bSShengjiu Wang 	regmap_update_bits(esai_priv->regmap, REG_ESAI_PRRC,
7097ccafa2bSShengjiu Wang 			   ESAI_PRRC_PDC_MASK, ESAI_PRRC_PDC(ESAI_GPIO));
7107ccafa2bSShengjiu Wang 	regmap_update_bits(esai_priv->regmap, REG_ESAI_PCRC,
7117ccafa2bSShengjiu Wang 			   ESAI_PCRC_PC_MASK, ESAI_PCRC_PC(ESAI_GPIO));
7127ccafa2bSShengjiu Wang 
7137ccafa2bSShengjiu Wang 	/* Restart tx / rx, if they already enabled */
7147ccafa2bSShengjiu Wang 	if (enabled[tx])
7157ccafa2bSShengjiu Wang 		fsl_esai_trigger_start(esai_priv, tx);
7167ccafa2bSShengjiu Wang 	if (enabled[rx])
7177ccafa2bSShengjiu Wang 		fsl_esai_trigger_start(esai_priv, rx);
7187ccafa2bSShengjiu Wang }
7197ccafa2bSShengjiu Wang 
7205be6155bSShengjiu Wang static int fsl_esai_trigger(struct snd_pcm_substream *substream, int cmd,
7215be6155bSShengjiu Wang 			    struct snd_soc_dai *dai)
7225be6155bSShengjiu Wang {
7235be6155bSShengjiu Wang 	struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
7245be6155bSShengjiu Wang 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
7255be6155bSShengjiu Wang 
7265be6155bSShengjiu Wang 	esai_priv->channels[tx] = substream->runtime->channels;
7275be6155bSShengjiu Wang 
7285be6155bSShengjiu Wang 	switch (cmd) {
7295be6155bSShengjiu Wang 	case SNDRV_PCM_TRIGGER_START:
7305be6155bSShengjiu Wang 	case SNDRV_PCM_TRIGGER_RESUME:
7315be6155bSShengjiu Wang 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
7325be6155bSShengjiu Wang 		fsl_esai_trigger_start(esai_priv, tx);
7335be6155bSShengjiu Wang 		break;
7345be6155bSShengjiu Wang 	case SNDRV_PCM_TRIGGER_SUSPEND:
7355be6155bSShengjiu Wang 	case SNDRV_PCM_TRIGGER_STOP:
7365be6155bSShengjiu Wang 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
7375be6155bSShengjiu Wang 		fsl_esai_trigger_stop(esai_priv, tx);
73843d24e76SNicolin Chen 		break;
73943d24e76SNicolin Chen 	default:
74043d24e76SNicolin Chen 		return -EINVAL;
74143d24e76SNicolin Chen 	}
74243d24e76SNicolin Chen 
74343d24e76SNicolin Chen 	return 0;
74443d24e76SNicolin Chen }
74543d24e76SNicolin Chen 
7465d29e95eSGustavo A. R. Silva static const struct snd_soc_dai_ops fsl_esai_dai_ops = {
74743d24e76SNicolin Chen 	.startup = fsl_esai_startup,
74843d24e76SNicolin Chen 	.trigger = fsl_esai_trigger,
74943d24e76SNicolin Chen 	.hw_params = fsl_esai_hw_params,
75043d24e76SNicolin Chen 	.set_sysclk = fsl_esai_set_dai_sysclk,
75143d24e76SNicolin Chen 	.set_fmt = fsl_esai_set_dai_fmt,
75243d24e76SNicolin Chen 	.set_tdm_slot = fsl_esai_set_dai_tdm_slot,
75343d24e76SNicolin Chen };
75443d24e76SNicolin Chen 
75543d24e76SNicolin Chen static int fsl_esai_dai_probe(struct snd_soc_dai *dai)
75643d24e76SNicolin Chen {
75743d24e76SNicolin Chen 	struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
75843d24e76SNicolin Chen 
75943d24e76SNicolin Chen 	snd_soc_dai_init_dma_data(dai, &esai_priv->dma_params_tx,
76043d24e76SNicolin Chen 				  &esai_priv->dma_params_rx);
76143d24e76SNicolin Chen 
76243d24e76SNicolin Chen 	return 0;
76343d24e76SNicolin Chen }
76443d24e76SNicolin Chen 
76543d24e76SNicolin Chen static struct snd_soc_dai_driver fsl_esai_dai = {
76643d24e76SNicolin Chen 	.probe = fsl_esai_dai_probe,
76743d24e76SNicolin Chen 	.playback = {
76874ccb27cSNicolin Chen 		.stream_name = "CPU-Playback",
76943d24e76SNicolin Chen 		.channels_min = 1,
77043d24e76SNicolin Chen 		.channels_max = 12,
771f2a3ee01SFabio Estevam 		.rates = SNDRV_PCM_RATE_8000_192000,
77243d24e76SNicolin Chen 		.formats = FSL_ESAI_FORMATS,
77343d24e76SNicolin Chen 	},
77443d24e76SNicolin Chen 	.capture = {
77574ccb27cSNicolin Chen 		.stream_name = "CPU-Capture",
77643d24e76SNicolin Chen 		.channels_min = 1,
77743d24e76SNicolin Chen 		.channels_max = 8,
778f2a3ee01SFabio Estevam 		.rates = SNDRV_PCM_RATE_8000_192000,
77943d24e76SNicolin Chen 		.formats = FSL_ESAI_FORMATS,
78043d24e76SNicolin Chen 	},
78143d24e76SNicolin Chen 	.ops = &fsl_esai_dai_ops,
78243d24e76SNicolin Chen };
78343d24e76SNicolin Chen 
78443d24e76SNicolin Chen static const struct snd_soc_component_driver fsl_esai_component = {
78543d24e76SNicolin Chen 	.name		= "fsl-esai",
78643d24e76SNicolin Chen };
78743d24e76SNicolin Chen 
788c64c6076SZidan Wang static const struct reg_default fsl_esai_reg_defaults[] = {
7898973112aSZidan Wang 	{REG_ESAI_ETDR,	 0x00000000},
7908973112aSZidan Wang 	{REG_ESAI_ECR,	 0x00000000},
7918973112aSZidan Wang 	{REG_ESAI_TFCR,	 0x00000000},
7928973112aSZidan Wang 	{REG_ESAI_RFCR,	 0x00000000},
7938973112aSZidan Wang 	{REG_ESAI_TX0,	 0x00000000},
7948973112aSZidan Wang 	{REG_ESAI_TX1,	 0x00000000},
7958973112aSZidan Wang 	{REG_ESAI_TX2,	 0x00000000},
7968973112aSZidan Wang 	{REG_ESAI_TX3,	 0x00000000},
7978973112aSZidan Wang 	{REG_ESAI_TX4,	 0x00000000},
7988973112aSZidan Wang 	{REG_ESAI_TX5,	 0x00000000},
7998973112aSZidan Wang 	{REG_ESAI_TSR,	 0x00000000},
8008973112aSZidan Wang 	{REG_ESAI_SAICR, 0x00000000},
8018973112aSZidan Wang 	{REG_ESAI_TCR,	 0x00000000},
8028973112aSZidan Wang 	{REG_ESAI_TCCR,	 0x00000000},
8038973112aSZidan Wang 	{REG_ESAI_RCR,	 0x00000000},
8048973112aSZidan Wang 	{REG_ESAI_RCCR,	 0x00000000},
8058973112aSZidan Wang 	{REG_ESAI_TSMA,  0x0000ffff},
8068973112aSZidan Wang 	{REG_ESAI_TSMB,  0x0000ffff},
8078973112aSZidan Wang 	{REG_ESAI_RSMA,  0x0000ffff},
8088973112aSZidan Wang 	{REG_ESAI_RSMB,  0x0000ffff},
8098973112aSZidan Wang 	{REG_ESAI_PRRC,  0x00000000},
8108973112aSZidan Wang 	{REG_ESAI_PCRC,  0x00000000},
811c64c6076SZidan Wang };
812c64c6076SZidan Wang 
81343d24e76SNicolin Chen static bool fsl_esai_readable_reg(struct device *dev, unsigned int reg)
81443d24e76SNicolin Chen {
81543d24e76SNicolin Chen 	switch (reg) {
81643d24e76SNicolin Chen 	case REG_ESAI_ERDR:
81743d24e76SNicolin Chen 	case REG_ESAI_ECR:
81843d24e76SNicolin Chen 	case REG_ESAI_ESR:
81943d24e76SNicolin Chen 	case REG_ESAI_TFCR:
82043d24e76SNicolin Chen 	case REG_ESAI_TFSR:
82143d24e76SNicolin Chen 	case REG_ESAI_RFCR:
82243d24e76SNicolin Chen 	case REG_ESAI_RFSR:
82343d24e76SNicolin Chen 	case REG_ESAI_RX0:
82443d24e76SNicolin Chen 	case REG_ESAI_RX1:
82543d24e76SNicolin Chen 	case REG_ESAI_RX2:
82643d24e76SNicolin Chen 	case REG_ESAI_RX3:
82743d24e76SNicolin Chen 	case REG_ESAI_SAISR:
82843d24e76SNicolin Chen 	case REG_ESAI_SAICR:
82943d24e76SNicolin Chen 	case REG_ESAI_TCR:
83043d24e76SNicolin Chen 	case REG_ESAI_TCCR:
83143d24e76SNicolin Chen 	case REG_ESAI_RCR:
83243d24e76SNicolin Chen 	case REG_ESAI_RCCR:
83343d24e76SNicolin Chen 	case REG_ESAI_TSMA:
83443d24e76SNicolin Chen 	case REG_ESAI_TSMB:
83543d24e76SNicolin Chen 	case REG_ESAI_RSMA:
83643d24e76SNicolin Chen 	case REG_ESAI_RSMB:
83743d24e76SNicolin Chen 	case REG_ESAI_PRRC:
83843d24e76SNicolin Chen 	case REG_ESAI_PCRC:
83943d24e76SNicolin Chen 		return true;
84043d24e76SNicolin Chen 	default:
84143d24e76SNicolin Chen 		return false;
84243d24e76SNicolin Chen 	}
84343d24e76SNicolin Chen }
84443d24e76SNicolin Chen 
845c64c6076SZidan Wang static bool fsl_esai_volatile_reg(struct device *dev, unsigned int reg)
846c64c6076SZidan Wang {
847c64c6076SZidan Wang 	switch (reg) {
848c64c6076SZidan Wang 	case REG_ESAI_ERDR:
849c64c6076SZidan Wang 	case REG_ESAI_ESR:
850c64c6076SZidan Wang 	case REG_ESAI_TFSR:
851c64c6076SZidan Wang 	case REG_ESAI_RFSR:
852c64c6076SZidan Wang 	case REG_ESAI_RX0:
853c64c6076SZidan Wang 	case REG_ESAI_RX1:
854c64c6076SZidan Wang 	case REG_ESAI_RX2:
855c64c6076SZidan Wang 	case REG_ESAI_RX3:
856c64c6076SZidan Wang 	case REG_ESAI_SAISR:
857c64c6076SZidan Wang 		return true;
858c64c6076SZidan Wang 	default:
859c64c6076SZidan Wang 		return false;
860c64c6076SZidan Wang 	}
861c64c6076SZidan Wang }
862c64c6076SZidan Wang 
86343d24e76SNicolin Chen static bool fsl_esai_writeable_reg(struct device *dev, unsigned int reg)
86443d24e76SNicolin Chen {
86543d24e76SNicolin Chen 	switch (reg) {
86643d24e76SNicolin Chen 	case REG_ESAI_ETDR:
86743d24e76SNicolin Chen 	case REG_ESAI_ECR:
86843d24e76SNicolin Chen 	case REG_ESAI_TFCR:
86943d24e76SNicolin Chen 	case REG_ESAI_RFCR:
87043d24e76SNicolin Chen 	case REG_ESAI_TX0:
87143d24e76SNicolin Chen 	case REG_ESAI_TX1:
87243d24e76SNicolin Chen 	case REG_ESAI_TX2:
87343d24e76SNicolin Chen 	case REG_ESAI_TX3:
87443d24e76SNicolin Chen 	case REG_ESAI_TX4:
87543d24e76SNicolin Chen 	case REG_ESAI_TX5:
87643d24e76SNicolin Chen 	case REG_ESAI_TSR:
87743d24e76SNicolin Chen 	case REG_ESAI_SAICR:
87843d24e76SNicolin Chen 	case REG_ESAI_TCR:
87943d24e76SNicolin Chen 	case REG_ESAI_TCCR:
88043d24e76SNicolin Chen 	case REG_ESAI_RCR:
88143d24e76SNicolin Chen 	case REG_ESAI_RCCR:
88243d24e76SNicolin Chen 	case REG_ESAI_TSMA:
88343d24e76SNicolin Chen 	case REG_ESAI_TSMB:
88443d24e76SNicolin Chen 	case REG_ESAI_RSMA:
88543d24e76SNicolin Chen 	case REG_ESAI_RSMB:
88643d24e76SNicolin Chen 	case REG_ESAI_PRRC:
88743d24e76SNicolin Chen 	case REG_ESAI_PCRC:
88843d24e76SNicolin Chen 		return true;
88943d24e76SNicolin Chen 	default:
89043d24e76SNicolin Chen 		return false;
89143d24e76SNicolin Chen 	}
89243d24e76SNicolin Chen }
89343d24e76SNicolin Chen 
89492bd0334SXiubo Li static const struct regmap_config fsl_esai_regmap_config = {
89543d24e76SNicolin Chen 	.reg_bits = 32,
89643d24e76SNicolin Chen 	.reg_stride = 4,
89743d24e76SNicolin Chen 	.val_bits = 32,
89843d24e76SNicolin Chen 
89943d24e76SNicolin Chen 	.max_register = REG_ESAI_PCRC,
900c64c6076SZidan Wang 	.reg_defaults = fsl_esai_reg_defaults,
901c64c6076SZidan Wang 	.num_reg_defaults = ARRAY_SIZE(fsl_esai_reg_defaults),
90243d24e76SNicolin Chen 	.readable_reg = fsl_esai_readable_reg,
903c64c6076SZidan Wang 	.volatile_reg = fsl_esai_volatile_reg,
90443d24e76SNicolin Chen 	.writeable_reg = fsl_esai_writeable_reg,
9050effb865SMarek Vasut 	.cache_type = REGCACHE_FLAT,
90643d24e76SNicolin Chen };
90743d24e76SNicolin Chen 
90843d24e76SNicolin Chen static int fsl_esai_probe(struct platform_device *pdev)
90943d24e76SNicolin Chen {
91043d24e76SNicolin Chen 	struct device_node *np = pdev->dev.of_node;
91143d24e76SNicolin Chen 	struct fsl_esai *esai_priv;
91243d24e76SNicolin Chen 	struct resource *res;
9130600b3e1SFabio Estevam 	const __be32 *iprop;
91443d24e76SNicolin Chen 	void __iomem *regs;
91543d24e76SNicolin Chen 	int irq, ret;
91643d24e76SNicolin Chen 
91743d24e76SNicolin Chen 	esai_priv = devm_kzalloc(&pdev->dev, sizeof(*esai_priv), GFP_KERNEL);
91843d24e76SNicolin Chen 	if (!esai_priv)
91943d24e76SNicolin Chen 		return -ENOMEM;
92043d24e76SNicolin Chen 
92143d24e76SNicolin Chen 	esai_priv->pdev = pdev;
9225d585e1eSRob Herring 	snprintf(esai_priv->name, sizeof(esai_priv->name), "%pOFn", np);
92343d24e76SNicolin Chen 
9247ccafa2bSShengjiu Wang 	if (of_device_is_compatible(np, "fsl,vf610-esai") ||
9257ccafa2bSShengjiu Wang 	    of_device_is_compatible(np, "fsl,imx35-esai"))
9267ccafa2bSShengjiu Wang 		esai_priv->reset_at_xrun = true;
9277ccafa2bSShengjiu Wang 
92843d24e76SNicolin Chen 	/* Get the addresses and IRQ */
92943d24e76SNicolin Chen 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
93043d24e76SNicolin Chen 	regs = devm_ioremap_resource(&pdev->dev, res);
93143d24e76SNicolin Chen 	if (IS_ERR(regs))
93243d24e76SNicolin Chen 		return PTR_ERR(regs);
93343d24e76SNicolin Chen 
93443d24e76SNicolin Chen 	esai_priv->regmap = devm_regmap_init_mmio_clk(&pdev->dev,
93543d24e76SNicolin Chen 			"core", regs, &fsl_esai_regmap_config);
93643d24e76SNicolin Chen 	if (IS_ERR(esai_priv->regmap)) {
93743d24e76SNicolin Chen 		dev_err(&pdev->dev, "failed to init regmap: %ld\n",
93843d24e76SNicolin Chen 				PTR_ERR(esai_priv->regmap));
93943d24e76SNicolin Chen 		return PTR_ERR(esai_priv->regmap);
94043d24e76SNicolin Chen 	}
94143d24e76SNicolin Chen 
94243d24e76SNicolin Chen 	esai_priv->coreclk = devm_clk_get(&pdev->dev, "core");
94343d24e76SNicolin Chen 	if (IS_ERR(esai_priv->coreclk)) {
94443d24e76SNicolin Chen 		dev_err(&pdev->dev, "failed to get core clock: %ld\n",
94543d24e76SNicolin Chen 				PTR_ERR(esai_priv->coreclk));
94643d24e76SNicolin Chen 		return PTR_ERR(esai_priv->coreclk);
94743d24e76SNicolin Chen 	}
94843d24e76SNicolin Chen 
94943d24e76SNicolin Chen 	esai_priv->extalclk = devm_clk_get(&pdev->dev, "extal");
95043d24e76SNicolin Chen 	if (IS_ERR(esai_priv->extalclk))
95143d24e76SNicolin Chen 		dev_warn(&pdev->dev, "failed to get extal clock: %ld\n",
95243d24e76SNicolin Chen 				PTR_ERR(esai_priv->extalclk));
95343d24e76SNicolin Chen 
95443d24e76SNicolin Chen 	esai_priv->fsysclk = devm_clk_get(&pdev->dev, "fsys");
95543d24e76SNicolin Chen 	if (IS_ERR(esai_priv->fsysclk))
95643d24e76SNicolin Chen 		dev_warn(&pdev->dev, "failed to get fsys clock: %ld\n",
95743d24e76SNicolin Chen 				PTR_ERR(esai_priv->fsysclk));
95843d24e76SNicolin Chen 
959a2a4d604SShengjiu Wang 	esai_priv->spbaclk = devm_clk_get(&pdev->dev, "spba");
960a2a4d604SShengjiu Wang 	if (IS_ERR(esai_priv->spbaclk))
961a2a4d604SShengjiu Wang 		dev_warn(&pdev->dev, "failed to get spba clock: %ld\n",
962a2a4d604SShengjiu Wang 				PTR_ERR(esai_priv->spbaclk));
963a2a4d604SShengjiu Wang 
96443d24e76SNicolin Chen 	irq = platform_get_irq(pdev, 0);
965*cf9441adSStephen Boyd 	if (irq < 0)
96643d24e76SNicolin Chen 		return irq;
96743d24e76SNicolin Chen 
96843d24e76SNicolin Chen 	ret = devm_request_irq(&pdev->dev, irq, esai_isr, 0,
96943d24e76SNicolin Chen 			       esai_priv->name, esai_priv);
97043d24e76SNicolin Chen 	if (ret) {
97143d24e76SNicolin Chen 		dev_err(&pdev->dev, "failed to claim irq %u\n", irq);
97243d24e76SNicolin Chen 		return ret;
97343d24e76SNicolin Chen 	}
97443d24e76SNicolin Chen 
975de0d712aSShengjiu Wang 	/* Set a default slot number */
976de0d712aSShengjiu Wang 	esai_priv->slots = 2;
977de0d712aSShengjiu Wang 
97843d24e76SNicolin Chen 	/* Set a default master/slave state */
97943d24e76SNicolin Chen 	esai_priv->slave_mode = true;
98043d24e76SNicolin Chen 
98143d24e76SNicolin Chen 	/* Determine the FIFO depth */
98243d24e76SNicolin Chen 	iprop = of_get_property(np, "fsl,fifo-depth", NULL);
98343d24e76SNicolin Chen 	if (iprop)
98443d24e76SNicolin Chen 		esai_priv->fifo_depth = be32_to_cpup(iprop);
98543d24e76SNicolin Chen 	else
98643d24e76SNicolin Chen 		esai_priv->fifo_depth = 64;
98743d24e76SNicolin Chen 
98843d24e76SNicolin Chen 	esai_priv->dma_params_tx.maxburst = 16;
98943d24e76SNicolin Chen 	esai_priv->dma_params_rx.maxburst = 16;
99043d24e76SNicolin Chen 	esai_priv->dma_params_tx.addr = res->start + REG_ESAI_ETDR;
99143d24e76SNicolin Chen 	esai_priv->dma_params_rx.addr = res->start + REG_ESAI_ERDR;
99243d24e76SNicolin Chen 
99343d24e76SNicolin Chen 	esai_priv->synchronous =
99443d24e76SNicolin Chen 		of_property_read_bool(np, "fsl,esai-synchronous");
99543d24e76SNicolin Chen 
99643d24e76SNicolin Chen 	/* Implement full symmetry for synchronous mode */
99743d24e76SNicolin Chen 	if (esai_priv->synchronous) {
99843d24e76SNicolin Chen 		fsl_esai_dai.symmetric_rates = 1;
99943d24e76SNicolin Chen 		fsl_esai_dai.symmetric_channels = 1;
100043d24e76SNicolin Chen 		fsl_esai_dai.symmetric_samplebits = 1;
100143d24e76SNicolin Chen 	}
100243d24e76SNicolin Chen 
100343d24e76SNicolin Chen 	dev_set_drvdata(&pdev->dev, esai_priv);
100443d24e76SNicolin Chen 
10055be6155bSShengjiu Wang 	ret = fsl_esai_hw_init(esai_priv);
10065be6155bSShengjiu Wang 	if (ret)
100743d24e76SNicolin Chen 		return ret;
100843d24e76SNicolin Chen 
10090ff4e8c6SS.j. Wang 	esai_priv->tx_mask = 0xFFFFFFFF;
10100ff4e8c6SS.j. Wang 	esai_priv->rx_mask = 0xFFFFFFFF;
10110ff4e8c6SS.j. Wang 
10120ff4e8c6SS.j. Wang 	/* Clear the TSMA, TSMB, RSMA, RSMB */
10130ff4e8c6SS.j. Wang 	regmap_write(esai_priv->regmap, REG_ESAI_TSMA, 0);
10140ff4e8c6SS.j. Wang 	regmap_write(esai_priv->regmap, REG_ESAI_TSMB, 0);
10150ff4e8c6SS.j. Wang 	regmap_write(esai_priv->regmap, REG_ESAI_RSMA, 0);
10160ff4e8c6SS.j. Wang 	regmap_write(esai_priv->regmap, REG_ESAI_RSMB, 0);
10170ff4e8c6SS.j. Wang 
101843d24e76SNicolin Chen 	ret = devm_snd_soc_register_component(&pdev->dev, &fsl_esai_component,
101943d24e76SNicolin Chen 					      &fsl_esai_dai, 1);
102043d24e76SNicolin Chen 	if (ret) {
102143d24e76SNicolin Chen 		dev_err(&pdev->dev, "failed to register DAI: %d\n", ret);
102243d24e76SNicolin Chen 		return ret;
102343d24e76SNicolin Chen 	}
102443d24e76SNicolin Chen 
10257ccafa2bSShengjiu Wang 	tasklet_init(&esai_priv->task, fsl_esai_hw_reset,
10267ccafa2bSShengjiu Wang 		     (unsigned long)esai_priv);
10277ccafa2bSShengjiu Wang 
1028b2d337d8SS.j. Wang 	pm_runtime_enable(&pdev->dev);
1029b2d337d8SS.j. Wang 
1030b2d337d8SS.j. Wang 	regcache_cache_only(esai_priv->regmap, true);
1031b2d337d8SS.j. Wang 
10320d69e0ddSShengjiu Wang 	ret = imx_pcm_dma_init(pdev, IMX_ESAI_DMABUF_SIZE);
103343d24e76SNicolin Chen 	if (ret)
103443d24e76SNicolin Chen 		dev_err(&pdev->dev, "failed to init imx pcm dma: %d\n", ret);
103543d24e76SNicolin Chen 
103643d24e76SNicolin Chen 	return ret;
103743d24e76SNicolin Chen }
103843d24e76SNicolin Chen 
1039b2d337d8SS.j. Wang static int fsl_esai_remove(struct platform_device *pdev)
1040b2d337d8SS.j. Wang {
10417ccafa2bSShengjiu Wang 	struct fsl_esai *esai_priv = platform_get_drvdata(pdev);
10427ccafa2bSShengjiu Wang 
1043b2d337d8SS.j. Wang 	pm_runtime_disable(&pdev->dev);
10447ccafa2bSShengjiu Wang 	tasklet_kill(&esai_priv->task);
1045b2d337d8SS.j. Wang 
1046b2d337d8SS.j. Wang 	return 0;
1047b2d337d8SS.j. Wang }
1048b2d337d8SS.j. Wang 
104943d24e76SNicolin Chen static const struct of_device_id fsl_esai_dt_ids[] = {
105043d24e76SNicolin Chen 	{ .compatible = "fsl,imx35-esai", },
1051b21cc2f5SXiubo Li 	{ .compatible = "fsl,vf610-esai", },
105243d24e76SNicolin Chen 	{}
105343d24e76SNicolin Chen };
105443d24e76SNicolin Chen MODULE_DEVICE_TABLE(of, fsl_esai_dt_ids);
105543d24e76SNicolin Chen 
1056b2d337d8SS.j. Wang #ifdef CONFIG_PM
1057b2d337d8SS.j. Wang static int fsl_esai_runtime_resume(struct device *dev)
1058c64c6076SZidan Wang {
1059c64c6076SZidan Wang 	struct fsl_esai *esai = dev_get_drvdata(dev);
1060c64c6076SZidan Wang 	int ret;
1061c64c6076SZidan Wang 
1062b2d337d8SS.j. Wang 	/*
1063b2d337d8SS.j. Wang 	 * Some platforms might use the same bit to gate all three or two of
1064b2d337d8SS.j. Wang 	 * clocks, so keep all clocks open/close at the same time for safety
1065b2d337d8SS.j. Wang 	 */
1066b2d337d8SS.j. Wang 	ret = clk_prepare_enable(esai->coreclk);
1067b2d337d8SS.j. Wang 	if (ret)
1068b2d337d8SS.j. Wang 		return ret;
1069b2d337d8SS.j. Wang 	if (!IS_ERR(esai->spbaclk)) {
1070b2d337d8SS.j. Wang 		ret = clk_prepare_enable(esai->spbaclk);
1071b2d337d8SS.j. Wang 		if (ret)
1072b2d337d8SS.j. Wang 			goto err_spbaclk;
1073b2d337d8SS.j. Wang 	}
1074b2d337d8SS.j. Wang 	if (!IS_ERR(esai->extalclk)) {
1075b2d337d8SS.j. Wang 		ret = clk_prepare_enable(esai->extalclk);
1076b2d337d8SS.j. Wang 		if (ret)
1077b2d337d8SS.j. Wang 			goto err_extalclk;
1078b2d337d8SS.j. Wang 	}
1079b2d337d8SS.j. Wang 	if (!IS_ERR(esai->fsysclk)) {
1080b2d337d8SS.j. Wang 		ret = clk_prepare_enable(esai->fsysclk);
1081b2d337d8SS.j. Wang 		if (ret)
1082b2d337d8SS.j. Wang 			goto err_fsysclk;
1083b2d337d8SS.j. Wang 	}
1084b2d337d8SS.j. Wang 
1085c64c6076SZidan Wang 	regcache_cache_only(esai->regmap, false);
1086c64c6076SZidan Wang 
10875be6155bSShengjiu Wang 	ret = fsl_esai_register_restore(esai);
1088c64c6076SZidan Wang 	if (ret)
1089b2d337d8SS.j. Wang 		goto err_regcache_sync;
1090c64c6076SZidan Wang 
1091c64c6076SZidan Wang 	return 0;
1092b2d337d8SS.j. Wang 
1093b2d337d8SS.j. Wang err_regcache_sync:
1094b2d337d8SS.j. Wang 	if (!IS_ERR(esai->fsysclk))
1095b2d337d8SS.j. Wang 		clk_disable_unprepare(esai->fsysclk);
1096b2d337d8SS.j. Wang err_fsysclk:
1097b2d337d8SS.j. Wang 	if (!IS_ERR(esai->extalclk))
1098b2d337d8SS.j. Wang 		clk_disable_unprepare(esai->extalclk);
1099b2d337d8SS.j. Wang err_extalclk:
1100b2d337d8SS.j. Wang 	if (!IS_ERR(esai->spbaclk))
1101b2d337d8SS.j. Wang 		clk_disable_unprepare(esai->spbaclk);
1102b2d337d8SS.j. Wang err_spbaclk:
1103b2d337d8SS.j. Wang 	clk_disable_unprepare(esai->coreclk);
1104b2d337d8SS.j. Wang 
1105b2d337d8SS.j. Wang 	return ret;
1106c64c6076SZidan Wang }
1107b2d337d8SS.j. Wang 
1108b2d337d8SS.j. Wang static int fsl_esai_runtime_suspend(struct device *dev)
1109b2d337d8SS.j. Wang {
1110b2d337d8SS.j. Wang 	struct fsl_esai *esai = dev_get_drvdata(dev);
1111b2d337d8SS.j. Wang 
1112b2d337d8SS.j. Wang 	regcache_cache_only(esai->regmap, true);
1113b2d337d8SS.j. Wang 
1114b2d337d8SS.j. Wang 	if (!IS_ERR(esai->fsysclk))
1115b2d337d8SS.j. Wang 		clk_disable_unprepare(esai->fsysclk);
1116b2d337d8SS.j. Wang 	if (!IS_ERR(esai->extalclk))
1117b2d337d8SS.j. Wang 		clk_disable_unprepare(esai->extalclk);
1118b2d337d8SS.j. Wang 	if (!IS_ERR(esai->spbaclk))
1119b2d337d8SS.j. Wang 		clk_disable_unprepare(esai->spbaclk);
1120b2d337d8SS.j. Wang 	clk_disable_unprepare(esai->coreclk);
1121b2d337d8SS.j. Wang 
1122b2d337d8SS.j. Wang 	return 0;
1123b2d337d8SS.j. Wang }
1124b2d337d8SS.j. Wang #endif /* CONFIG_PM */
1125c64c6076SZidan Wang 
1126c64c6076SZidan Wang static const struct dev_pm_ops fsl_esai_pm_ops = {
1127b2d337d8SS.j. Wang 	SET_RUNTIME_PM_OPS(fsl_esai_runtime_suspend,
1128b2d337d8SS.j. Wang 			   fsl_esai_runtime_resume,
1129b2d337d8SS.j. Wang 			   NULL)
1130b2d337d8SS.j. Wang 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1131b2d337d8SS.j. Wang 				pm_runtime_force_resume)
1132c64c6076SZidan Wang };
1133c64c6076SZidan Wang 
113443d24e76SNicolin Chen static struct platform_driver fsl_esai_driver = {
113543d24e76SNicolin Chen 	.probe = fsl_esai_probe,
1136b2d337d8SS.j. Wang 	.remove = fsl_esai_remove,
113743d24e76SNicolin Chen 	.driver = {
113843d24e76SNicolin Chen 		.name = "fsl-esai-dai",
1139c64c6076SZidan Wang 		.pm = &fsl_esai_pm_ops,
114043d24e76SNicolin Chen 		.of_match_table = fsl_esai_dt_ids,
114143d24e76SNicolin Chen 	},
114243d24e76SNicolin Chen };
114343d24e76SNicolin Chen 
114443d24e76SNicolin Chen module_platform_driver(fsl_esai_driver);
114543d24e76SNicolin Chen 
114643d24e76SNicolin Chen MODULE_AUTHOR("Freescale Semiconductor, Inc.");
114743d24e76SNicolin Chen MODULE_DESCRIPTION("Freescale ESAI CPU DAI driver");
114843d24e76SNicolin Chen MODULE_LICENSE("GPL v2");
114943d24e76SNicolin Chen MODULE_ALIAS("platform:fsl-esai-dai");
1150