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_device.h>
20 #include <linux/pm_runtime.h>
22 #include <sound/soc.h>
23 #include <sound/pcm_params.h>
25 #include <plat/audio.h>
26 #include <plat/dma-pl330.h>
32 #include "codec_plugin.h"
34 #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
37 /* Platform device for this DAI */
38 struct platform_device *pdev;
41 /* Physical base address of SFRs */
43 /* Rate of RCLK source clock */
44 unsigned long rclk_srcrate;
48 * Specifically requested RCLK,BCLK by MACHINE Driver.
49 * 0 indicates CPU driver is free to choose any value.
52 /* I2S Controller's core clock */
54 /* Clock for generating I2S signals */
56 /* Array of clock names for op_clk */
58 /* Pointer to the Primary_Fifo if this is Sec_Fifo, NULL otherwise */
59 struct i2s_dai *pri_dai;
60 /* Pointer to the Secondary_Fifo if it has one, NULL otherwise */
61 struct i2s_dai *sec_dai;
62 #define DAI_OPENED (1 << 0) /* Dai is opened */
63 #define DAI_MANAGER (1 << 1) /* Dai is the manager */
65 /* Driver for this DAI */
66 struct snd_soc_dai_driver i2s_dai_drv;
68 struct s3c_dma_params dma_playback;
69 struct s3c_dma_params dma_capture;
70 struct s3c_dma_params idma_playback;
71 struct audio_codec_plugin *plugin;
78 /* Lock for cross i/f checks */
79 static DEFINE_SPINLOCK(lock);
81 /* If this is the 'overlay' stereo DAI */
82 static inline bool is_secondary(struct i2s_dai *i2s)
84 return i2s->pri_dai ? true : false;
87 /* If operating in SoC-Slave mode */
88 static inline bool is_slave(struct i2s_dai *i2s)
90 return (readl(i2s->addr + I2SMOD) & MOD_SLAVE) ? true : false;
93 /* If this interface of the controller is transmitting data */
94 static inline bool tx_active(struct i2s_dai *i2s)
101 active = readl(i2s->addr + I2SCON);
103 if (is_secondary(i2s))
104 active &= CON_TXSDMA_ACTIVE;
106 active &= CON_TXDMA_ACTIVE;
108 return active ? true : false;
111 /* If the other interface of the controller is transmitting data */
112 static inline bool other_tx_active(struct i2s_dai *i2s)
114 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
116 return tx_active(other);
119 /* If any interface of the controller is transmitting data */
120 static inline bool any_tx_active(struct i2s_dai *i2s)
122 return tx_active(i2s) || other_tx_active(i2s);
125 /* If this interface of the controller is receiving data */
126 static inline bool rx_active(struct i2s_dai *i2s)
133 active = readl(i2s->addr + I2SCON) & CON_RXDMA_ACTIVE;
135 return active ? true : false;
138 /* If the other interface of the controller is receiving data */
139 static inline bool other_rx_active(struct i2s_dai *i2s)
141 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
143 return rx_active(other);
146 /* If any interface of the controller is receiving data */
147 static inline bool any_rx_active(struct i2s_dai *i2s)
149 return rx_active(i2s) || other_rx_active(i2s);
152 /* If the other DAI is transmitting or receiving data */
153 static inline bool other_active(struct i2s_dai *i2s)
155 return other_rx_active(i2s) || other_tx_active(i2s);
158 /* If this DAI is transmitting or receiving data */
159 static inline bool this_active(struct i2s_dai *i2s)
161 return tx_active(i2s) || rx_active(i2s);
164 /* If the controller is active anyway */
165 static inline bool any_active(struct i2s_dai *i2s)
167 return this_active(i2s) || other_active(i2s);
170 static inline struct i2s_dai *to_info(struct snd_soc_dai *dai)
172 return snd_soc_dai_get_drvdata(dai);
175 static inline bool is_opened(struct i2s_dai *i2s)
177 if (i2s && (i2s->mode & DAI_OPENED))
183 static inline bool is_manager(struct i2s_dai *i2s)
185 if (is_opened(i2s) && (i2s->mode & DAI_MANAGER))
191 /* Read RCLK of I2S (in multiples of LRCLK) */
192 static inline unsigned get_rfs(struct i2s_dai *i2s)
194 u32 rfs = (readl(i2s->addr + I2SMOD) >> 3) & 0x3;
204 /* Write RCLK of I2S (in multiples of LRCLK) */
205 static inline void set_rfs(struct i2s_dai *i2s, unsigned rfs)
207 u32 mod = readl(i2s->addr + I2SMOD);
209 mod &= ~MOD_RCLK_MASK;
213 mod |= MOD_RCLK_768FS;
216 mod |= MOD_RCLK_512FS;
219 mod |= MOD_RCLK_384FS;
222 mod |= MOD_RCLK_256FS;
226 writel(mod, i2s->addr + I2SMOD);
229 /* Read Bit-Clock of I2S (in multiples of LRCLK) */
230 static inline unsigned get_bfs(struct i2s_dai *i2s)
232 u32 bfs = (readl(i2s->addr + I2SMOD) >> 1) & 0x3;
242 /* Write Bit-Clock of I2S (in multiples of LRCLK) */
243 static inline void set_bfs(struct i2s_dai *i2s, unsigned bfs)
245 u32 mod = readl(i2s->addr + I2SMOD);
247 mod &= ~MOD_BCLK_MASK;
251 mod |= MOD_BCLK_48FS;
254 mod |= MOD_BCLK_32FS;
257 mod |= MOD_BCLK_24FS;
260 mod |= MOD_BCLK_16FS;
263 dev_err(&i2s->pdev->dev, "Wrong BCLK Divider!\n");
267 writel(mod, i2s->addr + I2SMOD);
271 static inline int get_blc(struct i2s_dai *i2s)
273 int blc = readl(i2s->addr + I2SMOD);
275 blc = (blc >> 13) & 0x3;
284 static int plugin_init(struct i2s_dai *i2s)
286 const char *plugin_name;
287 struct device_node *plugin_node = NULL;
288 struct platform_device *pdev;
289 struct audio_codec_plugin *plugin;
291 if (!of_property_read_string_index(i2s->pdev->dev.of_node,
292 "codec-plugin", 0, &plugin_name)) {
294 plugin_node = of_find_node_by_name(NULL, plugin_name);
298 pdev = of_find_device_by_node(plugin_node);
302 plugin = dev_get_drvdata(&pdev->dev);
307 dev_err(&i2s->pdev->dev, "plugin already intialised.\n");
308 i2s->plugin = plugin;
315 static void plugin_hw_params(struct snd_pcm_substream *substream,
316 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
318 struct i2s_dai *i2s = to_info(dai);
323 if (!i2s->plugin->ops.hw_params)
326 i2s->plugin->ops.hw_params(
327 i2s->plugin->dev, substream, params, dai);
331 static void plugin_trigger(struct snd_pcm_substream *substream,
332 int cmd, struct snd_soc_dai *dai)
334 struct i2s_dai *i2s = to_info(dai);
339 if (!i2s->plugin->ops.trigger)
342 i2s->plugin->ops.trigger(i2s->plugin->dev,
343 substream, cmd, dai);
347 /* TX Channel Control */
348 static void i2s_txctrl(struct i2s_dai *i2s, int on)
350 void __iomem *addr = i2s->addr;
351 u32 con = readl(addr + I2SCON);
352 u32 mod = readl(addr + I2SMOD) & ~MOD_MASK;
356 con &= ~CON_TXCH_PAUSE;
358 if (is_secondary(i2s)) {
359 con |= CON_TXSDMA_ACTIVE;
360 con &= ~CON_TXSDMA_PAUSE;
362 con |= CON_TXDMA_ACTIVE;
363 con &= ~CON_TXDMA_PAUSE;
366 if (any_rx_active(i2s))
371 if (is_secondary(i2s)) {
372 con |= CON_TXSDMA_PAUSE;
373 con &= ~CON_TXSDMA_ACTIVE;
375 con |= CON_TXDMA_PAUSE;
376 con &= ~CON_TXDMA_ACTIVE;
379 if (other_tx_active(i2s)) {
380 writel(con, addr + I2SCON);
384 con |= CON_TXCH_PAUSE;
386 if (any_rx_active(i2s))
392 writel(mod, addr + I2SMOD);
393 writel(con, addr + I2SCON);
396 /* RX Channel Control */
397 static void i2s_rxctrl(struct i2s_dai *i2s, int on)
399 void __iomem *addr = i2s->addr;
400 u32 con = readl(addr + I2SCON);
401 u32 mod = readl(addr + I2SMOD) & ~MOD_MASK;
404 con |= CON_RXDMA_ACTIVE | CON_ACTIVE;
405 con &= ~(CON_RXDMA_PAUSE | CON_RXCH_PAUSE);
407 if (any_tx_active(i2s))
412 con |= CON_RXDMA_PAUSE | CON_RXCH_PAUSE;
413 con &= ~CON_RXDMA_ACTIVE;
415 if (any_tx_active(i2s))
421 writel(mod, addr + I2SMOD);
422 writel(con, addr + I2SCON);
425 /* Flush FIFO of an interface */
426 static inline void i2s_fifo(struct i2s_dai *i2s, u32 flush)
434 if (is_secondary(i2s))
435 fic = i2s->addr + I2SFICS;
437 fic = i2s->addr + I2SFIC;
440 writel(readl(fic) | flush, fic);
443 val = msecs_to_loops(1) / 1000; /* 1 usec */
447 writel(readl(fic) & ~flush, fic);
450 static int i2s_set_sysclk(struct snd_soc_dai *dai,
451 int clk_id, unsigned int rfs, int dir)
453 struct i2s_dai *i2s = to_info(dai);
454 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
455 u32 mod = readl(i2s->addr + I2SMOD);
458 case SAMSUNG_I2S_CDCLK:
459 /* Shouldn't matter in GATING(CLOCK_IN) mode */
460 if (dir == SND_SOC_CLOCK_IN)
463 if ((rfs && other->rfs && (other->rfs != rfs)) ||
465 (((dir == SND_SOC_CLOCK_IN)
466 && !(mod & MOD_CDCLKCON)) ||
467 ((dir == SND_SOC_CLOCK_OUT)
468 && (mod & MOD_CDCLKCON))))) {
469 dev_err(&i2s->pdev->dev,
470 "%s:%d Other DAI busy\n", __func__, __LINE__);
474 if (dir == SND_SOC_CLOCK_IN)
477 mod &= ~MOD_CDCLKCON;
482 case SAMSUNG_I2S_RCLKSRC_0: /* clock corrsponding to IISMOD[10] := 0 */
483 case SAMSUNG_I2S_RCLKSRC_1: /* clock corrsponding to IISMOD[10] := 1 */
484 if ((i2s->quirks & QUIRK_NO_MUXPSR)
485 || (clk_id == SAMSUNG_I2S_RCLKSRC_0))
490 if (!any_active(i2s)) {
492 if ((clk_id && !(mod & MOD_IMS_SYSMUX)) ||
493 (!clk_id && (mod & MOD_IMS_SYSMUX))) {
494 clk_disable(i2s->op_clk);
495 clk_put(i2s->op_clk);
498 clk_get_rate(i2s->op_clk);
503 i2s->op_clk = clk_get(&i2s->pdev->dev,
504 i2s->src_clk[clk_id]);
505 clk_enable(i2s->op_clk);
506 i2s->rclk_srcrate = clk_get_rate(i2s->op_clk);
508 /* Over-ride the other's */
510 other->op_clk = i2s->op_clk;
511 other->rclk_srcrate = i2s->rclk_srcrate;
513 } else if ((!clk_id && (mod & MOD_IMS_SYSMUX))
514 || (clk_id && !(mod & MOD_IMS_SYSMUX))) {
515 dev_err(&i2s->pdev->dev,
516 "%s:%d Other DAI busy\n", __func__, __LINE__);
519 /* Call can't be on the active DAI */
520 i2s->op_clk = other->op_clk;
521 i2s->rclk_srcrate = other->rclk_srcrate;
526 mod &= ~MOD_IMS_SYSMUX;
528 mod |= MOD_IMS_SYSMUX;
532 dev_err(&i2s->pdev->dev, "We don't serve that!\n");
536 writel(mod, i2s->addr + I2SMOD);
541 static int i2s_set_fmt(struct snd_soc_dai *dai,
544 struct i2s_dai *i2s = to_info(dai);
545 u32 mod = readl(i2s->addr + I2SMOD);
548 /* Format is priority */
549 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
550 case SND_SOC_DAIFMT_RIGHT_J:
554 case SND_SOC_DAIFMT_LEFT_J:
558 case SND_SOC_DAIFMT_I2S:
562 dev_err(&i2s->pdev->dev, "Format not supported\n");
567 * INV flag is relative to the FORMAT flag - if set it simply
568 * flips the polarity specified by the Standard
570 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
571 case SND_SOC_DAIFMT_NB_NF:
573 case SND_SOC_DAIFMT_NB_IF:
574 if (tmp & MOD_LR_RLOW)
580 dev_err(&i2s->pdev->dev, "Polarity not supported\n");
584 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
585 case SND_SOC_DAIFMT_CBM_CFM:
588 case SND_SOC_DAIFMT_CBS_CFS:
589 /* Set default source clock in Master mode */
590 if (i2s->rclk_srcrate == 0)
591 i2s_set_sysclk(dai, SAMSUNG_I2S_RCLKSRC_0,
592 0, SND_SOC_CLOCK_IN);
595 dev_err(&i2s->pdev->dev, "master/slave format not supported\n");
599 if (any_active(i2s) &&
600 ((mod & (MOD_SDF_MASK | MOD_LR_RLOW
601 | MOD_SLAVE)) != tmp)) {
602 dev_err(&i2s->pdev->dev,
603 "%s:%d Other DAI busy\n", __func__, __LINE__);
607 mod &= ~(MOD_SDF_MASK | MOD_LR_RLOW | MOD_SLAVE);
609 writel(mod, i2s->addr + I2SMOD);
614 static int i2s_hw_params(struct snd_pcm_substream *substream,
615 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
617 struct i2s_dai *i2s = to_info(dai);
618 u32 mod = readl(i2s->addr + I2SMOD);
620 plugin_hw_params(substream, params, dai);
622 if (!is_secondary(i2s))
623 mod &= ~(MOD_DC2_EN | MOD_DC1_EN);
625 switch (params_channels(params)) {
632 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
633 i2s->dma_playback.dma_size = 4;
635 i2s->dma_capture.dma_size = 4;
638 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
639 i2s->dma_playback.dma_size = 2;
641 i2s->dma_capture.dma_size = 2;
645 dev_err(&i2s->pdev->dev, "%d channels not supported\n",
646 params_channels(params));
650 if (is_secondary(i2s))
651 mod &= ~MOD_BLCS_MASK;
653 mod &= ~MOD_BLCP_MASK;
656 mod &= ~MOD_BLC_MASK;
658 switch (params_format(params)) {
659 case SNDRV_PCM_FORMAT_S8:
660 if (is_secondary(i2s))
661 mod |= MOD_BLCS_8BIT;
663 mod |= MOD_BLCP_8BIT;
667 case SNDRV_PCM_FORMAT_S16_LE:
668 if (is_secondary(i2s))
669 mod |= MOD_BLCS_16BIT;
671 mod |= MOD_BLCP_16BIT;
673 mod |= MOD_BLC_16BIT;
675 case SNDRV_PCM_FORMAT_S24_LE:
676 if (is_secondary(i2s))
677 mod |= MOD_BLCS_24BIT;
679 mod |= MOD_BLCP_24BIT;
681 mod |= MOD_BLC_24BIT;
684 dev_err(&i2s->pdev->dev, "Format(%d) not supported\n",
685 params_format(params));
688 writel(mod, i2s->addr + I2SMOD);
690 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
691 snd_soc_dai_set_dma_data(dai, substream,
692 (void *)&i2s->dma_playback);
694 snd_soc_dai_set_dma_data(dai, substream,
695 (void *)&i2s->dma_capture);
697 i2s->frmclk = params_rate(params);
702 static void pm_runtime_ctl(struct i2s_dai *i2s, bool enabled)
704 struct platform_device *pdev = NULL;
706 pdev = is_secondary(i2s) ? i2s->pri_dai->pdev : i2s->pdev;
707 enabled ? pm_runtime_get_sync(&pdev->dev)
708 : pm_runtime_put_sync(&pdev->dev);
711 /* We set constraints on the substream acc to the version of I2S */
712 static int i2s_startup(struct snd_pcm_substream *substream,
713 struct snd_soc_dai *dai)
715 struct i2s_dai *i2s = to_info(dai);
716 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
719 pm_runtime_ctl(i2s, true);
721 /* Check not already running. */
722 if (dai->playback_active || dai->capture_active)
725 spin_lock_irqsave(&lock, flags);
727 i2s->mode |= DAI_OPENED;
729 if (is_manager(other))
730 i2s->mode &= ~DAI_MANAGER;
732 i2s->mode |= DAI_MANAGER;
734 /* Enforce set_sysclk in Master mode */
735 i2s->rclk_srcrate = 0;
737 if (!any_active(i2s) && (i2s->quirks & QUIRK_NEED_RSTCLR))
738 writel(CON_RSTCLR, i2s->addr + I2SCON);
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 /* Check not still running. */
753 if (dai->playback_active || dai->capture_active)
756 spin_lock_irqsave(&lock, flags);
758 i2s->mode &= ~DAI_OPENED;
759 i2s->mode &= ~DAI_MANAGER;
761 if (is_opened(other))
762 other->mode |= DAI_MANAGER;
764 /* Reset any constraint on RFS and BFS */
768 spin_unlock_irqrestore(&lock, flags);
770 /* Gate CDCLK by default */
771 if (!is_opened(other))
772 i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK,
773 0, SND_SOC_CLOCK_IN);
775 pm_runtime_ctl(i2s, false);
778 static int config_setup(struct i2s_dai *i2s)
780 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
781 unsigned rfs, bfs, blc;
791 /* Select least possible multiple(2) if no constraint set */
800 if ((rfs == 256 || rfs == 512) && (blc == 24)) {
801 dev_err(&i2s->pdev->dev,
802 "%d-RFS not supported for 24-blc\n", rfs);
807 if (bfs == 16 || bfs == 32)
813 /* If already setup and running */
814 if (any_active(i2s) && (get_rfs(i2s) != rfs || get_bfs(i2s) != bfs)) {
815 dev_err(&i2s->pdev->dev,
816 "%s:%d Other DAI busy\n", __func__, __LINE__);
820 /* Don't bother RFS, BFS & PSR in Slave mode */
827 if (!(i2s->quirks & QUIRK_NO_MUXPSR)) {
828 psr = i2s->rclk_srcrate / i2s->frmclk / rfs;
829 writel(((psr - 1) << 8) | PSR_PSREN, i2s->addr + I2SPSR);
830 dev_dbg(&i2s->pdev->dev,
831 "RCLK_SRC=%luHz PSR=%u, RCLK=%dfs, BCLK=%dfs\n",
832 i2s->rclk_srcrate, psr, rfs, bfs);
838 static int i2s_trigger(struct snd_pcm_substream *substream,
839 int cmd, struct snd_soc_dai *dai)
841 int capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
842 struct snd_soc_pcm_runtime *rtd = substream->private_data;
843 struct i2s_dai *i2s = to_info(rtd->cpu_dai);
846 plugin_trigger(substream, cmd, dai);
849 case SNDRV_PCM_TRIGGER_START:
850 case SNDRV_PCM_TRIGGER_RESUME:
851 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
852 local_irq_save(flags);
854 if (config_setup(i2s)) {
855 local_irq_restore(flags);
864 local_irq_restore(flags);
866 case SNDRV_PCM_TRIGGER_STOP:
867 case SNDRV_PCM_TRIGGER_SUSPEND:
868 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
869 local_irq_save(flags);
873 i2s_fifo(i2s, FIC_RXFLUSH);
876 i2s_fifo(i2s, FIC_TXFLUSH);
879 local_irq_restore(flags);
886 static int i2s_set_clkdiv(struct snd_soc_dai *dai,
889 struct i2s_dai *i2s = to_info(dai);
890 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
893 case SAMSUNG_I2S_DIV_BCLK:
894 if ((any_active(i2s) && div && (get_bfs(i2s) != div))
895 || (other && other->bfs && (other->bfs != div))) {
896 dev_err(&i2s->pdev->dev,
897 "%s:%d Other DAI busy\n", __func__, __LINE__);
903 dev_err(&i2s->pdev->dev,
904 "Invalid clock divider(%d)\n", div_id);
911 static snd_pcm_sframes_t
912 i2s_delay(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
914 struct i2s_dai *i2s = to_info(dai);
915 u32 reg = readl(i2s->addr + I2SFIC);
916 snd_pcm_sframes_t delay;
918 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
919 delay = FIC_RXCOUNT(reg);
920 else if (is_secondary(i2s))
921 delay = FICS_TXCOUNT(readl(i2s->addr + I2SFICS));
923 delay = FIC_TXCOUNT(reg);
929 static int i2s_suspend(struct snd_soc_dai *dai)
931 struct i2s_dai *i2s = to_info(dai);
934 i2s->suspend_i2smod = readl(i2s->addr + I2SMOD);
935 i2s->suspend_i2scon = readl(i2s->addr + I2SCON);
936 i2s->suspend_i2spsr = readl(i2s->addr + I2SPSR);
942 static int i2s_resume(struct snd_soc_dai *dai)
944 struct i2s_dai *i2s = to_info(dai);
947 writel(i2s->suspend_i2scon, i2s->addr + I2SCON);
948 writel(i2s->suspend_i2smod, i2s->addr + I2SMOD);
949 writel(i2s->suspend_i2spsr, i2s->addr + I2SPSR);
955 #define i2s_suspend NULL
956 #define i2s_resume NULL
959 #ifdef CONFIG_PM_RUNTIME
960 static int i2s_runtime_suspend(struct device *dev)
962 struct i2s_dai *i2s = dev_get_drvdata(dev);
964 clk_disable(i2s->clk);
969 static int i2s_runtime_resume(struct device *dev)
971 struct i2s_dai *i2s = dev_get_drvdata(dev);
973 clk_enable(i2s->clk);
977 #endif /* CONFIG_PM_RUNTIME */
979 static int samsung_i2s_dai_probe(struct snd_soc_dai *dai)
981 struct i2s_dai *i2s = to_info(dai);
982 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
984 if (other && other->clk) /* If this is probe on secondary */
987 i2s->addr = ioremap(i2s->base, 0x100);
988 if (i2s->addr == NULL) {
989 dev_err(&i2s->pdev->dev, "cannot ioremap registers\n");
993 i2s->clk = clk_get(&i2s->pdev->dev, "iis");
994 if (IS_ERR(i2s->clk)) {
995 dev_err(&i2s->pdev->dev, "failed to get i2s_clock\n");
999 clk_enable(i2s->clk);
1002 other->addr = i2s->addr;
1003 other->clk = i2s->clk;
1006 if (i2s->quirks & QUIRK_NEED_RSTCLR)
1007 writel(CON_RSTCLR, i2s->addr + I2SCON);
1009 if (i2s->quirks & QUIRK_SEC_DAI)
1010 idma_reg_addr_init(i2s->addr,
1011 i2s->sec_dai->idma_playback.dma_addr);
1014 /* Reset any constraint on RFS and BFS */
1019 i2s_fifo(i2s, FIC_TXFLUSH);
1020 i2s_fifo(other, FIC_TXFLUSH);
1021 i2s_fifo(i2s, FIC_RXFLUSH);
1023 /* Gate CDCLK by default */
1024 if (!is_opened(other))
1025 i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK,
1026 0, SND_SOC_CLOCK_IN);
1028 clk_disable(i2s->clk);
1032 static int samsung_i2s_dai_remove(struct snd_soc_dai *dai)
1034 struct i2s_dai *i2s = snd_soc_dai_get_drvdata(dai);
1035 struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
1037 if (!other || !other->clk) {
1039 if (i2s->quirks & QUIRK_NEED_RSTCLR)
1040 writel(0, i2s->addr + I2SCON);
1042 clk_disable(i2s->clk);
1053 static const struct snd_soc_dai_ops samsung_i2s_dai_ops = {
1054 .trigger = i2s_trigger,
1055 .hw_params = i2s_hw_params,
1056 .set_fmt = i2s_set_fmt,
1057 .set_clkdiv = i2s_set_clkdiv,
1058 .set_sysclk = i2s_set_sysclk,
1059 .startup = i2s_startup,
1060 .shutdown = i2s_shutdown,
1064 #define SAMSUNG_I2S_RATES SNDRV_PCM_RATE_8000_96000
1066 #define SAMSUNG_I2S_FMTS (SNDRV_PCM_FMTBIT_S8 | \
1067 SNDRV_PCM_FMTBIT_S16_LE | \
1068 SNDRV_PCM_FMTBIT_S24_LE)
1071 struct i2s_dai *i2s_alloc_dai(struct platform_device *pdev, bool sec)
1073 struct i2s_dai *i2s;
1076 i2s = devm_kzalloc(&pdev->dev, sizeof(struct i2s_dai), GFP_KERNEL);
1081 i2s->pri_dai = NULL;
1082 i2s->sec_dai = NULL;
1083 i2s->i2s_dai_drv.symmetric_rates = 1;
1084 i2s->i2s_dai_drv.probe = samsung_i2s_dai_probe;
1085 i2s->i2s_dai_drv.remove = samsung_i2s_dai_remove;
1086 i2s->i2s_dai_drv.ops = &samsung_i2s_dai_ops;
1087 i2s->i2s_dai_drv.suspend = i2s_suspend;
1088 i2s->i2s_dai_drv.resume = i2s_resume;
1089 i2s->i2s_dai_drv.playback.channels_min = 2;
1090 i2s->i2s_dai_drv.playback.channels_max = 2;
1091 i2s->i2s_dai_drv.playback.rates = SAMSUNG_I2S_RATES;
1092 i2s->i2s_dai_drv.playback.formats = SAMSUNG_I2S_FMTS;
1095 i2s->i2s_dai_drv.capture.channels_min = 1;
1096 i2s->i2s_dai_drv.capture.channels_max = 2;
1097 i2s->i2s_dai_drv.capture.rates = SAMSUNG_I2S_RATES;
1098 i2s->i2s_dai_drv.capture.formats = SAMSUNG_I2S_FMTS;
1099 } else { /* Create a new platform_device for Secondary */
1100 if (pdev->dev.of_node) {
1101 id = of_alias_get_id(pdev->dev.of_node, "i2s");
1104 "failed to get alias id:%d\n", id);
1109 i2s->pdev = platform_device_register_resndata(NULL,
1110 "samsung-i2s", id + SAMSUNG_I2S_SECOFF,
1112 if (IS_ERR(i2s->pdev))
1116 /* Pre-assign snd_soc_dai_set_drvdata */
1117 dev_set_drvdata(&i2s->pdev->dev, i2s);
1122 static __devinit int samsung_i2s_probe(struct platform_device *pdev)
1124 u32 dma_pl_chan, dma_cp_chan;
1125 u32 dma_pl_sec_chan = 0;
1126 struct i2s_dai *pri_dai, *sec_dai = NULL;
1127 struct s3c_audio_pdata *i2s_pdata;
1128 struct samsung_i2s *i2s_cfg;
1129 struct resource *res;
1130 u32 regs_base, quirks;
1131 struct property *prop;
1134 /* Call during Seconday interface registration */
1135 if (pdev->dev.of_node) {
1136 id = of_alias_get_id(pdev->dev.of_node, "i2s");
1138 dev_err(&pdev->dev, "failed to get alias id:%d\n", id);
1143 if (id >= SAMSUNG_I2S_SECOFF) {
1144 sec_dai = dev_get_drvdata(&pdev->dev);
1145 snd_soc_register_dai(&sec_dai->pdev->dev,
1146 &sec_dai->i2s_dai_drv);
1150 i2s_pdata = pdev->dev.platform_data;
1151 if (i2s_pdata == NULL) {
1152 dev_err(&pdev->dev, "Can't work without s3c_audio_pdata\n");
1156 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1158 dev_err(&pdev->dev, "Unable to get I2S SFR address\n");
1162 if (!request_mem_region(res->start, resource_size(res),
1164 dev_err(&pdev->dev, "Unable to request SFR region\n");
1167 regs_base = res->start;
1169 i2s_cfg = &i2s_pdata->type.i2s;
1170 quirks = i2s_cfg->quirks;
1172 pri_dai = i2s_alloc_dai(pdev, false);
1174 dev_err(&pdev->dev, "Unable to alloc I2S_pri\n");
1179 if (!pdev->dev.of_node) {
1180 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1183 "Unable to get I2S-TX dma resource\n");
1186 dma_pl_chan = res->start;
1188 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1191 "Unable to get I2S-RX dma resource\n");
1194 dma_cp_chan = res->start;
1196 res = platform_get_resource(pdev, IORESOURCE_DMA, 2);
1198 dma_pl_sec_chan = res->start;
1200 prop = of_find_property(pdev->dev.of_node,
1201 "tx-dma-channel", NULL);
1202 dma_pl_chan = DMACH_DT_PROP;
1203 pri_dai->dma_playback.dma_prop = prop;
1205 prop = of_find_property(pdev->dev.of_node,
1206 "rx-dma-channel", NULL);
1207 dma_cp_chan = DMACH_DT_PROP;
1208 pri_dai->dma_capture.dma_prop = prop;
1210 plugin_init(pri_dai);
1213 pri_dai->dma_playback.dma_addr = regs_base + I2STXD;
1214 pri_dai->dma_capture.dma_addr = regs_base + I2SRXD;
1215 pri_dai->dma_playback.client =
1216 (struct s3c2410_dma_client *)&pri_dai->dma_playback;
1217 pri_dai->dma_capture.client =
1218 (struct s3c2410_dma_client *)&pri_dai->dma_capture;
1219 pri_dai->dma_playback.channel = dma_pl_chan;
1220 pri_dai->dma_capture.channel = dma_cp_chan;
1221 pri_dai->src_clk = i2s_cfg->src_clk;
1222 pri_dai->dma_playback.dma_size = 4;
1223 pri_dai->dma_capture.dma_size = 4;
1224 pri_dai->base = regs_base;
1225 pri_dai->quirks = quirks;
1227 if (quirks & QUIRK_PRI_6CHAN)
1228 pri_dai->i2s_dai_drv.playback.channels_max = 6;
1230 if (quirks & QUIRK_SEC_DAI) {
1231 sec_dai = i2s_alloc_dai(pdev, true);
1233 dev_err(&pdev->dev, "Unable to alloc I2S_sec\n");
1237 sec_dai->dma_playback.dma_addr = regs_base + I2STXDS;
1238 sec_dai->dma_playback.client =
1239 (struct s3c2410_dma_client *)&sec_dai->dma_playback;
1241 if (pdev->dev.of_node) {
1242 prop = of_find_property(pdev->dev.of_node,
1243 "tx-dma-channel-secondary",
1245 sec_dai->dma_playback.dma_prop = prop;
1248 /* Use iDMA always if SysDMA not provided */
1249 sec_dai->dma_playback.channel = dma_pl_sec_chan ? : -1;
1250 sec_dai->src_clk = i2s_cfg->src_clk;
1251 sec_dai->dma_playback.dma_size = 4;
1252 sec_dai->base = regs_base;
1253 sec_dai->quirks = quirks;
1254 sec_dai->idma_playback.dma_addr = i2s_cfg->idma_addr;
1255 sec_dai->pri_dai = pri_dai;
1256 pri_dai->sec_dai = sec_dai;
1259 if (i2s_pdata->cfg_gpio && i2s_pdata->cfg_gpio(pdev)) {
1260 dev_err(&pdev->dev, "Unable to configure gpio\n");
1265 snd_soc_register_dai(&pri_dai->pdev->dev, &pri_dai->i2s_dai_drv);
1267 pm_runtime_enable(&pdev->dev);
1271 release_mem_region(regs_base, resource_size(res));
1276 static __devexit int samsung_i2s_remove(struct platform_device *pdev)
1278 struct i2s_dai *i2s, *other;
1279 struct resource *res;
1281 i2s = dev_get_drvdata(&pdev->dev);
1282 other = i2s->pri_dai ? : i2s->sec_dai;
1285 other->pri_dai = NULL;
1286 other->sec_dai = NULL;
1288 pm_runtime_disable(&pdev->dev);
1289 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1291 release_mem_region(res->start, resource_size(res));
1294 i2s->pri_dai = NULL;
1295 i2s->sec_dai = NULL;
1297 snd_soc_unregister_dai(&pdev->dev);
1303 static const struct of_device_id exynos_i2s_match[] = {
1304 { .compatible = "samsung,i2s" },
1307 MODULE_DEVICE_TABLE(of, exynos_i2s_match);
1310 static const struct dev_pm_ops samsung_i2s_pm = {
1311 SET_RUNTIME_PM_OPS(i2s_runtime_suspend,
1312 i2s_runtime_resume, NULL)
1315 static struct platform_driver samsung_i2s_driver = {
1316 .probe = samsung_i2s_probe,
1317 .remove = __devexit_p(samsung_i2s_remove),
1319 .name = "samsung-i2s",
1320 .owner = THIS_MODULE,
1321 .pm = &samsung_i2s_pm,
1322 .of_match_table = of_match_ptr(exynos_i2s_match),
1326 module_platform_driver(samsung_i2s_driver);
1328 /* Module information */
1329 MODULE_AUTHOR("Jaswinder Singh, <jassisinghbrar@gmail.com>");
1330 MODULE_DESCRIPTION("Samsung I2S Interface");
1331 MODULE_ALIAS("platform:samsung-i2s");
1332 MODULE_VERSION("1:1.0");
1333 MODULE_LICENSE("GPL");