1 /* sound/soc/samsung/i2s.c
3 * ALSA SoC Audio Layer - Samsung I2S Controller driver
5 * Copyright (c) 2010 Samsung Electronics Co. Ltd.
6 * Jaswinder Singh <jassisinghbrar@gmail.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/delay.h>
14 #include <linux/slab.h>
15 #include <linux/clk.h>
17 #include <linux/module.h>
19 #include <linux/of_gpio.h>
20 #include <linux/pm_runtime.h>
22 #include <sound/soc.h>
23 #include <sound/pcm_params.h>
25 #include <linux/platform_data/asoc-s3c.h>
32 #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
34 enum samsung_dai_type {
39 struct samsung_i2s_dai_data {
45 /* Platform device for this DAI */
46 struct platform_device *pdev;
49 /* Physical base address of SFRs */
51 /* Rate of RCLK source clock */
52 unsigned long rclk_srcrate;
56 * Specifically requested RCLK,BCLK by MACHINE Driver.
57 * 0 indicates CPU driver is free to choose any value.
60 /* I2S Controller's core clock */
62 /* Clock for generating I2S signals */
64 /* Pointer to the Primary_Fifo if this is Sec_Fifo, NULL otherwise */
65 struct i2s_dai *pri_dai;
66 /* Pointer to the Secondary_Fifo if it has one, NULL otherwise */
67 struct i2s_dai *sec_dai;
68 #define DAI_OPENED (1 << 0) /* Dai is opened */
69 #define DAI_MANAGER (1 << 1) /* Dai is the manager */
71 /* Driver for this DAI */
72 struct snd_soc_dai_driver i2s_dai_drv;
74 struct s3c_dma_params dma_playback;
75 struct s3c_dma_params dma_capture;
76 struct s3c_dma_params idma_playback;
81 unsigned long gpios[7]; /* i2s gpio line numbers */
84 /* Lock for cross i/f checks */
85 static DEFINE_SPINLOCK(lock);
87 /* If this is the 'overlay' stereo DAI */
88 static inline bool is_secondary(struct i2s_dai *i2s)
90 return i2s->pri_dai ? true : false;
93 /* If operating in SoC-Slave mode */
94 static inline bool is_slave(struct i2s_dai *i2s)
96 return (readl(i2s->addr + I2SMOD) & MOD_SLAVE) ? true : false;
99 /* If this interface of the controller is transmitting data */
100 static inline bool tx_active(struct i2s_dai *i2s)
107 active = readl(i2s->addr + I2SCON);
109 if (is_secondary(i2s))
110 active &= CON_TXSDMA_ACTIVE;
112 active &= CON_TXDMA_ACTIVE;
114 return active ? true : false;
117 /* If the other interface of the controller is transmitting data */
118 static inline bool other_tx_active(struct i2s_dai *i2s)
120 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
122 return tx_active(other);
125 /* If any interface of the controller is transmitting data */
126 static inline bool any_tx_active(struct i2s_dai *i2s)
128 return tx_active(i2s) || other_tx_active(i2s);
131 /* If this interface of the controller is receiving data */
132 static inline bool rx_active(struct i2s_dai *i2s)
139 active = readl(i2s->addr + I2SCON) & CON_RXDMA_ACTIVE;
141 return active ? true : false;
144 /* If the other interface of the controller is receiving data */
145 static inline bool other_rx_active(struct i2s_dai *i2s)
147 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
149 return rx_active(other);
152 /* If any interface of the controller is receiving data */
153 static inline bool any_rx_active(struct i2s_dai *i2s)
155 return rx_active(i2s) || other_rx_active(i2s);
158 /* If the other DAI is transmitting or receiving data */
159 static inline bool other_active(struct i2s_dai *i2s)
161 return other_rx_active(i2s) || other_tx_active(i2s);
164 /* If this DAI is transmitting or receiving data */
165 static inline bool this_active(struct i2s_dai *i2s)
167 return tx_active(i2s) || rx_active(i2s);
170 /* If the controller is active anyway */
171 static inline bool any_active(struct i2s_dai *i2s)
173 return this_active(i2s) || other_active(i2s);
176 static inline struct i2s_dai *to_info(struct snd_soc_dai *dai)
178 return snd_soc_dai_get_drvdata(dai);
181 static inline bool is_opened(struct i2s_dai *i2s)
183 if (i2s && (i2s->mode & DAI_OPENED))
189 static inline bool is_manager(struct i2s_dai *i2s)
191 if (is_opened(i2s) && (i2s->mode & DAI_MANAGER))
197 /* Read RCLK of I2S (in multiples of LRCLK) */
198 static inline unsigned get_rfs(struct i2s_dai *i2s)
202 if (i2s->quirks & QUIRK_SUPPORTS_TDM)
203 rfs = readl(i2s->addr + I2SMOD) >> EXYNOS5420_MOD_RCLK_SHIFT;
205 rfs = (readl(i2s->addr + I2SMOD) >> MOD_RCLK_SHIFT);
206 rfs &= MOD_RCLK_MASK;
216 /* Write RCLK of I2S (in multiples of LRCLK) */
217 static inline void set_rfs(struct i2s_dai *i2s, unsigned rfs)
219 u32 mod = readl(i2s->addr + I2SMOD);
222 if (i2s->quirks & QUIRK_SUPPORTS_TDM)
223 rfs_shift = EXYNOS5420_MOD_RCLK_SHIFT;
225 rfs_shift = MOD_RCLK_SHIFT;
226 mod &= ~(MOD_RCLK_MASK << rfs_shift);
230 mod |= (MOD_RCLK_768FS << rfs_shift);
233 mod |= (MOD_RCLK_512FS << rfs_shift);
236 mod |= (MOD_RCLK_384FS << rfs_shift);
239 mod |= (MOD_RCLK_256FS << rfs_shift);
243 writel(mod, i2s->addr + I2SMOD);
246 /* Read Bit-Clock of I2S (in multiples of LRCLK) */
247 static inline unsigned get_bfs(struct i2s_dai *i2s)
251 if (i2s->quirks & QUIRK_SUPPORTS_TDM) {
252 bfs = readl(i2s->addr + I2SMOD) >> EXYNOS5420_MOD_BCLK_SHIFT;
253 bfs &= EXYNOS5420_MOD_BCLK_MASK;
255 bfs = readl(i2s->addr + I2SMOD) >> MOD_BCLK_SHIFT;
256 bfs &= MOD_BCLK_MASK;
272 /* Write Bit-Clock of I2S (in multiples of LRCLK) */
273 static inline void set_bfs(struct i2s_dai *i2s, unsigned bfs)
275 u32 mod = readl(i2s->addr + I2SMOD);
277 int tdm = i2s->quirks & QUIRK_SUPPORTS_TDM;
279 if (i2s->quirks & QUIRK_SUPPORTS_TDM) {
280 bfs_shift = EXYNOS5420_MOD_BCLK_SHIFT;
281 mod &= ~(EXYNOS5420_MOD_BCLK_MASK << bfs_shift);
283 bfs_shift = MOD_BCLK_SHIFT;
284 mod &= ~(MOD_BCLK_MASK << bfs_shift);
287 /* Non-TDM I2S controllers do not support BCLK > 48 * FS */
288 if (!tdm && bfs > 48) {
289 dev_err(&i2s->pdev->dev, "Unsupported BCLK divider\n");
295 mod |= (MOD_BCLK_48FS << bfs_shift);
298 mod |= (MOD_BCLK_32FS << bfs_shift);
301 mod |= (MOD_BCLK_24FS << bfs_shift);
304 mod |= (MOD_BCLK_16FS << bfs_shift);
307 mod |= (EXYNOS5420_MOD_BCLK_64FS << bfs_shift);
310 mod |= (EXYNOS5420_MOD_BCLK_96FS << bfs_shift);
313 mod |= (EXYNOS5420_MOD_BCLK_128FS << bfs_shift);
316 mod |= (EXYNOS5420_MOD_BCLK_192FS << bfs_shift);
319 mod |= (EXYNOS5420_MOD_BCLK_256FS << bfs_shift);
322 dev_err(&i2s->pdev->dev, "Wrong BCLK Divider!\n");
326 writel(mod, i2s->addr + I2SMOD);
330 static inline int get_blc(struct i2s_dai *i2s)
332 int blc = readl(i2s->addr + I2SMOD);
334 blc = (blc >> 13) & 0x3;
343 /* TX Channel Control */
344 static void i2s_txctrl(struct i2s_dai *i2s, int on)
346 void __iomem *addr = i2s->addr;
347 u32 con = readl(addr + I2SCON);
348 u32 mod = readl(addr + I2SMOD) & ~MOD_MASK;
352 con &= ~CON_TXCH_PAUSE;
354 if (is_secondary(i2s)) {
355 con |= CON_TXSDMA_ACTIVE;
356 con &= ~CON_TXSDMA_PAUSE;
358 con |= CON_TXDMA_ACTIVE;
359 con &= ~CON_TXDMA_PAUSE;
362 if (any_rx_active(i2s))
367 if (is_secondary(i2s)) {
368 con |= CON_TXSDMA_PAUSE;
369 con &= ~CON_TXSDMA_ACTIVE;
371 con |= CON_TXDMA_PAUSE;
372 con &= ~CON_TXDMA_ACTIVE;
375 if (other_tx_active(i2s)) {
376 writel(con, addr + I2SCON);
380 con |= CON_TXCH_PAUSE;
382 if (any_rx_active(i2s))
388 writel(mod, addr + I2SMOD);
389 writel(con, addr + I2SCON);
392 /* RX Channel Control */
393 static void i2s_rxctrl(struct i2s_dai *i2s, int on)
395 void __iomem *addr = i2s->addr;
396 u32 con = readl(addr + I2SCON);
397 u32 mod = readl(addr + I2SMOD) & ~MOD_MASK;
400 con |= CON_RXDMA_ACTIVE | CON_ACTIVE;
401 con &= ~(CON_RXDMA_PAUSE | CON_RXCH_PAUSE);
403 if (any_tx_active(i2s))
408 con |= CON_RXDMA_PAUSE | CON_RXCH_PAUSE;
409 con &= ~CON_RXDMA_ACTIVE;
411 if (any_tx_active(i2s))
417 writel(mod, addr + I2SMOD);
418 writel(con, addr + I2SCON);
421 /* Flush FIFO of an interface */
422 static inline void i2s_fifo(struct i2s_dai *i2s, u32 flush)
430 if (is_secondary(i2s))
431 fic = i2s->addr + I2SFICS;
433 fic = i2s->addr + I2SFIC;
436 writel(readl(fic) | flush, fic);
439 val = msecs_to_loops(1) / 1000; /* 1 usec */
443 writel(readl(fic) & ~flush, fic);
446 static int i2s_set_sysclk(struct snd_soc_dai *dai,
447 int clk_id, unsigned int rfs, int dir)
449 struct i2s_dai *i2s = to_info(dai);
450 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
451 u32 mod = readl(i2s->addr + I2SMOD);
454 case SAMSUNG_I2S_CDCLK:
455 /* Shouldn't matter in GATING(CLOCK_IN) mode */
456 if (dir == SND_SOC_CLOCK_IN)
459 if ((rfs && other->rfs && (other->rfs != rfs)) ||
461 (((dir == SND_SOC_CLOCK_IN)
462 && !(mod & MOD_CDCLKCON)) ||
463 ((dir == SND_SOC_CLOCK_OUT)
464 && (mod & MOD_CDCLKCON))))) {
465 dev_err(&i2s->pdev->dev,
466 "%s:%d Other DAI busy\n", __func__, __LINE__);
470 if (dir == SND_SOC_CLOCK_IN)
473 mod &= ~MOD_CDCLKCON;
478 case SAMSUNG_I2S_RCLKSRC_0: /* clock corrsponding to IISMOD[10] := 0 */
479 case SAMSUNG_I2S_RCLKSRC_1: /* clock corrsponding to IISMOD[10] := 1 */
480 if ((i2s->quirks & QUIRK_NO_MUXPSR)
481 || (clk_id == SAMSUNG_I2S_RCLKSRC_0))
486 if (!any_active(i2s)) {
488 if ((clk_id && !(mod & MOD_IMS_SYSMUX)) ||
489 (!clk_id && (mod & MOD_IMS_SYSMUX))) {
490 clk_disable_unprepare(i2s->op_clk);
491 clk_put(i2s->op_clk);
494 clk_get_rate(i2s->op_clk);
500 i2s->op_clk = clk_get(&i2s->pdev->dev,
503 i2s->op_clk = clk_get(&i2s->pdev->dev,
505 clk_prepare_enable(i2s->op_clk);
506 i2s->rclk_srcrate = clk_get_rate(i2s->op_clk);
508 /* Over-ride the other's */
510 other->op_clk = i2s->op_clk;
511 other->rclk_srcrate = i2s->rclk_srcrate;
513 } else if ((!clk_id && (mod & MOD_IMS_SYSMUX))
514 || (clk_id && !(mod & MOD_IMS_SYSMUX))) {
515 dev_err(&i2s->pdev->dev,
516 "%s:%d Other DAI busy\n", __func__, __LINE__);
519 /* Call can't be on the active DAI */
520 i2s->op_clk = other->op_clk;
521 i2s->rclk_srcrate = other->rclk_srcrate;
526 mod &= ~MOD_IMS_SYSMUX;
528 mod |= MOD_IMS_SYSMUX;
532 dev_err(&i2s->pdev->dev, "We don't serve that!\n");
536 writel(mod, i2s->addr + I2SMOD);
541 static int i2s_set_fmt(struct snd_soc_dai *dai,
544 struct i2s_dai *i2s = to_info(dai);
545 u32 mod = readl(i2s->addr + I2SMOD);
546 int lrp_shift, sdf_shift, sdf_mask, lrp_rlow;
549 if (i2s->quirks & QUIRK_SUPPORTS_TDM) {
550 lrp_shift = EXYNOS5420_MOD_LRP_SHIFT;
551 sdf_shift = EXYNOS5420_MOD_SDF_SHIFT;
553 lrp_shift = MOD_LRP_SHIFT;
554 sdf_shift = MOD_SDF_SHIFT;
557 sdf_mask = MOD_SDF_MASK << sdf_shift;
558 lrp_rlow = MOD_LR_RLOW << lrp_shift;
560 /* Format is priority */
561 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
562 case SND_SOC_DAIFMT_RIGHT_J:
564 tmp |= (MOD_SDF_MSB << sdf_shift);
566 case SND_SOC_DAIFMT_LEFT_J:
568 tmp |= (MOD_SDF_LSB << sdf_shift);
570 case SND_SOC_DAIFMT_I2S:
571 tmp |= (MOD_SDF_IIS << sdf_shift);
574 dev_err(&i2s->pdev->dev, "Format not supported\n");
579 * INV flag is relative to the FORMAT flag - if set it simply
580 * flips the polarity specified by the Standard
582 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
583 case SND_SOC_DAIFMT_NB_NF:
585 case SND_SOC_DAIFMT_NB_IF:
592 dev_err(&i2s->pdev->dev, "Polarity not supported\n");
596 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
597 case SND_SOC_DAIFMT_CBM_CFM:
600 case SND_SOC_DAIFMT_CBS_CFS:
601 /* Set default source clock in Master mode */
602 if (i2s->rclk_srcrate == 0)
603 i2s_set_sysclk(dai, SAMSUNG_I2S_RCLKSRC_0,
604 0, SND_SOC_CLOCK_IN);
607 dev_err(&i2s->pdev->dev, "master/slave format not supported\n");
612 * Don't change the I2S mode if any controller is active on this
615 if (any_active(i2s) &&
616 ((mod & (sdf_mask | lrp_rlow | MOD_SLAVE)) != tmp)) {
617 dev_err(&i2s->pdev->dev,
618 "%s:%d Other DAI busy\n", __func__, __LINE__);
622 mod &= ~(sdf_mask | lrp_rlow | MOD_SLAVE);
624 writel(mod, i2s->addr + I2SMOD);
629 static int i2s_hw_params(struct snd_pcm_substream *substream,
630 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
632 struct i2s_dai *i2s = to_info(dai);
633 u32 mod = readl(i2s->addr + I2SMOD);
635 if (!is_secondary(i2s))
636 mod &= ~(MOD_DC2_EN | MOD_DC1_EN);
638 switch (params_channels(params)) {
645 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
646 i2s->dma_playback.dma_size = 4;
648 i2s->dma_capture.dma_size = 4;
651 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
652 i2s->dma_playback.dma_size = 2;
654 i2s->dma_capture.dma_size = 2;
658 dev_err(&i2s->pdev->dev, "%d channels not supported\n",
659 params_channels(params));
663 if (is_secondary(i2s))
664 mod &= ~MOD_BLCS_MASK;
666 mod &= ~MOD_BLCP_MASK;
669 mod &= ~MOD_BLC_MASK;
671 switch (params_format(params)) {
672 case SNDRV_PCM_FORMAT_S8:
673 if (is_secondary(i2s))
674 mod |= MOD_BLCS_8BIT;
676 mod |= MOD_BLCP_8BIT;
680 case SNDRV_PCM_FORMAT_S16_LE:
681 if (is_secondary(i2s))
682 mod |= MOD_BLCS_16BIT;
684 mod |= MOD_BLCP_16BIT;
686 mod |= MOD_BLC_16BIT;
688 case SNDRV_PCM_FORMAT_S24_LE:
689 if (is_secondary(i2s))
690 mod |= MOD_BLCS_24BIT;
692 mod |= MOD_BLCP_24BIT;
694 mod |= MOD_BLC_24BIT;
697 dev_err(&i2s->pdev->dev, "Format(%d) not supported\n",
698 params_format(params));
701 writel(mod, i2s->addr + I2SMOD);
703 samsung_asoc_init_dma_data(dai, &i2s->dma_playback, &i2s->dma_capture);
705 i2s->frmclk = params_rate(params);
710 /* We set constraints on the substream acc to the version of I2S */
711 static int i2s_startup(struct snd_pcm_substream *substream,
712 struct snd_soc_dai *dai)
714 struct i2s_dai *i2s = to_info(dai);
715 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
718 spin_lock_irqsave(&lock, flags);
720 i2s->mode |= DAI_OPENED;
722 if (is_manager(other))
723 i2s->mode &= ~DAI_MANAGER;
725 i2s->mode |= DAI_MANAGER;
727 /* Enforce set_sysclk in Master mode */
728 i2s->rclk_srcrate = 0;
730 if (!any_active(i2s) && (i2s->quirks & QUIRK_NEED_RSTCLR))
731 writel(CON_RSTCLR, i2s->addr + I2SCON);
733 spin_unlock_irqrestore(&lock, flags);
738 static void i2s_shutdown(struct snd_pcm_substream *substream,
739 struct snd_soc_dai *dai)
741 struct i2s_dai *i2s = to_info(dai);
742 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
745 spin_lock_irqsave(&lock, flags);
747 i2s->mode &= ~DAI_OPENED;
748 i2s->mode &= ~DAI_MANAGER;
750 if (is_opened(other))
751 other->mode |= DAI_MANAGER;
753 /* Reset any constraint on RFS and BFS */
757 spin_unlock_irqrestore(&lock, flags);
759 /* Gate CDCLK by default */
760 if (!is_opened(other))
761 i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK,
762 0, SND_SOC_CLOCK_IN);
765 static int config_setup(struct i2s_dai *i2s)
767 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
768 unsigned rfs, bfs, blc;
778 /* Select least possible multiple(2) if no constraint set */
787 if ((rfs == 256 || rfs == 512) && (blc == 24)) {
788 dev_err(&i2s->pdev->dev,
789 "%d-RFS not supported for 24-blc\n", rfs);
794 if (bfs == 16 || bfs == 32)
800 /* If already setup and running */
801 if (any_active(i2s) && (get_rfs(i2s) != rfs || get_bfs(i2s) != bfs)) {
802 dev_err(&i2s->pdev->dev,
803 "%s:%d Other DAI busy\n", __func__, __LINE__);
810 /* Don't bother with PSR in Slave mode */
814 if (!(i2s->quirks & QUIRK_NO_MUXPSR)) {
815 psr = i2s->rclk_srcrate / i2s->frmclk / rfs;
816 writel(((psr - 1) << 8) | PSR_PSREN, i2s->addr + I2SPSR);
817 dev_dbg(&i2s->pdev->dev,
818 "RCLK_SRC=%luHz PSR=%u, RCLK=%dfs, BCLK=%dfs\n",
819 i2s->rclk_srcrate, psr, rfs, bfs);
825 static int i2s_trigger(struct snd_pcm_substream *substream,
826 int cmd, struct snd_soc_dai *dai)
828 int capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
829 struct snd_soc_pcm_runtime *rtd = substream->private_data;
830 struct i2s_dai *i2s = to_info(rtd->cpu_dai);
834 case SNDRV_PCM_TRIGGER_START:
835 case SNDRV_PCM_TRIGGER_RESUME:
836 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
837 local_irq_save(flags);
839 if (config_setup(i2s)) {
840 local_irq_restore(flags);
849 local_irq_restore(flags);
851 case SNDRV_PCM_TRIGGER_STOP:
852 case SNDRV_PCM_TRIGGER_SUSPEND:
853 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
854 local_irq_save(flags);
858 i2s_fifo(i2s, FIC_RXFLUSH);
861 i2s_fifo(i2s, FIC_TXFLUSH);
864 local_irq_restore(flags);
871 static int i2s_set_clkdiv(struct snd_soc_dai *dai,
874 struct i2s_dai *i2s = to_info(dai);
875 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
878 case SAMSUNG_I2S_DIV_BCLK:
879 if ((any_active(i2s) && div && (get_bfs(i2s) != div))
880 || (other && other->bfs && (other->bfs != div))) {
881 dev_err(&i2s->pdev->dev,
882 "%s:%d Other DAI busy\n", __func__, __LINE__);
888 dev_err(&i2s->pdev->dev,
889 "Invalid clock divider(%d)\n", div_id);
896 static snd_pcm_sframes_t
897 i2s_delay(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
899 struct i2s_dai *i2s = to_info(dai);
900 u32 reg = readl(i2s->addr + I2SFIC);
901 snd_pcm_sframes_t delay;
903 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
904 delay = FIC_RXCOUNT(reg);
905 else if (is_secondary(i2s))
906 delay = FICS_TXCOUNT(readl(i2s->addr + I2SFICS));
908 delay = FIC_TXCOUNT(reg);
914 static int i2s_suspend(struct snd_soc_dai *dai)
916 struct i2s_dai *i2s = to_info(dai);
918 i2s->suspend_i2smod = readl(i2s->addr + I2SMOD);
919 i2s->suspend_i2scon = readl(i2s->addr + I2SCON);
920 i2s->suspend_i2spsr = readl(i2s->addr + I2SPSR);
925 static int i2s_resume(struct snd_soc_dai *dai)
927 struct i2s_dai *i2s = to_info(dai);
929 writel(i2s->suspend_i2scon, i2s->addr + I2SCON);
930 writel(i2s->suspend_i2smod, i2s->addr + I2SMOD);
931 writel(i2s->suspend_i2spsr, i2s->addr + I2SPSR);
936 #define i2s_suspend NULL
937 #define i2s_resume NULL
940 static int samsung_i2s_dai_probe(struct snd_soc_dai *dai)
942 struct i2s_dai *i2s = to_info(dai);
943 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
945 if (other && other->clk) { /* If this is probe on secondary */
946 samsung_asoc_init_dma_data(dai, &other->sec_dai->dma_playback,
951 i2s->addr = ioremap(i2s->base, 0x100);
952 if (i2s->addr == NULL) {
953 dev_err(&i2s->pdev->dev, "cannot ioremap registers\n");
957 i2s->clk = clk_get(&i2s->pdev->dev, "iis");
958 if (IS_ERR(i2s->clk)) {
959 dev_err(&i2s->pdev->dev, "failed to get i2s_clock\n");
963 clk_prepare_enable(i2s->clk);
965 samsung_asoc_init_dma_data(dai, &i2s->dma_playback, &i2s->dma_capture);
968 other->addr = i2s->addr;
969 other->clk = i2s->clk;
972 if (i2s->quirks & QUIRK_NEED_RSTCLR)
973 writel(CON_RSTCLR, i2s->addr + I2SCON);
975 if (i2s->quirks & QUIRK_SEC_DAI)
976 idma_reg_addr_init(i2s->addr,
977 i2s->sec_dai->idma_playback.dma_addr);
980 /* Reset any constraint on RFS and BFS */
985 i2s_fifo(i2s, FIC_TXFLUSH);
986 i2s_fifo(other, FIC_TXFLUSH);
987 i2s_fifo(i2s, FIC_RXFLUSH);
989 /* Gate CDCLK by default */
990 if (!is_opened(other))
991 i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK,
992 0, SND_SOC_CLOCK_IN);
997 static int samsung_i2s_dai_remove(struct snd_soc_dai *dai)
999 struct i2s_dai *i2s = snd_soc_dai_get_drvdata(dai);
1000 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
1002 if (!other || !other->clk) {
1004 if (i2s->quirks & QUIRK_NEED_RSTCLR)
1005 writel(0, i2s->addr + I2SCON);
1007 clk_disable_unprepare(i2s->clk);
1018 static const struct snd_soc_dai_ops samsung_i2s_dai_ops = {
1019 .trigger = i2s_trigger,
1020 .hw_params = i2s_hw_params,
1021 .set_fmt = i2s_set_fmt,
1022 .set_clkdiv = i2s_set_clkdiv,
1023 .set_sysclk = i2s_set_sysclk,
1024 .startup = i2s_startup,
1025 .shutdown = i2s_shutdown,
1029 static const struct snd_soc_component_driver samsung_i2s_component = {
1030 .name = "samsung-i2s",
1033 #define SAMSUNG_I2S_RATES SNDRV_PCM_RATE_8000_96000
1035 #define SAMSUNG_I2S_FMTS (SNDRV_PCM_FMTBIT_S8 | \
1036 SNDRV_PCM_FMTBIT_S16_LE | \
1037 SNDRV_PCM_FMTBIT_S24_LE)
1039 static struct i2s_dai *i2s_alloc_dai(struct platform_device *pdev, bool sec)
1041 struct i2s_dai *i2s;
1044 i2s = devm_kzalloc(&pdev->dev, sizeof(struct i2s_dai), GFP_KERNEL);
1049 i2s->pri_dai = NULL;
1050 i2s->sec_dai = NULL;
1051 i2s->i2s_dai_drv.symmetric_rates = 1;
1052 i2s->i2s_dai_drv.probe = samsung_i2s_dai_probe;
1053 i2s->i2s_dai_drv.remove = samsung_i2s_dai_remove;
1054 i2s->i2s_dai_drv.ops = &samsung_i2s_dai_ops;
1055 i2s->i2s_dai_drv.suspend = i2s_suspend;
1056 i2s->i2s_dai_drv.resume = i2s_resume;
1057 i2s->i2s_dai_drv.playback.channels_min = 1;
1058 i2s->i2s_dai_drv.playback.channels_max = 2;
1059 i2s->i2s_dai_drv.playback.rates = SAMSUNG_I2S_RATES;
1060 i2s->i2s_dai_drv.playback.formats = SAMSUNG_I2S_FMTS;
1063 i2s->i2s_dai_drv.capture.channels_min = 1;
1064 i2s->i2s_dai_drv.capture.channels_max = 2;
1065 i2s->i2s_dai_drv.capture.rates = SAMSUNG_I2S_RATES;
1066 i2s->i2s_dai_drv.capture.formats = SAMSUNG_I2S_FMTS;
1067 dev_set_drvdata(&i2s->pdev->dev, i2s);
1068 } else { /* Create a new platform_device for Secondary */
1069 i2s->pdev = platform_device_alloc("samsung-i2s-sec", -1);
1073 i2s->pdev->dev.parent = &pdev->dev;
1075 platform_set_drvdata(i2s->pdev, i2s);
1076 ret = platform_device_add(i2s->pdev);
1084 static const struct of_device_id exynos_i2s_match[];
1086 static inline const struct samsung_i2s_dai_data *samsung_i2s_get_driver_data(
1087 struct platform_device *pdev)
1090 if (pdev->dev.of_node) {
1091 const struct of_device_id *match;
1092 match = of_match_node(exynos_i2s_match, pdev->dev.of_node);
1096 return (struct samsung_i2s_dai_data *)
1097 platform_get_device_id(pdev)->driver_data;
1100 #ifdef CONFIG_PM_RUNTIME
1101 static int i2s_runtime_suspend(struct device *dev)
1103 struct i2s_dai *i2s = dev_get_drvdata(dev);
1105 clk_disable_unprepare(i2s->clk);
1110 static int i2s_runtime_resume(struct device *dev)
1112 struct i2s_dai *i2s = dev_get_drvdata(dev);
1114 clk_prepare_enable(i2s->clk);
1118 #endif /* CONFIG_PM_RUNTIME */
1120 static int samsung_i2s_probe(struct platform_device *pdev)
1122 struct i2s_dai *pri_dai, *sec_dai = NULL;
1123 struct s3c_audio_pdata *i2s_pdata = pdev->dev.platform_data;
1124 struct samsung_i2s *i2s_cfg = NULL;
1125 struct resource *res;
1126 u32 regs_base, quirks = 0, idma_addr = 0;
1127 struct device_node *np = pdev->dev.of_node;
1128 const struct samsung_i2s_dai_data *i2s_dai_data;
1131 /* Call during Seconday interface registration */
1132 i2s_dai_data = samsung_i2s_get_driver_data(pdev);
1134 if (i2s_dai_data->dai_type == TYPE_SEC) {
1135 sec_dai = dev_get_drvdata(&pdev->dev);
1137 dev_err(&pdev->dev, "Unable to get drvdata\n");
1140 devm_snd_soc_register_component(&sec_dai->pdev->dev,
1141 &samsung_i2s_component,
1142 &sec_dai->i2s_dai_drv, 1);
1143 samsung_asoc_dma_platform_register(&pdev->dev);
1147 pri_dai = i2s_alloc_dai(pdev, false);
1149 dev_err(&pdev->dev, "Unable to alloc I2S_pri\n");
1154 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1157 "Unable to get I2S-TX dma resource\n");
1160 pri_dai->dma_playback.channel = res->start;
1162 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1165 "Unable to get I2S-RX dma resource\n");
1168 pri_dai->dma_capture.channel = res->start;
1170 if (i2s_pdata == NULL) {
1171 dev_err(&pdev->dev, "Can't work without s3c_audio_pdata\n");
1175 if (&i2s_pdata->type)
1176 i2s_cfg = &i2s_pdata->type.i2s;
1179 quirks = i2s_cfg->quirks;
1180 idma_addr = i2s_cfg->idma_addr;
1183 quirks = i2s_dai_data->quirks;
1184 if (of_property_read_u32(np, "samsung,idma-addr",
1186 if (quirks & QUIRK_SEC_DAI) {
1187 dev_err(&pdev->dev, "idma address is not"\
1194 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1196 dev_err(&pdev->dev, "Unable to get I2S SFR address\n");
1200 if (!request_mem_region(res->start, resource_size(res),
1202 dev_err(&pdev->dev, "Unable to request SFR region\n");
1205 regs_base = res->start;
1207 pri_dai->dma_playback.dma_addr = regs_base + I2STXD;
1208 pri_dai->dma_capture.dma_addr = regs_base + I2SRXD;
1209 pri_dai->dma_playback.client =
1210 (struct s3c2410_dma_client *)&pri_dai->dma_playback;
1211 pri_dai->dma_playback.ch_name = "tx";
1212 pri_dai->dma_capture.client =
1213 (struct s3c2410_dma_client *)&pri_dai->dma_capture;
1214 pri_dai->dma_capture.ch_name = "rx";
1215 pri_dai->dma_playback.dma_size = 4;
1216 pri_dai->dma_capture.dma_size = 4;
1217 pri_dai->base = regs_base;
1218 pri_dai->quirks = quirks;
1220 if (quirks & QUIRK_PRI_6CHAN)
1221 pri_dai->i2s_dai_drv.playback.channels_max = 6;
1223 if (quirks & QUIRK_SEC_DAI) {
1224 sec_dai = i2s_alloc_dai(pdev, true);
1226 dev_err(&pdev->dev, "Unable to alloc I2S_sec\n");
1230 sec_dai->dma_playback.dma_addr = regs_base + I2STXDS;
1231 sec_dai->dma_playback.client =
1232 (struct s3c2410_dma_client *)&sec_dai->dma_playback;
1233 sec_dai->dma_playback.ch_name = "tx-sec";
1236 res = platform_get_resource(pdev, IORESOURCE_DMA, 2);
1238 sec_dai->dma_playback.channel = res->start;
1241 sec_dai->dma_playback.dma_size = 4;
1242 sec_dai->base = regs_base;
1243 sec_dai->quirks = quirks;
1244 sec_dai->idma_playback.dma_addr = idma_addr;
1245 sec_dai->pri_dai = pri_dai;
1246 pri_dai->sec_dai = sec_dai;
1249 if (i2s_pdata && i2s_pdata->cfg_gpio && i2s_pdata->cfg_gpio(pdev)) {
1250 dev_err(&pdev->dev, "Unable to configure gpio\n");
1255 devm_snd_soc_register_component(&pri_dai->pdev->dev,
1256 &samsung_i2s_component,
1257 &pri_dai->i2s_dai_drv, 1);
1259 pm_runtime_enable(&pdev->dev);
1261 samsung_asoc_dma_platform_register(&pdev->dev);
1266 release_mem_region(regs_base, resource_size(res));
1271 static int samsung_i2s_remove(struct platform_device *pdev)
1273 struct i2s_dai *i2s, *other;
1274 struct resource *res;
1276 i2s = dev_get_drvdata(&pdev->dev);
1277 other = i2s->pri_dai ? : i2s->sec_dai;
1280 other->pri_dai = NULL;
1281 other->sec_dai = NULL;
1283 pm_runtime_disable(&pdev->dev);
1284 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1286 release_mem_region(res->start, resource_size(res));
1289 i2s->pri_dai = NULL;
1290 i2s->sec_dai = NULL;
1292 samsung_asoc_dma_platform_unregister(&pdev->dev);
1297 static const struct samsung_i2s_dai_data i2sv3_dai_type = {
1298 .dai_type = TYPE_PRI,
1299 .quirks = QUIRK_NO_MUXPSR,
1302 static const struct samsung_i2s_dai_data i2sv5_dai_type = {
1303 .dai_type = TYPE_PRI,
1304 .quirks = QUIRK_PRI_6CHAN | QUIRK_SEC_DAI | QUIRK_NEED_RSTCLR,
1307 static const struct samsung_i2s_dai_data i2sv6_dai_type = {
1308 .dai_type = TYPE_PRI,
1309 .quirks = QUIRK_PRI_6CHAN | QUIRK_SEC_DAI | QUIRK_NEED_RSTCLR |
1313 static const struct samsung_i2s_dai_data samsung_dai_type_pri = {
1314 .dai_type = TYPE_PRI,
1317 static const struct samsung_i2s_dai_data samsung_dai_type_sec = {
1318 .dai_type = TYPE_SEC,
1321 static struct platform_device_id samsung_i2s_driver_ids[] = {
1323 .name = "samsung-i2s",
1324 .driver_data = (kernel_ulong_t)&samsung_dai_type_pri,
1326 .name = "samsung-i2s-sec",
1327 .driver_data = (kernel_ulong_t)&samsung_dai_type_sec,
1331 MODULE_DEVICE_TABLE(platform, samsung_i2s_driver_ids);
1334 static const struct of_device_id exynos_i2s_match[] = {
1336 .compatible = "samsung,s3c6410-i2s",
1337 .data = &i2sv3_dai_type,
1339 .compatible = "samsung,s5pv210-i2s",
1340 .data = &i2sv5_dai_type,
1342 .compatible = "samsung,exynos5420-i2s",
1343 .data = &i2sv6_dai_type,
1347 MODULE_DEVICE_TABLE(of, exynos_i2s_match);
1350 static const struct dev_pm_ops samsung_i2s_pm = {
1351 SET_RUNTIME_PM_OPS(i2s_runtime_suspend,
1352 i2s_runtime_resume, NULL)
1355 static struct platform_driver samsung_i2s_driver = {
1356 .probe = samsung_i2s_probe,
1357 .remove = samsung_i2s_remove,
1358 .id_table = samsung_i2s_driver_ids,
1360 .name = "samsung-i2s",
1361 .owner = THIS_MODULE,
1362 .of_match_table = of_match_ptr(exynos_i2s_match),
1363 .pm = &samsung_i2s_pm,
1367 module_platform_driver(samsung_i2s_driver);
1369 /* Module information */
1370 MODULE_AUTHOR("Jaswinder Singh, <jassisinghbrar@gmail.com>");
1371 MODULE_DESCRIPTION("Samsung I2S Interface");
1372 MODULE_ALIAS("platform:samsung-i2s");
1373 MODULE_LICENSE("GPL");