2 * imx-ssi.c -- ALSA Soc Audio Layer
4 * Copyright 2009 Sascha Hauer <s.hauer@pengutronix.de>
6 * This code is based on code copyrighted by Freescale,
7 * Liam Girdwood, Javier Martin and probably others.
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
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.
34 #include <linux/clk.h>
35 #include <linux/delay.h>
36 #include <linux/device.h>
37 #include <linux/dma-mapping.h>
38 #include <linux/init.h>
39 #include <linux/interrupt.h>
40 #include <linux/module.h>
41 #include <linux/platform_device.h>
42 #include <linux/slab.h>
44 #include <sound/core.h>
45 #include <sound/initval.h>
46 #include <sound/pcm.h>
47 #include <sound/pcm_params.h>
48 #include <sound/soc.h>
50 #include <linux/platform_data/asoc-imx-ssi.h>
53 #include "fsl_utils.h"
55 #define SSI_SACNT_DEFAULT (SSI_SACNT_AC97EN | SSI_SACNT_FV)
58 * SSI Network Mode or TDM slots configuration.
59 * Should only be called when port is inactive (i.e. SSIEN = 0).
61 static int imx_ssi_set_dai_tdm_slot(struct snd_soc_dai
*cpu_dai
,
62 unsigned int tx_mask
, unsigned int rx_mask
, int slots
, int slot_width
)
64 struct imx_ssi
*ssi
= snd_soc_dai_get_drvdata(cpu_dai
);
67 sccr
= readl(ssi
->base
+ SSI_STCCR
);
68 sccr
&= ~SSI_STCCR_DC_MASK
;
69 sccr
|= SSI_STCCR_DC(slots
- 1);
70 writel(sccr
, ssi
->base
+ SSI_STCCR
);
72 sccr
= readl(ssi
->base
+ SSI_SRCCR
);
73 sccr
&= ~SSI_STCCR_DC_MASK
;
74 sccr
|= SSI_STCCR_DC(slots
- 1);
75 writel(sccr
, ssi
->base
+ SSI_SRCCR
);
77 writel(~tx_mask
, ssi
->base
+ SSI_STMSK
);
78 writel(~rx_mask
, ssi
->base
+ SSI_SRMSK
);
84 * SSI DAI format configuration.
85 * Should only be called when port is inactive (i.e. SSIEN = 0).
87 static int imx_ssi_set_dai_fmt(struct snd_soc_dai
*cpu_dai
, unsigned int fmt
)
89 struct imx_ssi
*ssi
= snd_soc_dai_get_drvdata(cpu_dai
);
92 scr
= readl(ssi
->base
+ SSI_SCR
) & ~(SSI_SCR_SYN
| SSI_SCR_NET
);
95 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
96 case SND_SOC_DAIFMT_I2S
:
97 /* data on rising edge of bclk, frame low 1clk before data */
98 strcr
|= SSI_STCR_TXBIT0
| SSI_STCR_TSCKP
| SSI_STCR_TFSI
|
101 if (ssi
->flags
& IMX_SSI_USE_I2S_SLAVE
) {
102 scr
&= ~SSI_I2S_MODE_MASK
;
103 scr
|= SSI_SCR_I2S_MODE_SLAVE
;
106 case SND_SOC_DAIFMT_LEFT_J
:
107 /* data on rising edge of bclk, frame high with data */
108 strcr
|= SSI_STCR_TXBIT0
| SSI_STCR_TSCKP
;
110 case SND_SOC_DAIFMT_DSP_B
:
111 /* data on rising edge of bclk, frame high with data */
112 strcr
|= SSI_STCR_TXBIT0
| SSI_STCR_TSCKP
| SSI_STCR_TFSL
;
114 case SND_SOC_DAIFMT_DSP_A
:
115 /* data on rising edge of bclk, frame high 1clk before data */
116 strcr
|= SSI_STCR_TXBIT0
| SSI_STCR_TSCKP
| SSI_STCR_TFSL
|
121 /* DAI clock inversion */
122 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
123 case SND_SOC_DAIFMT_IB_IF
:
124 strcr
^= SSI_STCR_TSCKP
| SSI_STCR_TFSI
;
126 case SND_SOC_DAIFMT_IB_NF
:
127 strcr
^= SSI_STCR_TSCKP
;
129 case SND_SOC_DAIFMT_NB_IF
:
130 strcr
^= SSI_STCR_TFSI
;
132 case SND_SOC_DAIFMT_NB_NF
:
136 /* DAI clock master masks */
137 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
138 case SND_SOC_DAIFMT_CBM_CFM
:
141 /* Master mode not implemented, needs handling of clocks. */
145 strcr
|= SSI_STCR_TFEN0
;
147 if (ssi
->flags
& IMX_SSI_NET
)
149 if (ssi
->flags
& IMX_SSI_SYN
)
152 writel(strcr
, ssi
->base
+ SSI_STCR
);
153 writel(strcr
, ssi
->base
+ SSI_SRCR
);
154 writel(scr
, ssi
->base
+ SSI_SCR
);
160 * SSI system clock configuration.
161 * Should only be called when port is inactive (i.e. SSIEN = 0).
163 static int imx_ssi_set_dai_sysclk(struct snd_soc_dai
*cpu_dai
,
164 int clk_id
, unsigned int freq
, int dir
)
166 struct imx_ssi
*ssi
= snd_soc_dai_get_drvdata(cpu_dai
);
169 scr
= readl(ssi
->base
+ SSI_SCR
);
172 case IMX_SSP_SYS_CLK
:
173 if (dir
== SND_SOC_CLOCK_OUT
)
174 scr
|= SSI_SCR_SYS_CLK_EN
;
176 scr
&= ~SSI_SCR_SYS_CLK_EN
;
182 writel(scr
, ssi
->base
+ SSI_SCR
);
189 * Should only be called when port is inactive (i.e. SSIEN = 0).
191 static int imx_ssi_set_dai_clkdiv(struct snd_soc_dai
*cpu_dai
,
194 struct imx_ssi
*ssi
= snd_soc_dai_get_drvdata(cpu_dai
);
197 stccr
= readl(ssi
->base
+ SSI_STCCR
);
198 srccr
= readl(ssi
->base
+ SSI_SRCCR
);
201 case IMX_SSI_TX_DIV_2
:
202 stccr
&= ~SSI_STCCR_DIV2
;
205 case IMX_SSI_TX_DIV_PSR
:
206 stccr
&= ~SSI_STCCR_PSR
;
209 case IMX_SSI_TX_DIV_PM
:
211 stccr
|= SSI_STCCR_PM(div
);
213 case IMX_SSI_RX_DIV_2
:
214 stccr
&= ~SSI_STCCR_DIV2
;
217 case IMX_SSI_RX_DIV_PSR
:
218 stccr
&= ~SSI_STCCR_PSR
;
221 case IMX_SSI_RX_DIV_PM
:
223 stccr
|= SSI_STCCR_PM(div
);
229 writel(stccr
, ssi
->base
+ SSI_STCCR
);
230 writel(srccr
, ssi
->base
+ SSI_SRCCR
);
236 * Should only be called when port is inactive (i.e. SSIEN = 0),
237 * although can be called multiple times by upper layers.
239 static int imx_ssi_hw_params(struct snd_pcm_substream
*substream
,
240 struct snd_pcm_hw_params
*params
,
241 struct snd_soc_dai
*cpu_dai
)
243 struct imx_ssi
*ssi
= snd_soc_dai_get_drvdata(cpu_dai
);
247 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
252 if (ssi
->flags
& IMX_SSI_SYN
)
255 sccr
= readl(ssi
->base
+ reg
) & ~SSI_STCCR_WL_MASK
;
257 /* DAI data (word) size */
258 switch (params_format(params
)) {
259 case SNDRV_PCM_FORMAT_S16_LE
:
260 sccr
|= SSI_SRCCR_WL(16);
262 case SNDRV_PCM_FORMAT_S20_3LE
:
263 sccr
|= SSI_SRCCR_WL(20);
265 case SNDRV_PCM_FORMAT_S24_LE
:
266 sccr
|= SSI_SRCCR_WL(24);
270 writel(sccr
, ssi
->base
+ reg
);
275 static int imx_ssi_trigger(struct snd_pcm_substream
*substream
, int cmd
,
276 struct snd_soc_dai
*dai
)
278 struct imx_ssi
*ssi
= snd_soc_dai_get_drvdata(dai
);
279 unsigned int sier_bits
, sier
;
282 scr
= readl(ssi
->base
+ SSI_SCR
);
283 sier
= readl(ssi
->base
+ SSI_SIER
);
285 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
286 if (ssi
->flags
& IMX_SSI_DMA
)
287 sier_bits
= SSI_SIER_TDMAE
;
289 sier_bits
= SSI_SIER_TIE
| SSI_SIER_TFE0_EN
;
291 if (ssi
->flags
& IMX_SSI_DMA
)
292 sier_bits
= SSI_SIER_RDMAE
;
294 sier_bits
= SSI_SIER_RIE
| SSI_SIER_RFF0_EN
;
298 case SNDRV_PCM_TRIGGER_START
:
299 case SNDRV_PCM_TRIGGER_RESUME
:
300 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
301 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
307 scr
|= SSI_SCR_SSIEN
;
311 case SNDRV_PCM_TRIGGER_STOP
:
312 case SNDRV_PCM_TRIGGER_SUSPEND
:
313 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
314 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
320 if (!(scr
& (SSI_SCR_TE
| SSI_SCR_RE
)))
321 scr
&= ~SSI_SCR_SSIEN
;
328 if (!(ssi
->flags
& IMX_SSI_USE_AC97
))
329 /* rx/tx are always enabled to access ac97 registers */
330 writel(scr
, ssi
->base
+ SSI_SCR
);
332 writel(sier
, ssi
->base
+ SSI_SIER
);
337 static const struct snd_soc_dai_ops imx_ssi_pcm_dai_ops
= {
338 .hw_params
= imx_ssi_hw_params
,
339 .set_fmt
= imx_ssi_set_dai_fmt
,
340 .set_clkdiv
= imx_ssi_set_dai_clkdiv
,
341 .set_sysclk
= imx_ssi_set_dai_sysclk
,
342 .set_tdm_slot
= imx_ssi_set_dai_tdm_slot
,
343 .trigger
= imx_ssi_trigger
,
346 static int imx_ssi_dai_probe(struct snd_soc_dai
*dai
)
348 struct imx_ssi
*ssi
= dev_get_drvdata(dai
->dev
);
351 snd_soc_dai_set_drvdata(dai
, ssi
);
353 val
= SSI_SFCSR_TFWM0(ssi
->dma_params_tx
.maxburst
) |
354 SSI_SFCSR_RFWM0(ssi
->dma_params_rx
.maxburst
);
355 writel(val
, ssi
->base
+ SSI_SFCSR
);
358 dai
->playback_dma_data
= &ssi
->dma_params_tx
;
359 dai
->capture_dma_data
= &ssi
->dma_params_rx
;
364 static struct snd_soc_dai_driver imx_ssi_dai
= {
365 .probe
= imx_ssi_dai_probe
,
369 .rates
= SNDRV_PCM_RATE_8000_96000
,
370 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
375 .rates
= SNDRV_PCM_RATE_8000_96000
,
376 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
378 .ops
= &imx_ssi_pcm_dai_ops
,
381 static struct snd_soc_dai_driver imx_ac97_dai
= {
382 .probe
= imx_ssi_dai_probe
,
385 .stream_name
= "AC97 Playback",
388 .rates
= SNDRV_PCM_RATE_8000_48000
,
389 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
392 .stream_name
= "AC97 Capture",
395 .rates
= SNDRV_PCM_RATE_48000
,
396 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
398 .ops
= &imx_ssi_pcm_dai_ops
,
401 static const struct snd_soc_component_driver imx_component
= {
405 static void setup_channel_to_ac97(struct imx_ssi
*imx_ssi
)
407 void __iomem
*base
= imx_ssi
->base
;
409 writel(0x0, base
+ SSI_SCR
);
410 writel(0x0, base
+ SSI_STCR
);
411 writel(0x0, base
+ SSI_SRCR
);
413 writel(SSI_SCR_SYN
| SSI_SCR_NET
, base
+ SSI_SCR
);
415 writel(SSI_SFCSR_RFWM0(8) |
418 SSI_SFCSR_TFWM1(8), base
+ SSI_SFCSR
);
420 writel(SSI_STCCR_WL(16) | SSI_STCCR_DC(12), base
+ SSI_STCCR
);
421 writel(SSI_STCCR_WL(16) | SSI_STCCR_DC(12), base
+ SSI_SRCCR
);
423 writel(SSI_SCR_SYN
| SSI_SCR_NET
| SSI_SCR_SSIEN
, base
+ SSI_SCR
);
424 writel(SSI_SOR_WAIT(3), base
+ SSI_SOR
);
426 writel(SSI_SCR_SYN
| SSI_SCR_NET
| SSI_SCR_SSIEN
|
427 SSI_SCR_TE
| SSI_SCR_RE
,
430 writel(SSI_SACNT_DEFAULT
, base
+ SSI_SACNT
);
431 writel(0xff, base
+ SSI_SACCDIS
);
432 writel(0x300, base
+ SSI_SACCEN
);
435 static struct imx_ssi
*ac97_ssi
;
437 static void imx_ssi_ac97_write(struct snd_ac97
*ac97
, unsigned short reg
,
440 struct imx_ssi
*imx_ssi
= ac97_ssi
;
441 void __iomem
*base
= imx_ssi
->base
;
448 pr_debug("%s: 0x%02x 0x%04x\n", __func__
, reg
, val
);
451 writel(lreg
, base
+ SSI_SACADD
);
454 writel(lval
, base
+ SSI_SACDAT
);
456 writel(SSI_SACNT_DEFAULT
| SSI_SACNT_WR
, base
+ SSI_SACNT
);
460 static unsigned short imx_ssi_ac97_read(struct snd_ac97
*ac97
,
463 struct imx_ssi
*imx_ssi
= ac97_ssi
;
464 void __iomem
*base
= imx_ssi
->base
;
466 unsigned short val
= -1;
469 lreg
= (reg
& 0x7f) << 12 ;
470 writel(lreg
, base
+ SSI_SACADD
);
471 writel(SSI_SACNT_DEFAULT
| SSI_SACNT_RD
, base
+ SSI_SACNT
);
475 val
= (readl(base
+ SSI_SACDAT
) >> 4) & 0xffff;
477 pr_debug("%s: 0x%02x 0x%04x\n", __func__
, reg
, val
);
482 static void imx_ssi_ac97_reset(struct snd_ac97
*ac97
)
484 struct imx_ssi
*imx_ssi
= ac97_ssi
;
486 if (imx_ssi
->ac97_reset
)
487 imx_ssi
->ac97_reset(ac97
);
488 /* First read sometimes fails, do a dummy read */
489 imx_ssi_ac97_read(ac97
, 0);
492 static void imx_ssi_ac97_warm_reset(struct snd_ac97
*ac97
)
494 struct imx_ssi
*imx_ssi
= ac97_ssi
;
496 if (imx_ssi
->ac97_warm_reset
)
497 imx_ssi
->ac97_warm_reset(ac97
);
499 /* First read sometimes fails, do a dummy read */
500 imx_ssi_ac97_read(ac97
, 0);
503 static struct snd_ac97_bus_ops imx_ssi_ac97_ops
= {
504 .read
= imx_ssi_ac97_read
,
505 .write
= imx_ssi_ac97_write
,
506 .reset
= imx_ssi_ac97_reset
,
507 .warm_reset
= imx_ssi_ac97_warm_reset
510 static int imx_ssi_probe(struct platform_device
*pdev
)
512 struct resource
*res
;
514 struct imx_ssi_platform_data
*pdata
= pdev
->dev
.platform_data
;
516 struct snd_soc_dai_driver
*dai
;
518 ssi
= devm_kzalloc(&pdev
->dev
, sizeof(*ssi
), GFP_KERNEL
);
521 dev_set_drvdata(&pdev
->dev
, ssi
);
524 ssi
->ac97_reset
= pdata
->ac97_reset
;
525 ssi
->ac97_warm_reset
= pdata
->ac97_warm_reset
;
526 ssi
->flags
= pdata
->flags
;
529 ssi
->irq
= platform_get_irq(pdev
, 0);
531 dev_err(&pdev
->dev
, "Failed to get IRQ: %d\n", ssi
->irq
);
535 ssi
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
536 if (IS_ERR(ssi
->clk
)) {
537 ret
= PTR_ERR(ssi
->clk
);
538 dev_err(&pdev
->dev
, "Cannot get the clock: %d\n",
542 ret
= clk_prepare_enable(ssi
->clk
);
546 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
547 ssi
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
548 if (IS_ERR(ssi
->base
)) {
549 ret
= PTR_ERR(ssi
->base
);
550 goto failed_register
;
553 if (ssi
->flags
& IMX_SSI_USE_AC97
) {
555 dev_err(&pdev
->dev
, "AC'97 SSI already registered\n");
557 goto failed_register
;
560 setup_channel_to_ac97(ssi
);
565 writel(0x0, ssi
->base
+ SSI_SIER
);
567 ssi
->dma_params_rx
.addr
= res
->start
+ SSI_SRX0
;
568 ssi
->dma_params_tx
.addr
= res
->start
+ SSI_STX0
;
570 ssi
->dma_params_tx
.maxburst
= 6;
571 ssi
->dma_params_rx
.maxburst
= 4;
573 ssi
->dma_params_tx
.filter_data
= &ssi
->filter_data_tx
;
574 ssi
->dma_params_rx
.filter_data
= &ssi
->filter_data_rx
;
576 res
= platform_get_resource_byname(pdev
, IORESOURCE_DMA
, "tx0");
578 imx_pcm_dma_params_init_data(&ssi
->filter_data_tx
, res
->start
,
582 res
= platform_get_resource_byname(pdev
, IORESOURCE_DMA
, "rx0");
584 imx_pcm_dma_params_init_data(&ssi
->filter_data_rx
, res
->start
,
588 platform_set_drvdata(pdev
, ssi
);
590 ret
= snd_soc_set_ac97_ops(&imx_ssi_ac97_ops
);
592 dev_err(&pdev
->dev
, "Failed to set AC'97 ops: %d\n", ret
);
593 goto failed_register
;
596 ret
= snd_soc_register_component(&pdev
->dev
, &imx_component
,
599 dev_err(&pdev
->dev
, "register DAI failed\n");
600 goto failed_register
;
603 ssi
->fiq_params
.irq
= ssi
->irq
;
604 ssi
->fiq_params
.base
= ssi
->base
;
605 ssi
->fiq_params
.dma_params_rx
= &ssi
->dma_params_rx
;
606 ssi
->fiq_params
.dma_params_tx
= &ssi
->dma_params_tx
;
608 ssi
->fiq_init
= imx_pcm_fiq_init(pdev
, &ssi
->fiq_params
);
609 ssi
->dma_init
= imx_pcm_dma_init(pdev
, IMX_SSI_DMABUF_SIZE
);
611 if (ssi
->fiq_init
&& ssi
->dma_init
) {
619 snd_soc_unregister_component(&pdev
->dev
);
621 clk_disable_unprepare(ssi
->clk
);
623 snd_soc_set_ac97_ops(NULL
);
628 static int imx_ssi_remove(struct platform_device
*pdev
)
630 struct imx_ssi
*ssi
= platform_get_drvdata(pdev
);
633 imx_pcm_fiq_exit(pdev
);
635 snd_soc_unregister_component(&pdev
->dev
);
637 if (ssi
->flags
& IMX_SSI_USE_AC97
)
640 clk_disable_unprepare(ssi
->clk
);
641 snd_soc_set_ac97_ops(NULL
);
646 static struct platform_driver imx_ssi_driver
= {
647 .probe
= imx_ssi_probe
,
648 .remove
= imx_ssi_remove
,
655 module_platform_driver(imx_ssi_driver
);
657 /* Module information */
658 MODULE_AUTHOR("Sascha Hauer, <s.hauer@pengutronix.de>");
659 MODULE_DESCRIPTION("i.MX I2S/ac97 SoC Interface");
660 MODULE_LICENSE("GPL");
661 MODULE_ALIAS("platform:imx-ssi");