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/mutex.h>
42 #include <linux/slab.h>
43 #include <linux/spinlock.h>
45 #include <linux/of_address.h>
46 #include <linux/of_irq.h>
47 #include <linux/of_platform.h>
49 #include <sound/core.h>
50 #include <sound/pcm.h>
51 #include <sound/pcm_params.h>
52 #include <sound/initval.h>
53 #include <sound/soc.h>
54 #include <sound/dmaengine_pcm.h>
59 /* Define RX and TX to index ssi->regvals array; Can be 0 or 1 only */
64 * FSLSSI_I2S_FORMATS: audio formats supported by the SSI
66 * The SSI has a limitation in that the samples must be in the same byte
67 * order as the host CPU. This is because when multiple bytes are written
68 * to the STX register, the bytes and bits must be written in the same
69 * order. The STX is a shift register, so all the bits need to be aligned
70 * (bit-endianness must match byte-endianness). Processors typically write
71 * the bits within a byte in the same order that the bytes of a word are
72 * written in. So if the host CPU is big-endian, then only big-endian
73 * samples will be written to STX properly.
76 #define FSLSSI_I2S_FORMATS \
77 (SNDRV_PCM_FMTBIT_S8 | \
78 SNDRV_PCM_FMTBIT_S16_BE | \
79 SNDRV_PCM_FMTBIT_S18_3BE | \
80 SNDRV_PCM_FMTBIT_S20_3BE | \
81 SNDRV_PCM_FMTBIT_S24_3BE | \
82 SNDRV_PCM_FMTBIT_S24_BE)
84 #define FSLSSI_I2S_FORMATS \
85 (SNDRV_PCM_FMTBIT_S8 | \
86 SNDRV_PCM_FMTBIT_S16_LE | \
87 SNDRV_PCM_FMTBIT_S18_3LE | \
88 SNDRV_PCM_FMTBIT_S20_3LE | \
89 SNDRV_PCM_FMTBIT_S24_3LE | \
90 SNDRV_PCM_FMTBIT_S24_LE)
94 * In AC97 mode, TXDIR bit is forced to 0 and TFDIR bit is forced to 1:
95 * - SSI inputs external bit clock and outputs frame sync clock -- CBM_CFS
96 * - Also have NB_NF to mark these two clocks will not be inverted
98 #define FSLSSI_AC97_DAIFMT \
99 (SND_SOC_DAIFMT_AC97 | \
100 SND_SOC_DAIFMT_CBM_CFS | \
101 SND_SOC_DAIFMT_NB_NF)
103 #define FSLSSI_SIER_DBG_RX_FLAGS \
104 (SSI_SIER_RFF0_EN | \
109 #define FSLSSI_SIER_DBG_TX_FLAGS \
110 (SSI_SIER_TFE0_EN | \
123 struct fsl_ssi_regvals {
130 static bool fsl_ssi_readable_reg(struct device *dev, unsigned int reg)
134 case REG_SSI_SACCDIS:
141 static bool fsl_ssi_volatile_reg(struct device *dev, unsigned int reg)
162 static bool fsl_ssi_precious_reg(struct device *dev, unsigned int reg)
177 static bool fsl_ssi_writeable_reg(struct device *dev, unsigned int reg)
189 static const struct regmap_config fsl_ssi_regconfig = {
190 .max_register = REG_SSI_SACCDIS,
194 .val_format_endian = REGMAP_ENDIAN_NATIVE,
195 .num_reg_defaults_raw = REG_SSI_SACCDIS / sizeof(uint32_t) + 1,
196 .readable_reg = fsl_ssi_readable_reg,
197 .volatile_reg = fsl_ssi_volatile_reg,
198 .precious_reg = fsl_ssi_precious_reg,
199 .writeable_reg = fsl_ssi_writeable_reg,
200 .cache_type = REGCACHE_FLAT,
203 struct fsl_ssi_soc_data {
205 bool imx21regs; /* imx21-class SSI - no SACC{ST,EN,DIS} regs */
211 * fsl_ssi: per-SSI private data
213 * @regs: Pointer to the regmap registers
214 * @irq: IRQ of this SSI
215 * @cpu_dai_drv: CPU DAI driver for this device
217 * @dai_fmt: DAI configuration this device is currently used with
218 * @streams: Mask of current active streams: BIT(TX) and BIT(RX)
219 * @i2s_net: I2S and Network mode configurations of SCR register
220 * (this is the initial settings based on the DAI format)
221 * @synchronous: Use synchronous mode - both of TX and RX use STCK and SFCK
222 * @use_dma: DMA is used or FIQ with stream filter
223 * @use_dual_fifo: DMA with support for dual FIFO mode
224 * @has_ipg_clk_name: If "ipg" is in the clock name list of device tree
225 * @fifo_depth: Depth of the SSI FIFOs
226 * @slot_width: Width of each DAI slot
227 * @slots: Number of slots
228 * @regvals: Specific RX/TX register settings
230 * @clk: Clock source to access register
231 * @baudclk: Clock source to generate bit and frame-sync clocks
232 * @baudclk_streams: Active streams that are using baudclk
234 * @regcache_sfcsr: Cache sfcsr register value during suspend and resume
235 * @regcache_sacnt: Cache sacnt register value during suspend and resume
237 * @dma_params_tx: DMA transmit parameters
238 * @dma_params_rx: DMA receive parameters
239 * @ssi_phys: physical address of the SSI registers
241 * @fiq_params: FIQ stream filtering parameters
243 * @card_pdev: Platform_device pointer to register a sound card for PowerPC or
244 * to register a CODEC platform device for AC97
245 * @card_name: Platform_device name to register a sound card for PowerPC or
246 * to register a CODEC platform device for AC97
247 * @card_idx: The index of SSI to register a sound card for PowerPC or
248 * to register a CODEC platform device for AC97
250 * @dbg_stats: Debugging statistics
252 * @soc: SoC specific data
253 * @dev: Pointer to &pdev->dev
255 * @fifo_watermark: The FIFO watermark setting. Notifies DMA when there are
256 * @fifo_watermark or fewer words in TX fifo or
257 * @fifo_watermark or more empty words in RX fifo.
258 * @dma_maxburst: Max number of words to transfer in one go. So far,
259 * this is always the same as fifo_watermark.
261 * @ac97_reg_lock: Mutex lock to serialize AC97 register access operations
266 struct snd_soc_dai_driver cpu_dai_drv;
268 unsigned int dai_fmt;
274 bool has_ipg_clk_name;
275 unsigned int fifo_depth;
276 unsigned int slot_width;
278 struct fsl_ssi_regvals regvals[2];
282 unsigned int baudclk_streams;
287 struct snd_dmaengine_dai_dma_data dma_params_tx;
288 struct snd_dmaengine_dai_dma_data dma_params_rx;
291 struct imx_pcm_fiq_params fiq_params;
293 struct platform_device *card_pdev;
297 struct fsl_ssi_dbg dbg_stats;
299 const struct fsl_ssi_soc_data *soc;
305 struct mutex ac97_reg_lock;
312 * 1) SSI in earlier SoCS has critical bits in control registers that
313 * cannot be changed after SSI starts running -- a software reset
314 * (set SSIEN to 0) is required to change their values. So adding
315 * an offline_config flag for these SoCs.
316 * 2) SDMA is available since imx35. However, imx35 does not support
317 * DMA bits changing when SSI is running, so set offline_config.
318 * 3) imx51 and later versions support register configurations when
319 * SSI is running (SSIEN); For these versions, DMA needs to be
320 * configured before SSI sends DMA request to avoid an undefined
321 * DMA request on the SDMA side.
324 static struct fsl_ssi_soc_data fsl_ssi_mpc8610 = {
326 .offline_config = true,
327 .sisr_write_mask = SSI_SISR_RFRC | SSI_SISR_TFRC |
328 SSI_SISR_ROE0 | SSI_SISR_ROE1 |
329 SSI_SISR_TUE0 | SSI_SISR_TUE1,
332 static struct fsl_ssi_soc_data fsl_ssi_imx21 = {
335 .offline_config = true,
336 .sisr_write_mask = 0,
339 static struct fsl_ssi_soc_data fsl_ssi_imx35 = {
341 .offline_config = true,
342 .sisr_write_mask = SSI_SISR_RFRC | SSI_SISR_TFRC |
343 SSI_SISR_ROE0 | SSI_SISR_ROE1 |
344 SSI_SISR_TUE0 | SSI_SISR_TUE1,
347 static struct fsl_ssi_soc_data fsl_ssi_imx51 = {
349 .offline_config = false,
350 .sisr_write_mask = SSI_SISR_ROE0 | SSI_SISR_ROE1 |
351 SSI_SISR_TUE0 | SSI_SISR_TUE1,
354 static const struct of_device_id fsl_ssi_ids[] = {
355 { .compatible = "fsl,mpc8610-ssi", .data = &fsl_ssi_mpc8610 },
356 { .compatible = "fsl,imx51-ssi", .data = &fsl_ssi_imx51 },
357 { .compatible = "fsl,imx35-ssi", .data = &fsl_ssi_imx35 },
358 { .compatible = "fsl,imx21-ssi", .data = &fsl_ssi_imx21 },
361 MODULE_DEVICE_TABLE(of, fsl_ssi_ids);
363 static bool fsl_ssi_is_ac97(struct fsl_ssi *ssi)
365 return (ssi->dai_fmt & SND_SOC_DAIFMT_FORMAT_MASK) ==
369 static bool fsl_ssi_is_i2s_master(struct fsl_ssi *ssi)
371 return (ssi->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) ==
372 SND_SOC_DAIFMT_CBS_CFS;
375 static bool fsl_ssi_is_i2s_cbm_cfs(struct fsl_ssi *ssi)
377 return (ssi->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) ==
378 SND_SOC_DAIFMT_CBM_CFS;
382 * Interrupt handler to gather states
384 static irqreturn_t fsl_ssi_isr(int irq, void *dev_id)
386 struct fsl_ssi *ssi = dev_id;
387 struct regmap *regs = ssi->regs;
391 regmap_read(regs, REG_SSI_SISR, &sisr);
393 sisr2 = sisr & ssi->soc->sisr_write_mask;
394 /* Clear the bits that we set */
396 regmap_write(regs, REG_SSI_SISR, sisr2);
398 fsl_ssi_dbg_isr(&ssi->dbg_stats, sisr);
404 * Set SCR, SIER, STCR and SRCR registers with cached values in regvals
407 * 1) For offline_config SoCs, enable all necessary bits of both streams
408 * when 1st stream starts, even if the opposite stream will not start
409 * 2) It also clears FIFO before setting regvals; SOR is safe to set online
411 static void fsl_ssi_config_enable(struct fsl_ssi *ssi, bool tx)
413 struct fsl_ssi_regvals *vals = ssi->regvals;
414 int dir = tx ? TX : RX;
415 u32 sier, srcr, stcr;
417 /* Clear dirty data in the FIFO; It also prevents channel slipping */
418 regmap_update_bits(ssi->regs, REG_SSI_SOR,
419 SSI_SOR_xX_CLR(tx), SSI_SOR_xX_CLR(tx));
422 * On offline_config SoCs, SxCR and SIER are already configured when
423 * the previous stream started. So skip all SxCR and SIER settings
424 * to prevent online reconfigurations, then jump to set SCR directly
426 if (ssi->soc->offline_config && ssi->streams)
429 if (ssi->soc->offline_config) {
431 * Online reconfiguration not supported, so enable all bits for
432 * both streams at once to avoid necessity of reconfigurations
434 srcr = vals[RX].srcr | vals[TX].srcr;
435 stcr = vals[RX].stcr | vals[TX].stcr;
436 sier = vals[RX].sier | vals[TX].sier;
438 /* Otherwise, only set bits for the current stream */
439 srcr = vals[dir].srcr;
440 stcr = vals[dir].stcr;
441 sier = vals[dir].sier;
444 /* Configure SRCR, STCR and SIER at once */
445 regmap_update_bits(ssi->regs, REG_SSI_SRCR, srcr, srcr);
446 regmap_update_bits(ssi->regs, REG_SSI_STCR, stcr, stcr);
447 regmap_update_bits(ssi->regs, REG_SSI_SIER, sier, sier);
451 * Start DMA before setting TE to avoid FIFO underrun
452 * which may cause a channel slip or a channel swap
454 * TODO: FIQ cases might also need this upon testing
456 if (ssi->use_dma && tx) {
460 /* Enable SSI first to send TX DMA request */
461 regmap_update_bits(ssi->regs, REG_SSI_SCR,
462 SSI_SCR_SSIEN, SSI_SCR_SSIEN);
464 /* Busy wait until TX FIFO not empty -- DMA working */
466 regmap_read(ssi->regs, REG_SSI_SFCSR, &sfcsr);
467 if (SSI_SFCSR_TFCNT0(sfcsr))
471 /* FIFO still empty -- something might be wrong */
472 if (!SSI_SFCSR_TFCNT0(sfcsr))
473 dev_warn(ssi->dev, "Timeout waiting TX FIFO filling\n");
475 /* Enable all remaining bits in SCR */
476 regmap_update_bits(ssi->regs, REG_SSI_SCR,
477 vals[dir].scr, vals[dir].scr);
479 /* Log the enabled stream to the mask */
480 ssi->streams |= BIT(dir);
484 * Exclude bits that are used by the opposite stream
486 * When both streams are active, disabling some bits for the current stream
487 * might break the other stream if these bits are used by it.
489 * @vals : regvals of the current stream
490 * @avals: regvals of the opposite stream
491 * @aactive: active state of the opposite stream
493 * 1) XOR vals and avals to get the differences if the other stream is active;
494 * Otherwise, return current vals if the other stream is not active
495 * 2) AND the result of 1) with the current vals
497 #define _ssi_xor_shared_bits(vals, avals, aactive) \
498 ((vals) ^ ((avals) * (aactive)))
500 #define ssi_excl_shared_bits(vals, avals, aactive) \
501 ((vals) & _ssi_xor_shared_bits(vals, avals, aactive))
504 * Unset SCR, SIER, STCR and SRCR registers with cached values in regvals
507 * 1) For offline_config SoCs, to avoid online reconfigurations, disable all
508 * bits of both streams at once when the last stream is abort to end
509 * 2) It also clears FIFO after unsetting regvals; SOR is safe to set online
511 static void fsl_ssi_config_disable(struct fsl_ssi *ssi, bool tx)
513 struct fsl_ssi_regvals *vals, *avals;
514 u32 sier, srcr, stcr, scr;
515 int adir = tx ? RX : TX;
516 int dir = tx ? TX : RX;
519 /* Check if the opposite stream is active */
520 aactive = ssi->streams & BIT(adir);
522 vals = &ssi->regvals[dir];
524 /* Get regvals of the opposite stream to keep opposite stream safe */
525 avals = &ssi->regvals[adir];
528 * To keep the other stream safe, exclude shared bits between
529 * both streams, and get safe bits to disable current stream
531 scr = ssi_excl_shared_bits(vals->scr, avals->scr, aactive);
533 /* Disable safe bits of SCR register for the current stream */
534 regmap_update_bits(ssi->regs, REG_SSI_SCR, scr, 0);
536 /* Log the disabled stream to the mask */
537 ssi->streams &= ~BIT(dir);
540 * On offline_config SoCs, if the other stream is active, skip
541 * SxCR and SIER settings to prevent online reconfigurations
543 if (ssi->soc->offline_config && aactive)
546 if (ssi->soc->offline_config) {
547 /* Now there is only current stream active, disable all bits */
548 srcr = vals->srcr | avals->srcr;
549 stcr = vals->stcr | avals->stcr;
550 sier = vals->sier | avals->sier;
553 * To keep the other stream safe, exclude shared bits between
554 * both streams, and get safe bits to disable current stream
556 sier = ssi_excl_shared_bits(vals->sier, avals->sier, aactive);
557 srcr = ssi_excl_shared_bits(vals->srcr, avals->srcr, aactive);
558 stcr = ssi_excl_shared_bits(vals->stcr, avals->stcr, aactive);
561 /* Clear configurations of SRCR, STCR and SIER at once */
562 regmap_update_bits(ssi->regs, REG_SSI_SRCR, srcr, 0);
563 regmap_update_bits(ssi->regs, REG_SSI_STCR, stcr, 0);
564 regmap_update_bits(ssi->regs, REG_SSI_SIER, sier, 0);
567 /* Clear remaining data in the FIFO */
568 regmap_update_bits(ssi->regs, REG_SSI_SOR,
569 SSI_SOR_xX_CLR(tx), SSI_SOR_xX_CLR(tx));
572 static void fsl_ssi_tx_ac97_saccst_setup(struct fsl_ssi *ssi)
574 struct regmap *regs = ssi->regs;
576 /* no SACC{ST,EN,DIS} regs on imx21-class SSI */
577 if (!ssi->soc->imx21regs) {
578 /* Disable all channel slots */
579 regmap_write(regs, REG_SSI_SACCDIS, 0xff);
580 /* Enable slots 3 & 4 -- PCM Playback Left & Right channels */
581 regmap_write(regs, REG_SSI_SACCEN, 0x300);
586 * Cache critical bits of SIER, SRCR, STCR and SCR to later set them safely
588 static void fsl_ssi_setup_regvals(struct fsl_ssi *ssi)
590 struct fsl_ssi_regvals *vals = ssi->regvals;
592 vals[RX].sier = SSI_SIER_RFF0_EN | FSLSSI_SIER_DBG_RX_FLAGS;
593 vals[RX].srcr = SSI_SRCR_RFEN0;
594 vals[RX].scr = SSI_SCR_SSIEN | SSI_SCR_RE;
595 vals[TX].sier = SSI_SIER_TFE0_EN | FSLSSI_SIER_DBG_TX_FLAGS;
596 vals[TX].stcr = SSI_STCR_TFEN0;
597 vals[TX].scr = SSI_SCR_SSIEN | SSI_SCR_TE;
599 /* AC97 has already enabled SSIEN, RE and TE, so ignore them */
600 if (fsl_ssi_is_ac97(ssi))
601 vals[RX].scr = vals[TX].scr = 0;
603 if (ssi->use_dual_fifo) {
604 vals[RX].srcr |= SSI_SRCR_RFEN1;
605 vals[TX].stcr |= SSI_STCR_TFEN1;
609 vals[RX].sier |= SSI_SIER_RDMAE;
610 vals[TX].sier |= SSI_SIER_TDMAE;
612 vals[RX].sier |= SSI_SIER_RIE;
613 vals[TX].sier |= SSI_SIER_TIE;
617 static void fsl_ssi_setup_ac97(struct fsl_ssi *ssi)
619 struct regmap *regs = ssi->regs;
621 /* Setup the clock control register */
622 regmap_write(regs, REG_SSI_STCCR, SSI_SxCCR_WL(17) | SSI_SxCCR_DC(13));
623 regmap_write(regs, REG_SSI_SRCCR, SSI_SxCCR_WL(17) | SSI_SxCCR_DC(13));
625 /* Enable AC97 mode and startup the SSI */
626 regmap_write(regs, REG_SSI_SACNT, SSI_SACNT_AC97EN | SSI_SACNT_FV);
628 /* AC97 has to communicate with codec before starting a stream */
629 regmap_update_bits(regs, REG_SSI_SCR,
630 SSI_SCR_SSIEN | SSI_SCR_TE | SSI_SCR_RE,
631 SSI_SCR_SSIEN | SSI_SCR_TE | SSI_SCR_RE);
633 regmap_write(regs, REG_SSI_SOR, SSI_SOR_WAIT(3));
636 static int fsl_ssi_startup(struct snd_pcm_substream *substream,
637 struct snd_soc_dai *dai)
639 struct snd_soc_pcm_runtime *rtd = substream->private_data;
640 struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(rtd->cpu_dai);
643 ret = clk_prepare_enable(ssi->clk);
648 * When using dual fifo mode, it is safer to ensure an even period
649 * size. If appearing to an odd number while DMA always starts its
650 * task from fifo0, fifo1 would be neglected at the end of each
651 * period. But SSI would still access fifo1 with an invalid data.
653 if (ssi->use_dual_fifo)
654 snd_pcm_hw_constraint_step(substream->runtime, 0,
655 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 2);
660 static void fsl_ssi_shutdown(struct snd_pcm_substream *substream,
661 struct snd_soc_dai *dai)
663 struct snd_soc_pcm_runtime *rtd = substream->private_data;
664 struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(rtd->cpu_dai);
666 clk_disable_unprepare(ssi->clk);
670 * Configure Digital Audio Interface bit clock
672 * Note: This function can be only called when using SSI as DAI master
674 * Quick instruction for parameters:
675 * freq: Output BCLK frequency = samplerate * slots * slot_width
676 * (In 2-channel I2S Master mode, slot_width is fixed 32)
678 static int fsl_ssi_set_bclk(struct snd_pcm_substream *substream,
679 struct snd_soc_dai *dai,
680 struct snd_pcm_hw_params *hw_params)
682 bool tx2, tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
683 struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(dai);
684 struct regmap *regs = ssi->regs;
685 u32 pm = 999, div2, psr, stccr, mask, afreq, factor, i;
686 unsigned long clkrate, baudrate, tmprate;
687 unsigned int slots = params_channels(hw_params);
688 unsigned int slot_width = 32;
689 u64 sub, savesub = 100000;
691 bool baudclk_is_used;
694 /* Override slots and slot_width if being specifically set... */
697 /* ...but keep 32 bits if slots is 2 -- I2S Master mode */
698 if (ssi->slot_width && slots != 2)
699 slot_width = ssi->slot_width;
701 /* Generate bit clock based on the slot number and slot width */
702 freq = slots * slot_width * params_rate(hw_params);
704 /* Don't apply it to any non-baudclk circumstance */
705 if (IS_ERR(ssi->baudclk))
709 * Hardware limitation: The bclk rate must be
710 * never greater than 1/5 IPG clock rate
712 if (freq * 5 > clk_get_rate(ssi->clk)) {
713 dev_err(dai->dev, "bitclk > ipgclk / 5\n");
717 baudclk_is_used = ssi->baudclk_streams & ~(BIT(substream->stream));
719 /* It should be already enough to divide clock by setting pm alone */
723 factor = (div2 + 1) * (7 * psr + 1) * 2;
725 for (i = 0; i < 255; i++) {
726 tmprate = freq * factor * (i + 1);
729 clkrate = clk_get_rate(ssi->baudclk);
731 clkrate = clk_round_rate(ssi->baudclk, tmprate);
734 afreq = clkrate / (i + 1);
738 else if (freq / afreq == 1)
740 else if (afreq / freq == 1)
745 /* Calculate the fraction */
749 if (sub < savesub && !(i == 0 && psr == 0 && div2 == 0)) {
760 /* No proper pm found if it is still remaining the initial value */
762 dev_err(dai->dev, "failed to handle the required sysclk\n");
766 stccr = SSI_SxCCR_PM(pm + 1) | (div2 ? SSI_SxCCR_DIV2 : 0) |
767 (psr ? SSI_SxCCR_PSR : 0);
768 mask = SSI_SxCCR_PM_MASK | SSI_SxCCR_DIV2 | SSI_SxCCR_PSR;
770 /* STCCR is used for RX in synchronous mode */
771 tx2 = tx || ssi->synchronous;
772 regmap_update_bits(regs, REG_SSI_SxCCR(tx2), mask, stccr);
774 if (!baudclk_is_used) {
775 ret = clk_set_rate(ssi->baudclk, baudrate);
777 dev_err(dai->dev, "failed to set baudclk rate\n");
786 * Configure SSI based on PCM hardware parameters
789 * 1) SxCCR.WL bits are critical bits that require SSI to be temporarily
790 * disabled on offline_config SoCs. Even for online configurable SoCs
791 * running in synchronous mode (both TX and RX use STCCR), it is not
792 * safe to re-configure them when both two streams start running.
793 * 2) SxCCR.PM, SxCCR.DIV2 and SxCCR.PSR bits will be configured in the
794 * fsl_ssi_set_bclk() if SSI is the DAI clock master.
796 static int fsl_ssi_hw_params(struct snd_pcm_substream *substream,
797 struct snd_pcm_hw_params *hw_params,
798 struct snd_soc_dai *dai)
800 bool tx2, tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
801 struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(dai);
802 struct regmap *regs = ssi->regs;
803 unsigned int channels = params_channels(hw_params);
804 unsigned int sample_size = params_width(hw_params);
805 u32 wl = SSI_SxCCR_WL(sample_size);
809 * SSI is properly configured if it is enabled and running in
810 * the synchronous mode; Note that AC97 mode is an exception
811 * that should set separate configurations for STCCR and SRCCR
812 * despite running in the synchronous mode.
814 if (ssi->streams && ssi->synchronous)
817 if (fsl_ssi_is_i2s_master(ssi)) {
818 ret = fsl_ssi_set_bclk(substream, dai, hw_params);
822 /* Do not enable the clock if it is already enabled */
823 if (!(ssi->baudclk_streams & BIT(substream->stream))) {
824 ret = clk_prepare_enable(ssi->baudclk);
828 ssi->baudclk_streams |= BIT(substream->stream);
832 if (!fsl_ssi_is_ac97(ssi)) {
834 * Keep the ssi->i2s_net intact while having a local variable
835 * to override settings for special use cases. Otherwise, the
836 * ssi->i2s_net will lose the settings for regular use cases.
838 u8 i2s_net = ssi->i2s_net;
840 /* Normal + Network mode to send 16-bit data in 32-bit frames */
841 if (fsl_ssi_is_i2s_cbm_cfs(ssi) && sample_size == 16)
842 i2s_net = SSI_SCR_I2S_MODE_NORMAL | SSI_SCR_NET;
844 /* Use Normal mode to send mono data at 1st slot of 2 slots */
846 i2s_net = SSI_SCR_I2S_MODE_NORMAL;
848 regmap_update_bits(regs, REG_SSI_SCR,
849 SSI_SCR_I2S_NET_MASK, i2s_net);
852 /* In synchronous mode, the SSI uses STCCR for capture */
853 tx2 = tx || ssi->synchronous;
854 regmap_update_bits(regs, REG_SSI_SxCCR(tx2), SSI_SxCCR_WL_MASK, wl);
859 static int fsl_ssi_hw_free(struct snd_pcm_substream *substream,
860 struct snd_soc_dai *dai)
862 struct snd_soc_pcm_runtime *rtd = substream->private_data;
863 struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(rtd->cpu_dai);
865 if (fsl_ssi_is_i2s_master(ssi) &&
866 ssi->baudclk_streams & BIT(substream->stream)) {
867 clk_disable_unprepare(ssi->baudclk);
868 ssi->baudclk_streams &= ~BIT(substream->stream);
874 static int _fsl_ssi_set_dai_fmt(struct fsl_ssi *ssi, unsigned int fmt)
876 u32 strcr = 0, scr = 0, stcr, srcr, mask;
880 /* Synchronize frame sync clock for TE to avoid data slipping */
881 scr |= SSI_SCR_SYNC_TX_FS;
883 /* Set to default shifting settings: LSB_ALIGNED */
884 strcr |= SSI_STCR_TXBIT0;
886 /* Use Network mode as default */
887 ssi->i2s_net = SSI_SCR_NET;
888 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
889 case SND_SOC_DAIFMT_I2S:
890 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
891 case SND_SOC_DAIFMT_CBS_CFS:
892 if (IS_ERR(ssi->baudclk)) {
894 "missing baudclk for master mode\n");
898 case SND_SOC_DAIFMT_CBM_CFS:
899 ssi->i2s_net |= SSI_SCR_I2S_MODE_MASTER;
901 case SND_SOC_DAIFMT_CBM_CFM:
902 ssi->i2s_net |= SSI_SCR_I2S_MODE_SLAVE;
908 regmap_update_bits(ssi->regs, REG_SSI_STCCR,
909 SSI_SxCCR_DC_MASK, SSI_SxCCR_DC(2));
910 regmap_update_bits(ssi->regs, REG_SSI_SRCCR,
911 SSI_SxCCR_DC_MASK, SSI_SxCCR_DC(2));
913 /* Data on rising edge of bclk, frame low, 1clk before data */
914 strcr |= SSI_STCR_TFSI | SSI_STCR_TSCKP | SSI_STCR_TEFS;
916 case SND_SOC_DAIFMT_LEFT_J:
917 /* Data on rising edge of bclk, frame high */
918 strcr |= SSI_STCR_TSCKP;
920 case SND_SOC_DAIFMT_DSP_A:
921 /* Data on rising edge of bclk, frame high, 1clk before data */
922 strcr |= SSI_STCR_TFSL | SSI_STCR_TSCKP | SSI_STCR_TEFS;
924 case SND_SOC_DAIFMT_DSP_B:
925 /* Data on rising edge of bclk, frame high */
926 strcr |= SSI_STCR_TFSL | SSI_STCR_TSCKP;
928 case SND_SOC_DAIFMT_AC97:
929 /* Data on falling edge of bclk, frame high, 1clk before data */
930 strcr |= SSI_STCR_TEFS;
938 /* DAI clock inversion */
939 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
940 case SND_SOC_DAIFMT_NB_NF:
941 /* Nothing to do for both normal cases */
943 case SND_SOC_DAIFMT_IB_NF:
944 /* Invert bit clock */
945 strcr ^= SSI_STCR_TSCKP;
947 case SND_SOC_DAIFMT_NB_IF:
948 /* Invert frame clock */
949 strcr ^= SSI_STCR_TFSI;
951 case SND_SOC_DAIFMT_IB_IF:
952 /* Invert both clocks */
953 strcr ^= SSI_STCR_TSCKP;
954 strcr ^= SSI_STCR_TFSI;
960 /* DAI clock master masks */
961 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
962 case SND_SOC_DAIFMT_CBS_CFS:
963 /* Output bit and frame sync clocks */
964 strcr |= SSI_STCR_TFDIR | SSI_STCR_TXDIR;
965 scr |= SSI_SCR_SYS_CLK_EN;
967 case SND_SOC_DAIFMT_CBM_CFM:
968 /* Input bit or frame sync clocks */
970 case SND_SOC_DAIFMT_CBM_CFS:
971 /* Input bit clock but output frame sync clock */
972 strcr |= SSI_STCR_TFDIR;
981 /* Set SYN mode and clear RXDIR bit when using SYN or AC97 mode */
982 if (ssi->synchronous || fsl_ssi_is_ac97(ssi)) {
983 srcr &= ~SSI_SRCR_RXDIR;
987 mask = SSI_STCR_TFDIR | SSI_STCR_TXDIR | SSI_STCR_TSCKP |
988 SSI_STCR_TFSL | SSI_STCR_TFSI | SSI_STCR_TEFS | SSI_STCR_TXBIT0;
990 regmap_update_bits(ssi->regs, REG_SSI_STCR, mask, stcr);
991 regmap_update_bits(ssi->regs, REG_SSI_SRCR, mask, srcr);
993 mask = SSI_SCR_SYNC_TX_FS | SSI_SCR_I2S_MODE_MASK |
994 SSI_SCR_SYS_CLK_EN | SSI_SCR_SYN;
995 regmap_update_bits(ssi->regs, REG_SSI_SCR, mask, scr);
1001 * Configure Digital Audio Interface (DAI) Format
1003 static int fsl_ssi_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1005 struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(dai);
1007 /* AC97 configured DAIFMT earlier in the probe() */
1008 if (fsl_ssi_is_ac97(ssi))
1011 return _fsl_ssi_set_dai_fmt(ssi, fmt);
1015 * Set TDM slot number and slot width
1017 static int fsl_ssi_set_dai_tdm_slot(struct snd_soc_dai *dai, u32 tx_mask,
1018 u32 rx_mask, int slots, int slot_width)
1020 struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(dai);
1021 struct regmap *regs = ssi->regs;
1024 /* The word length should be 8, 10, 12, 16, 18, 20, 22 or 24 */
1025 if (slot_width & 1 || slot_width < 8 || slot_width > 24) {
1026 dev_err(dai->dev, "invalid slot width: %d\n", slot_width);
1030 /* The slot number should be >= 2 if using Network mode or I2S mode */
1031 if (ssi->i2s_net && slots < 2) {
1032 dev_err(dai->dev, "slot number should be >= 2 in I2S or NET\n");
1036 regmap_update_bits(regs, REG_SSI_STCCR,
1037 SSI_SxCCR_DC_MASK, SSI_SxCCR_DC(slots));
1038 regmap_update_bits(regs, REG_SSI_SRCCR,
1039 SSI_SxCCR_DC_MASK, SSI_SxCCR_DC(slots));
1041 /* Save the SCR register value */
1042 regmap_read(regs, REG_SSI_SCR, &val);
1043 /* Temporarily enable SSI to allow SxMSKs to be configurable */
1044 regmap_update_bits(regs, REG_SSI_SCR, SSI_SCR_SSIEN, SSI_SCR_SSIEN);
1046 regmap_write(regs, REG_SSI_STMSK, ~tx_mask);
1047 regmap_write(regs, REG_SSI_SRMSK, ~rx_mask);
1049 /* Restore the value of SSIEN bit */
1050 regmap_update_bits(regs, REG_SSI_SCR, SSI_SCR_SSIEN, val);
1052 ssi->slot_width = slot_width;
1059 * Start or stop SSI and corresponding DMA transaction.
1061 * The DMA channel is in external master start and pause mode, which
1062 * means the SSI completely controls the flow of data.
1064 static int fsl_ssi_trigger(struct snd_pcm_substream *substream, int cmd,
1065 struct snd_soc_dai *dai)
1067 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1068 struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(rtd->cpu_dai);
1069 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1072 case SNDRV_PCM_TRIGGER_START:
1073 case SNDRV_PCM_TRIGGER_RESUME:
1074 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1076 * SACCST might be modified via AC Link by a CODEC if it sends
1077 * extra bits in their SLOTREQ requests, which'll accidentally
1078 * send valid data to slots other than normal playback slots.
1080 * To be safe, configure SACCST right before TX starts.
1082 if (tx && fsl_ssi_is_ac97(ssi))
1083 fsl_ssi_tx_ac97_saccst_setup(ssi);
1084 fsl_ssi_config_enable(ssi, tx);
1087 case SNDRV_PCM_TRIGGER_STOP:
1088 case SNDRV_PCM_TRIGGER_SUSPEND:
1089 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1090 fsl_ssi_config_disable(ssi, tx);
1100 static int fsl_ssi_dai_probe(struct snd_soc_dai *dai)
1102 struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(dai);
1104 if (ssi->soc->imx && ssi->use_dma)
1105 snd_soc_dai_init_dma_data(dai, &ssi->dma_params_tx,
1106 &ssi->dma_params_rx);
1111 static const struct snd_soc_dai_ops fsl_ssi_dai_ops = {
1112 .startup = fsl_ssi_startup,
1113 .shutdown = fsl_ssi_shutdown,
1114 .hw_params = fsl_ssi_hw_params,
1115 .hw_free = fsl_ssi_hw_free,
1116 .set_fmt = fsl_ssi_set_dai_fmt,
1117 .set_tdm_slot = fsl_ssi_set_dai_tdm_slot,
1118 .trigger = fsl_ssi_trigger,
1121 static struct snd_soc_dai_driver fsl_ssi_dai_template = {
1122 .probe = fsl_ssi_dai_probe,
1124 .stream_name = "CPU-Playback",
1127 .rates = SNDRV_PCM_RATE_CONTINUOUS,
1128 .formats = FSLSSI_I2S_FORMATS,
1131 .stream_name = "CPU-Capture",
1134 .rates = SNDRV_PCM_RATE_CONTINUOUS,
1135 .formats = FSLSSI_I2S_FORMATS,
1137 .ops = &fsl_ssi_dai_ops,
1140 static const struct snd_soc_component_driver fsl_ssi_component = {
1144 static struct snd_soc_dai_driver fsl_ssi_ac97_dai = {
1145 .bus_control = true,
1146 .symmetric_channels = 1,
1147 .probe = fsl_ssi_dai_probe,
1149 .stream_name = "AC97 Playback",
1152 .rates = SNDRV_PCM_RATE_8000_48000,
1153 .formats = SNDRV_PCM_FMTBIT_S16 | SNDRV_PCM_FMTBIT_S20,
1156 .stream_name = "AC97 Capture",
1159 .rates = SNDRV_PCM_RATE_48000,
1160 /* 16-bit capture is broken (errata ERR003778) */
1161 .formats = SNDRV_PCM_FMTBIT_S20,
1163 .ops = &fsl_ssi_dai_ops,
1166 static struct fsl_ssi *fsl_ac97_data;
1168 static void fsl_ssi_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
1171 struct regmap *regs = fsl_ac97_data->regs;
1179 mutex_lock(&fsl_ac97_data->ac97_reg_lock);
1181 ret = clk_prepare_enable(fsl_ac97_data->clk);
1183 pr_err("ac97 write clk_prepare_enable failed: %d\n",
1189 regmap_write(regs, REG_SSI_SACADD, lreg);
1192 regmap_write(regs, REG_SSI_SACDAT, lval);
1194 regmap_update_bits(regs, REG_SSI_SACNT,
1195 SSI_SACNT_RDWR_MASK, SSI_SACNT_WR);
1198 clk_disable_unprepare(fsl_ac97_data->clk);
1201 mutex_unlock(&fsl_ac97_data->ac97_reg_lock);
1204 static unsigned short fsl_ssi_ac97_read(struct snd_ac97 *ac97,
1207 struct regmap *regs = fsl_ac97_data->regs;
1208 unsigned short val = 0;
1213 mutex_lock(&fsl_ac97_data->ac97_reg_lock);
1215 ret = clk_prepare_enable(fsl_ac97_data->clk);
1217 pr_err("ac97 read clk_prepare_enable failed: %d\n", ret);
1221 lreg = (reg & 0x7f) << 12;
1222 regmap_write(regs, REG_SSI_SACADD, lreg);
1223 regmap_update_bits(regs, REG_SSI_SACNT,
1224 SSI_SACNT_RDWR_MASK, SSI_SACNT_RD);
1228 regmap_read(regs, REG_SSI_SACDAT, ®_val);
1229 val = (reg_val >> 4) & 0xffff;
1231 clk_disable_unprepare(fsl_ac97_data->clk);
1234 mutex_unlock(&fsl_ac97_data->ac97_reg_lock);
1238 static struct snd_ac97_bus_ops fsl_ssi_ac97_ops = {
1239 .read = fsl_ssi_ac97_read,
1240 .write = fsl_ssi_ac97_write,
1244 * Initialize SSI registers
1246 static int fsl_ssi_hw_init(struct fsl_ssi *ssi)
1248 u32 wm = ssi->fifo_watermark;
1250 /* Initialize regvals */
1251 fsl_ssi_setup_regvals(ssi);
1253 /* Set watermarks */
1254 regmap_write(ssi->regs, REG_SSI_SFCSR,
1255 SSI_SFCSR_TFWM0(wm) | SSI_SFCSR_RFWM0(wm) |
1256 SSI_SFCSR_TFWM1(wm) | SSI_SFCSR_RFWM1(wm));
1258 /* Enable Dual FIFO mode */
1259 if (ssi->use_dual_fifo)
1260 regmap_update_bits(ssi->regs, REG_SSI_SCR,
1261 SSI_SCR_TCH_EN, SSI_SCR_TCH_EN);
1263 /* AC97 should start earlier to communicate with CODECs */
1264 if (fsl_ssi_is_ac97(ssi)) {
1265 _fsl_ssi_set_dai_fmt(ssi, ssi->dai_fmt);
1266 fsl_ssi_setup_ac97(ssi);
1273 * Clear SSI registers
1275 static void fsl_ssi_hw_clean(struct fsl_ssi *ssi)
1277 /* Disable registers for AC97 */
1278 if (fsl_ssi_is_ac97(ssi)) {
1279 /* Disable TE and RE bits first */
1280 regmap_update_bits(ssi->regs, REG_SSI_SCR,
1281 SSI_SCR_TE | SSI_SCR_RE, 0);
1282 /* Disable AC97 mode */
1283 regmap_write(ssi->regs, REG_SSI_SACNT, 0);
1284 /* Unset WAIT bits */
1285 regmap_write(ssi->regs, REG_SSI_SOR, 0);
1286 /* Disable SSI -- software reset */
1287 regmap_update_bits(ssi->regs, REG_SSI_SCR, SSI_SCR_SSIEN, 0);
1291 * Make every character in a string lower-case
1293 static void make_lowercase(char *s)
1301 static int fsl_ssi_imx_probe(struct platform_device *pdev,
1302 struct fsl_ssi *ssi, void __iomem *iomem)
1304 struct device *dev = &pdev->dev;
1307 /* Backward compatible for a DT without ipg clock name assigned */
1308 if (ssi->has_ipg_clk_name)
1309 ssi->clk = devm_clk_get(dev, "ipg");
1311 ssi->clk = devm_clk_get(dev, NULL);
1312 if (IS_ERR(ssi->clk)) {
1313 ret = PTR_ERR(ssi->clk);
1314 dev_err(dev, "failed to get clock: %d\n", ret);
1318 /* Enable the clock since regmap will not handle it in this case */
1319 if (!ssi->has_ipg_clk_name) {
1320 ret = clk_prepare_enable(ssi->clk);
1322 dev_err(dev, "clk_prepare_enable failed: %d\n", ret);
1327 /* Do not error out for slave cases that live without a baud clock */
1328 ssi->baudclk = devm_clk_get(dev, "baud");
1329 if (IS_ERR(ssi->baudclk))
1330 dev_dbg(dev, "failed to get baud clock: %ld\n",
1331 PTR_ERR(ssi->baudclk));
1333 ssi->dma_params_tx.maxburst = ssi->dma_maxburst;
1334 ssi->dma_params_rx.maxburst = ssi->dma_maxburst;
1335 ssi->dma_params_tx.addr = ssi->ssi_phys + REG_SSI_STX0;
1336 ssi->dma_params_rx.addr = ssi->ssi_phys + REG_SSI_SRX0;
1338 /* Use even numbers to avoid channel swap due to SDMA script design */
1339 if (ssi->use_dual_fifo) {
1340 ssi->dma_params_tx.maxburst &= ~0x1;
1341 ssi->dma_params_rx.maxburst &= ~0x1;
1344 if (!ssi->use_dma) {
1346 * Some boards use an incompatible codec. Use imx-fiq-pcm-audio
1347 * to get it working, as DMA is not possible in this situation.
1349 ssi->fiq_params.irq = ssi->irq;
1350 ssi->fiq_params.base = iomem;
1351 ssi->fiq_params.dma_params_rx = &ssi->dma_params_rx;
1352 ssi->fiq_params.dma_params_tx = &ssi->dma_params_tx;
1354 ret = imx_pcm_fiq_init(pdev, &ssi->fiq_params);
1358 ret = imx_pcm_dma_init(pdev, IMX_SSI_DMABUF_SIZE);
1366 if (!ssi->has_ipg_clk_name)
1367 clk_disable_unprepare(ssi->clk);
1372 static void fsl_ssi_imx_clean(struct platform_device *pdev, struct fsl_ssi *ssi)
1375 imx_pcm_fiq_exit(pdev);
1376 if (!ssi->has_ipg_clk_name)
1377 clk_disable_unprepare(ssi->clk);
1380 static int fsl_ssi_probe_from_dt(struct fsl_ssi *ssi)
1382 struct device *dev = ssi->dev;
1383 struct device_node *np = dev->of_node;
1384 const struct of_device_id *of_id;
1385 const char *p, *sprop;
1386 const __be32 *iprop;
1390 of_id = of_match_device(fsl_ssi_ids, dev);
1391 if (!of_id || !of_id->data)
1394 ssi->soc = of_id->data;
1396 ret = of_property_match_string(np, "clock-names", "ipg");
1397 /* Get error code if not found */
1398 ssi->has_ipg_clk_name = ret >= 0;
1400 /* Check if being used in AC97 mode */
1401 sprop = of_get_property(np, "fsl,mode", NULL);
1402 if (sprop && !strcmp(sprop, "ac97-slave")) {
1403 ssi->dai_fmt = FSLSSI_AC97_DAIFMT;
1405 ret = of_property_read_u32(np, "cell-index", &ssi->card_idx);
1407 dev_err(dev, "failed to get SSI index property\n");
1410 strcpy(ssi->card_name, "ac97-codec");
1411 } else if (!of_find_property(np, "fsl,ssi-asynchronous", NULL)) {
1413 * In synchronous mode, STCK and STFS ports are used by RX
1414 * as well. So the software should limit the sample rates,
1415 * sample bits and channels to be symmetric.
1417 * This is exclusive with FSLSSI_AC97_FORMATS as AC97 runs
1418 * in the SSI synchronous mode however it does not have to
1419 * limit symmetric sample rates and sample bits.
1421 ssi->synchronous = true;
1424 /* Select DMA or FIQ */
1425 ssi->use_dma = !of_property_read_bool(np, "fsl,fiq-stream-filter");
1427 /* Fetch FIFO depth; Set to 8 for older DT without this property */
1428 iprop = of_get_property(np, "fsl,fifo-depth", NULL);
1430 ssi->fifo_depth = be32_to_cpup(iprop);
1432 ssi->fifo_depth = 8;
1434 /* Use dual FIFO mode depending on the support from SDMA script */
1435 ret = of_property_read_u32_array(np, "dmas", dmas, 4);
1436 if (ssi->use_dma && !ret && dmas[2] == IMX_DMATYPE_SSI_DUAL)
1437 ssi->use_dual_fifo = true;
1440 * Backward compatible for older bindings by manually triggering the
1441 * machine driver's probe(). Use /compatible property, including the
1442 * address of CPU DAI driver structure, as the name of machine driver
1444 * If card_name is set by AC97 earlier, bypass here since it uses a
1445 * different name to register the device.
1447 if (!ssi->card_name[0] && of_get_property(np, "codec-handle", NULL)) {
1448 sprop = of_get_property(of_find_node_by_path("/"),
1449 "compatible", NULL);
1450 /* Strip "fsl," in the compatible name if applicable */
1451 p = strrchr(sprop, ',');
1454 snprintf(ssi->card_name, sizeof(ssi->card_name),
1455 "snd-soc-%s", sprop);
1456 make_lowercase(ssi->card_name);
1463 static int fsl_ssi_probe(struct platform_device *pdev)
1465 struct regmap_config regconfig = fsl_ssi_regconfig;
1466 struct device *dev = &pdev->dev;
1467 struct fsl_ssi *ssi;
1468 struct resource *res;
1469 void __iomem *iomem;
1472 ssi = devm_kzalloc(dev, sizeof(*ssi), GFP_KERNEL);
1479 ret = fsl_ssi_probe_from_dt(ssi);
1483 if (fsl_ssi_is_ac97(ssi)) {
1484 memcpy(&ssi->cpu_dai_drv, &fsl_ssi_ac97_dai,
1485 sizeof(fsl_ssi_ac97_dai));
1486 fsl_ac97_data = ssi;
1488 memcpy(&ssi->cpu_dai_drv, &fsl_ssi_dai_template,
1489 sizeof(fsl_ssi_dai_template));
1491 ssi->cpu_dai_drv.name = dev_name(dev);
1493 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1494 iomem = devm_ioremap_resource(dev, res);
1496 return PTR_ERR(iomem);
1497 ssi->ssi_phys = res->start;
1499 if (ssi->soc->imx21regs) {
1500 /* No SACC{ST,EN,DIS} regs in imx21-class SSI */
1501 regconfig.max_register = REG_SSI_SRMSK;
1502 regconfig.num_reg_defaults_raw =
1503 REG_SSI_SRMSK / sizeof(uint32_t) + 1;
1506 if (ssi->has_ipg_clk_name)
1507 ssi->regs = devm_regmap_init_mmio_clk(dev, "ipg", iomem,
1510 ssi->regs = devm_regmap_init_mmio(dev, iomem, ®config);
1511 if (IS_ERR(ssi->regs)) {
1512 dev_err(dev, "failed to init register map\n");
1513 return PTR_ERR(ssi->regs);
1516 ssi->irq = platform_get_irq(pdev, 0);
1518 dev_err(dev, "no irq for node %s\n", pdev->name);
1522 /* Set software limitations for synchronous mode except AC97 */
1523 if (ssi->synchronous && !fsl_ssi_is_ac97(ssi)) {
1524 ssi->cpu_dai_drv.symmetric_rates = 1;
1525 ssi->cpu_dai_drv.symmetric_channels = 1;
1526 ssi->cpu_dai_drv.symmetric_samplebits = 1;
1530 * Configure TX and RX DMA watermarks -- when to send a DMA request
1532 * Values should be tested to avoid FIFO under/over run. Set maxburst
1533 * to fifo_watermark to maxiumize DMA transaction to reduce overhead.
1535 switch (ssi->fifo_depth) {
1538 * Set to 8 as a balanced configuration -- When TX FIFO has 8
1539 * empty slots, send a DMA request to fill these 8 slots. The
1540 * remaining 7 slots should be able to allow DMA to finish the
1541 * transaction before TX FIFO underruns; Same applies to RX.
1543 * Tested with cases running at 48kHz @ 16 bits x 16 channels
1545 ssi->fifo_watermark = 8;
1546 ssi->dma_maxburst = 8;
1550 /* Safely use old watermark configurations for older chips */
1551 ssi->fifo_watermark = ssi->fifo_depth - 2;
1552 ssi->dma_maxburst = ssi->fifo_depth - 2;
1556 dev_set_drvdata(dev, ssi);
1558 if (ssi->soc->imx) {
1559 ret = fsl_ssi_imx_probe(pdev, ssi, iomem);
1564 if (fsl_ssi_is_ac97(ssi)) {
1565 mutex_init(&ssi->ac97_reg_lock);
1566 ret = snd_soc_set_ac97_ops_of_reset(&fsl_ssi_ac97_ops, pdev);
1568 dev_err(dev, "failed to set AC'97 ops\n");
1569 goto error_ac97_ops;
1573 ret = devm_snd_soc_register_component(dev, &fsl_ssi_component,
1574 &ssi->cpu_dai_drv, 1);
1576 dev_err(dev, "failed to register DAI: %d\n", ret);
1577 goto error_asoc_register;
1581 ret = devm_request_irq(dev, ssi->irq, fsl_ssi_isr, 0,
1582 dev_name(dev), ssi);
1584 dev_err(dev, "failed to claim irq %u\n", ssi->irq);
1585 goto error_asoc_register;
1589 ret = fsl_ssi_debugfs_create(&ssi->dbg_stats, dev);
1591 goto error_asoc_register;
1593 /* Initially configures SSI registers */
1594 fsl_ssi_hw_init(ssi);
1596 /* Register a platform device for older bindings or AC97 */
1597 if (ssi->card_name[0]) {
1598 struct device *parent = dev;
1600 * Do not set SSI dev as the parent of AC97 CODEC device since
1601 * it does not have a DT node. Otherwise ASoC core will assume
1602 * CODEC has the same DT node as the SSI, so it may bypass the
1603 * dai_probe() of SSI and then cause NULL DMA data pointers.
1605 if (fsl_ssi_is_ac97(ssi))
1608 ssi->card_pdev = platform_device_register_data(parent,
1609 ssi->card_name, ssi->card_idx, NULL, 0);
1610 if (IS_ERR(ssi->card_pdev)) {
1611 ret = PTR_ERR(ssi->card_pdev);
1612 dev_err(dev, "failed to register %s: %d\n",
1613 ssi->card_name, ret);
1614 goto error_sound_card;
1621 fsl_ssi_debugfs_remove(&ssi->dbg_stats);
1622 error_asoc_register:
1623 if (fsl_ssi_is_ac97(ssi))
1624 snd_soc_set_ac97_ops(NULL);
1626 if (fsl_ssi_is_ac97(ssi))
1627 mutex_destroy(&ssi->ac97_reg_lock);
1630 fsl_ssi_imx_clean(pdev, ssi);
1635 static int fsl_ssi_remove(struct platform_device *pdev)
1637 struct fsl_ssi *ssi = dev_get_drvdata(&pdev->dev);
1639 fsl_ssi_debugfs_remove(&ssi->dbg_stats);
1642 platform_device_unregister(ssi->card_pdev);
1644 /* Clean up SSI registers */
1645 fsl_ssi_hw_clean(ssi);
1648 fsl_ssi_imx_clean(pdev, ssi);
1650 if (fsl_ssi_is_ac97(ssi)) {
1651 snd_soc_set_ac97_ops(NULL);
1652 mutex_destroy(&ssi->ac97_reg_lock);
1658 #ifdef CONFIG_PM_SLEEP
1659 static int fsl_ssi_suspend(struct device *dev)
1661 struct fsl_ssi *ssi = dev_get_drvdata(dev);
1662 struct regmap *regs = ssi->regs;
1664 regmap_read(regs, REG_SSI_SFCSR, &ssi->regcache_sfcsr);
1665 regmap_read(regs, REG_SSI_SACNT, &ssi->regcache_sacnt);
1667 regcache_cache_only(regs, true);
1668 regcache_mark_dirty(regs);
1673 static int fsl_ssi_resume(struct device *dev)
1675 struct fsl_ssi *ssi = dev_get_drvdata(dev);
1676 struct regmap *regs = ssi->regs;
1678 regcache_cache_only(regs, false);
1680 regmap_update_bits(regs, REG_SSI_SFCSR,
1681 SSI_SFCSR_RFWM1_MASK | SSI_SFCSR_TFWM1_MASK |
1682 SSI_SFCSR_RFWM0_MASK | SSI_SFCSR_TFWM0_MASK,
1683 ssi->regcache_sfcsr);
1684 regmap_write(regs, REG_SSI_SACNT, ssi->regcache_sacnt);
1686 return regcache_sync(regs);
1688 #endif /* CONFIG_PM_SLEEP */
1690 static const struct dev_pm_ops fsl_ssi_pm = {
1691 SET_SYSTEM_SLEEP_PM_OPS(fsl_ssi_suspend, fsl_ssi_resume)
1694 static struct platform_driver fsl_ssi_driver = {
1696 .name = "fsl-ssi-dai",
1697 .of_match_table = fsl_ssi_ids,
1700 .probe = fsl_ssi_probe,
1701 .remove = fsl_ssi_remove,
1704 module_platform_driver(fsl_ssi_driver);
1706 MODULE_ALIAS("platform:fsl-ssi-dai");
1707 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
1708 MODULE_DESCRIPTION("Freescale Synchronous Serial Interface (SSI) ASoC Driver");
1709 MODULE_LICENSE("GPL v2");