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