• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Freescale ALSA SoC Digital Audio Interface (SAI) driver.
3  *
4  * Copyright 2012-2015 Freescale Semiconductor, Inc.
5  *
6  * This program is free software, you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation, either version 2 of the License, or(at your
9  * option) any later version.
10  *
11  */
12 
13 #include <linux/clk.h>
14 #include <linux/delay.h>
15 #include <linux/dmaengine.h>
16 #include <linux/module.h>
17 #include <linux/of_address.h>
18 #include <linux/regmap.h>
19 #include <linux/slab.h>
20 #include <sound/core.h>
21 #include <sound/dmaengine_pcm.h>
22 #include <sound/pcm_params.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 
fsl_sai_isr(int irq,void * devid)41 static irqreturn_t fsl_sai_isr(int irq, void *devid)
42 {
43 	struct fsl_sai *sai = (struct fsl_sai *)devid;
44 	struct device *dev = &sai->pdev->dev;
45 	u32 flags, xcsr, mask;
46 	bool irq_none = true;
47 
48 	/*
49 	 * Both IRQ status bits and IRQ mask bits are in the xCSR but
50 	 * different shifts. And we here create a mask only for those
51 	 * IRQs that we activated.
52 	 */
53 	mask = (FSL_SAI_FLAGS >> FSL_SAI_CSR_xIE_SHIFT) << FSL_SAI_CSR_xF_SHIFT;
54 
55 	/* Tx IRQ */
56 	regmap_read(sai->regmap, FSL_SAI_TCSR, &xcsr);
57 	flags = xcsr & mask;
58 
59 	if (flags)
60 		irq_none = false;
61 	else
62 		goto irq_rx;
63 
64 	if (flags & FSL_SAI_CSR_WSF)
65 		dev_dbg(dev, "isr: Start of Tx word detected\n");
66 
67 	if (flags & FSL_SAI_CSR_SEF)
68 		dev_warn(dev, "isr: Tx Frame sync error detected\n");
69 
70 	if (flags & FSL_SAI_CSR_FEF) {
71 		dev_warn(dev, "isr: Transmit underrun detected\n");
72 		/* FIFO reset for safety */
73 		xcsr |= FSL_SAI_CSR_FR;
74 	}
75 
76 	if (flags & FSL_SAI_CSR_FWF)
77 		dev_dbg(dev, "isr: Enabled transmit FIFO is empty\n");
78 
79 	if (flags & FSL_SAI_CSR_FRF)
80 		dev_dbg(dev, "isr: Transmit FIFO watermark has been reached\n");
81 
82 	flags &= FSL_SAI_CSR_xF_W_MASK;
83 	xcsr &= ~FSL_SAI_CSR_xF_MASK;
84 
85 	if (flags)
86 		regmap_write(sai->regmap, FSL_SAI_TCSR, flags | xcsr);
87 
88 irq_rx:
89 	/* Rx IRQ */
90 	regmap_read(sai->regmap, FSL_SAI_RCSR, &xcsr);
91 	flags = xcsr & mask;
92 
93 	if (flags)
94 		irq_none = false;
95 	else
96 		goto out;
97 
98 	if (flags & FSL_SAI_CSR_WSF)
99 		dev_dbg(dev, "isr: Start of Rx word detected\n");
100 
101 	if (flags & FSL_SAI_CSR_SEF)
102 		dev_warn(dev, "isr: Rx Frame sync error detected\n");
103 
104 	if (flags & FSL_SAI_CSR_FEF) {
105 		dev_warn(dev, "isr: Receive overflow detected\n");
106 		/* FIFO reset for safety */
107 		xcsr |= FSL_SAI_CSR_FR;
108 	}
109 
110 	if (flags & FSL_SAI_CSR_FWF)
111 		dev_dbg(dev, "isr: Enabled receive FIFO is full\n");
112 
113 	if (flags & FSL_SAI_CSR_FRF)
114 		dev_dbg(dev, "isr: Receive FIFO watermark has been reached\n");
115 
116 	flags &= FSL_SAI_CSR_xF_W_MASK;
117 	xcsr &= ~FSL_SAI_CSR_xF_MASK;
118 
119 	if (flags)
120 		regmap_write(sai->regmap, FSL_SAI_RCSR, flags | xcsr);
121 
122 out:
123 	if (irq_none)
124 		return IRQ_NONE;
125 	else
126 		return IRQ_HANDLED;
127 }
128 
fsl_sai_set_dai_sysclk_tr(struct snd_soc_dai * cpu_dai,int clk_id,unsigned int freq,int fsl_dir)129 static int fsl_sai_set_dai_sysclk_tr(struct snd_soc_dai *cpu_dai,
130 		int clk_id, unsigned int freq, int fsl_dir)
131 {
132 	struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
133 	bool tx = fsl_dir == FSL_FMT_TRANSMITTER;
134 	u32 val_cr2 = 0;
135 
136 	switch (clk_id) {
137 	case FSL_SAI_CLK_BUS:
138 		val_cr2 |= FSL_SAI_CR2_MSEL_BUS;
139 		break;
140 	case FSL_SAI_CLK_MAST1:
141 		val_cr2 |= FSL_SAI_CR2_MSEL_MCLK1;
142 		break;
143 	case FSL_SAI_CLK_MAST2:
144 		val_cr2 |= FSL_SAI_CR2_MSEL_MCLK2;
145 		break;
146 	case FSL_SAI_CLK_MAST3:
147 		val_cr2 |= FSL_SAI_CR2_MSEL_MCLK3;
148 		break;
149 	default:
150 		return -EINVAL;
151 	}
152 
153 	regmap_update_bits(sai->regmap, FSL_SAI_xCR2(tx),
154 			   FSL_SAI_CR2_MSEL_MASK, val_cr2);
155 
156 	return 0;
157 }
158 
fsl_sai_set_dai_sysclk(struct snd_soc_dai * cpu_dai,int clk_id,unsigned int freq,int dir)159 static int fsl_sai_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
160 		int clk_id, unsigned int freq, int dir)
161 {
162 	int ret;
163 
164 	if (dir == SND_SOC_CLOCK_IN)
165 		return 0;
166 
167 	ret = fsl_sai_set_dai_sysclk_tr(cpu_dai, clk_id, freq,
168 					FSL_FMT_TRANSMITTER);
169 	if (ret) {
170 		dev_err(cpu_dai->dev, "Cannot set tx sysclk: %d\n", ret);
171 		return ret;
172 	}
173 
174 	ret = fsl_sai_set_dai_sysclk_tr(cpu_dai, clk_id, freq,
175 					FSL_FMT_RECEIVER);
176 	if (ret)
177 		dev_err(cpu_dai->dev, "Cannot set rx sysclk: %d\n", ret);
178 
179 	return ret;
180 }
181 
fsl_sai_set_dai_fmt_tr(struct snd_soc_dai * cpu_dai,unsigned int fmt,int fsl_dir)182 static int fsl_sai_set_dai_fmt_tr(struct snd_soc_dai *cpu_dai,
183 				unsigned int fmt, int fsl_dir)
184 {
185 	struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
186 	bool tx = fsl_dir == FSL_FMT_TRANSMITTER;
187 	u32 val_cr2 = 0, val_cr4 = 0;
188 
189 	if (!sai->is_lsb_first)
190 		val_cr4 |= FSL_SAI_CR4_MF;
191 
192 	/* DAI mode */
193 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
194 	case SND_SOC_DAIFMT_I2S:
195 		/*
196 		 * Frame low, 1clk before data, one word length for frame sync,
197 		 * frame sync starts one serial clock cycle earlier,
198 		 * that is, together with the last bit of the previous
199 		 * data word.
200 		 */
201 		val_cr2 |= FSL_SAI_CR2_BCP;
202 		val_cr4 |= FSL_SAI_CR4_FSE | FSL_SAI_CR4_FSP;
203 		break;
204 	case SND_SOC_DAIFMT_LEFT_J:
205 		/*
206 		 * Frame high, one word length for frame sync,
207 		 * frame sync asserts with the first bit of the frame.
208 		 */
209 		val_cr2 |= FSL_SAI_CR2_BCP;
210 		break;
211 	case SND_SOC_DAIFMT_DSP_A:
212 		/*
213 		 * Frame high, 1clk before data, one bit for frame sync,
214 		 * frame sync starts one serial clock cycle earlier,
215 		 * that is, together with the last bit of the previous
216 		 * data word.
217 		 */
218 		val_cr2 |= FSL_SAI_CR2_BCP;
219 		val_cr4 |= FSL_SAI_CR4_FSE;
220 		sai->is_dsp_mode = true;
221 		break;
222 	case SND_SOC_DAIFMT_DSP_B:
223 		/*
224 		 * Frame high, one bit for frame sync,
225 		 * frame sync asserts with the first bit of the frame.
226 		 */
227 		val_cr2 |= FSL_SAI_CR2_BCP;
228 		sai->is_dsp_mode = true;
229 		break;
230 	case SND_SOC_DAIFMT_RIGHT_J:
231 		/* To be done */
232 	default:
233 		return -EINVAL;
234 	}
235 
236 	/* DAI clock inversion */
237 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
238 	case SND_SOC_DAIFMT_IB_IF:
239 		/* Invert both clocks */
240 		val_cr2 ^= FSL_SAI_CR2_BCP;
241 		val_cr4 ^= FSL_SAI_CR4_FSP;
242 		break;
243 	case SND_SOC_DAIFMT_IB_NF:
244 		/* Invert bit clock */
245 		val_cr2 ^= FSL_SAI_CR2_BCP;
246 		break;
247 	case SND_SOC_DAIFMT_NB_IF:
248 		/* Invert frame clock */
249 		val_cr4 ^= FSL_SAI_CR4_FSP;
250 		break;
251 	case SND_SOC_DAIFMT_NB_NF:
252 		/* Nothing to do for both normal cases */
253 		break;
254 	default:
255 		return -EINVAL;
256 	}
257 
258 	/* DAI clock master masks */
259 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
260 	case SND_SOC_DAIFMT_CBS_CFS:
261 		val_cr2 |= FSL_SAI_CR2_BCD_MSTR;
262 		val_cr4 |= FSL_SAI_CR4_FSD_MSTR;
263 		sai->is_slave_mode = false;
264 		break;
265 	case SND_SOC_DAIFMT_CBM_CFM:
266 		sai->is_slave_mode = true;
267 		break;
268 	case SND_SOC_DAIFMT_CBS_CFM:
269 		val_cr2 |= FSL_SAI_CR2_BCD_MSTR;
270 		sai->is_slave_mode = false;
271 		break;
272 	case SND_SOC_DAIFMT_CBM_CFS:
273 		val_cr4 |= FSL_SAI_CR4_FSD_MSTR;
274 		sai->is_slave_mode = true;
275 		break;
276 	default:
277 		return -EINVAL;
278 	}
279 
280 	regmap_update_bits(sai->regmap, FSL_SAI_xCR2(tx),
281 			   FSL_SAI_CR2_BCP | FSL_SAI_CR2_BCD_MSTR, val_cr2);
282 	regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx),
283 			   FSL_SAI_CR4_MF | FSL_SAI_CR4_FSE |
284 			   FSL_SAI_CR4_FSP | FSL_SAI_CR4_FSD_MSTR, val_cr4);
285 
286 	return 0;
287 }
288 
fsl_sai_set_dai_fmt(struct snd_soc_dai * cpu_dai,unsigned int fmt)289 static int fsl_sai_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
290 {
291 	int ret;
292 
293 	ret = fsl_sai_set_dai_fmt_tr(cpu_dai, fmt, FSL_FMT_TRANSMITTER);
294 	if (ret) {
295 		dev_err(cpu_dai->dev, "Cannot set tx format: %d\n", ret);
296 		return ret;
297 	}
298 
299 	ret = fsl_sai_set_dai_fmt_tr(cpu_dai, fmt, FSL_FMT_RECEIVER);
300 	if (ret)
301 		dev_err(cpu_dai->dev, "Cannot set rx format: %d\n", ret);
302 
303 	return ret;
304 }
305 
fsl_sai_set_bclk(struct snd_soc_dai * dai,bool tx,u32 freq)306 static int fsl_sai_set_bclk(struct snd_soc_dai *dai, bool tx, u32 freq)
307 {
308 	struct fsl_sai *sai = snd_soc_dai_get_drvdata(dai);
309 	unsigned long clk_rate;
310 	u32 savediv = 0, ratio, savesub = freq;
311 	u32 id;
312 	int ret = 0;
313 
314 	/* Don't apply to slave mode */
315 	if (sai->is_slave_mode)
316 		return 0;
317 
318 	for (id = 0; id < FSL_SAI_MCLK_MAX; id++) {
319 		clk_rate = clk_get_rate(sai->mclk_clk[id]);
320 		if (!clk_rate)
321 			continue;
322 
323 		ratio = clk_rate / freq;
324 
325 		ret = clk_rate - ratio * freq;
326 
327 		/*
328 		 * Drop the source that can not be
329 		 * divided into the required rate.
330 		 */
331 		if (ret != 0 && clk_rate / ret < 1000)
332 			continue;
333 
334 		dev_dbg(dai->dev,
335 			"ratio %d for freq %dHz based on clock %ldHz\n",
336 			ratio, freq, clk_rate);
337 
338 		if (ratio % 2 == 0 && ratio >= 2 && ratio <= 512)
339 			ratio /= 2;
340 		else
341 			continue;
342 
343 		if (ret < savesub) {
344 			savediv = ratio;
345 			sai->mclk_id[tx] = id;
346 			savesub = ret;
347 		}
348 
349 		if (ret == 0)
350 			break;
351 	}
352 
353 	if (savediv == 0) {
354 		dev_err(dai->dev, "failed to derive required %cx rate: %d\n",
355 				tx ? 'T' : 'R', freq);
356 		return -EINVAL;
357 	}
358 
359 	if ((tx && sai->synchronous[TX]) || (!tx && !sai->synchronous[RX])) {
360 		regmap_update_bits(sai->regmap, FSL_SAI_RCR2,
361 				   FSL_SAI_CR2_MSEL_MASK,
362 				   FSL_SAI_CR2_MSEL(sai->mclk_id[tx]));
363 		regmap_update_bits(sai->regmap, FSL_SAI_RCR2,
364 				   FSL_SAI_CR2_DIV_MASK, savediv - 1);
365 	} else {
366 		regmap_update_bits(sai->regmap, FSL_SAI_TCR2,
367 				   FSL_SAI_CR2_MSEL_MASK,
368 				   FSL_SAI_CR2_MSEL(sai->mclk_id[tx]));
369 		regmap_update_bits(sai->regmap, FSL_SAI_TCR2,
370 				   FSL_SAI_CR2_DIV_MASK, savediv - 1);
371 	}
372 
373 	dev_dbg(dai->dev, "best fit: clock id=%d, div=%d, deviation =%d\n",
374 			sai->mclk_id[tx], savediv, savesub);
375 
376 	return 0;
377 }
378 
fsl_sai_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * cpu_dai)379 static int fsl_sai_hw_params(struct snd_pcm_substream *substream,
380 		struct snd_pcm_hw_params *params,
381 		struct snd_soc_dai *cpu_dai)
382 {
383 	struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
384 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
385 	unsigned int channels = params_channels(params);
386 	u32 word_width = snd_pcm_format_width(params_format(params));
387 	u32 val_cr4 = 0, val_cr5 = 0;
388 	int ret;
389 
390 	if (!sai->is_slave_mode) {
391 		ret = fsl_sai_set_bclk(cpu_dai, tx,
392 			2 * word_width * params_rate(params));
393 		if (ret)
394 			return ret;
395 
396 		/* Do not enable the clock if it is already enabled */
397 		if (!(sai->mclk_streams & BIT(substream->stream))) {
398 			ret = clk_prepare_enable(sai->mclk_clk[sai->mclk_id[tx]]);
399 			if (ret)
400 				return ret;
401 
402 			sai->mclk_streams |= BIT(substream->stream);
403 		}
404 
405 	}
406 
407 	if (!sai->is_dsp_mode)
408 		val_cr4 |= FSL_SAI_CR4_SYWD(word_width);
409 
410 	val_cr5 |= FSL_SAI_CR5_WNW(word_width);
411 	val_cr5 |= FSL_SAI_CR5_W0W(word_width);
412 
413 	if (sai->is_lsb_first)
414 		val_cr5 |= FSL_SAI_CR5_FBT(0);
415 	else
416 		val_cr5 |= FSL_SAI_CR5_FBT(word_width - 1);
417 
418 	val_cr4 |= FSL_SAI_CR4_FRSZ(channels);
419 
420 	regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx),
421 			   FSL_SAI_CR4_SYWD_MASK | FSL_SAI_CR4_FRSZ_MASK,
422 			   val_cr4);
423 	regmap_update_bits(sai->regmap, FSL_SAI_xCR5(tx),
424 			   FSL_SAI_CR5_WNW_MASK | FSL_SAI_CR5_W0W_MASK |
425 			   FSL_SAI_CR5_FBT_MASK, val_cr5);
426 	regmap_write(sai->regmap, FSL_SAI_xMR(tx), ~0UL - ((1 << channels) - 1));
427 
428 	return 0;
429 }
430 
fsl_sai_hw_free(struct snd_pcm_substream * substream,struct snd_soc_dai * cpu_dai)431 static int fsl_sai_hw_free(struct snd_pcm_substream *substream,
432 		struct snd_soc_dai *cpu_dai)
433 {
434 	struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
435 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
436 
437 	if (!sai->is_slave_mode &&
438 			sai->mclk_streams & BIT(substream->stream)) {
439 		clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[tx]]);
440 		sai->mclk_streams &= ~BIT(substream->stream);
441 	}
442 
443 	return 0;
444 }
445 
446 
fsl_sai_trigger(struct snd_pcm_substream * substream,int cmd,struct snd_soc_dai * cpu_dai)447 static int fsl_sai_trigger(struct snd_pcm_substream *substream, int cmd,
448 		struct snd_soc_dai *cpu_dai)
449 {
450 	struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
451 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
452 	u32 xcsr, count = 100;
453 
454 	/*
455 	 * Asynchronous mode: Clear SYNC for both Tx and Rx.
456 	 * Rx sync with Tx clocks: Clear SYNC for Tx, set it for Rx.
457 	 * Tx sync with Rx clocks: Clear SYNC for Rx, set it for Tx.
458 	 */
459 	regmap_update_bits(sai->regmap, FSL_SAI_TCR2, FSL_SAI_CR2_SYNC,
460 		           sai->synchronous[TX] ? FSL_SAI_CR2_SYNC : 0);
461 	regmap_update_bits(sai->regmap, FSL_SAI_RCR2, FSL_SAI_CR2_SYNC,
462 			   sai->synchronous[RX] ? FSL_SAI_CR2_SYNC : 0);
463 
464 	/*
465 	 * It is recommended that the transmitter is the last enabled
466 	 * and the first disabled.
467 	 */
468 	switch (cmd) {
469 	case SNDRV_PCM_TRIGGER_START:
470 	case SNDRV_PCM_TRIGGER_RESUME:
471 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
472 		regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx),
473 				   FSL_SAI_CSR_FRDE, FSL_SAI_CSR_FRDE);
474 
475 		regmap_update_bits(sai->regmap, FSL_SAI_RCSR,
476 				   FSL_SAI_CSR_TERE, FSL_SAI_CSR_TERE);
477 		regmap_update_bits(sai->regmap, FSL_SAI_TCSR,
478 				   FSL_SAI_CSR_TERE, FSL_SAI_CSR_TERE);
479 
480 		regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx),
481 				   FSL_SAI_CSR_xIE_MASK, FSL_SAI_FLAGS);
482 		break;
483 	case SNDRV_PCM_TRIGGER_STOP:
484 	case SNDRV_PCM_TRIGGER_SUSPEND:
485 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
486 		regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx),
487 				   FSL_SAI_CSR_FRDE, 0);
488 		regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx),
489 				   FSL_SAI_CSR_xIE_MASK, 0);
490 
491 		/* Check if the opposite FRDE is also disabled */
492 		regmap_read(sai->regmap, FSL_SAI_xCSR(!tx), &xcsr);
493 		if (!(xcsr & FSL_SAI_CSR_FRDE)) {
494 			/* Disable both directions and reset their FIFOs */
495 			regmap_update_bits(sai->regmap, FSL_SAI_TCSR,
496 					   FSL_SAI_CSR_TERE, 0);
497 			regmap_update_bits(sai->regmap, FSL_SAI_RCSR,
498 					   FSL_SAI_CSR_TERE, 0);
499 
500 			/* TERE will remain set till the end of current frame */
501 			do {
502 				udelay(10);
503 				regmap_read(sai->regmap, FSL_SAI_xCSR(tx), &xcsr);
504 			} while (--count && xcsr & FSL_SAI_CSR_TERE);
505 
506 			regmap_update_bits(sai->regmap, FSL_SAI_TCSR,
507 					   FSL_SAI_CSR_FR, FSL_SAI_CSR_FR);
508 			regmap_update_bits(sai->regmap, FSL_SAI_RCSR,
509 					   FSL_SAI_CSR_FR, FSL_SAI_CSR_FR);
510 
511 			/*
512 			 * For sai master mode, after several open/close sai,
513 			 * there will be no frame clock, and can't recover
514 			 * anymore. Add software reset to fix this issue.
515 			 * This is a hardware bug, and will be fix in the
516 			 * next sai version.
517 			 */
518 			if (!sai->is_slave_mode) {
519 				/* Software Reset for both Tx and Rx */
520 				regmap_write(sai->regmap,
521 					     FSL_SAI_TCSR, FSL_SAI_CSR_SR);
522 				regmap_write(sai->regmap,
523 					     FSL_SAI_RCSR, FSL_SAI_CSR_SR);
524 				/* Clear SR bit to finish the reset */
525 				regmap_write(sai->regmap, FSL_SAI_TCSR, 0);
526 				regmap_write(sai->regmap, FSL_SAI_RCSR, 0);
527 			}
528 		}
529 		break;
530 	default:
531 		return -EINVAL;
532 	}
533 
534 	return 0;
535 }
536 
fsl_sai_startup(struct snd_pcm_substream * substream,struct snd_soc_dai * cpu_dai)537 static int fsl_sai_startup(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 	struct device *dev = &sai->pdev->dev;
543 	int ret;
544 
545 	ret = clk_prepare_enable(sai->bus_clk);
546 	if (ret) {
547 		dev_err(dev, "failed to enable bus clock: %d\n", ret);
548 		return ret;
549 	}
550 
551 	regmap_update_bits(sai->regmap, FSL_SAI_xCR3(tx), FSL_SAI_CR3_TRCE,
552 			   FSL_SAI_CR3_TRCE);
553 
554 	ret = snd_pcm_hw_constraint_list(substream->runtime, 0,
555 			SNDRV_PCM_HW_PARAM_RATE, &fsl_sai_rate_constraints);
556 
557 	return ret;
558 }
559 
fsl_sai_shutdown(struct snd_pcm_substream * substream,struct snd_soc_dai * cpu_dai)560 static void fsl_sai_shutdown(struct snd_pcm_substream *substream,
561 		struct snd_soc_dai *cpu_dai)
562 {
563 	struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
564 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
565 
566 	regmap_update_bits(sai->regmap, FSL_SAI_xCR3(tx), FSL_SAI_CR3_TRCE, 0);
567 
568 	clk_disable_unprepare(sai->bus_clk);
569 }
570 
571 static const struct snd_soc_dai_ops fsl_sai_pcm_dai_ops = {
572 	.set_sysclk	= fsl_sai_set_dai_sysclk,
573 	.set_fmt	= fsl_sai_set_dai_fmt,
574 	.hw_params	= fsl_sai_hw_params,
575 	.hw_free	= fsl_sai_hw_free,
576 	.trigger	= fsl_sai_trigger,
577 	.startup	= fsl_sai_startup,
578 	.shutdown	= fsl_sai_shutdown,
579 };
580 
fsl_sai_dai_probe(struct snd_soc_dai * cpu_dai)581 static int fsl_sai_dai_probe(struct snd_soc_dai *cpu_dai)
582 {
583 	struct fsl_sai *sai = dev_get_drvdata(cpu_dai->dev);
584 
585 	/* Software Reset for both Tx and Rx */
586 	regmap_write(sai->regmap, FSL_SAI_TCSR, FSL_SAI_CSR_SR);
587 	regmap_write(sai->regmap, FSL_SAI_RCSR, FSL_SAI_CSR_SR);
588 	/* Clear SR bit to finish the reset */
589 	regmap_write(sai->regmap, FSL_SAI_TCSR, 0);
590 	regmap_write(sai->regmap, FSL_SAI_RCSR, 0);
591 
592 	regmap_update_bits(sai->regmap, FSL_SAI_TCR1, FSL_SAI_CR1_RFW_MASK,
593 			   FSL_SAI_MAXBURST_TX * 2);
594 	regmap_update_bits(sai->regmap, FSL_SAI_RCR1, FSL_SAI_CR1_RFW_MASK,
595 			   FSL_SAI_MAXBURST_RX - 1);
596 
597 	snd_soc_dai_init_dma_data(cpu_dai, &sai->dma_params_tx,
598 				&sai->dma_params_rx);
599 
600 	snd_soc_dai_set_drvdata(cpu_dai, sai);
601 
602 	return 0;
603 }
604 
605 static struct snd_soc_dai_driver fsl_sai_dai = {
606 	.probe = fsl_sai_dai_probe,
607 	.playback = {
608 		.stream_name = "CPU-Playback",
609 		.channels_min = 1,
610 		.channels_max = 2,
611 		.rate_min = 8000,
612 		.rate_max = 192000,
613 		.rates = SNDRV_PCM_RATE_KNOT,
614 		.formats = FSL_SAI_FORMATS,
615 	},
616 	.capture = {
617 		.stream_name = "CPU-Capture",
618 		.channels_min = 1,
619 		.channels_max = 2,
620 		.rate_min = 8000,
621 		.rate_max = 192000,
622 		.rates = SNDRV_PCM_RATE_KNOT,
623 		.formats = FSL_SAI_FORMATS,
624 	},
625 	.ops = &fsl_sai_pcm_dai_ops,
626 };
627 
628 static const struct snd_soc_component_driver fsl_component = {
629 	.name           = "fsl-sai",
630 };
631 
fsl_sai_readable_reg(struct device * dev,unsigned int reg)632 static bool fsl_sai_readable_reg(struct device *dev, unsigned int reg)
633 {
634 	switch (reg) {
635 	case FSL_SAI_TCSR:
636 	case FSL_SAI_TCR1:
637 	case FSL_SAI_TCR2:
638 	case FSL_SAI_TCR3:
639 	case FSL_SAI_TCR4:
640 	case FSL_SAI_TCR5:
641 	case FSL_SAI_TFR:
642 	case FSL_SAI_TMR:
643 	case FSL_SAI_RCSR:
644 	case FSL_SAI_RCR1:
645 	case FSL_SAI_RCR2:
646 	case FSL_SAI_RCR3:
647 	case FSL_SAI_RCR4:
648 	case FSL_SAI_RCR5:
649 	case FSL_SAI_RDR:
650 	case FSL_SAI_RFR:
651 	case FSL_SAI_RMR:
652 		return true;
653 	default:
654 		return false;
655 	}
656 }
657 
fsl_sai_volatile_reg(struct device * dev,unsigned int reg)658 static bool fsl_sai_volatile_reg(struct device *dev, unsigned int reg)
659 {
660 	switch (reg) {
661 	case FSL_SAI_TCSR:
662 	case FSL_SAI_RCSR:
663 	case FSL_SAI_TFR:
664 	case FSL_SAI_RFR:
665 	case FSL_SAI_TDR:
666 	case FSL_SAI_RDR:
667 		return true;
668 	default:
669 		return false;
670 	}
671 
672 }
673 
fsl_sai_writeable_reg(struct device * dev,unsigned int reg)674 static bool fsl_sai_writeable_reg(struct device *dev, unsigned int reg)
675 {
676 	switch (reg) {
677 	case FSL_SAI_TCSR:
678 	case FSL_SAI_TCR1:
679 	case FSL_SAI_TCR2:
680 	case FSL_SAI_TCR3:
681 	case FSL_SAI_TCR4:
682 	case FSL_SAI_TCR5:
683 	case FSL_SAI_TDR:
684 	case FSL_SAI_TMR:
685 	case FSL_SAI_RCSR:
686 	case FSL_SAI_RCR1:
687 	case FSL_SAI_RCR2:
688 	case FSL_SAI_RCR3:
689 	case FSL_SAI_RCR4:
690 	case FSL_SAI_RCR5:
691 	case FSL_SAI_RMR:
692 		return true;
693 	default:
694 		return false;
695 	}
696 }
697 
698 static const struct regmap_config fsl_sai_regmap_config = {
699 	.reg_bits = 32,
700 	.reg_stride = 4,
701 	.val_bits = 32,
702 
703 	.max_register = FSL_SAI_RMR,
704 	.readable_reg = fsl_sai_readable_reg,
705 	.volatile_reg = fsl_sai_volatile_reg,
706 	.writeable_reg = fsl_sai_writeable_reg,
707 	.cache_type = REGCACHE_FLAT,
708 };
709 
fsl_sai_probe(struct platform_device * pdev)710 static int fsl_sai_probe(struct platform_device *pdev)
711 {
712 	struct device_node *np = pdev->dev.of_node;
713 	struct fsl_sai *sai;
714 	struct resource *res;
715 	void __iomem *base;
716 	char tmp[8];
717 	int irq, ret, i;
718 
719 	sai = devm_kzalloc(&pdev->dev, sizeof(*sai), GFP_KERNEL);
720 	if (!sai)
721 		return -ENOMEM;
722 
723 	sai->pdev = pdev;
724 
725 	if (of_device_is_compatible(pdev->dev.of_node, "fsl,imx6sx-sai"))
726 		sai->sai_on_imx = true;
727 
728 	sai->is_lsb_first = of_property_read_bool(np, "lsb-first");
729 
730 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
731 	base = devm_ioremap_resource(&pdev->dev, res);
732 	if (IS_ERR(base))
733 		return PTR_ERR(base);
734 
735 	sai->regmap = devm_regmap_init_mmio_clk(&pdev->dev,
736 			"bus", base, &fsl_sai_regmap_config);
737 
738 	/* Compatible with old DTB cases */
739 	if (IS_ERR(sai->regmap))
740 		sai->regmap = devm_regmap_init_mmio_clk(&pdev->dev,
741 				"sai", base, &fsl_sai_regmap_config);
742 	if (IS_ERR(sai->regmap)) {
743 		dev_err(&pdev->dev, "regmap init failed\n");
744 		return PTR_ERR(sai->regmap);
745 	}
746 
747 	/* No error out for old DTB cases but only mark the clock NULL */
748 	sai->bus_clk = devm_clk_get(&pdev->dev, "bus");
749 	if (IS_ERR(sai->bus_clk)) {
750 		dev_err(&pdev->dev, "failed to get bus clock: %ld\n",
751 				PTR_ERR(sai->bus_clk));
752 		sai->bus_clk = NULL;
753 	}
754 
755 	sai->mclk_clk[0] = sai->bus_clk;
756 	for (i = 1; i < FSL_SAI_MCLK_MAX; i++) {
757 		sprintf(tmp, "mclk%d", i);
758 		sai->mclk_clk[i] = devm_clk_get(&pdev->dev, tmp);
759 		if (IS_ERR(sai->mclk_clk[i])) {
760 			dev_err(&pdev->dev, "failed to get mclk%d clock: %ld\n",
761 					i + 1, PTR_ERR(sai->mclk_clk[i]));
762 			sai->mclk_clk[i] = NULL;
763 		}
764 	}
765 
766 	irq = platform_get_irq(pdev, 0);
767 	if (irq < 0) {
768 		dev_err(&pdev->dev, "no irq for node %s\n", pdev->name);
769 		return irq;
770 	}
771 
772 	ret = devm_request_irq(&pdev->dev, irq, fsl_sai_isr, 0, np->name, sai);
773 	if (ret) {
774 		dev_err(&pdev->dev, "failed to claim irq %u\n", irq);
775 		return ret;
776 	}
777 
778 	/* Sync Tx with Rx as default by following old DT binding */
779 	sai->synchronous[RX] = true;
780 	sai->synchronous[TX] = false;
781 	fsl_sai_dai.symmetric_rates = 1;
782 	fsl_sai_dai.symmetric_channels = 1;
783 	fsl_sai_dai.symmetric_samplebits = 1;
784 
785 	if (of_find_property(np, "fsl,sai-synchronous-rx", NULL) &&
786 	    of_find_property(np, "fsl,sai-asynchronous", NULL)) {
787 		/* error out if both synchronous and asynchronous are present */
788 		dev_err(&pdev->dev, "invalid binding for synchronous mode\n");
789 		return -EINVAL;
790 	}
791 
792 	if (of_find_property(np, "fsl,sai-synchronous-rx", NULL)) {
793 		/* Sync Rx with Tx */
794 		sai->synchronous[RX] = false;
795 		sai->synchronous[TX] = true;
796 	} else if (of_find_property(np, "fsl,sai-asynchronous", NULL)) {
797 		/* Discard all settings for asynchronous mode */
798 		sai->synchronous[RX] = false;
799 		sai->synchronous[TX] = false;
800 		fsl_sai_dai.symmetric_rates = 0;
801 		fsl_sai_dai.symmetric_channels = 0;
802 		fsl_sai_dai.symmetric_samplebits = 0;
803 	}
804 
805 	sai->dma_params_rx.addr = res->start + FSL_SAI_RDR;
806 	sai->dma_params_tx.addr = res->start + FSL_SAI_TDR;
807 	sai->dma_params_rx.maxburst = FSL_SAI_MAXBURST_RX;
808 	sai->dma_params_tx.maxburst = FSL_SAI_MAXBURST_TX;
809 
810 	platform_set_drvdata(pdev, sai);
811 
812 	ret = devm_snd_soc_register_component(&pdev->dev, &fsl_component,
813 			&fsl_sai_dai, 1);
814 	if (ret)
815 		return ret;
816 
817 	if (sai->sai_on_imx)
818 		return imx_pcm_dma_init(pdev, IMX_SAI_DMABUF_SIZE);
819 	else
820 		return devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
821 }
822 
823 static const struct of_device_id fsl_sai_ids[] = {
824 	{ .compatible = "fsl,vf610-sai", },
825 	{ .compatible = "fsl,imx6sx-sai", },
826 	{ /* sentinel */ }
827 };
828 MODULE_DEVICE_TABLE(of, fsl_sai_ids);
829 
830 #ifdef CONFIG_PM_SLEEP
fsl_sai_suspend(struct device * dev)831 static int fsl_sai_suspend(struct device *dev)
832 {
833 	struct fsl_sai *sai = dev_get_drvdata(dev);
834 
835 	regcache_cache_only(sai->regmap, true);
836 	regcache_mark_dirty(sai->regmap);
837 
838 	return 0;
839 }
840 
fsl_sai_resume(struct device * dev)841 static int fsl_sai_resume(struct device *dev)
842 {
843 	struct fsl_sai *sai = dev_get_drvdata(dev);
844 
845 	regcache_cache_only(sai->regmap, false);
846 	regmap_write(sai->regmap, FSL_SAI_TCSR, FSL_SAI_CSR_SR);
847 	regmap_write(sai->regmap, FSL_SAI_RCSR, FSL_SAI_CSR_SR);
848 	msleep(1);
849 	regmap_write(sai->regmap, FSL_SAI_TCSR, 0);
850 	regmap_write(sai->regmap, FSL_SAI_RCSR, 0);
851 	return regcache_sync(sai->regmap);
852 }
853 #endif /* CONFIG_PM_SLEEP */
854 
855 static const struct dev_pm_ops fsl_sai_pm_ops = {
856 	SET_SYSTEM_SLEEP_PM_OPS(fsl_sai_suspend, fsl_sai_resume)
857 };
858 
859 static struct platform_driver fsl_sai_driver = {
860 	.probe = fsl_sai_probe,
861 	.driver = {
862 		.name = "fsl-sai",
863 		.pm = &fsl_sai_pm_ops,
864 		.of_match_table = fsl_sai_ids,
865 	},
866 };
867 module_platform_driver(fsl_sai_driver);
868 
869 MODULE_DESCRIPTION("Freescale Soc SAI Interface");
870 MODULE_AUTHOR("Xiubo Li, <Li.Xiubo@freescale.com>");
871 MODULE_ALIAS("platform:fsl-sai");
872 MODULE_LICENSE("GPL");
873