blob: 819c3c086d696bebb3637b46b50d71ecceabb7d1 [file] [log] [blame]
Ben Dooksdc854472009-03-04 00:49:30 +00001/* sound/soc/s3c24xx/s3c-i2c-v2.c
2 *
3 * ALSA Soc Audio Layer - I2S core for newer Samsung SoCs.
4 *
5 * Copyright (c) 2006 Wolfson Microelectronics PLC.
6 * Graeme Gregory graeme.gregory@wolfsonmicro.com
7 * linux@wolfsonmicro.com
8 *
9 * Copyright (c) 2008, 2007, 2004-2005 Simtec Electronics
10 * http://armlinux.simtec.co.uk/
11 * Ben Dooks <ben@simtec.co.uk>
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
17 */
18
19#include <linux/init.h>
20#include <linux/module.h>
21#include <linux/device.h>
22#include <linux/delay.h>
23#include <linux/clk.h>
24#include <linux/kernel.h>
25#include <linux/io.h>
26
27#include <sound/core.h>
28#include <sound/pcm.h>
29#include <sound/pcm_params.h>
30#include <sound/initval.h>
31#include <sound/soc.h>
32
33#include <plat/regs-s3c2412-iis.h>
34
35#include <plat/audio.h>
36#include <mach/dma.h>
37
38#include "s3c-i2s-v2.h"
Mark Brown5dc07482009-08-26 14:17:23 +010039#include "s3c24xx-pcm.h"
Ben Dooksdc854472009-03-04 00:49:30 +000040
Mark Brown8a0f62b2009-04-29 20:28:47 +010041#undef S3C_IIS_V2_SUPPORTED
42
43#if defined(CONFIG_CPU_S3C2412) || defined(CONFIG_CPU_S3C2413)
44#define S3C_IIS_V2_SUPPORTED
45#endif
46
47#ifdef CONFIG_PLAT_S3C64XX
48#define S3C_IIS_V2_SUPPORTED
49#endif
50
51#ifndef S3C_IIS_V2_SUPPORTED
52#error Unsupported CPU model
53#endif
54
Ben Dooksdc854472009-03-04 00:49:30 +000055#define S3C2412_I2S_DEBUG_CON 0
Ben Dooksdc854472009-03-04 00:49:30 +000056
57static inline struct s3c_i2sv2_info *to_info(struct snd_soc_dai *cpu_dai)
58{
59 return cpu_dai->private_data;
60}
61
62#define bit_set(v, b) (((v) & (b)) ? 1 : 0)
63
64#if S3C2412_I2S_DEBUG_CON
65static void dbg_showcon(const char *fn, u32 con)
66{
67 printk(KERN_DEBUG "%s: LRI=%d, TXFEMPT=%d, RXFEMPT=%d, TXFFULL=%d, RXFFULL=%d\n", fn,
68 bit_set(con, S3C2412_IISCON_LRINDEX),
69 bit_set(con, S3C2412_IISCON_TXFIFO_EMPTY),
70 bit_set(con, S3C2412_IISCON_RXFIFO_EMPTY),
71 bit_set(con, S3C2412_IISCON_TXFIFO_FULL),
72 bit_set(con, S3C2412_IISCON_RXFIFO_FULL));
73
74 printk(KERN_DEBUG "%s: PAUSE: TXDMA=%d, RXDMA=%d, TXCH=%d, RXCH=%d\n",
75 fn,
76 bit_set(con, S3C2412_IISCON_TXDMA_PAUSE),
77 bit_set(con, S3C2412_IISCON_RXDMA_PAUSE),
78 bit_set(con, S3C2412_IISCON_TXCH_PAUSE),
79 bit_set(con, S3C2412_IISCON_RXCH_PAUSE));
80 printk(KERN_DEBUG "%s: ACTIVE: TXDMA=%d, RXDMA=%d, IIS=%d\n", fn,
81 bit_set(con, S3C2412_IISCON_TXDMA_ACTIVE),
82 bit_set(con, S3C2412_IISCON_RXDMA_ACTIVE),
83 bit_set(con, S3C2412_IISCON_IIS_ACTIVE));
84}
85#else
86static inline void dbg_showcon(const char *fn, u32 con)
87{
88}
89#endif
90
91
92/* Turn on or off the transmission path. */
Mark Brownabbc82462009-04-30 13:21:52 +010093static void s3c2412_snd_txctrl(struct s3c_i2sv2_info *i2s, int on)
Ben Dooksdc854472009-03-04 00:49:30 +000094{
95 void __iomem *regs = i2s->regs;
96 u32 fic, con, mod;
97
Mark Brownee7d4762009-03-06 18:04:34 +000098 pr_debug("%s(%d)\n", __func__, on);
Ben Dooksdc854472009-03-04 00:49:30 +000099
100 fic = readl(regs + S3C2412_IISFIC);
101 con = readl(regs + S3C2412_IISCON);
102 mod = readl(regs + S3C2412_IISMOD);
103
Mark Brownee7d4762009-03-06 18:04:34 +0000104 pr_debug("%s: IIS: CON=%x MOD=%x FIC=%x\n", __func__, con, mod, fic);
Ben Dooksdc854472009-03-04 00:49:30 +0000105
106 if (on) {
107 con |= S3C2412_IISCON_TXDMA_ACTIVE | S3C2412_IISCON_IIS_ACTIVE;
108 con &= ~S3C2412_IISCON_TXDMA_PAUSE;
109 con &= ~S3C2412_IISCON_TXCH_PAUSE;
110
111 switch (mod & S3C2412_IISMOD_MODE_MASK) {
112 case S3C2412_IISMOD_MODE_TXONLY:
113 case S3C2412_IISMOD_MODE_TXRX:
114 /* do nothing, we are in the right mode */
115 break;
116
117 case S3C2412_IISMOD_MODE_RXONLY:
118 mod &= ~S3C2412_IISMOD_MODE_MASK;
119 mod |= S3C2412_IISMOD_MODE_TXRX;
120 break;
121
122 default:
Mark Brownabbc82462009-04-30 13:21:52 +0100123 dev_err(i2s->dev, "TXEN: Invalid MODE %x in IISMOD\n",
124 mod & S3C2412_IISMOD_MODE_MASK);
125 break;
Ben Dooksdc854472009-03-04 00:49:30 +0000126 }
127
128 writel(con, regs + S3C2412_IISCON);
129 writel(mod, regs + S3C2412_IISMOD);
130 } else {
131 /* Note, we do not have any indication that the FIFO problems
132 * tha the S3C2410/2440 had apply here, so we should be able
133 * to disable the DMA and TX without resetting the FIFOS.
134 */
135
136 con |= S3C2412_IISCON_TXDMA_PAUSE;
137 con |= S3C2412_IISCON_TXCH_PAUSE;
138 con &= ~S3C2412_IISCON_TXDMA_ACTIVE;
139
140 switch (mod & S3C2412_IISMOD_MODE_MASK) {
141 case S3C2412_IISMOD_MODE_TXRX:
142 mod &= ~S3C2412_IISMOD_MODE_MASK;
143 mod |= S3C2412_IISMOD_MODE_RXONLY;
144 break;
145
146 case S3C2412_IISMOD_MODE_TXONLY:
147 mod &= ~S3C2412_IISMOD_MODE_MASK;
148 con &= ~S3C2412_IISCON_IIS_ACTIVE;
149 break;
150
151 default:
Mark Brownabbc82462009-04-30 13:21:52 +0100152 dev_err(i2s->dev, "TXDIS: Invalid MODE %x in IISMOD\n",
153 mod & S3C2412_IISMOD_MODE_MASK);
154 break;
Ben Dooksdc854472009-03-04 00:49:30 +0000155 }
156
157 writel(mod, regs + S3C2412_IISMOD);
158 writel(con, regs + S3C2412_IISCON);
159 }
160
161 fic = readl(regs + S3C2412_IISFIC);
162 dbg_showcon(__func__, con);
Mark Brownee7d4762009-03-06 18:04:34 +0000163 pr_debug("%s: IIS: CON=%x MOD=%x FIC=%x\n", __func__, con, mod, fic);
Ben Dooksdc854472009-03-04 00:49:30 +0000164}
Ben Dooksdc854472009-03-04 00:49:30 +0000165
Mark Brownabbc82462009-04-30 13:21:52 +0100166static void s3c2412_snd_rxctrl(struct s3c_i2sv2_info *i2s, int on)
Ben Dooksdc854472009-03-04 00:49:30 +0000167{
168 void __iomem *regs = i2s->regs;
169 u32 fic, con, mod;
170
Mark Brownee7d4762009-03-06 18:04:34 +0000171 pr_debug("%s(%d)\n", __func__, on);
Ben Dooksdc854472009-03-04 00:49:30 +0000172
173 fic = readl(regs + S3C2412_IISFIC);
174 con = readl(regs + S3C2412_IISCON);
175 mod = readl(regs + S3C2412_IISMOD);
176
Mark Brownee7d4762009-03-06 18:04:34 +0000177 pr_debug("%s: IIS: CON=%x MOD=%x FIC=%x\n", __func__, con, mod, fic);
Ben Dooksdc854472009-03-04 00:49:30 +0000178
179 if (on) {
180 con |= S3C2412_IISCON_RXDMA_ACTIVE | S3C2412_IISCON_IIS_ACTIVE;
181 con &= ~S3C2412_IISCON_RXDMA_PAUSE;
182 con &= ~S3C2412_IISCON_RXCH_PAUSE;
183
184 switch (mod & S3C2412_IISMOD_MODE_MASK) {
185 case S3C2412_IISMOD_MODE_TXRX:
186 case S3C2412_IISMOD_MODE_RXONLY:
187 /* do nothing, we are in the right mode */
188 break;
189
190 case S3C2412_IISMOD_MODE_TXONLY:
191 mod &= ~S3C2412_IISMOD_MODE_MASK;
192 mod |= S3C2412_IISMOD_MODE_TXRX;
193 break;
194
195 default:
Mark Brownabbc82462009-04-30 13:21:52 +0100196 dev_err(i2s->dev, "RXEN: Invalid MODE %x in IISMOD\n",
197 mod & S3C2412_IISMOD_MODE_MASK);
Ben Dooksdc854472009-03-04 00:49:30 +0000198 }
199
200 writel(mod, regs + S3C2412_IISMOD);
201 writel(con, regs + S3C2412_IISCON);
202 } else {
203 /* See txctrl notes on FIFOs. */
204
205 con &= ~S3C2412_IISCON_RXDMA_ACTIVE;
206 con |= S3C2412_IISCON_RXDMA_PAUSE;
207 con |= S3C2412_IISCON_RXCH_PAUSE;
208
209 switch (mod & S3C2412_IISMOD_MODE_MASK) {
210 case S3C2412_IISMOD_MODE_RXONLY:
211 con &= ~S3C2412_IISCON_IIS_ACTIVE;
212 mod &= ~S3C2412_IISMOD_MODE_MASK;
213 break;
214
215 case S3C2412_IISMOD_MODE_TXRX:
216 mod &= ~S3C2412_IISMOD_MODE_MASK;
217 mod |= S3C2412_IISMOD_MODE_TXONLY;
218 break;
219
220 default:
Mark Brownabbc82462009-04-30 13:21:52 +0100221 dev_err(i2s->dev, "RXDIS: Invalid MODE %x in IISMOD\n",
222 mod & S3C2412_IISMOD_MODE_MASK);
Ben Dooksdc854472009-03-04 00:49:30 +0000223 }
224
225 writel(con, regs + S3C2412_IISCON);
226 writel(mod, regs + S3C2412_IISMOD);
227 }
228
229 fic = readl(regs + S3C2412_IISFIC);
Mark Brownee7d4762009-03-06 18:04:34 +0000230 pr_debug("%s: IIS: CON=%x MOD=%x FIC=%x\n", __func__, con, mod, fic);
Ben Dooksdc854472009-03-04 00:49:30 +0000231}
Ben Dooksdc854472009-03-04 00:49:30 +0000232
233/*
234 * Wait for the LR signal to allow synchronisation to the L/R clock
235 * from the codec. May only be needed for slave mode.
236 */
237static int s3c2412_snd_lrsync(struct s3c_i2sv2_info *i2s)
238{
239 u32 iiscon;
240 unsigned long timeout = jiffies + msecs_to_jiffies(5);
241
Mark Brownee7d4762009-03-06 18:04:34 +0000242 pr_debug("Entered %s\n", __func__);
Ben Dooksdc854472009-03-04 00:49:30 +0000243
244 while (1) {
245 iiscon = readl(i2s->regs + S3C2412_IISCON);
246 if (iiscon & S3C2412_IISCON_LRINDEX)
247 break;
248
249 if (timeout < jiffies) {
250 printk(KERN_ERR "%s: timeout\n", __func__);
251 return -ETIMEDOUT;
252 }
253 }
254
255 return 0;
256}
257
258/*
259 * Set S3C2412 I2S DAI format
260 */
261static int s3c2412_i2s_set_fmt(struct snd_soc_dai *cpu_dai,
262 unsigned int fmt)
263{
264 struct s3c_i2sv2_info *i2s = to_info(cpu_dai);
265 u32 iismod;
266
Mark Brownee7d4762009-03-06 18:04:34 +0000267 pr_debug("Entered %s\n", __func__);
Ben Dooksdc854472009-03-04 00:49:30 +0000268
269 iismod = readl(i2s->regs + S3C2412_IISMOD);
Mark Brownee7d4762009-03-06 18:04:34 +0000270 pr_debug("hw_params r: IISMOD: %x \n", iismod);
Ben Dooksdc854472009-03-04 00:49:30 +0000271
272#if defined(CONFIG_CPU_S3C2412) || defined(CONFIG_CPU_S3C2413)
273#define IISMOD_MASTER_MASK S3C2412_IISMOD_MASTER_MASK
274#define IISMOD_SLAVE S3C2412_IISMOD_SLAVE
275#define IISMOD_MASTER S3C2412_IISMOD_MASTER_INTERNAL
276#endif
277
278#if defined(CONFIG_PLAT_S3C64XX)
279/* From Rev1.1 datasheet, we have two master and two slave modes:
280 * IMS[11:10]:
281 * 00 = master mode, fed from PCLK
282 * 01 = master mode, fed from CLKAUDIO
283 * 10 = slave mode, using PCLK
284 * 11 = slave mode, using I2SCLK
285 */
286#define IISMOD_MASTER_MASK (1 << 11)
287#define IISMOD_SLAVE (1 << 11)
Mark Brown553b1dd2009-04-29 20:29:25 +0100288#define IISMOD_MASTER (0 << 11)
Ben Dooksdc854472009-03-04 00:49:30 +0000289#endif
290
291 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
292 case SND_SOC_DAIFMT_CBM_CFM:
293 i2s->master = 0;
294 iismod &= ~IISMOD_MASTER_MASK;
295 iismod |= IISMOD_SLAVE;
296 break;
297 case SND_SOC_DAIFMT_CBS_CFS:
298 i2s->master = 1;
299 iismod &= ~IISMOD_MASTER_MASK;
300 iismod |= IISMOD_MASTER;
301 break;
302 default:
Mark Brown38e43c82009-04-30 13:14:38 +0100303 pr_err("unknwon master/slave format\n");
Ben Dooksdc854472009-03-04 00:49:30 +0000304 return -EINVAL;
305 }
306
307 iismod &= ~S3C2412_IISMOD_SDF_MASK;
308
309 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
310 case SND_SOC_DAIFMT_RIGHT_J:
Jassifd5ad652009-09-15 19:02:38 +0900311 iismod |= S3C2412_IISMOD_LR_RLOW;
Ben Dooksdc854472009-03-04 00:49:30 +0000312 iismod |= S3C2412_IISMOD_SDF_MSB;
313 break;
314 case SND_SOC_DAIFMT_LEFT_J:
Jassifd5ad652009-09-15 19:02:38 +0900315 iismod |= S3C2412_IISMOD_LR_RLOW;
Ben Dooksdc854472009-03-04 00:49:30 +0000316 iismod |= S3C2412_IISMOD_SDF_LSB;
317 break;
318 case SND_SOC_DAIFMT_I2S:
Jassifd5ad652009-09-15 19:02:38 +0900319 iismod &= ~S3C2412_IISMOD_LR_RLOW;
Ben Dooksdc854472009-03-04 00:49:30 +0000320 iismod |= S3C2412_IISMOD_SDF_IIS;
321 break;
322 default:
Mark Brown38e43c82009-04-30 13:14:38 +0100323 pr_err("Unknown data format\n");
Ben Dooksdc854472009-03-04 00:49:30 +0000324 return -EINVAL;
325 }
326
327 writel(iismod, i2s->regs + S3C2412_IISMOD);
Mark Brownee7d4762009-03-06 18:04:34 +0000328 pr_debug("hw_params w: IISMOD: %x \n", iismod);
Ben Dooksdc854472009-03-04 00:49:30 +0000329 return 0;
330}
331
332static int s3c2412_i2s_hw_params(struct snd_pcm_substream *substream,
333 struct snd_pcm_hw_params *params,
334 struct snd_soc_dai *socdai)
335{
336 struct snd_soc_pcm_runtime *rtd = substream->private_data;
337 struct snd_soc_dai_link *dai = rtd->dai;
338 struct s3c_i2sv2_info *i2s = to_info(dai->cpu_dai);
339 u32 iismod;
340
Mark Brownee7d4762009-03-06 18:04:34 +0000341 pr_debug("Entered %s\n", __func__);
Ben Dooksdc854472009-03-04 00:49:30 +0000342
343 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
344 dai->cpu_dai->dma_data = i2s->dma_playback;
345 else
346 dai->cpu_dai->dma_data = i2s->dma_capture;
347
348 /* Working copies of register */
349 iismod = readl(i2s->regs + S3C2412_IISMOD);
Mark Brownee7d4762009-03-06 18:04:34 +0000350 pr_debug("%s: r: IISMOD: %x\n", __func__, iismod);
Ben Dooksdc854472009-03-04 00:49:30 +0000351
Mark Brown553b1dd2009-04-29 20:29:25 +0100352#if defined(CONFIG_CPU_S3C2412) || defined(CONFIG_CPU_S3C2413)
Ben Dooksdc854472009-03-04 00:49:30 +0000353 switch (params_format(params)) {
354 case SNDRV_PCM_FORMAT_S8:
355 iismod |= S3C2412_IISMOD_8BIT;
356 break;
357 case SNDRV_PCM_FORMAT_S16_LE:
358 iismod &= ~S3C2412_IISMOD_8BIT;
359 break;
360 }
Mark Brown553b1dd2009-04-29 20:29:25 +0100361#endif
362
363#ifdef CONFIG_PLAT_S3C64XX
Joonyoung Shim0914b932009-08-18 21:56:19 +0900364 iismod &= ~(S3C64XX_IISMOD_BLC_MASK | S3C2412_IISMOD_BCLK_MASK);
Mark Brown553b1dd2009-04-29 20:29:25 +0100365 /* Sample size */
366 switch (params_format(params)) {
367 case SNDRV_PCM_FORMAT_S8:
368 /* 8 bit sample, 16fs BCLK */
Joonyoung Shim0914b932009-08-18 21:56:19 +0900369 iismod |= (S3C64XX_IISMOD_BLC_8BIT | S3C2412_IISMOD_BCLK_16FS);
Mark Brown553b1dd2009-04-29 20:29:25 +0100370 break;
371 case SNDRV_PCM_FORMAT_S16_LE:
372 /* 16 bit sample, 32fs BCLK */
373 break;
374 case SNDRV_PCM_FORMAT_S24_LE:
375 /* 24 bit sample, 48fs BCLK */
Joonyoung Shim0914b932009-08-18 21:56:19 +0900376 iismod |= (S3C64XX_IISMOD_BLC_24BIT | S3C2412_IISMOD_BCLK_48FS);
Mark Brown553b1dd2009-04-29 20:29:25 +0100377 break;
378 }
379#endif
Ben Dooksdc854472009-03-04 00:49:30 +0000380
381 writel(iismod, i2s->regs + S3C2412_IISMOD);
Mark Brownee7d4762009-03-06 18:04:34 +0000382 pr_debug("%s: w: IISMOD: %x\n", __func__, iismod);
Ben Dooksdc854472009-03-04 00:49:30 +0000383 return 0;
384}
385
386static int s3c2412_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
387 struct snd_soc_dai *dai)
388{
389 struct snd_soc_pcm_runtime *rtd = substream->private_data;
390 struct s3c_i2sv2_info *i2s = to_info(rtd->dai->cpu_dai);
391 int capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
392 unsigned long irqs;
393 int ret = 0;
Shine Liufaf907c2009-08-25 20:05:50 +0800394 int channel = ((struct s3c24xx_pcm_dma_params *)
395 rtd->dai->cpu_dai->dma_data)->channel;
Ben Dooksdc854472009-03-04 00:49:30 +0000396
Mark Brownee7d4762009-03-06 18:04:34 +0000397 pr_debug("Entered %s\n", __func__);
Ben Dooksdc854472009-03-04 00:49:30 +0000398
399 switch (cmd) {
400 case SNDRV_PCM_TRIGGER_START:
401 /* On start, ensure that the FIFOs are cleared and reset. */
402
403 writel(capture ? S3C2412_IISFIC_RXFLUSH : S3C2412_IISFIC_TXFLUSH,
404 i2s->regs + S3C2412_IISFIC);
405
406 /* clear again, just in case */
407 writel(0x0, i2s->regs + S3C2412_IISFIC);
408
409 case SNDRV_PCM_TRIGGER_RESUME:
410 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
411 if (!i2s->master) {
412 ret = s3c2412_snd_lrsync(i2s);
413 if (ret)
414 goto exit_err;
415 }
416
417 local_irq_save(irqs);
418
419 if (capture)
420 s3c2412_snd_rxctrl(i2s, 1);
421 else
422 s3c2412_snd_txctrl(i2s, 1);
423
424 local_irq_restore(irqs);
Shine Liufaf907c2009-08-25 20:05:50 +0800425
426 /*
427 * Load the next buffer to DMA to meet the reqirement
428 * of the auto reload mechanism of S3C24XX.
429 * This call won't bother S3C64XX.
430 */
431 s3c2410_dma_ctrl(channel, S3C2410_DMAOP_STARTED);
432
Ben Dooksdc854472009-03-04 00:49:30 +0000433 break;
434
435 case SNDRV_PCM_TRIGGER_STOP:
436 case SNDRV_PCM_TRIGGER_SUSPEND:
437 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
438 local_irq_save(irqs);
439
440 if (capture)
441 s3c2412_snd_rxctrl(i2s, 0);
442 else
443 s3c2412_snd_txctrl(i2s, 0);
444
445 local_irq_restore(irqs);
446 break;
447 default:
448 ret = -EINVAL;
449 break;
450 }
451
452exit_err:
453 return ret;
454}
455
456/*
457 * Set S3C2412 Clock dividers
458 */
459static int s3c2412_i2s_set_clkdiv(struct snd_soc_dai *cpu_dai,
460 int div_id, int div)
461{
462 struct s3c_i2sv2_info *i2s = to_info(cpu_dai);
463 u32 reg;
464
Mark Brownee7d4762009-03-06 18:04:34 +0000465 pr_debug("%s(%p, %d, %d)\n", __func__, cpu_dai, div_id, div);
Ben Dooksdc854472009-03-04 00:49:30 +0000466
467 switch (div_id) {
468 case S3C_I2SV2_DIV_BCLK:
Jassifd5ad652009-09-15 19:02:38 +0900469 if (div > 3) {
470 /* convert value to bit field */
471
472 switch (div) {
473 case 16:
474 div = S3C2412_IISMOD_BCLK_16FS;
475 break;
476
477 case 32:
478 div = S3C2412_IISMOD_BCLK_32FS;
479 break;
480
481 case 24:
482 div = S3C2412_IISMOD_BCLK_24FS;
483 break;
484
485 case 48:
486 div = S3C2412_IISMOD_BCLK_48FS;
487 break;
488
489 default:
490 return -EINVAL;
491 }
492 }
493
Ben Dooksdc854472009-03-04 00:49:30 +0000494 reg = readl(i2s->regs + S3C2412_IISMOD);
495 reg &= ~S3C2412_IISMOD_BCLK_MASK;
496 writel(reg | div, i2s->regs + S3C2412_IISMOD);
497
Mark Brownee7d4762009-03-06 18:04:34 +0000498 pr_debug("%s: MOD=%08x\n", __func__, readl(i2s->regs + S3C2412_IISMOD));
Ben Dooksdc854472009-03-04 00:49:30 +0000499 break;
500
501 case S3C_I2SV2_DIV_RCLK:
502 if (div > 3) {
503 /* convert value to bit field */
504
505 switch (div) {
506 case 256:
507 div = S3C2412_IISMOD_RCLK_256FS;
508 break;
509
510 case 384:
511 div = S3C2412_IISMOD_RCLK_384FS;
512 break;
513
514 case 512:
515 div = S3C2412_IISMOD_RCLK_512FS;
516 break;
517
518 case 768:
519 div = S3C2412_IISMOD_RCLK_768FS;
520 break;
521
522 default:
523 return -EINVAL;
524 }
525 }
526
527 reg = readl(i2s->regs + S3C2412_IISMOD);
528 reg &= ~S3C2412_IISMOD_RCLK_MASK;
529 writel(reg | div, i2s->regs + S3C2412_IISMOD);
Mark Brownee7d4762009-03-06 18:04:34 +0000530 pr_debug("%s: MOD=%08x\n", __func__, readl(i2s->regs + S3C2412_IISMOD));
Ben Dooksdc854472009-03-04 00:49:30 +0000531 break;
532
533 case S3C_I2SV2_DIV_PRESCALER:
534 if (div >= 0) {
535 writel((div << 8) | S3C2412_IISPSR_PSREN,
536 i2s->regs + S3C2412_IISPSR);
537 } else {
538 writel(0x0, i2s->regs + S3C2412_IISPSR);
539 }
Mark Brownee7d4762009-03-06 18:04:34 +0000540 pr_debug("%s: PSR=%08x\n", __func__, readl(i2s->regs + S3C2412_IISPSR));
Ben Dooksdc854472009-03-04 00:49:30 +0000541 break;
542
543 default:
544 return -EINVAL;
545 }
546
547 return 0;
548}
549
550/* default table of all avaialable root fs divisors */
551static unsigned int iis_fs_tab[] = { 256, 512, 384, 768 };
552
Ben Dooks1d2b7ae2009-04-16 10:32:23 +0100553int s3c_i2sv2_iis_calc_rate(struct s3c_i2sv2_rate_calc *info,
554 unsigned int *fstab,
555 unsigned int rate, struct clk *clk)
Ben Dooksdc854472009-03-04 00:49:30 +0000556{
557 unsigned long clkrate = clk_get_rate(clk);
558 unsigned int div;
559 unsigned int fsclk;
560 unsigned int actual;
561 unsigned int fs;
562 unsigned int fsdiv;
563 signed int deviation = 0;
564 unsigned int best_fs = 0;
565 unsigned int best_div = 0;
566 unsigned int best_rate = 0;
567 unsigned int best_deviation = INT_MAX;
568
Mark Brownaf3ea7b2009-04-30 13:13:55 +0100569 pr_debug("Input clock rate %ldHz\n", clkrate);
570
Ben Dooksdc854472009-03-04 00:49:30 +0000571 if (fstab == NULL)
572 fstab = iis_fs_tab;
573
574 for (fs = 0; fs < ARRAY_SIZE(iis_fs_tab); fs++) {
575 fsdiv = iis_fs_tab[fs];
576
577 fsclk = clkrate / fsdiv;
578 div = fsclk / rate;
579
580 if ((fsclk % rate) > (rate / 2))
581 div++;
582
583 if (div <= 1)
584 continue;
585
586 actual = clkrate / (fsdiv * div);
587 deviation = actual - rate;
588
Roel Kluin449bd542009-05-27 17:08:39 -0700589 printk(KERN_DEBUG "%ufs: div %u => result %u, deviation %d\n",
Ben Dooksdc854472009-03-04 00:49:30 +0000590 fsdiv, div, actual, deviation);
591
592 deviation = abs(deviation);
593
594 if (deviation < best_deviation) {
595 best_fs = fsdiv;
596 best_div = div;
597 best_rate = actual;
598 best_deviation = deviation;
599 }
600
601 if (deviation == 0)
602 break;
603 }
604
Roel Kluin449bd542009-05-27 17:08:39 -0700605 printk(KERN_DEBUG "best: fs=%u, div=%u, rate=%u\n",
Ben Dooksdc854472009-03-04 00:49:30 +0000606 best_fs, best_div, best_rate);
607
608 info->fs_div = best_fs;
609 info->clk_div = best_div;
610
611 return 0;
612}
Ben Dooks1d2b7ae2009-04-16 10:32:23 +0100613EXPORT_SYMBOL_GPL(s3c_i2sv2_iis_calc_rate);
Ben Dooksdc854472009-03-04 00:49:30 +0000614
615int s3c_i2sv2_probe(struct platform_device *pdev,
616 struct snd_soc_dai *dai,
617 struct s3c_i2sv2_info *i2s,
618 unsigned long base)
619{
620 struct device *dev = &pdev->dev;
Mark Brown07736d42009-04-30 13:13:14 +0100621 unsigned int iismod;
Ben Dooksdc854472009-03-04 00:49:30 +0000622
623 i2s->dev = dev;
624
625 /* record our i2s structure for later use in the callbacks */
626 dai->private_data = i2s;
627
Mark Brownc86bde52009-04-30 13:09:33 +0100628 if (!base) {
629 struct resource *res = platform_get_resource(pdev,
630 IORESOURCE_MEM,
631 0);
632 if (!res) {
633 dev_err(dev, "Unable to get register resource\n");
634 return -ENXIO;
635 }
636
637 if (!request_mem_region(res->start, resource_size(res),
638 "s3c64xx-i2s-v4")) {
639 dev_err(dev, "Unable to request register region\n");
640 return -EBUSY;
641 }
642
643 base = res->start;
644 }
645
Ben Dooksdc854472009-03-04 00:49:30 +0000646 i2s->regs = ioremap(base, 0x100);
647 if (i2s->regs == NULL) {
648 dev_err(dev, "cannot ioremap registers\n");
649 return -ENXIO;
650 }
651
652 i2s->iis_pclk = clk_get(dev, "iis");
Jassifd5ad652009-09-15 19:02:38 +0900653 if (IS_ERR(i2s->iis_pclk)) {
Mark Brownb52a5192009-03-06 18:13:43 +0000654 dev_err(dev, "failed to get iis_clock\n");
Ben Dooksdc854472009-03-04 00:49:30 +0000655 iounmap(i2s->regs);
656 return -ENOENT;
657 }
658
659 clk_enable(i2s->iis_pclk);
660
Mark Brown07736d42009-04-30 13:13:14 +0100661 /* Mark ourselves as in TXRX mode so we can run through our cleanup
662 * process without warnings. */
663 iismod = readl(i2s->regs + S3C2412_IISMOD);
664 iismod |= S3C2412_IISMOD_MODE_TXRX;
665 writel(iismod, i2s->regs + S3C2412_IISMOD);
Ben Dooksdc854472009-03-04 00:49:30 +0000666 s3c2412_snd_txctrl(i2s, 0);
667 s3c2412_snd_rxctrl(i2s, 0);
668
669 return 0;
670}
Ben Dooksdc854472009-03-04 00:49:30 +0000671EXPORT_SYMBOL_GPL(s3c_i2sv2_probe);
672
673#ifdef CONFIG_PM
674static int s3c2412_i2s_suspend(struct snd_soc_dai *dai)
675{
676 struct s3c_i2sv2_info *i2s = to_info(dai);
677 u32 iismod;
678
679 if (dai->active) {
680 i2s->suspend_iismod = readl(i2s->regs + S3C2412_IISMOD);
681 i2s->suspend_iiscon = readl(i2s->regs + S3C2412_IISCON);
682 i2s->suspend_iispsr = readl(i2s->regs + S3C2412_IISPSR);
683
684 /* some basic suspend checks */
685
686 iismod = readl(i2s->regs + S3C2412_IISMOD);
687
688 if (iismod & S3C2412_IISCON_RXDMA_ACTIVE)
689 pr_warning("%s: RXDMA active?\n", __func__);
690
691 if (iismod & S3C2412_IISCON_TXDMA_ACTIVE)
692 pr_warning("%s: TXDMA active?\n", __func__);
693
694 if (iismod & S3C2412_IISCON_IIS_ACTIVE)
695 pr_warning("%s: IIS active\n", __func__);
696 }
697
698 return 0;
699}
700
701static int s3c2412_i2s_resume(struct snd_soc_dai *dai)
702{
703 struct s3c_i2sv2_info *i2s = to_info(dai);
704
705 pr_info("dai_active %d, IISMOD %08x, IISCON %08x\n",
706 dai->active, i2s->suspend_iismod, i2s->suspend_iiscon);
707
708 if (dai->active) {
709 writel(i2s->suspend_iiscon, i2s->regs + S3C2412_IISCON);
710 writel(i2s->suspend_iismod, i2s->regs + S3C2412_IISMOD);
711 writel(i2s->suspend_iispsr, i2s->regs + S3C2412_IISPSR);
712
713 writel(S3C2412_IISFIC_RXFLUSH | S3C2412_IISFIC_TXFLUSH,
714 i2s->regs + S3C2412_IISFIC);
715
716 ndelay(250);
717 writel(0x0, i2s->regs + S3C2412_IISFIC);
718 }
719
720 return 0;
721}
722#else
723#define s3c2412_i2s_suspend NULL
724#define s3c2412_i2s_resume NULL
725#endif
726
727int s3c_i2sv2_register_dai(struct snd_soc_dai *dai)
728{
Ben Dooks3715c6a2009-04-16 10:32:22 +0100729 struct snd_soc_dai_ops *ops = dai->ops;
730
731 ops->trigger = s3c2412_i2s_trigger;
732 ops->hw_params = s3c2412_i2s_hw_params;
733 ops->set_fmt = s3c2412_i2s_set_fmt;
734 ops->set_clkdiv = s3c2412_i2s_set_clkdiv;
Ben Dooksdc854472009-03-04 00:49:30 +0000735
736 dai->suspend = s3c2412_i2s_suspend;
737 dai->resume = s3c2412_i2s_resume;
738
739 return snd_soc_register_dai(dai);
740}
Ben Dooksdc854472009-03-04 00:49:30 +0000741EXPORT_SYMBOL_GPL(s3c_i2sv2_register_dai);
Mark Browna396e322009-04-23 15:43:45 +0100742
743MODULE_LICENSE("GPL");