90ff1071e29c51a59b7d4c1bc36d6b0c4121331d
[firefly-linux-kernel-4.4.55.git] / sound / soc / fsl / fsl_ssi.c
1 /*
2  * Freescale SSI ALSA SoC Digital Audio Interface (DAI) driver
3  *
4  * Author: Timur Tabi <timur@freescale.com>
5  *
6  * Copyright 2007-2010 Freescale Semiconductor, Inc.
7  *
8  * This file is licensed under the terms of the GNU General Public License
9  * version 2.  This program is licensed "as is" without any warranty of any
10  * kind, whether express or implied.
11  *
12  *
13  * Some notes why imx-pcm-fiq is used instead of DMA on some boards:
14  *
15  * The i.MX SSI core has some nasty limitations in AC97 mode. While most
16  * sane processor vendors have a FIFO per AC97 slot, the i.MX has only
17  * one FIFO which combines all valid receive slots. We cannot even select
18  * which slots we want to receive. The WM9712 with which this driver
19  * was developed with always sends GPIO status data in slot 12 which
20  * we receive in our (PCM-) data stream. The only chance we have is to
21  * manually skip this data in the FIQ handler. With sampling rates different
22  * from 48000Hz not every frame has valid receive data, so the ratio
23  * between pcm data and GPIO status data changes. Our FIQ handler is not
24  * able to handle this, hence this driver only works with 48000Hz sampling
25  * rate.
26  * Reading and writing AC97 registers is another challenge. The core
27  * provides us status bits when the read register is updated with *another*
28  * value. When we read the same register two times (and the register still
29  * contains the same value) these status bits are not set. We work
30  * around this by not polling these bits but only wait a fixed delay.
31  */
32
33 #include <linux/init.h>
34 #include <linux/io.h>
35 #include <linux/module.h>
36 #include <linux/interrupt.h>
37 #include <linux/clk.h>
38 #include <linux/device.h>
39 #include <linux/delay.h>
40 #include <linux/slab.h>
41 #include <linux/of_address.h>
42 #include <linux/of_irq.h>
43 #include <linux/of_platform.h>
44
45 #include <sound/core.h>
46 #include <sound/pcm.h>
47 #include <sound/pcm_params.h>
48 #include <sound/initval.h>
49 #include <sound/soc.h>
50 #include <sound/dmaengine_pcm.h>
51
52 #include "fsl_ssi.h"
53 #include "imx-pcm.h"
54
55 #ifdef PPC
56 #define read_ssi(addr)                   in_be32(addr)
57 #define write_ssi(val, addr)             out_be32(addr, val)
58 #define write_ssi_mask(addr, clear, set) clrsetbits_be32(addr, clear, set)
59 #else
60 #define read_ssi(addr)                   readl(addr)
61 #define write_ssi(val, addr)             writel(val, addr)
62 /*
63  * FIXME: Proper locking should be added at write_ssi_mask caller level
64  * to ensure this register read/modify/write sequence is race free.
65  */
66 static inline void write_ssi_mask(u32 __iomem *addr, u32 clear, u32 set)
67 {
68         u32 val = readl(addr);
69         val = (val & ~clear) | set;
70         writel(val, addr);
71 }
72 #endif
73
74 /**
75  * FSLSSI_I2S_RATES: sample rates supported by the I2S
76  *
77  * This driver currently only supports the SSI running in I2S slave mode,
78  * which means the codec determines the sample rate.  Therefore, we tell
79  * ALSA that we support all rates and let the codec driver decide what rates
80  * are really supported.
81  */
82 #define FSLSSI_I2S_RATES (SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_192000 | \
83                           SNDRV_PCM_RATE_CONTINUOUS)
84
85 /**
86  * FSLSSI_I2S_FORMATS: audio formats supported by the SSI
87  *
88  * This driver currently only supports the SSI running in I2S slave mode.
89  *
90  * The SSI has a limitation in that the samples must be in the same byte
91  * order as the host CPU.  This is because when multiple bytes are written
92  * to the STX register, the bytes and bits must be written in the same
93  * order.  The STX is a shift register, so all the bits need to be aligned
94  * (bit-endianness must match byte-endianness).  Processors typically write
95  * the bits within a byte in the same order that the bytes of a word are
96  * written in.  So if the host CPU is big-endian, then only big-endian
97  * samples will be written to STX properly.
98  */
99 #ifdef __BIG_ENDIAN
100 #define FSLSSI_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_BE | \
101          SNDRV_PCM_FMTBIT_S18_3BE | SNDRV_PCM_FMTBIT_S20_3BE | \
102          SNDRV_PCM_FMTBIT_S24_3BE | SNDRV_PCM_FMTBIT_S24_BE)
103 #else
104 #define FSLSSI_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE | \
105          SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S20_3LE | \
106          SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE)
107 #endif
108
109 /* SIER bitflag of interrupts to enable */
110 #define SIER_FLAGS (CCSR_SSI_SIER_TFRC_EN | CCSR_SSI_SIER_TDMAE | \
111                     CCSR_SSI_SIER_TIE | CCSR_SSI_SIER_TUE0_EN | \
112                     CCSR_SSI_SIER_TUE1_EN | CCSR_SSI_SIER_RFRC_EN | \
113                     CCSR_SSI_SIER_RDMAE | CCSR_SSI_SIER_RIE | \
114                     CCSR_SSI_SIER_ROE0_EN | CCSR_SSI_SIER_ROE1_EN)
115
116 /**
117  * fsl_ssi_private: per-SSI private data
118  *
119  * @ssi: pointer to the SSI's registers
120  * @ssi_phys: physical address of the SSI registers
121  * @irq: IRQ of this SSI
122  * @first_stream: pointer to the stream that was opened first
123  * @second_stream: pointer to second stream
124  * @playback: the number of playback streams opened
125  * @capture: the number of capture streams opened
126  * @cpu_dai: the CPU DAI for this device
127  * @dev_attr: the sysfs device attribute structure
128  * @stats: SSI statistics
129  * @name: name for this device
130  */
131 struct fsl_ssi_private {
132         struct ccsr_ssi __iomem *ssi;
133         dma_addr_t ssi_phys;
134         unsigned int irq;
135         struct snd_pcm_substream *first_stream;
136         struct snd_pcm_substream *second_stream;
137         unsigned int fifo_depth;
138         struct snd_soc_dai_driver cpu_dai_drv;
139         struct device_attribute dev_attr;
140         struct platform_device *pdev;
141
142         bool new_binding;
143         bool ssi_on_imx;
144         bool imx_ac97;
145         bool use_dma;
146         u8 i2s_mode;
147         struct clk *clk;
148         struct snd_dmaengine_dai_dma_data dma_params_tx;
149         struct snd_dmaengine_dai_dma_data dma_params_rx;
150         struct imx_dma_data filter_data_tx;
151         struct imx_dma_data filter_data_rx;
152         struct imx_pcm_fiq_params fiq_params;
153
154         struct {
155                 unsigned int rfrc;
156                 unsigned int tfrc;
157                 unsigned int cmdau;
158                 unsigned int cmddu;
159                 unsigned int rxt;
160                 unsigned int rdr1;
161                 unsigned int rdr0;
162                 unsigned int tde1;
163                 unsigned int tde0;
164                 unsigned int roe1;
165                 unsigned int roe0;
166                 unsigned int tue1;
167                 unsigned int tue0;
168                 unsigned int tfs;
169                 unsigned int rfs;
170                 unsigned int tls;
171                 unsigned int rls;
172                 unsigned int rff1;
173                 unsigned int rff0;
174                 unsigned int tfe1;
175                 unsigned int tfe0;
176         } stats;
177
178         char name[1];
179 };
180
181 /**
182  * fsl_ssi_isr: SSI interrupt handler
183  *
184  * Although it's possible to use the interrupt handler to send and receive
185  * data to/from the SSI, we use the DMA instead.  Programming is more
186  * complicated, but the performance is much better.
187  *
188  * This interrupt handler is used only to gather statistics.
189  *
190  * @irq: IRQ of the SSI device
191  * @dev_id: pointer to the ssi_private structure for this SSI device
192  */
193 static irqreturn_t fsl_ssi_isr(int irq, void *dev_id)
194 {
195         struct fsl_ssi_private *ssi_private = dev_id;
196         struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
197         irqreturn_t ret = IRQ_NONE;
198         __be32 sisr;
199         __be32 sisr2 = 0;
200
201         /* We got an interrupt, so read the status register to see what we
202            were interrupted for.  We mask it with the Interrupt Enable register
203            so that we only check for events that we're interested in.
204          */
205         sisr = read_ssi(&ssi->sisr) & SIER_FLAGS;
206
207         if (sisr & CCSR_SSI_SISR_RFRC) {
208                 ssi_private->stats.rfrc++;
209                 sisr2 |= CCSR_SSI_SISR_RFRC;
210                 ret = IRQ_HANDLED;
211         }
212
213         if (sisr & CCSR_SSI_SISR_TFRC) {
214                 ssi_private->stats.tfrc++;
215                 sisr2 |= CCSR_SSI_SISR_TFRC;
216                 ret = IRQ_HANDLED;
217         }
218
219         if (sisr & CCSR_SSI_SISR_CMDAU) {
220                 ssi_private->stats.cmdau++;
221                 ret = IRQ_HANDLED;
222         }
223
224         if (sisr & CCSR_SSI_SISR_CMDDU) {
225                 ssi_private->stats.cmddu++;
226                 ret = IRQ_HANDLED;
227         }
228
229         if (sisr & CCSR_SSI_SISR_RXT) {
230                 ssi_private->stats.rxt++;
231                 ret = IRQ_HANDLED;
232         }
233
234         if (sisr & CCSR_SSI_SISR_RDR1) {
235                 ssi_private->stats.rdr1++;
236                 ret = IRQ_HANDLED;
237         }
238
239         if (sisr & CCSR_SSI_SISR_RDR0) {
240                 ssi_private->stats.rdr0++;
241                 ret = IRQ_HANDLED;
242         }
243
244         if (sisr & CCSR_SSI_SISR_TDE1) {
245                 ssi_private->stats.tde1++;
246                 ret = IRQ_HANDLED;
247         }
248
249         if (sisr & CCSR_SSI_SISR_TDE0) {
250                 ssi_private->stats.tde0++;
251                 ret = IRQ_HANDLED;
252         }
253
254         if (sisr & CCSR_SSI_SISR_ROE1) {
255                 ssi_private->stats.roe1++;
256                 sisr2 |= CCSR_SSI_SISR_ROE1;
257                 ret = IRQ_HANDLED;
258         }
259
260         if (sisr & CCSR_SSI_SISR_ROE0) {
261                 ssi_private->stats.roe0++;
262                 sisr2 |= CCSR_SSI_SISR_ROE0;
263                 ret = IRQ_HANDLED;
264         }
265
266         if (sisr & CCSR_SSI_SISR_TUE1) {
267                 ssi_private->stats.tue1++;
268                 sisr2 |= CCSR_SSI_SISR_TUE1;
269                 ret = IRQ_HANDLED;
270         }
271
272         if (sisr & CCSR_SSI_SISR_TUE0) {
273                 ssi_private->stats.tue0++;
274                 sisr2 |= CCSR_SSI_SISR_TUE0;
275                 ret = IRQ_HANDLED;
276         }
277
278         if (sisr & CCSR_SSI_SISR_TFS) {
279                 ssi_private->stats.tfs++;
280                 ret = IRQ_HANDLED;
281         }
282
283         if (sisr & CCSR_SSI_SISR_RFS) {
284                 ssi_private->stats.rfs++;
285                 ret = IRQ_HANDLED;
286         }
287
288         if (sisr & CCSR_SSI_SISR_TLS) {
289                 ssi_private->stats.tls++;
290                 ret = IRQ_HANDLED;
291         }
292
293         if (sisr & CCSR_SSI_SISR_RLS) {
294                 ssi_private->stats.rls++;
295                 ret = IRQ_HANDLED;
296         }
297
298         if (sisr & CCSR_SSI_SISR_RFF1) {
299                 ssi_private->stats.rff1++;
300                 ret = IRQ_HANDLED;
301         }
302
303         if (sisr & CCSR_SSI_SISR_RFF0) {
304                 ssi_private->stats.rff0++;
305                 ret = IRQ_HANDLED;
306         }
307
308         if (sisr & CCSR_SSI_SISR_TFE1) {
309                 ssi_private->stats.tfe1++;
310                 ret = IRQ_HANDLED;
311         }
312
313         if (sisr & CCSR_SSI_SISR_TFE0) {
314                 ssi_private->stats.tfe0++;
315                 ret = IRQ_HANDLED;
316         }
317
318         /* Clear the bits that we set */
319         if (sisr2)
320                 write_ssi(sisr2, &ssi->sisr);
321
322         return ret;
323 }
324
325 static void fsl_ssi_setup_ac97(struct fsl_ssi_private *ssi_private)
326 {
327         struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
328
329         /*
330          * Setup the clock control register
331          */
332         write_ssi(CCSR_SSI_SxCCR_WL(17) | CCSR_SSI_SxCCR_DC(13),
333                         &ssi->stccr);
334         write_ssi(CCSR_SSI_SxCCR_WL(17) | CCSR_SSI_SxCCR_DC(13),
335                         &ssi->srccr);
336
337         /*
338          * Enable AC97 mode and startup the SSI
339          */
340         write_ssi(CCSR_SSI_SACNT_AC97EN | CCSR_SSI_SACNT_FV,
341                         &ssi->sacnt);
342         write_ssi(0xff, &ssi->saccdis);
343         write_ssi(0x300, &ssi->saccen);
344
345         /*
346          * Enable SSI, Transmit and Receive. AC97 has to communicate with the
347          * codec before a stream is started.
348          */
349         write_ssi_mask(&ssi->scr, 0, CCSR_SSI_SCR_SSIEN |
350                         CCSR_SSI_SCR_TE | CCSR_SSI_SCR_RE);
351
352         write_ssi(CCSR_SSI_SOR_WAIT(3), &ssi->sor);
353 }
354
355 static int fsl_ssi_setup(struct fsl_ssi_private *ssi_private)
356 {
357         struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
358         u8 wm;
359         int synchronous = ssi_private->cpu_dai_drv.symmetric_rates;
360
361         if (ssi_private->imx_ac97)
362                 ssi_private->i2s_mode = CCSR_SSI_SCR_I2S_MODE_NORMAL | CCSR_SSI_SCR_NET;
363         else
364                 ssi_private->i2s_mode = CCSR_SSI_SCR_I2S_MODE_SLAVE;
365
366         /*
367          * Section 16.5 of the MPC8610 reference manual says that the SSI needs
368          * to be disabled before updating the registers we set here.
369          */
370         write_ssi_mask(&ssi->scr, CCSR_SSI_SCR_SSIEN, 0);
371
372         /*
373          * Program the SSI into I2S Slave Non-Network Synchronous mode. Also
374          * enable the transmit and receive FIFO.
375          *
376          * FIXME: Little-endian samples require a different shift dir
377          */
378         write_ssi_mask(&ssi->scr,
379                 CCSR_SSI_SCR_I2S_MODE_MASK | CCSR_SSI_SCR_SYN,
380                 CCSR_SSI_SCR_TFR_CLK_DIS |
381                 ssi_private->i2s_mode |
382                 (synchronous ? CCSR_SSI_SCR_SYN : 0));
383
384         write_ssi(CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TFEN0 |
385                  CCSR_SSI_STCR_TFSI | CCSR_SSI_STCR_TEFS |
386                  CCSR_SSI_STCR_TSCKP, &ssi->stcr);
387
388         write_ssi(CCSR_SSI_SRCR_RXBIT0 | CCSR_SSI_SRCR_RFEN0 |
389                  CCSR_SSI_SRCR_RFSI | CCSR_SSI_SRCR_REFS |
390                  CCSR_SSI_SRCR_RSCKP, &ssi->srcr);
391         /*
392          * The DC and PM bits are only used if the SSI is the clock master.
393          */
394
395         /*
396          * Set the watermark for transmit FIFI 0 and receive FIFO 0. We don't
397          * use FIFO 1. We program the transmit water to signal a DMA transfer
398          * if there are only two (or fewer) elements left in the FIFO. Two
399          * elements equals one frame (left channel, right channel). This value,
400          * however, depends on the depth of the transmit buffer.
401          *
402          * We set the watermark on the same level as the DMA burstsize.  For
403          * fiq it is probably better to use the biggest possible watermark
404          * size.
405          */
406         if (ssi_private->use_dma)
407                 wm = ssi_private->fifo_depth - 2;
408         else
409                 wm = ssi_private->fifo_depth;
410
411         write_ssi(CCSR_SSI_SFCSR_TFWM0(wm) | CCSR_SSI_SFCSR_RFWM0(wm) |
412                 CCSR_SSI_SFCSR_TFWM1(wm) | CCSR_SSI_SFCSR_RFWM1(wm),
413                 &ssi->sfcsr);
414
415         /*
416          * For ac97 interrupts are enabled with the startup of the substream
417          * because it is also running without an active substream. Normally SSI
418          * is only enabled when there is a substream.
419          */
420         if (ssi_private->imx_ac97)
421                 fsl_ssi_setup_ac97(ssi_private);
422
423         return 0;
424 }
425
426
427 /**
428  * fsl_ssi_startup: create a new substream
429  *
430  * This is the first function called when a stream is opened.
431  *
432  * If this is the first stream open, then grab the IRQ and program most of
433  * the SSI registers.
434  */
435 static int fsl_ssi_startup(struct snd_pcm_substream *substream,
436                            struct snd_soc_dai *dai)
437 {
438         struct snd_soc_pcm_runtime *rtd = substream->private_data;
439         struct fsl_ssi_private *ssi_private =
440                 snd_soc_dai_get_drvdata(rtd->cpu_dai);
441         int synchronous = ssi_private->cpu_dai_drv.symmetric_rates;
442
443         /*
444          * If this is the first stream opened, then request the IRQ
445          * and initialize the SSI registers.
446          */
447         if (!ssi_private->first_stream) {
448                 ssi_private->first_stream = substream;
449
450                 /*
451                  * fsl_ssi_setup was already called by ac97_init earlier if
452                  * the driver is in ac97 mode.
453                  */
454                 if (!ssi_private->imx_ac97)
455                         fsl_ssi_setup(ssi_private);
456         } else {
457                 if (synchronous) {
458                         struct snd_pcm_runtime *first_runtime =
459                                 ssi_private->first_stream->runtime;
460                         /*
461                          * This is the second stream open, and we're in
462                          * synchronous mode, so we need to impose sample
463                          * sample size constraints. This is because STCCR is
464                          * used for playback and capture in synchronous mode,
465                          * so there's no way to specify different word
466                          * lengths.
467                          *
468                          * Note that this can cause a race condition if the
469                          * second stream is opened before the first stream is
470                          * fully initialized.  We provide some protection by
471                          * checking to make sure the first stream is
472                          * initialized, but it's not perfect.  ALSA sometimes
473                          * re-initializes the driver with a different sample
474                          * rate or size.  If the second stream is opened
475                          * before the first stream has received its final
476                          * parameters, then the second stream may be
477                          * constrained to the wrong sample rate or size.
478                          */
479                         if (first_runtime->sample_bits) {
480                                 snd_pcm_hw_constraint_minmax(substream->runtime,
481                                                 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
482                                 first_runtime->sample_bits,
483                                 first_runtime->sample_bits);
484                         }
485                 }
486
487                 ssi_private->second_stream = substream;
488         }
489
490         return 0;
491 }
492
493 /**
494  * fsl_ssi_hw_params - program the sample size
495  *
496  * Most of the SSI registers have been programmed in the startup function,
497  * but the word length must be programmed here.  Unfortunately, programming
498  * the SxCCR.WL bits requires the SSI to be temporarily disabled.  This can
499  * cause a problem with supporting simultaneous playback and capture.  If
500  * the SSI is already playing a stream, then that stream may be temporarily
501  * stopped when you start capture.
502  *
503  * Note: The SxCCR.DC and SxCCR.PM bits are only used if the SSI is the
504  * clock master.
505  */
506 static int fsl_ssi_hw_params(struct snd_pcm_substream *substream,
507         struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *cpu_dai)
508 {
509         struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
510         struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
511         unsigned int channels = params_channels(hw_params);
512         unsigned int sample_size =
513                 snd_pcm_format_width(params_format(hw_params));
514         u32 wl = CCSR_SSI_SxCCR_WL(sample_size);
515         int enabled = read_ssi(&ssi->scr) & CCSR_SSI_SCR_SSIEN;
516
517         /*
518          * If we're in synchronous mode, and the SSI is already enabled,
519          * then STCCR is already set properly.
520          */
521         if (enabled && ssi_private->cpu_dai_drv.symmetric_rates)
522                 return 0;
523
524         /*
525          * FIXME: The documentation says that SxCCR[WL] should not be
526          * modified while the SSI is enabled.  The only time this can
527          * happen is if we're trying to do simultaneous playback and
528          * capture in asynchronous mode.  Unfortunately, I have been enable
529          * to get that to work at all on the P1022DS.  Therefore, we don't
530          * bother to disable/enable the SSI when setting SxCCR[WL], because
531          * the SSI will stop anyway.  Maybe one day, this will get fixed.
532          */
533
534         /* In synchronous mode, the SSI uses STCCR for capture */
535         if ((substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ||
536             ssi_private->cpu_dai_drv.symmetric_rates)
537                 write_ssi_mask(&ssi->stccr, CCSR_SSI_SxCCR_WL_MASK, wl);
538         else
539                 write_ssi_mask(&ssi->srccr, CCSR_SSI_SxCCR_WL_MASK, wl);
540
541         if (!ssi_private->imx_ac97)
542                 write_ssi_mask(&ssi->scr,
543                                 CCSR_SSI_SCR_NET | CCSR_SSI_SCR_I2S_MODE_MASK,
544                                 channels == 1 ? 0 : ssi_private->i2s_mode);
545
546         return 0;
547 }
548
549 /**
550  * fsl_ssi_trigger: start and stop the DMA transfer.
551  *
552  * This function is called by ALSA to start, stop, pause, and resume the DMA
553  * transfer of data.
554  *
555  * The DMA channel is in external master start and pause mode, which
556  * means the SSI completely controls the flow of data.
557  */
558 static int fsl_ssi_trigger(struct snd_pcm_substream *substream, int cmd,
559                            struct snd_soc_dai *dai)
560 {
561         struct snd_soc_pcm_runtime *rtd = substream->private_data;
562         struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(rtd->cpu_dai);
563         struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
564         unsigned int sier_bits;
565
566         /*
567          *  Enable only the interrupts and DMA requests
568          *  that are needed for the channel. As the fiq
569          *  is polling for this bits, we have to ensure
570          *  that this are aligned with the preallocated
571          *  buffers
572          */
573
574         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
575                 if (ssi_private->use_dma)
576                         sier_bits = SIER_FLAGS;
577                 else
578                         sier_bits = CCSR_SSI_SIER_TIE | CCSR_SSI_SIER_TFE0_EN;
579         } else {
580                 if (ssi_private->use_dma)
581                         sier_bits = SIER_FLAGS;
582                 else
583                         sier_bits = CCSR_SSI_SIER_RIE | CCSR_SSI_SIER_RFF0_EN;
584         }
585
586         switch (cmd) {
587         case SNDRV_PCM_TRIGGER_START:
588         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
589                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
590                         write_ssi_mask(&ssi->scr, 0,
591                                 CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_TE);
592                 else
593                         write_ssi_mask(&ssi->scr, 0,
594                                 CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_RE);
595                 break;
596
597         case SNDRV_PCM_TRIGGER_STOP:
598         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
599                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
600                         write_ssi_mask(&ssi->scr, CCSR_SSI_SCR_TE, 0);
601                 else
602                         write_ssi_mask(&ssi->scr, CCSR_SSI_SCR_RE, 0);
603
604                 if (!ssi_private->imx_ac97 && (read_ssi(&ssi->scr) &
605                                         (CCSR_SSI_SCR_TE | CCSR_SSI_SCR_RE)) == 0)
606                         write_ssi_mask(&ssi->scr, CCSR_SSI_SCR_SSIEN, 0);
607                 break;
608
609         default:
610                 return -EINVAL;
611         }
612
613         write_ssi(sier_bits, &ssi->sier);
614
615         return 0;
616 }
617
618 /**
619  * fsl_ssi_shutdown: shutdown the SSI
620  *
621  * Shutdown the SSI if there are no other substreams open.
622  */
623 static void fsl_ssi_shutdown(struct snd_pcm_substream *substream,
624                              struct snd_soc_dai *dai)
625 {
626         struct snd_soc_pcm_runtime *rtd = substream->private_data;
627         struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(rtd->cpu_dai);
628
629         if (ssi_private->first_stream == substream)
630                 ssi_private->first_stream = ssi_private->second_stream;
631
632         ssi_private->second_stream = NULL;
633 }
634
635 static int fsl_ssi_dai_probe(struct snd_soc_dai *dai)
636 {
637         struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(dai);
638
639         if (ssi_private->ssi_on_imx && ssi_private->use_dma) {
640                 dai->playback_dma_data = &ssi_private->dma_params_tx;
641                 dai->capture_dma_data = &ssi_private->dma_params_rx;
642         }
643
644         return 0;
645 }
646
647 static const struct snd_soc_dai_ops fsl_ssi_dai_ops = {
648         .startup        = fsl_ssi_startup,
649         .hw_params      = fsl_ssi_hw_params,
650         .shutdown       = fsl_ssi_shutdown,
651         .trigger        = fsl_ssi_trigger,
652 };
653
654 /* Template for the CPU dai driver structure */
655 static struct snd_soc_dai_driver fsl_ssi_dai_template = {
656         .probe = fsl_ssi_dai_probe,
657         .playback = {
658                 .channels_min = 1,
659                 .channels_max = 2,
660                 .rates = FSLSSI_I2S_RATES,
661                 .formats = FSLSSI_I2S_FORMATS,
662         },
663         .capture = {
664                 .channels_min = 1,
665                 .channels_max = 2,
666                 .rates = FSLSSI_I2S_RATES,
667                 .formats = FSLSSI_I2S_FORMATS,
668         },
669         .ops = &fsl_ssi_dai_ops,
670 };
671
672 static const struct snd_soc_component_driver fsl_ssi_component = {
673         .name           = "fsl-ssi",
674 };
675
676 /**
677  * fsl_ssi_ac97_trigger: start and stop the AC97 receive/transmit.
678  *
679  * This function is called by ALSA to start, stop, pause, and resume the
680  * transfer of data.
681  */
682 static int fsl_ssi_ac97_trigger(struct snd_pcm_substream *substream, int cmd,
683                            struct snd_soc_dai *dai)
684 {
685         struct snd_soc_pcm_runtime *rtd = substream->private_data;
686         struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(
687                         rtd->cpu_dai);
688         struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
689
690         switch (cmd) {
691         case SNDRV_PCM_TRIGGER_START:
692         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
693                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
694                         write_ssi_mask(&ssi->sier, 0, CCSR_SSI_SIER_TIE |
695                                         CCSR_SSI_SIER_TFE0_EN);
696                 else
697                         write_ssi_mask(&ssi->sier, 0, CCSR_SSI_SIER_RIE |
698                                         CCSR_SSI_SIER_RFF0_EN);
699                 break;
700
701         case SNDRV_PCM_TRIGGER_STOP:
702         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
703                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
704                         write_ssi_mask(&ssi->sier, CCSR_SSI_SIER_TIE |
705                                         CCSR_SSI_SIER_TFE0_EN, 0);
706                 else
707                         write_ssi_mask(&ssi->sier, CCSR_SSI_SIER_RIE |
708                                         CCSR_SSI_SIER_RFF0_EN, 0);
709                 break;
710
711         default:
712                 return -EINVAL;
713         }
714
715         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
716                 write_ssi(CCSR_SSI_SOR_TX_CLR, &ssi->sor);
717         else
718                 write_ssi(CCSR_SSI_SOR_RX_CLR, &ssi->sor);
719
720         return 0;
721 }
722
723 static const struct snd_soc_dai_ops fsl_ssi_ac97_dai_ops = {
724         .startup        = fsl_ssi_startup,
725         .shutdown       = fsl_ssi_shutdown,
726         .trigger        = fsl_ssi_ac97_trigger,
727 };
728
729 static struct snd_soc_dai_driver fsl_ssi_ac97_dai = {
730         .ac97_control = 1,
731         .playback = {
732                 .stream_name = "AC97 Playback",
733                 .channels_min = 2,
734                 .channels_max = 2,
735                 .rates = SNDRV_PCM_RATE_8000_48000,
736                 .formats = SNDRV_PCM_FMTBIT_S16_LE,
737         },
738         .capture = {
739                 .stream_name = "AC97 Capture",
740                 .channels_min = 2,
741                 .channels_max = 2,
742                 .rates = SNDRV_PCM_RATE_48000,
743                 .formats = SNDRV_PCM_FMTBIT_S16_LE,
744         },
745         .ops = &fsl_ssi_ac97_dai_ops,
746 };
747
748
749 static struct fsl_ssi_private *fsl_ac97_data;
750
751 static void fsl_ssi_ac97_init(void)
752 {
753         fsl_ssi_setup(fsl_ac97_data);
754 }
755
756 static void fsl_ssi_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
757                 unsigned short val)
758 {
759         struct ccsr_ssi *ssi = fsl_ac97_data->ssi;
760         unsigned int lreg;
761         unsigned int lval;
762
763         if (reg > 0x7f)
764                 return;
765
766
767         lreg = reg <<  12;
768         write_ssi(lreg, &ssi->sacadd);
769
770         lval = val << 4;
771         write_ssi(lval , &ssi->sacdat);
772
773         write_ssi_mask(&ssi->sacnt, CCSR_SSI_SACNT_RDWR_MASK,
774                         CCSR_SSI_SACNT_WR);
775         udelay(100);
776 }
777
778 static unsigned short fsl_ssi_ac97_read(struct snd_ac97 *ac97,
779                 unsigned short reg)
780 {
781         struct ccsr_ssi *ssi = fsl_ac97_data->ssi;
782
783         unsigned short val = -1;
784         unsigned int lreg;
785
786         lreg = (reg & 0x7f) <<  12;
787         write_ssi(lreg, &ssi->sacadd);
788         write_ssi_mask(&ssi->sacnt, CCSR_SSI_SACNT_RDWR_MASK,
789                         CCSR_SSI_SACNT_RD);
790
791         udelay(100);
792
793         val = (read_ssi(&ssi->sacdat) >> 4) & 0xffff;
794
795         return val;
796 }
797
798 static struct snd_ac97_bus_ops fsl_ssi_ac97_ops = {
799         .read           = fsl_ssi_ac97_read,
800         .write          = fsl_ssi_ac97_write,
801 };
802
803 /* Show the statistics of a flag only if its interrupt is enabled.  The
804  * compiler will optimze this code to a no-op if the interrupt is not
805  * enabled.
806  */
807 #define SIER_SHOW(flag, name) \
808         do { \
809                 if (SIER_FLAGS & CCSR_SSI_SIER_##flag) \
810                         length += sprintf(buf + length, #name "=%u\n", \
811                                 ssi_private->stats.name); \
812         } while (0)
813
814
815 /**
816  * fsl_sysfs_ssi_show: display SSI statistics
817  *
818  * Display the statistics for the current SSI device.  To avoid confusion,
819  * we only show those counts that are enabled.
820  */
821 static ssize_t fsl_sysfs_ssi_show(struct device *dev,
822         struct device_attribute *attr, char *buf)
823 {
824         struct fsl_ssi_private *ssi_private =
825                 container_of(attr, struct fsl_ssi_private, dev_attr);
826         ssize_t length = 0;
827
828         SIER_SHOW(RFRC_EN, rfrc);
829         SIER_SHOW(TFRC_EN, tfrc);
830         SIER_SHOW(CMDAU_EN, cmdau);
831         SIER_SHOW(CMDDU_EN, cmddu);
832         SIER_SHOW(RXT_EN, rxt);
833         SIER_SHOW(RDR1_EN, rdr1);
834         SIER_SHOW(RDR0_EN, rdr0);
835         SIER_SHOW(TDE1_EN, tde1);
836         SIER_SHOW(TDE0_EN, tde0);
837         SIER_SHOW(ROE1_EN, roe1);
838         SIER_SHOW(ROE0_EN, roe0);
839         SIER_SHOW(TUE1_EN, tue1);
840         SIER_SHOW(TUE0_EN, tue0);
841         SIER_SHOW(TFS_EN, tfs);
842         SIER_SHOW(RFS_EN, rfs);
843         SIER_SHOW(TLS_EN, tls);
844         SIER_SHOW(RLS_EN, rls);
845         SIER_SHOW(RFF1_EN, rff1);
846         SIER_SHOW(RFF0_EN, rff0);
847         SIER_SHOW(TFE1_EN, tfe1);
848         SIER_SHOW(TFE0_EN, tfe0);
849
850         return length;
851 }
852
853 /**
854  * Make every character in a string lower-case
855  */
856 static void make_lowercase(char *s)
857 {
858         char *p = s;
859         char c;
860
861         while ((c = *p)) {
862                 if ((c >= 'A') && (c <= 'Z'))
863                         *p = c + ('a' - 'A');
864                 p++;
865         }
866 }
867
868 static int fsl_ssi_probe(struct platform_device *pdev)
869 {
870         struct fsl_ssi_private *ssi_private;
871         int ret = 0;
872         struct device_attribute *dev_attr = NULL;
873         struct device_node *np = pdev->dev.of_node;
874         const char *p, *sprop;
875         const uint32_t *iprop;
876         struct resource res;
877         char name[64];
878         bool shared;
879         bool ac97 = false;
880
881         /* SSIs that are not connected on the board should have a
882          *      status = "disabled"
883          * property in their device tree nodes.
884          */
885         if (!of_device_is_available(np))
886                 return -ENODEV;
887
888         /* We only support the SSI in "I2S Slave" mode */
889         sprop = of_get_property(np, "fsl,mode", NULL);
890         if (!sprop) {
891                 dev_err(&pdev->dev, "fsl,mode property is necessary\n");
892                 return -EINVAL;
893         }
894         if (!strcmp(sprop, "ac97-slave")) {
895                 ac97 = true;
896         } else if (strcmp(sprop, "i2s-slave")) {
897                 dev_notice(&pdev->dev, "mode %s is unsupported\n", sprop);
898                 return -ENODEV;
899         }
900
901         /* The DAI name is the last part of the full name of the node. */
902         p = strrchr(np->full_name, '/') + 1;
903         ssi_private = devm_kzalloc(&pdev->dev, sizeof(*ssi_private) + strlen(p),
904                               GFP_KERNEL);
905         if (!ssi_private) {
906                 dev_err(&pdev->dev, "could not allocate DAI object\n");
907                 return -ENOMEM;
908         }
909
910         strcpy(ssi_private->name, p);
911
912         ssi_private->use_dma = !of_property_read_bool(np,
913                         "fsl,fiq-stream-filter");
914
915         if (ac97) {
916                 memcpy(&ssi_private->cpu_dai_drv, &fsl_ssi_ac97_dai,
917                                 sizeof(fsl_ssi_ac97_dai));
918
919                 fsl_ac97_data = ssi_private;
920                 ssi_private->imx_ac97 = true;
921
922                 snd_soc_set_ac97_ops_of_reset(&fsl_ssi_ac97_ops, pdev);
923         } else {
924                 /* Initialize this copy of the CPU DAI driver structure */
925                 memcpy(&ssi_private->cpu_dai_drv, &fsl_ssi_dai_template,
926                        sizeof(fsl_ssi_dai_template));
927         }
928         ssi_private->cpu_dai_drv.name = ssi_private->name;
929
930         /* Get the addresses and IRQ */
931         ret = of_address_to_resource(np, 0, &res);
932         if (ret) {
933                 dev_err(&pdev->dev, "could not determine device resources\n");
934                 return ret;
935         }
936         ssi_private->ssi = of_iomap(np, 0);
937         if (!ssi_private->ssi) {
938                 dev_err(&pdev->dev, "could not map device resources\n");
939                 return -ENOMEM;
940         }
941         ssi_private->ssi_phys = res.start;
942
943         ssi_private->irq = irq_of_parse_and_map(np, 0);
944         if (!ssi_private->irq) {
945                 dev_err(&pdev->dev, "no irq for node %s\n", np->full_name);
946                 return -ENXIO;
947         }
948
949         /* Are the RX and the TX clocks locked? */
950         if (!of_find_property(np, "fsl,ssi-asynchronous", NULL))
951                 ssi_private->cpu_dai_drv.symmetric_rates = 1;
952
953         /* Determine the FIFO depth. */
954         iprop = of_get_property(np, "fsl,fifo-depth", NULL);
955         if (iprop)
956                 ssi_private->fifo_depth = be32_to_cpup(iprop);
957         else
958                 /* Older 8610 DTs didn't have the fifo-depth property */
959                 ssi_private->fifo_depth = 8;
960
961         if (of_device_is_compatible(pdev->dev.of_node, "fsl,imx21-ssi")) {
962                 u32 dma_events[2];
963                 ssi_private->ssi_on_imx = true;
964
965                 ssi_private->clk = devm_clk_get(&pdev->dev, NULL);
966                 if (IS_ERR(ssi_private->clk)) {
967                         ret = PTR_ERR(ssi_private->clk);
968                         dev_err(&pdev->dev, "could not get clock: %d\n", ret);
969                         goto error_irqmap;
970                 }
971                 ret = clk_prepare_enable(ssi_private->clk);
972                 if (ret) {
973                         dev_err(&pdev->dev, "clk_prepare_enable failed: %d\n",
974                                 ret);
975                         goto error_irqmap;
976                 }
977
978                 /*
979                  * We have burstsize be "fifo_depth - 2" to match the SSI
980                  * watermark setting in fsl_ssi_startup().
981                  */
982                 ssi_private->dma_params_tx.maxburst =
983                         ssi_private->fifo_depth - 2;
984                 ssi_private->dma_params_rx.maxburst =
985                         ssi_private->fifo_depth - 2;
986                 ssi_private->dma_params_tx.addr =
987                         ssi_private->ssi_phys + offsetof(struct ccsr_ssi, stx0);
988                 ssi_private->dma_params_rx.addr =
989                         ssi_private->ssi_phys + offsetof(struct ccsr_ssi, srx0);
990                 ssi_private->dma_params_tx.filter_data =
991                         &ssi_private->filter_data_tx;
992                 ssi_private->dma_params_rx.filter_data =
993                         &ssi_private->filter_data_rx;
994                 if (!of_property_read_bool(pdev->dev.of_node, "dmas") &&
995                                 ssi_private->use_dma) {
996                         /*
997                          * FIXME: This is a temporary solution until all
998                          * necessary dma drivers support the generic dma
999                          * bindings.
1000                          */
1001                         ret = of_property_read_u32_array(pdev->dev.of_node,
1002                                         "fsl,ssi-dma-events", dma_events, 2);
1003                         if (ret && ssi_private->use_dma) {
1004                                 dev_err(&pdev->dev, "could not get dma events but fsl-ssi is configured to use DMA\n");
1005                                 goto error_clk;
1006                         }
1007                 }
1008
1009                 shared = of_device_is_compatible(of_get_parent(np),
1010                             "fsl,spba-bus");
1011
1012                 imx_pcm_dma_params_init_data(&ssi_private->filter_data_tx,
1013                         dma_events[0], shared ? IMX_DMATYPE_SSI_SP : IMX_DMATYPE_SSI);
1014                 imx_pcm_dma_params_init_data(&ssi_private->filter_data_rx,
1015                         dma_events[1], shared ? IMX_DMATYPE_SSI_SP : IMX_DMATYPE_SSI);
1016         } else if (ssi_private->use_dma) {
1017                 /* The 'name' should not have any slashes in it. */
1018                 ret = devm_request_irq(&pdev->dev, ssi_private->irq,
1019                                         fsl_ssi_isr, 0, ssi_private->name,
1020                                         ssi_private);
1021                 if (ret < 0) {
1022                         dev_err(&pdev->dev, "could not claim irq %u\n",
1023                                         ssi_private->irq);
1024                         goto error_irqmap;
1025                 }
1026         }
1027
1028         /* Initialize the the device_attribute structure */
1029         dev_attr = &ssi_private->dev_attr;
1030         sysfs_attr_init(&dev_attr->attr);
1031         dev_attr->attr.name = "statistics";
1032         dev_attr->attr.mode = S_IRUGO;
1033         dev_attr->show = fsl_sysfs_ssi_show;
1034
1035         ret = device_create_file(&pdev->dev, dev_attr);
1036         if (ret) {
1037                 dev_err(&pdev->dev, "could not create sysfs %s file\n",
1038                         ssi_private->dev_attr.attr.name);
1039                 goto error_clk;
1040         }
1041
1042         /* Register with ASoC */
1043         dev_set_drvdata(&pdev->dev, ssi_private);
1044
1045         ret = snd_soc_register_component(&pdev->dev, &fsl_ssi_component,
1046                                          &ssi_private->cpu_dai_drv, 1);
1047         if (ret) {
1048                 dev_err(&pdev->dev, "failed to register DAI: %d\n", ret);
1049                 goto error_dev;
1050         }
1051
1052         if (ssi_private->ssi_on_imx) {
1053                 if (!ssi_private->use_dma) {
1054
1055                         /*
1056                          * Some boards use an incompatible codec. To get it
1057                          * working, we are using imx-fiq-pcm-audio, that
1058                          * can handle those codecs. DMA is not possible in this
1059                          * situation.
1060                          */
1061
1062                         ssi_private->fiq_params.irq = ssi_private->irq;
1063                         ssi_private->fiq_params.base = ssi_private->ssi;
1064                         ssi_private->fiq_params.dma_params_rx =
1065                                 &ssi_private->dma_params_rx;
1066                         ssi_private->fiq_params.dma_params_tx =
1067                                 &ssi_private->dma_params_tx;
1068
1069                         ret = imx_pcm_fiq_init(pdev, &ssi_private->fiq_params);
1070                         if (ret)
1071                                 goto error_dev;
1072                 } else {
1073                         ret = imx_pcm_dma_init(pdev);
1074                         if (ret)
1075                                 goto error_dev;
1076                 }
1077         }
1078
1079         /*
1080          * If codec-handle property is missing from SSI node, we assume
1081          * that the machine driver uses new binding which does not require
1082          * SSI driver to trigger machine driver's probe.
1083          */
1084         if (!of_get_property(np, "codec-handle", NULL)) {
1085                 ssi_private->new_binding = true;
1086                 goto done;
1087         }
1088
1089         /* Trigger the machine driver's probe function.  The platform driver
1090          * name of the machine driver is taken from /compatible property of the
1091          * device tree.  We also pass the address of the CPU DAI driver
1092          * structure.
1093          */
1094         sprop = of_get_property(of_find_node_by_path("/"), "compatible", NULL);
1095         /* Sometimes the compatible name has a "fsl," prefix, so we strip it. */
1096         p = strrchr(sprop, ',');
1097         if (p)
1098                 sprop = p + 1;
1099         snprintf(name, sizeof(name), "snd-soc-%s", sprop);
1100         make_lowercase(name);
1101
1102         ssi_private->pdev =
1103                 platform_device_register_data(&pdev->dev, name, 0, NULL, 0);
1104         if (IS_ERR(ssi_private->pdev)) {
1105                 ret = PTR_ERR(ssi_private->pdev);
1106                 dev_err(&pdev->dev, "failed to register platform: %d\n", ret);
1107                 goto error_dai;
1108         }
1109
1110 done:
1111         if (ssi_private->imx_ac97)
1112                 fsl_ssi_ac97_init();
1113
1114         return 0;
1115
1116 error_dai:
1117         snd_soc_unregister_component(&pdev->dev);
1118
1119 error_dev:
1120         device_remove_file(&pdev->dev, dev_attr);
1121
1122 error_clk:
1123         if (ssi_private->ssi_on_imx)
1124                 clk_disable_unprepare(ssi_private->clk);
1125
1126 error_irqmap:
1127         irq_dispose_mapping(ssi_private->irq);
1128
1129         return ret;
1130 }
1131
1132 static int fsl_ssi_remove(struct platform_device *pdev)
1133 {
1134         struct fsl_ssi_private *ssi_private = dev_get_drvdata(&pdev->dev);
1135
1136         if (!ssi_private->new_binding)
1137                 platform_device_unregister(ssi_private->pdev);
1138         snd_soc_unregister_component(&pdev->dev);
1139         device_remove_file(&pdev->dev, &ssi_private->dev_attr);
1140         if (ssi_private->ssi_on_imx)
1141                 clk_disable_unprepare(ssi_private->clk);
1142         irq_dispose_mapping(ssi_private->irq);
1143
1144         return 0;
1145 }
1146
1147 static const struct of_device_id fsl_ssi_ids[] = {
1148         { .compatible = "fsl,mpc8610-ssi", },
1149         { .compatible = "fsl,imx21-ssi", },
1150         {}
1151 };
1152 MODULE_DEVICE_TABLE(of, fsl_ssi_ids);
1153
1154 static struct platform_driver fsl_ssi_driver = {
1155         .driver = {
1156                 .name = "fsl-ssi-dai",
1157                 .owner = THIS_MODULE,
1158                 .of_match_table = fsl_ssi_ids,
1159         },
1160         .probe = fsl_ssi_probe,
1161         .remove = fsl_ssi_remove,
1162 };
1163
1164 module_platform_driver(fsl_ssi_driver);
1165
1166 MODULE_ALIAS("platform:fsl-ssi-dai");
1167 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
1168 MODULE_DESCRIPTION("Freescale Synchronous Serial Interface (SSI) ASoC Driver");
1169 MODULE_LICENSE("GPL v2");