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