1 // SPDX-License-Identifier: GPL-2.0+
3 // Freescale ALSA SoC Digital Audio Interface (SAI) driver.
5 // Copyright 2012-2015 Freescale Semiconductor, Inc.
8 #include <linux/delay.h>
9 #include <linux/dmaengine.h>
10 #include <linux/module.h>
11 #include <linux/of_address.h>
12 #include <linux/regmap.h>
13 #include <linux/slab.h>
14 #include <linux/time.h>
15 #include <sound/core.h>
16 #include <sound/dmaengine_pcm.h>
17 #include <sound/pcm_params.h>
18 #include <linux/mfd/syscon.h>
19 #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
24 #define FSL_SAI_FLAGS (FSL_SAI_CSR_SEIE |\
27 static const unsigned int fsl_sai_rates[] = {
28 8000, 11025, 12000, 16000, 22050,
29 24000, 32000, 44100, 48000, 64000,
30 88200, 96000, 176400, 192000
33 static const struct snd_pcm_hw_constraint_list fsl_sai_rate_constraints = {
34 .count = ARRAY_SIZE(fsl_sai_rates),
35 .list = fsl_sai_rates,
38 static irqreturn_t fsl_sai_isr(int irq, void *devid)
40 struct fsl_sai *sai = (struct fsl_sai *)devid;
41 struct device *dev = &sai->pdev->dev;
42 u32 flags, xcsr, mask;
46 * Both IRQ status bits and IRQ mask bits are in the xCSR but
47 * different shifts. And we here create a mask only for those
48 * IRQs that we activated.
50 mask = (FSL_SAI_FLAGS >> FSL_SAI_CSR_xIE_SHIFT) << FSL_SAI_CSR_xF_SHIFT;
53 regmap_read(sai->regmap, FSL_SAI_TCSR, &xcsr);
61 if (flags & FSL_SAI_CSR_WSF)
62 dev_dbg(dev, "isr: Start of Tx word detected\n");
64 if (flags & FSL_SAI_CSR_SEF)
65 dev_warn(dev, "isr: Tx Frame sync error detected\n");
67 if (flags & FSL_SAI_CSR_FEF) {
68 dev_warn(dev, "isr: Transmit underrun detected\n");
69 /* FIFO reset for safety */
70 xcsr |= FSL_SAI_CSR_FR;
73 if (flags & FSL_SAI_CSR_FWF)
74 dev_dbg(dev, "isr: Enabled transmit FIFO is empty\n");
76 if (flags & FSL_SAI_CSR_FRF)
77 dev_dbg(dev, "isr: Transmit FIFO watermark has been reached\n");
79 flags &= FSL_SAI_CSR_xF_W_MASK;
80 xcsr &= ~FSL_SAI_CSR_xF_MASK;
83 regmap_write(sai->regmap, FSL_SAI_TCSR, flags | xcsr);
87 regmap_read(sai->regmap, FSL_SAI_RCSR, &xcsr);
95 if (flags & FSL_SAI_CSR_WSF)
96 dev_dbg(dev, "isr: Start of Rx word detected\n");
98 if (flags & FSL_SAI_CSR_SEF)
99 dev_warn(dev, "isr: Rx Frame sync error detected\n");
101 if (flags & FSL_SAI_CSR_FEF) {
102 dev_warn(dev, "isr: Receive overflow detected\n");
103 /* FIFO reset for safety */
104 xcsr |= FSL_SAI_CSR_FR;
107 if (flags & FSL_SAI_CSR_FWF)
108 dev_dbg(dev, "isr: Enabled receive FIFO is full\n");
110 if (flags & FSL_SAI_CSR_FRF)
111 dev_dbg(dev, "isr: Receive FIFO watermark has been reached\n");
113 flags &= FSL_SAI_CSR_xF_W_MASK;
114 xcsr &= ~FSL_SAI_CSR_xF_MASK;
117 regmap_write(sai->regmap, FSL_SAI_RCSR, flags | xcsr);
126 static int fsl_sai_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai, u32 tx_mask,
127 u32 rx_mask, int slots, int slot_width)
129 struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
132 sai->slot_width = slot_width;
137 static int fsl_sai_set_dai_sysclk_tr(struct snd_soc_dai *cpu_dai,
138 int clk_id, unsigned int freq, int fsl_dir)
140 struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
141 bool tx = fsl_dir == FSL_FMT_TRANSMITTER;
145 case FSL_SAI_CLK_BUS:
146 val_cr2 |= FSL_SAI_CR2_MSEL_BUS;
148 case FSL_SAI_CLK_MAST1:
149 val_cr2 |= FSL_SAI_CR2_MSEL_MCLK1;
151 case FSL_SAI_CLK_MAST2:
152 val_cr2 |= FSL_SAI_CR2_MSEL_MCLK2;
154 case FSL_SAI_CLK_MAST3:
155 val_cr2 |= FSL_SAI_CR2_MSEL_MCLK3;
161 regmap_update_bits(sai->regmap, FSL_SAI_xCR2(tx),
162 FSL_SAI_CR2_MSEL_MASK, val_cr2);
167 static int fsl_sai_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
168 int clk_id, unsigned int freq, int dir)
172 if (dir == SND_SOC_CLOCK_IN)
175 ret = fsl_sai_set_dai_sysclk_tr(cpu_dai, clk_id, freq,
176 FSL_FMT_TRANSMITTER);
178 dev_err(cpu_dai->dev, "Cannot set tx sysclk: %d\n", ret);
182 ret = fsl_sai_set_dai_sysclk_tr(cpu_dai, clk_id, freq,
185 dev_err(cpu_dai->dev, "Cannot set rx sysclk: %d\n", ret);
190 static int fsl_sai_set_dai_fmt_tr(struct snd_soc_dai *cpu_dai,
191 unsigned int fmt, int fsl_dir)
193 struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
194 bool tx = fsl_dir == FSL_FMT_TRANSMITTER;
195 u32 val_cr2 = 0, val_cr4 = 0;
197 if (!sai->is_lsb_first)
198 val_cr4 |= FSL_SAI_CR4_MF;
201 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
202 case SND_SOC_DAIFMT_I2S:
204 * Frame low, 1clk before data, one word length for frame sync,
205 * frame sync starts one serial clock cycle earlier,
206 * that is, together with the last bit of the previous
209 val_cr2 |= FSL_SAI_CR2_BCP;
210 val_cr4 |= FSL_SAI_CR4_FSE | FSL_SAI_CR4_FSP;
212 case SND_SOC_DAIFMT_LEFT_J:
214 * Frame high, one word length for frame sync,
215 * frame sync asserts with the first bit of the frame.
217 val_cr2 |= FSL_SAI_CR2_BCP;
219 case SND_SOC_DAIFMT_DSP_A:
221 * Frame high, 1clk before data, one bit for frame sync,
222 * frame sync starts one serial clock cycle earlier,
223 * that is, together with the last bit of the previous
226 val_cr2 |= FSL_SAI_CR2_BCP;
227 val_cr4 |= FSL_SAI_CR4_FSE;
228 sai->is_dsp_mode = true;
230 case SND_SOC_DAIFMT_DSP_B:
232 * Frame high, one bit for frame sync,
233 * frame sync asserts with the first bit of the frame.
235 val_cr2 |= FSL_SAI_CR2_BCP;
236 sai->is_dsp_mode = true;
238 case SND_SOC_DAIFMT_RIGHT_J:
244 /* DAI clock inversion */
245 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
246 case SND_SOC_DAIFMT_IB_IF:
247 /* Invert both clocks */
248 val_cr2 ^= FSL_SAI_CR2_BCP;
249 val_cr4 ^= FSL_SAI_CR4_FSP;
251 case SND_SOC_DAIFMT_IB_NF:
252 /* Invert bit clock */
253 val_cr2 ^= FSL_SAI_CR2_BCP;
255 case SND_SOC_DAIFMT_NB_IF:
256 /* Invert frame clock */
257 val_cr4 ^= FSL_SAI_CR4_FSP;
259 case SND_SOC_DAIFMT_NB_NF:
260 /* Nothing to do for both normal cases */
266 /* DAI clock master masks */
267 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
268 case SND_SOC_DAIFMT_CBS_CFS:
269 val_cr2 |= FSL_SAI_CR2_BCD_MSTR;
270 val_cr4 |= FSL_SAI_CR4_FSD_MSTR;
272 case SND_SOC_DAIFMT_CBM_CFM:
273 sai->is_slave_mode = true;
275 case SND_SOC_DAIFMT_CBS_CFM:
276 val_cr2 |= FSL_SAI_CR2_BCD_MSTR;
278 case SND_SOC_DAIFMT_CBM_CFS:
279 val_cr4 |= FSL_SAI_CR4_FSD_MSTR;
280 sai->is_slave_mode = true;
286 regmap_update_bits(sai->regmap, FSL_SAI_xCR2(tx),
287 FSL_SAI_CR2_BCP | FSL_SAI_CR2_BCD_MSTR, val_cr2);
288 regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx),
289 FSL_SAI_CR4_MF | FSL_SAI_CR4_FSE |
290 FSL_SAI_CR4_FSP | FSL_SAI_CR4_FSD_MSTR, val_cr4);
295 static int fsl_sai_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
299 ret = fsl_sai_set_dai_fmt_tr(cpu_dai, fmt, FSL_FMT_TRANSMITTER);
301 dev_err(cpu_dai->dev, "Cannot set tx format: %d\n", ret);
305 ret = fsl_sai_set_dai_fmt_tr(cpu_dai, fmt, FSL_FMT_RECEIVER);
307 dev_err(cpu_dai->dev, "Cannot set rx format: %d\n", ret);
312 static int fsl_sai_set_bclk(struct snd_soc_dai *dai, bool tx, u32 freq)
314 struct fsl_sai *sai = snd_soc_dai_get_drvdata(dai);
315 unsigned long clk_rate;
316 u32 savediv = 0, ratio, savesub = freq;
320 /* Don't apply to slave mode */
321 if (sai->is_slave_mode)
324 for (id = 0; id < FSL_SAI_MCLK_MAX; id++) {
325 clk_rate = clk_get_rate(sai->mclk_clk[id]);
329 ratio = clk_rate / freq;
331 ret = clk_rate - ratio * freq;
334 * Drop the source that can not be
335 * divided into the required rate.
337 if (ret != 0 && clk_rate / ret < 1000)
341 "ratio %d for freq %dHz based on clock %ldHz\n",
342 ratio, freq, clk_rate);
344 if (ratio % 2 == 0 && ratio >= 2 && ratio <= 512)
351 sai->mclk_id[tx] = id;
360 dev_err(dai->dev, "failed to derive required %cx rate: %d\n",
361 tx ? 'T' : 'R', freq);
366 * 1) For Asynchronous mode, we must set RCR2 register for capture, and
367 * set TCR2 register for playback.
368 * 2) For Tx sync with Rx clock, we must set RCR2 register for playback
370 * 3) For Rx sync with Tx clock, we must set TCR2 register for playback
372 * 4) For Tx and Rx are both Synchronous with another SAI, we just
375 if ((sai->synchronous[TX] && !sai->synchronous[RX]) ||
376 (!tx && !sai->synchronous[RX])) {
377 regmap_update_bits(sai->regmap, FSL_SAI_RCR2,
378 FSL_SAI_CR2_MSEL_MASK,
379 FSL_SAI_CR2_MSEL(sai->mclk_id[tx]));
380 regmap_update_bits(sai->regmap, FSL_SAI_RCR2,
381 FSL_SAI_CR2_DIV_MASK, savediv - 1);
382 } else if ((sai->synchronous[RX] && !sai->synchronous[TX]) ||
383 (tx && !sai->synchronous[TX])) {
384 regmap_update_bits(sai->regmap, FSL_SAI_TCR2,
385 FSL_SAI_CR2_MSEL_MASK,
386 FSL_SAI_CR2_MSEL(sai->mclk_id[tx]));
387 regmap_update_bits(sai->regmap, FSL_SAI_TCR2,
388 FSL_SAI_CR2_DIV_MASK, savediv - 1);
391 dev_dbg(dai->dev, "best fit: clock id=%d, div=%d, deviation =%d\n",
392 sai->mclk_id[tx], savediv, savesub);
397 static int fsl_sai_hw_params(struct snd_pcm_substream *substream,
398 struct snd_pcm_hw_params *params,
399 struct snd_soc_dai *cpu_dai)
401 struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
402 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
403 unsigned int channels = params_channels(params);
404 u32 word_width = params_width(params);
405 u32 val_cr4 = 0, val_cr5 = 0;
406 u32 slots = (channels == 1) ? 2 : channels;
407 u32 slot_width = word_width;
414 slot_width = sai->slot_width;
416 if (!sai->is_slave_mode) {
417 ret = fsl_sai_set_bclk(cpu_dai, tx,
418 slots * slot_width * params_rate(params));
422 /* Do not enable the clock if it is already enabled */
423 if (!(sai->mclk_streams & BIT(substream->stream))) {
424 ret = clk_prepare_enable(sai->mclk_clk[sai->mclk_id[tx]]);
428 sai->mclk_streams |= BIT(substream->stream);
432 if (!sai->is_dsp_mode)
433 val_cr4 |= FSL_SAI_CR4_SYWD(slot_width);
435 val_cr5 |= FSL_SAI_CR5_WNW(slot_width);
436 val_cr5 |= FSL_SAI_CR5_W0W(slot_width);
438 if (sai->is_lsb_first)
439 val_cr5 |= FSL_SAI_CR5_FBT(0);
441 val_cr5 |= FSL_SAI_CR5_FBT(word_width - 1);
443 val_cr4 |= FSL_SAI_CR4_FRSZ(slots);
446 * For SAI master mode, when Tx(Rx) sync with Rx(Tx) clock, Rx(Tx) will
447 * generate bclk and frame clock for Tx(Rx), we should set RCR4(TCR4),
448 * RCR5(TCR5) and RMR(TMR) for playback(capture), or there will be sync
452 if (!sai->is_slave_mode) {
453 if (!sai->synchronous[TX] && sai->synchronous[RX] && !tx) {
454 regmap_update_bits(sai->regmap, FSL_SAI_TCR4,
455 FSL_SAI_CR4_SYWD_MASK | FSL_SAI_CR4_FRSZ_MASK,
457 regmap_update_bits(sai->regmap, FSL_SAI_TCR5,
458 FSL_SAI_CR5_WNW_MASK | FSL_SAI_CR5_W0W_MASK |
459 FSL_SAI_CR5_FBT_MASK, val_cr5);
460 regmap_write(sai->regmap, FSL_SAI_TMR,
461 ~0UL - ((1 << channels) - 1));
462 } else if (!sai->synchronous[RX] && sai->synchronous[TX] && tx) {
463 regmap_update_bits(sai->regmap, FSL_SAI_RCR4,
464 FSL_SAI_CR4_SYWD_MASK | FSL_SAI_CR4_FRSZ_MASK,
466 regmap_update_bits(sai->regmap, FSL_SAI_RCR5,
467 FSL_SAI_CR5_WNW_MASK | FSL_SAI_CR5_W0W_MASK |
468 FSL_SAI_CR5_FBT_MASK, val_cr5);
469 regmap_write(sai->regmap, FSL_SAI_RMR,
470 ~0UL - ((1 << channels) - 1));
474 regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx),
475 FSL_SAI_CR4_SYWD_MASK | FSL_SAI_CR4_FRSZ_MASK,
477 regmap_update_bits(sai->regmap, FSL_SAI_xCR5(tx),
478 FSL_SAI_CR5_WNW_MASK | FSL_SAI_CR5_W0W_MASK |
479 FSL_SAI_CR5_FBT_MASK, val_cr5);
480 regmap_write(sai->regmap, FSL_SAI_xMR(tx), ~0UL - ((1 << channels) - 1));
485 static int fsl_sai_hw_free(struct snd_pcm_substream *substream,
486 struct snd_soc_dai *cpu_dai)
488 struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
489 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
491 if (!sai->is_slave_mode &&
492 sai->mclk_streams & BIT(substream->stream)) {
493 clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[tx]]);
494 sai->mclk_streams &= ~BIT(substream->stream);
501 static int fsl_sai_trigger(struct snd_pcm_substream *substream, int cmd,
502 struct snd_soc_dai *cpu_dai)
504 struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
505 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
506 u32 xcsr, count = 100;
509 * Asynchronous mode: Clear SYNC for both Tx and Rx.
510 * Rx sync with Tx clocks: Clear SYNC for Tx, set it for Rx.
511 * Tx sync with Rx clocks: Clear SYNC for Rx, set it for Tx.
513 regmap_update_bits(sai->regmap, FSL_SAI_TCR2, FSL_SAI_CR2_SYNC,
514 sai->synchronous[TX] ? FSL_SAI_CR2_SYNC : 0);
515 regmap_update_bits(sai->regmap, FSL_SAI_RCR2, FSL_SAI_CR2_SYNC,
516 sai->synchronous[RX] ? FSL_SAI_CR2_SYNC : 0);
519 * It is recommended that the transmitter is the last enabled
520 * and the first disabled.
523 case SNDRV_PCM_TRIGGER_START:
524 case SNDRV_PCM_TRIGGER_RESUME:
525 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
526 regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx),
527 FSL_SAI_CSR_FRDE, FSL_SAI_CSR_FRDE);
529 regmap_update_bits(sai->regmap, FSL_SAI_RCSR,
530 FSL_SAI_CSR_TERE, FSL_SAI_CSR_TERE);
531 regmap_update_bits(sai->regmap, FSL_SAI_TCSR,
532 FSL_SAI_CSR_TERE, FSL_SAI_CSR_TERE);
534 regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx),
535 FSL_SAI_CSR_xIE_MASK, FSL_SAI_FLAGS);
537 case SNDRV_PCM_TRIGGER_STOP:
538 case SNDRV_PCM_TRIGGER_SUSPEND:
539 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
540 regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx),
541 FSL_SAI_CSR_FRDE, 0);
542 regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx),
543 FSL_SAI_CSR_xIE_MASK, 0);
545 /* Check if the opposite FRDE is also disabled */
546 regmap_read(sai->regmap, FSL_SAI_xCSR(!tx), &xcsr);
547 if (!(xcsr & FSL_SAI_CSR_FRDE)) {
548 /* Disable both directions and reset their FIFOs */
549 regmap_update_bits(sai->regmap, FSL_SAI_TCSR,
550 FSL_SAI_CSR_TERE, 0);
551 regmap_update_bits(sai->regmap, FSL_SAI_RCSR,
552 FSL_SAI_CSR_TERE, 0);
554 /* TERE will remain set till the end of current frame */
557 regmap_read(sai->regmap, FSL_SAI_xCSR(tx), &xcsr);
558 } while (--count && xcsr & FSL_SAI_CSR_TERE);
560 regmap_update_bits(sai->regmap, FSL_SAI_TCSR,
561 FSL_SAI_CSR_FR, FSL_SAI_CSR_FR);
562 regmap_update_bits(sai->regmap, FSL_SAI_RCSR,
563 FSL_SAI_CSR_FR, FSL_SAI_CSR_FR);
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
572 if (!sai->is_slave_mode) {
573 /* Software Reset for both Tx and Rx */
574 regmap_write(sai->regmap,
575 FSL_SAI_TCSR, FSL_SAI_CSR_SR);
576 regmap_write(sai->regmap,
577 FSL_SAI_RCSR, FSL_SAI_CSR_SR);
578 /* Clear SR bit to finish the reset */
579 regmap_write(sai->regmap, FSL_SAI_TCSR, 0);
580 regmap_write(sai->regmap, FSL_SAI_RCSR, 0);
591 static int fsl_sai_startup(struct snd_pcm_substream *substream,
592 struct snd_soc_dai *cpu_dai)
594 struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
595 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
596 struct device *dev = &sai->pdev->dev;
599 ret = clk_prepare_enable(sai->bus_clk);
601 dev_err(dev, "failed to enable bus clock: %d\n", ret);
605 regmap_update_bits(sai->regmap, FSL_SAI_xCR3(tx), FSL_SAI_CR3_TRCE,
608 ret = snd_pcm_hw_constraint_list(substream->runtime, 0,
609 SNDRV_PCM_HW_PARAM_RATE, &fsl_sai_rate_constraints);
614 static void fsl_sai_shutdown(struct snd_pcm_substream *substream,
615 struct snd_soc_dai *cpu_dai)
617 struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai);
618 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
620 regmap_update_bits(sai->regmap, FSL_SAI_xCR3(tx), FSL_SAI_CR3_TRCE, 0);
622 clk_disable_unprepare(sai->bus_clk);
625 static const struct snd_soc_dai_ops fsl_sai_pcm_dai_ops = {
626 .set_sysclk = fsl_sai_set_dai_sysclk,
627 .set_fmt = fsl_sai_set_dai_fmt,
628 .set_tdm_slot = fsl_sai_set_dai_tdm_slot,
629 .hw_params = fsl_sai_hw_params,
630 .hw_free = fsl_sai_hw_free,
631 .trigger = fsl_sai_trigger,
632 .startup = fsl_sai_startup,
633 .shutdown = fsl_sai_shutdown,
636 static int fsl_sai_dai_probe(struct snd_soc_dai *cpu_dai)
638 struct fsl_sai *sai = dev_get_drvdata(cpu_dai->dev);
640 /* Software Reset for both Tx and Rx */
641 regmap_write(sai->regmap, FSL_SAI_TCSR, FSL_SAI_CSR_SR);
642 regmap_write(sai->regmap, FSL_SAI_RCSR, FSL_SAI_CSR_SR);
643 /* Clear SR bit to finish the reset */
644 regmap_write(sai->regmap, FSL_SAI_TCSR, 0);
645 regmap_write(sai->regmap, FSL_SAI_RCSR, 0);
647 regmap_update_bits(sai->regmap, FSL_SAI_TCR1, FSL_SAI_CR1_RFW_MASK,
648 FSL_SAI_MAXBURST_TX * 2);
649 regmap_update_bits(sai->regmap, FSL_SAI_RCR1, FSL_SAI_CR1_RFW_MASK,
650 FSL_SAI_MAXBURST_RX - 1);
652 snd_soc_dai_init_dma_data(cpu_dai, &sai->dma_params_tx,
653 &sai->dma_params_rx);
655 snd_soc_dai_set_drvdata(cpu_dai, sai);
660 static struct snd_soc_dai_driver fsl_sai_dai = {
661 .probe = fsl_sai_dai_probe,
663 .stream_name = "CPU-Playback",
668 .rates = SNDRV_PCM_RATE_KNOT,
669 .formats = FSL_SAI_FORMATS,
672 .stream_name = "CPU-Capture",
677 .rates = SNDRV_PCM_RATE_KNOT,
678 .formats = FSL_SAI_FORMATS,
680 .ops = &fsl_sai_pcm_dai_ops,
683 static const struct snd_soc_component_driver fsl_component = {
687 static struct reg_default fsl_sai_reg_defaults[] = {
703 static bool fsl_sai_readable_reg(struct device *dev, unsigned int reg)
729 static bool fsl_sai_volatile_reg(struct device *dev, unsigned int reg)
743 static bool fsl_sai_writeable_reg(struct device *dev, unsigned int reg)
767 static const struct regmap_config fsl_sai_regmap_config = {
772 .max_register = FSL_SAI_RMR,
773 .reg_defaults = fsl_sai_reg_defaults,
774 .num_reg_defaults = ARRAY_SIZE(fsl_sai_reg_defaults),
775 .readable_reg = fsl_sai_readable_reg,
776 .volatile_reg = fsl_sai_volatile_reg,
777 .writeable_reg = fsl_sai_writeable_reg,
778 .cache_type = REGCACHE_FLAT,
781 static int fsl_sai_probe(struct platform_device *pdev)
783 struct device_node *np = pdev->dev.of_node;
786 struct resource *res;
792 sai = devm_kzalloc(&pdev->dev, sizeof(*sai), GFP_KERNEL);
798 if (of_device_is_compatible(np, "fsl,imx6sx-sai") ||
799 of_device_is_compatible(np, "fsl,imx6ul-sai"))
800 sai->sai_on_imx = true;
802 sai->is_lsb_first = of_property_read_bool(np, "lsb-first");
804 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
805 base = devm_ioremap_resource(&pdev->dev, res);
807 return PTR_ERR(base);
809 sai->regmap = devm_regmap_init_mmio_clk(&pdev->dev,
810 "bus", base, &fsl_sai_regmap_config);
812 /* Compatible with old DTB cases */
813 if (IS_ERR(sai->regmap))
814 sai->regmap = devm_regmap_init_mmio_clk(&pdev->dev,
815 "sai", base, &fsl_sai_regmap_config);
816 if (IS_ERR(sai->regmap)) {
817 dev_err(&pdev->dev, "regmap init failed\n");
818 return PTR_ERR(sai->regmap);
821 /* No error out for old DTB cases but only mark the clock NULL */
822 sai->bus_clk = devm_clk_get(&pdev->dev, "bus");
823 if (IS_ERR(sai->bus_clk)) {
824 dev_err(&pdev->dev, "failed to get bus clock: %ld\n",
825 PTR_ERR(sai->bus_clk));
829 sai->mclk_clk[0] = sai->bus_clk;
830 for (i = 1; i < FSL_SAI_MCLK_MAX; i++) {
831 sprintf(tmp, "mclk%d", i);
832 sai->mclk_clk[i] = devm_clk_get(&pdev->dev, tmp);
833 if (IS_ERR(sai->mclk_clk[i])) {
834 dev_err(&pdev->dev, "failed to get mclk%d clock: %ld\n",
835 i + 1, PTR_ERR(sai->mclk_clk[i]));
836 sai->mclk_clk[i] = NULL;
840 irq = platform_get_irq(pdev, 0);
842 dev_err(&pdev->dev, "no irq for node %s\n", pdev->name);
846 ret = devm_request_irq(&pdev->dev, irq, fsl_sai_isr, 0, np->name, sai);
848 dev_err(&pdev->dev, "failed to claim irq %u\n", irq);
852 /* Sync Tx with Rx as default by following old DT binding */
853 sai->synchronous[RX] = true;
854 sai->synchronous[TX] = false;
855 fsl_sai_dai.symmetric_rates = 1;
856 fsl_sai_dai.symmetric_channels = 1;
857 fsl_sai_dai.symmetric_samplebits = 1;
859 if (of_find_property(np, "fsl,sai-synchronous-rx", NULL) &&
860 of_find_property(np, "fsl,sai-asynchronous", NULL)) {
861 /* error out if both synchronous and asynchronous are present */
862 dev_err(&pdev->dev, "invalid binding for synchronous mode\n");
866 if (of_find_property(np, "fsl,sai-synchronous-rx", NULL)) {
867 /* Sync Rx with Tx */
868 sai->synchronous[RX] = false;
869 sai->synchronous[TX] = true;
870 } else if (of_find_property(np, "fsl,sai-asynchronous", NULL)) {
871 /* Discard all settings for asynchronous mode */
872 sai->synchronous[RX] = false;
873 sai->synchronous[TX] = false;
874 fsl_sai_dai.symmetric_rates = 0;
875 fsl_sai_dai.symmetric_channels = 0;
876 fsl_sai_dai.symmetric_samplebits = 0;
879 if (of_find_property(np, "fsl,sai-mclk-direction-output", NULL) &&
880 of_device_is_compatible(np, "fsl,imx6ul-sai")) {
881 gpr = syscon_regmap_lookup_by_compatible("fsl,imx6ul-iomuxc-gpr");
883 dev_err(&pdev->dev, "cannot find iomuxc registers\n");
887 index = of_alias_get_id(np, "sai");
891 regmap_update_bits(gpr, IOMUXC_GPR1, MCLK_DIR(index),
895 sai->dma_params_rx.addr = res->start + FSL_SAI_RDR;
896 sai->dma_params_tx.addr = res->start + FSL_SAI_TDR;
897 sai->dma_params_rx.maxburst = FSL_SAI_MAXBURST_RX;
898 sai->dma_params_tx.maxburst = FSL_SAI_MAXBURST_TX;
900 platform_set_drvdata(pdev, sai);
902 ret = devm_snd_soc_register_component(&pdev->dev, &fsl_component,
908 return imx_pcm_dma_init(pdev, IMX_SAI_DMABUF_SIZE);
910 return devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
913 static const struct of_device_id fsl_sai_ids[] = {
914 { .compatible = "fsl,vf610-sai", },
915 { .compatible = "fsl,imx6sx-sai", },
916 { .compatible = "fsl,imx6ul-sai", },
919 MODULE_DEVICE_TABLE(of, fsl_sai_ids);
921 #ifdef CONFIG_PM_SLEEP
922 static int fsl_sai_suspend(struct device *dev)
924 struct fsl_sai *sai = dev_get_drvdata(dev);
926 regcache_cache_only(sai->regmap, true);
927 regcache_mark_dirty(sai->regmap);
932 static int fsl_sai_resume(struct device *dev)
934 struct fsl_sai *sai = dev_get_drvdata(dev);
936 regcache_cache_only(sai->regmap, false);
937 regmap_write(sai->regmap, FSL_SAI_TCSR, FSL_SAI_CSR_SR);
938 regmap_write(sai->regmap, FSL_SAI_RCSR, FSL_SAI_CSR_SR);
939 usleep_range(1000, 2000);
940 regmap_write(sai->regmap, FSL_SAI_TCSR, 0);
941 regmap_write(sai->regmap, FSL_SAI_RCSR, 0);
942 return regcache_sync(sai->regmap);
944 #endif /* CONFIG_PM_SLEEP */
946 static const struct dev_pm_ops fsl_sai_pm_ops = {
947 SET_SYSTEM_SLEEP_PM_OPS(fsl_sai_suspend, fsl_sai_resume)
950 static struct platform_driver fsl_sai_driver = {
951 .probe = fsl_sai_probe,
954 .pm = &fsl_sai_pm_ops,
955 .of_match_table = fsl_sai_ids,
958 module_platform_driver(fsl_sai_driver);
960 MODULE_DESCRIPTION("Freescale Soc SAI Interface");
961 MODULE_AUTHOR("Xiubo Li, <Li.Xiubo@freescale.com>");
962 MODULE_ALIAS("platform:fsl-sai");
963 MODULE_LICENSE("GPL");