xref: /linux/sound/soc/samsung/i2s.c (revision fd61576fdda88c9677f9ea62e6f32997ce39daea)
15033f43cSJassi Brar /* sound/soc/samsung/i2s.c
25033f43cSJassi Brar  *
35033f43cSJassi Brar  * ALSA SoC Audio Layer - Samsung I2S Controller driver
45033f43cSJassi Brar  *
55033f43cSJassi Brar  * Copyright (c) 2010 Samsung Electronics Co. Ltd.
6df8ad335SJaswinder Singh  *	Jaswinder Singh <jassisinghbrar@gmail.com>
75033f43cSJassi Brar  *
85033f43cSJassi Brar  * This program is free software; you can redistribute it and/or modify
95033f43cSJassi Brar  * it under the terms of the GNU General Public License version 2 as
105033f43cSJassi Brar  * published by the Free Software Foundation.
115033f43cSJassi Brar  */
125033f43cSJassi Brar 
13074b89bbSSylwester Nawrocki #include <dt-bindings/sound/samsung-i2s.h>
145033f43cSJassi Brar #include <linux/delay.h>
155033f43cSJassi Brar #include <linux/slab.h>
165033f43cSJassi Brar #include <linux/clk.h>
17074b89bbSSylwester Nawrocki #include <linux/clk-provider.h>
185033f43cSJassi Brar #include <linux/io.h>
19da155d5bSPaul Gortmaker #include <linux/module.h>
2040476f61SPadmavathi Venna #include <linux/of.h>
212f7b5d14SSylwester Nawrocki #include <linux/of_device.h>
2240476f61SPadmavathi Venna #include <linux/of_gpio.h>
23c5cf4dbcSMark Brown #include <linux/pm_runtime.h>
245033f43cSJassi Brar 
255033f43cSJassi Brar #include <sound/soc.h>
260378b6acSSeungwhan Youn #include <sound/pcm_params.h>
275033f43cSJassi Brar 
28436d42c6SArnd Bergmann #include <linux/platform_data/asoc-s3c.h>
295033f43cSJassi Brar 
305033f43cSJassi Brar #include "dma.h"
3161100f40SSangbeom Kim #include "idma.h"
325033f43cSJassi Brar #include "i2s.h"
33172a453dSSangbeom Kim #include "i2s-regs.h"
345033f43cSJassi Brar 
355033f43cSJassi Brar #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
365033f43cSJassi Brar 
377c62eebbSPadmavathi Venna enum samsung_dai_type {
387c62eebbSPadmavathi Venna 	TYPE_PRI,
397c62eebbSPadmavathi Venna 	TYPE_SEC,
407c62eebbSPadmavathi Venna };
417c62eebbSPadmavathi Venna 
42a5a56871SPadmavathi Venna struct samsung_i2s_variant_regs {
43a5a56871SPadmavathi Venna 	unsigned int	bfs_off;
44a5a56871SPadmavathi Venna 	unsigned int	rfs_off;
45a5a56871SPadmavathi Venna 	unsigned int	sdf_off;
46a5a56871SPadmavathi Venna 	unsigned int	txr_off;
47a5a56871SPadmavathi Venna 	unsigned int	rclksrc_off;
48a5a56871SPadmavathi Venna 	unsigned int	mss_off;
49a5a56871SPadmavathi Venna 	unsigned int	cdclkcon_off;
50a5a56871SPadmavathi Venna 	unsigned int	lrp_off;
51a5a56871SPadmavathi Venna 	unsigned int	bfs_mask;
52a5a56871SPadmavathi Venna 	unsigned int	rfs_mask;
53a5a56871SPadmavathi Venna 	unsigned int	ftx0cnt_off;
54a5a56871SPadmavathi Venna };
55a5a56871SPadmavathi Venna 
5640476f61SPadmavathi Venna struct samsung_i2s_dai_data {
5740476f61SPadmavathi Venna 	int dai_type;
587da493e9SPadmavathi Venna 	u32 quirks;
59a5a56871SPadmavathi Venna 	const struct samsung_i2s_variant_regs *i2s_variant_regs;
6040476f61SPadmavathi Venna };
6140476f61SPadmavathi Venna 
625033f43cSJassi Brar struct i2s_dai {
635033f43cSJassi Brar 	/* Platform device for this DAI */
645033f43cSJassi Brar 	struct platform_device *pdev;
65af1cf5cfSSylwester Nawrocki 	/* Memory mapped SFR region */
665033f43cSJassi Brar 	void __iomem	*addr;
675033f43cSJassi Brar 	/* Rate of RCLK source clock */
685033f43cSJassi Brar 	unsigned long rclk_srcrate;
695033f43cSJassi Brar 	/* Frame Clock */
705033f43cSJassi Brar 	unsigned frmclk;
715033f43cSJassi Brar 	/*
725033f43cSJassi Brar 	 * Specifically requested RCLK,BCLK by MACHINE Driver.
735033f43cSJassi Brar 	 * 0 indicates CPU driver is free to choose any value.
745033f43cSJassi Brar 	 */
755033f43cSJassi Brar 	unsigned rfs, bfs;
765033f43cSJassi Brar 	/* I2S Controller's core clock */
775033f43cSJassi Brar 	struct clk *clk;
785033f43cSJassi Brar 	/* Clock for generating I2S signals */
795033f43cSJassi Brar 	struct clk *op_clk;
805033f43cSJassi Brar 	/* Pointer to the Primary_Fifo if this is Sec_Fifo, NULL otherwise */
815033f43cSJassi Brar 	struct i2s_dai *pri_dai;
825033f43cSJassi Brar 	/* Pointer to the Secondary_Fifo if it has one, NULL otherwise */
835033f43cSJassi Brar 	struct i2s_dai *sec_dai;
845033f43cSJassi Brar #define DAI_OPENED	(1 << 0) /* Dai is opened */
855033f43cSJassi Brar #define DAI_MANAGER	(1 << 1) /* Dai is the manager */
865033f43cSJassi Brar 	unsigned mode;
875033f43cSJassi Brar 	/* Driver for this DAI */
885033f43cSJassi Brar 	struct snd_soc_dai_driver i2s_dai_drv;
895033f43cSJassi Brar 	/* DMA parameters */
9069e7a69aSSylwester Nawrocki 	struct snd_dmaengine_dai_dma_data dma_playback;
9169e7a69aSSylwester Nawrocki 	struct snd_dmaengine_dai_dma_data dma_capture;
9269e7a69aSSylwester Nawrocki 	struct snd_dmaengine_dai_dma_data idma_playback;
939bdca822SArnd Bergmann 	dma_filter_fn filter;
945033f43cSJassi Brar 	u32	quirks;
955033f43cSJassi Brar 	u32	suspend_i2smod;
965033f43cSJassi Brar 	u32	suspend_i2scon;
975033f43cSJassi Brar 	u32	suspend_i2spsr;
98a5a56871SPadmavathi Venna 	const struct samsung_i2s_variant_regs *variant_regs;
99f3670536SSylwester Nawrocki 
100f3670536SSylwester Nawrocki 	/* Spinlock protecting access to the device's registers */
101f3670536SSylwester Nawrocki 	spinlock_t spinlock;
102f3670536SSylwester Nawrocki 	spinlock_t *lock;
103074b89bbSSylwester Nawrocki 
104074b89bbSSylwester Nawrocki 	/* Below fields are only valid if this is the primary FIFO */
105074b89bbSSylwester Nawrocki 	struct clk *clk_table[3];
106074b89bbSSylwester Nawrocki 	struct clk_onecell_data clk_data;
1075033f43cSJassi Brar };
1085033f43cSJassi Brar 
1095033f43cSJassi Brar /* Lock for cross i/f checks */
1105033f43cSJassi Brar static DEFINE_SPINLOCK(lock);
1115033f43cSJassi Brar 
1125033f43cSJassi Brar /* If this is the 'overlay' stereo DAI */
1135033f43cSJassi Brar static inline bool is_secondary(struct i2s_dai *i2s)
1145033f43cSJassi Brar {
1155033f43cSJassi Brar 	return i2s->pri_dai ? true : false;
1165033f43cSJassi Brar }
1175033f43cSJassi Brar 
1185033f43cSJassi Brar /* If operating in SoC-Slave mode */
1195033f43cSJassi Brar static inline bool is_slave(struct i2s_dai *i2s)
1205033f43cSJassi Brar {
121a5a56871SPadmavathi Venna 	u32 mod = readl(i2s->addr + I2SMOD);
122a5a56871SPadmavathi Venna 	return (mod & (1 << i2s->variant_regs->mss_off)) ? true : false;
1235033f43cSJassi Brar }
1245033f43cSJassi Brar 
1255033f43cSJassi Brar /* If this interface of the controller is transmitting data */
1265033f43cSJassi Brar static inline bool tx_active(struct i2s_dai *i2s)
1275033f43cSJassi Brar {
1285033f43cSJassi Brar 	u32 active;
1295033f43cSJassi Brar 
1305033f43cSJassi Brar 	if (!i2s)
1315033f43cSJassi Brar 		return false;
1325033f43cSJassi Brar 
13333195500SSangbeom Kim 	active = readl(i2s->addr + I2SCON);
1345033f43cSJassi Brar 
1355033f43cSJassi Brar 	if (is_secondary(i2s))
1365033f43cSJassi Brar 		active &= CON_TXSDMA_ACTIVE;
1375033f43cSJassi Brar 	else
1385033f43cSJassi Brar 		active &= CON_TXDMA_ACTIVE;
1395033f43cSJassi Brar 
1405033f43cSJassi Brar 	return active ? true : false;
1415033f43cSJassi Brar }
1425033f43cSJassi Brar 
143dcd60fc3SSylwester Nawrocki /* Return pointer to the other DAI */
144dcd60fc3SSylwester Nawrocki static inline struct i2s_dai *get_other_dai(struct i2s_dai *i2s)
145dcd60fc3SSylwester Nawrocki {
146dcd60fc3SSylwester Nawrocki 	return i2s->pri_dai ? : i2s->sec_dai;
147dcd60fc3SSylwester Nawrocki }
148dcd60fc3SSylwester Nawrocki 
1495033f43cSJassi Brar /* If the other interface of the controller is transmitting data */
1505033f43cSJassi Brar static inline bool other_tx_active(struct i2s_dai *i2s)
1515033f43cSJassi Brar {
152dcd60fc3SSylwester Nawrocki 	struct i2s_dai *other = get_other_dai(i2s);
1535033f43cSJassi Brar 
1545033f43cSJassi Brar 	return tx_active(other);
1555033f43cSJassi Brar }
1565033f43cSJassi Brar 
1575033f43cSJassi Brar /* If any interface of the controller is transmitting data */
1585033f43cSJassi Brar static inline bool any_tx_active(struct i2s_dai *i2s)
1595033f43cSJassi Brar {
1605033f43cSJassi Brar 	return tx_active(i2s) || other_tx_active(i2s);
1615033f43cSJassi Brar }
1625033f43cSJassi Brar 
1635033f43cSJassi Brar /* If this interface of the controller is receiving data */
1645033f43cSJassi Brar static inline bool rx_active(struct i2s_dai *i2s)
1655033f43cSJassi Brar {
1665033f43cSJassi Brar 	u32 active;
1675033f43cSJassi Brar 
1685033f43cSJassi Brar 	if (!i2s)
1695033f43cSJassi Brar 		return false;
1705033f43cSJassi Brar 
17133195500SSangbeom Kim 	active = readl(i2s->addr + I2SCON) & CON_RXDMA_ACTIVE;
1725033f43cSJassi Brar 
1735033f43cSJassi Brar 	return active ? true : false;
1745033f43cSJassi Brar }
1755033f43cSJassi Brar 
1765033f43cSJassi Brar /* If the other interface of the controller is receiving data */
1775033f43cSJassi Brar static inline bool other_rx_active(struct i2s_dai *i2s)
1785033f43cSJassi Brar {
179dcd60fc3SSylwester Nawrocki 	struct i2s_dai *other = get_other_dai(i2s);
1805033f43cSJassi Brar 
1815033f43cSJassi Brar 	return rx_active(other);
1825033f43cSJassi Brar }
1835033f43cSJassi Brar 
1845033f43cSJassi Brar /* If any interface of the controller is receiving data */
1855033f43cSJassi Brar static inline bool any_rx_active(struct i2s_dai *i2s)
1865033f43cSJassi Brar {
1875033f43cSJassi Brar 	return rx_active(i2s) || other_rx_active(i2s);
1885033f43cSJassi Brar }
1895033f43cSJassi Brar 
1905033f43cSJassi Brar /* If the other DAI is transmitting or receiving data */
1915033f43cSJassi Brar static inline bool other_active(struct i2s_dai *i2s)
1925033f43cSJassi Brar {
1935033f43cSJassi Brar 	return other_rx_active(i2s) || other_tx_active(i2s);
1945033f43cSJassi Brar }
1955033f43cSJassi Brar 
1965033f43cSJassi Brar /* If this DAI is transmitting or receiving data */
1975033f43cSJassi Brar static inline bool this_active(struct i2s_dai *i2s)
1985033f43cSJassi Brar {
1995033f43cSJassi Brar 	return tx_active(i2s) || rx_active(i2s);
2005033f43cSJassi Brar }
2015033f43cSJassi Brar 
2025033f43cSJassi Brar /* If the controller is active anyway */
2035033f43cSJassi Brar static inline bool any_active(struct i2s_dai *i2s)
2045033f43cSJassi Brar {
2055033f43cSJassi Brar 	return this_active(i2s) || other_active(i2s);
2065033f43cSJassi Brar }
2075033f43cSJassi Brar 
2085033f43cSJassi Brar static inline struct i2s_dai *to_info(struct snd_soc_dai *dai)
2095033f43cSJassi Brar {
2105033f43cSJassi Brar 	return snd_soc_dai_get_drvdata(dai);
2115033f43cSJassi Brar }
2125033f43cSJassi Brar 
2135033f43cSJassi Brar static inline bool is_opened(struct i2s_dai *i2s)
2145033f43cSJassi Brar {
2155033f43cSJassi Brar 	if (i2s && (i2s->mode & DAI_OPENED))
2165033f43cSJassi Brar 		return true;
2175033f43cSJassi Brar 	else
2185033f43cSJassi Brar 		return false;
2195033f43cSJassi Brar }
2205033f43cSJassi Brar 
2215033f43cSJassi Brar static inline bool is_manager(struct i2s_dai *i2s)
2225033f43cSJassi Brar {
2235033f43cSJassi Brar 	if (is_opened(i2s) && (i2s->mode & DAI_MANAGER))
2245033f43cSJassi Brar 		return true;
2255033f43cSJassi Brar 	else
2265033f43cSJassi Brar 		return false;
2275033f43cSJassi Brar }
2285033f43cSJassi Brar 
2295033f43cSJassi Brar /* Read RCLK of I2S (in multiples of LRCLK) */
2305033f43cSJassi Brar static inline unsigned get_rfs(struct i2s_dai *i2s)
2315033f43cSJassi Brar {
2324ca0c0d4SPadmavathi Venna 	u32 rfs;
233a5a56871SPadmavathi Venna 	rfs = readl(i2s->addr + I2SMOD) >> i2s->variant_regs->rfs_off;
234a5a56871SPadmavathi Venna 	rfs &= i2s->variant_regs->rfs_mask;
2355033f43cSJassi Brar 
2365033f43cSJassi Brar 	switch (rfs) {
237a5a56871SPadmavathi Venna 	case 7: return 192;
238a5a56871SPadmavathi Venna 	case 6: return 96;
239a5a56871SPadmavathi Venna 	case 5: return 128;
240a5a56871SPadmavathi Venna 	case 4: return 64;
2415033f43cSJassi Brar 	case 3:	return 768;
2425033f43cSJassi Brar 	case 2: return 384;
2435033f43cSJassi Brar 	case 1:	return 512;
2445033f43cSJassi Brar 	default: return 256;
2455033f43cSJassi Brar 	}
2465033f43cSJassi Brar }
2475033f43cSJassi Brar 
2485033f43cSJassi Brar /* Write RCLK of I2S (in multiples of LRCLK) */
2495033f43cSJassi Brar static inline void set_rfs(struct i2s_dai *i2s, unsigned rfs)
2505033f43cSJassi Brar {
2515033f43cSJassi Brar 	u32 mod = readl(i2s->addr + I2SMOD);
252a5a56871SPadmavathi Venna 	int rfs_shift = i2s->variant_regs->rfs_off;
2535033f43cSJassi Brar 
254a5a56871SPadmavathi Venna 	mod &= ~(i2s->variant_regs->rfs_mask << rfs_shift);
2555033f43cSJassi Brar 
2565033f43cSJassi Brar 	switch (rfs) {
257a5a56871SPadmavathi Venna 	case 192:
258a5a56871SPadmavathi Venna 		mod |= (EXYNOS7_MOD_RCLK_192FS << rfs_shift);
259a5a56871SPadmavathi Venna 		break;
260a5a56871SPadmavathi Venna 	case 96:
261a5a56871SPadmavathi Venna 		mod |= (EXYNOS7_MOD_RCLK_96FS << rfs_shift);
262a5a56871SPadmavathi Venna 		break;
263a5a56871SPadmavathi Venna 	case 128:
264a5a56871SPadmavathi Venna 		mod |= (EXYNOS7_MOD_RCLK_128FS << rfs_shift);
265a5a56871SPadmavathi Venna 		break;
266a5a56871SPadmavathi Venna 	case 64:
267a5a56871SPadmavathi Venna 		mod |= (EXYNOS7_MOD_RCLK_64FS << rfs_shift);
268a5a56871SPadmavathi Venna 		break;
2695033f43cSJassi Brar 	case 768:
270b60be4aaSPadmavathi Venna 		mod |= (MOD_RCLK_768FS << rfs_shift);
2715033f43cSJassi Brar 		break;
2725033f43cSJassi Brar 	case 512:
273b60be4aaSPadmavathi Venna 		mod |= (MOD_RCLK_512FS << rfs_shift);
2745033f43cSJassi Brar 		break;
2755033f43cSJassi Brar 	case 384:
276b60be4aaSPadmavathi Venna 		mod |= (MOD_RCLK_384FS << rfs_shift);
2775033f43cSJassi Brar 		break;
2785033f43cSJassi Brar 	default:
279b60be4aaSPadmavathi Venna 		mod |= (MOD_RCLK_256FS << rfs_shift);
2805033f43cSJassi Brar 		break;
2815033f43cSJassi Brar 	}
2825033f43cSJassi Brar 
2835033f43cSJassi Brar 	writel(mod, i2s->addr + I2SMOD);
2845033f43cSJassi Brar }
2855033f43cSJassi Brar 
2865033f43cSJassi Brar /* Read Bit-Clock of I2S (in multiples of LRCLK) */
2875033f43cSJassi Brar static inline unsigned get_bfs(struct i2s_dai *i2s)
2885033f43cSJassi Brar {
2894ca0c0d4SPadmavathi Venna 	u32 bfs;
290a5a56871SPadmavathi Venna 	bfs = readl(i2s->addr + I2SMOD) >> i2s->variant_regs->bfs_off;
291a5a56871SPadmavathi Venna 	bfs &= i2s->variant_regs->bfs_mask;
2925033f43cSJassi Brar 
2935033f43cSJassi Brar 	switch (bfs) {
2944ca0c0d4SPadmavathi Venna 	case 8: return 256;
2954ca0c0d4SPadmavathi Venna 	case 7: return 192;
2964ca0c0d4SPadmavathi Venna 	case 6: return 128;
2974ca0c0d4SPadmavathi Venna 	case 5: return 96;
2984ca0c0d4SPadmavathi Venna 	case 4: return 64;
2995033f43cSJassi Brar 	case 3: return 24;
3005033f43cSJassi Brar 	case 2: return 16;
3015033f43cSJassi Brar 	case 1:	return 48;
3025033f43cSJassi Brar 	default: return 32;
3035033f43cSJassi Brar 	}
3045033f43cSJassi Brar }
3055033f43cSJassi Brar 
3065033f43cSJassi Brar /* Write Bit-Clock of I2S (in multiples of LRCLK) */
3075033f43cSJassi Brar static inline void set_bfs(struct i2s_dai *i2s, unsigned bfs)
3085033f43cSJassi Brar {
3095033f43cSJassi Brar 	u32 mod = readl(i2s->addr + I2SMOD);
3104ca0c0d4SPadmavathi Venna 	int tdm = i2s->quirks & QUIRK_SUPPORTS_TDM;
311a5a56871SPadmavathi Venna 	int bfs_shift = i2s->variant_regs->bfs_off;
3124ca0c0d4SPadmavathi Venna 
3134ca0c0d4SPadmavathi Venna 	/* Non-TDM I2S controllers do not support BCLK > 48 * FS */
3144ca0c0d4SPadmavathi Venna 	if (!tdm && bfs > 48) {
3154ca0c0d4SPadmavathi Venna 		dev_err(&i2s->pdev->dev, "Unsupported BCLK divider\n");
3164ca0c0d4SPadmavathi Venna 		return;
3174ca0c0d4SPadmavathi Venna 	}
3185033f43cSJassi Brar 
319a5a56871SPadmavathi Venna 	mod &= ~(i2s->variant_regs->bfs_mask << bfs_shift);
320a5a56871SPadmavathi Venna 
3215033f43cSJassi Brar 	switch (bfs) {
3225033f43cSJassi Brar 	case 48:
323b60be4aaSPadmavathi Venna 		mod |= (MOD_BCLK_48FS << bfs_shift);
3245033f43cSJassi Brar 		break;
3255033f43cSJassi Brar 	case 32:
326b60be4aaSPadmavathi Venna 		mod |= (MOD_BCLK_32FS << bfs_shift);
3275033f43cSJassi Brar 		break;
3285033f43cSJassi Brar 	case 24:
329b60be4aaSPadmavathi Venna 		mod |= (MOD_BCLK_24FS << bfs_shift);
3305033f43cSJassi Brar 		break;
3315033f43cSJassi Brar 	case 16:
332b60be4aaSPadmavathi Venna 		mod |= (MOD_BCLK_16FS << bfs_shift);
3335033f43cSJassi Brar 		break;
3344ca0c0d4SPadmavathi Venna 	case 64:
3354ca0c0d4SPadmavathi Venna 		mod |= (EXYNOS5420_MOD_BCLK_64FS << bfs_shift);
3364ca0c0d4SPadmavathi Venna 		break;
3374ca0c0d4SPadmavathi Venna 	case 96:
3384ca0c0d4SPadmavathi Venna 		mod |= (EXYNOS5420_MOD_BCLK_96FS << bfs_shift);
3394ca0c0d4SPadmavathi Venna 		break;
3404ca0c0d4SPadmavathi Venna 	case 128:
3414ca0c0d4SPadmavathi Venna 		mod |= (EXYNOS5420_MOD_BCLK_128FS << bfs_shift);
3424ca0c0d4SPadmavathi Venna 		break;
3434ca0c0d4SPadmavathi Venna 	case 192:
3444ca0c0d4SPadmavathi Venna 		mod |= (EXYNOS5420_MOD_BCLK_192FS << bfs_shift);
3454ca0c0d4SPadmavathi Venna 		break;
3464ca0c0d4SPadmavathi Venna 	case 256:
3474ca0c0d4SPadmavathi Venna 		mod |= (EXYNOS5420_MOD_BCLK_256FS << bfs_shift);
3485033f43cSJassi Brar 		break;
3495033f43cSJassi Brar 	default:
3505033f43cSJassi Brar 		dev_err(&i2s->pdev->dev, "Wrong BCLK Divider!\n");
3515033f43cSJassi Brar 		return;
3525033f43cSJassi Brar 	}
3535033f43cSJassi Brar 
3545033f43cSJassi Brar 	writel(mod, i2s->addr + I2SMOD);
3555033f43cSJassi Brar }
3565033f43cSJassi Brar 
3575033f43cSJassi Brar /* Sample-Size */
3585033f43cSJassi Brar static inline int get_blc(struct i2s_dai *i2s)
3595033f43cSJassi Brar {
3605033f43cSJassi Brar 	int blc = readl(i2s->addr + I2SMOD);
3615033f43cSJassi Brar 
3625033f43cSJassi Brar 	blc = (blc >> 13) & 0x3;
3635033f43cSJassi Brar 
3645033f43cSJassi Brar 	switch (blc) {
3655033f43cSJassi Brar 	case 2: return 24;
3665033f43cSJassi Brar 	case 1:	return 8;
3675033f43cSJassi Brar 	default: return 16;
3685033f43cSJassi Brar 	}
3695033f43cSJassi Brar }
3705033f43cSJassi Brar 
3715033f43cSJassi Brar /* TX Channel Control */
3725033f43cSJassi Brar static void i2s_txctrl(struct i2s_dai *i2s, int on)
3735033f43cSJassi Brar {
3745033f43cSJassi Brar 	void __iomem *addr = i2s->addr;
375a5a56871SPadmavathi Venna 	int txr_off = i2s->variant_regs->txr_off;
3765033f43cSJassi Brar 	u32 con = readl(addr + I2SCON);
377a5a56871SPadmavathi Venna 	u32 mod = readl(addr + I2SMOD) & ~(3 << txr_off);
3785033f43cSJassi Brar 
3795033f43cSJassi Brar 	if (on) {
3805033f43cSJassi Brar 		con |= CON_ACTIVE;
3815033f43cSJassi Brar 		con &= ~CON_TXCH_PAUSE;
3825033f43cSJassi Brar 
3835033f43cSJassi Brar 		if (is_secondary(i2s)) {
3845033f43cSJassi Brar 			con |= CON_TXSDMA_ACTIVE;
3855033f43cSJassi Brar 			con &= ~CON_TXSDMA_PAUSE;
3865033f43cSJassi Brar 		} else {
3875033f43cSJassi Brar 			con |= CON_TXDMA_ACTIVE;
3885033f43cSJassi Brar 			con &= ~CON_TXDMA_PAUSE;
3895033f43cSJassi Brar 		}
3905033f43cSJassi Brar 
3915033f43cSJassi Brar 		if (any_rx_active(i2s))
392a5a56871SPadmavathi Venna 			mod |= 2 << txr_off;
3935033f43cSJassi Brar 		else
394a5a56871SPadmavathi Venna 			mod |= 0 << txr_off;
3955033f43cSJassi Brar 	} else {
3965033f43cSJassi Brar 		if (is_secondary(i2s)) {
3975033f43cSJassi Brar 			con |=  CON_TXSDMA_PAUSE;
3985033f43cSJassi Brar 			con &= ~CON_TXSDMA_ACTIVE;
3995033f43cSJassi Brar 		} else {
4005033f43cSJassi Brar 			con |=  CON_TXDMA_PAUSE;
4015033f43cSJassi Brar 			con &= ~CON_TXDMA_ACTIVE;
4025033f43cSJassi Brar 		}
4035033f43cSJassi Brar 
4045033f43cSJassi Brar 		if (other_tx_active(i2s)) {
4055033f43cSJassi Brar 			writel(con, addr + I2SCON);
4065033f43cSJassi Brar 			return;
4075033f43cSJassi Brar 		}
4085033f43cSJassi Brar 
4095033f43cSJassi Brar 		con |=  CON_TXCH_PAUSE;
4105033f43cSJassi Brar 
4115033f43cSJassi Brar 		if (any_rx_active(i2s))
412a5a56871SPadmavathi Venna 			mod |= 1 << txr_off;
4135033f43cSJassi Brar 		else
4145033f43cSJassi Brar 			con &= ~CON_ACTIVE;
4155033f43cSJassi Brar 	}
4165033f43cSJassi Brar 
4175033f43cSJassi Brar 	writel(mod, addr + I2SMOD);
4185033f43cSJassi Brar 	writel(con, addr + I2SCON);
4195033f43cSJassi Brar }
4205033f43cSJassi Brar 
4215033f43cSJassi Brar /* RX Channel Control */
4225033f43cSJassi Brar static void i2s_rxctrl(struct i2s_dai *i2s, int on)
4235033f43cSJassi Brar {
4245033f43cSJassi Brar 	void __iomem *addr = i2s->addr;
425a5a56871SPadmavathi Venna 	int txr_off = i2s->variant_regs->txr_off;
4265033f43cSJassi Brar 	u32 con = readl(addr + I2SCON);
427a5a56871SPadmavathi Venna 	u32 mod = readl(addr + I2SMOD) & ~(3 << txr_off);
4285033f43cSJassi Brar 
4295033f43cSJassi Brar 	if (on) {
4305033f43cSJassi Brar 		con |= CON_RXDMA_ACTIVE | CON_ACTIVE;
4315033f43cSJassi Brar 		con &= ~(CON_RXDMA_PAUSE | CON_RXCH_PAUSE);
4325033f43cSJassi Brar 
4335033f43cSJassi Brar 		if (any_tx_active(i2s))
434a5a56871SPadmavathi Venna 			mod |= 2 << txr_off;
4355033f43cSJassi Brar 		else
436a5a56871SPadmavathi Venna 			mod |= 1 << txr_off;
4375033f43cSJassi Brar 	} else {
4385033f43cSJassi Brar 		con |=  CON_RXDMA_PAUSE | CON_RXCH_PAUSE;
4395033f43cSJassi Brar 		con &= ~CON_RXDMA_ACTIVE;
4405033f43cSJassi Brar 
4415033f43cSJassi Brar 		if (any_tx_active(i2s))
442a5a56871SPadmavathi Venna 			mod |= 0 << txr_off;
4435033f43cSJassi Brar 		else
4445033f43cSJassi Brar 			con &= ~CON_ACTIVE;
4455033f43cSJassi Brar 	}
4465033f43cSJassi Brar 
4475033f43cSJassi Brar 	writel(mod, addr + I2SMOD);
4485033f43cSJassi Brar 	writel(con, addr + I2SCON);
4495033f43cSJassi Brar }
4505033f43cSJassi Brar 
4515033f43cSJassi Brar /* Flush FIFO of an interface */
4525033f43cSJassi Brar static inline void i2s_fifo(struct i2s_dai *i2s, u32 flush)
4535033f43cSJassi Brar {
4545033f43cSJassi Brar 	void __iomem *fic;
4555033f43cSJassi Brar 	u32 val;
4565033f43cSJassi Brar 
4575033f43cSJassi Brar 	if (!i2s)
4585033f43cSJassi Brar 		return;
4595033f43cSJassi Brar 
4605033f43cSJassi Brar 	if (is_secondary(i2s))
4615033f43cSJassi Brar 		fic = i2s->addr + I2SFICS;
4625033f43cSJassi Brar 	else
4635033f43cSJassi Brar 		fic = i2s->addr + I2SFIC;
4645033f43cSJassi Brar 
4655033f43cSJassi Brar 	/* Flush the FIFO */
4665033f43cSJassi Brar 	writel(readl(fic) | flush, fic);
4675033f43cSJassi Brar 
4685033f43cSJassi Brar 	/* Be patient */
4695033f43cSJassi Brar 	val = msecs_to_loops(1) / 1000; /* 1 usec */
4705033f43cSJassi Brar 	while (--val)
4715033f43cSJassi Brar 		cpu_relax();
4725033f43cSJassi Brar 
4735033f43cSJassi Brar 	writel(readl(fic) & ~flush, fic);
4745033f43cSJassi Brar }
4755033f43cSJassi Brar 
4765033f43cSJassi Brar static int i2s_set_sysclk(struct snd_soc_dai *dai,
4775033f43cSJassi Brar 	  int clk_id, unsigned int rfs, int dir)
4785033f43cSJassi Brar {
4795033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
480dcd60fc3SSylwester Nawrocki 	struct i2s_dai *other = get_other_dai(i2s);
481a5a56871SPadmavathi Venna 	const struct samsung_i2s_variant_regs *i2s_regs = i2s->variant_regs;
482a5a56871SPadmavathi Venna 	unsigned int cdcon_mask = 1 << i2s_regs->cdclkcon_off;
483a5a56871SPadmavathi Venna 	unsigned int rsrc_mask = 1 << i2s_regs->rclksrc_off;
484ce8bcdbbSSylwester Nawrocki 	u32 mod, mask, val = 0;
485316fa9e0SCharles Keepax 	unsigned long flags;
486ce8bcdbbSSylwester Nawrocki 
487316fa9e0SCharles Keepax 	spin_lock_irqsave(i2s->lock, flags);
488ce8bcdbbSSylwester Nawrocki 	mod = readl(i2s->addr + I2SMOD);
489316fa9e0SCharles Keepax 	spin_unlock_irqrestore(i2s->lock, flags);
4905033f43cSJassi Brar 
4915033f43cSJassi Brar 	switch (clk_id) {
492c86d50f9SSylwester Nawrocki 	case SAMSUNG_I2S_OPCLK:
493ce8bcdbbSSylwester Nawrocki 		mask = MOD_OPCLK_MASK;
494ce8bcdbbSSylwester Nawrocki 		val = dir;
495c86d50f9SSylwester Nawrocki 		break;
4965033f43cSJassi Brar 	case SAMSUNG_I2S_CDCLK:
497ce8bcdbbSSylwester Nawrocki 		mask = 1 << i2s_regs->cdclkcon_off;
4985033f43cSJassi Brar 		/* Shouldn't matter in GATING(CLOCK_IN) mode */
4995033f43cSJassi Brar 		if (dir == SND_SOC_CLOCK_IN)
5005033f43cSJassi Brar 			rfs = 0;
5015033f43cSJassi Brar 
502133c2681SCharles Keepax 		if ((rfs && other && other->rfs && (other->rfs != rfs)) ||
5035033f43cSJassi Brar 				(any_active(i2s) &&
5045033f43cSJassi Brar 				(((dir == SND_SOC_CLOCK_IN)
505a5a56871SPadmavathi Venna 					&& !(mod & cdcon_mask)) ||
5065033f43cSJassi Brar 				((dir == SND_SOC_CLOCK_OUT)
507a5a56871SPadmavathi Venna 					&& (mod & cdcon_mask))))) {
5085033f43cSJassi Brar 			dev_err(&i2s->pdev->dev,
5095033f43cSJassi Brar 				"%s:%d Other DAI busy\n", __func__, __LINE__);
5105033f43cSJassi Brar 			return -EAGAIN;
5115033f43cSJassi Brar 		}
5125033f43cSJassi Brar 
5135033f43cSJassi Brar 		if (dir == SND_SOC_CLOCK_IN)
514ce8bcdbbSSylwester Nawrocki 			val = 1 << i2s_regs->cdclkcon_off;
5155033f43cSJassi Brar 
5165033f43cSJassi Brar 		i2s->rfs = rfs;
5175033f43cSJassi Brar 		break;
5185033f43cSJassi Brar 
5195033f43cSJassi Brar 	case SAMSUNG_I2S_RCLKSRC_0: /* clock corrsponding to IISMOD[10] := 0 */
5205033f43cSJassi Brar 	case SAMSUNG_I2S_RCLKSRC_1: /* clock corrsponding to IISMOD[10] := 1 */
521ce8bcdbbSSylwester Nawrocki 		mask = 1 << i2s_regs->rclksrc_off;
522ce8bcdbbSSylwester Nawrocki 
5235033f43cSJassi Brar 		if ((i2s->quirks & QUIRK_NO_MUXPSR)
5245033f43cSJassi Brar 				|| (clk_id == SAMSUNG_I2S_RCLKSRC_0))
5255033f43cSJassi Brar 			clk_id = 0;
5265033f43cSJassi Brar 		else
5275033f43cSJassi Brar 			clk_id = 1;
5285033f43cSJassi Brar 
5295033f43cSJassi Brar 		if (!any_active(i2s)) {
530a6aba536SSylwester Nawrocki 			if (i2s->op_clk && !IS_ERR(i2s->op_clk)) {
531a5a56871SPadmavathi Venna 				if ((clk_id && !(mod & rsrc_mask)) ||
532a5a56871SPadmavathi Venna 					(!clk_id && (mod & rsrc_mask))) {
53398614cf6SThomas Abraham 					clk_disable_unprepare(i2s->op_clk);
5345033f43cSJassi Brar 					clk_put(i2s->op_clk);
5355033f43cSJassi Brar 				} else {
5366ce534aaSJassi Brar 					i2s->rclk_srcrate =
5376ce534aaSJassi Brar 						clk_get_rate(i2s->op_clk);
5385033f43cSJassi Brar 					return 0;
5395033f43cSJassi Brar 				}
5405033f43cSJassi Brar 			}
5415033f43cSJassi Brar 
5421974a042SPadmavathi Venna 			if (clk_id)
5435033f43cSJassi Brar 				i2s->op_clk = clk_get(&i2s->pdev->dev,
5441974a042SPadmavathi Venna 						"i2s_opclk1");
5451974a042SPadmavathi Venna 			else
5461974a042SPadmavathi Venna 				i2s->op_clk = clk_get(&i2s->pdev->dev,
5471974a042SPadmavathi Venna 						"i2s_opclk0");
548a6aba536SSylwester Nawrocki 
549a6aba536SSylwester Nawrocki 			if (WARN_ON(IS_ERR(i2s->op_clk)))
550a6aba536SSylwester Nawrocki 				return PTR_ERR(i2s->op_clk);
551a6aba536SSylwester Nawrocki 
55298614cf6SThomas Abraham 			clk_prepare_enable(i2s->op_clk);
5535033f43cSJassi Brar 			i2s->rclk_srcrate = clk_get_rate(i2s->op_clk);
5545033f43cSJassi Brar 
5555033f43cSJassi Brar 			/* Over-ride the other's */
5565033f43cSJassi Brar 			if (other) {
5575033f43cSJassi Brar 				other->op_clk = i2s->op_clk;
5585033f43cSJassi Brar 				other->rclk_srcrate = i2s->rclk_srcrate;
5595033f43cSJassi Brar 			}
560a5a56871SPadmavathi Venna 		} else if ((!clk_id && (mod & rsrc_mask))
561a5a56871SPadmavathi Venna 				|| (clk_id && !(mod & rsrc_mask))) {
5625033f43cSJassi Brar 			dev_err(&i2s->pdev->dev,
5635033f43cSJassi Brar 				"%s:%d Other DAI busy\n", __func__, __LINE__);
5645033f43cSJassi Brar 			return -EAGAIN;
5655033f43cSJassi Brar 		} else {
5665033f43cSJassi Brar 			/* Call can't be on the active DAI */
5675033f43cSJassi Brar 			i2s->op_clk = other->op_clk;
5685033f43cSJassi Brar 			i2s->rclk_srcrate = other->rclk_srcrate;
5695033f43cSJassi Brar 			return 0;
5705033f43cSJassi Brar 		}
5715033f43cSJassi Brar 
572ce8bcdbbSSylwester Nawrocki 		if (clk_id == 1)
573ce8bcdbbSSylwester Nawrocki 			val = 1 << i2s_regs->rclksrc_off;
574b2de1d20SPadmavathi Venna 		break;
5755033f43cSJassi Brar 	default:
5765033f43cSJassi Brar 		dev_err(&i2s->pdev->dev, "We don't serve that!\n");
5775033f43cSJassi Brar 		return -EINVAL;
5785033f43cSJassi Brar 	}
5795033f43cSJassi Brar 
580316fa9e0SCharles Keepax 	spin_lock_irqsave(i2s->lock, flags);
581ce8bcdbbSSylwester Nawrocki 	mod = readl(i2s->addr + I2SMOD);
582ce8bcdbbSSylwester Nawrocki 	mod = (mod & ~mask) | val;
5835033f43cSJassi Brar 	writel(mod, i2s->addr + I2SMOD);
584316fa9e0SCharles Keepax 	spin_unlock_irqrestore(i2s->lock, flags);
5855033f43cSJassi Brar 
5865033f43cSJassi Brar 	return 0;
5875033f43cSJassi Brar }
5885033f43cSJassi Brar 
5895033f43cSJassi Brar static int i2s_set_fmt(struct snd_soc_dai *dai,
5905033f43cSJassi Brar 	unsigned int fmt)
5915033f43cSJassi Brar {
5925033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
593a5a56871SPadmavathi Venna 	int lrp_shift, sdf_shift, sdf_mask, lrp_rlow, mod_slave;
594ce8bcdbbSSylwester Nawrocki 	u32 mod, tmp = 0;
595316fa9e0SCharles Keepax 	unsigned long flags;
5965033f43cSJassi Brar 
597a5a56871SPadmavathi Venna 	lrp_shift = i2s->variant_regs->lrp_off;
598a5a56871SPadmavathi Venna 	sdf_shift = i2s->variant_regs->sdf_off;
599a5a56871SPadmavathi Venna 	mod_slave = 1 << i2s->variant_regs->mss_off;
6004ca0c0d4SPadmavathi Venna 
601b60be4aaSPadmavathi Venna 	sdf_mask = MOD_SDF_MASK << sdf_shift;
602b60be4aaSPadmavathi Venna 	lrp_rlow = MOD_LR_RLOW << lrp_shift;
603b60be4aaSPadmavathi Venna 
6045033f43cSJassi Brar 	/* Format is priority */
6055033f43cSJassi Brar 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
6065033f43cSJassi Brar 	case SND_SOC_DAIFMT_RIGHT_J:
607b60be4aaSPadmavathi Venna 		tmp |= lrp_rlow;
608b60be4aaSPadmavathi Venna 		tmp |= (MOD_SDF_MSB << sdf_shift);
6095033f43cSJassi Brar 		break;
6105033f43cSJassi Brar 	case SND_SOC_DAIFMT_LEFT_J:
611b60be4aaSPadmavathi Venna 		tmp |= lrp_rlow;
612b60be4aaSPadmavathi Venna 		tmp |= (MOD_SDF_LSB << sdf_shift);
6135033f43cSJassi Brar 		break;
6145033f43cSJassi Brar 	case SND_SOC_DAIFMT_I2S:
615b60be4aaSPadmavathi Venna 		tmp |= (MOD_SDF_IIS << sdf_shift);
6165033f43cSJassi Brar 		break;
6175033f43cSJassi Brar 	default:
6185033f43cSJassi Brar 		dev_err(&i2s->pdev->dev, "Format not supported\n");
6195033f43cSJassi Brar 		return -EINVAL;
6205033f43cSJassi Brar 	}
6215033f43cSJassi Brar 
6225033f43cSJassi Brar 	/*
6235033f43cSJassi Brar 	 * INV flag is relative to the FORMAT flag - if set it simply
6245033f43cSJassi Brar 	 * flips the polarity specified by the Standard
6255033f43cSJassi Brar 	 */
6265033f43cSJassi Brar 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
6275033f43cSJassi Brar 	case SND_SOC_DAIFMT_NB_NF:
6285033f43cSJassi Brar 		break;
6295033f43cSJassi Brar 	case SND_SOC_DAIFMT_NB_IF:
630b60be4aaSPadmavathi Venna 		if (tmp & lrp_rlow)
631b60be4aaSPadmavathi Venna 			tmp &= ~lrp_rlow;
6325033f43cSJassi Brar 		else
633b60be4aaSPadmavathi Venna 			tmp |= lrp_rlow;
6345033f43cSJassi Brar 		break;
6355033f43cSJassi Brar 	default:
6365033f43cSJassi Brar 		dev_err(&i2s->pdev->dev, "Polarity not supported\n");
6375033f43cSJassi Brar 		return -EINVAL;
6385033f43cSJassi Brar 	}
6395033f43cSJassi Brar 
6405033f43cSJassi Brar 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
6415033f43cSJassi Brar 	case SND_SOC_DAIFMT_CBM_CFM:
642a5a56871SPadmavathi Venna 		tmp |= mod_slave;
6435033f43cSJassi Brar 		break;
6445033f43cSJassi Brar 	case SND_SOC_DAIFMT_CBS_CFS:
6455033f43cSJassi Brar 		/* Set default source clock in Master mode */
6465033f43cSJassi Brar 		if (i2s->rclk_srcrate == 0)
6475033f43cSJassi Brar 			i2s_set_sysclk(dai, SAMSUNG_I2S_RCLKSRC_0,
6485033f43cSJassi Brar 							0, SND_SOC_CLOCK_IN);
6495033f43cSJassi Brar 		break;
6505033f43cSJassi Brar 	default:
6515033f43cSJassi Brar 		dev_err(&i2s->pdev->dev, "master/slave format not supported\n");
6525033f43cSJassi Brar 		return -EINVAL;
6535033f43cSJassi Brar 	}
6545033f43cSJassi Brar 
655316fa9e0SCharles Keepax 	spin_lock_irqsave(i2s->lock, flags);
656ce8bcdbbSSylwester Nawrocki 	mod = readl(i2s->addr + I2SMOD);
657b60be4aaSPadmavathi Venna 	/*
658b60be4aaSPadmavathi Venna 	 * Don't change the I2S mode if any controller is active on this
659b60be4aaSPadmavathi Venna 	 * channel.
660b60be4aaSPadmavathi Venna 	 */
6615033f43cSJassi Brar 	if (any_active(i2s) &&
662a5a56871SPadmavathi Venna 		((mod & (sdf_mask | lrp_rlow | mod_slave)) != tmp)) {
663316fa9e0SCharles Keepax 		spin_unlock_irqrestore(i2s->lock, flags);
6645033f43cSJassi Brar 		dev_err(&i2s->pdev->dev,
6655033f43cSJassi Brar 				"%s:%d Other DAI busy\n", __func__, __LINE__);
6665033f43cSJassi Brar 		return -EAGAIN;
6675033f43cSJassi Brar 	}
6685033f43cSJassi Brar 
669a5a56871SPadmavathi Venna 	mod &= ~(sdf_mask | lrp_rlow | mod_slave);
6705033f43cSJassi Brar 	mod |= tmp;
6715033f43cSJassi Brar 	writel(mod, i2s->addr + I2SMOD);
672316fa9e0SCharles Keepax 	spin_unlock_irqrestore(i2s->lock, flags);
6735033f43cSJassi Brar 
6745033f43cSJassi Brar 	return 0;
6755033f43cSJassi Brar }
6765033f43cSJassi Brar 
6775033f43cSJassi Brar static int i2s_hw_params(struct snd_pcm_substream *substream,
6785033f43cSJassi Brar 	struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
6795033f43cSJassi Brar {
6805033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
681ce8bcdbbSSylwester Nawrocki 	u32 mod, mask = 0, val = 0;
682316fa9e0SCharles Keepax 	unsigned long flags;
6835033f43cSJassi Brar 
6845033f43cSJassi Brar 	if (!is_secondary(i2s))
685ce8bcdbbSSylwester Nawrocki 		mask |= (MOD_DC2_EN | MOD_DC1_EN);
6865033f43cSJassi Brar 
6875033f43cSJassi Brar 	switch (params_channels(params)) {
6885033f43cSJassi Brar 	case 6:
689ce8bcdbbSSylwester Nawrocki 		val |= MOD_DC2_EN;
6905033f43cSJassi Brar 	case 4:
691ce8bcdbbSSylwester Nawrocki 		val |= MOD_DC1_EN;
6925033f43cSJassi Brar 		break;
6935033f43cSJassi Brar 	case 2:
694588fb705SSangsu Park 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
69569e7a69aSSylwester Nawrocki 			i2s->dma_playback.addr_width = 4;
696588fb705SSangsu Park 		else
69769e7a69aSSylwester Nawrocki 			i2s->dma_capture.addr_width = 4;
698588fb705SSangsu Park 		break;
699588fb705SSangsu Park 	case 1:
700588fb705SSangsu Park 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
70169e7a69aSSylwester Nawrocki 			i2s->dma_playback.addr_width = 2;
702588fb705SSangsu Park 		else
70369e7a69aSSylwester Nawrocki 			i2s->dma_capture.addr_width = 2;
704588fb705SSangsu Park 
7055033f43cSJassi Brar 		break;
7065033f43cSJassi Brar 	default:
7075033f43cSJassi Brar 		dev_err(&i2s->pdev->dev, "%d channels not supported\n",
7085033f43cSJassi Brar 				params_channels(params));
7095033f43cSJassi Brar 		return -EINVAL;
7105033f43cSJassi Brar 	}
7115033f43cSJassi Brar 
7125033f43cSJassi Brar 	if (is_secondary(i2s))
713ce8bcdbbSSylwester Nawrocki 		mask |= MOD_BLCS_MASK;
7145033f43cSJassi Brar 	else
715ce8bcdbbSSylwester Nawrocki 		mask |= MOD_BLCP_MASK;
7165033f43cSJassi Brar 
7175033f43cSJassi Brar 	if (is_manager(i2s))
718ce8bcdbbSSylwester Nawrocki 		mask |= MOD_BLC_MASK;
7195033f43cSJassi Brar 
72088ce1465STushar Behera 	switch (params_width(params)) {
72188ce1465STushar Behera 	case 8:
7225033f43cSJassi Brar 		if (is_secondary(i2s))
723ce8bcdbbSSylwester Nawrocki 			val |= MOD_BLCS_8BIT;
7245033f43cSJassi Brar 		else
725ce8bcdbbSSylwester Nawrocki 			val |= MOD_BLCP_8BIT;
7265033f43cSJassi Brar 		if (is_manager(i2s))
727ce8bcdbbSSylwester Nawrocki 			val |= MOD_BLC_8BIT;
7285033f43cSJassi Brar 		break;
72988ce1465STushar Behera 	case 16:
7305033f43cSJassi Brar 		if (is_secondary(i2s))
731ce8bcdbbSSylwester Nawrocki 			val |= MOD_BLCS_16BIT;
7325033f43cSJassi Brar 		else
733ce8bcdbbSSylwester Nawrocki 			val |= MOD_BLCP_16BIT;
7345033f43cSJassi Brar 		if (is_manager(i2s))
735ce8bcdbbSSylwester Nawrocki 			val |= MOD_BLC_16BIT;
7365033f43cSJassi Brar 		break;
73788ce1465STushar Behera 	case 24:
7385033f43cSJassi Brar 		if (is_secondary(i2s))
739ce8bcdbbSSylwester Nawrocki 			val |= MOD_BLCS_24BIT;
7405033f43cSJassi Brar 		else
741ce8bcdbbSSylwester Nawrocki 			val |= MOD_BLCP_24BIT;
7425033f43cSJassi Brar 		if (is_manager(i2s))
743ce8bcdbbSSylwester Nawrocki 			val |= MOD_BLC_24BIT;
7445033f43cSJassi Brar 		break;
7455033f43cSJassi Brar 	default:
7465033f43cSJassi Brar 		dev_err(&i2s->pdev->dev, "Format(%d) not supported\n",
7475033f43cSJassi Brar 				params_format(params));
7485033f43cSJassi Brar 		return -EINVAL;
7495033f43cSJassi Brar 	}
750ce8bcdbbSSylwester Nawrocki 
751316fa9e0SCharles Keepax 	spin_lock_irqsave(i2s->lock, flags);
752ce8bcdbbSSylwester Nawrocki 	mod = readl(i2s->addr + I2SMOD);
753ce8bcdbbSSylwester Nawrocki 	mod = (mod & ~mask) | val;
7545033f43cSJassi Brar 	writel(mod, i2s->addr + I2SMOD);
755316fa9e0SCharles Keepax 	spin_unlock_irqrestore(i2s->lock, flags);
7565033f43cSJassi Brar 
75769e7a69aSSylwester Nawrocki 	snd_soc_dai_init_dma_data(dai, &i2s->dma_playback, &i2s->dma_capture);
758d37bdf73SMark Brown 
7595033f43cSJassi Brar 	i2s->frmclk = params_rate(params);
7605033f43cSJassi Brar 
7615033f43cSJassi Brar 	return 0;
7625033f43cSJassi Brar }
7635033f43cSJassi Brar 
7645033f43cSJassi Brar /* We set constraints on the substream acc to the version of I2S */
7655033f43cSJassi Brar static int i2s_startup(struct snd_pcm_substream *substream,
7665033f43cSJassi Brar 	  struct snd_soc_dai *dai)
7675033f43cSJassi Brar {
7685033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
769dcd60fc3SSylwester Nawrocki 	struct i2s_dai *other = get_other_dai(i2s);
7705033f43cSJassi Brar 	unsigned long flags;
7715033f43cSJassi Brar 
7725033f43cSJassi Brar 	spin_lock_irqsave(&lock, flags);
7735033f43cSJassi Brar 
7745033f43cSJassi Brar 	i2s->mode |= DAI_OPENED;
7755033f43cSJassi Brar 
7765033f43cSJassi Brar 	if (is_manager(other))
7775033f43cSJassi Brar 		i2s->mode &= ~DAI_MANAGER;
7785033f43cSJassi Brar 	else
7795033f43cSJassi Brar 		i2s->mode |= DAI_MANAGER;
7805033f43cSJassi Brar 
7812d77828dSPadmavathi Venna 	if (!any_active(i2s) && (i2s->quirks & QUIRK_NEED_RSTCLR))
7822d77828dSPadmavathi Venna 		writel(CON_RSTCLR, i2s->addr + I2SCON);
7832d77828dSPadmavathi Venna 
7845033f43cSJassi Brar 	spin_unlock_irqrestore(&lock, flags);
7855033f43cSJassi Brar 
7865033f43cSJassi Brar 	return 0;
7875033f43cSJassi Brar }
7885033f43cSJassi Brar 
7895033f43cSJassi Brar static void i2s_shutdown(struct snd_pcm_substream *substream,
7905033f43cSJassi Brar 	struct snd_soc_dai *dai)
7915033f43cSJassi Brar {
7925033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
793dcd60fc3SSylwester Nawrocki 	struct i2s_dai *other = get_other_dai(i2s);
7945033f43cSJassi Brar 	unsigned long flags;
7955033f43cSJassi Brar 
7965033f43cSJassi Brar 	spin_lock_irqsave(&lock, flags);
7975033f43cSJassi Brar 
7985033f43cSJassi Brar 	i2s->mode &= ~DAI_OPENED;
7995033f43cSJassi Brar 	i2s->mode &= ~DAI_MANAGER;
8005033f43cSJassi Brar 
801074b89bbSSylwester Nawrocki 	if (is_opened(other))
8025033f43cSJassi Brar 		other->mode |= DAI_MANAGER;
803074b89bbSSylwester Nawrocki 
8045033f43cSJassi Brar 	/* Reset any constraint on RFS and BFS */
8055033f43cSJassi Brar 	i2s->rfs = 0;
8065033f43cSJassi Brar 	i2s->bfs = 0;
8075033f43cSJassi Brar 
8085033f43cSJassi Brar 	spin_unlock_irqrestore(&lock, flags);
8095033f43cSJassi Brar }
8105033f43cSJassi Brar 
8115033f43cSJassi Brar static int config_setup(struct i2s_dai *i2s)
8125033f43cSJassi Brar {
813dcd60fc3SSylwester Nawrocki 	struct i2s_dai *other = get_other_dai(i2s);
8145033f43cSJassi Brar 	unsigned rfs, bfs, blc;
8155033f43cSJassi Brar 	u32 psr;
8165033f43cSJassi Brar 
8175033f43cSJassi Brar 	blc = get_blc(i2s);
8185033f43cSJassi Brar 
8195033f43cSJassi Brar 	bfs = i2s->bfs;
8205033f43cSJassi Brar 
8215033f43cSJassi Brar 	if (!bfs && other)
8225033f43cSJassi Brar 		bfs = other->bfs;
8235033f43cSJassi Brar 
8245033f43cSJassi Brar 	/* Select least possible multiple(2) if no constraint set */
8255033f43cSJassi Brar 	if (!bfs)
8265033f43cSJassi Brar 		bfs = blc * 2;
8275033f43cSJassi Brar 
8285033f43cSJassi Brar 	rfs = i2s->rfs;
8295033f43cSJassi Brar 
8305033f43cSJassi Brar 	if (!rfs && other)
8315033f43cSJassi Brar 		rfs = other->rfs;
8325033f43cSJassi Brar 
8335033f43cSJassi Brar 	if ((rfs == 256 || rfs == 512) && (blc == 24)) {
8345033f43cSJassi Brar 		dev_err(&i2s->pdev->dev,
8355033f43cSJassi Brar 			"%d-RFS not supported for 24-blc\n", rfs);
8365033f43cSJassi Brar 		return -EINVAL;
8375033f43cSJassi Brar 	}
8385033f43cSJassi Brar 
8395033f43cSJassi Brar 	if (!rfs) {
8405033f43cSJassi Brar 		if (bfs == 16 || bfs == 32)
8415033f43cSJassi Brar 			rfs = 256;
8425033f43cSJassi Brar 		else
8435033f43cSJassi Brar 			rfs = 384;
8445033f43cSJassi Brar 	}
8455033f43cSJassi Brar 
8465033f43cSJassi Brar 	/* If already setup and running */
8475033f43cSJassi Brar 	if (any_active(i2s) && (get_rfs(i2s) != rfs || get_bfs(i2s) != bfs)) {
8485033f43cSJassi Brar 		dev_err(&i2s->pdev->dev,
8495033f43cSJassi Brar 				"%s:%d Other DAI busy\n", __func__, __LINE__);
8505033f43cSJassi Brar 		return -EAGAIN;
8515033f43cSJassi Brar 	}
8525033f43cSJassi Brar 
8535033f43cSJassi Brar 	set_bfs(i2s, bfs);
8545033f43cSJassi Brar 	set_rfs(i2s, rfs);
8555033f43cSJassi Brar 
85677010010SPadmavathi Venna 	/* Don't bother with PSR in Slave mode */
85777010010SPadmavathi Venna 	if (is_slave(i2s))
85877010010SPadmavathi Venna 		return 0;
85977010010SPadmavathi Venna 
8605033f43cSJassi Brar 	if (!(i2s->quirks & QUIRK_NO_MUXPSR)) {
8615033f43cSJassi Brar 		psr = i2s->rclk_srcrate / i2s->frmclk / rfs;
8625033f43cSJassi Brar 		writel(((psr - 1) << 8) | PSR_PSREN, i2s->addr + I2SPSR);
8635033f43cSJassi Brar 		dev_dbg(&i2s->pdev->dev,
8645033f43cSJassi Brar 			"RCLK_SRC=%luHz PSR=%u, RCLK=%dfs, BCLK=%dfs\n",
8655033f43cSJassi Brar 				i2s->rclk_srcrate, psr, rfs, bfs);
8665033f43cSJassi Brar 	}
8675033f43cSJassi Brar 
8685033f43cSJassi Brar 	return 0;
8695033f43cSJassi Brar }
8705033f43cSJassi Brar 
8715033f43cSJassi Brar static int i2s_trigger(struct snd_pcm_substream *substream,
8725033f43cSJassi Brar 	int cmd, struct snd_soc_dai *dai)
8735033f43cSJassi Brar {
8745033f43cSJassi Brar 	int capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
8755033f43cSJassi Brar 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
8765033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(rtd->cpu_dai);
8775033f43cSJassi Brar 	unsigned long flags;
8785033f43cSJassi Brar 
8795033f43cSJassi Brar 	switch (cmd) {
8805033f43cSJassi Brar 	case SNDRV_PCM_TRIGGER_START:
8815033f43cSJassi Brar 	case SNDRV_PCM_TRIGGER_RESUME:
8825033f43cSJassi Brar 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
883f3670536SSylwester Nawrocki 		spin_lock_irqsave(i2s->lock, flags);
8845033f43cSJassi Brar 
8855033f43cSJassi Brar 		if (config_setup(i2s)) {
886f3670536SSylwester Nawrocki 			spin_unlock_irqrestore(i2s->lock, flags);
8875033f43cSJassi Brar 			return -EINVAL;
8885033f43cSJassi Brar 		}
8895033f43cSJassi Brar 
8905033f43cSJassi Brar 		if (capture)
8915033f43cSJassi Brar 			i2s_rxctrl(i2s, 1);
8925033f43cSJassi Brar 		else
8935033f43cSJassi Brar 			i2s_txctrl(i2s, 1);
8945033f43cSJassi Brar 
895f3670536SSylwester Nawrocki 		spin_unlock_irqrestore(i2s->lock, flags);
8965033f43cSJassi Brar 		break;
8975033f43cSJassi Brar 	case SNDRV_PCM_TRIGGER_STOP:
8985033f43cSJassi Brar 	case SNDRV_PCM_TRIGGER_SUSPEND:
8995033f43cSJassi Brar 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
900f3670536SSylwester Nawrocki 		spin_lock_irqsave(i2s->lock, flags);
9015033f43cSJassi Brar 
902c90887feSJassi Brar 		if (capture) {
9035033f43cSJassi Brar 			i2s_rxctrl(i2s, 0);
904775bc971SJassi Brar 			i2s_fifo(i2s, FIC_RXFLUSH);
905c90887feSJassi Brar 		} else {
906c90887feSJassi Brar 			i2s_txctrl(i2s, 0);
907775bc971SJassi Brar 			i2s_fifo(i2s, FIC_TXFLUSH);
908c90887feSJassi Brar 		}
909775bc971SJassi Brar 
910f3670536SSylwester Nawrocki 		spin_unlock_irqrestore(i2s->lock, flags);
9115033f43cSJassi Brar 		break;
9125033f43cSJassi Brar 	}
9135033f43cSJassi Brar 
9145033f43cSJassi Brar 	return 0;
9155033f43cSJassi Brar }
9165033f43cSJassi Brar 
9175033f43cSJassi Brar static int i2s_set_clkdiv(struct snd_soc_dai *dai,
9185033f43cSJassi Brar 	int div_id, int div)
9195033f43cSJassi Brar {
9205033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
921dcd60fc3SSylwester Nawrocki 	struct i2s_dai *other = get_other_dai(i2s);
9225033f43cSJassi Brar 
9235033f43cSJassi Brar 	switch (div_id) {
9245033f43cSJassi Brar 	case SAMSUNG_I2S_DIV_BCLK:
9255033f43cSJassi Brar 		if ((any_active(i2s) && div && (get_bfs(i2s) != div))
9265033f43cSJassi Brar 			|| (other && other->bfs && (other->bfs != div))) {
9275033f43cSJassi Brar 			dev_err(&i2s->pdev->dev,
9285033f43cSJassi Brar 				"%s:%d Other DAI busy\n", __func__, __LINE__);
9295033f43cSJassi Brar 			return -EAGAIN;
9305033f43cSJassi Brar 		}
9315033f43cSJassi Brar 		i2s->bfs = div;
9325033f43cSJassi Brar 		break;
9335033f43cSJassi Brar 	default:
9345033f43cSJassi Brar 		dev_err(&i2s->pdev->dev,
9355033f43cSJassi Brar 			"Invalid clock divider(%d)\n", div_id);
9365033f43cSJassi Brar 		return -EINVAL;
9375033f43cSJassi Brar 	}
9385033f43cSJassi Brar 
9395033f43cSJassi Brar 	return 0;
9405033f43cSJassi Brar }
9415033f43cSJassi Brar 
9425033f43cSJassi Brar static snd_pcm_sframes_t
9435033f43cSJassi Brar i2s_delay(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
9445033f43cSJassi Brar {
9455033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
9465033f43cSJassi Brar 	u32 reg = readl(i2s->addr + I2SFIC);
9475033f43cSJassi Brar 	snd_pcm_sframes_t delay;
948a5a56871SPadmavathi Venna 	const struct samsung_i2s_variant_regs *i2s_regs = i2s->variant_regs;
9495033f43cSJassi Brar 
9505033f43cSJassi Brar 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
9515033f43cSJassi Brar 		delay = FIC_RXCOUNT(reg);
9525033f43cSJassi Brar 	else if (is_secondary(i2s))
9535033f43cSJassi Brar 		delay = FICS_TXCOUNT(readl(i2s->addr + I2SFICS));
9545033f43cSJassi Brar 	else
955a5a56871SPadmavathi Venna 		delay = (reg >> i2s_regs->ftx0cnt_off) & 0x7f;
9565033f43cSJassi Brar 
9575033f43cSJassi Brar 	return delay;
9585033f43cSJassi Brar }
9595033f43cSJassi Brar 
9605033f43cSJassi Brar #ifdef CONFIG_PM
9615033f43cSJassi Brar static int i2s_suspend(struct snd_soc_dai *dai)
9625033f43cSJassi Brar {
9635033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
9645033f43cSJassi Brar 
9655033f43cSJassi Brar 	i2s->suspend_i2smod = readl(i2s->addr + I2SMOD);
9665033f43cSJassi Brar 	i2s->suspend_i2scon = readl(i2s->addr + I2SCON);
9675033f43cSJassi Brar 	i2s->suspend_i2spsr = readl(i2s->addr + I2SPSR);
9685033f43cSJassi Brar 
9695033f43cSJassi Brar 	return 0;
9705033f43cSJassi Brar }
9715033f43cSJassi Brar 
9725033f43cSJassi Brar static int i2s_resume(struct snd_soc_dai *dai)
9735033f43cSJassi Brar {
9745033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
9755033f43cSJassi Brar 
9765033f43cSJassi Brar 	writel(i2s->suspend_i2scon, i2s->addr + I2SCON);
9775033f43cSJassi Brar 	writel(i2s->suspend_i2smod, i2s->addr + I2SMOD);
9785033f43cSJassi Brar 	writel(i2s->suspend_i2spsr, i2s->addr + I2SPSR);
9795033f43cSJassi Brar 
9805033f43cSJassi Brar 	return 0;
9815033f43cSJassi Brar }
9825033f43cSJassi Brar #else
9835033f43cSJassi Brar #define i2s_suspend NULL
9845033f43cSJassi Brar #define i2s_resume  NULL
9855033f43cSJassi Brar #endif
9865033f43cSJassi Brar 
9875033f43cSJassi Brar static int samsung_i2s_dai_probe(struct snd_soc_dai *dai)
9885033f43cSJassi Brar {
9895033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
990dcd60fc3SSylwester Nawrocki 	struct i2s_dai *other = get_other_dai(i2s);
991ce8bcdbbSSylwester Nawrocki 	unsigned long flags;
9925033f43cSJassi Brar 
9930ec2ba80SSylwester Nawrocki 	if (is_secondary(i2s)) { /* If this is probe on the secondary DAI */
99469e7a69aSSylwester Nawrocki 		snd_soc_dai_init_dma_data(dai, &other->sec_dai->dma_playback,
9953688569eSMark Brown 					   NULL);
996872c26bdSSylwester Nawrocki 	} else {
99769e7a69aSSylwester Nawrocki 		snd_soc_dai_init_dma_data(dai, &i2s->dma_playback,
998872c26bdSSylwester Nawrocki 					   &i2s->dma_capture);
999511e3033SMark Brown 
10005033f43cSJassi Brar 		if (i2s->quirks & QUIRK_NEED_RSTCLR)
10015033f43cSJassi Brar 			writel(CON_RSTCLR, i2s->addr + I2SCON);
10025033f43cSJassi Brar 
1003b0759736SPadmavathi Venna 		if (i2s->quirks & QUIRK_SUPPORTS_IDMA)
10049b8f5695SMark Brown 			idma_reg_addr_init(i2s->addr,
100569e7a69aSSylwester Nawrocki 					i2s->sec_dai->idma_playback.addr);
1006872c26bdSSylwester Nawrocki 	}
100761100f40SSangbeom Kim 
10085033f43cSJassi Brar 	/* Reset any constraint on RFS and BFS */
10095033f43cSJassi Brar 	i2s->rfs = 0;
10105033f43cSJassi Brar 	i2s->bfs = 0;
1011d66eac3eSTushar Behera 	i2s->rclk_srcrate = 0;
1012ce8bcdbbSSylwester Nawrocki 
1013ce8bcdbbSSylwester Nawrocki 	spin_lock_irqsave(i2s->lock, flags);
10145033f43cSJassi Brar 	i2s_txctrl(i2s, 0);
10155033f43cSJassi Brar 	i2s_rxctrl(i2s, 0);
10165033f43cSJassi Brar 	i2s_fifo(i2s, FIC_TXFLUSH);
10175033f43cSJassi Brar 	i2s_fifo(other, FIC_TXFLUSH);
10185033f43cSJassi Brar 	i2s_fifo(i2s, FIC_RXFLUSH);
1019ce8bcdbbSSylwester Nawrocki 	spin_unlock_irqrestore(i2s->lock, flags);
10205033f43cSJassi Brar 
10215033f43cSJassi Brar 	/* Gate CDCLK by default */
10225033f43cSJassi Brar 	if (!is_opened(other))
10235033f43cSJassi Brar 		i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK,
10245033f43cSJassi Brar 				0, SND_SOC_CLOCK_IN);
10255033f43cSJassi Brar 
10265033f43cSJassi Brar 	return 0;
10275033f43cSJassi Brar }
10285033f43cSJassi Brar 
10295033f43cSJassi Brar static int samsung_i2s_dai_remove(struct snd_soc_dai *dai)
10305033f43cSJassi Brar {
10315033f43cSJassi Brar 	struct i2s_dai *i2s = snd_soc_dai_get_drvdata(dai);
10325033f43cSJassi Brar 
1033c92f1d0eSSylwester Nawrocki 	if (!is_secondary(i2s)) {
1034ce8bcdbbSSylwester Nawrocki 		if (i2s->quirks & QUIRK_NEED_RSTCLR) {
1035ce8bcdbbSSylwester Nawrocki 			spin_lock(i2s->lock);
10365033f43cSJassi Brar 			writel(0, i2s->addr + I2SCON);
1037ce8bcdbbSSylwester Nawrocki 			spin_unlock(i2s->lock);
1038ce8bcdbbSSylwester Nawrocki 		}
10395033f43cSJassi Brar 	}
10405033f43cSJassi Brar 
10415033f43cSJassi Brar 	return 0;
10425033f43cSJassi Brar }
10435033f43cSJassi Brar 
104485e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops samsung_i2s_dai_ops = {
10455033f43cSJassi Brar 	.trigger = i2s_trigger,
10465033f43cSJassi Brar 	.hw_params = i2s_hw_params,
10475033f43cSJassi Brar 	.set_fmt = i2s_set_fmt,
10485033f43cSJassi Brar 	.set_clkdiv = i2s_set_clkdiv,
10495033f43cSJassi Brar 	.set_sysclk = i2s_set_sysclk,
10505033f43cSJassi Brar 	.startup = i2s_startup,
10515033f43cSJassi Brar 	.shutdown = i2s_shutdown,
10525033f43cSJassi Brar 	.delay = i2s_delay,
10535033f43cSJassi Brar };
10545033f43cSJassi Brar 
10554b828535SKuninori Morimoto static const struct snd_soc_component_driver samsung_i2s_component = {
10564b828535SKuninori Morimoto 	.name		= "samsung-i2s",
10574b828535SKuninori Morimoto };
10584b828535SKuninori Morimoto 
10595033f43cSJassi Brar #define SAMSUNG_I2S_RATES	SNDRV_PCM_RATE_8000_96000
10605033f43cSJassi Brar 
10615033f43cSJassi Brar #define SAMSUNG_I2S_FMTS	(SNDRV_PCM_FMTBIT_S8 | \
10625033f43cSJassi Brar 					SNDRV_PCM_FMTBIT_S16_LE | \
10635033f43cSJassi Brar 					SNDRV_PCM_FMTBIT_S24_LE)
10645033f43cSJassi Brar 
1065fdca21adSBill Pemberton static struct i2s_dai *i2s_alloc_dai(struct platform_device *pdev, bool sec)
10665033f43cSJassi Brar {
10675033f43cSJassi Brar 	struct i2s_dai *i2s;
1068c6f9b1ebSPrathyush K 	int ret;
10695033f43cSJassi Brar 
1070b960ce74SMark Brown 	i2s = devm_kzalloc(&pdev->dev, sizeof(struct i2s_dai), GFP_KERNEL);
10715033f43cSJassi Brar 	if (i2s == NULL)
10725033f43cSJassi Brar 		return NULL;
10735033f43cSJassi Brar 
10745033f43cSJassi Brar 	i2s->pdev = pdev;
10755033f43cSJassi Brar 	i2s->pri_dai = NULL;
10765033f43cSJassi Brar 	i2s->sec_dai = NULL;
10775033f43cSJassi Brar 	i2s->i2s_dai_drv.symmetric_rates = 1;
10785033f43cSJassi Brar 	i2s->i2s_dai_drv.probe = samsung_i2s_dai_probe;
10795033f43cSJassi Brar 	i2s->i2s_dai_drv.remove = samsung_i2s_dai_remove;
10805033f43cSJassi Brar 	i2s->i2s_dai_drv.ops = &samsung_i2s_dai_ops;
10815033f43cSJassi Brar 	i2s->i2s_dai_drv.suspend = i2s_suspend;
10825033f43cSJassi Brar 	i2s->i2s_dai_drv.resume = i2s_resume;
1083a0ff6ea2SCharles Keepax 	i2s->i2s_dai_drv.playback.channels_min = 1;
10845033f43cSJassi Brar 	i2s->i2s_dai_drv.playback.channels_max = 2;
10855033f43cSJassi Brar 	i2s->i2s_dai_drv.playback.rates = SAMSUNG_I2S_RATES;
10865033f43cSJassi Brar 	i2s->i2s_dai_drv.playback.formats = SAMSUNG_I2S_FMTS;
10875033f43cSJassi Brar 
10885033f43cSJassi Brar 	if (!sec) {
1089588fb705SSangsu Park 		i2s->i2s_dai_drv.capture.channels_min = 1;
10905033f43cSJassi Brar 		i2s->i2s_dai_drv.capture.channels_max = 2;
10915033f43cSJassi Brar 		i2s->i2s_dai_drv.capture.rates = SAMSUNG_I2S_RATES;
10925033f43cSJassi Brar 		i2s->i2s_dai_drv.capture.formats = SAMSUNG_I2S_FMTS;
1093c6f9b1ebSPrathyush K 		dev_set_drvdata(&i2s->pdev->dev, i2s);
10945033f43cSJassi Brar 	} else {	/* Create a new platform_device for Secondary */
1095c6f9b1ebSPrathyush K 		i2s->pdev = platform_device_alloc("samsung-i2s-sec", -1);
109629ca9c73SWei Yongjun 		if (!i2s->pdev)
10975033f43cSJassi Brar 			return NULL;
10985033f43cSJassi Brar 
10992f6f0ffbSMark Brown 		i2s->pdev->dev.parent = &pdev->dev;
11002f6f0ffbSMark Brown 
1101c6f9b1ebSPrathyush K 		platform_set_drvdata(i2s->pdev, i2s);
1102c6f9b1ebSPrathyush K 		ret = platform_device_add(i2s->pdev);
1103c6f9b1ebSPrathyush K 		if (ret < 0)
1104c6f9b1ebSPrathyush K 			return NULL;
1105c6f9b1ebSPrathyush K 	}
11065033f43cSJassi Brar 
11075033f43cSJassi Brar 	return i2s;
11085033f43cSJassi Brar }
11095033f43cSJassi Brar 
11102b960386SSylwester Nawrocki static void i2s_free_sec_dai(struct i2s_dai *i2s)
11112b960386SSylwester Nawrocki {
11122b960386SSylwester Nawrocki 	platform_device_del(i2s->pdev);
11132b960386SSylwester Nawrocki }
11142b960386SSylwester Nawrocki 
1115641d334bSRafael J. Wysocki #ifdef CONFIG_PM
11165b1d3c34SR. Chandrasekar static int i2s_runtime_suspend(struct device *dev)
11175b1d3c34SR. Chandrasekar {
11185b1d3c34SR. Chandrasekar 	struct i2s_dai *i2s = dev_get_drvdata(dev);
11195b1d3c34SR. Chandrasekar 
11205b1d3c34SR. Chandrasekar 	clk_disable_unprepare(i2s->clk);
11215b1d3c34SR. Chandrasekar 
11225b1d3c34SR. Chandrasekar 	return 0;
11235b1d3c34SR. Chandrasekar }
11245b1d3c34SR. Chandrasekar 
11255b1d3c34SR. Chandrasekar static int i2s_runtime_resume(struct device *dev)
11265b1d3c34SR. Chandrasekar {
11275b1d3c34SR. Chandrasekar 	struct i2s_dai *i2s = dev_get_drvdata(dev);
11285b1d3c34SR. Chandrasekar 
11295b1d3c34SR. Chandrasekar 	clk_prepare_enable(i2s->clk);
11305b1d3c34SR. Chandrasekar 
11315b1d3c34SR. Chandrasekar 	return 0;
11325b1d3c34SR. Chandrasekar }
1133641d334bSRafael J. Wysocki #endif /* CONFIG_PM */
11345b1d3c34SR. Chandrasekar 
1135074b89bbSSylwester Nawrocki static void i2s_unregister_clocks(struct i2s_dai *i2s)
1136074b89bbSSylwester Nawrocki {
1137074b89bbSSylwester Nawrocki 	int i;
1138074b89bbSSylwester Nawrocki 
1139074b89bbSSylwester Nawrocki 	for (i = 0; i < i2s->clk_data.clk_num; i++) {
1140074b89bbSSylwester Nawrocki 		if (!IS_ERR(i2s->clk_table[i]))
1141074b89bbSSylwester Nawrocki 			clk_unregister(i2s->clk_table[i]);
1142074b89bbSSylwester Nawrocki 	}
1143074b89bbSSylwester Nawrocki }
1144074b89bbSSylwester Nawrocki 
1145074b89bbSSylwester Nawrocki static void i2s_unregister_clock_provider(struct platform_device *pdev)
1146074b89bbSSylwester Nawrocki {
1147074b89bbSSylwester Nawrocki 	struct i2s_dai *i2s = dev_get_drvdata(&pdev->dev);
1148074b89bbSSylwester Nawrocki 
1149074b89bbSSylwester Nawrocki 	of_clk_del_provider(pdev->dev.of_node);
1150074b89bbSSylwester Nawrocki 	i2s_unregister_clocks(i2s);
1151074b89bbSSylwester Nawrocki }
1152074b89bbSSylwester Nawrocki 
1153074b89bbSSylwester Nawrocki static int i2s_register_clock_provider(struct platform_device *pdev)
1154074b89bbSSylwester Nawrocki {
1155074b89bbSSylwester Nawrocki 	struct device *dev = &pdev->dev;
1156074b89bbSSylwester Nawrocki 	struct i2s_dai *i2s = dev_get_drvdata(dev);
1157074b89bbSSylwester Nawrocki 	const char *clk_name[2] = { "i2s_opclk0", "i2s_opclk1" };
1158074b89bbSSylwester Nawrocki 	const char *p_names[2] = { NULL };
1159074b89bbSSylwester Nawrocki 	const struct samsung_i2s_variant_regs *reg_info = i2s->variant_regs;
1160074b89bbSSylwester Nawrocki 	struct clk *rclksrc;
1161074b89bbSSylwester Nawrocki 	int ret, i;
1162074b89bbSSylwester Nawrocki 
1163074b89bbSSylwester Nawrocki 	/* Register the clock provider only if it's expected in the DTB */
1164074b89bbSSylwester Nawrocki 	if (!of_find_property(dev->of_node, "#clock-cells", NULL))
1165074b89bbSSylwester Nawrocki 		return 0;
1166074b89bbSSylwester Nawrocki 
1167074b89bbSSylwester Nawrocki 	/* Get the RCLKSRC mux clock parent clock names */
1168074b89bbSSylwester Nawrocki 	for (i = 0; i < ARRAY_SIZE(p_names); i++) {
1169074b89bbSSylwester Nawrocki 		rclksrc = clk_get(dev, clk_name[i]);
1170074b89bbSSylwester Nawrocki 		if (IS_ERR(rclksrc))
1171074b89bbSSylwester Nawrocki 			continue;
1172074b89bbSSylwester Nawrocki 		p_names[i] = __clk_get_name(rclksrc);
1173074b89bbSSylwester Nawrocki 		clk_put(rclksrc);
1174074b89bbSSylwester Nawrocki 	}
1175074b89bbSSylwester Nawrocki 
1176074b89bbSSylwester Nawrocki 	if (!(i2s->quirks & QUIRK_NO_MUXPSR)) {
1177074b89bbSSylwester Nawrocki 		/* Activate the prescaler */
1178074b89bbSSylwester Nawrocki 		u32 val = readl(i2s->addr + I2SPSR);
1179074b89bbSSylwester Nawrocki 		writel(val | PSR_PSREN, i2s->addr + I2SPSR);
1180074b89bbSSylwester Nawrocki 
1181074b89bbSSylwester Nawrocki 		i2s->clk_table[CLK_I2S_RCLK_SRC] = clk_register_mux(NULL,
1182074b89bbSSylwester Nawrocki 				"i2s_rclksrc", p_names, ARRAY_SIZE(p_names),
1183074b89bbSSylwester Nawrocki 				CLK_SET_RATE_NO_REPARENT | CLK_SET_RATE_PARENT,
1184074b89bbSSylwester Nawrocki 				i2s->addr + I2SMOD, reg_info->rclksrc_off,
1185074b89bbSSylwester Nawrocki 				1, 0, i2s->lock);
1186074b89bbSSylwester Nawrocki 
1187074b89bbSSylwester Nawrocki 		i2s->clk_table[CLK_I2S_RCLK_PSR] = clk_register_divider(NULL,
1188074b89bbSSylwester Nawrocki 				"i2s_presc", "i2s_rclksrc",
1189074b89bbSSylwester Nawrocki 				CLK_SET_RATE_PARENT,
1190074b89bbSSylwester Nawrocki 				i2s->addr + I2SPSR, 8, 6, 0, i2s->lock);
1191074b89bbSSylwester Nawrocki 
1192074b89bbSSylwester Nawrocki 		p_names[0] = "i2s_presc";
1193074b89bbSSylwester Nawrocki 		i2s->clk_data.clk_num = 2;
1194074b89bbSSylwester Nawrocki 	}
1195074b89bbSSylwester Nawrocki 	of_property_read_string_index(dev->of_node,
1196074b89bbSSylwester Nawrocki 				"clock-output-names", 0, &clk_name[0]);
1197074b89bbSSylwester Nawrocki 
1198074b89bbSSylwester Nawrocki 	i2s->clk_table[CLK_I2S_CDCLK] = clk_register_gate(NULL, clk_name[0],
1199074b89bbSSylwester Nawrocki 				p_names[0], CLK_SET_RATE_PARENT,
1200074b89bbSSylwester Nawrocki 				i2s->addr + I2SMOD, reg_info->cdclkcon_off,
1201074b89bbSSylwester Nawrocki 				CLK_GATE_SET_TO_DISABLE, i2s->lock);
1202074b89bbSSylwester Nawrocki 
1203074b89bbSSylwester Nawrocki 	i2s->clk_data.clk_num += 1;
1204074b89bbSSylwester Nawrocki 	i2s->clk_data.clks = i2s->clk_table;
1205074b89bbSSylwester Nawrocki 
1206074b89bbSSylwester Nawrocki 	ret = of_clk_add_provider(dev->of_node, of_clk_src_onecell_get,
1207074b89bbSSylwester Nawrocki 				  &i2s->clk_data);
1208074b89bbSSylwester Nawrocki 	if (ret < 0) {
1209074b89bbSSylwester Nawrocki 		dev_err(dev, "failed to add clock provider: %d\n", ret);
1210074b89bbSSylwester Nawrocki 		i2s_unregister_clocks(i2s);
1211074b89bbSSylwester Nawrocki 	}
1212074b89bbSSylwester Nawrocki 
1213074b89bbSSylwester Nawrocki 	return ret;
1214074b89bbSSylwester Nawrocki }
1215074b89bbSSylwester Nawrocki 
1216fdca21adSBill Pemberton static int samsung_i2s_probe(struct platform_device *pdev)
12175033f43cSJassi Brar {
12185033f43cSJassi Brar 	struct i2s_dai *pri_dai, *sec_dai = NULL;
121940476f61SPadmavathi Venna 	struct s3c_audio_pdata *i2s_pdata = pdev->dev.platform_data;
122040476f61SPadmavathi Venna 	struct samsung_i2s *i2s_cfg = NULL;
12215033f43cSJassi Brar 	struct resource *res;
122240476f61SPadmavathi Venna 	u32 regs_base, quirks = 0, idma_addr = 0;
122340476f61SPadmavathi Venna 	struct device_node *np = pdev->dev.of_node;
12247da493e9SPadmavathi Venna 	const struct samsung_i2s_dai_data *i2s_dai_data;
1225c92f1d0eSSylwester Nawrocki 	int ret;
12265033f43cSJassi Brar 
12272f7b5d14SSylwester Nawrocki 	if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node)
12282f7b5d14SSylwester Nawrocki 		i2s_dai_data = of_device_get_match_data(&pdev->dev);
12292f7b5d14SSylwester Nawrocki 	else
12302f7b5d14SSylwester Nawrocki 		i2s_dai_data = (struct samsung_i2s_dai_data *)
12312f7b5d14SSylwester Nawrocki 				platform_get_device_id(pdev)->driver_data;
12327c62eebbSPadmavathi Venna 
12332f7b5d14SSylwester Nawrocki 	/* Call during the secondary interface registration */
12347da493e9SPadmavathi Venna 	if (i2s_dai_data->dai_type == TYPE_SEC) {
12355033f43cSJassi Brar 		sec_dai = dev_get_drvdata(&pdev->dev);
1236a9b977ecSPrathyush K 		if (!sec_dai) {
1237a9b977ecSPrathyush K 			dev_err(&pdev->dev, "Unable to get drvdata\n");
1238a9b977ecSPrathyush K 			return -EFAULT;
1239a9b977ecSPrathyush K 		}
124053f7faa1SSylwester Nawrocki 		ret = devm_snd_soc_register_component(&sec_dai->pdev->dev,
12414b828535SKuninori Morimoto 						&samsung_i2s_component,
12424b828535SKuninori Morimoto 						&sec_dai->i2s_dai_drv, 1);
124353f7faa1SSylwester Nawrocki 		if (ret != 0)
124453f7faa1SSylwester Nawrocki 			return ret;
124553f7faa1SSylwester Nawrocki 
12469bdca822SArnd Bergmann 		return samsung_asoc_dma_platform_register(&pdev->dev,
124742a74e77SSylwester Nawrocki 					sec_dai->filter, "tx-sec", NULL);
12485033f43cSJassi Brar 	}
12495033f43cSJassi Brar 
125040476f61SPadmavathi Venna 	pri_dai = i2s_alloc_dai(pdev, false);
125140476f61SPadmavathi Venna 	if (!pri_dai) {
125240476f61SPadmavathi Venna 		dev_err(&pdev->dev, "Unable to alloc I2S_pri\n");
125340476f61SPadmavathi Venna 		return -ENOMEM;
125440476f61SPadmavathi Venna 	}
125540476f61SPadmavathi Venna 
1256f3670536SSylwester Nawrocki 	spin_lock_init(&pri_dai->spinlock);
1257f3670536SSylwester Nawrocki 	pri_dai->lock = &pri_dai->spinlock;
1258f3670536SSylwester Nawrocki 
125940476f61SPadmavathi Venna 	if (!np) {
12605033f43cSJassi Brar 		if (i2s_pdata == NULL) {
12615033f43cSJassi Brar 			dev_err(&pdev->dev, "Can't work without s3c_audio_pdata\n");
12625033f43cSJassi Brar 			return -EINVAL;
12635033f43cSJassi Brar 		}
12645033f43cSJassi Brar 
126569e7a69aSSylwester Nawrocki 		pri_dai->dma_playback.filter_data = i2s_pdata->dma_playback;
126669e7a69aSSylwester Nawrocki 		pri_dai->dma_capture.filter_data = i2s_pdata->dma_capture;
12679bdca822SArnd Bergmann 		pri_dai->filter = i2s_pdata->dma_filter;
1268b9a1a743SArnd Bergmann 
126940476f61SPadmavathi Venna 		if (&i2s_pdata->type)
127040476f61SPadmavathi Venna 			i2s_cfg = &i2s_pdata->type.i2s;
12715033f43cSJassi Brar 
127240476f61SPadmavathi Venna 		if (i2s_cfg) {
127340476f61SPadmavathi Venna 			quirks = i2s_cfg->quirks;
127440476f61SPadmavathi Venna 			idma_addr = i2s_cfg->idma_addr;
12755033f43cSJassi Brar 		}
127640476f61SPadmavathi Venna 	} else {
12777da493e9SPadmavathi Venna 		quirks = i2s_dai_data->quirks;
127840476f61SPadmavathi Venna 		if (of_property_read_u32(np, "samsung,idma-addr",
127940476f61SPadmavathi Venna 					 &idma_addr)) {
1280b0759736SPadmavathi Venna 			if (quirks & QUIRK_SUPPORTS_IDMA) {
1281b0759736SPadmavathi Venna 				dev_info(&pdev->dev, "idma address is not"\
128240476f61SPadmavathi Venna 						"specified");
128340476f61SPadmavathi Venna 			}
128440476f61SPadmavathi Venna 		}
128540476f61SPadmavathi Venna 	}
12865033f43cSJassi Brar 
12875033f43cSJassi Brar 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1288af1cf5cfSSylwester Nawrocki 	pri_dai->addr = devm_ioremap_resource(&pdev->dev, res);
1289af1cf5cfSSylwester Nawrocki 	if (IS_ERR(pri_dai->addr))
1290af1cf5cfSSylwester Nawrocki 		return PTR_ERR(pri_dai->addr);
12915033f43cSJassi Brar 
12925033f43cSJassi Brar 	regs_base = res->start;
12935033f43cSJassi Brar 
12940ec2ba80SSylwester Nawrocki 	pri_dai->clk = devm_clk_get(&pdev->dev, "iis");
12950ec2ba80SSylwester Nawrocki 	if (IS_ERR(pri_dai->clk)) {
12960ec2ba80SSylwester Nawrocki 		dev_err(&pdev->dev, "Failed to get iis clock\n");
12970ec2ba80SSylwester Nawrocki 		return PTR_ERR(pri_dai->clk);
12980ec2ba80SSylwester Nawrocki 	}
1299c92f1d0eSSylwester Nawrocki 
1300c92f1d0eSSylwester Nawrocki 	ret = clk_prepare_enable(pri_dai->clk);
1301c92f1d0eSSylwester Nawrocki 	if (ret != 0) {
1302c92f1d0eSSylwester Nawrocki 		dev_err(&pdev->dev, "failed to enable clock: %d\n", ret);
1303c92f1d0eSSylwester Nawrocki 		return ret;
1304c92f1d0eSSylwester Nawrocki 	}
130569e7a69aSSylwester Nawrocki 	pri_dai->dma_playback.addr = regs_base + I2STXD;
130669e7a69aSSylwester Nawrocki 	pri_dai->dma_capture.addr = regs_base + I2SRXD;
130769e7a69aSSylwester Nawrocki 	pri_dai->dma_playback.chan_name = "tx";
130869e7a69aSSylwester Nawrocki 	pri_dai->dma_capture.chan_name = "rx";
130969e7a69aSSylwester Nawrocki 	pri_dai->dma_playback.addr_width = 4;
131069e7a69aSSylwester Nawrocki 	pri_dai->dma_capture.addr_width = 4;
13115033f43cSJassi Brar 	pri_dai->quirks = quirks;
1312a5a56871SPadmavathi Venna 	pri_dai->variant_regs = i2s_dai_data->i2s_variant_regs;
13135033f43cSJassi Brar 
13145033f43cSJassi Brar 	if (quirks & QUIRK_PRI_6CHAN)
13155033f43cSJassi Brar 		pri_dai->i2s_dai_drv.playback.channels_max = 6;
13165033f43cSJassi Brar 
13175033f43cSJassi Brar 	if (quirks & QUIRK_SEC_DAI) {
13185033f43cSJassi Brar 		sec_dai = i2s_alloc_dai(pdev, true);
13195033f43cSJassi Brar 		if (!sec_dai) {
13205033f43cSJassi Brar 			dev_err(&pdev->dev, "Unable to alloc I2S_sec\n");
1321*fd61576fSWei Yongjun 			ret = -ENOMEM;
1322*fd61576fSWei Yongjun 			goto err_disable_clk;
13235033f43cSJassi Brar 		}
13247e5d8706SSylwester Nawrocki 
1325f3670536SSylwester Nawrocki 		sec_dai->lock = &pri_dai->spinlock;
13267e5d8706SSylwester Nawrocki 		sec_dai->variant_regs = pri_dai->variant_regs;
132769e7a69aSSylwester Nawrocki 		sec_dai->dma_playback.addr = regs_base + I2STXDS;
132869e7a69aSSylwester Nawrocki 		sec_dai->dma_playback.chan_name = "tx-sec";
132940476f61SPadmavathi Venna 
13309bdca822SArnd Bergmann 		if (!np) {
133169e7a69aSSylwester Nawrocki 			sec_dai->dma_playback.filter_data = i2s_pdata->dma_play_sec;
13329bdca822SArnd Bergmann 			sec_dai->filter = i2s_pdata->dma_filter;
13339bdca822SArnd Bergmann 		}
133440476f61SPadmavathi Venna 
133569e7a69aSSylwester Nawrocki 		sec_dai->dma_playback.addr_width = 4;
1336af1cf5cfSSylwester Nawrocki 		sec_dai->addr = pri_dai->addr;
13370ec2ba80SSylwester Nawrocki 		sec_dai->clk = pri_dai->clk;
13385033f43cSJassi Brar 		sec_dai->quirks = quirks;
133969e7a69aSSylwester Nawrocki 		sec_dai->idma_playback.addr = idma_addr;
13405033f43cSJassi Brar 		sec_dai->pri_dai = pri_dai;
13415033f43cSJassi Brar 		pri_dai->sec_dai = sec_dai;
13425033f43cSJassi Brar 	}
13435033f43cSJassi Brar 
13440429ffefSMark Brown 	if (i2s_pdata && i2s_pdata->cfg_gpio && i2s_pdata->cfg_gpio(pdev)) {
134540476f61SPadmavathi Venna 		dev_err(&pdev->dev, "Unable to configure gpio\n");
1346*fd61576fSWei Yongjun 		ret = -EINVAL;
1347*fd61576fSWei Yongjun 		goto err_disable_clk;
134840476f61SPadmavathi Venna 	}
13495033f43cSJassi Brar 
13502b960386SSylwester Nawrocki 	ret = devm_snd_soc_register_component(&pri_dai->pdev->dev,
1351d644a115SMark Brown 					&samsung_i2s_component,
13524b828535SKuninori Morimoto 					&pri_dai->i2s_dai_drv, 1);
13532b960386SSylwester Nawrocki 	if (ret < 0)
13542b960386SSylwester Nawrocki 		goto err_free_dai;
13552b960386SSylwester Nawrocki 
135642a74e77SSylwester Nawrocki 	ret = samsung_asoc_dma_platform_register(&pdev->dev, pri_dai->filter,
135742a74e77SSylwester Nawrocki 						 NULL, NULL);
13582b960386SSylwester Nawrocki 	if (ret < 0)
13592b960386SSylwester Nawrocki 		goto err_free_dai;
13605033f43cSJassi Brar 
1361c5cf4dbcSMark Brown 	pm_runtime_enable(&pdev->dev);
1362c5cf4dbcSMark Brown 
13632b960386SSylwester Nawrocki 	ret = i2s_register_clock_provider(pdev);
13642b960386SSylwester Nawrocki 	if (!ret)
13652b960386SSylwester Nawrocki 		return 0;
1366a08485d8SPadmavathi Venna 
13672b960386SSylwester Nawrocki 	pm_runtime_disable(&pdev->dev);
13682b960386SSylwester Nawrocki err_free_dai:
13692b960386SSylwester Nawrocki 	if (sec_dai)
13702b960386SSylwester Nawrocki 		i2s_free_sec_dai(sec_dai);
1371*fd61576fSWei Yongjun err_disable_clk:
1372*fd61576fSWei Yongjun 	clk_disable_unprepare(pri_dai->clk);
13732b960386SSylwester Nawrocki 	return ret;
13745033f43cSJassi Brar }
13755033f43cSJassi Brar 
1376fdca21adSBill Pemberton static int samsung_i2s_remove(struct platform_device *pdev)
13775033f43cSJassi Brar {
13785033f43cSJassi Brar 	struct i2s_dai *i2s, *other;
13795033f43cSJassi Brar 
13805033f43cSJassi Brar 	i2s = dev_get_drvdata(&pdev->dev);
1381dcd60fc3SSylwester Nawrocki 	other = get_other_dai(i2s);
13825033f43cSJassi Brar 
13835033f43cSJassi Brar 	if (other) {
13845033f43cSJassi Brar 		other->pri_dai = NULL;
13855033f43cSJassi Brar 		other->sec_dai = NULL;
13865033f43cSJassi Brar 	} else {
1387c5cf4dbcSMark Brown 		pm_runtime_disable(&pdev->dev);
13885033f43cSJassi Brar 	}
13895033f43cSJassi Brar 
1390074b89bbSSylwester Nawrocki 	if (!is_secondary(i2s)) {
1391074b89bbSSylwester Nawrocki 		i2s_unregister_clock_provider(pdev);
1392c92f1d0eSSylwester Nawrocki 		clk_disable_unprepare(i2s->clk);
1393074b89bbSSylwester Nawrocki 	}
1394c92f1d0eSSylwester Nawrocki 
13955033f43cSJassi Brar 	i2s->pri_dai = NULL;
13965033f43cSJassi Brar 	i2s->sec_dai = NULL;
13975033f43cSJassi Brar 
13985033f43cSJassi Brar 	return 0;
13995033f43cSJassi Brar }
14005033f43cSJassi Brar 
1401a5a56871SPadmavathi Venna static const struct samsung_i2s_variant_regs i2sv3_regs = {
1402a5a56871SPadmavathi Venna 	.bfs_off = 1,
1403a5a56871SPadmavathi Venna 	.rfs_off = 3,
1404a5a56871SPadmavathi Venna 	.sdf_off = 5,
1405a5a56871SPadmavathi Venna 	.txr_off = 8,
1406a5a56871SPadmavathi Venna 	.rclksrc_off = 10,
1407a5a56871SPadmavathi Venna 	.mss_off = 11,
1408a5a56871SPadmavathi Venna 	.cdclkcon_off = 12,
1409a5a56871SPadmavathi Venna 	.lrp_off = 7,
1410a5a56871SPadmavathi Venna 	.bfs_mask = 0x3,
1411a5a56871SPadmavathi Venna 	.rfs_mask = 0x3,
1412a5a56871SPadmavathi Venna 	.ftx0cnt_off = 8,
1413a5a56871SPadmavathi Venna };
1414a5a56871SPadmavathi Venna 
1415a5a56871SPadmavathi Venna static const struct samsung_i2s_variant_regs i2sv6_regs = {
1416a5a56871SPadmavathi Venna 	.bfs_off = 0,
1417a5a56871SPadmavathi Venna 	.rfs_off = 4,
1418a5a56871SPadmavathi Venna 	.sdf_off = 6,
1419a5a56871SPadmavathi Venna 	.txr_off = 8,
1420a5a56871SPadmavathi Venna 	.rclksrc_off = 10,
1421a5a56871SPadmavathi Venna 	.mss_off = 11,
1422a5a56871SPadmavathi Venna 	.cdclkcon_off = 12,
1423a5a56871SPadmavathi Venna 	.lrp_off = 15,
1424a5a56871SPadmavathi Venna 	.bfs_mask = 0xf,
1425a5a56871SPadmavathi Venna 	.rfs_mask = 0x3,
1426a5a56871SPadmavathi Venna 	.ftx0cnt_off = 8,
1427a5a56871SPadmavathi Venna };
1428a5a56871SPadmavathi Venna 
1429a5a56871SPadmavathi Venna static const struct samsung_i2s_variant_regs i2sv7_regs = {
1430a5a56871SPadmavathi Venna 	.bfs_off = 0,
1431a5a56871SPadmavathi Venna 	.rfs_off = 4,
1432a5a56871SPadmavathi Venna 	.sdf_off = 7,
1433a5a56871SPadmavathi Venna 	.txr_off = 9,
1434a5a56871SPadmavathi Venna 	.rclksrc_off = 11,
1435a5a56871SPadmavathi Venna 	.mss_off = 12,
1436a5a56871SPadmavathi Venna 	.cdclkcon_off = 22,
1437a5a56871SPadmavathi Venna 	.lrp_off = 15,
1438a5a56871SPadmavathi Venna 	.bfs_mask = 0xf,
1439a5a56871SPadmavathi Venna 	.rfs_mask = 0x7,
1440a5a56871SPadmavathi Venna 	.ftx0cnt_off = 0,
1441a5a56871SPadmavathi Venna };
1442a5a56871SPadmavathi Venna 
1443a5a56871SPadmavathi Venna static const struct samsung_i2s_variant_regs i2sv5_i2s1_regs = {
1444a5a56871SPadmavathi Venna 	.bfs_off = 0,
1445a5a56871SPadmavathi Venna 	.rfs_off = 3,
1446a5a56871SPadmavathi Venna 	.sdf_off = 6,
1447a5a56871SPadmavathi Venna 	.txr_off = 8,
1448a5a56871SPadmavathi Venna 	.rclksrc_off = 10,
1449a5a56871SPadmavathi Venna 	.mss_off = 11,
1450a5a56871SPadmavathi Venna 	.cdclkcon_off = 12,
1451a5a56871SPadmavathi Venna 	.lrp_off = 15,
1452a5a56871SPadmavathi Venna 	.bfs_mask = 0x7,
1453a5a56871SPadmavathi Venna 	.rfs_mask = 0x7,
1454a5a56871SPadmavathi Venna 	.ftx0cnt_off = 8,
1455a5a56871SPadmavathi Venna };
1456a5a56871SPadmavathi Venna 
14577da493e9SPadmavathi Venna static const struct samsung_i2s_dai_data i2sv3_dai_type = {
14587da493e9SPadmavathi Venna 	.dai_type = TYPE_PRI,
14597da493e9SPadmavathi Venna 	.quirks = QUIRK_NO_MUXPSR,
1460a5a56871SPadmavathi Venna 	.i2s_variant_regs = &i2sv3_regs,
14617da493e9SPadmavathi Venna };
14627da493e9SPadmavathi Venna 
14637da493e9SPadmavathi Venna static const struct samsung_i2s_dai_data i2sv5_dai_type = {
14647da493e9SPadmavathi Venna 	.dai_type = TYPE_PRI,
1465b0759736SPadmavathi Venna 	.quirks = QUIRK_PRI_6CHAN | QUIRK_SEC_DAI | QUIRK_NEED_RSTCLR |
1466b0759736SPadmavathi Venna 			QUIRK_SUPPORTS_IDMA,
1467a5a56871SPadmavathi Venna 	.i2s_variant_regs = &i2sv3_regs,
14687da493e9SPadmavathi Venna };
14697da493e9SPadmavathi Venna 
14704ca0c0d4SPadmavathi Venna static const struct samsung_i2s_dai_data i2sv6_dai_type = {
14714ca0c0d4SPadmavathi Venna 	.dai_type = TYPE_PRI,
14724ca0c0d4SPadmavathi Venna 	.quirks = QUIRK_PRI_6CHAN | QUIRK_SEC_DAI | QUIRK_NEED_RSTCLR |
1473b0759736SPadmavathi Venna 			QUIRK_SUPPORTS_TDM | QUIRK_SUPPORTS_IDMA,
1474a5a56871SPadmavathi Venna 	.i2s_variant_regs = &i2sv6_regs,
1475a5a56871SPadmavathi Venna };
1476a5a56871SPadmavathi Venna 
1477a5a56871SPadmavathi Venna static const struct samsung_i2s_dai_data i2sv7_dai_type = {
1478a5a56871SPadmavathi Venna 	.dai_type = TYPE_PRI,
1479a5a56871SPadmavathi Venna 	.quirks = QUIRK_PRI_6CHAN | QUIRK_SEC_DAI | QUIRK_NEED_RSTCLR |
1480a5a56871SPadmavathi Venna 			QUIRK_SUPPORTS_TDM,
1481a5a56871SPadmavathi Venna 	.i2s_variant_regs = &i2sv7_regs,
1482a5a56871SPadmavathi Venna };
1483a5a56871SPadmavathi Venna 
1484a5a56871SPadmavathi Venna static const struct samsung_i2s_dai_data i2sv5_dai_type_i2s1 = {
1485a5a56871SPadmavathi Venna 	.dai_type = TYPE_PRI,
1486a5a56871SPadmavathi Venna 	.quirks = QUIRK_PRI_6CHAN | QUIRK_NEED_RSTCLR,
1487a5a56871SPadmavathi Venna 	.i2s_variant_regs = &i2sv5_i2s1_regs,
14884ca0c0d4SPadmavathi Venna };
14894ca0c0d4SPadmavathi Venna 
14907da493e9SPadmavathi Venna static const struct samsung_i2s_dai_data samsung_dai_type_sec = {
14917da493e9SPadmavathi Venna 	.dai_type = TYPE_SEC,
14927da493e9SPadmavathi Venna };
14937da493e9SPadmavathi Venna 
1494eb8ca0faSKrzysztof Kozlowski static const struct platform_device_id samsung_i2s_driver_ids[] = {
14957c62eebbSPadmavathi Venna 	{
14967c62eebbSPadmavathi Venna 		.name           = "samsung-i2s",
14973f024980SMark Brown 		.driver_data	= (kernel_ulong_t)&i2sv3_dai_type,
14987c62eebbSPadmavathi Venna 	}, {
14997c62eebbSPadmavathi Venna 		.name           = "samsung-i2s-sec",
15007da493e9SPadmavathi Venna 		.driver_data    = (kernel_ulong_t)&samsung_dai_type_sec,
15017c62eebbSPadmavathi Venna 	},
15027c62eebbSPadmavathi Venna 	{},
15037c62eebbSPadmavathi Venna };
15042af19558SArnd Bergmann MODULE_DEVICE_TABLE(platform, samsung_i2s_driver_ids);
15057c62eebbSPadmavathi Venna 
150640476f61SPadmavathi Venna #ifdef CONFIG_OF
150740476f61SPadmavathi Venna static const struct of_device_id exynos_i2s_match[] = {
15087da493e9SPadmavathi Venna 	{
15097da493e9SPadmavathi Venna 		.compatible = "samsung,s3c6410-i2s",
15107da493e9SPadmavathi Venna 		.data = &i2sv3_dai_type,
15117da493e9SPadmavathi Venna 	}, {
15127da493e9SPadmavathi Venna 		.compatible = "samsung,s5pv210-i2s",
15137da493e9SPadmavathi Venna 		.data = &i2sv5_dai_type,
15144ca0c0d4SPadmavathi Venna 	}, {
15154ca0c0d4SPadmavathi Venna 		.compatible = "samsung,exynos5420-i2s",
15164ca0c0d4SPadmavathi Venna 		.data = &i2sv6_dai_type,
1517a5a56871SPadmavathi Venna 	}, {
1518a5a56871SPadmavathi Venna 		.compatible = "samsung,exynos7-i2s",
1519a5a56871SPadmavathi Venna 		.data = &i2sv7_dai_type,
1520a5a56871SPadmavathi Venna 	}, {
1521a5a56871SPadmavathi Venna 		.compatible = "samsung,exynos7-i2s1",
1522a5a56871SPadmavathi Venna 		.data = &i2sv5_dai_type_i2s1,
152340476f61SPadmavathi Venna 	},
152440476f61SPadmavathi Venna 	{},
152540476f61SPadmavathi Venna };
152640476f61SPadmavathi Venna MODULE_DEVICE_TABLE(of, exynos_i2s_match);
152740476f61SPadmavathi Venna #endif
152840476f61SPadmavathi Venna 
15295b1d3c34SR. Chandrasekar static const struct dev_pm_ops samsung_i2s_pm = {
15305b1d3c34SR. Chandrasekar 	SET_RUNTIME_PM_OPS(i2s_runtime_suspend,
15315b1d3c34SR. Chandrasekar 				i2s_runtime_resume, NULL)
15325b1d3c34SR. Chandrasekar };
15335b1d3c34SR. Chandrasekar 
15345033f43cSJassi Brar static struct platform_driver samsung_i2s_driver = {
15355033f43cSJassi Brar 	.probe  = samsung_i2s_probe,
1536fdca21adSBill Pemberton 	.remove = samsung_i2s_remove,
15377c62eebbSPadmavathi Venna 	.id_table = samsung_i2s_driver_ids,
15385033f43cSJassi Brar 	.driver = {
15395033f43cSJassi Brar 		.name = "samsung-i2s",
154040476f61SPadmavathi Venna 		.of_match_table = of_match_ptr(exynos_i2s_match),
15415b1d3c34SR. Chandrasekar 		.pm = &samsung_i2s_pm,
15425033f43cSJassi Brar 	},
15435033f43cSJassi Brar };
15445033f43cSJassi Brar 
1545e00c3f55SMark Brown module_platform_driver(samsung_i2s_driver);
15465033f43cSJassi Brar 
15475033f43cSJassi Brar /* Module information */
1548df8ad335SJaswinder Singh MODULE_AUTHOR("Jaswinder Singh, <jassisinghbrar@gmail.com>");
15495033f43cSJassi Brar MODULE_DESCRIPTION("Samsung I2S Interface");
15505033f43cSJassi Brar MODULE_ALIAS("platform:samsung-i2s");
15515033f43cSJassi Brar MODULE_LICENSE("GPL");
1552