1 // SPDX-License-Identifier: GPL-2.0+
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 #include <linux/module.h>
14 #include <linux/delay.h>
15 #include <linux/clk.h>
18 #include <sound/soc.h>
19 #include <sound/pcm_params.h>
21 #include "regs-i2s-v2.h"
22 #include "s3c-i2s-v2.h"
24 #undef S3C_IIS_V2_SUPPORTED
26 #if defined(CONFIG_CPU_S3C2412) \
27 || defined(CONFIG_ARCH_S3C64XX) || defined(CONFIG_CPU_S5PV210)
28 #define S3C_IIS_V2_SUPPORTED
31 #ifndef S3C_IIS_V2_SUPPORTED
32 #error Unsupported CPU model
35 #define S3C2412_I2S_DEBUG_CON 0
37 static inline struct s3c_i2sv2_info *to_info(struct snd_soc_dai *cpu_dai)
39 return snd_soc_dai_get_drvdata(cpu_dai);
42 #define bit_set(v, b) (((v) & (b)) ? 1 : 0)
44 #if S3C2412_I2S_DEBUG_CON
45 static void dbg_showcon(const char *fn, u32 con)
47 printk(KERN_DEBUG "%s: LRI=%d, TXFEMPT=%d, RXFEMPT=%d, TXFFULL=%d, RXFFULL=%d\n", fn,
48 bit_set(con, S3C2412_IISCON_LRINDEX),
49 bit_set(con, S3C2412_IISCON_TXFIFO_EMPTY),
50 bit_set(con, S3C2412_IISCON_RXFIFO_EMPTY),
51 bit_set(con, S3C2412_IISCON_TXFIFO_FULL),
52 bit_set(con, S3C2412_IISCON_RXFIFO_FULL));
54 printk(KERN_DEBUG "%s: PAUSE: TXDMA=%d, RXDMA=%d, TXCH=%d, RXCH=%d\n",
56 bit_set(con, S3C2412_IISCON_TXDMA_PAUSE),
57 bit_set(con, S3C2412_IISCON_RXDMA_PAUSE),
58 bit_set(con, S3C2412_IISCON_TXCH_PAUSE),
59 bit_set(con, S3C2412_IISCON_RXCH_PAUSE));
60 printk(KERN_DEBUG "%s: ACTIVE: TXDMA=%d, RXDMA=%d, IIS=%d\n", fn,
61 bit_set(con, S3C2412_IISCON_TXDMA_ACTIVE),
62 bit_set(con, S3C2412_IISCON_RXDMA_ACTIVE),
63 bit_set(con, S3C2412_IISCON_IIS_ACTIVE));
66 static inline void dbg_showcon(const char *fn, u32 con)
71 /* Turn on or off the transmission path. */
72 static void s3c2412_snd_txctrl(struct s3c_i2sv2_info *i2s, int on)
74 void __iomem *regs = i2s->regs;
77 pr_debug("%s(%d)\n", __func__, on);
79 fic = readl(regs + S3C2412_IISFIC);
80 con = readl(regs + S3C2412_IISCON);
81 mod = readl(regs + S3C2412_IISMOD);
83 pr_debug("%s: IIS: CON=%x MOD=%x FIC=%x\n", __func__, con, mod, fic);
86 con |= S3C2412_IISCON_TXDMA_ACTIVE | S3C2412_IISCON_IIS_ACTIVE;
87 con &= ~S3C2412_IISCON_TXDMA_PAUSE;
88 con &= ~S3C2412_IISCON_TXCH_PAUSE;
90 switch (mod & S3C2412_IISMOD_MODE_MASK) {
91 case S3C2412_IISMOD_MODE_TXONLY:
92 case S3C2412_IISMOD_MODE_TXRX:
93 /* do nothing, we are in the right mode */
96 case S3C2412_IISMOD_MODE_RXONLY:
97 mod &= ~S3C2412_IISMOD_MODE_MASK;
98 mod |= S3C2412_IISMOD_MODE_TXRX;
102 dev_err(i2s->dev, "TXEN: Invalid MODE %x in IISMOD\n",
103 mod & S3C2412_IISMOD_MODE_MASK);
107 writel(con, regs + S3C2412_IISCON);
108 writel(mod, regs + S3C2412_IISMOD);
110 /* Note, we do not have any indication that the FIFO problems
111 * tha the S3C2410/2440 had apply here, so we should be able
112 * to disable the DMA and TX without resetting the FIFOS.
115 con |= S3C2412_IISCON_TXDMA_PAUSE;
116 con |= S3C2412_IISCON_TXCH_PAUSE;
117 con &= ~S3C2412_IISCON_TXDMA_ACTIVE;
119 switch (mod & S3C2412_IISMOD_MODE_MASK) {
120 case S3C2412_IISMOD_MODE_TXRX:
121 mod &= ~S3C2412_IISMOD_MODE_MASK;
122 mod |= S3C2412_IISMOD_MODE_RXONLY;
125 case S3C2412_IISMOD_MODE_TXONLY:
126 mod &= ~S3C2412_IISMOD_MODE_MASK;
127 con &= ~S3C2412_IISCON_IIS_ACTIVE;
131 dev_err(i2s->dev, "TXDIS: Invalid MODE %x in IISMOD\n",
132 mod & S3C2412_IISMOD_MODE_MASK);
136 writel(mod, regs + S3C2412_IISMOD);
137 writel(con, regs + S3C2412_IISCON);
140 fic = readl(regs + S3C2412_IISFIC);
141 dbg_showcon(__func__, con);
142 pr_debug("%s: IIS: CON=%x MOD=%x FIC=%x\n", __func__, con, mod, fic);
145 static void s3c2412_snd_rxctrl(struct s3c_i2sv2_info *i2s, int on)
147 void __iomem *regs = i2s->regs;
150 pr_debug("%s(%d)\n", __func__, on);
152 fic = readl(regs + S3C2412_IISFIC);
153 con = readl(regs + S3C2412_IISCON);
154 mod = readl(regs + S3C2412_IISMOD);
156 pr_debug("%s: IIS: CON=%x MOD=%x FIC=%x\n", __func__, con, mod, fic);
159 con |= S3C2412_IISCON_RXDMA_ACTIVE | S3C2412_IISCON_IIS_ACTIVE;
160 con &= ~S3C2412_IISCON_RXDMA_PAUSE;
161 con &= ~S3C2412_IISCON_RXCH_PAUSE;
163 switch (mod & S3C2412_IISMOD_MODE_MASK) {
164 case S3C2412_IISMOD_MODE_TXRX:
165 case S3C2412_IISMOD_MODE_RXONLY:
166 /* do nothing, we are in the right mode */
169 case S3C2412_IISMOD_MODE_TXONLY:
170 mod &= ~S3C2412_IISMOD_MODE_MASK;
171 mod |= S3C2412_IISMOD_MODE_TXRX;
175 dev_err(i2s->dev, "RXEN: Invalid MODE %x in IISMOD\n",
176 mod & S3C2412_IISMOD_MODE_MASK);
179 writel(mod, regs + S3C2412_IISMOD);
180 writel(con, regs + S3C2412_IISCON);
182 /* See txctrl notes on FIFOs. */
184 con &= ~S3C2412_IISCON_RXDMA_ACTIVE;
185 con |= S3C2412_IISCON_RXDMA_PAUSE;
186 con |= S3C2412_IISCON_RXCH_PAUSE;
188 switch (mod & S3C2412_IISMOD_MODE_MASK) {
189 case S3C2412_IISMOD_MODE_RXONLY:
190 con &= ~S3C2412_IISCON_IIS_ACTIVE;
191 mod &= ~S3C2412_IISMOD_MODE_MASK;
194 case S3C2412_IISMOD_MODE_TXRX:
195 mod &= ~S3C2412_IISMOD_MODE_MASK;
196 mod |= S3C2412_IISMOD_MODE_TXONLY;
200 dev_err(i2s->dev, "RXDIS: Invalid MODE %x in IISMOD\n",
201 mod & S3C2412_IISMOD_MODE_MASK);
204 writel(con, regs + S3C2412_IISCON);
205 writel(mod, regs + S3C2412_IISMOD);
208 fic = readl(regs + S3C2412_IISFIC);
209 pr_debug("%s: IIS: CON=%x MOD=%x FIC=%x\n", __func__, con, mod, fic);
212 #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
215 * Wait for the LR signal to allow synchronisation to the L/R clock
216 * from the codec. May only be needed for slave mode.
218 static int s3c2412_snd_lrsync(struct s3c_i2sv2_info *i2s)
221 unsigned long loops = msecs_to_loops(5);
223 pr_debug("Entered %s\n", __func__);
226 iiscon = readl(i2s->regs + S3C2412_IISCON);
227 if (iiscon & S3C2412_IISCON_LRINDEX)
234 printk(KERN_ERR "%s: timeout\n", __func__);
242 * Set S3C2412 I2S DAI format
244 static int s3c2412_i2s_set_fmt(struct snd_soc_dai *cpu_dai,
247 struct s3c_i2sv2_info *i2s = to_info(cpu_dai);
250 pr_debug("Entered %s\n", __func__);
252 iismod = readl(i2s->regs + S3C2412_IISMOD);
253 pr_debug("hw_params r: IISMOD: %x \n", iismod);
255 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
256 case SND_SOC_DAIFMT_CBM_CFM:
258 iismod |= S3C2412_IISMOD_SLAVE;
260 case SND_SOC_DAIFMT_CBS_CFS:
262 iismod &= ~S3C2412_IISMOD_SLAVE;
265 pr_err("unknown master/slave format\n");
269 iismod &= ~S3C2412_IISMOD_SDF_MASK;
271 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
272 case SND_SOC_DAIFMT_RIGHT_J:
273 iismod |= S3C2412_IISMOD_LR_RLOW;
274 iismod |= S3C2412_IISMOD_SDF_MSB;
276 case SND_SOC_DAIFMT_LEFT_J:
277 iismod |= S3C2412_IISMOD_LR_RLOW;
278 iismod |= S3C2412_IISMOD_SDF_LSB;
280 case SND_SOC_DAIFMT_I2S:
281 iismod &= ~S3C2412_IISMOD_LR_RLOW;
282 iismod |= S3C2412_IISMOD_SDF_IIS;
285 pr_err("Unknown data format\n");
289 writel(iismod, i2s->regs + S3C2412_IISMOD);
290 pr_debug("hw_params w: IISMOD: %x \n", iismod);
294 static int s3c_i2sv2_hw_params(struct snd_pcm_substream *substream,
295 struct snd_pcm_hw_params *params,
296 struct snd_soc_dai *dai)
298 struct s3c_i2sv2_info *i2s = to_info(dai);
299 struct snd_dmaengine_dai_dma_data *dma_data;
302 pr_debug("Entered %s\n", __func__);
304 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
305 dma_data = i2s->dma_playback;
307 dma_data = i2s->dma_capture;
309 snd_soc_dai_set_dma_data(dai, substream, dma_data);
311 /* Working copies of register */
312 iismod = readl(i2s->regs + S3C2412_IISMOD);
313 pr_debug("%s: r: IISMOD: %x\n", __func__, iismod);
315 iismod &= ~S3C64XX_IISMOD_BLC_MASK;
317 switch (params_width(params)) {
319 iismod |= S3C64XX_IISMOD_BLC_8BIT;
324 iismod |= S3C64XX_IISMOD_BLC_24BIT;
328 writel(iismod, i2s->regs + S3C2412_IISMOD);
329 pr_debug("%s: w: IISMOD: %x\n", __func__, iismod);
334 static int s3c_i2sv2_set_sysclk(struct snd_soc_dai *cpu_dai,
335 int clk_id, unsigned int freq, int dir)
337 struct s3c_i2sv2_info *i2s = to_info(cpu_dai);
338 u32 iismod = readl(i2s->regs + S3C2412_IISMOD);
340 pr_debug("Entered %s\n", __func__);
341 pr_debug("%s r: IISMOD: %x\n", __func__, iismod);
344 case S3C_I2SV2_CLKSRC_PCLK:
345 iismod &= ~S3C2412_IISMOD_IMS_SYSMUX;
348 case S3C_I2SV2_CLKSRC_AUDIOBUS:
349 iismod |= S3C2412_IISMOD_IMS_SYSMUX;
352 case S3C_I2SV2_CLKSRC_CDCLK:
353 /* Error if controller doesn't have the CDCLKCON bit */
354 if (!(i2s->feature & S3C_FEATURE_CDCLKCON))
358 case SND_SOC_CLOCK_IN:
359 iismod |= S3C64XX_IISMOD_CDCLKCON;
361 case SND_SOC_CLOCK_OUT:
362 iismod &= ~S3C64XX_IISMOD_CDCLKCON;
373 writel(iismod, i2s->regs + S3C2412_IISMOD);
374 pr_debug("%s w: IISMOD: %x\n", __func__, iismod);
379 static int s3c2412_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
380 struct snd_soc_dai *dai)
382 struct snd_soc_pcm_runtime *rtd = substream->private_data;
383 struct s3c_i2sv2_info *i2s = to_info(rtd->cpu_dai);
384 int capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
388 pr_debug("Entered %s\n", __func__);
391 case SNDRV_PCM_TRIGGER_START:
392 /* On start, ensure that the FIFOs are cleared and reset. */
394 writel(capture ? S3C2412_IISFIC_RXFLUSH : S3C2412_IISFIC_TXFLUSH,
395 i2s->regs + S3C2412_IISFIC);
397 /* clear again, just in case */
398 writel(0x0, i2s->regs + S3C2412_IISFIC);
400 case SNDRV_PCM_TRIGGER_RESUME:
401 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
403 ret = s3c2412_snd_lrsync(i2s);
408 local_irq_save(irqs);
411 s3c2412_snd_rxctrl(i2s, 1);
413 s3c2412_snd_txctrl(i2s, 1);
415 local_irq_restore(irqs);
419 case SNDRV_PCM_TRIGGER_STOP:
420 case SNDRV_PCM_TRIGGER_SUSPEND:
421 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
422 local_irq_save(irqs);
425 s3c2412_snd_rxctrl(i2s, 0);
427 s3c2412_snd_txctrl(i2s, 0);
429 local_irq_restore(irqs);
441 * Set S3C2412 Clock dividers
443 static int s3c2412_i2s_set_clkdiv(struct snd_soc_dai *cpu_dai,
446 struct s3c_i2sv2_info *i2s = to_info(cpu_dai);
449 pr_debug("%s(%p, %d, %d)\n", __func__, cpu_dai, div_id, div);
452 case S3C_I2SV2_DIV_BCLK:
455 div = S3C2412_IISMOD_BCLK_16FS;
459 div = S3C2412_IISMOD_BCLK_32FS;
463 div = S3C2412_IISMOD_BCLK_24FS;
467 div = S3C2412_IISMOD_BCLK_48FS;
474 reg = readl(i2s->regs + S3C2412_IISMOD);
475 reg &= ~S3C2412_IISMOD_BCLK_MASK;
476 writel(reg | div, i2s->regs + S3C2412_IISMOD);
478 pr_debug("%s: MOD=%08x\n", __func__, readl(i2s->regs + S3C2412_IISMOD));
481 case S3C_I2SV2_DIV_RCLK:
484 div = S3C2412_IISMOD_RCLK_256FS;
488 div = S3C2412_IISMOD_RCLK_384FS;
492 div = S3C2412_IISMOD_RCLK_512FS;
496 div = S3C2412_IISMOD_RCLK_768FS;
503 reg = readl(i2s->regs + S3C2412_IISMOD);
504 reg &= ~S3C2412_IISMOD_RCLK_MASK;
505 writel(reg | div, i2s->regs + S3C2412_IISMOD);
506 pr_debug("%s: MOD=%08x\n", __func__, readl(i2s->regs + S3C2412_IISMOD));
509 case S3C_I2SV2_DIV_PRESCALER:
511 writel((div << 8) | S3C2412_IISPSR_PSREN,
512 i2s->regs + S3C2412_IISPSR);
514 writel(0x0, i2s->regs + S3C2412_IISPSR);
516 pr_debug("%s: PSR=%08x\n", __func__, readl(i2s->regs + S3C2412_IISPSR));
526 static snd_pcm_sframes_t s3c2412_i2s_delay(struct snd_pcm_substream *substream,
527 struct snd_soc_dai *dai)
529 struct s3c_i2sv2_info *i2s = to_info(dai);
530 u32 reg = readl(i2s->regs + S3C2412_IISFIC);
531 snd_pcm_sframes_t delay;
533 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
534 delay = S3C2412_IISFIC_TXCOUNT(reg);
536 delay = S3C2412_IISFIC_RXCOUNT(reg);
541 struct clk *s3c_i2sv2_get_clock(struct snd_soc_dai *cpu_dai)
543 struct s3c_i2sv2_info *i2s = to_info(cpu_dai);
544 u32 iismod = readl(i2s->regs + S3C2412_IISMOD);
546 if (iismod & S3C2412_IISMOD_IMS_SYSMUX)
547 return i2s->iis_cclk;
549 return i2s->iis_pclk;
551 EXPORT_SYMBOL_GPL(s3c_i2sv2_get_clock);
553 /* default table of all avaialable root fs divisors */
554 static unsigned int iis_fs_tab[] = { 256, 512, 384, 768 };
556 int s3c_i2sv2_iis_calc_rate(struct s3c_i2sv2_rate_calc *info,
558 unsigned int rate, struct clk *clk)
560 unsigned long clkrate = clk_get_rate(clk);
566 signed int deviation = 0;
567 unsigned int best_fs = 0;
568 unsigned int best_div = 0;
569 unsigned int best_rate = 0;
570 unsigned int best_deviation = INT_MAX;
572 pr_debug("Input clock rate %ldHz\n", clkrate);
577 for (fs = 0; fs < ARRAY_SIZE(iis_fs_tab); fs++) {
578 fsdiv = iis_fs_tab[fs];
580 fsclk = clkrate / fsdiv;
583 if ((fsclk % rate) > (rate / 2))
589 actual = clkrate / (fsdiv * div);
590 deviation = actual - rate;
592 printk(KERN_DEBUG "%ufs: div %u => result %u, deviation %d\n",
593 fsdiv, div, actual, deviation);
595 deviation = abs(deviation);
597 if (deviation < best_deviation) {
601 best_deviation = deviation;
608 printk(KERN_DEBUG "best: fs=%u, div=%u, rate=%u\n",
609 best_fs, best_div, best_rate);
611 info->fs_div = best_fs;
612 info->clk_div = best_div;
616 EXPORT_SYMBOL_GPL(s3c_i2sv2_iis_calc_rate);
618 int s3c_i2sv2_probe(struct snd_soc_dai *dai,
619 struct s3c_i2sv2_info *i2s,
622 struct device *dev = dai->dev;
627 /* record our i2s structure for later use in the callbacks */
628 snd_soc_dai_set_drvdata(dai, i2s);
630 i2s->iis_pclk = clk_get(dev, "iis");
631 if (IS_ERR(i2s->iis_pclk)) {
632 dev_err(dev, "failed to get iis_clock\n");
636 clk_prepare_enable(i2s->iis_pclk);
638 /* Mark ourselves as in TXRX mode so we can run through our cleanup
639 * process without warnings. */
640 iismod = readl(i2s->regs + S3C2412_IISMOD);
641 iismod |= S3C2412_IISMOD_MODE_TXRX;
642 writel(iismod, i2s->regs + S3C2412_IISMOD);
643 s3c2412_snd_txctrl(i2s, 0);
644 s3c2412_snd_rxctrl(i2s, 0);
648 EXPORT_SYMBOL_GPL(s3c_i2sv2_probe);
650 void s3c_i2sv2_cleanup(struct snd_soc_dai *dai,
651 struct s3c_i2sv2_info *i2s)
653 clk_disable_unprepare(i2s->iis_pclk);
654 clk_put(i2s->iis_pclk);
655 i2s->iis_pclk = NULL;
657 EXPORT_SYMBOL_GPL(s3c_i2sv2_cleanup);
660 static int s3c2412_i2s_suspend(struct snd_soc_dai *dai)
662 struct s3c_i2sv2_info *i2s = to_info(dai);
666 i2s->suspend_iismod = readl(i2s->regs + S3C2412_IISMOD);
667 i2s->suspend_iiscon = readl(i2s->regs + S3C2412_IISCON);
668 i2s->suspend_iispsr = readl(i2s->regs + S3C2412_IISPSR);
670 /* some basic suspend checks */
672 iismod = readl(i2s->regs + S3C2412_IISMOD);
674 if (iismod & S3C2412_IISCON_RXDMA_ACTIVE)
675 pr_warning("%s: RXDMA active?\n", __func__);
677 if (iismod & S3C2412_IISCON_TXDMA_ACTIVE)
678 pr_warning("%s: TXDMA active?\n", __func__);
680 if (iismod & S3C2412_IISCON_IIS_ACTIVE)
681 pr_warning("%s: IIS active\n", __func__);
687 static int s3c2412_i2s_resume(struct snd_soc_dai *dai)
689 struct s3c_i2sv2_info *i2s = to_info(dai);
691 pr_info("dai_active %d, IISMOD %08x, IISCON %08x\n",
692 dai->active, i2s->suspend_iismod, i2s->suspend_iiscon);
695 writel(i2s->suspend_iiscon, i2s->regs + S3C2412_IISCON);
696 writel(i2s->suspend_iismod, i2s->regs + S3C2412_IISMOD);
697 writel(i2s->suspend_iispsr, i2s->regs + S3C2412_IISPSR);
699 writel(S3C2412_IISFIC_RXFLUSH | S3C2412_IISFIC_TXFLUSH,
700 i2s->regs + S3C2412_IISFIC);
703 writel(0x0, i2s->regs + S3C2412_IISFIC);
709 #define s3c2412_i2s_suspend NULL
710 #define s3c2412_i2s_resume NULL
713 int s3c_i2sv2_register_component(struct device *dev, int id,
714 const struct snd_soc_component_driver *cmp_drv,
715 struct snd_soc_dai_driver *dai_drv)
717 struct snd_soc_dai_ops *ops = (struct snd_soc_dai_ops *)dai_drv->ops;
719 ops->trigger = s3c2412_i2s_trigger;
721 ops->hw_params = s3c_i2sv2_hw_params;
722 ops->set_fmt = s3c2412_i2s_set_fmt;
723 ops->set_clkdiv = s3c2412_i2s_set_clkdiv;
724 ops->set_sysclk = s3c_i2sv2_set_sysclk;
726 /* Allow overriding by (for example) IISv4 */
728 ops->delay = s3c2412_i2s_delay;
730 dai_drv->suspend = s3c2412_i2s_suspend;
731 dai_drv->resume = s3c2412_i2s_resume;
733 return devm_snd_soc_register_component(dev, cmp_drv, dai_drv, 1);
735 EXPORT_SYMBOL_GPL(s3c_i2sv2_register_component);
737 MODULE_LICENSE("GPL");