1 /* sound/soc/s3c24xx/s3c-i2c-v2.c
3 * ALSA Soc Audio Layer - I2S core for newer Samsung SoCs.
5 * Copyright (c) 2006 Wolfson Microelectronics PLC.
6 * Graeme Gregory graeme.gregory@wolfsonmicro.com
7 * linux@wolfsonmicro.com
9 * Copyright (c) 2008, 2007, 2004-2005 Simtec Electronics
10 * http://armlinux.simtec.co.uk/
11 * Ben Dooks <ben@simtec.co.uk>
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.
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>
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>
33 #include <plat/regs-s3c2412-iis.h>
35 #include <plat/audio.h>
38 #include "s3c-i2s-v2.h"
39 #include "s3c24xx-pcm.h"
41 #undef S3C_IIS_V2_SUPPORTED
43 #if defined(CONFIG_CPU_S3C2412) || defined(CONFIG_CPU_S3C2413)
44 #define S3C_IIS_V2_SUPPORTED
47 #ifdef CONFIG_PLAT_S3C64XX
48 #define S3C_IIS_V2_SUPPORTED
51 #ifndef S3C_IIS_V2_SUPPORTED
52 #error Unsupported CPU model
55 #define S3C2412_I2S_DEBUG_CON 0
57 static inline struct s3c_i2sv2_info *to_info(struct snd_soc_dai *cpu_dai)
59 return cpu_dai->private_data;
62 #define bit_set(v, b) (((v) & (b)) ? 1 : 0)
64 #if S3C2412_I2S_DEBUG_CON
65 static void dbg_showcon(const char *fn, u32 con)
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));
74 printk(KERN_DEBUG "%s: PAUSE: TXDMA=%d, RXDMA=%d, TXCH=%d, RXCH=%d\n",
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));
86 static inline void dbg_showcon(const char *fn, u32 con)
92 /* Turn on or off the transmission path. */
93 static void s3c2412_snd_txctrl(struct s3c_i2sv2_info *i2s, int on)
95 void __iomem *regs = i2s->regs;
98 pr_debug("%s(%d)\n", __func__, on);
100 fic = readl(regs + S3C2412_IISFIC);
101 con = readl(regs + S3C2412_IISCON);
102 mod = readl(regs + S3C2412_IISMOD);
104 pr_debug("%s: IIS: CON=%x MOD=%x FIC=%x\n", __func__, con, mod, fic);
107 con |= S3C2412_IISCON_TXDMA_ACTIVE | S3C2412_IISCON_IIS_ACTIVE;
108 con &= ~S3C2412_IISCON_TXDMA_PAUSE;
109 con &= ~S3C2412_IISCON_TXCH_PAUSE;
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 */
117 case S3C2412_IISMOD_MODE_RXONLY:
118 mod &= ~S3C2412_IISMOD_MODE_MASK;
119 mod |= S3C2412_IISMOD_MODE_TXRX;
123 dev_err(i2s->dev, "TXEN: Invalid MODE %x in IISMOD\n",
124 mod & S3C2412_IISMOD_MODE_MASK);
128 writel(con, regs + S3C2412_IISCON);
129 writel(mod, regs + S3C2412_IISMOD);
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.
136 con |= S3C2412_IISCON_TXDMA_PAUSE;
137 con |= S3C2412_IISCON_TXCH_PAUSE;
138 con &= ~S3C2412_IISCON_TXDMA_ACTIVE;
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;
146 case S3C2412_IISMOD_MODE_TXONLY:
147 mod &= ~S3C2412_IISMOD_MODE_MASK;
148 con &= ~S3C2412_IISCON_IIS_ACTIVE;
152 dev_err(i2s->dev, "TXDIS: Invalid MODE %x in IISMOD\n",
153 mod & S3C2412_IISMOD_MODE_MASK);
157 writel(mod, regs + S3C2412_IISMOD);
158 writel(con, regs + S3C2412_IISCON);
161 fic = readl(regs + S3C2412_IISFIC);
162 dbg_showcon(__func__, con);
163 pr_debug("%s: IIS: CON=%x MOD=%x FIC=%x\n", __func__, con, mod, fic);
166 static void s3c2412_snd_rxctrl(struct s3c_i2sv2_info *i2s, int on)
168 void __iomem *regs = i2s->regs;
171 pr_debug("%s(%d)\n", __func__, on);
173 fic = readl(regs + S3C2412_IISFIC);
174 con = readl(regs + S3C2412_IISCON);
175 mod = readl(regs + S3C2412_IISMOD);
177 pr_debug("%s: IIS: CON=%x MOD=%x FIC=%x\n", __func__, con, mod, fic);
180 con |= S3C2412_IISCON_RXDMA_ACTIVE | S3C2412_IISCON_IIS_ACTIVE;
181 con &= ~S3C2412_IISCON_RXDMA_PAUSE;
182 con &= ~S3C2412_IISCON_RXCH_PAUSE;
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 */
190 case S3C2412_IISMOD_MODE_TXONLY:
191 mod &= ~S3C2412_IISMOD_MODE_MASK;
192 mod |= S3C2412_IISMOD_MODE_TXRX;
196 dev_err(i2s->dev, "RXEN: Invalid MODE %x in IISMOD\n",
197 mod & S3C2412_IISMOD_MODE_MASK);
200 writel(mod, regs + S3C2412_IISMOD);
201 writel(con, regs + S3C2412_IISCON);
203 /* See txctrl notes on FIFOs. */
205 con &= ~S3C2412_IISCON_RXDMA_ACTIVE;
206 con |= S3C2412_IISCON_RXDMA_PAUSE;
207 con |= S3C2412_IISCON_RXCH_PAUSE;
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;
215 case S3C2412_IISMOD_MODE_TXRX:
216 mod &= ~S3C2412_IISMOD_MODE_MASK;
217 mod |= S3C2412_IISMOD_MODE_TXONLY;
221 dev_err(i2s->dev, "RXDIS: Invalid MODE %x in IISMOD\n",
222 mod & S3C2412_IISMOD_MODE_MASK);
225 writel(con, regs + S3C2412_IISCON);
226 writel(mod, regs + S3C2412_IISMOD);
229 fic = readl(regs + S3C2412_IISFIC);
230 pr_debug("%s: IIS: CON=%x MOD=%x FIC=%x\n", __func__, con, mod, fic);
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.
237 static int s3c2412_snd_lrsync(struct s3c_i2sv2_info *i2s)
240 unsigned long timeout = jiffies + msecs_to_jiffies(5);
242 pr_debug("Entered %s\n", __func__);
245 iiscon = readl(i2s->regs + S3C2412_IISCON);
246 if (iiscon & S3C2412_IISCON_LRINDEX)
249 if (timeout < jiffies) {
250 printk(KERN_ERR "%s: timeout\n", __func__);
259 * Set S3C2412 I2S DAI format
261 static int s3c2412_i2s_set_fmt(struct snd_soc_dai *cpu_dai,
264 struct s3c_i2sv2_info *i2s = to_info(cpu_dai);
267 pr_debug("Entered %s\n", __func__);
269 iismod = readl(i2s->regs + S3C2412_IISMOD);
270 pr_debug("hw_params r: IISMOD: %x \n", iismod);
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
278 #if defined(CONFIG_PLAT_S3C64XX)
279 /* From Rev1.1 datasheet, we have two master and two slave modes:
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
286 #define IISMOD_MASTER_MASK (1 << 11)
287 #define IISMOD_SLAVE (1 << 11)
288 #define IISMOD_MASTER (0 << 11)
291 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
292 case SND_SOC_DAIFMT_CBM_CFM:
294 iismod &= ~IISMOD_MASTER_MASK;
295 iismod |= IISMOD_SLAVE;
297 case SND_SOC_DAIFMT_CBS_CFS:
299 iismod &= ~IISMOD_MASTER_MASK;
300 iismod |= IISMOD_MASTER;
303 pr_err("unknwon master/slave format\n");
307 iismod &= ~S3C2412_IISMOD_SDF_MASK;
309 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
310 case SND_SOC_DAIFMT_RIGHT_J:
311 iismod |= S3C2412_IISMOD_SDF_MSB;
313 case SND_SOC_DAIFMT_LEFT_J:
314 iismod |= S3C2412_IISMOD_SDF_LSB;
316 case SND_SOC_DAIFMT_I2S:
317 iismod |= S3C2412_IISMOD_SDF_IIS;
320 pr_err("Unknown data format\n");
324 writel(iismod, i2s->regs + S3C2412_IISMOD);
325 pr_debug("hw_params w: IISMOD: %x \n", iismod);
329 static int s3c2412_i2s_hw_params(struct snd_pcm_substream *substream,
330 struct snd_pcm_hw_params *params,
331 struct snd_soc_dai *socdai)
333 struct snd_soc_pcm_runtime *rtd = substream->private_data;
334 struct snd_soc_dai_link *dai = rtd->dai;
335 struct s3c_i2sv2_info *i2s = to_info(dai->cpu_dai);
338 pr_debug("Entered %s\n", __func__);
340 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
341 dai->cpu_dai->dma_data = i2s->dma_playback;
343 dai->cpu_dai->dma_data = i2s->dma_capture;
345 /* Working copies of register */
346 iismod = readl(i2s->regs + S3C2412_IISMOD);
347 pr_debug("%s: r: IISMOD: %x\n", __func__, iismod);
349 #if defined(CONFIG_CPU_S3C2412) || defined(CONFIG_CPU_S3C2413)
350 switch (params_format(params)) {
351 case SNDRV_PCM_FORMAT_S8:
352 iismod |= S3C2412_IISMOD_8BIT;
354 case SNDRV_PCM_FORMAT_S16_LE:
355 iismod &= ~S3C2412_IISMOD_8BIT;
360 #ifdef CONFIG_PLAT_S3C64XX
361 iismod &= ~(S3C64XX_IISMOD_BLC_MASK | S3C2412_IISMOD_BCLK_MASK);
363 switch (params_format(params)) {
364 case SNDRV_PCM_FORMAT_S8:
365 /* 8 bit sample, 16fs BCLK */
366 iismod |= (S3C64XX_IISMOD_BLC_8BIT | S3C2412_IISMOD_BCLK_16FS);
368 case SNDRV_PCM_FORMAT_S16_LE:
369 /* 16 bit sample, 32fs BCLK */
371 case SNDRV_PCM_FORMAT_S24_LE:
372 /* 24 bit sample, 48fs BCLK */
373 iismod |= (S3C64XX_IISMOD_BLC_24BIT | S3C2412_IISMOD_BCLK_48FS);
378 writel(iismod, i2s->regs + S3C2412_IISMOD);
379 pr_debug("%s: w: IISMOD: %x\n", __func__, iismod);
383 static int s3c2412_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
384 struct snd_soc_dai *dai)
386 struct snd_soc_pcm_runtime *rtd = substream->private_data;
387 struct s3c_i2sv2_info *i2s = to_info(rtd->dai->cpu_dai);
388 int capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
391 int channel = ((struct s3c24xx_pcm_dma_params *)
392 rtd->dai->cpu_dai->dma_data)->channel;
394 pr_debug("Entered %s\n", __func__);
397 case SNDRV_PCM_TRIGGER_START:
398 /* On start, ensure that the FIFOs are cleared and reset. */
400 writel(capture ? S3C2412_IISFIC_RXFLUSH : S3C2412_IISFIC_TXFLUSH,
401 i2s->regs + S3C2412_IISFIC);
403 /* clear again, just in case */
404 writel(0x0, i2s->regs + S3C2412_IISFIC);
406 case SNDRV_PCM_TRIGGER_RESUME:
407 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
409 ret = s3c2412_snd_lrsync(i2s);
414 local_irq_save(irqs);
417 s3c2412_snd_rxctrl(i2s, 1);
419 s3c2412_snd_txctrl(i2s, 1);
421 local_irq_restore(irqs);
424 * Load the next buffer to DMA to meet the reqirement
425 * of the auto reload mechanism of S3C24XX.
426 * This call won't bother S3C64XX.
428 s3c2410_dma_ctrl(channel, S3C2410_DMAOP_STARTED);
432 case SNDRV_PCM_TRIGGER_STOP:
433 case SNDRV_PCM_TRIGGER_SUSPEND:
434 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
435 local_irq_save(irqs);
438 s3c2412_snd_rxctrl(i2s, 0);
440 s3c2412_snd_txctrl(i2s, 0);
442 local_irq_restore(irqs);
454 * Set S3C2412 Clock dividers
456 static int s3c2412_i2s_set_clkdiv(struct snd_soc_dai *cpu_dai,
459 struct s3c_i2sv2_info *i2s = to_info(cpu_dai);
462 pr_debug("%s(%p, %d, %d)\n", __func__, cpu_dai, div_id, div);
465 case S3C_I2SV2_DIV_BCLK:
466 reg = readl(i2s->regs + S3C2412_IISMOD);
467 reg &= ~S3C2412_IISMOD_BCLK_MASK;
468 writel(reg | div, i2s->regs + S3C2412_IISMOD);
470 pr_debug("%s: MOD=%08x\n", __func__, readl(i2s->regs + S3C2412_IISMOD));
473 case S3C_I2SV2_DIV_RCLK:
475 /* convert value to bit field */
479 div = S3C2412_IISMOD_RCLK_256FS;
483 div = S3C2412_IISMOD_RCLK_384FS;
487 div = S3C2412_IISMOD_RCLK_512FS;
491 div = S3C2412_IISMOD_RCLK_768FS;
499 reg = readl(i2s->regs + S3C2412_IISMOD);
500 reg &= ~S3C2412_IISMOD_RCLK_MASK;
501 writel(reg | div, i2s->regs + S3C2412_IISMOD);
502 pr_debug("%s: MOD=%08x\n", __func__, readl(i2s->regs + S3C2412_IISMOD));
505 case S3C_I2SV2_DIV_PRESCALER:
507 writel((div << 8) | S3C2412_IISPSR_PSREN,
508 i2s->regs + S3C2412_IISPSR);
510 writel(0x0, i2s->regs + S3C2412_IISPSR);
512 pr_debug("%s: PSR=%08x\n", __func__, readl(i2s->regs + S3C2412_IISPSR));
522 /* default table of all avaialable root fs divisors */
523 static unsigned int iis_fs_tab[] = { 256, 512, 384, 768 };
525 int s3c_i2sv2_iis_calc_rate(struct s3c_i2sv2_rate_calc *info,
527 unsigned int rate, struct clk *clk)
529 unsigned long clkrate = clk_get_rate(clk);
535 signed int deviation = 0;
536 unsigned int best_fs = 0;
537 unsigned int best_div = 0;
538 unsigned int best_rate = 0;
539 unsigned int best_deviation = INT_MAX;
541 pr_debug("Input clock rate %ldHz\n", clkrate);
546 for (fs = 0; fs < ARRAY_SIZE(iis_fs_tab); fs++) {
547 fsdiv = iis_fs_tab[fs];
549 fsclk = clkrate / fsdiv;
552 if ((fsclk % rate) > (rate / 2))
558 actual = clkrate / (fsdiv * div);
559 deviation = actual - rate;
561 printk(KERN_DEBUG "%ufs: div %u => result %u, deviation %d\n",
562 fsdiv, div, actual, deviation);
564 deviation = abs(deviation);
566 if (deviation < best_deviation) {
570 best_deviation = deviation;
577 printk(KERN_DEBUG "best: fs=%u, div=%u, rate=%u\n",
578 best_fs, best_div, best_rate);
580 info->fs_div = best_fs;
581 info->clk_div = best_div;
585 EXPORT_SYMBOL_GPL(s3c_i2sv2_iis_calc_rate);
587 int s3c_i2sv2_probe(struct platform_device *pdev,
588 struct snd_soc_dai *dai,
589 struct s3c_i2sv2_info *i2s,
592 struct device *dev = &pdev->dev;
597 /* record our i2s structure for later use in the callbacks */
598 dai->private_data = i2s;
601 struct resource *res = platform_get_resource(pdev,
605 dev_err(dev, "Unable to get register resource\n");
609 if (!request_mem_region(res->start, resource_size(res),
611 dev_err(dev, "Unable to request register region\n");
618 i2s->regs = ioremap(base, 0x100);
619 if (i2s->regs == NULL) {
620 dev_err(dev, "cannot ioremap registers\n");
624 i2s->iis_pclk = clk_get(dev, "iis");
625 if (i2s->iis_pclk == NULL) {
626 dev_err(dev, "failed to get iis_clock\n");
631 clk_enable(i2s->iis_pclk);
633 /* Mark ourselves as in TXRX mode so we can run through our cleanup
634 * process without warnings. */
635 iismod = readl(i2s->regs + S3C2412_IISMOD);
636 iismod |= S3C2412_IISMOD_MODE_TXRX;
637 writel(iismod, i2s->regs + S3C2412_IISMOD);
638 s3c2412_snd_txctrl(i2s, 0);
639 s3c2412_snd_rxctrl(i2s, 0);
643 EXPORT_SYMBOL_GPL(s3c_i2sv2_probe);
646 static int s3c2412_i2s_suspend(struct snd_soc_dai *dai)
648 struct s3c_i2sv2_info *i2s = to_info(dai);
652 i2s->suspend_iismod = readl(i2s->regs + S3C2412_IISMOD);
653 i2s->suspend_iiscon = readl(i2s->regs + S3C2412_IISCON);
654 i2s->suspend_iispsr = readl(i2s->regs + S3C2412_IISPSR);
656 /* some basic suspend checks */
658 iismod = readl(i2s->regs + S3C2412_IISMOD);
660 if (iismod & S3C2412_IISCON_RXDMA_ACTIVE)
661 pr_warning("%s: RXDMA active?\n", __func__);
663 if (iismod & S3C2412_IISCON_TXDMA_ACTIVE)
664 pr_warning("%s: TXDMA active?\n", __func__);
666 if (iismod & S3C2412_IISCON_IIS_ACTIVE)
667 pr_warning("%s: IIS active\n", __func__);
673 static int s3c2412_i2s_resume(struct snd_soc_dai *dai)
675 struct s3c_i2sv2_info *i2s = to_info(dai);
677 pr_info("dai_active %d, IISMOD %08x, IISCON %08x\n",
678 dai->active, i2s->suspend_iismod, i2s->suspend_iiscon);
681 writel(i2s->suspend_iiscon, i2s->regs + S3C2412_IISCON);
682 writel(i2s->suspend_iismod, i2s->regs + S3C2412_IISMOD);
683 writel(i2s->suspend_iispsr, i2s->regs + S3C2412_IISPSR);
685 writel(S3C2412_IISFIC_RXFLUSH | S3C2412_IISFIC_TXFLUSH,
686 i2s->regs + S3C2412_IISFIC);
689 writel(0x0, i2s->regs + S3C2412_IISFIC);
695 #define s3c2412_i2s_suspend NULL
696 #define s3c2412_i2s_resume NULL
699 int s3c_i2sv2_register_dai(struct snd_soc_dai *dai)
701 struct snd_soc_dai_ops *ops = dai->ops;
703 ops->trigger = s3c2412_i2s_trigger;
704 ops->hw_params = s3c2412_i2s_hw_params;
705 ops->set_fmt = s3c2412_i2s_set_fmt;
706 ops->set_clkdiv = s3c2412_i2s_set_clkdiv;
708 dai->suspend = s3c2412_i2s_suspend;
709 dai->resume = s3c2412_i2s_resume;
711 return snd_soc_register_dai(dai);
713 EXPORT_SYMBOL_GPL(s3c_i2sv2_register_dai);
715 MODULE_LICENSE("GPL");