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>
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_dai_data
{
46 /* Platform device for this DAI */
47 struct platform_device
*pdev
;
50 /* Physical base address of SFRs */
52 /* Rate of RCLK source clock */
53 unsigned long rclk_srcrate
;
57 * Specifically requested RCLK,BCLK by MACHINE Driver.
58 * 0 indicates CPU driver is free to choose any value.
61 /* I2S Controller's core clock */
63 /* Clock for generating I2S signals */
65 /* Pointer to the Primary_Fifo if this is Sec_Fifo, NULL otherwise */
66 struct i2s_dai
*pri_dai
;
67 /* Pointer to the Secondary_Fifo if it has one, NULL otherwise */
68 struct i2s_dai
*sec_dai
;
69 #define DAI_OPENED (1 << 0) /* Dai is opened */
70 #define DAI_MANAGER (1 << 1) /* Dai is the manager */
72 /* Driver for this DAI */
73 struct snd_soc_dai_driver i2s_dai_drv
;
75 struct s3c_dma_params dma_playback
;
76 struct s3c_dma_params dma_capture
;
77 struct s3c_dma_params idma_playback
;
82 unsigned long gpios
[7]; /* i2s gpio line numbers */
85 /* Lock for cross i/f checks */
86 static DEFINE_SPINLOCK(lock
);
88 /* If this is the 'overlay' stereo DAI */
89 static inline bool is_secondary(struct i2s_dai
*i2s
)
91 return i2s
->pri_dai
? true : false;
94 /* If operating in SoC-Slave mode */
95 static inline bool is_slave(struct i2s_dai
*i2s
)
97 return (readl(i2s
->addr
+ I2SMOD
) & MOD_SLAVE
) ? true : false;
100 /* If this interface of the controller is transmitting data */
101 static inline bool tx_active(struct i2s_dai
*i2s
)
108 active
= readl(i2s
->addr
+ I2SCON
);
110 if (is_secondary(i2s
))
111 active
&= CON_TXSDMA_ACTIVE
;
113 active
&= CON_TXDMA_ACTIVE
;
115 return active
? true : false;
118 /* If the other interface of the controller is transmitting data */
119 static inline bool other_tx_active(struct i2s_dai
*i2s
)
121 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
123 return tx_active(other
);
126 /* If any interface of the controller is transmitting data */
127 static inline bool any_tx_active(struct i2s_dai
*i2s
)
129 return tx_active(i2s
) || other_tx_active(i2s
);
132 /* If this interface of the controller is receiving data */
133 static inline bool rx_active(struct i2s_dai
*i2s
)
140 active
= readl(i2s
->addr
+ I2SCON
) & CON_RXDMA_ACTIVE
;
142 return active
? true : false;
145 /* If the other interface of the controller is receiving data */
146 static inline bool other_rx_active(struct i2s_dai
*i2s
)
148 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
150 return rx_active(other
);
153 /* If any interface of the controller is receiving data */
154 static inline bool any_rx_active(struct i2s_dai
*i2s
)
156 return rx_active(i2s
) || other_rx_active(i2s
);
159 /* If the other DAI is transmitting or receiving data */
160 static inline bool other_active(struct i2s_dai
*i2s
)
162 return other_rx_active(i2s
) || other_tx_active(i2s
);
165 /* If this DAI is transmitting or receiving data */
166 static inline bool this_active(struct i2s_dai
*i2s
)
168 return tx_active(i2s
) || rx_active(i2s
);
171 /* If the controller is active anyway */
172 static inline bool any_active(struct i2s_dai
*i2s
)
174 return this_active(i2s
) || other_active(i2s
);
177 static inline struct i2s_dai
*to_info(struct snd_soc_dai
*dai
)
179 return snd_soc_dai_get_drvdata(dai
);
182 static inline bool is_opened(struct i2s_dai
*i2s
)
184 if (i2s
&& (i2s
->mode
& DAI_OPENED
))
190 static inline bool is_manager(struct i2s_dai
*i2s
)
192 if (is_opened(i2s
) && (i2s
->mode
& DAI_MANAGER
))
198 /* Read RCLK of I2S (in multiples of LRCLK) */
199 static inline unsigned get_rfs(struct i2s_dai
*i2s
)
201 u32 rfs
= (readl(i2s
->addr
+ I2SMOD
) >> 3) & 0x3;
211 /* Write RCLK of I2S (in multiples of LRCLK) */
212 static inline void set_rfs(struct i2s_dai
*i2s
, unsigned rfs
)
214 u32 mod
= readl(i2s
->addr
+ I2SMOD
);
216 mod
&= ~MOD_RCLK_MASK
;
220 mod
|= MOD_RCLK_768FS
;
223 mod
|= MOD_RCLK_512FS
;
226 mod
|= MOD_RCLK_384FS
;
229 mod
|= MOD_RCLK_256FS
;
233 writel(mod
, i2s
->addr
+ I2SMOD
);
236 /* Read Bit-Clock of I2S (in multiples of LRCLK) */
237 static inline unsigned get_bfs(struct i2s_dai
*i2s
)
239 u32 bfs
= (readl(i2s
->addr
+ I2SMOD
) >> 1) & 0x3;
249 /* Write Bit-Clock of I2S (in multiples of LRCLK) */
250 static inline void set_bfs(struct i2s_dai
*i2s
, unsigned bfs
)
252 u32 mod
= readl(i2s
->addr
+ I2SMOD
);
254 mod
&= ~MOD_BCLK_MASK
;
258 mod
|= MOD_BCLK_48FS
;
261 mod
|= MOD_BCLK_32FS
;
264 mod
|= MOD_BCLK_24FS
;
267 mod
|= MOD_BCLK_16FS
;
270 dev_err(&i2s
->pdev
->dev
, "Wrong BCLK Divider!\n");
274 writel(mod
, i2s
->addr
+ I2SMOD
);
278 static inline int get_blc(struct i2s_dai
*i2s
)
280 int blc
= readl(i2s
->addr
+ I2SMOD
);
282 blc
= (blc
>> 13) & 0x3;
291 /* TX Channel Control */
292 static void i2s_txctrl(struct i2s_dai
*i2s
, int on
)
294 void __iomem
*addr
= i2s
->addr
;
295 u32 con
= readl(addr
+ I2SCON
);
296 u32 mod
= readl(addr
+ I2SMOD
) & ~MOD_MASK
;
300 con
&= ~CON_TXCH_PAUSE
;
302 if (is_secondary(i2s
)) {
303 con
|= CON_TXSDMA_ACTIVE
;
304 con
&= ~CON_TXSDMA_PAUSE
;
306 con
|= CON_TXDMA_ACTIVE
;
307 con
&= ~CON_TXDMA_PAUSE
;
310 if (any_rx_active(i2s
))
315 if (is_secondary(i2s
)) {
316 con
|= CON_TXSDMA_PAUSE
;
317 con
&= ~CON_TXSDMA_ACTIVE
;
319 con
|= CON_TXDMA_PAUSE
;
320 con
&= ~CON_TXDMA_ACTIVE
;
323 if (other_tx_active(i2s
)) {
324 writel(con
, addr
+ I2SCON
);
328 con
|= CON_TXCH_PAUSE
;
330 if (any_rx_active(i2s
))
336 writel(mod
, addr
+ I2SMOD
);
337 writel(con
, addr
+ I2SCON
);
340 /* RX Channel Control */
341 static void i2s_rxctrl(struct i2s_dai
*i2s
, int on
)
343 void __iomem
*addr
= i2s
->addr
;
344 u32 con
= readl(addr
+ I2SCON
);
345 u32 mod
= readl(addr
+ I2SMOD
) & ~MOD_MASK
;
348 con
|= CON_RXDMA_ACTIVE
| CON_ACTIVE
;
349 con
&= ~(CON_RXDMA_PAUSE
| CON_RXCH_PAUSE
);
351 if (any_tx_active(i2s
))
356 con
|= CON_RXDMA_PAUSE
| CON_RXCH_PAUSE
;
357 con
&= ~CON_RXDMA_ACTIVE
;
359 if (any_tx_active(i2s
))
365 writel(mod
, addr
+ I2SMOD
);
366 writel(con
, addr
+ I2SCON
);
369 /* Flush FIFO of an interface */
370 static inline void i2s_fifo(struct i2s_dai
*i2s
, u32 flush
)
378 if (is_secondary(i2s
))
379 fic
= i2s
->addr
+ I2SFICS
;
381 fic
= i2s
->addr
+ I2SFIC
;
384 writel(readl(fic
) | flush
, fic
);
387 val
= msecs_to_loops(1) / 1000; /* 1 usec */
391 writel(readl(fic
) & ~flush
, fic
);
394 static int i2s_set_sysclk(struct snd_soc_dai
*dai
,
395 int clk_id
, unsigned int rfs
, int dir
)
397 struct i2s_dai
*i2s
= to_info(dai
);
398 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
399 u32 mod
= readl(i2s
->addr
+ I2SMOD
);
402 case SAMSUNG_I2S_CDCLK
:
403 /* Shouldn't matter in GATING(CLOCK_IN) mode */
404 if (dir
== SND_SOC_CLOCK_IN
)
407 if ((rfs
&& other
->rfs
&& (other
->rfs
!= rfs
)) ||
409 (((dir
== SND_SOC_CLOCK_IN
)
410 && !(mod
& MOD_CDCLKCON
)) ||
411 ((dir
== SND_SOC_CLOCK_OUT
)
412 && (mod
& MOD_CDCLKCON
))))) {
413 dev_err(&i2s
->pdev
->dev
,
414 "%s:%d Other DAI busy\n", __func__
, __LINE__
);
418 if (dir
== SND_SOC_CLOCK_IN
)
421 mod
&= ~MOD_CDCLKCON
;
426 case SAMSUNG_I2S_RCLKSRC_0
: /* clock corrsponding to IISMOD[10] := 0 */
427 case SAMSUNG_I2S_RCLKSRC_1
: /* clock corrsponding to IISMOD[10] := 1 */
428 if ((i2s
->quirks
& QUIRK_NO_MUXPSR
)
429 || (clk_id
== SAMSUNG_I2S_RCLKSRC_0
))
434 if (!any_active(i2s
)) {
436 if ((clk_id
&& !(mod
& MOD_IMS_SYSMUX
)) ||
437 (!clk_id
&& (mod
& MOD_IMS_SYSMUX
))) {
438 clk_disable_unprepare(i2s
->op_clk
);
439 clk_put(i2s
->op_clk
);
442 clk_get_rate(i2s
->op_clk
);
448 i2s
->op_clk
= clk_get(&i2s
->pdev
->dev
,
451 i2s
->op_clk
= clk_get(&i2s
->pdev
->dev
,
453 clk_prepare_enable(i2s
->op_clk
);
454 i2s
->rclk_srcrate
= clk_get_rate(i2s
->op_clk
);
456 /* Over-ride the other's */
458 other
->op_clk
= i2s
->op_clk
;
459 other
->rclk_srcrate
= i2s
->rclk_srcrate
;
461 } else if ((!clk_id
&& (mod
& MOD_IMS_SYSMUX
))
462 || (clk_id
&& !(mod
& MOD_IMS_SYSMUX
))) {
463 dev_err(&i2s
->pdev
->dev
,
464 "%s:%d Other DAI busy\n", __func__
, __LINE__
);
467 /* Call can't be on the active DAI */
468 i2s
->op_clk
= other
->op_clk
;
469 i2s
->rclk_srcrate
= other
->rclk_srcrate
;
474 mod
&= ~MOD_IMS_SYSMUX
;
476 mod
|= MOD_IMS_SYSMUX
;
480 dev_err(&i2s
->pdev
->dev
, "We don't serve that!\n");
484 writel(mod
, i2s
->addr
+ I2SMOD
);
489 static int i2s_set_fmt(struct snd_soc_dai
*dai
,
492 struct i2s_dai
*i2s
= to_info(dai
);
493 u32 mod
= readl(i2s
->addr
+ I2SMOD
);
496 /* Format is priority */
497 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
498 case SND_SOC_DAIFMT_RIGHT_J
:
502 case SND_SOC_DAIFMT_LEFT_J
:
506 case SND_SOC_DAIFMT_I2S
:
510 dev_err(&i2s
->pdev
->dev
, "Format not supported\n");
515 * INV flag is relative to the FORMAT flag - if set it simply
516 * flips the polarity specified by the Standard
518 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
519 case SND_SOC_DAIFMT_NB_NF
:
521 case SND_SOC_DAIFMT_NB_IF
:
522 if (tmp
& MOD_LR_RLOW
)
528 dev_err(&i2s
->pdev
->dev
, "Polarity not supported\n");
532 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
533 case SND_SOC_DAIFMT_CBM_CFM
:
536 case SND_SOC_DAIFMT_CBS_CFS
:
537 /* Set default source clock in Master mode */
538 if (i2s
->rclk_srcrate
== 0)
539 i2s_set_sysclk(dai
, SAMSUNG_I2S_RCLKSRC_0
,
540 0, SND_SOC_CLOCK_IN
);
543 dev_err(&i2s
->pdev
->dev
, "master/slave format not supported\n");
547 if (any_active(i2s
) &&
548 ((mod
& (MOD_SDF_MASK
| MOD_LR_RLOW
549 | MOD_SLAVE
)) != tmp
)) {
550 dev_err(&i2s
->pdev
->dev
,
551 "%s:%d Other DAI busy\n", __func__
, __LINE__
);
555 mod
&= ~(MOD_SDF_MASK
| MOD_LR_RLOW
| MOD_SLAVE
);
557 writel(mod
, i2s
->addr
+ I2SMOD
);
562 static int i2s_hw_params(struct snd_pcm_substream
*substream
,
563 struct snd_pcm_hw_params
*params
, struct snd_soc_dai
*dai
)
565 struct i2s_dai
*i2s
= to_info(dai
);
566 u32 mod
= readl(i2s
->addr
+ I2SMOD
);
568 if (!is_secondary(i2s
))
569 mod
&= ~(MOD_DC2_EN
| MOD_DC1_EN
);
571 switch (params_channels(params
)) {
578 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
579 i2s
->dma_playback
.dma_size
= 4;
581 i2s
->dma_capture
.dma_size
= 4;
584 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
585 i2s
->dma_playback
.dma_size
= 2;
587 i2s
->dma_capture
.dma_size
= 2;
591 dev_err(&i2s
->pdev
->dev
, "%d channels not supported\n",
592 params_channels(params
));
596 if (is_secondary(i2s
))
597 mod
&= ~MOD_BLCS_MASK
;
599 mod
&= ~MOD_BLCP_MASK
;
602 mod
&= ~MOD_BLC_MASK
;
604 switch (params_format(params
)) {
605 case SNDRV_PCM_FORMAT_S8
:
606 if (is_secondary(i2s
))
607 mod
|= MOD_BLCS_8BIT
;
609 mod
|= MOD_BLCP_8BIT
;
613 case SNDRV_PCM_FORMAT_S16_LE
:
614 if (is_secondary(i2s
))
615 mod
|= MOD_BLCS_16BIT
;
617 mod
|= MOD_BLCP_16BIT
;
619 mod
|= MOD_BLC_16BIT
;
621 case SNDRV_PCM_FORMAT_S24_LE
:
622 if (is_secondary(i2s
))
623 mod
|= MOD_BLCS_24BIT
;
625 mod
|= MOD_BLCP_24BIT
;
627 mod
|= MOD_BLC_24BIT
;
630 dev_err(&i2s
->pdev
->dev
, "Format(%d) not supported\n",
631 params_format(params
));
634 writel(mod
, i2s
->addr
+ I2SMOD
);
636 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
637 snd_soc_dai_set_dma_data(dai
, substream
,
638 (void *)&i2s
->dma_playback
);
640 snd_soc_dai_set_dma_data(dai
, substream
,
641 (void *)&i2s
->dma_capture
);
643 i2s
->frmclk
= params_rate(params
);
648 /* We set constraints on the substream acc to the version of I2S */
649 static int i2s_startup(struct snd_pcm_substream
*substream
,
650 struct snd_soc_dai
*dai
)
652 struct i2s_dai
*i2s
= to_info(dai
);
653 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
656 spin_lock_irqsave(&lock
, flags
);
658 i2s
->mode
|= DAI_OPENED
;
660 if (is_manager(other
))
661 i2s
->mode
&= ~DAI_MANAGER
;
663 i2s
->mode
|= DAI_MANAGER
;
665 /* Enforce set_sysclk in Master mode */
666 i2s
->rclk_srcrate
= 0;
668 if (!any_active(i2s
) && (i2s
->quirks
& QUIRK_NEED_RSTCLR
))
669 writel(CON_RSTCLR
, i2s
->addr
+ I2SCON
);
671 spin_unlock_irqrestore(&lock
, flags
);
676 static void i2s_shutdown(struct snd_pcm_substream
*substream
,
677 struct snd_soc_dai
*dai
)
679 struct i2s_dai
*i2s
= to_info(dai
);
680 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
683 spin_lock_irqsave(&lock
, flags
);
685 i2s
->mode
&= ~DAI_OPENED
;
686 i2s
->mode
&= ~DAI_MANAGER
;
688 if (is_opened(other
))
689 other
->mode
|= DAI_MANAGER
;
691 /* Reset any constraint on RFS and BFS */
695 spin_unlock_irqrestore(&lock
, flags
);
697 /* Gate CDCLK by default */
698 if (!is_opened(other
))
699 i2s_set_sysclk(dai
, SAMSUNG_I2S_CDCLK
,
700 0, SND_SOC_CLOCK_IN
);
703 static int config_setup(struct i2s_dai
*i2s
)
705 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
706 unsigned rfs
, bfs
, blc
;
716 /* Select least possible multiple(2) if no constraint set */
725 if ((rfs
== 256 || rfs
== 512) && (blc
== 24)) {
726 dev_err(&i2s
->pdev
->dev
,
727 "%d-RFS not supported for 24-blc\n", rfs
);
732 if (bfs
== 16 || bfs
== 32)
738 /* If already setup and running */
739 if (any_active(i2s
) && (get_rfs(i2s
) != rfs
|| get_bfs(i2s
) != bfs
)) {
740 dev_err(&i2s
->pdev
->dev
,
741 "%s:%d Other DAI busy\n", __func__
, __LINE__
);
748 /* Don't bother with PSR in Slave mode */
752 if (!(i2s
->quirks
& QUIRK_NO_MUXPSR
)) {
753 psr
= i2s
->rclk_srcrate
/ i2s
->frmclk
/ rfs
;
754 writel(((psr
- 1) << 8) | PSR_PSREN
, i2s
->addr
+ I2SPSR
);
755 dev_dbg(&i2s
->pdev
->dev
,
756 "RCLK_SRC=%luHz PSR=%u, RCLK=%dfs, BCLK=%dfs\n",
757 i2s
->rclk_srcrate
, psr
, rfs
, bfs
);
763 static int i2s_trigger(struct snd_pcm_substream
*substream
,
764 int cmd
, struct snd_soc_dai
*dai
)
766 int capture
= (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
);
767 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
768 struct i2s_dai
*i2s
= to_info(rtd
->cpu_dai
);
772 case SNDRV_PCM_TRIGGER_START
:
773 case SNDRV_PCM_TRIGGER_RESUME
:
774 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
775 local_irq_save(flags
);
777 if (config_setup(i2s
)) {
778 local_irq_restore(flags
);
787 local_irq_restore(flags
);
789 case SNDRV_PCM_TRIGGER_STOP
:
790 case SNDRV_PCM_TRIGGER_SUSPEND
:
791 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
792 local_irq_save(flags
);
796 i2s_fifo(i2s
, FIC_RXFLUSH
);
799 i2s_fifo(i2s
, FIC_TXFLUSH
);
802 local_irq_restore(flags
);
809 static int i2s_set_clkdiv(struct snd_soc_dai
*dai
,
812 struct i2s_dai
*i2s
= to_info(dai
);
813 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
816 case SAMSUNG_I2S_DIV_BCLK
:
817 if ((any_active(i2s
) && div
&& (get_bfs(i2s
) != div
))
818 || (other
&& other
->bfs
&& (other
->bfs
!= div
))) {
819 dev_err(&i2s
->pdev
->dev
,
820 "%s:%d Other DAI busy\n", __func__
, __LINE__
);
826 dev_err(&i2s
->pdev
->dev
,
827 "Invalid clock divider(%d)\n", div_id
);
834 static snd_pcm_sframes_t
835 i2s_delay(struct snd_pcm_substream
*substream
, struct snd_soc_dai
*dai
)
837 struct i2s_dai
*i2s
= to_info(dai
);
838 u32 reg
= readl(i2s
->addr
+ I2SFIC
);
839 snd_pcm_sframes_t delay
;
841 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
842 delay
= FIC_RXCOUNT(reg
);
843 else if (is_secondary(i2s
))
844 delay
= FICS_TXCOUNT(readl(i2s
->addr
+ I2SFICS
));
846 delay
= FIC_TXCOUNT(reg
);
852 static int i2s_suspend(struct snd_soc_dai
*dai
)
854 struct i2s_dai
*i2s
= to_info(dai
);
857 i2s
->suspend_i2smod
= readl(i2s
->addr
+ I2SMOD
);
858 i2s
->suspend_i2scon
= readl(i2s
->addr
+ I2SCON
);
859 i2s
->suspend_i2spsr
= readl(i2s
->addr
+ I2SPSR
);
865 static int i2s_resume(struct snd_soc_dai
*dai
)
867 struct i2s_dai
*i2s
= to_info(dai
);
870 writel(i2s
->suspend_i2scon
, i2s
->addr
+ I2SCON
);
871 writel(i2s
->suspend_i2smod
, i2s
->addr
+ I2SMOD
);
872 writel(i2s
->suspend_i2spsr
, i2s
->addr
+ I2SPSR
);
878 #define i2s_suspend NULL
879 #define i2s_resume NULL
882 static int samsung_i2s_dai_probe(struct snd_soc_dai
*dai
)
884 struct i2s_dai
*i2s
= to_info(dai
);
885 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
887 if (other
&& other
->clk
) /* If this is probe on secondary */
890 i2s
->addr
= ioremap(i2s
->base
, 0x100);
891 if (i2s
->addr
== NULL
) {
892 dev_err(&i2s
->pdev
->dev
, "cannot ioremap registers\n");
896 i2s
->clk
= clk_get(&i2s
->pdev
->dev
, "iis");
897 if (IS_ERR(i2s
->clk
)) {
898 dev_err(&i2s
->pdev
->dev
, "failed to get i2s_clock\n");
902 clk_prepare_enable(i2s
->clk
);
905 other
->addr
= i2s
->addr
;
906 other
->clk
= i2s
->clk
;
909 if (i2s
->quirks
& QUIRK_NEED_RSTCLR
)
910 writel(CON_RSTCLR
, i2s
->addr
+ I2SCON
);
912 if (i2s
->quirks
& QUIRK_SEC_DAI
)
913 idma_reg_addr_init(i2s
->addr
,
914 i2s
->sec_dai
->idma_playback
.dma_addr
);
917 /* Reset any constraint on RFS and BFS */
922 i2s_fifo(i2s
, FIC_TXFLUSH
);
923 i2s_fifo(other
, FIC_TXFLUSH
);
924 i2s_fifo(i2s
, FIC_RXFLUSH
);
926 /* Gate CDCLK by default */
927 if (!is_opened(other
))
928 i2s_set_sysclk(dai
, SAMSUNG_I2S_CDCLK
,
929 0, SND_SOC_CLOCK_IN
);
934 static int samsung_i2s_dai_remove(struct snd_soc_dai
*dai
)
936 struct i2s_dai
*i2s
= snd_soc_dai_get_drvdata(dai
);
937 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
939 if (!other
|| !other
->clk
) {
941 if (i2s
->quirks
& QUIRK_NEED_RSTCLR
)
942 writel(0, i2s
->addr
+ I2SCON
);
944 clk_disable_unprepare(i2s
->clk
);
955 static const struct snd_soc_dai_ops samsung_i2s_dai_ops
= {
956 .trigger
= i2s_trigger
,
957 .hw_params
= i2s_hw_params
,
958 .set_fmt
= i2s_set_fmt
,
959 .set_clkdiv
= i2s_set_clkdiv
,
960 .set_sysclk
= i2s_set_sysclk
,
961 .startup
= i2s_startup
,
962 .shutdown
= i2s_shutdown
,
966 static const struct snd_soc_component_driver samsung_i2s_component
= {
967 .name
= "samsung-i2s",
970 #define SAMSUNG_I2S_RATES SNDRV_PCM_RATE_8000_96000
972 #define SAMSUNG_I2S_FMTS (SNDRV_PCM_FMTBIT_S8 | \
973 SNDRV_PCM_FMTBIT_S16_LE | \
974 SNDRV_PCM_FMTBIT_S24_LE)
976 static struct i2s_dai
*i2s_alloc_dai(struct platform_device
*pdev
, bool sec
)
981 i2s
= devm_kzalloc(&pdev
->dev
, sizeof(struct i2s_dai
), GFP_KERNEL
);
988 i2s
->i2s_dai_drv
.symmetric_rates
= 1;
989 i2s
->i2s_dai_drv
.probe
= samsung_i2s_dai_probe
;
990 i2s
->i2s_dai_drv
.remove
= samsung_i2s_dai_remove
;
991 i2s
->i2s_dai_drv
.ops
= &samsung_i2s_dai_ops
;
992 i2s
->i2s_dai_drv
.suspend
= i2s_suspend
;
993 i2s
->i2s_dai_drv
.resume
= i2s_resume
;
994 i2s
->i2s_dai_drv
.playback
.channels_min
= 2;
995 i2s
->i2s_dai_drv
.playback
.channels_max
= 2;
996 i2s
->i2s_dai_drv
.playback
.rates
= SAMSUNG_I2S_RATES
;
997 i2s
->i2s_dai_drv
.playback
.formats
= SAMSUNG_I2S_FMTS
;
1000 i2s
->i2s_dai_drv
.capture
.channels_min
= 1;
1001 i2s
->i2s_dai_drv
.capture
.channels_max
= 2;
1002 i2s
->i2s_dai_drv
.capture
.rates
= SAMSUNG_I2S_RATES
;
1003 i2s
->i2s_dai_drv
.capture
.formats
= SAMSUNG_I2S_FMTS
;
1004 dev_set_drvdata(&i2s
->pdev
->dev
, i2s
);
1005 } else { /* Create a new platform_device for Secondary */
1006 i2s
->pdev
= platform_device_alloc("samsung-i2s-sec", -1);
1007 if (IS_ERR(i2s
->pdev
))
1010 platform_set_drvdata(i2s
->pdev
, i2s
);
1011 ret
= platform_device_add(i2s
->pdev
);
1019 static const struct of_device_id exynos_i2s_match
[];
1021 static inline int samsung_i2s_get_driver_data(struct platform_device
*pdev
)
1024 struct samsung_i2s_dai_data
*data
;
1025 if (pdev
->dev
.of_node
) {
1026 const struct of_device_id
*match
;
1027 match
= of_match_node(exynos_i2s_match
, pdev
->dev
.of_node
);
1028 data
= (struct samsung_i2s_dai_data
*) match
->data
;
1029 return data
->dai_type
;
1032 return platform_get_device_id(pdev
)->driver_data
;
1035 #ifdef CONFIG_PM_RUNTIME
1036 static int i2s_runtime_suspend(struct device
*dev
)
1038 struct i2s_dai
*i2s
= dev_get_drvdata(dev
);
1040 clk_disable_unprepare(i2s
->clk
);
1045 static int i2s_runtime_resume(struct device
*dev
)
1047 struct i2s_dai
*i2s
= dev_get_drvdata(dev
);
1049 clk_prepare_enable(i2s
->clk
);
1053 #endif /* CONFIG_PM_RUNTIME */
1055 static int samsung_i2s_probe(struct platform_device
*pdev
)
1057 struct i2s_dai
*pri_dai
, *sec_dai
= NULL
;
1058 struct s3c_audio_pdata
*i2s_pdata
= pdev
->dev
.platform_data
;
1059 struct samsung_i2s
*i2s_cfg
= NULL
;
1060 struct resource
*res
;
1061 u32 regs_base
, quirks
= 0, idma_addr
= 0;
1062 struct device_node
*np
= pdev
->dev
.of_node
;
1063 enum samsung_dai_type samsung_dai_type
;
1066 /* Call during Seconday interface registration */
1067 samsung_dai_type
= samsung_i2s_get_driver_data(pdev
);
1069 if (samsung_dai_type
== TYPE_SEC
) {
1070 sec_dai
= dev_get_drvdata(&pdev
->dev
);
1072 dev_err(&pdev
->dev
, "Unable to get drvdata\n");
1075 snd_soc_register_component(&sec_dai
->pdev
->dev
,
1076 &samsung_i2s_component
,
1077 &sec_dai
->i2s_dai_drv
, 1);
1078 asoc_dma_platform_register(&pdev
->dev
);
1082 pri_dai
= i2s_alloc_dai(pdev
, false);
1084 dev_err(&pdev
->dev
, "Unable to alloc I2S_pri\n");
1089 res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 0);
1092 "Unable to get I2S-TX dma resource\n");
1095 pri_dai
->dma_playback
.channel
= res
->start
;
1097 res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 1);
1100 "Unable to get I2S-RX dma resource\n");
1103 pri_dai
->dma_capture
.channel
= res
->start
;
1105 if (i2s_pdata
== NULL
) {
1106 dev_err(&pdev
->dev
, "Can't work without s3c_audio_pdata\n");
1110 if (&i2s_pdata
->type
)
1111 i2s_cfg
= &i2s_pdata
->type
.i2s
;
1114 quirks
= i2s_cfg
->quirks
;
1115 idma_addr
= i2s_cfg
->idma_addr
;
1118 if (of_find_property(np
, "samsung,supports-6ch", NULL
))
1119 quirks
|= QUIRK_PRI_6CHAN
;
1121 if (of_find_property(np
, "samsung,supports-secdai", NULL
))
1122 quirks
|= QUIRK_SEC_DAI
;
1124 if (of_find_property(np
, "samsung,supports-rstclr", NULL
))
1125 quirks
|= QUIRK_NEED_RSTCLR
;
1127 if (of_property_read_u32(np
, "samsung,idma-addr",
1129 if (quirks
& QUIRK_SEC_DAI
) {
1130 dev_err(&pdev
->dev
, "idma address is not"\
1137 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1139 dev_err(&pdev
->dev
, "Unable to get I2S SFR address\n");
1143 if (!request_mem_region(res
->start
, resource_size(res
),
1145 dev_err(&pdev
->dev
, "Unable to request SFR region\n");
1148 regs_base
= res
->start
;
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_playback
.ch_name
= "tx";
1155 pri_dai
->dma_capture
.client
=
1156 (struct s3c2410_dma_client
*)&pri_dai
->dma_capture
;
1157 pri_dai
->dma_capture
.ch_name
= "rx";
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 sec_dai
->dma_playback
.ch_name
= "tx-sec";
1179 res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 2);
1181 sec_dai
->dma_playback
.channel
= res
->start
;
1184 sec_dai
->dma_playback
.dma_size
= 4;
1185 sec_dai
->base
= regs_base
;
1186 sec_dai
->quirks
= quirks
;
1187 sec_dai
->idma_playback
.dma_addr
= idma_addr
;
1188 sec_dai
->pri_dai
= pri_dai
;
1189 pri_dai
->sec_dai
= sec_dai
;
1192 if (i2s_pdata
&& i2s_pdata
->cfg_gpio
&& i2s_pdata
->cfg_gpio(pdev
)) {
1193 dev_err(&pdev
->dev
, "Unable to configure gpio\n");
1198 snd_soc_register_component(&pri_dai
->pdev
->dev
, &samsung_i2s_component
,
1199 &pri_dai
->i2s_dai_drv
, 1);
1201 pm_runtime_enable(&pdev
->dev
);
1203 asoc_dma_platform_register(&pdev
->dev
);
1207 release_mem_region(regs_base
, resource_size(res
));
1212 static int samsung_i2s_remove(struct platform_device
*pdev
)
1214 struct i2s_dai
*i2s
, *other
;
1215 struct resource
*res
;
1217 i2s
= dev_get_drvdata(&pdev
->dev
);
1218 other
= i2s
->pri_dai
? : i2s
->sec_dai
;
1221 other
->pri_dai
= NULL
;
1222 other
->sec_dai
= NULL
;
1224 pm_runtime_disable(&pdev
->dev
);
1225 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1227 release_mem_region(res
->start
, resource_size(res
));
1230 i2s
->pri_dai
= NULL
;
1231 i2s
->sec_dai
= NULL
;
1233 asoc_dma_platform_unregister(&pdev
->dev
);
1234 snd_soc_unregister_component(&pdev
->dev
);
1239 static struct platform_device_id samsung_i2s_driver_ids
[] = {
1241 .name
= "samsung-i2s",
1242 .driver_data
= TYPE_PRI
,
1244 .name
= "samsung-i2s-sec",
1245 .driver_data
= TYPE_SEC
,
1249 MODULE_DEVICE_TABLE(platform
, samsung_i2s_driver_ids
);
1252 static struct samsung_i2s_dai_data samsung_i2s_dai_data_array
[] = {
1253 [TYPE_PRI
] = { TYPE_PRI
},
1254 [TYPE_SEC
] = { TYPE_SEC
},
1257 static const struct of_device_id exynos_i2s_match
[] = {
1258 { .compatible
= "samsung,i2s-v5",
1259 .data
= &samsung_i2s_dai_data_array
[TYPE_PRI
],
1263 MODULE_DEVICE_TABLE(of
, exynos_i2s_match
);
1266 static const struct dev_pm_ops samsung_i2s_pm
= {
1267 SET_RUNTIME_PM_OPS(i2s_runtime_suspend
,
1268 i2s_runtime_resume
, NULL
)
1271 static struct platform_driver samsung_i2s_driver
= {
1272 .probe
= samsung_i2s_probe
,
1273 .remove
= samsung_i2s_remove
,
1274 .id_table
= samsung_i2s_driver_ids
,
1276 .name
= "samsung-i2s",
1277 .owner
= THIS_MODULE
,
1278 .of_match_table
= of_match_ptr(exynos_i2s_match
),
1279 .pm
= &samsung_i2s_pm
,
1283 module_platform_driver(samsung_i2s_driver
);
1285 /* Module information */
1286 MODULE_AUTHOR("Jaswinder Singh, <jassisinghbrar@gmail.com>");
1287 MODULE_DESCRIPTION("Samsung I2S Interface");
1288 MODULE_ALIAS("platform:samsung-i2s");
1289 MODULE_LICENSE("GPL");