xref: /linux/sound/soc/samsung/i2s.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1c1b2db4dSSylwester Nawrocki // SPDX-License-Identifier: GPL-2.0
2c1b2db4dSSylwester Nawrocki //
3c1b2db4dSSylwester Nawrocki // ALSA SoC Audio Layer - Samsung I2S Controller driver
4c1b2db4dSSylwester Nawrocki //
5c1b2db4dSSylwester Nawrocki // Copyright (c) 2010 Samsung Electronics Co. Ltd.
6c1b2db4dSSylwester Nawrocki //	Jaswinder Singh <jassisinghbrar@gmail.com>
75033f43cSJassi Brar 
8074b89bbSSylwester Nawrocki #include <dt-bindings/sound/samsung-i2s.h>
95033f43cSJassi Brar #include <linux/delay.h>
105033f43cSJassi Brar #include <linux/slab.h>
115033f43cSJassi Brar #include <linux/clk.h>
12074b89bbSSylwester Nawrocki #include <linux/clk-provider.h>
135033f43cSJassi Brar #include <linux/io.h>
14da155d5bSPaul Gortmaker #include <linux/module.h>
1540476f61SPadmavathi Venna #include <linux/of.h>
16c5cf4dbcSMark Brown #include <linux/pm_runtime.h>
175033f43cSJassi Brar 
185033f43cSJassi Brar #include <sound/soc.h>
190378b6acSSeungwhan Youn #include <sound/pcm_params.h>
205033f43cSJassi Brar 
21436d42c6SArnd Bergmann #include <linux/platform_data/asoc-s3c.h>
225033f43cSJassi Brar 
235033f43cSJassi Brar #include "dma.h"
2461100f40SSangbeom Kim #include "idma.h"
255033f43cSJassi Brar #include "i2s.h"
26172a453dSSangbeom Kim #include "i2s-regs.h"
275033f43cSJassi Brar 
285033f43cSJassi Brar #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
295033f43cSJassi Brar 
30a404b72dSSylwester Nawrocki #define SAMSUNG_I2S_ID_PRIMARY		1
31a404b72dSSylwester Nawrocki #define SAMSUNG_I2S_ID_SECONDARY	2
32a404b72dSSylwester Nawrocki 
33a5a56871SPadmavathi Venna struct samsung_i2s_variant_regs {
34a5a56871SPadmavathi Venna 	unsigned int	bfs_off;
35a5a56871SPadmavathi Venna 	unsigned int	rfs_off;
36a5a56871SPadmavathi Venna 	unsigned int	sdf_off;
37a5a56871SPadmavathi Venna 	unsigned int	txr_off;
38a5a56871SPadmavathi Venna 	unsigned int	rclksrc_off;
39a5a56871SPadmavathi Venna 	unsigned int	mss_off;
40a5a56871SPadmavathi Venna 	unsigned int	cdclkcon_off;
41a5a56871SPadmavathi Venna 	unsigned int	lrp_off;
42a5a56871SPadmavathi Venna 	unsigned int	bfs_mask;
43a5a56871SPadmavathi Venna 	unsigned int	rfs_mask;
44a5a56871SPadmavathi Venna 	unsigned int	ftx0cnt_off;
45a5a56871SPadmavathi Venna };
46a5a56871SPadmavathi Venna 
4740476f61SPadmavathi Venna struct samsung_i2s_dai_data {
487da493e9SPadmavathi Venna 	u32 quirks;
494720c2feSJaechul Lee 	unsigned int pcm_rates;
50a5a56871SPadmavathi Venna 	const struct samsung_i2s_variant_regs *i2s_variant_regs;
51bc36d761SPadmanabhan Rajanbabu 	void (*fixup_early)(struct snd_pcm_substream *substream,
52bc36d761SPadmanabhan Rajanbabu 					struct snd_soc_dai *dai);
53bc36d761SPadmanabhan Rajanbabu 	void (*fixup_late)(struct snd_pcm_substream *substream,
54bc36d761SPadmanabhan Rajanbabu 					struct snd_soc_dai *dai);
5540476f61SPadmavathi Venna };
5640476f61SPadmavathi Venna 
575033f43cSJassi Brar struct i2s_dai {
585033f43cSJassi Brar 	/* Platform device for this DAI */
595033f43cSJassi Brar 	struct platform_device *pdev;
60e2e16fa6SSylwester Nawrocki 
619f9f8a5bSSylwester Nawrocki 	/* Frame clock */
625033f43cSJassi Brar 	unsigned frmclk;
635033f43cSJassi Brar 	/*
649f9f8a5bSSylwester Nawrocki 	 * Specifically requested RCLK, BCLK by machine driver.
655033f43cSJassi Brar 	 * 0 indicates CPU driver is free to choose any value.
665033f43cSJassi Brar 	 */
675033f43cSJassi Brar 	unsigned rfs, bfs;
685033f43cSJassi Brar 	/* Pointer to the Primary_Fifo if this is Sec_Fifo, NULL otherwise */
695033f43cSJassi Brar 	struct i2s_dai *pri_dai;
705033f43cSJassi Brar 	/* Pointer to the Secondary_Fifo if it has one, NULL otherwise */
715033f43cSJassi Brar 	struct i2s_dai *sec_dai;
729f9f8a5bSSylwester Nawrocki 
739f9f8a5bSSylwester Nawrocki #define DAI_OPENED	(1 << 0) /* DAI is opened */
749f9f8a5bSSylwester Nawrocki #define DAI_MANAGER	(1 << 1) /* DAI is the manager */
755033f43cSJassi Brar 	unsigned mode;
76a404b72dSSylwester Nawrocki 
775033f43cSJassi Brar 	/* Driver for this DAI */
78a404b72dSSylwester Nawrocki 	struct snd_soc_dai_driver *drv;
79a404b72dSSylwester Nawrocki 
805033f43cSJassi Brar 	/* DMA parameters */
8169e7a69aSSylwester Nawrocki 	struct snd_dmaengine_dai_dma_data dma_playback;
8269e7a69aSSylwester Nawrocki 	struct snd_dmaengine_dai_dma_data dma_capture;
8369e7a69aSSylwester Nawrocki 	struct snd_dmaengine_dai_dma_data idma_playback;
849bdca822SArnd Bergmann 	dma_filter_fn filter;
85f3670536SSylwester Nawrocki 
8689d2e831SSylwester Nawrocki 	struct samsung_i2s_priv *priv;
875033f43cSJassi Brar };
885033f43cSJassi Brar 
89a404b72dSSylwester Nawrocki struct samsung_i2s_priv {
90a404b72dSSylwester Nawrocki 	struct platform_device *pdev;
917196c64cSSylwester Nawrocki 	struct platform_device *pdev_sec;
92a404b72dSSylwester Nawrocki 
939f9f8a5bSSylwester Nawrocki 	/* Lock for cross interface checks */
94defc67c6SSylwester Nawrocki 	spinlock_t pcm_lock;
95defc67c6SSylwester Nawrocki 
96a404b72dSSylwester Nawrocki 	/* CPU DAIs and their corresponding drivers */
97a404b72dSSylwester Nawrocki 	struct i2s_dai *dai;
98a404b72dSSylwester Nawrocki 	struct snd_soc_dai_driver *dai_drv;
99a404b72dSSylwester Nawrocki 	int num_dais;
10089d2e831SSylwester Nawrocki 
101b5d015e6SSylwester Nawrocki 	/* The I2S controller's core clock */
102b5d015e6SSylwester Nawrocki 	struct clk *clk;
103b5d015e6SSylwester Nawrocki 
1043b0fa51fSSylwester Nawrocki 	/* Clock for generating I2S signals */
1053b0fa51fSSylwester Nawrocki 	struct clk *op_clk;
1063b0fa51fSSylwester Nawrocki 
1073b0fa51fSSylwester Nawrocki 	/* Rate of RCLK source clock */
1083b0fa51fSSylwester Nawrocki 	unsigned long rclk_srcrate;
1093b0fa51fSSylwester Nawrocki 
11081bcbf2cSSylwester Nawrocki 	/* Cache of selected I2S registers for system suspend */
11181bcbf2cSSylwester Nawrocki 	u32 suspend_i2smod;
11281bcbf2cSSylwester Nawrocki 	u32 suspend_i2scon;
11381bcbf2cSSylwester Nawrocki 	u32 suspend_i2spsr;
11481bcbf2cSSylwester Nawrocki 
1155bfaeddcSSylwester Nawrocki 	const struct samsung_i2s_variant_regs *variant_regs;
116bc36d761SPadmanabhan Rajanbabu 	void (*fixup_early)(struct snd_pcm_substream *substream,
117bc36d761SPadmanabhan Rajanbabu 						struct snd_soc_dai *dai);
118bc36d761SPadmanabhan Rajanbabu 	void (*fixup_late)(struct snd_pcm_substream *substream,
119bc36d761SPadmanabhan Rajanbabu 						struct snd_soc_dai *dai);
1205944170fSSylwester Nawrocki 	u32 quirks;
1215bfaeddcSSylwester Nawrocki 
12289d2e831SSylwester Nawrocki 	/* The clock provider's data */
12389d2e831SSylwester Nawrocki 	struct clk *clk_table[3];
12489d2e831SSylwester Nawrocki 	struct clk_onecell_data clk_data;
125f29eec79SSylwester Nawrocki 
126f29eec79SSylwester Nawrocki 	/* Spinlock protecting member fields below */
127f29eec79SSylwester Nawrocki 	spinlock_t lock;
128f29eec79SSylwester Nawrocki 
129f29eec79SSylwester Nawrocki 	/* Memory mapped SFR region */
130f29eec79SSylwester Nawrocki 	void __iomem *addr;
131f29eec79SSylwester Nawrocki 
132f29eec79SSylwester Nawrocki 	/* A flag indicating the I2S slave mode operation */
133f29eec79SSylwester Nawrocki 	bool slave_mode;
134a404b72dSSylwester Nawrocki };
135a404b72dSSylwester Nawrocki 
136a404b72dSSylwester Nawrocki /* Returns true if this is the 'overlay' stereo DAI */
is_secondary(struct i2s_dai * i2s)1375033f43cSJassi Brar static inline bool is_secondary(struct i2s_dai *i2s)
1385033f43cSJassi Brar {
139a404b72dSSylwester Nawrocki 	return i2s->drv->id == SAMSUNG_I2S_ID_SECONDARY;
1405033f43cSJassi Brar }
1415033f43cSJassi Brar 
1425033f43cSJassi Brar /* If this interface of the controller is transmitting data */
tx_active(struct i2s_dai * i2s)1435033f43cSJassi Brar static inline bool tx_active(struct i2s_dai *i2s)
1445033f43cSJassi Brar {
1455033f43cSJassi Brar 	u32 active;
1465033f43cSJassi Brar 
1475033f43cSJassi Brar 	if (!i2s)
1485033f43cSJassi Brar 		return false;
1495033f43cSJassi Brar 
150e2e16fa6SSylwester Nawrocki 	active = readl(i2s->priv->addr + I2SCON);
1515033f43cSJassi Brar 
1525033f43cSJassi Brar 	if (is_secondary(i2s))
1535033f43cSJassi Brar 		active &= CON_TXSDMA_ACTIVE;
1545033f43cSJassi Brar 	else
1555033f43cSJassi Brar 		active &= CON_TXDMA_ACTIVE;
1565033f43cSJassi Brar 
1575033f43cSJassi Brar 	return active ? true : false;
1585033f43cSJassi Brar }
1595033f43cSJassi Brar 
160dcd60fc3SSylwester Nawrocki /* Return pointer to the other DAI */
get_other_dai(struct i2s_dai * i2s)161dcd60fc3SSylwester Nawrocki static inline struct i2s_dai *get_other_dai(struct i2s_dai *i2s)
162dcd60fc3SSylwester Nawrocki {
163dcd60fc3SSylwester Nawrocki 	return i2s->pri_dai ? : i2s->sec_dai;
164dcd60fc3SSylwester Nawrocki }
165dcd60fc3SSylwester Nawrocki 
1665033f43cSJassi Brar /* If the other interface of the controller is transmitting data */
other_tx_active(struct i2s_dai * i2s)1675033f43cSJassi Brar static inline bool other_tx_active(struct i2s_dai *i2s)
1685033f43cSJassi Brar {
169dcd60fc3SSylwester Nawrocki 	struct i2s_dai *other = get_other_dai(i2s);
1705033f43cSJassi Brar 
1715033f43cSJassi Brar 	return tx_active(other);
1725033f43cSJassi Brar }
1735033f43cSJassi Brar 
1745033f43cSJassi Brar /* If any interface of the controller is transmitting data */
any_tx_active(struct i2s_dai * i2s)1755033f43cSJassi Brar static inline bool any_tx_active(struct i2s_dai *i2s)
1765033f43cSJassi Brar {
1775033f43cSJassi Brar 	return tx_active(i2s) || other_tx_active(i2s);
1785033f43cSJassi Brar }
1795033f43cSJassi Brar 
1805033f43cSJassi Brar /* If this interface of the controller is receiving data */
rx_active(struct i2s_dai * i2s)1815033f43cSJassi Brar static inline bool rx_active(struct i2s_dai *i2s)
1825033f43cSJassi Brar {
1835033f43cSJassi Brar 	u32 active;
1845033f43cSJassi Brar 
1855033f43cSJassi Brar 	if (!i2s)
1865033f43cSJassi Brar 		return false;
1875033f43cSJassi Brar 
188e2e16fa6SSylwester Nawrocki 	active = readl(i2s->priv->addr + I2SCON) & CON_RXDMA_ACTIVE;
1895033f43cSJassi Brar 
1905033f43cSJassi Brar 	return active ? true : false;
1915033f43cSJassi Brar }
1925033f43cSJassi Brar 
1935033f43cSJassi Brar /* If the other interface of the controller is receiving data */
other_rx_active(struct i2s_dai * i2s)1945033f43cSJassi Brar static inline bool other_rx_active(struct i2s_dai *i2s)
1955033f43cSJassi Brar {
196dcd60fc3SSylwester Nawrocki 	struct i2s_dai *other = get_other_dai(i2s);
1975033f43cSJassi Brar 
1985033f43cSJassi Brar 	return rx_active(other);
1995033f43cSJassi Brar }
2005033f43cSJassi Brar 
2015033f43cSJassi Brar /* If any interface of the controller is receiving data */
any_rx_active(struct i2s_dai * i2s)2025033f43cSJassi Brar static inline bool any_rx_active(struct i2s_dai *i2s)
2035033f43cSJassi Brar {
2045033f43cSJassi Brar 	return rx_active(i2s) || other_rx_active(i2s);
2055033f43cSJassi Brar }
2065033f43cSJassi Brar 
2075033f43cSJassi Brar /* If the other DAI is transmitting or receiving data */
other_active(struct i2s_dai * i2s)2085033f43cSJassi Brar static inline bool other_active(struct i2s_dai *i2s)
2095033f43cSJassi Brar {
2105033f43cSJassi Brar 	return other_rx_active(i2s) || other_tx_active(i2s);
2115033f43cSJassi Brar }
2125033f43cSJassi Brar 
2135033f43cSJassi Brar /* If this DAI is transmitting or receiving data */
this_active(struct i2s_dai * i2s)2145033f43cSJassi Brar static inline bool this_active(struct i2s_dai *i2s)
2155033f43cSJassi Brar {
2165033f43cSJassi Brar 	return tx_active(i2s) || rx_active(i2s);
2175033f43cSJassi Brar }
2185033f43cSJassi Brar 
2195033f43cSJassi Brar /* If the controller is active anyway */
any_active(struct i2s_dai * i2s)2205033f43cSJassi Brar static inline bool any_active(struct i2s_dai *i2s)
2215033f43cSJassi Brar {
2225033f43cSJassi Brar 	return this_active(i2s) || other_active(i2s);
2235033f43cSJassi Brar }
2245033f43cSJassi Brar 
to_info(struct snd_soc_dai * dai)2255033f43cSJassi Brar static inline struct i2s_dai *to_info(struct snd_soc_dai *dai)
2265033f43cSJassi Brar {
227a404b72dSSylwester Nawrocki 	struct samsung_i2s_priv *priv = snd_soc_dai_get_drvdata(dai);
228a404b72dSSylwester Nawrocki 
229a404b72dSSylwester Nawrocki 	return &priv->dai[dai->id - 1];
2305033f43cSJassi Brar }
2315033f43cSJassi Brar 
is_opened(struct i2s_dai * i2s)2325033f43cSJassi Brar static inline bool is_opened(struct i2s_dai *i2s)
2335033f43cSJassi Brar {
2345033f43cSJassi Brar 	if (i2s && (i2s->mode & DAI_OPENED))
2355033f43cSJassi Brar 		return true;
2365033f43cSJassi Brar 	else
2375033f43cSJassi Brar 		return false;
2385033f43cSJassi Brar }
2395033f43cSJassi Brar 
is_manager(struct i2s_dai * i2s)2405033f43cSJassi Brar static inline bool is_manager(struct i2s_dai *i2s)
2415033f43cSJassi Brar {
2425033f43cSJassi Brar 	if (is_opened(i2s) && (i2s->mode & DAI_MANAGER))
2435033f43cSJassi Brar 		return true;
2445033f43cSJassi Brar 	else
2455033f43cSJassi Brar 		return false;
2465033f43cSJassi Brar }
2475033f43cSJassi Brar 
2485033f43cSJassi Brar /* Read RCLK of I2S (in multiples of LRCLK) */
get_rfs(struct i2s_dai * i2s)2495033f43cSJassi Brar static inline unsigned get_rfs(struct i2s_dai *i2s)
2505033f43cSJassi Brar {
251e2e16fa6SSylwester Nawrocki 	struct samsung_i2s_priv *priv = i2s->priv;
2524ca0c0d4SPadmavathi Venna 	u32 rfs;
253e2e16fa6SSylwester Nawrocki 
2545bfaeddcSSylwester Nawrocki 	rfs = readl(priv->addr + I2SMOD) >> priv->variant_regs->rfs_off;
2555bfaeddcSSylwester Nawrocki 	rfs &= priv->variant_regs->rfs_mask;
2565033f43cSJassi Brar 
2575033f43cSJassi Brar 	switch (rfs) {
258a5a56871SPadmavathi Venna 	case 7: return 192;
259a5a56871SPadmavathi Venna 	case 6: return 96;
260a5a56871SPadmavathi Venna 	case 5: return 128;
261a5a56871SPadmavathi Venna 	case 4: return 64;
2625033f43cSJassi Brar 	case 3:	return 768;
2635033f43cSJassi Brar 	case 2: return 384;
2645033f43cSJassi Brar 	case 1:	return 512;
2655033f43cSJassi Brar 	default: return 256;
2665033f43cSJassi Brar 	}
2675033f43cSJassi Brar }
2685033f43cSJassi Brar 
2695033f43cSJassi Brar /* Write RCLK of I2S (in multiples of LRCLK) */
set_rfs(struct i2s_dai * i2s,unsigned rfs)2705033f43cSJassi Brar static inline void set_rfs(struct i2s_dai *i2s, unsigned rfs)
2715033f43cSJassi Brar {
272e2e16fa6SSylwester Nawrocki 	struct samsung_i2s_priv *priv = i2s->priv;
273e2e16fa6SSylwester Nawrocki 	u32 mod = readl(priv->addr + I2SMOD);
2745bfaeddcSSylwester Nawrocki 	int rfs_shift = priv->variant_regs->rfs_off;
2755033f43cSJassi Brar 
2765bfaeddcSSylwester Nawrocki 	mod &= ~(priv->variant_regs->rfs_mask << rfs_shift);
2775033f43cSJassi Brar 
2785033f43cSJassi Brar 	switch (rfs) {
279a5a56871SPadmavathi Venna 	case 192:
280a5a56871SPadmavathi Venna 		mod |= (EXYNOS7_MOD_RCLK_192FS << rfs_shift);
281a5a56871SPadmavathi Venna 		break;
282a5a56871SPadmavathi Venna 	case 96:
283a5a56871SPadmavathi Venna 		mod |= (EXYNOS7_MOD_RCLK_96FS << rfs_shift);
284a5a56871SPadmavathi Venna 		break;
285a5a56871SPadmavathi Venna 	case 128:
286a5a56871SPadmavathi Venna 		mod |= (EXYNOS7_MOD_RCLK_128FS << rfs_shift);
287a5a56871SPadmavathi Venna 		break;
288a5a56871SPadmavathi Venna 	case 64:
289a5a56871SPadmavathi Venna 		mod |= (EXYNOS7_MOD_RCLK_64FS << rfs_shift);
290a5a56871SPadmavathi Venna 		break;
2915033f43cSJassi Brar 	case 768:
292b60be4aaSPadmavathi Venna 		mod |= (MOD_RCLK_768FS << rfs_shift);
2935033f43cSJassi Brar 		break;
2945033f43cSJassi Brar 	case 512:
295b60be4aaSPadmavathi Venna 		mod |= (MOD_RCLK_512FS << rfs_shift);
2965033f43cSJassi Brar 		break;
2975033f43cSJassi Brar 	case 384:
298b60be4aaSPadmavathi Venna 		mod |= (MOD_RCLK_384FS << rfs_shift);
2995033f43cSJassi Brar 		break;
3005033f43cSJassi Brar 	default:
301b60be4aaSPadmavathi Venna 		mod |= (MOD_RCLK_256FS << rfs_shift);
3025033f43cSJassi Brar 		break;
3035033f43cSJassi Brar 	}
3045033f43cSJassi Brar 
305e2e16fa6SSylwester Nawrocki 	writel(mod, priv->addr + I2SMOD);
3065033f43cSJassi Brar }
3075033f43cSJassi Brar 
3089f9f8a5bSSylwester Nawrocki /* Read bit-clock of I2S (in multiples of LRCLK) */
get_bfs(struct i2s_dai * i2s)3095033f43cSJassi Brar static inline unsigned get_bfs(struct i2s_dai *i2s)
3105033f43cSJassi Brar {
311e2e16fa6SSylwester Nawrocki 	struct samsung_i2s_priv *priv = i2s->priv;
3124ca0c0d4SPadmavathi Venna 	u32 bfs;
313e2e16fa6SSylwester Nawrocki 
3145bfaeddcSSylwester Nawrocki 	bfs = readl(priv->addr + I2SMOD) >> priv->variant_regs->bfs_off;
3155bfaeddcSSylwester Nawrocki 	bfs &= priv->variant_regs->bfs_mask;
3165033f43cSJassi Brar 
3175033f43cSJassi Brar 	switch (bfs) {
3184ca0c0d4SPadmavathi Venna 	case 8: return 256;
3194ca0c0d4SPadmavathi Venna 	case 7: return 192;
3204ca0c0d4SPadmavathi Venna 	case 6: return 128;
3214ca0c0d4SPadmavathi Venna 	case 5: return 96;
3224ca0c0d4SPadmavathi Venna 	case 4: return 64;
3235033f43cSJassi Brar 	case 3: return 24;
3245033f43cSJassi Brar 	case 2: return 16;
3255033f43cSJassi Brar 	case 1:	return 48;
3265033f43cSJassi Brar 	default: return 32;
3275033f43cSJassi Brar 	}
3285033f43cSJassi Brar }
3295033f43cSJassi Brar 
3309f9f8a5bSSylwester Nawrocki /* Write bit-clock of I2S (in multiples of LRCLK) */
set_bfs(struct i2s_dai * i2s,unsigned bfs)3315033f43cSJassi Brar static inline void set_bfs(struct i2s_dai *i2s, unsigned bfs)
3325033f43cSJassi Brar {
333e2e16fa6SSylwester Nawrocki 	struct samsung_i2s_priv *priv = i2s->priv;
334e2e16fa6SSylwester Nawrocki 	u32 mod = readl(priv->addr + I2SMOD);
3355944170fSSylwester Nawrocki 	int tdm = priv->quirks & QUIRK_SUPPORTS_TDM;
3365bfaeddcSSylwester Nawrocki 	int bfs_shift = priv->variant_regs->bfs_off;
3374ca0c0d4SPadmavathi Venna 
3384ca0c0d4SPadmavathi Venna 	/* Non-TDM I2S controllers do not support BCLK > 48 * FS */
3394ca0c0d4SPadmavathi Venna 	if (!tdm && bfs > 48) {
3404ca0c0d4SPadmavathi Venna 		dev_err(&i2s->pdev->dev, "Unsupported BCLK divider\n");
3414ca0c0d4SPadmavathi Venna 		return;
3424ca0c0d4SPadmavathi Venna 	}
3435033f43cSJassi Brar 
3445bfaeddcSSylwester Nawrocki 	mod &= ~(priv->variant_regs->bfs_mask << bfs_shift);
345a5a56871SPadmavathi Venna 
3465033f43cSJassi Brar 	switch (bfs) {
3475033f43cSJassi Brar 	case 48:
348b60be4aaSPadmavathi Venna 		mod |= (MOD_BCLK_48FS << bfs_shift);
3495033f43cSJassi Brar 		break;
3505033f43cSJassi Brar 	case 32:
351b60be4aaSPadmavathi Venna 		mod |= (MOD_BCLK_32FS << bfs_shift);
3525033f43cSJassi Brar 		break;
3535033f43cSJassi Brar 	case 24:
354b60be4aaSPadmavathi Venna 		mod |= (MOD_BCLK_24FS << bfs_shift);
3555033f43cSJassi Brar 		break;
3565033f43cSJassi Brar 	case 16:
357b60be4aaSPadmavathi Venna 		mod |= (MOD_BCLK_16FS << bfs_shift);
3585033f43cSJassi Brar 		break;
3594ca0c0d4SPadmavathi Venna 	case 64:
3604ca0c0d4SPadmavathi Venna 		mod |= (EXYNOS5420_MOD_BCLK_64FS << bfs_shift);
3614ca0c0d4SPadmavathi Venna 		break;
3624ca0c0d4SPadmavathi Venna 	case 96:
3634ca0c0d4SPadmavathi Venna 		mod |= (EXYNOS5420_MOD_BCLK_96FS << bfs_shift);
3644ca0c0d4SPadmavathi Venna 		break;
3654ca0c0d4SPadmavathi Venna 	case 128:
3664ca0c0d4SPadmavathi Venna 		mod |= (EXYNOS5420_MOD_BCLK_128FS << bfs_shift);
3674ca0c0d4SPadmavathi Venna 		break;
3684ca0c0d4SPadmavathi Venna 	case 192:
3694ca0c0d4SPadmavathi Venna 		mod |= (EXYNOS5420_MOD_BCLK_192FS << bfs_shift);
3704ca0c0d4SPadmavathi Venna 		break;
3714ca0c0d4SPadmavathi Venna 	case 256:
3724ca0c0d4SPadmavathi Venna 		mod |= (EXYNOS5420_MOD_BCLK_256FS << bfs_shift);
3735033f43cSJassi Brar 		break;
3745033f43cSJassi Brar 	default:
3755033f43cSJassi Brar 		dev_err(&i2s->pdev->dev, "Wrong BCLK Divider!\n");
3765033f43cSJassi Brar 		return;
3775033f43cSJassi Brar 	}
3785033f43cSJassi Brar 
379e2e16fa6SSylwester Nawrocki 	writel(mod, priv->addr + I2SMOD);
3805033f43cSJassi Brar }
3815033f43cSJassi Brar 
3829f9f8a5bSSylwester Nawrocki /* Sample size */
get_blc(struct i2s_dai * i2s)3835033f43cSJassi Brar static inline int get_blc(struct i2s_dai *i2s)
3845033f43cSJassi Brar {
385e2e16fa6SSylwester Nawrocki 	int blc = readl(i2s->priv->addr + I2SMOD);
3865033f43cSJassi Brar 
3875033f43cSJassi Brar 	blc = (blc >> 13) & 0x3;
3885033f43cSJassi Brar 
3895033f43cSJassi Brar 	switch (blc) {
3905033f43cSJassi Brar 	case 2: return 24;
3915033f43cSJassi Brar 	case 1:	return 8;
3925033f43cSJassi Brar 	default: return 16;
3935033f43cSJassi Brar 	}
3945033f43cSJassi Brar }
3955033f43cSJassi Brar 
3969f9f8a5bSSylwester Nawrocki /* TX channel control */
i2s_txctrl(struct i2s_dai * i2s,int on)3975033f43cSJassi Brar static void i2s_txctrl(struct i2s_dai *i2s, int on)
3985033f43cSJassi Brar {
399e2e16fa6SSylwester Nawrocki 	struct samsung_i2s_priv *priv = i2s->priv;
400e2e16fa6SSylwester Nawrocki 	void __iomem *addr = priv->addr;
4015bfaeddcSSylwester Nawrocki 	int txr_off = priv->variant_regs->txr_off;
4025033f43cSJassi Brar 	u32 con = readl(addr + I2SCON);
403a5a56871SPadmavathi Venna 	u32 mod = readl(addr + I2SMOD) & ~(3 << txr_off);
4045033f43cSJassi Brar 
4055033f43cSJassi Brar 	if (on) {
4065033f43cSJassi Brar 		con |= CON_ACTIVE;
4075033f43cSJassi Brar 		con &= ~CON_TXCH_PAUSE;
4085033f43cSJassi Brar 
4095033f43cSJassi Brar 		if (is_secondary(i2s)) {
4105033f43cSJassi Brar 			con |= CON_TXSDMA_ACTIVE;
4115033f43cSJassi Brar 			con &= ~CON_TXSDMA_PAUSE;
4125033f43cSJassi Brar 		} else {
4135033f43cSJassi Brar 			con |= CON_TXDMA_ACTIVE;
4145033f43cSJassi Brar 			con &= ~CON_TXDMA_PAUSE;
4155033f43cSJassi Brar 		}
4165033f43cSJassi Brar 
4175033f43cSJassi Brar 		if (any_rx_active(i2s))
418a5a56871SPadmavathi Venna 			mod |= 2 << txr_off;
4195033f43cSJassi Brar 		else
420a5a56871SPadmavathi Venna 			mod |= 0 << txr_off;
4215033f43cSJassi Brar 	} else {
4225033f43cSJassi Brar 		if (is_secondary(i2s)) {
4235033f43cSJassi Brar 			con |=  CON_TXSDMA_PAUSE;
4245033f43cSJassi Brar 			con &= ~CON_TXSDMA_ACTIVE;
4255033f43cSJassi Brar 		} else {
4265033f43cSJassi Brar 			con |=  CON_TXDMA_PAUSE;
4275033f43cSJassi Brar 			con &= ~CON_TXDMA_ACTIVE;
4285033f43cSJassi Brar 		}
4295033f43cSJassi Brar 
4305033f43cSJassi Brar 		if (other_tx_active(i2s)) {
4315033f43cSJassi Brar 			writel(con, addr + I2SCON);
4325033f43cSJassi Brar 			return;
4335033f43cSJassi Brar 		}
4345033f43cSJassi Brar 
4355033f43cSJassi Brar 		con |=  CON_TXCH_PAUSE;
4365033f43cSJassi Brar 
4375033f43cSJassi Brar 		if (any_rx_active(i2s))
438a5a56871SPadmavathi Venna 			mod |= 1 << txr_off;
4395033f43cSJassi Brar 		else
4405033f43cSJassi Brar 			con &= ~CON_ACTIVE;
4415033f43cSJassi Brar 	}
4425033f43cSJassi Brar 
4435033f43cSJassi Brar 	writel(mod, addr + I2SMOD);
4445033f43cSJassi Brar 	writel(con, addr + I2SCON);
4455033f43cSJassi Brar }
4465033f43cSJassi Brar 
4475033f43cSJassi Brar /* RX Channel Control */
i2s_rxctrl(struct i2s_dai * i2s,int on)4485033f43cSJassi Brar static void i2s_rxctrl(struct i2s_dai *i2s, int on)
4495033f43cSJassi Brar {
450e2e16fa6SSylwester Nawrocki 	struct samsung_i2s_priv *priv = i2s->priv;
451e2e16fa6SSylwester Nawrocki 	void __iomem *addr = priv->addr;
4525bfaeddcSSylwester Nawrocki 	int txr_off = priv->variant_regs->txr_off;
4535033f43cSJassi Brar 	u32 con = readl(addr + I2SCON);
454a5a56871SPadmavathi Venna 	u32 mod = readl(addr + I2SMOD) & ~(3 << txr_off);
4555033f43cSJassi Brar 
4565033f43cSJassi Brar 	if (on) {
4575033f43cSJassi Brar 		con |= CON_RXDMA_ACTIVE | CON_ACTIVE;
4585033f43cSJassi Brar 		con &= ~(CON_RXDMA_PAUSE | CON_RXCH_PAUSE);
4595033f43cSJassi Brar 
4605033f43cSJassi Brar 		if (any_tx_active(i2s))
461a5a56871SPadmavathi Venna 			mod |= 2 << txr_off;
4625033f43cSJassi Brar 		else
463a5a56871SPadmavathi Venna 			mod |= 1 << txr_off;
4645033f43cSJassi Brar 	} else {
4655033f43cSJassi Brar 		con |=  CON_RXDMA_PAUSE | CON_RXCH_PAUSE;
4665033f43cSJassi Brar 		con &= ~CON_RXDMA_ACTIVE;
4675033f43cSJassi Brar 
4685033f43cSJassi Brar 		if (any_tx_active(i2s))
469a5a56871SPadmavathi Venna 			mod |= 0 << txr_off;
4705033f43cSJassi Brar 		else
4715033f43cSJassi Brar 			con &= ~CON_ACTIVE;
4725033f43cSJassi Brar 	}
4735033f43cSJassi Brar 
4745033f43cSJassi Brar 	writel(mod, addr + I2SMOD);
4755033f43cSJassi Brar 	writel(con, addr + I2SCON);
4765033f43cSJassi Brar }
4775033f43cSJassi Brar 
4785033f43cSJassi Brar /* Flush FIFO of an interface */
i2s_fifo(struct i2s_dai * i2s,u32 flush)4795033f43cSJassi Brar static inline void i2s_fifo(struct i2s_dai *i2s, u32 flush)
4805033f43cSJassi Brar {
4815033f43cSJassi Brar 	void __iomem *fic;
4825033f43cSJassi Brar 	u32 val;
4835033f43cSJassi Brar 
4845033f43cSJassi Brar 	if (!i2s)
4855033f43cSJassi Brar 		return;
4865033f43cSJassi Brar 
4875033f43cSJassi Brar 	if (is_secondary(i2s))
488e2e16fa6SSylwester Nawrocki 		fic = i2s->priv->addr + I2SFICS;
4895033f43cSJassi Brar 	else
490e2e16fa6SSylwester Nawrocki 		fic = i2s->priv->addr + I2SFIC;
4915033f43cSJassi Brar 
4925033f43cSJassi Brar 	/* Flush the FIFO */
4935033f43cSJassi Brar 	writel(readl(fic) | flush, fic);
4945033f43cSJassi Brar 
4955033f43cSJassi Brar 	/* Be patient */
4965033f43cSJassi Brar 	val = msecs_to_loops(1) / 1000; /* 1 usec */
4975033f43cSJassi Brar 	while (--val)
4985033f43cSJassi Brar 		cpu_relax();
4995033f43cSJassi Brar 
5005033f43cSJassi Brar 	writel(readl(fic) & ~flush, fic);
5015033f43cSJassi Brar }
5025033f43cSJassi Brar 
i2s_set_sysclk(struct snd_soc_dai * dai,int clk_id,unsigned int rfs,int dir)5033b0fa51fSSylwester Nawrocki static int i2s_set_sysclk(struct snd_soc_dai *dai, int clk_id, unsigned int rfs,
5043b0fa51fSSylwester Nawrocki 			  int dir)
5055033f43cSJassi Brar {
5063b0fa51fSSylwester Nawrocki 	struct samsung_i2s_priv *priv = snd_soc_dai_get_drvdata(dai);
5075033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
508dcd60fc3SSylwester Nawrocki 	struct i2s_dai *other = get_other_dai(i2s);
5095bfaeddcSSylwester Nawrocki 	const struct samsung_i2s_variant_regs *i2s_regs = priv->variant_regs;
510a5a56871SPadmavathi Venna 	unsigned int cdcon_mask = 1 << i2s_regs->cdclkcon_off;
511a5a56871SPadmavathi Venna 	unsigned int rsrc_mask = 1 << i2s_regs->rclksrc_off;
512ce8bcdbbSSylwester Nawrocki 	u32 mod, mask, val = 0;
513316fa9e0SCharles Keepax 	unsigned long flags;
514dc938ddbSMarek Szyprowski 	int ret = 0;
515dc938ddbSMarek Szyprowski 
516dc938ddbSMarek Szyprowski 	pm_runtime_get_sync(dai->dev);
517ce8bcdbbSSylwester Nawrocki 
5189d7939c9SSylwester Nawrocki 	spin_lock_irqsave(&priv->lock, flags);
519e2e16fa6SSylwester Nawrocki 	mod = readl(priv->addr + I2SMOD);
5209d7939c9SSylwester Nawrocki 	spin_unlock_irqrestore(&priv->lock, flags);
5215033f43cSJassi Brar 
5225033f43cSJassi Brar 	switch (clk_id) {
523c86d50f9SSylwester Nawrocki 	case SAMSUNG_I2S_OPCLK:
524ce8bcdbbSSylwester Nawrocki 		mask = MOD_OPCLK_MASK;
52545ae70e8SSylwester Nawrocki 		val = (dir << MOD_OPCLK_SHIFT) & MOD_OPCLK_MASK;
526c86d50f9SSylwester Nawrocki 		break;
5275033f43cSJassi Brar 	case SAMSUNG_I2S_CDCLK:
528ce8bcdbbSSylwester Nawrocki 		mask = 1 << i2s_regs->cdclkcon_off;
5295033f43cSJassi Brar 		/* Shouldn't matter in GATING(CLOCK_IN) mode */
5305033f43cSJassi Brar 		if (dir == SND_SOC_CLOCK_IN)
5315033f43cSJassi Brar 			rfs = 0;
5325033f43cSJassi Brar 
533133c2681SCharles Keepax 		if ((rfs && other && other->rfs && (other->rfs != rfs)) ||
5345033f43cSJassi Brar 				(any_active(i2s) &&
5355033f43cSJassi Brar 				(((dir == SND_SOC_CLOCK_IN)
536a5a56871SPadmavathi Venna 					&& !(mod & cdcon_mask)) ||
5375033f43cSJassi Brar 				((dir == SND_SOC_CLOCK_OUT)
538a5a56871SPadmavathi Venna 					&& (mod & cdcon_mask))))) {
5395033f43cSJassi Brar 			dev_err(&i2s->pdev->dev,
5405033f43cSJassi Brar 				"%s:%d Other DAI busy\n", __func__, __LINE__);
541dc938ddbSMarek Szyprowski 			ret = -EAGAIN;
542dc938ddbSMarek Szyprowski 			goto err;
5435033f43cSJassi Brar 		}
5445033f43cSJassi Brar 
5455033f43cSJassi Brar 		if (dir == SND_SOC_CLOCK_IN)
546ce8bcdbbSSylwester Nawrocki 			val = 1 << i2s_regs->cdclkcon_off;
5475033f43cSJassi Brar 
5485033f43cSJassi Brar 		i2s->rfs = rfs;
5495033f43cSJassi Brar 		break;
5505033f43cSJassi Brar 
5515033f43cSJassi Brar 	case SAMSUNG_I2S_RCLKSRC_0: /* clock corrsponding to IISMOD[10] := 0 */
5525033f43cSJassi Brar 	case SAMSUNG_I2S_RCLKSRC_1: /* clock corrsponding to IISMOD[10] := 1 */
553ce8bcdbbSSylwester Nawrocki 		mask = 1 << i2s_regs->rclksrc_off;
554ce8bcdbbSSylwester Nawrocki 
5555944170fSSylwester Nawrocki 		if ((priv->quirks & QUIRK_NO_MUXPSR)
5565033f43cSJassi Brar 				|| (clk_id == SAMSUNG_I2S_RCLKSRC_0))
5575033f43cSJassi Brar 			clk_id = 0;
5585033f43cSJassi Brar 		else
5595033f43cSJassi Brar 			clk_id = 1;
5605033f43cSJassi Brar 
5615033f43cSJassi Brar 		if (!any_active(i2s)) {
5623b0fa51fSSylwester Nawrocki 			if (priv->op_clk && !IS_ERR(priv->op_clk)) {
563a5a56871SPadmavathi Venna 				if ((clk_id && !(mod & rsrc_mask)) ||
564a5a56871SPadmavathi Venna 					(!clk_id && (mod & rsrc_mask))) {
5653b0fa51fSSylwester Nawrocki 					clk_disable_unprepare(priv->op_clk);
5663b0fa51fSSylwester Nawrocki 					clk_put(priv->op_clk);
5675033f43cSJassi Brar 				} else {
5683b0fa51fSSylwester Nawrocki 					priv->rclk_srcrate =
5693b0fa51fSSylwester Nawrocki 						clk_get_rate(priv->op_clk);
570dc938ddbSMarek Szyprowski 					goto done;
5715033f43cSJassi Brar 				}
5725033f43cSJassi Brar 			}
5735033f43cSJassi Brar 
5741974a042SPadmavathi Venna 			if (clk_id)
5753b0fa51fSSylwester Nawrocki 				priv->op_clk = clk_get(&i2s->pdev->dev,
5761974a042SPadmavathi Venna 						"i2s_opclk1");
5771974a042SPadmavathi Venna 			else
5783b0fa51fSSylwester Nawrocki 				priv->op_clk = clk_get(&i2s->pdev->dev,
5791974a042SPadmavathi Venna 						"i2s_opclk0");
580a6aba536SSylwester Nawrocki 
5813b0fa51fSSylwester Nawrocki 			if (WARN_ON(IS_ERR(priv->op_clk))) {
5823b0fa51fSSylwester Nawrocki 				ret = PTR_ERR(priv->op_clk);
5833b0fa51fSSylwester Nawrocki 				priv->op_clk = NULL;
584dc938ddbSMarek Szyprowski 				goto err;
585dc938ddbSMarek Szyprowski 			}
586a6aba536SSylwester Nawrocki 
5873b0fa51fSSylwester Nawrocki 			ret = clk_prepare_enable(priv->op_clk);
5886431a7e3SChristophe Jaillet 			if (ret) {
5893b0fa51fSSylwester Nawrocki 				clk_put(priv->op_clk);
5903b0fa51fSSylwester Nawrocki 				priv->op_clk = NULL;
591f5c97c7bSArvind Yadav 				goto err;
5926431a7e3SChristophe Jaillet 			}
5933b0fa51fSSylwester Nawrocki 			priv->rclk_srcrate = clk_get_rate(priv->op_clk);
5945033f43cSJassi Brar 
595a5a56871SPadmavathi Venna 		} else if ((!clk_id && (mod & rsrc_mask))
596a5a56871SPadmavathi Venna 				|| (clk_id && !(mod & rsrc_mask))) {
5975033f43cSJassi Brar 			dev_err(&i2s->pdev->dev,
5985033f43cSJassi Brar 				"%s:%d Other DAI busy\n", __func__, __LINE__);
599dc938ddbSMarek Szyprowski 			ret = -EAGAIN;
600dc938ddbSMarek Szyprowski 			goto err;
6015033f43cSJassi Brar 		} else {
6025033f43cSJassi Brar 			/* Call can't be on the active DAI */
603dc938ddbSMarek Szyprowski 			goto done;
6045033f43cSJassi Brar 		}
6055033f43cSJassi Brar 
606ce8bcdbbSSylwester Nawrocki 		if (clk_id == 1)
607ce8bcdbbSSylwester Nawrocki 			val = 1 << i2s_regs->rclksrc_off;
608b2de1d20SPadmavathi Venna 		break;
6095033f43cSJassi Brar 	default:
6105033f43cSJassi Brar 		dev_err(&i2s->pdev->dev, "We don't serve that!\n");
611dc938ddbSMarek Szyprowski 		ret = -EINVAL;
612dc938ddbSMarek Szyprowski 		goto err;
6135033f43cSJassi Brar 	}
6145033f43cSJassi Brar 
6159d7939c9SSylwester Nawrocki 	spin_lock_irqsave(&priv->lock, flags);
616e2e16fa6SSylwester Nawrocki 	mod = readl(priv->addr + I2SMOD);
617ce8bcdbbSSylwester Nawrocki 	mod = (mod & ~mask) | val;
618e2e16fa6SSylwester Nawrocki 	writel(mod, priv->addr + I2SMOD);
6199d7939c9SSylwester Nawrocki 	spin_unlock_irqrestore(&priv->lock, flags);
620dc938ddbSMarek Szyprowski done:
621dc938ddbSMarek Szyprowski 	pm_runtime_put(dai->dev);
6225033f43cSJassi Brar 
6235033f43cSJassi Brar 	return 0;
624dc938ddbSMarek Szyprowski err:
625dc938ddbSMarek Szyprowski 	pm_runtime_put(dai->dev);
626dc938ddbSMarek Szyprowski 	return ret;
6275033f43cSJassi Brar }
6285033f43cSJassi Brar 
i2s_set_fmt(struct snd_soc_dai * dai,unsigned int fmt)62989d2e831SSylwester Nawrocki static int i2s_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
6305033f43cSJassi Brar {
63189d2e831SSylwester Nawrocki 	struct samsung_i2s_priv *priv = snd_soc_dai_get_drvdata(dai);
6325033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
633a5a56871SPadmavathi Venna 	int lrp_shift, sdf_shift, sdf_mask, lrp_rlow, mod_slave;
634ce8bcdbbSSylwester Nawrocki 	u32 mod, tmp = 0;
635316fa9e0SCharles Keepax 	unsigned long flags;
6365033f43cSJassi Brar 
6375bfaeddcSSylwester Nawrocki 	lrp_shift = priv->variant_regs->lrp_off;
6385bfaeddcSSylwester Nawrocki 	sdf_shift = priv->variant_regs->sdf_off;
6395bfaeddcSSylwester Nawrocki 	mod_slave = 1 << priv->variant_regs->mss_off;
6404ca0c0d4SPadmavathi Venna 
641b60be4aaSPadmavathi Venna 	sdf_mask = MOD_SDF_MASK << sdf_shift;
642b60be4aaSPadmavathi Venna 	lrp_rlow = MOD_LR_RLOW << lrp_shift;
643b60be4aaSPadmavathi Venna 
6445033f43cSJassi Brar 	/* Format is priority */
6455033f43cSJassi Brar 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
6465033f43cSJassi Brar 	case SND_SOC_DAIFMT_RIGHT_J:
647b60be4aaSPadmavathi Venna 		tmp |= lrp_rlow;
648b60be4aaSPadmavathi Venna 		tmp |= (MOD_SDF_MSB << sdf_shift);
6495033f43cSJassi Brar 		break;
6505033f43cSJassi Brar 	case SND_SOC_DAIFMT_LEFT_J:
651b60be4aaSPadmavathi Venna 		tmp |= lrp_rlow;
652b60be4aaSPadmavathi Venna 		tmp |= (MOD_SDF_LSB << sdf_shift);
6535033f43cSJassi Brar 		break;
6545033f43cSJassi Brar 	case SND_SOC_DAIFMT_I2S:
655b60be4aaSPadmavathi Venna 		tmp |= (MOD_SDF_IIS << sdf_shift);
6565033f43cSJassi Brar 		break;
6575033f43cSJassi Brar 	default:
6585033f43cSJassi Brar 		dev_err(&i2s->pdev->dev, "Format not supported\n");
6595033f43cSJassi Brar 		return -EINVAL;
6605033f43cSJassi Brar 	}
6615033f43cSJassi Brar 
6625033f43cSJassi Brar 	/*
6635033f43cSJassi Brar 	 * INV flag is relative to the FORMAT flag - if set it simply
6645033f43cSJassi Brar 	 * flips the polarity specified by the Standard
6655033f43cSJassi Brar 	 */
6665033f43cSJassi Brar 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
6675033f43cSJassi Brar 	case SND_SOC_DAIFMT_NB_NF:
6685033f43cSJassi Brar 		break;
6695033f43cSJassi Brar 	case SND_SOC_DAIFMT_NB_IF:
670b60be4aaSPadmavathi Venna 		if (tmp & lrp_rlow)
671b60be4aaSPadmavathi Venna 			tmp &= ~lrp_rlow;
6725033f43cSJassi Brar 		else
673b60be4aaSPadmavathi Venna 			tmp |= lrp_rlow;
6745033f43cSJassi Brar 		break;
6755033f43cSJassi Brar 	default:
6765033f43cSJassi Brar 		dev_err(&i2s->pdev->dev, "Polarity not supported\n");
6775033f43cSJassi Brar 		return -EINVAL;
6785033f43cSJassi Brar 	}
6795033f43cSJassi Brar 
6800b491c7cSCharles Keepax 	switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
6810b491c7cSCharles Keepax 	case SND_SOC_DAIFMT_BC_FC:
682a5a56871SPadmavathi Venna 		tmp |= mod_slave;
6835033f43cSJassi Brar 		break;
6840b491c7cSCharles Keepax 	case SND_SOC_DAIFMT_BP_FP:
685647d04f8SSylwester Nawrocki 		/*
686647d04f8SSylwester Nawrocki 		 * Set default source clock in Master mode, only when the
687647d04f8SSylwester Nawrocki 		 * CLK_I2S_RCLK_SRC clock is not exposed so we ensure any
688647d04f8SSylwester Nawrocki 		 * clock configuration assigned in DT is not overwritten.
689647d04f8SSylwester Nawrocki 		 */
6903b0fa51fSSylwester Nawrocki 		if (priv->rclk_srcrate == 0 && priv->clk_data.clks == NULL)
6915033f43cSJassi Brar 			i2s_set_sysclk(dai, SAMSUNG_I2S_RCLKSRC_0,
6925033f43cSJassi Brar 							0, SND_SOC_CLOCK_IN);
6935033f43cSJassi Brar 		break;
6945033f43cSJassi Brar 	default:
6955033f43cSJassi Brar 		dev_err(&i2s->pdev->dev, "master/slave format not supported\n");
6965033f43cSJassi Brar 		return -EINVAL;
6975033f43cSJassi Brar 	}
6985033f43cSJassi Brar 
699dc938ddbSMarek Szyprowski 	pm_runtime_get_sync(dai->dev);
7009d7939c9SSylwester Nawrocki 	spin_lock_irqsave(&priv->lock, flags);
701e2e16fa6SSylwester Nawrocki 	mod = readl(priv->addr + I2SMOD);
702b60be4aaSPadmavathi Venna 	/*
703b60be4aaSPadmavathi Venna 	 * Don't change the I2S mode if any controller is active on this
704b60be4aaSPadmavathi Venna 	 * channel.
705b60be4aaSPadmavathi Venna 	 */
7065033f43cSJassi Brar 	if (any_active(i2s) &&
707a5a56871SPadmavathi Venna 		((mod & (sdf_mask | lrp_rlow | mod_slave)) != tmp)) {
7089d7939c9SSylwester Nawrocki 		spin_unlock_irqrestore(&priv->lock, flags);
709dc938ddbSMarek Szyprowski 		pm_runtime_put(dai->dev);
7105033f43cSJassi Brar 		dev_err(&i2s->pdev->dev,
7115033f43cSJassi Brar 				"%s:%d Other DAI busy\n", __func__, __LINE__);
7125033f43cSJassi Brar 		return -EAGAIN;
7135033f43cSJassi Brar 	}
7145033f43cSJassi Brar 
715a5a56871SPadmavathi Venna 	mod &= ~(sdf_mask | lrp_rlow | mod_slave);
7165033f43cSJassi Brar 	mod |= tmp;
717e2e16fa6SSylwester Nawrocki 	writel(mod, priv->addr + I2SMOD);
718f29eec79SSylwester Nawrocki 	priv->slave_mode = (mod & mod_slave);
7199d7939c9SSylwester Nawrocki 	spin_unlock_irqrestore(&priv->lock, flags);
720dc938ddbSMarek Szyprowski 	pm_runtime_put(dai->dev);
7215033f43cSJassi Brar 
7225033f43cSJassi Brar 	return 0;
7235033f43cSJassi Brar }
7245033f43cSJassi Brar 
i2s_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)7255033f43cSJassi Brar static int i2s_hw_params(struct snd_pcm_substream *substream,
7265033f43cSJassi Brar 	struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
7275033f43cSJassi Brar {
72889d2e831SSylwester Nawrocki 	struct samsung_i2s_priv *priv = snd_soc_dai_get_drvdata(dai);
7295033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
730ce8bcdbbSSylwester Nawrocki 	u32 mod, mask = 0, val = 0;
731860b454cSSylwester Nawrocki 	struct clk *rclksrc;
732316fa9e0SCharles Keepax 	unsigned long flags;
7335033f43cSJassi Brar 
734dc938ddbSMarek Szyprowski 	WARN_ON(!pm_runtime_active(dai->dev));
735dc938ddbSMarek Szyprowski 
7365033f43cSJassi Brar 	if (!is_secondary(i2s))
737ce8bcdbbSSylwester Nawrocki 		mask |= (MOD_DC2_EN | MOD_DC1_EN);
7385033f43cSJassi Brar 
7395033f43cSJassi Brar 	switch (params_channels(params)) {
7405033f43cSJassi Brar 	case 6:
741ce8bcdbbSSylwester Nawrocki 		val |= MOD_DC2_EN;
742df561f66SGustavo A. R. Silva 		fallthrough;
7435033f43cSJassi Brar 	case 4:
744ce8bcdbbSSylwester Nawrocki 		val |= MOD_DC1_EN;
7455033f43cSJassi Brar 		break;
7465033f43cSJassi Brar 	case 2:
747588fb705SSangsu Park 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
74869e7a69aSSylwester Nawrocki 			i2s->dma_playback.addr_width = 4;
749588fb705SSangsu Park 		else
75069e7a69aSSylwester Nawrocki 			i2s->dma_capture.addr_width = 4;
751588fb705SSangsu Park 		break;
752588fb705SSangsu Park 	case 1:
753588fb705SSangsu Park 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
75469e7a69aSSylwester Nawrocki 			i2s->dma_playback.addr_width = 2;
755588fb705SSangsu Park 		else
75669e7a69aSSylwester Nawrocki 			i2s->dma_capture.addr_width = 2;
757588fb705SSangsu Park 
7585033f43cSJassi Brar 		break;
7595033f43cSJassi Brar 	default:
7605033f43cSJassi Brar 		dev_err(&i2s->pdev->dev, "%d channels not supported\n",
7615033f43cSJassi Brar 				params_channels(params));
7625033f43cSJassi Brar 		return -EINVAL;
7635033f43cSJassi Brar 	}
7645033f43cSJassi Brar 
7655033f43cSJassi Brar 	if (is_secondary(i2s))
766ce8bcdbbSSylwester Nawrocki 		mask |= MOD_BLCS_MASK;
7675033f43cSJassi Brar 	else
768ce8bcdbbSSylwester Nawrocki 		mask |= MOD_BLCP_MASK;
7695033f43cSJassi Brar 
7705033f43cSJassi Brar 	if (is_manager(i2s))
771ce8bcdbbSSylwester Nawrocki 		mask |= MOD_BLC_MASK;
7725033f43cSJassi Brar 
77388ce1465STushar Behera 	switch (params_width(params)) {
77488ce1465STushar Behera 	case 8:
7755033f43cSJassi Brar 		if (is_secondary(i2s))
776ce8bcdbbSSylwester Nawrocki 			val |= MOD_BLCS_8BIT;
7775033f43cSJassi Brar 		else
778ce8bcdbbSSylwester Nawrocki 			val |= MOD_BLCP_8BIT;
7795033f43cSJassi Brar 		if (is_manager(i2s))
780ce8bcdbbSSylwester Nawrocki 			val |= MOD_BLC_8BIT;
7815033f43cSJassi Brar 		break;
78288ce1465STushar Behera 	case 16:
7835033f43cSJassi Brar 		if (is_secondary(i2s))
784ce8bcdbbSSylwester Nawrocki 			val |= MOD_BLCS_16BIT;
7855033f43cSJassi Brar 		else
786ce8bcdbbSSylwester Nawrocki 			val |= MOD_BLCP_16BIT;
7875033f43cSJassi Brar 		if (is_manager(i2s))
788ce8bcdbbSSylwester Nawrocki 			val |= MOD_BLC_16BIT;
7895033f43cSJassi Brar 		break;
79088ce1465STushar Behera 	case 24:
7915033f43cSJassi Brar 		if (is_secondary(i2s))
792ce8bcdbbSSylwester Nawrocki 			val |= MOD_BLCS_24BIT;
7935033f43cSJassi Brar 		else
794ce8bcdbbSSylwester Nawrocki 			val |= MOD_BLCP_24BIT;
7955033f43cSJassi Brar 		if (is_manager(i2s))
796ce8bcdbbSSylwester Nawrocki 			val |= MOD_BLC_24BIT;
7975033f43cSJassi Brar 		break;
7985033f43cSJassi Brar 	default:
7995033f43cSJassi Brar 		dev_err(&i2s->pdev->dev, "Format(%d) not supported\n",
8005033f43cSJassi Brar 				params_format(params));
8015033f43cSJassi Brar 		return -EINVAL;
8025033f43cSJassi Brar 	}
803ce8bcdbbSSylwester Nawrocki 
8049d7939c9SSylwester Nawrocki 	spin_lock_irqsave(&priv->lock, flags);
805e2e16fa6SSylwester Nawrocki 	mod = readl(priv->addr + I2SMOD);
806ce8bcdbbSSylwester Nawrocki 	mod = (mod & ~mask) | val;
807e2e16fa6SSylwester Nawrocki 	writel(mod, priv->addr + I2SMOD);
8089d7939c9SSylwester Nawrocki 	spin_unlock_irqrestore(&priv->lock, flags);
8095033f43cSJassi Brar 
81069e7a69aSSylwester Nawrocki 	snd_soc_dai_init_dma_data(dai, &i2s->dma_playback, &i2s->dma_capture);
811d37bdf73SMark Brown 
8125033f43cSJassi Brar 	i2s->frmclk = params_rate(params);
8135033f43cSJassi Brar 
81489d2e831SSylwester Nawrocki 	rclksrc = priv->clk_table[CLK_I2S_RCLK_SRC];
815860b454cSSylwester Nawrocki 	if (rclksrc && !IS_ERR(rclksrc))
8163b0fa51fSSylwester Nawrocki 		priv->rclk_srcrate = clk_get_rate(rclksrc);
817860b454cSSylwester Nawrocki 
8185033f43cSJassi Brar 	return 0;
8195033f43cSJassi Brar }
8205033f43cSJassi Brar 
8219f9f8a5bSSylwester Nawrocki /* We set constraints on the substream according to the version of I2S */
i2s_startup(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)8225033f43cSJassi Brar static int i2s_startup(struct snd_pcm_substream *substream,
8235033f43cSJassi Brar 	  struct snd_soc_dai *dai)
8245033f43cSJassi Brar {
8255944170fSSylwester Nawrocki 	struct samsung_i2s_priv *priv = snd_soc_dai_get_drvdata(dai);
8265033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
827dcd60fc3SSylwester Nawrocki 	struct i2s_dai *other = get_other_dai(i2s);
8285033f43cSJassi Brar 	unsigned long flags;
8295033f43cSJassi Brar 
830dc938ddbSMarek Szyprowski 	pm_runtime_get_sync(dai->dev);
831dc938ddbSMarek Szyprowski 
832defc67c6SSylwester Nawrocki 	spin_lock_irqsave(&priv->pcm_lock, flags);
8335033f43cSJassi Brar 
8345033f43cSJassi Brar 	i2s->mode |= DAI_OPENED;
8355033f43cSJassi Brar 
8365033f43cSJassi Brar 	if (is_manager(other))
8375033f43cSJassi Brar 		i2s->mode &= ~DAI_MANAGER;
8385033f43cSJassi Brar 	else
8395033f43cSJassi Brar 		i2s->mode |= DAI_MANAGER;
8405033f43cSJassi Brar 
8415944170fSSylwester Nawrocki 	if (!any_active(i2s) && (priv->quirks & QUIRK_NEED_RSTCLR))
842e2e16fa6SSylwester Nawrocki 		writel(CON_RSTCLR, i2s->priv->addr + I2SCON);
8432d77828dSPadmavathi Venna 
844defc67c6SSylwester Nawrocki 	spin_unlock_irqrestore(&priv->pcm_lock, flags);
8455033f43cSJassi Brar 
8465033f43cSJassi Brar 	return 0;
8475033f43cSJassi Brar }
8485033f43cSJassi Brar 
i2s_shutdown(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)8495033f43cSJassi Brar static void i2s_shutdown(struct snd_pcm_substream *substream,
8505033f43cSJassi Brar 	struct snd_soc_dai *dai)
8515033f43cSJassi Brar {
852defc67c6SSylwester Nawrocki 	struct samsung_i2s_priv *priv = snd_soc_dai_get_drvdata(dai);
8535033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
854dcd60fc3SSylwester Nawrocki 	struct i2s_dai *other = get_other_dai(i2s);
8555033f43cSJassi Brar 	unsigned long flags;
8565033f43cSJassi Brar 
857defc67c6SSylwester Nawrocki 	spin_lock_irqsave(&priv->pcm_lock, flags);
8585033f43cSJassi Brar 
8595033f43cSJassi Brar 	i2s->mode &= ~DAI_OPENED;
8605033f43cSJassi Brar 	i2s->mode &= ~DAI_MANAGER;
8615033f43cSJassi Brar 
862074b89bbSSylwester Nawrocki 	if (is_opened(other))
8635033f43cSJassi Brar 		other->mode |= DAI_MANAGER;
864074b89bbSSylwester Nawrocki 
8655033f43cSJassi Brar 	/* Reset any constraint on RFS and BFS */
8665033f43cSJassi Brar 	i2s->rfs = 0;
8675033f43cSJassi Brar 	i2s->bfs = 0;
8685033f43cSJassi Brar 
869defc67c6SSylwester Nawrocki 	spin_unlock_irqrestore(&priv->pcm_lock, flags);
870dc938ddbSMarek Szyprowski 
871dc938ddbSMarek Szyprowski 	pm_runtime_put(dai->dev);
8725033f43cSJassi Brar }
8735033f43cSJassi Brar 
config_setup(struct i2s_dai * i2s)8745033f43cSJassi Brar static int config_setup(struct i2s_dai *i2s)
8755033f43cSJassi Brar {
8763b0fa51fSSylwester Nawrocki 	struct samsung_i2s_priv *priv = i2s->priv;
877dcd60fc3SSylwester Nawrocki 	struct i2s_dai *other = get_other_dai(i2s);
8785033f43cSJassi Brar 	unsigned rfs, bfs, blc;
8795033f43cSJassi Brar 	u32 psr;
8805033f43cSJassi Brar 
8815033f43cSJassi Brar 	blc = get_blc(i2s);
8825033f43cSJassi Brar 
8835033f43cSJassi Brar 	bfs = i2s->bfs;
8845033f43cSJassi Brar 
8855033f43cSJassi Brar 	if (!bfs && other)
8865033f43cSJassi Brar 		bfs = other->bfs;
8875033f43cSJassi Brar 
8885033f43cSJassi Brar 	/* Select least possible multiple(2) if no constraint set */
8895033f43cSJassi Brar 	if (!bfs)
8905033f43cSJassi Brar 		bfs = blc * 2;
8915033f43cSJassi Brar 
8925033f43cSJassi Brar 	rfs = i2s->rfs;
8935033f43cSJassi Brar 
8945033f43cSJassi Brar 	if (!rfs && other)
8955033f43cSJassi Brar 		rfs = other->rfs;
8965033f43cSJassi Brar 
8975033f43cSJassi Brar 	if ((rfs == 256 || rfs == 512) && (blc == 24)) {
8985033f43cSJassi Brar 		dev_err(&i2s->pdev->dev,
8995033f43cSJassi Brar 			"%d-RFS not supported for 24-blc\n", rfs);
9005033f43cSJassi Brar 		return -EINVAL;
9015033f43cSJassi Brar 	}
9025033f43cSJassi Brar 
9035033f43cSJassi Brar 	if (!rfs) {
9045033f43cSJassi Brar 		if (bfs == 16 || bfs == 32)
9055033f43cSJassi Brar 			rfs = 256;
9065033f43cSJassi Brar 		else
9075033f43cSJassi Brar 			rfs = 384;
9085033f43cSJassi Brar 	}
9095033f43cSJassi Brar 
9105033f43cSJassi Brar 	/* If already setup and running */
9115033f43cSJassi Brar 	if (any_active(i2s) && (get_rfs(i2s) != rfs || get_bfs(i2s) != bfs)) {
9125033f43cSJassi Brar 		dev_err(&i2s->pdev->dev,
9135033f43cSJassi Brar 				"%s:%d Other DAI busy\n", __func__, __LINE__);
9145033f43cSJassi Brar 		return -EAGAIN;
9155033f43cSJassi Brar 	}
9165033f43cSJassi Brar 
9175033f43cSJassi Brar 	set_bfs(i2s, bfs);
9185033f43cSJassi Brar 	set_rfs(i2s, rfs);
9195033f43cSJassi Brar 
92077010010SPadmavathi Venna 	/* Don't bother with PSR in Slave mode */
921f29eec79SSylwester Nawrocki 	if (priv->slave_mode)
92277010010SPadmavathi Venna 		return 0;
92377010010SPadmavathi Venna 
9245944170fSSylwester Nawrocki 	if (!(priv->quirks & QUIRK_NO_MUXPSR)) {
9253b0fa51fSSylwester Nawrocki 		psr = priv->rclk_srcrate / i2s->frmclk / rfs;
926e2e16fa6SSylwester Nawrocki 		writel(((psr - 1) << 8) | PSR_PSREN, priv->addr + I2SPSR);
9275033f43cSJassi Brar 		dev_dbg(&i2s->pdev->dev,
9285033f43cSJassi Brar 			"RCLK_SRC=%luHz PSR=%u, RCLK=%dfs, BCLK=%dfs\n",
9293b0fa51fSSylwester Nawrocki 				priv->rclk_srcrate, psr, rfs, bfs);
9305033f43cSJassi Brar 	}
9315033f43cSJassi Brar 
9325033f43cSJassi Brar 	return 0;
9335033f43cSJassi Brar }
9345033f43cSJassi Brar 
i2s_trigger(struct snd_pcm_substream * substream,int cmd,struct snd_soc_dai * dai)9355033f43cSJassi Brar static int i2s_trigger(struct snd_pcm_substream *substream,
9365033f43cSJassi Brar 	int cmd, struct snd_soc_dai *dai)
9375033f43cSJassi Brar {
9389d7939c9SSylwester Nawrocki 	struct samsung_i2s_priv *priv = snd_soc_dai_get_drvdata(dai);
9395033f43cSJassi Brar 	int capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
94021b6cd54SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
94121b6cd54SKuninori Morimoto 	struct i2s_dai *i2s = to_info(snd_soc_rtd_to_cpu(rtd, 0));
9425033f43cSJassi Brar 	unsigned long flags;
9435033f43cSJassi Brar 
9445033f43cSJassi Brar 	switch (cmd) {
9455033f43cSJassi Brar 	case SNDRV_PCM_TRIGGER_START:
9465033f43cSJassi Brar 	case SNDRV_PCM_TRIGGER_RESUME:
9475033f43cSJassi Brar 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
948dc938ddbSMarek Szyprowski 		pm_runtime_get_sync(dai->dev);
949bc36d761SPadmanabhan Rajanbabu 
950bc36d761SPadmanabhan Rajanbabu 		if (priv->fixup_early)
951bc36d761SPadmanabhan Rajanbabu 			priv->fixup_early(substream, dai);
952bc36d761SPadmanabhan Rajanbabu 
9539d7939c9SSylwester Nawrocki 		spin_lock_irqsave(&priv->lock, flags);
9545033f43cSJassi Brar 
9555033f43cSJassi Brar 		if (config_setup(i2s)) {
9569d7939c9SSylwester Nawrocki 			spin_unlock_irqrestore(&priv->lock, flags);
9575033f43cSJassi Brar 			return -EINVAL;
9585033f43cSJassi Brar 		}
9595033f43cSJassi Brar 
960bc36d761SPadmanabhan Rajanbabu 		if (priv->fixup_late)
961bc36d761SPadmanabhan Rajanbabu 			priv->fixup_late(substream, dai);
962bc36d761SPadmanabhan Rajanbabu 
9635033f43cSJassi Brar 		if (capture)
9645033f43cSJassi Brar 			i2s_rxctrl(i2s, 1);
9655033f43cSJassi Brar 		else
9665033f43cSJassi Brar 			i2s_txctrl(i2s, 1);
9675033f43cSJassi Brar 
9689d7939c9SSylwester Nawrocki 		spin_unlock_irqrestore(&priv->lock, flags);
9695033f43cSJassi Brar 		break;
9705033f43cSJassi Brar 	case SNDRV_PCM_TRIGGER_STOP:
9715033f43cSJassi Brar 	case SNDRV_PCM_TRIGGER_SUSPEND:
9725033f43cSJassi Brar 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
9739d7939c9SSylwester Nawrocki 		spin_lock_irqsave(&priv->lock, flags);
9745033f43cSJassi Brar 
975c90887feSJassi Brar 		if (capture) {
9765033f43cSJassi Brar 			i2s_rxctrl(i2s, 0);
977775bc971SJassi Brar 			i2s_fifo(i2s, FIC_RXFLUSH);
978c90887feSJassi Brar 		} else {
979c90887feSJassi Brar 			i2s_txctrl(i2s, 0);
980775bc971SJassi Brar 			i2s_fifo(i2s, FIC_TXFLUSH);
981c90887feSJassi Brar 		}
982775bc971SJassi Brar 
9839d7939c9SSylwester Nawrocki 		spin_unlock_irqrestore(&priv->lock, flags);
984dc938ddbSMarek Szyprowski 		pm_runtime_put(dai->dev);
9855033f43cSJassi Brar 		break;
9865033f43cSJassi Brar 	}
9875033f43cSJassi Brar 
9885033f43cSJassi Brar 	return 0;
9895033f43cSJassi Brar }
9905033f43cSJassi Brar 
i2s_set_clkdiv(struct snd_soc_dai * dai,int div_id,int div)9915033f43cSJassi Brar static int i2s_set_clkdiv(struct snd_soc_dai *dai,
9925033f43cSJassi Brar 	int div_id, int div)
9935033f43cSJassi Brar {
9945033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
995dcd60fc3SSylwester Nawrocki 	struct i2s_dai *other = get_other_dai(i2s);
9965033f43cSJassi Brar 
9975033f43cSJassi Brar 	switch (div_id) {
9985033f43cSJassi Brar 	case SAMSUNG_I2S_DIV_BCLK:
999dc938ddbSMarek Szyprowski 		pm_runtime_get_sync(dai->dev);
10005033f43cSJassi Brar 		if ((any_active(i2s) && div && (get_bfs(i2s) != div))
10015033f43cSJassi Brar 			|| (other && other->bfs && (other->bfs != div))) {
1002dc938ddbSMarek Szyprowski 			pm_runtime_put(dai->dev);
10035033f43cSJassi Brar 			dev_err(&i2s->pdev->dev,
10045033f43cSJassi Brar 				"%s:%d Other DAI busy\n", __func__, __LINE__);
10055033f43cSJassi Brar 			return -EAGAIN;
10065033f43cSJassi Brar 		}
10075033f43cSJassi Brar 		i2s->bfs = div;
1008dc938ddbSMarek Szyprowski 		pm_runtime_put(dai->dev);
10095033f43cSJassi Brar 		break;
10105033f43cSJassi Brar 	default:
10115033f43cSJassi Brar 		dev_err(&i2s->pdev->dev,
10125033f43cSJassi Brar 			"Invalid clock divider(%d)\n", div_id);
10135033f43cSJassi Brar 		return -EINVAL;
10145033f43cSJassi Brar 	}
10155033f43cSJassi Brar 
10165033f43cSJassi Brar 	return 0;
10175033f43cSJassi Brar }
10185033f43cSJassi Brar 
10195033f43cSJassi Brar static snd_pcm_sframes_t
i2s_delay(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)10205033f43cSJassi Brar i2s_delay(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
10215033f43cSJassi Brar {
1022e2e16fa6SSylwester Nawrocki 	struct samsung_i2s_priv *priv = snd_soc_dai_get_drvdata(dai);
10235033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
1024e2e16fa6SSylwester Nawrocki 	u32 reg = readl(priv->addr + I2SFIC);
10255033f43cSJassi Brar 	snd_pcm_sframes_t delay;
10265033f43cSJassi Brar 
1027dc938ddbSMarek Szyprowski 	WARN_ON(!pm_runtime_active(dai->dev));
1028dc938ddbSMarek Szyprowski 
10295033f43cSJassi Brar 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
10305033f43cSJassi Brar 		delay = FIC_RXCOUNT(reg);
10315033f43cSJassi Brar 	else if (is_secondary(i2s))
1032e2e16fa6SSylwester Nawrocki 		delay = FICS_TXCOUNT(readl(priv->addr + I2SFICS));
10335033f43cSJassi Brar 	else
10345bfaeddcSSylwester Nawrocki 		delay = (reg >> priv->variant_regs->ftx0cnt_off) & 0x7f;
10355033f43cSJassi Brar 
10365033f43cSJassi Brar 	return delay;
10375033f43cSJassi Brar }
10385033f43cSJassi Brar 
10395033f43cSJassi Brar #ifdef CONFIG_PM
i2s_suspend(struct snd_soc_component * component)104021385a4fSKuninori Morimoto static int i2s_suspend(struct snd_soc_component *component)
10415033f43cSJassi Brar {
104221385a4fSKuninori Morimoto 	return pm_runtime_force_suspend(component->dev);
10435033f43cSJassi Brar }
10445033f43cSJassi Brar 
i2s_resume(struct snd_soc_component * component)104521385a4fSKuninori Morimoto static int i2s_resume(struct snd_soc_component *component)
10465033f43cSJassi Brar {
104721385a4fSKuninori Morimoto 	return pm_runtime_force_resume(component->dev);
10485033f43cSJassi Brar }
10495033f43cSJassi Brar #else
10505033f43cSJassi Brar #define i2s_suspend NULL
10515033f43cSJassi Brar #define i2s_resume  NULL
10525033f43cSJassi Brar #endif
10535033f43cSJassi Brar 
samsung_i2s_dai_probe(struct snd_soc_dai * dai)10545033f43cSJassi Brar static int samsung_i2s_dai_probe(struct snd_soc_dai *dai)
10555033f43cSJassi Brar {
1056e2e16fa6SSylwester Nawrocki 	struct samsung_i2s_priv *priv = snd_soc_dai_get_drvdata(dai);
10575033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
1058dcd60fc3SSylwester Nawrocki 	struct i2s_dai *other = get_other_dai(i2s);
1059ce8bcdbbSSylwester Nawrocki 	unsigned long flags;
10605033f43cSJassi Brar 
1061dc938ddbSMarek Szyprowski 	pm_runtime_get_sync(dai->dev);
1062dc938ddbSMarek Szyprowski 
10639f9f8a5bSSylwester Nawrocki 	if (is_secondary(i2s)) {
10649f9f8a5bSSylwester Nawrocki 		/* If this is probe on the secondary DAI */
1065eb540d39SSylwester Nawrocki 		snd_soc_dai_init_dma_data(dai, &i2s->dma_playback, NULL);
1066872c26bdSSylwester Nawrocki 	} else {
106769e7a69aSSylwester Nawrocki 		snd_soc_dai_init_dma_data(dai, &i2s->dma_playback,
1068872c26bdSSylwester Nawrocki 					  &i2s->dma_capture);
1069511e3033SMark Brown 
10705944170fSSylwester Nawrocki 		if (priv->quirks & QUIRK_NEED_RSTCLR)
1071e2e16fa6SSylwester Nawrocki 			writel(CON_RSTCLR, priv->addr + I2SCON);
10725033f43cSJassi Brar 
10735944170fSSylwester Nawrocki 		if (priv->quirks & QUIRK_SUPPORTS_IDMA)
1074e2e16fa6SSylwester Nawrocki 			idma_reg_addr_init(priv->addr,
1075eb540d39SSylwester Nawrocki 					   other->idma_playback.addr);
1076872c26bdSSylwester Nawrocki 	}
107761100f40SSangbeom Kim 
10785033f43cSJassi Brar 	/* Reset any constraint on RFS and BFS */
10795033f43cSJassi Brar 	i2s->rfs = 0;
10805033f43cSJassi Brar 	i2s->bfs = 0;
1081ce8bcdbbSSylwester Nawrocki 
10829d7939c9SSylwester Nawrocki 	spin_lock_irqsave(&priv->lock, flags);
10835033f43cSJassi Brar 	i2s_txctrl(i2s, 0);
10845033f43cSJassi Brar 	i2s_rxctrl(i2s, 0);
10855033f43cSJassi Brar 	i2s_fifo(i2s, FIC_TXFLUSH);
10865033f43cSJassi Brar 	i2s_fifo(other, FIC_TXFLUSH);
10875033f43cSJassi Brar 	i2s_fifo(i2s, FIC_RXFLUSH);
10889d7939c9SSylwester Nawrocki 	spin_unlock_irqrestore(&priv->lock, flags);
10895033f43cSJassi Brar 
10905033f43cSJassi Brar 	/* Gate CDCLK by default */
10915033f43cSJassi Brar 	if (!is_opened(other))
10925033f43cSJassi Brar 		i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK,
10935033f43cSJassi Brar 				0, SND_SOC_CLOCK_IN);
1094dc938ddbSMarek Szyprowski 	pm_runtime_put(dai->dev);
10955033f43cSJassi Brar 
10965033f43cSJassi Brar 	return 0;
10975033f43cSJassi Brar }
10985033f43cSJassi Brar 
samsung_i2s_dai_remove(struct snd_soc_dai * dai)10995033f43cSJassi Brar static int samsung_i2s_dai_remove(struct snd_soc_dai *dai)
11005033f43cSJassi Brar {
1101e2e16fa6SSylwester Nawrocki 	struct samsung_i2s_priv *priv = snd_soc_dai_get_drvdata(dai);
1102a404b72dSSylwester Nawrocki 	struct i2s_dai *i2s = to_info(dai);
11035faf071dSCharles Keepax 	unsigned long flags;
11045033f43cSJassi Brar 
1105dc938ddbSMarek Szyprowski 	pm_runtime_get_sync(dai->dev);
1106dc938ddbSMarek Szyprowski 
1107c92f1d0eSSylwester Nawrocki 	if (!is_secondary(i2s)) {
11085944170fSSylwester Nawrocki 		if (priv->quirks & QUIRK_NEED_RSTCLR) {
11099d7939c9SSylwester Nawrocki 			spin_lock_irqsave(&priv->lock, flags);
1110e2e16fa6SSylwester Nawrocki 			writel(0, priv->addr + I2SCON);
11119d7939c9SSylwester Nawrocki 			spin_unlock_irqrestore(&priv->lock, flags);
1112ce8bcdbbSSylwester Nawrocki 		}
11135033f43cSJassi Brar 	}
11145033f43cSJassi Brar 
1115dc938ddbSMarek Szyprowski 	pm_runtime_put(dai->dev);
1116dc938ddbSMarek Szyprowski 
11175033f43cSJassi Brar 	return 0;
11185033f43cSJassi Brar }
11195033f43cSJassi Brar 
112085e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops samsung_i2s_dai_ops = {
1121fda5c5e7SKuninori Morimoto 	.probe = samsung_i2s_dai_probe,
1122fda5c5e7SKuninori Morimoto 	.remove = samsung_i2s_dai_remove,
11235033f43cSJassi Brar 	.trigger = i2s_trigger,
11245033f43cSJassi Brar 	.hw_params = i2s_hw_params,
1125b99d00c7SCharles Keepax 	.set_fmt = i2s_set_fmt,
11265033f43cSJassi Brar 	.set_clkdiv = i2s_set_clkdiv,
11275033f43cSJassi Brar 	.set_sysclk = i2s_set_sysclk,
11285033f43cSJassi Brar 	.startup = i2s_startup,
11295033f43cSJassi Brar 	.shutdown = i2s_shutdown,
11305033f43cSJassi Brar 	.delay = i2s_delay,
11315033f43cSJassi Brar };
11325033f43cSJassi Brar 
113364aba9bcSSylwester Nawrocki static const struct snd_soc_dapm_widget samsung_i2s_widgets[] = {
113464aba9bcSSylwester Nawrocki 	/* Backend DAI  */
113564aba9bcSSylwester Nawrocki 	SND_SOC_DAPM_AIF_OUT("Mixer DAI TX", NULL, 0, SND_SOC_NOPM, 0, 0),
113664aba9bcSSylwester Nawrocki 	SND_SOC_DAPM_AIF_IN("Mixer DAI RX", NULL, 0, SND_SOC_NOPM, 0, 0),
113764aba9bcSSylwester Nawrocki 
113864aba9bcSSylwester Nawrocki 	/* Playback Mixer */
113964aba9bcSSylwester Nawrocki 	SND_SOC_DAPM_MIXER("Playback Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
114064aba9bcSSylwester Nawrocki };
114164aba9bcSSylwester Nawrocki 
114264aba9bcSSylwester Nawrocki static const struct snd_soc_dapm_route samsung_i2s_dapm_routes[] = {
114342e4ceddSSylwester Nawrocki 	{ "Playback Mixer", NULL, "Primary Playback" },
114442e4ceddSSylwester Nawrocki 	{ "Playback Mixer", NULL, "Secondary Playback" },
114564aba9bcSSylwester Nawrocki 
114664aba9bcSSylwester Nawrocki 	{ "Mixer DAI TX", NULL, "Playback Mixer" },
114742e4ceddSSylwester Nawrocki 	{ "Primary Capture", NULL, "Mixer DAI RX" },
114864aba9bcSSylwester Nawrocki };
114964aba9bcSSylwester Nawrocki 
11504b828535SKuninori Morimoto static const struct snd_soc_component_driver samsung_i2s_component = {
11514b828535SKuninori Morimoto 	.name = "samsung-i2s",
115264aba9bcSSylwester Nawrocki 
115364aba9bcSSylwester Nawrocki 	.dapm_widgets = samsung_i2s_widgets,
115464aba9bcSSylwester Nawrocki 	.num_dapm_widgets = ARRAY_SIZE(samsung_i2s_widgets),
115564aba9bcSSylwester Nawrocki 
115664aba9bcSSylwester Nawrocki 	.dapm_routes = samsung_i2s_dapm_routes,
115764aba9bcSSylwester Nawrocki 	.num_dapm_routes = ARRAY_SIZE(samsung_i2s_dapm_routes),
115821385a4fSKuninori Morimoto 
115921385a4fSKuninori Morimoto 	.suspend = i2s_suspend,
116021385a4fSKuninori Morimoto 	.resume = i2s_resume,
1161f7bfa516SCharles Keepax 
1162f7bfa516SCharles Keepax 	.legacy_dai_naming = 1,
11634b828535SKuninori Morimoto };
11644b828535SKuninori Morimoto 
1165c5ba6192SSylwester Nawrocki #define SAMSUNG_I2S_FMTS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE | \
11665033f43cSJassi Brar 			  SNDRV_PCM_FMTBIT_S24_LE)
11675033f43cSJassi Brar 
i2s_alloc_dais(struct samsung_i2s_priv * priv,const struct samsung_i2s_dai_data * i2s_dai_data,int num_dais)1168a404b72dSSylwester Nawrocki static int i2s_alloc_dais(struct samsung_i2s_priv *priv,
11694720c2feSJaechul Lee 			  const struct samsung_i2s_dai_data *i2s_dai_data,
1170a404b72dSSylwester Nawrocki 			  int num_dais)
11715033f43cSJassi Brar {
1172a404b72dSSylwester Nawrocki 	static const char *dai_names[] = { "samsung-i2s", "samsung-i2s-sec" };
117342e4ceddSSylwester Nawrocki 	static const char *stream_names[] = { "Primary Playback",
117442e4ceddSSylwester Nawrocki 					      "Secondary Playback" };
1175a404b72dSSylwester Nawrocki 	struct snd_soc_dai_driver *dai_drv;
1176a404b72dSSylwester Nawrocki 	int i;
11775033f43cSJassi Brar 
1178a404b72dSSylwester Nawrocki 	priv->dai = devm_kcalloc(&priv->pdev->dev, num_dais,
11795a81abc2SPierre-Louis Bossart 				     sizeof(struct i2s_dai), GFP_KERNEL);
1180a404b72dSSylwester Nawrocki 	if (!priv->dai)
1181a404b72dSSylwester Nawrocki 		return -ENOMEM;
11825033f43cSJassi Brar 
1183a404b72dSSylwester Nawrocki 	priv->dai_drv = devm_kcalloc(&priv->pdev->dev, num_dais,
1184a404b72dSSylwester Nawrocki 				     sizeof(*dai_drv), GFP_KERNEL);
1185a404b72dSSylwester Nawrocki 	if (!priv->dai_drv)
1186a404b72dSSylwester Nawrocki 		return -ENOMEM;
11875033f43cSJassi Brar 
1188a404b72dSSylwester Nawrocki 	for (i = 0; i < num_dais; i++) {
1189a404b72dSSylwester Nawrocki 		dai_drv = &priv->dai_drv[i];
1190a404b72dSSylwester Nawrocki 
1191d11ff0bfSKuninori Morimoto 		dai_drv->symmetric_rate = 1;
1192a404b72dSSylwester Nawrocki 		dai_drv->ops = &samsung_i2s_dai_ops;
1193a404b72dSSylwester Nawrocki 
1194a404b72dSSylwester Nawrocki 		dai_drv->playback.channels_min = 1;
1195a404b72dSSylwester Nawrocki 		dai_drv->playback.channels_max = 2;
1196a404b72dSSylwester Nawrocki 		dai_drv->playback.rates = i2s_dai_data->pcm_rates;
1197a404b72dSSylwester Nawrocki 		dai_drv->playback.formats = SAMSUNG_I2S_FMTS;
119864aba9bcSSylwester Nawrocki 		dai_drv->playback.stream_name = stream_names[i];
1199a404b72dSSylwester Nawrocki 
1200a404b72dSSylwester Nawrocki 		dai_drv->id = i + 1;
1201a404b72dSSylwester Nawrocki 		dai_drv->name = dai_names[i];
1202a404b72dSSylwester Nawrocki 
1203a404b72dSSylwester Nawrocki 		priv->dai[i].drv = &priv->dai_drv[i];
1204a404b72dSSylwester Nawrocki 		priv->dai[i].pdev = priv->pdev;
1205c6f9b1ebSPrathyush K 	}
1206a404b72dSSylwester Nawrocki 
1207a404b72dSSylwester Nawrocki 	/* Initialize capture only for the primary DAI */
1208a404b72dSSylwester Nawrocki 	dai_drv = &priv->dai_drv[SAMSUNG_I2S_ID_PRIMARY - 1];
1209a404b72dSSylwester Nawrocki 
1210a404b72dSSylwester Nawrocki 	dai_drv->capture.channels_min = 1;
1211a404b72dSSylwester Nawrocki 	dai_drv->capture.channels_max = 2;
1212a404b72dSSylwester Nawrocki 	dai_drv->capture.rates = i2s_dai_data->pcm_rates;
1213a404b72dSSylwester Nawrocki 	dai_drv->capture.formats = SAMSUNG_I2S_FMTS;
121442e4ceddSSylwester Nawrocki 	dai_drv->capture.stream_name = "Primary Capture";
1215a404b72dSSylwester Nawrocki 
1216a404b72dSSylwester Nawrocki 	return 0;
12175033f43cSJassi Brar }
12185033f43cSJassi Brar 
1219641d334bSRafael J. Wysocki #ifdef CONFIG_PM
i2s_runtime_suspend(struct device * dev)12205b1d3c34SR. Chandrasekar static int i2s_runtime_suspend(struct device *dev)
12215b1d3c34SR. Chandrasekar {
1222b5d015e6SSylwester Nawrocki 	struct samsung_i2s_priv *priv = dev_get_drvdata(dev);
12235b1d3c34SR. Chandrasekar 
1224e2e16fa6SSylwester Nawrocki 	priv->suspend_i2smod = readl(priv->addr + I2SMOD);
1225e2e16fa6SSylwester Nawrocki 	priv->suspend_i2scon = readl(priv->addr + I2SCON);
1226e2e16fa6SSylwester Nawrocki 	priv->suspend_i2spsr = readl(priv->addr + I2SPSR);
1227e7e52dfcSMarek Szyprowski 
12283b0fa51fSSylwester Nawrocki 	clk_disable_unprepare(priv->op_clk);
1229b5d015e6SSylwester Nawrocki 	clk_disable_unprepare(priv->clk);
12305b1d3c34SR. Chandrasekar 
12315b1d3c34SR. Chandrasekar 	return 0;
12325b1d3c34SR. Chandrasekar }
12335b1d3c34SR. Chandrasekar 
i2s_runtime_resume(struct device * dev)12345b1d3c34SR. Chandrasekar static int i2s_runtime_resume(struct device *dev)
12355b1d3c34SR. Chandrasekar {
1236b5d015e6SSylwester Nawrocki 	struct samsung_i2s_priv *priv = dev_get_drvdata(dev);
1237f5c97c7bSArvind Yadav 	int ret;
12385b1d3c34SR. Chandrasekar 
1239b5d015e6SSylwester Nawrocki 	ret = clk_prepare_enable(priv->clk);
1240f5c97c7bSArvind Yadav 	if (ret)
1241f5c97c7bSArvind Yadav 		return ret;
1242f5c97c7bSArvind Yadav 
12433b0fa51fSSylwester Nawrocki 	if (priv->op_clk) {
12443b0fa51fSSylwester Nawrocki 		ret = clk_prepare_enable(priv->op_clk);
1245f5c97c7bSArvind Yadav 		if (ret) {
1246b5d015e6SSylwester Nawrocki 			clk_disable_unprepare(priv->clk);
1247f5c97c7bSArvind Yadav 			return ret;
1248f5c97c7bSArvind Yadav 		}
1249f5c97c7bSArvind Yadav 	}
12505b1d3c34SR. Chandrasekar 
1251e2e16fa6SSylwester Nawrocki 	writel(priv->suspend_i2scon, priv->addr + I2SCON);
1252e2e16fa6SSylwester Nawrocki 	writel(priv->suspend_i2smod, priv->addr + I2SMOD);
1253e2e16fa6SSylwester Nawrocki 	writel(priv->suspend_i2spsr, priv->addr + I2SPSR);
12545b1d3c34SR. Chandrasekar 
12555b1d3c34SR. Chandrasekar 	return 0;
12565b1d3c34SR. Chandrasekar }
1257641d334bSRafael J. Wysocki #endif /* CONFIG_PM */
12585b1d3c34SR. Chandrasekar 
i2s_unregister_clocks(struct samsung_i2s_priv * priv)125989d2e831SSylwester Nawrocki static void i2s_unregister_clocks(struct samsung_i2s_priv *priv)
1260074b89bbSSylwester Nawrocki {
1261074b89bbSSylwester Nawrocki 	int i;
1262074b89bbSSylwester Nawrocki 
126389d2e831SSylwester Nawrocki 	for (i = 0; i < priv->clk_data.clk_num; i++) {
126489d2e831SSylwester Nawrocki 		if (!IS_ERR(priv->clk_table[i]))
126589d2e831SSylwester Nawrocki 			clk_unregister(priv->clk_table[i]);
1266074b89bbSSylwester Nawrocki 	}
1267074b89bbSSylwester Nawrocki }
1268074b89bbSSylwester Nawrocki 
i2s_unregister_clock_provider(struct samsung_i2s_priv * priv)126989d2e831SSylwester Nawrocki static void i2s_unregister_clock_provider(struct samsung_i2s_priv *priv)
1270074b89bbSSylwester Nawrocki {
127189d2e831SSylwester Nawrocki 	of_clk_del_provider(priv->pdev->dev.of_node);
127289d2e831SSylwester Nawrocki 	i2s_unregister_clocks(priv);
1273074b89bbSSylwester Nawrocki }
1274074b89bbSSylwester Nawrocki 
127589d2e831SSylwester Nawrocki 
i2s_register_clock_provider(struct samsung_i2s_priv * priv)127689d2e831SSylwester Nawrocki static int i2s_register_clock_provider(struct samsung_i2s_priv *priv)
1277074b89bbSSylwester Nawrocki {
1278a404b72dSSylwester Nawrocki 
1279aa274c5cSSylwester Nawrocki 	const char * const i2s_clk_desc[] = { "cdclk", "rclk_src", "prescaler" };
1280074b89bbSSylwester Nawrocki 	const char *clk_name[2] = { "i2s_opclk0", "i2s_opclk1" };
1281074b89bbSSylwester Nawrocki 	const char *p_names[2] = { NULL };
128289d2e831SSylwester Nawrocki 	struct device *dev = &priv->pdev->dev;
12835bfaeddcSSylwester Nawrocki 	const struct samsung_i2s_variant_regs *reg_info = priv->variant_regs;
1284aa274c5cSSylwester Nawrocki 	const char *i2s_clk_name[ARRAY_SIZE(i2s_clk_desc)];
1285074b89bbSSylwester Nawrocki 	struct clk *rclksrc;
1286074b89bbSSylwester Nawrocki 	int ret, i;
1287074b89bbSSylwester Nawrocki 
1288074b89bbSSylwester Nawrocki 	/* Register the clock provider only if it's expected in the DTB */
12891e108e60SRob Herring 	if (!of_property_present(dev->of_node, "#clock-cells"))
1290074b89bbSSylwester Nawrocki 		return 0;
1291074b89bbSSylwester Nawrocki 
1292074b89bbSSylwester Nawrocki 	/* Get the RCLKSRC mux clock parent clock names */
1293074b89bbSSylwester Nawrocki 	for (i = 0; i < ARRAY_SIZE(p_names); i++) {
1294074b89bbSSylwester Nawrocki 		rclksrc = clk_get(dev, clk_name[i]);
1295074b89bbSSylwester Nawrocki 		if (IS_ERR(rclksrc))
1296074b89bbSSylwester Nawrocki 			continue;
1297074b89bbSSylwester Nawrocki 		p_names[i] = __clk_get_name(rclksrc);
1298074b89bbSSylwester Nawrocki 		clk_put(rclksrc);
1299074b89bbSSylwester Nawrocki 	}
1300074b89bbSSylwester Nawrocki 
1301aa274c5cSSylwester Nawrocki 	for (i = 0; i < ARRAY_SIZE(i2s_clk_desc); i++) {
1302aa274c5cSSylwester Nawrocki 		i2s_clk_name[i] = devm_kasprintf(dev, GFP_KERNEL, "%s_%s",
1303aa274c5cSSylwester Nawrocki 						dev_name(dev), i2s_clk_desc[i]);
1304aa274c5cSSylwester Nawrocki 		if (!i2s_clk_name[i])
1305aa274c5cSSylwester Nawrocki 			return -ENOMEM;
1306aa274c5cSSylwester Nawrocki 	}
1307aa274c5cSSylwester Nawrocki 
13085944170fSSylwester Nawrocki 	if (!(priv->quirks & QUIRK_NO_MUXPSR)) {
1309074b89bbSSylwester Nawrocki 		/* Activate the prescaler */
1310e2e16fa6SSylwester Nawrocki 		u32 val = readl(priv->addr + I2SPSR);
1311e2e16fa6SSylwester Nawrocki 		writel(val | PSR_PSREN, priv->addr + I2SPSR);
1312074b89bbSSylwester Nawrocki 
131389d2e831SSylwester Nawrocki 		priv->clk_table[CLK_I2S_RCLK_SRC] = clk_register_mux(dev,
1314aa274c5cSSylwester Nawrocki 				i2s_clk_name[CLK_I2S_RCLK_SRC], p_names,
1315aa274c5cSSylwester Nawrocki 				ARRAY_SIZE(p_names),
1316074b89bbSSylwester Nawrocki 				CLK_SET_RATE_NO_REPARENT | CLK_SET_RATE_PARENT,
1317e2e16fa6SSylwester Nawrocki 				priv->addr + I2SMOD, reg_info->rclksrc_off,
13189d7939c9SSylwester Nawrocki 				1, 0, &priv->lock);
1319074b89bbSSylwester Nawrocki 
132089d2e831SSylwester Nawrocki 		priv->clk_table[CLK_I2S_RCLK_PSR] = clk_register_divider(dev,
1321aa274c5cSSylwester Nawrocki 				i2s_clk_name[CLK_I2S_RCLK_PSR],
1322aa274c5cSSylwester Nawrocki 				i2s_clk_name[CLK_I2S_RCLK_SRC],
1323074b89bbSSylwester Nawrocki 				CLK_SET_RATE_PARENT,
13249d7939c9SSylwester Nawrocki 				priv->addr + I2SPSR, 8, 6, 0, &priv->lock);
1325074b89bbSSylwester Nawrocki 
1326aa274c5cSSylwester Nawrocki 		p_names[0] = i2s_clk_name[CLK_I2S_RCLK_PSR];
132789d2e831SSylwester Nawrocki 		priv->clk_data.clk_num = 2;
1328074b89bbSSylwester Nawrocki 	}
1329074b89bbSSylwester Nawrocki 
133089d2e831SSylwester Nawrocki 	priv->clk_table[CLK_I2S_CDCLK] = clk_register_gate(dev,
1331aa274c5cSSylwester Nawrocki 				i2s_clk_name[CLK_I2S_CDCLK], p_names[0],
1332aa274c5cSSylwester Nawrocki 				CLK_SET_RATE_PARENT,
1333e2e16fa6SSylwester Nawrocki 				priv->addr + I2SMOD, reg_info->cdclkcon_off,
13349d7939c9SSylwester Nawrocki 				CLK_GATE_SET_TO_DISABLE, &priv->lock);
1335074b89bbSSylwester Nawrocki 
133689d2e831SSylwester Nawrocki 	priv->clk_data.clk_num += 1;
133789d2e831SSylwester Nawrocki 	priv->clk_data.clks = priv->clk_table;
1338074b89bbSSylwester Nawrocki 
1339074b89bbSSylwester Nawrocki 	ret = of_clk_add_provider(dev->of_node, of_clk_src_onecell_get,
134089d2e831SSylwester Nawrocki 				  &priv->clk_data);
1341074b89bbSSylwester Nawrocki 	if (ret < 0) {
1342074b89bbSSylwester Nawrocki 		dev_err(dev, "failed to add clock provider: %d\n", ret);
134389d2e831SSylwester Nawrocki 		i2s_unregister_clocks(priv);
1344074b89bbSSylwester Nawrocki 	}
1345074b89bbSSylwester Nawrocki 
1346074b89bbSSylwester Nawrocki 	return ret;
1347074b89bbSSylwester Nawrocki }
1348074b89bbSSylwester Nawrocki 
13497196c64cSSylwester Nawrocki /* Create platform device for the secondary PCM */
i2s_create_secondary_device(struct samsung_i2s_priv * priv)13507196c64cSSylwester Nawrocki static int i2s_create_secondary_device(struct samsung_i2s_priv *priv)
13517196c64cSSylwester Nawrocki {
1352c6bebefaSSylwester Nawrocki 	struct platform_device *pdev_sec;
1353c6bebefaSSylwester Nawrocki 	const char *devname;
13547196c64cSSylwester Nawrocki 	int ret;
13557196c64cSSylwester Nawrocki 
1356c6bebefaSSylwester Nawrocki 	devname = devm_kasprintf(&priv->pdev->dev, GFP_KERNEL, "%s-sec",
1357c6bebefaSSylwester Nawrocki 				 dev_name(&priv->pdev->dev));
1358c6bebefaSSylwester Nawrocki 	if (!devname)
13597196c64cSSylwester Nawrocki 		return -ENOMEM;
13607196c64cSSylwester Nawrocki 
1361c6bebefaSSylwester Nawrocki 	pdev_sec = platform_device_alloc(devname, -1);
1362c6bebefaSSylwester Nawrocki 	if (!pdev_sec)
1363c6bebefaSSylwester Nawrocki 		return -ENOMEM;
1364c6bebefaSSylwester Nawrocki 
1365c6bebefaSSylwester Nawrocki 	pdev_sec->driver_override = kstrdup("samsung-i2s", GFP_KERNEL);
1366d39664cbSXiaoke Wang 	if (!pdev_sec->driver_override) {
1367d39664cbSXiaoke Wang 		platform_device_put(pdev_sec);
1368d39664cbSXiaoke Wang 		return -ENOMEM;
1369d39664cbSXiaoke Wang 	}
1370c6bebefaSSylwester Nawrocki 
1371c6bebefaSSylwester Nawrocki 	ret = platform_device_add(pdev_sec);
13727196c64cSSylwester Nawrocki 	if (ret < 0) {
1373c6bebefaSSylwester Nawrocki 		platform_device_put(pdev_sec);
13747196c64cSSylwester Nawrocki 		return ret;
13757196c64cSSylwester Nawrocki 	}
13767196c64cSSylwester Nawrocki 
1377c6bebefaSSylwester Nawrocki 	ret = device_attach(&pdev_sec->dev);
1378c6bebefaSSylwester Nawrocki 	if (ret <= 0) {
1379c6bebefaSSylwester Nawrocki 		platform_device_unregister(priv->pdev_sec);
1380c6bebefaSSylwester Nawrocki 		dev_info(&pdev_sec->dev, "device_attach() failed\n");
1381c6bebefaSSylwester Nawrocki 		return ret;
1382c6bebefaSSylwester Nawrocki 	}
1383c6bebefaSSylwester Nawrocki 
1384c6bebefaSSylwester Nawrocki 	priv->pdev_sec = pdev_sec;
13857196c64cSSylwester Nawrocki 
13867196c64cSSylwester Nawrocki 	return 0;
13877196c64cSSylwester Nawrocki }
13887196c64cSSylwester Nawrocki 
i2s_delete_secondary_device(struct samsung_i2s_priv * priv)13897196c64cSSylwester Nawrocki static void i2s_delete_secondary_device(struct samsung_i2s_priv *priv)
13907196c64cSSylwester Nawrocki {
1391022c4156SSylwester Nawrocki 	platform_device_unregister(priv->pdev_sec);
13927196c64cSSylwester Nawrocki 	priv->pdev_sec = NULL;
13937196c64cSSylwester Nawrocki }
1394022c4156SSylwester Nawrocki 
samsung_i2s_probe(struct platform_device * pdev)1395fdca21adSBill Pemberton static int samsung_i2s_probe(struct platform_device *pdev)
13965033f43cSJassi Brar {
13975033f43cSJassi Brar 	struct i2s_dai *pri_dai, *sec_dai = NULL;
139840476f61SPadmavathi Venna 	struct s3c_audio_pdata *i2s_pdata = pdev->dev.platform_data;
13996e434122SSylwester Nawrocki 	u32 regs_base, idma_addr = 0;
140040476f61SPadmavathi Venna 	struct device_node *np = pdev->dev.of_node;
14017da493e9SPadmavathi Venna 	const struct samsung_i2s_dai_data *i2s_dai_data;
1402c6bebefaSSylwester Nawrocki 	const struct platform_device_id *id;
1403a404b72dSSylwester Nawrocki 	struct samsung_i2s_priv *priv;
1404c6bebefaSSylwester Nawrocki 	struct resource *res;
1405c6bebefaSSylwester Nawrocki 	int num_dais, ret;
14065033f43cSJassi Brar 
1407c6bebefaSSylwester Nawrocki 	if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
14082f7b5d14SSylwester Nawrocki 		i2s_dai_data = of_device_get_match_data(&pdev->dev);
1409c6bebefaSSylwester Nawrocki 	} else {
1410c6bebefaSSylwester Nawrocki 		id = platform_get_device_id(pdev);
14117c62eebbSPadmavathi Venna 
14127196c64cSSylwester Nawrocki 		/* Nothing to do if it is the secondary device probe */
1413c6bebefaSSylwester Nawrocki 		if (!id)
14147196c64cSSylwester Nawrocki 			return 0;
14157196c64cSSylwester Nawrocki 
1416c6bebefaSSylwester Nawrocki 		i2s_dai_data = (struct samsung_i2s_dai_data *)id->driver_data;
1417c6bebefaSSylwester Nawrocki 	}
1418c6bebefaSSylwester Nawrocki 
1419a404b72dSSylwester Nawrocki 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1420a404b72dSSylwester Nawrocki 	if (!priv)
142140476f61SPadmavathi Venna 		return -ENOMEM;
142240476f61SPadmavathi Venna 
14236e434122SSylwester Nawrocki 	if (np) {
14246e434122SSylwester Nawrocki 		priv->quirks = i2s_dai_data->quirks;
1425bc36d761SPadmanabhan Rajanbabu 		priv->fixup_early = i2s_dai_data->fixup_early;
1426bc36d761SPadmanabhan Rajanbabu 		priv->fixup_late = i2s_dai_data->fixup_late;
14276e434122SSylwester Nawrocki 	} else {
14286e434122SSylwester Nawrocki 		if (!i2s_pdata) {
14296e434122SSylwester Nawrocki 			dev_err(&pdev->dev, "Missing platform data\n");
14306e434122SSylwester Nawrocki 			return -EINVAL;
14316e434122SSylwester Nawrocki 		}
14326e434122SSylwester Nawrocki 		priv->quirks = i2s_pdata->type.quirks;
14336e434122SSylwester Nawrocki 	}
14346e434122SSylwester Nawrocki 
14356e434122SSylwester Nawrocki 	num_dais = (priv->quirks & QUIRK_SEC_DAI) ? 2 : 1;
1436a404b72dSSylwester Nawrocki 	priv->pdev = pdev;
14375944170fSSylwester Nawrocki 	priv->variant_regs = i2s_dai_data->i2s_variant_regs;
1438a404b72dSSylwester Nawrocki 
1439a404b72dSSylwester Nawrocki 	ret = i2s_alloc_dais(priv, i2s_dai_data, num_dais);
1440a404b72dSSylwester Nawrocki 	if (ret < 0)
1441a404b72dSSylwester Nawrocki 		return ret;
1442a404b72dSSylwester Nawrocki 
1443a404b72dSSylwester Nawrocki 	pri_dai = &priv->dai[SAMSUNG_I2S_ID_PRIMARY - 1];
1444a404b72dSSylwester Nawrocki 
14459d7939c9SSylwester Nawrocki 	spin_lock_init(&priv->lock);
1446defc67c6SSylwester Nawrocki 	spin_lock_init(&priv->pcm_lock);
1447f3670536SSylwester Nawrocki 
144840476f61SPadmavathi Venna 	if (!np) {
144969e7a69aSSylwester Nawrocki 		pri_dai->dma_playback.filter_data = i2s_pdata->dma_playback;
145069e7a69aSSylwester Nawrocki 		pri_dai->dma_capture.filter_data = i2s_pdata->dma_capture;
14519bdca822SArnd Bergmann 		pri_dai->filter = i2s_pdata->dma_filter;
1452b9a1a743SArnd Bergmann 
1453409c69beSKrzysztof Kozlowski 		idma_addr = i2s_pdata->type.idma_addr;
145440476f61SPadmavathi Venna 	} else {
145540476f61SPadmavathi Venna 		if (of_property_read_u32(np, "samsung,idma-addr",
145640476f61SPadmavathi Venna 					 &idma_addr)) {
14576e434122SSylwester Nawrocki 			if (priv->quirks & QUIRK_SUPPORTS_IDMA) {
1458b0759736SPadmavathi Venna 				dev_info(&pdev->dev, "idma address is not"\
145940476f61SPadmavathi Venna 						"specified");
146040476f61SPadmavathi Venna 			}
146140476f61SPadmavathi Venna 		}
146240476f61SPadmavathi Venna 	}
14635033f43cSJassi Brar 
14645eb8262cSYang Yingliang 	priv->addr = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
1465e2e16fa6SSylwester Nawrocki 	if (IS_ERR(priv->addr))
1466e2e16fa6SSylwester Nawrocki 		return PTR_ERR(priv->addr);
14675033f43cSJassi Brar 
14685033f43cSJassi Brar 	regs_base = res->start;
14695033f43cSJassi Brar 
1470b5d015e6SSylwester Nawrocki 	priv->clk = devm_clk_get(&pdev->dev, "iis");
1471b5d015e6SSylwester Nawrocki 	if (IS_ERR(priv->clk)) {
14720ec2ba80SSylwester Nawrocki 		dev_err(&pdev->dev, "Failed to get iis clock\n");
1473b5d015e6SSylwester Nawrocki 		return PTR_ERR(priv->clk);
14740ec2ba80SSylwester Nawrocki 	}
1475c92f1d0eSSylwester Nawrocki 
1476b5d015e6SSylwester Nawrocki 	ret = clk_prepare_enable(priv->clk);
1477c92f1d0eSSylwester Nawrocki 	if (ret != 0) {
1478c92f1d0eSSylwester Nawrocki 		dev_err(&pdev->dev, "failed to enable clock: %d\n", ret);
1479c92f1d0eSSylwester Nawrocki 		return ret;
1480c92f1d0eSSylwester Nawrocki 	}
148169e7a69aSSylwester Nawrocki 	pri_dai->dma_playback.addr = regs_base + I2STXD;
148269e7a69aSSylwester Nawrocki 	pri_dai->dma_capture.addr = regs_base + I2SRXD;
1483b8ab0cccSSylwester Nawrocki 	pri_dai->dma_playback.chan_name = "tx";
1484b8ab0cccSSylwester Nawrocki 	pri_dai->dma_capture.chan_name = "rx";
148569e7a69aSSylwester Nawrocki 	pri_dai->dma_playback.addr_width = 4;
148669e7a69aSSylwester Nawrocki 	pri_dai->dma_capture.addr_width = 4;
148789d2e831SSylwester Nawrocki 	pri_dai->priv = priv;
14885033f43cSJassi Brar 
14896e434122SSylwester Nawrocki 	if (priv->quirks & QUIRK_PRI_6CHAN)
1490a404b72dSSylwester Nawrocki 		pri_dai->drv->playback.channels_max = 6;
14915033f43cSJassi Brar 
149273f5dfc6SMarek Szyprowski 	ret = samsung_asoc_dma_platform_register(&pdev->dev, pri_dai->filter,
14930f928c19SSylwester Nawrocki 						 "tx", "rx", NULL);
149473f5dfc6SMarek Szyprowski 	if (ret < 0)
149573f5dfc6SMarek Szyprowski 		goto err_disable_clk;
149673f5dfc6SMarek Szyprowski 
14976e434122SSylwester Nawrocki 	if (priv->quirks & QUIRK_SEC_DAI) {
1498a404b72dSSylwester Nawrocki 		sec_dai = &priv->dai[SAMSUNG_I2S_ID_SECONDARY - 1];
14997e5d8706SSylwester Nawrocki 
150069e7a69aSSylwester Nawrocki 		sec_dai->dma_playback.addr = regs_base + I2STXDS;
1501b8ab0cccSSylwester Nawrocki 		sec_dai->dma_playback.chan_name = "tx-sec";
150240476f61SPadmavathi Venna 
15039bdca822SArnd Bergmann 		if (!np) {
150469e7a69aSSylwester Nawrocki 			sec_dai->dma_playback.filter_data = i2s_pdata->dma_play_sec;
15059bdca822SArnd Bergmann 			sec_dai->filter = i2s_pdata->dma_filter;
15069bdca822SArnd Bergmann 		}
150740476f61SPadmavathi Venna 
150869e7a69aSSylwester Nawrocki 		sec_dai->dma_playback.addr_width = 4;
150969e7a69aSSylwester Nawrocki 		sec_dai->idma_playback.addr = idma_addr;
15105033f43cSJassi Brar 		sec_dai->pri_dai = pri_dai;
151189d2e831SSylwester Nawrocki 		sec_dai->priv = priv;
15125033f43cSJassi Brar 		pri_dai->sec_dai = sec_dai;
1513be2c92ebSMarek Szyprowski 
15147196c64cSSylwester Nawrocki 		ret = i2s_create_secondary_device(priv);
15157196c64cSSylwester Nawrocki 		if (ret < 0)
15167196c64cSSylwester Nawrocki 			goto err_disable_clk;
15177196c64cSSylwester Nawrocki 
15187196c64cSSylwester Nawrocki 		ret = samsung_asoc_dma_platform_register(&priv->pdev_sec->dev,
15197196c64cSSylwester Nawrocki 						sec_dai->filter, "tx-sec", NULL,
15207196c64cSSylwester Nawrocki 						&pdev->dev);
1521be2c92ebSMarek Szyprowski 		if (ret < 0)
1522022c4156SSylwester Nawrocki 			goto err_del_sec;
1523be2c92ebSMarek Szyprowski 
15245033f43cSJassi Brar 	}
15255033f43cSJassi Brar 
15260429ffefSMark Brown 	if (i2s_pdata && i2s_pdata->cfg_gpio && i2s_pdata->cfg_gpio(pdev)) {
152740476f61SPadmavathi Venna 		dev_err(&pdev->dev, "Unable to configure gpio\n");
1528fd61576fSWei Yongjun 		ret = -EINVAL;
1529022c4156SSylwester Nawrocki 		goto err_del_sec;
153040476f61SPadmavathi Venna 	}
15315033f43cSJassi Brar 
1532a404b72dSSylwester Nawrocki 	dev_set_drvdata(&pdev->dev, priv);
1533a404b72dSSylwester Nawrocki 
1534a404b72dSSylwester Nawrocki 	ret = devm_snd_soc_register_component(&pdev->dev,
1535a404b72dSSylwester Nawrocki 					&samsung_i2s_component,
1536a404b72dSSylwester Nawrocki 					priv->dai_drv, num_dais);
1537a404b72dSSylwester Nawrocki 	if (ret < 0)
1538022c4156SSylwester Nawrocki 		goto err_del_sec;
15392b960386SSylwester Nawrocki 
1540dc938ddbSMarek Szyprowski 	pm_runtime_set_active(&pdev->dev);
1541c5cf4dbcSMark Brown 	pm_runtime_enable(&pdev->dev);
1542c5cf4dbcSMark Brown 
154389d2e831SSylwester Nawrocki 	ret = i2s_register_clock_provider(priv);
154448279c53SSylwester Nawrocki 	if (ret < 0)
154548279c53SSylwester Nawrocki 		goto err_disable_pm;
154648279c53SSylwester Nawrocki 
15473b0fa51fSSylwester Nawrocki 	priv->op_clk = clk_get_parent(priv->clk_table[CLK_I2S_RCLK_SRC]);
154848279c53SSylwester Nawrocki 
15492b960386SSylwester Nawrocki 	return 0;
1550a08485d8SPadmavathi Venna 
155148279c53SSylwester Nawrocki err_disable_pm:
15522b960386SSylwester Nawrocki 	pm_runtime_disable(&pdev->dev);
1553022c4156SSylwester Nawrocki err_del_sec:
1554022c4156SSylwester Nawrocki 	i2s_delete_secondary_device(priv);
1555fd61576fSWei Yongjun err_disable_clk:
1556b5d015e6SSylwester Nawrocki 	clk_disable_unprepare(priv->clk);
15572b960386SSylwester Nawrocki 	return ret;
15585033f43cSJassi Brar }
15595033f43cSJassi Brar 
samsung_i2s_remove(struct platform_device * pdev)15600b309358SUwe Kleine-König static void samsung_i2s_remove(struct platform_device *pdev)
15615033f43cSJassi Brar {
1562a404b72dSSylwester Nawrocki 	struct samsung_i2s_priv *priv = dev_get_drvdata(&pdev->dev);
1563be2c92ebSMarek Szyprowski 
15647196c64cSSylwester Nawrocki 	/* The secondary device has no driver data assigned */
15657196c64cSSylwester Nawrocki 	if (!priv)
15660b309358SUwe Kleine-König 		return;
15677196c64cSSylwester Nawrocki 
1568dc938ddbSMarek Szyprowski 	pm_runtime_get_sync(&pdev->dev);
1569c5cf4dbcSMark Brown 	pm_runtime_disable(&pdev->dev);
15705033f43cSJassi Brar 
157189d2e831SSylwester Nawrocki 	i2s_unregister_clock_provider(priv);
15727196c64cSSylwester Nawrocki 	i2s_delete_secondary_device(priv);
1573022c4156SSylwester Nawrocki 	clk_disable_unprepare(priv->clk);
1574022c4156SSylwester Nawrocki 
1575022c4156SSylwester Nawrocki 	pm_runtime_put_noidle(&pdev->dev);
15765033f43cSJassi Brar }
15775033f43cSJassi Brar 
fsd_i2s_fixup_early(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)1578bc36d761SPadmanabhan Rajanbabu static void fsd_i2s_fixup_early(struct snd_pcm_substream *substream,
1579bc36d761SPadmanabhan Rajanbabu 		struct snd_soc_dai *dai)
1580bc36d761SPadmanabhan Rajanbabu {
158121b6cd54SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
158221b6cd54SKuninori Morimoto 	struct i2s_dai *i2s = to_info(snd_soc_rtd_to_cpu(rtd, 0));
1583bc36d761SPadmanabhan Rajanbabu 	struct i2s_dai *other = get_other_dai(i2s);
1584bc36d761SPadmanabhan Rajanbabu 
1585bc36d761SPadmanabhan Rajanbabu 	if (!is_opened(other)) {
1586bc36d761SPadmanabhan Rajanbabu 		i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK, 0, SND_SOC_CLOCK_OUT);
1587bc36d761SPadmanabhan Rajanbabu 		i2s_set_sysclk(dai, SAMSUNG_I2S_OPCLK, 0, MOD_OPCLK_PCLK);
1588bc36d761SPadmanabhan Rajanbabu 	}
1589bc36d761SPadmanabhan Rajanbabu }
1590bc36d761SPadmanabhan Rajanbabu 
fsd_i2s_fixup_late(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)1591bc36d761SPadmanabhan Rajanbabu static void fsd_i2s_fixup_late(struct snd_pcm_substream *substream,
1592bc36d761SPadmanabhan Rajanbabu 		struct snd_soc_dai *dai)
1593bc36d761SPadmanabhan Rajanbabu {
159421b6cd54SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
1595bc36d761SPadmanabhan Rajanbabu 	struct samsung_i2s_priv *priv = snd_soc_dai_get_drvdata(dai);
159621b6cd54SKuninori Morimoto 	struct i2s_dai *i2s = to_info(snd_soc_rtd_to_cpu(rtd, 0));
1597bc36d761SPadmanabhan Rajanbabu 	struct i2s_dai *other = get_other_dai(i2s);
1598bc36d761SPadmanabhan Rajanbabu 
1599bc36d761SPadmanabhan Rajanbabu 	if (!is_opened(other))
1600bc36d761SPadmanabhan Rajanbabu 		writel(PSR_PSVAL(2) | PSR_PSREN, priv->addr + I2SPSR);
1601bc36d761SPadmanabhan Rajanbabu }
1602bc36d761SPadmanabhan Rajanbabu 
1603a5a56871SPadmavathi Venna static const struct samsung_i2s_variant_regs i2sv3_regs = {
1604a5a56871SPadmavathi Venna 	.bfs_off = 1,
1605a5a56871SPadmavathi Venna 	.rfs_off = 3,
1606a5a56871SPadmavathi Venna 	.sdf_off = 5,
1607a5a56871SPadmavathi Venna 	.txr_off = 8,
1608a5a56871SPadmavathi Venna 	.rclksrc_off = 10,
1609a5a56871SPadmavathi Venna 	.mss_off = 11,
1610a5a56871SPadmavathi Venna 	.cdclkcon_off = 12,
1611a5a56871SPadmavathi Venna 	.lrp_off = 7,
1612a5a56871SPadmavathi Venna 	.bfs_mask = 0x3,
1613a5a56871SPadmavathi Venna 	.rfs_mask = 0x3,
1614a5a56871SPadmavathi Venna 	.ftx0cnt_off = 8,
1615a5a56871SPadmavathi Venna };
1616a5a56871SPadmavathi Venna 
1617a5a56871SPadmavathi Venna static const struct samsung_i2s_variant_regs i2sv6_regs = {
1618a5a56871SPadmavathi Venna 	.bfs_off = 0,
1619a5a56871SPadmavathi Venna 	.rfs_off = 4,
1620a5a56871SPadmavathi Venna 	.sdf_off = 6,
1621a5a56871SPadmavathi Venna 	.txr_off = 8,
1622a5a56871SPadmavathi Venna 	.rclksrc_off = 10,
1623a5a56871SPadmavathi Venna 	.mss_off = 11,
1624a5a56871SPadmavathi Venna 	.cdclkcon_off = 12,
1625a5a56871SPadmavathi Venna 	.lrp_off = 15,
1626a5a56871SPadmavathi Venna 	.bfs_mask = 0xf,
1627a5a56871SPadmavathi Venna 	.rfs_mask = 0x3,
1628a5a56871SPadmavathi Venna 	.ftx0cnt_off = 8,
1629a5a56871SPadmavathi Venna };
1630a5a56871SPadmavathi Venna 
1631a5a56871SPadmavathi Venna static const struct samsung_i2s_variant_regs i2sv7_regs = {
1632a5a56871SPadmavathi Venna 	.bfs_off = 0,
1633a5a56871SPadmavathi Venna 	.rfs_off = 4,
1634a5a56871SPadmavathi Venna 	.sdf_off = 7,
1635a5a56871SPadmavathi Venna 	.txr_off = 9,
1636a5a56871SPadmavathi Venna 	.rclksrc_off = 11,
1637a5a56871SPadmavathi Venna 	.mss_off = 12,
1638a5a56871SPadmavathi Venna 	.cdclkcon_off = 22,
1639a5a56871SPadmavathi Venna 	.lrp_off = 15,
1640a5a56871SPadmavathi Venna 	.bfs_mask = 0xf,
1641a5a56871SPadmavathi Venna 	.rfs_mask = 0x7,
1642a5a56871SPadmavathi Venna 	.ftx0cnt_off = 0,
1643a5a56871SPadmavathi Venna };
1644a5a56871SPadmavathi Venna 
1645a5a56871SPadmavathi Venna static const struct samsung_i2s_variant_regs i2sv5_i2s1_regs = {
1646a5a56871SPadmavathi Venna 	.bfs_off = 0,
1647a5a56871SPadmavathi Venna 	.rfs_off = 3,
1648a5a56871SPadmavathi Venna 	.sdf_off = 6,
1649a5a56871SPadmavathi Venna 	.txr_off = 8,
1650a5a56871SPadmavathi Venna 	.rclksrc_off = 10,
1651a5a56871SPadmavathi Venna 	.mss_off = 11,
1652a5a56871SPadmavathi Venna 	.cdclkcon_off = 12,
1653a5a56871SPadmavathi Venna 	.lrp_off = 15,
1654a5a56871SPadmavathi Venna 	.bfs_mask = 0x7,
1655a5a56871SPadmavathi Venna 	.rfs_mask = 0x7,
1656a5a56871SPadmavathi Venna 	.ftx0cnt_off = 8,
1657a5a56871SPadmavathi Venna };
1658a5a56871SPadmavathi Venna 
16597da493e9SPadmavathi Venna static const struct samsung_i2s_dai_data i2sv3_dai_type = {
16607da493e9SPadmavathi Venna 	.quirks = QUIRK_NO_MUXPSR,
16614720c2feSJaechul Lee 	.pcm_rates = SNDRV_PCM_RATE_8000_96000,
1662a5a56871SPadmavathi Venna 	.i2s_variant_regs = &i2sv3_regs,
16637da493e9SPadmavathi Venna };
16647da493e9SPadmavathi Venna 
1665da773b2bSKrzysztof Kozlowski static const struct samsung_i2s_dai_data i2sv5_dai_type __maybe_unused = {
1666b0759736SPadmavathi Venna 	.quirks = QUIRK_PRI_6CHAN | QUIRK_SEC_DAI | QUIRK_NEED_RSTCLR |
1667b0759736SPadmavathi Venna 			QUIRK_SUPPORTS_IDMA,
16684720c2feSJaechul Lee 	.pcm_rates = SNDRV_PCM_RATE_8000_96000,
1669a5a56871SPadmavathi Venna 	.i2s_variant_regs = &i2sv3_regs,
16707da493e9SPadmavathi Venna };
16717da493e9SPadmavathi Venna 
1672da773b2bSKrzysztof Kozlowski static const struct samsung_i2s_dai_data i2sv6_dai_type __maybe_unused = {
16734ca0c0d4SPadmavathi Venna 	.quirks = QUIRK_PRI_6CHAN | QUIRK_SEC_DAI | QUIRK_NEED_RSTCLR |
1674b0759736SPadmavathi Venna 			QUIRK_SUPPORTS_TDM | QUIRK_SUPPORTS_IDMA,
16754720c2feSJaechul Lee 	.pcm_rates = SNDRV_PCM_RATE_8000_96000,
1676a5a56871SPadmavathi Venna 	.i2s_variant_regs = &i2sv6_regs,
1677a5a56871SPadmavathi Venna };
1678a5a56871SPadmavathi Venna 
1679da773b2bSKrzysztof Kozlowski static const struct samsung_i2s_dai_data i2sv7_dai_type __maybe_unused = {
1680a5a56871SPadmavathi Venna 	.quirks = QUIRK_PRI_6CHAN | QUIRK_SEC_DAI | QUIRK_NEED_RSTCLR |
1681a5a56871SPadmavathi Venna 			QUIRK_SUPPORTS_TDM,
16824720c2feSJaechul Lee 	.pcm_rates = SNDRV_PCM_RATE_8000_192000,
1683a5a56871SPadmavathi Venna 	.i2s_variant_regs = &i2sv7_regs,
1684a5a56871SPadmavathi Venna };
1685a5a56871SPadmavathi Venna 
1686da773b2bSKrzysztof Kozlowski static const struct samsung_i2s_dai_data i2sv5_dai_type_i2s1 __maybe_unused = {
1687a5a56871SPadmavathi Venna 	.quirks = QUIRK_PRI_6CHAN | QUIRK_NEED_RSTCLR,
16884720c2feSJaechul Lee 	.pcm_rates = SNDRV_PCM_RATE_8000_96000,
1689a5a56871SPadmavathi Venna 	.i2s_variant_regs = &i2sv5_i2s1_regs,
16904ca0c0d4SPadmavathi Venna };
16914ca0c0d4SPadmavathi Venna 
1692bc36d761SPadmanabhan Rajanbabu static const struct samsung_i2s_dai_data fsd_dai_type __maybe_unused = {
1693bc36d761SPadmanabhan Rajanbabu 	.quirks = QUIRK_SEC_DAI | QUIRK_NEED_RSTCLR | QUIRK_SUPPORTS_TDM,
1694bc36d761SPadmanabhan Rajanbabu 	.pcm_rates = SNDRV_PCM_RATE_8000_192000,
1695bc36d761SPadmanabhan Rajanbabu 	.i2s_variant_regs = &i2sv7_regs,
1696bc36d761SPadmanabhan Rajanbabu 	.fixup_early = fsd_i2s_fixup_early,
1697bc36d761SPadmanabhan Rajanbabu 	.fixup_late = fsd_i2s_fixup_late,
1698bc36d761SPadmanabhan Rajanbabu };
1699bc36d761SPadmanabhan Rajanbabu 
1700eb8ca0faSKrzysztof Kozlowski static const struct platform_device_id samsung_i2s_driver_ids[] = {
17017c62eebbSPadmavathi Venna 	{
17027c62eebbSPadmavathi Venna 		.name           = "samsung-i2s",
17033f024980SMark Brown 		.driver_data	= (kernel_ulong_t)&i2sv3_dai_type,
17047c62eebbSPadmavathi Venna 	},
17057c62eebbSPadmavathi Venna 	{},
17067c62eebbSPadmavathi Venna };
17072af19558SArnd Bergmann MODULE_DEVICE_TABLE(platform, samsung_i2s_driver_ids);
17087c62eebbSPadmavathi Venna 
170940476f61SPadmavathi Venna #ifdef CONFIG_OF
171040476f61SPadmavathi Venna static const struct of_device_id exynos_i2s_match[] = {
17117da493e9SPadmavathi Venna 	{
17127da493e9SPadmavathi Venna 		.compatible = "samsung,s3c6410-i2s",
17137da493e9SPadmavathi Venna 		.data = &i2sv3_dai_type,
17147da493e9SPadmavathi Venna 	}, {
17157da493e9SPadmavathi Venna 		.compatible = "samsung,s5pv210-i2s",
17167da493e9SPadmavathi Venna 		.data = &i2sv5_dai_type,
17174ca0c0d4SPadmavathi Venna 	}, {
17184ca0c0d4SPadmavathi Venna 		.compatible = "samsung,exynos5420-i2s",
17194ca0c0d4SPadmavathi Venna 		.data = &i2sv6_dai_type,
1720a5a56871SPadmavathi Venna 	}, {
1721a5a56871SPadmavathi Venna 		.compatible = "samsung,exynos7-i2s",
1722a5a56871SPadmavathi Venna 		.data = &i2sv7_dai_type,
1723a5a56871SPadmavathi Venna 	}, {
1724a5a56871SPadmavathi Venna 		.compatible = "samsung,exynos7-i2s1",
1725a5a56871SPadmavathi Venna 		.data = &i2sv5_dai_type_i2s1,
1726bc36d761SPadmanabhan Rajanbabu 	}, {
1727bc36d761SPadmanabhan Rajanbabu 		.compatible = "tesla,fsd-i2s",
1728bc36d761SPadmanabhan Rajanbabu 		.data = &fsd_dai_type,
172940476f61SPadmavathi Venna 	},
173040476f61SPadmavathi Venna 	{},
173140476f61SPadmavathi Venna };
173240476f61SPadmavathi Venna MODULE_DEVICE_TABLE(of, exynos_i2s_match);
173340476f61SPadmavathi Venna #endif
173440476f61SPadmavathi Venna 
17355b1d3c34SR. Chandrasekar static const struct dev_pm_ops samsung_i2s_pm = {
17365b1d3c34SR. Chandrasekar 	SET_RUNTIME_PM_OPS(i2s_runtime_suspend,
17375b1d3c34SR. Chandrasekar 				i2s_runtime_resume, NULL)
1738e7e52dfcSMarek Szyprowski 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1739e7e52dfcSMarek Szyprowski 				     pm_runtime_force_resume)
17405b1d3c34SR. Chandrasekar };
17415b1d3c34SR. Chandrasekar 
17425033f43cSJassi Brar static struct platform_driver samsung_i2s_driver = {
17435033f43cSJassi Brar 	.probe  = samsung_i2s_probe,
1744*130af75bSUwe Kleine-König 	.remove = samsung_i2s_remove,
17457c62eebbSPadmavathi Venna 	.id_table = samsung_i2s_driver_ids,
17465033f43cSJassi Brar 	.driver = {
17475033f43cSJassi Brar 		.name = "samsung-i2s",
174840476f61SPadmavathi Venna 		.of_match_table = of_match_ptr(exynos_i2s_match),
17495b1d3c34SR. Chandrasekar 		.pm = &samsung_i2s_pm,
17505033f43cSJassi Brar 	},
17515033f43cSJassi Brar };
17525033f43cSJassi Brar 
1753e00c3f55SMark Brown module_platform_driver(samsung_i2s_driver);
17545033f43cSJassi Brar 
17555033f43cSJassi Brar /* Module information */
1756df8ad335SJaswinder Singh MODULE_AUTHOR("Jaswinder Singh, <jassisinghbrar@gmail.com>");
17575033f43cSJassi Brar MODULE_DESCRIPTION("Samsung I2S Interface");
17585033f43cSJassi Brar MODULE_LICENSE("GPL");
1759