xref: /openbmc/linux/sound/soc/fsl/fsl_sai.c (revision 150b2e86)
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_address.h>
12 #include <linux/of_device.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/regmap.h>
15 #include <linux/slab.h>
16 #include <linux/time.h>
17 #include <sound/core.h>
18 #include <sound/dmaengine_pcm.h>
19 #include <sound/pcm_params.h>
20 #include <linux/mfd/syscon.h>
21 #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
22 
23 #include "fsl_sai.h"
24 #include "imx-pcm.h"
25 
26 #define FSL_SAI_FLAGS (FSL_SAI_CSR_SEIE |\
27 		       FSL_SAI_CSR_FEIE)
28 
29 static const unsigned int fsl_sai_rates[] = {
30 	8000, 11025, 12000, 16000, 22050,
31 	24000, 32000, 44100, 48000, 64000,
32 	88200, 96000, 176400, 192000
33 };
34 
35 static const struct snd_pcm_hw_constraint_list fsl_sai_rate_constraints = {
36 	.count = ARRAY_SIZE(fsl_sai_rates),
37 	.list = fsl_sai_rates,
38 };
39 
40 /**
41  * fsl_sai_dir_is_synced - Check if stream is synced by the opposite stream
42  *
43  * SAI supports synchronous mode using bit/frame clocks of either Transmitter's
44  * or Receiver's for both streams. This function is used to check if clocks of
45  * the stream's are synced by the opposite stream.
46  *
47  * @sai: SAI context
48  * @dir: stream direction
49  */
50 static inline bool fsl_sai_dir_is_synced(struct fsl_sai *sai, int dir)
51 {
52 	int adir = (dir == TX) ? RX : TX;
53 
54 	/* current dir in async mode while opposite dir in sync mode */
55 	return !sai->synchronous[dir] && sai->synchronous[adir];
56 }
57 
58 static irqreturn_t fsl_sai_isr(int irq, void *devid)
59 {
60 	struct fsl_sai *sai = (struct fsl_sai *)devid;
61 	unsigned int ofs = sai->soc_data->reg_offset;
62 	struct device *dev = &sai->pdev->dev;
63 	u32 flags, xcsr, mask;
64 	bool irq_none = true;
65 
66 	/*
67 	 * Both IRQ status bits and IRQ mask bits are in the xCSR but
68 	 * different shifts. And we here create a mask only for those
69 	 * IRQs that we activated.
70 	 */
71 	mask = (FSL_SAI_FLAGS >> FSL_SAI_CSR_xIE_SHIFT) << FSL_SAI_CSR_xF_SHIFT;
72 
73 	/* Tx IRQ */
74 	regmap_read(sai->regmap, FSL_SAI_TCSR(ofs), &xcsr);
75 	flags = xcsr & mask;
76 
77 	if (flags)
78 		irq_none = false;
79 	else
80 		goto irq_rx;
81 
82 	if (flags & FSL_SAI_CSR_WSF)
83 		dev_dbg(dev, "isr: Start of Tx word detected\n");
84 
85 	if (flags & FSL_SAI_CSR_SEF)
86 		dev_dbg(dev, "isr: Tx Frame sync error detected\n");
87 
88 	if (flags & FSL_SAI_CSR_FEF) {
89 		dev_dbg(dev, "isr: Transmit underrun detected\n");
90 		/* FIFO reset for safety */
91 		xcsr |= FSL_SAI_CSR_FR;
92 	}
93 
94 	if (flags & FSL_SAI_CSR_FWF)
95 		dev_dbg(dev, "isr: Enabled transmit FIFO is empty\n");
96 
97 	if (flags & FSL_SAI_CSR_FRF)
98 		dev_dbg(dev, "isr: Transmit FIFO watermark has been reached\n");
99 
100 	flags &= FSL_SAI_CSR_xF_W_MASK;
101 	xcsr &= ~FSL_SAI_CSR_xF_MASK;
102 
103 	if (flags)
104 		regmap_write(sai->regmap, FSL_SAI_TCSR(ofs), flags | xcsr);
105 
106 irq_rx:
107 	/* Rx IRQ */
108 	regmap_read(sai->regmap, FSL_SAI_RCSR(ofs), &xcsr);
109 	flags = xcsr & mask;
110 
111 	if (flags)
112 		irq_none = false;
113 	else
114 		goto out;
115 
116 	if (flags & FSL_SAI_CSR_WSF)
117 		dev_dbg(dev, "isr: Start of Rx word detected\n");
118 
119 	if (flags & FSL_SAI_CSR_SEF)
120 		dev_dbg(dev, "isr: Rx Frame sync error detected\n");
121 
122 	if (flags & FSL_SAI_CSR_FEF) {
123 		dev_dbg(dev, "isr: Receive overflow detected\n");
124 		/* FIFO reset for safety */
125 		xcsr |= FSL_SAI_CSR_FR;
126 	}
127 
128 	if (flags & FSL_SAI_CSR_FWF)
129 		dev_dbg(dev, "isr: Enabled receive FIFO is full\n");
130 
131 	if (flags & FSL_SAI_CSR_FRF)
132 		dev_dbg(dev, "isr: Receive FIFO watermark has been reached\n");
133 
134 	flags &= FSL_SAI_CSR_xF_W_MASK;
135 	xcsr &= ~FSL_SAI_CSR_xF_MASK;
136 
137 	if (flags)
138 		regmap_write(sai->regmap, FSL_SAI_RCSR(ofs), flags | xcsr);
139 
140 out:
141 	if (irq_none)
142 		return IRQ_NONE;
143 	else
144 		return IRQ_HANDLED;
145 }
146 
147 static int fsl_sai_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai, u32 tx_mask,
148 				u32 rx_mask, int slots, int slot_width)
149 {
150 	struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
151 
152 	sai->slots = slots;
153 	sai->slot_width = slot_width;
154 
155 	return 0;
156 }
157 
158 static int fsl_sai_set_dai_bclk_ratio(struct snd_soc_dai *dai,
159 				      unsigned int ratio)
160 {
161 	struct fsl_sai *sai = snd_soc_dai_get_drvdata(dai);
162 
163 	sai->bclk_ratio = ratio;
164 
165 	return 0;
166 }
167 
168 static int fsl_sai_set_dai_sysclk_tr(struct snd_soc_dai *cpu_dai,
169 		int clk_id, unsigned int freq, int fsl_dir)
170 {
171 	struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
172 	unsigned int ofs = sai->soc_data->reg_offset;
173 	bool tx = fsl_dir == FSL_FMT_TRANSMITTER;
174 	u32 val_cr2 = 0;
175 
176 	switch (clk_id) {
177 	case FSL_SAI_CLK_BUS:
178 		val_cr2 |= FSL_SAI_CR2_MSEL_BUS;
179 		break;
180 	case FSL_SAI_CLK_MAST1:
181 		val_cr2 |= FSL_SAI_CR2_MSEL_MCLK1;
182 		break;
183 	case FSL_SAI_CLK_MAST2:
184 		val_cr2 |= FSL_SAI_CR2_MSEL_MCLK2;
185 		break;
186 	case FSL_SAI_CLK_MAST3:
187 		val_cr2 |= FSL_SAI_CR2_MSEL_MCLK3;
188 		break;
189 	default:
190 		return -EINVAL;
191 	}
192 
193 	regmap_update_bits(sai->regmap, FSL_SAI_xCR2(tx, ofs),
194 			   FSL_SAI_CR2_MSEL_MASK, val_cr2);
195 
196 	return 0;
197 }
198 
199 static int fsl_sai_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
200 		int clk_id, unsigned int freq, int dir)
201 {
202 	int ret;
203 
204 	if (dir == SND_SOC_CLOCK_IN)
205 		return 0;
206 
207 	ret = fsl_sai_set_dai_sysclk_tr(cpu_dai, clk_id, freq,
208 					FSL_FMT_TRANSMITTER);
209 	if (ret) {
210 		dev_err(cpu_dai->dev, "Cannot set tx sysclk: %d\n", ret);
211 		return ret;
212 	}
213 
214 	ret = fsl_sai_set_dai_sysclk_tr(cpu_dai, clk_id, freq,
215 					FSL_FMT_RECEIVER);
216 	if (ret)
217 		dev_err(cpu_dai->dev, "Cannot set rx sysclk: %d\n", ret);
218 
219 	return ret;
220 }
221 
222 static int fsl_sai_set_dai_fmt_tr(struct snd_soc_dai *cpu_dai,
223 				unsigned int fmt, int fsl_dir)
224 {
225 	struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
226 	unsigned int ofs = sai->soc_data->reg_offset;
227 	bool tx = fsl_dir == FSL_FMT_TRANSMITTER;
228 	u32 val_cr2 = 0, val_cr4 = 0;
229 
230 	if (!sai->is_lsb_first)
231 		val_cr4 |= FSL_SAI_CR4_MF;
232 
233 	/* DAI mode */
234 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
235 	case SND_SOC_DAIFMT_I2S:
236 		/*
237 		 * Frame low, 1clk before data, one word length for frame sync,
238 		 * frame sync starts one serial clock cycle earlier,
239 		 * that is, together with the last bit of the previous
240 		 * data word.
241 		 */
242 		val_cr2 |= FSL_SAI_CR2_BCP;
243 		val_cr4 |= FSL_SAI_CR4_FSE | FSL_SAI_CR4_FSP;
244 		break;
245 	case SND_SOC_DAIFMT_LEFT_J:
246 		/*
247 		 * Frame high, one word length for frame sync,
248 		 * frame sync asserts with the first bit of the frame.
249 		 */
250 		val_cr2 |= FSL_SAI_CR2_BCP;
251 		break;
252 	case SND_SOC_DAIFMT_DSP_A:
253 		/*
254 		 * Frame high, 1clk before data, one bit for frame sync,
255 		 * frame sync starts one serial clock cycle earlier,
256 		 * that is, together with the last bit of the previous
257 		 * data word.
258 		 */
259 		val_cr2 |= FSL_SAI_CR2_BCP;
260 		val_cr4 |= FSL_SAI_CR4_FSE;
261 		sai->is_dsp_mode = true;
262 		break;
263 	case SND_SOC_DAIFMT_DSP_B:
264 		/*
265 		 * Frame high, one bit for frame sync,
266 		 * frame sync asserts with the first bit of the frame.
267 		 */
268 		val_cr2 |= FSL_SAI_CR2_BCP;
269 		sai->is_dsp_mode = true;
270 		break;
271 	case SND_SOC_DAIFMT_RIGHT_J:
272 		/* To be done */
273 	default:
274 		return -EINVAL;
275 	}
276 
277 	/* DAI clock inversion */
278 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
279 	case SND_SOC_DAIFMT_IB_IF:
280 		/* Invert both clocks */
281 		val_cr2 ^= FSL_SAI_CR2_BCP;
282 		val_cr4 ^= FSL_SAI_CR4_FSP;
283 		break;
284 	case SND_SOC_DAIFMT_IB_NF:
285 		/* Invert bit clock */
286 		val_cr2 ^= FSL_SAI_CR2_BCP;
287 		break;
288 	case SND_SOC_DAIFMT_NB_IF:
289 		/* Invert frame clock */
290 		val_cr4 ^= FSL_SAI_CR4_FSP;
291 		break;
292 	case SND_SOC_DAIFMT_NB_NF:
293 		/* Nothing to do for both normal cases */
294 		break;
295 	default:
296 		return -EINVAL;
297 	}
298 
299 	/* DAI clock master masks */
300 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
301 	case SND_SOC_DAIFMT_CBS_CFS:
302 		val_cr2 |= FSL_SAI_CR2_BCD_MSTR;
303 		val_cr4 |= FSL_SAI_CR4_FSD_MSTR;
304 		sai->is_slave_mode = false;
305 		break;
306 	case SND_SOC_DAIFMT_CBM_CFM:
307 		sai->is_slave_mode = true;
308 		break;
309 	case SND_SOC_DAIFMT_CBS_CFM:
310 		val_cr2 |= FSL_SAI_CR2_BCD_MSTR;
311 		sai->is_slave_mode = false;
312 		break;
313 	case SND_SOC_DAIFMT_CBM_CFS:
314 		val_cr4 |= FSL_SAI_CR4_FSD_MSTR;
315 		sai->is_slave_mode = true;
316 		break;
317 	default:
318 		return -EINVAL;
319 	}
320 
321 	regmap_update_bits(sai->regmap, FSL_SAI_xCR2(tx, ofs),
322 			   FSL_SAI_CR2_BCP | FSL_SAI_CR2_BCD_MSTR, val_cr2);
323 	regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx, ofs),
324 			   FSL_SAI_CR4_MF | FSL_SAI_CR4_FSE |
325 			   FSL_SAI_CR4_FSP | FSL_SAI_CR4_FSD_MSTR, val_cr4);
326 
327 	return 0;
328 }
329 
330 static int fsl_sai_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
331 {
332 	int ret;
333 
334 	ret = fsl_sai_set_dai_fmt_tr(cpu_dai, fmt, FSL_FMT_TRANSMITTER);
335 	if (ret) {
336 		dev_err(cpu_dai->dev, "Cannot set tx format: %d\n", ret);
337 		return ret;
338 	}
339 
340 	ret = fsl_sai_set_dai_fmt_tr(cpu_dai, fmt, FSL_FMT_RECEIVER);
341 	if (ret)
342 		dev_err(cpu_dai->dev, "Cannot set rx format: %d\n", ret);
343 
344 	return ret;
345 }
346 
347 static int fsl_sai_set_bclk(struct snd_soc_dai *dai, bool tx, u32 freq)
348 {
349 	struct fsl_sai *sai = snd_soc_dai_get_drvdata(dai);
350 	unsigned int ofs = sai->soc_data->reg_offset;
351 	unsigned long clk_rate;
352 	u32 savediv = 0, ratio, savesub = freq;
353 	int adir = tx ? RX : TX;
354 	int dir = tx ? TX : RX;
355 	u32 id;
356 	int ret = 0;
357 
358 	/* Don't apply to slave mode */
359 	if (sai->is_slave_mode)
360 		return 0;
361 
362 	for (id = 0; id < FSL_SAI_MCLK_MAX; id++) {
363 		clk_rate = clk_get_rate(sai->mclk_clk[id]);
364 		if (!clk_rate)
365 			continue;
366 
367 		ratio = clk_rate / freq;
368 
369 		ret = clk_rate - ratio * freq;
370 
371 		/*
372 		 * Drop the source that can not be
373 		 * divided into the required rate.
374 		 */
375 		if (ret != 0 && clk_rate / ret < 1000)
376 			continue;
377 
378 		dev_dbg(dai->dev,
379 			"ratio %d for freq %dHz based on clock %ldHz\n",
380 			ratio, freq, clk_rate);
381 
382 		if (ratio % 2 == 0 && ratio >= 2 && ratio <= 512)
383 			ratio /= 2;
384 		else
385 			continue;
386 
387 		if (ret < savesub) {
388 			savediv = ratio;
389 			sai->mclk_id[tx] = id;
390 			savesub = ret;
391 		}
392 
393 		if (ret == 0)
394 			break;
395 	}
396 
397 	if (savediv == 0) {
398 		dev_err(dai->dev, "failed to derive required %cx rate: %d\n",
399 				tx ? 'T' : 'R', freq);
400 		return -EINVAL;
401 	}
402 
403 	/*
404 	 * 1) For Asynchronous mode, we must set RCR2 register for capture, and
405 	 *    set TCR2 register for playback.
406 	 * 2) For Tx sync with Rx clock, we must set RCR2 register for playback
407 	 *    and capture.
408 	 * 3) For Rx sync with Tx clock, we must set TCR2 register for playback
409 	 *    and capture.
410 	 * 4) For Tx and Rx are both Synchronous with another SAI, we just
411 	 *    ignore it.
412 	 */
413 	if (fsl_sai_dir_is_synced(sai, adir)) {
414 		regmap_update_bits(sai->regmap, FSL_SAI_xCR2(!tx, ofs),
415 				   FSL_SAI_CR2_MSEL_MASK,
416 				   FSL_SAI_CR2_MSEL(sai->mclk_id[tx]));
417 		regmap_update_bits(sai->regmap, FSL_SAI_xCR2(!tx, ofs),
418 				   FSL_SAI_CR2_DIV_MASK, savediv - 1);
419 	} else if (!sai->synchronous[dir]) {
420 		regmap_update_bits(sai->regmap, FSL_SAI_xCR2(tx, ofs),
421 				   FSL_SAI_CR2_MSEL_MASK,
422 				   FSL_SAI_CR2_MSEL(sai->mclk_id[tx]));
423 		regmap_update_bits(sai->regmap, FSL_SAI_xCR2(tx, ofs),
424 				   FSL_SAI_CR2_DIV_MASK, savediv - 1);
425 	}
426 
427 	dev_dbg(dai->dev, "best fit: clock id=%d, div=%d, deviation =%d\n",
428 			sai->mclk_id[tx], savediv, savesub);
429 
430 	return 0;
431 }
432 
433 static int fsl_sai_hw_params(struct snd_pcm_substream *substream,
434 		struct snd_pcm_hw_params *params,
435 		struct snd_soc_dai *cpu_dai)
436 {
437 	struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
438 	unsigned int ofs = sai->soc_data->reg_offset;
439 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
440 	unsigned int channels = params_channels(params);
441 	u32 word_width = params_width(params);
442 	u32 val_cr4 = 0, val_cr5 = 0;
443 	u32 slots = (channels == 1) ? 2 : channels;
444 	u32 slot_width = word_width;
445 	int adir = tx ? RX : TX;
446 	u32 pins;
447 	int ret;
448 
449 	if (sai->slots)
450 		slots = sai->slots;
451 
452 	if (sai->slot_width)
453 		slot_width = sai->slot_width;
454 
455 	pins = DIV_ROUND_UP(channels, slots);
456 
457 	if (!sai->is_slave_mode) {
458 		if (sai->bclk_ratio)
459 			ret = fsl_sai_set_bclk(cpu_dai, tx,
460 					       sai->bclk_ratio *
461 					       params_rate(params));
462 		else
463 			ret = fsl_sai_set_bclk(cpu_dai, tx,
464 					       slots * slot_width *
465 					       params_rate(params));
466 		if (ret)
467 			return ret;
468 
469 		/* Do not enable the clock if it is already enabled */
470 		if (!(sai->mclk_streams & BIT(substream->stream))) {
471 			ret = clk_prepare_enable(sai->mclk_clk[sai->mclk_id[tx]]);
472 			if (ret)
473 				return ret;
474 
475 			sai->mclk_streams |= BIT(substream->stream);
476 		}
477 	}
478 
479 	if (!sai->is_dsp_mode)
480 		val_cr4 |= FSL_SAI_CR4_SYWD(slot_width);
481 
482 	val_cr5 |= FSL_SAI_CR5_WNW(slot_width);
483 	val_cr5 |= FSL_SAI_CR5_W0W(slot_width);
484 
485 	if (sai->is_lsb_first)
486 		val_cr5 |= FSL_SAI_CR5_FBT(0);
487 	else
488 		val_cr5 |= FSL_SAI_CR5_FBT(word_width - 1);
489 
490 	val_cr4 |= FSL_SAI_CR4_FRSZ(slots);
491 
492 	/* Set to output mode to avoid tri-stated data pins */
493 	if (tx)
494 		val_cr4 |= FSL_SAI_CR4_CHMOD;
495 
496 	/*
497 	 * For SAI master mode, when Tx(Rx) sync with Rx(Tx) clock, Rx(Tx) will
498 	 * generate bclk and frame clock for Tx(Rx), we should set RCR4(TCR4),
499 	 * RCR5(TCR5) for playback(capture), or there will be sync error.
500 	 */
501 
502 	if (!sai->is_slave_mode && fsl_sai_dir_is_synced(sai, adir)) {
503 		regmap_update_bits(sai->regmap, FSL_SAI_xCR4(!tx, ofs),
504 				   FSL_SAI_CR4_SYWD_MASK | FSL_SAI_CR4_FRSZ_MASK |
505 				   FSL_SAI_CR4_CHMOD_MASK,
506 				   val_cr4);
507 		regmap_update_bits(sai->regmap, FSL_SAI_xCR5(!tx, ofs),
508 				   FSL_SAI_CR5_WNW_MASK | FSL_SAI_CR5_W0W_MASK |
509 				   FSL_SAI_CR5_FBT_MASK, val_cr5);
510 	}
511 
512 	regmap_update_bits(sai->regmap, FSL_SAI_xCR3(tx, ofs),
513 			   FSL_SAI_CR3_TRCE_MASK,
514 			   FSL_SAI_CR3_TRCE((1 << pins) - 1));
515 	regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx, ofs),
516 			   FSL_SAI_CR4_SYWD_MASK | FSL_SAI_CR4_FRSZ_MASK |
517 			   FSL_SAI_CR4_CHMOD_MASK,
518 			   val_cr4);
519 	regmap_update_bits(sai->regmap, FSL_SAI_xCR5(tx, ofs),
520 			   FSL_SAI_CR5_WNW_MASK | FSL_SAI_CR5_W0W_MASK |
521 			   FSL_SAI_CR5_FBT_MASK, val_cr5);
522 	regmap_write(sai->regmap, FSL_SAI_xMR(tx),
523 		     ~0UL - ((1 << min(channels, slots)) - 1));
524 
525 	return 0;
526 }
527 
528 static int fsl_sai_hw_free(struct snd_pcm_substream *substream,
529 		struct snd_soc_dai *cpu_dai)
530 {
531 	struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
532 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
533 	unsigned int ofs = sai->soc_data->reg_offset;
534 
535 	regmap_update_bits(sai->regmap, FSL_SAI_xCR3(tx, ofs),
536 			   FSL_SAI_CR3_TRCE_MASK, 0);
537 
538 	if (!sai->is_slave_mode &&
539 			sai->mclk_streams & BIT(substream->stream)) {
540 		clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[tx]]);
541 		sai->mclk_streams &= ~BIT(substream->stream);
542 	}
543 
544 	return 0;
545 }
546 
547 static void fsl_sai_config_disable(struct fsl_sai *sai, int dir)
548 {
549 	unsigned int ofs = sai->soc_data->reg_offset;
550 	bool tx = dir == TX;
551 	u32 xcsr, count = 100;
552 
553 	regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
554 			   FSL_SAI_CSR_TERE, 0);
555 
556 	/* TERE will remain set till the end of current frame */
557 	do {
558 		udelay(10);
559 		regmap_read(sai->regmap, FSL_SAI_xCSR(tx, ofs), &xcsr);
560 	} while (--count && xcsr & FSL_SAI_CSR_TERE);
561 
562 	regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
563 			   FSL_SAI_CSR_FR, FSL_SAI_CSR_FR);
564 
565 	/*
566 	 * For sai master mode, after several open/close sai,
567 	 * there will be no frame clock, and can't recover
568 	 * anymore. Add software reset to fix this issue.
569 	 * This is a hardware bug, and will be fix in the
570 	 * next sai version.
571 	 */
572 	if (!sai->is_slave_mode) {
573 		/* Software Reset */
574 		regmap_write(sai->regmap, FSL_SAI_xCSR(tx, ofs), FSL_SAI_CSR_SR);
575 		/* Clear SR bit to finish the reset */
576 		regmap_write(sai->regmap, FSL_SAI_xCSR(tx, ofs), 0);
577 	}
578 }
579 
580 static int fsl_sai_trigger(struct snd_pcm_substream *substream, int cmd,
581 		struct snd_soc_dai *cpu_dai)
582 {
583 	struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
584 	unsigned int ofs = sai->soc_data->reg_offset;
585 
586 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
587 	int adir = tx ? RX : TX;
588 	int dir = tx ? TX : RX;
589 	u32 xcsr;
590 
591 	/*
592 	 * Asynchronous mode: Clear SYNC for both Tx and Rx.
593 	 * Rx sync with Tx clocks: Clear SYNC for Tx, set it for Rx.
594 	 * Tx sync with Rx clocks: Clear SYNC for Rx, set it for Tx.
595 	 */
596 	regmap_update_bits(sai->regmap, FSL_SAI_TCR2(ofs), FSL_SAI_CR2_SYNC,
597 			   sai->synchronous[TX] ? FSL_SAI_CR2_SYNC : 0);
598 	regmap_update_bits(sai->regmap, FSL_SAI_RCR2(ofs), FSL_SAI_CR2_SYNC,
599 			   sai->synchronous[RX] ? FSL_SAI_CR2_SYNC : 0);
600 
601 	/*
602 	 * It is recommended that the transmitter is the last enabled
603 	 * and the first disabled.
604 	 */
605 	switch (cmd) {
606 	case SNDRV_PCM_TRIGGER_START:
607 	case SNDRV_PCM_TRIGGER_RESUME:
608 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
609 		regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
610 				   FSL_SAI_CSR_FRDE, FSL_SAI_CSR_FRDE);
611 
612 		regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
613 				   FSL_SAI_CSR_TERE, FSL_SAI_CSR_TERE);
614 		/*
615 		 * Enable the opposite direction for synchronous mode
616 		 * 1. Tx sync with Rx: only set RE for Rx; set TE & RE for Tx
617 		 * 2. Rx sync with Tx: only set TE for Tx; set RE & TE for Rx
618 		 *
619 		 * RM recommends to enable RE after TE for case 1 and to enable
620 		 * TE after RE for case 2, but we here may not always guarantee
621 		 * that happens: "arecord 1.wav; aplay 2.wav" in case 1 enables
622 		 * TE after RE, which is against what RM recommends but should
623 		 * be safe to do, judging by years of testing results.
624 		 */
625 		if (fsl_sai_dir_is_synced(sai, adir))
626 			regmap_update_bits(sai->regmap, FSL_SAI_xCSR((!tx), ofs),
627 					   FSL_SAI_CSR_TERE, FSL_SAI_CSR_TERE);
628 
629 		regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
630 				   FSL_SAI_CSR_xIE_MASK, FSL_SAI_FLAGS);
631 		break;
632 	case SNDRV_PCM_TRIGGER_STOP:
633 	case SNDRV_PCM_TRIGGER_SUSPEND:
634 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
635 		regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
636 				   FSL_SAI_CSR_FRDE, 0);
637 		regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs),
638 				   FSL_SAI_CSR_xIE_MASK, 0);
639 
640 		/* Check if the opposite FRDE is also disabled */
641 		regmap_read(sai->regmap, FSL_SAI_xCSR(!tx, ofs), &xcsr);
642 
643 		/*
644 		 * If opposite stream provides clocks for synchronous mode and
645 		 * it is inactive, disable it before disabling the current one
646 		 */
647 		if (fsl_sai_dir_is_synced(sai, adir) && !(xcsr & FSL_SAI_CSR_FRDE))
648 			fsl_sai_config_disable(sai, adir);
649 
650 		/*
651 		 * Disable current stream if either of:
652 		 * 1. current stream doesn't provide clocks for synchronous mode
653 		 * 2. current stream provides clocks for synchronous mode but no
654 		 *    more stream is active.
655 		 */
656 		if (!fsl_sai_dir_is_synced(sai, dir) || !(xcsr & FSL_SAI_CSR_FRDE))
657 			fsl_sai_config_disable(sai, dir);
658 
659 		break;
660 	default:
661 		return -EINVAL;
662 	}
663 
664 	return 0;
665 }
666 
667 static int fsl_sai_startup(struct snd_pcm_substream *substream,
668 		struct snd_soc_dai *cpu_dai)
669 {
670 	struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
671 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
672 	int ret;
673 
674 	/*
675 	 * EDMA controller needs period size to be a multiple of
676 	 * tx/rx maxburst
677 	 */
678 	if (sai->soc_data->use_edma)
679 		snd_pcm_hw_constraint_step(substream->runtime, 0,
680 					   SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
681 					   tx ? sai->dma_params_tx.maxburst :
682 					   sai->dma_params_rx.maxburst);
683 
684 	ret = snd_pcm_hw_constraint_list(substream->runtime, 0,
685 			SNDRV_PCM_HW_PARAM_RATE, &fsl_sai_rate_constraints);
686 
687 	return ret;
688 }
689 
690 static const struct snd_soc_dai_ops fsl_sai_pcm_dai_ops = {
691 	.set_bclk_ratio	= fsl_sai_set_dai_bclk_ratio,
692 	.set_sysclk	= fsl_sai_set_dai_sysclk,
693 	.set_fmt	= fsl_sai_set_dai_fmt,
694 	.set_tdm_slot	= fsl_sai_set_dai_tdm_slot,
695 	.hw_params	= fsl_sai_hw_params,
696 	.hw_free	= fsl_sai_hw_free,
697 	.trigger	= fsl_sai_trigger,
698 	.startup	= fsl_sai_startup,
699 };
700 
701 static int fsl_sai_dai_probe(struct snd_soc_dai *cpu_dai)
702 {
703 	struct fsl_sai *sai = dev_get_drvdata(cpu_dai->dev);
704 	unsigned int ofs = sai->soc_data->reg_offset;
705 
706 	/* Software Reset for both Tx and Rx */
707 	regmap_write(sai->regmap, FSL_SAI_TCSR(ofs), FSL_SAI_CSR_SR);
708 	regmap_write(sai->regmap, FSL_SAI_RCSR(ofs), FSL_SAI_CSR_SR);
709 	/* Clear SR bit to finish the reset */
710 	regmap_write(sai->regmap, FSL_SAI_TCSR(ofs), 0);
711 	regmap_write(sai->regmap, FSL_SAI_RCSR(ofs), 0);
712 
713 	regmap_update_bits(sai->regmap, FSL_SAI_TCR1(ofs),
714 			   FSL_SAI_CR1_RFW_MASK(sai->soc_data->fifo_depth),
715 			   sai->soc_data->fifo_depth - FSL_SAI_MAXBURST_TX);
716 	regmap_update_bits(sai->regmap, FSL_SAI_RCR1(ofs),
717 			   FSL_SAI_CR1_RFW_MASK(sai->soc_data->fifo_depth),
718 			   FSL_SAI_MAXBURST_RX - 1);
719 
720 	snd_soc_dai_init_dma_data(cpu_dai, &sai->dma_params_tx,
721 				&sai->dma_params_rx);
722 
723 	snd_soc_dai_set_drvdata(cpu_dai, sai);
724 
725 	return 0;
726 }
727 
728 static struct snd_soc_dai_driver fsl_sai_dai = {
729 	.probe = fsl_sai_dai_probe,
730 	.playback = {
731 		.stream_name = "CPU-Playback",
732 		.channels_min = 1,
733 		.channels_max = 32,
734 		.rate_min = 8000,
735 		.rate_max = 192000,
736 		.rates = SNDRV_PCM_RATE_KNOT,
737 		.formats = FSL_SAI_FORMATS,
738 	},
739 	.capture = {
740 		.stream_name = "CPU-Capture",
741 		.channels_min = 1,
742 		.channels_max = 32,
743 		.rate_min = 8000,
744 		.rate_max = 192000,
745 		.rates = SNDRV_PCM_RATE_KNOT,
746 		.formats = FSL_SAI_FORMATS,
747 	},
748 	.ops = &fsl_sai_pcm_dai_ops,
749 };
750 
751 static const struct snd_soc_component_driver fsl_component = {
752 	.name           = "fsl-sai",
753 };
754 
755 static struct reg_default fsl_sai_reg_defaults_ofs0[] = {
756 	{FSL_SAI_TCR1(0), 0},
757 	{FSL_SAI_TCR2(0), 0},
758 	{FSL_SAI_TCR3(0), 0},
759 	{FSL_SAI_TCR4(0), 0},
760 	{FSL_SAI_TCR5(0), 0},
761 	{FSL_SAI_TDR0, 0},
762 	{FSL_SAI_TDR1, 0},
763 	{FSL_SAI_TDR2, 0},
764 	{FSL_SAI_TDR3, 0},
765 	{FSL_SAI_TDR4, 0},
766 	{FSL_SAI_TDR5, 0},
767 	{FSL_SAI_TDR6, 0},
768 	{FSL_SAI_TDR7, 0},
769 	{FSL_SAI_TMR, 0},
770 	{FSL_SAI_RCR1(0), 0},
771 	{FSL_SAI_RCR2(0), 0},
772 	{FSL_SAI_RCR3(0), 0},
773 	{FSL_SAI_RCR4(0), 0},
774 	{FSL_SAI_RCR5(0), 0},
775 	{FSL_SAI_RMR, 0},
776 };
777 
778 static struct reg_default fsl_sai_reg_defaults_ofs8[] = {
779 	{FSL_SAI_TCR1(8), 0},
780 	{FSL_SAI_TCR2(8), 0},
781 	{FSL_SAI_TCR3(8), 0},
782 	{FSL_SAI_TCR4(8), 0},
783 	{FSL_SAI_TCR5(8), 0},
784 	{FSL_SAI_TDR0, 0},
785 	{FSL_SAI_TDR1, 0},
786 	{FSL_SAI_TDR2, 0},
787 	{FSL_SAI_TDR3, 0},
788 	{FSL_SAI_TDR4, 0},
789 	{FSL_SAI_TDR5, 0},
790 	{FSL_SAI_TDR6, 0},
791 	{FSL_SAI_TDR7, 0},
792 	{FSL_SAI_TMR, 0},
793 	{FSL_SAI_RCR1(8), 0},
794 	{FSL_SAI_RCR2(8), 0},
795 	{FSL_SAI_RCR3(8), 0},
796 	{FSL_SAI_RCR4(8), 0},
797 	{FSL_SAI_RCR5(8), 0},
798 	{FSL_SAI_RMR, 0},
799 };
800 
801 static bool fsl_sai_readable_reg(struct device *dev, unsigned int reg)
802 {
803 	struct fsl_sai *sai = dev_get_drvdata(dev);
804 	unsigned int ofs = sai->soc_data->reg_offset;
805 
806 	if (reg >= FSL_SAI_TCSR(ofs) && reg <= FSL_SAI_TCR5(ofs))
807 		return true;
808 
809 	if (reg >= FSL_SAI_RCSR(ofs) && reg <= FSL_SAI_RCR5(ofs))
810 		return true;
811 
812 	switch (reg) {
813 	case FSL_SAI_TFR0:
814 	case FSL_SAI_TFR1:
815 	case FSL_SAI_TFR2:
816 	case FSL_SAI_TFR3:
817 	case FSL_SAI_TFR4:
818 	case FSL_SAI_TFR5:
819 	case FSL_SAI_TFR6:
820 	case FSL_SAI_TFR7:
821 	case FSL_SAI_TMR:
822 	case FSL_SAI_RDR0:
823 	case FSL_SAI_RDR1:
824 	case FSL_SAI_RDR2:
825 	case FSL_SAI_RDR3:
826 	case FSL_SAI_RDR4:
827 	case FSL_SAI_RDR5:
828 	case FSL_SAI_RDR6:
829 	case FSL_SAI_RDR7:
830 	case FSL_SAI_RFR0:
831 	case FSL_SAI_RFR1:
832 	case FSL_SAI_RFR2:
833 	case FSL_SAI_RFR3:
834 	case FSL_SAI_RFR4:
835 	case FSL_SAI_RFR5:
836 	case FSL_SAI_RFR6:
837 	case FSL_SAI_RFR7:
838 	case FSL_SAI_RMR:
839 		return true;
840 	default:
841 		return false;
842 	}
843 }
844 
845 static bool fsl_sai_volatile_reg(struct device *dev, unsigned int reg)
846 {
847 	struct fsl_sai *sai = dev_get_drvdata(dev);
848 	unsigned int ofs = sai->soc_data->reg_offset;
849 
850 	if (reg == FSL_SAI_TCSR(ofs) || reg == FSL_SAI_RCSR(ofs))
851 		return true;
852 
853 	switch (reg) {
854 	case FSL_SAI_TFR0:
855 	case FSL_SAI_TFR1:
856 	case FSL_SAI_TFR2:
857 	case FSL_SAI_TFR3:
858 	case FSL_SAI_TFR4:
859 	case FSL_SAI_TFR5:
860 	case FSL_SAI_TFR6:
861 	case FSL_SAI_TFR7:
862 	case FSL_SAI_RFR0:
863 	case FSL_SAI_RFR1:
864 	case FSL_SAI_RFR2:
865 	case FSL_SAI_RFR3:
866 	case FSL_SAI_RFR4:
867 	case FSL_SAI_RFR5:
868 	case FSL_SAI_RFR6:
869 	case FSL_SAI_RFR7:
870 	case FSL_SAI_RDR0:
871 	case FSL_SAI_RDR1:
872 	case FSL_SAI_RDR2:
873 	case FSL_SAI_RDR3:
874 	case FSL_SAI_RDR4:
875 	case FSL_SAI_RDR5:
876 	case FSL_SAI_RDR6:
877 	case FSL_SAI_RDR7:
878 		return true;
879 	default:
880 		return false;
881 	}
882 }
883 
884 static bool fsl_sai_writeable_reg(struct device *dev, unsigned int reg)
885 {
886 	struct fsl_sai *sai = dev_get_drvdata(dev);
887 	unsigned int ofs = sai->soc_data->reg_offset;
888 
889 	if (reg >= FSL_SAI_TCSR(ofs) && reg <= FSL_SAI_TCR5(ofs))
890 		return true;
891 
892 	if (reg >= FSL_SAI_RCSR(ofs) && reg <= FSL_SAI_RCR5(ofs))
893 		return true;
894 
895 	switch (reg) {
896 	case FSL_SAI_TDR0:
897 	case FSL_SAI_TDR1:
898 	case FSL_SAI_TDR2:
899 	case FSL_SAI_TDR3:
900 	case FSL_SAI_TDR4:
901 	case FSL_SAI_TDR5:
902 	case FSL_SAI_TDR6:
903 	case FSL_SAI_TDR7:
904 	case FSL_SAI_TMR:
905 	case FSL_SAI_RMR:
906 		return true;
907 	default:
908 		return false;
909 	}
910 }
911 
912 static struct regmap_config fsl_sai_regmap_config = {
913 	.reg_bits = 32,
914 	.reg_stride = 4,
915 	.val_bits = 32,
916 	.fast_io = true,
917 
918 	.max_register = FSL_SAI_RMR,
919 	.reg_defaults = fsl_sai_reg_defaults_ofs0,
920 	.num_reg_defaults = ARRAY_SIZE(fsl_sai_reg_defaults_ofs0),
921 	.readable_reg = fsl_sai_readable_reg,
922 	.volatile_reg = fsl_sai_volatile_reg,
923 	.writeable_reg = fsl_sai_writeable_reg,
924 	.cache_type = REGCACHE_FLAT,
925 };
926 
927 static int fsl_sai_probe(struct platform_device *pdev)
928 {
929 	struct device_node *np = pdev->dev.of_node;
930 	struct fsl_sai *sai;
931 	struct regmap *gpr;
932 	struct resource *res;
933 	void __iomem *base;
934 	char tmp[8];
935 	int irq, ret, i;
936 	int index;
937 
938 	sai = devm_kzalloc(&pdev->dev, sizeof(*sai), GFP_KERNEL);
939 	if (!sai)
940 		return -ENOMEM;
941 
942 	sai->pdev = pdev;
943 	sai->soc_data = of_device_get_match_data(&pdev->dev);
944 
945 	sai->is_lsb_first = of_property_read_bool(np, "lsb-first");
946 
947 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
948 	base = devm_ioremap_resource(&pdev->dev, res);
949 	if (IS_ERR(base))
950 		return PTR_ERR(base);
951 
952 	if (sai->soc_data->reg_offset == 8) {
953 		fsl_sai_regmap_config.reg_defaults = fsl_sai_reg_defaults_ofs8;
954 		fsl_sai_regmap_config.num_reg_defaults =
955 			ARRAY_SIZE(fsl_sai_reg_defaults_ofs8);
956 	}
957 
958 	sai->regmap = devm_regmap_init_mmio_clk(&pdev->dev,
959 			"bus", base, &fsl_sai_regmap_config);
960 
961 	/* Compatible with old DTB cases */
962 	if (IS_ERR(sai->regmap) && PTR_ERR(sai->regmap) != -EPROBE_DEFER)
963 		sai->regmap = devm_regmap_init_mmio_clk(&pdev->dev,
964 				"sai", base, &fsl_sai_regmap_config);
965 	if (IS_ERR(sai->regmap)) {
966 		dev_err(&pdev->dev, "regmap init failed\n");
967 		return PTR_ERR(sai->regmap);
968 	}
969 
970 	/* No error out for old DTB cases but only mark the clock NULL */
971 	sai->bus_clk = devm_clk_get(&pdev->dev, "bus");
972 	if (IS_ERR(sai->bus_clk)) {
973 		dev_err(&pdev->dev, "failed to get bus clock: %ld\n",
974 				PTR_ERR(sai->bus_clk));
975 		sai->bus_clk = NULL;
976 	}
977 
978 	sai->mclk_clk[0] = sai->bus_clk;
979 	for (i = 1; i < FSL_SAI_MCLK_MAX; i++) {
980 		sprintf(tmp, "mclk%d", i);
981 		sai->mclk_clk[i] = devm_clk_get(&pdev->dev, tmp);
982 		if (IS_ERR(sai->mclk_clk[i])) {
983 			dev_err(&pdev->dev, "failed to get mclk%d clock: %ld\n",
984 					i + 1, PTR_ERR(sai->mclk_clk[i]));
985 			sai->mclk_clk[i] = NULL;
986 		}
987 	}
988 
989 	irq = platform_get_irq(pdev, 0);
990 	if (irq < 0)
991 		return irq;
992 
993 	ret = devm_request_irq(&pdev->dev, irq, fsl_sai_isr, IRQF_SHARED,
994 			       np->name, sai);
995 	if (ret) {
996 		dev_err(&pdev->dev, "failed to claim irq %u\n", irq);
997 		return ret;
998 	}
999 
1000 	/* Sync Tx with Rx as default by following old DT binding */
1001 	sai->synchronous[RX] = true;
1002 	sai->synchronous[TX] = false;
1003 	fsl_sai_dai.symmetric_rates = 1;
1004 	fsl_sai_dai.symmetric_channels = 1;
1005 	fsl_sai_dai.symmetric_samplebits = 1;
1006 
1007 	if (of_find_property(np, "fsl,sai-synchronous-rx", NULL) &&
1008 	    of_find_property(np, "fsl,sai-asynchronous", NULL)) {
1009 		/* error out if both synchronous and asynchronous are present */
1010 		dev_err(&pdev->dev, "invalid binding for synchronous mode\n");
1011 		return -EINVAL;
1012 	}
1013 
1014 	if (of_find_property(np, "fsl,sai-synchronous-rx", NULL)) {
1015 		/* Sync Rx with Tx */
1016 		sai->synchronous[RX] = false;
1017 		sai->synchronous[TX] = true;
1018 	} else if (of_find_property(np, "fsl,sai-asynchronous", NULL)) {
1019 		/* Discard all settings for asynchronous mode */
1020 		sai->synchronous[RX] = false;
1021 		sai->synchronous[TX] = false;
1022 		fsl_sai_dai.symmetric_rates = 0;
1023 		fsl_sai_dai.symmetric_channels = 0;
1024 		fsl_sai_dai.symmetric_samplebits = 0;
1025 	}
1026 
1027 	if (of_find_property(np, "fsl,sai-mclk-direction-output", NULL) &&
1028 	    of_device_is_compatible(np, "fsl,imx6ul-sai")) {
1029 		gpr = syscon_regmap_lookup_by_compatible("fsl,imx6ul-iomuxc-gpr");
1030 		if (IS_ERR(gpr)) {
1031 			dev_err(&pdev->dev, "cannot find iomuxc registers\n");
1032 			return PTR_ERR(gpr);
1033 		}
1034 
1035 		index = of_alias_get_id(np, "sai");
1036 		if (index < 0)
1037 			return index;
1038 
1039 		regmap_update_bits(gpr, IOMUXC_GPR1, MCLK_DIR(index),
1040 				   MCLK_DIR(index));
1041 	}
1042 
1043 	sai->dma_params_rx.addr = res->start + FSL_SAI_RDR0;
1044 	sai->dma_params_tx.addr = res->start + FSL_SAI_TDR0;
1045 	sai->dma_params_rx.maxburst = FSL_SAI_MAXBURST_RX;
1046 	sai->dma_params_tx.maxburst = FSL_SAI_MAXBURST_TX;
1047 
1048 	platform_set_drvdata(pdev, sai);
1049 
1050 	pm_runtime_enable(&pdev->dev);
1051 	regcache_cache_only(sai->regmap, true);
1052 
1053 	ret = devm_snd_soc_register_component(&pdev->dev, &fsl_component,
1054 			&fsl_sai_dai, 1);
1055 	if (ret)
1056 		goto err_pm_disable;
1057 
1058 	if (sai->soc_data->use_imx_pcm) {
1059 		ret = imx_pcm_dma_init(pdev, IMX_SAI_DMABUF_SIZE);
1060 		if (ret)
1061 			goto err_pm_disable;
1062 	} else {
1063 		ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
1064 		if (ret)
1065 			goto err_pm_disable;
1066 	}
1067 
1068 	return ret;
1069 
1070 err_pm_disable:
1071 	pm_runtime_disable(&pdev->dev);
1072 
1073 	return ret;
1074 }
1075 
1076 static int fsl_sai_remove(struct platform_device *pdev)
1077 {
1078 	pm_runtime_disable(&pdev->dev);
1079 
1080 	return 0;
1081 }
1082 
1083 static const struct fsl_sai_soc_data fsl_sai_vf610_data = {
1084 	.use_imx_pcm = false,
1085 	.use_edma = false,
1086 	.fifo_depth = 32,
1087 	.reg_offset = 0,
1088 };
1089 
1090 static const struct fsl_sai_soc_data fsl_sai_imx6sx_data = {
1091 	.use_imx_pcm = true,
1092 	.use_edma = false,
1093 	.fifo_depth = 32,
1094 	.reg_offset = 0,
1095 };
1096 
1097 static const struct fsl_sai_soc_data fsl_sai_imx7ulp_data = {
1098 	.use_imx_pcm = true,
1099 	.use_edma = false,
1100 	.fifo_depth = 16,
1101 	.reg_offset = 8,
1102 };
1103 
1104 static const struct fsl_sai_soc_data fsl_sai_imx8mq_data = {
1105 	.use_imx_pcm = true,
1106 	.use_edma = false,
1107 	.fifo_depth = 128,
1108 	.reg_offset = 8,
1109 };
1110 
1111 static const struct fsl_sai_soc_data fsl_sai_imx8qm_data = {
1112 	.use_imx_pcm = true,
1113 	.use_edma = true,
1114 	.fifo_depth = 64,
1115 	.reg_offset = 0,
1116 };
1117 
1118 static const struct of_device_id fsl_sai_ids[] = {
1119 	{ .compatible = "fsl,vf610-sai", .data = &fsl_sai_vf610_data },
1120 	{ .compatible = "fsl,imx6sx-sai", .data = &fsl_sai_imx6sx_data },
1121 	{ .compatible = "fsl,imx6ul-sai", .data = &fsl_sai_imx6sx_data },
1122 	{ .compatible = "fsl,imx7ulp-sai", .data = &fsl_sai_imx7ulp_data },
1123 	{ .compatible = "fsl,imx8mq-sai", .data = &fsl_sai_imx8mq_data },
1124 	{ .compatible = "fsl,imx8qm-sai", .data = &fsl_sai_imx8qm_data },
1125 	{ /* sentinel */ }
1126 };
1127 MODULE_DEVICE_TABLE(of, fsl_sai_ids);
1128 
1129 #ifdef CONFIG_PM
1130 static int fsl_sai_runtime_suspend(struct device *dev)
1131 {
1132 	struct fsl_sai *sai = dev_get_drvdata(dev);
1133 
1134 	if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_CAPTURE))
1135 		clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[0]]);
1136 
1137 	if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_PLAYBACK))
1138 		clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[1]]);
1139 
1140 	clk_disable_unprepare(sai->bus_clk);
1141 
1142 	regcache_cache_only(sai->regmap, true);
1143 
1144 	return 0;
1145 }
1146 
1147 static int fsl_sai_runtime_resume(struct device *dev)
1148 {
1149 	struct fsl_sai *sai = dev_get_drvdata(dev);
1150 	unsigned int ofs = sai->soc_data->reg_offset;
1151 	int ret;
1152 
1153 	ret = clk_prepare_enable(sai->bus_clk);
1154 	if (ret) {
1155 		dev_err(dev, "failed to enable bus clock: %d\n", ret);
1156 		return ret;
1157 	}
1158 
1159 	if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_PLAYBACK)) {
1160 		ret = clk_prepare_enable(sai->mclk_clk[sai->mclk_id[1]]);
1161 		if (ret)
1162 			goto disable_bus_clk;
1163 	}
1164 
1165 	if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_CAPTURE)) {
1166 		ret = clk_prepare_enable(sai->mclk_clk[sai->mclk_id[0]]);
1167 		if (ret)
1168 			goto disable_tx_clk;
1169 	}
1170 
1171 	regcache_cache_only(sai->regmap, false);
1172 	regcache_mark_dirty(sai->regmap);
1173 	regmap_write(sai->regmap, FSL_SAI_TCSR(ofs), FSL_SAI_CSR_SR);
1174 	regmap_write(sai->regmap, FSL_SAI_RCSR(ofs), FSL_SAI_CSR_SR);
1175 	usleep_range(1000, 2000);
1176 	regmap_write(sai->regmap, FSL_SAI_TCSR(ofs), 0);
1177 	regmap_write(sai->regmap, FSL_SAI_RCSR(ofs), 0);
1178 
1179 	ret = regcache_sync(sai->regmap);
1180 	if (ret)
1181 		goto disable_rx_clk;
1182 
1183 	return 0;
1184 
1185 disable_rx_clk:
1186 	if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_CAPTURE))
1187 		clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[0]]);
1188 disable_tx_clk:
1189 	if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_PLAYBACK))
1190 		clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[1]]);
1191 disable_bus_clk:
1192 	clk_disable_unprepare(sai->bus_clk);
1193 
1194 	return ret;
1195 }
1196 #endif /* CONFIG_PM */
1197 
1198 static const struct dev_pm_ops fsl_sai_pm_ops = {
1199 	SET_RUNTIME_PM_OPS(fsl_sai_runtime_suspend,
1200 			   fsl_sai_runtime_resume, NULL)
1201 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1202 				pm_runtime_force_resume)
1203 };
1204 
1205 static struct platform_driver fsl_sai_driver = {
1206 	.probe = fsl_sai_probe,
1207 	.remove = fsl_sai_remove,
1208 	.driver = {
1209 		.name = "fsl-sai",
1210 		.pm = &fsl_sai_pm_ops,
1211 		.of_match_table = fsl_sai_ids,
1212 	},
1213 };
1214 module_platform_driver(fsl_sai_driver);
1215 
1216 MODULE_DESCRIPTION("Freescale Soc SAI Interface");
1217 MODULE_AUTHOR("Xiubo Li, <Li.Xiubo@freescale.com>");
1218 MODULE_ALIAS("platform:fsl-sai");
1219 MODULE_LICENSE("GPL");
1220