xref: /linux/sound/soc/fsl/fsl_esai.c (revision 4e0ae876f77bc01a7e77724dea57b4b82bd53244)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Freescale ESAI ALSA SoC Digital Audio Interface (DAI) driver
4 //
5 // Copyright (C) 2014 Freescale Semiconductor, Inc.
6 
7 #include <linux/clk.h>
8 #include <linux/dmaengine.h>
9 #include <linux/module.h>
10 #include <linux/of_irq.h>
11 #include <linux/of_platform.h>
12 #include <sound/dmaengine_pcm.h>
13 #include <sound/pcm_params.h>
14 
15 #include "fsl_esai.h"
16 #include "imx-pcm.h"
17 
18 #define FSL_ESAI_FORMATS	(SNDRV_PCM_FMTBIT_S8 | \
19 				SNDRV_PCM_FMTBIT_S16_LE | \
20 				SNDRV_PCM_FMTBIT_S20_3LE | \
21 				SNDRV_PCM_FMTBIT_S24_LE)
22 
23 /**
24  * fsl_esai: ESAI private data
25  *
26  * @dma_params_rx: DMA parameters for receive channel
27  * @dma_params_tx: DMA parameters for transmit channel
28  * @pdev: platform device pointer
29  * @regmap: regmap handler
30  * @coreclk: clock source to access register
31  * @extalclk: esai clock source to derive HCK, SCK and FS
32  * @fsysclk: system clock source to derive HCK, SCK and FS
33  * @spbaclk: SPBA clock (optional, depending on SoC design)
34  * @fifo_depth: depth of tx/rx FIFO
35  * @slot_width: width of each DAI slot
36  * @slots: number of slots
37  * @hck_rate: clock rate of desired HCKx clock
38  * @sck_rate: clock rate of desired SCKx clock
39  * @hck_dir: the direction of HCKx pads
40  * @sck_div: if using PSR/PM dividers for SCKx clock
41  * @slave_mode: if fully using DAI slave mode
42  * @synchronous: if using tx/rx synchronous mode
43  * @name: driver name
44  */
45 struct fsl_esai {
46 	struct snd_dmaengine_dai_dma_data dma_params_rx;
47 	struct snd_dmaengine_dai_dma_data dma_params_tx;
48 	struct platform_device *pdev;
49 	struct regmap *regmap;
50 	struct clk *coreclk;
51 	struct clk *extalclk;
52 	struct clk *fsysclk;
53 	struct clk *spbaclk;
54 	u32 fifo_depth;
55 	u32 slot_width;
56 	u32 slots;
57 	u32 hck_rate[2];
58 	u32 sck_rate[2];
59 	bool hck_dir[2];
60 	bool sck_div[2];
61 	bool slave_mode;
62 	bool synchronous;
63 	char name[32];
64 };
65 
66 static irqreturn_t esai_isr(int irq, void *devid)
67 {
68 	struct fsl_esai *esai_priv = (struct fsl_esai *)devid;
69 	struct platform_device *pdev = esai_priv->pdev;
70 	u32 esr;
71 
72 	regmap_read(esai_priv->regmap, REG_ESAI_ESR, &esr);
73 
74 	if (esr & ESAI_ESR_TINIT_MASK)
75 		dev_dbg(&pdev->dev, "isr: Transmission Initialized\n");
76 
77 	if (esr & ESAI_ESR_RFF_MASK)
78 		dev_warn(&pdev->dev, "isr: Receiving overrun\n");
79 
80 	if (esr & ESAI_ESR_TFE_MASK)
81 		dev_warn(&pdev->dev, "isr: Transmission underrun\n");
82 
83 	if (esr & ESAI_ESR_TLS_MASK)
84 		dev_dbg(&pdev->dev, "isr: Just transmitted the last slot\n");
85 
86 	if (esr & ESAI_ESR_TDE_MASK)
87 		dev_dbg(&pdev->dev, "isr: Transmission data exception\n");
88 
89 	if (esr & ESAI_ESR_TED_MASK)
90 		dev_dbg(&pdev->dev, "isr: Transmitting even slots\n");
91 
92 	if (esr & ESAI_ESR_TD_MASK)
93 		dev_dbg(&pdev->dev, "isr: Transmitting data\n");
94 
95 	if (esr & ESAI_ESR_RLS_MASK)
96 		dev_dbg(&pdev->dev, "isr: Just received the last slot\n");
97 
98 	if (esr & ESAI_ESR_RDE_MASK)
99 		dev_dbg(&pdev->dev, "isr: Receiving data exception\n");
100 
101 	if (esr & ESAI_ESR_RED_MASK)
102 		dev_dbg(&pdev->dev, "isr: Receiving even slots\n");
103 
104 	if (esr & ESAI_ESR_RD_MASK)
105 		dev_dbg(&pdev->dev, "isr: Receiving data\n");
106 
107 	return IRQ_HANDLED;
108 }
109 
110 /**
111  * This function is used to calculate the divisors of psr, pm, fp and it is
112  * supposed to be called in set_dai_sysclk() and set_bclk().
113  *
114  * @ratio: desired overall ratio for the paticipating dividers
115  * @usefp: for HCK setting, there is no need to set fp divider
116  * @fp: bypass other dividers by setting fp directly if fp != 0
117  * @tx: current setting is for playback or capture
118  */
119 static int fsl_esai_divisor_cal(struct snd_soc_dai *dai, bool tx, u32 ratio,
120 				bool usefp, u32 fp)
121 {
122 	struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
123 	u32 psr, pm = 999, maxfp, prod, sub, savesub, i, j;
124 
125 	maxfp = usefp ? 16 : 1;
126 
127 	if (usefp && fp)
128 		goto out_fp;
129 
130 	if (ratio > 2 * 8 * 256 * maxfp || ratio < 2) {
131 		dev_err(dai->dev, "the ratio is out of range (2 ~ %d)\n",
132 				2 * 8 * 256 * maxfp);
133 		return -EINVAL;
134 	} else if (ratio % 2) {
135 		dev_err(dai->dev, "the raio must be even if using upper divider\n");
136 		return -EINVAL;
137 	}
138 
139 	ratio /= 2;
140 
141 	psr = ratio <= 256 * maxfp ? ESAI_xCCR_xPSR_BYPASS : ESAI_xCCR_xPSR_DIV8;
142 
143 	/* Do not loop-search if PM (1 ~ 256) alone can serve the ratio */
144 	if (ratio <= 256) {
145 		pm = ratio;
146 		fp = 1;
147 		goto out;
148 	}
149 
150 	/* Set the max fluctuation -- 0.1% of the max devisor */
151 	savesub = (psr ? 1 : 8)  * 256 * maxfp / 1000;
152 
153 	/* Find the best value for PM */
154 	for (i = 1; i <= 256; i++) {
155 		for (j = 1; j <= maxfp; j++) {
156 			/* PSR (1 or 8) * PM (1 ~ 256) * FP (1 ~ 16) */
157 			prod = (psr ? 1 : 8) * i * j;
158 
159 			if (prod == ratio)
160 				sub = 0;
161 			else if (prod / ratio == 1)
162 				sub = prod - ratio;
163 			else if (ratio / prod == 1)
164 				sub = ratio - prod;
165 			else
166 				continue;
167 
168 			/* Calculate the fraction */
169 			sub = sub * 1000 / ratio;
170 			if (sub < savesub) {
171 				savesub = sub;
172 				pm = i;
173 				fp = j;
174 			}
175 
176 			/* We are lucky */
177 			if (savesub == 0)
178 				goto out;
179 		}
180 	}
181 
182 	if (pm == 999) {
183 		dev_err(dai->dev, "failed to calculate proper divisors\n");
184 		return -EINVAL;
185 	}
186 
187 out:
188 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xCCR(tx),
189 			   ESAI_xCCR_xPSR_MASK | ESAI_xCCR_xPM_MASK,
190 			   psr | ESAI_xCCR_xPM(pm));
191 
192 out_fp:
193 	/* Bypass fp if not being required */
194 	if (maxfp <= 1)
195 		return 0;
196 
197 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xCCR(tx),
198 			   ESAI_xCCR_xFP_MASK, ESAI_xCCR_xFP(fp));
199 
200 	return 0;
201 }
202 
203 /**
204  * This function mainly configures the clock frequency of MCLK (HCKT/HCKR)
205  *
206  * @Parameters:
207  * clk_id: The clock source of HCKT/HCKR
208  *	  (Input from outside; output from inside, FSYS or EXTAL)
209  * freq: The required clock rate of HCKT/HCKR
210  * dir: The clock direction of HCKT/HCKR
211  *
212  * Note: If the direction is input, we do not care about clk_id.
213  */
214 static int fsl_esai_set_dai_sysclk(struct snd_soc_dai *dai, int clk_id,
215 				   unsigned int freq, int dir)
216 {
217 	struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
218 	struct clk *clksrc = esai_priv->extalclk;
219 	bool tx = clk_id <= ESAI_HCKT_EXTAL;
220 	bool in = dir == SND_SOC_CLOCK_IN;
221 	u32 ratio, ecr = 0;
222 	unsigned long clk_rate;
223 	int ret;
224 
225 	if (freq == 0) {
226 		dev_err(dai->dev, "%sput freq of HCK%c should not be 0Hz\n",
227 			in ? "in" : "out", tx ? 'T' : 'R');
228 		return -EINVAL;
229 	}
230 
231 	/* Bypass divider settings if the requirement doesn't change */
232 	if (freq == esai_priv->hck_rate[tx] && dir == esai_priv->hck_dir[tx])
233 		return 0;
234 
235 	/* sck_div can be only bypassed if ETO/ERO=0 and SNC_SOC_CLOCK_OUT */
236 	esai_priv->sck_div[tx] = true;
237 
238 	/* Set the direction of HCKT/HCKR pins */
239 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xCCR(tx),
240 			   ESAI_xCCR_xHCKD, in ? 0 : ESAI_xCCR_xHCKD);
241 
242 	if (in)
243 		goto out;
244 
245 	switch (clk_id) {
246 	case ESAI_HCKT_FSYS:
247 	case ESAI_HCKR_FSYS:
248 		clksrc = esai_priv->fsysclk;
249 		break;
250 	case ESAI_HCKT_EXTAL:
251 		ecr |= ESAI_ECR_ETI;
252 		/* fall through */
253 	case ESAI_HCKR_EXTAL:
254 		ecr |= ESAI_ECR_ERI;
255 		break;
256 	default:
257 		return -EINVAL;
258 	}
259 
260 	if (IS_ERR(clksrc)) {
261 		dev_err(dai->dev, "no assigned %s clock\n",
262 				clk_id % 2 ? "extal" : "fsys");
263 		return PTR_ERR(clksrc);
264 	}
265 	clk_rate = clk_get_rate(clksrc);
266 
267 	ratio = clk_rate / freq;
268 	if (ratio * freq > clk_rate)
269 		ret = ratio * freq - clk_rate;
270 	else if (ratio * freq < clk_rate)
271 		ret = clk_rate - ratio * freq;
272 	else
273 		ret = 0;
274 
275 	/* Block if clock source can not be divided into the required rate */
276 	if (ret != 0 && clk_rate / ret < 1000) {
277 		dev_err(dai->dev, "failed to derive required HCK%c rate\n",
278 				tx ? 'T' : 'R');
279 		return -EINVAL;
280 	}
281 
282 	/* Only EXTAL source can be output directly without using PSR and PM */
283 	if (ratio == 1 && clksrc == esai_priv->extalclk) {
284 		/* Bypass all the dividers if not being needed */
285 		ecr |= tx ? ESAI_ECR_ETO : ESAI_ECR_ERO;
286 		goto out;
287 	} else if (ratio < 2) {
288 		/* The ratio should be no less than 2 if using other sources */
289 		dev_err(dai->dev, "failed to derive required HCK%c rate\n",
290 				tx ? 'T' : 'R');
291 		return -EINVAL;
292 	}
293 
294 	ret = fsl_esai_divisor_cal(dai, tx, ratio, false, 0);
295 	if (ret)
296 		return ret;
297 
298 	esai_priv->sck_div[tx] = false;
299 
300 out:
301 	esai_priv->hck_dir[tx] = dir;
302 	esai_priv->hck_rate[tx] = freq;
303 
304 	regmap_update_bits(esai_priv->regmap, REG_ESAI_ECR,
305 			   tx ? ESAI_ECR_ETI | ESAI_ECR_ETO :
306 			   ESAI_ECR_ERI | ESAI_ECR_ERO, ecr);
307 
308 	return 0;
309 }
310 
311 /**
312  * This function configures the related dividers according to the bclk rate
313  */
314 static int fsl_esai_set_bclk(struct snd_soc_dai *dai, bool tx, u32 freq)
315 {
316 	struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
317 	u32 hck_rate = esai_priv->hck_rate[tx];
318 	u32 sub, ratio = hck_rate / freq;
319 	int ret;
320 
321 	/* Don't apply for fully slave mode or unchanged bclk */
322 	if (esai_priv->slave_mode || esai_priv->sck_rate[tx] == freq)
323 		return 0;
324 
325 	if (ratio * freq > hck_rate)
326 		sub = ratio * freq - hck_rate;
327 	else if (ratio * freq < hck_rate)
328 		sub = hck_rate - ratio * freq;
329 	else
330 		sub = 0;
331 
332 	/* Block if clock source can not be divided into the required rate */
333 	if (sub != 0 && hck_rate / sub < 1000) {
334 		dev_err(dai->dev, "failed to derive required SCK%c rate\n",
335 				tx ? 'T' : 'R');
336 		return -EINVAL;
337 	}
338 
339 	/* The ratio should be contented by FP alone if bypassing PM and PSR */
340 	if (!esai_priv->sck_div[tx] && (ratio > 16 || ratio == 0)) {
341 		dev_err(dai->dev, "the ratio is out of range (1 ~ 16)\n");
342 		return -EINVAL;
343 	}
344 
345 	ret = fsl_esai_divisor_cal(dai, tx, ratio, true,
346 			esai_priv->sck_div[tx] ? 0 : ratio);
347 	if (ret)
348 		return ret;
349 
350 	/* Save current bclk rate */
351 	esai_priv->sck_rate[tx] = freq;
352 
353 	return 0;
354 }
355 
356 static int fsl_esai_set_dai_tdm_slot(struct snd_soc_dai *dai, u32 tx_mask,
357 				     u32 rx_mask, int slots, int slot_width)
358 {
359 	struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
360 
361 	regmap_update_bits(esai_priv->regmap, REG_ESAI_TCCR,
362 			   ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(slots));
363 
364 	regmap_update_bits(esai_priv->regmap, REG_ESAI_TSMA,
365 			   ESAI_xSMA_xS_MASK, ESAI_xSMA_xS(tx_mask));
366 	regmap_update_bits(esai_priv->regmap, REG_ESAI_TSMB,
367 			   ESAI_xSMB_xS_MASK, ESAI_xSMB_xS(tx_mask));
368 
369 	regmap_update_bits(esai_priv->regmap, REG_ESAI_RCCR,
370 			   ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(slots));
371 
372 	regmap_update_bits(esai_priv->regmap, REG_ESAI_RSMA,
373 			   ESAI_xSMA_xS_MASK, ESAI_xSMA_xS(rx_mask));
374 	regmap_update_bits(esai_priv->regmap, REG_ESAI_RSMB,
375 			   ESAI_xSMB_xS_MASK, ESAI_xSMB_xS(rx_mask));
376 
377 	esai_priv->slot_width = slot_width;
378 	esai_priv->slots = slots;
379 
380 	return 0;
381 }
382 
383 static int fsl_esai_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
384 {
385 	struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
386 	u32 xcr = 0, xccr = 0, mask;
387 
388 	/* DAI mode */
389 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
390 	case SND_SOC_DAIFMT_I2S:
391 		/* Data on rising edge of bclk, frame low, 1clk before data */
392 		xcr |= ESAI_xCR_xFSR;
393 		xccr |= ESAI_xCCR_xFSP | ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP;
394 		break;
395 	case SND_SOC_DAIFMT_LEFT_J:
396 		/* Data on rising edge of bclk, frame high */
397 		xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP;
398 		break;
399 	case SND_SOC_DAIFMT_RIGHT_J:
400 		/* Data on rising edge of bclk, frame high, right aligned */
401 		xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP;
402 		xcr  |= ESAI_xCR_xWA;
403 		break;
404 	case SND_SOC_DAIFMT_DSP_A:
405 		/* Data on rising edge of bclk, frame high, 1clk before data */
406 		xcr |= ESAI_xCR_xFSL | ESAI_xCR_xFSR;
407 		xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP;
408 		break;
409 	case SND_SOC_DAIFMT_DSP_B:
410 		/* Data on rising edge of bclk, frame high */
411 		xcr |= ESAI_xCR_xFSL;
412 		xccr |= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP;
413 		break;
414 	default:
415 		return -EINVAL;
416 	}
417 
418 	/* DAI clock inversion */
419 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
420 	case SND_SOC_DAIFMT_NB_NF:
421 		/* Nothing to do for both normal cases */
422 		break;
423 	case SND_SOC_DAIFMT_IB_NF:
424 		/* Invert bit clock */
425 		xccr ^= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP;
426 		break;
427 	case SND_SOC_DAIFMT_NB_IF:
428 		/* Invert frame clock */
429 		xccr ^= ESAI_xCCR_xFSP;
430 		break;
431 	case SND_SOC_DAIFMT_IB_IF:
432 		/* Invert both clocks */
433 		xccr ^= ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP | ESAI_xCCR_xFSP;
434 		break;
435 	default:
436 		return -EINVAL;
437 	}
438 
439 	esai_priv->slave_mode = false;
440 
441 	/* DAI clock master masks */
442 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
443 	case SND_SOC_DAIFMT_CBM_CFM:
444 		esai_priv->slave_mode = true;
445 		break;
446 	case SND_SOC_DAIFMT_CBS_CFM:
447 		xccr |= ESAI_xCCR_xCKD;
448 		break;
449 	case SND_SOC_DAIFMT_CBM_CFS:
450 		xccr |= ESAI_xCCR_xFSD;
451 		break;
452 	case SND_SOC_DAIFMT_CBS_CFS:
453 		xccr |= ESAI_xCCR_xFSD | ESAI_xCCR_xCKD;
454 		break;
455 	default:
456 		return -EINVAL;
457 	}
458 
459 	mask = ESAI_xCR_xFSL | ESAI_xCR_xFSR | ESAI_xCR_xWA;
460 	regmap_update_bits(esai_priv->regmap, REG_ESAI_TCR, mask, xcr);
461 	regmap_update_bits(esai_priv->regmap, REG_ESAI_RCR, mask, xcr);
462 
463 	mask = ESAI_xCCR_xCKP | ESAI_xCCR_xHCKP | ESAI_xCCR_xFSP |
464 		ESAI_xCCR_xFSD | ESAI_xCCR_xCKD;
465 	regmap_update_bits(esai_priv->regmap, REG_ESAI_TCCR, mask, xccr);
466 	regmap_update_bits(esai_priv->regmap, REG_ESAI_RCCR, mask, xccr);
467 
468 	return 0;
469 }
470 
471 static int fsl_esai_startup(struct snd_pcm_substream *substream,
472 			    struct snd_soc_dai *dai)
473 {
474 	struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
475 	int ret;
476 
477 	/*
478 	 * Some platforms might use the same bit to gate all three or two of
479 	 * clocks, so keep all clocks open/close at the same time for safety
480 	 */
481 	ret = clk_prepare_enable(esai_priv->coreclk);
482 	if (ret)
483 		return ret;
484 	if (!IS_ERR(esai_priv->spbaclk)) {
485 		ret = clk_prepare_enable(esai_priv->spbaclk);
486 		if (ret)
487 			goto err_spbaclk;
488 	}
489 	if (!IS_ERR(esai_priv->extalclk)) {
490 		ret = clk_prepare_enable(esai_priv->extalclk);
491 		if (ret)
492 			goto err_extalck;
493 	}
494 	if (!IS_ERR(esai_priv->fsysclk)) {
495 		ret = clk_prepare_enable(esai_priv->fsysclk);
496 		if (ret)
497 			goto err_fsysclk;
498 	}
499 
500 	if (!dai->active) {
501 		/* Set synchronous mode */
502 		regmap_update_bits(esai_priv->regmap, REG_ESAI_SAICR,
503 				   ESAI_SAICR_SYNC, esai_priv->synchronous ?
504 				   ESAI_SAICR_SYNC : 0);
505 
506 		/* Set a default slot number -- 2 */
507 		regmap_update_bits(esai_priv->regmap, REG_ESAI_TCCR,
508 				   ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(2));
509 		regmap_update_bits(esai_priv->regmap, REG_ESAI_RCCR,
510 				   ESAI_xCCR_xDC_MASK, ESAI_xCCR_xDC(2));
511 	}
512 
513 	return 0;
514 
515 err_fsysclk:
516 	if (!IS_ERR(esai_priv->extalclk))
517 		clk_disable_unprepare(esai_priv->extalclk);
518 err_extalck:
519 	if (!IS_ERR(esai_priv->spbaclk))
520 		clk_disable_unprepare(esai_priv->spbaclk);
521 err_spbaclk:
522 	clk_disable_unprepare(esai_priv->coreclk);
523 
524 	return ret;
525 }
526 
527 static int fsl_esai_hw_params(struct snd_pcm_substream *substream,
528 			      struct snd_pcm_hw_params *params,
529 			      struct snd_soc_dai *dai)
530 {
531 	struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
532 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
533 	u32 width = params_width(params);
534 	u32 channels = params_channels(params);
535 	u32 pins = DIV_ROUND_UP(channels, esai_priv->slots);
536 	u32 slot_width = width;
537 	u32 bclk, mask, val;
538 	int ret;
539 
540 	/* Override slot_width if being specifically set */
541 	if (esai_priv->slot_width)
542 		slot_width = esai_priv->slot_width;
543 
544 	bclk = params_rate(params) * slot_width * esai_priv->slots;
545 
546 	ret = fsl_esai_set_bclk(dai, tx, bclk);
547 	if (ret)
548 		return ret;
549 
550 	/* Use Normal mode to support monaural audio */
551 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx),
552 			   ESAI_xCR_xMOD_MASK, params_channels(params) > 1 ?
553 			   ESAI_xCR_xMOD_NETWORK : 0);
554 
555 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx),
556 			   ESAI_xFCR_xFR_MASK, ESAI_xFCR_xFR);
557 
558 	mask = ESAI_xFCR_xFR_MASK | ESAI_xFCR_xWA_MASK | ESAI_xFCR_xFWM_MASK |
559 	      (tx ? ESAI_xFCR_TE_MASK | ESAI_xFCR_TIEN : ESAI_xFCR_RE_MASK);
560 	val = ESAI_xFCR_xWA(width) | ESAI_xFCR_xFWM(esai_priv->fifo_depth) |
561 	     (tx ? ESAI_xFCR_TE(pins) | ESAI_xFCR_TIEN : ESAI_xFCR_RE(pins));
562 
563 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx), mask, val);
564 
565 	mask = ESAI_xCR_xSWS_MASK | (tx ? ESAI_xCR_PADC : 0);
566 	val = ESAI_xCR_xSWS(slot_width, width) | (tx ? ESAI_xCR_PADC : 0);
567 
568 	regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx), mask, val);
569 
570 	/* Remove ESAI personal reset by configuring ESAI_PCRC and ESAI_PRRC */
571 	regmap_update_bits(esai_priv->regmap, REG_ESAI_PRRC,
572 			   ESAI_PRRC_PDC_MASK, ESAI_PRRC_PDC(ESAI_GPIO));
573 	regmap_update_bits(esai_priv->regmap, REG_ESAI_PCRC,
574 			   ESAI_PCRC_PC_MASK, ESAI_PCRC_PC(ESAI_GPIO));
575 	return 0;
576 }
577 
578 static void fsl_esai_shutdown(struct snd_pcm_substream *substream,
579 			      struct snd_soc_dai *dai)
580 {
581 	struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
582 
583 	if (!IS_ERR(esai_priv->fsysclk))
584 		clk_disable_unprepare(esai_priv->fsysclk);
585 	if (!IS_ERR(esai_priv->extalclk))
586 		clk_disable_unprepare(esai_priv->extalclk);
587 	if (!IS_ERR(esai_priv->spbaclk))
588 		clk_disable_unprepare(esai_priv->spbaclk);
589 	clk_disable_unprepare(esai_priv->coreclk);
590 }
591 
592 static int fsl_esai_trigger(struct snd_pcm_substream *substream, int cmd,
593 			    struct snd_soc_dai *dai)
594 {
595 	struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
596 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
597 	u8 i, channels = substream->runtime->channels;
598 	u32 pins = DIV_ROUND_UP(channels, esai_priv->slots);
599 
600 	switch (cmd) {
601 	case SNDRV_PCM_TRIGGER_START:
602 	case SNDRV_PCM_TRIGGER_RESUME:
603 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
604 		regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx),
605 				   ESAI_xFCR_xFEN_MASK, ESAI_xFCR_xFEN);
606 
607 		/* Write initial words reqiured by ESAI as normal procedure */
608 		for (i = 0; tx && i < channels; i++)
609 			regmap_write(esai_priv->regmap, REG_ESAI_ETDR, 0x0);
610 
611 		regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx),
612 				   tx ? ESAI_xCR_TE_MASK : ESAI_xCR_RE_MASK,
613 				   tx ? ESAI_xCR_TE(pins) : ESAI_xCR_RE(pins));
614 		break;
615 	case SNDRV_PCM_TRIGGER_SUSPEND:
616 	case SNDRV_PCM_TRIGGER_STOP:
617 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
618 		regmap_update_bits(esai_priv->regmap, REG_ESAI_xCR(tx),
619 				   tx ? ESAI_xCR_TE_MASK : ESAI_xCR_RE_MASK, 0);
620 
621 		/* Disable and reset FIFO */
622 		regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx),
623 				   ESAI_xFCR_xFR | ESAI_xFCR_xFEN, ESAI_xFCR_xFR);
624 		regmap_update_bits(esai_priv->regmap, REG_ESAI_xFCR(tx),
625 				   ESAI_xFCR_xFR, 0);
626 		break;
627 	default:
628 		return -EINVAL;
629 	}
630 
631 	return 0;
632 }
633 
634 static const struct snd_soc_dai_ops fsl_esai_dai_ops = {
635 	.startup = fsl_esai_startup,
636 	.shutdown = fsl_esai_shutdown,
637 	.trigger = fsl_esai_trigger,
638 	.hw_params = fsl_esai_hw_params,
639 	.set_sysclk = fsl_esai_set_dai_sysclk,
640 	.set_fmt = fsl_esai_set_dai_fmt,
641 	.set_tdm_slot = fsl_esai_set_dai_tdm_slot,
642 };
643 
644 static int fsl_esai_dai_probe(struct snd_soc_dai *dai)
645 {
646 	struct fsl_esai *esai_priv = snd_soc_dai_get_drvdata(dai);
647 
648 	snd_soc_dai_init_dma_data(dai, &esai_priv->dma_params_tx,
649 				  &esai_priv->dma_params_rx);
650 
651 	return 0;
652 }
653 
654 static struct snd_soc_dai_driver fsl_esai_dai = {
655 	.probe = fsl_esai_dai_probe,
656 	.playback = {
657 		.stream_name = "CPU-Playback",
658 		.channels_min = 1,
659 		.channels_max = 12,
660 		.rates = SNDRV_PCM_RATE_8000_192000,
661 		.formats = FSL_ESAI_FORMATS,
662 	},
663 	.capture = {
664 		.stream_name = "CPU-Capture",
665 		.channels_min = 1,
666 		.channels_max = 8,
667 		.rates = SNDRV_PCM_RATE_8000_192000,
668 		.formats = FSL_ESAI_FORMATS,
669 	},
670 	.ops = &fsl_esai_dai_ops,
671 };
672 
673 static const struct snd_soc_component_driver fsl_esai_component = {
674 	.name		= "fsl-esai",
675 };
676 
677 static const struct reg_default fsl_esai_reg_defaults[] = {
678 	{REG_ESAI_ETDR,	 0x00000000},
679 	{REG_ESAI_ECR,	 0x00000000},
680 	{REG_ESAI_TFCR,	 0x00000000},
681 	{REG_ESAI_RFCR,	 0x00000000},
682 	{REG_ESAI_TX0,	 0x00000000},
683 	{REG_ESAI_TX1,	 0x00000000},
684 	{REG_ESAI_TX2,	 0x00000000},
685 	{REG_ESAI_TX3,	 0x00000000},
686 	{REG_ESAI_TX4,	 0x00000000},
687 	{REG_ESAI_TX5,	 0x00000000},
688 	{REG_ESAI_TSR,	 0x00000000},
689 	{REG_ESAI_SAICR, 0x00000000},
690 	{REG_ESAI_TCR,	 0x00000000},
691 	{REG_ESAI_TCCR,	 0x00000000},
692 	{REG_ESAI_RCR,	 0x00000000},
693 	{REG_ESAI_RCCR,	 0x00000000},
694 	{REG_ESAI_TSMA,  0x0000ffff},
695 	{REG_ESAI_TSMB,  0x0000ffff},
696 	{REG_ESAI_RSMA,  0x0000ffff},
697 	{REG_ESAI_RSMB,  0x0000ffff},
698 	{REG_ESAI_PRRC,  0x00000000},
699 	{REG_ESAI_PCRC,  0x00000000},
700 };
701 
702 static bool fsl_esai_readable_reg(struct device *dev, unsigned int reg)
703 {
704 	switch (reg) {
705 	case REG_ESAI_ERDR:
706 	case REG_ESAI_ECR:
707 	case REG_ESAI_ESR:
708 	case REG_ESAI_TFCR:
709 	case REG_ESAI_TFSR:
710 	case REG_ESAI_RFCR:
711 	case REG_ESAI_RFSR:
712 	case REG_ESAI_RX0:
713 	case REG_ESAI_RX1:
714 	case REG_ESAI_RX2:
715 	case REG_ESAI_RX3:
716 	case REG_ESAI_SAISR:
717 	case REG_ESAI_SAICR:
718 	case REG_ESAI_TCR:
719 	case REG_ESAI_TCCR:
720 	case REG_ESAI_RCR:
721 	case REG_ESAI_RCCR:
722 	case REG_ESAI_TSMA:
723 	case REG_ESAI_TSMB:
724 	case REG_ESAI_RSMA:
725 	case REG_ESAI_RSMB:
726 	case REG_ESAI_PRRC:
727 	case REG_ESAI_PCRC:
728 		return true;
729 	default:
730 		return false;
731 	}
732 }
733 
734 static bool fsl_esai_volatile_reg(struct device *dev, unsigned int reg)
735 {
736 	switch (reg) {
737 	case REG_ESAI_ERDR:
738 	case REG_ESAI_ESR:
739 	case REG_ESAI_TFSR:
740 	case REG_ESAI_RFSR:
741 	case REG_ESAI_RX0:
742 	case REG_ESAI_RX1:
743 	case REG_ESAI_RX2:
744 	case REG_ESAI_RX3:
745 	case REG_ESAI_SAISR:
746 		return true;
747 	default:
748 		return false;
749 	}
750 }
751 
752 static bool fsl_esai_writeable_reg(struct device *dev, unsigned int reg)
753 {
754 	switch (reg) {
755 	case REG_ESAI_ETDR:
756 	case REG_ESAI_ECR:
757 	case REG_ESAI_TFCR:
758 	case REG_ESAI_RFCR:
759 	case REG_ESAI_TX0:
760 	case REG_ESAI_TX1:
761 	case REG_ESAI_TX2:
762 	case REG_ESAI_TX3:
763 	case REG_ESAI_TX4:
764 	case REG_ESAI_TX5:
765 	case REG_ESAI_TSR:
766 	case REG_ESAI_SAICR:
767 	case REG_ESAI_TCR:
768 	case REG_ESAI_TCCR:
769 	case REG_ESAI_RCR:
770 	case REG_ESAI_RCCR:
771 	case REG_ESAI_TSMA:
772 	case REG_ESAI_TSMB:
773 	case REG_ESAI_RSMA:
774 	case REG_ESAI_RSMB:
775 	case REG_ESAI_PRRC:
776 	case REG_ESAI_PCRC:
777 		return true;
778 	default:
779 		return false;
780 	}
781 }
782 
783 static const struct regmap_config fsl_esai_regmap_config = {
784 	.reg_bits = 32,
785 	.reg_stride = 4,
786 	.val_bits = 32,
787 
788 	.max_register = REG_ESAI_PCRC,
789 	.reg_defaults = fsl_esai_reg_defaults,
790 	.num_reg_defaults = ARRAY_SIZE(fsl_esai_reg_defaults),
791 	.readable_reg = fsl_esai_readable_reg,
792 	.volatile_reg = fsl_esai_volatile_reg,
793 	.writeable_reg = fsl_esai_writeable_reg,
794 	.cache_type = REGCACHE_FLAT,
795 };
796 
797 static int fsl_esai_probe(struct platform_device *pdev)
798 {
799 	struct device_node *np = pdev->dev.of_node;
800 	struct fsl_esai *esai_priv;
801 	struct resource *res;
802 	const __be32 *iprop;
803 	void __iomem *regs;
804 	int irq, ret;
805 
806 	esai_priv = devm_kzalloc(&pdev->dev, sizeof(*esai_priv), GFP_KERNEL);
807 	if (!esai_priv)
808 		return -ENOMEM;
809 
810 	esai_priv->pdev = pdev;
811 	snprintf(esai_priv->name, sizeof(esai_priv->name), "%pOFn", np);
812 
813 	/* Get the addresses and IRQ */
814 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
815 	regs = devm_ioremap_resource(&pdev->dev, res);
816 	if (IS_ERR(regs))
817 		return PTR_ERR(regs);
818 
819 	esai_priv->regmap = devm_regmap_init_mmio_clk(&pdev->dev,
820 			"core", regs, &fsl_esai_regmap_config);
821 	if (IS_ERR(esai_priv->regmap)) {
822 		dev_err(&pdev->dev, "failed to init regmap: %ld\n",
823 				PTR_ERR(esai_priv->regmap));
824 		return PTR_ERR(esai_priv->regmap);
825 	}
826 
827 	esai_priv->coreclk = devm_clk_get(&pdev->dev, "core");
828 	if (IS_ERR(esai_priv->coreclk)) {
829 		dev_err(&pdev->dev, "failed to get core clock: %ld\n",
830 				PTR_ERR(esai_priv->coreclk));
831 		return PTR_ERR(esai_priv->coreclk);
832 	}
833 
834 	esai_priv->extalclk = devm_clk_get(&pdev->dev, "extal");
835 	if (IS_ERR(esai_priv->extalclk))
836 		dev_warn(&pdev->dev, "failed to get extal clock: %ld\n",
837 				PTR_ERR(esai_priv->extalclk));
838 
839 	esai_priv->fsysclk = devm_clk_get(&pdev->dev, "fsys");
840 	if (IS_ERR(esai_priv->fsysclk))
841 		dev_warn(&pdev->dev, "failed to get fsys clock: %ld\n",
842 				PTR_ERR(esai_priv->fsysclk));
843 
844 	esai_priv->spbaclk = devm_clk_get(&pdev->dev, "spba");
845 	if (IS_ERR(esai_priv->spbaclk))
846 		dev_warn(&pdev->dev, "failed to get spba clock: %ld\n",
847 				PTR_ERR(esai_priv->spbaclk));
848 
849 	irq = platform_get_irq(pdev, 0);
850 	if (irq < 0) {
851 		dev_err(&pdev->dev, "no irq for node %s\n", pdev->name);
852 		return irq;
853 	}
854 
855 	ret = devm_request_irq(&pdev->dev, irq, esai_isr, 0,
856 			       esai_priv->name, esai_priv);
857 	if (ret) {
858 		dev_err(&pdev->dev, "failed to claim irq %u\n", irq);
859 		return ret;
860 	}
861 
862 	/* Set a default slot number */
863 	esai_priv->slots = 2;
864 
865 	/* Set a default master/slave state */
866 	esai_priv->slave_mode = true;
867 
868 	/* Determine the FIFO depth */
869 	iprop = of_get_property(np, "fsl,fifo-depth", NULL);
870 	if (iprop)
871 		esai_priv->fifo_depth = be32_to_cpup(iprop);
872 	else
873 		esai_priv->fifo_depth = 64;
874 
875 	esai_priv->dma_params_tx.maxburst = 16;
876 	esai_priv->dma_params_rx.maxburst = 16;
877 	esai_priv->dma_params_tx.addr = res->start + REG_ESAI_ETDR;
878 	esai_priv->dma_params_rx.addr = res->start + REG_ESAI_ERDR;
879 
880 	esai_priv->synchronous =
881 		of_property_read_bool(np, "fsl,esai-synchronous");
882 
883 	/* Implement full symmetry for synchronous mode */
884 	if (esai_priv->synchronous) {
885 		fsl_esai_dai.symmetric_rates = 1;
886 		fsl_esai_dai.symmetric_channels = 1;
887 		fsl_esai_dai.symmetric_samplebits = 1;
888 	}
889 
890 	dev_set_drvdata(&pdev->dev, esai_priv);
891 
892 	/* Reset ESAI unit */
893 	ret = regmap_write(esai_priv->regmap, REG_ESAI_ECR, ESAI_ECR_ERST);
894 	if (ret) {
895 		dev_err(&pdev->dev, "failed to reset ESAI: %d\n", ret);
896 		return ret;
897 	}
898 
899 	/*
900 	 * We need to enable ESAI so as to access some of its registers.
901 	 * Otherwise, we would fail to dump regmap from user space.
902 	 */
903 	ret = regmap_write(esai_priv->regmap, REG_ESAI_ECR, ESAI_ECR_ESAIEN);
904 	if (ret) {
905 		dev_err(&pdev->dev, "failed to enable ESAI: %d\n", ret);
906 		return ret;
907 	}
908 
909 	ret = devm_snd_soc_register_component(&pdev->dev, &fsl_esai_component,
910 					      &fsl_esai_dai, 1);
911 	if (ret) {
912 		dev_err(&pdev->dev, "failed to register DAI: %d\n", ret);
913 		return ret;
914 	}
915 
916 	ret = imx_pcm_dma_init(pdev, IMX_ESAI_DMABUF_SIZE);
917 	if (ret)
918 		dev_err(&pdev->dev, "failed to init imx pcm dma: %d\n", ret);
919 
920 	return ret;
921 }
922 
923 static const struct of_device_id fsl_esai_dt_ids[] = {
924 	{ .compatible = "fsl,imx35-esai", },
925 	{ .compatible = "fsl,vf610-esai", },
926 	{}
927 };
928 MODULE_DEVICE_TABLE(of, fsl_esai_dt_ids);
929 
930 #ifdef CONFIG_PM_SLEEP
931 static int fsl_esai_suspend(struct device *dev)
932 {
933 	struct fsl_esai *esai = dev_get_drvdata(dev);
934 
935 	regcache_cache_only(esai->regmap, true);
936 	regcache_mark_dirty(esai->regmap);
937 
938 	return 0;
939 }
940 
941 static int fsl_esai_resume(struct device *dev)
942 {
943 	struct fsl_esai *esai = dev_get_drvdata(dev);
944 	int ret;
945 
946 	regcache_cache_only(esai->regmap, false);
947 
948 	/* FIFO reset for safety */
949 	regmap_update_bits(esai->regmap, REG_ESAI_TFCR,
950 			   ESAI_xFCR_xFR, ESAI_xFCR_xFR);
951 	regmap_update_bits(esai->regmap, REG_ESAI_RFCR,
952 			   ESAI_xFCR_xFR, ESAI_xFCR_xFR);
953 
954 	ret = regcache_sync(esai->regmap);
955 	if (ret)
956 		return ret;
957 
958 	/* FIFO reset done */
959 	regmap_update_bits(esai->regmap, REG_ESAI_TFCR, ESAI_xFCR_xFR, 0);
960 	regmap_update_bits(esai->regmap, REG_ESAI_RFCR, ESAI_xFCR_xFR, 0);
961 
962 	return 0;
963 }
964 #endif /* CONFIG_PM_SLEEP */
965 
966 static const struct dev_pm_ops fsl_esai_pm_ops = {
967 	SET_SYSTEM_SLEEP_PM_OPS(fsl_esai_suspend, fsl_esai_resume)
968 };
969 
970 static struct platform_driver fsl_esai_driver = {
971 	.probe = fsl_esai_probe,
972 	.driver = {
973 		.name = "fsl-esai-dai",
974 		.pm = &fsl_esai_pm_ops,
975 		.of_match_table = fsl_esai_dt_ids,
976 	},
977 };
978 
979 module_platform_driver(fsl_esai_driver);
980 
981 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
982 MODULE_DESCRIPTION("Freescale ESAI CPU DAI driver");
983 MODULE_LICENSE("GPL v2");
984 MODULE_ALIAS("platform:fsl-esai-dai");
985