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.
18 * 11th Dec 2006 Merged with Simtec driver
19 * 10th Nov 2006 Initial version.
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/device.h>
25 #include <linux/delay.h>
26 #include <linux/clk.h>
27 #include <linux/jiffies.h>
28 #include <sound/driver.h>
29 #include <sound/core.h>
30 #include <sound/pcm.h>
31 #include <sound/pcm_params.h>
32 #include <sound/initval.h>
33 #include <sound/soc.h>
35 #include <asm/hardware.h>
37 #include <asm/arch/regs-gpio.h>
38 #include <asm/arch/regs-clock.h>
39 #include <asm/arch/audio.h>
41 #include <asm/arch/dma.h>
43 #include <asm/plat-s3c24xx/regs-iis.h>
45 #include "s3c24xx-pcm.h"
46 #include "s3c24xx-i2s.h"
48 #define S3C24XX_I2S_DEBUG 0
50 #define DBG(x...) printk(KERN_DEBUG x)
55 static struct s3c2410_dma_client s3c24xx_dma_client_out = {
56 .name = "I2S PCM Stereo out"
59 static struct s3c2410_dma_client s3c24xx_dma_client_in = {
60 .name = "I2S PCM Stereo in"
63 static struct s3c24xx_pcm_dma_params s3c24xx_i2s_pcm_stereo_out = {
64 .client = &s3c24xx_dma_client_out,
65 .channel = DMACH_I2S_OUT,
66 .dma_addr = S3C2410_PA_IIS + S3C2410_IISFIFO,
70 static struct s3c24xx_pcm_dma_params s3c24xx_i2s_pcm_stereo_in = {
71 .client = &s3c24xx_dma_client_in,
72 .channel = DMACH_I2S_IN,
73 .dma_addr = S3C2410_PA_IIS + S3C2410_IISFIFO,
77 struct s3c24xx_i2s_info {
81 static struct s3c24xx_i2s_info s3c24xx_i2s;
83 static void s3c24xx_snd_txctrl(int on)
89 DBG("Entered %s\n", __FUNCTION__);
91 iisfcon = readl(s3c24xx_i2s.regs + S3C2410_IISFCON);
92 iiscon = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
93 iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
95 DBG("r: IISCON: %lx IISMOD: %lx IISFCON: %lx\n", iiscon, iismod, iisfcon);
98 iisfcon |= S3C2410_IISFCON_TXDMA | S3C2410_IISFCON_TXENABLE;
99 iiscon |= S3C2410_IISCON_TXDMAEN | S3C2410_IISCON_IISEN;
100 iiscon &= ~S3C2410_IISCON_TXIDLE;
101 iismod |= S3C2410_IISMOD_TXMODE;
103 writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
104 writel(iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
105 writel(iiscon, s3c24xx_i2s.regs + S3C2410_IISCON);
107 /* note, we have to disable the FIFOs otherwise bad things
108 * seem to happen when the DMA stops. According to the
109 * Samsung supplied kernel, this should allow the DMA
110 * engine and FIFOs to reset. If this isn't allowed, the
111 * DMA engine will simply freeze randomly.
114 iisfcon &= ~S3C2410_IISFCON_TXENABLE;
115 iisfcon &= ~S3C2410_IISFCON_TXDMA;
116 iiscon |= S3C2410_IISCON_TXIDLE;
117 iiscon &= ~S3C2410_IISCON_TXDMAEN;
118 iismod &= ~S3C2410_IISMOD_TXMODE;
120 writel(iiscon, s3c24xx_i2s.regs + S3C2410_IISCON);
121 writel(iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
122 writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
125 DBG("w: IISCON: %lx IISMOD: %lx IISFCON: %lx\n", iiscon, iismod, iisfcon);
128 static void s3c24xx_snd_rxctrl(int on)
134 DBG("Entered %s\n", __FUNCTION__);
136 iisfcon = readl(s3c24xx_i2s.regs + S3C2410_IISFCON);
137 iiscon = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
138 iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
140 DBG("r: IISCON: %lx IISMOD: %lx IISFCON: %lx\n", iiscon, iismod, iisfcon);
143 iisfcon |= S3C2410_IISFCON_RXDMA | S3C2410_IISFCON_RXENABLE;
144 iiscon |= S3C2410_IISCON_RXDMAEN | S3C2410_IISCON_IISEN;
145 iiscon &= ~S3C2410_IISCON_RXIDLE;
146 iismod |= S3C2410_IISMOD_RXMODE;
148 writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
149 writel(iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
150 writel(iiscon, s3c24xx_i2s.regs + S3C2410_IISCON);
152 /* note, we have to disable the FIFOs otherwise bad things
153 * seem to happen when the DMA stops. According to the
154 * Samsung supplied kernel, this should allow the DMA
155 * engine and FIFOs to reset. If this isn't allowed, the
156 * DMA engine will simply freeze randomly.
159 iisfcon &= ~S3C2410_IISFCON_RXENABLE;
160 iisfcon &= ~S3C2410_IISFCON_RXDMA;
161 iiscon |= S3C2410_IISCON_RXIDLE;
162 iiscon &= ~S3C2410_IISCON_RXDMAEN;
163 iismod &= ~S3C2410_IISMOD_RXMODE;
165 writel(iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
166 writel(iiscon, s3c24xx_i2s.regs + S3C2410_IISCON);
167 writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
170 DBG("w: IISCON: %lx IISMOD: %lx IISFCON: %lx\n", iiscon, iismod, iisfcon);
174 * Wait for the LR signal to allow synchronisation to the L/R clock
175 * from the codec. May only be needed for slave mode.
177 static int s3c24xx_snd_lrsync(void)
180 unsigned long timeout = jiffies + msecs_to_jiffies(5);
182 DBG("Entered %s\n", __FUNCTION__);
185 iiscon = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
186 if (iiscon & S3C2410_IISCON_LRINDEX)
189 if (time_after(jiffies, timeout))
197 * Check whether CPU is the master or slave
199 static inline int s3c24xx_snd_is_clkmaster(void)
201 DBG("Entered %s\n", __FUNCTION__);
203 return (readl(s3c24xx_i2s.regs + S3C2410_IISMOD) & S3C2410_IISMOD_SLAVE) ? 0:1;
207 * Set S3C24xx I2S DAI format
209 static int s3c24xx_i2s_set_fmt(struct snd_soc_cpu_dai *cpu_dai,
214 DBG("Entered %s\n", __FUNCTION__);
216 iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
217 DBG("hw_params r: IISMOD: %lx \n", iismod);
219 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
220 case SND_SOC_DAIFMT_CBM_CFM:
221 iismod |= S3C2410_IISMOD_SLAVE;
223 case SND_SOC_DAIFMT_CBS_CFS:
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:
239 writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
240 DBG("hw_params w: IISMOD: %lx \n", iismod);
244 static int s3c24xx_i2s_hw_params(struct snd_pcm_substream *substream,
245 struct snd_pcm_hw_params *params)
247 struct snd_soc_pcm_runtime *rtd = substream->private_data;
250 DBG("Entered %s\n", __FUNCTION__);
252 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
253 rtd->dai->cpu_dai->dma_data = &s3c24xx_i2s_pcm_stereo_out;
255 rtd->dai->cpu_dai->dma_data = &s3c24xx_i2s_pcm_stereo_in;
257 /* Working copies of register */
258 iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
259 DBG("hw_params r: IISMOD: %lx\n", iismod);
261 switch (params_format(params)) {
262 case SNDRV_PCM_FORMAT_S8:
264 case SNDRV_PCM_FORMAT_S16_LE:
265 iismod |= S3C2410_IISMOD_16BIT;
269 writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
270 DBG("hw_params w: IISMOD: %lx\n", iismod);
274 static int s3c24xx_i2s_trigger(struct snd_pcm_substream *substream, int cmd)
278 DBG("Entered %s\n", __FUNCTION__);
281 case SNDRV_PCM_TRIGGER_START:
282 case SNDRV_PCM_TRIGGER_RESUME:
283 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
284 if (!s3c24xx_snd_is_clkmaster()) {
285 ret = s3c24xx_snd_lrsync();
290 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
291 s3c24xx_snd_rxctrl(1);
293 s3c24xx_snd_txctrl(1);
295 case SNDRV_PCM_TRIGGER_STOP:
296 case SNDRV_PCM_TRIGGER_SUSPEND:
297 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
298 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
299 s3c24xx_snd_rxctrl(0);
301 s3c24xx_snd_txctrl(0);
313 * Set S3C24xx Clock source
315 static int s3c24xx_i2s_set_sysclk(struct snd_soc_cpu_dai *cpu_dai,
316 int clk_id, unsigned int freq, int dir)
318 u32 iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
320 DBG("Entered %s\n", __FUNCTION__);
322 iismod &= ~S3C2440_IISMOD_MPLL;
325 case S3C24XX_CLKSRC_PCLK:
327 case S3C24XX_CLKSRC_MPLL:
328 iismod |= S3C2440_IISMOD_MPLL;
334 writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
339 * Set S3C24xx Clock dividers
341 static int s3c24xx_i2s_set_clkdiv(struct snd_soc_cpu_dai *cpu_dai,
346 DBG("Entered %s\n", __FUNCTION__);
349 case S3C24XX_DIV_BCLK:
350 reg = readl(s3c24xx_i2s.regs + S3C2410_IISMOD) & ~S3C2410_IISMOD_FS_MASK;
351 writel(reg | div, s3c24xx_i2s.regs + S3C2410_IISMOD);
353 case S3C24XX_DIV_MCLK:
354 reg = readl(s3c24xx_i2s.regs + S3C2410_IISMOD) & ~(S3C2410_IISMOD_384FS);
355 writel(reg | div, s3c24xx_i2s.regs + S3C2410_IISMOD);
357 case S3C24XX_DIV_PRESCALER:
358 writel(div, s3c24xx_i2s.regs + S3C2410_IISPSR);
359 reg = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
360 writel(reg | S3C2410_IISCON_PSCEN, s3c24xx_i2s.regs + S3C2410_IISCON);
370 * To avoid duplicating clock code, allow machine driver to
371 * get the clockrate from here.
373 u32 s3c24xx_i2s_get_clockrate(void)
375 return clk_get_rate(s3c24xx_i2s.iis_clk);
377 EXPORT_SYMBOL_GPL(s3c24xx_i2s_get_clockrate);
379 static int s3c24xx_i2s_probe(struct platform_device *pdev)
381 DBG("Entered %s\n", __FUNCTION__);
383 s3c24xx_i2s.regs = ioremap(S3C2410_PA_IIS, 0x100);
384 if (s3c24xx_i2s.regs == NULL)
387 s3c24xx_i2s.iis_clk=clk_get(&pdev->dev, "iis");
388 if (s3c24xx_i2s.iis_clk == NULL) {
389 DBG("failed to get iis_clock\n");
390 iounmap(s3c24xx_i2s.regs);
393 clk_enable(s3c24xx_i2s.iis_clk);
395 /* Configure the I2S pins in correct mode */
396 s3c2410_gpio_cfgpin(S3C2410_GPE0, S3C2410_GPE0_I2SLRCK);
397 s3c2410_gpio_cfgpin(S3C2410_GPE1, S3C2410_GPE1_I2SSCLK);
398 s3c2410_gpio_cfgpin(S3C2410_GPE2, S3C2410_GPE2_CDCLK);
399 s3c2410_gpio_cfgpin(S3C2410_GPE3, S3C2410_GPE3_I2SSDI);
400 s3c2410_gpio_cfgpin(S3C2410_GPE4, S3C2410_GPE4_I2SSDO);
402 writel(S3C2410_IISCON_IISEN, s3c24xx_i2s.regs + S3C2410_IISCON);
404 s3c24xx_snd_txctrl(0);
405 s3c24xx_snd_rxctrl(0);
410 #define S3C24XX_I2S_RATES \
411 (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 | SNDRV_PCM_RATE_16000 | \
412 SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
413 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
415 struct snd_soc_cpu_dai s3c24xx_i2s_dai = {
416 .name = "s3c24xx-i2s",
418 .type = SND_SOC_DAI_I2S,
419 .probe = s3c24xx_i2s_probe,
423 .rates = S3C24XX_I2S_RATES,
424 .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE,},
428 .rates = S3C24XX_I2S_RATES,
429 .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE,},
431 .trigger = s3c24xx_i2s_trigger,
432 .hw_params = s3c24xx_i2s_hw_params,},
434 .set_fmt = s3c24xx_i2s_set_fmt,
435 .set_clkdiv = s3c24xx_i2s_set_clkdiv,
436 .set_sysclk = s3c24xx_i2s_set_sysclk,
439 EXPORT_SYMBOL_GPL(s3c24xx_i2s_dai);
441 /* Module information */
442 MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
443 MODULE_DESCRIPTION("s3c24xx I2S SoC Interface");
444 MODULE_LICENSE("GPL");