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>
18 #include <linux/pm_runtime.h>
20 #include <sound/soc.h>
21 #include <sound/pcm_params.h>
23 #include <linux/platform_data/asoc-s3c.h>
30 #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
32 enum samsung_dai_type
{
38 /* Platform device for this DAI */
39 struct platform_device
*pdev
;
42 /* Physical base address of SFRs */
44 /* Rate of RCLK source clock */
45 unsigned long rclk_srcrate
;
49 * Specifically requested RCLK,BCLK by MACHINE Driver.
50 * 0 indicates CPU driver is free to choose any value.
53 /* I2S Controller's core clock */
55 /* Clock for generating I2S signals */
57 /* Pointer to the Primary_Fifo if this is Sec_Fifo, NULL otherwise */
58 struct i2s_dai
*pri_dai
;
59 /* Pointer to the Secondary_Fifo if it has one, NULL otherwise */
60 struct i2s_dai
*sec_dai
;
61 #define DAI_OPENED (1 << 0) /* Dai is opened */
62 #define DAI_MANAGER (1 << 1) /* Dai is the manager */
64 /* Driver for this DAI */
65 struct snd_soc_dai_driver i2s_dai_drv
;
67 struct s3c_dma_params dma_playback
;
68 struct s3c_dma_params dma_capture
;
69 struct s3c_dma_params idma_playback
;
76 /* Lock for cross i/f checks */
77 static DEFINE_SPINLOCK(lock
);
79 /* If this is the 'overlay' stereo DAI */
80 static inline bool is_secondary(struct i2s_dai
*i2s
)
82 return i2s
->pri_dai
? true : false;
85 /* If operating in SoC-Slave mode */
86 static inline bool is_slave(struct i2s_dai
*i2s
)
88 return (readl(i2s
->addr
+ I2SMOD
) & MOD_SLAVE
) ? true : false;
91 /* If this interface of the controller is transmitting data */
92 static inline bool tx_active(struct i2s_dai
*i2s
)
99 active
= readl(i2s
->addr
+ I2SCON
);
101 if (is_secondary(i2s
))
102 active
&= CON_TXSDMA_ACTIVE
;
104 active
&= CON_TXDMA_ACTIVE
;
106 return active
? true : false;
109 /* If the other interface of the controller is transmitting data */
110 static inline bool other_tx_active(struct i2s_dai
*i2s
)
112 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
114 return tx_active(other
);
117 /* If any interface of the controller is transmitting data */
118 static inline bool any_tx_active(struct i2s_dai
*i2s
)
120 return tx_active(i2s
) || other_tx_active(i2s
);
123 /* If this interface of the controller is receiving data */
124 static inline bool rx_active(struct i2s_dai
*i2s
)
131 active
= readl(i2s
->addr
+ I2SCON
) & CON_RXDMA_ACTIVE
;
133 return active
? true : false;
136 /* If the other interface of the controller is receiving data */
137 static inline bool other_rx_active(struct i2s_dai
*i2s
)
139 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
141 return rx_active(other
);
144 /* If any interface of the controller is receiving data */
145 static inline bool any_rx_active(struct i2s_dai
*i2s
)
147 return rx_active(i2s
) || other_rx_active(i2s
);
150 /* If the other DAI is transmitting or receiving data */
151 static inline bool other_active(struct i2s_dai
*i2s
)
153 return other_rx_active(i2s
) || other_tx_active(i2s
);
156 /* If this DAI is transmitting or receiving data */
157 static inline bool this_active(struct i2s_dai
*i2s
)
159 return tx_active(i2s
) || rx_active(i2s
);
162 /* If the controller is active anyway */
163 static inline bool any_active(struct i2s_dai
*i2s
)
165 return this_active(i2s
) || other_active(i2s
);
168 static inline struct i2s_dai
*to_info(struct snd_soc_dai
*dai
)
170 return snd_soc_dai_get_drvdata(dai
);
173 static inline bool is_opened(struct i2s_dai
*i2s
)
175 if (i2s
&& (i2s
->mode
& DAI_OPENED
))
181 static inline bool is_manager(struct i2s_dai
*i2s
)
183 if (is_opened(i2s
) && (i2s
->mode
& DAI_MANAGER
))
189 /* Read RCLK of I2S (in multiples of LRCLK) */
190 static inline unsigned get_rfs(struct i2s_dai
*i2s
)
192 u32 rfs
= (readl(i2s
->addr
+ I2SMOD
) >> 3) & 0x3;
202 /* Write RCLK of I2S (in multiples of LRCLK) */
203 static inline void set_rfs(struct i2s_dai
*i2s
, unsigned rfs
)
205 u32 mod
= readl(i2s
->addr
+ I2SMOD
);
207 mod
&= ~MOD_RCLK_MASK
;
211 mod
|= MOD_RCLK_768FS
;
214 mod
|= MOD_RCLK_512FS
;
217 mod
|= MOD_RCLK_384FS
;
220 mod
|= MOD_RCLK_256FS
;
224 writel(mod
, i2s
->addr
+ I2SMOD
);
227 /* Read Bit-Clock of I2S (in multiples of LRCLK) */
228 static inline unsigned get_bfs(struct i2s_dai
*i2s
)
230 u32 bfs
= (readl(i2s
->addr
+ I2SMOD
) >> 1) & 0x3;
240 /* Write Bit-Clock of I2S (in multiples of LRCLK) */
241 static inline void set_bfs(struct i2s_dai
*i2s
, unsigned bfs
)
243 u32 mod
= readl(i2s
->addr
+ I2SMOD
);
245 mod
&= ~MOD_BCLK_MASK
;
249 mod
|= MOD_BCLK_48FS
;
252 mod
|= MOD_BCLK_32FS
;
255 mod
|= MOD_BCLK_24FS
;
258 mod
|= MOD_BCLK_16FS
;
261 dev_err(&i2s
->pdev
->dev
, "Wrong BCLK Divider!\n");
265 writel(mod
, i2s
->addr
+ I2SMOD
);
269 static inline int get_blc(struct i2s_dai
*i2s
)
271 int blc
= readl(i2s
->addr
+ I2SMOD
);
273 blc
= (blc
>> 13) & 0x3;
282 /* TX Channel Control */
283 static void i2s_txctrl(struct i2s_dai
*i2s
, int on
)
285 void __iomem
*addr
= i2s
->addr
;
286 u32 con
= readl(addr
+ I2SCON
);
287 u32 mod
= readl(addr
+ I2SMOD
) & ~MOD_MASK
;
291 con
&= ~CON_TXCH_PAUSE
;
293 if (is_secondary(i2s
)) {
294 con
|= CON_TXSDMA_ACTIVE
;
295 con
&= ~CON_TXSDMA_PAUSE
;
297 con
|= CON_TXDMA_ACTIVE
;
298 con
&= ~CON_TXDMA_PAUSE
;
301 if (any_rx_active(i2s
))
306 if (is_secondary(i2s
)) {
307 con
|= CON_TXSDMA_PAUSE
;
308 con
&= ~CON_TXSDMA_ACTIVE
;
310 con
|= CON_TXDMA_PAUSE
;
311 con
&= ~CON_TXDMA_ACTIVE
;
314 if (other_tx_active(i2s
)) {
315 writel(con
, addr
+ I2SCON
);
319 con
|= CON_TXCH_PAUSE
;
321 if (any_rx_active(i2s
))
327 writel(mod
, addr
+ I2SMOD
);
328 writel(con
, addr
+ I2SCON
);
331 /* RX Channel Control */
332 static void i2s_rxctrl(struct i2s_dai
*i2s
, int on
)
334 void __iomem
*addr
= i2s
->addr
;
335 u32 con
= readl(addr
+ I2SCON
);
336 u32 mod
= readl(addr
+ I2SMOD
) & ~MOD_MASK
;
339 con
|= CON_RXDMA_ACTIVE
| CON_ACTIVE
;
340 con
&= ~(CON_RXDMA_PAUSE
| CON_RXCH_PAUSE
);
342 if (any_tx_active(i2s
))
347 con
|= CON_RXDMA_PAUSE
| CON_RXCH_PAUSE
;
348 con
&= ~CON_RXDMA_ACTIVE
;
350 if (any_tx_active(i2s
))
356 writel(mod
, addr
+ I2SMOD
);
357 writel(con
, addr
+ I2SCON
);
360 /* Flush FIFO of an interface */
361 static inline void i2s_fifo(struct i2s_dai
*i2s
, u32 flush
)
369 if (is_secondary(i2s
))
370 fic
= i2s
->addr
+ I2SFICS
;
372 fic
= i2s
->addr
+ I2SFIC
;
375 writel(readl(fic
) | flush
, fic
);
378 val
= msecs_to_loops(1) / 1000; /* 1 usec */
382 writel(readl(fic
) & ~flush
, fic
);
385 static int i2s_set_sysclk(struct snd_soc_dai
*dai
,
386 int clk_id
, unsigned int rfs
, int dir
)
388 struct i2s_dai
*i2s
= to_info(dai
);
389 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
390 u32 mod
= readl(i2s
->addr
+ I2SMOD
);
393 case SAMSUNG_I2S_CDCLK
:
394 /* Shouldn't matter in GATING(CLOCK_IN) mode */
395 if (dir
== SND_SOC_CLOCK_IN
)
398 if ((rfs
&& other
->rfs
&& (other
->rfs
!= rfs
)) ||
400 (((dir
== SND_SOC_CLOCK_IN
)
401 && !(mod
& MOD_CDCLKCON
)) ||
402 ((dir
== SND_SOC_CLOCK_OUT
)
403 && (mod
& MOD_CDCLKCON
))))) {
404 dev_err(&i2s
->pdev
->dev
,
405 "%s:%d Other DAI busy\n", __func__
, __LINE__
);
409 if (dir
== SND_SOC_CLOCK_IN
)
412 mod
&= ~MOD_CDCLKCON
;
417 case SAMSUNG_I2S_RCLKSRC_0
: /* clock corrsponding to IISMOD[10] := 0 */
418 case SAMSUNG_I2S_RCLKSRC_1
: /* clock corrsponding to IISMOD[10] := 1 */
419 if ((i2s
->quirks
& QUIRK_NO_MUXPSR
)
420 || (clk_id
== SAMSUNG_I2S_RCLKSRC_0
))
425 if (!any_active(i2s
)) {
427 if ((clk_id
&& !(mod
& MOD_IMS_SYSMUX
)) ||
428 (!clk_id
&& (mod
& MOD_IMS_SYSMUX
))) {
429 clk_disable_unprepare(i2s
->op_clk
);
430 clk_put(i2s
->op_clk
);
433 clk_get_rate(i2s
->op_clk
);
439 i2s
->op_clk
= clk_get(&i2s
->pdev
->dev
,
442 i2s
->op_clk
= clk_get(&i2s
->pdev
->dev
,
444 clk_prepare_enable(i2s
->op_clk
);
445 i2s
->rclk_srcrate
= clk_get_rate(i2s
->op_clk
);
447 /* Over-ride the other's */
449 other
->op_clk
= i2s
->op_clk
;
450 other
->rclk_srcrate
= i2s
->rclk_srcrate
;
452 } else if ((!clk_id
&& (mod
& MOD_IMS_SYSMUX
))
453 || (clk_id
&& !(mod
& MOD_IMS_SYSMUX
))) {
454 dev_err(&i2s
->pdev
->dev
,
455 "%s:%d Other DAI busy\n", __func__
, __LINE__
);
458 /* Call can't be on the active DAI */
459 i2s
->op_clk
= other
->op_clk
;
460 i2s
->rclk_srcrate
= other
->rclk_srcrate
;
465 mod
&= ~MOD_IMS_SYSMUX
;
467 mod
|= MOD_IMS_SYSMUX
;
471 dev_err(&i2s
->pdev
->dev
, "We don't serve that!\n");
475 writel(mod
, i2s
->addr
+ I2SMOD
);
480 static int i2s_set_fmt(struct snd_soc_dai
*dai
,
483 struct i2s_dai
*i2s
= to_info(dai
);
484 u32 mod
= readl(i2s
->addr
+ I2SMOD
);
487 /* Format is priority */
488 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
489 case SND_SOC_DAIFMT_RIGHT_J
:
493 case SND_SOC_DAIFMT_LEFT_J
:
497 case SND_SOC_DAIFMT_I2S
:
501 dev_err(&i2s
->pdev
->dev
, "Format not supported\n");
506 * INV flag is relative to the FORMAT flag - if set it simply
507 * flips the polarity specified by the Standard
509 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
510 case SND_SOC_DAIFMT_NB_NF
:
512 case SND_SOC_DAIFMT_NB_IF
:
513 if (tmp
& MOD_LR_RLOW
)
519 dev_err(&i2s
->pdev
->dev
, "Polarity not supported\n");
523 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
524 case SND_SOC_DAIFMT_CBM_CFM
:
527 case SND_SOC_DAIFMT_CBS_CFS
:
528 /* Set default source clock in Master mode */
529 if (i2s
->rclk_srcrate
== 0)
530 i2s_set_sysclk(dai
, SAMSUNG_I2S_RCLKSRC_0
,
531 0, SND_SOC_CLOCK_IN
);
534 dev_err(&i2s
->pdev
->dev
, "master/slave format not supported\n");
538 if (any_active(i2s
) &&
539 ((mod
& (MOD_SDF_MASK
| MOD_LR_RLOW
540 | MOD_SLAVE
)) != tmp
)) {
541 dev_err(&i2s
->pdev
->dev
,
542 "%s:%d Other DAI busy\n", __func__
, __LINE__
);
546 mod
&= ~(MOD_SDF_MASK
| MOD_LR_RLOW
| MOD_SLAVE
);
548 writel(mod
, i2s
->addr
+ I2SMOD
);
553 static int i2s_hw_params(struct snd_pcm_substream
*substream
,
554 struct snd_pcm_hw_params
*params
, struct snd_soc_dai
*dai
)
556 struct i2s_dai
*i2s
= to_info(dai
);
557 u32 mod
= readl(i2s
->addr
+ I2SMOD
);
559 if (!is_secondary(i2s
))
560 mod
&= ~(MOD_DC2_EN
| MOD_DC1_EN
);
562 switch (params_channels(params
)) {
569 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
570 i2s
->dma_playback
.dma_size
= 4;
572 i2s
->dma_capture
.dma_size
= 4;
575 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
576 i2s
->dma_playback
.dma_size
= 2;
578 i2s
->dma_capture
.dma_size
= 2;
582 dev_err(&i2s
->pdev
->dev
, "%d channels not supported\n",
583 params_channels(params
));
587 if (is_secondary(i2s
))
588 mod
&= ~MOD_BLCS_MASK
;
590 mod
&= ~MOD_BLCP_MASK
;
593 mod
&= ~MOD_BLC_MASK
;
595 switch (params_format(params
)) {
596 case SNDRV_PCM_FORMAT_S8
:
597 if (is_secondary(i2s
))
598 mod
|= MOD_BLCS_8BIT
;
600 mod
|= MOD_BLCP_8BIT
;
604 case SNDRV_PCM_FORMAT_S16_LE
:
605 if (is_secondary(i2s
))
606 mod
|= MOD_BLCS_16BIT
;
608 mod
|= MOD_BLCP_16BIT
;
610 mod
|= MOD_BLC_16BIT
;
612 case SNDRV_PCM_FORMAT_S24_LE
:
613 if (is_secondary(i2s
))
614 mod
|= MOD_BLCS_24BIT
;
616 mod
|= MOD_BLCP_24BIT
;
618 mod
|= MOD_BLC_24BIT
;
621 dev_err(&i2s
->pdev
->dev
, "Format(%d) not supported\n",
622 params_format(params
));
625 writel(mod
, i2s
->addr
+ I2SMOD
);
627 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
628 snd_soc_dai_set_dma_data(dai
, substream
,
629 (void *)&i2s
->dma_playback
);
631 snd_soc_dai_set_dma_data(dai
, substream
,
632 (void *)&i2s
->dma_capture
);
634 i2s
->frmclk
= params_rate(params
);
639 /* We set constraints on the substream acc to the version of I2S */
640 static int i2s_startup(struct snd_pcm_substream
*substream
,
641 struct snd_soc_dai
*dai
)
643 struct i2s_dai
*i2s
= to_info(dai
);
644 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
647 spin_lock_irqsave(&lock
, flags
);
649 i2s
->mode
|= DAI_OPENED
;
651 if (is_manager(other
))
652 i2s
->mode
&= ~DAI_MANAGER
;
654 i2s
->mode
|= DAI_MANAGER
;
656 /* Enforce set_sysclk in Master mode */
657 i2s
->rclk_srcrate
= 0;
659 if (!any_active(i2s
) && (i2s
->quirks
& QUIRK_NEED_RSTCLR
))
660 writel(CON_RSTCLR
, i2s
->addr
+ I2SCON
);
662 spin_unlock_irqrestore(&lock
, flags
);
667 static void i2s_shutdown(struct snd_pcm_substream
*substream
,
668 struct snd_soc_dai
*dai
)
670 struct i2s_dai
*i2s
= to_info(dai
);
671 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
674 spin_lock_irqsave(&lock
, flags
);
676 i2s
->mode
&= ~DAI_OPENED
;
677 i2s
->mode
&= ~DAI_MANAGER
;
679 if (is_opened(other
))
680 other
->mode
|= DAI_MANAGER
;
682 /* Reset any constraint on RFS and BFS */
686 spin_unlock_irqrestore(&lock
, flags
);
688 /* Gate CDCLK by default */
689 if (!is_opened(other
))
690 i2s_set_sysclk(dai
, SAMSUNG_I2S_CDCLK
,
691 0, SND_SOC_CLOCK_IN
);
694 static int config_setup(struct i2s_dai
*i2s
)
696 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
697 unsigned rfs
, bfs
, blc
;
707 /* Select least possible multiple(2) if no constraint set */
716 if ((rfs
== 256 || rfs
== 512) && (blc
== 24)) {
717 dev_err(&i2s
->pdev
->dev
,
718 "%d-RFS not supported for 24-blc\n", rfs
);
723 if (bfs
== 16 || bfs
== 32)
729 /* If already setup and running */
730 if (any_active(i2s
) && (get_rfs(i2s
) != rfs
|| get_bfs(i2s
) != bfs
)) {
731 dev_err(&i2s
->pdev
->dev
,
732 "%s:%d Other DAI busy\n", __func__
, __LINE__
);
736 /* Don't bother RFS, BFS & PSR in Slave mode */
743 if (!(i2s
->quirks
& QUIRK_NO_MUXPSR
)) {
744 psr
= i2s
->rclk_srcrate
/ i2s
->frmclk
/ rfs
;
745 writel(((psr
- 1) << 8) | PSR_PSREN
, i2s
->addr
+ I2SPSR
);
746 dev_dbg(&i2s
->pdev
->dev
,
747 "RCLK_SRC=%luHz PSR=%u, RCLK=%dfs, BCLK=%dfs\n",
748 i2s
->rclk_srcrate
, psr
, rfs
, bfs
);
754 static int i2s_trigger(struct snd_pcm_substream
*substream
,
755 int cmd
, struct snd_soc_dai
*dai
)
757 int capture
= (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
);
758 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
759 struct i2s_dai
*i2s
= to_info(rtd
->cpu_dai
);
763 case SNDRV_PCM_TRIGGER_START
:
764 case SNDRV_PCM_TRIGGER_RESUME
:
765 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
766 local_irq_save(flags
);
768 if (config_setup(i2s
)) {
769 local_irq_restore(flags
);
778 local_irq_restore(flags
);
780 case SNDRV_PCM_TRIGGER_STOP
:
781 case SNDRV_PCM_TRIGGER_SUSPEND
:
782 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
783 local_irq_save(flags
);
787 i2s_fifo(i2s
, FIC_RXFLUSH
);
790 i2s_fifo(i2s
, FIC_TXFLUSH
);
793 local_irq_restore(flags
);
800 static int i2s_set_clkdiv(struct snd_soc_dai
*dai
,
803 struct i2s_dai
*i2s
= to_info(dai
);
804 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
807 case SAMSUNG_I2S_DIV_BCLK
:
808 if ((any_active(i2s
) && div
&& (get_bfs(i2s
) != div
))
809 || (other
&& other
->bfs
&& (other
->bfs
!= div
))) {
810 dev_err(&i2s
->pdev
->dev
,
811 "%s:%d Other DAI busy\n", __func__
, __LINE__
);
817 dev_err(&i2s
->pdev
->dev
,
818 "Invalid clock divider(%d)\n", div_id
);
825 static snd_pcm_sframes_t
826 i2s_delay(struct snd_pcm_substream
*substream
, struct snd_soc_dai
*dai
)
828 struct i2s_dai
*i2s
= to_info(dai
);
829 u32 reg
= readl(i2s
->addr
+ I2SFIC
);
830 snd_pcm_sframes_t delay
;
832 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
)
833 delay
= FIC_RXCOUNT(reg
);
834 else if (is_secondary(i2s
))
835 delay
= FICS_TXCOUNT(readl(i2s
->addr
+ I2SFICS
));
837 delay
= FIC_TXCOUNT(reg
);
843 static int i2s_suspend(struct snd_soc_dai
*dai
)
845 struct i2s_dai
*i2s
= to_info(dai
);
848 i2s
->suspend_i2smod
= readl(i2s
->addr
+ I2SMOD
);
849 i2s
->suspend_i2scon
= readl(i2s
->addr
+ I2SCON
);
850 i2s
->suspend_i2spsr
= readl(i2s
->addr
+ I2SPSR
);
856 static int i2s_resume(struct snd_soc_dai
*dai
)
858 struct i2s_dai
*i2s
= to_info(dai
);
861 writel(i2s
->suspend_i2scon
, i2s
->addr
+ I2SCON
);
862 writel(i2s
->suspend_i2smod
, i2s
->addr
+ I2SMOD
);
863 writel(i2s
->suspend_i2spsr
, i2s
->addr
+ I2SPSR
);
869 #define i2s_suspend NULL
870 #define i2s_resume NULL
873 static int samsung_i2s_dai_probe(struct snd_soc_dai
*dai
)
875 struct i2s_dai
*i2s
= to_info(dai
);
876 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
878 if (other
&& other
->clk
) /* If this is probe on secondary */
881 i2s
->addr
= ioremap(i2s
->base
, 0x100);
882 if (i2s
->addr
== NULL
) {
883 dev_err(&i2s
->pdev
->dev
, "cannot ioremap registers\n");
887 i2s
->clk
= clk_get(&i2s
->pdev
->dev
, "iis");
888 if (IS_ERR(i2s
->clk
)) {
889 dev_err(&i2s
->pdev
->dev
, "failed to get i2s_clock\n");
893 clk_prepare_enable(i2s
->clk
);
896 other
->addr
= i2s
->addr
;
897 other
->clk
= i2s
->clk
;
900 if (i2s
->quirks
& QUIRK_NEED_RSTCLR
)
901 writel(CON_RSTCLR
, i2s
->addr
+ I2SCON
);
903 if (i2s
->quirks
& QUIRK_SEC_DAI
)
904 idma_reg_addr_init(i2s
->addr
,
905 i2s
->sec_dai
->idma_playback
.dma_addr
);
908 /* Reset any constraint on RFS and BFS */
913 i2s_fifo(i2s
, FIC_TXFLUSH
);
914 i2s_fifo(other
, FIC_TXFLUSH
);
915 i2s_fifo(i2s
, FIC_RXFLUSH
);
917 /* Gate CDCLK by default */
918 if (!is_opened(other
))
919 i2s_set_sysclk(dai
, SAMSUNG_I2S_CDCLK
,
920 0, SND_SOC_CLOCK_IN
);
925 static int samsung_i2s_dai_remove(struct snd_soc_dai
*dai
)
927 struct i2s_dai
*i2s
= snd_soc_dai_get_drvdata(dai
);
928 struct i2s_dai
*other
= i2s
->pri_dai
? : i2s
->sec_dai
;
930 if (!other
|| !other
->clk
) {
932 if (i2s
->quirks
& QUIRK_NEED_RSTCLR
)
933 writel(0, i2s
->addr
+ I2SCON
);
935 clk_disable_unprepare(i2s
->clk
);
946 static const struct snd_soc_dai_ops samsung_i2s_dai_ops
= {
947 .trigger
= i2s_trigger
,
948 .hw_params
= i2s_hw_params
,
949 .set_fmt
= i2s_set_fmt
,
950 .set_clkdiv
= i2s_set_clkdiv
,
951 .set_sysclk
= i2s_set_sysclk
,
952 .startup
= i2s_startup
,
953 .shutdown
= i2s_shutdown
,
957 #define SAMSUNG_I2S_RATES SNDRV_PCM_RATE_8000_96000
959 #define SAMSUNG_I2S_FMTS (SNDRV_PCM_FMTBIT_S8 | \
960 SNDRV_PCM_FMTBIT_S16_LE | \
961 SNDRV_PCM_FMTBIT_S24_LE)
963 static struct i2s_dai
*i2s_alloc_dai(struct platform_device
*pdev
, bool sec
)
967 i2s
= devm_kzalloc(&pdev
->dev
, sizeof(struct i2s_dai
), GFP_KERNEL
);
974 i2s
->i2s_dai_drv
.symmetric_rates
= 1;
975 i2s
->i2s_dai_drv
.probe
= samsung_i2s_dai_probe
;
976 i2s
->i2s_dai_drv
.remove
= samsung_i2s_dai_remove
;
977 i2s
->i2s_dai_drv
.ops
= &samsung_i2s_dai_ops
;
978 i2s
->i2s_dai_drv
.suspend
= i2s_suspend
;
979 i2s
->i2s_dai_drv
.resume
= i2s_resume
;
980 i2s
->i2s_dai_drv
.playback
.channels_min
= 2;
981 i2s
->i2s_dai_drv
.playback
.channels_max
= 2;
982 i2s
->i2s_dai_drv
.playback
.rates
= SAMSUNG_I2S_RATES
;
983 i2s
->i2s_dai_drv
.playback
.formats
= SAMSUNG_I2S_FMTS
;
986 i2s
->i2s_dai_drv
.capture
.channels_min
= 1;
987 i2s
->i2s_dai_drv
.capture
.channels_max
= 2;
988 i2s
->i2s_dai_drv
.capture
.rates
= SAMSUNG_I2S_RATES
;
989 i2s
->i2s_dai_drv
.capture
.formats
= SAMSUNG_I2S_FMTS
;
990 } else { /* Create a new platform_device for Secondary */
991 i2s
->pdev
= platform_device_register_resndata(NULL
,
992 "samsung-i2s-sec", -1, NULL
, 0, NULL
, 0);
993 if (IS_ERR(i2s
->pdev
))
997 /* Pre-assign snd_soc_dai_set_drvdata */
998 dev_set_drvdata(&i2s
->pdev
->dev
, i2s
);
1003 static inline int samsung_i2s_get_driver_data(struct platform_device
*pdev
)
1005 return platform_get_device_id(pdev
)->driver_data
;
1008 static int samsung_i2s_probe(struct platform_device
*pdev
)
1010 u32 dma_pl_chan
, dma_cp_chan
, dma_pl_sec_chan
;
1011 struct i2s_dai
*pri_dai
, *sec_dai
= NULL
;
1012 struct s3c_audio_pdata
*i2s_pdata
;
1013 struct samsung_i2s
*i2s_cfg
;
1014 struct resource
*res
;
1015 u32 regs_base
, quirks
;
1016 enum samsung_dai_type samsung_dai_type
;
1019 /* Call during Seconday interface registration */
1020 samsung_dai_type
= samsung_i2s_get_driver_data(pdev
);
1022 if (samsung_dai_type
== TYPE_SEC
) {
1023 sec_dai
= dev_get_drvdata(&pdev
->dev
);
1024 snd_soc_register_dai(&sec_dai
->pdev
->dev
,
1025 &sec_dai
->i2s_dai_drv
);
1026 asoc_dma_platform_register(&pdev
->dev
);
1030 i2s_pdata
= pdev
->dev
.platform_data
;
1031 if (i2s_pdata
== NULL
) {
1032 dev_err(&pdev
->dev
, "Can't work without s3c_audio_pdata\n");
1036 res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 0);
1038 dev_err(&pdev
->dev
, "Unable to get I2S-TX dma resource\n");
1041 dma_pl_chan
= res
->start
;
1043 res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 1);
1045 dev_err(&pdev
->dev
, "Unable to get I2S-RX dma resource\n");
1048 dma_cp_chan
= res
->start
;
1050 res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 2);
1052 dma_pl_sec_chan
= res
->start
;
1054 dma_pl_sec_chan
= 0;
1056 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1058 dev_err(&pdev
->dev
, "Unable to get I2S SFR address\n");
1062 if (!request_mem_region(res
->start
, resource_size(res
),
1064 dev_err(&pdev
->dev
, "Unable to request SFR region\n");
1067 regs_base
= res
->start
;
1069 i2s_cfg
= &i2s_pdata
->type
.i2s
;
1070 quirks
= i2s_cfg
->quirks
;
1072 pri_dai
= i2s_alloc_dai(pdev
, false);
1074 dev_err(&pdev
->dev
, "Unable to alloc I2S_pri\n");
1079 pri_dai
->dma_playback
.dma_addr
= regs_base
+ I2STXD
;
1080 pri_dai
->dma_capture
.dma_addr
= regs_base
+ I2SRXD
;
1081 pri_dai
->dma_playback
.client
=
1082 (struct s3c2410_dma_client
*)&pri_dai
->dma_playback
;
1083 pri_dai
->dma_capture
.client
=
1084 (struct s3c2410_dma_client
*)&pri_dai
->dma_capture
;
1085 pri_dai
->dma_playback
.channel
= dma_pl_chan
;
1086 pri_dai
->dma_capture
.channel
= dma_cp_chan
;
1087 pri_dai
->dma_playback
.dma_size
= 4;
1088 pri_dai
->dma_capture
.dma_size
= 4;
1089 pri_dai
->base
= regs_base
;
1090 pri_dai
->quirks
= quirks
;
1092 if (quirks
& QUIRK_PRI_6CHAN
)
1093 pri_dai
->i2s_dai_drv
.playback
.channels_max
= 6;
1095 if (quirks
& QUIRK_SEC_DAI
) {
1096 sec_dai
= i2s_alloc_dai(pdev
, true);
1098 dev_err(&pdev
->dev
, "Unable to alloc I2S_sec\n");
1102 sec_dai
->dma_playback
.dma_addr
= regs_base
+ I2STXDS
;
1103 sec_dai
->dma_playback
.client
=
1104 (struct s3c2410_dma_client
*)&sec_dai
->dma_playback
;
1105 /* Use iDMA always if SysDMA not provided */
1106 sec_dai
->dma_playback
.channel
= dma_pl_sec_chan
? : -1;
1107 sec_dai
->dma_playback
.dma_size
= 4;
1108 sec_dai
->base
= regs_base
;
1109 sec_dai
->quirks
= quirks
;
1110 sec_dai
->idma_playback
.dma_addr
= i2s_cfg
->idma_addr
;
1111 sec_dai
->pri_dai
= pri_dai
;
1112 pri_dai
->sec_dai
= sec_dai
;
1115 if (i2s_pdata
->cfg_gpio
&& i2s_pdata
->cfg_gpio(pdev
)) {
1116 dev_err(&pdev
->dev
, "Unable to configure gpio\n");
1121 snd_soc_register_dai(&pri_dai
->pdev
->dev
, &pri_dai
->i2s_dai_drv
);
1123 pm_runtime_enable(&pdev
->dev
);
1125 asoc_dma_platform_register(&pdev
->dev
);
1129 release_mem_region(regs_base
, resource_size(res
));
1134 static int samsung_i2s_remove(struct platform_device
*pdev
)
1136 struct i2s_dai
*i2s
, *other
;
1137 struct resource
*res
;
1139 i2s
= dev_get_drvdata(&pdev
->dev
);
1140 other
= i2s
->pri_dai
? : i2s
->sec_dai
;
1143 other
->pri_dai
= NULL
;
1144 other
->sec_dai
= NULL
;
1146 pm_runtime_disable(&pdev
->dev
);
1147 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1149 release_mem_region(res
->start
, resource_size(res
));
1152 i2s
->pri_dai
= NULL
;
1153 i2s
->sec_dai
= NULL
;
1155 asoc_dma_platform_unregister(&pdev
->dev
);
1156 snd_soc_unregister_dai(&pdev
->dev
);
1161 static struct platform_device_id samsung_i2s_driver_ids
[] = {
1163 .name
= "samsung-i2s",
1164 .driver_data
= TYPE_PRI
,
1166 .name
= "samsung-i2s-sec",
1167 .driver_data
= TYPE_SEC
,
1171 MODULE_DEVICE_TABLE(platform
, samsung
-i2s
-driver
-ids
);
1173 static struct platform_driver samsung_i2s_driver
= {
1174 .probe
= samsung_i2s_probe
,
1175 .remove
= samsung_i2s_remove
,
1176 .id_table
= samsung_i2s_driver_ids
,
1178 .name
= "samsung-i2s",
1179 .owner
= THIS_MODULE
,
1183 module_platform_driver(samsung_i2s_driver
);
1185 /* Module information */
1186 MODULE_AUTHOR("Jaswinder Singh, <jassisinghbrar@gmail.com>");
1187 MODULE_DESCRIPTION("Samsung I2S Interface");
1188 MODULE_ALIAS("platform:samsung-i2s");
1189 MODULE_LICENSE("GPL");