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 <dt-bindings/sound/samsung-i2s.h>
14 #include <linux/delay.h>
15 #include <linux/slab.h>
16 #include <linux/clk.h>
17 #include <linux/clk-provider.h>
19 #include <linux/module.h>
21 #include <linux/of_gpio.h>
22 #include <linux/pm_runtime.h>
24 #include <sound/soc.h>
25 #include <sound/pcm_params.h>
27 #include <linux/platform_data/asoc-s3c.h>
34 #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
36 enum samsung_dai_type
{
41 struct samsung_i2s_variant_regs
{
46 unsigned int rclksrc_off
;
48 unsigned int cdclkcon_off
;
50 unsigned int bfs_mask
;
51 unsigned int rfs_mask
;
52 unsigned int ftx0cnt_off
;
55 struct samsung_i2s_dai_data
{
58 const struct samsung_i2s_variant_regs
*i2s_variant_regs
;
62 /* Platform device for this DAI */
63 struct platform_device
*pdev
;
64 /* Memory mapped SFR region */
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 /* Driver for this DAI */
87 struct snd_soc_dai_driver i2s_dai_drv
;
89 struct s3c_dma_params dma_playback
;
90 struct s3c_dma_params dma_capture
;
91 struct s3c_dma_params idma_playback
;
97 const struct samsung_i2s_variant_regs
*variant_regs
;
99 /* Spinlock protecting access to the device's registers */
103 /* Below fields are only valid if this is the primary FIFO */
104 struct clk
*clk_table
[3];
105 struct clk_onecell_data clk_data
;
108 /* Lock for cross i/f checks */
109 static DEFINE_SPINLOCK(lock
);
111 /* If this is the 'overlay' stereo DAI */
112 static inline bool is_secondary(struct i2s_dai
*i2s
)
114 return i2s
->pri_dai
? true : false;
117 /* If operating in SoC-Slave mode */
118 static inline bool is_slave(struct i2s_dai
*i2s
)
120 u32 mod
= readl(i2s
->addr
+ I2SMOD
);
121 return (mod
& (1 << i2s
->variant_regs
->mss_off
)) ? true : false;
124 /* If this interface of the controller is transmitting data */
125 static inline bool tx_active(struct i2s_dai
*i2s
)
132 active
= readl(i2s
->addr
+ I2SCON
);
134 if (is_secondary(i2s
))
135 active
&= CON_TXSDMA_ACTIVE
;
137 active
&= CON_TXDMA_ACTIVE
;
139 return active
? true : false;
142 /* Return pointer to the other DAI */
143 static inline struct i2s_dai
*get_other_dai(struct i2s_dai
*i2s
)
145 return i2s
->pri_dai
? : i2s
->sec_dai
;
148 /* If the other interface of the controller is transmitting data */
149 static inline bool other_tx_active(struct i2s_dai
*i2s
)
151 struct i2s_dai
*other
= get_other_dai(i2s
);
153 return tx_active(other
);
156 /* If any interface of the controller is transmitting data */
157 static inline bool any_tx_active(struct i2s_dai
*i2s
)
159 return tx_active(i2s
) || other_tx_active(i2s
);
162 /* If this interface of the controller is receiving data */
163 static inline bool rx_active(struct i2s_dai
*i2s
)
170 active
= readl(i2s
->addr
+ I2SCON
) & CON_RXDMA_ACTIVE
;
172 return active
? true : false;
175 /* If the other interface of the controller is receiving data */
176 static inline bool other_rx_active(struct i2s_dai
*i2s
)
178 struct i2s_dai
*other
= get_other_dai(i2s
);
180 return rx_active(other
);
183 /* If any interface of the controller is receiving data */
184 static inline bool any_rx_active(struct i2s_dai
*i2s
)
186 return rx_active(i2s
) || other_rx_active(i2s
);
189 /* If the other DAI is transmitting or receiving data */
190 static inline bool other_active(struct i2s_dai
*i2s
)
192 return other_rx_active(i2s
) || other_tx_active(i2s
);
195 /* If this DAI is transmitting or receiving data */
196 static inline bool this_active(struct i2s_dai
*i2s
)
198 return tx_active(i2s
) || rx_active(i2s
);
201 /* If the controller is active anyway */
202 static inline bool any_active(struct i2s_dai
*i2s
)
204 return this_active(i2s
) || other_active(i2s
);
207 static inline struct i2s_dai
*to_info(struct snd_soc_dai
*dai
)
209 return snd_soc_dai_get_drvdata(dai
);
212 static inline bool is_opened(struct i2s_dai
*i2s
)
214 if (i2s
&& (i2s
->mode
& DAI_OPENED
))
220 static inline bool is_manager(struct i2s_dai
*i2s
)
222 if (is_opened(i2s
) && (i2s
->mode
& DAI_MANAGER
))
228 /* Read RCLK of I2S (in multiples of LRCLK) */
229 static inline unsigned get_rfs(struct i2s_dai
*i2s
)
232 rfs
= readl(i2s
->addr
+ I2SMOD
) >> i2s
->variant_regs
->rfs_off
;
233 rfs
&= i2s
->variant_regs
->rfs_mask
;
247 /* Write RCLK of I2S (in multiples of LRCLK) */
248 static inline void set_rfs(struct i2s_dai
*i2s
, unsigned rfs
)
250 u32 mod
= readl(i2s
->addr
+ I2SMOD
);
251 int rfs_shift
= i2s
->variant_regs
->rfs_off
;
253 mod
&= ~(i2s
->variant_regs
->rfs_mask
<< rfs_shift
);
257 mod
|= (EXYNOS7_MOD_RCLK_192FS
<< rfs_shift
);
260 mod
|= (EXYNOS7_MOD_RCLK_96FS
<< rfs_shift
);
263 mod
|= (EXYNOS7_MOD_RCLK_128FS
<< rfs_shift
);
266 mod
|= (EXYNOS7_MOD_RCLK_64FS
<< rfs_shift
);
269 mod
|= (MOD_RCLK_768FS
<< rfs_shift
);
272 mod
|= (MOD_RCLK_512FS
<< rfs_shift
);
275 mod
|= (MOD_RCLK_384FS
<< rfs_shift
);
278 mod
|= (MOD_RCLK_256FS
<< rfs_shift
);
282 writel(mod
, i2s
->addr
+ I2SMOD
);
285 /* Read Bit-Clock of I2S (in multiples of LRCLK) */
286 static inline unsigned get_bfs(struct i2s_dai
*i2s
)
289 bfs
= readl(i2s
->addr
+ I2SMOD
) >> i2s
->variant_regs
->bfs_off
;
290 bfs
&= i2s
->variant_regs
->bfs_mask
;
305 /* Write Bit-Clock of I2S (in multiples of LRCLK) */
306 static inline void set_bfs(struct i2s_dai
*i2s
, unsigned bfs
)
308 u32 mod
= readl(i2s
->addr
+ I2SMOD
);
309 int tdm
= i2s
->quirks
& QUIRK_SUPPORTS_TDM
;
310 int bfs_shift
= i2s
->variant_regs
->bfs_off
;
312 /* Non-TDM I2S controllers do not support BCLK > 48 * FS */
313 if (!tdm
&& bfs
> 48) {
314 dev_err(&i2s
->pdev
->dev
, "Unsupported BCLK divider\n");
318 mod
&= ~(i2s
->variant_regs
->bfs_mask
<< bfs_shift
);
322 mod
|= (MOD_BCLK_48FS
<< bfs_shift
);
325 mod
|= (MOD_BCLK_32FS
<< bfs_shift
);
328 mod
|= (MOD_BCLK_24FS
<< bfs_shift
);
331 mod
|= (MOD_BCLK_16FS
<< bfs_shift
);
334 mod
|= (EXYNOS5420_MOD_BCLK_64FS
<< bfs_shift
);
337 mod
|= (EXYNOS5420_MOD_BCLK_96FS
<< bfs_shift
);
340 mod
|= (EXYNOS5420_MOD_BCLK_128FS
<< bfs_shift
);
343 mod
|= (EXYNOS5420_MOD_BCLK_192FS
<< bfs_shift
);
346 mod
|= (EXYNOS5420_MOD_BCLK_256FS
<< bfs_shift
);
349 dev_err(&i2s
->pdev
->dev
, "Wrong BCLK Divider!\n");
353 writel(mod
, i2s
->addr
+ I2SMOD
);
357 static inline int get_blc(struct i2s_dai
*i2s
)
359 int blc
= readl(i2s
->addr
+ I2SMOD
);
361 blc
= (blc
>> 13) & 0x3;
370 /* TX Channel Control */
371 static void i2s_txctrl(struct i2s_dai
*i2s
, int on
)
373 void __iomem
*addr
= i2s
->addr
;
374 int txr_off
= i2s
->variant_regs
->txr_off
;
375 u32 con
= readl(addr
+ I2SCON
);
376 u32 mod
= readl(addr
+ I2SMOD
) & ~(3 << txr_off
);
380 con
&= ~CON_TXCH_PAUSE
;
382 if (is_secondary(i2s
)) {
383 con
|= CON_TXSDMA_ACTIVE
;
384 con
&= ~CON_TXSDMA_PAUSE
;
386 con
|= CON_TXDMA_ACTIVE
;
387 con
&= ~CON_TXDMA_PAUSE
;
390 if (any_rx_active(i2s
))
395 if (is_secondary(i2s
)) {
396 con
|= CON_TXSDMA_PAUSE
;
397 con
&= ~CON_TXSDMA_ACTIVE
;
399 con
|= CON_TXDMA_PAUSE
;
400 con
&= ~CON_TXDMA_ACTIVE
;
403 if (other_tx_active(i2s
)) {
404 writel(con
, addr
+ I2SCON
);
408 con
|= CON_TXCH_PAUSE
;
410 if (any_rx_active(i2s
))
416 writel(mod
, addr
+ I2SMOD
);
417 writel(con
, addr
+ I2SCON
);
420 /* RX Channel Control */
421 static void i2s_rxctrl(struct i2s_dai
*i2s
, int on
)
423 void __iomem
*addr
= i2s
->addr
;
424 int txr_off
= i2s
->variant_regs
->txr_off
;
425 u32 con
= readl(addr
+ I2SCON
);
426 u32 mod
= readl(addr
+ I2SMOD
) & ~(3 << txr_off
);
429 con
|= CON_RXDMA_ACTIVE
| CON_ACTIVE
;
430 con
&= ~(CON_RXDMA_PAUSE
| CON_RXCH_PAUSE
);
432 if (any_tx_active(i2s
))
437 con
|= CON_RXDMA_PAUSE
| CON_RXCH_PAUSE
;
438 con
&= ~CON_RXDMA_ACTIVE
;
440 if (any_tx_active(i2s
))
446 writel(mod
, addr
+ I2SMOD
);
447 writel(con
, addr
+ I2SCON
);
450 /* Flush FIFO of an interface */
451 static inline void i2s_fifo(struct i2s_dai
*i2s
, u32 flush
)
459 if (is_secondary(i2s
))
460 fic
= i2s
->addr
+ I2SFICS
;
462 fic
= i2s
->addr
+ I2SFIC
;
465 writel(readl(fic
) | flush
, fic
);
468 val
= msecs_to_loops(1) / 1000; /* 1 usec */
472 writel(readl(fic
) & ~flush
, fic
);
475 static int i2s_set_sysclk(struct snd_soc_dai
*dai
,
476 int clk_id
, unsigned int rfs
, int dir
)
478 struct i2s_dai
*i2s
= to_info(dai
);
479 struct i2s_dai
*other
= get_other_dai(i2s
);
480 const struct samsung_i2s_variant_regs
*i2s_regs
= i2s
->variant_regs
;
481 unsigned int cdcon_mask
= 1 << i2s_regs
->cdclkcon_off
;
482 unsigned int rsrc_mask
= 1 << i2s_regs
->rclksrc_off
;
483 u32 mod
, mask
, val
= 0;
486 spin_lock_irqsave(i2s
->lock
, flags
);
487 mod
= readl(i2s
->addr
+ I2SMOD
);
488 spin_unlock_irqrestore(i2s
->lock
, flags
);
491 case SAMSUNG_I2S_OPCLK
:
492 mask
= MOD_OPCLK_MASK
;
495 case SAMSUNG_I2S_CDCLK
:
496 mask
= 1 << i2s_regs
->cdclkcon_off
;
497 /* Shouldn't matter in GATING(CLOCK_IN) mode */
498 if (dir
== SND_SOC_CLOCK_IN
)
501 if ((rfs
&& other
&& other
->rfs
&& (other
->rfs
!= rfs
)) ||
503 (((dir
== SND_SOC_CLOCK_IN
)
504 && !(mod
& cdcon_mask
)) ||
505 ((dir
== SND_SOC_CLOCK_OUT
)
506 && (mod
& cdcon_mask
))))) {
507 dev_err(&i2s
->pdev
->dev
,
508 "%s:%d Other DAI busy\n", __func__
, __LINE__
);
512 if (dir
== SND_SOC_CLOCK_IN
)
513 val
= 1 << i2s_regs
->cdclkcon_off
;
518 case SAMSUNG_I2S_RCLKSRC_0
: /* clock corrsponding to IISMOD[10] := 0 */
519 case SAMSUNG_I2S_RCLKSRC_1
: /* clock corrsponding to IISMOD[10] := 1 */
520 mask
= 1 << i2s_regs
->rclksrc_off
;
522 if ((i2s
->quirks
& QUIRK_NO_MUXPSR
)
523 || (clk_id
== SAMSUNG_I2S_RCLKSRC_0
))
528 if (!any_active(i2s
)) {
529 if (i2s
->op_clk
&& !IS_ERR(i2s
->op_clk
)) {
530 if ((clk_id
&& !(mod
& rsrc_mask
)) ||
531 (!clk_id
&& (mod
& rsrc_mask
))) {
532 clk_disable_unprepare(i2s
->op_clk
);
533 clk_put(i2s
->op_clk
);
536 clk_get_rate(i2s
->op_clk
);
542 i2s
->op_clk
= clk_get(&i2s
->pdev
->dev
,
545 i2s
->op_clk
= clk_get(&i2s
->pdev
->dev
,
548 if (WARN_ON(IS_ERR(i2s
->op_clk
)))
549 return PTR_ERR(i2s
->op_clk
);
551 clk_prepare_enable(i2s
->op_clk
);
552 i2s
->rclk_srcrate
= clk_get_rate(i2s
->op_clk
);
554 /* Over-ride the other's */
556 other
->op_clk
= i2s
->op_clk
;
557 other
->rclk_srcrate
= i2s
->rclk_srcrate
;
559 } else if ((!clk_id
&& (mod
& rsrc_mask
))
560 || (clk_id
&& !(mod
& rsrc_mask
))) {
561 dev_err(&i2s
->pdev
->dev
,
562 "%s:%d Other DAI busy\n", __func__
, __LINE__
);
565 /* Call can't be on the active DAI */
566 i2s
->op_clk
= other
->op_clk
;
567 i2s
->rclk_srcrate
= other
->rclk_srcrate
;
572 val
= 1 << i2s_regs
->rclksrc_off
;
575 dev_err(&i2s
->pdev
->dev
, "We don't serve that!\n");
579 spin_lock_irqsave(i2s
->lock
, flags
);
580 mod
= readl(i2s
->addr
+ I2SMOD
);
581 mod
= (mod
& ~mask
) | val
;
582 writel(mod
, i2s
->addr
+ I2SMOD
);
583 spin_unlock_irqrestore(i2s
->lock
, flags
);
588 static int i2s_set_fmt(struct snd_soc_dai
*dai
,
591 struct i2s_dai
*i2s
= to_info(dai
);
592 int lrp_shift
, sdf_shift
, sdf_mask
, lrp_rlow
, mod_slave
;
596 lrp_shift
= i2s
->variant_regs
->lrp_off
;
597 sdf_shift
= i2s
->variant_regs
->sdf_off
;
598 mod_slave
= 1 << i2s
->variant_regs
->mss_off
;
600 sdf_mask
= MOD_SDF_MASK
<< sdf_shift
;
601 lrp_rlow
= MOD_LR_RLOW
<< lrp_shift
;
603 /* Format is priority */
604 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
605 case SND_SOC_DAIFMT_RIGHT_J
:
607 tmp
|= (MOD_SDF_MSB
<< sdf_shift
);
609 case SND_SOC_DAIFMT_LEFT_J
:
611 tmp
|= (MOD_SDF_LSB
<< sdf_shift
);
613 case SND_SOC_DAIFMT_I2S
:
614 tmp
|= (MOD_SDF_IIS
<< sdf_shift
);
617 dev_err(&i2s
->pdev
->dev
, "Format not supported\n");
622 * INV flag is relative to the FORMAT flag - if set it simply
623 * flips the polarity specified by the Standard
625 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
626 case SND_SOC_DAIFMT_NB_NF
:
628 case SND_SOC_DAIFMT_NB_IF
:
635 dev_err(&i2s
->pdev
->dev
, "Polarity not supported\n");
639 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
640 case SND_SOC_DAIFMT_CBM_CFM
:
643 case SND_SOC_DAIFMT_CBS_CFS
:
644 /* Set default source clock in Master mode */
645 if (i2s
->rclk_srcrate
== 0)
646 i2s_set_sysclk(dai
, SAMSUNG_I2S_RCLKSRC_0
,
647 0, SND_SOC_CLOCK_IN
);
650 dev_err(&i2s
->pdev
->dev
, "master/slave format not supported\n");
654 spin_lock_irqsave(i2s
->lock
, flags
);
655 mod
= readl(i2s
->addr
+ I2SMOD
);
657 * Don't change the I2S mode if any controller is active on this
660 if (any_active(i2s
) &&
661 ((mod
& (sdf_mask
| lrp_rlow
| mod_slave
)) != tmp
)) {
662 spin_unlock_irqrestore(i2s
->lock
, flags
);
663 dev_err(&i2s
->pdev
->dev
,
664 "%s:%d Other DAI busy\n", __func__
, __LINE__
);
668 mod
&= ~(sdf_mask
| lrp_rlow
| mod_slave
);
670 writel(mod
, i2s
->addr
+ I2SMOD
);
671 spin_unlock_irqrestore(i2s
->lock
, flags
);
676 static int i2s_hw_params(struct snd_pcm_substream
*substream
,
677 struct snd_pcm_hw_params
*params
, struct snd_soc_dai
*dai
)
679 struct i2s_dai
*i2s
= to_info(dai
);
680 u32 mod
, mask
= 0, val
= 0;
683 if (!is_secondary(i2s
))
684 mask
|= (MOD_DC2_EN
| MOD_DC1_EN
);
686 switch (params_channels(params
)) {
693 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
694 i2s
->dma_playback
.dma_size
= 4;
696 i2s
->dma_capture
.dma_size
= 4;
699 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
700 i2s
->dma_playback
.dma_size
= 2;
702 i2s
->dma_capture
.dma_size
= 2;
706 dev_err(&i2s
->pdev
->dev
, "%d channels not supported\n",
707 params_channels(params
));
711 if (is_secondary(i2s
))
712 mask
|= MOD_BLCS_MASK
;
714 mask
|= MOD_BLCP_MASK
;
717 mask
|= MOD_BLC_MASK
;
719 switch (params_width(params
)) {
721 if (is_secondary(i2s
))
722 val
|= MOD_BLCS_8BIT
;
724 val
|= MOD_BLCP_8BIT
;
729 if (is_secondary(i2s
))
730 val
|= MOD_BLCS_16BIT
;
732 val
|= MOD_BLCP_16BIT
;
734 val
|= MOD_BLC_16BIT
;
737 if (is_secondary(i2s
))
738 val
|= MOD_BLCS_24BIT
;
740 val
|= MOD_BLCP_24BIT
;
742 val
|= MOD_BLC_24BIT
;
745 dev_err(&i2s
->pdev
->dev
, "Format(%d) not supported\n",
746 params_format(params
));
750 spin_lock_irqsave(i2s
->lock
, flags
);
751 mod
= readl(i2s
->addr
+ I2SMOD
);
752 mod
= (mod
& ~mask
) | val
;
753 writel(mod
, i2s
->addr
+ I2SMOD
);
754 spin_unlock_irqrestore(i2s
->lock
, flags
);
756 samsung_asoc_init_dma_data(dai
, &i2s
->dma_playback
, &i2s
->dma_capture
);
758 i2s
->frmclk
= params_rate(params
);
763 /* We set constraints on the substream acc to the version of I2S */
764 static int i2s_startup(struct snd_pcm_substream
*substream
,
765 struct snd_soc_dai
*dai
)
767 struct i2s_dai
*i2s
= to_info(dai
);
768 struct i2s_dai
*other
= get_other_dai(i2s
);
771 spin_lock_irqsave(&lock
, flags
);
773 i2s
->mode
|= DAI_OPENED
;
775 if (is_manager(other
))
776 i2s
->mode
&= ~DAI_MANAGER
;
778 i2s
->mode
|= DAI_MANAGER
;
780 if (!any_active(i2s
) && (i2s
->quirks
& QUIRK_NEED_RSTCLR
))
781 writel(CON_RSTCLR
, i2s
->addr
+ I2SCON
);
783 spin_unlock_irqrestore(&lock
, flags
);
788 static void i2s_shutdown(struct snd_pcm_substream
*substream
,
789 struct snd_soc_dai
*dai
)
791 struct i2s_dai
*i2s
= to_info(dai
);
792 struct i2s_dai
*other
= get_other_dai(i2s
);
795 spin_lock_irqsave(&lock
, flags
);
797 i2s
->mode
&= ~DAI_OPENED
;
798 i2s
->mode
&= ~DAI_MANAGER
;
800 if (is_opened(other
))
801 other
->mode
|= DAI_MANAGER
;
803 /* Reset any constraint on RFS and BFS */
807 spin_unlock_irqrestore(&lock
, flags
);
810 static int config_setup(struct i2s_dai
*i2s
)
812 struct i2s_dai
*other
= get_other_dai(i2s
);
813 unsigned rfs
, bfs
, blc
;
823 /* Select least possible multiple(2) if no constraint set */
832 if ((rfs
== 256 || rfs
== 512) && (blc
== 24)) {
833 dev_err(&i2s
->pdev
->dev
,
834 "%d-RFS not supported for 24-blc\n", rfs
);
839 if (bfs
== 16 || bfs
== 32)
845 /* If already setup and running */
846 if (any_active(i2s
) && (get_rfs(i2s
) != rfs
|| get_bfs(i2s
) != bfs
)) {
847 dev_err(&i2s
->pdev
->dev
,
848 "%s:%d Other DAI busy\n", __func__
, __LINE__
);
855 /* Don't bother with PSR in Slave mode */
859 if (!(i2s
->quirks
& QUIRK_NO_MUXPSR
)) {
860 psr
= i2s
->rclk_srcrate
/ i2s
->frmclk
/ rfs
;
861 writel(((psr
- 1) << 8) | PSR_PSREN
, i2s
->addr
+ I2SPSR
);
862 dev_dbg(&i2s
->pdev
->dev
,
863 "RCLK_SRC=%luHz PSR=%u, RCLK=%dfs, BCLK=%dfs\n",
864 i2s
->rclk_srcrate
, psr
, rfs
, bfs
);
870 static int i2s_trigger(struct snd_pcm_substream
*substream
,
871 int cmd
, struct snd_soc_dai
*dai
)
873 int capture
= (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
);
874 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
875 struct i2s_dai
*i2s
= to_info(rtd
->cpu_dai
);
879 case SNDRV_PCM_TRIGGER_START
:
880 case SNDRV_PCM_TRIGGER_RESUME
:
881 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
882 spin_lock_irqsave(i2s
->lock
, flags
);
884 if (config_setup(i2s
)) {
885 spin_unlock_irqrestore(i2s
->lock
, flags
);
894 spin_unlock_irqrestore(i2s
->lock
, flags
);
896 case SNDRV_PCM_TRIGGER_STOP
:
897 case SNDRV_PCM_TRIGGER_SUSPEND
:
898 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
899 spin_lock_irqsave(i2s
->lock
, flags
);
903 i2s_fifo(i2s
, FIC_RXFLUSH
);
906 i2s_fifo(i2s
, FIC_TXFLUSH
);
909 spin_unlock_irqrestore(i2s
->lock
, flags
);
916 static int i2s_set_clkdiv(struct snd_soc_dai
*dai
,
919 struct i2s_dai
*i2s
= to_info(dai
);
920 struct i2s_dai
*other
= get_other_dai(i2s
);
923 case SAMSUNG_I2S_DIV_BCLK
:
924 if ((any_active(i2s
) && div
&& (get_bfs(i2s
) != div
))
925 || (other
&& other
->bfs
&& (other
->bfs
!= div
))) {
926 dev_err(&i2s
->pdev
->dev
,
927 "%s:%d Other DAI busy\n", __func__
, __LINE__
);
933 dev_err(&i2s
->pdev
->dev
,
934 "Invalid clock divider(%d)\n", div_id
);
941 static snd_pcm_sframes_t
942 i2s_delay(struct snd_pcm_substream
*substream
, struct snd_soc_dai
*dai
)
944 struct i2s_dai
*i2s
= to_info(dai
);
945 u32 reg
= readl(i2s
->addr
+ I2SFIC
);
946 snd_pcm_sframes_t delay
;
947 const struct samsung_i2s_variant_regs
*i2s_regs
= i2s
->variant_regs
;
949 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
950 delay
= FIC_RXCOUNT(reg
);
951 else if (is_secondary(i2s
))
952 delay
= FICS_TXCOUNT(readl(i2s
->addr
+ I2SFICS
));
954 delay
= (reg
>> i2s_regs
->ftx0cnt_off
) & 0x7f;
960 static int i2s_suspend(struct snd_soc_dai
*dai
)
962 struct i2s_dai
*i2s
= to_info(dai
);
964 i2s
->suspend_i2smod
= readl(i2s
->addr
+ I2SMOD
);
965 i2s
->suspend_i2scon
= readl(i2s
->addr
+ I2SCON
);
966 i2s
->suspend_i2spsr
= readl(i2s
->addr
+ I2SPSR
);
971 static int i2s_resume(struct snd_soc_dai
*dai
)
973 struct i2s_dai
*i2s
= to_info(dai
);
975 writel(i2s
->suspend_i2scon
, i2s
->addr
+ I2SCON
);
976 writel(i2s
->suspend_i2smod
, i2s
->addr
+ I2SMOD
);
977 writel(i2s
->suspend_i2spsr
, i2s
->addr
+ I2SPSR
);
982 #define i2s_suspend NULL
983 #define i2s_resume NULL
986 static int samsung_i2s_dai_probe(struct snd_soc_dai
*dai
)
988 struct i2s_dai
*i2s
= to_info(dai
);
989 struct i2s_dai
*other
= get_other_dai(i2s
);
992 if (is_secondary(i2s
)) { /* If this is probe on the secondary DAI */
993 samsung_asoc_init_dma_data(dai
, &other
->sec_dai
->dma_playback
,
996 samsung_asoc_init_dma_data(dai
, &i2s
->dma_playback
,
999 if (i2s
->quirks
& QUIRK_NEED_RSTCLR
)
1000 writel(CON_RSTCLR
, i2s
->addr
+ I2SCON
);
1002 if (i2s
->quirks
& QUIRK_SUPPORTS_IDMA
)
1003 idma_reg_addr_init(i2s
->addr
,
1004 i2s
->sec_dai
->idma_playback
.dma_addr
);
1007 /* Reset any constraint on RFS and BFS */
1010 i2s
->rclk_srcrate
= 0;
1012 spin_lock_irqsave(i2s
->lock
, flags
);
1015 i2s_fifo(i2s
, FIC_TXFLUSH
);
1016 i2s_fifo(other
, FIC_TXFLUSH
);
1017 i2s_fifo(i2s
, FIC_RXFLUSH
);
1018 spin_unlock_irqrestore(i2s
->lock
, flags
);
1020 /* Gate CDCLK by default */
1021 if (!is_opened(other
))
1022 i2s_set_sysclk(dai
, SAMSUNG_I2S_CDCLK
,
1023 0, SND_SOC_CLOCK_IN
);
1028 static int samsung_i2s_dai_remove(struct snd_soc_dai
*dai
)
1030 struct i2s_dai
*i2s
= snd_soc_dai_get_drvdata(dai
);
1032 if (!is_secondary(i2s
)) {
1033 if (i2s
->quirks
& QUIRK_NEED_RSTCLR
) {
1034 spin_lock(i2s
->lock
);
1035 writel(0, i2s
->addr
+ I2SCON
);
1036 spin_unlock(i2s
->lock
);
1043 static const struct snd_soc_dai_ops samsung_i2s_dai_ops
= {
1044 .trigger
= i2s_trigger
,
1045 .hw_params
= i2s_hw_params
,
1046 .set_fmt
= i2s_set_fmt
,
1047 .set_clkdiv
= i2s_set_clkdiv
,
1048 .set_sysclk
= i2s_set_sysclk
,
1049 .startup
= i2s_startup
,
1050 .shutdown
= i2s_shutdown
,
1054 static const struct snd_soc_component_driver samsung_i2s_component
= {
1055 .name
= "samsung-i2s",
1058 #define SAMSUNG_I2S_RATES SNDRV_PCM_RATE_8000_96000
1060 #define SAMSUNG_I2S_FMTS (SNDRV_PCM_FMTBIT_S8 | \
1061 SNDRV_PCM_FMTBIT_S16_LE | \
1062 SNDRV_PCM_FMTBIT_S24_LE)
1064 static struct i2s_dai
*i2s_alloc_dai(struct platform_device
*pdev
, bool sec
)
1066 struct i2s_dai
*i2s
;
1069 i2s
= devm_kzalloc(&pdev
->dev
, sizeof(struct i2s_dai
), GFP_KERNEL
);
1074 i2s
->pri_dai
= NULL
;
1075 i2s
->sec_dai
= NULL
;
1076 i2s
->i2s_dai_drv
.symmetric_rates
= 1;
1077 i2s
->i2s_dai_drv
.probe
= samsung_i2s_dai_probe
;
1078 i2s
->i2s_dai_drv
.remove
= samsung_i2s_dai_remove
;
1079 i2s
->i2s_dai_drv
.ops
= &samsung_i2s_dai_ops
;
1080 i2s
->i2s_dai_drv
.suspend
= i2s_suspend
;
1081 i2s
->i2s_dai_drv
.resume
= i2s_resume
;
1082 i2s
->i2s_dai_drv
.playback
.channels_min
= 1;
1083 i2s
->i2s_dai_drv
.playback
.channels_max
= 2;
1084 i2s
->i2s_dai_drv
.playback
.rates
= SAMSUNG_I2S_RATES
;
1085 i2s
->i2s_dai_drv
.playback
.formats
= SAMSUNG_I2S_FMTS
;
1088 i2s
->i2s_dai_drv
.capture
.channels_min
= 1;
1089 i2s
->i2s_dai_drv
.capture
.channels_max
= 2;
1090 i2s
->i2s_dai_drv
.capture
.rates
= SAMSUNG_I2S_RATES
;
1091 i2s
->i2s_dai_drv
.capture
.formats
= SAMSUNG_I2S_FMTS
;
1092 dev_set_drvdata(&i2s
->pdev
->dev
, i2s
);
1093 } else { /* Create a new platform_device for Secondary */
1094 i2s
->pdev
= platform_device_alloc("samsung-i2s-sec", -1);
1098 i2s
->pdev
->dev
.parent
= &pdev
->dev
;
1100 platform_set_drvdata(i2s
->pdev
, i2s
);
1101 ret
= platform_device_add(i2s
->pdev
);
1109 static const struct of_device_id exynos_i2s_match
[];
1111 static inline const struct samsung_i2s_dai_data
*samsung_i2s_get_driver_data(
1112 struct platform_device
*pdev
)
1114 if (IS_ENABLED(CONFIG_OF
) && pdev
->dev
.of_node
) {
1115 const struct of_device_id
*match
;
1116 match
= of_match_node(exynos_i2s_match
, pdev
->dev
.of_node
);
1117 return match
? match
->data
: NULL
;
1119 return (struct samsung_i2s_dai_data
*)
1120 platform_get_device_id(pdev
)->driver_data
;
1125 static int i2s_runtime_suspend(struct device
*dev
)
1127 struct i2s_dai
*i2s
= dev_get_drvdata(dev
);
1129 clk_disable_unprepare(i2s
->clk
);
1134 static int i2s_runtime_resume(struct device
*dev
)
1136 struct i2s_dai
*i2s
= dev_get_drvdata(dev
);
1138 clk_prepare_enable(i2s
->clk
);
1142 #endif /* CONFIG_PM */
1144 static void i2s_unregister_clocks(struct i2s_dai
*i2s
)
1148 for (i
= 0; i
< i2s
->clk_data
.clk_num
; i
++) {
1149 if (!IS_ERR(i2s
->clk_table
[i
]))
1150 clk_unregister(i2s
->clk_table
[i
]);
1154 static void i2s_unregister_clock_provider(struct platform_device
*pdev
)
1156 struct i2s_dai
*i2s
= dev_get_drvdata(&pdev
->dev
);
1158 of_clk_del_provider(pdev
->dev
.of_node
);
1159 i2s_unregister_clocks(i2s
);
1162 static int i2s_register_clock_provider(struct platform_device
*pdev
)
1164 struct device
*dev
= &pdev
->dev
;
1165 struct i2s_dai
*i2s
= dev_get_drvdata(dev
);
1166 const char *clk_name
[2] = { "i2s_opclk0", "i2s_opclk1" };
1167 const char *p_names
[2] = { NULL
};
1168 const struct samsung_i2s_variant_regs
*reg_info
= i2s
->variant_regs
;
1169 struct clk
*rclksrc
;
1172 /* Register the clock provider only if it's expected in the DTB */
1173 if (!of_find_property(dev
->of_node
, "#clock-cells", NULL
))
1176 /* Get the RCLKSRC mux clock parent clock names */
1177 for (i
= 0; i
< ARRAY_SIZE(p_names
); i
++) {
1178 rclksrc
= clk_get(dev
, clk_name
[i
]);
1179 if (IS_ERR(rclksrc
))
1181 p_names
[i
] = __clk_get_name(rclksrc
);
1185 if (!(i2s
->quirks
& QUIRK_NO_MUXPSR
)) {
1186 /* Activate the prescaler */
1187 u32 val
= readl(i2s
->addr
+ I2SPSR
);
1188 writel(val
| PSR_PSREN
, i2s
->addr
+ I2SPSR
);
1190 i2s
->clk_table
[CLK_I2S_RCLK_SRC
] = clk_register_mux(NULL
,
1191 "i2s_rclksrc", p_names
, ARRAY_SIZE(p_names
),
1192 CLK_SET_RATE_NO_REPARENT
| CLK_SET_RATE_PARENT
,
1193 i2s
->addr
+ I2SMOD
, reg_info
->rclksrc_off
,
1196 i2s
->clk_table
[CLK_I2S_RCLK_PSR
] = clk_register_divider(NULL
,
1197 "i2s_presc", "i2s_rclksrc",
1198 CLK_SET_RATE_PARENT
,
1199 i2s
->addr
+ I2SPSR
, 8, 6, 0, i2s
->lock
);
1201 p_names
[0] = "i2s_presc";
1202 i2s
->clk_data
.clk_num
= 2;
1204 of_property_read_string_index(dev
->of_node
,
1205 "clock-output-names", 0, &clk_name
[0]);
1207 i2s
->clk_table
[CLK_I2S_CDCLK
] = clk_register_gate(NULL
, clk_name
[0],
1208 p_names
[0], CLK_SET_RATE_PARENT
,
1209 i2s
->addr
+ I2SMOD
, reg_info
->cdclkcon_off
,
1210 CLK_GATE_SET_TO_DISABLE
, i2s
->lock
);
1212 i2s
->clk_data
.clk_num
+= 1;
1213 i2s
->clk_data
.clks
= i2s
->clk_table
;
1215 ret
= of_clk_add_provider(dev
->of_node
, of_clk_src_onecell_get
,
1218 dev_err(dev
, "failed to add clock provider: %d\n", ret
);
1219 i2s_unregister_clocks(i2s
);
1225 static int samsung_i2s_probe(struct platform_device
*pdev
)
1227 struct i2s_dai
*pri_dai
, *sec_dai
= NULL
;
1228 struct s3c_audio_pdata
*i2s_pdata
= pdev
->dev
.platform_data
;
1229 struct samsung_i2s
*i2s_cfg
= NULL
;
1230 struct resource
*res
;
1231 u32 regs_base
, quirks
= 0, idma_addr
= 0;
1232 struct device_node
*np
= pdev
->dev
.of_node
;
1233 const struct samsung_i2s_dai_data
*i2s_dai_data
;
1236 /* Call during Seconday interface registration */
1237 i2s_dai_data
= samsung_i2s_get_driver_data(pdev
);
1239 if (i2s_dai_data
->dai_type
== TYPE_SEC
) {
1240 sec_dai
= dev_get_drvdata(&pdev
->dev
);
1242 dev_err(&pdev
->dev
, "Unable to get drvdata\n");
1245 ret
= devm_snd_soc_register_component(&sec_dai
->pdev
->dev
,
1246 &samsung_i2s_component
,
1247 &sec_dai
->i2s_dai_drv
, 1);
1251 return samsung_asoc_dma_platform_register(&pdev
->dev
,
1255 pri_dai
= i2s_alloc_dai(pdev
, false);
1257 dev_err(&pdev
->dev
, "Unable to alloc I2S_pri\n");
1261 spin_lock_init(&pri_dai
->spinlock
);
1262 pri_dai
->lock
= &pri_dai
->spinlock
;
1265 if (i2s_pdata
== NULL
) {
1266 dev_err(&pdev
->dev
, "Can't work without s3c_audio_pdata\n");
1270 pri_dai
->dma_playback
.slave
= i2s_pdata
->dma_playback
;
1271 pri_dai
->dma_capture
.slave
= i2s_pdata
->dma_capture
;
1272 pri_dai
->filter
= i2s_pdata
->dma_filter
;
1274 if (&i2s_pdata
->type
)
1275 i2s_cfg
= &i2s_pdata
->type
.i2s
;
1278 quirks
= i2s_cfg
->quirks
;
1279 idma_addr
= i2s_cfg
->idma_addr
;
1282 quirks
= i2s_dai_data
->quirks
;
1283 if (of_property_read_u32(np
, "samsung,idma-addr",
1285 if (quirks
& QUIRK_SUPPORTS_IDMA
) {
1286 dev_info(&pdev
->dev
, "idma address is not"\
1292 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1293 pri_dai
->addr
= devm_ioremap_resource(&pdev
->dev
, res
);
1294 if (IS_ERR(pri_dai
->addr
))
1295 return PTR_ERR(pri_dai
->addr
);
1297 regs_base
= res
->start
;
1299 pri_dai
->clk
= devm_clk_get(&pdev
->dev
, "iis");
1300 if (IS_ERR(pri_dai
->clk
)) {
1301 dev_err(&pdev
->dev
, "Failed to get iis clock\n");
1302 return PTR_ERR(pri_dai
->clk
);
1305 ret
= clk_prepare_enable(pri_dai
->clk
);
1307 dev_err(&pdev
->dev
, "failed to enable clock: %d\n", ret
);
1310 pri_dai
->dma_playback
.dma_addr
= regs_base
+ I2STXD
;
1311 pri_dai
->dma_capture
.dma_addr
= regs_base
+ I2SRXD
;
1312 pri_dai
->dma_playback
.ch_name
= "tx";
1313 pri_dai
->dma_capture
.ch_name
= "rx";
1314 pri_dai
->dma_playback
.dma_size
= 4;
1315 pri_dai
->dma_capture
.dma_size
= 4;
1316 pri_dai
->quirks
= quirks
;
1317 pri_dai
->variant_regs
= i2s_dai_data
->i2s_variant_regs
;
1319 if (quirks
& QUIRK_PRI_6CHAN
)
1320 pri_dai
->i2s_dai_drv
.playback
.channels_max
= 6;
1322 if (quirks
& QUIRK_SEC_DAI
) {
1323 sec_dai
= i2s_alloc_dai(pdev
, true);
1325 dev_err(&pdev
->dev
, "Unable to alloc I2S_sec\n");
1329 sec_dai
->lock
= &pri_dai
->spinlock
;
1330 sec_dai
->variant_regs
= pri_dai
->variant_regs
;
1331 sec_dai
->dma_playback
.dma_addr
= regs_base
+ I2STXDS
;
1332 sec_dai
->dma_playback
.ch_name
= "tx-sec";
1335 sec_dai
->dma_playback
.slave
= i2s_pdata
->dma_play_sec
;
1336 sec_dai
->filter
= i2s_pdata
->dma_filter
;
1339 sec_dai
->dma_playback
.dma_size
= 4;
1340 sec_dai
->addr
= pri_dai
->addr
;
1341 sec_dai
->clk
= pri_dai
->clk
;
1342 sec_dai
->quirks
= quirks
;
1343 sec_dai
->idma_playback
.dma_addr
= idma_addr
;
1344 sec_dai
->pri_dai
= pri_dai
;
1345 pri_dai
->sec_dai
= sec_dai
;
1348 if (i2s_pdata
&& i2s_pdata
->cfg_gpio
&& i2s_pdata
->cfg_gpio(pdev
)) {
1349 dev_err(&pdev
->dev
, "Unable to configure gpio\n");
1353 devm_snd_soc_register_component(&pri_dai
->pdev
->dev
,
1354 &samsung_i2s_component
,
1355 &pri_dai
->i2s_dai_drv
, 1);
1357 pm_runtime_enable(&pdev
->dev
);
1359 ret
= samsung_asoc_dma_platform_register(&pdev
->dev
, pri_dai
->filter
);
1363 return i2s_register_clock_provider(pdev
);
1366 static int samsung_i2s_remove(struct platform_device
*pdev
)
1368 struct i2s_dai
*i2s
, *other
;
1370 i2s
= dev_get_drvdata(&pdev
->dev
);
1371 other
= get_other_dai(i2s
);
1374 other
->pri_dai
= NULL
;
1375 other
->sec_dai
= NULL
;
1377 pm_runtime_disable(&pdev
->dev
);
1380 if (!is_secondary(i2s
)) {
1381 i2s_unregister_clock_provider(pdev
);
1382 clk_disable_unprepare(i2s
->clk
);
1385 i2s
->pri_dai
= NULL
;
1386 i2s
->sec_dai
= NULL
;
1391 static const struct samsung_i2s_variant_regs i2sv3_regs
= {
1405 static const struct samsung_i2s_variant_regs i2sv6_regs
= {
1419 static const struct samsung_i2s_variant_regs i2sv7_regs
= {
1433 static const struct samsung_i2s_variant_regs i2sv5_i2s1_regs
= {
1447 static const struct samsung_i2s_dai_data i2sv3_dai_type
= {
1448 .dai_type
= TYPE_PRI
,
1449 .quirks
= QUIRK_NO_MUXPSR
,
1450 .i2s_variant_regs
= &i2sv3_regs
,
1453 static const struct samsung_i2s_dai_data i2sv5_dai_type
= {
1454 .dai_type
= TYPE_PRI
,
1455 .quirks
= QUIRK_PRI_6CHAN
| QUIRK_SEC_DAI
| QUIRK_NEED_RSTCLR
|
1456 QUIRK_SUPPORTS_IDMA
,
1457 .i2s_variant_regs
= &i2sv3_regs
,
1460 static const struct samsung_i2s_dai_data i2sv6_dai_type
= {
1461 .dai_type
= TYPE_PRI
,
1462 .quirks
= QUIRK_PRI_6CHAN
| QUIRK_SEC_DAI
| QUIRK_NEED_RSTCLR
|
1463 QUIRK_SUPPORTS_TDM
| QUIRK_SUPPORTS_IDMA
,
1464 .i2s_variant_regs
= &i2sv6_regs
,
1467 static const struct samsung_i2s_dai_data i2sv7_dai_type
= {
1468 .dai_type
= TYPE_PRI
,
1469 .quirks
= QUIRK_PRI_6CHAN
| QUIRK_SEC_DAI
| QUIRK_NEED_RSTCLR
|
1471 .i2s_variant_regs
= &i2sv7_regs
,
1474 static const struct samsung_i2s_dai_data i2sv5_dai_type_i2s1
= {
1475 .dai_type
= TYPE_PRI
,
1476 .quirks
= QUIRK_PRI_6CHAN
| QUIRK_NEED_RSTCLR
,
1477 .i2s_variant_regs
= &i2sv5_i2s1_regs
,
1480 static const struct samsung_i2s_dai_data samsung_dai_type_pri
= {
1481 .dai_type
= TYPE_PRI
,
1484 static const struct samsung_i2s_dai_data samsung_dai_type_sec
= {
1485 .dai_type
= TYPE_SEC
,
1488 static const struct platform_device_id samsung_i2s_driver_ids
[] = {
1490 .name
= "samsung-i2s",
1491 .driver_data
= (kernel_ulong_t
)&i2sv3_dai_type
,
1493 .name
= "samsung-i2s-sec",
1494 .driver_data
= (kernel_ulong_t
)&samsung_dai_type_sec
,
1496 .name
= "samsung-i2sv4",
1497 .driver_data
= (kernel_ulong_t
)&i2sv5_dai_type
,
1501 MODULE_DEVICE_TABLE(platform
, samsung_i2s_driver_ids
);
1504 static const struct of_device_id exynos_i2s_match
[] = {
1506 .compatible
= "samsung,s3c6410-i2s",
1507 .data
= &i2sv3_dai_type
,
1509 .compatible
= "samsung,s5pv210-i2s",
1510 .data
= &i2sv5_dai_type
,
1512 .compatible
= "samsung,exynos5420-i2s",
1513 .data
= &i2sv6_dai_type
,
1515 .compatible
= "samsung,exynos7-i2s",
1516 .data
= &i2sv7_dai_type
,
1518 .compatible
= "samsung,exynos7-i2s1",
1519 .data
= &i2sv5_dai_type_i2s1
,
1523 MODULE_DEVICE_TABLE(of
, exynos_i2s_match
);
1526 static const struct dev_pm_ops samsung_i2s_pm
= {
1527 SET_RUNTIME_PM_OPS(i2s_runtime_suspend
,
1528 i2s_runtime_resume
, NULL
)
1531 static struct platform_driver samsung_i2s_driver
= {
1532 .probe
= samsung_i2s_probe
,
1533 .remove
= samsung_i2s_remove
,
1534 .id_table
= samsung_i2s_driver_ids
,
1536 .name
= "samsung-i2s",
1537 .of_match_table
= of_match_ptr(exynos_i2s_match
),
1538 .pm
= &samsung_i2s_pm
,
1542 module_platform_driver(samsung_i2s_driver
);
1544 /* Module information */
1545 MODULE_AUTHOR("Jaswinder Singh, <jassisinghbrar@gmail.com>");
1546 MODULE_DESCRIPTION("Samsung I2S Interface");
1547 MODULE_ALIAS("platform:samsung-i2s");
1548 MODULE_LICENSE("GPL");