blob: 9d8af3a46da8bfc5b05c9db860af19dc0160750c [file] [log] [blame]
Ben Dooksc1422a62007-02-14 13:17:49 +01001/*
2 * s3c24xx-i2s.c -- ALSA Soc Audio Layer
3 *
4 * (c) 2006 Wolfson Microelectronics PLC.
5 * Graeme Gregory graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com
6 *
7 * (c) 2004-2005 Simtec Electronics
8 * http://armlinux.simtec.co.uk/
9 * Ben Dooks <ben@simtec.co.uk>
10 *
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.
15 *
16 *
17 * Revision history
18 * 11th Dec 2006 Merged with Simtec driver
19 * 10th Nov 2006 Initial version.
20 */
21
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>
Julia Lawallf11b7992008-01-07 13:33:45 +010027#include <linux/jiffies.h>
Ben Dooksc1422a62007-02-14 13:17:49 +010028#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>
34
35#include <asm/hardware.h>
36#include <asm/io.h>
Ben Dooksc1422a62007-02-14 13:17:49 +010037#include <asm/arch/regs-gpio.h>
38#include <asm/arch/regs-clock.h>
39#include <asm/arch/audio.h>
40#include <asm/dma.h>
41#include <asm/arch/dma.h>
42
Harald Welteaa9673c2007-12-19 15:37:49 +010043#include <asm/plat-s3c24xx/regs-iis.h>
44
Ben Dooksc1422a62007-02-14 13:17:49 +010045#include "s3c24xx-pcm.h"
46#include "s3c24xx-i2s.h"
47
48#define S3C24XX_I2S_DEBUG 0
49#if S3C24XX_I2S_DEBUG
50#define DBG(x...) printk(KERN_DEBUG x)
51#else
52#define DBG(x...)
53#endif
54
55static struct s3c2410_dma_client s3c24xx_dma_client_out = {
56 .name = "I2S PCM Stereo out"
57};
58
59static struct s3c2410_dma_client s3c24xx_dma_client_in = {
60 .name = "I2S PCM Stereo in"
61};
62
63static struct s3c24xx_pcm_dma_params s3c24xx_i2s_pcm_stereo_out = {
64 .client = &s3c24xx_dma_client_out,
65 .channel = DMACH_I2S_OUT,
Graeme Gregorye81208f2007-04-17 12:35:48 +020066 .dma_addr = S3C2410_PA_IIS + S3C2410_IISFIFO,
67 .dma_size = 2,
Ben Dooksc1422a62007-02-14 13:17:49 +010068};
69
70static struct s3c24xx_pcm_dma_params s3c24xx_i2s_pcm_stereo_in = {
71 .client = &s3c24xx_dma_client_in,
72 .channel = DMACH_I2S_IN,
Graeme Gregorye81208f2007-04-17 12:35:48 +020073 .dma_addr = S3C2410_PA_IIS + S3C2410_IISFIFO,
74 .dma_size = 2,
Ben Dooksc1422a62007-02-14 13:17:49 +010075};
76
77struct s3c24xx_i2s_info {
78 void __iomem *regs;
79 struct clk *iis_clk;
80};
81static struct s3c24xx_i2s_info s3c24xx_i2s;
82
83static void s3c24xx_snd_txctrl(int on)
84{
85 u32 iisfcon;
86 u32 iiscon;
87 u32 iismod;
88
89 DBG("Entered %s\n", __FUNCTION__);
90
91 iisfcon = readl(s3c24xx_i2s.regs + S3C2410_IISFCON);
92 iiscon = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
93 iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
94
95 DBG("r: IISCON: %lx IISMOD: %lx IISFCON: %lx\n", iiscon, iismod, iisfcon);
96
97 if (on) {
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;
102
103 writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
104 writel(iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
105 writel(iiscon, s3c24xx_i2s.regs + S3C2410_IISCON);
106 } else {
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.
112 */
113
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;
119
120 writel(iiscon, s3c24xx_i2s.regs + S3C2410_IISCON);
121 writel(iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
122 writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
123 }
124
125 DBG("w: IISCON: %lx IISMOD: %lx IISFCON: %lx\n", iiscon, iismod, iisfcon);
126}
127
128static void s3c24xx_snd_rxctrl(int on)
129{
130 u32 iisfcon;
131 u32 iiscon;
132 u32 iismod;
133
134 DBG("Entered %s\n", __FUNCTION__);
135
136 iisfcon = readl(s3c24xx_i2s.regs + S3C2410_IISFCON);
137 iiscon = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
138 iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
139
140 DBG("r: IISCON: %lx IISMOD: %lx IISFCON: %lx\n", iiscon, iismod, iisfcon);
141
142 if (on) {
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;
147
148 writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
149 writel(iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
150 writel(iiscon, s3c24xx_i2s.regs + S3C2410_IISCON);
151 } else {
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.
157 */
158
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;
164
165 writel(iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
166 writel(iiscon, s3c24xx_i2s.regs + S3C2410_IISCON);
167 writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
168 }
169
170 DBG("w: IISCON: %lx IISMOD: %lx IISFCON: %lx\n", iiscon, iismod, iisfcon);
171}
172
173/*
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.
176 */
177static int s3c24xx_snd_lrsync(void)
178{
179 u32 iiscon;
180 unsigned long timeout = jiffies + msecs_to_jiffies(5);
181
182 DBG("Entered %s\n", __FUNCTION__);
183
184 while (1) {
185 iiscon = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
186 if (iiscon & S3C2410_IISCON_LRINDEX)
187 break;
188
Julia Lawallf11b7992008-01-07 13:33:45 +0100189 if (time_after(jiffies, timeout))
Ben Dooksc1422a62007-02-14 13:17:49 +0100190 return -ETIMEDOUT;
191 }
192
193 return 0;
194}
195
196/*
197 * Check whether CPU is the master or slave
198 */
199static inline int s3c24xx_snd_is_clkmaster(void)
200{
201 DBG("Entered %s\n", __FUNCTION__);
202
203 return (readl(s3c24xx_i2s.regs + S3C2410_IISMOD) & S3C2410_IISMOD_SLAVE) ? 0:1;
204}
205
206/*
207 * Set S3C24xx I2S DAI format
208 */
209static int s3c24xx_i2s_set_fmt(struct snd_soc_cpu_dai *cpu_dai,
210 unsigned int fmt)
211{
212 u32 iismod;
213
214 DBG("Entered %s\n", __FUNCTION__);
215
216 iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
217 DBG("hw_params r: IISMOD: %lx \n", iismod);
218
219 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
220 case SND_SOC_DAIFMT_CBM_CFM:
221 iismod |= S3C2410_IISMOD_SLAVE;
222 break;
223 case SND_SOC_DAIFMT_CBS_CFS:
224 break;
225 default:
226 return -EINVAL;
227 }
228
229 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
230 case SND_SOC_DAIFMT_LEFT_J:
231 iismod |= S3C2410_IISMOD_MSB;
232 break;
233 case SND_SOC_DAIFMT_I2S:
234 break;
235 default:
236 return -EINVAL;
237 }
238
239 writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
240 DBG("hw_params w: IISMOD: %lx \n", iismod);
241 return 0;
242}
243
244static int s3c24xx_i2s_hw_params(struct snd_pcm_substream *substream,
245 struct snd_pcm_hw_params *params)
246{
247 struct snd_soc_pcm_runtime *rtd = substream->private_data;
248 u32 iismod;
249
250 DBG("Entered %s\n", __FUNCTION__);
251
252 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
253 rtd->dai->cpu_dai->dma_data = &s3c24xx_i2s_pcm_stereo_out;
254 else
255 rtd->dai->cpu_dai->dma_data = &s3c24xx_i2s_pcm_stereo_in;
256
257 /* Working copies of register */
258 iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
259 DBG("hw_params r: IISMOD: %lx\n", iismod);
260
261 switch (params_format(params)) {
262 case SNDRV_PCM_FORMAT_S8:
263 break;
264 case SNDRV_PCM_FORMAT_S16_LE:
265 iismod |= S3C2410_IISMOD_16BIT;
266 break;
267 }
268
269 writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
270 DBG("hw_params w: IISMOD: %lx\n", iismod);
271 return 0;
272}
273
274static int s3c24xx_i2s_trigger(struct snd_pcm_substream *substream, int cmd)
275{
276 int ret = 0;
277
278 DBG("Entered %s\n", __FUNCTION__);
279
280 switch (cmd) {
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();
286 if (ret)
287 goto exit_err;
288 }
289
290 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
291 s3c24xx_snd_rxctrl(1);
292 else
293 s3c24xx_snd_txctrl(1);
294 break;
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);
300 else
301 s3c24xx_snd_txctrl(0);
302 break;
303 default:
304 ret = -EINVAL;
305 break;
306 }
307
308exit_err:
309 return ret;
310}
311
312/*
313 * Set S3C24xx Clock source
314 */
315static int s3c24xx_i2s_set_sysclk(struct snd_soc_cpu_dai *cpu_dai,
316 int clk_id, unsigned int freq, int dir)
317{
318 u32 iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
319
320 DBG("Entered %s\n", __FUNCTION__);
321
322 iismod &= ~S3C2440_IISMOD_MPLL;
323
324 switch (clk_id) {
325 case S3C24XX_CLKSRC_PCLK:
326 break;
327 case S3C24XX_CLKSRC_MPLL:
328 iismod |= S3C2440_IISMOD_MPLL;
329 break;
330 default:
331 return -EINVAL;
332 }
333
334 writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
335 return 0;
336}
337
338/*
339 * Set S3C24xx Clock dividers
340 */
341static int s3c24xx_i2s_set_clkdiv(struct snd_soc_cpu_dai *cpu_dai,
342 int div_id, int div)
343{
344 u32 reg;
345
346 DBG("Entered %s\n", __FUNCTION__);
347
348 switch (div_id) {
Matt Reimer82fb1592007-07-12 12:27:24 +0200349 case S3C24XX_DIV_BCLK:
Ben Dooksc1422a62007-02-14 13:17:49 +0100350 reg = readl(s3c24xx_i2s.regs + S3C2410_IISMOD) & ~S3C2410_IISMOD_FS_MASK;
351 writel(reg | div, s3c24xx_i2s.regs + S3C2410_IISMOD);
352 break;
Matt Reimer82fb1592007-07-12 12:27:24 +0200353 case S3C24XX_DIV_MCLK:
Ben Dooksc1422a62007-02-14 13:17:49 +0100354 reg = readl(s3c24xx_i2s.regs + S3C2410_IISMOD) & ~(S3C2410_IISMOD_384FS);
355 writel(reg | div, s3c24xx_i2s.regs + S3C2410_IISMOD);
356 break;
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);
361 break;
362 default:
363 return -EINVAL;
364 }
365
366 return 0;
367}
368
369/*
370 * To avoid duplicating clock code, allow machine driver to
371 * get the clockrate from here.
372 */
373u32 s3c24xx_i2s_get_clockrate(void)
374{
375 return clk_get_rate(s3c24xx_i2s.iis_clk);
376}
377EXPORT_SYMBOL_GPL(s3c24xx_i2s_get_clockrate);
378
379static int s3c24xx_i2s_probe(struct platform_device *pdev)
380{
381 DBG("Entered %s\n", __FUNCTION__);
382
383 s3c24xx_i2s.regs = ioremap(S3C2410_PA_IIS, 0x100);
384 if (s3c24xx_i2s.regs == NULL)
385 return -ENXIO;
386
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");
Scott Thompson8642a4b2007-08-01 13:38:59 +0200390 iounmap(s3c24xx_i2s.regs);
Ben Dooksc1422a62007-02-14 13:17:49 +0100391 return -ENODEV;
392 }
393 clk_enable(s3c24xx_i2s.iis_clk);
394
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);
401
402 writel(S3C2410_IISCON_IISEN, s3c24xx_i2s.regs + S3C2410_IISCON);
403
404 s3c24xx_snd_txctrl(0);
405 s3c24xx_snd_rxctrl(0);
406
407 return 0;
408}
409
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)
414
415struct snd_soc_cpu_dai s3c24xx_i2s_dai = {
416 .name = "s3c24xx-i2s",
417 .id = 0,
418 .type = SND_SOC_DAI_I2S,
419 .probe = s3c24xx_i2s_probe,
420 .playback = {
421 .channels_min = 2,
422 .channels_max = 2,
423 .rates = S3C24XX_I2S_RATES,
424 .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE,},
425 .capture = {
426 .channels_min = 2,
427 .channels_max = 2,
428 .rates = S3C24XX_I2S_RATES,
429 .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE,},
430 .ops = {
431 .trigger = s3c24xx_i2s_trigger,
432 .hw_params = s3c24xx_i2s_hw_params,},
433 .dai_ops = {
434 .set_fmt = s3c24xx_i2s_set_fmt,
435 .set_clkdiv = s3c24xx_i2s_set_clkdiv,
436 .set_sysclk = s3c24xx_i2s_set_sysclk,
437 },
438};
439EXPORT_SYMBOL_GPL(s3c24xx_i2s_dai);
440
441/* Module information */
442MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
443MODULE_DESCRIPTION("s3c24xx I2S SoC Interface");
444MODULE_LICENSE("GPL");