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