b73c102f1cf2eb72b452661903bc59b1451aa3a8
[cascardo/linux.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/spinlock.h>
42 #include <linux/of.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 /**
58  * FSLSSI_I2S_RATES: sample rates supported by the I2S
59  *
60  * This driver currently only supports the SSI running in I2S slave mode,
61  * which means the codec determines the sample rate.  Therefore, we tell
62  * ALSA that we support all rates and let the codec driver decide what rates
63  * are really supported.
64  */
65 #define FSLSSI_I2S_RATES SNDRV_PCM_RATE_CONTINUOUS
66
67 /**
68  * FSLSSI_I2S_FORMATS: audio formats supported by the SSI
69  *
70  * The SSI has a limitation in that the samples must be in the same byte
71  * order as the host CPU.  This is because when multiple bytes are written
72  * to the STX register, the bytes and bits must be written in the same
73  * order.  The STX is a shift register, so all the bits need to be aligned
74  * (bit-endianness must match byte-endianness).  Processors typically write
75  * the bits within a byte in the same order that the bytes of a word are
76  * written in.  So if the host CPU is big-endian, then only big-endian
77  * samples will be written to STX properly.
78  */
79 #ifdef __BIG_ENDIAN
80 #define FSLSSI_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_BE | \
81          SNDRV_PCM_FMTBIT_S18_3BE | SNDRV_PCM_FMTBIT_S20_3BE | \
82          SNDRV_PCM_FMTBIT_S24_3BE | SNDRV_PCM_FMTBIT_S24_BE)
83 #else
84 #define FSLSSI_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE | \
85          SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S20_3LE | \
86          SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE)
87 #endif
88
89 #define FSLSSI_SIER_DBG_RX_FLAGS (CCSR_SSI_SIER_RFF0_EN | \
90                 CCSR_SSI_SIER_RLS_EN | CCSR_SSI_SIER_RFS_EN | \
91                 CCSR_SSI_SIER_ROE0_EN | CCSR_SSI_SIER_RFRC_EN)
92 #define FSLSSI_SIER_DBG_TX_FLAGS (CCSR_SSI_SIER_TFE0_EN | \
93                 CCSR_SSI_SIER_TLS_EN | CCSR_SSI_SIER_TFS_EN | \
94                 CCSR_SSI_SIER_TUE0_EN | CCSR_SSI_SIER_TFRC_EN)
95
96 enum fsl_ssi_type {
97         FSL_SSI_MCP8610,
98         FSL_SSI_MX21,
99         FSL_SSI_MX35,
100         FSL_SSI_MX51,
101 };
102
103 struct fsl_ssi_reg_val {
104         u32 sier;
105         u32 srcr;
106         u32 stcr;
107         u32 scr;
108 };
109
110 struct fsl_ssi_rxtx_reg_val {
111         struct fsl_ssi_reg_val rx;
112         struct fsl_ssi_reg_val tx;
113 };
114
115 static bool fsl_ssi_readable_reg(struct device *dev, unsigned int reg)
116 {
117         switch (reg) {
118         case CCSR_SSI_SACCEN:
119         case CCSR_SSI_SACCDIS:
120                 return false;
121         default:
122                 return true;
123         }
124 }
125
126 static bool fsl_ssi_volatile_reg(struct device *dev, unsigned int reg)
127 {
128         switch (reg) {
129         case CCSR_SSI_STX0:
130         case CCSR_SSI_STX1:
131         case CCSR_SSI_SRX0:
132         case CCSR_SSI_SRX1:
133         case CCSR_SSI_SISR:
134         case CCSR_SSI_SFCSR:
135         case CCSR_SSI_SACNT:
136         case CCSR_SSI_SACADD:
137         case CCSR_SSI_SACDAT:
138         case CCSR_SSI_SATAG:
139         case CCSR_SSI_SACCST:
140         case CCSR_SSI_SOR:
141                 return true;
142         default:
143                 return false;
144         }
145 }
146
147 static bool fsl_ssi_precious_reg(struct device *dev, unsigned int reg)
148 {
149         switch (reg) {
150         case CCSR_SSI_SRX0:
151         case CCSR_SSI_SRX1:
152         case CCSR_SSI_SISR:
153         case CCSR_SSI_SACADD:
154         case CCSR_SSI_SACDAT:
155         case CCSR_SSI_SATAG:
156                 return true;
157         default:
158                 return false;
159         }
160 }
161
162 static bool fsl_ssi_writeable_reg(struct device *dev, unsigned int reg)
163 {
164         switch (reg) {
165         case CCSR_SSI_SRX0:
166         case CCSR_SSI_SRX1:
167         case CCSR_SSI_SACCST:
168                 return false;
169         default:
170                 return true;
171         }
172 }
173
174 static const struct regmap_config fsl_ssi_regconfig = {
175         .max_register = CCSR_SSI_SACCDIS,
176         .reg_bits = 32,
177         .val_bits = 32,
178         .reg_stride = 4,
179         .val_format_endian = REGMAP_ENDIAN_NATIVE,
180         .readable_reg = fsl_ssi_readable_reg,
181         .volatile_reg = fsl_ssi_volatile_reg,
182         .precious_reg = fsl_ssi_precious_reg,
183         .writeable_reg = fsl_ssi_writeable_reg,
184         .cache_type = REGCACHE_FLAT,
185 };
186
187 struct fsl_ssi_soc_data {
188         bool imx;
189         bool imx21regs; /* imx21-class SSI - no SACC{ST,EN,DIS} regs */
190         bool offline_config;
191         u32 sisr_write_mask;
192 };
193
194 /**
195  * fsl_ssi_private: per-SSI private data
196  *
197  * @reg: Pointer to the regmap registers
198  * @irq: IRQ of this SSI
199  * @cpu_dai_drv: CPU DAI driver for this device
200  *
201  * @dai_fmt: DAI configuration this device is currently used with
202  * @i2s_mode: i2s and network mode configuration of the device. Is used to
203  * switch between normal and i2s/network mode
204  * mode depending on the number of channels
205  * @use_dma: DMA is used or FIQ with stream filter
206  * @use_dual_fifo: DMA with support for both FIFOs used
207  * @fifo_deph: Depth of the SSI FIFOs
208  * @rxtx_reg_val: Specific register settings for receive/transmit configuration
209  *
210  * @clk: SSI clock
211  * @baudclk: SSI baud clock for master mode
212  * @baudclk_streams: Active streams that are using baudclk
213  * @bitclk_freq: bitclock frequency set by .set_dai_sysclk
214  *
215  * @dma_params_tx: DMA transmit parameters
216  * @dma_params_rx: DMA receive parameters
217  * @ssi_phys: physical address of the SSI registers
218  *
219  * @fiq_params: FIQ stream filtering parameters
220  *
221  * @pdev: Pointer to pdev used for deprecated fsl-ssi sound card
222  *
223  * @dbg_stats: Debugging statistics
224  *
225  * @soc: SoC specific data
226  */
227 struct fsl_ssi_private {
228         struct regmap *regs;
229         int irq;
230         struct snd_soc_dai_driver cpu_dai_drv;
231
232         unsigned int dai_fmt;
233         u8 i2s_mode;
234         bool use_dma;
235         bool use_dual_fifo;
236         bool has_ipg_clk_name;
237         unsigned int fifo_depth;
238         struct fsl_ssi_rxtx_reg_val rxtx_reg_val;
239
240         struct clk *clk;
241         struct clk *baudclk;
242         unsigned int baudclk_streams;
243         unsigned int bitclk_freq;
244
245         /* regcache for volatile regs */
246         u32 regcache_sfcsr;
247         u32 regcache_sacnt;
248
249         /* DMA params */
250         struct snd_dmaengine_dai_dma_data dma_params_tx;
251         struct snd_dmaengine_dai_dma_data dma_params_rx;
252         dma_addr_t ssi_phys;
253
254         /* params for non-dma FIQ stream filtered mode */
255         struct imx_pcm_fiq_params fiq_params;
256
257         /* Used when using fsl-ssi as sound-card. This is only used by ppc and
258          * should be replaced with simple-sound-card. */
259         struct platform_device *pdev;
260
261         struct fsl_ssi_dbg dbg_stats;
262
263         const struct fsl_ssi_soc_data *soc;
264         struct device *dev;
265 };
266
267 /*
268  * imx51 and later SoCs have a slightly different IP that allows the
269  * SSI configuration while the SSI unit is running.
270  *
271  * More important, it is necessary on those SoCs to configure the
272  * sperate TX/RX DMA bits just before starting the stream
273  * (fsl_ssi_trigger). The SDMA unit has to be configured before fsl_ssi
274  * sends any DMA requests to the SDMA unit, otherwise it is not defined
275  * how the SDMA unit handles the DMA request.
276  *
277  * SDMA units are present on devices starting at imx35 but the imx35
278  * reference manual states that the DMA bits should not be changed
279  * while the SSI unit is running (SSIEN). So we support the necessary
280  * online configuration of fsl-ssi starting at imx51.
281  */
282
283 static struct fsl_ssi_soc_data fsl_ssi_mpc8610 = {
284         .imx = false,
285         .offline_config = true,
286         .sisr_write_mask = CCSR_SSI_SISR_RFRC | CCSR_SSI_SISR_TFRC |
287                         CCSR_SSI_SISR_ROE0 | CCSR_SSI_SISR_ROE1 |
288                         CCSR_SSI_SISR_TUE0 | CCSR_SSI_SISR_TUE1,
289 };
290
291 static struct fsl_ssi_soc_data fsl_ssi_imx21 = {
292         .imx = true,
293         .imx21regs = true,
294         .offline_config = true,
295         .sisr_write_mask = 0,
296 };
297
298 static struct fsl_ssi_soc_data fsl_ssi_imx35 = {
299         .imx = true,
300         .offline_config = true,
301         .sisr_write_mask = CCSR_SSI_SISR_RFRC | CCSR_SSI_SISR_TFRC |
302                         CCSR_SSI_SISR_ROE0 | CCSR_SSI_SISR_ROE1 |
303                         CCSR_SSI_SISR_TUE0 | CCSR_SSI_SISR_TUE1,
304 };
305
306 static struct fsl_ssi_soc_data fsl_ssi_imx51 = {
307         .imx = true,
308         .offline_config = false,
309         .sisr_write_mask = CCSR_SSI_SISR_ROE0 | CCSR_SSI_SISR_ROE1 |
310                 CCSR_SSI_SISR_TUE0 | CCSR_SSI_SISR_TUE1,
311 };
312
313 static const struct of_device_id fsl_ssi_ids[] = {
314         { .compatible = "fsl,mpc8610-ssi", .data = &fsl_ssi_mpc8610 },
315         { .compatible = "fsl,imx51-ssi", .data = &fsl_ssi_imx51 },
316         { .compatible = "fsl,imx35-ssi", .data = &fsl_ssi_imx35 },
317         { .compatible = "fsl,imx21-ssi", .data = &fsl_ssi_imx21 },
318         {}
319 };
320 MODULE_DEVICE_TABLE(of, fsl_ssi_ids);
321
322 static bool fsl_ssi_is_ac97(struct fsl_ssi_private *ssi_private)
323 {
324         return (ssi_private->dai_fmt & SND_SOC_DAIFMT_FORMAT_MASK) ==
325                 SND_SOC_DAIFMT_AC97;
326 }
327
328 static bool fsl_ssi_is_i2s_master(struct fsl_ssi_private *ssi_private)
329 {
330         return (ssi_private->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) ==
331                 SND_SOC_DAIFMT_CBS_CFS;
332 }
333
334 static bool fsl_ssi_is_i2s_cbm_cfs(struct fsl_ssi_private *ssi_private)
335 {
336         return (ssi_private->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) ==
337                 SND_SOC_DAIFMT_CBM_CFS;
338 }
339 /**
340  * fsl_ssi_isr: SSI interrupt handler
341  *
342  * Although it's possible to use the interrupt handler to send and receive
343  * data to/from the SSI, we use the DMA instead.  Programming is more
344  * complicated, but the performance is much better.
345  *
346  * This interrupt handler is used only to gather statistics.
347  *
348  * @irq: IRQ of the SSI device
349  * @dev_id: pointer to the ssi_private structure for this SSI device
350  */
351 static irqreturn_t fsl_ssi_isr(int irq, void *dev_id)
352 {
353         struct fsl_ssi_private *ssi_private = dev_id;
354         struct regmap *regs = ssi_private->regs;
355         __be32 sisr;
356         __be32 sisr2;
357
358         /* We got an interrupt, so read the status register to see what we
359            were interrupted for.  We mask it with the Interrupt Enable register
360            so that we only check for events that we're interested in.
361          */
362         regmap_read(regs, CCSR_SSI_SISR, &sisr);
363
364         sisr2 = sisr & ssi_private->soc->sisr_write_mask;
365         /* Clear the bits that we set */
366         if (sisr2)
367                 regmap_write(regs, CCSR_SSI_SISR, sisr2);
368
369         fsl_ssi_dbg_isr(&ssi_private->dbg_stats, sisr);
370
371         return IRQ_HANDLED;
372 }
373
374 /*
375  * Enable/Disable all rx/tx config flags at once.
376  */
377 static void fsl_ssi_rxtx_config(struct fsl_ssi_private *ssi_private,
378                 bool enable)
379 {
380         struct regmap *regs = ssi_private->regs;
381         struct fsl_ssi_rxtx_reg_val *vals = &ssi_private->rxtx_reg_val;
382
383         if (enable) {
384                 regmap_update_bits(regs, CCSR_SSI_SIER,
385                                 vals->rx.sier | vals->tx.sier,
386                                 vals->rx.sier | vals->tx.sier);
387                 regmap_update_bits(regs, CCSR_SSI_SRCR,
388                                 vals->rx.srcr | vals->tx.srcr,
389                                 vals->rx.srcr | vals->tx.srcr);
390                 regmap_update_bits(regs, CCSR_SSI_STCR,
391                                 vals->rx.stcr | vals->tx.stcr,
392                                 vals->rx.stcr | vals->tx.stcr);
393         } else {
394                 regmap_update_bits(regs, CCSR_SSI_SRCR,
395                                 vals->rx.srcr | vals->tx.srcr, 0);
396                 regmap_update_bits(regs, CCSR_SSI_STCR,
397                                 vals->rx.stcr | vals->tx.stcr, 0);
398                 regmap_update_bits(regs, CCSR_SSI_SIER,
399                                 vals->rx.sier | vals->tx.sier, 0);
400         }
401 }
402
403 /*
404  * Clear RX or TX FIFO to remove samples from the previous
405  * stream session which may be still present in the FIFO and
406  * may introduce bad samples and/or channel slipping.
407  *
408  * Note: The SOR is not documented in recent IMX datasheet, but
409  * is described in IMX51 reference manual at section 56.3.3.15.
410  */
411 static void fsl_ssi_fifo_clear(struct fsl_ssi_private *ssi_private,
412                 bool is_rx)
413 {
414         if (is_rx) {
415                 regmap_update_bits(ssi_private->regs, CCSR_SSI_SOR,
416                         CCSR_SSI_SOR_RX_CLR, CCSR_SSI_SOR_RX_CLR);
417         } else {
418                 regmap_update_bits(ssi_private->regs, CCSR_SSI_SOR,
419                         CCSR_SSI_SOR_TX_CLR, CCSR_SSI_SOR_TX_CLR);
420         }
421 }
422
423 /*
424  * Calculate the bits that have to be disabled for the current stream that is
425  * getting disabled. This keeps the bits enabled that are necessary for the
426  * second stream to work if 'stream_active' is true.
427  *
428  * Detailed calculation:
429  * These are the values that need to be active after disabling. For non-active
430  * second stream, this is 0:
431  *      vals_stream * !!stream_active
432  *
433  * The following computes the overall differences between the setup for the
434  * to-disable stream and the active stream, a simple XOR:
435  *      vals_disable ^ (vals_stream * !!(stream_active))
436  *
437  * The full expression adds a mask on all values we care about
438  */
439 #define fsl_ssi_disable_val(vals_disable, vals_stream, stream_active) \
440         ((vals_disable) & \
441          ((vals_disable) ^ ((vals_stream) * (u32)!!(stream_active))))
442
443 /*
444  * Enable/Disable a ssi configuration. You have to pass either
445  * ssi_private->rxtx_reg_val.rx or tx as vals parameter.
446  */
447 static void fsl_ssi_config(struct fsl_ssi_private *ssi_private, bool enable,
448                 struct fsl_ssi_reg_val *vals)
449 {
450         struct regmap *regs = ssi_private->regs;
451         struct fsl_ssi_reg_val *avals;
452         int nr_active_streams;
453         u32 scr_val;
454         int keep_active;
455
456         regmap_read(regs, CCSR_SSI_SCR, &scr_val);
457
458         nr_active_streams = !!(scr_val & CCSR_SSI_SCR_TE) +
459                                 !!(scr_val & CCSR_SSI_SCR_RE);
460
461         if (nr_active_streams - 1 > 0)
462                 keep_active = 1;
463         else
464                 keep_active = 0;
465
466         /* Find the other direction values rx or tx which we do not want to
467          * modify */
468         if (&ssi_private->rxtx_reg_val.rx == vals)
469                 avals = &ssi_private->rxtx_reg_val.tx;
470         else
471                 avals = &ssi_private->rxtx_reg_val.rx;
472
473         /* If vals should be disabled, start with disabling the unit */
474         if (!enable) {
475                 u32 scr = fsl_ssi_disable_val(vals->scr, avals->scr,
476                                 keep_active);
477                 regmap_update_bits(regs, CCSR_SSI_SCR, scr, 0);
478         }
479
480         /*
481          * We are running on a SoC which does not support online SSI
482          * reconfiguration, so we have to enable all necessary flags at once
483          * even if we do not use them later (capture and playback configuration)
484          */
485         if (ssi_private->soc->offline_config) {
486                 if ((enable && !nr_active_streams) ||
487                                 (!enable && !keep_active))
488                         fsl_ssi_rxtx_config(ssi_private, enable);
489
490                 goto config_done;
491         }
492
493         /*
494          * Configure single direction units while the SSI unit is running
495          * (online configuration)
496          */
497         if (enable) {
498                 fsl_ssi_fifo_clear(ssi_private, vals->scr & CCSR_SSI_SCR_RE);
499
500                 regmap_update_bits(regs, CCSR_SSI_SRCR, vals->srcr, vals->srcr);
501                 regmap_update_bits(regs, CCSR_SSI_STCR, vals->stcr, vals->stcr);
502                 regmap_update_bits(regs, CCSR_SSI_SIER, vals->sier, vals->sier);
503         } else {
504                 u32 sier;
505                 u32 srcr;
506                 u32 stcr;
507
508                 /*
509                  * Disabling the necessary flags for one of rx/tx while the
510                  * other stream is active is a little bit more difficult. We
511                  * have to disable only those flags that differ between both
512                  * streams (rx XOR tx) and that are set in the stream that is
513                  * disabled now. Otherwise we could alter flags of the other
514                  * stream
515                  */
516
517                 /* These assignments are simply vals without bits set in avals*/
518                 sier = fsl_ssi_disable_val(vals->sier, avals->sier,
519                                 keep_active);
520                 srcr = fsl_ssi_disable_val(vals->srcr, avals->srcr,
521                                 keep_active);
522                 stcr = fsl_ssi_disable_val(vals->stcr, avals->stcr,
523                                 keep_active);
524
525                 regmap_update_bits(regs, CCSR_SSI_SRCR, srcr, 0);
526                 regmap_update_bits(regs, CCSR_SSI_STCR, stcr, 0);
527                 regmap_update_bits(regs, CCSR_SSI_SIER, sier, 0);
528         }
529
530 config_done:
531         /* Enabling of subunits is done after configuration */
532         if (enable) {
533                 if (ssi_private->use_dma && (vals->scr & CCSR_SSI_SCR_TE)) {
534                         /*
535                          * Be sure the Tx FIFO is filled when TE is set.
536                          * Otherwise, there are some chances to start the
537                          * playback with some void samples inserted first,
538                          * generating a channel slip.
539                          *
540                          * First, SSIEN must be set, to let the FIFO be filled.
541                          *
542                          * Notes:
543                          * - Limit this fix to the DMA case until FIQ cases can
544                          *   be tested.
545                          * - Limit the length of the busy loop to not lock the
546                          *   system too long, even if 1-2 loops are sufficient
547                          *   in general.
548                          */
549                         int i;
550                         int max_loop = 100;
551                         regmap_update_bits(regs, CCSR_SSI_SCR,
552                                         CCSR_SSI_SCR_SSIEN, CCSR_SSI_SCR_SSIEN);
553                         for (i = 0; i < max_loop; i++) {
554                                 u32 sfcsr;
555                                 regmap_read(regs, CCSR_SSI_SFCSR, &sfcsr);
556                                 if (CCSR_SSI_SFCSR_TFCNT0(sfcsr))
557                                         break;
558                         }
559                         if (i == max_loop) {
560                                 dev_err(ssi_private->dev,
561                                         "Timeout waiting TX FIFO filling\n");
562                         }
563                 }
564                 regmap_update_bits(regs, CCSR_SSI_SCR, vals->scr, vals->scr);
565         }
566 }
567
568
569 static void fsl_ssi_rx_config(struct fsl_ssi_private *ssi_private, bool enable)
570 {
571         fsl_ssi_config(ssi_private, enable, &ssi_private->rxtx_reg_val.rx);
572 }
573
574 static void fsl_ssi_tx_config(struct fsl_ssi_private *ssi_private, bool enable)
575 {
576         fsl_ssi_config(ssi_private, enable, &ssi_private->rxtx_reg_val.tx);
577 }
578
579 /*
580  * Setup rx/tx register values used to enable/disable the streams. These will
581  * be used later in fsl_ssi_config to setup the streams without the need to
582  * check for all different SSI modes.
583  */
584 static void fsl_ssi_setup_reg_vals(struct fsl_ssi_private *ssi_private)
585 {
586         struct fsl_ssi_rxtx_reg_val *reg = &ssi_private->rxtx_reg_val;
587
588         reg->rx.sier = CCSR_SSI_SIER_RFF0_EN;
589         reg->rx.srcr = CCSR_SSI_SRCR_RFEN0;
590         reg->rx.scr = 0;
591         reg->tx.sier = CCSR_SSI_SIER_TFE0_EN;
592         reg->tx.stcr = CCSR_SSI_STCR_TFEN0;
593         reg->tx.scr = 0;
594
595         if (!fsl_ssi_is_ac97(ssi_private)) {
596                 reg->rx.scr = CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_RE;
597                 reg->rx.sier |= CCSR_SSI_SIER_RFF0_EN;
598                 reg->tx.scr = CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_TE;
599                 reg->tx.sier |= CCSR_SSI_SIER_TFE0_EN;
600         }
601
602         if (ssi_private->use_dma) {
603                 reg->rx.sier |= CCSR_SSI_SIER_RDMAE;
604                 reg->tx.sier |= CCSR_SSI_SIER_TDMAE;
605         } else {
606                 reg->rx.sier |= CCSR_SSI_SIER_RIE;
607                 reg->tx.sier |= CCSR_SSI_SIER_TIE;
608         }
609
610         reg->rx.sier |= FSLSSI_SIER_DBG_RX_FLAGS;
611         reg->tx.sier |= FSLSSI_SIER_DBG_TX_FLAGS;
612 }
613
614 static void fsl_ssi_setup_ac97(struct fsl_ssi_private *ssi_private)
615 {
616         struct regmap *regs = ssi_private->regs;
617
618         /*
619          * Setup the clock control register
620          */
621         regmap_write(regs, CCSR_SSI_STCCR,
622                         CCSR_SSI_SxCCR_WL(17) | CCSR_SSI_SxCCR_DC(13));
623         regmap_write(regs, CCSR_SSI_SRCCR,
624                         CCSR_SSI_SxCCR_WL(17) | CCSR_SSI_SxCCR_DC(13));
625
626         /*
627          * Enable AC97 mode and startup the SSI
628          */
629         regmap_write(regs, CCSR_SSI_SACNT,
630                         CCSR_SSI_SACNT_AC97EN | CCSR_SSI_SACNT_FV);
631
632         /* no SACC{ST,EN,DIS} regs on imx21-class SSI */
633         if (!ssi_private->soc->imx21regs) {
634                 regmap_write(regs, CCSR_SSI_SACCDIS, 0xff);
635                 regmap_write(regs, CCSR_SSI_SACCEN, 0x300);
636         }
637
638         /*
639          * Enable SSI, Transmit and Receive. AC97 has to communicate with the
640          * codec before a stream is started.
641          */
642         regmap_update_bits(regs, CCSR_SSI_SCR,
643                         CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_TE | CCSR_SSI_SCR_RE,
644                         CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_TE | CCSR_SSI_SCR_RE);
645
646         regmap_write(regs, CCSR_SSI_SOR, CCSR_SSI_SOR_WAIT(3));
647 }
648
649 /**
650  * fsl_ssi_startup: create a new substream
651  *
652  * This is the first function called when a stream is opened.
653  *
654  * If this is the first stream open, then grab the IRQ and program most of
655  * the SSI registers.
656  */
657 static int fsl_ssi_startup(struct snd_pcm_substream *substream,
658                            struct snd_soc_dai *dai)
659 {
660         struct snd_soc_pcm_runtime *rtd = substream->private_data;
661         struct fsl_ssi_private *ssi_private =
662                 snd_soc_dai_get_drvdata(rtd->cpu_dai);
663         int ret;
664
665         ret = clk_prepare_enable(ssi_private->clk);
666         if (ret)
667                 return ret;
668
669         /* When using dual fifo mode, it is safer to ensure an even period
670          * size. If appearing to an odd number while DMA always starts its
671          * task from fifo0, fifo1 would be neglected at the end of each
672          * period. But SSI would still access fifo1 with an invalid data.
673          */
674         if (ssi_private->use_dual_fifo)
675                 snd_pcm_hw_constraint_step(substream->runtime, 0,
676                                 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 2);
677
678         return 0;
679 }
680
681 /**
682  * fsl_ssi_shutdown: shutdown the SSI
683  *
684  */
685 static void fsl_ssi_shutdown(struct snd_pcm_substream *substream,
686                                 struct snd_soc_dai *dai)
687 {
688         struct snd_soc_pcm_runtime *rtd = substream->private_data;
689         struct fsl_ssi_private *ssi_private =
690                 snd_soc_dai_get_drvdata(rtd->cpu_dai);
691
692         clk_disable_unprepare(ssi_private->clk);
693
694 }
695
696 /**
697  * fsl_ssi_set_bclk - configure Digital Audio Interface bit clock
698  *
699  * Note: This function can be only called when using SSI as DAI master
700  *
701  * Quick instruction for parameters:
702  * freq: Output BCLK frequency = samplerate * 32 (fixed) * channels
703  * dir: SND_SOC_CLOCK_OUT -> TxBCLK, SND_SOC_CLOCK_IN -> RxBCLK.
704  */
705 static int fsl_ssi_set_bclk(struct snd_pcm_substream *substream,
706                 struct snd_soc_dai *cpu_dai,
707                 struct snd_pcm_hw_params *hw_params)
708 {
709         struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
710         struct regmap *regs = ssi_private->regs;
711         int synchronous = ssi_private->cpu_dai_drv.symmetric_rates, ret;
712         u32 pm = 999, div2, psr, stccr, mask, afreq, factor, i;
713         unsigned long clkrate, baudrate, tmprate;
714         u64 sub, savesub = 100000;
715         unsigned int freq;
716         bool baudclk_is_used;
717
718         /* Prefer the explicitly set bitclock frequency */
719         if (ssi_private->bitclk_freq)
720                 freq = ssi_private->bitclk_freq;
721         else
722                 freq = params_channels(hw_params) * 32 * params_rate(hw_params);
723
724         /* Don't apply it to any non-baudclk circumstance */
725         if (IS_ERR(ssi_private->baudclk))
726                 return -EINVAL;
727
728         /*
729          * Hardware limitation: The bclk rate must be
730          * never greater than 1/5 IPG clock rate
731          */
732         if (freq * 5 > clk_get_rate(ssi_private->clk)) {
733                 dev_err(cpu_dai->dev, "bitclk > ipgclk/5\n");
734                 return -EINVAL;
735         }
736
737         baudclk_is_used = ssi_private->baudclk_streams & ~(BIT(substream->stream));
738
739         /* It should be already enough to divide clock by setting pm alone */
740         psr = 0;
741         div2 = 0;
742
743         factor = (div2 + 1) * (7 * psr + 1) * 2;
744
745         for (i = 0; i < 255; i++) {
746                 tmprate = freq * factor * (i + 1);
747
748                 if (baudclk_is_used)
749                         clkrate = clk_get_rate(ssi_private->baudclk);
750                 else
751                         clkrate = clk_round_rate(ssi_private->baudclk, tmprate);
752
753                 clkrate /= factor;
754                 afreq = clkrate / (i + 1);
755
756                 if (freq == afreq)
757                         sub = 0;
758                 else if (freq / afreq == 1)
759                         sub = freq - afreq;
760                 else if (afreq / freq == 1)
761                         sub = afreq - freq;
762                 else
763                         continue;
764
765                 /* Calculate the fraction */
766                 sub *= 100000;
767                 do_div(sub, freq);
768
769                 if (sub < savesub && !(i == 0 && psr == 0 && div2 == 0)) {
770                         baudrate = tmprate;
771                         savesub = sub;
772                         pm = i;
773                 }
774
775                 /* We are lucky */
776                 if (savesub == 0)
777                         break;
778         }
779
780         /* No proper pm found if it is still remaining the initial value */
781         if (pm == 999) {
782                 dev_err(cpu_dai->dev, "failed to handle the required sysclk\n");
783                 return -EINVAL;
784         }
785
786         stccr = CCSR_SSI_SxCCR_PM(pm + 1) | (div2 ? CCSR_SSI_SxCCR_DIV2 : 0) |
787                 (psr ? CCSR_SSI_SxCCR_PSR : 0);
788         mask = CCSR_SSI_SxCCR_PM_MASK | CCSR_SSI_SxCCR_DIV2 |
789                 CCSR_SSI_SxCCR_PSR;
790
791         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK || synchronous)
792                 regmap_update_bits(regs, CCSR_SSI_STCCR, mask, stccr);
793         else
794                 regmap_update_bits(regs, CCSR_SSI_SRCCR, mask, stccr);
795
796         if (!baudclk_is_used) {
797                 ret = clk_set_rate(ssi_private->baudclk, baudrate);
798                 if (ret) {
799                         dev_err(cpu_dai->dev, "failed to set baudclk rate\n");
800                         return -EINVAL;
801                 }
802         }
803
804         return 0;
805 }
806
807 static int fsl_ssi_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
808                 int clk_id, unsigned int freq, int dir)
809 {
810         struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
811
812         ssi_private->bitclk_freq = freq;
813
814         return 0;
815 }
816
817 /**
818  * fsl_ssi_hw_params - program the sample size
819  *
820  * Most of the SSI registers have been programmed in the startup function,
821  * but the word length must be programmed here.  Unfortunately, programming
822  * the SxCCR.WL bits requires the SSI to be temporarily disabled.  This can
823  * cause a problem with supporting simultaneous playback and capture.  If
824  * the SSI is already playing a stream, then that stream may be temporarily
825  * stopped when you start capture.
826  *
827  * Note: The SxCCR.DC and SxCCR.PM bits are only used if the SSI is the
828  * clock master.
829  */
830 static int fsl_ssi_hw_params(struct snd_pcm_substream *substream,
831         struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *cpu_dai)
832 {
833         struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
834         struct regmap *regs = ssi_private->regs;
835         unsigned int channels = params_channels(hw_params);
836         unsigned int sample_size = params_width(hw_params);
837         u32 wl = CCSR_SSI_SxCCR_WL(sample_size);
838         int ret;
839         u32 scr_val;
840         int enabled;
841
842         regmap_read(regs, CCSR_SSI_SCR, &scr_val);
843         enabled = scr_val & CCSR_SSI_SCR_SSIEN;
844
845         /*
846          * If we're in synchronous mode, and the SSI is already enabled,
847          * then STCCR is already set properly.
848          */
849         if (enabled && ssi_private->cpu_dai_drv.symmetric_rates)
850                 return 0;
851
852         if (fsl_ssi_is_i2s_master(ssi_private)) {
853                 ret = fsl_ssi_set_bclk(substream, cpu_dai, hw_params);
854                 if (ret)
855                         return ret;
856
857                 /* Do not enable the clock if it is already enabled */
858                 if (!(ssi_private->baudclk_streams & BIT(substream->stream))) {
859                         ret = clk_prepare_enable(ssi_private->baudclk);
860                         if (ret)
861                                 return ret;
862
863                         ssi_private->baudclk_streams |= BIT(substream->stream);
864                 }
865         }
866
867         if (!fsl_ssi_is_ac97(ssi_private)) {
868                 u8 i2smode;
869                 /*
870                  * Switch to normal net mode in order to have a frame sync
871                  * signal every 32 bits instead of 16 bits
872                  */
873                 if (fsl_ssi_is_i2s_cbm_cfs(ssi_private) && sample_size == 16)
874                         i2smode = CCSR_SSI_SCR_I2S_MODE_NORMAL |
875                                 CCSR_SSI_SCR_NET;
876                 else
877                         i2smode = ssi_private->i2s_mode;
878
879                 regmap_update_bits(regs, CCSR_SSI_SCR,
880                                 CCSR_SSI_SCR_NET | CCSR_SSI_SCR_I2S_MODE_MASK,
881                                 channels == 1 ? 0 : i2smode);
882         }
883
884         /*
885          * FIXME: The documentation says that SxCCR[WL] should not be
886          * modified while the SSI is enabled.  The only time this can
887          * happen is if we're trying to do simultaneous playback and
888          * capture in asynchronous mode.  Unfortunately, I have been enable
889          * to get that to work at all on the P1022DS.  Therefore, we don't
890          * bother to disable/enable the SSI when setting SxCCR[WL], because
891          * the SSI will stop anyway.  Maybe one day, this will get fixed.
892          */
893
894         /* In synchronous mode, the SSI uses STCCR for capture */
895         if ((substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ||
896             ssi_private->cpu_dai_drv.symmetric_rates)
897                 regmap_update_bits(regs, CCSR_SSI_STCCR, CCSR_SSI_SxCCR_WL_MASK,
898                                 wl);
899         else
900                 regmap_update_bits(regs, CCSR_SSI_SRCCR, CCSR_SSI_SxCCR_WL_MASK,
901                                 wl);
902
903         return 0;
904 }
905
906 static int fsl_ssi_hw_free(struct snd_pcm_substream *substream,
907                 struct snd_soc_dai *cpu_dai)
908 {
909         struct snd_soc_pcm_runtime *rtd = substream->private_data;
910         struct fsl_ssi_private *ssi_private =
911                 snd_soc_dai_get_drvdata(rtd->cpu_dai);
912
913         if (fsl_ssi_is_i2s_master(ssi_private) &&
914                         ssi_private->baudclk_streams & BIT(substream->stream)) {
915                 clk_disable_unprepare(ssi_private->baudclk);
916                 ssi_private->baudclk_streams &= ~BIT(substream->stream);
917         }
918
919         return 0;
920 }
921
922 static int _fsl_ssi_set_dai_fmt(struct device *dev,
923                                 struct fsl_ssi_private *ssi_private,
924                                 unsigned int fmt)
925 {
926         struct regmap *regs = ssi_private->regs;
927         u32 strcr = 0, stcr, srcr, scr, mask;
928         u8 wm;
929
930         ssi_private->dai_fmt = fmt;
931
932         if (fsl_ssi_is_i2s_master(ssi_private) && IS_ERR(ssi_private->baudclk)) {
933                 dev_err(dev, "baudclk is missing which is necessary for master mode\n");
934                 return -EINVAL;
935         }
936
937         fsl_ssi_setup_reg_vals(ssi_private);
938
939         regmap_read(regs, CCSR_SSI_SCR, &scr);
940         scr &= ~(CCSR_SSI_SCR_SYN | CCSR_SSI_SCR_I2S_MODE_MASK);
941         scr |= CCSR_SSI_SCR_SYNC_TX_FS;
942
943         mask = CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TFDIR | CCSR_SSI_STCR_TXDIR |
944                 CCSR_SSI_STCR_TSCKP | CCSR_SSI_STCR_TFSI | CCSR_SSI_STCR_TFSL |
945                 CCSR_SSI_STCR_TEFS;
946         regmap_read(regs, CCSR_SSI_STCR, &stcr);
947         regmap_read(regs, CCSR_SSI_SRCR, &srcr);
948         stcr &= ~mask;
949         srcr &= ~mask;
950
951         ssi_private->i2s_mode = CCSR_SSI_SCR_NET;
952         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
953         case SND_SOC_DAIFMT_I2S:
954                 regmap_update_bits(regs, CCSR_SSI_STCCR,
955                                    CCSR_SSI_SxCCR_DC_MASK,
956                                    CCSR_SSI_SxCCR_DC(2));
957                 regmap_update_bits(regs, CCSR_SSI_SRCCR,
958                                    CCSR_SSI_SxCCR_DC_MASK,
959                                    CCSR_SSI_SxCCR_DC(2));
960                 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
961                 case SND_SOC_DAIFMT_CBM_CFS:
962                 case SND_SOC_DAIFMT_CBS_CFS:
963                         ssi_private->i2s_mode |= CCSR_SSI_SCR_I2S_MODE_MASTER;
964                         break;
965                 case SND_SOC_DAIFMT_CBM_CFM:
966                         ssi_private->i2s_mode |= CCSR_SSI_SCR_I2S_MODE_SLAVE;
967                         break;
968                 default:
969                         return -EINVAL;
970                 }
971
972                 /* Data on rising edge of bclk, frame low, 1clk before data */
973                 strcr |= CCSR_SSI_STCR_TFSI | CCSR_SSI_STCR_TSCKP |
974                         CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TEFS;
975                 break;
976         case SND_SOC_DAIFMT_LEFT_J:
977                 /* Data on rising edge of bclk, frame high */
978                 strcr |= CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TSCKP;
979                 break;
980         case SND_SOC_DAIFMT_DSP_A:
981                 /* Data on rising edge of bclk, frame high, 1clk before data */
982                 strcr |= CCSR_SSI_STCR_TFSL | CCSR_SSI_STCR_TSCKP |
983                         CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TEFS;
984                 break;
985         case SND_SOC_DAIFMT_DSP_B:
986                 /* Data on rising edge of bclk, frame high */
987                 strcr |= CCSR_SSI_STCR_TFSL | CCSR_SSI_STCR_TSCKP |
988                         CCSR_SSI_STCR_TXBIT0;
989                 break;
990         case SND_SOC_DAIFMT_AC97:
991                 ssi_private->i2s_mode |= CCSR_SSI_SCR_I2S_MODE_NORMAL;
992                 break;
993         default:
994                 return -EINVAL;
995         }
996         scr |= ssi_private->i2s_mode;
997
998         /* DAI clock inversion */
999         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1000         case SND_SOC_DAIFMT_NB_NF:
1001                 /* Nothing to do for both normal cases */
1002                 break;
1003         case SND_SOC_DAIFMT_IB_NF:
1004                 /* Invert bit clock */
1005                 strcr ^= CCSR_SSI_STCR_TSCKP;
1006                 break;
1007         case SND_SOC_DAIFMT_NB_IF:
1008                 /* Invert frame clock */
1009                 strcr ^= CCSR_SSI_STCR_TFSI;
1010                 break;
1011         case SND_SOC_DAIFMT_IB_IF:
1012                 /* Invert both clocks */
1013                 strcr ^= CCSR_SSI_STCR_TSCKP;
1014                 strcr ^= CCSR_SSI_STCR_TFSI;
1015                 break;
1016         default:
1017                 return -EINVAL;
1018         }
1019
1020         /* DAI clock master masks */
1021         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1022         case SND_SOC_DAIFMT_CBS_CFS:
1023                 strcr |= CCSR_SSI_STCR_TFDIR | CCSR_SSI_STCR_TXDIR;
1024                 scr |= CCSR_SSI_SCR_SYS_CLK_EN;
1025                 break;
1026         case SND_SOC_DAIFMT_CBM_CFM:
1027                 scr &= ~CCSR_SSI_SCR_SYS_CLK_EN;
1028                 break;
1029         case SND_SOC_DAIFMT_CBM_CFS:
1030                 strcr &= ~CCSR_SSI_STCR_TXDIR;
1031                 strcr |= CCSR_SSI_STCR_TFDIR;
1032                 scr &= ~CCSR_SSI_SCR_SYS_CLK_EN;
1033                 break;
1034         default:
1035                 if (!fsl_ssi_is_ac97(ssi_private))
1036                         return -EINVAL;
1037         }
1038
1039         stcr |= strcr;
1040         srcr |= strcr;
1041
1042         if (ssi_private->cpu_dai_drv.symmetric_rates
1043                         || fsl_ssi_is_ac97(ssi_private)) {
1044                 /* Need to clear RXDIR when using SYNC or AC97 mode */
1045                 srcr &= ~CCSR_SSI_SRCR_RXDIR;
1046                 scr |= CCSR_SSI_SCR_SYN;
1047         }
1048
1049         regmap_write(regs, CCSR_SSI_STCR, stcr);
1050         regmap_write(regs, CCSR_SSI_SRCR, srcr);
1051         regmap_write(regs, CCSR_SSI_SCR, scr);
1052
1053         /*
1054          * Set the watermark for transmit FIFI 0 and receive FIFO 0. We don't
1055          * use FIFO 1. We program the transmit water to signal a DMA transfer
1056          * if there are only two (or fewer) elements left in the FIFO. Two
1057          * elements equals one frame (left channel, right channel). This value,
1058          * however, depends on the depth of the transmit buffer.
1059          *
1060          * We set the watermark on the same level as the DMA burstsize.  For
1061          * fiq it is probably better to use the biggest possible watermark
1062          * size.
1063          */
1064         if (ssi_private->use_dma)
1065                 wm = ssi_private->fifo_depth - 2;
1066         else
1067                 wm = ssi_private->fifo_depth;
1068
1069         regmap_write(regs, CCSR_SSI_SFCSR,
1070                         CCSR_SSI_SFCSR_TFWM0(wm) | CCSR_SSI_SFCSR_RFWM0(wm) |
1071                         CCSR_SSI_SFCSR_TFWM1(wm) | CCSR_SSI_SFCSR_RFWM1(wm));
1072
1073         if (ssi_private->use_dual_fifo) {
1074                 regmap_update_bits(regs, CCSR_SSI_SRCR, CCSR_SSI_SRCR_RFEN1,
1075                                 CCSR_SSI_SRCR_RFEN1);
1076                 regmap_update_bits(regs, CCSR_SSI_STCR, CCSR_SSI_STCR_TFEN1,
1077                                 CCSR_SSI_STCR_TFEN1);
1078                 regmap_update_bits(regs, CCSR_SSI_SCR, CCSR_SSI_SCR_TCH_EN,
1079                                 CCSR_SSI_SCR_TCH_EN);
1080         }
1081
1082         if ((fmt & SND_SOC_DAIFMT_FORMAT_MASK) == SND_SOC_DAIFMT_AC97)
1083                 fsl_ssi_setup_ac97(ssi_private);
1084
1085         return 0;
1086
1087 }
1088
1089 /**
1090  * fsl_ssi_set_dai_fmt - configure Digital Audio Interface Format.
1091  */
1092 static int fsl_ssi_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
1093 {
1094         struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
1095
1096         return _fsl_ssi_set_dai_fmt(cpu_dai->dev, ssi_private, fmt);
1097 }
1098
1099 /**
1100  * fsl_ssi_set_dai_tdm_slot - set TDM slot number
1101  *
1102  * Note: This function can be only called when using SSI as DAI master
1103  */
1104 static int fsl_ssi_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai, u32 tx_mask,
1105                                 u32 rx_mask, int slots, int slot_width)
1106 {
1107         struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
1108         struct regmap *regs = ssi_private->regs;
1109         u32 val;
1110
1111         /* The slot number should be >= 2 if using Network mode or I2S mode */
1112         regmap_read(regs, CCSR_SSI_SCR, &val);
1113         val &= CCSR_SSI_SCR_I2S_MODE_MASK | CCSR_SSI_SCR_NET;
1114         if (val && slots < 2) {
1115                 dev_err(cpu_dai->dev, "slot number should be >= 2 in I2S or NET\n");
1116                 return -EINVAL;
1117         }
1118
1119         regmap_update_bits(regs, CCSR_SSI_STCCR, CCSR_SSI_SxCCR_DC_MASK,
1120                         CCSR_SSI_SxCCR_DC(slots));
1121         regmap_update_bits(regs, CCSR_SSI_SRCCR, CCSR_SSI_SxCCR_DC_MASK,
1122                         CCSR_SSI_SxCCR_DC(slots));
1123
1124         /* The register SxMSKs needs SSI to provide essential clock due to
1125          * hardware design. So we here temporarily enable SSI to set them.
1126          */
1127         regmap_read(regs, CCSR_SSI_SCR, &val);
1128         val &= CCSR_SSI_SCR_SSIEN;
1129         regmap_update_bits(regs, CCSR_SSI_SCR, CCSR_SSI_SCR_SSIEN,
1130                         CCSR_SSI_SCR_SSIEN);
1131
1132         regmap_write(regs, CCSR_SSI_STMSK, ~tx_mask);
1133         regmap_write(regs, CCSR_SSI_SRMSK, ~rx_mask);
1134
1135         regmap_update_bits(regs, CCSR_SSI_SCR, CCSR_SSI_SCR_SSIEN, val);
1136
1137         return 0;
1138 }
1139
1140 /**
1141  * fsl_ssi_trigger: start and stop the DMA transfer.
1142  *
1143  * This function is called by ALSA to start, stop, pause, and resume the DMA
1144  * transfer of data.
1145  *
1146  * The DMA channel is in external master start and pause mode, which
1147  * means the SSI completely controls the flow of data.
1148  */
1149 static int fsl_ssi_trigger(struct snd_pcm_substream *substream, int cmd,
1150                            struct snd_soc_dai *dai)
1151 {
1152         struct snd_soc_pcm_runtime *rtd = substream->private_data;
1153         struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(rtd->cpu_dai);
1154         struct regmap *regs = ssi_private->regs;
1155
1156         switch (cmd) {
1157         case SNDRV_PCM_TRIGGER_START:
1158         case SNDRV_PCM_TRIGGER_RESUME:
1159         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1160                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1161                         fsl_ssi_tx_config(ssi_private, true);
1162                 else
1163                         fsl_ssi_rx_config(ssi_private, true);
1164                 break;
1165
1166         case SNDRV_PCM_TRIGGER_STOP:
1167         case SNDRV_PCM_TRIGGER_SUSPEND:
1168         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1169                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1170                         fsl_ssi_tx_config(ssi_private, false);
1171                 else
1172                         fsl_ssi_rx_config(ssi_private, false);
1173                 break;
1174
1175         default:
1176                 return -EINVAL;
1177         }
1178
1179         if (fsl_ssi_is_ac97(ssi_private)) {
1180                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1181                         regmap_write(regs, CCSR_SSI_SOR, CCSR_SSI_SOR_TX_CLR);
1182                 else
1183                         regmap_write(regs, CCSR_SSI_SOR, CCSR_SSI_SOR_RX_CLR);
1184         }
1185
1186         return 0;
1187 }
1188
1189 static int fsl_ssi_dai_probe(struct snd_soc_dai *dai)
1190 {
1191         struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(dai);
1192
1193         if (ssi_private->soc->imx && ssi_private->use_dma) {
1194                 dai->playback_dma_data = &ssi_private->dma_params_tx;
1195                 dai->capture_dma_data = &ssi_private->dma_params_rx;
1196         }
1197
1198         return 0;
1199 }
1200
1201 static const struct snd_soc_dai_ops fsl_ssi_dai_ops = {
1202         .startup        = fsl_ssi_startup,
1203         .shutdown       = fsl_ssi_shutdown,
1204         .hw_params      = fsl_ssi_hw_params,
1205         .hw_free        = fsl_ssi_hw_free,
1206         .set_fmt        = fsl_ssi_set_dai_fmt,
1207         .set_sysclk     = fsl_ssi_set_dai_sysclk,
1208         .set_tdm_slot   = fsl_ssi_set_dai_tdm_slot,
1209         .trigger        = fsl_ssi_trigger,
1210 };
1211
1212 /* Template for the CPU dai driver structure */
1213 static struct snd_soc_dai_driver fsl_ssi_dai_template = {
1214         .probe = fsl_ssi_dai_probe,
1215         .playback = {
1216                 .stream_name = "CPU-Playback",
1217                 .channels_min = 1,
1218                 .channels_max = 32,
1219                 .rates = FSLSSI_I2S_RATES,
1220                 .formats = FSLSSI_I2S_FORMATS,
1221         },
1222         .capture = {
1223                 .stream_name = "CPU-Capture",
1224                 .channels_min = 1,
1225                 .channels_max = 32,
1226                 .rates = FSLSSI_I2S_RATES,
1227                 .formats = FSLSSI_I2S_FORMATS,
1228         },
1229         .ops = &fsl_ssi_dai_ops,
1230 };
1231
1232 static const struct snd_soc_component_driver fsl_ssi_component = {
1233         .name           = "fsl-ssi",
1234 };
1235
1236 static struct snd_soc_dai_driver fsl_ssi_ac97_dai = {
1237         .bus_control = true,
1238         .probe = fsl_ssi_dai_probe,
1239         .playback = {
1240                 .stream_name = "AC97 Playback",
1241                 .channels_min = 2,
1242                 .channels_max = 2,
1243                 .rates = SNDRV_PCM_RATE_8000_48000,
1244                 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1245         },
1246         .capture = {
1247                 .stream_name = "AC97 Capture",
1248                 .channels_min = 2,
1249                 .channels_max = 2,
1250                 .rates = SNDRV_PCM_RATE_48000,
1251                 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1252         },
1253         .ops = &fsl_ssi_dai_ops,
1254 };
1255
1256
1257 static struct fsl_ssi_private *fsl_ac97_data;
1258
1259 static void fsl_ssi_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
1260                 unsigned short val)
1261 {
1262         struct regmap *regs = fsl_ac97_data->regs;
1263         unsigned int lreg;
1264         unsigned int lval;
1265         int ret;
1266
1267         if (reg > 0x7f)
1268                 return;
1269
1270         ret = clk_prepare_enable(fsl_ac97_data->clk);
1271         if (ret) {
1272                 pr_err("ac97 write clk_prepare_enable failed: %d\n",
1273                         ret);
1274                 return;
1275         }
1276
1277         lreg = reg <<  12;
1278         regmap_write(regs, CCSR_SSI_SACADD, lreg);
1279
1280         lval = val << 4;
1281         regmap_write(regs, CCSR_SSI_SACDAT, lval);
1282
1283         regmap_update_bits(regs, CCSR_SSI_SACNT, CCSR_SSI_SACNT_RDWR_MASK,
1284                         CCSR_SSI_SACNT_WR);
1285         udelay(100);
1286
1287         clk_disable_unprepare(fsl_ac97_data->clk);
1288 }
1289
1290 static unsigned short fsl_ssi_ac97_read(struct snd_ac97 *ac97,
1291                 unsigned short reg)
1292 {
1293         struct regmap *regs = fsl_ac97_data->regs;
1294
1295         unsigned short val = -1;
1296         u32 reg_val;
1297         unsigned int lreg;
1298         int ret;
1299
1300         ret = clk_prepare_enable(fsl_ac97_data->clk);
1301         if (ret) {
1302                 pr_err("ac97 read clk_prepare_enable failed: %d\n",
1303                         ret);
1304                 return -1;
1305         }
1306
1307         lreg = (reg & 0x7f) <<  12;
1308         regmap_write(regs, CCSR_SSI_SACADD, lreg);
1309         regmap_update_bits(regs, CCSR_SSI_SACNT, CCSR_SSI_SACNT_RDWR_MASK,
1310                         CCSR_SSI_SACNT_RD);
1311
1312         udelay(100);
1313
1314         regmap_read(regs, CCSR_SSI_SACDAT, &reg_val);
1315         val = (reg_val >> 4) & 0xffff;
1316
1317         clk_disable_unprepare(fsl_ac97_data->clk);
1318
1319         return val;
1320 }
1321
1322 static struct snd_ac97_bus_ops fsl_ssi_ac97_ops = {
1323         .read           = fsl_ssi_ac97_read,
1324         .write          = fsl_ssi_ac97_write,
1325 };
1326
1327 /**
1328  * Make every character in a string lower-case
1329  */
1330 static void make_lowercase(char *s)
1331 {
1332         char *p = s;
1333         char c;
1334
1335         while ((c = *p)) {
1336                 if ((c >= 'A') && (c <= 'Z'))
1337                         *p = c + ('a' - 'A');
1338                 p++;
1339         }
1340 }
1341
1342 static int fsl_ssi_imx_probe(struct platform_device *pdev,
1343                 struct fsl_ssi_private *ssi_private, void __iomem *iomem)
1344 {
1345         struct device_node *np = pdev->dev.of_node;
1346         u32 dmas[4];
1347         int ret;
1348
1349         if (ssi_private->has_ipg_clk_name)
1350                 ssi_private->clk = devm_clk_get(&pdev->dev, "ipg");
1351         else
1352                 ssi_private->clk = devm_clk_get(&pdev->dev, NULL);
1353         if (IS_ERR(ssi_private->clk)) {
1354                 ret = PTR_ERR(ssi_private->clk);
1355                 dev_err(&pdev->dev, "could not get clock: %d\n", ret);
1356                 return ret;
1357         }
1358
1359         if (!ssi_private->has_ipg_clk_name) {
1360                 ret = clk_prepare_enable(ssi_private->clk);
1361                 if (ret) {
1362                         dev_err(&pdev->dev, "clk_prepare_enable failed: %d\n", ret);
1363                         return ret;
1364                 }
1365         }
1366
1367         /* For those SLAVE implementations, we ignore non-baudclk cases
1368          * and, instead, abandon MASTER mode that needs baud clock.
1369          */
1370         ssi_private->baudclk = devm_clk_get(&pdev->dev, "baud");
1371         if (IS_ERR(ssi_private->baudclk))
1372                 dev_dbg(&pdev->dev, "could not get baud clock: %ld\n",
1373                          PTR_ERR(ssi_private->baudclk));
1374
1375         /*
1376          * We have burstsize be "fifo_depth - 2" to match the SSI
1377          * watermark setting in fsl_ssi_startup().
1378          */
1379         ssi_private->dma_params_tx.maxburst = ssi_private->fifo_depth - 2;
1380         ssi_private->dma_params_rx.maxburst = ssi_private->fifo_depth - 2;
1381         ssi_private->dma_params_tx.addr = ssi_private->ssi_phys + CCSR_SSI_STX0;
1382         ssi_private->dma_params_rx.addr = ssi_private->ssi_phys + CCSR_SSI_SRX0;
1383
1384         ret = of_property_read_u32_array(np, "dmas", dmas, 4);
1385         if (ssi_private->use_dma && !ret && dmas[2] == IMX_DMATYPE_SSI_DUAL) {
1386                 ssi_private->use_dual_fifo = true;
1387                 /* When using dual fifo mode, we need to keep watermark
1388                  * as even numbers due to dma script limitation.
1389                  */
1390                 ssi_private->dma_params_tx.maxburst &= ~0x1;
1391                 ssi_private->dma_params_rx.maxburst &= ~0x1;
1392         }
1393
1394         if (!ssi_private->use_dma) {
1395
1396                 /*
1397                  * Some boards use an incompatible codec. To get it
1398                  * working, we are using imx-fiq-pcm-audio, that
1399                  * can handle those codecs. DMA is not possible in this
1400                  * situation.
1401                  */
1402
1403                 ssi_private->fiq_params.irq = ssi_private->irq;
1404                 ssi_private->fiq_params.base = iomem;
1405                 ssi_private->fiq_params.dma_params_rx =
1406                         &ssi_private->dma_params_rx;
1407                 ssi_private->fiq_params.dma_params_tx =
1408                         &ssi_private->dma_params_tx;
1409
1410                 ret = imx_pcm_fiq_init(pdev, &ssi_private->fiq_params);
1411                 if (ret)
1412                         goto error_pcm;
1413         } else {
1414                 ret = imx_pcm_dma_init(pdev, IMX_SSI_DMABUF_SIZE);
1415                 if (ret)
1416                         goto error_pcm;
1417         }
1418
1419         return 0;
1420
1421 error_pcm:
1422
1423         if (!ssi_private->has_ipg_clk_name)
1424                 clk_disable_unprepare(ssi_private->clk);
1425         return ret;
1426 }
1427
1428 static void fsl_ssi_imx_clean(struct platform_device *pdev,
1429                 struct fsl_ssi_private *ssi_private)
1430 {
1431         if (!ssi_private->use_dma)
1432                 imx_pcm_fiq_exit(pdev);
1433         if (!ssi_private->has_ipg_clk_name)
1434                 clk_disable_unprepare(ssi_private->clk);
1435 }
1436
1437 static int fsl_ssi_probe(struct platform_device *pdev)
1438 {
1439         struct fsl_ssi_private *ssi_private;
1440         int ret = 0;
1441         struct device_node *np = pdev->dev.of_node;
1442         const struct of_device_id *of_id;
1443         const char *p, *sprop;
1444         const uint32_t *iprop;
1445         struct resource *res;
1446         void __iomem *iomem;
1447         char name[64];
1448         struct regmap_config regconfig = fsl_ssi_regconfig;
1449
1450         of_id = of_match_device(fsl_ssi_ids, &pdev->dev);
1451         if (!of_id || !of_id->data)
1452                 return -EINVAL;
1453
1454         ssi_private = devm_kzalloc(&pdev->dev, sizeof(*ssi_private),
1455                         GFP_KERNEL);
1456         if (!ssi_private) {
1457                 dev_err(&pdev->dev, "could not allocate DAI object\n");
1458                 return -ENOMEM;
1459         }
1460
1461         ssi_private->soc = of_id->data;
1462         ssi_private->dev = &pdev->dev;
1463
1464         sprop = of_get_property(np, "fsl,mode", NULL);
1465         if (sprop) {
1466                 if (!strcmp(sprop, "ac97-slave"))
1467                         ssi_private->dai_fmt = SND_SOC_DAIFMT_AC97;
1468         }
1469
1470         ssi_private->use_dma = !of_property_read_bool(np,
1471                         "fsl,fiq-stream-filter");
1472
1473         if (fsl_ssi_is_ac97(ssi_private)) {
1474                 memcpy(&ssi_private->cpu_dai_drv, &fsl_ssi_ac97_dai,
1475                                 sizeof(fsl_ssi_ac97_dai));
1476
1477                 fsl_ac97_data = ssi_private;
1478
1479                 ret = snd_soc_set_ac97_ops_of_reset(&fsl_ssi_ac97_ops, pdev);
1480                 if (ret) {
1481                         dev_err(&pdev->dev, "could not set AC'97 ops\n");
1482                         return ret;
1483                 }
1484         } else {
1485                 /* Initialize this copy of the CPU DAI driver structure */
1486                 memcpy(&ssi_private->cpu_dai_drv, &fsl_ssi_dai_template,
1487                        sizeof(fsl_ssi_dai_template));
1488         }
1489         ssi_private->cpu_dai_drv.name = dev_name(&pdev->dev);
1490
1491         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1492         iomem = devm_ioremap_resource(&pdev->dev, res);
1493         if (IS_ERR(iomem))
1494                 return PTR_ERR(iomem);
1495         ssi_private->ssi_phys = res->start;
1496
1497         if (ssi_private->soc->imx21regs) {
1498                 /*
1499                  * According to datasheet imx21-class SSI
1500                  * don't have SACC{ST,EN,DIS} regs.
1501                  */
1502                 regconfig.max_register = CCSR_SSI_SRMSK;
1503         }
1504
1505         ret = of_property_match_string(np, "clock-names", "ipg");
1506         if (ret < 0) {
1507                 ssi_private->has_ipg_clk_name = false;
1508                 ssi_private->regs = devm_regmap_init_mmio(&pdev->dev, iomem,
1509                         &regconfig);
1510         } else {
1511                 ssi_private->has_ipg_clk_name = true;
1512                 ssi_private->regs = devm_regmap_init_mmio_clk(&pdev->dev,
1513                         "ipg", iomem, &regconfig);
1514         }
1515         if (IS_ERR(ssi_private->regs)) {
1516                 dev_err(&pdev->dev, "Failed to init register map\n");
1517                 return PTR_ERR(ssi_private->regs);
1518         }
1519
1520         ssi_private->irq = platform_get_irq(pdev, 0);
1521         if (ssi_private->irq < 0) {
1522                 dev_err(&pdev->dev, "no irq for node %s\n", pdev->name);
1523                 return ssi_private->irq;
1524         }
1525
1526         /* Are the RX and the TX clocks locked? */
1527         if (!of_find_property(np, "fsl,ssi-asynchronous", NULL)) {
1528                 if (!fsl_ssi_is_ac97(ssi_private))
1529                         ssi_private->cpu_dai_drv.symmetric_rates = 1;
1530
1531                 ssi_private->cpu_dai_drv.symmetric_channels = 1;
1532                 ssi_private->cpu_dai_drv.symmetric_samplebits = 1;
1533         }
1534
1535         /* Determine the FIFO depth. */
1536         iprop = of_get_property(np, "fsl,fifo-depth", NULL);
1537         if (iprop)
1538                 ssi_private->fifo_depth = be32_to_cpup(iprop);
1539         else
1540                 /* Older 8610 DTs didn't have the fifo-depth property */
1541                 ssi_private->fifo_depth = 8;
1542
1543         dev_set_drvdata(&pdev->dev, ssi_private);
1544
1545         if (ssi_private->soc->imx) {
1546                 ret = fsl_ssi_imx_probe(pdev, ssi_private, iomem);
1547                 if (ret)
1548                         return ret;
1549         }
1550
1551         ret = devm_snd_soc_register_component(&pdev->dev, &fsl_ssi_component,
1552                                               &ssi_private->cpu_dai_drv, 1);
1553         if (ret) {
1554                 dev_err(&pdev->dev, "failed to register DAI: %d\n", ret);
1555                 goto error_asoc_register;
1556         }
1557
1558         if (ssi_private->use_dma) {
1559                 ret = devm_request_irq(&pdev->dev, ssi_private->irq,
1560                                         fsl_ssi_isr, 0, dev_name(&pdev->dev),
1561                                         ssi_private);
1562                 if (ret < 0) {
1563                         dev_err(&pdev->dev, "could not claim irq %u\n",
1564                                         ssi_private->irq);
1565                         goto error_asoc_register;
1566                 }
1567         }
1568
1569         ret = fsl_ssi_debugfs_create(&ssi_private->dbg_stats, &pdev->dev);
1570         if (ret)
1571                 goto error_asoc_register;
1572
1573         /*
1574          * If codec-handle property is missing from SSI node, we assume
1575          * that the machine driver uses new binding which does not require
1576          * SSI driver to trigger machine driver's probe.
1577          */
1578         if (!of_get_property(np, "codec-handle", NULL))
1579                 goto done;
1580
1581         /* Trigger the machine driver's probe function.  The platform driver
1582          * name of the machine driver is taken from /compatible property of the
1583          * device tree.  We also pass the address of the CPU DAI driver
1584          * structure.
1585          */
1586         sprop = of_get_property(of_find_node_by_path("/"), "compatible", NULL);
1587         /* Sometimes the compatible name has a "fsl," prefix, so we strip it. */
1588         p = strrchr(sprop, ',');
1589         if (p)
1590                 sprop = p + 1;
1591         snprintf(name, sizeof(name), "snd-soc-%s", sprop);
1592         make_lowercase(name);
1593
1594         ssi_private->pdev =
1595                 platform_device_register_data(&pdev->dev, name, 0, NULL, 0);
1596         if (IS_ERR(ssi_private->pdev)) {
1597                 ret = PTR_ERR(ssi_private->pdev);
1598                 dev_err(&pdev->dev, "failed to register platform: %d\n", ret);
1599                 goto error_sound_card;
1600         }
1601
1602 done:
1603         if (ssi_private->dai_fmt)
1604                 _fsl_ssi_set_dai_fmt(&pdev->dev, ssi_private,
1605                                      ssi_private->dai_fmt);
1606
1607         if (fsl_ssi_is_ac97(ssi_private)) {
1608                 u32 ssi_idx;
1609
1610                 ret = of_property_read_u32(np, "cell-index", &ssi_idx);
1611                 if (ret) {
1612                         dev_err(&pdev->dev, "cannot get SSI index property\n");
1613                         goto error_sound_card;
1614                 }
1615
1616                 ssi_private->pdev =
1617                         platform_device_register_data(NULL,
1618                                         "ac97-codec", ssi_idx, NULL, 0);
1619                 if (IS_ERR(ssi_private->pdev)) {
1620                         ret = PTR_ERR(ssi_private->pdev);
1621                         dev_err(&pdev->dev,
1622                                 "failed to register AC97 codec platform: %d\n",
1623                                 ret);
1624                         goto error_sound_card;
1625                 }
1626         }
1627
1628         return 0;
1629
1630 error_sound_card:
1631         fsl_ssi_debugfs_remove(&ssi_private->dbg_stats);
1632
1633 error_asoc_register:
1634         if (ssi_private->soc->imx)
1635                 fsl_ssi_imx_clean(pdev, ssi_private);
1636
1637         return ret;
1638 }
1639
1640 static int fsl_ssi_remove(struct platform_device *pdev)
1641 {
1642         struct fsl_ssi_private *ssi_private = dev_get_drvdata(&pdev->dev);
1643
1644         fsl_ssi_debugfs_remove(&ssi_private->dbg_stats);
1645
1646         if (ssi_private->pdev)
1647                 platform_device_unregister(ssi_private->pdev);
1648
1649         if (ssi_private->soc->imx)
1650                 fsl_ssi_imx_clean(pdev, ssi_private);
1651
1652         if (fsl_ssi_is_ac97(ssi_private))
1653                 snd_soc_set_ac97_ops(NULL);
1654
1655         return 0;
1656 }
1657
1658 #ifdef CONFIG_PM_SLEEP
1659 static int fsl_ssi_suspend(struct device *dev)
1660 {
1661         struct fsl_ssi_private *ssi_private = dev_get_drvdata(dev);
1662         struct regmap *regs = ssi_private->regs;
1663
1664         regmap_read(regs, CCSR_SSI_SFCSR,
1665                         &ssi_private->regcache_sfcsr);
1666         regmap_read(regs, CCSR_SSI_SACNT,
1667                         &ssi_private->regcache_sacnt);
1668
1669         regcache_cache_only(regs, true);
1670         regcache_mark_dirty(regs);
1671
1672         return 0;
1673 }
1674
1675 static int fsl_ssi_resume(struct device *dev)
1676 {
1677         struct fsl_ssi_private *ssi_private = dev_get_drvdata(dev);
1678         struct regmap *regs = ssi_private->regs;
1679
1680         regcache_cache_only(regs, false);
1681
1682         regmap_update_bits(regs, CCSR_SSI_SFCSR,
1683                         CCSR_SSI_SFCSR_RFWM1_MASK | CCSR_SSI_SFCSR_TFWM1_MASK |
1684                         CCSR_SSI_SFCSR_RFWM0_MASK | CCSR_SSI_SFCSR_TFWM0_MASK,
1685                         ssi_private->regcache_sfcsr);
1686         regmap_write(regs, CCSR_SSI_SACNT,
1687                         ssi_private->regcache_sacnt);
1688
1689         return regcache_sync(regs);
1690 }
1691 #endif /* CONFIG_PM_SLEEP */
1692
1693 static const struct dev_pm_ops fsl_ssi_pm = {
1694         SET_SYSTEM_SLEEP_PM_OPS(fsl_ssi_suspend, fsl_ssi_resume)
1695 };
1696
1697 static struct platform_driver fsl_ssi_driver = {
1698         .driver = {
1699                 .name = "fsl-ssi-dai",
1700                 .of_match_table = fsl_ssi_ids,
1701                 .pm = &fsl_ssi_pm,
1702         },
1703         .probe = fsl_ssi_probe,
1704         .remove = fsl_ssi_remove,
1705 };
1706
1707 module_platform_driver(fsl_ssi_driver);
1708
1709 MODULE_ALIAS("platform:fsl-ssi-dai");
1710 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
1711 MODULE_DESCRIPTION("Freescale Synchronous Serial Interface (SSI) ASoC Driver");
1712 MODULE_LICENSE("GPL v2");