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