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 * Copyright 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 <plat/audio.h>
37 #include <plat/regs-iis.h>
39 #include "s3c24xx-pcm.h"
40 #include "s3c24xx-i2s.h"
42 static struct s3c2410_dma_client s3c24xx_dma_client_out = {
43 .name = "I2S PCM Stereo out"
46 static struct s3c2410_dma_client s3c24xx_dma_client_in = {
47 .name = "I2S PCM Stereo in"
50 static struct s3c24xx_pcm_dma_params s3c24xx_i2s_pcm_stereo_out = {
51 .client = &s3c24xx_dma_client_out,
52 .channel = DMACH_I2S_OUT,
53 .dma_addr = S3C2410_PA_IIS + S3C2410_IISFIFO,
57 static struct s3c24xx_pcm_dma_params s3c24xx_i2s_pcm_stereo_in = {
58 .client = &s3c24xx_dma_client_in,
59 .channel = DMACH_I2S_IN,
60 .dma_addr = S3C2410_PA_IIS + S3C2410_IISFIFO,
64 struct s3c24xx_i2s_info {
72 static struct s3c24xx_i2s_info s3c24xx_i2s;
74 static void s3c24xx_snd_txctrl(int on)
80 pr_debug("Entered %s\n", __func__);
82 iisfcon = readl(s3c24xx_i2s.regs + S3C2410_IISFCON);
83 iiscon = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
84 iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
86 pr_debug("r: IISCON: %x IISMOD: %x IISFCON: %x\n", iiscon, iismod, iisfcon);
89 iisfcon |= S3C2410_IISFCON_TXDMA | S3C2410_IISFCON_TXENABLE;
90 iiscon |= S3C2410_IISCON_TXDMAEN | S3C2410_IISCON_IISEN;
91 iiscon &= ~S3C2410_IISCON_TXIDLE;
92 iismod |= S3C2410_IISMOD_TXMODE;
94 writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
95 writel(iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
96 writel(iiscon, s3c24xx_i2s.regs + S3C2410_IISCON);
98 /* note, we have to disable the FIFOs otherwise bad things
99 * seem to happen when the DMA stops. According to the
100 * Samsung supplied kernel, this should allow the DMA
101 * engine and FIFOs to reset. If this isn't allowed, the
102 * DMA engine will simply freeze randomly.
105 iisfcon &= ~S3C2410_IISFCON_TXENABLE;
106 iisfcon &= ~S3C2410_IISFCON_TXDMA;
107 iiscon |= S3C2410_IISCON_TXIDLE;
108 iiscon &= ~S3C2410_IISCON_TXDMAEN;
109 iismod &= ~S3C2410_IISMOD_TXMODE;
111 writel(iiscon, s3c24xx_i2s.regs + S3C2410_IISCON);
112 writel(iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
113 writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
116 pr_debug("w: IISCON: %x IISMOD: %x IISFCON: %x\n", iiscon, iismod, iisfcon);
119 static void s3c24xx_snd_rxctrl(int on)
125 pr_debug("Entered %s\n", __func__);
127 iisfcon = readl(s3c24xx_i2s.regs + S3C2410_IISFCON);
128 iiscon = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
129 iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
131 pr_debug("r: IISCON: %x IISMOD: %x IISFCON: %x\n", iiscon, iismod, iisfcon);
134 iisfcon |= S3C2410_IISFCON_RXDMA | S3C2410_IISFCON_RXENABLE;
135 iiscon |= S3C2410_IISCON_RXDMAEN | S3C2410_IISCON_IISEN;
136 iiscon &= ~S3C2410_IISCON_RXIDLE;
137 iismod |= S3C2410_IISMOD_RXMODE;
139 writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
140 writel(iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
141 writel(iiscon, s3c24xx_i2s.regs + S3C2410_IISCON);
143 /* note, we have to disable the FIFOs otherwise bad things
144 * seem to happen when the DMA stops. According to the
145 * Samsung supplied kernel, this should allow the DMA
146 * engine and FIFOs to reset. If this isn't allowed, the
147 * DMA engine will simply freeze randomly.
150 iisfcon &= ~S3C2410_IISFCON_RXENABLE;
151 iisfcon &= ~S3C2410_IISFCON_RXDMA;
152 iiscon |= S3C2410_IISCON_RXIDLE;
153 iiscon &= ~S3C2410_IISCON_RXDMAEN;
154 iismod &= ~S3C2410_IISMOD_RXMODE;
156 writel(iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
157 writel(iiscon, s3c24xx_i2s.regs + S3C2410_IISCON);
158 writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
161 pr_debug("w: IISCON: %x IISMOD: %x IISFCON: %x\n", iiscon, iismod, iisfcon);
165 * Wait for the LR signal to allow synchronisation to the L/R clock
166 * from the codec. May only be needed for slave mode.
168 static int s3c24xx_snd_lrsync(void)
171 int timeout = 50; /* 5ms */
173 pr_debug("Entered %s\n", __func__);
176 iiscon = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
177 if (iiscon & S3C2410_IISCON_LRINDEX)
189 * Check whether CPU is the master or slave
191 static inline int s3c24xx_snd_is_clkmaster(void)
193 pr_debug("Entered %s\n", __func__);
195 return (readl(s3c24xx_i2s.regs + S3C2410_IISMOD) & S3C2410_IISMOD_SLAVE) ? 0:1;
199 * Set S3C24xx I2S DAI format
201 static int s3c24xx_i2s_set_fmt(struct snd_soc_dai *cpu_dai,
206 pr_debug("Entered %s\n", __func__);
208 iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
209 pr_debug("hw_params r: IISMOD: %x \n", iismod);
211 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
212 case SND_SOC_DAIFMT_CBM_CFM:
213 iismod |= S3C2410_IISMOD_SLAVE;
215 case SND_SOC_DAIFMT_CBS_CFS:
216 iismod &= ~S3C2410_IISMOD_SLAVE;
222 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
223 case SND_SOC_DAIFMT_LEFT_J:
224 iismod |= S3C2410_IISMOD_MSB;
226 case SND_SOC_DAIFMT_I2S:
227 iismod &= ~S3C2410_IISMOD_MSB;
233 writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
234 pr_debug("hw_params w: IISMOD: %x \n", iismod);
238 static int s3c24xx_i2s_hw_params(struct snd_pcm_substream *substream,
239 struct snd_pcm_hw_params *params,
240 struct snd_soc_dai *dai)
242 struct snd_soc_pcm_runtime *rtd = substream->private_data;
245 pr_debug("Entered %s\n", __func__);
247 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
248 rtd->dai->cpu_dai->dma_data = &s3c24xx_i2s_pcm_stereo_out;
250 rtd->dai->cpu_dai->dma_data = &s3c24xx_i2s_pcm_stereo_in;
252 /* Working copies of register */
253 iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
254 pr_debug("hw_params r: IISMOD: %x\n", iismod);
256 switch (params_format(params)) {
257 case SNDRV_PCM_FORMAT_S8:
258 iismod &= ~S3C2410_IISMOD_16BIT;
259 ((struct s3c24xx_pcm_dma_params *)
260 rtd->dai->cpu_dai->dma_data)->dma_size = 1;
262 case SNDRV_PCM_FORMAT_S16_LE:
263 iismod |= S3C2410_IISMOD_16BIT;
264 ((struct s3c24xx_pcm_dma_params *)
265 rtd->dai->cpu_dai->dma_data)->dma_size = 2;
271 writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
272 pr_debug("hw_params w: IISMOD: %x\n", iismod);
276 static int s3c24xx_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
277 struct snd_soc_dai *dai)
281 pr_debug("Entered %s\n", __func__);
284 case SNDRV_PCM_TRIGGER_START:
285 case SNDRV_PCM_TRIGGER_RESUME:
286 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
287 if (!s3c24xx_snd_is_clkmaster()) {
288 ret = s3c24xx_snd_lrsync();
293 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
294 s3c24xx_snd_rxctrl(1);
296 s3c24xx_snd_txctrl(1);
298 case SNDRV_PCM_TRIGGER_STOP:
299 case SNDRV_PCM_TRIGGER_SUSPEND:
300 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
301 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
302 s3c24xx_snd_rxctrl(0);
304 s3c24xx_snd_txctrl(0);
316 * Set S3C24xx Clock source
318 static int s3c24xx_i2s_set_sysclk(struct snd_soc_dai *cpu_dai,
319 int clk_id, unsigned int freq, int dir)
321 u32 iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
323 pr_debug("Entered %s\n", __func__);
325 iismod &= ~S3C2440_IISMOD_MPLL;
328 case S3C24XX_CLKSRC_PCLK:
330 case S3C24XX_CLKSRC_MPLL:
331 iismod |= S3C2440_IISMOD_MPLL;
337 writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
342 * Set S3C24xx Clock dividers
344 static int s3c24xx_i2s_set_clkdiv(struct snd_soc_dai *cpu_dai,
349 pr_debug("Entered %s\n", __func__);
352 case S3C24XX_DIV_BCLK:
353 reg = readl(s3c24xx_i2s.regs + S3C2410_IISMOD) & ~S3C2410_IISMOD_FS_MASK;
354 writel(reg | div, s3c24xx_i2s.regs + S3C2410_IISMOD);
356 case S3C24XX_DIV_MCLK:
357 reg = readl(s3c24xx_i2s.regs + S3C2410_IISMOD) & ~(S3C2410_IISMOD_384FS);
358 writel(reg | div, s3c24xx_i2s.regs + S3C2410_IISMOD);
360 case S3C24XX_DIV_PRESCALER:
361 writel(div, s3c24xx_i2s.regs + S3C2410_IISPSR);
362 reg = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
363 writel(reg | S3C2410_IISCON_PSCEN, s3c24xx_i2s.regs + S3C2410_IISCON);
373 * To avoid duplicating clock code, allow machine driver to
374 * get the clockrate from here.
376 u32 s3c24xx_i2s_get_clockrate(void)
378 return clk_get_rate(s3c24xx_i2s.iis_clk);
380 EXPORT_SYMBOL_GPL(s3c24xx_i2s_get_clockrate);
382 static int s3c24xx_i2s_probe(struct platform_device *pdev,
383 struct snd_soc_dai *dai)
385 pr_debug("Entered %s\n", __func__);
387 s3c24xx_i2s.regs = ioremap(S3C2410_PA_IIS, 0x100);
388 if (s3c24xx_i2s.regs == NULL)
391 s3c24xx_i2s.iis_clk = clk_get(&pdev->dev, "iis");
392 if (s3c24xx_i2s.iis_clk == NULL) {
393 pr_err("failed to get iis_clock\n");
394 iounmap(s3c24xx_i2s.regs);
397 clk_enable(s3c24xx_i2s.iis_clk);
399 /* Configure the I2S pins in correct mode */
400 s3c2410_gpio_cfgpin(S3C2410_GPE0, S3C2410_GPE0_I2SLRCK);
401 s3c2410_gpio_cfgpin(S3C2410_GPE1, S3C2410_GPE1_I2SSCLK);
402 s3c2410_gpio_cfgpin(S3C2410_GPE2, S3C2410_GPE2_CDCLK);
403 s3c2410_gpio_cfgpin(S3C2410_GPE3, S3C2410_GPE3_I2SSDI);
404 s3c2410_gpio_cfgpin(S3C2410_GPE4, S3C2410_GPE4_I2SSDO);
406 writel(S3C2410_IISCON_IISEN, s3c24xx_i2s.regs + S3C2410_IISCON);
408 s3c24xx_snd_txctrl(0);
409 s3c24xx_snd_rxctrl(0);
415 static int s3c24xx_i2s_suspend(struct snd_soc_dai *cpu_dai)
417 pr_debug("Entered %s\n", __func__);
419 s3c24xx_i2s.iiscon = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
420 s3c24xx_i2s.iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
421 s3c24xx_i2s.iisfcon = readl(s3c24xx_i2s.regs + S3C2410_IISFCON);
422 s3c24xx_i2s.iispsr = readl(s3c24xx_i2s.regs + S3C2410_IISPSR);
424 clk_disable(s3c24xx_i2s.iis_clk);
429 static int s3c24xx_i2s_resume(struct snd_soc_dai *cpu_dai)
431 pr_debug("Entered %s\n", __func__);
432 clk_enable(s3c24xx_i2s.iis_clk);
434 writel(s3c24xx_i2s.iiscon, s3c24xx_i2s.regs + S3C2410_IISCON);
435 writel(s3c24xx_i2s.iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
436 writel(s3c24xx_i2s.iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
437 writel(s3c24xx_i2s.iispsr, s3c24xx_i2s.regs + S3C2410_IISPSR);
442 #define s3c24xx_i2s_suspend NULL
443 #define s3c24xx_i2s_resume NULL
447 #define S3C24XX_I2S_RATES \
448 (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 | SNDRV_PCM_RATE_16000 | \
449 SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
450 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
452 static struct snd_soc_dai_ops s3c24xx_i2s_dai_ops = {
453 .trigger = s3c24xx_i2s_trigger,
454 .hw_params = s3c24xx_i2s_hw_params,
455 .set_fmt = s3c24xx_i2s_set_fmt,
456 .set_clkdiv = s3c24xx_i2s_set_clkdiv,
457 .set_sysclk = s3c24xx_i2s_set_sysclk,
460 struct snd_soc_dai s3c24xx_i2s_dai = {
461 .name = "s3c24xx-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,},
476 .ops = &s3c24xx_i2s_dai_ops,
478 EXPORT_SYMBOL_GPL(s3c24xx_i2s_dai);
480 static int __init s3c24xx_i2s_init(void)
482 return snd_soc_register_dai(&s3c24xx_i2s_dai);
484 module_init(s3c24xx_i2s_init);
486 static void __exit s3c24xx_i2s_exit(void)
488 snd_soc_unregister_dai(&s3c24xx_i2s_dai);
490 module_exit(s3c24xx_i2s_exit);
492 /* Module information */
493 MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
494 MODULE_DESCRIPTION("s3c24xx I2S SoC Interface");
495 MODULE_LICENSE("GPL");