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