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