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 <jassi.brar@samsung.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>
18 #include <sound/pcm.h>
19 #include <sound/pcm_params.h>
20 #include <sound/soc.h>
22 #include <plat/audio.h>
36 #define CON_RSTCLR (1 << 31)
37 #define CON_FRXOFSTATUS (1 << 26)
38 #define CON_FRXORINTEN (1 << 25)
39 #define CON_FTXSURSTAT (1 << 24)
40 #define CON_FTXSURINTEN (1 << 23)
41 #define CON_TXSDMA_PAUSE (1 << 20)
42 #define CON_TXSDMA_ACTIVE (1 << 18)
44 #define CON_FTXURSTATUS (1 << 17)
45 #define CON_FTXURINTEN (1 << 16)
46 #define CON_TXFIFO2_EMPTY (1 << 15)
47 #define CON_TXFIFO1_EMPTY (1 << 14)
48 #define CON_TXFIFO2_FULL (1 << 13)
49 #define CON_TXFIFO1_FULL (1 << 12)
51 #define CON_LRINDEX (1 << 11)
52 #define CON_TXFIFO_EMPTY (1 << 10)
53 #define CON_RXFIFO_EMPTY (1 << 9)
54 #define CON_TXFIFO_FULL (1 << 8)
55 #define CON_RXFIFO_FULL (1 << 7)
56 #define CON_TXDMA_PAUSE (1 << 6)
57 #define CON_RXDMA_PAUSE (1 << 5)
58 #define CON_TXCH_PAUSE (1 << 4)
59 #define CON_RXCH_PAUSE (1 << 3)
60 #define CON_TXDMA_ACTIVE (1 << 2)
61 #define CON_RXDMA_ACTIVE (1 << 1)
62 #define CON_ACTIVE (1 << 0)
64 #define MOD_OPCLK_CDCLK_OUT (0 << 30)
65 #define MOD_OPCLK_CDCLK_IN (1 << 30)
66 #define MOD_OPCLK_BCLK_OUT (2 << 30)
67 #define MOD_OPCLK_PCLK (3 << 30)
68 #define MOD_OPCLK_MASK (3 << 30)
69 #define MOD_TXS_IDMA (1 << 28) /* Sec_TXFIFO use I-DMA */
71 #define MOD_BLCS_SHIFT 26
72 #define MOD_BLCS_16BIT (0 << MOD_BLCS_SHIFT)
73 #define MOD_BLCS_8BIT (1 << MOD_BLCS_SHIFT)
74 #define MOD_BLCS_24BIT (2 << MOD_BLCS_SHIFT)
75 #define MOD_BLCS_MASK (3 << MOD_BLCS_SHIFT)
76 #define MOD_BLCP_SHIFT 24
77 #define MOD_BLCP_16BIT (0 << MOD_BLCP_SHIFT)
78 #define MOD_BLCP_8BIT (1 << MOD_BLCP_SHIFT)
79 #define MOD_BLCP_24BIT (2 << MOD_BLCP_SHIFT)
80 #define MOD_BLCP_MASK (3 << MOD_BLCP_SHIFT)
82 #define MOD_C2DD_HHALF (1 << 21) /* Discard Higher-half */
83 #define MOD_C2DD_LHALF (1 << 20) /* Discard Lower-half */
84 #define MOD_C1DD_HHALF (1 << 19)
85 #define MOD_C1DD_LHALF (1 << 18)
86 #define MOD_DC2_EN (1 << 17)
87 #define MOD_DC1_EN (1 << 16)
88 #define MOD_BLC_16BIT (0 << 13)
89 #define MOD_BLC_8BIT (1 << 13)
90 #define MOD_BLC_24BIT (2 << 13)
91 #define MOD_BLC_MASK (3 << 13)
93 #define MOD_IMS_SYSMUX (1 << 10)
94 #define MOD_SLAVE (1 << 11)
95 #define MOD_TXONLY (0 << 8)
96 #define MOD_RXONLY (1 << 8)
97 #define MOD_TXRX (2 << 8)
98 #define MOD_MASK (3 << 8)
99 #define MOD_LR_LLOW (0 << 7)
100 #define MOD_LR_RLOW (1 << 7)
101 #define MOD_SDF_IIS (0 << 5)
102 #define MOD_SDF_MSB (1 << 5)
103 #define MOD_SDF_LSB (2 << 5)
104 #define MOD_SDF_MASK (3 << 5)
105 #define MOD_RCLK_256FS (0 << 3)
106 #define MOD_RCLK_512FS (1 << 3)
107 #define MOD_RCLK_384FS (2 << 3)
108 #define MOD_RCLK_768FS (3 << 3)
109 #define MOD_RCLK_MASK (3 << 3)
110 #define MOD_BCLK_32FS (0 << 1)
111 #define MOD_BCLK_48FS (1 << 1)
112 #define MOD_BCLK_16FS (2 << 1)
113 #define MOD_BCLK_24FS (3 << 1)
114 #define MOD_BCLK_MASK (3 << 1)
115 #define MOD_8BIT (1 << 0)
117 #define MOD_CDCLKCON (1 << 12)
119 #define PSR_PSREN (1 << 15)
121 #define FIC_TX2COUNT(x) (((x) >> 24) & 0xf)
122 #define FIC_TX1COUNT(x) (((x) >> 16) & 0xf)
124 #define FIC_TXFLUSH (1 << 15)
125 #define FIC_RXFLUSH (1 << 7)
126 #define FIC_TXCOUNT(x) (((x) >> 8) & 0xf)
127 #define FIC_RXCOUNT(x) (((x) >> 0) & 0xf)
128 #define FICS_TXCOUNT(x) (((x) >> 8) & 0x7f)
130 #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
133 /* Platform device for this DAI */
134 struct platform_device
*pdev
;
137 /* Physical base address of SFRs */
139 /* Rate of RCLK source clock */
140 unsigned long rclk_srcrate
;
144 * Specifically requested RCLK,BCLK by MACHINE Driver.
145 * 0 indicates CPU driver is free to choose any value.
148 /* I2S Controller's core clock */
150 /* Clock for generating I2S signals */
152 /* Array of clock names for op_clk */
153 const char **src_clk
;
154 /* Pointer to the Primary_Fifo if this is Sec_Fifo, NULL otherwise */
155 struct i2s_dai
*pri_dai
;
156 /* Pointer to the Secondary_Fifo if it has one, NULL otherwise */
157 struct i2s_dai
*sec_dai
;
158 #define DAI_OPENED (1 << 0) /* Dai is opened */
159 #define DAI_MANAGER (1 << 1) /* Dai is the manager */
161 /* Driver for this DAI */
162 struct snd_soc_dai_driver i2s_dai_drv
;
164 struct s3c_dma_params dma_playback
;
165 struct s3c_dma_params dma_capture
;
172 /* Lock for cross i/f checks */
173 static DEFINE_SPINLOCK(lock
);
175 /* If this is the 'overlay' stereo DAI */
176 static inline bool is_secondary(struct i2s_dai
*i2s
)
178 return i2s
->pri_dai
? true : false;
181 /* If operating in SoC-Slave mode */
182 static inline bool is_slave(struct i2s_dai
*i2s
)
184 return (readl(i2s
->addr
+ I2SMOD
) & MOD_SLAVE
) ? true : false;
187 /* If this interface of the controller is transmitting data */
188 static inline bool tx_active(struct i2s_dai
*i2s
)
195 active
= readl(i2s
->addr
+ I2SMOD
);
197 if (is_secondary(i2s
))
198 active
&= CON_TXSDMA_ACTIVE
;
200 active
&= CON_TXDMA_ACTIVE
;
202 return active
? true : false;
205 /* If the other interface of the controller is transmitting data */
206 static inline bool other_tx_active(struct i2s_dai
*i2s
)
208 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
210 return tx_active(other
);
213 /* If any interface of the controller is transmitting data */
214 static inline bool any_tx_active(struct i2s_dai
*i2s
)
216 return tx_active(i2s
) || other_tx_active(i2s
);
219 /* If this interface of the controller is receiving data */
220 static inline bool rx_active(struct i2s_dai
*i2s
)
227 active
= readl(i2s
->addr
+ I2SMOD
) & CON_RXDMA_ACTIVE
;
229 return active
? true : false;
232 /* If the other interface of the controller is receiving data */
233 static inline bool other_rx_active(struct i2s_dai
*i2s
)
235 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
237 return rx_active(other
);
240 /* If any interface of the controller is receiving data */
241 static inline bool any_rx_active(struct i2s_dai
*i2s
)
243 return rx_active(i2s
) || other_rx_active(i2s
);
246 /* If the other DAI is transmitting or receiving data */
247 static inline bool other_active(struct i2s_dai
*i2s
)
249 return other_rx_active(i2s
) || other_tx_active(i2s
);
252 /* If this DAI is transmitting or receiving data */
253 static inline bool this_active(struct i2s_dai
*i2s
)
255 return tx_active(i2s
) || rx_active(i2s
);
258 /* If the controller is active anyway */
259 static inline bool any_active(struct i2s_dai
*i2s
)
261 return this_active(i2s
) || other_active(i2s
);
264 static inline struct i2s_dai
*to_info(struct snd_soc_dai
*dai
)
266 return snd_soc_dai_get_drvdata(dai
);
269 static inline bool is_opened(struct i2s_dai
*i2s
)
271 if (i2s
&& (i2s
->mode
& DAI_OPENED
))
277 static inline bool is_manager(struct i2s_dai
*i2s
)
279 if (is_opened(i2s
) && (i2s
->mode
& DAI_MANAGER
))
285 /* Read RCLK of I2S (in multiples of LRCLK) */
286 static inline unsigned get_rfs(struct i2s_dai
*i2s
)
288 u32 rfs
= (readl(i2s
->addr
+ I2SMOD
) >> 3) & 0x3;
298 /* Write RCLK of I2S (in multiples of LRCLK) */
299 static inline void set_rfs(struct i2s_dai
*i2s
, unsigned rfs
)
301 u32 mod
= readl(i2s
->addr
+ I2SMOD
);
303 mod
&= ~MOD_RCLK_MASK
;
307 mod
|= MOD_RCLK_768FS
;
310 mod
|= MOD_RCLK_512FS
;
313 mod
|= MOD_RCLK_384FS
;
316 mod
|= MOD_RCLK_256FS
;
320 writel(mod
, i2s
->addr
+ I2SMOD
);
323 /* Read Bit-Clock of I2S (in multiples of LRCLK) */
324 static inline unsigned get_bfs(struct i2s_dai
*i2s
)
326 u32 bfs
= (readl(i2s
->addr
+ I2SMOD
) >> 1) & 0x3;
336 /* Write Bit-Clock of I2S (in multiples of LRCLK) */
337 static inline void set_bfs(struct i2s_dai
*i2s
, unsigned bfs
)
339 u32 mod
= readl(i2s
->addr
+ I2SMOD
);
341 mod
&= ~MOD_BCLK_MASK
;
345 mod
|= MOD_BCLK_48FS
;
348 mod
|= MOD_BCLK_32FS
;
351 mod
|= MOD_BCLK_24FS
;
354 mod
|= MOD_BCLK_16FS
;
357 dev_err(&i2s
->pdev
->dev
, "Wrong BCLK Divider!\n");
361 writel(mod
, i2s
->addr
+ I2SMOD
);
365 static inline int get_blc(struct i2s_dai
*i2s
)
367 int blc
= readl(i2s
->addr
+ I2SMOD
);
369 blc
= (blc
>> 13) & 0x3;
378 /* TX Channel Control */
379 static void i2s_txctrl(struct i2s_dai
*i2s
, int on
)
381 void __iomem
*addr
= i2s
->addr
;
382 u32 con
= readl(addr
+ I2SCON
);
383 u32 mod
= readl(addr
+ I2SMOD
) & ~MOD_MASK
;
387 con
&= ~CON_TXCH_PAUSE
;
389 if (is_secondary(i2s
)) {
390 con
|= CON_TXSDMA_ACTIVE
;
391 con
&= ~CON_TXSDMA_PAUSE
;
393 con
|= CON_TXDMA_ACTIVE
;
394 con
&= ~CON_TXDMA_PAUSE
;
397 if (any_rx_active(i2s
))
402 if (is_secondary(i2s
)) {
403 con
|= CON_TXSDMA_PAUSE
;
404 con
&= ~CON_TXSDMA_ACTIVE
;
406 con
|= CON_TXDMA_PAUSE
;
407 con
&= ~CON_TXDMA_ACTIVE
;
410 if (other_tx_active(i2s
)) {
411 writel(con
, addr
+ I2SCON
);
415 con
|= CON_TXCH_PAUSE
;
417 if (any_rx_active(i2s
))
423 writel(mod
, addr
+ I2SMOD
);
424 writel(con
, addr
+ I2SCON
);
427 /* RX Channel Control */
428 static void i2s_rxctrl(struct i2s_dai
*i2s
, int on
)
430 void __iomem
*addr
= i2s
->addr
;
431 u32 con
= readl(addr
+ I2SCON
);
432 u32 mod
= readl(addr
+ I2SMOD
) & ~MOD_MASK
;
435 con
|= CON_RXDMA_ACTIVE
| CON_ACTIVE
;
436 con
&= ~(CON_RXDMA_PAUSE
| CON_RXCH_PAUSE
);
438 if (any_tx_active(i2s
))
443 con
|= CON_RXDMA_PAUSE
| CON_RXCH_PAUSE
;
444 con
&= ~CON_RXDMA_ACTIVE
;
446 if (any_tx_active(i2s
))
452 writel(mod
, addr
+ I2SMOD
);
453 writel(con
, addr
+ I2SCON
);
456 /* Flush FIFO of an interface */
457 static inline void i2s_fifo(struct i2s_dai
*i2s
, u32 flush
)
465 if (is_secondary(i2s
))
466 fic
= i2s
->addr
+ I2SFICS
;
468 fic
= i2s
->addr
+ I2SFIC
;
471 writel(readl(fic
) | flush
, fic
);
474 val
= msecs_to_loops(1) / 1000; /* 1 usec */
478 writel(readl(fic
) & ~flush
, fic
);
481 static int i2s_set_sysclk(struct snd_soc_dai
*dai
,
482 int clk_id
, unsigned int rfs
, int dir
)
484 struct i2s_dai
*i2s
= to_info(dai
);
485 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
486 u32 mod
= readl(i2s
->addr
+ I2SMOD
);
489 case SAMSUNG_I2S_CDCLK
:
490 /* Shouldn't matter in GATING(CLOCK_IN) mode */
491 if (dir
== SND_SOC_CLOCK_IN
)
494 if ((rfs
&& other
->rfs
&& (other
->rfs
!= rfs
)) ||
496 (((dir
== SND_SOC_CLOCK_IN
)
497 && !(mod
& MOD_CDCLKCON
)) ||
498 ((dir
== SND_SOC_CLOCK_OUT
)
499 && (mod
& MOD_CDCLKCON
))))) {
500 dev_err(&i2s
->pdev
->dev
,
501 "%s:%d Other DAI busy\n", __func__
, __LINE__
);
505 if (dir
== SND_SOC_CLOCK_IN
)
508 mod
&= ~MOD_CDCLKCON
;
513 case SAMSUNG_I2S_RCLKSRC_0
: /* clock corrsponding to IISMOD[10] := 0 */
514 case SAMSUNG_I2S_RCLKSRC_1
: /* clock corrsponding to IISMOD[10] := 1 */
515 if ((i2s
->quirks
& QUIRK_NO_MUXPSR
)
516 || (clk_id
== SAMSUNG_I2S_RCLKSRC_0
))
521 if (!any_active(i2s
)) {
523 if ((clk_id
&& !(mod
& MOD_IMS_SYSMUX
)) ||
524 (!clk_id
&& (mod
& MOD_IMS_SYSMUX
))) {
525 clk_disable(i2s
->op_clk
);
526 clk_put(i2s
->op_clk
);
529 clk_get_rate(i2s
->op_clk
);
534 i2s
->op_clk
= clk_get(&i2s
->pdev
->dev
,
535 i2s
->src_clk
[clk_id
]);
536 clk_enable(i2s
->op_clk
);
537 i2s
->rclk_srcrate
= clk_get_rate(i2s
->op_clk
);
539 /* Over-ride the other's */
541 other
->op_clk
= i2s
->op_clk
;
542 other
->rclk_srcrate
= i2s
->rclk_srcrate
;
544 } else if ((!clk_id
&& (mod
& MOD_IMS_SYSMUX
))
545 || (clk_id
&& !(mod
& MOD_IMS_SYSMUX
))) {
546 dev_err(&i2s
->pdev
->dev
,
547 "%s:%d Other DAI busy\n", __func__
, __LINE__
);
550 /* Call can't be on the active DAI */
551 i2s
->op_clk
= other
->op_clk
;
552 i2s
->rclk_srcrate
= other
->rclk_srcrate
;
557 mod
&= ~MOD_IMS_SYSMUX
;
559 mod
|= MOD_IMS_SYSMUX
;
563 dev_err(&i2s
->pdev
->dev
, "We don't serve that!\n");
567 writel(mod
, i2s
->addr
+ I2SMOD
);
572 static int i2s_set_fmt(struct snd_soc_dai
*dai
,
575 struct i2s_dai
*i2s
= to_info(dai
);
576 u32 mod
= readl(i2s
->addr
+ I2SMOD
);
579 /* Format is priority */
580 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
581 case SND_SOC_DAIFMT_RIGHT_J
:
585 case SND_SOC_DAIFMT_LEFT_J
:
589 case SND_SOC_DAIFMT_I2S
:
593 dev_err(&i2s
->pdev
->dev
, "Format not supported\n");
598 * INV flag is relative to the FORMAT flag - if set it simply
599 * flips the polarity specified by the Standard
601 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
602 case SND_SOC_DAIFMT_NB_NF
:
604 case SND_SOC_DAIFMT_NB_IF
:
605 if (tmp
& MOD_LR_RLOW
)
611 dev_err(&i2s
->pdev
->dev
, "Polarity not supported\n");
615 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
616 case SND_SOC_DAIFMT_CBM_CFM
:
619 case SND_SOC_DAIFMT_CBS_CFS
:
620 /* Set default source clock in Master mode */
621 if (i2s
->rclk_srcrate
== 0)
622 i2s_set_sysclk(dai
, SAMSUNG_I2S_RCLKSRC_0
,
623 0, SND_SOC_CLOCK_IN
);
626 dev_err(&i2s
->pdev
->dev
, "master/slave format not supported\n");
630 if (any_active(i2s
) &&
631 ((mod
& (MOD_SDF_MASK
| MOD_LR_RLOW
632 | MOD_SLAVE
)) != tmp
)) {
633 dev_err(&i2s
->pdev
->dev
,
634 "%s:%d Other DAI busy\n", __func__
, __LINE__
);
638 mod
&= ~(MOD_SDF_MASK
| MOD_LR_RLOW
| MOD_SLAVE
);
640 writel(mod
, i2s
->addr
+ I2SMOD
);
645 static int i2s_hw_params(struct snd_pcm_substream
*substream
,
646 struct snd_pcm_hw_params
*params
, struct snd_soc_dai
*dai
)
648 struct i2s_dai
*i2s
= to_info(dai
);
649 u32 mod
= readl(i2s
->addr
+ I2SMOD
);
651 if (!is_secondary(i2s
))
652 mod
&= ~(MOD_DC2_EN
| MOD_DC1_EN
);
654 switch (params_channels(params
)) {
663 dev_err(&i2s
->pdev
->dev
, "%d channels not supported\n",
664 params_channels(params
));
668 if (is_secondary(i2s
))
669 mod
&= ~MOD_BLCS_MASK
;
671 mod
&= ~MOD_BLCP_MASK
;
674 mod
&= ~MOD_BLC_MASK
;
676 switch (params_format(params
)) {
677 case SNDRV_PCM_FORMAT_S8
:
678 if (is_secondary(i2s
))
679 mod
|= MOD_BLCS_8BIT
;
681 mod
|= MOD_BLCP_8BIT
;
685 case SNDRV_PCM_FORMAT_S16_LE
:
686 if (is_secondary(i2s
))
687 mod
|= MOD_BLCS_16BIT
;
689 mod
|= MOD_BLCP_16BIT
;
691 mod
|= MOD_BLC_16BIT
;
693 case SNDRV_PCM_FORMAT_S24_LE
:
694 if (is_secondary(i2s
))
695 mod
|= MOD_BLCS_24BIT
;
697 mod
|= MOD_BLCP_24BIT
;
699 mod
|= MOD_BLC_24BIT
;
702 dev_err(&i2s
->pdev
->dev
, "Format(%d) not supported\n",
703 params_format(params
));
706 writel(mod
, i2s
->addr
+ I2SMOD
);
708 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
709 snd_soc_dai_set_dma_data(dai
, substream
,
710 (void *)&i2s
->dma_playback
);
712 snd_soc_dai_set_dma_data(dai
, substream
,
713 (void *)&i2s
->dma_capture
);
715 i2s
->frmclk
= params_rate(params
);
720 /* We set constraints on the substream acc to the version of I2S */
721 static int i2s_startup(struct snd_pcm_substream
*substream
,
722 struct snd_soc_dai
*dai
)
724 struct i2s_dai
*i2s
= to_info(dai
);
725 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
728 spin_lock_irqsave(&lock
, flags
);
730 i2s
->mode
|= DAI_OPENED
;
732 if (is_manager(other
))
733 i2s
->mode
&= ~DAI_MANAGER
;
735 i2s
->mode
|= DAI_MANAGER
;
737 /* Enforce set_sysclk in Master mode */
738 i2s
->rclk_srcrate
= 0;
740 spin_unlock_irqrestore(&lock
, flags
);
745 static void i2s_shutdown(struct snd_pcm_substream
*substream
,
746 struct snd_soc_dai
*dai
)
748 struct i2s_dai
*i2s
= to_info(dai
);
749 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
752 spin_lock_irqsave(&lock
, flags
);
754 i2s
->mode
&= ~DAI_OPENED
;
755 i2s
->mode
&= ~DAI_MANAGER
;
757 if (is_opened(other
))
758 other
->mode
|= DAI_MANAGER
;
760 /* Reset any constraint on RFS and BFS */
764 spin_unlock_irqrestore(&lock
, flags
);
766 /* Gate CDCLK by default */
767 if (!is_opened(other
))
768 i2s_set_sysclk(dai
, SAMSUNG_I2S_CDCLK
,
769 0, SND_SOC_CLOCK_IN
);
772 static int config_setup(struct i2s_dai
*i2s
)
774 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
775 unsigned rfs
, bfs
, blc
;
785 /* Select least possible multiple(2) if no constraint set */
794 if ((rfs
== 256 || rfs
== 512) && (blc
== 24)) {
795 dev_err(&i2s
->pdev
->dev
,
796 "%d-RFS not supported for 24-blc\n", rfs
);
801 if (bfs
== 16 || bfs
== 32)
807 /* If already setup and running */
808 if (any_active(i2s
) && (get_rfs(i2s
) != rfs
|| get_bfs(i2s
) != bfs
)) {
809 dev_err(&i2s
->pdev
->dev
,
810 "%s:%d Other DAI busy\n", __func__
, __LINE__
);
814 /* Don't bother RFS, BFS & PSR in Slave mode */
821 if (!(i2s
->quirks
& QUIRK_NO_MUXPSR
)) {
822 psr
= i2s
->rclk_srcrate
/ i2s
->frmclk
/ rfs
;
823 writel(((psr
- 1) << 8) | PSR_PSREN
, i2s
->addr
+ I2SPSR
);
824 dev_dbg(&i2s
->pdev
->dev
,
825 "RCLK_SRC=%luHz PSR=%u, RCLK=%dfs, BCLK=%dfs\n",
826 i2s
->rclk_srcrate
, psr
, rfs
, bfs
);
832 static int i2s_trigger(struct snd_pcm_substream
*substream
,
833 int cmd
, struct snd_soc_dai
*dai
)
835 int capture
= (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
);
836 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
837 struct i2s_dai
*i2s
= to_info(rtd
->cpu_dai
);
841 case SNDRV_PCM_TRIGGER_START
:
842 case SNDRV_PCM_TRIGGER_RESUME
:
843 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
844 local_irq_save(flags
);
846 if (config_setup(i2s
)) {
847 local_irq_restore(flags
);
856 local_irq_restore(flags
);
858 case SNDRV_PCM_TRIGGER_STOP
:
859 case SNDRV_PCM_TRIGGER_SUSPEND
:
860 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
861 local_irq_save(flags
);
869 i2s_fifo(i2s
, FIC_RXFLUSH
);
871 i2s_fifo(i2s
, FIC_TXFLUSH
);
873 local_irq_restore(flags
);
880 static int i2s_set_clkdiv(struct snd_soc_dai
*dai
,
883 struct i2s_dai
*i2s
= to_info(dai
);
884 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
887 case SAMSUNG_I2S_DIV_BCLK
:
888 if ((any_active(i2s
) && div
&& (get_bfs(i2s
) != div
))
889 || (other
&& other
->bfs
&& (other
->bfs
!= div
))) {
890 dev_err(&i2s
->pdev
->dev
,
891 "%s:%d Other DAI busy\n", __func__
, __LINE__
);
897 dev_err(&i2s
->pdev
->dev
,
898 "Invalid clock divider(%d)\n", div_id
);
905 static snd_pcm_sframes_t
906 i2s_delay(struct snd_pcm_substream
*substream
, struct snd_soc_dai
*dai
)
908 struct i2s_dai
*i2s
= to_info(dai
);
909 u32 reg
= readl(i2s
->addr
+ I2SFIC
);
910 snd_pcm_sframes_t delay
;
912 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
913 delay
= FIC_RXCOUNT(reg
);
914 else if (is_secondary(i2s
))
915 delay
= FICS_TXCOUNT(readl(i2s
->addr
+ I2SFICS
));
917 delay
= FIC_TXCOUNT(reg
);
923 static int i2s_suspend(struct snd_soc_dai
*dai
)
925 struct i2s_dai
*i2s
= to_info(dai
);
928 i2s
->suspend_i2smod
= readl(i2s
->addr
+ I2SMOD
);
929 i2s
->suspend_i2scon
= readl(i2s
->addr
+ I2SCON
);
930 i2s
->suspend_i2spsr
= readl(i2s
->addr
+ I2SPSR
);
936 static int i2s_resume(struct snd_soc_dai
*dai
)
938 struct i2s_dai
*i2s
= to_info(dai
);
941 writel(i2s
->suspend_i2scon
, i2s
->addr
+ I2SCON
);
942 writel(i2s
->suspend_i2smod
, i2s
->addr
+ I2SMOD
);
943 writel(i2s
->suspend_i2spsr
, i2s
->addr
+ I2SPSR
);
949 #define i2s_suspend NULL
950 #define i2s_resume NULL
953 static int samsung_i2s_dai_probe(struct snd_soc_dai
*dai
)
955 struct i2s_dai
*i2s
= to_info(dai
);
956 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
958 if (other
&& other
->clk
) /* If this is probe on secondary */
961 i2s
->addr
= ioremap(i2s
->base
, 0x100);
962 if (i2s
->addr
== NULL
) {
963 dev_err(&i2s
->pdev
->dev
, "cannot ioremap registers\n");
967 i2s
->clk
= clk_get(&i2s
->pdev
->dev
, "iis");
968 if (IS_ERR(i2s
->clk
)) {
969 dev_err(&i2s
->pdev
->dev
, "failed to get i2s_clock\n");
973 clk_enable(i2s
->clk
);
976 other
->addr
= i2s
->addr
;
977 other
->clk
= i2s
->clk
;
980 if (i2s
->quirks
& QUIRK_NEED_RSTCLR
)
981 writel(CON_RSTCLR
, i2s
->addr
+ I2SCON
);
984 /* Reset any constraint on RFS and BFS */
989 i2s_fifo(i2s
, FIC_TXFLUSH
);
990 i2s_fifo(other
, FIC_TXFLUSH
);
991 i2s_fifo(i2s
, FIC_RXFLUSH
);
993 /* Gate CDCLK by default */
994 if (!is_opened(other
))
995 i2s_set_sysclk(dai
, SAMSUNG_I2S_CDCLK
,
996 0, SND_SOC_CLOCK_IN
);
1001 static int samsung_i2s_dai_remove(struct snd_soc_dai
*dai
)
1003 struct i2s_dai
*i2s
= snd_soc_dai_get_drvdata(dai
);
1004 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
1006 if (!other
|| !other
->clk
) {
1008 if (i2s
->quirks
& QUIRK_NEED_RSTCLR
)
1009 writel(0, i2s
->addr
+ I2SCON
);
1011 clk_disable(i2s
->clk
);
1022 static struct snd_soc_dai_ops samsung_i2s_dai_ops
= {
1023 .trigger
= i2s_trigger
,
1024 .hw_params
= i2s_hw_params
,
1025 .set_fmt
= i2s_set_fmt
,
1026 .set_clkdiv
= i2s_set_clkdiv
,
1027 .set_sysclk
= i2s_set_sysclk
,
1028 .startup
= i2s_startup
,
1029 .shutdown
= i2s_shutdown
,
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)
1040 struct i2s_dai
*i2s_alloc_dai(struct platform_device
*pdev
, bool sec
)
1042 struct i2s_dai
*i2s
;
1044 i2s
= kzalloc(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
= 2;
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
= 2;
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 } else { /* Create a new platform_device for Secondary */
1068 i2s
->pdev
= platform_device_register_resndata(NULL
,
1069 pdev
->name
, pdev
->id
+ SAMSUNG_I2S_SECOFF
,
1071 if (IS_ERR(i2s
->pdev
)) {
1077 /* Pre-assign snd_soc_dai_set_drvdata */
1078 dev_set_drvdata(&i2s
->pdev
->dev
, i2s
);
1083 static __devinit
int samsung_i2s_probe(struct platform_device
*pdev
)
1085 u32 dma_pl_chan
, dma_cp_chan
, dma_pl_sec_chan
;
1086 struct i2s_dai
*pri_dai
, *sec_dai
= NULL
;
1087 struct s3c_audio_pdata
*i2s_pdata
;
1088 struct samsung_i2s
*i2s_cfg
;
1089 struct resource
*res
;
1090 u32 regs_base
, quirks
;
1093 /* Call during Seconday interface registration */
1094 if (pdev
->id
>= SAMSUNG_I2S_SECOFF
) {
1095 sec_dai
= dev_get_drvdata(&pdev
->dev
);
1096 snd_soc_register_dai(&sec_dai
->pdev
->dev
,
1097 &sec_dai
->i2s_dai_drv
);
1101 i2s_pdata
= pdev
->dev
.platform_data
;
1102 if (i2s_pdata
== NULL
) {
1103 dev_err(&pdev
->dev
, "Can't work without s3c_audio_pdata\n");
1107 res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 0);
1109 dev_err(&pdev
->dev
, "Unable to get I2S-TX dma resource\n");
1112 dma_pl_chan
= res
->start
;
1114 res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 1);
1116 dev_err(&pdev
->dev
, "Unable to get I2S-RX dma resource\n");
1119 dma_cp_chan
= res
->start
;
1121 res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 2);
1123 dma_pl_sec_chan
= res
->start
;
1125 dma_pl_sec_chan
= 0;
1127 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1129 dev_err(&pdev
->dev
, "Unable to get I2S SFR address\n");
1133 if (!request_mem_region(res
->start
, resource_size(res
),
1135 dev_err(&pdev
->dev
, "Unable to request SFR region\n");
1138 regs_base
= res
->start
;
1140 i2s_cfg
= &i2s_pdata
->type
.i2s
;
1141 quirks
= i2s_cfg
->quirks
;
1143 pri_dai
= i2s_alloc_dai(pdev
, false);
1145 dev_err(&pdev
->dev
, "Unable to alloc I2S_pri\n");
1150 pri_dai
->dma_playback
.dma_addr
= regs_base
+ I2STXD
;
1151 pri_dai
->dma_capture
.dma_addr
= regs_base
+ I2SRXD
;
1152 pri_dai
->dma_playback
.client
=
1153 (struct s3c2410_dma_client
*)&pri_dai
->dma_playback
;
1154 pri_dai
->dma_capture
.client
=
1155 (struct s3c2410_dma_client
*)&pri_dai
->dma_capture
;
1156 pri_dai
->dma_playback
.channel
= dma_pl_chan
;
1157 pri_dai
->dma_capture
.channel
= dma_cp_chan
;
1158 pri_dai
->src_clk
= i2s_cfg
->src_clk
;
1159 pri_dai
->dma_playback
.dma_size
= 4;
1160 pri_dai
->dma_capture
.dma_size
= 4;
1161 pri_dai
->base
= regs_base
;
1162 pri_dai
->quirks
= quirks
;
1164 if (quirks
& QUIRK_PRI_6CHAN
)
1165 pri_dai
->i2s_dai_drv
.playback
.channels_max
= 6;
1167 if (quirks
& QUIRK_SEC_DAI
) {
1168 sec_dai
= i2s_alloc_dai(pdev
, true);
1170 dev_err(&pdev
->dev
, "Unable to alloc I2S_sec\n");
1174 sec_dai
->dma_playback
.dma_addr
= regs_base
+ I2STXDS
;
1175 sec_dai
->dma_playback
.client
=
1176 (struct s3c2410_dma_client
*)&sec_dai
->dma_playback
;
1177 /* Use iDMA always if SysDMA not provided */
1178 sec_dai
->dma_playback
.channel
= dma_pl_sec_chan
? : -1;
1179 sec_dai
->src_clk
= i2s_cfg
->src_clk
;
1180 sec_dai
->dma_playback
.dma_size
= 4;
1181 sec_dai
->base
= regs_base
;
1182 sec_dai
->quirks
= quirks
;
1183 sec_dai
->pri_dai
= pri_dai
;
1184 pri_dai
->sec_dai
= sec_dai
;
1187 if (i2s_pdata
->cfg_gpio
&& i2s_pdata
->cfg_gpio(pdev
)) {
1188 dev_err(&pdev
->dev
, "Unable to configure gpio\n");
1193 snd_soc_register_dai(&pri_dai
->pdev
->dev
, &pri_dai
->i2s_dai_drv
);
1201 release_mem_region(regs_base
, resource_size(res
));
1206 static __devexit
int samsung_i2s_remove(struct platform_device
*pdev
)
1208 struct i2s_dai
*i2s
, *other
;
1210 i2s
= dev_get_drvdata(&pdev
->dev
);
1211 other
= i2s
->pri_dai
? : i2s
->sec_dai
;
1214 other
->pri_dai
= NULL
;
1215 other
->sec_dai
= NULL
;
1217 struct resource
*res
;
1218 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1220 release_mem_region(res
->start
, resource_size(res
));
1223 i2s
->pri_dai
= NULL
;
1224 i2s
->sec_dai
= NULL
;
1228 snd_soc_unregister_dai(&pdev
->dev
);
1233 static struct platform_driver samsung_i2s_driver
= {
1234 .probe
= samsung_i2s_probe
,
1235 .remove
= samsung_i2s_remove
,
1237 .name
= "samsung-i2s",
1238 .owner
= THIS_MODULE
,
1242 static int __init
samsung_i2s_init(void)
1244 return platform_driver_register(&samsung_i2s_driver
);
1246 module_init(samsung_i2s_init
);
1248 static void __exit
samsung_i2s_exit(void)
1250 platform_driver_unregister(&samsung_i2s_driver
);
1252 module_exit(samsung_i2s_exit
);
1254 /* Module information */
1255 MODULE_AUTHOR("Jaswinder Singh, <jassi.brar@samsung.com>");
1256 MODULE_DESCRIPTION("Samsung I2S Interface");
1257 MODULE_ALIAS("platform:samsung-i2s");
1258 MODULE_LICENSE("GPL");