]> Pileus Git - ~andy/linux/blob - sound/soc/fsl/fsl_ssi.c
ASoC: fsl-ssi: Move sysfs stats to debugfs
[~andy/linux] / 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/debugfs.h>
39 #include <linux/device.h>
40 #include <linux/delay.h>
41 #include <linux/slab.h>
42 #include <linux/spinlock.h>
43 #include <linux/of_address.h>
44 #include <linux/of_irq.h>
45 #include <linux/of_platform.h>
46
47 #include <sound/core.h>
48 #include <sound/pcm.h>
49 #include <sound/pcm_params.h>
50 #include <sound/initval.h>
51 #include <sound/soc.h>
52 #include <sound/dmaengine_pcm.h>
53
54 #include "fsl_ssi.h"
55 #include "imx-pcm.h"
56
57 #ifdef PPC
58 #define read_ssi(addr)                   in_be32(addr)
59 #define write_ssi(val, addr)             out_be32(addr, val)
60 #define write_ssi_mask(addr, clear, set) clrsetbits_be32(addr, clear, set)
61 #else
62 #define read_ssi(addr)                   readl(addr)
63 #define write_ssi(val, addr)             writel(val, addr)
64 /*
65  * FIXME: Proper locking should be added at write_ssi_mask caller level
66  * to ensure this register read/modify/write sequence is race free.
67  */
68 static inline void write_ssi_mask(u32 __iomem *addr, u32 clear, u32 set)
69 {
70         u32 val = readl(addr);
71         val = (val & ~clear) | set;
72         writel(val, addr);
73 }
74 #endif
75
76 /**
77  * FSLSSI_I2S_RATES: sample rates supported by the I2S
78  *
79  * This driver currently only supports the SSI running in I2S slave mode,
80  * which means the codec determines the sample rate.  Therefore, we tell
81  * ALSA that we support all rates and let the codec driver decide what rates
82  * are really supported.
83  */
84 #define FSLSSI_I2S_RATES (SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_192000 | \
85                           SNDRV_PCM_RATE_CONTINUOUS)
86
87 /**
88  * FSLSSI_I2S_FORMATS: audio formats supported by the SSI
89  *
90  * This driver currently only supports the SSI running in I2S slave mode.
91  *
92  * The SSI has a limitation in that the samples must be in the same byte
93  * order as the host CPU.  This is because when multiple bytes are written
94  * to the STX register, the bytes and bits must be written in the same
95  * order.  The STX is a shift register, so all the bits need to be aligned
96  * (bit-endianness must match byte-endianness).  Processors typically write
97  * the bits within a byte in the same order that the bytes of a word are
98  * written in.  So if the host CPU is big-endian, then only big-endian
99  * samples will be written to STX properly.
100  */
101 #ifdef __BIG_ENDIAN
102 #define FSLSSI_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_BE | \
103          SNDRV_PCM_FMTBIT_S18_3BE | SNDRV_PCM_FMTBIT_S20_3BE | \
104          SNDRV_PCM_FMTBIT_S24_3BE | SNDRV_PCM_FMTBIT_S24_BE)
105 #else
106 #define FSLSSI_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE | \
107          SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S20_3LE | \
108          SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE)
109 #endif
110
111 /* SIER bitflag of interrupts to enable */
112 #define SIER_FLAGS (CCSR_SSI_SIER_TFRC_EN | CCSR_SSI_SIER_TDMAE | \
113                     CCSR_SSI_SIER_TIE | CCSR_SSI_SIER_TUE0_EN | \
114                     CCSR_SSI_SIER_TUE1_EN | CCSR_SSI_SIER_RFRC_EN | \
115                     CCSR_SSI_SIER_RDMAE | CCSR_SSI_SIER_RIE | \
116                     CCSR_SSI_SIER_ROE0_EN | CCSR_SSI_SIER_ROE1_EN)
117
118 #define FSLSSI_SIER_DBG_RX_FLAGS (CCSR_SSI_SIER_RFF0_EN | \
119                 CCSR_SSI_SIER_RLS_EN | CCSR_SSI_SIER_RFS_EN | \
120                 CCSR_SSI_SIER_ROE0_EN | CCSR_SSI_SIER_RFRC_EN)
121 #define FSLSSI_SIER_DBG_TX_FLAGS (CCSR_SSI_SIER_TFE0_EN | \
122                 CCSR_SSI_SIER_TLS_EN | CCSR_SSI_SIER_TFS_EN | \
123                 CCSR_SSI_SIER_TUE0_EN | CCSR_SSI_SIER_TFRC_EN)
124 #define FSLSSI_SISR_MASK (FSLSSI_SIER_DBG_RX_FLAGS | FSLSSI_SIER_DBG_TX_FLAGS)
125
126 /**
127  * fsl_ssi_private: per-SSI private data
128  *
129  * @ssi: pointer to the SSI's registers
130  * @ssi_phys: physical address of the SSI registers
131  * @irq: IRQ of this SSI
132  * @playback: the number of playback streams opened
133  * @capture: the number of capture streams opened
134  * @cpu_dai: the CPU DAI for this device
135  * @dev_attr: the sysfs device attribute structure
136  * @stats: SSI statistics
137  * @name: name for this device
138  */
139 struct fsl_ssi_private {
140         struct ccsr_ssi __iomem *ssi;
141         dma_addr_t ssi_phys;
142         unsigned int irq;
143         unsigned int fifo_depth;
144         struct snd_soc_dai_driver cpu_dai_drv;
145         struct platform_device *pdev;
146
147         bool new_binding;
148         bool ssi_on_imx;
149         bool imx_ac97;
150         bool use_dma;
151         bool baudclk_locked;
152         bool irq_stats;
153         u8 i2s_mode;
154         spinlock_t baudclk_lock;
155         struct clk *baudclk;
156         struct clk *clk;
157         struct snd_dmaengine_dai_dma_data dma_params_tx;
158         struct snd_dmaengine_dai_dma_data dma_params_rx;
159         struct imx_dma_data filter_data_tx;
160         struct imx_dma_data filter_data_rx;
161         struct imx_pcm_fiq_params fiq_params;
162
163         struct {
164                 unsigned int rfrc;
165                 unsigned int tfrc;
166                 unsigned int cmdau;
167                 unsigned int cmddu;
168                 unsigned int rxt;
169                 unsigned int rdr1;
170                 unsigned int rdr0;
171                 unsigned int tde1;
172                 unsigned int tde0;
173                 unsigned int roe1;
174                 unsigned int roe0;
175                 unsigned int tue1;
176                 unsigned int tue0;
177                 unsigned int tfs;
178                 unsigned int rfs;
179                 unsigned int tls;
180                 unsigned int rls;
181                 unsigned int rff1;
182                 unsigned int rff0;
183                 unsigned int tfe1;
184                 unsigned int tfe0;
185         } stats;
186         struct dentry *dbg_dir;
187         struct dentry *dbg_stats;
188
189         char name[1];
190 };
191
192 /**
193  * fsl_ssi_isr: SSI interrupt handler
194  *
195  * Although it's possible to use the interrupt handler to send and receive
196  * data to/from the SSI, we use the DMA instead.  Programming is more
197  * complicated, but the performance is much better.
198  *
199  * This interrupt handler is used only to gather statistics.
200  *
201  * @irq: IRQ of the SSI device
202  * @dev_id: pointer to the ssi_private structure for this SSI device
203  */
204 static irqreturn_t fsl_ssi_isr(int irq, void *dev_id)
205 {
206         struct fsl_ssi_private *ssi_private = dev_id;
207         struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
208         irqreturn_t ret = IRQ_NONE;
209         __be32 sisr;
210         __be32 sisr2 = 0;
211
212         /* We got an interrupt, so read the status register to see what we
213            were interrupted for.  We mask it with the Interrupt Enable register
214            so that we only check for events that we're interested in.
215          */
216         sisr = read_ssi(&ssi->sisr) & FSLSSI_SISR_MASK;
217
218         if (sisr & CCSR_SSI_SISR_RFRC) {
219                 ssi_private->stats.rfrc++;
220                 sisr2 |= CCSR_SSI_SISR_RFRC;
221                 ret = IRQ_HANDLED;
222         }
223
224         if (sisr & CCSR_SSI_SISR_TFRC) {
225                 ssi_private->stats.tfrc++;
226                 sisr2 |= CCSR_SSI_SISR_TFRC;
227                 ret = IRQ_HANDLED;
228         }
229
230         if (sisr & CCSR_SSI_SISR_CMDAU) {
231                 ssi_private->stats.cmdau++;
232                 ret = IRQ_HANDLED;
233         }
234
235         if (sisr & CCSR_SSI_SISR_CMDDU) {
236                 ssi_private->stats.cmddu++;
237                 ret = IRQ_HANDLED;
238         }
239
240         if (sisr & CCSR_SSI_SISR_RXT) {
241                 ssi_private->stats.rxt++;
242                 ret = IRQ_HANDLED;
243         }
244
245         if (sisr & CCSR_SSI_SISR_RDR1) {
246                 ssi_private->stats.rdr1++;
247                 ret = IRQ_HANDLED;
248         }
249
250         if (sisr & CCSR_SSI_SISR_RDR0) {
251                 ssi_private->stats.rdr0++;
252                 ret = IRQ_HANDLED;
253         }
254
255         if (sisr & CCSR_SSI_SISR_TDE1) {
256                 ssi_private->stats.tde1++;
257                 ret = IRQ_HANDLED;
258         }
259
260         if (sisr & CCSR_SSI_SISR_TDE0) {
261                 ssi_private->stats.tde0++;
262                 ret = IRQ_HANDLED;
263         }
264
265         if (sisr & CCSR_SSI_SISR_ROE1) {
266                 ssi_private->stats.roe1++;
267                 sisr2 |= CCSR_SSI_SISR_ROE1;
268                 ret = IRQ_HANDLED;
269         }
270
271         if (sisr & CCSR_SSI_SISR_ROE0) {
272                 ssi_private->stats.roe0++;
273                 sisr2 |= CCSR_SSI_SISR_ROE0;
274                 ret = IRQ_HANDLED;
275         }
276
277         if (sisr & CCSR_SSI_SISR_TUE1) {
278                 ssi_private->stats.tue1++;
279                 sisr2 |= CCSR_SSI_SISR_TUE1;
280                 ret = IRQ_HANDLED;
281         }
282
283         if (sisr & CCSR_SSI_SISR_TUE0) {
284                 ssi_private->stats.tue0++;
285                 sisr2 |= CCSR_SSI_SISR_TUE0;
286                 ret = IRQ_HANDLED;
287         }
288
289         if (sisr & CCSR_SSI_SISR_TFS) {
290                 ssi_private->stats.tfs++;
291                 ret = IRQ_HANDLED;
292         }
293
294         if (sisr & CCSR_SSI_SISR_RFS) {
295                 ssi_private->stats.rfs++;
296                 ret = IRQ_HANDLED;
297         }
298
299         if (sisr & CCSR_SSI_SISR_TLS) {
300                 ssi_private->stats.tls++;
301                 ret = IRQ_HANDLED;
302         }
303
304         if (sisr & CCSR_SSI_SISR_RLS) {
305                 ssi_private->stats.rls++;
306                 ret = IRQ_HANDLED;
307         }
308
309         if (sisr & CCSR_SSI_SISR_RFF1) {
310                 ssi_private->stats.rff1++;
311                 ret = IRQ_HANDLED;
312         }
313
314         if (sisr & CCSR_SSI_SISR_RFF0) {
315                 ssi_private->stats.rff0++;
316                 ret = IRQ_HANDLED;
317         }
318
319         if (sisr & CCSR_SSI_SISR_TFE1) {
320                 ssi_private->stats.tfe1++;
321                 ret = IRQ_HANDLED;
322         }
323
324         if (sisr & CCSR_SSI_SISR_TFE0) {
325                 ssi_private->stats.tfe0++;
326                 ret = IRQ_HANDLED;
327         }
328
329         /* Clear the bits that we set */
330         if (sisr2)
331                 write_ssi(sisr2, &ssi->sisr);
332
333         return ret;
334 }
335
336 #if IS_ENABLED(CONFIG_DEBUG_FS)
337 /* Show the statistics of a flag only if its interrupt is enabled.  The
338  * compiler will optimze this code to a no-op if the interrupt is not
339  * enabled.
340  */
341 #define SIER_SHOW(flag, name) \
342         do { \
343                 if (FSLSSI_SISR_MASK & CCSR_SSI_SIER_##flag) \
344                         seq_printf(s, #name "=%u\n", ssi_private->stats.name); \
345         } while (0)
346
347
348 /**
349  * fsl_sysfs_ssi_show: display SSI statistics
350  *
351  * Display the statistics for the current SSI device.  To avoid confusion,
352  * we only show those counts that are enabled.
353  */
354 static ssize_t fsl_ssi_stats_show(struct seq_file *s, void *unused)
355 {
356         struct fsl_ssi_private *ssi_private = s->private;
357
358         SIER_SHOW(RFRC_EN, rfrc);
359         SIER_SHOW(TFRC_EN, tfrc);
360         SIER_SHOW(CMDAU_EN, cmdau);
361         SIER_SHOW(CMDDU_EN, cmddu);
362         SIER_SHOW(RXT_EN, rxt);
363         SIER_SHOW(RDR1_EN, rdr1);
364         SIER_SHOW(RDR0_EN, rdr0);
365         SIER_SHOW(TDE1_EN, tde1);
366         SIER_SHOW(TDE0_EN, tde0);
367         SIER_SHOW(ROE1_EN, roe1);
368         SIER_SHOW(ROE0_EN, roe0);
369         SIER_SHOW(TUE1_EN, tue1);
370         SIER_SHOW(TUE0_EN, tue0);
371         SIER_SHOW(TFS_EN, tfs);
372         SIER_SHOW(RFS_EN, rfs);
373         SIER_SHOW(TLS_EN, tls);
374         SIER_SHOW(RLS_EN, rls);
375         SIER_SHOW(RFF1_EN, rff1);
376         SIER_SHOW(RFF0_EN, rff0);
377         SIER_SHOW(TFE1_EN, tfe1);
378         SIER_SHOW(TFE0_EN, tfe0);
379
380         return 0;
381 }
382
383 static int fsl_ssi_stats_open(struct inode *inode, struct file *file)
384 {
385         return single_open(file, fsl_ssi_stats_show, inode->i_private);
386 }
387
388 static const struct file_operations fsl_ssi_stats_ops = {
389         .open = fsl_ssi_stats_open,
390         .read = seq_read,
391         .llseek = seq_lseek,
392         .release = single_release,
393 };
394
395 static int fsl_ssi_debugfs_create(struct fsl_ssi_private *ssi_private,
396                 struct device *dev)
397 {
398         ssi_private->dbg_dir = debugfs_create_dir(dev_name(dev), NULL);
399         if (!ssi_private->dbg_dir)
400                 return -ENOMEM;
401
402         ssi_private->dbg_stats = debugfs_create_file("stats", S_IRUGO,
403                         ssi_private->dbg_dir, ssi_private, &fsl_ssi_stats_ops);
404         if (!ssi_private->dbg_stats) {
405                 debugfs_remove(ssi_private->dbg_dir);
406                 return -ENOMEM;
407         }
408
409         return 0;
410 }
411
412 static void fsl_ssi_debugfs_remove(struct fsl_ssi_private *ssi_private)
413 {
414         debugfs_remove(ssi_private->dbg_stats);
415         debugfs_remove(ssi_private->dbg_dir);
416 }
417
418 #else
419
420 static int fsl_ssi_debugfs_create(struct fsl_ssi_private *ssi_private,
421                 struct device *dev)
422 {
423         return 0;
424 }
425
426 static void fsl_ssi_debugfs_remove(struct fsl_ssi_private *ssi_private)
427 {
428 }
429
430 #endif /* IS_ENABLED(CONFIG_DEBUG_FS) */
431
432 static void fsl_ssi_setup_ac97(struct fsl_ssi_private *ssi_private)
433 {
434         struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
435
436         /*
437          * Setup the clock control register
438          */
439         write_ssi(CCSR_SSI_SxCCR_WL(17) | CCSR_SSI_SxCCR_DC(13),
440                         &ssi->stccr);
441         write_ssi(CCSR_SSI_SxCCR_WL(17) | CCSR_SSI_SxCCR_DC(13),
442                         &ssi->srccr);
443
444         /*
445          * Enable AC97 mode and startup the SSI
446          */
447         write_ssi(CCSR_SSI_SACNT_AC97EN | CCSR_SSI_SACNT_FV,
448                         &ssi->sacnt);
449         write_ssi(0xff, &ssi->saccdis);
450         write_ssi(0x300, &ssi->saccen);
451
452         /*
453          * Enable SSI, Transmit and Receive. AC97 has to communicate with the
454          * codec before a stream is started.
455          */
456         write_ssi_mask(&ssi->scr, 0, CCSR_SSI_SCR_SSIEN |
457                         CCSR_SSI_SCR_TE | CCSR_SSI_SCR_RE);
458
459         write_ssi(CCSR_SSI_SOR_WAIT(3), &ssi->sor);
460 }
461
462 static int fsl_ssi_setup(struct fsl_ssi_private *ssi_private)
463 {
464         struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
465         u8 wm;
466         int synchronous = ssi_private->cpu_dai_drv.symmetric_rates;
467
468         if (ssi_private->imx_ac97)
469                 ssi_private->i2s_mode = CCSR_SSI_SCR_I2S_MODE_NORMAL | CCSR_SSI_SCR_NET;
470         else
471                 ssi_private->i2s_mode = CCSR_SSI_SCR_I2S_MODE_SLAVE;
472
473         /*
474          * Section 16.5 of the MPC8610 reference manual says that the SSI needs
475          * to be disabled before updating the registers we set here.
476          */
477         write_ssi_mask(&ssi->scr, CCSR_SSI_SCR_SSIEN, 0);
478
479         /*
480          * Program the SSI into I2S Slave Non-Network Synchronous mode. Also
481          * enable the transmit and receive FIFO.
482          *
483          * FIXME: Little-endian samples require a different shift dir
484          */
485         write_ssi_mask(&ssi->scr,
486                 CCSR_SSI_SCR_I2S_MODE_MASK | CCSR_SSI_SCR_SYN,
487                 CCSR_SSI_SCR_TFR_CLK_DIS |
488                 ssi_private->i2s_mode |
489                 (synchronous ? CCSR_SSI_SCR_SYN : 0));
490
491         write_ssi(CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TFEN0 |
492                  CCSR_SSI_STCR_TFSI | CCSR_SSI_STCR_TEFS |
493                  CCSR_SSI_STCR_TSCKP, &ssi->stcr);
494
495         write_ssi(CCSR_SSI_SRCR_RXBIT0 | CCSR_SSI_SRCR_RFEN0 |
496                  CCSR_SSI_SRCR_RFSI | CCSR_SSI_SRCR_REFS |
497                  CCSR_SSI_SRCR_RSCKP, &ssi->srcr);
498         /*
499          * The DC and PM bits are only used if the SSI is the clock master.
500          */
501
502         /*
503          * Set the watermark for transmit FIFI 0 and receive FIFO 0. We don't
504          * use FIFO 1. We program the transmit water to signal a DMA transfer
505          * if there are only two (or fewer) elements left in the FIFO. Two
506          * elements equals one frame (left channel, right channel). This value,
507          * however, depends on the depth of the transmit buffer.
508          *
509          * We set the watermark on the same level as the DMA burstsize.  For
510          * fiq it is probably better to use the biggest possible watermark
511          * size.
512          */
513         if (ssi_private->use_dma)
514                 wm = ssi_private->fifo_depth - 2;
515         else
516                 wm = ssi_private->fifo_depth;
517
518         write_ssi(CCSR_SSI_SFCSR_TFWM0(wm) | CCSR_SSI_SFCSR_RFWM0(wm) |
519                 CCSR_SSI_SFCSR_TFWM1(wm) | CCSR_SSI_SFCSR_RFWM1(wm),
520                 &ssi->sfcsr);
521
522         /*
523          * For ac97 interrupts are enabled with the startup of the substream
524          * because it is also running without an active substream. Normally SSI
525          * is only enabled when there is a substream.
526          */
527         if (ssi_private->imx_ac97)
528                 fsl_ssi_setup_ac97(ssi_private);
529
530         return 0;
531 }
532
533
534 /**
535  * fsl_ssi_startup: create a new substream
536  *
537  * This is the first function called when a stream is opened.
538  *
539  * If this is the first stream open, then grab the IRQ and program most of
540  * the SSI registers.
541  */
542 static int fsl_ssi_startup(struct snd_pcm_substream *substream,
543                            struct snd_soc_dai *dai)
544 {
545         struct snd_soc_pcm_runtime *rtd = substream->private_data;
546         struct fsl_ssi_private *ssi_private =
547                 snd_soc_dai_get_drvdata(rtd->cpu_dai);
548         unsigned long flags;
549
550         /* First, we only do fsl_ssi_setup() when SSI is going to be active.
551          * Second, fsl_ssi_setup was already called by ac97_init earlier if
552          * the driver is in ac97 mode.
553          */
554         if (!dai->active && !ssi_private->imx_ac97) {
555                 fsl_ssi_setup(ssi_private);
556                 spin_lock_irqsave(&ssi_private->baudclk_lock, flags);
557                 ssi_private->baudclk_locked = false;
558                 spin_unlock_irqrestore(&ssi_private->baudclk_lock, flags);
559         }
560
561         return 0;
562 }
563
564 /**
565  * fsl_ssi_hw_params - program the sample size
566  *
567  * Most of the SSI registers have been programmed in the startup function,
568  * but the word length must be programmed here.  Unfortunately, programming
569  * the SxCCR.WL bits requires the SSI to be temporarily disabled.  This can
570  * cause a problem with supporting simultaneous playback and capture.  If
571  * the SSI is already playing a stream, then that stream may be temporarily
572  * stopped when you start capture.
573  *
574  * Note: The SxCCR.DC and SxCCR.PM bits are only used if the SSI is the
575  * clock master.
576  */
577 static int fsl_ssi_hw_params(struct snd_pcm_substream *substream,
578         struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *cpu_dai)
579 {
580         struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
581         struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
582         unsigned int channels = params_channels(hw_params);
583         unsigned int sample_size =
584                 snd_pcm_format_width(params_format(hw_params));
585         u32 wl = CCSR_SSI_SxCCR_WL(sample_size);
586         int enabled = read_ssi(&ssi->scr) & CCSR_SSI_SCR_SSIEN;
587
588         /*
589          * If we're in synchronous mode, and the SSI is already enabled,
590          * then STCCR is already set properly.
591          */
592         if (enabled && ssi_private->cpu_dai_drv.symmetric_rates)
593                 return 0;
594
595         /*
596          * FIXME: The documentation says that SxCCR[WL] should not be
597          * modified while the SSI is enabled.  The only time this can
598          * happen is if we're trying to do simultaneous playback and
599          * capture in asynchronous mode.  Unfortunately, I have been enable
600          * to get that to work at all on the P1022DS.  Therefore, we don't
601          * bother to disable/enable the SSI when setting SxCCR[WL], because
602          * the SSI will stop anyway.  Maybe one day, this will get fixed.
603          */
604
605         /* In synchronous mode, the SSI uses STCCR for capture */
606         if ((substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ||
607             ssi_private->cpu_dai_drv.symmetric_rates)
608                 write_ssi_mask(&ssi->stccr, CCSR_SSI_SxCCR_WL_MASK, wl);
609         else
610                 write_ssi_mask(&ssi->srccr, CCSR_SSI_SxCCR_WL_MASK, wl);
611
612         if (!ssi_private->imx_ac97)
613                 write_ssi_mask(&ssi->scr,
614                                 CCSR_SSI_SCR_NET | CCSR_SSI_SCR_I2S_MODE_MASK,
615                                 channels == 1 ? 0 : ssi_private->i2s_mode);
616
617         return 0;
618 }
619
620 /**
621  * fsl_ssi_set_dai_fmt - configure Digital Audio Interface Format.
622  */
623 static int fsl_ssi_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
624 {
625         struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
626         struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
627         u32 strcr = 0, stcr, srcr, scr, mask;
628
629         scr = read_ssi(&ssi->scr) & ~(CCSR_SSI_SCR_SYN | CCSR_SSI_SCR_I2S_MODE_MASK);
630         scr |= CCSR_SSI_SCR_NET;
631
632         mask = CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TFDIR | CCSR_SSI_STCR_TXDIR |
633                 CCSR_SSI_STCR_TSCKP | CCSR_SSI_STCR_TFSI | CCSR_SSI_STCR_TFSL |
634                 CCSR_SSI_STCR_TEFS;
635         stcr = read_ssi(&ssi->stcr) & ~mask;
636         srcr = read_ssi(&ssi->srcr) & ~mask;
637
638         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
639         case SND_SOC_DAIFMT_I2S:
640                 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
641                 case SND_SOC_DAIFMT_CBS_CFS:
642                         ssi_private->i2s_mode = CCSR_SSI_SCR_I2S_MODE_MASTER;
643                         break;
644                 case SND_SOC_DAIFMT_CBM_CFM:
645                         ssi_private->i2s_mode = CCSR_SSI_SCR_I2S_MODE_SLAVE;
646                         break;
647                 default:
648                         return -EINVAL;
649                 }
650                 scr |= ssi_private->i2s_mode;
651
652                 /* Data on rising edge of bclk, frame low, 1clk before data */
653                 strcr |= CCSR_SSI_STCR_TFSI | CCSR_SSI_STCR_TSCKP |
654                         CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TEFS;
655                 break;
656         case SND_SOC_DAIFMT_LEFT_J:
657                 /* Data on rising edge of bclk, frame high */
658                 strcr |= CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TSCKP;
659                 break;
660         case SND_SOC_DAIFMT_DSP_A:
661                 /* Data on rising edge of bclk, frame high, 1clk before data */
662                 strcr |= CCSR_SSI_STCR_TFSL | CCSR_SSI_STCR_TSCKP |
663                         CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TEFS;
664                 break;
665         case SND_SOC_DAIFMT_DSP_B:
666                 /* Data on rising edge of bclk, frame high */
667                 strcr |= CCSR_SSI_STCR_TFSL | CCSR_SSI_STCR_TSCKP |
668                         CCSR_SSI_STCR_TXBIT0;
669                 break;
670         default:
671                 return -EINVAL;
672         }
673
674         /* DAI clock inversion */
675         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
676         case SND_SOC_DAIFMT_NB_NF:
677                 /* Nothing to do for both normal cases */
678                 break;
679         case SND_SOC_DAIFMT_IB_NF:
680                 /* Invert bit clock */
681                 strcr ^= CCSR_SSI_STCR_TSCKP;
682                 break;
683         case SND_SOC_DAIFMT_NB_IF:
684                 /* Invert frame clock */
685                 strcr ^= CCSR_SSI_STCR_TFSI;
686                 break;
687         case SND_SOC_DAIFMT_IB_IF:
688                 /* Invert both clocks */
689                 strcr ^= CCSR_SSI_STCR_TSCKP;
690                 strcr ^= CCSR_SSI_STCR_TFSI;
691                 break;
692         default:
693                 return -EINVAL;
694         }
695
696         /* DAI clock master masks */
697         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
698         case SND_SOC_DAIFMT_CBS_CFS:
699                 strcr |= CCSR_SSI_STCR_TFDIR | CCSR_SSI_STCR_TXDIR;
700                 scr |= CCSR_SSI_SCR_SYS_CLK_EN;
701                 break;
702         case SND_SOC_DAIFMT_CBM_CFM:
703                 scr &= ~CCSR_SSI_SCR_SYS_CLK_EN;
704                 break;
705         default:
706                 return -EINVAL;
707         }
708
709         stcr |= strcr;
710         srcr |= strcr;
711
712         if (ssi_private->cpu_dai_drv.symmetric_rates) {
713                 /* Need to clear RXDIR when using SYNC mode */
714                 srcr &= ~CCSR_SSI_SRCR_RXDIR;
715                 scr |= CCSR_SSI_SCR_SYN;
716         }
717
718         write_ssi(stcr, &ssi->stcr);
719         write_ssi(srcr, &ssi->srcr);
720         write_ssi(scr, &ssi->scr);
721
722         return 0;
723 }
724
725 /**
726  * fsl_ssi_set_dai_sysclk - configure Digital Audio Interface bit clock
727  *
728  * Note: This function can be only called when using SSI as DAI master
729  *
730  * Quick instruction for parameters:
731  * freq: Output BCLK frequency = samplerate * 32 (fixed) * channels
732  * dir: SND_SOC_CLOCK_OUT -> TxBCLK, SND_SOC_CLOCK_IN -> RxBCLK.
733  */
734 static int fsl_ssi_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
735                                   int clk_id, unsigned int freq, int dir)
736 {
737         struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
738         struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
739         int synchronous = ssi_private->cpu_dai_drv.symmetric_rates, ret;
740         u32 pm = 999, div2, psr, stccr, mask, afreq, factor, i;
741         unsigned long flags, clkrate, baudrate, tmprate;
742         u64 sub, savesub = 100000;
743
744         /* Don't apply it to any non-baudclk circumstance */
745         if (IS_ERR(ssi_private->baudclk))
746                 return -EINVAL;
747
748         /* It should be already enough to divide clock by setting pm alone */
749         psr = 0;
750         div2 = 0;
751
752         factor = (div2 + 1) * (7 * psr + 1) * 2;
753
754         for (i = 0; i < 255; i++) {
755                 /* The bclk rate must be smaller than 1/5 sysclk rate */
756                 if (factor * (i + 1) < 5)
757                         continue;
758
759                 tmprate = freq * factor * (i + 2);
760                 clkrate = clk_round_rate(ssi_private->baudclk, tmprate);
761
762                 do_div(clkrate, factor);
763                 afreq = (u32)clkrate / (i + 1);
764
765                 if (freq == afreq)
766                         sub = 0;
767                 else if (freq / afreq == 1)
768                         sub = freq - afreq;
769                 else if (afreq / freq == 1)
770                         sub = afreq - freq;
771                 else
772                         continue;
773
774                 /* Calculate the fraction */
775                 sub *= 100000;
776                 do_div(sub, freq);
777
778                 if (sub < savesub) {
779                         baudrate = tmprate;
780                         savesub = sub;
781                         pm = i;
782                 }
783
784                 /* We are lucky */
785                 if (savesub == 0)
786                         break;
787         }
788
789         /* No proper pm found if it is still remaining the initial value */
790         if (pm == 999) {
791                 dev_err(cpu_dai->dev, "failed to handle the required sysclk\n");
792                 return -EINVAL;
793         }
794
795         stccr = CCSR_SSI_SxCCR_PM(pm + 1) | (div2 ? CCSR_SSI_SxCCR_DIV2 : 0) |
796                 (psr ? CCSR_SSI_SxCCR_PSR : 0);
797         mask = CCSR_SSI_SxCCR_PM_MASK | CCSR_SSI_SxCCR_DIV2 | CCSR_SSI_SxCCR_PSR;
798
799         if (dir == SND_SOC_CLOCK_OUT || synchronous)
800                 write_ssi_mask(&ssi->stccr, mask, stccr);
801         else
802                 write_ssi_mask(&ssi->srccr, mask, stccr);
803
804         spin_lock_irqsave(&ssi_private->baudclk_lock, flags);
805         if (!ssi_private->baudclk_locked) {
806                 ret = clk_set_rate(ssi_private->baudclk, baudrate);
807                 if (ret) {
808                         spin_unlock_irqrestore(&ssi_private->baudclk_lock, flags);
809                         dev_err(cpu_dai->dev, "failed to set baudclk rate\n");
810                         return -EINVAL;
811                 }
812                 ssi_private->baudclk_locked = true;
813         }
814         spin_unlock_irqrestore(&ssi_private->baudclk_lock, flags);
815
816         return 0;
817 }
818
819 /**
820  * fsl_ssi_set_dai_tdm_slot - set TDM slot number
821  *
822  * Note: This function can be only called when using SSI as DAI master
823  */
824 static int fsl_ssi_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai, u32 tx_mask,
825                                 u32 rx_mask, int slots, int slot_width)
826 {
827         struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
828         struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
829         u32 val;
830
831         /* The slot number should be >= 2 if using Network mode or I2S mode */
832         val = read_ssi(&ssi->scr) & (CCSR_SSI_SCR_I2S_MODE_MASK | CCSR_SSI_SCR_NET);
833         if (val && slots < 2) {
834                 dev_err(cpu_dai->dev, "slot number should be >= 2 in I2S or NET\n");
835                 return -EINVAL;
836         }
837
838         write_ssi_mask(&ssi->stccr, CCSR_SSI_SxCCR_DC_MASK,
839                         CCSR_SSI_SxCCR_DC(slots));
840         write_ssi_mask(&ssi->srccr, CCSR_SSI_SxCCR_DC_MASK,
841                         CCSR_SSI_SxCCR_DC(slots));
842
843         /* The register SxMSKs needs SSI to provide essential clock due to
844          * hardware design. So we here temporarily enable SSI to set them.
845          */
846         val = read_ssi(&ssi->scr) & CCSR_SSI_SCR_SSIEN;
847         write_ssi_mask(&ssi->scr, 0, CCSR_SSI_SCR_SSIEN);
848
849         write_ssi(tx_mask, &ssi->stmsk);
850         write_ssi(rx_mask, &ssi->srmsk);
851
852         write_ssi_mask(&ssi->scr, CCSR_SSI_SCR_SSIEN, val);
853
854         return 0;
855 }
856
857 /**
858  * fsl_ssi_trigger: start and stop the DMA transfer.
859  *
860  * This function is called by ALSA to start, stop, pause, and resume the DMA
861  * transfer of data.
862  *
863  * The DMA channel is in external master start and pause mode, which
864  * means the SSI completely controls the flow of data.
865  */
866 static int fsl_ssi_trigger(struct snd_pcm_substream *substream, int cmd,
867                            struct snd_soc_dai *dai)
868 {
869         struct snd_soc_pcm_runtime *rtd = substream->private_data;
870         struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(rtd->cpu_dai);
871         struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
872         unsigned int sier_bits;
873         unsigned long flags;
874
875         /*
876          *  Enable only the interrupts and DMA requests
877          *  that are needed for the channel. As the fiq
878          *  is polling for this bits, we have to ensure
879          *  that this are aligned with the preallocated
880          *  buffers
881          */
882
883         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
884                 if (ssi_private->use_dma)
885                         sier_bits = SIER_FLAGS;
886                 else
887                         sier_bits = CCSR_SSI_SIER_TIE | CCSR_SSI_SIER_TFE0_EN;
888         } else {
889                 if (ssi_private->use_dma)
890                         sier_bits = SIER_FLAGS;
891                 else
892                         sier_bits = CCSR_SSI_SIER_RIE | CCSR_SSI_SIER_RFF0_EN;
893         }
894
895         switch (cmd) {
896         case SNDRV_PCM_TRIGGER_START:
897         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
898                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
899                         write_ssi_mask(&ssi->scr, 0,
900                                 CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_TE);
901                 else
902                         write_ssi_mask(&ssi->scr, 0,
903                                 CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_RE);
904                 break;
905
906         case SNDRV_PCM_TRIGGER_STOP:
907         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
908                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
909                         write_ssi_mask(&ssi->scr, CCSR_SSI_SCR_TE, 0);
910                 else
911                         write_ssi_mask(&ssi->scr, CCSR_SSI_SCR_RE, 0);
912
913                 if (!ssi_private->imx_ac97 && (read_ssi(&ssi->scr) &
914                                         (CCSR_SSI_SCR_TE | CCSR_SSI_SCR_RE)) == 0) {
915                         write_ssi_mask(&ssi->scr, CCSR_SSI_SCR_SSIEN, 0);
916                         spin_lock_irqsave(&ssi_private->baudclk_lock, flags);
917                         ssi_private->baudclk_locked = false;
918                         spin_unlock_irqrestore(&ssi_private->baudclk_lock, flags);
919                 }
920                 break;
921
922         default:
923                 return -EINVAL;
924         }
925
926         write_ssi(sier_bits, &ssi->sier);
927
928         return 0;
929 }
930
931 static int fsl_ssi_dai_probe(struct snd_soc_dai *dai)
932 {
933         struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(dai);
934
935         if (ssi_private->ssi_on_imx && ssi_private->use_dma) {
936                 dai->playback_dma_data = &ssi_private->dma_params_tx;
937                 dai->capture_dma_data = &ssi_private->dma_params_rx;
938         }
939
940         return 0;
941 }
942
943 static const struct snd_soc_dai_ops fsl_ssi_dai_ops = {
944         .startup        = fsl_ssi_startup,
945         .hw_params      = fsl_ssi_hw_params,
946         .set_fmt        = fsl_ssi_set_dai_fmt,
947         .set_sysclk     = fsl_ssi_set_dai_sysclk,
948         .set_tdm_slot   = fsl_ssi_set_dai_tdm_slot,
949         .trigger        = fsl_ssi_trigger,
950 };
951
952 /* Template for the CPU dai driver structure */
953 static struct snd_soc_dai_driver fsl_ssi_dai_template = {
954         .probe = fsl_ssi_dai_probe,
955         .playback = {
956                 .channels_min = 1,
957                 .channels_max = 2,
958                 .rates = FSLSSI_I2S_RATES,
959                 .formats = FSLSSI_I2S_FORMATS,
960         },
961         .capture = {
962                 .channels_min = 1,
963                 .channels_max = 2,
964                 .rates = FSLSSI_I2S_RATES,
965                 .formats = FSLSSI_I2S_FORMATS,
966         },
967         .ops = &fsl_ssi_dai_ops,
968 };
969
970 static const struct snd_soc_component_driver fsl_ssi_component = {
971         .name           = "fsl-ssi",
972 };
973
974 /**
975  * fsl_ssi_ac97_trigger: start and stop the AC97 receive/transmit.
976  *
977  * This function is called by ALSA to start, stop, pause, and resume the
978  * transfer of data.
979  */
980 static int fsl_ssi_ac97_trigger(struct snd_pcm_substream *substream, int cmd,
981                            struct snd_soc_dai *dai)
982 {
983         struct snd_soc_pcm_runtime *rtd = substream->private_data;
984         struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(
985                         rtd->cpu_dai);
986         struct ccsr_ssi __iomem *ssi = ssi_private->ssi;
987
988         switch (cmd) {
989         case SNDRV_PCM_TRIGGER_START:
990         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
991                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
992                         write_ssi_mask(&ssi->sier, 0, CCSR_SSI_SIER_TIE |
993                                         CCSR_SSI_SIER_TFE0_EN);
994                 else
995                         write_ssi_mask(&ssi->sier, 0, CCSR_SSI_SIER_RIE |
996                                         CCSR_SSI_SIER_RFF0_EN);
997                 break;
998
999         case SNDRV_PCM_TRIGGER_STOP:
1000         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1001                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1002                         write_ssi_mask(&ssi->sier, CCSR_SSI_SIER_TIE |
1003                                         CCSR_SSI_SIER_TFE0_EN, 0);
1004                 else
1005                         write_ssi_mask(&ssi->sier, CCSR_SSI_SIER_RIE |
1006                                         CCSR_SSI_SIER_RFF0_EN, 0);
1007                 break;
1008
1009         default:
1010                 return -EINVAL;
1011         }
1012
1013         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1014                 write_ssi(CCSR_SSI_SOR_TX_CLR, &ssi->sor);
1015         else
1016                 write_ssi(CCSR_SSI_SOR_RX_CLR, &ssi->sor);
1017
1018         return 0;
1019 }
1020
1021 static const struct snd_soc_dai_ops fsl_ssi_ac97_dai_ops = {
1022         .startup        = fsl_ssi_startup,
1023         .trigger        = fsl_ssi_ac97_trigger,
1024 };
1025
1026 static struct snd_soc_dai_driver fsl_ssi_ac97_dai = {
1027         .ac97_control = 1,
1028         .playback = {
1029                 .stream_name = "AC97 Playback",
1030                 .channels_min = 2,
1031                 .channels_max = 2,
1032                 .rates = SNDRV_PCM_RATE_8000_48000,
1033                 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1034         },
1035         .capture = {
1036                 .stream_name = "AC97 Capture",
1037                 .channels_min = 2,
1038                 .channels_max = 2,
1039                 .rates = SNDRV_PCM_RATE_48000,
1040                 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1041         },
1042         .ops = &fsl_ssi_ac97_dai_ops,
1043 };
1044
1045
1046 static struct fsl_ssi_private *fsl_ac97_data;
1047
1048 static void fsl_ssi_ac97_init(void)
1049 {
1050         fsl_ssi_setup(fsl_ac97_data);
1051 }
1052
1053 static void fsl_ssi_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
1054                 unsigned short val)
1055 {
1056         struct ccsr_ssi *ssi = fsl_ac97_data->ssi;
1057         unsigned int lreg;
1058         unsigned int lval;
1059
1060         if (reg > 0x7f)
1061                 return;
1062
1063
1064         lreg = reg <<  12;
1065         write_ssi(lreg, &ssi->sacadd);
1066
1067         lval = val << 4;
1068         write_ssi(lval , &ssi->sacdat);
1069
1070         write_ssi_mask(&ssi->sacnt, CCSR_SSI_SACNT_RDWR_MASK,
1071                         CCSR_SSI_SACNT_WR);
1072         udelay(100);
1073 }
1074
1075 static unsigned short fsl_ssi_ac97_read(struct snd_ac97 *ac97,
1076                 unsigned short reg)
1077 {
1078         struct ccsr_ssi *ssi = fsl_ac97_data->ssi;
1079
1080         unsigned short val = -1;
1081         unsigned int lreg;
1082
1083         lreg = (reg & 0x7f) <<  12;
1084         write_ssi(lreg, &ssi->sacadd);
1085         write_ssi_mask(&ssi->sacnt, CCSR_SSI_SACNT_RDWR_MASK,
1086                         CCSR_SSI_SACNT_RD);
1087
1088         udelay(100);
1089
1090         val = (read_ssi(&ssi->sacdat) >> 4) & 0xffff;
1091
1092         return val;
1093 }
1094
1095 static struct snd_ac97_bus_ops fsl_ssi_ac97_ops = {
1096         .read           = fsl_ssi_ac97_read,
1097         .write          = fsl_ssi_ac97_write,
1098 };
1099
1100 /**
1101  * Make every character in a string lower-case
1102  */
1103 static void make_lowercase(char *s)
1104 {
1105         char *p = s;
1106         char c;
1107
1108         while ((c = *p)) {
1109                 if ((c >= 'A') && (c <= 'Z'))
1110                         *p = c + ('a' - 'A');
1111                 p++;
1112         }
1113 }
1114
1115 static int fsl_ssi_probe(struct platform_device *pdev)
1116 {
1117         struct fsl_ssi_private *ssi_private;
1118         int ret = 0;
1119         struct device_attribute *dev_attr = NULL;
1120         struct device_node *np = pdev->dev.of_node;
1121         const char *p, *sprop;
1122         const uint32_t *iprop;
1123         struct resource res;
1124         char name[64];
1125         bool shared;
1126         bool ac97 = false;
1127
1128         /* SSIs that are not connected on the board should have a
1129          *      status = "disabled"
1130          * property in their device tree nodes.
1131          */
1132         if (!of_device_is_available(np))
1133                 return -ENODEV;
1134
1135         /* We only support the SSI in "I2S Slave" mode */
1136         sprop = of_get_property(np, "fsl,mode", NULL);
1137         if (!sprop) {
1138                 dev_err(&pdev->dev, "fsl,mode property is necessary\n");
1139                 return -EINVAL;
1140         }
1141         if (!strcmp(sprop, "ac97-slave")) {
1142                 ac97 = true;
1143         } else if (strcmp(sprop, "i2s-slave")) {
1144                 dev_notice(&pdev->dev, "mode %s is unsupported\n", sprop);
1145                 return -ENODEV;
1146         }
1147
1148         /* The DAI name is the last part of the full name of the node. */
1149         p = strrchr(np->full_name, '/') + 1;
1150         ssi_private = devm_kzalloc(&pdev->dev, sizeof(*ssi_private) + strlen(p),
1151                               GFP_KERNEL);
1152         if (!ssi_private) {
1153                 dev_err(&pdev->dev, "could not allocate DAI object\n");
1154                 return -ENOMEM;
1155         }
1156
1157         strcpy(ssi_private->name, p);
1158
1159         ssi_private->use_dma = !of_property_read_bool(np,
1160                         "fsl,fiq-stream-filter");
1161
1162         if (ac97) {
1163                 memcpy(&ssi_private->cpu_dai_drv, &fsl_ssi_ac97_dai,
1164                                 sizeof(fsl_ssi_ac97_dai));
1165
1166                 fsl_ac97_data = ssi_private;
1167                 ssi_private->imx_ac97 = true;
1168
1169                 snd_soc_set_ac97_ops_of_reset(&fsl_ssi_ac97_ops, pdev);
1170         } else {
1171                 /* Initialize this copy of the CPU DAI driver structure */
1172                 memcpy(&ssi_private->cpu_dai_drv, &fsl_ssi_dai_template,
1173                        sizeof(fsl_ssi_dai_template));
1174         }
1175         ssi_private->cpu_dai_drv.name = ssi_private->name;
1176
1177         /* Get the addresses and IRQ */
1178         ret = of_address_to_resource(np, 0, &res);
1179         if (ret) {
1180                 dev_err(&pdev->dev, "could not determine device resources\n");
1181                 return ret;
1182         }
1183         ssi_private->ssi = of_iomap(np, 0);
1184         if (!ssi_private->ssi) {
1185                 dev_err(&pdev->dev, "could not map device resources\n");
1186                 return -ENOMEM;
1187         }
1188         ssi_private->ssi_phys = res.start;
1189
1190         ssi_private->irq = irq_of_parse_and_map(np, 0);
1191         if (!ssi_private->irq) {
1192                 dev_err(&pdev->dev, "no irq for node %s\n", np->full_name);
1193                 return -ENXIO;
1194         }
1195
1196         /* Are the RX and the TX clocks locked? */
1197         if (!of_find_property(np, "fsl,ssi-asynchronous", NULL)) {
1198                 ssi_private->cpu_dai_drv.symmetric_rates = 1;
1199                 ssi_private->cpu_dai_drv.symmetric_channels = 1;
1200                 ssi_private->cpu_dai_drv.symmetric_samplebits = 1;
1201         }
1202
1203         /* Determine the FIFO depth. */
1204         iprop = of_get_property(np, "fsl,fifo-depth", NULL);
1205         if (iprop)
1206                 ssi_private->fifo_depth = be32_to_cpup(iprop);
1207         else
1208                 /* Older 8610 DTs didn't have the fifo-depth property */
1209                 ssi_private->fifo_depth = 8;
1210
1211         ssi_private->baudclk_locked = false;
1212         spin_lock_init(&ssi_private->baudclk_lock);
1213
1214         if (of_device_is_compatible(pdev->dev.of_node, "fsl,imx21-ssi")) {
1215                 u32 dma_events[2];
1216                 ssi_private->ssi_on_imx = true;
1217
1218                 ssi_private->clk = devm_clk_get(&pdev->dev, NULL);
1219                 if (IS_ERR(ssi_private->clk)) {
1220                         ret = PTR_ERR(ssi_private->clk);
1221                         dev_err(&pdev->dev, "could not get clock: %d\n", ret);
1222                         goto error_irqmap;
1223                 }
1224                 ret = clk_prepare_enable(ssi_private->clk);
1225                 if (ret) {
1226                         dev_err(&pdev->dev, "clk_prepare_enable failed: %d\n",
1227                                 ret);
1228                         goto error_irqmap;
1229                 }
1230
1231                 /* For those SLAVE implementations, we ingore non-baudclk cases
1232                  * and, instead, abandon MASTER mode that needs baud clock.
1233                  */
1234                 ssi_private->baudclk = devm_clk_get(&pdev->dev, "baud");
1235                 if (IS_ERR(ssi_private->baudclk))
1236                         dev_warn(&pdev->dev, "could not get baud clock: %ld\n",
1237                                  PTR_ERR(ssi_private->baudclk));
1238                 else
1239                         clk_prepare_enable(ssi_private->baudclk);
1240
1241                 /*
1242                  * We have burstsize be "fifo_depth - 2" to match the SSI
1243                  * watermark setting in fsl_ssi_startup().
1244                  */
1245                 ssi_private->dma_params_tx.maxburst =
1246                         ssi_private->fifo_depth - 2;
1247                 ssi_private->dma_params_rx.maxburst =
1248                         ssi_private->fifo_depth - 2;
1249                 ssi_private->dma_params_tx.addr =
1250                         ssi_private->ssi_phys + offsetof(struct ccsr_ssi, stx0);
1251                 ssi_private->dma_params_rx.addr =
1252                         ssi_private->ssi_phys + offsetof(struct ccsr_ssi, srx0);
1253                 ssi_private->dma_params_tx.filter_data =
1254                         &ssi_private->filter_data_tx;
1255                 ssi_private->dma_params_rx.filter_data =
1256                         &ssi_private->filter_data_rx;
1257                 if (!of_property_read_bool(pdev->dev.of_node, "dmas") &&
1258                                 ssi_private->use_dma) {
1259                         /*
1260                          * FIXME: This is a temporary solution until all
1261                          * necessary dma drivers support the generic dma
1262                          * bindings.
1263                          */
1264                         ret = of_property_read_u32_array(pdev->dev.of_node,
1265                                         "fsl,ssi-dma-events", dma_events, 2);
1266                         if (ret && ssi_private->use_dma) {
1267                                 dev_err(&pdev->dev, "could not get dma events but fsl-ssi is configured to use DMA\n");
1268                                 goto error_clk;
1269                         }
1270                 }
1271
1272                 shared = of_device_is_compatible(of_get_parent(np),
1273                             "fsl,spba-bus");
1274
1275                 imx_pcm_dma_params_init_data(&ssi_private->filter_data_tx,
1276                         dma_events[0], shared ? IMX_DMATYPE_SSI_SP : IMX_DMATYPE_SSI);
1277                 imx_pcm_dma_params_init_data(&ssi_private->filter_data_rx,
1278                         dma_events[1], shared ? IMX_DMATYPE_SSI_SP : IMX_DMATYPE_SSI);
1279         } else if (ssi_private->use_dma) {
1280                 /* The 'name' should not have any slashes in it. */
1281                 ret = devm_request_irq(&pdev->dev, ssi_private->irq,
1282                                         fsl_ssi_isr, 0, ssi_private->name,
1283                                         ssi_private);
1284                 ssi_private->irq_stats = true;
1285                 if (ret < 0) {
1286                         dev_err(&pdev->dev, "could not claim irq %u\n",
1287                                         ssi_private->irq);
1288                         goto error_irqmap;
1289                 }
1290         }
1291
1292         /* Register with ASoC */
1293         dev_set_drvdata(&pdev->dev, ssi_private);
1294
1295         ret = snd_soc_register_component(&pdev->dev, &fsl_ssi_component,
1296                                          &ssi_private->cpu_dai_drv, 1);
1297         if (ret) {
1298                 dev_err(&pdev->dev, "failed to register DAI: %d\n", ret);
1299                 goto error_dev;
1300         }
1301
1302         ret = fsl_ssi_debugfs_create(ssi_private, &pdev->dev);
1303         if (ret)
1304                 goto error_dbgfs;
1305
1306         if (ssi_private->ssi_on_imx) {
1307                 if (!ssi_private->use_dma) {
1308
1309                         /*
1310                          * Some boards use an incompatible codec. To get it
1311                          * working, we are using imx-fiq-pcm-audio, that
1312                          * can handle those codecs. DMA is not possible in this
1313                          * situation.
1314                          */
1315
1316                         ssi_private->fiq_params.irq = ssi_private->irq;
1317                         ssi_private->fiq_params.base = ssi_private->ssi;
1318                         ssi_private->fiq_params.dma_params_rx =
1319                                 &ssi_private->dma_params_rx;
1320                         ssi_private->fiq_params.dma_params_tx =
1321                                 &ssi_private->dma_params_tx;
1322
1323                         ret = imx_pcm_fiq_init(pdev, &ssi_private->fiq_params);
1324                         if (ret)
1325                                 goto error_pcm;
1326                 } else {
1327                         ret = imx_pcm_dma_init(pdev);
1328                         if (ret)
1329                                 goto error_pcm;
1330                 }
1331         }
1332
1333         /*
1334          * If codec-handle property is missing from SSI node, we assume
1335          * that the machine driver uses new binding which does not require
1336          * SSI driver to trigger machine driver's probe.
1337          */
1338         if (!of_get_property(np, "codec-handle", NULL)) {
1339                 ssi_private->new_binding = true;
1340                 goto done;
1341         }
1342
1343         /* Trigger the machine driver's probe function.  The platform driver
1344          * name of the machine driver is taken from /compatible property of the
1345          * device tree.  We also pass the address of the CPU DAI driver
1346          * structure.
1347          */
1348         sprop = of_get_property(of_find_node_by_path("/"), "compatible", NULL);
1349         /* Sometimes the compatible name has a "fsl," prefix, so we strip it. */
1350         p = strrchr(sprop, ',');
1351         if (p)
1352                 sprop = p + 1;
1353         snprintf(name, sizeof(name), "snd-soc-%s", sprop);
1354         make_lowercase(name);
1355
1356         ssi_private->pdev =
1357                 platform_device_register_data(&pdev->dev, name, 0, NULL, 0);
1358         if (IS_ERR(ssi_private->pdev)) {
1359                 ret = PTR_ERR(ssi_private->pdev);
1360                 dev_err(&pdev->dev, "failed to register platform: %d\n", ret);
1361                 goto error_dai;
1362         }
1363
1364 done:
1365         if (ssi_private->imx_ac97)
1366                 fsl_ssi_ac97_init();
1367
1368         return 0;
1369
1370 error_dai:
1371         if (ssi_private->ssi_on_imx && !ssi_private->use_dma)
1372                 imx_pcm_fiq_exit(pdev);
1373
1374 error_pcm:
1375         fsl_ssi_debugfs_remove(ssi_private);
1376
1377 error_dbgfs:
1378         snd_soc_unregister_component(&pdev->dev);
1379
1380 error_dev:
1381         device_remove_file(&pdev->dev, dev_attr);
1382
1383 error_clk:
1384         if (ssi_private->ssi_on_imx) {
1385                 if (!IS_ERR(ssi_private->baudclk))
1386                         clk_disable_unprepare(ssi_private->baudclk);
1387                 clk_disable_unprepare(ssi_private->clk);
1388         }
1389
1390 error_irqmap:
1391         if (ssi_private->irq_stats)
1392                 irq_dispose_mapping(ssi_private->irq);
1393
1394         return ret;
1395 }
1396
1397 static int fsl_ssi_remove(struct platform_device *pdev)
1398 {
1399         struct fsl_ssi_private *ssi_private = dev_get_drvdata(&pdev->dev);
1400
1401         fsl_ssi_debugfs_remove(ssi_private);
1402
1403         if (!ssi_private->new_binding)
1404                 platform_device_unregister(ssi_private->pdev);
1405         snd_soc_unregister_component(&pdev->dev);
1406         if (ssi_private->ssi_on_imx) {
1407                 if (!IS_ERR(ssi_private->baudclk))
1408                         clk_disable_unprepare(ssi_private->baudclk);
1409                 clk_disable_unprepare(ssi_private->clk);
1410         }
1411         if (ssi_private->irq_stats)
1412                 irq_dispose_mapping(ssi_private->irq);
1413
1414         return 0;
1415 }
1416
1417 static const struct of_device_id fsl_ssi_ids[] = {
1418         { .compatible = "fsl,mpc8610-ssi", },
1419         { .compatible = "fsl,imx21-ssi", },
1420         {}
1421 };
1422 MODULE_DEVICE_TABLE(of, fsl_ssi_ids);
1423
1424 static struct platform_driver fsl_ssi_driver = {
1425         .driver = {
1426                 .name = "fsl-ssi-dai",
1427                 .owner = THIS_MODULE,
1428                 .of_match_table = fsl_ssi_ids,
1429         },
1430         .probe = fsl_ssi_probe,
1431         .remove = fsl_ssi_remove,
1432 };
1433
1434 module_platform_driver(fsl_ssi_driver);
1435
1436 MODULE_ALIAS("platform:fsl-ssi-dai");
1437 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
1438 MODULE_DESCRIPTION("Freescale Synchronous Serial Interface (SSI) ASoC Driver");
1439 MODULE_LICENSE("GPL v2");