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/soc.h>
19 #include <sound/pcm_params.h>
21 #include <plat/audio.h>
35 #define CON_RSTCLR (1 << 31)
36 #define CON_FRXOFSTATUS (1 << 26)
37 #define CON_FRXORINTEN (1 << 25)
38 #define CON_FTXSURSTAT (1 << 24)
39 #define CON_FTXSURINTEN (1 << 23)
40 #define CON_TXSDMA_PAUSE (1 << 20)
41 #define CON_TXSDMA_ACTIVE (1 << 18)
43 #define CON_FTXURSTATUS (1 << 17)
44 #define CON_FTXURINTEN (1 << 16)
45 #define CON_TXFIFO2_EMPTY (1 << 15)
46 #define CON_TXFIFO1_EMPTY (1 << 14)
47 #define CON_TXFIFO2_FULL (1 << 13)
48 #define CON_TXFIFO1_FULL (1 << 12)
50 #define CON_LRINDEX (1 << 11)
51 #define CON_TXFIFO_EMPTY (1 << 10)
52 #define CON_RXFIFO_EMPTY (1 << 9)
53 #define CON_TXFIFO_FULL (1 << 8)
54 #define CON_RXFIFO_FULL (1 << 7)
55 #define CON_TXDMA_PAUSE (1 << 6)
56 #define CON_RXDMA_PAUSE (1 << 5)
57 #define CON_TXCH_PAUSE (1 << 4)
58 #define CON_RXCH_PAUSE (1 << 3)
59 #define CON_TXDMA_ACTIVE (1 << 2)
60 #define CON_RXDMA_ACTIVE (1 << 1)
61 #define CON_ACTIVE (1 << 0)
63 #define MOD_OPCLK_CDCLK_OUT (0 << 30)
64 #define MOD_OPCLK_CDCLK_IN (1 << 30)
65 #define MOD_OPCLK_BCLK_OUT (2 << 30)
66 #define MOD_OPCLK_PCLK (3 << 30)
67 #define MOD_OPCLK_MASK (3 << 30)
68 #define MOD_TXS_IDMA (1 << 28) /* Sec_TXFIFO use I-DMA */
70 #define MOD_BLCS_SHIFT 26
71 #define MOD_BLCS_16BIT (0 << MOD_BLCS_SHIFT)
72 #define MOD_BLCS_8BIT (1 << MOD_BLCS_SHIFT)
73 #define MOD_BLCS_24BIT (2 << MOD_BLCS_SHIFT)
74 #define MOD_BLCS_MASK (3 << MOD_BLCS_SHIFT)
75 #define MOD_BLCP_SHIFT 24
76 #define MOD_BLCP_16BIT (0 << MOD_BLCP_SHIFT)
77 #define MOD_BLCP_8BIT (1 << MOD_BLCP_SHIFT)
78 #define MOD_BLCP_24BIT (2 << MOD_BLCP_SHIFT)
79 #define MOD_BLCP_MASK (3 << MOD_BLCP_SHIFT)
81 #define MOD_C2DD_HHALF (1 << 21) /* Discard Higher-half */
82 #define MOD_C2DD_LHALF (1 << 20) /* Discard Lower-half */
83 #define MOD_C1DD_HHALF (1 << 19)
84 #define MOD_C1DD_LHALF (1 << 18)
85 #define MOD_DC2_EN (1 << 17)
86 #define MOD_DC1_EN (1 << 16)
87 #define MOD_BLC_16BIT (0 << 13)
88 #define MOD_BLC_8BIT (1 << 13)
89 #define MOD_BLC_24BIT (2 << 13)
90 #define MOD_BLC_MASK (3 << 13)
92 #define MOD_IMS_SYSMUX (1 << 10)
93 #define MOD_SLAVE (1 << 11)
94 #define MOD_TXONLY (0 << 8)
95 #define MOD_RXONLY (1 << 8)
96 #define MOD_TXRX (2 << 8)
97 #define MOD_MASK (3 << 8)
98 #define MOD_LR_LLOW (0 << 7)
99 #define MOD_LR_RLOW (1 << 7)
100 #define MOD_SDF_IIS (0 << 5)
101 #define MOD_SDF_MSB (1 << 5)
102 #define MOD_SDF_LSB (2 << 5)
103 #define MOD_SDF_MASK (3 << 5)
104 #define MOD_RCLK_256FS (0 << 3)
105 #define MOD_RCLK_512FS (1 << 3)
106 #define MOD_RCLK_384FS (2 << 3)
107 #define MOD_RCLK_768FS (3 << 3)
108 #define MOD_RCLK_MASK (3 << 3)
109 #define MOD_BCLK_32FS (0 << 1)
110 #define MOD_BCLK_48FS (1 << 1)
111 #define MOD_BCLK_16FS (2 << 1)
112 #define MOD_BCLK_24FS (3 << 1)
113 #define MOD_BCLK_MASK (3 << 1)
114 #define MOD_8BIT (1 << 0)
116 #define MOD_CDCLKCON (1 << 12)
118 #define PSR_PSREN (1 << 15)
120 #define FIC_TX2COUNT(x) (((x) >> 24) & 0xf)
121 #define FIC_TX1COUNT(x) (((x) >> 16) & 0xf)
123 #define FIC_TXFLUSH (1 << 15)
124 #define FIC_RXFLUSH (1 << 7)
125 #define FIC_TXCOUNT(x) (((x) >> 8) & 0xf)
126 #define FIC_RXCOUNT(x) (((x) >> 0) & 0xf)
127 #define FICS_TXCOUNT(x) (((x) >> 8) & 0x7f)
129 #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
132 /* Platform device for this DAI */
133 struct platform_device
*pdev
;
136 /* Physical base address of SFRs */
138 /* Rate of RCLK source clock */
139 unsigned long rclk_srcrate
;
143 * Specifically requested RCLK,BCLK by MACHINE Driver.
144 * 0 indicates CPU driver is free to choose any value.
147 /* I2S Controller's core clock */
149 /* Clock for generating I2S signals */
151 /* Array of clock names for op_clk */
152 const char **src_clk
;
153 /* Pointer to the Primary_Fifo if this is Sec_Fifo, NULL otherwise */
154 struct i2s_dai
*pri_dai
;
155 /* Pointer to the Secondary_Fifo if it has one, NULL otherwise */
156 struct i2s_dai
*sec_dai
;
157 #define DAI_OPENED (1 << 0) /* Dai is opened */
158 #define DAI_MANAGER (1 << 1) /* Dai is the manager */
160 /* Driver for this DAI */
161 struct snd_soc_dai_driver i2s_dai_drv
;
163 struct s3c_dma_params dma_playback
;
164 struct s3c_dma_params dma_capture
;
171 /* Lock for cross i/f checks */
172 static DEFINE_SPINLOCK(lock
);
174 /* If this is the 'overlay' stereo DAI */
175 static inline bool is_secondary(struct i2s_dai
*i2s
)
177 return i2s
->pri_dai
? true : false;
180 /* If operating in SoC-Slave mode */
181 static inline bool is_slave(struct i2s_dai
*i2s
)
183 return (readl(i2s
->addr
+ I2SMOD
) & MOD_SLAVE
) ? true : false;
186 /* If this interface of the controller is transmitting data */
187 static inline bool tx_active(struct i2s_dai
*i2s
)
194 active
= readl(i2s
->addr
+ I2SMOD
);
196 if (is_secondary(i2s
))
197 active
&= CON_TXSDMA_ACTIVE
;
199 active
&= CON_TXDMA_ACTIVE
;
201 return active
? true : false;
204 /* If the other interface of the controller is transmitting data */
205 static inline bool other_tx_active(struct i2s_dai
*i2s
)
207 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
209 return tx_active(other
);
212 /* If any interface of the controller is transmitting data */
213 static inline bool any_tx_active(struct i2s_dai
*i2s
)
215 return tx_active(i2s
) || other_tx_active(i2s
);
218 /* If this interface of the controller is receiving data */
219 static inline bool rx_active(struct i2s_dai
*i2s
)
226 active
= readl(i2s
->addr
+ I2SMOD
) & CON_RXDMA_ACTIVE
;
228 return active
? true : false;
231 /* If the other interface of the controller is receiving data */
232 static inline bool other_rx_active(struct i2s_dai
*i2s
)
234 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
236 return rx_active(other
);
239 /* If any interface of the controller is receiving data */
240 static inline bool any_rx_active(struct i2s_dai
*i2s
)
242 return rx_active(i2s
) || other_rx_active(i2s
);
245 /* If the other DAI is transmitting or receiving data */
246 static inline bool other_active(struct i2s_dai
*i2s
)
248 return other_rx_active(i2s
) || other_tx_active(i2s
);
251 /* If this DAI is transmitting or receiving data */
252 static inline bool this_active(struct i2s_dai
*i2s
)
254 return tx_active(i2s
) || rx_active(i2s
);
257 /* If the controller is active anyway */
258 static inline bool any_active(struct i2s_dai
*i2s
)
260 return this_active(i2s
) || other_active(i2s
);
263 static inline struct i2s_dai
*to_info(struct snd_soc_dai
*dai
)
265 return snd_soc_dai_get_drvdata(dai
);
268 static inline bool is_opened(struct i2s_dai
*i2s
)
270 if (i2s
&& (i2s
->mode
& DAI_OPENED
))
276 static inline bool is_manager(struct i2s_dai
*i2s
)
278 if (is_opened(i2s
) && (i2s
->mode
& DAI_MANAGER
))
284 /* Read RCLK of I2S (in multiples of LRCLK) */
285 static inline unsigned get_rfs(struct i2s_dai
*i2s
)
287 u32 rfs
= (readl(i2s
->addr
+ I2SMOD
) >> 3) & 0x3;
297 /* Write RCLK of I2S (in multiples of LRCLK) */
298 static inline void set_rfs(struct i2s_dai
*i2s
, unsigned rfs
)
300 u32 mod
= readl(i2s
->addr
+ I2SMOD
);
302 mod
&= ~MOD_RCLK_MASK
;
306 mod
|= MOD_RCLK_768FS
;
309 mod
|= MOD_RCLK_512FS
;
312 mod
|= MOD_RCLK_384FS
;
315 mod
|= MOD_RCLK_256FS
;
319 writel(mod
, i2s
->addr
+ I2SMOD
);
322 /* Read Bit-Clock of I2S (in multiples of LRCLK) */
323 static inline unsigned get_bfs(struct i2s_dai
*i2s
)
325 u32 bfs
= (readl(i2s
->addr
+ I2SMOD
) >> 1) & 0x3;
335 /* Write Bit-Clock of I2S (in multiples of LRCLK) */
336 static inline void set_bfs(struct i2s_dai
*i2s
, unsigned bfs
)
338 u32 mod
= readl(i2s
->addr
+ I2SMOD
);
340 mod
&= ~MOD_BCLK_MASK
;
344 mod
|= MOD_BCLK_48FS
;
347 mod
|= MOD_BCLK_32FS
;
350 mod
|= MOD_BCLK_24FS
;
353 mod
|= MOD_BCLK_16FS
;
356 dev_err(&i2s
->pdev
->dev
, "Wrong BCLK Divider!\n");
360 writel(mod
, i2s
->addr
+ I2SMOD
);
364 static inline int get_blc(struct i2s_dai
*i2s
)
366 int blc
= readl(i2s
->addr
+ I2SMOD
);
368 blc
= (blc
>> 13) & 0x3;
377 /* TX Channel Control */
378 static void i2s_txctrl(struct i2s_dai
*i2s
, int on
)
380 void __iomem
*addr
= i2s
->addr
;
381 u32 con
= readl(addr
+ I2SCON
);
382 u32 mod
= readl(addr
+ I2SMOD
) & ~MOD_MASK
;
386 con
&= ~CON_TXCH_PAUSE
;
388 if (is_secondary(i2s
)) {
389 con
|= CON_TXSDMA_ACTIVE
;
390 con
&= ~CON_TXSDMA_PAUSE
;
392 con
|= CON_TXDMA_ACTIVE
;
393 con
&= ~CON_TXDMA_PAUSE
;
396 if (any_rx_active(i2s
))
401 if (is_secondary(i2s
)) {
402 con
|= CON_TXSDMA_PAUSE
;
403 con
&= ~CON_TXSDMA_ACTIVE
;
405 con
|= CON_TXDMA_PAUSE
;
406 con
&= ~CON_TXDMA_ACTIVE
;
409 if (other_tx_active(i2s
)) {
410 writel(con
, addr
+ I2SCON
);
414 con
|= CON_TXCH_PAUSE
;
416 if (any_rx_active(i2s
))
422 writel(mod
, addr
+ I2SMOD
);
423 writel(con
, addr
+ I2SCON
);
426 /* RX Channel Control */
427 static void i2s_rxctrl(struct i2s_dai
*i2s
, int on
)
429 void __iomem
*addr
= i2s
->addr
;
430 u32 con
= readl(addr
+ I2SCON
);
431 u32 mod
= readl(addr
+ I2SMOD
) & ~MOD_MASK
;
434 con
|= CON_RXDMA_ACTIVE
| CON_ACTIVE
;
435 con
&= ~(CON_RXDMA_PAUSE
| CON_RXCH_PAUSE
);
437 if (any_tx_active(i2s
))
442 con
|= CON_RXDMA_PAUSE
| CON_RXCH_PAUSE
;
443 con
&= ~CON_RXDMA_ACTIVE
;
445 if (any_tx_active(i2s
))
451 writel(mod
, addr
+ I2SMOD
);
452 writel(con
, addr
+ I2SCON
);
455 /* Flush FIFO of an interface */
456 static inline void i2s_fifo(struct i2s_dai
*i2s
, u32 flush
)
464 if (is_secondary(i2s
))
465 fic
= i2s
->addr
+ I2SFICS
;
467 fic
= i2s
->addr
+ I2SFIC
;
470 writel(readl(fic
) | flush
, fic
);
473 val
= msecs_to_loops(1) / 1000; /* 1 usec */
477 writel(readl(fic
) & ~flush
, fic
);
480 static int i2s_set_sysclk(struct snd_soc_dai
*dai
,
481 int clk_id
, unsigned int rfs
, int dir
)
483 struct i2s_dai
*i2s
= to_info(dai
);
484 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
485 u32 mod
= readl(i2s
->addr
+ I2SMOD
);
488 case SAMSUNG_I2S_CDCLK
:
489 /* Shouldn't matter in GATING(CLOCK_IN) mode */
490 if (dir
== SND_SOC_CLOCK_IN
)
493 if ((rfs
&& other
->rfs
&& (other
->rfs
!= rfs
)) ||
495 (((dir
== SND_SOC_CLOCK_IN
)
496 && !(mod
& MOD_CDCLKCON
)) ||
497 ((dir
== SND_SOC_CLOCK_OUT
)
498 && (mod
& MOD_CDCLKCON
))))) {
499 dev_err(&i2s
->pdev
->dev
,
500 "%s:%d Other DAI busy\n", __func__
, __LINE__
);
504 if (dir
== SND_SOC_CLOCK_IN
)
507 mod
&= ~MOD_CDCLKCON
;
512 case SAMSUNG_I2S_RCLKSRC_0
: /* clock corrsponding to IISMOD[10] := 0 */
513 case SAMSUNG_I2S_RCLKSRC_1
: /* clock corrsponding to IISMOD[10] := 1 */
514 if ((i2s
->quirks
& QUIRK_NO_MUXPSR
)
515 || (clk_id
== SAMSUNG_I2S_RCLKSRC_0
))
520 if (!any_active(i2s
)) {
522 if ((clk_id
&& !(mod
& MOD_IMS_SYSMUX
)) ||
523 (!clk_id
&& (mod
& MOD_IMS_SYSMUX
))) {
524 clk_disable(i2s
->op_clk
);
525 clk_put(i2s
->op_clk
);
528 clk_get_rate(i2s
->op_clk
);
533 i2s
->op_clk
= clk_get(&i2s
->pdev
->dev
,
534 i2s
->src_clk
[clk_id
]);
535 clk_enable(i2s
->op_clk
);
536 i2s
->rclk_srcrate
= clk_get_rate(i2s
->op_clk
);
538 /* Over-ride the other's */
540 other
->op_clk
= i2s
->op_clk
;
541 other
->rclk_srcrate
= i2s
->rclk_srcrate
;
543 } else if ((!clk_id
&& (mod
& MOD_IMS_SYSMUX
))
544 || (clk_id
&& !(mod
& MOD_IMS_SYSMUX
))) {
545 dev_err(&i2s
->pdev
->dev
,
546 "%s:%d Other DAI busy\n", __func__
, __LINE__
);
549 /* Call can't be on the active DAI */
550 i2s
->op_clk
= other
->op_clk
;
551 i2s
->rclk_srcrate
= other
->rclk_srcrate
;
556 mod
&= ~MOD_IMS_SYSMUX
;
558 mod
|= MOD_IMS_SYSMUX
;
562 dev_err(&i2s
->pdev
->dev
, "We don't serve that!\n");
566 writel(mod
, i2s
->addr
+ I2SMOD
);
571 static int i2s_set_fmt(struct snd_soc_dai
*dai
,
574 struct i2s_dai
*i2s
= to_info(dai
);
575 u32 mod
= readl(i2s
->addr
+ I2SMOD
);
578 /* Format is priority */
579 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
580 case SND_SOC_DAIFMT_RIGHT_J
:
584 case SND_SOC_DAIFMT_LEFT_J
:
588 case SND_SOC_DAIFMT_I2S
:
592 dev_err(&i2s
->pdev
->dev
, "Format not supported\n");
597 * INV flag is relative to the FORMAT flag - if set it simply
598 * flips the polarity specified by the Standard
600 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
601 case SND_SOC_DAIFMT_NB_NF
:
603 case SND_SOC_DAIFMT_NB_IF
:
604 if (tmp
& MOD_LR_RLOW
)
610 dev_err(&i2s
->pdev
->dev
, "Polarity not supported\n");
614 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
615 case SND_SOC_DAIFMT_CBM_CFM
:
618 case SND_SOC_DAIFMT_CBS_CFS
:
619 /* Set default source clock in Master mode */
620 if (i2s
->rclk_srcrate
== 0)
621 i2s_set_sysclk(dai
, SAMSUNG_I2S_RCLKSRC_0
,
622 0, SND_SOC_CLOCK_IN
);
625 dev_err(&i2s
->pdev
->dev
, "master/slave format not supported\n");
629 if (any_active(i2s
) &&
630 ((mod
& (MOD_SDF_MASK
| MOD_LR_RLOW
631 | MOD_SLAVE
)) != tmp
)) {
632 dev_err(&i2s
->pdev
->dev
,
633 "%s:%d Other DAI busy\n", __func__
, __LINE__
);
637 mod
&= ~(MOD_SDF_MASK
| MOD_LR_RLOW
| MOD_SLAVE
);
639 writel(mod
, i2s
->addr
+ I2SMOD
);
644 static int i2s_hw_params(struct snd_pcm_substream
*substream
,
645 struct snd_pcm_hw_params
*params
, struct snd_soc_dai
*dai
)
647 struct i2s_dai
*i2s
= to_info(dai
);
648 u32 mod
= readl(i2s
->addr
+ I2SMOD
);
650 if (!is_secondary(i2s
))
651 mod
&= ~(MOD_DC2_EN
| MOD_DC1_EN
);
653 switch (params_channels(params
)) {
662 dev_err(&i2s
->pdev
->dev
, "%d channels not supported\n",
663 params_channels(params
));
667 if (is_secondary(i2s
))
668 mod
&= ~MOD_BLCS_MASK
;
670 mod
&= ~MOD_BLCP_MASK
;
673 mod
&= ~MOD_BLC_MASK
;
675 switch (params_format(params
)) {
676 case SNDRV_PCM_FORMAT_S8
:
677 if (is_secondary(i2s
))
678 mod
|= MOD_BLCS_8BIT
;
680 mod
|= MOD_BLCP_8BIT
;
684 case SNDRV_PCM_FORMAT_S16_LE
:
685 if (is_secondary(i2s
))
686 mod
|= MOD_BLCS_16BIT
;
688 mod
|= MOD_BLCP_16BIT
;
690 mod
|= MOD_BLC_16BIT
;
692 case SNDRV_PCM_FORMAT_S24_LE
:
693 if (is_secondary(i2s
))
694 mod
|= MOD_BLCS_24BIT
;
696 mod
|= MOD_BLCP_24BIT
;
698 mod
|= MOD_BLC_24BIT
;
701 dev_err(&i2s
->pdev
->dev
, "Format(%d) not supported\n",
702 params_format(params
));
705 writel(mod
, i2s
->addr
+ I2SMOD
);
707 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
708 snd_soc_dai_set_dma_data(dai
, substream
,
709 (void *)&i2s
->dma_playback
);
711 snd_soc_dai_set_dma_data(dai
, substream
,
712 (void *)&i2s
->dma_capture
);
714 i2s
->frmclk
= params_rate(params
);
719 /* We set constraints on the substream acc to the version of I2S */
720 static int i2s_startup(struct snd_pcm_substream
*substream
,
721 struct snd_soc_dai
*dai
)
723 struct i2s_dai
*i2s
= to_info(dai
);
724 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
727 spin_lock_irqsave(&lock
, flags
);
729 i2s
->mode
|= DAI_OPENED
;
731 if (is_manager(other
))
732 i2s
->mode
&= ~DAI_MANAGER
;
734 i2s
->mode
|= DAI_MANAGER
;
736 /* Enforce set_sysclk in Master mode */
737 i2s
->rclk_srcrate
= 0;
739 spin_unlock_irqrestore(&lock
, flags
);
744 static void i2s_shutdown(struct snd_pcm_substream
*substream
,
745 struct snd_soc_dai
*dai
)
747 struct i2s_dai
*i2s
= to_info(dai
);
748 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
751 spin_lock_irqsave(&lock
, flags
);
753 i2s
->mode
&= ~DAI_OPENED
;
754 i2s
->mode
&= ~DAI_MANAGER
;
756 if (is_opened(other
))
757 other
->mode
|= DAI_MANAGER
;
759 /* Reset any constraint on RFS and BFS */
763 spin_unlock_irqrestore(&lock
, flags
);
765 /* Gate CDCLK by default */
766 if (!is_opened(other
))
767 i2s_set_sysclk(dai
, SAMSUNG_I2S_CDCLK
,
768 0, SND_SOC_CLOCK_IN
);
771 static int config_setup(struct i2s_dai
*i2s
)
773 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
774 unsigned rfs
, bfs
, blc
;
784 /* Select least possible multiple(2) if no constraint set */
793 if ((rfs
== 256 || rfs
== 512) && (blc
== 24)) {
794 dev_err(&i2s
->pdev
->dev
,
795 "%d-RFS not supported for 24-blc\n", rfs
);
800 if (bfs
== 16 || bfs
== 32)
806 /* If already setup and running */
807 if (any_active(i2s
) && (get_rfs(i2s
) != rfs
|| get_bfs(i2s
) != bfs
)) {
808 dev_err(&i2s
->pdev
->dev
,
809 "%s:%d Other DAI busy\n", __func__
, __LINE__
);
813 /* Don't bother RFS, BFS & PSR in Slave mode */
820 if (!(i2s
->quirks
& QUIRK_NO_MUXPSR
)) {
821 psr
= i2s
->rclk_srcrate
/ i2s
->frmclk
/ rfs
;
822 writel(((psr
- 1) << 8) | PSR_PSREN
, i2s
->addr
+ I2SPSR
);
823 dev_dbg(&i2s
->pdev
->dev
,
824 "RCLK_SRC=%luHz PSR=%u, RCLK=%dfs, BCLK=%dfs\n",
825 i2s
->rclk_srcrate
, psr
, rfs
, bfs
);
831 static int i2s_trigger(struct snd_pcm_substream
*substream
,
832 int cmd
, struct snd_soc_dai
*dai
)
834 int capture
= (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
);
835 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
836 struct i2s_dai
*i2s
= to_info(rtd
->cpu_dai
);
840 case SNDRV_PCM_TRIGGER_START
:
841 case SNDRV_PCM_TRIGGER_RESUME
:
842 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
843 local_irq_save(flags
);
845 if (config_setup(i2s
)) {
846 local_irq_restore(flags
);
855 local_irq_restore(flags
);
857 case SNDRV_PCM_TRIGGER_STOP
:
858 case SNDRV_PCM_TRIGGER_SUSPEND
:
859 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
860 local_irq_save(flags
);
868 i2s_fifo(i2s
, FIC_RXFLUSH
);
870 i2s_fifo(i2s
, FIC_TXFLUSH
);
872 local_irq_restore(flags
);
879 static int i2s_set_clkdiv(struct snd_soc_dai
*dai
,
882 struct i2s_dai
*i2s
= to_info(dai
);
883 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
886 case SAMSUNG_I2S_DIV_BCLK
:
887 if ((any_active(i2s
) && div
&& (get_bfs(i2s
) != div
))
888 || (other
&& other
->bfs
&& (other
->bfs
!= div
))) {
889 dev_err(&i2s
->pdev
->dev
,
890 "%s:%d Other DAI busy\n", __func__
, __LINE__
);
896 dev_err(&i2s
->pdev
->dev
,
897 "Invalid clock divider(%d)\n", div_id
);
904 static snd_pcm_sframes_t
905 i2s_delay(struct snd_pcm_substream
*substream
, struct snd_soc_dai
*dai
)
907 struct i2s_dai
*i2s
= to_info(dai
);
908 u32 reg
= readl(i2s
->addr
+ I2SFIC
);
909 snd_pcm_sframes_t delay
;
911 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
912 delay
= FIC_RXCOUNT(reg
);
913 else if (is_secondary(i2s
))
914 delay
= FICS_TXCOUNT(readl(i2s
->addr
+ I2SFICS
));
916 delay
= FIC_TXCOUNT(reg
);
922 static int i2s_suspend(struct snd_soc_dai
*dai
)
924 struct i2s_dai
*i2s
= to_info(dai
);
927 i2s
->suspend_i2smod
= readl(i2s
->addr
+ I2SMOD
);
928 i2s
->suspend_i2scon
= readl(i2s
->addr
+ I2SCON
);
929 i2s
->suspend_i2spsr
= readl(i2s
->addr
+ I2SPSR
);
935 static int i2s_resume(struct snd_soc_dai
*dai
)
937 struct i2s_dai
*i2s
= to_info(dai
);
940 writel(i2s
->suspend_i2scon
, i2s
->addr
+ I2SCON
);
941 writel(i2s
->suspend_i2smod
, i2s
->addr
+ I2SMOD
);
942 writel(i2s
->suspend_i2spsr
, i2s
->addr
+ I2SPSR
);
948 #define i2s_suspend NULL
949 #define i2s_resume NULL
952 static int samsung_i2s_dai_probe(struct snd_soc_dai
*dai
)
954 struct i2s_dai
*i2s
= to_info(dai
);
955 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
957 if (other
&& other
->clk
) /* If this is probe on secondary */
960 i2s
->addr
= ioremap(i2s
->base
, 0x100);
961 if (i2s
->addr
== NULL
) {
962 dev_err(&i2s
->pdev
->dev
, "cannot ioremap registers\n");
966 i2s
->clk
= clk_get(&i2s
->pdev
->dev
, "iis");
967 if (IS_ERR(i2s
->clk
)) {
968 dev_err(&i2s
->pdev
->dev
, "failed to get i2s_clock\n");
972 clk_enable(i2s
->clk
);
975 other
->addr
= i2s
->addr
;
976 other
->clk
= i2s
->clk
;
979 if (i2s
->quirks
& QUIRK_NEED_RSTCLR
)
980 writel(CON_RSTCLR
, i2s
->addr
+ I2SCON
);
983 /* Reset any constraint on RFS and BFS */
988 i2s_fifo(i2s
, FIC_TXFLUSH
);
989 i2s_fifo(other
, FIC_TXFLUSH
);
990 i2s_fifo(i2s
, FIC_RXFLUSH
);
992 /* Gate CDCLK by default */
993 if (!is_opened(other
))
994 i2s_set_sysclk(dai
, SAMSUNG_I2S_CDCLK
,
995 0, SND_SOC_CLOCK_IN
);
1000 static int samsung_i2s_dai_remove(struct snd_soc_dai
*dai
)
1002 struct i2s_dai
*i2s
= snd_soc_dai_get_drvdata(dai
);
1003 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
1005 if (!other
|| !other
->clk
) {
1007 if (i2s
->quirks
& QUIRK_NEED_RSTCLR
)
1008 writel(0, i2s
->addr
+ I2SCON
);
1010 clk_disable(i2s
->clk
);
1021 static struct snd_soc_dai_ops samsung_i2s_dai_ops
= {
1022 .trigger
= i2s_trigger
,
1023 .hw_params
= i2s_hw_params
,
1024 .set_fmt
= i2s_set_fmt
,
1025 .set_clkdiv
= i2s_set_clkdiv
,
1026 .set_sysclk
= i2s_set_sysclk
,
1027 .startup
= i2s_startup
,
1028 .shutdown
= i2s_shutdown
,
1032 #define SAMSUNG_I2S_RATES SNDRV_PCM_RATE_8000_96000
1034 #define SAMSUNG_I2S_FMTS (SNDRV_PCM_FMTBIT_S8 | \
1035 SNDRV_PCM_FMTBIT_S16_LE | \
1036 SNDRV_PCM_FMTBIT_S24_LE)
1039 struct i2s_dai
*i2s_alloc_dai(struct platform_device
*pdev
, bool sec
)
1041 struct i2s_dai
*i2s
;
1043 i2s
= kzalloc(sizeof(struct i2s_dai
), GFP_KERNEL
);
1048 i2s
->pri_dai
= NULL
;
1049 i2s
->sec_dai
= NULL
;
1050 i2s
->i2s_dai_drv
.symmetric_rates
= 1;
1051 i2s
->i2s_dai_drv
.probe
= samsung_i2s_dai_probe
;
1052 i2s
->i2s_dai_drv
.remove
= samsung_i2s_dai_remove
;
1053 i2s
->i2s_dai_drv
.ops
= &samsung_i2s_dai_ops
;
1054 i2s
->i2s_dai_drv
.suspend
= i2s_suspend
;
1055 i2s
->i2s_dai_drv
.resume
= i2s_resume
;
1056 i2s
->i2s_dai_drv
.playback
.channels_min
= 2;
1057 i2s
->i2s_dai_drv
.playback
.channels_max
= 2;
1058 i2s
->i2s_dai_drv
.playback
.rates
= SAMSUNG_I2S_RATES
;
1059 i2s
->i2s_dai_drv
.playback
.formats
= SAMSUNG_I2S_FMTS
;
1062 i2s
->i2s_dai_drv
.capture
.channels_min
= 2;
1063 i2s
->i2s_dai_drv
.capture
.channels_max
= 2;
1064 i2s
->i2s_dai_drv
.capture
.rates
= SAMSUNG_I2S_RATES
;
1065 i2s
->i2s_dai_drv
.capture
.formats
= SAMSUNG_I2S_FMTS
;
1066 } else { /* Create a new platform_device for Secondary */
1067 i2s
->pdev
= platform_device_register_resndata(NULL
,
1068 pdev
->name
, pdev
->id
+ SAMSUNG_I2S_SECOFF
,
1070 if (IS_ERR(i2s
->pdev
)) {
1076 /* Pre-assign snd_soc_dai_set_drvdata */
1077 dev_set_drvdata(&i2s
->pdev
->dev
, i2s
);
1082 static __devinit
int samsung_i2s_probe(struct platform_device
*pdev
)
1084 u32 dma_pl_chan
, dma_cp_chan
, dma_pl_sec_chan
;
1085 struct i2s_dai
*pri_dai
, *sec_dai
= NULL
;
1086 struct s3c_audio_pdata
*i2s_pdata
;
1087 struct samsung_i2s
*i2s_cfg
;
1088 struct resource
*res
;
1089 u32 regs_base
, quirks
;
1092 /* Call during Seconday interface registration */
1093 if (pdev
->id
>= SAMSUNG_I2S_SECOFF
) {
1094 sec_dai
= dev_get_drvdata(&pdev
->dev
);
1095 snd_soc_register_dai(&sec_dai
->pdev
->dev
,
1096 &sec_dai
->i2s_dai_drv
);
1100 i2s_pdata
= pdev
->dev
.platform_data
;
1101 if (i2s_pdata
== NULL
) {
1102 dev_err(&pdev
->dev
, "Can't work without s3c_audio_pdata\n");
1106 res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 0);
1108 dev_err(&pdev
->dev
, "Unable to get I2S-TX dma resource\n");
1111 dma_pl_chan
= res
->start
;
1113 res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 1);
1115 dev_err(&pdev
->dev
, "Unable to get I2S-RX dma resource\n");
1118 dma_cp_chan
= res
->start
;
1120 res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 2);
1122 dma_pl_sec_chan
= res
->start
;
1124 dma_pl_sec_chan
= 0;
1126 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1128 dev_err(&pdev
->dev
, "Unable to get I2S SFR address\n");
1132 if (!request_mem_region(res
->start
, resource_size(res
),
1134 dev_err(&pdev
->dev
, "Unable to request SFR region\n");
1137 regs_base
= res
->start
;
1139 i2s_cfg
= &i2s_pdata
->type
.i2s
;
1140 quirks
= i2s_cfg
->quirks
;
1142 pri_dai
= i2s_alloc_dai(pdev
, false);
1144 dev_err(&pdev
->dev
, "Unable to alloc I2S_pri\n");
1149 pri_dai
->dma_playback
.dma_addr
= regs_base
+ I2STXD
;
1150 pri_dai
->dma_capture
.dma_addr
= regs_base
+ I2SRXD
;
1151 pri_dai
->dma_playback
.client
=
1152 (struct s3c2410_dma_client
*)&pri_dai
->dma_playback
;
1153 pri_dai
->dma_capture
.client
=
1154 (struct s3c2410_dma_client
*)&pri_dai
->dma_capture
;
1155 pri_dai
->dma_playback
.channel
= dma_pl_chan
;
1156 pri_dai
->dma_capture
.channel
= dma_cp_chan
;
1157 pri_dai
->src_clk
= i2s_cfg
->src_clk
;
1158 pri_dai
->dma_playback
.dma_size
= 4;
1159 pri_dai
->dma_capture
.dma_size
= 4;
1160 pri_dai
->base
= regs_base
;
1161 pri_dai
->quirks
= quirks
;
1163 if (quirks
& QUIRK_PRI_6CHAN
)
1164 pri_dai
->i2s_dai_drv
.playback
.channels_max
= 6;
1166 if (quirks
& QUIRK_SEC_DAI
) {
1167 sec_dai
= i2s_alloc_dai(pdev
, true);
1169 dev_err(&pdev
->dev
, "Unable to alloc I2S_sec\n");
1173 sec_dai
->dma_playback
.dma_addr
= regs_base
+ I2STXDS
;
1174 sec_dai
->dma_playback
.client
=
1175 (struct s3c2410_dma_client
*)&sec_dai
->dma_playback
;
1176 /* Use iDMA always if SysDMA not provided */
1177 sec_dai
->dma_playback
.channel
= dma_pl_sec_chan
? : -1;
1178 sec_dai
->src_clk
= i2s_cfg
->src_clk
;
1179 sec_dai
->dma_playback
.dma_size
= 4;
1180 sec_dai
->base
= regs_base
;
1181 sec_dai
->quirks
= quirks
;
1182 sec_dai
->pri_dai
= pri_dai
;
1183 pri_dai
->sec_dai
= sec_dai
;
1186 if (i2s_pdata
->cfg_gpio
&& i2s_pdata
->cfg_gpio(pdev
)) {
1187 dev_err(&pdev
->dev
, "Unable to configure gpio\n");
1192 snd_soc_register_dai(&pri_dai
->pdev
->dev
, &pri_dai
->i2s_dai_drv
);
1200 release_mem_region(regs_base
, resource_size(res
));
1205 static __devexit
int samsung_i2s_remove(struct platform_device
*pdev
)
1207 struct i2s_dai
*i2s
, *other
;
1209 i2s
= dev_get_drvdata(&pdev
->dev
);
1210 other
= i2s
->pri_dai
? : i2s
->sec_dai
;
1213 other
->pri_dai
= NULL
;
1214 other
->sec_dai
= NULL
;
1216 struct resource
*res
;
1217 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1219 release_mem_region(res
->start
, resource_size(res
));
1222 i2s
->pri_dai
= NULL
;
1223 i2s
->sec_dai
= NULL
;
1227 snd_soc_unregister_dai(&pdev
->dev
);
1232 static struct platform_driver samsung_i2s_driver
= {
1233 .probe
= samsung_i2s_probe
,
1234 .remove
= samsung_i2s_remove
,
1236 .name
= "samsung-i2s",
1237 .owner
= THIS_MODULE
,
1241 static int __init
samsung_i2s_init(void)
1243 return platform_driver_register(&samsung_i2s_driver
);
1245 module_init(samsung_i2s_init
);
1247 static void __exit
samsung_i2s_exit(void)
1249 platform_driver_unregister(&samsung_i2s_driver
);
1251 module_exit(samsung_i2s_exit
);
1253 /* Module information */
1254 MODULE_AUTHOR("Jaswinder Singh, <jassi.brar@samsung.com>");
1255 MODULE_DESCRIPTION("Samsung I2S Interface");
1256 MODULE_ALIAS("platform:samsung-i2s");
1257 MODULE_LICENSE("GPL");