2 * s3c24xx-i2s.c -- ALSA Soc Audio Layer
4 * (c) 2006 Wolfson Microelectronics PLC.
5 * Graeme Gregory graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com
7 * (c) 2004-2005 Simtec Electronics
8 * http://armlinux.simtec.co.uk/
9 * Ben Dooks <ben@simtec.co.uk>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/device.h>
20 #include <linux/delay.h>
21 #include <linux/clk.h>
22 #include <linux/jiffies.h>
24 #include <sound/core.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/initval.h>
28 #include <sound/soc.h>
30 #include <mach/hardware.h>
31 #include <mach/regs-gpio.h>
32 #include <mach/regs-clock.h>
33 #include <mach/audio.h>
37 #include <asm/plat-s3c24xx/regs-iis.h>
39 #include "s3c24xx-pcm.h"
40 #include "s3c24xx-i2s.h"
42 #define S3C24XX_I2S_DEBUG 0
44 #define DBG(x...) printk(KERN_DEBUG "s3c24xx-i2s: " x)
49 static struct s3c2410_dma_client s3c24xx_dma_client_out
= {
50 .name
= "I2S PCM Stereo out"
53 static struct s3c2410_dma_client s3c24xx_dma_client_in
= {
54 .name
= "I2S PCM Stereo in"
57 static struct s3c24xx_pcm_dma_params s3c24xx_i2s_pcm_stereo_out
= {
58 .client
= &s3c24xx_dma_client_out
,
59 .channel
= DMACH_I2S_OUT
,
60 .dma_addr
= S3C2410_PA_IIS
+ S3C2410_IISFIFO
,
64 static struct s3c24xx_pcm_dma_params s3c24xx_i2s_pcm_stereo_in
= {
65 .client
= &s3c24xx_dma_client_in
,
66 .channel
= DMACH_I2S_IN
,
67 .dma_addr
= S3C2410_PA_IIS
+ S3C2410_IISFIFO
,
71 struct s3c24xx_i2s_info
{
79 static struct s3c24xx_i2s_info s3c24xx_i2s
;
81 static void s3c24xx_snd_txctrl(int on
)
87 DBG("Entered %s\n", __func__
);
89 iisfcon
= readl(s3c24xx_i2s
.regs
+ S3C2410_IISFCON
);
90 iiscon
= readl(s3c24xx_i2s
.regs
+ S3C2410_IISCON
);
91 iismod
= readl(s3c24xx_i2s
.regs
+ S3C2410_IISMOD
);
93 DBG("r: IISCON: %lx IISMOD: %lx IISFCON: %lx\n", iiscon
, iismod
, iisfcon
);
96 iisfcon
|= S3C2410_IISFCON_TXDMA
| S3C2410_IISFCON_TXENABLE
;
97 iiscon
|= S3C2410_IISCON_TXDMAEN
| S3C2410_IISCON_IISEN
;
98 iiscon
&= ~S3C2410_IISCON_TXIDLE
;
99 iismod
|= S3C2410_IISMOD_TXMODE
;
101 writel(iismod
, s3c24xx_i2s
.regs
+ S3C2410_IISMOD
);
102 writel(iisfcon
, s3c24xx_i2s
.regs
+ S3C2410_IISFCON
);
103 writel(iiscon
, s3c24xx_i2s
.regs
+ S3C2410_IISCON
);
105 /* note, we have to disable the FIFOs otherwise bad things
106 * seem to happen when the DMA stops. According to the
107 * Samsung supplied kernel, this should allow the DMA
108 * engine and FIFOs to reset. If this isn't allowed, the
109 * DMA engine will simply freeze randomly.
112 iisfcon
&= ~S3C2410_IISFCON_TXENABLE
;
113 iisfcon
&= ~S3C2410_IISFCON_TXDMA
;
114 iiscon
|= S3C2410_IISCON_TXIDLE
;
115 iiscon
&= ~S3C2410_IISCON_TXDMAEN
;
116 iismod
&= ~S3C2410_IISMOD_TXMODE
;
118 writel(iiscon
, s3c24xx_i2s
.regs
+ S3C2410_IISCON
);
119 writel(iisfcon
, s3c24xx_i2s
.regs
+ S3C2410_IISFCON
);
120 writel(iismod
, s3c24xx_i2s
.regs
+ S3C2410_IISMOD
);
123 DBG("w: IISCON: %lx IISMOD: %lx IISFCON: %lx\n", iiscon
, iismod
, iisfcon
);
126 static void s3c24xx_snd_rxctrl(int on
)
132 DBG("Entered %s\n", __func__
);
134 iisfcon
= readl(s3c24xx_i2s
.regs
+ S3C2410_IISFCON
);
135 iiscon
= readl(s3c24xx_i2s
.regs
+ S3C2410_IISCON
);
136 iismod
= readl(s3c24xx_i2s
.regs
+ S3C2410_IISMOD
);
138 DBG("r: IISCON: %lx IISMOD: %lx IISFCON: %lx\n", iiscon
, iismod
, iisfcon
);
141 iisfcon
|= S3C2410_IISFCON_RXDMA
| S3C2410_IISFCON_RXENABLE
;
142 iiscon
|= S3C2410_IISCON_RXDMAEN
| S3C2410_IISCON_IISEN
;
143 iiscon
&= ~S3C2410_IISCON_RXIDLE
;
144 iismod
|= S3C2410_IISMOD_RXMODE
;
146 writel(iismod
, s3c24xx_i2s
.regs
+ S3C2410_IISMOD
);
147 writel(iisfcon
, s3c24xx_i2s
.regs
+ S3C2410_IISFCON
);
148 writel(iiscon
, s3c24xx_i2s
.regs
+ S3C2410_IISCON
);
150 /* note, we have to disable the FIFOs otherwise bad things
151 * seem to happen when the DMA stops. According to the
152 * Samsung supplied kernel, this should allow the DMA
153 * engine and FIFOs to reset. If this isn't allowed, the
154 * DMA engine will simply freeze randomly.
157 iisfcon
&= ~S3C2410_IISFCON_RXENABLE
;
158 iisfcon
&= ~S3C2410_IISFCON_RXDMA
;
159 iiscon
|= S3C2410_IISCON_RXIDLE
;
160 iiscon
&= ~S3C2410_IISCON_RXDMAEN
;
161 iismod
&= ~S3C2410_IISMOD_RXMODE
;
163 writel(iisfcon
, s3c24xx_i2s
.regs
+ S3C2410_IISFCON
);
164 writel(iiscon
, s3c24xx_i2s
.regs
+ S3C2410_IISCON
);
165 writel(iismod
, s3c24xx_i2s
.regs
+ S3C2410_IISMOD
);
168 DBG("w: IISCON: %lx IISMOD: %lx IISFCON: %lx\n", iiscon
, iismod
, iisfcon
);
172 * Wait for the LR signal to allow synchronisation to the L/R clock
173 * from the codec. May only be needed for slave mode.
175 static int s3c24xx_snd_lrsync(void)
178 int timeout
= 50; /* 5ms */
180 DBG("Entered %s\n", __func__
);
183 iiscon
= readl(s3c24xx_i2s
.regs
+ S3C2410_IISCON
);
184 if (iiscon
& S3C2410_IISCON_LRINDEX
)
196 * Check whether CPU is the master or slave
198 static inline int s3c24xx_snd_is_clkmaster(void)
200 DBG("Entered %s\n", __func__
);
202 return (readl(s3c24xx_i2s
.regs
+ S3C2410_IISMOD
) & S3C2410_IISMOD_SLAVE
) ? 0:1;
206 * Set S3C24xx I2S DAI format
208 static int s3c24xx_i2s_set_fmt(struct snd_soc_dai
*cpu_dai
,
213 DBG("Entered %s\n", __func__
);
215 iismod
= readl(s3c24xx_i2s
.regs
+ S3C2410_IISMOD
);
216 DBG("hw_params r: IISMOD: %lx \n", iismod
);
218 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
219 case SND_SOC_DAIFMT_CBM_CFM
:
220 iismod
|= S3C2410_IISMOD_SLAVE
;
222 case SND_SOC_DAIFMT_CBS_CFS
:
223 iismod
&= ~S3C2410_IISMOD_SLAVE
;
229 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
230 case SND_SOC_DAIFMT_LEFT_J
:
231 iismod
|= S3C2410_IISMOD_MSB
;
233 case SND_SOC_DAIFMT_I2S
:
234 iismod
&= ~S3C2410_IISMOD_MSB
;
240 writel(iismod
, s3c24xx_i2s
.regs
+ S3C2410_IISMOD
);
241 DBG("hw_params w: IISMOD: %lx \n", iismod
);
245 static int s3c24xx_i2s_hw_params(struct snd_pcm_substream
*substream
,
246 struct snd_pcm_hw_params
*params
)
248 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
251 DBG("Entered %s\n", __func__
);
253 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
254 rtd
->dai
->cpu_dai
->dma_data
= &s3c24xx_i2s_pcm_stereo_out
;
256 rtd
->dai
->cpu_dai
->dma_data
= &s3c24xx_i2s_pcm_stereo_in
;
258 /* Working copies of register */
259 iismod
= readl(s3c24xx_i2s
.regs
+ S3C2410_IISMOD
);
260 DBG("hw_params r: IISMOD: %lx\n", iismod
);
262 switch (params_format(params
)) {
263 case SNDRV_PCM_FORMAT_S8
:
264 iismod
&= ~S3C2410_IISMOD_16BIT
;
265 ((struct s3c24xx_pcm_dma_params
*)
266 rtd
->dai
->cpu_dai
->dma_data
)->dma_size
= 1;
268 case SNDRV_PCM_FORMAT_S16_LE
:
269 iismod
|= S3C2410_IISMOD_16BIT
;
270 ((struct s3c24xx_pcm_dma_params
*)
271 rtd
->dai
->cpu_dai
->dma_data
)->dma_size
= 2;
277 writel(iismod
, s3c24xx_i2s
.regs
+ S3C2410_IISMOD
);
278 DBG("hw_params w: IISMOD: %lx\n", iismod
);
282 static int s3c24xx_i2s_trigger(struct snd_pcm_substream
*substream
, int cmd
)
286 DBG("Entered %s\n", __func__
);
289 case SNDRV_PCM_TRIGGER_START
:
290 case SNDRV_PCM_TRIGGER_RESUME
:
291 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
292 if (!s3c24xx_snd_is_clkmaster()) {
293 ret
= s3c24xx_snd_lrsync();
298 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
299 s3c24xx_snd_rxctrl(1);
301 s3c24xx_snd_txctrl(1);
303 case SNDRV_PCM_TRIGGER_STOP
:
304 case SNDRV_PCM_TRIGGER_SUSPEND
:
305 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
306 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
307 s3c24xx_snd_rxctrl(0);
309 s3c24xx_snd_txctrl(0);
321 * Set S3C24xx Clock source
323 static int s3c24xx_i2s_set_sysclk(struct snd_soc_dai
*cpu_dai
,
324 int clk_id
, unsigned int freq
, int dir
)
326 u32 iismod
= readl(s3c24xx_i2s
.regs
+ S3C2410_IISMOD
);
328 DBG("Entered %s\n", __func__
);
330 iismod
&= ~S3C2440_IISMOD_MPLL
;
333 case S3C24XX_CLKSRC_PCLK
:
335 case S3C24XX_CLKSRC_MPLL
:
336 iismod
|= S3C2440_IISMOD_MPLL
;
342 writel(iismod
, s3c24xx_i2s
.regs
+ S3C2410_IISMOD
);
347 * Set S3C24xx Clock dividers
349 static int s3c24xx_i2s_set_clkdiv(struct snd_soc_dai
*cpu_dai
,
354 DBG("Entered %s\n", __func__
);
357 case S3C24XX_DIV_BCLK
:
358 reg
= readl(s3c24xx_i2s
.regs
+ S3C2410_IISMOD
) & ~S3C2410_IISMOD_FS_MASK
;
359 writel(reg
| div
, s3c24xx_i2s
.regs
+ S3C2410_IISMOD
);
361 case S3C24XX_DIV_MCLK
:
362 reg
= readl(s3c24xx_i2s
.regs
+ S3C2410_IISMOD
) & ~(S3C2410_IISMOD_384FS
);
363 writel(reg
| div
, s3c24xx_i2s
.regs
+ S3C2410_IISMOD
);
365 case S3C24XX_DIV_PRESCALER
:
366 writel(div
, s3c24xx_i2s
.regs
+ S3C2410_IISPSR
);
367 reg
= readl(s3c24xx_i2s
.regs
+ S3C2410_IISCON
);
368 writel(reg
| S3C2410_IISCON_PSCEN
, s3c24xx_i2s
.regs
+ S3C2410_IISCON
);
378 * To avoid duplicating clock code, allow machine driver to
379 * get the clockrate from here.
381 u32
s3c24xx_i2s_get_clockrate(void)
383 return clk_get_rate(s3c24xx_i2s
.iis_clk
);
385 EXPORT_SYMBOL_GPL(s3c24xx_i2s_get_clockrate
);
387 static int s3c24xx_i2s_probe(struct platform_device
*pdev
,
388 struct snd_soc_dai
*dai
)
390 DBG("Entered %s\n", __func__
);
392 s3c24xx_i2s
.regs
= ioremap(S3C2410_PA_IIS
, 0x100);
393 if (s3c24xx_i2s
.regs
== NULL
)
396 s3c24xx_i2s
.iis_clk
= clk_get(&pdev
->dev
, "iis");
397 if (s3c24xx_i2s
.iis_clk
== NULL
) {
398 DBG("failed to get iis_clock\n");
399 iounmap(s3c24xx_i2s
.regs
);
402 clk_enable(s3c24xx_i2s
.iis_clk
);
404 /* Configure the I2S pins in correct mode */
405 s3c2410_gpio_cfgpin(S3C2410_GPE0
, S3C2410_GPE0_I2SLRCK
);
406 s3c2410_gpio_cfgpin(S3C2410_GPE1
, S3C2410_GPE1_I2SSCLK
);
407 s3c2410_gpio_cfgpin(S3C2410_GPE2
, S3C2410_GPE2_CDCLK
);
408 s3c2410_gpio_cfgpin(S3C2410_GPE3
, S3C2410_GPE3_I2SSDI
);
409 s3c2410_gpio_cfgpin(S3C2410_GPE4
, S3C2410_GPE4_I2SSDO
);
411 writel(S3C2410_IISCON_IISEN
, s3c24xx_i2s
.regs
+ S3C2410_IISCON
);
413 s3c24xx_snd_txctrl(0);
414 s3c24xx_snd_rxctrl(0);
420 static int s3c24xx_i2s_suspend(struct platform_device
*pdev
,
421 struct snd_soc_dai
*cpu_dai
)
423 DBG("Entered %s\n", __func__
);
425 s3c24xx_i2s
.iiscon
= readl(s3c24xx_i2s
.regs
+ S3C2410_IISCON
);
426 s3c24xx_i2s
.iismod
= readl(s3c24xx_i2s
.regs
+ S3C2410_IISMOD
);
427 s3c24xx_i2s
.iisfcon
= readl(s3c24xx_i2s
.regs
+ S3C2410_IISFCON
);
428 s3c24xx_i2s
.iispsr
= readl(s3c24xx_i2s
.regs
+ S3C2410_IISPSR
);
430 clk_disable(s3c24xx_i2s
.iis_clk
);
435 static int s3c24xx_i2s_resume(struct platform_device
*pdev
,
436 struct snd_soc_dai
*cpu_dai
)
438 DBG("Entered %s\n", __func__
);
439 clk_enable(s3c24xx_i2s
.iis_clk
);
441 writel(s3c24xx_i2s
.iiscon
, s3c24xx_i2s
.regs
+ S3C2410_IISCON
);
442 writel(s3c24xx_i2s
.iismod
, s3c24xx_i2s
.regs
+ S3C2410_IISMOD
);
443 writel(s3c24xx_i2s
.iisfcon
, s3c24xx_i2s
.regs
+ S3C2410_IISFCON
);
444 writel(s3c24xx_i2s
.iispsr
, s3c24xx_i2s
.regs
+ S3C2410_IISPSR
);
449 #define s3c24xx_i2s_suspend NULL
450 #define s3c24xx_i2s_resume NULL
454 #define S3C24XX_I2S_RATES \
455 (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 | SNDRV_PCM_RATE_16000 | \
456 SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
457 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
459 struct snd_soc_dai s3c24xx_i2s_dai
= {
460 .name
= "s3c24xx-i2s",
462 .type
= SND_SOC_DAI_I2S
,
463 .probe
= s3c24xx_i2s_probe
,
464 .suspend
= s3c24xx_i2s_suspend
,
465 .resume
= s3c24xx_i2s_resume
,
469 .rates
= S3C24XX_I2S_RATES
,
470 .formats
= SNDRV_PCM_FMTBIT_S8
| SNDRV_PCM_FMTBIT_S16_LE
,},
474 .rates
= S3C24XX_I2S_RATES
,
475 .formats
= SNDRV_PCM_FMTBIT_S8
| SNDRV_PCM_FMTBIT_S16_LE
,},
477 .trigger
= s3c24xx_i2s_trigger
,
478 .hw_params
= s3c24xx_i2s_hw_params
,},
480 .set_fmt
= s3c24xx_i2s_set_fmt
,
481 .set_clkdiv
= s3c24xx_i2s_set_clkdiv
,
482 .set_sysclk
= s3c24xx_i2s_set_sysclk
,
485 EXPORT_SYMBOL_GPL(s3c24xx_i2s_dai
);
487 /* Module information */
488 MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
489 MODULE_DESCRIPTION("s3c24xx I2S SoC Interface");
490 MODULE_LICENSE("GPL");