ASoC: TLV320AIC23B Support more sample rates
[deliverable/linux.git] / sound / soc / s3c24xx / s3c24xx-i2s.c
CommitLineData
c1422a66
BD
1/*
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.
c1422a66
BD
15 */
16
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>
f11b7992 22#include <linux/jiffies.h>
40efc15f 23#include <linux/io.h>
c1422a66
BD
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>
29
a09e64fb
RK
30#include <mach/hardware.h>
31#include <mach/regs-gpio.h>
32#include <mach/regs-clock.h>
33#include <mach/audio.h>
c1422a66 34#include <asm/dma.h>
a09e64fb 35#include <mach/dma.h>
c1422a66 36
aa9673cf
HW
37#include <asm/plat-s3c24xx/regs-iis.h>
38
c1422a66
BD
39#include "s3c24xx-pcm.h"
40#include "s3c24xx-i2s.h"
41
42#define S3C24XX_I2S_DEBUG 0
43#if S3C24XX_I2S_DEBUG
40920307 44#define DBG(x...) printk(KERN_DEBUG "s3c24xx-i2s: " x)
c1422a66
BD
45#else
46#define DBG(x...)
47#endif
48
49static struct s3c2410_dma_client s3c24xx_dma_client_out = {
50 .name = "I2S PCM Stereo out"
51};
52
53static struct s3c2410_dma_client s3c24xx_dma_client_in = {
54 .name = "I2S PCM Stereo in"
55};
56
57static struct s3c24xx_pcm_dma_params s3c24xx_i2s_pcm_stereo_out = {
58 .client = &s3c24xx_dma_client_out,
59 .channel = DMACH_I2S_OUT,
e81208fe
GG
60 .dma_addr = S3C2410_PA_IIS + S3C2410_IISFIFO,
61 .dma_size = 2,
c1422a66
BD
62};
63
64static struct s3c24xx_pcm_dma_params s3c24xx_i2s_pcm_stereo_in = {
65 .client = &s3c24xx_dma_client_in,
66 .channel = DMACH_I2S_IN,
e81208fe
GG
67 .dma_addr = S3C2410_PA_IIS + S3C2410_IISFIFO,
68 .dma_size = 2,
c1422a66
BD
69};
70
71struct s3c24xx_i2s_info {
72 void __iomem *regs;
73 struct clk *iis_clk;
5cd919a2
GG
74 u32 iiscon;
75 u32 iismod;
76 u32 iisfcon;
77 u32 iispsr;
c1422a66
BD
78};
79static struct s3c24xx_i2s_info s3c24xx_i2s;
80
81static void s3c24xx_snd_txctrl(int on)
82{
83 u32 iisfcon;
84 u32 iiscon;
85 u32 iismod;
86
9bf8e7dd 87 DBG("Entered %s\n", __func__);
c1422a66
BD
88
89 iisfcon = readl(s3c24xx_i2s.regs + S3C2410_IISFCON);
90 iiscon = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
91 iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
92
93 DBG("r: IISCON: %lx IISMOD: %lx IISFCON: %lx\n", iiscon, iismod, iisfcon);
94
95 if (on) {
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;
100
101 writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
102 writel(iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
103 writel(iiscon, s3c24xx_i2s.regs + S3C2410_IISCON);
104 } else {
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.
110 */
111
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;
117
118 writel(iiscon, s3c24xx_i2s.regs + S3C2410_IISCON);
119 writel(iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
120 writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
121 }
122
123 DBG("w: IISCON: %lx IISMOD: %lx IISFCON: %lx\n", iiscon, iismod, iisfcon);
124}
125
126static void s3c24xx_snd_rxctrl(int on)
127{
128 u32 iisfcon;
129 u32 iiscon;
130 u32 iismod;
131
9bf8e7dd 132 DBG("Entered %s\n", __func__);
c1422a66
BD
133
134 iisfcon = readl(s3c24xx_i2s.regs + S3C2410_IISFCON);
135 iiscon = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
136 iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
137
138 DBG("r: IISCON: %lx IISMOD: %lx IISFCON: %lx\n", iiscon, iismod, iisfcon);
139
140 if (on) {
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;
145
146 writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
147 writel(iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
148 writel(iiscon, s3c24xx_i2s.regs + S3C2410_IISCON);
149 } else {
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.
155 */
156
0015e7d1
MB
157 iisfcon &= ~S3C2410_IISFCON_RXENABLE;
158 iisfcon &= ~S3C2410_IISFCON_RXDMA;
159 iiscon |= S3C2410_IISCON_RXIDLE;
160 iiscon &= ~S3C2410_IISCON_RXDMAEN;
c1422a66
BD
161 iismod &= ~S3C2410_IISMOD_RXMODE;
162
163 writel(iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
164 writel(iiscon, s3c24xx_i2s.regs + S3C2410_IISCON);
165 writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
166 }
167
168 DBG("w: IISCON: %lx IISMOD: %lx IISFCON: %lx\n", iiscon, iismod, iisfcon);
169}
170
171/*
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.
174 */
175static int s3c24xx_snd_lrsync(void)
176{
177 u32 iiscon;
33e5b222 178 int timeout = 50; /* 5ms */
c1422a66 179
9bf8e7dd 180 DBG("Entered %s\n", __func__);
c1422a66
BD
181
182 while (1) {
183 iiscon = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
184 if (iiscon & S3C2410_IISCON_LRINDEX)
185 break;
186
33e5b222 187 if (!timeout--)
c1422a66 188 return -ETIMEDOUT;
33e5b222 189 udelay(100);
c1422a66
BD
190 }
191
192 return 0;
193}
194
195/*
196 * Check whether CPU is the master or slave
197 */
198static inline int s3c24xx_snd_is_clkmaster(void)
199{
9bf8e7dd 200 DBG("Entered %s\n", __func__);
c1422a66
BD
201
202 return (readl(s3c24xx_i2s.regs + S3C2410_IISMOD) & S3C2410_IISMOD_SLAVE) ? 0:1;
203}
204
205/*
206 * Set S3C24xx I2S DAI format
207 */
1992a6fb 208static int s3c24xx_i2s_set_fmt(struct snd_soc_dai *cpu_dai,
c1422a66
BD
209 unsigned int fmt)
210{
211 u32 iismod;
212
9bf8e7dd 213 DBG("Entered %s\n", __func__);
c1422a66
BD
214
215 iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
216 DBG("hw_params r: IISMOD: %lx \n", iismod);
217
218 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
219 case SND_SOC_DAIFMT_CBM_CFM:
220 iismod |= S3C2410_IISMOD_SLAVE;
221 break;
222 case SND_SOC_DAIFMT_CBS_CFS:
2c36eecf 223 iismod &= ~S3C2410_IISMOD_SLAVE;
c1422a66
BD
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:
2c36eecf 234 iismod &= ~S3C2410_IISMOD_MSB;
c1422a66
BD
235 break;
236 default:
237 return -EINVAL;
238 }
239
240 writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
241 DBG("hw_params w: IISMOD: %lx \n", iismod);
242 return 0;
243}
244
245static int s3c24xx_i2s_hw_params(struct snd_pcm_substream *substream,
246 struct snd_pcm_hw_params *params)
247{
248 struct snd_soc_pcm_runtime *rtd = substream->private_data;
249 u32 iismod;
250
9bf8e7dd 251 DBG("Entered %s\n", __func__);
c1422a66
BD
252
253 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
254 rtd->dai->cpu_dai->dma_data = &s3c24xx_i2s_pcm_stereo_out;
255 else
256 rtd->dai->cpu_dai->dma_data = &s3c24xx_i2s_pcm_stereo_in;
257
258 /* Working copies of register */
259 iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
260 DBG("hw_params r: IISMOD: %lx\n", iismod);
261
262 switch (params_format(params)) {
263 case SNDRV_PCM_FORMAT_S8:
264 break;
265 case SNDRV_PCM_FORMAT_S16_LE:
266 iismod |= S3C2410_IISMOD_16BIT;
267 break;
268 }
269
270 writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
271 DBG("hw_params w: IISMOD: %lx\n", iismod);
272 return 0;
273}
274
275static int s3c24xx_i2s_trigger(struct snd_pcm_substream *substream, int cmd)
276{
277 int ret = 0;
278
9bf8e7dd 279 DBG("Entered %s\n", __func__);
c1422a66
BD
280
281 switch (cmd) {
282 case SNDRV_PCM_TRIGGER_START:
283 case SNDRV_PCM_TRIGGER_RESUME:
284 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
285 if (!s3c24xx_snd_is_clkmaster()) {
286 ret = s3c24xx_snd_lrsync();
287 if (ret)
288 goto exit_err;
289 }
290
291 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
292 s3c24xx_snd_rxctrl(1);
293 else
294 s3c24xx_snd_txctrl(1);
295 break;
296 case SNDRV_PCM_TRIGGER_STOP:
297 case SNDRV_PCM_TRIGGER_SUSPEND:
298 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
299 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
300 s3c24xx_snd_rxctrl(0);
301 else
302 s3c24xx_snd_txctrl(0);
303 break;
304 default:
305 ret = -EINVAL;
306 break;
307 }
308
309exit_err:
310 return ret;
311}
312
313/*
314 * Set S3C24xx Clock source
315 */
1992a6fb 316static int s3c24xx_i2s_set_sysclk(struct snd_soc_dai *cpu_dai,
c1422a66
BD
317 int clk_id, unsigned int freq, int dir)
318{
319 u32 iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
320
9bf8e7dd 321 DBG("Entered %s\n", __func__);
c1422a66
BD
322
323 iismod &= ~S3C2440_IISMOD_MPLL;
324
325 switch (clk_id) {
326 case S3C24XX_CLKSRC_PCLK:
327 break;
328 case S3C24XX_CLKSRC_MPLL:
329 iismod |= S3C2440_IISMOD_MPLL;
330 break;
331 default:
332 return -EINVAL;
333 }
334
335 writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
336 return 0;
337}
338
339/*
340 * Set S3C24xx Clock dividers
341 */
1992a6fb 342static int s3c24xx_i2s_set_clkdiv(struct snd_soc_dai *cpu_dai,
c1422a66
BD
343 int div_id, int div)
344{
345 u32 reg;
346
9bf8e7dd 347 DBG("Entered %s\n", __func__);
c1422a66
BD
348
349 switch (div_id) {
82fb159a 350 case S3C24XX_DIV_BCLK:
c1422a66
BD
351 reg = readl(s3c24xx_i2s.regs + S3C2410_IISMOD) & ~S3C2410_IISMOD_FS_MASK;
352 writel(reg | div, s3c24xx_i2s.regs + S3C2410_IISMOD);
353 break;
82fb159a 354 case S3C24XX_DIV_MCLK:
c1422a66
BD
355 reg = readl(s3c24xx_i2s.regs + S3C2410_IISMOD) & ~(S3C2410_IISMOD_384FS);
356 writel(reg | div, s3c24xx_i2s.regs + S3C2410_IISMOD);
357 break;
358 case S3C24XX_DIV_PRESCALER:
359 writel(div, s3c24xx_i2s.regs + S3C2410_IISPSR);
360 reg = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
361 writel(reg | S3C2410_IISCON_PSCEN, s3c24xx_i2s.regs + S3C2410_IISCON);
362 break;
363 default:
364 return -EINVAL;
365 }
366
367 return 0;
368}
369
370/*
371 * To avoid duplicating clock code, allow machine driver to
372 * get the clockrate from here.
373 */
374u32 s3c24xx_i2s_get_clockrate(void)
375{
376 return clk_get_rate(s3c24xx_i2s.iis_clk);
377}
378EXPORT_SYMBOL_GPL(s3c24xx_i2s_get_clockrate);
379
bdb92876 380static int s3c24xx_i2s_probe(struct platform_device *pdev,
1992a6fb 381 struct snd_soc_dai *dai)
c1422a66 382{
9bf8e7dd 383 DBG("Entered %s\n", __func__);
c1422a66
BD
384
385 s3c24xx_i2s.regs = ioremap(S3C2410_PA_IIS, 0x100);
386 if (s3c24xx_i2s.regs == NULL)
387 return -ENXIO;
388
0fe564a5 389 s3c24xx_i2s.iis_clk = clk_get(&pdev->dev, "iis");
c1422a66
BD
390 if (s3c24xx_i2s.iis_clk == NULL) {
391 DBG("failed to get iis_clock\n");
8642a4ba 392 iounmap(s3c24xx_i2s.regs);
c1422a66
BD
393 return -ENODEV;
394 }
395 clk_enable(s3c24xx_i2s.iis_clk);
396
397 /* Configure the I2S pins in correct mode */
398 s3c2410_gpio_cfgpin(S3C2410_GPE0, S3C2410_GPE0_I2SLRCK);
399 s3c2410_gpio_cfgpin(S3C2410_GPE1, S3C2410_GPE1_I2SSCLK);
400 s3c2410_gpio_cfgpin(S3C2410_GPE2, S3C2410_GPE2_CDCLK);
401 s3c2410_gpio_cfgpin(S3C2410_GPE3, S3C2410_GPE3_I2SSDI);
402 s3c2410_gpio_cfgpin(S3C2410_GPE4, S3C2410_GPE4_I2SSDO);
403
404 writel(S3C2410_IISCON_IISEN, s3c24xx_i2s.regs + S3C2410_IISCON);
405
406 s3c24xx_snd_txctrl(0);
407 s3c24xx_snd_rxctrl(0);
408
409 return 0;
410}
411
5cd919a2 412#ifdef CONFIG_PM
d8ed061a 413static int s3c24xx_i2s_suspend(struct platform_device *pdev,
1992a6fb 414 struct snd_soc_dai *cpu_dai)
5cd919a2 415{
40920307
TN
416 DBG("Entered %s\n", __func__);
417
5cd919a2
GG
418 s3c24xx_i2s.iiscon = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
419 s3c24xx_i2s.iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
420 s3c24xx_i2s.iisfcon = readl(s3c24xx_i2s.regs + S3C2410_IISFCON);
421 s3c24xx_i2s.iispsr = readl(s3c24xx_i2s.regs + S3C2410_IISPSR);
422
423 clk_disable(s3c24xx_i2s.iis_clk);
424
425 return 0;
426}
427
d8ed061a 428static int s3c24xx_i2s_resume(struct platform_device *pdev,
1992a6fb 429 struct snd_soc_dai *cpu_dai)
5cd919a2 430{
40920307 431 DBG("Entered %s\n", __func__);
5cd919a2
GG
432 clk_enable(s3c24xx_i2s.iis_clk);
433
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);
438
439 return 0;
440}
441#else
442#define s3c24xx_i2s_suspend NULL
443#define s3c24xx_i2s_resume NULL
444#endif
445
446
c1422a66
BD
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)
451
1992a6fb 452struct snd_soc_dai s3c24xx_i2s_dai = {
c1422a66
BD
453 .name = "s3c24xx-i2s",
454 .id = 0,
455 .type = SND_SOC_DAI_I2S,
456 .probe = s3c24xx_i2s_probe,
5cd919a2
GG
457 .suspend = s3c24xx_i2s_suspend,
458 .resume = s3c24xx_i2s_resume,
c1422a66
BD
459 .playback = {
460 .channels_min = 2,
461 .channels_max = 2,
462 .rates = S3C24XX_I2S_RATES,
463 .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE,},
464 .capture = {
465 .channels_min = 2,
466 .channels_max = 2,
467 .rates = S3C24XX_I2S_RATES,
468 .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE,},
469 .ops = {
470 .trigger = s3c24xx_i2s_trigger,
471 .hw_params = s3c24xx_i2s_hw_params,},
472 .dai_ops = {
473 .set_fmt = s3c24xx_i2s_set_fmt,
474 .set_clkdiv = s3c24xx_i2s_set_clkdiv,
475 .set_sysclk = s3c24xx_i2s_set_sysclk,
476 },
477};
478EXPORT_SYMBOL_GPL(s3c24xx_i2s_dai);
479
480/* Module information */
481MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
482MODULE_DESCRIPTION("s3c24xx I2S SoC Interface");
483MODULE_LICENSE("GPL");
This page took 0.203531 seconds and 5 git commands to generate.