2 * Freescale SSI ALSA SoC Digital Audio Interface (DAI) driver
4 * Author: Timur Tabi <timur@freescale.com>
6 * Copyright 2007-2010 Freescale Semiconductor, Inc.
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.
13 * Some notes why imx-pcm-fiq is used instead of DMA on some boards:
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
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.
33 #include <linux/init.h>
35 #include <linux/module.h>
36 #include <linux/interrupt.h>
37 #include <linux/clk.h>
38 #include <linux/ctype.h>
39 #include <linux/device.h>
40 #include <linux/delay.h>
41 #include <linux/slab.h>
42 #include <linux/spinlock.h>
44 #include <linux/of_address.h>
45 #include <linux/of_irq.h>
46 #include <linux/of_platform.h>
48 #include <sound/core.h>
49 #include <sound/pcm.h>
50 #include <sound/pcm_params.h>
51 #include <sound/initval.h>
52 #include <sound/soc.h>
53 #include <sound/dmaengine_pcm.h>
59 * FSLSSI_I2S_FORMATS: audio formats supported by the SSI
61 * The SSI has a limitation in that the samples must be in the same byte
62 * order as the host CPU. This is because when multiple bytes are written
63 * to the STX register, the bytes and bits must be written in the same
64 * order. The STX is a shift register, so all the bits need to be aligned
65 * (bit-endianness must match byte-endianness). Processors typically write
66 * the bits within a byte in the same order that the bytes of a word are
67 * written in. So if the host CPU is big-endian, then only big-endian
68 * samples will be written to STX properly.
71 #define FSLSSI_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_BE | \
72 SNDRV_PCM_FMTBIT_S18_3BE | SNDRV_PCM_FMTBIT_S20_3BE | \
73 SNDRV_PCM_FMTBIT_S24_3BE | SNDRV_PCM_FMTBIT_S24_BE)
75 #define FSLSSI_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE | \
76 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S20_3LE | \
77 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE)
80 #define FSLSSI_SIER_DBG_RX_FLAGS (CCSR_SSI_SIER_RFF0_EN | \
81 CCSR_SSI_SIER_RLS_EN | CCSR_SSI_SIER_RFS_EN | \
82 CCSR_SSI_SIER_ROE0_EN | CCSR_SSI_SIER_RFRC_EN)
83 #define FSLSSI_SIER_DBG_TX_FLAGS (CCSR_SSI_SIER_TFE0_EN | \
84 CCSR_SSI_SIER_TLS_EN | CCSR_SSI_SIER_TFS_EN | \
85 CCSR_SSI_SIER_TUE0_EN | CCSR_SSI_SIER_TFRC_EN)
94 struct fsl_ssi_reg_val {
101 struct fsl_ssi_rxtx_reg_val {
102 struct fsl_ssi_reg_val rx;
103 struct fsl_ssi_reg_val tx;
106 static bool fsl_ssi_readable_reg(struct device *dev, unsigned int reg)
109 case CCSR_SSI_SACCEN:
110 case CCSR_SSI_SACCDIS:
117 static bool fsl_ssi_volatile_reg(struct device *dev, unsigned int reg)
127 case CCSR_SSI_SACADD:
128 case CCSR_SSI_SACDAT:
130 case CCSR_SSI_SACCST:
138 static bool fsl_ssi_precious_reg(struct device *dev, unsigned int reg)
144 case CCSR_SSI_SACADD:
145 case CCSR_SSI_SACDAT:
153 static bool fsl_ssi_writeable_reg(struct device *dev, unsigned int reg)
158 case CCSR_SSI_SACCST:
165 static const struct regmap_config fsl_ssi_regconfig = {
166 .max_register = CCSR_SSI_SACCDIS,
170 .val_format_endian = REGMAP_ENDIAN_NATIVE,
171 .num_reg_defaults_raw = CCSR_SSI_SACCDIS / sizeof(uint32_t) + 1,
172 .readable_reg = fsl_ssi_readable_reg,
173 .volatile_reg = fsl_ssi_volatile_reg,
174 .precious_reg = fsl_ssi_precious_reg,
175 .writeable_reg = fsl_ssi_writeable_reg,
176 .cache_type = REGCACHE_FLAT,
179 struct fsl_ssi_soc_data {
181 bool imx21regs; /* imx21-class SSI - no SACC{ST,EN,DIS} regs */
187 * fsl_ssi_private: per-SSI private data
189 * @reg: Pointer to the regmap registers
190 * @irq: IRQ of this SSI
191 * @cpu_dai_drv: CPU DAI driver for this device
193 * @dai_fmt: DAI configuration this device is currently used with
194 * @i2s_mode: i2s and network mode configuration of the device. Is used to
195 * switch between normal and i2s/network mode
196 * mode depending on the number of channels
197 * @use_dma: DMA is used or FIQ with stream filter
198 * @use_dual_fifo: DMA with support for both FIFOs used
199 * @fifo_deph: Depth of the SSI FIFOs
200 * @rxtx_reg_val: Specific register settings for receive/transmit configuration
203 * @baudclk: SSI baud clock for master mode
204 * @baudclk_streams: Active streams that are using baudclk
205 * @bitclk_freq: bitclock frequency set by .set_dai_sysclk
207 * @dma_params_tx: DMA transmit parameters
208 * @dma_params_rx: DMA receive parameters
209 * @ssi_phys: physical address of the SSI registers
211 * @fiq_params: FIQ stream filtering parameters
213 * @pdev: Pointer to pdev used for deprecated fsl-ssi sound card
215 * @dbg_stats: Debugging statistics
217 * @soc: SoC specific data
219 struct fsl_ssi_private {
222 struct snd_soc_dai_driver cpu_dai_drv;
224 unsigned int dai_fmt;
228 bool has_ipg_clk_name;
229 unsigned int fifo_depth;
230 struct fsl_ssi_rxtx_reg_val rxtx_reg_val;
234 unsigned int baudclk_streams;
235 unsigned int bitclk_freq;
237 /* regcache for volatile regs */
242 struct snd_dmaengine_dai_dma_data dma_params_tx;
243 struct snd_dmaengine_dai_dma_data dma_params_rx;
246 /* params for non-dma FIQ stream filtered mode */
247 struct imx_pcm_fiq_params fiq_params;
249 /* Used when using fsl-ssi as sound-card. This is only used by ppc and
250 * should be replaced with simple-sound-card. */
251 struct platform_device *pdev;
253 struct fsl_ssi_dbg dbg_stats;
255 const struct fsl_ssi_soc_data *soc;
260 * imx51 and later SoCs have a slightly different IP that allows the
261 * SSI configuration while the SSI unit is running.
263 * More important, it is necessary on those SoCs to configure the
264 * sperate TX/RX DMA bits just before starting the stream
265 * (fsl_ssi_trigger). The SDMA unit has to be configured before fsl_ssi
266 * sends any DMA requests to the SDMA unit, otherwise it is not defined
267 * how the SDMA unit handles the DMA request.
269 * SDMA units are present on devices starting at imx35 but the imx35
270 * reference manual states that the DMA bits should not be changed
271 * while the SSI unit is running (SSIEN). So we support the necessary
272 * online configuration of fsl-ssi starting at imx51.
275 static struct fsl_ssi_soc_data fsl_ssi_mpc8610 = {
277 .offline_config = true,
278 .sisr_write_mask = CCSR_SSI_SISR_RFRC | CCSR_SSI_SISR_TFRC |
279 CCSR_SSI_SISR_ROE0 | CCSR_SSI_SISR_ROE1 |
280 CCSR_SSI_SISR_TUE0 | CCSR_SSI_SISR_TUE1,
283 static struct fsl_ssi_soc_data fsl_ssi_imx21 = {
286 .offline_config = true,
287 .sisr_write_mask = 0,
290 static struct fsl_ssi_soc_data fsl_ssi_imx35 = {
292 .offline_config = true,
293 .sisr_write_mask = CCSR_SSI_SISR_RFRC | CCSR_SSI_SISR_TFRC |
294 CCSR_SSI_SISR_ROE0 | CCSR_SSI_SISR_ROE1 |
295 CCSR_SSI_SISR_TUE0 | CCSR_SSI_SISR_TUE1,
298 static struct fsl_ssi_soc_data fsl_ssi_imx51 = {
300 .offline_config = false,
301 .sisr_write_mask = CCSR_SSI_SISR_ROE0 | CCSR_SSI_SISR_ROE1 |
302 CCSR_SSI_SISR_TUE0 | CCSR_SSI_SISR_TUE1,
305 static const struct of_device_id fsl_ssi_ids[] = {
306 { .compatible = "fsl,mpc8610-ssi", .data = &fsl_ssi_mpc8610 },
307 { .compatible = "fsl,imx51-ssi", .data = &fsl_ssi_imx51 },
308 { .compatible = "fsl,imx35-ssi", .data = &fsl_ssi_imx35 },
309 { .compatible = "fsl,imx21-ssi", .data = &fsl_ssi_imx21 },
312 MODULE_DEVICE_TABLE(of, fsl_ssi_ids);
314 static bool fsl_ssi_is_ac97(struct fsl_ssi_private *ssi_private)
316 return (ssi_private->dai_fmt & SND_SOC_DAIFMT_FORMAT_MASK) ==
320 static bool fsl_ssi_is_i2s_master(struct fsl_ssi_private *ssi_private)
322 return (ssi_private->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) ==
323 SND_SOC_DAIFMT_CBS_CFS;
326 static bool fsl_ssi_is_i2s_cbm_cfs(struct fsl_ssi_private *ssi_private)
328 return (ssi_private->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) ==
329 SND_SOC_DAIFMT_CBM_CFS;
332 * fsl_ssi_isr: SSI interrupt handler
334 * Although it's possible to use the interrupt handler to send and receive
335 * data to/from the SSI, we use the DMA instead. Programming is more
336 * complicated, but the performance is much better.
338 * This interrupt handler is used only to gather statistics.
340 * @irq: IRQ of the SSI device
341 * @dev_id: pointer to the ssi_private structure for this SSI device
343 static irqreturn_t fsl_ssi_isr(int irq, void *dev_id)
345 struct fsl_ssi_private *ssi_private = dev_id;
346 struct regmap *regs = ssi_private->regs;
350 /* We got an interrupt, so read the status register to see what we
351 were interrupted for. We mask it with the Interrupt Enable register
352 so that we only check for events that we're interested in.
354 regmap_read(regs, CCSR_SSI_SISR, &sisr);
356 sisr2 = sisr & ssi_private->soc->sisr_write_mask;
357 /* Clear the bits that we set */
359 regmap_write(regs, CCSR_SSI_SISR, sisr2);
361 fsl_ssi_dbg_isr(&ssi_private->dbg_stats, sisr);
367 * Enable/Disable all rx/tx config flags at once.
369 static void fsl_ssi_rxtx_config(struct fsl_ssi_private *ssi_private,
372 struct regmap *regs = ssi_private->regs;
373 struct fsl_ssi_rxtx_reg_val *vals = &ssi_private->rxtx_reg_val;
376 regmap_update_bits(regs, CCSR_SSI_SIER,
377 vals->rx.sier | vals->tx.sier,
378 vals->rx.sier | vals->tx.sier);
379 regmap_update_bits(regs, CCSR_SSI_SRCR,
380 vals->rx.srcr | vals->tx.srcr,
381 vals->rx.srcr | vals->tx.srcr);
382 regmap_update_bits(regs, CCSR_SSI_STCR,
383 vals->rx.stcr | vals->tx.stcr,
384 vals->rx.stcr | vals->tx.stcr);
386 regmap_update_bits(regs, CCSR_SSI_SRCR,
387 vals->rx.srcr | vals->tx.srcr, 0);
388 regmap_update_bits(regs, CCSR_SSI_STCR,
389 vals->rx.stcr | vals->tx.stcr, 0);
390 regmap_update_bits(regs, CCSR_SSI_SIER,
391 vals->rx.sier | vals->tx.sier, 0);
396 * Clear RX or TX FIFO to remove samples from the previous
397 * stream session which may be still present in the FIFO and
398 * may introduce bad samples and/or channel slipping.
400 * Note: The SOR is not documented in recent IMX datasheet, but
401 * is described in IMX51 reference manual at section 56.3.3.15.
403 static void fsl_ssi_fifo_clear(struct fsl_ssi_private *ssi_private,
407 regmap_update_bits(ssi_private->regs, CCSR_SSI_SOR,
408 CCSR_SSI_SOR_RX_CLR, CCSR_SSI_SOR_RX_CLR);
410 regmap_update_bits(ssi_private->regs, CCSR_SSI_SOR,
411 CCSR_SSI_SOR_TX_CLR, CCSR_SSI_SOR_TX_CLR);
416 * Calculate the bits that have to be disabled for the current stream that is
417 * getting disabled. This keeps the bits enabled that are necessary for the
418 * second stream to work if 'stream_active' is true.
420 * Detailed calculation:
421 * These are the values that need to be active after disabling. For non-active
422 * second stream, this is 0:
423 * vals_stream * !!stream_active
425 * The following computes the overall differences between the setup for the
426 * to-disable stream and the active stream, a simple XOR:
427 * vals_disable ^ (vals_stream * !!(stream_active))
429 * The full expression adds a mask on all values we care about
431 #define fsl_ssi_disable_val(vals_disable, vals_stream, stream_active) \
433 ((vals_disable) ^ ((vals_stream) * (u32)!!(stream_active))))
436 * Enable/Disable a ssi configuration. You have to pass either
437 * ssi_private->rxtx_reg_val.rx or tx as vals parameter.
439 static void fsl_ssi_config(struct fsl_ssi_private *ssi_private, bool enable,
440 struct fsl_ssi_reg_val *vals)
442 struct regmap *regs = ssi_private->regs;
443 struct fsl_ssi_reg_val *avals;
444 int nr_active_streams;
448 regmap_read(regs, CCSR_SSI_SCR, &scr_val);
450 nr_active_streams = !!(scr_val & CCSR_SSI_SCR_TE) +
451 !!(scr_val & CCSR_SSI_SCR_RE);
453 if (nr_active_streams - 1 > 0)
458 /* Find the other direction values rx or tx which we do not want to
460 if (&ssi_private->rxtx_reg_val.rx == vals)
461 avals = &ssi_private->rxtx_reg_val.tx;
463 avals = &ssi_private->rxtx_reg_val.rx;
465 /* If vals should be disabled, start with disabling the unit */
467 u32 scr = fsl_ssi_disable_val(vals->scr, avals->scr,
469 regmap_update_bits(regs, CCSR_SSI_SCR, scr, 0);
473 * We are running on a SoC which does not support online SSI
474 * reconfiguration, so we have to enable all necessary flags at once
475 * even if we do not use them later (capture and playback configuration)
477 if (ssi_private->soc->offline_config) {
478 if ((enable && !nr_active_streams) ||
479 (!enable && !keep_active))
480 fsl_ssi_rxtx_config(ssi_private, enable);
486 * Configure single direction units while the SSI unit is running
487 * (online configuration)
490 fsl_ssi_fifo_clear(ssi_private, vals->scr & CCSR_SSI_SCR_RE);
492 regmap_update_bits(regs, CCSR_SSI_SRCR, vals->srcr, vals->srcr);
493 regmap_update_bits(regs, CCSR_SSI_STCR, vals->stcr, vals->stcr);
494 regmap_update_bits(regs, CCSR_SSI_SIER, vals->sier, vals->sier);
501 * Disabling the necessary flags for one of rx/tx while the
502 * other stream is active is a little bit more difficult. We
503 * have to disable only those flags that differ between both
504 * streams (rx XOR tx) and that are set in the stream that is
505 * disabled now. Otherwise we could alter flags of the other
509 /* These assignments are simply vals without bits set in avals*/
510 sier = fsl_ssi_disable_val(vals->sier, avals->sier,
512 srcr = fsl_ssi_disable_val(vals->srcr, avals->srcr,
514 stcr = fsl_ssi_disable_val(vals->stcr, avals->stcr,
517 regmap_update_bits(regs, CCSR_SSI_SRCR, srcr, 0);
518 regmap_update_bits(regs, CCSR_SSI_STCR, stcr, 0);
519 regmap_update_bits(regs, CCSR_SSI_SIER, sier, 0);
523 /* Enabling of subunits is done after configuration */
525 if (ssi_private->use_dma && (vals->scr & CCSR_SSI_SCR_TE)) {
527 * Be sure the Tx FIFO is filled when TE is set.
528 * Otherwise, there are some chances to start the
529 * playback with some void samples inserted first,
530 * generating a channel slip.
532 * First, SSIEN must be set, to let the FIFO be filled.
535 * - Limit this fix to the DMA case until FIQ cases can
537 * - Limit the length of the busy loop to not lock the
538 * system too long, even if 1-2 loops are sufficient
543 regmap_update_bits(regs, CCSR_SSI_SCR,
544 CCSR_SSI_SCR_SSIEN, CCSR_SSI_SCR_SSIEN);
545 for (i = 0; i < max_loop; i++) {
547 regmap_read(regs, CCSR_SSI_SFCSR, &sfcsr);
548 if (CCSR_SSI_SFCSR_TFCNT0(sfcsr))
552 dev_err(ssi_private->dev,
553 "Timeout waiting TX FIFO filling\n");
556 regmap_update_bits(regs, CCSR_SSI_SCR, vals->scr, vals->scr);
561 static void fsl_ssi_rx_config(struct fsl_ssi_private *ssi_private, bool enable)
563 fsl_ssi_config(ssi_private, enable, &ssi_private->rxtx_reg_val.rx);
566 static void fsl_ssi_tx_config(struct fsl_ssi_private *ssi_private, bool enable)
568 fsl_ssi_config(ssi_private, enable, &ssi_private->rxtx_reg_val.tx);
572 * Setup rx/tx register values used to enable/disable the streams. These will
573 * be used later in fsl_ssi_config to setup the streams without the need to
574 * check for all different SSI modes.
576 static void fsl_ssi_setup_reg_vals(struct fsl_ssi_private *ssi_private)
578 struct fsl_ssi_rxtx_reg_val *reg = &ssi_private->rxtx_reg_val;
580 reg->rx.sier = CCSR_SSI_SIER_RFF0_EN;
581 reg->rx.srcr = CCSR_SSI_SRCR_RFEN0;
583 reg->tx.sier = CCSR_SSI_SIER_TFE0_EN;
584 reg->tx.stcr = CCSR_SSI_STCR_TFEN0;
587 if (!fsl_ssi_is_ac97(ssi_private)) {
588 reg->rx.scr = CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_RE;
589 reg->rx.sier |= CCSR_SSI_SIER_RFF0_EN;
590 reg->tx.scr = CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_TE;
591 reg->tx.sier |= CCSR_SSI_SIER_TFE0_EN;
594 if (ssi_private->use_dma) {
595 reg->rx.sier |= CCSR_SSI_SIER_RDMAE;
596 reg->tx.sier |= CCSR_SSI_SIER_TDMAE;
598 reg->rx.sier |= CCSR_SSI_SIER_RIE;
599 reg->tx.sier |= CCSR_SSI_SIER_TIE;
602 reg->rx.sier |= FSLSSI_SIER_DBG_RX_FLAGS;
603 reg->tx.sier |= FSLSSI_SIER_DBG_TX_FLAGS;
606 static void fsl_ssi_setup_ac97(struct fsl_ssi_private *ssi_private)
608 struct regmap *regs = ssi_private->regs;
611 * Setup the clock control register
613 regmap_write(regs, CCSR_SSI_STCCR,
614 CCSR_SSI_SxCCR_WL(17) | CCSR_SSI_SxCCR_DC(13));
615 regmap_write(regs, CCSR_SSI_SRCCR,
616 CCSR_SSI_SxCCR_WL(17) | CCSR_SSI_SxCCR_DC(13));
619 * Enable AC97 mode and startup the SSI
621 regmap_write(regs, CCSR_SSI_SACNT,
622 CCSR_SSI_SACNT_AC97EN | CCSR_SSI_SACNT_FV);
624 /* no SACC{ST,EN,DIS} regs on imx21-class SSI */
625 if (!ssi_private->soc->imx21regs) {
626 regmap_write(regs, CCSR_SSI_SACCDIS, 0xff);
627 regmap_write(regs, CCSR_SSI_SACCEN, 0x300);
631 * Enable SSI, Transmit and Receive. AC97 has to communicate with the
632 * codec before a stream is started.
634 regmap_update_bits(regs, CCSR_SSI_SCR,
635 CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_TE | CCSR_SSI_SCR_RE,
636 CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_TE | CCSR_SSI_SCR_RE);
638 regmap_write(regs, CCSR_SSI_SOR, CCSR_SSI_SOR_WAIT(3));
642 * fsl_ssi_startup: create a new substream
644 * This is the first function called when a stream is opened.
646 * If this is the first stream open, then grab the IRQ and program most of
649 static int fsl_ssi_startup(struct snd_pcm_substream *substream,
650 struct snd_soc_dai *dai)
652 struct snd_soc_pcm_runtime *rtd = substream->private_data;
653 struct fsl_ssi_private *ssi_private =
654 snd_soc_dai_get_drvdata(rtd->cpu_dai);
657 ret = clk_prepare_enable(ssi_private->clk);
661 /* When using dual fifo mode, it is safer to ensure an even period
662 * size. If appearing to an odd number while DMA always starts its
663 * task from fifo0, fifo1 would be neglected at the end of each
664 * period. But SSI would still access fifo1 with an invalid data.
666 if (ssi_private->use_dual_fifo)
667 snd_pcm_hw_constraint_step(substream->runtime, 0,
668 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 2);
674 * fsl_ssi_shutdown: shutdown the SSI
677 static void fsl_ssi_shutdown(struct snd_pcm_substream *substream,
678 struct snd_soc_dai *dai)
680 struct snd_soc_pcm_runtime *rtd = substream->private_data;
681 struct fsl_ssi_private *ssi_private =
682 snd_soc_dai_get_drvdata(rtd->cpu_dai);
684 clk_disable_unprepare(ssi_private->clk);
689 * fsl_ssi_set_bclk - configure Digital Audio Interface bit clock
691 * Note: This function can be only called when using SSI as DAI master
693 * Quick instruction for parameters:
694 * freq: Output BCLK frequency = samplerate * 32 (fixed) * channels
695 * dir: SND_SOC_CLOCK_OUT -> TxBCLK, SND_SOC_CLOCK_IN -> RxBCLK.
697 static int fsl_ssi_set_bclk(struct snd_pcm_substream *substream,
698 struct snd_soc_dai *cpu_dai,
699 struct snd_pcm_hw_params *hw_params)
701 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
702 struct regmap *regs = ssi_private->regs;
703 int synchronous = ssi_private->cpu_dai_drv.symmetric_rates, ret;
704 u32 pm = 999, div2, psr, stccr, mask, afreq, factor, i;
705 unsigned long clkrate, baudrate, tmprate;
706 u64 sub, savesub = 100000;
708 bool baudclk_is_used;
710 /* Prefer the explicitly set bitclock frequency */
711 if (ssi_private->bitclk_freq)
712 freq = ssi_private->bitclk_freq;
714 freq = params_channels(hw_params) * 32 * params_rate(hw_params);
716 /* Don't apply it to any non-baudclk circumstance */
717 if (IS_ERR(ssi_private->baudclk))
721 * Hardware limitation: The bclk rate must be
722 * never greater than 1/5 IPG clock rate
724 if (freq * 5 > clk_get_rate(ssi_private->clk)) {
725 dev_err(cpu_dai->dev, "bitclk > ipgclk/5\n");
729 baudclk_is_used = ssi_private->baudclk_streams & ~(BIT(substream->stream));
731 /* It should be already enough to divide clock by setting pm alone */
735 factor = (div2 + 1) * (7 * psr + 1) * 2;
737 for (i = 0; i < 255; i++) {
738 tmprate = freq * factor * (i + 1);
741 clkrate = clk_get_rate(ssi_private->baudclk);
743 clkrate = clk_round_rate(ssi_private->baudclk, tmprate);
746 afreq = clkrate / (i + 1);
750 else if (freq / afreq == 1)
752 else if (afreq / freq == 1)
757 /* Calculate the fraction */
761 if (sub < savesub && !(i == 0 && psr == 0 && div2 == 0)) {
772 /* No proper pm found if it is still remaining the initial value */
774 dev_err(cpu_dai->dev, "failed to handle the required sysclk\n");
778 stccr = CCSR_SSI_SxCCR_PM(pm + 1) | (div2 ? CCSR_SSI_SxCCR_DIV2 : 0) |
779 (psr ? CCSR_SSI_SxCCR_PSR : 0);
780 mask = CCSR_SSI_SxCCR_PM_MASK | CCSR_SSI_SxCCR_DIV2 |
783 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK || synchronous)
784 regmap_update_bits(regs, CCSR_SSI_STCCR, mask, stccr);
786 regmap_update_bits(regs, CCSR_SSI_SRCCR, mask, stccr);
788 if (!baudclk_is_used) {
789 ret = clk_set_rate(ssi_private->baudclk, baudrate);
791 dev_err(cpu_dai->dev, "failed to set baudclk rate\n");
799 static int fsl_ssi_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
800 int clk_id, unsigned int freq, int dir)
802 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
804 ssi_private->bitclk_freq = freq;
810 * fsl_ssi_hw_params - program the sample size
812 * Most of the SSI registers have been programmed in the startup function,
813 * but the word length must be programmed here. Unfortunately, programming
814 * the SxCCR.WL bits requires the SSI to be temporarily disabled. This can
815 * cause a problem with supporting simultaneous playback and capture. If
816 * the SSI is already playing a stream, then that stream may be temporarily
817 * stopped when you start capture.
819 * Note: The SxCCR.DC and SxCCR.PM bits are only used if the SSI is the
822 static int fsl_ssi_hw_params(struct snd_pcm_substream *substream,
823 struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *cpu_dai)
825 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
826 struct regmap *regs = ssi_private->regs;
827 unsigned int channels = params_channels(hw_params);
828 unsigned int sample_size = params_width(hw_params);
829 u32 wl = CCSR_SSI_SxCCR_WL(sample_size);
834 regmap_read(regs, CCSR_SSI_SCR, &scr_val);
835 enabled = scr_val & CCSR_SSI_SCR_SSIEN;
838 * If we're in synchronous mode, and the SSI is already enabled,
839 * then STCCR is already set properly.
841 if (enabled && ssi_private->cpu_dai_drv.symmetric_rates)
844 if (fsl_ssi_is_i2s_master(ssi_private)) {
845 ret = fsl_ssi_set_bclk(substream, cpu_dai, hw_params);
849 /* Do not enable the clock if it is already enabled */
850 if (!(ssi_private->baudclk_streams & BIT(substream->stream))) {
851 ret = clk_prepare_enable(ssi_private->baudclk);
855 ssi_private->baudclk_streams |= BIT(substream->stream);
859 if (!fsl_ssi_is_ac97(ssi_private)) {
862 * Switch to normal net mode in order to have a frame sync
863 * signal every 32 bits instead of 16 bits
865 if (fsl_ssi_is_i2s_cbm_cfs(ssi_private) && sample_size == 16)
866 i2smode = CCSR_SSI_SCR_I2S_MODE_NORMAL |
869 i2smode = ssi_private->i2s_mode;
871 regmap_update_bits(regs, CCSR_SSI_SCR,
872 CCSR_SSI_SCR_NET | CCSR_SSI_SCR_I2S_MODE_MASK,
873 channels == 1 ? 0 : i2smode);
877 * FIXME: The documentation says that SxCCR[WL] should not be
878 * modified while the SSI is enabled. The only time this can
879 * happen is if we're trying to do simultaneous playback and
880 * capture in asynchronous mode. Unfortunately, I have been enable
881 * to get that to work at all on the P1022DS. Therefore, we don't
882 * bother to disable/enable the SSI when setting SxCCR[WL], because
883 * the SSI will stop anyway. Maybe one day, this will get fixed.
886 /* In synchronous mode, the SSI uses STCCR for capture */
887 if ((substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ||
888 ssi_private->cpu_dai_drv.symmetric_rates)
889 regmap_update_bits(regs, CCSR_SSI_STCCR, CCSR_SSI_SxCCR_WL_MASK,
892 regmap_update_bits(regs, CCSR_SSI_SRCCR, CCSR_SSI_SxCCR_WL_MASK,
898 static int fsl_ssi_hw_free(struct snd_pcm_substream *substream,
899 struct snd_soc_dai *cpu_dai)
901 struct snd_soc_pcm_runtime *rtd = substream->private_data;
902 struct fsl_ssi_private *ssi_private =
903 snd_soc_dai_get_drvdata(rtd->cpu_dai);
905 if (fsl_ssi_is_i2s_master(ssi_private) &&
906 ssi_private->baudclk_streams & BIT(substream->stream)) {
907 clk_disable_unprepare(ssi_private->baudclk);
908 ssi_private->baudclk_streams &= ~BIT(substream->stream);
914 static int _fsl_ssi_set_dai_fmt(struct device *dev,
915 struct fsl_ssi_private *ssi_private,
918 struct regmap *regs = ssi_private->regs;
919 u32 strcr = 0, stcr, srcr, scr, mask;
922 ssi_private->dai_fmt = fmt;
924 if (fsl_ssi_is_i2s_master(ssi_private) && IS_ERR(ssi_private->baudclk)) {
925 dev_err(dev, "baudclk is missing which is necessary for master mode\n");
929 fsl_ssi_setup_reg_vals(ssi_private);
931 regmap_read(regs, CCSR_SSI_SCR, &scr);
932 scr &= ~(CCSR_SSI_SCR_SYN | CCSR_SSI_SCR_I2S_MODE_MASK);
933 scr |= CCSR_SSI_SCR_SYNC_TX_FS;
935 mask = CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TFDIR | CCSR_SSI_STCR_TXDIR |
936 CCSR_SSI_STCR_TSCKP | CCSR_SSI_STCR_TFSI | CCSR_SSI_STCR_TFSL |
938 regmap_read(regs, CCSR_SSI_STCR, &stcr);
939 regmap_read(regs, CCSR_SSI_SRCR, &srcr);
943 ssi_private->i2s_mode = CCSR_SSI_SCR_NET;
944 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
945 case SND_SOC_DAIFMT_I2S:
946 regmap_update_bits(regs, CCSR_SSI_STCCR,
947 CCSR_SSI_SxCCR_DC_MASK,
948 CCSR_SSI_SxCCR_DC(2));
949 regmap_update_bits(regs, CCSR_SSI_SRCCR,
950 CCSR_SSI_SxCCR_DC_MASK,
951 CCSR_SSI_SxCCR_DC(2));
952 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
953 case SND_SOC_DAIFMT_CBM_CFS:
954 case SND_SOC_DAIFMT_CBS_CFS:
955 ssi_private->i2s_mode |= CCSR_SSI_SCR_I2S_MODE_MASTER;
957 case SND_SOC_DAIFMT_CBM_CFM:
958 ssi_private->i2s_mode |= CCSR_SSI_SCR_I2S_MODE_SLAVE;
964 /* Data on rising edge of bclk, frame low, 1clk before data */
965 strcr |= CCSR_SSI_STCR_TFSI | CCSR_SSI_STCR_TSCKP |
966 CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TEFS;
968 case SND_SOC_DAIFMT_LEFT_J:
969 /* Data on rising edge of bclk, frame high */
970 strcr |= CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TSCKP;
972 case SND_SOC_DAIFMT_DSP_A:
973 /* Data on rising edge of bclk, frame high, 1clk before data */
974 strcr |= CCSR_SSI_STCR_TFSL | CCSR_SSI_STCR_TSCKP |
975 CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TEFS;
977 case SND_SOC_DAIFMT_DSP_B:
978 /* Data on rising edge of bclk, frame high */
979 strcr |= CCSR_SSI_STCR_TFSL | CCSR_SSI_STCR_TSCKP |
980 CCSR_SSI_STCR_TXBIT0;
982 case SND_SOC_DAIFMT_AC97:
983 ssi_private->i2s_mode |= CCSR_SSI_SCR_I2S_MODE_NORMAL;
988 scr |= ssi_private->i2s_mode;
990 /* DAI clock inversion */
991 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
992 case SND_SOC_DAIFMT_NB_NF:
993 /* Nothing to do for both normal cases */
995 case SND_SOC_DAIFMT_IB_NF:
996 /* Invert bit clock */
997 strcr ^= CCSR_SSI_STCR_TSCKP;
999 case SND_SOC_DAIFMT_NB_IF:
1000 /* Invert frame clock */
1001 strcr ^= CCSR_SSI_STCR_TFSI;
1003 case SND_SOC_DAIFMT_IB_IF:
1004 /* Invert both clocks */
1005 strcr ^= CCSR_SSI_STCR_TSCKP;
1006 strcr ^= CCSR_SSI_STCR_TFSI;
1012 /* DAI clock master masks */
1013 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1014 case SND_SOC_DAIFMT_CBS_CFS:
1015 strcr |= CCSR_SSI_STCR_TFDIR | CCSR_SSI_STCR_TXDIR;
1016 scr |= CCSR_SSI_SCR_SYS_CLK_EN;
1018 case SND_SOC_DAIFMT_CBM_CFM:
1019 scr &= ~CCSR_SSI_SCR_SYS_CLK_EN;
1021 case SND_SOC_DAIFMT_CBM_CFS:
1022 strcr &= ~CCSR_SSI_STCR_TXDIR;
1023 strcr |= CCSR_SSI_STCR_TFDIR;
1024 scr &= ~CCSR_SSI_SCR_SYS_CLK_EN;
1027 if (!fsl_ssi_is_ac97(ssi_private))
1034 if (ssi_private->cpu_dai_drv.symmetric_rates
1035 || fsl_ssi_is_ac97(ssi_private)) {
1036 /* Need to clear RXDIR when using SYNC or AC97 mode */
1037 srcr &= ~CCSR_SSI_SRCR_RXDIR;
1038 scr |= CCSR_SSI_SCR_SYN;
1041 regmap_write(regs, CCSR_SSI_STCR, stcr);
1042 regmap_write(regs, CCSR_SSI_SRCR, srcr);
1043 regmap_write(regs, CCSR_SSI_SCR, scr);
1046 * Set the watermark for transmit FIFI 0 and receive FIFO 0. We don't
1047 * use FIFO 1. We program the transmit water to signal a DMA transfer
1048 * if there are only two (or fewer) elements left in the FIFO. Two
1049 * elements equals one frame (left channel, right channel). This value,
1050 * however, depends on the depth of the transmit buffer.
1052 * We set the watermark on the same level as the DMA burstsize. For
1053 * fiq it is probably better to use the biggest possible watermark
1056 if (ssi_private->use_dma)
1057 wm = ssi_private->fifo_depth - 2;
1059 wm = ssi_private->fifo_depth;
1061 regmap_write(regs, CCSR_SSI_SFCSR,
1062 CCSR_SSI_SFCSR_TFWM0(wm) | CCSR_SSI_SFCSR_RFWM0(wm) |
1063 CCSR_SSI_SFCSR_TFWM1(wm) | CCSR_SSI_SFCSR_RFWM1(wm));
1065 if (ssi_private->use_dual_fifo) {
1066 regmap_update_bits(regs, CCSR_SSI_SRCR, CCSR_SSI_SRCR_RFEN1,
1067 CCSR_SSI_SRCR_RFEN1);
1068 regmap_update_bits(regs, CCSR_SSI_STCR, CCSR_SSI_STCR_TFEN1,
1069 CCSR_SSI_STCR_TFEN1);
1070 regmap_update_bits(regs, CCSR_SSI_SCR, CCSR_SSI_SCR_TCH_EN,
1071 CCSR_SSI_SCR_TCH_EN);
1074 if ((fmt & SND_SOC_DAIFMT_FORMAT_MASK) == SND_SOC_DAIFMT_AC97)
1075 fsl_ssi_setup_ac97(ssi_private);
1082 * fsl_ssi_set_dai_fmt - configure Digital Audio Interface Format.
1084 static int fsl_ssi_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
1086 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
1088 return _fsl_ssi_set_dai_fmt(cpu_dai->dev, ssi_private, fmt);
1092 * fsl_ssi_set_dai_tdm_slot - set TDM slot number
1094 * Note: This function can be only called when using SSI as DAI master
1096 static int fsl_ssi_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai, u32 tx_mask,
1097 u32 rx_mask, int slots, int slot_width)
1099 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
1100 struct regmap *regs = ssi_private->regs;
1103 /* The slot number should be >= 2 if using Network mode or I2S mode */
1104 regmap_read(regs, CCSR_SSI_SCR, &val);
1105 val &= CCSR_SSI_SCR_I2S_MODE_MASK | CCSR_SSI_SCR_NET;
1106 if (val && slots < 2) {
1107 dev_err(cpu_dai->dev, "slot number should be >= 2 in I2S or NET\n");
1111 regmap_update_bits(regs, CCSR_SSI_STCCR, CCSR_SSI_SxCCR_DC_MASK,
1112 CCSR_SSI_SxCCR_DC(slots));
1113 regmap_update_bits(regs, CCSR_SSI_SRCCR, CCSR_SSI_SxCCR_DC_MASK,
1114 CCSR_SSI_SxCCR_DC(slots));
1116 /* The register SxMSKs needs SSI to provide essential clock due to
1117 * hardware design. So we here temporarily enable SSI to set them.
1119 regmap_read(regs, CCSR_SSI_SCR, &val);
1120 val &= CCSR_SSI_SCR_SSIEN;
1121 regmap_update_bits(regs, CCSR_SSI_SCR, CCSR_SSI_SCR_SSIEN,
1122 CCSR_SSI_SCR_SSIEN);
1124 regmap_write(regs, CCSR_SSI_STMSK, ~tx_mask);
1125 regmap_write(regs, CCSR_SSI_SRMSK, ~rx_mask);
1127 regmap_update_bits(regs, CCSR_SSI_SCR, CCSR_SSI_SCR_SSIEN, val);
1133 * fsl_ssi_trigger: start and stop the DMA transfer.
1135 * This function is called by ALSA to start, stop, pause, and resume the DMA
1138 * The DMA channel is in external master start and pause mode, which
1139 * means the SSI completely controls the flow of data.
1141 static int fsl_ssi_trigger(struct snd_pcm_substream *substream, int cmd,
1142 struct snd_soc_dai *dai)
1144 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1145 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(rtd->cpu_dai);
1146 struct regmap *regs = ssi_private->regs;
1149 case SNDRV_PCM_TRIGGER_START:
1150 case SNDRV_PCM_TRIGGER_RESUME:
1151 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1152 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1153 fsl_ssi_tx_config(ssi_private, true);
1155 fsl_ssi_rx_config(ssi_private, true);
1158 case SNDRV_PCM_TRIGGER_STOP:
1159 case SNDRV_PCM_TRIGGER_SUSPEND:
1160 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1161 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1162 fsl_ssi_tx_config(ssi_private, false);
1164 fsl_ssi_rx_config(ssi_private, false);
1171 if (fsl_ssi_is_ac97(ssi_private)) {
1172 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1173 regmap_write(regs, CCSR_SSI_SOR, CCSR_SSI_SOR_TX_CLR);
1175 regmap_write(regs, CCSR_SSI_SOR, CCSR_SSI_SOR_RX_CLR);
1181 static int fsl_ssi_dai_probe(struct snd_soc_dai *dai)
1183 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(dai);
1185 if (ssi_private->soc->imx && ssi_private->use_dma) {
1186 dai->playback_dma_data = &ssi_private->dma_params_tx;
1187 dai->capture_dma_data = &ssi_private->dma_params_rx;
1193 static const struct snd_soc_dai_ops fsl_ssi_dai_ops = {
1194 .startup = fsl_ssi_startup,
1195 .shutdown = fsl_ssi_shutdown,
1196 .hw_params = fsl_ssi_hw_params,
1197 .hw_free = fsl_ssi_hw_free,
1198 .set_fmt = fsl_ssi_set_dai_fmt,
1199 .set_sysclk = fsl_ssi_set_dai_sysclk,
1200 .set_tdm_slot = fsl_ssi_set_dai_tdm_slot,
1201 .trigger = fsl_ssi_trigger,
1204 /* Template for the CPU dai driver structure */
1205 static struct snd_soc_dai_driver fsl_ssi_dai_template = {
1206 .probe = fsl_ssi_dai_probe,
1208 .stream_name = "CPU-Playback",
1211 .rates = SNDRV_PCM_RATE_CONTINUOUS,
1212 .formats = FSLSSI_I2S_FORMATS,
1215 .stream_name = "CPU-Capture",
1218 .rates = SNDRV_PCM_RATE_CONTINUOUS,
1219 .formats = FSLSSI_I2S_FORMATS,
1221 .ops = &fsl_ssi_dai_ops,
1224 static const struct snd_soc_component_driver fsl_ssi_component = {
1228 static struct snd_soc_dai_driver fsl_ssi_ac97_dai = {
1229 .bus_control = true,
1230 .probe = fsl_ssi_dai_probe,
1232 .stream_name = "AC97 Playback",
1235 .rates = SNDRV_PCM_RATE_8000_48000,
1236 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1239 .stream_name = "AC97 Capture",
1242 .rates = SNDRV_PCM_RATE_48000,
1243 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1245 .ops = &fsl_ssi_dai_ops,
1249 static struct fsl_ssi_private *fsl_ac97_data;
1251 static void fsl_ssi_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
1254 struct regmap *regs = fsl_ac97_data->regs;
1262 ret = clk_prepare_enable(fsl_ac97_data->clk);
1264 pr_err("ac97 write clk_prepare_enable failed: %d\n",
1270 regmap_write(regs, CCSR_SSI_SACADD, lreg);
1273 regmap_write(regs, CCSR_SSI_SACDAT, lval);
1275 regmap_update_bits(regs, CCSR_SSI_SACNT, CCSR_SSI_SACNT_RDWR_MASK,
1279 clk_disable_unprepare(fsl_ac97_data->clk);
1282 static unsigned short fsl_ssi_ac97_read(struct snd_ac97 *ac97,
1285 struct regmap *regs = fsl_ac97_data->regs;
1287 unsigned short val = -1;
1292 ret = clk_prepare_enable(fsl_ac97_data->clk);
1294 pr_err("ac97 read clk_prepare_enable failed: %d\n",
1299 lreg = (reg & 0x7f) << 12;
1300 regmap_write(regs, CCSR_SSI_SACADD, lreg);
1301 regmap_update_bits(regs, CCSR_SSI_SACNT, CCSR_SSI_SACNT_RDWR_MASK,
1306 regmap_read(regs, CCSR_SSI_SACDAT, ®_val);
1307 val = (reg_val >> 4) & 0xffff;
1309 clk_disable_unprepare(fsl_ac97_data->clk);
1314 static struct snd_ac97_bus_ops fsl_ssi_ac97_ops = {
1315 .read = fsl_ssi_ac97_read,
1316 .write = fsl_ssi_ac97_write,
1320 * Make every character in a string lower-case
1322 static void make_lowercase(char *s)
1330 static int fsl_ssi_imx_probe(struct platform_device *pdev,
1331 struct fsl_ssi_private *ssi_private, void __iomem *iomem)
1333 struct device_node *np = pdev->dev.of_node;
1337 if (ssi_private->has_ipg_clk_name)
1338 ssi_private->clk = devm_clk_get(&pdev->dev, "ipg");
1340 ssi_private->clk = devm_clk_get(&pdev->dev, NULL);
1341 if (IS_ERR(ssi_private->clk)) {
1342 ret = PTR_ERR(ssi_private->clk);
1343 dev_err(&pdev->dev, "could not get clock: %d\n", ret);
1347 if (!ssi_private->has_ipg_clk_name) {
1348 ret = clk_prepare_enable(ssi_private->clk);
1350 dev_err(&pdev->dev, "clk_prepare_enable failed: %d\n", ret);
1355 /* For those SLAVE implementations, we ignore non-baudclk cases
1356 * and, instead, abandon MASTER mode that needs baud clock.
1358 ssi_private->baudclk = devm_clk_get(&pdev->dev, "baud");
1359 if (IS_ERR(ssi_private->baudclk))
1360 dev_dbg(&pdev->dev, "could not get baud clock: %ld\n",
1361 PTR_ERR(ssi_private->baudclk));
1364 * We have burstsize be "fifo_depth - 2" to match the SSI
1365 * watermark setting in fsl_ssi_startup().
1367 ssi_private->dma_params_tx.maxburst = ssi_private->fifo_depth - 2;
1368 ssi_private->dma_params_rx.maxburst = ssi_private->fifo_depth - 2;
1369 ssi_private->dma_params_tx.addr = ssi_private->ssi_phys + CCSR_SSI_STX0;
1370 ssi_private->dma_params_rx.addr = ssi_private->ssi_phys + CCSR_SSI_SRX0;
1372 ret = of_property_read_u32_array(np, "dmas", dmas, 4);
1373 if (ssi_private->use_dma && !ret && dmas[2] == IMX_DMATYPE_SSI_DUAL) {
1374 ssi_private->use_dual_fifo = true;
1375 /* When using dual fifo mode, we need to keep watermark
1376 * as even numbers due to dma script limitation.
1378 ssi_private->dma_params_tx.maxburst &= ~0x1;
1379 ssi_private->dma_params_rx.maxburst &= ~0x1;
1382 if (!ssi_private->use_dma) {
1385 * Some boards use an incompatible codec. To get it
1386 * working, we are using imx-fiq-pcm-audio, that
1387 * can handle those codecs. DMA is not possible in this
1391 ssi_private->fiq_params.irq = ssi_private->irq;
1392 ssi_private->fiq_params.base = iomem;
1393 ssi_private->fiq_params.dma_params_rx =
1394 &ssi_private->dma_params_rx;
1395 ssi_private->fiq_params.dma_params_tx =
1396 &ssi_private->dma_params_tx;
1398 ret = imx_pcm_fiq_init(pdev, &ssi_private->fiq_params);
1402 ret = imx_pcm_dma_init(pdev, IMX_SSI_DMABUF_SIZE);
1411 if (!ssi_private->has_ipg_clk_name)
1412 clk_disable_unprepare(ssi_private->clk);
1416 static void fsl_ssi_imx_clean(struct platform_device *pdev,
1417 struct fsl_ssi_private *ssi_private)
1419 if (!ssi_private->use_dma)
1420 imx_pcm_fiq_exit(pdev);
1421 if (!ssi_private->has_ipg_clk_name)
1422 clk_disable_unprepare(ssi_private->clk);
1425 static int fsl_ssi_probe(struct platform_device *pdev)
1427 struct fsl_ssi_private *ssi_private;
1429 struct device_node *np = pdev->dev.of_node;
1430 const struct of_device_id *of_id;
1431 const char *p, *sprop;
1432 const uint32_t *iprop;
1433 struct resource *res;
1434 void __iomem *iomem;
1436 struct regmap_config regconfig = fsl_ssi_regconfig;
1438 of_id = of_match_device(fsl_ssi_ids, &pdev->dev);
1439 if (!of_id || !of_id->data)
1442 ssi_private = devm_kzalloc(&pdev->dev, sizeof(*ssi_private),
1445 dev_err(&pdev->dev, "could not allocate DAI object\n");
1449 ssi_private->soc = of_id->data;
1450 ssi_private->dev = &pdev->dev;
1452 sprop = of_get_property(np, "fsl,mode", NULL);
1454 if (!strcmp(sprop, "ac97-slave"))
1455 ssi_private->dai_fmt = SND_SOC_DAIFMT_AC97;
1458 ssi_private->use_dma = !of_property_read_bool(np,
1459 "fsl,fiq-stream-filter");
1461 if (fsl_ssi_is_ac97(ssi_private)) {
1462 memcpy(&ssi_private->cpu_dai_drv, &fsl_ssi_ac97_dai,
1463 sizeof(fsl_ssi_ac97_dai));
1465 fsl_ac97_data = ssi_private;
1467 ret = snd_soc_set_ac97_ops_of_reset(&fsl_ssi_ac97_ops, pdev);
1469 dev_err(&pdev->dev, "could not set AC'97 ops\n");
1473 /* Initialize this copy of the CPU DAI driver structure */
1474 memcpy(&ssi_private->cpu_dai_drv, &fsl_ssi_dai_template,
1475 sizeof(fsl_ssi_dai_template));
1477 ssi_private->cpu_dai_drv.name = dev_name(&pdev->dev);
1479 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1480 iomem = devm_ioremap_resource(&pdev->dev, res);
1482 return PTR_ERR(iomem);
1483 ssi_private->ssi_phys = res->start;
1485 if (ssi_private->soc->imx21regs) {
1487 * According to datasheet imx21-class SSI
1488 * don't have SACC{ST,EN,DIS} regs.
1490 regconfig.max_register = CCSR_SSI_SRMSK;
1491 regconfig.num_reg_defaults_raw =
1492 CCSR_SSI_SRMSK / sizeof(uint32_t) + 1;
1495 ret = of_property_match_string(np, "clock-names", "ipg");
1497 ssi_private->has_ipg_clk_name = false;
1498 ssi_private->regs = devm_regmap_init_mmio(&pdev->dev, iomem,
1501 ssi_private->has_ipg_clk_name = true;
1502 ssi_private->regs = devm_regmap_init_mmio_clk(&pdev->dev,
1503 "ipg", iomem, ®config);
1505 if (IS_ERR(ssi_private->regs)) {
1506 dev_err(&pdev->dev, "Failed to init register map\n");
1507 return PTR_ERR(ssi_private->regs);
1510 ssi_private->irq = platform_get_irq(pdev, 0);
1511 if (ssi_private->irq < 0) {
1512 dev_err(&pdev->dev, "no irq for node %s\n", pdev->name);
1513 return ssi_private->irq;
1516 /* Are the RX and the TX clocks locked? */
1517 if (!of_find_property(np, "fsl,ssi-asynchronous", NULL)) {
1518 if (!fsl_ssi_is_ac97(ssi_private))
1519 ssi_private->cpu_dai_drv.symmetric_rates = 1;
1521 ssi_private->cpu_dai_drv.symmetric_channels = 1;
1522 ssi_private->cpu_dai_drv.symmetric_samplebits = 1;
1525 /* Determine the FIFO depth. */
1526 iprop = of_get_property(np, "fsl,fifo-depth", NULL);
1528 ssi_private->fifo_depth = be32_to_cpup(iprop);
1530 /* Older 8610 DTs didn't have the fifo-depth property */
1531 ssi_private->fifo_depth = 8;
1533 dev_set_drvdata(&pdev->dev, ssi_private);
1535 if (ssi_private->soc->imx) {
1536 ret = fsl_ssi_imx_probe(pdev, ssi_private, iomem);
1541 ret = devm_snd_soc_register_component(&pdev->dev, &fsl_ssi_component,
1542 &ssi_private->cpu_dai_drv, 1);
1544 dev_err(&pdev->dev, "failed to register DAI: %d\n", ret);
1545 goto error_asoc_register;
1548 if (ssi_private->use_dma) {
1549 ret = devm_request_irq(&pdev->dev, ssi_private->irq,
1550 fsl_ssi_isr, 0, dev_name(&pdev->dev),
1553 dev_err(&pdev->dev, "could not claim irq %u\n",
1555 goto error_asoc_register;
1559 ret = fsl_ssi_debugfs_create(&ssi_private->dbg_stats, &pdev->dev);
1561 goto error_asoc_register;
1564 * If codec-handle property is missing from SSI node, we assume
1565 * that the machine driver uses new binding which does not require
1566 * SSI driver to trigger machine driver's probe.
1568 if (!of_get_property(np, "codec-handle", NULL))
1571 /* Trigger the machine driver's probe function. The platform driver
1572 * name of the machine driver is taken from /compatible property of the
1573 * device tree. We also pass the address of the CPU DAI driver
1576 sprop = of_get_property(of_find_node_by_path("/"), "compatible", NULL);
1577 /* Sometimes the compatible name has a "fsl," prefix, so we strip it. */
1578 p = strrchr(sprop, ',');
1581 snprintf(name, sizeof(name), "snd-soc-%s", sprop);
1582 make_lowercase(name);
1585 platform_device_register_data(&pdev->dev, name, 0, NULL, 0);
1586 if (IS_ERR(ssi_private->pdev)) {
1587 ret = PTR_ERR(ssi_private->pdev);
1588 dev_err(&pdev->dev, "failed to register platform: %d\n", ret);
1589 goto error_sound_card;
1593 if (ssi_private->dai_fmt)
1594 _fsl_ssi_set_dai_fmt(&pdev->dev, ssi_private,
1595 ssi_private->dai_fmt);
1597 if (fsl_ssi_is_ac97(ssi_private)) {
1600 ret = of_property_read_u32(np, "cell-index", &ssi_idx);
1602 dev_err(&pdev->dev, "cannot get SSI index property\n");
1603 goto error_sound_card;
1607 platform_device_register_data(NULL,
1608 "ac97-codec", ssi_idx, NULL, 0);
1609 if (IS_ERR(ssi_private->pdev)) {
1610 ret = PTR_ERR(ssi_private->pdev);
1612 "failed to register AC97 codec platform: %d\n",
1614 goto error_sound_card;
1621 fsl_ssi_debugfs_remove(&ssi_private->dbg_stats);
1623 error_asoc_register:
1624 if (ssi_private->soc->imx)
1625 fsl_ssi_imx_clean(pdev, ssi_private);
1630 static int fsl_ssi_remove(struct platform_device *pdev)
1632 struct fsl_ssi_private *ssi_private = dev_get_drvdata(&pdev->dev);
1634 fsl_ssi_debugfs_remove(&ssi_private->dbg_stats);
1636 if (ssi_private->pdev)
1637 platform_device_unregister(ssi_private->pdev);
1639 if (ssi_private->soc->imx)
1640 fsl_ssi_imx_clean(pdev, ssi_private);
1642 if (fsl_ssi_is_ac97(ssi_private))
1643 snd_soc_set_ac97_ops(NULL);
1648 #ifdef CONFIG_PM_SLEEP
1649 static int fsl_ssi_suspend(struct device *dev)
1651 struct fsl_ssi_private *ssi_private = dev_get_drvdata(dev);
1652 struct regmap *regs = ssi_private->regs;
1654 regmap_read(regs, CCSR_SSI_SFCSR,
1655 &ssi_private->regcache_sfcsr);
1656 regmap_read(regs, CCSR_SSI_SACNT,
1657 &ssi_private->regcache_sacnt);
1659 regcache_cache_only(regs, true);
1660 regcache_mark_dirty(regs);
1665 static int fsl_ssi_resume(struct device *dev)
1667 struct fsl_ssi_private *ssi_private = dev_get_drvdata(dev);
1668 struct regmap *regs = ssi_private->regs;
1670 regcache_cache_only(regs, false);
1672 regmap_update_bits(regs, CCSR_SSI_SFCSR,
1673 CCSR_SSI_SFCSR_RFWM1_MASK | CCSR_SSI_SFCSR_TFWM1_MASK |
1674 CCSR_SSI_SFCSR_RFWM0_MASK | CCSR_SSI_SFCSR_TFWM0_MASK,
1675 ssi_private->regcache_sfcsr);
1676 regmap_write(regs, CCSR_SSI_SACNT,
1677 ssi_private->regcache_sacnt);
1679 return regcache_sync(regs);
1681 #endif /* CONFIG_PM_SLEEP */
1683 static const struct dev_pm_ops fsl_ssi_pm = {
1684 SET_SYSTEM_SLEEP_PM_OPS(fsl_ssi_suspend, fsl_ssi_resume)
1687 static struct platform_driver fsl_ssi_driver = {
1689 .name = "fsl-ssi-dai",
1690 .of_match_table = fsl_ssi_ids,
1693 .probe = fsl_ssi_probe,
1694 .remove = fsl_ssi_remove,
1697 module_platform_driver(fsl_ssi_driver);
1699 MODULE_ALIAS("platform:fsl-ssi-dai");
1700 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
1701 MODULE_DESCRIPTION("Freescale Synchronous Serial Interface (SSI) ASoC Driver");
1702 MODULE_LICENSE("GPL v2");