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/delay.h>
20 #include <linux/clk.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
29 #include "regs-i2s-v2.h"
30 #include "s3c-i2s-v2.h"
33 #undef S3C_IIS_V2_SUPPORTED
35 #if defined(CONFIG_CPU_S3C2412) || defined(CONFIG_CPU_S3C2413)
36 #define S3C_IIS_V2_SUPPORTED
39 #ifdef CONFIG_PLAT_S3C64XX
40 #define S3C_IIS_V2_SUPPORTED
43 #ifndef S3C_IIS_V2_SUPPORTED
44 #error Unsupported CPU model
47 #define S3C2412_I2S_DEBUG_CON 0
49 static inline struct s3c_i2sv2_info *to_info(struct snd_soc_dai *cpu_dai)
51 return cpu_dai->private_data;
54 #define bit_set(v, b) (((v) & (b)) ? 1 : 0)
56 #if S3C2412_I2S_DEBUG_CON
57 static void dbg_showcon(const char *fn, u32 con)
59 printk(KERN_DEBUG "%s: LRI=%d, TXFEMPT=%d, RXFEMPT=%d, TXFFULL=%d, RXFFULL=%d\n", fn,
60 bit_set(con, S3C2412_IISCON_LRINDEX),
61 bit_set(con, S3C2412_IISCON_TXFIFO_EMPTY),
62 bit_set(con, S3C2412_IISCON_RXFIFO_EMPTY),
63 bit_set(con, S3C2412_IISCON_TXFIFO_FULL),
64 bit_set(con, S3C2412_IISCON_RXFIFO_FULL));
66 printk(KERN_DEBUG "%s: PAUSE: TXDMA=%d, RXDMA=%d, TXCH=%d, RXCH=%d\n",
68 bit_set(con, S3C2412_IISCON_TXDMA_PAUSE),
69 bit_set(con, S3C2412_IISCON_RXDMA_PAUSE),
70 bit_set(con, S3C2412_IISCON_TXCH_PAUSE),
71 bit_set(con, S3C2412_IISCON_RXCH_PAUSE));
72 printk(KERN_DEBUG "%s: ACTIVE: TXDMA=%d, RXDMA=%d, IIS=%d\n", fn,
73 bit_set(con, S3C2412_IISCON_TXDMA_ACTIVE),
74 bit_set(con, S3C2412_IISCON_RXDMA_ACTIVE),
75 bit_set(con, S3C2412_IISCON_IIS_ACTIVE));
78 static inline void dbg_showcon(const char *fn, u32 con)
84 /* Turn on or off the transmission path. */
85 static void s3c2412_snd_txctrl(struct s3c_i2sv2_info *i2s, int on)
87 void __iomem *regs = i2s->regs;
90 pr_debug("%s(%d)\n", __func__, on);
92 fic = readl(regs + S3C2412_IISFIC);
93 con = readl(regs + S3C2412_IISCON);
94 mod = readl(regs + S3C2412_IISMOD);
96 pr_debug("%s: IIS: CON=%x MOD=%x FIC=%x\n", __func__, con, mod, fic);
99 con |= S3C2412_IISCON_TXDMA_ACTIVE | S3C2412_IISCON_IIS_ACTIVE;
100 con &= ~S3C2412_IISCON_TXDMA_PAUSE;
101 con &= ~S3C2412_IISCON_TXCH_PAUSE;
103 switch (mod & S3C2412_IISMOD_MODE_MASK) {
104 case S3C2412_IISMOD_MODE_TXONLY:
105 case S3C2412_IISMOD_MODE_TXRX:
106 /* do nothing, we are in the right mode */
109 case S3C2412_IISMOD_MODE_RXONLY:
110 mod &= ~S3C2412_IISMOD_MODE_MASK;
111 mod |= S3C2412_IISMOD_MODE_TXRX;
115 dev_err(i2s->dev, "TXEN: Invalid MODE %x in IISMOD\n",
116 mod & S3C2412_IISMOD_MODE_MASK);
120 writel(con, regs + S3C2412_IISCON);
121 writel(mod, regs + S3C2412_IISMOD);
123 /* Note, we do not have any indication that the FIFO problems
124 * tha the S3C2410/2440 had apply here, so we should be able
125 * to disable the DMA and TX without resetting the FIFOS.
128 con |= S3C2412_IISCON_TXDMA_PAUSE;
129 con |= S3C2412_IISCON_TXCH_PAUSE;
130 con &= ~S3C2412_IISCON_TXDMA_ACTIVE;
132 switch (mod & S3C2412_IISMOD_MODE_MASK) {
133 case S3C2412_IISMOD_MODE_TXRX:
134 mod &= ~S3C2412_IISMOD_MODE_MASK;
135 mod |= S3C2412_IISMOD_MODE_RXONLY;
138 case S3C2412_IISMOD_MODE_TXONLY:
139 mod &= ~S3C2412_IISMOD_MODE_MASK;
140 con &= ~S3C2412_IISCON_IIS_ACTIVE;
144 dev_err(i2s->dev, "TXDIS: Invalid MODE %x in IISMOD\n",
145 mod & S3C2412_IISMOD_MODE_MASK);
149 writel(mod, regs + S3C2412_IISMOD);
150 writel(con, regs + S3C2412_IISCON);
153 fic = readl(regs + S3C2412_IISFIC);
154 dbg_showcon(__func__, con);
155 pr_debug("%s: IIS: CON=%x MOD=%x FIC=%x\n", __func__, con, mod, fic);
158 static void s3c2412_snd_rxctrl(struct s3c_i2sv2_info *i2s, int on)
160 void __iomem *regs = i2s->regs;
163 pr_debug("%s(%d)\n", __func__, on);
165 fic = readl(regs + S3C2412_IISFIC);
166 con = readl(regs + S3C2412_IISCON);
167 mod = readl(regs + S3C2412_IISMOD);
169 pr_debug("%s: IIS: CON=%x MOD=%x FIC=%x\n", __func__, con, mod, fic);
172 con |= S3C2412_IISCON_RXDMA_ACTIVE | S3C2412_IISCON_IIS_ACTIVE;
173 con &= ~S3C2412_IISCON_RXDMA_PAUSE;
174 con &= ~S3C2412_IISCON_RXCH_PAUSE;
176 switch (mod & S3C2412_IISMOD_MODE_MASK) {
177 case S3C2412_IISMOD_MODE_TXRX:
178 case S3C2412_IISMOD_MODE_RXONLY:
179 /* do nothing, we are in the right mode */
182 case S3C2412_IISMOD_MODE_TXONLY:
183 mod &= ~S3C2412_IISMOD_MODE_MASK;
184 mod |= S3C2412_IISMOD_MODE_TXRX;
188 dev_err(i2s->dev, "RXEN: Invalid MODE %x in IISMOD\n",
189 mod & S3C2412_IISMOD_MODE_MASK);
192 writel(mod, regs + S3C2412_IISMOD);
193 writel(con, regs + S3C2412_IISCON);
195 /* See txctrl notes on FIFOs. */
197 con &= ~S3C2412_IISCON_RXDMA_ACTIVE;
198 con |= S3C2412_IISCON_RXDMA_PAUSE;
199 con |= S3C2412_IISCON_RXCH_PAUSE;
201 switch (mod & S3C2412_IISMOD_MODE_MASK) {
202 case S3C2412_IISMOD_MODE_RXONLY:
203 con &= ~S3C2412_IISCON_IIS_ACTIVE;
204 mod &= ~S3C2412_IISMOD_MODE_MASK;
207 case S3C2412_IISMOD_MODE_TXRX:
208 mod &= ~S3C2412_IISMOD_MODE_MASK;
209 mod |= S3C2412_IISMOD_MODE_TXONLY;
213 dev_err(i2s->dev, "RXDIS: Invalid MODE %x in IISMOD\n",
214 mod & S3C2412_IISMOD_MODE_MASK);
217 writel(con, regs + S3C2412_IISCON);
218 writel(mod, regs + S3C2412_IISMOD);
221 fic = readl(regs + S3C2412_IISFIC);
222 pr_debug("%s: IIS: CON=%x MOD=%x FIC=%x\n", __func__, con, mod, fic);
225 #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
228 * Wait for the LR signal to allow synchronisation to the L/R clock
229 * from the codec. May only be needed for slave mode.
231 static int s3c2412_snd_lrsync(struct s3c_i2sv2_info *i2s)
234 unsigned long loops = msecs_to_loops(5);
236 pr_debug("Entered %s\n", __func__);
239 iiscon = readl(i2s->regs + S3C2412_IISCON);
240 if (iiscon & S3C2412_IISCON_LRINDEX)
247 printk(KERN_ERR "%s: timeout\n", __func__);
255 * Set S3C2412 I2S DAI format
257 static int s3c2412_i2s_set_fmt(struct snd_soc_dai *cpu_dai,
260 struct s3c_i2sv2_info *i2s = to_info(cpu_dai);
263 pr_debug("Entered %s\n", __func__);
265 iismod = readl(i2s->regs + S3C2412_IISMOD);
266 pr_debug("hw_params r: IISMOD: %x \n", iismod);
268 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
269 case SND_SOC_DAIFMT_CBM_CFM:
271 iismod |= S3C2412_IISMOD_SLAVE;
273 case SND_SOC_DAIFMT_CBS_CFS:
275 iismod &= ~S3C2412_IISMOD_SLAVE;
278 pr_err("unknwon master/slave format\n");
282 iismod &= ~S3C2412_IISMOD_SDF_MASK;
284 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
285 case SND_SOC_DAIFMT_RIGHT_J:
286 iismod |= S3C2412_IISMOD_LR_RLOW;
287 iismod |= S3C2412_IISMOD_SDF_MSB;
289 case SND_SOC_DAIFMT_LEFT_J:
290 iismod |= S3C2412_IISMOD_LR_RLOW;
291 iismod |= S3C2412_IISMOD_SDF_LSB;
293 case SND_SOC_DAIFMT_I2S:
294 iismod &= ~S3C2412_IISMOD_LR_RLOW;
295 iismod |= S3C2412_IISMOD_SDF_IIS;
298 pr_err("Unknown data format\n");
302 writel(iismod, i2s->regs + S3C2412_IISMOD);
303 pr_debug("hw_params w: IISMOD: %x \n", iismod);
307 static int s3c_i2sv2_hw_params(struct snd_pcm_substream *substream,
308 struct snd_pcm_hw_params *params,
309 struct snd_soc_dai *socdai)
311 struct snd_soc_pcm_runtime *rtd = substream->private_data;
312 struct snd_soc_dai_link *dai = rtd->dai;
313 struct s3c_i2sv2_info *i2s = to_info(dai->cpu_dai);
314 struct s3c_dma_params *dma_data;
317 pr_debug("Entered %s\n", __func__);
319 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
320 dma_data = i2s->dma_playback;
322 dma_data = i2s->dma_capture;
324 snd_soc_dai_set_dma_data(dai->cpu_dai, substream, dma_data);
326 /* Working copies of register */
327 iismod = readl(i2s->regs + S3C2412_IISMOD);
328 pr_debug("%s: r: IISMOD: %x\n", __func__, iismod);
330 iismod &= ~S3C64XX_IISMOD_BLC_MASK;
332 switch (params_format(params)) {
333 case SNDRV_PCM_FORMAT_S8:
334 iismod |= S3C64XX_IISMOD_BLC_8BIT;
336 case SNDRV_PCM_FORMAT_S16_LE:
338 case SNDRV_PCM_FORMAT_S24_LE:
339 iismod |= S3C64XX_IISMOD_BLC_24BIT;
343 writel(iismod, i2s->regs + S3C2412_IISMOD);
344 pr_debug("%s: w: IISMOD: %x\n", __func__, iismod);
349 static int s3c_i2sv2_set_sysclk(struct snd_soc_dai *cpu_dai,
350 int clk_id, unsigned int freq, int dir)
352 struct s3c_i2sv2_info *i2s = to_info(cpu_dai);
353 u32 iismod = readl(i2s->regs + S3C2412_IISMOD);
355 pr_debug("Entered %s\n", __func__);
356 pr_debug("%s r: IISMOD: %x\n", __func__, iismod);
359 case S3C_I2SV2_CLKSRC_PCLK:
360 iismod &= ~S3C2412_IISMOD_IMS_SYSMUX;
363 case S3C_I2SV2_CLKSRC_AUDIOBUS:
364 iismod |= S3C2412_IISMOD_IMS_SYSMUX;
367 case S3C_I2SV2_CLKSRC_CDCLK:
368 /* Error if controller doesn't have the CDCLKCON bit */
369 if (!(i2s->feature & S3C_FEATURE_CDCLKCON))
373 case SND_SOC_CLOCK_IN:
374 iismod |= S3C64XX_IISMOD_CDCLKCON;
376 case SND_SOC_CLOCK_OUT:
377 iismod &= ~S3C64XX_IISMOD_CDCLKCON;
388 writel(iismod, i2s->regs + S3C2412_IISMOD);
389 pr_debug("%s w: IISMOD: %x\n", __func__, iismod);
394 static int s3c2412_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
395 struct snd_soc_dai *dai)
397 struct snd_soc_pcm_runtime *rtd = substream->private_data;
398 struct s3c_i2sv2_info *i2s = to_info(rtd->dai->cpu_dai);
399 int capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
402 struct s3c_dma_params *dma_data =
403 snd_soc_dai_get_dma_data(rtd->dai->cpu_dai, substream);
405 pr_debug("Entered %s\n", __func__);
408 case SNDRV_PCM_TRIGGER_START:
409 /* On start, ensure that the FIFOs are cleared and reset. */
411 writel(capture ? S3C2412_IISFIC_RXFLUSH : S3C2412_IISFIC_TXFLUSH,
412 i2s->regs + S3C2412_IISFIC);
414 /* clear again, just in case */
415 writel(0x0, i2s->regs + S3C2412_IISFIC);
417 case SNDRV_PCM_TRIGGER_RESUME:
418 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
420 ret = s3c2412_snd_lrsync(i2s);
425 local_irq_save(irqs);
428 s3c2412_snd_rxctrl(i2s, 1);
430 s3c2412_snd_txctrl(i2s, 1);
432 local_irq_restore(irqs);
435 * Load the next buffer to DMA to meet the reqirement
436 * of the auto reload mechanism of S3C24XX.
437 * This call won't bother S3C64XX.
439 s3c2410_dma_ctrl(dma_data->channel, S3C2410_DMAOP_STARTED);
443 case SNDRV_PCM_TRIGGER_STOP:
444 case SNDRV_PCM_TRIGGER_SUSPEND:
445 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
446 local_irq_save(irqs);
449 s3c2412_snd_rxctrl(i2s, 0);
451 s3c2412_snd_txctrl(i2s, 0);
453 local_irq_restore(irqs);
465 * Set S3C2412 Clock dividers
467 static int s3c2412_i2s_set_clkdiv(struct snd_soc_dai *cpu_dai,
470 struct s3c_i2sv2_info *i2s = to_info(cpu_dai);
473 pr_debug("%s(%p, %d, %d)\n", __func__, cpu_dai, div_id, div);
476 case S3C_I2SV2_DIV_BCLK:
479 div = S3C2412_IISMOD_BCLK_16FS;
483 div = S3C2412_IISMOD_BCLK_32FS;
487 div = S3C2412_IISMOD_BCLK_24FS;
491 div = S3C2412_IISMOD_BCLK_48FS;
498 reg = readl(i2s->regs + S3C2412_IISMOD);
499 reg &= ~S3C2412_IISMOD_BCLK_MASK;
500 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_RCLK:
508 div = S3C2412_IISMOD_RCLK_256FS;
512 div = S3C2412_IISMOD_RCLK_384FS;
516 div = S3C2412_IISMOD_RCLK_512FS;
520 div = S3C2412_IISMOD_RCLK_768FS;
527 reg = readl(i2s->regs + S3C2412_IISMOD);
528 reg &= ~S3C2412_IISMOD_RCLK_MASK;
529 writel(reg | div, i2s->regs + S3C2412_IISMOD);
530 pr_debug("%s: MOD=%08x\n", __func__, readl(i2s->regs + S3C2412_IISMOD));
533 case S3C_I2SV2_DIV_PRESCALER:
535 writel((div << 8) | S3C2412_IISPSR_PSREN,
536 i2s->regs + S3C2412_IISPSR);
538 writel(0x0, i2s->regs + S3C2412_IISPSR);
540 pr_debug("%s: PSR=%08x\n", __func__, readl(i2s->regs + S3C2412_IISPSR));
550 static snd_pcm_sframes_t s3c2412_i2s_delay(struct snd_pcm_substream *substream,
551 struct snd_soc_dai *dai)
553 struct s3c_i2sv2_info *i2s = to_info(dai);
554 u32 reg = readl(i2s->regs + S3C2412_IISFIC);
555 snd_pcm_sframes_t delay;
557 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
558 delay = S3C2412_IISFIC_TXCOUNT(reg);
560 delay = S3C2412_IISFIC_RXCOUNT(reg);
565 struct clk *s3c_i2sv2_get_clock(struct snd_soc_dai *cpu_dai)
567 struct s3c_i2sv2_info *i2s = to_info(cpu_dai);
568 u32 iismod = readl(i2s->regs + S3C2412_IISMOD);
570 if (iismod & S3C2412_IISMOD_IMS_SYSMUX)
571 return i2s->iis_cclk;
573 return i2s->iis_pclk;
575 EXPORT_SYMBOL_GPL(s3c_i2sv2_get_clock);
577 /* default table of all avaialable root fs divisors */
578 static unsigned int iis_fs_tab[] = { 256, 512, 384, 768 };
580 int s3c_i2sv2_iis_calc_rate(struct s3c_i2sv2_rate_calc *info,
582 unsigned int rate, struct clk *clk)
584 unsigned long clkrate = clk_get_rate(clk);
590 signed int deviation = 0;
591 unsigned int best_fs = 0;
592 unsigned int best_div = 0;
593 unsigned int best_rate = 0;
594 unsigned int best_deviation = INT_MAX;
596 pr_debug("Input clock rate %ldHz\n", clkrate);
601 for (fs = 0; fs < ARRAY_SIZE(iis_fs_tab); fs++) {
602 fsdiv = iis_fs_tab[fs];
604 fsclk = clkrate / fsdiv;
607 if ((fsclk % rate) > (rate / 2))
613 actual = clkrate / (fsdiv * div);
614 deviation = actual - rate;
616 printk(KERN_DEBUG "%ufs: div %u => result %u, deviation %d\n",
617 fsdiv, div, actual, deviation);
619 deviation = abs(deviation);
621 if (deviation < best_deviation) {
625 best_deviation = deviation;
632 printk(KERN_DEBUG "best: fs=%u, div=%u, rate=%u\n",
633 best_fs, best_div, best_rate);
635 info->fs_div = best_fs;
636 info->clk_div = best_div;
640 EXPORT_SYMBOL_GPL(s3c_i2sv2_iis_calc_rate);
642 int s3c_i2sv2_probe(struct platform_device *pdev,
643 struct snd_soc_dai *dai,
644 struct s3c_i2sv2_info *i2s,
647 struct device *dev = &pdev->dev;
652 /* record our i2s structure for later use in the callbacks */
653 dai->private_data = i2s;
656 struct resource *res = platform_get_resource(pdev,
660 dev_err(dev, "Unable to get register resource\n");
664 if (!request_mem_region(res->start, resource_size(res),
666 dev_err(dev, "Unable to request register region\n");
673 i2s->regs = ioremap(base, 0x100);
674 if (i2s->regs == NULL) {
675 dev_err(dev, "cannot ioremap registers\n");
679 i2s->iis_pclk = clk_get(dev, "iis");
680 if (IS_ERR(i2s->iis_pclk)) {
681 dev_err(dev, "failed to get iis_clock\n");
686 clk_enable(i2s->iis_pclk);
688 /* Mark ourselves as in TXRX mode so we can run through our cleanup
689 * process without warnings. */
690 iismod = readl(i2s->regs + S3C2412_IISMOD);
691 iismod |= S3C2412_IISMOD_MODE_TXRX;
692 writel(iismod, i2s->regs + S3C2412_IISMOD);
693 s3c2412_snd_txctrl(i2s, 0);
694 s3c2412_snd_rxctrl(i2s, 0);
698 EXPORT_SYMBOL_GPL(s3c_i2sv2_probe);
701 static int s3c2412_i2s_suspend(struct snd_soc_dai *dai)
703 struct s3c_i2sv2_info *i2s = to_info(dai);
707 i2s->suspend_iismod = readl(i2s->regs + S3C2412_IISMOD);
708 i2s->suspend_iiscon = readl(i2s->regs + S3C2412_IISCON);
709 i2s->suspend_iispsr = readl(i2s->regs + S3C2412_IISPSR);
711 /* some basic suspend checks */
713 iismod = readl(i2s->regs + S3C2412_IISMOD);
715 if (iismod & S3C2412_IISCON_RXDMA_ACTIVE)
716 pr_warning("%s: RXDMA active?\n", __func__);
718 if (iismod & S3C2412_IISCON_TXDMA_ACTIVE)
719 pr_warning("%s: TXDMA active?\n", __func__);
721 if (iismod & S3C2412_IISCON_IIS_ACTIVE)
722 pr_warning("%s: IIS active\n", __func__);
728 static int s3c2412_i2s_resume(struct snd_soc_dai *dai)
730 struct s3c_i2sv2_info *i2s = to_info(dai);
732 pr_info("dai_active %d, IISMOD %08x, IISCON %08x\n",
733 dai->active, i2s->suspend_iismod, i2s->suspend_iiscon);
736 writel(i2s->suspend_iiscon, i2s->regs + S3C2412_IISCON);
737 writel(i2s->suspend_iismod, i2s->regs + S3C2412_IISMOD);
738 writel(i2s->suspend_iispsr, i2s->regs + S3C2412_IISPSR);
740 writel(S3C2412_IISFIC_RXFLUSH | S3C2412_IISFIC_TXFLUSH,
741 i2s->regs + S3C2412_IISFIC);
744 writel(0x0, i2s->regs + S3C2412_IISFIC);
750 #define s3c2412_i2s_suspend NULL
751 #define s3c2412_i2s_resume NULL
754 int s3c_i2sv2_register_dai(struct snd_soc_dai *dai)
756 struct snd_soc_dai_ops *ops = dai->ops;
758 ops->trigger = s3c2412_i2s_trigger;
760 ops->hw_params = s3c_i2sv2_hw_params;
761 ops->set_fmt = s3c2412_i2s_set_fmt;
762 ops->set_clkdiv = s3c2412_i2s_set_clkdiv;
763 ops->set_sysclk = s3c_i2sv2_set_sysclk;
765 /* Allow overriding by (for example) IISv4 */
767 ops->delay = s3c2412_i2s_delay;
769 dai->suspend = s3c2412_i2s_suspend;
770 dai->resume = s3c2412_i2s_resume;
772 return snd_soc_register_dai(dai);
774 EXPORT_SYMBOL_GPL(s3c_i2sv2_register_dai);
776 MODULE_LICENSE("GPL");