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