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_variant_regs {
44 unsigned int rclksrc_off;
46 unsigned int cdclkcon_off;
48 unsigned int bfs_mask;
49 unsigned int rfs_mask;
50 unsigned int ftx0cnt_off;
53 struct samsung_i2s_dai_data {
56 const struct samsung_i2s_variant_regs *i2s_variant_regs;
60 /* Platform device for this DAI */
61 struct platform_device *pdev;
64 /* Physical base address of SFRs */
66 /* Rate of RCLK source clock */
67 unsigned long rclk_srcrate;
71 * Specifically requested RCLK,BCLK by MACHINE Driver.
72 * 0 indicates CPU driver is free to choose any value.
75 /* I2S Controller's core clock */
77 /* Clock for generating I2S signals */
79 /* Pointer to the Primary_Fifo if this is Sec_Fifo, NULL otherwise */
80 struct i2s_dai *pri_dai;
81 /* Pointer to the Secondary_Fifo if it has one, NULL otherwise */
82 struct i2s_dai *sec_dai;
83 #define DAI_OPENED (1 << 0) /* Dai is opened */
84 #define DAI_MANAGER (1 << 1) /* Dai is the manager */
86 /* CDCLK pin direction: 0 - input, 1 - output */
87 unsigned int cdclk_out:1;
88 /* Driver for this DAI */
89 struct snd_soc_dai_driver i2s_dai_drv;
91 struct s3c_dma_params dma_playback;
92 struct s3c_dma_params dma_capture;
93 struct s3c_dma_params idma_playback;
98 unsigned long gpios[7]; /* i2s gpio line numbers */
99 const struct samsung_i2s_variant_regs *variant_regs;
102 /* Lock for cross i/f checks */
103 static DEFINE_SPINLOCK(lock);
105 /* If this is the 'overlay' stereo DAI */
106 static inline bool is_secondary(struct i2s_dai *i2s)
108 return i2s->pri_dai ? true : false;
111 /* If operating in SoC-Slave mode */
112 static inline bool is_slave(struct i2s_dai *i2s)
114 u32 mod = readl(i2s->addr + I2SMOD);
115 return (mod & (1 << i2s->variant_regs->mss_off)) ? true : false;
118 /* If this interface of the controller is transmitting data */
119 static inline bool tx_active(struct i2s_dai *i2s)
126 active = readl(i2s->addr + I2SCON);
128 if (is_secondary(i2s))
129 active &= CON_TXSDMA_ACTIVE;
131 active &= CON_TXDMA_ACTIVE;
133 return active ? true : false;
136 /* If the other interface of the controller is transmitting data */
137 static inline bool other_tx_active(struct i2s_dai *i2s)
139 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
141 return tx_active(other);
144 /* If any interface of the controller is transmitting data */
145 static inline bool any_tx_active(struct i2s_dai *i2s)
147 return tx_active(i2s) || other_tx_active(i2s);
150 /* If this interface of the controller is receiving data */
151 static inline bool rx_active(struct i2s_dai *i2s)
158 active = readl(i2s->addr + I2SCON) & CON_RXDMA_ACTIVE;
160 return active ? true : false;
163 /* If the other interface of the controller is receiving data */
164 static inline bool other_rx_active(struct i2s_dai *i2s)
166 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
168 return rx_active(other);
171 /* If any interface of the controller is receiving data */
172 static inline bool any_rx_active(struct i2s_dai *i2s)
174 return rx_active(i2s) || other_rx_active(i2s);
177 /* If the other DAI is transmitting or receiving data */
178 static inline bool other_active(struct i2s_dai *i2s)
180 return other_rx_active(i2s) || other_tx_active(i2s);
183 /* If this DAI is transmitting or receiving data */
184 static inline bool this_active(struct i2s_dai *i2s)
186 return tx_active(i2s) || rx_active(i2s);
189 /* If the controller is active anyway */
190 static inline bool any_active(struct i2s_dai *i2s)
192 return this_active(i2s) || other_active(i2s);
195 static inline struct i2s_dai *to_info(struct snd_soc_dai *dai)
197 return snd_soc_dai_get_drvdata(dai);
200 static inline bool is_opened(struct i2s_dai *i2s)
202 if (i2s && (i2s->mode & DAI_OPENED))
208 static inline bool is_manager(struct i2s_dai *i2s)
210 if (is_opened(i2s) && (i2s->mode & DAI_MANAGER))
216 /* Read RCLK of I2S (in multiples of LRCLK) */
217 static inline unsigned get_rfs(struct i2s_dai *i2s)
220 rfs = readl(i2s->addr + I2SMOD) >> i2s->variant_regs->rfs_off;
221 rfs &= i2s->variant_regs->rfs_mask;
235 /* Write RCLK of I2S (in multiples of LRCLK) */
236 static inline void set_rfs(struct i2s_dai *i2s, unsigned rfs)
238 u32 mod = readl(i2s->addr + I2SMOD);
239 int rfs_shift = i2s->variant_regs->rfs_off;
241 mod &= ~(i2s->variant_regs->rfs_mask << rfs_shift);
245 mod |= (EXYNOS7_MOD_RCLK_192FS << rfs_shift);
248 mod |= (EXYNOS7_MOD_RCLK_96FS << rfs_shift);
251 mod |= (EXYNOS7_MOD_RCLK_128FS << rfs_shift);
254 mod |= (EXYNOS7_MOD_RCLK_64FS << rfs_shift);
257 mod |= (MOD_RCLK_768FS << rfs_shift);
260 mod |= (MOD_RCLK_512FS << rfs_shift);
263 mod |= (MOD_RCLK_384FS << rfs_shift);
266 mod |= (MOD_RCLK_256FS << rfs_shift);
270 writel(mod, i2s->addr + I2SMOD);
273 /* Read Bit-Clock of I2S (in multiples of LRCLK) */
274 static inline unsigned get_bfs(struct i2s_dai *i2s)
277 bfs = readl(i2s->addr + I2SMOD) >> i2s->variant_regs->bfs_off;
278 bfs &= i2s->variant_regs->bfs_mask;
293 /* Write Bit-Clock of I2S (in multiples of LRCLK) */
294 static inline void set_bfs(struct i2s_dai *i2s, unsigned bfs)
296 u32 mod = readl(i2s->addr + I2SMOD);
297 int tdm = i2s->quirks & QUIRK_SUPPORTS_TDM;
298 int bfs_shift = i2s->variant_regs->bfs_off;
300 /* Non-TDM I2S controllers do not support BCLK > 48 * FS */
301 if (!tdm && bfs > 48) {
302 dev_err(&i2s->pdev->dev, "Unsupported BCLK divider\n");
306 mod &= ~(i2s->variant_regs->bfs_mask << bfs_shift);
310 mod |= (MOD_BCLK_48FS << bfs_shift);
313 mod |= (MOD_BCLK_32FS << bfs_shift);
316 mod |= (MOD_BCLK_24FS << bfs_shift);
319 mod |= (MOD_BCLK_16FS << bfs_shift);
322 mod |= (EXYNOS5420_MOD_BCLK_64FS << bfs_shift);
325 mod |= (EXYNOS5420_MOD_BCLK_96FS << bfs_shift);
328 mod |= (EXYNOS5420_MOD_BCLK_128FS << bfs_shift);
331 mod |= (EXYNOS5420_MOD_BCLK_192FS << bfs_shift);
334 mod |= (EXYNOS5420_MOD_BCLK_256FS << bfs_shift);
337 dev_err(&i2s->pdev->dev, "Wrong BCLK Divider!\n");
341 writel(mod, i2s->addr + I2SMOD);
345 static inline int get_blc(struct i2s_dai *i2s)
347 int blc = readl(i2s->addr + I2SMOD);
349 blc = (blc >> 13) & 0x3;
358 /* TX Channel Control */
359 static void i2s_txctrl(struct i2s_dai *i2s, int on)
361 void __iomem *addr = i2s->addr;
362 int txr_off = i2s->variant_regs->txr_off;
363 u32 con = readl(addr + I2SCON);
364 u32 mod = readl(addr + I2SMOD) & ~(3 << txr_off);
368 con &= ~CON_TXCH_PAUSE;
370 if (is_secondary(i2s)) {
371 con |= CON_TXSDMA_ACTIVE;
372 con &= ~CON_TXSDMA_PAUSE;
374 con |= CON_TXDMA_ACTIVE;
375 con &= ~CON_TXDMA_PAUSE;
378 if (any_rx_active(i2s))
383 if (is_secondary(i2s)) {
384 con |= CON_TXSDMA_PAUSE;
385 con &= ~CON_TXSDMA_ACTIVE;
387 con |= CON_TXDMA_PAUSE;
388 con &= ~CON_TXDMA_ACTIVE;
391 if (other_tx_active(i2s)) {
392 writel(con, addr + I2SCON);
396 con |= CON_TXCH_PAUSE;
398 if (any_rx_active(i2s))
404 writel(mod, addr + I2SMOD);
405 writel(con, addr + I2SCON);
408 /* RX Channel Control */
409 static void i2s_rxctrl(struct i2s_dai *i2s, int on)
411 void __iomem *addr = i2s->addr;
412 int txr_off = i2s->variant_regs->txr_off;
413 u32 con = readl(addr + I2SCON);
414 u32 mod = readl(addr + I2SMOD) & ~(3 << txr_off);
417 con |= CON_RXDMA_ACTIVE | CON_ACTIVE;
418 con &= ~(CON_RXDMA_PAUSE | CON_RXCH_PAUSE);
420 if (any_tx_active(i2s))
425 con |= CON_RXDMA_PAUSE | CON_RXCH_PAUSE;
426 con &= ~CON_RXDMA_ACTIVE;
428 if (any_tx_active(i2s))
434 writel(mod, addr + I2SMOD);
435 writel(con, addr + I2SCON);
438 /* Flush FIFO of an interface */
439 static inline void i2s_fifo(struct i2s_dai *i2s, u32 flush)
447 if (is_secondary(i2s))
448 fic = i2s->addr + I2SFICS;
450 fic = i2s->addr + I2SFIC;
453 writel(readl(fic) | flush, fic);
456 val = msecs_to_loops(1) / 1000; /* 1 usec */
460 writel(readl(fic) & ~flush, fic);
463 static int i2s_set_sysclk(struct snd_soc_dai *dai,
464 int clk_id, unsigned int rfs, int dir)
466 struct i2s_dai *i2s = to_info(dai);
467 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
468 u32 mod = readl(i2s->addr + I2SMOD);
469 const struct samsung_i2s_variant_regs *i2s_regs = i2s->variant_regs;
470 unsigned int cdcon_mask = 1 << i2s_regs->cdclkcon_off;
471 unsigned int rsrc_mask = 1 << i2s_regs->rclksrc_off;
474 case SAMSUNG_I2S_OPCLK:
475 mod &= ~MOD_OPCLK_MASK;
478 case SAMSUNG_I2S_CDCLK:
479 /* Shouldn't matter in GATING(CLOCK_IN) mode */
480 if (dir == SND_SOC_CLOCK_IN)
483 if ((rfs && other && other->rfs && (other->rfs != rfs)) ||
485 (((dir == SND_SOC_CLOCK_IN)
486 && !(mod & cdcon_mask)) ||
487 ((dir == SND_SOC_CLOCK_OUT)
488 && (mod & cdcon_mask))))) {
489 dev_err(&i2s->pdev->dev,
490 "%s:%d Other DAI busy\n", __func__, __LINE__);
494 if (dir == SND_SOC_CLOCK_IN)
495 mod |= 1 << i2s_regs->cdclkcon_off;
497 mod &= ~(1 << i2s_regs->cdclkcon_off);
502 case SAMSUNG_I2S_RCLKSRC_0: /* clock corrsponding to IISMOD[10] := 0 */
503 case SAMSUNG_I2S_RCLKSRC_1: /* clock corrsponding to IISMOD[10] := 1 */
504 if ((i2s->quirks & QUIRK_NO_MUXPSR)
505 || (clk_id == SAMSUNG_I2S_RCLKSRC_0))
510 if (!any_active(i2s)) {
511 if (i2s->op_clk && !IS_ERR(i2s->op_clk)) {
512 if ((clk_id && !(mod & rsrc_mask)) ||
513 (!clk_id && (mod & rsrc_mask))) {
514 clk_disable_unprepare(i2s->op_clk);
515 clk_put(i2s->op_clk);
518 clk_get_rate(i2s->op_clk);
524 i2s->op_clk = clk_get(&i2s->pdev->dev,
527 i2s->op_clk = clk_get(&i2s->pdev->dev,
530 if (WARN_ON(IS_ERR(i2s->op_clk)))
531 return PTR_ERR(i2s->op_clk);
533 clk_prepare_enable(i2s->op_clk);
534 i2s->rclk_srcrate = clk_get_rate(i2s->op_clk);
536 /* Over-ride the other's */
538 other->op_clk = i2s->op_clk;
539 other->rclk_srcrate = i2s->rclk_srcrate;
541 } else if ((!clk_id && (mod & rsrc_mask))
542 || (clk_id && !(mod & rsrc_mask))) {
543 dev_err(&i2s->pdev->dev,
544 "%s:%d Other DAI busy\n", __func__, __LINE__);
547 /* Call can't be on the active DAI */
548 i2s->op_clk = other->op_clk;
549 i2s->rclk_srcrate = other->rclk_srcrate;
554 mod &= ~(1 << i2s_regs->rclksrc_off);
556 mod |= 1 << i2s_regs->rclksrc_off;
560 dev_err(&i2s->pdev->dev, "We don't serve that!\n");
564 writel(mod, i2s->addr + I2SMOD);
569 static int i2s_set_fmt(struct snd_soc_dai *dai,
572 struct i2s_dai *i2s = to_info(dai);
573 u32 mod = readl(i2s->addr + I2SMOD);
574 int lrp_shift, sdf_shift, sdf_mask, lrp_rlow, mod_slave;
577 lrp_shift = i2s->variant_regs->lrp_off;
578 sdf_shift = i2s->variant_regs->sdf_off;
579 mod_slave = 1 << i2s->variant_regs->mss_off;
581 sdf_mask = MOD_SDF_MASK << sdf_shift;
582 lrp_rlow = MOD_LR_RLOW << lrp_shift;
584 /* Format is priority */
585 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
586 case SND_SOC_DAIFMT_RIGHT_J:
588 tmp |= (MOD_SDF_MSB << sdf_shift);
590 case SND_SOC_DAIFMT_LEFT_J:
592 tmp |= (MOD_SDF_LSB << sdf_shift);
594 case SND_SOC_DAIFMT_I2S:
595 tmp |= (MOD_SDF_IIS << sdf_shift);
598 dev_err(&i2s->pdev->dev, "Format not supported\n");
603 * INV flag is relative to the FORMAT flag - if set it simply
604 * flips the polarity specified by the Standard
606 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
607 case SND_SOC_DAIFMT_NB_NF:
609 case SND_SOC_DAIFMT_NB_IF:
616 dev_err(&i2s->pdev->dev, "Polarity not supported\n");
620 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
621 case SND_SOC_DAIFMT_CBM_CFM:
624 case SND_SOC_DAIFMT_CBS_CFS:
625 /* Set default source clock in Master mode */
626 if (i2s->rclk_srcrate == 0)
627 i2s_set_sysclk(dai, SAMSUNG_I2S_RCLKSRC_0,
628 0, SND_SOC_CLOCK_IN);
631 dev_err(&i2s->pdev->dev, "master/slave format not supported\n");
636 * Don't change the I2S mode if any controller is active on this
639 if (any_active(i2s) &&
640 ((mod & (sdf_mask | lrp_rlow | mod_slave)) != tmp)) {
641 dev_err(&i2s->pdev->dev,
642 "%s:%d Other DAI busy\n", __func__, __LINE__);
646 mod &= ~(sdf_mask | lrp_rlow | mod_slave);
648 writel(mod, i2s->addr + I2SMOD);
653 static int i2s_hw_params(struct snd_pcm_substream *substream,
654 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
656 struct i2s_dai *i2s = to_info(dai);
657 u32 mod = readl(i2s->addr + I2SMOD);
659 if (!is_secondary(i2s))
660 mod &= ~(MOD_DC2_EN | MOD_DC1_EN);
662 switch (params_channels(params)) {
669 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
670 i2s->dma_playback.dma_size = 4;
672 i2s->dma_capture.dma_size = 4;
675 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
676 i2s->dma_playback.dma_size = 2;
678 i2s->dma_capture.dma_size = 2;
682 dev_err(&i2s->pdev->dev, "%d channels not supported\n",
683 params_channels(params));
687 if (is_secondary(i2s))
688 mod &= ~MOD_BLCS_MASK;
690 mod &= ~MOD_BLCP_MASK;
693 mod &= ~MOD_BLC_MASK;
695 switch (params_width(params)) {
697 if (is_secondary(i2s))
698 mod |= MOD_BLCS_8BIT;
700 mod |= MOD_BLCP_8BIT;
705 if (is_secondary(i2s))
706 mod |= MOD_BLCS_16BIT;
708 mod |= MOD_BLCP_16BIT;
710 mod |= MOD_BLC_16BIT;
713 if (is_secondary(i2s))
714 mod |= MOD_BLCS_24BIT;
716 mod |= MOD_BLCP_24BIT;
718 mod |= MOD_BLC_24BIT;
721 dev_err(&i2s->pdev->dev, "Format(%d) not supported\n",
722 params_format(params));
725 writel(mod, i2s->addr + I2SMOD);
727 samsung_asoc_init_dma_data(dai, &i2s->dma_playback, &i2s->dma_capture);
729 i2s->frmclk = params_rate(params);
734 /* We set constraints on the substream acc to the version of I2S */
735 static int i2s_startup(struct snd_pcm_substream *substream,
736 struct snd_soc_dai *dai)
738 struct i2s_dai *i2s = to_info(dai);
739 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
742 spin_lock_irqsave(&lock, flags);
744 i2s->mode |= DAI_OPENED;
746 if (is_manager(other))
747 i2s->mode &= ~DAI_MANAGER;
749 i2s->mode |= DAI_MANAGER;
751 if (!any_active(i2s) && (i2s->quirks & QUIRK_NEED_RSTCLR))
752 writel(CON_RSTCLR, i2s->addr + I2SCON);
754 spin_unlock_irqrestore(&lock, flags);
756 if (!is_opened(other) && i2s->cdclk_out)
757 i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK,
758 0, SND_SOC_CLOCK_OUT);
762 static void i2s_shutdown(struct snd_pcm_substream *substream,
763 struct snd_soc_dai *dai)
765 struct i2s_dai *i2s = to_info(dai);
766 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
768 const struct samsung_i2s_variant_regs *i2s_regs = i2s->variant_regs;
770 spin_lock_irqsave(&lock, flags);
772 i2s->mode &= ~DAI_OPENED;
773 i2s->mode &= ~DAI_MANAGER;
775 if (is_opened(other)) {
776 other->mode |= DAI_MANAGER;
778 u32 mod = readl(i2s->addr + I2SMOD);
779 i2s->cdclk_out = !(mod & (1 << i2s_regs->cdclkcon_off));
781 other->cdclk_out = i2s->cdclk_out;
783 /* Reset any constraint on RFS and BFS */
787 spin_unlock_irqrestore(&lock, flags);
789 /* Gate CDCLK by default */
790 if (!is_opened(other))
791 i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK,
792 0, SND_SOC_CLOCK_IN);
795 static int config_setup(struct i2s_dai *i2s)
797 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
798 unsigned rfs, bfs, blc;
808 /* Select least possible multiple(2) if no constraint set */
817 if ((rfs == 256 || rfs == 512) && (blc == 24)) {
818 dev_err(&i2s->pdev->dev,
819 "%d-RFS not supported for 24-blc\n", rfs);
824 if (bfs == 16 || bfs == 32)
830 /* If already setup and running */
831 if (any_active(i2s) && (get_rfs(i2s) != rfs || get_bfs(i2s) != bfs)) {
832 dev_err(&i2s->pdev->dev,
833 "%s:%d Other DAI busy\n", __func__, __LINE__);
840 /* Don't bother with PSR in Slave mode */
844 if (!(i2s->quirks & QUIRK_NO_MUXPSR)) {
845 psr = i2s->rclk_srcrate / i2s->frmclk / rfs;
846 writel(((psr - 1) << 8) | PSR_PSREN, i2s->addr + I2SPSR);
847 dev_dbg(&i2s->pdev->dev,
848 "RCLK_SRC=%luHz PSR=%u, RCLK=%dfs, BCLK=%dfs\n",
849 i2s->rclk_srcrate, psr, rfs, bfs);
855 static int i2s_trigger(struct snd_pcm_substream *substream,
856 int cmd, struct snd_soc_dai *dai)
858 int capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
859 struct snd_soc_pcm_runtime *rtd = substream->private_data;
860 struct i2s_dai *i2s = to_info(rtd->cpu_dai);
864 case SNDRV_PCM_TRIGGER_START:
865 case SNDRV_PCM_TRIGGER_RESUME:
866 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
867 local_irq_save(flags);
869 if (config_setup(i2s)) {
870 local_irq_restore(flags);
879 local_irq_restore(flags);
881 case SNDRV_PCM_TRIGGER_STOP:
882 case SNDRV_PCM_TRIGGER_SUSPEND:
883 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
884 local_irq_save(flags);
888 i2s_fifo(i2s, FIC_RXFLUSH);
891 i2s_fifo(i2s, FIC_TXFLUSH);
894 local_irq_restore(flags);
901 static int i2s_set_clkdiv(struct snd_soc_dai *dai,
904 struct i2s_dai *i2s = to_info(dai);
905 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
908 case SAMSUNG_I2S_DIV_BCLK:
909 if ((any_active(i2s) && div && (get_bfs(i2s) != div))
910 || (other && other->bfs && (other->bfs != div))) {
911 dev_err(&i2s->pdev->dev,
912 "%s:%d Other DAI busy\n", __func__, __LINE__);
918 dev_err(&i2s->pdev->dev,
919 "Invalid clock divider(%d)\n", div_id);
926 static snd_pcm_sframes_t
927 i2s_delay(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
929 struct i2s_dai *i2s = to_info(dai);
930 u32 reg = readl(i2s->addr + I2SFIC);
931 snd_pcm_sframes_t delay;
932 const struct samsung_i2s_variant_regs *i2s_regs = i2s->variant_regs;
934 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
935 delay = FIC_RXCOUNT(reg);
936 else if (is_secondary(i2s))
937 delay = FICS_TXCOUNT(readl(i2s->addr + I2SFICS));
939 delay = (reg >> i2s_regs->ftx0cnt_off) & 0x7f;
945 static int i2s_suspend(struct snd_soc_dai *dai)
947 struct i2s_dai *i2s = to_info(dai);
949 i2s->suspend_i2smod = readl(i2s->addr + I2SMOD);
950 i2s->suspend_i2scon = readl(i2s->addr + I2SCON);
951 i2s->suspend_i2spsr = readl(i2s->addr + I2SPSR);
956 static int i2s_resume(struct snd_soc_dai *dai)
958 struct i2s_dai *i2s = to_info(dai);
960 writel(i2s->suspend_i2scon, i2s->addr + I2SCON);
961 writel(i2s->suspend_i2smod, i2s->addr + I2SMOD);
962 writel(i2s->suspend_i2spsr, i2s->addr + I2SPSR);
967 #define i2s_suspend NULL
968 #define i2s_resume NULL
971 static int samsung_i2s_dai_probe(struct snd_soc_dai *dai)
973 struct i2s_dai *i2s = to_info(dai);
974 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
977 if (other && other->clk) { /* If this is probe on secondary */
978 samsung_asoc_init_dma_data(dai, &other->sec_dai->dma_playback,
983 i2s->addr = ioremap(i2s->base, 0x100);
984 if (i2s->addr == NULL) {
985 dev_err(&i2s->pdev->dev, "cannot ioremap registers\n");
989 i2s->clk = clk_get(&i2s->pdev->dev, "iis");
990 if (IS_ERR(i2s->clk)) {
991 dev_err(&i2s->pdev->dev, "failed to get i2s_clock\n");
993 return PTR_ERR(i2s->clk);
996 ret = clk_prepare_enable(i2s->clk);
998 dev_err(&i2s->pdev->dev, "failed to enable clock: %d\n", ret);
1002 samsung_asoc_init_dma_data(dai, &i2s->dma_playback, &i2s->dma_capture);
1005 other->addr = i2s->addr;
1006 other->clk = i2s->clk;
1009 if (i2s->quirks & QUIRK_NEED_RSTCLR)
1010 writel(CON_RSTCLR, i2s->addr + I2SCON);
1012 if (i2s->quirks & QUIRK_SUPPORTS_IDMA)
1013 idma_reg_addr_init(i2s->addr,
1014 i2s->sec_dai->idma_playback.dma_addr);
1017 /* Reset any constraint on RFS and BFS */
1020 i2s->rclk_srcrate = 0;
1023 i2s_fifo(i2s, FIC_TXFLUSH);
1024 i2s_fifo(other, FIC_TXFLUSH);
1025 i2s_fifo(i2s, FIC_RXFLUSH);
1027 /* Gate CDCLK by default */
1028 if (!is_opened(other))
1029 i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK,
1030 0, SND_SOC_CLOCK_IN);
1035 static int samsung_i2s_dai_remove(struct snd_soc_dai *dai)
1037 struct i2s_dai *i2s = snd_soc_dai_get_drvdata(dai);
1038 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
1040 if (!other || !other->clk) {
1042 if (i2s->quirks & QUIRK_NEED_RSTCLR)
1043 writel(0, i2s->addr + I2SCON);
1045 clk_disable_unprepare(i2s->clk);
1056 static const struct snd_soc_dai_ops samsung_i2s_dai_ops = {
1057 .trigger = i2s_trigger,
1058 .hw_params = i2s_hw_params,
1059 .set_fmt = i2s_set_fmt,
1060 .set_clkdiv = i2s_set_clkdiv,
1061 .set_sysclk = i2s_set_sysclk,
1062 .startup = i2s_startup,
1063 .shutdown = i2s_shutdown,
1067 static const struct snd_soc_component_driver samsung_i2s_component = {
1068 .name = "samsung-i2s",
1071 #define SAMSUNG_I2S_RATES SNDRV_PCM_RATE_8000_96000
1073 #define SAMSUNG_I2S_FMTS (SNDRV_PCM_FMTBIT_S8 | \
1074 SNDRV_PCM_FMTBIT_S16_LE | \
1075 SNDRV_PCM_FMTBIT_S24_LE)
1077 static struct i2s_dai *i2s_alloc_dai(struct platform_device *pdev, bool sec)
1079 struct i2s_dai *i2s;
1082 i2s = devm_kzalloc(&pdev->dev, sizeof(struct i2s_dai), GFP_KERNEL);
1087 i2s->pri_dai = NULL;
1088 i2s->sec_dai = NULL;
1089 i2s->i2s_dai_drv.symmetric_rates = 1;
1090 i2s->i2s_dai_drv.probe = samsung_i2s_dai_probe;
1091 i2s->i2s_dai_drv.remove = samsung_i2s_dai_remove;
1092 i2s->i2s_dai_drv.ops = &samsung_i2s_dai_ops;
1093 i2s->i2s_dai_drv.suspend = i2s_suspend;
1094 i2s->i2s_dai_drv.resume = i2s_resume;
1095 i2s->i2s_dai_drv.playback.channels_min = 1;
1096 i2s->i2s_dai_drv.playback.channels_max = 2;
1097 i2s->i2s_dai_drv.playback.rates = SAMSUNG_I2S_RATES;
1098 i2s->i2s_dai_drv.playback.formats = SAMSUNG_I2S_FMTS;
1101 i2s->i2s_dai_drv.capture.channels_min = 1;
1102 i2s->i2s_dai_drv.capture.channels_max = 2;
1103 i2s->i2s_dai_drv.capture.rates = SAMSUNG_I2S_RATES;
1104 i2s->i2s_dai_drv.capture.formats = SAMSUNG_I2S_FMTS;
1105 dev_set_drvdata(&i2s->pdev->dev, i2s);
1106 } else { /* Create a new platform_device for Secondary */
1107 i2s->pdev = platform_device_alloc("samsung-i2s-sec", -1);
1111 i2s->pdev->dev.parent = &pdev->dev;
1113 platform_set_drvdata(i2s->pdev, i2s);
1114 ret = platform_device_add(i2s->pdev);
1122 static const struct of_device_id exynos_i2s_match[];
1124 static inline const struct samsung_i2s_dai_data *samsung_i2s_get_driver_data(
1125 struct platform_device *pdev)
1128 if (pdev->dev.of_node) {
1129 const struct of_device_id *match;
1130 match = of_match_node(exynos_i2s_match, pdev->dev.of_node);
1134 return (struct samsung_i2s_dai_data *)
1135 platform_get_device_id(pdev)->driver_data;
1139 static int i2s_runtime_suspend(struct device *dev)
1141 struct i2s_dai *i2s = dev_get_drvdata(dev);
1143 clk_disable_unprepare(i2s->clk);
1148 static int i2s_runtime_resume(struct device *dev)
1150 struct i2s_dai *i2s = dev_get_drvdata(dev);
1152 clk_prepare_enable(i2s->clk);
1156 #endif /* CONFIG_PM */
1158 static int samsung_i2s_probe(struct platform_device *pdev)
1160 struct i2s_dai *pri_dai, *sec_dai = NULL;
1161 struct s3c_audio_pdata *i2s_pdata = pdev->dev.platform_data;
1162 struct samsung_i2s *i2s_cfg = NULL;
1163 struct resource *res;
1164 u32 regs_base, quirks = 0, idma_addr = 0;
1165 struct device_node *np = pdev->dev.of_node;
1166 const struct samsung_i2s_dai_data *i2s_dai_data;
1169 /* Call during Seconday interface registration */
1170 i2s_dai_data = samsung_i2s_get_driver_data(pdev);
1172 if (i2s_dai_data->dai_type == TYPE_SEC) {
1173 sec_dai = dev_get_drvdata(&pdev->dev);
1175 dev_err(&pdev->dev, "Unable to get drvdata\n");
1178 devm_snd_soc_register_component(&sec_dai->pdev->dev,
1179 &samsung_i2s_component,
1180 &sec_dai->i2s_dai_drv, 1);
1181 samsung_asoc_dma_platform_register(&pdev->dev);
1185 pri_dai = i2s_alloc_dai(pdev, false);
1187 dev_err(&pdev->dev, "Unable to alloc I2S_pri\n");
1192 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1195 "Unable to get I2S-TX dma resource\n");
1198 pri_dai->dma_playback.channel = res->start;
1200 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1203 "Unable to get I2S-RX dma resource\n");
1206 pri_dai->dma_capture.channel = res->start;
1208 if (i2s_pdata == NULL) {
1209 dev_err(&pdev->dev, "Can't work without s3c_audio_pdata\n");
1213 if (&i2s_pdata->type)
1214 i2s_cfg = &i2s_pdata->type.i2s;
1217 quirks = i2s_cfg->quirks;
1218 idma_addr = i2s_cfg->idma_addr;
1221 quirks = i2s_dai_data->quirks;
1222 if (of_property_read_u32(np, "samsung,idma-addr",
1224 if (quirks & QUIRK_SUPPORTS_IDMA) {
1225 dev_info(&pdev->dev, "idma address is not"\
1231 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1233 dev_err(&pdev->dev, "Unable to get I2S SFR address\n");
1237 if (!request_mem_region(res->start, resource_size(res),
1239 dev_err(&pdev->dev, "Unable to request SFR region\n");
1242 regs_base = res->start;
1244 pri_dai->dma_playback.dma_addr = regs_base + I2STXD;
1245 pri_dai->dma_capture.dma_addr = regs_base + I2SRXD;
1246 pri_dai->dma_playback.ch_name = "tx";
1247 pri_dai->dma_capture.ch_name = "rx";
1248 pri_dai->dma_playback.dma_size = 4;
1249 pri_dai->dma_capture.dma_size = 4;
1250 pri_dai->base = regs_base;
1251 pri_dai->quirks = quirks;
1252 pri_dai->variant_regs = i2s_dai_data->i2s_variant_regs;
1254 if (quirks & QUIRK_PRI_6CHAN)
1255 pri_dai->i2s_dai_drv.playback.channels_max = 6;
1257 if (quirks & QUIRK_SEC_DAI) {
1258 sec_dai = i2s_alloc_dai(pdev, true);
1260 dev_err(&pdev->dev, "Unable to alloc I2S_sec\n");
1264 sec_dai->dma_playback.dma_addr = regs_base + I2STXDS;
1265 sec_dai->dma_playback.ch_name = "tx-sec";
1268 res = platform_get_resource(pdev, IORESOURCE_DMA, 2);
1270 sec_dai->dma_playback.channel = res->start;
1273 sec_dai->dma_playback.dma_size = 4;
1274 sec_dai->base = regs_base;
1275 sec_dai->quirks = quirks;
1276 sec_dai->idma_playback.dma_addr = idma_addr;
1277 sec_dai->pri_dai = pri_dai;
1278 pri_dai->sec_dai = sec_dai;
1281 if (i2s_pdata && i2s_pdata->cfg_gpio && i2s_pdata->cfg_gpio(pdev)) {
1282 dev_err(&pdev->dev, "Unable to configure gpio\n");
1287 devm_snd_soc_register_component(&pri_dai->pdev->dev,
1288 &samsung_i2s_component,
1289 &pri_dai->i2s_dai_drv, 1);
1291 pm_runtime_enable(&pdev->dev);
1293 samsung_asoc_dma_platform_register(&pdev->dev);
1298 release_mem_region(regs_base, resource_size(res));
1303 static int samsung_i2s_remove(struct platform_device *pdev)
1305 struct i2s_dai *i2s, *other;
1306 struct resource *res;
1308 i2s = dev_get_drvdata(&pdev->dev);
1309 other = i2s->pri_dai ? : i2s->sec_dai;
1312 other->pri_dai = NULL;
1313 other->sec_dai = NULL;
1315 pm_runtime_disable(&pdev->dev);
1316 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1318 release_mem_region(res->start, resource_size(res));
1321 i2s->pri_dai = NULL;
1322 i2s->sec_dai = NULL;
1327 static const struct samsung_i2s_variant_regs i2sv3_regs = {
1341 static const struct samsung_i2s_variant_regs i2sv6_regs = {
1355 static const struct samsung_i2s_variant_regs i2sv7_regs = {
1369 static const struct samsung_i2s_variant_regs i2sv5_i2s1_regs = {
1383 static const struct samsung_i2s_dai_data i2sv3_dai_type = {
1384 .dai_type = TYPE_PRI,
1385 .quirks = QUIRK_NO_MUXPSR,
1386 .i2s_variant_regs = &i2sv3_regs,
1389 static const struct samsung_i2s_dai_data i2sv5_dai_type = {
1390 .dai_type = TYPE_PRI,
1391 .quirks = QUIRK_PRI_6CHAN | QUIRK_SEC_DAI | QUIRK_NEED_RSTCLR |
1392 QUIRK_SUPPORTS_IDMA,
1393 .i2s_variant_regs = &i2sv3_regs,
1396 static const struct samsung_i2s_dai_data i2sv6_dai_type = {
1397 .dai_type = TYPE_PRI,
1398 .quirks = QUIRK_PRI_6CHAN | QUIRK_SEC_DAI | QUIRK_NEED_RSTCLR |
1399 QUIRK_SUPPORTS_TDM | QUIRK_SUPPORTS_IDMA,
1400 .i2s_variant_regs = &i2sv6_regs,
1403 static const struct samsung_i2s_dai_data i2sv7_dai_type = {
1404 .dai_type = TYPE_PRI,
1405 .quirks = QUIRK_PRI_6CHAN | QUIRK_SEC_DAI | QUIRK_NEED_RSTCLR |
1407 .i2s_variant_regs = &i2sv7_regs,
1410 static const struct samsung_i2s_dai_data i2sv5_dai_type_i2s1 = {
1411 .dai_type = TYPE_PRI,
1412 .quirks = QUIRK_PRI_6CHAN | QUIRK_NEED_RSTCLR,
1413 .i2s_variant_regs = &i2sv5_i2s1_regs,
1416 static const struct samsung_i2s_dai_data samsung_dai_type_pri = {
1417 .dai_type = TYPE_PRI,
1420 static const struct samsung_i2s_dai_data samsung_dai_type_sec = {
1421 .dai_type = TYPE_SEC,
1424 static struct platform_device_id samsung_i2s_driver_ids[] = {
1426 .name = "samsung-i2s",
1427 .driver_data = (kernel_ulong_t)&i2sv3_dai_type,
1429 .name = "samsung-i2s-sec",
1430 .driver_data = (kernel_ulong_t)&samsung_dai_type_sec,
1432 .name = "samsung-i2sv4",
1433 .driver_data = (kernel_ulong_t)&i2sv5_dai_type,
1437 MODULE_DEVICE_TABLE(platform, samsung_i2s_driver_ids);
1440 static const struct of_device_id exynos_i2s_match[] = {
1442 .compatible = "samsung,s3c6410-i2s",
1443 .data = &i2sv3_dai_type,
1445 .compatible = "samsung,s5pv210-i2s",
1446 .data = &i2sv5_dai_type,
1448 .compatible = "samsung,exynos5420-i2s",
1449 .data = &i2sv6_dai_type,
1451 .compatible = "samsung,exynos7-i2s",
1452 .data = &i2sv7_dai_type,
1454 .compatible = "samsung,exynos7-i2s1",
1455 .data = &i2sv5_dai_type_i2s1,
1459 MODULE_DEVICE_TABLE(of, exynos_i2s_match);
1462 static const struct dev_pm_ops samsung_i2s_pm = {
1463 SET_RUNTIME_PM_OPS(i2s_runtime_suspend,
1464 i2s_runtime_resume, NULL)
1467 static struct platform_driver samsung_i2s_driver = {
1468 .probe = samsung_i2s_probe,
1469 .remove = samsung_i2s_remove,
1470 .id_table = samsung_i2s_driver_ids,
1472 .name = "samsung-i2s",
1473 .of_match_table = of_match_ptr(exynos_i2s_match),
1474 .pm = &samsung_i2s_pm,
1478 module_platform_driver(samsung_i2s_driver);
1480 /* Module information */
1481 MODULE_AUTHOR("Jaswinder Singh, <jassisinghbrar@gmail.com>");
1482 MODULE_DESCRIPTION("Samsung I2S Interface");
1483 MODULE_ALIAS("platform:samsung-i2s");
1484 MODULE_LICENSE("GPL");