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/device.h>
39 #include <linux/delay.h>
40 #include <linux/slab.h>
41 #include <linux/spinlock.h>
43 #include <linux/of_address.h>
44 #include <linux/of_irq.h>
45 #include <linux/of_platform.h>
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>
58 * FSLSSI_I2S_FORMATS: audio formats supported by the SSI
60 * The SSI has a limitation in that the samples must be in the same byte
61 * order as the host CPU. This is because when multiple bytes are written
62 * to the STX register, the bytes and bits must be written in the same
63 * order. The STX is a shift register, so all the bits need to be aligned
64 * (bit-endianness must match byte-endianness). Processors typically write
65 * the bits within a byte in the same order that the bytes of a word are
66 * written in. So if the host CPU is big-endian, then only big-endian
67 * samples will be written to STX properly.
70 #define FSLSSI_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_BE | \
71 SNDRV_PCM_FMTBIT_S18_3BE | SNDRV_PCM_FMTBIT_S20_3BE | \
72 SNDRV_PCM_FMTBIT_S24_3BE | SNDRV_PCM_FMTBIT_S24_BE)
74 #define FSLSSI_I2S_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE | \
75 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S20_3LE | \
76 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE)
79 #define FSLSSI_SIER_DBG_RX_FLAGS (CCSR_SSI_SIER_RFF0_EN | \
80 CCSR_SSI_SIER_RLS_EN | CCSR_SSI_SIER_RFS_EN | \
81 CCSR_SSI_SIER_ROE0_EN | CCSR_SSI_SIER_RFRC_EN)
82 #define FSLSSI_SIER_DBG_TX_FLAGS (CCSR_SSI_SIER_TFE0_EN | \
83 CCSR_SSI_SIER_TLS_EN | CCSR_SSI_SIER_TFS_EN | \
84 CCSR_SSI_SIER_TUE0_EN | CCSR_SSI_SIER_TFRC_EN)
93 struct fsl_ssi_reg_val {
100 struct fsl_ssi_rxtx_reg_val {
101 struct fsl_ssi_reg_val rx;
102 struct fsl_ssi_reg_val tx;
105 static bool fsl_ssi_readable_reg(struct device *dev, unsigned int reg)
108 case CCSR_SSI_SACCEN:
109 case CCSR_SSI_SACCDIS:
116 static bool fsl_ssi_volatile_reg(struct device *dev, unsigned int reg)
126 case CCSR_SSI_SACADD:
127 case CCSR_SSI_SACDAT:
129 case CCSR_SSI_SACCST:
137 static bool fsl_ssi_precious_reg(struct device *dev, unsigned int reg)
143 case CCSR_SSI_SACADD:
144 case CCSR_SSI_SACDAT:
152 static bool fsl_ssi_writeable_reg(struct device *dev, unsigned int reg)
157 case CCSR_SSI_SACCST:
164 static const struct regmap_config fsl_ssi_regconfig = {
165 .max_register = CCSR_SSI_SACCDIS,
169 .val_format_endian = REGMAP_ENDIAN_NATIVE,
170 .num_reg_defaults_raw = CCSR_SSI_SACCDIS / sizeof(uint32_t) + 1,
171 .readable_reg = fsl_ssi_readable_reg,
172 .volatile_reg = fsl_ssi_volatile_reg,
173 .precious_reg = fsl_ssi_precious_reg,
174 .writeable_reg = fsl_ssi_writeable_reg,
175 .cache_type = REGCACHE_FLAT,
178 struct fsl_ssi_soc_data {
180 bool imx21regs; /* imx21-class SSI - no SACC{ST,EN,DIS} regs */
186 * fsl_ssi_private: per-SSI private data
188 * @reg: Pointer to the regmap registers
189 * @irq: IRQ of this SSI
190 * @cpu_dai_drv: CPU DAI driver for this device
192 * @dai_fmt: DAI configuration this device is currently used with
193 * @i2s_mode: i2s and network mode configuration of the device. Is used to
194 * switch between normal and i2s/network mode
195 * mode depending on the number of channels
196 * @use_dma: DMA is used or FIQ with stream filter
197 * @use_dual_fifo: DMA with support for both FIFOs used
198 * @fifo_deph: Depth of the SSI FIFOs
199 * @rxtx_reg_val: Specific register settings for receive/transmit configuration
202 * @baudclk: SSI baud clock for master mode
203 * @baudclk_streams: Active streams that are using baudclk
204 * @bitclk_freq: bitclock frequency set by .set_dai_sysclk
206 * @dma_params_tx: DMA transmit parameters
207 * @dma_params_rx: DMA receive parameters
208 * @ssi_phys: physical address of the SSI registers
210 * @fiq_params: FIQ stream filtering parameters
212 * @pdev: Pointer to pdev used for deprecated fsl-ssi sound card
214 * @dbg_stats: Debugging statistics
216 * @soc: SoC specific data
218 struct fsl_ssi_private {
221 struct snd_soc_dai_driver cpu_dai_drv;
223 unsigned int dai_fmt;
227 bool has_ipg_clk_name;
228 unsigned int fifo_depth;
229 struct fsl_ssi_rxtx_reg_val rxtx_reg_val;
233 unsigned int baudclk_streams;
234 unsigned int bitclk_freq;
236 /* regcache for volatile regs */
241 struct snd_dmaengine_dai_dma_data dma_params_tx;
242 struct snd_dmaengine_dai_dma_data dma_params_rx;
245 /* params for non-dma FIQ stream filtered mode */
246 struct imx_pcm_fiq_params fiq_params;
248 /* Used when using fsl-ssi as sound-card. This is only used by ppc and
249 * should be replaced with simple-sound-card. */
250 struct platform_device *pdev;
252 struct fsl_ssi_dbg dbg_stats;
254 const struct fsl_ssi_soc_data *soc;
259 * imx51 and later SoCs have a slightly different IP that allows the
260 * SSI configuration while the SSI unit is running.
262 * More important, it is necessary on those SoCs to configure the
263 * sperate TX/RX DMA bits just before starting the stream
264 * (fsl_ssi_trigger). The SDMA unit has to be configured before fsl_ssi
265 * sends any DMA requests to the SDMA unit, otherwise it is not defined
266 * how the SDMA unit handles the DMA request.
268 * SDMA units are present on devices starting at imx35 but the imx35
269 * reference manual states that the DMA bits should not be changed
270 * while the SSI unit is running (SSIEN). So we support the necessary
271 * online configuration of fsl-ssi starting at imx51.
274 static struct fsl_ssi_soc_data fsl_ssi_mpc8610 = {
276 .offline_config = true,
277 .sisr_write_mask = CCSR_SSI_SISR_RFRC | CCSR_SSI_SISR_TFRC |
278 CCSR_SSI_SISR_ROE0 | CCSR_SSI_SISR_ROE1 |
279 CCSR_SSI_SISR_TUE0 | CCSR_SSI_SISR_TUE1,
282 static struct fsl_ssi_soc_data fsl_ssi_imx21 = {
285 .offline_config = true,
286 .sisr_write_mask = 0,
289 static struct fsl_ssi_soc_data fsl_ssi_imx35 = {
291 .offline_config = true,
292 .sisr_write_mask = CCSR_SSI_SISR_RFRC | CCSR_SSI_SISR_TFRC |
293 CCSR_SSI_SISR_ROE0 | CCSR_SSI_SISR_ROE1 |
294 CCSR_SSI_SISR_TUE0 | CCSR_SSI_SISR_TUE1,
297 static struct fsl_ssi_soc_data fsl_ssi_imx51 = {
299 .offline_config = false,
300 .sisr_write_mask = CCSR_SSI_SISR_ROE0 | CCSR_SSI_SISR_ROE1 |
301 CCSR_SSI_SISR_TUE0 | CCSR_SSI_SISR_TUE1,
304 static const struct of_device_id fsl_ssi_ids[] = {
305 { .compatible = "fsl,mpc8610-ssi", .data = &fsl_ssi_mpc8610 },
306 { .compatible = "fsl,imx51-ssi", .data = &fsl_ssi_imx51 },
307 { .compatible = "fsl,imx35-ssi", .data = &fsl_ssi_imx35 },
308 { .compatible = "fsl,imx21-ssi", .data = &fsl_ssi_imx21 },
311 MODULE_DEVICE_TABLE(of, fsl_ssi_ids);
313 static bool fsl_ssi_is_ac97(struct fsl_ssi_private *ssi_private)
315 return (ssi_private->dai_fmt & SND_SOC_DAIFMT_FORMAT_MASK) ==
319 static bool fsl_ssi_is_i2s_master(struct fsl_ssi_private *ssi_private)
321 return (ssi_private->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) ==
322 SND_SOC_DAIFMT_CBS_CFS;
325 static bool fsl_ssi_is_i2s_cbm_cfs(struct fsl_ssi_private *ssi_private)
327 return (ssi_private->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) ==
328 SND_SOC_DAIFMT_CBM_CFS;
331 * fsl_ssi_isr: SSI interrupt handler
333 * Although it's possible to use the interrupt handler to send and receive
334 * data to/from the SSI, we use the DMA instead. Programming is more
335 * complicated, but the performance is much better.
337 * This interrupt handler is used only to gather statistics.
339 * @irq: IRQ of the SSI device
340 * @dev_id: pointer to the ssi_private structure for this SSI device
342 static irqreturn_t fsl_ssi_isr(int irq, void *dev_id)
344 struct fsl_ssi_private *ssi_private = dev_id;
345 struct regmap *regs = ssi_private->regs;
349 /* We got an interrupt, so read the status register to see what we
350 were interrupted for. We mask it with the Interrupt Enable register
351 so that we only check for events that we're interested in.
353 regmap_read(regs, CCSR_SSI_SISR, &sisr);
355 sisr2 = sisr & ssi_private->soc->sisr_write_mask;
356 /* Clear the bits that we set */
358 regmap_write(regs, CCSR_SSI_SISR, sisr2);
360 fsl_ssi_dbg_isr(&ssi_private->dbg_stats, sisr);
366 * Enable/Disable all rx/tx config flags at once.
368 static void fsl_ssi_rxtx_config(struct fsl_ssi_private *ssi_private,
371 struct regmap *regs = ssi_private->regs;
372 struct fsl_ssi_rxtx_reg_val *vals = &ssi_private->rxtx_reg_val;
375 regmap_update_bits(regs, CCSR_SSI_SIER,
376 vals->rx.sier | vals->tx.sier,
377 vals->rx.sier | vals->tx.sier);
378 regmap_update_bits(regs, CCSR_SSI_SRCR,
379 vals->rx.srcr | vals->tx.srcr,
380 vals->rx.srcr | vals->tx.srcr);
381 regmap_update_bits(regs, CCSR_SSI_STCR,
382 vals->rx.stcr | vals->tx.stcr,
383 vals->rx.stcr | vals->tx.stcr);
385 regmap_update_bits(regs, CCSR_SSI_SRCR,
386 vals->rx.srcr | vals->tx.srcr, 0);
387 regmap_update_bits(regs, CCSR_SSI_STCR,
388 vals->rx.stcr | vals->tx.stcr, 0);
389 regmap_update_bits(regs, CCSR_SSI_SIER,
390 vals->rx.sier | vals->tx.sier, 0);
395 * Clear RX or TX FIFO to remove samples from the previous
396 * stream session which may be still present in the FIFO and
397 * may introduce bad samples and/or channel slipping.
399 * Note: The SOR is not documented in recent IMX datasheet, but
400 * is described in IMX51 reference manual at section 56.3.3.15.
402 static void fsl_ssi_fifo_clear(struct fsl_ssi_private *ssi_private,
406 regmap_update_bits(ssi_private->regs, CCSR_SSI_SOR,
407 CCSR_SSI_SOR_RX_CLR, CCSR_SSI_SOR_RX_CLR);
409 regmap_update_bits(ssi_private->regs, CCSR_SSI_SOR,
410 CCSR_SSI_SOR_TX_CLR, CCSR_SSI_SOR_TX_CLR);
415 * Calculate the bits that have to be disabled for the current stream that is
416 * getting disabled. This keeps the bits enabled that are necessary for the
417 * second stream to work if 'stream_active' is true.
419 * Detailed calculation:
420 * These are the values that need to be active after disabling. For non-active
421 * second stream, this is 0:
422 * vals_stream * !!stream_active
424 * The following computes the overall differences between the setup for the
425 * to-disable stream and the active stream, a simple XOR:
426 * vals_disable ^ (vals_stream * !!(stream_active))
428 * The full expression adds a mask on all values we care about
430 #define fsl_ssi_disable_val(vals_disable, vals_stream, stream_active) \
432 ((vals_disable) ^ ((vals_stream) * (u32)!!(stream_active))))
435 * Enable/Disable a ssi configuration. You have to pass either
436 * ssi_private->rxtx_reg_val.rx or tx as vals parameter.
438 static void fsl_ssi_config(struct fsl_ssi_private *ssi_private, bool enable,
439 struct fsl_ssi_reg_val *vals)
441 struct regmap *regs = ssi_private->regs;
442 struct fsl_ssi_reg_val *avals;
443 int nr_active_streams;
447 regmap_read(regs, CCSR_SSI_SCR, &scr_val);
449 nr_active_streams = !!(scr_val & CCSR_SSI_SCR_TE) +
450 !!(scr_val & CCSR_SSI_SCR_RE);
452 if (nr_active_streams - 1 > 0)
457 /* Find the other direction values rx or tx which we do not want to
459 if (&ssi_private->rxtx_reg_val.rx == vals)
460 avals = &ssi_private->rxtx_reg_val.tx;
462 avals = &ssi_private->rxtx_reg_val.rx;
464 /* If vals should be disabled, start with disabling the unit */
466 u32 scr = fsl_ssi_disable_val(vals->scr, avals->scr,
468 regmap_update_bits(regs, CCSR_SSI_SCR, scr, 0);
472 * We are running on a SoC which does not support online SSI
473 * reconfiguration, so we have to enable all necessary flags at once
474 * even if we do not use them later (capture and playback configuration)
476 if (ssi_private->soc->offline_config) {
477 if ((enable && !nr_active_streams) ||
478 (!enable && !keep_active))
479 fsl_ssi_rxtx_config(ssi_private, enable);
485 * Configure single direction units while the SSI unit is running
486 * (online configuration)
489 fsl_ssi_fifo_clear(ssi_private, vals->scr & CCSR_SSI_SCR_RE);
491 regmap_update_bits(regs, CCSR_SSI_SRCR, vals->srcr, vals->srcr);
492 regmap_update_bits(regs, CCSR_SSI_STCR, vals->stcr, vals->stcr);
493 regmap_update_bits(regs, CCSR_SSI_SIER, vals->sier, vals->sier);
500 * Disabling the necessary flags for one of rx/tx while the
501 * other stream is active is a little bit more difficult. We
502 * have to disable only those flags that differ between both
503 * streams (rx XOR tx) and that are set in the stream that is
504 * disabled now. Otherwise we could alter flags of the other
508 /* These assignments are simply vals without bits set in avals*/
509 sier = fsl_ssi_disable_val(vals->sier, avals->sier,
511 srcr = fsl_ssi_disable_val(vals->srcr, avals->srcr,
513 stcr = fsl_ssi_disable_val(vals->stcr, avals->stcr,
516 regmap_update_bits(regs, CCSR_SSI_SRCR, srcr, 0);
517 regmap_update_bits(regs, CCSR_SSI_STCR, stcr, 0);
518 regmap_update_bits(regs, CCSR_SSI_SIER, sier, 0);
522 /* Enabling of subunits is done after configuration */
524 if (ssi_private->use_dma && (vals->scr & CCSR_SSI_SCR_TE)) {
526 * Be sure the Tx FIFO is filled when TE is set.
527 * Otherwise, there are some chances to start the
528 * playback with some void samples inserted first,
529 * generating a channel slip.
531 * First, SSIEN must be set, to let the FIFO be filled.
534 * - Limit this fix to the DMA case until FIQ cases can
536 * - Limit the length of the busy loop to not lock the
537 * system too long, even if 1-2 loops are sufficient
542 regmap_update_bits(regs, CCSR_SSI_SCR,
543 CCSR_SSI_SCR_SSIEN, CCSR_SSI_SCR_SSIEN);
544 for (i = 0; i < max_loop; i++) {
546 regmap_read(regs, CCSR_SSI_SFCSR, &sfcsr);
547 if (CCSR_SSI_SFCSR_TFCNT0(sfcsr))
551 dev_err(ssi_private->dev,
552 "Timeout waiting TX FIFO filling\n");
555 regmap_update_bits(regs, CCSR_SSI_SCR, vals->scr, vals->scr);
560 static void fsl_ssi_rx_config(struct fsl_ssi_private *ssi_private, bool enable)
562 fsl_ssi_config(ssi_private, enable, &ssi_private->rxtx_reg_val.rx);
565 static void fsl_ssi_tx_config(struct fsl_ssi_private *ssi_private, bool enable)
567 fsl_ssi_config(ssi_private, enable, &ssi_private->rxtx_reg_val.tx);
571 * Setup rx/tx register values used to enable/disable the streams. These will
572 * be used later in fsl_ssi_config to setup the streams without the need to
573 * check for all different SSI modes.
575 static void fsl_ssi_setup_reg_vals(struct fsl_ssi_private *ssi_private)
577 struct fsl_ssi_rxtx_reg_val *reg = &ssi_private->rxtx_reg_val;
579 reg->rx.sier = CCSR_SSI_SIER_RFF0_EN;
580 reg->rx.srcr = CCSR_SSI_SRCR_RFEN0;
582 reg->tx.sier = CCSR_SSI_SIER_TFE0_EN;
583 reg->tx.stcr = CCSR_SSI_STCR_TFEN0;
586 if (!fsl_ssi_is_ac97(ssi_private)) {
587 reg->rx.scr = CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_RE;
588 reg->rx.sier |= CCSR_SSI_SIER_RFF0_EN;
589 reg->tx.scr = CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_TE;
590 reg->tx.sier |= CCSR_SSI_SIER_TFE0_EN;
593 if (ssi_private->use_dma) {
594 reg->rx.sier |= CCSR_SSI_SIER_RDMAE;
595 reg->tx.sier |= CCSR_SSI_SIER_TDMAE;
597 reg->rx.sier |= CCSR_SSI_SIER_RIE;
598 reg->tx.sier |= CCSR_SSI_SIER_TIE;
601 reg->rx.sier |= FSLSSI_SIER_DBG_RX_FLAGS;
602 reg->tx.sier |= FSLSSI_SIER_DBG_TX_FLAGS;
605 static void fsl_ssi_setup_ac97(struct fsl_ssi_private *ssi_private)
607 struct regmap *regs = ssi_private->regs;
610 * Setup the clock control register
612 regmap_write(regs, CCSR_SSI_STCCR,
613 CCSR_SSI_SxCCR_WL(17) | CCSR_SSI_SxCCR_DC(13));
614 regmap_write(regs, CCSR_SSI_SRCCR,
615 CCSR_SSI_SxCCR_WL(17) | CCSR_SSI_SxCCR_DC(13));
618 * Enable AC97 mode and startup the SSI
620 regmap_write(regs, CCSR_SSI_SACNT,
621 CCSR_SSI_SACNT_AC97EN | CCSR_SSI_SACNT_FV);
623 /* no SACC{ST,EN,DIS} regs on imx21-class SSI */
624 if (!ssi_private->soc->imx21regs) {
625 regmap_write(regs, CCSR_SSI_SACCDIS, 0xff);
626 regmap_write(regs, CCSR_SSI_SACCEN, 0x300);
630 * Enable SSI, Transmit and Receive. AC97 has to communicate with the
631 * codec before a stream is started.
633 regmap_update_bits(regs, CCSR_SSI_SCR,
634 CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_TE | CCSR_SSI_SCR_RE,
635 CCSR_SSI_SCR_SSIEN | CCSR_SSI_SCR_TE | CCSR_SSI_SCR_RE);
637 regmap_write(regs, CCSR_SSI_SOR, CCSR_SSI_SOR_WAIT(3));
641 * fsl_ssi_startup: create a new substream
643 * This is the first function called when a stream is opened.
645 * If this is the first stream open, then grab the IRQ and program most of
648 static int fsl_ssi_startup(struct snd_pcm_substream *substream,
649 struct snd_soc_dai *dai)
651 struct snd_soc_pcm_runtime *rtd = substream->private_data;
652 struct fsl_ssi_private *ssi_private =
653 snd_soc_dai_get_drvdata(rtd->cpu_dai);
656 ret = clk_prepare_enable(ssi_private->clk);
660 /* When using dual fifo mode, it is safer to ensure an even period
661 * size. If appearing to an odd number while DMA always starts its
662 * task from fifo0, fifo1 would be neglected at the end of each
663 * period. But SSI would still access fifo1 with an invalid data.
665 if (ssi_private->use_dual_fifo)
666 snd_pcm_hw_constraint_step(substream->runtime, 0,
667 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 2);
673 * fsl_ssi_shutdown: shutdown the SSI
676 static void fsl_ssi_shutdown(struct snd_pcm_substream *substream,
677 struct snd_soc_dai *dai)
679 struct snd_soc_pcm_runtime *rtd = substream->private_data;
680 struct fsl_ssi_private *ssi_private =
681 snd_soc_dai_get_drvdata(rtd->cpu_dai);
683 clk_disable_unprepare(ssi_private->clk);
688 * fsl_ssi_set_bclk - configure Digital Audio Interface bit clock
690 * Note: This function can be only called when using SSI as DAI master
692 * Quick instruction for parameters:
693 * freq: Output BCLK frequency = samplerate * 32 (fixed) * channels
694 * dir: SND_SOC_CLOCK_OUT -> TxBCLK, SND_SOC_CLOCK_IN -> RxBCLK.
696 static int fsl_ssi_set_bclk(struct snd_pcm_substream *substream,
697 struct snd_soc_dai *cpu_dai,
698 struct snd_pcm_hw_params *hw_params)
700 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
701 struct regmap *regs = ssi_private->regs;
702 int synchronous = ssi_private->cpu_dai_drv.symmetric_rates, ret;
703 u32 pm = 999, div2, psr, stccr, mask, afreq, factor, i;
704 unsigned long clkrate, baudrate, tmprate;
705 u64 sub, savesub = 100000;
707 bool baudclk_is_used;
709 /* Prefer the explicitly set bitclock frequency */
710 if (ssi_private->bitclk_freq)
711 freq = ssi_private->bitclk_freq;
713 freq = params_channels(hw_params) * 32 * params_rate(hw_params);
715 /* Don't apply it to any non-baudclk circumstance */
716 if (IS_ERR(ssi_private->baudclk))
720 * Hardware limitation: The bclk rate must be
721 * never greater than 1/5 IPG clock rate
723 if (freq * 5 > clk_get_rate(ssi_private->clk)) {
724 dev_err(cpu_dai->dev, "bitclk > ipgclk/5\n");
728 baudclk_is_used = ssi_private->baudclk_streams & ~(BIT(substream->stream));
730 /* It should be already enough to divide clock by setting pm alone */
734 factor = (div2 + 1) * (7 * psr + 1) * 2;
736 for (i = 0; i < 255; i++) {
737 tmprate = freq * factor * (i + 1);
740 clkrate = clk_get_rate(ssi_private->baudclk);
742 clkrate = clk_round_rate(ssi_private->baudclk, tmprate);
745 afreq = clkrate / (i + 1);
749 else if (freq / afreq == 1)
751 else if (afreq / freq == 1)
756 /* Calculate the fraction */
760 if (sub < savesub && !(i == 0 && psr == 0 && div2 == 0)) {
771 /* No proper pm found if it is still remaining the initial value */
773 dev_err(cpu_dai->dev, "failed to handle the required sysclk\n");
777 stccr = CCSR_SSI_SxCCR_PM(pm + 1) | (div2 ? CCSR_SSI_SxCCR_DIV2 : 0) |
778 (psr ? CCSR_SSI_SxCCR_PSR : 0);
779 mask = CCSR_SSI_SxCCR_PM_MASK | CCSR_SSI_SxCCR_DIV2 |
782 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK || synchronous)
783 regmap_update_bits(regs, CCSR_SSI_STCCR, mask, stccr);
785 regmap_update_bits(regs, CCSR_SSI_SRCCR, mask, stccr);
787 if (!baudclk_is_used) {
788 ret = clk_set_rate(ssi_private->baudclk, baudrate);
790 dev_err(cpu_dai->dev, "failed to set baudclk rate\n");
798 static int fsl_ssi_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
799 int clk_id, unsigned int freq, int dir)
801 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
803 ssi_private->bitclk_freq = freq;
809 * fsl_ssi_hw_params - program the sample size
811 * Most of the SSI registers have been programmed in the startup function,
812 * but the word length must be programmed here. Unfortunately, programming
813 * the SxCCR.WL bits requires the SSI to be temporarily disabled. This can
814 * cause a problem with supporting simultaneous playback and capture. If
815 * the SSI is already playing a stream, then that stream may be temporarily
816 * stopped when you start capture.
818 * Note: The SxCCR.DC and SxCCR.PM bits are only used if the SSI is the
821 static int fsl_ssi_hw_params(struct snd_pcm_substream *substream,
822 struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *cpu_dai)
824 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
825 struct regmap *regs = ssi_private->regs;
826 unsigned int channels = params_channels(hw_params);
827 unsigned int sample_size = params_width(hw_params);
828 u32 wl = CCSR_SSI_SxCCR_WL(sample_size);
833 regmap_read(regs, CCSR_SSI_SCR, &scr_val);
834 enabled = scr_val & CCSR_SSI_SCR_SSIEN;
837 * If we're in synchronous mode, and the SSI is already enabled,
838 * then STCCR is already set properly.
840 if (enabled && ssi_private->cpu_dai_drv.symmetric_rates)
843 if (fsl_ssi_is_i2s_master(ssi_private)) {
844 ret = fsl_ssi_set_bclk(substream, cpu_dai, hw_params);
848 /* Do not enable the clock if it is already enabled */
849 if (!(ssi_private->baudclk_streams & BIT(substream->stream))) {
850 ret = clk_prepare_enable(ssi_private->baudclk);
854 ssi_private->baudclk_streams |= BIT(substream->stream);
858 if (!fsl_ssi_is_ac97(ssi_private)) {
861 * Switch to normal net mode in order to have a frame sync
862 * signal every 32 bits instead of 16 bits
864 if (fsl_ssi_is_i2s_cbm_cfs(ssi_private) && sample_size == 16)
865 i2smode = CCSR_SSI_SCR_I2S_MODE_NORMAL |
868 i2smode = ssi_private->i2s_mode;
870 regmap_update_bits(regs, CCSR_SSI_SCR,
871 CCSR_SSI_SCR_NET | CCSR_SSI_SCR_I2S_MODE_MASK,
872 channels == 1 ? 0 : i2smode);
876 * FIXME: The documentation says that SxCCR[WL] should not be
877 * modified while the SSI is enabled. The only time this can
878 * happen is if we're trying to do simultaneous playback and
879 * capture in asynchronous mode. Unfortunately, I have been enable
880 * to get that to work at all on the P1022DS. Therefore, we don't
881 * bother to disable/enable the SSI when setting SxCCR[WL], because
882 * the SSI will stop anyway. Maybe one day, this will get fixed.
885 /* In synchronous mode, the SSI uses STCCR for capture */
886 if ((substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ||
887 ssi_private->cpu_dai_drv.symmetric_rates)
888 regmap_update_bits(regs, CCSR_SSI_STCCR, CCSR_SSI_SxCCR_WL_MASK,
891 regmap_update_bits(regs, CCSR_SSI_SRCCR, CCSR_SSI_SxCCR_WL_MASK,
897 static int fsl_ssi_hw_free(struct snd_pcm_substream *substream,
898 struct snd_soc_dai *cpu_dai)
900 struct snd_soc_pcm_runtime *rtd = substream->private_data;
901 struct fsl_ssi_private *ssi_private =
902 snd_soc_dai_get_drvdata(rtd->cpu_dai);
904 if (fsl_ssi_is_i2s_master(ssi_private) &&
905 ssi_private->baudclk_streams & BIT(substream->stream)) {
906 clk_disable_unprepare(ssi_private->baudclk);
907 ssi_private->baudclk_streams &= ~BIT(substream->stream);
913 static int _fsl_ssi_set_dai_fmt(struct device *dev,
914 struct fsl_ssi_private *ssi_private,
917 struct regmap *regs = ssi_private->regs;
918 u32 strcr = 0, stcr, srcr, scr, mask;
921 ssi_private->dai_fmt = fmt;
923 if (fsl_ssi_is_i2s_master(ssi_private) && IS_ERR(ssi_private->baudclk)) {
924 dev_err(dev, "baudclk is missing which is necessary for master mode\n");
928 fsl_ssi_setup_reg_vals(ssi_private);
930 regmap_read(regs, CCSR_SSI_SCR, &scr);
931 scr &= ~(CCSR_SSI_SCR_SYN | CCSR_SSI_SCR_I2S_MODE_MASK);
932 scr |= CCSR_SSI_SCR_SYNC_TX_FS;
934 mask = CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TFDIR | CCSR_SSI_STCR_TXDIR |
935 CCSR_SSI_STCR_TSCKP | CCSR_SSI_STCR_TFSI | CCSR_SSI_STCR_TFSL |
937 regmap_read(regs, CCSR_SSI_STCR, &stcr);
938 regmap_read(regs, CCSR_SSI_SRCR, &srcr);
942 ssi_private->i2s_mode = CCSR_SSI_SCR_NET;
943 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
944 case SND_SOC_DAIFMT_I2S:
945 regmap_update_bits(regs, CCSR_SSI_STCCR,
946 CCSR_SSI_SxCCR_DC_MASK,
947 CCSR_SSI_SxCCR_DC(2));
948 regmap_update_bits(regs, CCSR_SSI_SRCCR,
949 CCSR_SSI_SxCCR_DC_MASK,
950 CCSR_SSI_SxCCR_DC(2));
951 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
952 case SND_SOC_DAIFMT_CBM_CFS:
953 case SND_SOC_DAIFMT_CBS_CFS:
954 ssi_private->i2s_mode |= CCSR_SSI_SCR_I2S_MODE_MASTER;
956 case SND_SOC_DAIFMT_CBM_CFM:
957 ssi_private->i2s_mode |= CCSR_SSI_SCR_I2S_MODE_SLAVE;
963 /* Data on rising edge of bclk, frame low, 1clk before data */
964 strcr |= CCSR_SSI_STCR_TFSI | CCSR_SSI_STCR_TSCKP |
965 CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TEFS;
967 case SND_SOC_DAIFMT_LEFT_J:
968 /* Data on rising edge of bclk, frame high */
969 strcr |= CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TSCKP;
971 case SND_SOC_DAIFMT_DSP_A:
972 /* Data on rising edge of bclk, frame high, 1clk before data */
973 strcr |= CCSR_SSI_STCR_TFSL | CCSR_SSI_STCR_TSCKP |
974 CCSR_SSI_STCR_TXBIT0 | CCSR_SSI_STCR_TEFS;
976 case SND_SOC_DAIFMT_DSP_B:
977 /* Data on rising edge of bclk, frame high */
978 strcr |= CCSR_SSI_STCR_TFSL | CCSR_SSI_STCR_TSCKP |
979 CCSR_SSI_STCR_TXBIT0;
981 case SND_SOC_DAIFMT_AC97:
982 ssi_private->i2s_mode |= CCSR_SSI_SCR_I2S_MODE_NORMAL;
987 scr |= ssi_private->i2s_mode;
989 /* DAI clock inversion */
990 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
991 case SND_SOC_DAIFMT_NB_NF:
992 /* Nothing to do for both normal cases */
994 case SND_SOC_DAIFMT_IB_NF:
995 /* Invert bit clock */
996 strcr ^= CCSR_SSI_STCR_TSCKP;
998 case SND_SOC_DAIFMT_NB_IF:
999 /* Invert frame clock */
1000 strcr ^= CCSR_SSI_STCR_TFSI;
1002 case SND_SOC_DAIFMT_IB_IF:
1003 /* Invert both clocks */
1004 strcr ^= CCSR_SSI_STCR_TSCKP;
1005 strcr ^= CCSR_SSI_STCR_TFSI;
1011 /* DAI clock master masks */
1012 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1013 case SND_SOC_DAIFMT_CBS_CFS:
1014 strcr |= CCSR_SSI_STCR_TFDIR | CCSR_SSI_STCR_TXDIR;
1015 scr |= CCSR_SSI_SCR_SYS_CLK_EN;
1017 case SND_SOC_DAIFMT_CBM_CFM:
1018 scr &= ~CCSR_SSI_SCR_SYS_CLK_EN;
1020 case SND_SOC_DAIFMT_CBM_CFS:
1021 strcr &= ~CCSR_SSI_STCR_TXDIR;
1022 strcr |= CCSR_SSI_STCR_TFDIR;
1023 scr &= ~CCSR_SSI_SCR_SYS_CLK_EN;
1026 if (!fsl_ssi_is_ac97(ssi_private))
1033 if (ssi_private->cpu_dai_drv.symmetric_rates
1034 || fsl_ssi_is_ac97(ssi_private)) {
1035 /* Need to clear RXDIR when using SYNC or AC97 mode */
1036 srcr &= ~CCSR_SSI_SRCR_RXDIR;
1037 scr |= CCSR_SSI_SCR_SYN;
1040 regmap_write(regs, CCSR_SSI_STCR, stcr);
1041 regmap_write(regs, CCSR_SSI_SRCR, srcr);
1042 regmap_write(regs, CCSR_SSI_SCR, scr);
1045 * Set the watermark for transmit FIFI 0 and receive FIFO 0. We don't
1046 * use FIFO 1. We program the transmit water to signal a DMA transfer
1047 * if there are only two (or fewer) elements left in the FIFO. Two
1048 * elements equals one frame (left channel, right channel). This value,
1049 * however, depends on the depth of the transmit buffer.
1051 * We set the watermark on the same level as the DMA burstsize. For
1052 * fiq it is probably better to use the biggest possible watermark
1055 if (ssi_private->use_dma)
1056 wm = ssi_private->fifo_depth - 2;
1058 wm = ssi_private->fifo_depth;
1060 regmap_write(regs, CCSR_SSI_SFCSR,
1061 CCSR_SSI_SFCSR_TFWM0(wm) | CCSR_SSI_SFCSR_RFWM0(wm) |
1062 CCSR_SSI_SFCSR_TFWM1(wm) | CCSR_SSI_SFCSR_RFWM1(wm));
1064 if (ssi_private->use_dual_fifo) {
1065 regmap_update_bits(regs, CCSR_SSI_SRCR, CCSR_SSI_SRCR_RFEN1,
1066 CCSR_SSI_SRCR_RFEN1);
1067 regmap_update_bits(regs, CCSR_SSI_STCR, CCSR_SSI_STCR_TFEN1,
1068 CCSR_SSI_STCR_TFEN1);
1069 regmap_update_bits(regs, CCSR_SSI_SCR, CCSR_SSI_SCR_TCH_EN,
1070 CCSR_SSI_SCR_TCH_EN);
1073 if ((fmt & SND_SOC_DAIFMT_FORMAT_MASK) == SND_SOC_DAIFMT_AC97)
1074 fsl_ssi_setup_ac97(ssi_private);
1081 * fsl_ssi_set_dai_fmt - configure Digital Audio Interface Format.
1083 static int fsl_ssi_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
1085 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
1087 return _fsl_ssi_set_dai_fmt(cpu_dai->dev, ssi_private, fmt);
1091 * fsl_ssi_set_dai_tdm_slot - set TDM slot number
1093 * Note: This function can be only called when using SSI as DAI master
1095 static int fsl_ssi_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai, u32 tx_mask,
1096 u32 rx_mask, int slots, int slot_width)
1098 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(cpu_dai);
1099 struct regmap *regs = ssi_private->regs;
1102 /* The slot number should be >= 2 if using Network mode or I2S mode */
1103 regmap_read(regs, CCSR_SSI_SCR, &val);
1104 val &= CCSR_SSI_SCR_I2S_MODE_MASK | CCSR_SSI_SCR_NET;
1105 if (val && slots < 2) {
1106 dev_err(cpu_dai->dev, "slot number should be >= 2 in I2S or NET\n");
1110 regmap_update_bits(regs, CCSR_SSI_STCCR, CCSR_SSI_SxCCR_DC_MASK,
1111 CCSR_SSI_SxCCR_DC(slots));
1112 regmap_update_bits(regs, CCSR_SSI_SRCCR, CCSR_SSI_SxCCR_DC_MASK,
1113 CCSR_SSI_SxCCR_DC(slots));
1115 /* The register SxMSKs needs SSI to provide essential clock due to
1116 * hardware design. So we here temporarily enable SSI to set them.
1118 regmap_read(regs, CCSR_SSI_SCR, &val);
1119 val &= CCSR_SSI_SCR_SSIEN;
1120 regmap_update_bits(regs, CCSR_SSI_SCR, CCSR_SSI_SCR_SSIEN,
1121 CCSR_SSI_SCR_SSIEN);
1123 regmap_write(regs, CCSR_SSI_STMSK, ~tx_mask);
1124 regmap_write(regs, CCSR_SSI_SRMSK, ~rx_mask);
1126 regmap_update_bits(regs, CCSR_SSI_SCR, CCSR_SSI_SCR_SSIEN, val);
1132 * fsl_ssi_trigger: start and stop the DMA transfer.
1134 * This function is called by ALSA to start, stop, pause, and resume the DMA
1137 * The DMA channel is in external master start and pause mode, which
1138 * means the SSI completely controls the flow of data.
1140 static int fsl_ssi_trigger(struct snd_pcm_substream *substream, int cmd,
1141 struct snd_soc_dai *dai)
1143 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1144 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(rtd->cpu_dai);
1145 struct regmap *regs = ssi_private->regs;
1148 case SNDRV_PCM_TRIGGER_START:
1149 case SNDRV_PCM_TRIGGER_RESUME:
1150 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1151 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1152 fsl_ssi_tx_config(ssi_private, true);
1154 fsl_ssi_rx_config(ssi_private, true);
1157 case SNDRV_PCM_TRIGGER_STOP:
1158 case SNDRV_PCM_TRIGGER_SUSPEND:
1159 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1160 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1161 fsl_ssi_tx_config(ssi_private, false);
1163 fsl_ssi_rx_config(ssi_private, false);
1170 if (fsl_ssi_is_ac97(ssi_private)) {
1171 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1172 regmap_write(regs, CCSR_SSI_SOR, CCSR_SSI_SOR_TX_CLR);
1174 regmap_write(regs, CCSR_SSI_SOR, CCSR_SSI_SOR_RX_CLR);
1180 static int fsl_ssi_dai_probe(struct snd_soc_dai *dai)
1182 struct fsl_ssi_private *ssi_private = snd_soc_dai_get_drvdata(dai);
1184 if (ssi_private->soc->imx && ssi_private->use_dma) {
1185 dai->playback_dma_data = &ssi_private->dma_params_tx;
1186 dai->capture_dma_data = &ssi_private->dma_params_rx;
1192 static const struct snd_soc_dai_ops fsl_ssi_dai_ops = {
1193 .startup = fsl_ssi_startup,
1194 .shutdown = fsl_ssi_shutdown,
1195 .hw_params = fsl_ssi_hw_params,
1196 .hw_free = fsl_ssi_hw_free,
1197 .set_fmt = fsl_ssi_set_dai_fmt,
1198 .set_sysclk = fsl_ssi_set_dai_sysclk,
1199 .set_tdm_slot = fsl_ssi_set_dai_tdm_slot,
1200 .trigger = fsl_ssi_trigger,
1203 /* Template for the CPU dai driver structure */
1204 static struct snd_soc_dai_driver fsl_ssi_dai_template = {
1205 .probe = fsl_ssi_dai_probe,
1207 .stream_name = "CPU-Playback",
1210 .rates = SNDRV_PCM_RATE_CONTINUOUS,
1211 .formats = FSLSSI_I2S_FORMATS,
1214 .stream_name = "CPU-Capture",
1217 .rates = SNDRV_PCM_RATE_CONTINUOUS,
1218 .formats = FSLSSI_I2S_FORMATS,
1220 .ops = &fsl_ssi_dai_ops,
1223 static const struct snd_soc_component_driver fsl_ssi_component = {
1227 static struct snd_soc_dai_driver fsl_ssi_ac97_dai = {
1228 .bus_control = true,
1229 .probe = fsl_ssi_dai_probe,
1231 .stream_name = "AC97 Playback",
1234 .rates = SNDRV_PCM_RATE_8000_48000,
1235 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1238 .stream_name = "AC97 Capture",
1241 .rates = SNDRV_PCM_RATE_48000,
1242 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1244 .ops = &fsl_ssi_dai_ops,
1248 static struct fsl_ssi_private *fsl_ac97_data;
1250 static void fsl_ssi_ac97_write(struct snd_ac97 *ac97, unsigned short reg,
1253 struct regmap *regs = fsl_ac97_data->regs;
1261 ret = clk_prepare_enable(fsl_ac97_data->clk);
1263 pr_err("ac97 write clk_prepare_enable failed: %d\n",
1269 regmap_write(regs, CCSR_SSI_SACADD, lreg);
1272 regmap_write(regs, CCSR_SSI_SACDAT, lval);
1274 regmap_update_bits(regs, CCSR_SSI_SACNT, CCSR_SSI_SACNT_RDWR_MASK,
1278 clk_disable_unprepare(fsl_ac97_data->clk);
1281 static unsigned short fsl_ssi_ac97_read(struct snd_ac97 *ac97,
1284 struct regmap *regs = fsl_ac97_data->regs;
1286 unsigned short val = -1;
1291 ret = clk_prepare_enable(fsl_ac97_data->clk);
1293 pr_err("ac97 read clk_prepare_enable failed: %d\n",
1298 lreg = (reg & 0x7f) << 12;
1299 regmap_write(regs, CCSR_SSI_SACADD, lreg);
1300 regmap_update_bits(regs, CCSR_SSI_SACNT, CCSR_SSI_SACNT_RDWR_MASK,
1305 regmap_read(regs, CCSR_SSI_SACDAT, ®_val);
1306 val = (reg_val >> 4) & 0xffff;
1308 clk_disable_unprepare(fsl_ac97_data->clk);
1313 static struct snd_ac97_bus_ops fsl_ssi_ac97_ops = {
1314 .read = fsl_ssi_ac97_read,
1315 .write = fsl_ssi_ac97_write,
1319 * Make every character in a string lower-case
1321 static void make_lowercase(char *s)
1327 if ((c >= 'A') && (c <= 'Z'))
1328 *p = c + ('a' - 'A');
1333 static int fsl_ssi_imx_probe(struct platform_device *pdev,
1334 struct fsl_ssi_private *ssi_private, void __iomem *iomem)
1336 struct device_node *np = pdev->dev.of_node;
1340 if (ssi_private->has_ipg_clk_name)
1341 ssi_private->clk = devm_clk_get(&pdev->dev, "ipg");
1343 ssi_private->clk = devm_clk_get(&pdev->dev, NULL);
1344 if (IS_ERR(ssi_private->clk)) {
1345 ret = PTR_ERR(ssi_private->clk);
1346 dev_err(&pdev->dev, "could not get clock: %d\n", ret);
1350 if (!ssi_private->has_ipg_clk_name) {
1351 ret = clk_prepare_enable(ssi_private->clk);
1353 dev_err(&pdev->dev, "clk_prepare_enable failed: %d\n", ret);
1358 /* For those SLAVE implementations, we ignore non-baudclk cases
1359 * and, instead, abandon MASTER mode that needs baud clock.
1361 ssi_private->baudclk = devm_clk_get(&pdev->dev, "baud");
1362 if (IS_ERR(ssi_private->baudclk))
1363 dev_dbg(&pdev->dev, "could not get baud clock: %ld\n",
1364 PTR_ERR(ssi_private->baudclk));
1367 * We have burstsize be "fifo_depth - 2" to match the SSI
1368 * watermark setting in fsl_ssi_startup().
1370 ssi_private->dma_params_tx.maxburst = ssi_private->fifo_depth - 2;
1371 ssi_private->dma_params_rx.maxburst = ssi_private->fifo_depth - 2;
1372 ssi_private->dma_params_tx.addr = ssi_private->ssi_phys + CCSR_SSI_STX0;
1373 ssi_private->dma_params_rx.addr = ssi_private->ssi_phys + CCSR_SSI_SRX0;
1375 ret = of_property_read_u32_array(np, "dmas", dmas, 4);
1376 if (ssi_private->use_dma && !ret && dmas[2] == IMX_DMATYPE_SSI_DUAL) {
1377 ssi_private->use_dual_fifo = true;
1378 /* When using dual fifo mode, we need to keep watermark
1379 * as even numbers due to dma script limitation.
1381 ssi_private->dma_params_tx.maxburst &= ~0x1;
1382 ssi_private->dma_params_rx.maxburst &= ~0x1;
1385 if (!ssi_private->use_dma) {
1388 * Some boards use an incompatible codec. To get it
1389 * working, we are using imx-fiq-pcm-audio, that
1390 * can handle those codecs. DMA is not possible in this
1394 ssi_private->fiq_params.irq = ssi_private->irq;
1395 ssi_private->fiq_params.base = iomem;
1396 ssi_private->fiq_params.dma_params_rx =
1397 &ssi_private->dma_params_rx;
1398 ssi_private->fiq_params.dma_params_tx =
1399 &ssi_private->dma_params_tx;
1401 ret = imx_pcm_fiq_init(pdev, &ssi_private->fiq_params);
1405 ret = imx_pcm_dma_init(pdev, IMX_SSI_DMABUF_SIZE);
1414 if (!ssi_private->has_ipg_clk_name)
1415 clk_disable_unprepare(ssi_private->clk);
1419 static void fsl_ssi_imx_clean(struct platform_device *pdev,
1420 struct fsl_ssi_private *ssi_private)
1422 if (!ssi_private->use_dma)
1423 imx_pcm_fiq_exit(pdev);
1424 if (!ssi_private->has_ipg_clk_name)
1425 clk_disable_unprepare(ssi_private->clk);
1428 static int fsl_ssi_probe(struct platform_device *pdev)
1430 struct fsl_ssi_private *ssi_private;
1432 struct device_node *np = pdev->dev.of_node;
1433 const struct of_device_id *of_id;
1434 const char *p, *sprop;
1435 const uint32_t *iprop;
1436 struct resource *res;
1437 void __iomem *iomem;
1439 struct regmap_config regconfig = fsl_ssi_regconfig;
1441 of_id = of_match_device(fsl_ssi_ids, &pdev->dev);
1442 if (!of_id || !of_id->data)
1445 ssi_private = devm_kzalloc(&pdev->dev, sizeof(*ssi_private),
1448 dev_err(&pdev->dev, "could not allocate DAI object\n");
1452 ssi_private->soc = of_id->data;
1453 ssi_private->dev = &pdev->dev;
1455 sprop = of_get_property(np, "fsl,mode", NULL);
1457 if (!strcmp(sprop, "ac97-slave"))
1458 ssi_private->dai_fmt = SND_SOC_DAIFMT_AC97;
1461 ssi_private->use_dma = !of_property_read_bool(np,
1462 "fsl,fiq-stream-filter");
1464 if (fsl_ssi_is_ac97(ssi_private)) {
1465 memcpy(&ssi_private->cpu_dai_drv, &fsl_ssi_ac97_dai,
1466 sizeof(fsl_ssi_ac97_dai));
1468 fsl_ac97_data = ssi_private;
1470 ret = snd_soc_set_ac97_ops_of_reset(&fsl_ssi_ac97_ops, pdev);
1472 dev_err(&pdev->dev, "could not set AC'97 ops\n");
1476 /* Initialize this copy of the CPU DAI driver structure */
1477 memcpy(&ssi_private->cpu_dai_drv, &fsl_ssi_dai_template,
1478 sizeof(fsl_ssi_dai_template));
1480 ssi_private->cpu_dai_drv.name = dev_name(&pdev->dev);
1482 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1483 iomem = devm_ioremap_resource(&pdev->dev, res);
1485 return PTR_ERR(iomem);
1486 ssi_private->ssi_phys = res->start;
1488 if (ssi_private->soc->imx21regs) {
1490 * According to datasheet imx21-class SSI
1491 * don't have SACC{ST,EN,DIS} regs.
1493 regconfig.max_register = CCSR_SSI_SRMSK;
1494 regconfig.num_reg_defaults_raw =
1495 CCSR_SSI_SRMSK / sizeof(uint32_t) + 1;
1498 ret = of_property_match_string(np, "clock-names", "ipg");
1500 ssi_private->has_ipg_clk_name = false;
1501 ssi_private->regs = devm_regmap_init_mmio(&pdev->dev, iomem,
1504 ssi_private->has_ipg_clk_name = true;
1505 ssi_private->regs = devm_regmap_init_mmio_clk(&pdev->dev,
1506 "ipg", iomem, ®config);
1508 if (IS_ERR(ssi_private->regs)) {
1509 dev_err(&pdev->dev, "Failed to init register map\n");
1510 return PTR_ERR(ssi_private->regs);
1513 ssi_private->irq = platform_get_irq(pdev, 0);
1514 if (ssi_private->irq < 0) {
1515 dev_err(&pdev->dev, "no irq for node %s\n", pdev->name);
1516 return ssi_private->irq;
1519 /* Are the RX and the TX clocks locked? */
1520 if (!of_find_property(np, "fsl,ssi-asynchronous", NULL)) {
1521 if (!fsl_ssi_is_ac97(ssi_private))
1522 ssi_private->cpu_dai_drv.symmetric_rates = 1;
1524 ssi_private->cpu_dai_drv.symmetric_channels = 1;
1525 ssi_private->cpu_dai_drv.symmetric_samplebits = 1;
1528 /* Determine the FIFO depth. */
1529 iprop = of_get_property(np, "fsl,fifo-depth", NULL);
1531 ssi_private->fifo_depth = be32_to_cpup(iprop);
1533 /* Older 8610 DTs didn't have the fifo-depth property */
1534 ssi_private->fifo_depth = 8;
1536 dev_set_drvdata(&pdev->dev, ssi_private);
1538 if (ssi_private->soc->imx) {
1539 ret = fsl_ssi_imx_probe(pdev, ssi_private, iomem);
1544 ret = devm_snd_soc_register_component(&pdev->dev, &fsl_ssi_component,
1545 &ssi_private->cpu_dai_drv, 1);
1547 dev_err(&pdev->dev, "failed to register DAI: %d\n", ret);
1548 goto error_asoc_register;
1551 if (ssi_private->use_dma) {
1552 ret = devm_request_irq(&pdev->dev, ssi_private->irq,
1553 fsl_ssi_isr, 0, dev_name(&pdev->dev),
1556 dev_err(&pdev->dev, "could not claim irq %u\n",
1558 goto error_asoc_register;
1562 ret = fsl_ssi_debugfs_create(&ssi_private->dbg_stats, &pdev->dev);
1564 goto error_asoc_register;
1567 * If codec-handle property is missing from SSI node, we assume
1568 * that the machine driver uses new binding which does not require
1569 * SSI driver to trigger machine driver's probe.
1571 if (!of_get_property(np, "codec-handle", NULL))
1574 /* Trigger the machine driver's probe function. The platform driver
1575 * name of the machine driver is taken from /compatible property of the
1576 * device tree. We also pass the address of the CPU DAI driver
1579 sprop = of_get_property(of_find_node_by_path("/"), "compatible", NULL);
1580 /* Sometimes the compatible name has a "fsl," prefix, so we strip it. */
1581 p = strrchr(sprop, ',');
1584 snprintf(name, sizeof(name), "snd-soc-%s", sprop);
1585 make_lowercase(name);
1588 platform_device_register_data(&pdev->dev, name, 0, NULL, 0);
1589 if (IS_ERR(ssi_private->pdev)) {
1590 ret = PTR_ERR(ssi_private->pdev);
1591 dev_err(&pdev->dev, "failed to register platform: %d\n", ret);
1592 goto error_sound_card;
1596 if (ssi_private->dai_fmt)
1597 _fsl_ssi_set_dai_fmt(&pdev->dev, ssi_private,
1598 ssi_private->dai_fmt);
1600 if (fsl_ssi_is_ac97(ssi_private)) {
1603 ret = of_property_read_u32(np, "cell-index", &ssi_idx);
1605 dev_err(&pdev->dev, "cannot get SSI index property\n");
1606 goto error_sound_card;
1610 platform_device_register_data(NULL,
1611 "ac97-codec", ssi_idx, NULL, 0);
1612 if (IS_ERR(ssi_private->pdev)) {
1613 ret = PTR_ERR(ssi_private->pdev);
1615 "failed to register AC97 codec platform: %d\n",
1617 goto error_sound_card;
1624 fsl_ssi_debugfs_remove(&ssi_private->dbg_stats);
1626 error_asoc_register:
1627 if (ssi_private->soc->imx)
1628 fsl_ssi_imx_clean(pdev, ssi_private);
1633 static int fsl_ssi_remove(struct platform_device *pdev)
1635 struct fsl_ssi_private *ssi_private = dev_get_drvdata(&pdev->dev);
1637 fsl_ssi_debugfs_remove(&ssi_private->dbg_stats);
1639 if (ssi_private->pdev)
1640 platform_device_unregister(ssi_private->pdev);
1642 if (ssi_private->soc->imx)
1643 fsl_ssi_imx_clean(pdev, ssi_private);
1645 if (fsl_ssi_is_ac97(ssi_private))
1646 snd_soc_set_ac97_ops(NULL);
1651 #ifdef CONFIG_PM_SLEEP
1652 static int fsl_ssi_suspend(struct device *dev)
1654 struct fsl_ssi_private *ssi_private = dev_get_drvdata(dev);
1655 struct regmap *regs = ssi_private->regs;
1657 regmap_read(regs, CCSR_SSI_SFCSR,
1658 &ssi_private->regcache_sfcsr);
1659 regmap_read(regs, CCSR_SSI_SACNT,
1660 &ssi_private->regcache_sacnt);
1662 regcache_cache_only(regs, true);
1663 regcache_mark_dirty(regs);
1668 static int fsl_ssi_resume(struct device *dev)
1670 struct fsl_ssi_private *ssi_private = dev_get_drvdata(dev);
1671 struct regmap *regs = ssi_private->regs;
1673 regcache_cache_only(regs, false);
1675 regmap_update_bits(regs, CCSR_SSI_SFCSR,
1676 CCSR_SSI_SFCSR_RFWM1_MASK | CCSR_SSI_SFCSR_TFWM1_MASK |
1677 CCSR_SSI_SFCSR_RFWM0_MASK | CCSR_SSI_SFCSR_TFWM0_MASK,
1678 ssi_private->regcache_sfcsr);
1679 regmap_write(regs, CCSR_SSI_SACNT,
1680 ssi_private->regcache_sacnt);
1682 return regcache_sync(regs);
1684 #endif /* CONFIG_PM_SLEEP */
1686 static const struct dev_pm_ops fsl_ssi_pm = {
1687 SET_SYSTEM_SLEEP_PM_OPS(fsl_ssi_suspend, fsl_ssi_resume)
1690 static struct platform_driver fsl_ssi_driver = {
1692 .name = "fsl-ssi-dai",
1693 .of_match_table = fsl_ssi_ids,
1696 .probe = fsl_ssi_probe,
1697 .remove = fsl_ssi_remove,
1700 module_platform_driver(fsl_ssi_driver);
1702 MODULE_ALIAS("platform:fsl-ssi-dai");
1703 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
1704 MODULE_DESCRIPTION("Freescale Synchronous Serial Interface (SSI) ASoC Driver");
1705 MODULE_LICENSE("GPL v2");