xref: /linux/sound/soc/fsl/fsl_sai.c (revision ebc733e54a1a79ea2dde2ba5121ae73a188e20d4)
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // Freescale ALSA SoC Digital Audio Interface (SAI) driver.
4 //
5 // Copyright 2012-2015 Freescale Semiconductor, Inc.
6 
7 #include <linux/clk.h>
8 #include <linux/delay.h>
9 #include <linux/dmaengine.h>
10 #include <linux/module.h>
11 #include <linux/of.h>
12 #include <linux/pinctrl/consumer.h>
13 #include <linux/pm_qos.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/regmap.h>
16 #include <linux/slab.h>
17 #include <linux/time.h>
18 #include <sound/core.h>
19 #include <sound/dmaengine_pcm.h>
20 #include <sound/pcm_params.h>
21 #include <linux/mfd/syscon.h>
22 #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
23 
24 #include "fsl_sai.h"
25 #include "fsl_utils.h"
26 #include "imx-pcm.h"
27 
28 #define FSL_SAI_FLAGS (FSL_SAI_CSR_SEIE |\
29 		       FSL_SAI_CSR_FEIE)
30 
31 static const unsigned int fsl_sai_rates[] = {
32 	8000, 11025, 12000, 16000, 22050,
33 	24000, 32000, 44100, 48000, 64000,
34 	88200, 96000, 176400, 192000, 352800,
35 	384000, 705600, 768000, 1411200, 2822400,
36 };
37 
38 static const struct snd_pcm_hw_constraint_list fsl_sai_rate_constraints = {
39 	.count = ARRAY_SIZE(fsl_sai_rates),
40 	.list = fsl_sai_rates,
41 };
42 
43 /**
44  * fsl_sai_dir_is_synced - Check if stream is synced by the opposite stream
45  *
46  * SAI supports synchronous mode using bit/frame clocks of either Transmitter's
47  * or Receiver's for both streams. This function is used to check if clocks of
48  * the stream's are synced by the opposite stream.
49  *
50  * @sai: SAI context
51  * @dir: stream direction
52  */
53 static inline bool fsl_sai_dir_is_synced(struct fsl_sai *sai, int dir)
54 {
55 	int adir = (dir == TX) ? RX : TX;
56 
57 	/* current dir in async mode while opposite dir in sync mode */
58 	return !sai->synchronous[dir] && sai->synchronous[adir];
59 }
60 
61 static struct pinctrl_state *fsl_sai_get_pins_state(struct fsl_sai *sai, u32 bclk)
62 {
63 	struct pinctrl_state *state = NULL;
64 
65 	if (sai->is_pdm_mode) {
66 		/* DSD512@44.1kHz, DSD512@48kHz */
67 		if (bclk >= 22579200)
68 			state = pinctrl_lookup_state(sai->pinctrl, "dsd512");
69 
70 		/* Get default DSD state */
71 		if (IS_ERR_OR_NULL(state))
72 			state = pinctrl_lookup_state(sai->pinctrl, "dsd");
73 	} else {
74 		/* 706k32b2c, 768k32b2c, etc */
75 		if (bclk >= 45158400)
76 			state = pinctrl_lookup_state(sai->pinctrl, "pcm_b2m");
77 	}
78 
79 	/* Get default state */
80 	if (IS_ERR_OR_NULL(state))
81 		state = pinctrl_lookup_state(sai->pinctrl, "default");
82 
83 	return state;
84 }
85 
86 static irqreturn_t fsl_sai_isr(int irq, void *devid)
87 {
88 	struct fsl_sai *sai = (struct fsl_sai *)devid;
89 	unsigned int ofs = sai->soc_data->reg_offset;
90 	struct device *dev = &sai->pdev->dev;
91 	u32 flags, xcsr, mask;
92 	irqreturn_t iret = IRQ_NONE;
93 
94 	/*
95 	 * Both IRQ status bits and IRQ mask bits are in the xCSR but
96 	 * different shifts. And we here create a mask only for those
97 	 * IRQs that we activated.
98 	 */
99 	mask = (FSL_SAI_FLAGS >> FSL_SAI_CSR_xIE_SHIFT) << FSL_SAI_CSR_xF_SHIFT;
100 
101 	/* Tx IRQ */
102 	regmap_read(sai->regmap, FSL_SAI_TCSR(ofs), &xcsr);
103 	flags = xcsr & mask;
104 
105 	if (flags)
106 		iret = IRQ_HANDLED;
107 	else
108 		goto irq_rx;
109 
110 	if (flags & FSL_SAI_CSR_WSF)
111 		dev_dbg(dev, "isr: Start of Tx word detected\n");
112 
113 	if (flags & FSL_SAI_CSR_SEF)
114 		dev_dbg(dev, "isr: Tx Frame sync error detected\n");
115 
116 	if (flags & FSL_SAI_CSR_FEF)
117 		dev_dbg(dev, "isr: Transmit underrun detected\n");
118 
119 	if (flags & FSL_SAI_CSR_FWF)
120 		dev_dbg(dev, "isr: Enabled transmit FIFO is empty\n");
121 
122 	if (flags & FSL_SAI_CSR_FRF)
123 		dev_dbg(dev, "isr: Transmit FIFO watermark has been reached\n");
124 
125 	flags &= FSL_SAI_CSR_xF_W_MASK;
126 	xcsr &= ~FSL_SAI_CSR_xF_MASK;
127 
128 	if (flags)
129 		regmap_write(sai->regmap, FSL_SAI_TCSR(ofs), flags | xcsr);
130 
131 irq_rx:
132 	/* Rx IRQ */
133 	regmap_read(sai->regmap, FSL_SAI_RCSR(ofs), &xcsr);
134 	flags = xcsr & mask;
135 
136 	if (flags)
137 		iret = IRQ_HANDLED;
138 	else
139 		goto out;
140 
141 	if (flags & FSL_SAI_CSR_WSF)
142 		dev_dbg(dev, "isr: Start of Rx word detected\n");
143 
144 	if (flags & FSL_SAI_CSR_SEF)
145 		dev_dbg(dev, "isr: Rx Frame sync error detected\n");
146 
147 	if (flags & FSL_SAI_CSR_FEF)
148 		dev_dbg(dev, "isr: Receive overflow detected\n");
149 
150 	if (flags & FSL_SAI_CSR_FWF)
151 		dev_dbg(dev, "isr: Enabled receive FIFO is full\n");
152 
153 	if (flags & FSL_SAI_CSR_FRF)
154 		dev_dbg(dev, "isr: Receive FIFO watermark has been reached\n");
155 
156 	flags &= FSL_SAI_CSR_xF_W_MASK;
157 	xcsr &= ~FSL_SAI_CSR_xF_MASK;
158 
159 	if (flags)
160 		regmap_write(sai->regmap, FSL_SAI_RCSR(ofs), flags | xcsr);
161 
162 out:
163 	return iret;
164 }
165 
166 static int fsl_sai_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai, u32 tx_mask,
167 				u32 rx_mask, int slots, int slot_width)
168 {
169 	struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
170 
171 	sai->slots = slots;
172 	sai->slot_width = slot_width;
173 
174 	return 0;
175 }
176 
177 static int fsl_sai_set_dai_bclk_ratio(struct snd_soc_dai *dai,
178 				      unsigned int ratio)
179 {
180 	struct fsl_sai *sai = snd_soc_dai_get_drvdata(dai);
181 
182 	sai->bclk_ratio = ratio;
183 
184 	return 0;
185 }
186 
187 static int fsl_sai_set_dai_sysclk_tr(struct snd_soc_dai *cpu_dai,
188 		int clk_id, unsigned int freq, bool tx)
189 {
190 	struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
191 	unsigned int ofs = sai->soc_data->reg_offset;
192 	u32 val_cr2 = 0;
193 
194 	switch (clk_id) {
195 	case FSL_SAI_CLK_BUS:
196 		val_cr2 |= FSL_SAI_CR2_MSEL_BUS;
197 		break;
198 	case FSL_SAI_CLK_MAST1:
199 		val_cr2 |= FSL_SAI_CR2_MSEL_MCLK1;
200 		break;
201 	case FSL_SAI_CLK_MAST2:
202 		val_cr2 |= FSL_SAI_CR2_MSEL_MCLK2;
203 		break;
204 	case FSL_SAI_CLK_MAST3:
205 		val_cr2 |= FSL_SAI_CR2_MSEL_MCLK3;
206 		break;
207 	default:
208 		return -EINVAL;
209 	}
210 
211 	regmap_update_bits(sai->regmap, FSL_SAI_xCR2(tx, ofs),
212 			   FSL_SAI_CR2_MSEL_MASK, val_cr2);
213 
214 	return 0;
215 }
216 
217 static int fsl_sai_set_mclk_rate(struct snd_soc_dai *dai, int clk_id, unsigned int freq)
218 {
219 	struct fsl_sai *sai = snd_soc_dai_get_drvdata(dai);
220 	int ret;
221 
222 	fsl_asoc_reparent_pll_clocks(dai->dev, sai->mclk_clk[clk_id],
223 				     sai->pll8k_clk, sai->pll11k_clk, freq);
224 
225 	ret = clk_set_rate(sai->mclk_clk[clk_id], freq);
226 	if (ret < 0)
227 		dev_err(dai->dev, "failed to set clock rate (%u): %d\n", freq, ret);
228 
229 	return ret;
230 }
231 
232 static int fsl_sai_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
233 		int clk_id, unsigned int freq, int dir)
234 {
235 	struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
236 	int ret;
237 
238 	if (dir == SND_SOC_CLOCK_IN)
239 		return 0;
240 
241 	if (freq > 0 && clk_id != FSL_SAI_CLK_BUS) {
242 		if (clk_id < 0 || clk_id >= FSL_SAI_MCLK_MAX) {
243 			dev_err(cpu_dai->dev, "Unknown clock id: %d\n", clk_id);
244 			return -EINVAL;
245 		}
246 
247 		if (IS_ERR_OR_NULL(sai->mclk_clk[clk_id])) {
248 			dev_err(cpu_dai->dev, "Unassigned clock: %d\n", clk_id);
249 			return -EINVAL;
250 		}
251 
252 		if (sai->mclk_streams == 0) {
253 			ret = fsl_sai_set_mclk_rate(cpu_dai, clk_id, freq);
254 			if (ret < 0)
255 				return ret;
256 		}
257 	}
258 
259 	ret = fsl_sai_set_dai_sysclk_tr(cpu_dai, clk_id, freq, true);
260 	if (ret) {
261 		dev_err(cpu_dai->dev, "Cannot set tx sysclk: %d\n", ret);
262 		return ret;
263 	}
264 
265 	ret = fsl_sai_set_dai_sysclk_tr(cpu_dai, clk_id, freq, false);
266 	if (ret)
267 		dev_err(cpu_dai->dev, "Cannot set rx sysclk: %d\n", ret);
268 
269 	return ret;
270 }
271 
272 static int fsl_sai_set_dai_fmt_tr(struct snd_soc_dai *cpu_dai,
273 				unsigned int fmt, bool tx)
274 {
275 	struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
276 	unsigned int ofs = sai->soc_data->reg_offset;
277 	u32 val_cr2 = 0, val_cr4 = 0;
278 
279 	if (!sai->is_lsb_first)
280 		val_cr4 |= FSL_SAI_CR4_MF;
281 
282 	sai->is_pdm_mode = false;
283 	sai->is_dsp_mode = false;
284 	/* DAI mode */
285 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
286 	case SND_SOC_DAIFMT_I2S:
287 		/*
288 		 * Frame low, 1clk before data, one word length for frame sync,
289 		 * frame sync starts one serial clock cycle earlier,
290 		 * that is, together with the last bit of the previous
291 		 * data word.
292 		 */
293 		val_cr2 |= FSL_SAI_CR2_BCP;
294 		val_cr4 |= FSL_SAI_CR4_FSE | FSL_SAI_CR4_FSP;
295 		break;
296 	case SND_SOC_DAIFMT_LEFT_J:
297 		/*
298 		 * Frame high, one word length for frame sync,
299 		 * frame sync asserts with the first bit of the frame.
300 		 */
301 		val_cr2 |= FSL_SAI_CR2_BCP;
302 		break;
303 	case SND_SOC_DAIFMT_DSP_A:
304 		/*
305 		 * Frame high, 1clk before data, one bit for frame sync,
306 		 * frame sync starts one serial clock cycle earlier,
307 		 * that is, together with the last bit of the previous
308 		 * data word.
309 		 */
310 		val_cr2 |= FSL_SAI_CR2_BCP;
311 		val_cr4 |= FSL_SAI_CR4_FSE;
312 		sai->is_dsp_mode = true;
313 		break;
314 	case SND_SOC_DAIFMT_DSP_B:
315 		/*
316 		 * Frame high, one bit for frame sync,
317 		 * frame sync asserts with the first bit of the frame.
318 		 */
319 		val_cr2 |= FSL_SAI_CR2_BCP;
320 		sai->is_dsp_mode = true;
321 		break;
322 	case SND_SOC_DAIFMT_PDM:
323 		val_cr2 |= FSL_SAI_CR2_BCP;
324 		val_cr4 &= ~FSL_SAI_CR4_MF;
325 		sai->is_pdm_mode = true;
326 		break;
327 	case SND_SOC_DAIFMT_RIGHT_J:
328 		/* To be done */
329 	default:
330 		return -EINVAL;
331 	}
332 
333 	/* DAI clock inversion */
334 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
335 	case SND_SOC_DAIFMT_IB_IF:
336 		/* Invert both clocks */
337 		val_cr2 ^= FSL_SAI_CR2_BCP;
338 		val_cr4 ^= FSL_SAI_CR4_FSP;
339 		break;
340 	case SND_SOC_DAIFMT_IB_NF:
341 		/* Invert bit clock */
342 		val_cr2 ^= FSL_SAI_CR2_BCP;
343 		break;
344 	case SND_SOC_DAIFMT_NB_IF:
345 		/* Invert frame clock */
346 		val_cr4 ^= FSL_SAI_CR4_FSP;
347 		break;
348 	case SND_SOC_DAIFMT_NB_NF:
349 		/* Nothing to do for both normal cases */
350 		break;
351 	default:
352 		return -EINVAL;
353 	}
354 
355 	/* DAI clock provider masks */
356 	switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
357 	case SND_SOC_DAIFMT_BP_FP:
358 		val_cr2 |= FSL_SAI_CR2_BCD_MSTR;
359 		val_cr4 |= FSL_SAI_CR4_FSD_MSTR;
360 		sai->is_consumer_mode = false;
361 		break;
362 	case SND_SOC_DAIFMT_BC_FC:
363 		sai->is_consumer_mode = true;
364 		break;
365 	case SND_SOC_DAIFMT_BP_FC:
366 		val_cr2 |= FSL_SAI_CR2_BCD_MSTR;
367 		sai->is_consumer_mode = false;
368 		break;
369 	case SND_SOC_DAIFMT_BC_FP:
370 		val_cr4 |= FSL_SAI_CR4_FSD_MSTR;
371 		sai->is_consumer_mode = true;
372 		break;
373 	default:
374 		return -EINVAL;
375 	}
376 
377 	regmap_update_bits(sai->regmap, FSL_SAI_xCR2(tx, ofs),
378 			   FSL_SAI_CR2_BCP | FSL_SAI_CR2_BCD_MSTR, val_cr2);
379 	regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx, ofs),
380 			   FSL_SAI_CR4_MF | FSL_SAI_CR4_FSE |
381 			   FSL_SAI_CR4_FSP | FSL_SAI_CR4_FSD_MSTR, val_cr4);
382 
383 	return 0;
384 }
385 
386 static int fsl_sai_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
387 {
388 	int ret;
389 
390 	ret = fsl_sai_set_dai_fmt_tr(cpu_dai, fmt, true);
391 	if (ret) {
392 		dev_err(cpu_dai->dev, "Cannot set tx format: %d\n", ret);
393 		return ret;
394 	}
395 
396 	ret = fsl_sai_set_dai_fmt_tr(cpu_dai, fmt, false);
397 	if (ret)
398 		dev_err(cpu_dai->dev, "Cannot set rx format: %d\n", ret);
399 
400 	return ret;
401 }
402 
403 static int fsl_sai_set_bclk(struct snd_soc_dai *dai, bool tx, u32 freq)
404 {
405 	struct fsl_sai *sai = snd_soc_dai_get_drvdata(dai);
406 	unsigned int reg, ofs = sai->soc_data->reg_offset;
407 	unsigned long clk_rate;
408 	u32 savediv = 0, ratio, bestdiff = freq;
409 	int adir = tx ? RX : TX;
410 	int dir = tx ? TX : RX;
411 	u32 id;
412 	bool support_1_1_ratio = sai->verid.version >= 0x0301;
413 
414 	/* Don't apply to consumer mode */
415 	if (sai->is_consumer_mode)
416 		return 0;
417 
418 	/*
419 	 * There is no point in polling MCLK0 if it is identical to MCLK1.
420 	 * And given that MQS use case has to use MCLK1 though two clocks
421 	 * are the same, we simply skip MCLK0 and start to find from MCLK1.
422 	 */
423 	id = sai->soc_data->mclk0_is_mclk1 ? 1 : 0;
424 
425 	for (; id < FSL_SAI_MCLK_MAX; id++) {
426 		int diff;
427 
428 		clk_rate = clk_get_rate(sai->mclk_clk[id]);
429 		if (!clk_rate)
430 			continue;
431 
432 		ratio = DIV_ROUND_CLOSEST(clk_rate, freq);
433 		if (!ratio || ratio > 512)
434 			continue;
435 		if (ratio == 1 && !support_1_1_ratio)
436 			continue;
437 		if ((ratio & 1) && ratio > 1)
438 			continue;
439 
440 		diff = abs((long)clk_rate - ratio * freq);
441 
442 		/*
443 		 * Drop the source that can not be
444 		 * divided into the required rate.
445 		 */
446 		if (diff != 0 && clk_rate / diff < 1000)
447 			continue;
448 
449 		dev_dbg(dai->dev,
450 			"ratio %d for freq %dHz based on clock %ldHz\n",
451 			ratio, freq, clk_rate);
452 
453 
454 		if (diff < bestdiff) {
455 			savediv = ratio;
456 			sai->mclk_id[tx] = id;
457 			bestdiff = diff;
458 		}
459 
460 		if (diff == 0)
461 			break;
462 	}
463 
464 	if (savediv == 0) {
465 		dev_err(dai->dev, "failed to derive required %cx rate: %d\n",
466 				tx ? 'T' : 'R', freq);
467 		return -EINVAL;
468 	}
469 
470 	dev_dbg(dai->dev, "best fit: clock id=%d, div=%d, deviation =%d\n",
471 			sai->mclk_id[tx], savediv, bestdiff);
472 
473 	/*
474 	 * 1) For Asynchronous mode, we must set RCR2 register for capture, and
475 	 *    set TCR2 register for playback.
476 	 * 2) For Tx sync with Rx clock, we must set RCR2 register for playback
477 	 *    and capture.
478 	 * 3) For Rx sync with Tx clock, we must set TCR2 register for playback
479 	 *    and capture.
480 	 * 4) For Tx and Rx are both Synchronous with another SAI, we just
481 	 *    ignore it.
482 	 */
483 	if (fsl_sai_dir_is_synced(sai, adir))
484 		reg = FSL_SAI_xCR2(!tx, ofs);
485 	else if (!sai->synchronous[dir])
486 		reg = FSL_SAI_xCR2(tx, ofs);
487 	else
488 		return 0;
489 
490 	regmap_update_bits(sai->regmap, reg, FSL_SAI_CR2_MSEL_MASK,
491 			   FSL_SAI_CR2_MSEL(sai->mclk_id[tx]));
492 
493 	if (savediv == 1) {
494 		regmap_update_bits(sai->regmap, reg,
495 				   FSL_SAI_CR2_DIV_MASK | FSL_SAI_CR2_BYP,
496 				   FSL_SAI_CR2_BYP);
497 		if (fsl_sai_dir_is_synced(sai, adir))
498 			regmap_update_bits(sai->regmap, FSL_SAI_xCR2(tx, ofs),
499 					   FSL_SAI_CR2_BCI, FSL_SAI_CR2_BCI);
500 		else
501 			regmap_update_bits(sai->regmap, FSL_SAI_xCR2(tx, ofs),
502 					   FSL_SAI_CR2_BCI, 0);
503 	} else {
504 		regmap_update_bits(sai->regmap, reg,
505 				   FSL_SAI_CR2_DIV_MASK | FSL_SAI_CR2_BYP,
506 				   savediv / 2 - 1);
507 	}
508 
509 	return 0;
510 }
511 
512 static int fsl_sai_hw_params(struct snd_pcm_substream *substream,
513 		struct snd_pcm_hw_params *params,
514 		struct snd_soc_dai *cpu_dai)
515 {
516 	struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
517 	unsigned int ofs = sai->soc_data->reg_offset;
518 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
519 	unsigned int channels = params_channels(params);
520 	struct snd_dmaengine_dai_dma_data *dma_params;
521 	struct fsl_sai_dl_cfg *dl_cfg = sai->dl_cfg;
522 	u32 word_width = params_width(params);
523 	int trce_mask = 0, dl_cfg_idx = 0;
524 	int dl_cfg_cnt = sai->dl_cfg_cnt;
525 	u32 dl_type = FSL_SAI_DL_I2S;
526 	u32 val_cr4 = 0, val_cr5 = 0;
527 	u32 slots = (channels == 1) ? 2 : channels;
528 	u32 slot_width = word_width;
529 	int adir = tx ? RX : TX;
530 	u32 pins, bclk;
531 	u32 watermark;
532 	int ret, i;
533 
534 	if (sai->slot_width)
535 		slot_width = sai->slot_width;
536 
537 	if (sai->slots)
538 		slots = sai->slots;
539 	else if (sai->bclk_ratio)
540 		slots = sai->bclk_ratio / slot_width;
541 
542 	pins = DIV_ROUND_UP(channels, slots);
543 
544 	/*
545 	 * PDM mode, channels are independent
546 	 * each channels are on one dataline/FIFO.
547 	 */
548 	if (sai->is_pdm_mode) {
549 		pins = channels;
550 		dl_type = FSL_SAI_DL_PDM;
551 	}
552 
553 	for (i = 0; i < dl_cfg_cnt; i++) {
554 		if (dl_cfg[i].type == dl_type && dl_cfg[i].pins[tx] == pins) {
555 			dl_cfg_idx = i;
556 			break;
557 		}
558 	}
559 
560 	if (hweight8(dl_cfg[dl_cfg_idx].mask[tx]) < pins) {
561 		dev_err(cpu_dai->dev, "channel not supported\n");
562 		return -EINVAL;
563 	}
564 
565 	bclk = params_rate(params) * (sai->bclk_ratio ? sai->bclk_ratio : slots * slot_width);
566 
567 	if (!IS_ERR_OR_NULL(sai->pinctrl)) {
568 		sai->pins_state = fsl_sai_get_pins_state(sai, bclk);
569 		if (!IS_ERR_OR_NULL(sai->pins_state)) {
570 			ret = pinctrl_select_state(sai->pinctrl, sai->pins_state);
571 			if (ret) {
572 				dev_err(cpu_dai->dev, "failed to set proper pins state: %d\n", ret);
573 				return ret;
574 			}
575 		}
576 	}
577 
578 	if (!sai->is_consumer_mode) {
579 		ret = fsl_sai_set_bclk(cpu_dai, tx, bclk);
580 		if (ret)
581 			return ret;
582 
583 		/* Do not enable the clock if it is already enabled */
584 		if (!(sai->mclk_streams & BIT(substream->stream))) {
585 			ret = clk_prepare_enable(sai->mclk_clk[sai->mclk_id[tx]]);
586 			if (ret)
587 				return ret;
588 
589 			sai->mclk_streams |= BIT(substream->stream);
590 		}
591 	}
592 
593 	if (!sai->is_dsp_mode && !sai->is_pdm_mode)
594 		val_cr4 |= FSL_SAI_CR4_SYWD(slot_width);
595 
596 	val_cr5 |= FSL_SAI_CR5_WNW(slot_width);
597 	val_cr5 |= FSL_SAI_CR5_W0W(slot_width);
598 
599 	if (sai->is_lsb_first || sai->is_pdm_mode)
600 		val_cr5 |= FSL_SAI_CR5_FBT(0);
601 	else
602 		val_cr5 |= FSL_SAI_CR5_FBT(word_width - 1);
603 
604 	val_cr4 |= FSL_SAI_CR4_FRSZ(slots);
605 
606 	/* Set to output mode to avoid tri-stated data pins */
607 	if (tx)
608 		val_cr4 |= FSL_SAI_CR4_CHMOD;
609 
610 	/*
611 	 * For SAI provider mode, when Tx(Rx) sync with Rx(Tx) clock, Rx(Tx) will
612 	 * generate bclk and frame clock for Tx(Rx), we should set RCR4(TCR4),
613 	 * RCR5(TCR5) for playback(capture), or there will be sync error.
614 	 */
615 
616 	if (!sai->is_consumer_mode && fsl_sai_dir_is_synced(sai, adir)) {
617 		regmap_update_bits(sai->regmap, FSL_SAI_xCR4(!tx, ofs),
618 				   FSL_SAI_CR4_SYWD_MASK | FSL_SAI_CR4_FRSZ_MASK |
619 				   FSL_SAI_CR4_CHMOD_MASK,
620 				   val_cr4);
621 		regmap_update_bits(sai->regmap, FSL_SAI_xCR5(!tx, ofs),
622 				   FSL_SAI_CR5_WNW_MASK | FSL_SAI_CR5_W0W_MASK |
623 				   FSL_SAI_CR5_FBT_MASK, val_cr5);
624 	}
625 
626 	/*
627 	 * Combine mode has limation:
628 	 * - Can't used for singel dataline/FIFO case except the FIFO0
629 	 * - Can't used for multi dataline/FIFO case except the enabled FIFOs
630 	 *   are successive and start from FIFO0
631 	 *
632 	 * So for common usage, all multi fifo case disable the combine mode.
633 	 */
634 	if (hweight8(dl_cfg[dl_cfg_idx].mask[tx]) <= 1 || sai->is_multi_fifo_dma)
635 		regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx, ofs),
636 				   FSL_SAI_CR4_FCOMB_MASK, 0);
637 	else
638 		regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx, ofs),
639 				   FSL_SAI_CR4_FCOMB_MASK, FSL_SAI_CR4_FCOMB_SOFT);
640 
641 	dma_params = tx ? &sai->dma_params_tx : &sai->dma_params_rx;
642 	dma_params->addr = sai->res->start + FSL_SAI_xDR0(tx) +
643 			   dl_cfg[dl_cfg_idx].start_off[tx] * 0x4;
644 
645 	if (sai->is_multi_fifo_dma) {
646 		sai->audio_config[tx].words_per_fifo = min(slots, channels);
647 		if (tx) {
648 			sai->audio_config[tx].n_fifos_dst = pins;
649 			sai->audio_config[tx].stride_fifos_dst = dl_cfg[dl_cfg_idx].next_off[tx];
650 		} else {
651 			sai->audio_config[tx].n_fifos_src = pins;
652 			sai->audio_config[tx].stride_fifos_src = dl_cfg[dl_cfg_idx].next_off[tx];
653 		}
654 		dma_params->maxburst = sai->audio_config[tx].words_per_fifo * pins;
655 		dma_params->peripheral_config = &sai->audio_config[tx];
656 		dma_params->peripheral_size = sizeof(sai->audio_config[tx]);
657 
658 		watermark = tx ? (sai->soc_data->fifo_depth - dma_params->maxburst) :
659 				 (dma_params->maxburst - 1);
660 		regmap_update_bits(sai->regmap, FSL_SAI_xCR1(tx, ofs),
661 				   FSL_SAI_CR1_RFW_MASK(sai->soc_data->fifo_depth),
662 				   watermark);
663 	}
664 
665 	/* Find a proper tcre setting */
666 	for (i = 0; i < sai->soc_data->pins; i++) {
667 		trce_mask = (1 << (i + 1)) - 1;
668 		if (hweight8(dl_cfg[dl_cfg_idx].mask[tx] & trce_mask) == pins)
669 			break;
670 	}
671 
672 	regmap_update_bits(sai->regmap, FSL_SAI_xCR3(tx, ofs),
673 			   FSL_SAI_CR3_TRCE_MASK,
674 			   FSL_SAI_CR3_TRCE((dl_cfg[dl_cfg_idx].mask[tx] & trce_mask)));
675 
676 	regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx, ofs),
677 			   FSL_SAI_CR4_SYWD_MASK | FSL_SAI_CR4_FRSZ_MASK |
678 			   FSL_SAI_CR4_CHMOD_MASK,
679 			   val_cr4);
680 	regmap_update_bits(sai->regmap, FSL_SAI_xCR5(tx, ofs),
681 			   FSL_SAI_CR5_WNW_MASK | FSL_SAI_CR5_W0W_MASK |
682 			   FSL_SAI_CR5_FBT_MASK, val_cr5);
683 	regmap_write(sai->regmap, FSL_SAI_xMR(tx),
684 		     ~0UL - ((1 << min(channels, slots)) - 1));
685 
686 	return 0;
687 }
688 
689 static int fsl_sai_hw_free(struct snd_pcm_substream *substream,
690 		struct snd_soc_dai *cpu_dai)
691 {
692 	struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
693 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
694 	unsigned int ofs = sai->soc_data->reg_offset;
695 
696 	regmap_update_bits(sai->regmap, FSL_SAI_xCR3(tx, ofs),
697 			   FSL_SAI_CR3_TRCE_MASK, 0);
698 
699 	if (!sai->is_consumer_mode &&
700 			sai->mclk_streams & BIT(substream->stream)) {
701 		clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[tx]]);
702 		sai->mclk_streams &= ~BIT(substream->stream);
703 	}
704 
705 	return 0;
706 }
707 
708 static void fsl_sai_config_disable(struct fsl_sai *sai, int dir)
709 {
710 	unsigned int ofs = sai->soc_data->reg_offset;
711 	bool tx = dir == TX;
712 	u32 xcsr, count = 100, mask;
713 
714 	if (sai->soc_data->mclk_with_tere && sai->mclk_direction_output)
715 		mask = FSL_SAI_CSR_TERE;
716 	else
717 		mask = FSL_SAI_CSR_TERE | FSL_SAI_CSR_BCE;
718 
719 	regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
720 			   mask, 0);
721 
722 	/* TERE will remain set till the end of current frame */
723 	do {
724 		udelay(10);
725 		regmap_read(sai->regmap, FSL_SAI_xCSR(tx, ofs), &xcsr);
726 	} while (--count && xcsr & FSL_SAI_CSR_TERE);
727 
728 	regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
729 			   FSL_SAI_CSR_FR, FSL_SAI_CSR_FR);
730 
731 	/*
732 	 * For sai master mode, after several open/close sai,
733 	 * there will be no frame clock, and can't recover
734 	 * anymore. Add software reset to fix this issue.
735 	 * This is a hardware bug, and will be fix in the
736 	 * next sai version.
737 	 */
738 	if (!sai->is_consumer_mode) {
739 		/* Software Reset */
740 		regmap_write(sai->regmap, FSL_SAI_xCSR(tx, ofs), FSL_SAI_CSR_SR);
741 		/* Clear SR bit to finish the reset */
742 		regmap_write(sai->regmap, FSL_SAI_xCSR(tx, ofs), 0);
743 	}
744 }
745 
746 static int fsl_sai_trigger(struct snd_pcm_substream *substream, int cmd,
747 		struct snd_soc_dai *cpu_dai)
748 {
749 	struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
750 	unsigned int ofs = sai->soc_data->reg_offset;
751 
752 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
753 	int adir = tx ? RX : TX;
754 	int dir = tx ? TX : RX;
755 	u32 xcsr;
756 
757 	/*
758 	 * Asynchronous mode: Clear SYNC for both Tx and Rx.
759 	 * Rx sync with Tx clocks: Clear SYNC for Tx, set it for Rx.
760 	 * Tx sync with Rx clocks: Clear SYNC for Rx, set it for Tx.
761 	 */
762 	regmap_update_bits(sai->regmap, FSL_SAI_TCR2(ofs), FSL_SAI_CR2_SYNC,
763 			   sai->synchronous[TX] ? FSL_SAI_CR2_SYNC : 0);
764 	regmap_update_bits(sai->regmap, FSL_SAI_RCR2(ofs), FSL_SAI_CR2_SYNC,
765 			   sai->synchronous[RX] ? FSL_SAI_CR2_SYNC : 0);
766 
767 	/*
768 	 * It is recommended that the transmitter is the last enabled
769 	 * and the first disabled.
770 	 */
771 	switch (cmd) {
772 	case SNDRV_PCM_TRIGGER_START:
773 	case SNDRV_PCM_TRIGGER_RESUME:
774 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
775 		regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
776 				   FSL_SAI_CSR_FRDE, FSL_SAI_CSR_FRDE);
777 
778 		regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
779 				   FSL_SAI_CSR_TERE, FSL_SAI_CSR_TERE);
780 		/*
781 		 * Enable the opposite direction for synchronous mode
782 		 * 1. Tx sync with Rx: only set RE for Rx; set TE & RE for Tx
783 		 * 2. Rx sync with Tx: only set TE for Tx; set RE & TE for Rx
784 		 *
785 		 * RM recommends to enable RE after TE for case 1 and to enable
786 		 * TE after RE for case 2, but we here may not always guarantee
787 		 * that happens: "arecord 1.wav; aplay 2.wav" in case 1 enables
788 		 * TE after RE, which is against what RM recommends but should
789 		 * be safe to do, judging by years of testing results.
790 		 */
791 		if (fsl_sai_dir_is_synced(sai, adir))
792 			regmap_update_bits(sai->regmap, FSL_SAI_xCSR((!tx), ofs),
793 					   FSL_SAI_CSR_TERE, FSL_SAI_CSR_TERE);
794 
795 		regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
796 				   FSL_SAI_CSR_xIE_MASK, FSL_SAI_FLAGS);
797 		break;
798 	case SNDRV_PCM_TRIGGER_STOP:
799 	case SNDRV_PCM_TRIGGER_SUSPEND:
800 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
801 		regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
802 				   FSL_SAI_CSR_FRDE, 0);
803 		regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
804 				   FSL_SAI_CSR_xIE_MASK, 0);
805 
806 		/* Check if the opposite FRDE is also disabled */
807 		regmap_read(sai->regmap, FSL_SAI_xCSR(!tx, ofs), &xcsr);
808 
809 		/*
810 		 * If opposite stream provides clocks for synchronous mode and
811 		 * it is inactive, disable it before disabling the current one
812 		 */
813 		if (fsl_sai_dir_is_synced(sai, adir) && !(xcsr & FSL_SAI_CSR_FRDE))
814 			fsl_sai_config_disable(sai, adir);
815 
816 		/*
817 		 * Disable current stream if either of:
818 		 * 1. current stream doesn't provide clocks for synchronous mode
819 		 * 2. current stream provides clocks for synchronous mode but no
820 		 *    more stream is active.
821 		 */
822 		if (!fsl_sai_dir_is_synced(sai, dir) || !(xcsr & FSL_SAI_CSR_FRDE))
823 			fsl_sai_config_disable(sai, dir);
824 
825 		break;
826 	default:
827 		return -EINVAL;
828 	}
829 
830 	return 0;
831 }
832 
833 static int fsl_sai_startup(struct snd_pcm_substream *substream,
834 		struct snd_soc_dai *cpu_dai)
835 {
836 	struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
837 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
838 	int ret;
839 
840 	/*
841 	 * EDMA controller needs period size to be a multiple of
842 	 * tx/rx maxburst
843 	 */
844 	if (sai->soc_data->use_edma)
845 		snd_pcm_hw_constraint_step(substream->runtime, 0,
846 					   SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
847 					   tx ? sai->dma_params_tx.maxburst :
848 					   sai->dma_params_rx.maxburst);
849 
850 	ret = snd_pcm_hw_constraint_list(substream->runtime, 0,
851 			SNDRV_PCM_HW_PARAM_RATE, &fsl_sai_rate_constraints);
852 
853 	return ret;
854 }
855 
856 static int fsl_sai_dai_probe(struct snd_soc_dai *cpu_dai)
857 {
858 	struct fsl_sai *sai = dev_get_drvdata(cpu_dai->dev);
859 	unsigned int ofs = sai->soc_data->reg_offset;
860 
861 	/* Software Reset for both Tx and Rx */
862 	regmap_write(sai->regmap, FSL_SAI_TCSR(ofs), FSL_SAI_CSR_SR);
863 	regmap_write(sai->regmap, FSL_SAI_RCSR(ofs), FSL_SAI_CSR_SR);
864 	/* Clear SR bit to finish the reset */
865 	regmap_write(sai->regmap, FSL_SAI_TCSR(ofs), 0);
866 	regmap_write(sai->regmap, FSL_SAI_RCSR(ofs), 0);
867 
868 	regmap_update_bits(sai->regmap, FSL_SAI_TCR1(ofs),
869 			   FSL_SAI_CR1_RFW_MASK(sai->soc_data->fifo_depth),
870 			   sai->soc_data->fifo_depth - sai->dma_params_tx.maxburst);
871 	regmap_update_bits(sai->regmap, FSL_SAI_RCR1(ofs),
872 			   FSL_SAI_CR1_RFW_MASK(sai->soc_data->fifo_depth),
873 			   sai->dma_params_rx.maxburst - 1);
874 
875 	snd_soc_dai_init_dma_data(cpu_dai, &sai->dma_params_tx,
876 				&sai->dma_params_rx);
877 
878 	return 0;
879 }
880 
881 static const struct snd_soc_dai_ops fsl_sai_pcm_dai_ops = {
882 	.probe		= fsl_sai_dai_probe,
883 	.set_bclk_ratio	= fsl_sai_set_dai_bclk_ratio,
884 	.set_sysclk	= fsl_sai_set_dai_sysclk,
885 	.set_fmt	= fsl_sai_set_dai_fmt,
886 	.set_tdm_slot	= fsl_sai_set_dai_tdm_slot,
887 	.hw_params	= fsl_sai_hw_params,
888 	.hw_free	= fsl_sai_hw_free,
889 	.trigger	= fsl_sai_trigger,
890 	.startup	= fsl_sai_startup,
891 };
892 
893 static int fsl_sai_dai_resume(struct snd_soc_component *component)
894 {
895 	struct fsl_sai *sai = snd_soc_component_get_drvdata(component);
896 	struct device *dev = &sai->pdev->dev;
897 	int ret;
898 
899 	if (!IS_ERR_OR_NULL(sai->pinctrl) && !IS_ERR_OR_NULL(sai->pins_state)) {
900 		ret = pinctrl_select_state(sai->pinctrl, sai->pins_state);
901 		if (ret) {
902 			dev_err(dev, "failed to set proper pins state: %d\n", ret);
903 			return ret;
904 		}
905 	}
906 
907 	return 0;
908 }
909 
910 static struct snd_soc_dai_driver fsl_sai_dai_template = {
911 	.playback = {
912 		.stream_name = "CPU-Playback",
913 		.channels_min = 1,
914 		.channels_max = 32,
915 		.rate_min = 8000,
916 		.rate_max = 2822400,
917 		.rates = SNDRV_PCM_RATE_KNOT,
918 		.formats = FSL_SAI_FORMATS,
919 	},
920 	.capture = {
921 		.stream_name = "CPU-Capture",
922 		.channels_min = 1,
923 		.channels_max = 32,
924 		.rate_min = 8000,
925 		.rate_max = 2822400,
926 		.rates = SNDRV_PCM_RATE_KNOT,
927 		.formats = FSL_SAI_FORMATS,
928 	},
929 	.ops = &fsl_sai_pcm_dai_ops,
930 };
931 
932 static const struct snd_soc_component_driver fsl_component = {
933 	.name			= "fsl-sai",
934 	.resume			= fsl_sai_dai_resume,
935 	.legacy_dai_naming	= 1,
936 };
937 
938 static struct reg_default fsl_sai_reg_defaults_ofs0[] = {
939 	{FSL_SAI_TCR1(0), 0},
940 	{FSL_SAI_TCR2(0), 0},
941 	{FSL_SAI_TCR3(0), 0},
942 	{FSL_SAI_TCR4(0), 0},
943 	{FSL_SAI_TCR5(0), 0},
944 	{FSL_SAI_TDR0, 0},
945 	{FSL_SAI_TDR1, 0},
946 	{FSL_SAI_TDR2, 0},
947 	{FSL_SAI_TDR3, 0},
948 	{FSL_SAI_TDR4, 0},
949 	{FSL_SAI_TDR5, 0},
950 	{FSL_SAI_TDR6, 0},
951 	{FSL_SAI_TDR7, 0},
952 	{FSL_SAI_TMR, 0},
953 	{FSL_SAI_RCR1(0), 0},
954 	{FSL_SAI_RCR2(0), 0},
955 	{FSL_SAI_RCR3(0), 0},
956 	{FSL_SAI_RCR4(0), 0},
957 	{FSL_SAI_RCR5(0), 0},
958 	{FSL_SAI_RMR, 0},
959 };
960 
961 static struct reg_default fsl_sai_reg_defaults_ofs8[] = {
962 	{FSL_SAI_TCR1(8), 0},
963 	{FSL_SAI_TCR2(8), 0},
964 	{FSL_SAI_TCR3(8), 0},
965 	{FSL_SAI_TCR4(8), 0},
966 	{FSL_SAI_TCR5(8), 0},
967 	{FSL_SAI_TDR0, 0},
968 	{FSL_SAI_TDR1, 0},
969 	{FSL_SAI_TDR2, 0},
970 	{FSL_SAI_TDR3, 0},
971 	{FSL_SAI_TDR4, 0},
972 	{FSL_SAI_TDR5, 0},
973 	{FSL_SAI_TDR6, 0},
974 	{FSL_SAI_TDR7, 0},
975 	{FSL_SAI_TMR, 0},
976 	{FSL_SAI_RCR1(8), 0},
977 	{FSL_SAI_RCR2(8), 0},
978 	{FSL_SAI_RCR3(8), 0},
979 	{FSL_SAI_RCR4(8), 0},
980 	{FSL_SAI_RCR5(8), 0},
981 	{FSL_SAI_RMR, 0},
982 	{FSL_SAI_MCTL, 0},
983 	{FSL_SAI_MDIV, 0},
984 };
985 
986 static bool fsl_sai_readable_reg(struct device *dev, unsigned int reg)
987 {
988 	struct fsl_sai *sai = dev_get_drvdata(dev);
989 	unsigned int ofs = sai->soc_data->reg_offset;
990 
991 	if (reg >= FSL_SAI_TCSR(ofs) && reg <= FSL_SAI_TCR5(ofs))
992 		return true;
993 
994 	if (reg >= FSL_SAI_RCSR(ofs) && reg <= FSL_SAI_RCR5(ofs))
995 		return true;
996 
997 	switch (reg) {
998 	case FSL_SAI_TFR0:
999 	case FSL_SAI_TFR1:
1000 	case FSL_SAI_TFR2:
1001 	case FSL_SAI_TFR3:
1002 	case FSL_SAI_TFR4:
1003 	case FSL_SAI_TFR5:
1004 	case FSL_SAI_TFR6:
1005 	case FSL_SAI_TFR7:
1006 	case FSL_SAI_TMR:
1007 	case FSL_SAI_RDR0:
1008 	case FSL_SAI_RDR1:
1009 	case FSL_SAI_RDR2:
1010 	case FSL_SAI_RDR3:
1011 	case FSL_SAI_RDR4:
1012 	case FSL_SAI_RDR5:
1013 	case FSL_SAI_RDR6:
1014 	case FSL_SAI_RDR7:
1015 	case FSL_SAI_RFR0:
1016 	case FSL_SAI_RFR1:
1017 	case FSL_SAI_RFR2:
1018 	case FSL_SAI_RFR3:
1019 	case FSL_SAI_RFR4:
1020 	case FSL_SAI_RFR5:
1021 	case FSL_SAI_RFR6:
1022 	case FSL_SAI_RFR7:
1023 	case FSL_SAI_RMR:
1024 	case FSL_SAI_MCTL:
1025 	case FSL_SAI_MDIV:
1026 	case FSL_SAI_VERID:
1027 	case FSL_SAI_PARAM:
1028 	case FSL_SAI_TTCTN:
1029 	case FSL_SAI_RTCTN:
1030 	case FSL_SAI_TTCTL:
1031 	case FSL_SAI_TBCTN:
1032 	case FSL_SAI_TTCAP:
1033 	case FSL_SAI_RTCTL:
1034 	case FSL_SAI_RBCTN:
1035 	case FSL_SAI_RTCAP:
1036 		return true;
1037 	default:
1038 		return false;
1039 	}
1040 }
1041 
1042 static bool fsl_sai_volatile_reg(struct device *dev, unsigned int reg)
1043 {
1044 	struct fsl_sai *sai = dev_get_drvdata(dev);
1045 	unsigned int ofs = sai->soc_data->reg_offset;
1046 
1047 	if (reg == FSL_SAI_TCSR(ofs) || reg == FSL_SAI_RCSR(ofs))
1048 		return true;
1049 
1050 	/* Set VERID and PARAM be volatile for reading value in probe */
1051 	if (ofs == 8 && (reg == FSL_SAI_VERID || reg == FSL_SAI_PARAM))
1052 		return true;
1053 
1054 	switch (reg) {
1055 	case FSL_SAI_TFR0:
1056 	case FSL_SAI_TFR1:
1057 	case FSL_SAI_TFR2:
1058 	case FSL_SAI_TFR3:
1059 	case FSL_SAI_TFR4:
1060 	case FSL_SAI_TFR5:
1061 	case FSL_SAI_TFR6:
1062 	case FSL_SAI_TFR7:
1063 	case FSL_SAI_RFR0:
1064 	case FSL_SAI_RFR1:
1065 	case FSL_SAI_RFR2:
1066 	case FSL_SAI_RFR3:
1067 	case FSL_SAI_RFR4:
1068 	case FSL_SAI_RFR5:
1069 	case FSL_SAI_RFR6:
1070 	case FSL_SAI_RFR7:
1071 	case FSL_SAI_RDR0:
1072 	case FSL_SAI_RDR1:
1073 	case FSL_SAI_RDR2:
1074 	case FSL_SAI_RDR3:
1075 	case FSL_SAI_RDR4:
1076 	case FSL_SAI_RDR5:
1077 	case FSL_SAI_RDR6:
1078 	case FSL_SAI_RDR7:
1079 		return true;
1080 	default:
1081 		return false;
1082 	}
1083 }
1084 
1085 static bool fsl_sai_writeable_reg(struct device *dev, unsigned int reg)
1086 {
1087 	struct fsl_sai *sai = dev_get_drvdata(dev);
1088 	unsigned int ofs = sai->soc_data->reg_offset;
1089 
1090 	if (reg >= FSL_SAI_TCSR(ofs) && reg <= FSL_SAI_TCR5(ofs))
1091 		return true;
1092 
1093 	if (reg >= FSL_SAI_RCSR(ofs) && reg <= FSL_SAI_RCR5(ofs))
1094 		return true;
1095 
1096 	switch (reg) {
1097 	case FSL_SAI_TDR0:
1098 	case FSL_SAI_TDR1:
1099 	case FSL_SAI_TDR2:
1100 	case FSL_SAI_TDR3:
1101 	case FSL_SAI_TDR4:
1102 	case FSL_SAI_TDR5:
1103 	case FSL_SAI_TDR6:
1104 	case FSL_SAI_TDR7:
1105 	case FSL_SAI_TMR:
1106 	case FSL_SAI_RMR:
1107 	case FSL_SAI_MCTL:
1108 	case FSL_SAI_MDIV:
1109 	case FSL_SAI_TTCTL:
1110 	case FSL_SAI_RTCTL:
1111 		return true;
1112 	default:
1113 		return false;
1114 	}
1115 }
1116 
1117 static struct regmap_config fsl_sai_regmap_config = {
1118 	.reg_bits = 32,
1119 	.reg_stride = 4,
1120 	.val_bits = 32,
1121 	.fast_io = true,
1122 
1123 	.max_register = FSL_SAI_RMR,
1124 	.reg_defaults = fsl_sai_reg_defaults_ofs0,
1125 	.num_reg_defaults = ARRAY_SIZE(fsl_sai_reg_defaults_ofs0),
1126 	.readable_reg = fsl_sai_readable_reg,
1127 	.volatile_reg = fsl_sai_volatile_reg,
1128 	.writeable_reg = fsl_sai_writeable_reg,
1129 	.cache_type = REGCACHE_FLAT,
1130 };
1131 
1132 static int fsl_sai_check_version(struct device *dev)
1133 {
1134 	struct fsl_sai *sai = dev_get_drvdata(dev);
1135 	unsigned char ofs = sai->soc_data->reg_offset;
1136 	unsigned int val;
1137 	int ret;
1138 
1139 	if (FSL_SAI_TCSR(ofs) == FSL_SAI_VERID)
1140 		return 0;
1141 
1142 	ret = regmap_read(sai->regmap, FSL_SAI_VERID, &val);
1143 	if (ret < 0)
1144 		return ret;
1145 
1146 	dev_dbg(dev, "VERID: 0x%016X\n", val);
1147 
1148 	sai->verid.version = val &
1149 		(FSL_SAI_VERID_MAJOR_MASK | FSL_SAI_VERID_MINOR_MASK);
1150 	sai->verid.version >>= FSL_SAI_VERID_MINOR_SHIFT;
1151 	sai->verid.feature = val & FSL_SAI_VERID_FEATURE_MASK;
1152 
1153 	ret = regmap_read(sai->regmap, FSL_SAI_PARAM, &val);
1154 	if (ret < 0)
1155 		return ret;
1156 
1157 	dev_dbg(dev, "PARAM: 0x%016X\n", val);
1158 
1159 	/* Max slots per frame, power of 2 */
1160 	sai->param.slot_num = 1 <<
1161 		((val & FSL_SAI_PARAM_SPF_MASK) >> FSL_SAI_PARAM_SPF_SHIFT);
1162 
1163 	/* Words per fifo, power of 2 */
1164 	sai->param.fifo_depth = 1 <<
1165 		((val & FSL_SAI_PARAM_WPF_MASK) >> FSL_SAI_PARAM_WPF_SHIFT);
1166 
1167 	/* Number of datalines implemented */
1168 	sai->param.dataline = val & FSL_SAI_PARAM_DLN_MASK;
1169 
1170 	return 0;
1171 }
1172 
1173 /*
1174  * Calculate the offset between first two datalines, don't
1175  * different offset in one case.
1176  */
1177 static unsigned int fsl_sai_calc_dl_off(unsigned long dl_mask)
1178 {
1179 	int fbidx, nbidx, offset;
1180 
1181 	fbidx = find_first_bit(&dl_mask, FSL_SAI_DL_NUM);
1182 	nbidx = find_next_bit(&dl_mask, FSL_SAI_DL_NUM, fbidx + 1);
1183 	offset = nbidx - fbidx - 1;
1184 
1185 	return (offset < 0 || offset >= (FSL_SAI_DL_NUM - 1) ? 0 : offset);
1186 }
1187 
1188 /*
1189  * read the fsl,dataline property from dts file.
1190  * It has 3 value for each configuration, first one means the type:
1191  * I2S(1) or PDM(2), second one is dataline mask for 'rx', third one is
1192  * dataline mask for 'tx'. for example
1193  *
1194  * fsl,dataline = <1 0xff 0xff 2 0xff 0x11>,
1195  *
1196  * It means I2S type rx mask is 0xff, tx mask is 0xff, PDM type
1197  * rx mask is 0xff, tx mask is 0x11 (dataline 1 and 4 enabled).
1198  *
1199  */
1200 static int fsl_sai_read_dlcfg(struct fsl_sai *sai)
1201 {
1202 	struct platform_device *pdev = sai->pdev;
1203 	struct device_node *np = pdev->dev.of_node;
1204 	struct device *dev = &pdev->dev;
1205 	int ret, elems, i, index, num_cfg;
1206 	char *propname = "fsl,dataline";
1207 	struct fsl_sai_dl_cfg *cfg;
1208 	unsigned long dl_mask;
1209 	unsigned int soc_dl;
1210 	u32 rx, tx, type;
1211 
1212 	elems = of_property_count_u32_elems(np, propname);
1213 
1214 	if (elems <= 0) {
1215 		elems = 0;
1216 	} else if (elems % 3) {
1217 		dev_err(dev, "Number of elements must be divisible to 3.\n");
1218 		return -EINVAL;
1219 	}
1220 
1221 	num_cfg = elems / 3;
1222 	/*  Add one more for default value */
1223 	cfg = devm_kzalloc(&pdev->dev, (num_cfg + 1) * sizeof(*cfg), GFP_KERNEL);
1224 	if (!cfg)
1225 		return -ENOMEM;
1226 
1227 	/* Consider default value "0 0xFF 0xFF" if property is missing */
1228 	soc_dl = BIT(sai->soc_data->pins) - 1;
1229 	cfg[0].type = FSL_SAI_DL_DEFAULT;
1230 	cfg[0].pins[0] = sai->soc_data->pins;
1231 	cfg[0].mask[0] = soc_dl;
1232 	cfg[0].start_off[0] = 0;
1233 	cfg[0].next_off[0] = 0;
1234 
1235 	cfg[0].pins[1] = sai->soc_data->pins;
1236 	cfg[0].mask[1] = soc_dl;
1237 	cfg[0].start_off[1] = 0;
1238 	cfg[0].next_off[1] = 0;
1239 	for (i = 1, index = 0; i < num_cfg + 1; i++) {
1240 		/*
1241 		 * type of dataline
1242 		 * 0 means default mode
1243 		 * 1 means I2S mode
1244 		 * 2 means PDM mode
1245 		 */
1246 		ret = of_property_read_u32_index(np, propname, index++, &type);
1247 		if (ret)
1248 			return -EINVAL;
1249 
1250 		ret = of_property_read_u32_index(np, propname, index++, &rx);
1251 		if (ret)
1252 			return -EINVAL;
1253 
1254 		ret = of_property_read_u32_index(np, propname, index++, &tx);
1255 		if (ret)
1256 			return -EINVAL;
1257 
1258 		if ((rx & ~soc_dl) || (tx & ~soc_dl)) {
1259 			dev_err(dev, "dataline cfg[%d] setting error, mask is 0x%x\n", i, soc_dl);
1260 			return -EINVAL;
1261 		}
1262 
1263 		rx = rx & soc_dl;
1264 		tx = tx & soc_dl;
1265 
1266 		cfg[i].type = type;
1267 		cfg[i].pins[0] = hweight8(rx);
1268 		cfg[i].mask[0] = rx;
1269 		dl_mask = rx;
1270 		cfg[i].start_off[0] = find_first_bit(&dl_mask, FSL_SAI_DL_NUM);
1271 		cfg[i].next_off[0] = fsl_sai_calc_dl_off(rx);
1272 
1273 		cfg[i].pins[1] = hweight8(tx);
1274 		cfg[i].mask[1] = tx;
1275 		dl_mask = tx;
1276 		cfg[i].start_off[1] = find_first_bit(&dl_mask, FSL_SAI_DL_NUM);
1277 		cfg[i].next_off[1] = fsl_sai_calc_dl_off(tx);
1278 	}
1279 
1280 	sai->dl_cfg = cfg;
1281 	sai->dl_cfg_cnt = num_cfg + 1;
1282 	return 0;
1283 }
1284 
1285 static int fsl_sai_runtime_suspend(struct device *dev);
1286 static int fsl_sai_runtime_resume(struct device *dev);
1287 
1288 static int fsl_sai_probe(struct platform_device *pdev)
1289 {
1290 	struct device_node *np = pdev->dev.of_node;
1291 	struct device *dev = &pdev->dev;
1292 	struct fsl_sai *sai;
1293 	struct regmap *gpr;
1294 	void __iomem *base;
1295 	char tmp[8];
1296 	int irq, ret, i;
1297 	int index;
1298 	u32 dmas[4];
1299 
1300 	sai = devm_kzalloc(dev, sizeof(*sai), GFP_KERNEL);
1301 	if (!sai)
1302 		return -ENOMEM;
1303 
1304 	sai->pdev = pdev;
1305 	sai->soc_data = of_device_get_match_data(dev);
1306 
1307 	sai->is_lsb_first = of_property_read_bool(np, "lsb-first");
1308 
1309 	base = devm_platform_get_and_ioremap_resource(pdev, 0, &sai->res);
1310 	if (IS_ERR(base))
1311 		return PTR_ERR(base);
1312 
1313 	if (sai->soc_data->reg_offset == 8) {
1314 		fsl_sai_regmap_config.reg_defaults = fsl_sai_reg_defaults_ofs8;
1315 		fsl_sai_regmap_config.max_register = FSL_SAI_MDIV;
1316 		fsl_sai_regmap_config.num_reg_defaults =
1317 			ARRAY_SIZE(fsl_sai_reg_defaults_ofs8);
1318 	}
1319 
1320 	sai->regmap = devm_regmap_init_mmio(dev, base, &fsl_sai_regmap_config);
1321 	if (IS_ERR(sai->regmap)) {
1322 		dev_err(dev, "regmap init failed\n");
1323 		return PTR_ERR(sai->regmap);
1324 	}
1325 
1326 	sai->bus_clk = devm_clk_get(dev, "bus");
1327 	/* Compatible with old DTB cases */
1328 	if (IS_ERR(sai->bus_clk) && PTR_ERR(sai->bus_clk) != -EPROBE_DEFER)
1329 		sai->bus_clk = devm_clk_get(dev, "sai");
1330 	if (IS_ERR(sai->bus_clk)) {
1331 		dev_err(dev, "failed to get bus clock: %ld\n",
1332 				PTR_ERR(sai->bus_clk));
1333 		/* -EPROBE_DEFER */
1334 		return PTR_ERR(sai->bus_clk);
1335 	}
1336 
1337 	for (i = 1; i < FSL_SAI_MCLK_MAX; i++) {
1338 		sprintf(tmp, "mclk%d", i);
1339 		sai->mclk_clk[i] = devm_clk_get(dev, tmp);
1340 		if (IS_ERR(sai->mclk_clk[i])) {
1341 			dev_err(dev, "failed to get mclk%d clock: %ld\n",
1342 					i, PTR_ERR(sai->mclk_clk[i]));
1343 			sai->mclk_clk[i] = NULL;
1344 		}
1345 	}
1346 
1347 	if (sai->soc_data->mclk0_is_mclk1)
1348 		sai->mclk_clk[0] = sai->mclk_clk[1];
1349 	else
1350 		sai->mclk_clk[0] = sai->bus_clk;
1351 
1352 	fsl_asoc_get_pll_clocks(&pdev->dev, &sai->pll8k_clk,
1353 				&sai->pll11k_clk);
1354 
1355 	/* Use Multi FIFO mode depending on the support from SDMA script */
1356 	ret = of_property_read_u32_array(np, "dmas", dmas, 4);
1357 	if (!sai->soc_data->use_edma && !ret && dmas[2] == IMX_DMATYPE_MULTI_SAI)
1358 		sai->is_multi_fifo_dma = true;
1359 
1360 	/* read dataline mask for rx and tx*/
1361 	ret = fsl_sai_read_dlcfg(sai);
1362 	if (ret < 0) {
1363 		dev_err(dev, "failed to read dlcfg %d\n", ret);
1364 		return ret;
1365 	}
1366 
1367 	irq = platform_get_irq(pdev, 0);
1368 	if (irq < 0)
1369 		return irq;
1370 
1371 	ret = devm_request_irq(dev, irq, fsl_sai_isr, IRQF_SHARED,
1372 			       np->name, sai);
1373 	if (ret) {
1374 		dev_err(dev, "failed to claim irq %u\n", irq);
1375 		return ret;
1376 	}
1377 
1378 	memcpy(&sai->cpu_dai_drv, &fsl_sai_dai_template,
1379 	       sizeof(fsl_sai_dai_template));
1380 
1381 	/* Sync Tx with Rx as default by following old DT binding */
1382 	sai->synchronous[RX] = true;
1383 	sai->synchronous[TX] = false;
1384 	sai->cpu_dai_drv.symmetric_rate = 1;
1385 	sai->cpu_dai_drv.symmetric_channels = 1;
1386 	sai->cpu_dai_drv.symmetric_sample_bits = 1;
1387 
1388 	if (of_property_read_bool(np, "fsl,sai-synchronous-rx") &&
1389 	    of_property_read_bool(np, "fsl,sai-asynchronous")) {
1390 		/* error out if both synchronous and asynchronous are present */
1391 		dev_err(dev, "invalid binding for synchronous mode\n");
1392 		return -EINVAL;
1393 	}
1394 
1395 	if (of_property_read_bool(np, "fsl,sai-synchronous-rx")) {
1396 		/* Sync Rx with Tx */
1397 		sai->synchronous[RX] = false;
1398 		sai->synchronous[TX] = true;
1399 	} else if (of_property_read_bool(np, "fsl,sai-asynchronous")) {
1400 		/* Discard all settings for asynchronous mode */
1401 		sai->synchronous[RX] = false;
1402 		sai->synchronous[TX] = false;
1403 		sai->cpu_dai_drv.symmetric_rate = 0;
1404 		sai->cpu_dai_drv.symmetric_channels = 0;
1405 		sai->cpu_dai_drv.symmetric_sample_bits = 0;
1406 	}
1407 
1408 	sai->mclk_direction_output = of_property_read_bool(np, "fsl,sai-mclk-direction-output");
1409 
1410 	if (sai->mclk_direction_output &&
1411 	    of_device_is_compatible(np, "fsl,imx6ul-sai")) {
1412 		gpr = syscon_regmap_lookup_by_compatible("fsl,imx6ul-iomuxc-gpr");
1413 		if (IS_ERR(gpr)) {
1414 			dev_err(dev, "cannot find iomuxc registers\n");
1415 			return PTR_ERR(gpr);
1416 		}
1417 
1418 		index = of_alias_get_id(np, "sai");
1419 		if (index < 0)
1420 			return index;
1421 
1422 		regmap_update_bits(gpr, IOMUXC_GPR1, MCLK_DIR(index),
1423 				   MCLK_DIR(index));
1424 	}
1425 
1426 	sai->dma_params_rx.addr = sai->res->start + FSL_SAI_RDR0;
1427 	sai->dma_params_tx.addr = sai->res->start + FSL_SAI_TDR0;
1428 	sai->dma_params_rx.maxburst =
1429 		sai->soc_data->max_burst[RX] ? sai->soc_data->max_burst[RX] : FSL_SAI_MAXBURST_RX;
1430 	sai->dma_params_tx.maxburst =
1431 		sai->soc_data->max_burst[TX] ? sai->soc_data->max_burst[TX] : FSL_SAI_MAXBURST_TX;
1432 
1433 	sai->pinctrl = devm_pinctrl_get(&pdev->dev);
1434 
1435 	platform_set_drvdata(pdev, sai);
1436 	pm_runtime_enable(dev);
1437 	if (!pm_runtime_enabled(dev)) {
1438 		ret = fsl_sai_runtime_resume(dev);
1439 		if (ret)
1440 			goto err_pm_disable;
1441 	}
1442 
1443 	ret = pm_runtime_resume_and_get(dev);
1444 	if (ret < 0)
1445 		goto err_pm_get_sync;
1446 
1447 	/* Get sai version */
1448 	ret = fsl_sai_check_version(dev);
1449 	if (ret < 0)
1450 		dev_warn(dev, "Error reading SAI version: %d\n", ret);
1451 
1452 	/* Select MCLK direction */
1453 	if (sai->mclk_direction_output &&
1454 	    sai->soc_data->max_register >= FSL_SAI_MCTL) {
1455 		regmap_update_bits(sai->regmap, FSL_SAI_MCTL,
1456 				   FSL_SAI_MCTL_MCLK_EN, FSL_SAI_MCTL_MCLK_EN);
1457 	}
1458 
1459 	ret = pm_runtime_put_sync(dev);
1460 	if (ret < 0 && ret != -ENOSYS)
1461 		goto err_pm_get_sync;
1462 
1463 	/*
1464 	 * Register platform component before registering cpu dai for there
1465 	 * is not defer probe for platform component in snd_soc_add_pcm_runtime().
1466 	 */
1467 	if (sai->soc_data->use_imx_pcm) {
1468 		ret = imx_pcm_dma_init(pdev);
1469 		if (ret) {
1470 			dev_err_probe(dev, ret, "PCM DMA init failed\n");
1471 			if (!IS_ENABLED(CONFIG_SND_SOC_IMX_PCM_DMA))
1472 				dev_err(dev, "Error: You must enable the imx-pcm-dma support!\n");
1473 			goto err_pm_get_sync;
1474 		}
1475 	} else {
1476 		ret = devm_snd_dmaengine_pcm_register(dev, NULL, 0);
1477 		if (ret) {
1478 			dev_err_probe(dev, ret, "Registering PCM dmaengine failed\n");
1479 			goto err_pm_get_sync;
1480 		}
1481 	}
1482 
1483 	ret = devm_snd_soc_register_component(dev, &fsl_component,
1484 					      &sai->cpu_dai_drv, 1);
1485 	if (ret)
1486 		goto err_pm_get_sync;
1487 
1488 	return ret;
1489 
1490 err_pm_get_sync:
1491 	if (!pm_runtime_status_suspended(dev))
1492 		fsl_sai_runtime_suspend(dev);
1493 err_pm_disable:
1494 	pm_runtime_disable(dev);
1495 
1496 	return ret;
1497 }
1498 
1499 static void fsl_sai_remove(struct platform_device *pdev)
1500 {
1501 	pm_runtime_disable(&pdev->dev);
1502 	if (!pm_runtime_status_suspended(&pdev->dev))
1503 		fsl_sai_runtime_suspend(&pdev->dev);
1504 }
1505 
1506 static const struct fsl_sai_soc_data fsl_sai_vf610_data = {
1507 	.use_imx_pcm = false,
1508 	.use_edma = false,
1509 	.fifo_depth = 32,
1510 	.pins = 1,
1511 	.reg_offset = 0,
1512 	.mclk0_is_mclk1 = false,
1513 	.flags = 0,
1514 	.max_register = FSL_SAI_RMR,
1515 };
1516 
1517 static const struct fsl_sai_soc_data fsl_sai_imx6sx_data = {
1518 	.use_imx_pcm = true,
1519 	.use_edma = false,
1520 	.fifo_depth = 32,
1521 	.pins = 1,
1522 	.reg_offset = 0,
1523 	.mclk0_is_mclk1 = true,
1524 	.flags = 0,
1525 	.max_register = FSL_SAI_RMR,
1526 };
1527 
1528 static const struct fsl_sai_soc_data fsl_sai_imx7ulp_data = {
1529 	.use_imx_pcm = true,
1530 	.use_edma = false,
1531 	.fifo_depth = 16,
1532 	.pins = 2,
1533 	.reg_offset = 8,
1534 	.mclk0_is_mclk1 = false,
1535 	.flags = PMQOS_CPU_LATENCY,
1536 	.max_register = FSL_SAI_RMR,
1537 };
1538 
1539 static const struct fsl_sai_soc_data fsl_sai_imx8mq_data = {
1540 	.use_imx_pcm = true,
1541 	.use_edma = false,
1542 	.fifo_depth = 128,
1543 	.pins = 8,
1544 	.reg_offset = 8,
1545 	.mclk0_is_mclk1 = false,
1546 	.flags = 0,
1547 	.max_register = FSL_SAI_RMR,
1548 };
1549 
1550 static const struct fsl_sai_soc_data fsl_sai_imx8qm_data = {
1551 	.use_imx_pcm = true,
1552 	.use_edma = true,
1553 	.fifo_depth = 64,
1554 	.pins = 4,
1555 	.reg_offset = 0,
1556 	.mclk0_is_mclk1 = false,
1557 	.flags = 0,
1558 	.max_register = FSL_SAI_RMR,
1559 };
1560 
1561 static const struct fsl_sai_soc_data fsl_sai_imx8mm_data = {
1562 	.use_imx_pcm = true,
1563 	.use_edma = false,
1564 	.fifo_depth = 128,
1565 	.reg_offset = 8,
1566 	.mclk0_is_mclk1 = false,
1567 	.pins = 8,
1568 	.flags = 0,
1569 	.max_register = FSL_SAI_MCTL,
1570 };
1571 
1572 static const struct fsl_sai_soc_data fsl_sai_imx8mn_data = {
1573 	.use_imx_pcm = true,
1574 	.use_edma = false,
1575 	.fifo_depth = 128,
1576 	.reg_offset = 8,
1577 	.mclk0_is_mclk1 = false,
1578 	.pins = 8,
1579 	.flags = 0,
1580 	.max_register = FSL_SAI_MDIV,
1581 };
1582 
1583 static const struct fsl_sai_soc_data fsl_sai_imx8mp_data = {
1584 	.use_imx_pcm = true,
1585 	.use_edma = false,
1586 	.fifo_depth = 128,
1587 	.reg_offset = 8,
1588 	.mclk0_is_mclk1 = false,
1589 	.pins = 8,
1590 	.flags = 0,
1591 	.max_register = FSL_SAI_MDIV,
1592 	.mclk_with_tere = true,
1593 };
1594 
1595 static const struct fsl_sai_soc_data fsl_sai_imx8ulp_data = {
1596 	.use_imx_pcm = true,
1597 	.use_edma = true,
1598 	.fifo_depth = 16,
1599 	.reg_offset = 8,
1600 	.mclk0_is_mclk1 = false,
1601 	.pins = 4,
1602 	.flags = PMQOS_CPU_LATENCY,
1603 	.max_register = FSL_SAI_RTCAP,
1604 };
1605 
1606 static const struct fsl_sai_soc_data fsl_sai_imx93_data = {
1607 	.use_imx_pcm = true,
1608 	.use_edma = true,
1609 	.fifo_depth = 128,
1610 	.reg_offset = 8,
1611 	.mclk0_is_mclk1 = false,
1612 	.pins = 4,
1613 	.flags = 0,
1614 	.max_register = FSL_SAI_MCTL,
1615 	.max_burst = {8, 8},
1616 };
1617 
1618 static const struct of_device_id fsl_sai_ids[] = {
1619 	{ .compatible = "fsl,vf610-sai", .data = &fsl_sai_vf610_data },
1620 	{ .compatible = "fsl,imx6sx-sai", .data = &fsl_sai_imx6sx_data },
1621 	{ .compatible = "fsl,imx6ul-sai", .data = &fsl_sai_imx6sx_data },
1622 	{ .compatible = "fsl,imx7ulp-sai", .data = &fsl_sai_imx7ulp_data },
1623 	{ .compatible = "fsl,imx8mq-sai", .data = &fsl_sai_imx8mq_data },
1624 	{ .compatible = "fsl,imx8qm-sai", .data = &fsl_sai_imx8qm_data },
1625 	{ .compatible = "fsl,imx8mm-sai", .data = &fsl_sai_imx8mm_data },
1626 	{ .compatible = "fsl,imx8mp-sai", .data = &fsl_sai_imx8mp_data },
1627 	{ .compatible = "fsl,imx8ulp-sai", .data = &fsl_sai_imx8ulp_data },
1628 	{ .compatible = "fsl,imx8mn-sai", .data = &fsl_sai_imx8mn_data },
1629 	{ .compatible = "fsl,imx93-sai", .data = &fsl_sai_imx93_data },
1630 	{ /* sentinel */ }
1631 };
1632 MODULE_DEVICE_TABLE(of, fsl_sai_ids);
1633 
1634 static int fsl_sai_runtime_suspend(struct device *dev)
1635 {
1636 	struct fsl_sai *sai = dev_get_drvdata(dev);
1637 
1638 	if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_CAPTURE))
1639 		clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[0]]);
1640 
1641 	if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_PLAYBACK))
1642 		clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[1]]);
1643 
1644 	clk_disable_unprepare(sai->bus_clk);
1645 
1646 	if (sai->soc_data->flags & PMQOS_CPU_LATENCY)
1647 		cpu_latency_qos_remove_request(&sai->pm_qos_req);
1648 
1649 	regcache_cache_only(sai->regmap, true);
1650 
1651 	return 0;
1652 }
1653 
1654 static int fsl_sai_runtime_resume(struct device *dev)
1655 {
1656 	struct fsl_sai *sai = dev_get_drvdata(dev);
1657 	unsigned int ofs = sai->soc_data->reg_offset;
1658 	int ret;
1659 
1660 	ret = clk_prepare_enable(sai->bus_clk);
1661 	if (ret) {
1662 		dev_err(dev, "failed to enable bus clock: %d\n", ret);
1663 		return ret;
1664 	}
1665 
1666 	if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_PLAYBACK)) {
1667 		ret = clk_prepare_enable(sai->mclk_clk[sai->mclk_id[1]]);
1668 		if (ret)
1669 			goto disable_bus_clk;
1670 	}
1671 
1672 	if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_CAPTURE)) {
1673 		ret = clk_prepare_enable(sai->mclk_clk[sai->mclk_id[0]]);
1674 		if (ret)
1675 			goto disable_tx_clk;
1676 	}
1677 
1678 	if (sai->soc_data->flags & PMQOS_CPU_LATENCY)
1679 		cpu_latency_qos_add_request(&sai->pm_qos_req, 0);
1680 
1681 	regcache_cache_only(sai->regmap, false);
1682 	regcache_mark_dirty(sai->regmap);
1683 	regmap_write(sai->regmap, FSL_SAI_TCSR(ofs), FSL_SAI_CSR_SR);
1684 	regmap_write(sai->regmap, FSL_SAI_RCSR(ofs), FSL_SAI_CSR_SR);
1685 	usleep_range(1000, 2000);
1686 	regmap_write(sai->regmap, FSL_SAI_TCSR(ofs), 0);
1687 	regmap_write(sai->regmap, FSL_SAI_RCSR(ofs), 0);
1688 
1689 	ret = regcache_sync(sai->regmap);
1690 	if (ret)
1691 		goto disable_rx_clk;
1692 
1693 	if (sai->soc_data->mclk_with_tere && sai->mclk_direction_output)
1694 		regmap_update_bits(sai->regmap, FSL_SAI_TCSR(ofs),
1695 				   FSL_SAI_CSR_TERE, FSL_SAI_CSR_TERE);
1696 
1697 	return 0;
1698 
1699 disable_rx_clk:
1700 	if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_CAPTURE))
1701 		clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[0]]);
1702 disable_tx_clk:
1703 	if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_PLAYBACK))
1704 		clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[1]]);
1705 disable_bus_clk:
1706 	clk_disable_unprepare(sai->bus_clk);
1707 
1708 	return ret;
1709 }
1710 
1711 static const struct dev_pm_ops fsl_sai_pm_ops = {
1712 	SET_RUNTIME_PM_OPS(fsl_sai_runtime_suspend,
1713 			   fsl_sai_runtime_resume, NULL)
1714 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1715 				pm_runtime_force_resume)
1716 };
1717 
1718 static struct platform_driver fsl_sai_driver = {
1719 	.probe = fsl_sai_probe,
1720 	.remove_new = fsl_sai_remove,
1721 	.driver = {
1722 		.name = "fsl-sai",
1723 		.pm = &fsl_sai_pm_ops,
1724 		.of_match_table = fsl_sai_ids,
1725 	},
1726 };
1727 module_platform_driver(fsl_sai_driver);
1728 
1729 MODULE_DESCRIPTION("Freescale Soc SAI Interface");
1730 MODULE_AUTHOR("Xiubo Li, <Li.Xiubo@freescale.com>");
1731 MODULE_ALIAS("platform:fsl-sai");
1732 MODULE_LICENSE("GPL");
1733