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