1 /* ALSA Soc Audio Layer - I2S core for newer Samsung SoCs.
3 * Copyright (c) 2006 Wolfson Microelectronics PLC.
4 * Graeme Gregory graeme.gregory@wolfsonmicro.com
5 * linux@wolfsonmicro.com
7 * Copyright (c) 2008, 2007, 2004-2005 Simtec Electronics
8 * http://armlinux.simtec.co.uk/
9 * Ben Dooks <ben@simtec.co.uk>
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.
17 #include <linux/module.h>
18 #include <linux/delay.h>
19 #include <linux/clk.h>
22 #include <sound/soc.h>
23 #include <sound/pcm_params.h>
25 #include "regs-i2s-v2.h"
26 #include "s3c-i2s-v2.h"
29 #undef S3C_IIS_V2_SUPPORTED
31 #if defined(CONFIG_CPU_S3C2412) || defined(CONFIG_CPU_S3C2413) \
32 || defined(CONFIG_ARCH_S3C64XX) || defined(CONFIG_CPU_S5PV210)
33 #define S3C_IIS_V2_SUPPORTED
36 #ifndef S3C_IIS_V2_SUPPORTED
37 #error Unsupported CPU model
40 #define S3C2412_I2S_DEBUG_CON 0
42 static inline struct s3c_i2sv2_info *to_info(struct snd_soc_dai *cpu_dai)
44 return snd_soc_dai_get_drvdata(cpu_dai);
47 #define bit_set(v, b) (((v) & (b)) ? 1 : 0)
49 #if S3C2412_I2S_DEBUG_CON
50 static void dbg_showcon(const char *fn, u32 con)
52 printk(KERN_DEBUG "%s: LRI=%d, TXFEMPT=%d, RXFEMPT=%d, TXFFULL=%d, RXFFULL=%d\n", fn,
53 bit_set(con, S3C2412_IISCON_LRINDEX),
54 bit_set(con, S3C2412_IISCON_TXFIFO_EMPTY),
55 bit_set(con, S3C2412_IISCON_RXFIFO_EMPTY),
56 bit_set(con, S3C2412_IISCON_TXFIFO_FULL),
57 bit_set(con, S3C2412_IISCON_RXFIFO_FULL));
59 printk(KERN_DEBUG "%s: PAUSE: TXDMA=%d, RXDMA=%d, TXCH=%d, RXCH=%d\n",
61 bit_set(con, S3C2412_IISCON_TXDMA_PAUSE),
62 bit_set(con, S3C2412_IISCON_RXDMA_PAUSE),
63 bit_set(con, S3C2412_IISCON_TXCH_PAUSE),
64 bit_set(con, S3C2412_IISCON_RXCH_PAUSE));
65 printk(KERN_DEBUG "%s: ACTIVE: TXDMA=%d, RXDMA=%d, IIS=%d\n", fn,
66 bit_set(con, S3C2412_IISCON_TXDMA_ACTIVE),
67 bit_set(con, S3C2412_IISCON_RXDMA_ACTIVE),
68 bit_set(con, S3C2412_IISCON_IIS_ACTIVE));
71 static inline void dbg_showcon(const char *fn, u32 con)
77 /* Turn on or off the transmission path. */
78 static void s3c2412_snd_txctrl(struct s3c_i2sv2_info *i2s, int on)
80 void __iomem *regs = i2s->regs;
83 pr_debug("%s(%d)\n", __func__, on);
85 fic = readl(regs + S3C2412_IISFIC);
86 con = readl(regs + S3C2412_IISCON);
87 mod = readl(regs + S3C2412_IISMOD);
89 pr_debug("%s: IIS: CON=%x MOD=%x FIC=%x\n", __func__, con, mod, fic);
92 con |= S3C2412_IISCON_TXDMA_ACTIVE | S3C2412_IISCON_IIS_ACTIVE;
93 con &= ~S3C2412_IISCON_TXDMA_PAUSE;
94 con &= ~S3C2412_IISCON_TXCH_PAUSE;
96 switch (mod & S3C2412_IISMOD_MODE_MASK) {
97 case S3C2412_IISMOD_MODE_TXONLY:
98 case S3C2412_IISMOD_MODE_TXRX:
99 /* do nothing, we are in the right mode */
102 case S3C2412_IISMOD_MODE_RXONLY:
103 mod &= ~S3C2412_IISMOD_MODE_MASK;
104 mod |= S3C2412_IISMOD_MODE_TXRX;
108 dev_err(i2s->dev, "TXEN: Invalid MODE %x in IISMOD\n",
109 mod & S3C2412_IISMOD_MODE_MASK);
113 writel(con, regs + S3C2412_IISCON);
114 writel(mod, regs + S3C2412_IISMOD);
116 /* Note, we do not have any indication that the FIFO problems
117 * tha the S3C2410/2440 had apply here, so we should be able
118 * to disable the DMA and TX without resetting the FIFOS.
121 con |= S3C2412_IISCON_TXDMA_PAUSE;
122 con |= S3C2412_IISCON_TXCH_PAUSE;
123 con &= ~S3C2412_IISCON_TXDMA_ACTIVE;
125 switch (mod & S3C2412_IISMOD_MODE_MASK) {
126 case S3C2412_IISMOD_MODE_TXRX:
127 mod &= ~S3C2412_IISMOD_MODE_MASK;
128 mod |= S3C2412_IISMOD_MODE_RXONLY;
131 case S3C2412_IISMOD_MODE_TXONLY:
132 mod &= ~S3C2412_IISMOD_MODE_MASK;
133 con &= ~S3C2412_IISCON_IIS_ACTIVE;
137 dev_err(i2s->dev, "TXDIS: Invalid MODE %x in IISMOD\n",
138 mod & S3C2412_IISMOD_MODE_MASK);
142 writel(mod, regs + S3C2412_IISMOD);
143 writel(con, regs + S3C2412_IISCON);
146 fic = readl(regs + S3C2412_IISFIC);
147 dbg_showcon(__func__, con);
148 pr_debug("%s: IIS: CON=%x MOD=%x FIC=%x\n", __func__, con, mod, fic);
151 static void s3c2412_snd_rxctrl(struct s3c_i2sv2_info *i2s, int on)
153 void __iomem *regs = i2s->regs;
156 pr_debug("%s(%d)\n", __func__, on);
158 fic = readl(regs + S3C2412_IISFIC);
159 con = readl(regs + S3C2412_IISCON);
160 mod = readl(regs + S3C2412_IISMOD);
162 pr_debug("%s: IIS: CON=%x MOD=%x FIC=%x\n", __func__, con, mod, fic);
165 con |= S3C2412_IISCON_RXDMA_ACTIVE | S3C2412_IISCON_IIS_ACTIVE;
166 con &= ~S3C2412_IISCON_RXDMA_PAUSE;
167 con &= ~S3C2412_IISCON_RXCH_PAUSE;
169 switch (mod & S3C2412_IISMOD_MODE_MASK) {
170 case S3C2412_IISMOD_MODE_TXRX:
171 case S3C2412_IISMOD_MODE_RXONLY:
172 /* do nothing, we are in the right mode */
175 case S3C2412_IISMOD_MODE_TXONLY:
176 mod &= ~S3C2412_IISMOD_MODE_MASK;
177 mod |= S3C2412_IISMOD_MODE_TXRX;
181 dev_err(i2s->dev, "RXEN: Invalid MODE %x in IISMOD\n",
182 mod & S3C2412_IISMOD_MODE_MASK);
185 writel(mod, regs + S3C2412_IISMOD);
186 writel(con, regs + S3C2412_IISCON);
188 /* See txctrl notes on FIFOs. */
190 con &= ~S3C2412_IISCON_RXDMA_ACTIVE;
191 con |= S3C2412_IISCON_RXDMA_PAUSE;
192 con |= S3C2412_IISCON_RXCH_PAUSE;
194 switch (mod & S3C2412_IISMOD_MODE_MASK) {
195 case S3C2412_IISMOD_MODE_RXONLY:
196 con &= ~S3C2412_IISCON_IIS_ACTIVE;
197 mod &= ~S3C2412_IISMOD_MODE_MASK;
200 case S3C2412_IISMOD_MODE_TXRX:
201 mod &= ~S3C2412_IISMOD_MODE_MASK;
202 mod |= S3C2412_IISMOD_MODE_TXONLY;
206 dev_err(i2s->dev, "RXDIS: Invalid MODE %x in IISMOD\n",
207 mod & S3C2412_IISMOD_MODE_MASK);
210 writel(con, regs + S3C2412_IISCON);
211 writel(mod, regs + S3C2412_IISMOD);
214 fic = readl(regs + S3C2412_IISFIC);
215 pr_debug("%s: IIS: CON=%x MOD=%x FIC=%x\n", __func__, con, mod, fic);
218 #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
221 * Wait for the LR signal to allow synchronisation to the L/R clock
222 * from the codec. May only be needed for slave mode.
224 static int s3c2412_snd_lrsync(struct s3c_i2sv2_info *i2s)
227 unsigned long loops = msecs_to_loops(5);
229 pr_debug("Entered %s\n", __func__);
232 iiscon = readl(i2s->regs + S3C2412_IISCON);
233 if (iiscon & S3C2412_IISCON_LRINDEX)
240 printk(KERN_ERR "%s: timeout\n", __func__);
248 * Set S3C2412 I2S DAI format
250 static int s3c2412_i2s_set_fmt(struct snd_soc_dai *cpu_dai,
253 struct s3c_i2sv2_info *i2s = to_info(cpu_dai);
256 pr_debug("Entered %s\n", __func__);
258 iismod = readl(i2s->regs + S3C2412_IISMOD);
259 pr_debug("hw_params r: IISMOD: %x \n", iismod);
261 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
262 case SND_SOC_DAIFMT_CBM_CFM:
264 iismod |= S3C2412_IISMOD_SLAVE;
266 case SND_SOC_DAIFMT_CBS_CFS:
268 iismod &= ~S3C2412_IISMOD_SLAVE;
271 pr_err("unknwon master/slave format\n");
275 iismod &= ~S3C2412_IISMOD_SDF_MASK;
277 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
278 case SND_SOC_DAIFMT_RIGHT_J:
279 iismod |= S3C2412_IISMOD_LR_RLOW;
280 iismod |= S3C2412_IISMOD_SDF_MSB;
282 case SND_SOC_DAIFMT_LEFT_J:
283 iismod |= S3C2412_IISMOD_LR_RLOW;
284 iismod |= S3C2412_IISMOD_SDF_LSB;
286 case SND_SOC_DAIFMT_I2S:
287 iismod &= ~S3C2412_IISMOD_LR_RLOW;
288 iismod |= S3C2412_IISMOD_SDF_IIS;
291 pr_err("Unknown data format\n");
295 writel(iismod, i2s->regs + S3C2412_IISMOD);
296 pr_debug("hw_params w: IISMOD: %x \n", iismod);
300 static int s3c_i2sv2_hw_params(struct snd_pcm_substream *substream,
301 struct snd_pcm_hw_params *params,
302 struct snd_soc_dai *dai)
304 struct s3c_i2sv2_info *i2s = to_info(dai);
305 struct s3c_dma_params *dma_data;
308 pr_debug("Entered %s\n", __func__);
310 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
311 dma_data = i2s->dma_playback;
313 dma_data = i2s->dma_capture;
315 snd_soc_dai_set_dma_data(dai, substream, dma_data);
317 /* Working copies of register */
318 iismod = readl(i2s->regs + S3C2412_IISMOD);
319 pr_debug("%s: r: IISMOD: %x\n", __func__, iismod);
321 iismod &= ~S3C64XX_IISMOD_BLC_MASK;
323 switch (params_width(params)) {
325 iismod |= S3C64XX_IISMOD_BLC_8BIT;
330 iismod |= S3C64XX_IISMOD_BLC_24BIT;
334 writel(iismod, i2s->regs + S3C2412_IISMOD);
335 pr_debug("%s: w: IISMOD: %x\n", __func__, iismod);
340 static int s3c_i2sv2_set_sysclk(struct snd_soc_dai *cpu_dai,
341 int clk_id, unsigned int freq, int dir)
343 struct s3c_i2sv2_info *i2s = to_info(cpu_dai);
344 u32 iismod = readl(i2s->regs + S3C2412_IISMOD);
346 pr_debug("Entered %s\n", __func__);
347 pr_debug("%s r: IISMOD: %x\n", __func__, iismod);
350 case S3C_I2SV2_CLKSRC_PCLK:
351 iismod &= ~S3C2412_IISMOD_IMS_SYSMUX;
354 case S3C_I2SV2_CLKSRC_AUDIOBUS:
355 iismod |= S3C2412_IISMOD_IMS_SYSMUX;
358 case S3C_I2SV2_CLKSRC_CDCLK:
359 /* Error if controller doesn't have the CDCLKCON bit */
360 if (!(i2s->feature & S3C_FEATURE_CDCLKCON))
364 case SND_SOC_CLOCK_IN:
365 iismod |= S3C64XX_IISMOD_CDCLKCON;
367 case SND_SOC_CLOCK_OUT:
368 iismod &= ~S3C64XX_IISMOD_CDCLKCON;
379 writel(iismod, i2s->regs + S3C2412_IISMOD);
380 pr_debug("%s w: IISMOD: %x\n", __func__, iismod);
385 static int s3c2412_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
386 struct snd_soc_dai *dai)
388 struct snd_soc_pcm_runtime *rtd = substream->private_data;
389 struct s3c_i2sv2_info *i2s = to_info(rtd->cpu_dai);
390 int capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
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);
425 case SNDRV_PCM_TRIGGER_STOP:
426 case SNDRV_PCM_TRIGGER_SUSPEND:
427 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
428 local_irq_save(irqs);
431 s3c2412_snd_rxctrl(i2s, 0);
433 s3c2412_snd_txctrl(i2s, 0);
435 local_irq_restore(irqs);
447 * Set S3C2412 Clock dividers
449 static int s3c2412_i2s_set_clkdiv(struct snd_soc_dai *cpu_dai,
452 struct s3c_i2sv2_info *i2s = to_info(cpu_dai);
455 pr_debug("%s(%p, %d, %d)\n", __func__, cpu_dai, div_id, div);
458 case S3C_I2SV2_DIV_BCLK:
461 div = S3C2412_IISMOD_BCLK_16FS;
465 div = S3C2412_IISMOD_BCLK_32FS;
469 div = S3C2412_IISMOD_BCLK_24FS;
473 div = S3C2412_IISMOD_BCLK_48FS;
480 reg = readl(i2s->regs + S3C2412_IISMOD);
481 reg &= ~S3C2412_IISMOD_BCLK_MASK;
482 writel(reg | div, i2s->regs + S3C2412_IISMOD);
484 pr_debug("%s: MOD=%08x\n", __func__, readl(i2s->regs + S3C2412_IISMOD));
487 case S3C_I2SV2_DIV_RCLK:
490 div = S3C2412_IISMOD_RCLK_256FS;
494 div = S3C2412_IISMOD_RCLK_384FS;
498 div = S3C2412_IISMOD_RCLK_512FS;
502 div = S3C2412_IISMOD_RCLK_768FS;
509 reg = readl(i2s->regs + S3C2412_IISMOD);
510 reg &= ~S3C2412_IISMOD_RCLK_MASK;
511 writel(reg | div, i2s->regs + S3C2412_IISMOD);
512 pr_debug("%s: MOD=%08x\n", __func__, readl(i2s->regs + S3C2412_IISMOD));
515 case S3C_I2SV2_DIV_PRESCALER:
517 writel((div << 8) | S3C2412_IISPSR_PSREN,
518 i2s->regs + S3C2412_IISPSR);
520 writel(0x0, i2s->regs + S3C2412_IISPSR);
522 pr_debug("%s: PSR=%08x\n", __func__, readl(i2s->regs + S3C2412_IISPSR));
532 static snd_pcm_sframes_t s3c2412_i2s_delay(struct snd_pcm_substream *substream,
533 struct snd_soc_dai *dai)
535 struct s3c_i2sv2_info *i2s = to_info(dai);
536 u32 reg = readl(i2s->regs + S3C2412_IISFIC);
537 snd_pcm_sframes_t delay;
539 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
540 delay = S3C2412_IISFIC_TXCOUNT(reg);
542 delay = S3C2412_IISFIC_RXCOUNT(reg);
547 struct clk *s3c_i2sv2_get_clock(struct snd_soc_dai *cpu_dai)
549 struct s3c_i2sv2_info *i2s = to_info(cpu_dai);
550 u32 iismod = readl(i2s->regs + S3C2412_IISMOD);
552 if (iismod & S3C2412_IISMOD_IMS_SYSMUX)
553 return i2s->iis_cclk;
555 return i2s->iis_pclk;
557 EXPORT_SYMBOL_GPL(s3c_i2sv2_get_clock);
559 /* default table of all avaialable root fs divisors */
560 static unsigned int iis_fs_tab[] = { 256, 512, 384, 768 };
562 int s3c_i2sv2_iis_calc_rate(struct s3c_i2sv2_rate_calc *info,
564 unsigned int rate, struct clk *clk)
566 unsigned long clkrate = clk_get_rate(clk);
572 signed int deviation = 0;
573 unsigned int best_fs = 0;
574 unsigned int best_div = 0;
575 unsigned int best_rate = 0;
576 unsigned int best_deviation = INT_MAX;
578 pr_debug("Input clock rate %ldHz\n", clkrate);
583 for (fs = 0; fs < ARRAY_SIZE(iis_fs_tab); fs++) {
584 fsdiv = iis_fs_tab[fs];
586 fsclk = clkrate / fsdiv;
589 if ((fsclk % rate) > (rate / 2))
595 actual = clkrate / (fsdiv * div);
596 deviation = actual - rate;
598 printk(KERN_DEBUG "%ufs: div %u => result %u, deviation %d\n",
599 fsdiv, div, actual, deviation);
601 deviation = abs(deviation);
603 if (deviation < best_deviation) {
607 best_deviation = deviation;
614 printk(KERN_DEBUG "best: fs=%u, div=%u, rate=%u\n",
615 best_fs, best_div, best_rate);
617 info->fs_div = best_fs;
618 info->clk_div = best_div;
622 EXPORT_SYMBOL_GPL(s3c_i2sv2_iis_calc_rate);
624 int s3c_i2sv2_probe(struct snd_soc_dai *dai,
625 struct s3c_i2sv2_info *i2s,
628 struct device *dev = dai->dev;
633 /* record our i2s structure for later use in the callbacks */
634 snd_soc_dai_set_drvdata(dai, i2s);
636 i2s->iis_pclk = clk_get(dev, "iis");
637 if (IS_ERR(i2s->iis_pclk)) {
638 dev_err(dev, "failed to get iis_clock\n");
643 clk_enable(i2s->iis_pclk);
645 /* Mark ourselves as in TXRX mode so we can run through our cleanup
646 * process without warnings. */
647 iismod = readl(i2s->regs + S3C2412_IISMOD);
648 iismod |= S3C2412_IISMOD_MODE_TXRX;
649 writel(iismod, i2s->regs + S3C2412_IISMOD);
650 s3c2412_snd_txctrl(i2s, 0);
651 s3c2412_snd_rxctrl(i2s, 0);
655 EXPORT_SYMBOL_GPL(s3c_i2sv2_probe);
658 static int s3c2412_i2s_suspend(struct snd_soc_dai *dai)
660 struct s3c_i2sv2_info *i2s = to_info(dai);
664 i2s->suspend_iismod = readl(i2s->regs + S3C2412_IISMOD);
665 i2s->suspend_iiscon = readl(i2s->regs + S3C2412_IISCON);
666 i2s->suspend_iispsr = readl(i2s->regs + S3C2412_IISPSR);
668 /* some basic suspend checks */
670 iismod = readl(i2s->regs + S3C2412_IISMOD);
672 if (iismod & S3C2412_IISCON_RXDMA_ACTIVE)
673 pr_warning("%s: RXDMA active?\n", __func__);
675 if (iismod & S3C2412_IISCON_TXDMA_ACTIVE)
676 pr_warning("%s: TXDMA active?\n", __func__);
678 if (iismod & S3C2412_IISCON_IIS_ACTIVE)
679 pr_warning("%s: IIS active\n", __func__);
685 static int s3c2412_i2s_resume(struct snd_soc_dai *dai)
687 struct s3c_i2sv2_info *i2s = to_info(dai);
689 pr_info("dai_active %d, IISMOD %08x, IISCON %08x\n",
690 dai->active, i2s->suspend_iismod, i2s->suspend_iiscon);
693 writel(i2s->suspend_iiscon, i2s->regs + S3C2412_IISCON);
694 writel(i2s->suspend_iismod, i2s->regs + S3C2412_IISMOD);
695 writel(i2s->suspend_iispsr, i2s->regs + S3C2412_IISPSR);
697 writel(S3C2412_IISFIC_RXFLUSH | S3C2412_IISFIC_TXFLUSH,
698 i2s->regs + S3C2412_IISFIC);
701 writel(0x0, i2s->regs + S3C2412_IISFIC);
707 #define s3c2412_i2s_suspend NULL
708 #define s3c2412_i2s_resume NULL
711 int s3c_i2sv2_register_component(struct device *dev, int id,
712 const struct snd_soc_component_driver *cmp_drv,
713 struct snd_soc_dai_driver *dai_drv)
715 struct snd_soc_dai_ops *ops = (struct snd_soc_dai_ops *)dai_drv->ops;
717 ops->trigger = s3c2412_i2s_trigger;
719 ops->hw_params = s3c_i2sv2_hw_params;
720 ops->set_fmt = s3c2412_i2s_set_fmt;
721 ops->set_clkdiv = s3c2412_i2s_set_clkdiv;
722 ops->set_sysclk = s3c_i2sv2_set_sysclk;
724 /* Allow overriding by (for example) IISv4 */
726 ops->delay = s3c2412_i2s_delay;
728 dai_drv->suspend = s3c2412_i2s_suspend;
729 dai_drv->resume = s3c2412_i2s_resume;
731 return devm_snd_soc_register_component(dev, cmp_drv, dai_drv, 1);
733 EXPORT_SYMBOL_GPL(s3c_i2sv2_register_component);
735 MODULE_LICENSE("GPL");