xref: /linux/sound/soc/samsung/i2s.c (revision e7e52dfc68a2160570c7ec51415e391961160edb)
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 
37a5a56871SPadmavathi Venna struct samsung_i2s_variant_regs {
38a5a56871SPadmavathi Venna 	unsigned int	bfs_off;
39a5a56871SPadmavathi Venna 	unsigned int	rfs_off;
40a5a56871SPadmavathi Venna 	unsigned int	sdf_off;
41a5a56871SPadmavathi Venna 	unsigned int	txr_off;
42a5a56871SPadmavathi Venna 	unsigned int	rclksrc_off;
43a5a56871SPadmavathi Venna 	unsigned int	mss_off;
44a5a56871SPadmavathi Venna 	unsigned int	cdclkcon_off;
45a5a56871SPadmavathi Venna 	unsigned int	lrp_off;
46a5a56871SPadmavathi Venna 	unsigned int	bfs_mask;
47a5a56871SPadmavathi Venna 	unsigned int	rfs_mask;
48a5a56871SPadmavathi Venna 	unsigned int	ftx0cnt_off;
49a5a56871SPadmavathi Venna };
50a5a56871SPadmavathi Venna 
5140476f61SPadmavathi Venna struct samsung_i2s_dai_data {
527da493e9SPadmavathi Venna 	u32 quirks;
53a5a56871SPadmavathi Venna 	const struct samsung_i2s_variant_regs *i2s_variant_regs;
5440476f61SPadmavathi Venna };
5540476f61SPadmavathi Venna 
565033f43cSJassi Brar struct i2s_dai {
575033f43cSJassi Brar 	/* Platform device for this DAI */
585033f43cSJassi Brar 	struct platform_device *pdev;
59af1cf5cfSSylwester Nawrocki 	/* Memory mapped SFR region */
605033f43cSJassi Brar 	void __iomem	*addr;
615033f43cSJassi Brar 	/* Rate of RCLK source clock */
625033f43cSJassi Brar 	unsigned long rclk_srcrate;
635033f43cSJassi Brar 	/* Frame Clock */
645033f43cSJassi Brar 	unsigned frmclk;
655033f43cSJassi Brar 	/*
665033f43cSJassi Brar 	 * Specifically requested RCLK,BCLK by MACHINE Driver.
675033f43cSJassi Brar 	 * 0 indicates CPU driver is free to choose any value.
685033f43cSJassi Brar 	 */
695033f43cSJassi Brar 	unsigned rfs, bfs;
705033f43cSJassi Brar 	/* I2S Controller's core clock */
715033f43cSJassi Brar 	struct clk *clk;
725033f43cSJassi Brar 	/* Clock for generating I2S signals */
735033f43cSJassi Brar 	struct clk *op_clk;
745033f43cSJassi Brar 	/* Pointer to the Primary_Fifo if this is Sec_Fifo, NULL otherwise */
755033f43cSJassi Brar 	struct i2s_dai *pri_dai;
765033f43cSJassi Brar 	/* Pointer to the Secondary_Fifo if it has one, NULL otherwise */
775033f43cSJassi Brar 	struct i2s_dai *sec_dai;
785033f43cSJassi Brar #define DAI_OPENED	(1 << 0) /* Dai is opened */
795033f43cSJassi Brar #define DAI_MANAGER	(1 << 1) /* Dai is the manager */
805033f43cSJassi Brar 	unsigned mode;
815033f43cSJassi Brar 	/* Driver for this DAI */
825033f43cSJassi Brar 	struct snd_soc_dai_driver i2s_dai_drv;
835033f43cSJassi Brar 	/* DMA parameters */
8469e7a69aSSylwester Nawrocki 	struct snd_dmaengine_dai_dma_data dma_playback;
8569e7a69aSSylwester Nawrocki 	struct snd_dmaengine_dai_dma_data dma_capture;
8669e7a69aSSylwester Nawrocki 	struct snd_dmaengine_dai_dma_data idma_playback;
879bdca822SArnd Bergmann 	dma_filter_fn filter;
885033f43cSJassi Brar 	u32	quirks;
895033f43cSJassi Brar 	u32	suspend_i2smod;
905033f43cSJassi Brar 	u32	suspend_i2scon;
915033f43cSJassi Brar 	u32	suspend_i2spsr;
92a5a56871SPadmavathi Venna 	const struct samsung_i2s_variant_regs *variant_regs;
93f3670536SSylwester Nawrocki 
94f3670536SSylwester Nawrocki 	/* Spinlock protecting access to the device's registers */
95f3670536SSylwester Nawrocki 	spinlock_t spinlock;
96f3670536SSylwester Nawrocki 	spinlock_t *lock;
97074b89bbSSylwester Nawrocki 
98074b89bbSSylwester Nawrocki 	/* Below fields are only valid if this is the primary FIFO */
99074b89bbSSylwester Nawrocki 	struct clk *clk_table[3];
100074b89bbSSylwester Nawrocki 	struct clk_onecell_data clk_data;
1015033f43cSJassi Brar };
1025033f43cSJassi Brar 
1035033f43cSJassi Brar /* Lock for cross i/f checks */
1045033f43cSJassi Brar static DEFINE_SPINLOCK(lock);
1055033f43cSJassi Brar 
1065033f43cSJassi Brar /* If this is the 'overlay' stereo DAI */
1075033f43cSJassi Brar static inline bool is_secondary(struct i2s_dai *i2s)
1085033f43cSJassi Brar {
1095033f43cSJassi Brar 	return i2s->pri_dai ? true : false;
1105033f43cSJassi Brar }
1115033f43cSJassi Brar 
1125033f43cSJassi Brar /* If operating in SoC-Slave mode */
1135033f43cSJassi Brar static inline bool is_slave(struct i2s_dai *i2s)
1145033f43cSJassi Brar {
115a5a56871SPadmavathi Venna 	u32 mod = readl(i2s->addr + I2SMOD);
116a5a56871SPadmavathi Venna 	return (mod & (1 << i2s->variant_regs->mss_off)) ? true : false;
1175033f43cSJassi Brar }
1185033f43cSJassi Brar 
1195033f43cSJassi Brar /* If this interface of the controller is transmitting data */
1205033f43cSJassi Brar static inline bool tx_active(struct i2s_dai *i2s)
1215033f43cSJassi Brar {
1225033f43cSJassi Brar 	u32 active;
1235033f43cSJassi Brar 
1245033f43cSJassi Brar 	if (!i2s)
1255033f43cSJassi Brar 		return false;
1265033f43cSJassi Brar 
12733195500SSangbeom Kim 	active = readl(i2s->addr + I2SCON);
1285033f43cSJassi Brar 
1295033f43cSJassi Brar 	if (is_secondary(i2s))
1305033f43cSJassi Brar 		active &= CON_TXSDMA_ACTIVE;
1315033f43cSJassi Brar 	else
1325033f43cSJassi Brar 		active &= CON_TXDMA_ACTIVE;
1335033f43cSJassi Brar 
1345033f43cSJassi Brar 	return active ? true : false;
1355033f43cSJassi Brar }
1365033f43cSJassi Brar 
137dcd60fc3SSylwester Nawrocki /* Return pointer to the other DAI */
138dcd60fc3SSylwester Nawrocki static inline struct i2s_dai *get_other_dai(struct i2s_dai *i2s)
139dcd60fc3SSylwester Nawrocki {
140dcd60fc3SSylwester Nawrocki 	return i2s->pri_dai ? : i2s->sec_dai;
141dcd60fc3SSylwester Nawrocki }
142dcd60fc3SSylwester Nawrocki 
1435033f43cSJassi Brar /* If the other interface of the controller is transmitting data */
1445033f43cSJassi Brar static inline bool other_tx_active(struct i2s_dai *i2s)
1455033f43cSJassi Brar {
146dcd60fc3SSylwester Nawrocki 	struct i2s_dai *other = get_other_dai(i2s);
1475033f43cSJassi Brar 
1485033f43cSJassi Brar 	return tx_active(other);
1495033f43cSJassi Brar }
1505033f43cSJassi Brar 
1515033f43cSJassi Brar /* If any interface of the controller is transmitting data */
1525033f43cSJassi Brar static inline bool any_tx_active(struct i2s_dai *i2s)
1535033f43cSJassi Brar {
1545033f43cSJassi Brar 	return tx_active(i2s) || other_tx_active(i2s);
1555033f43cSJassi Brar }
1565033f43cSJassi Brar 
1575033f43cSJassi Brar /* If this interface of the controller is receiving data */
1585033f43cSJassi Brar static inline bool rx_active(struct i2s_dai *i2s)
1595033f43cSJassi Brar {
1605033f43cSJassi Brar 	u32 active;
1615033f43cSJassi Brar 
1625033f43cSJassi Brar 	if (!i2s)
1635033f43cSJassi Brar 		return false;
1645033f43cSJassi Brar 
16533195500SSangbeom Kim 	active = readl(i2s->addr + I2SCON) & CON_RXDMA_ACTIVE;
1665033f43cSJassi Brar 
1675033f43cSJassi Brar 	return active ? true : false;
1685033f43cSJassi Brar }
1695033f43cSJassi Brar 
1705033f43cSJassi Brar /* If the other interface of the controller is receiving data */
1715033f43cSJassi Brar static inline bool other_rx_active(struct i2s_dai *i2s)
1725033f43cSJassi Brar {
173dcd60fc3SSylwester Nawrocki 	struct i2s_dai *other = get_other_dai(i2s);
1745033f43cSJassi Brar 
1755033f43cSJassi Brar 	return rx_active(other);
1765033f43cSJassi Brar }
1775033f43cSJassi Brar 
1785033f43cSJassi Brar /* If any interface of the controller is receiving data */
1795033f43cSJassi Brar static inline bool any_rx_active(struct i2s_dai *i2s)
1805033f43cSJassi Brar {
1815033f43cSJassi Brar 	return rx_active(i2s) || other_rx_active(i2s);
1825033f43cSJassi Brar }
1835033f43cSJassi Brar 
1845033f43cSJassi Brar /* If the other DAI is transmitting or receiving data */
1855033f43cSJassi Brar static inline bool other_active(struct i2s_dai *i2s)
1865033f43cSJassi Brar {
1875033f43cSJassi Brar 	return other_rx_active(i2s) || other_tx_active(i2s);
1885033f43cSJassi Brar }
1895033f43cSJassi Brar 
1905033f43cSJassi Brar /* If this DAI is transmitting or receiving data */
1915033f43cSJassi Brar static inline bool this_active(struct i2s_dai *i2s)
1925033f43cSJassi Brar {
1935033f43cSJassi Brar 	return tx_active(i2s) || rx_active(i2s);
1945033f43cSJassi Brar }
1955033f43cSJassi Brar 
1965033f43cSJassi Brar /* If the controller is active anyway */
1975033f43cSJassi Brar static inline bool any_active(struct i2s_dai *i2s)
1985033f43cSJassi Brar {
1995033f43cSJassi Brar 	return this_active(i2s) || other_active(i2s);
2005033f43cSJassi Brar }
2015033f43cSJassi Brar 
2025033f43cSJassi Brar static inline struct i2s_dai *to_info(struct snd_soc_dai *dai)
2035033f43cSJassi Brar {
2045033f43cSJassi Brar 	return snd_soc_dai_get_drvdata(dai);
2055033f43cSJassi Brar }
2065033f43cSJassi Brar 
2075033f43cSJassi Brar static inline bool is_opened(struct i2s_dai *i2s)
2085033f43cSJassi Brar {
2095033f43cSJassi Brar 	if (i2s && (i2s->mode & DAI_OPENED))
2105033f43cSJassi Brar 		return true;
2115033f43cSJassi Brar 	else
2125033f43cSJassi Brar 		return false;
2135033f43cSJassi Brar }
2145033f43cSJassi Brar 
2155033f43cSJassi Brar static inline bool is_manager(struct i2s_dai *i2s)
2165033f43cSJassi Brar {
2175033f43cSJassi Brar 	if (is_opened(i2s) && (i2s->mode & DAI_MANAGER))
2185033f43cSJassi Brar 		return true;
2195033f43cSJassi Brar 	else
2205033f43cSJassi Brar 		return false;
2215033f43cSJassi Brar }
2225033f43cSJassi Brar 
2235033f43cSJassi Brar /* Read RCLK of I2S (in multiples of LRCLK) */
2245033f43cSJassi Brar static inline unsigned get_rfs(struct i2s_dai *i2s)
2255033f43cSJassi Brar {
2264ca0c0d4SPadmavathi Venna 	u32 rfs;
227a5a56871SPadmavathi Venna 	rfs = readl(i2s->addr + I2SMOD) >> i2s->variant_regs->rfs_off;
228a5a56871SPadmavathi Venna 	rfs &= i2s->variant_regs->rfs_mask;
2295033f43cSJassi Brar 
2305033f43cSJassi Brar 	switch (rfs) {
231a5a56871SPadmavathi Venna 	case 7: return 192;
232a5a56871SPadmavathi Venna 	case 6: return 96;
233a5a56871SPadmavathi Venna 	case 5: return 128;
234a5a56871SPadmavathi Venna 	case 4: return 64;
2355033f43cSJassi Brar 	case 3:	return 768;
2365033f43cSJassi Brar 	case 2: return 384;
2375033f43cSJassi Brar 	case 1:	return 512;
2385033f43cSJassi Brar 	default: return 256;
2395033f43cSJassi Brar 	}
2405033f43cSJassi Brar }
2415033f43cSJassi Brar 
2425033f43cSJassi Brar /* Write RCLK of I2S (in multiples of LRCLK) */
2435033f43cSJassi Brar static inline void set_rfs(struct i2s_dai *i2s, unsigned rfs)
2445033f43cSJassi Brar {
2455033f43cSJassi Brar 	u32 mod = readl(i2s->addr + I2SMOD);
246a5a56871SPadmavathi Venna 	int rfs_shift = i2s->variant_regs->rfs_off;
2475033f43cSJassi Brar 
248a5a56871SPadmavathi Venna 	mod &= ~(i2s->variant_regs->rfs_mask << rfs_shift);
2495033f43cSJassi Brar 
2505033f43cSJassi Brar 	switch (rfs) {
251a5a56871SPadmavathi Venna 	case 192:
252a5a56871SPadmavathi Venna 		mod |= (EXYNOS7_MOD_RCLK_192FS << rfs_shift);
253a5a56871SPadmavathi Venna 		break;
254a5a56871SPadmavathi Venna 	case 96:
255a5a56871SPadmavathi Venna 		mod |= (EXYNOS7_MOD_RCLK_96FS << rfs_shift);
256a5a56871SPadmavathi Venna 		break;
257a5a56871SPadmavathi Venna 	case 128:
258a5a56871SPadmavathi Venna 		mod |= (EXYNOS7_MOD_RCLK_128FS << rfs_shift);
259a5a56871SPadmavathi Venna 		break;
260a5a56871SPadmavathi Venna 	case 64:
261a5a56871SPadmavathi Venna 		mod |= (EXYNOS7_MOD_RCLK_64FS << rfs_shift);
262a5a56871SPadmavathi Venna 		break;
2635033f43cSJassi Brar 	case 768:
264b60be4aaSPadmavathi Venna 		mod |= (MOD_RCLK_768FS << rfs_shift);
2655033f43cSJassi Brar 		break;
2665033f43cSJassi Brar 	case 512:
267b60be4aaSPadmavathi Venna 		mod |= (MOD_RCLK_512FS << rfs_shift);
2685033f43cSJassi Brar 		break;
2695033f43cSJassi Brar 	case 384:
270b60be4aaSPadmavathi Venna 		mod |= (MOD_RCLK_384FS << rfs_shift);
2715033f43cSJassi Brar 		break;
2725033f43cSJassi Brar 	default:
273b60be4aaSPadmavathi Venna 		mod |= (MOD_RCLK_256FS << rfs_shift);
2745033f43cSJassi Brar 		break;
2755033f43cSJassi Brar 	}
2765033f43cSJassi Brar 
2775033f43cSJassi Brar 	writel(mod, i2s->addr + I2SMOD);
2785033f43cSJassi Brar }
2795033f43cSJassi Brar 
2805033f43cSJassi Brar /* Read Bit-Clock of I2S (in multiples of LRCLK) */
2815033f43cSJassi Brar static inline unsigned get_bfs(struct i2s_dai *i2s)
2825033f43cSJassi Brar {
2834ca0c0d4SPadmavathi Venna 	u32 bfs;
284a5a56871SPadmavathi Venna 	bfs = readl(i2s->addr + I2SMOD) >> i2s->variant_regs->bfs_off;
285a5a56871SPadmavathi Venna 	bfs &= i2s->variant_regs->bfs_mask;
2865033f43cSJassi Brar 
2875033f43cSJassi Brar 	switch (bfs) {
2884ca0c0d4SPadmavathi Venna 	case 8: return 256;
2894ca0c0d4SPadmavathi Venna 	case 7: return 192;
2904ca0c0d4SPadmavathi Venna 	case 6: return 128;
2914ca0c0d4SPadmavathi Venna 	case 5: return 96;
2924ca0c0d4SPadmavathi Venna 	case 4: return 64;
2935033f43cSJassi Brar 	case 3: return 24;
2945033f43cSJassi Brar 	case 2: return 16;
2955033f43cSJassi Brar 	case 1:	return 48;
2965033f43cSJassi Brar 	default: return 32;
2975033f43cSJassi Brar 	}
2985033f43cSJassi Brar }
2995033f43cSJassi Brar 
3005033f43cSJassi Brar /* Write Bit-Clock of I2S (in multiples of LRCLK) */
3015033f43cSJassi Brar static inline void set_bfs(struct i2s_dai *i2s, unsigned bfs)
3025033f43cSJassi Brar {
3035033f43cSJassi Brar 	u32 mod = readl(i2s->addr + I2SMOD);
3044ca0c0d4SPadmavathi Venna 	int tdm = i2s->quirks & QUIRK_SUPPORTS_TDM;
305a5a56871SPadmavathi Venna 	int bfs_shift = i2s->variant_regs->bfs_off;
3064ca0c0d4SPadmavathi Venna 
3074ca0c0d4SPadmavathi Venna 	/* Non-TDM I2S controllers do not support BCLK > 48 * FS */
3084ca0c0d4SPadmavathi Venna 	if (!tdm && bfs > 48) {
3094ca0c0d4SPadmavathi Venna 		dev_err(&i2s->pdev->dev, "Unsupported BCLK divider\n");
3104ca0c0d4SPadmavathi Venna 		return;
3114ca0c0d4SPadmavathi Venna 	}
3125033f43cSJassi Brar 
313a5a56871SPadmavathi Venna 	mod &= ~(i2s->variant_regs->bfs_mask << bfs_shift);
314a5a56871SPadmavathi Venna 
3155033f43cSJassi Brar 	switch (bfs) {
3165033f43cSJassi Brar 	case 48:
317b60be4aaSPadmavathi Venna 		mod |= (MOD_BCLK_48FS << bfs_shift);
3185033f43cSJassi Brar 		break;
3195033f43cSJassi Brar 	case 32:
320b60be4aaSPadmavathi Venna 		mod |= (MOD_BCLK_32FS << bfs_shift);
3215033f43cSJassi Brar 		break;
3225033f43cSJassi Brar 	case 24:
323b60be4aaSPadmavathi Venna 		mod |= (MOD_BCLK_24FS << bfs_shift);
3245033f43cSJassi Brar 		break;
3255033f43cSJassi Brar 	case 16:
326b60be4aaSPadmavathi Venna 		mod |= (MOD_BCLK_16FS << bfs_shift);
3275033f43cSJassi Brar 		break;
3284ca0c0d4SPadmavathi Venna 	case 64:
3294ca0c0d4SPadmavathi Venna 		mod |= (EXYNOS5420_MOD_BCLK_64FS << bfs_shift);
3304ca0c0d4SPadmavathi Venna 		break;
3314ca0c0d4SPadmavathi Venna 	case 96:
3324ca0c0d4SPadmavathi Venna 		mod |= (EXYNOS5420_MOD_BCLK_96FS << bfs_shift);
3334ca0c0d4SPadmavathi Venna 		break;
3344ca0c0d4SPadmavathi Venna 	case 128:
3354ca0c0d4SPadmavathi Venna 		mod |= (EXYNOS5420_MOD_BCLK_128FS << bfs_shift);
3364ca0c0d4SPadmavathi Venna 		break;
3374ca0c0d4SPadmavathi Venna 	case 192:
3384ca0c0d4SPadmavathi Venna 		mod |= (EXYNOS5420_MOD_BCLK_192FS << bfs_shift);
3394ca0c0d4SPadmavathi Venna 		break;
3404ca0c0d4SPadmavathi Venna 	case 256:
3414ca0c0d4SPadmavathi Venna 		mod |= (EXYNOS5420_MOD_BCLK_256FS << bfs_shift);
3425033f43cSJassi Brar 		break;
3435033f43cSJassi Brar 	default:
3445033f43cSJassi Brar 		dev_err(&i2s->pdev->dev, "Wrong BCLK Divider!\n");
3455033f43cSJassi Brar 		return;
3465033f43cSJassi Brar 	}
3475033f43cSJassi Brar 
3485033f43cSJassi Brar 	writel(mod, i2s->addr + I2SMOD);
3495033f43cSJassi Brar }
3505033f43cSJassi Brar 
3515033f43cSJassi Brar /* Sample-Size */
3525033f43cSJassi Brar static inline int get_blc(struct i2s_dai *i2s)
3535033f43cSJassi Brar {
3545033f43cSJassi Brar 	int blc = readl(i2s->addr + I2SMOD);
3555033f43cSJassi Brar 
3565033f43cSJassi Brar 	blc = (blc >> 13) & 0x3;
3575033f43cSJassi Brar 
3585033f43cSJassi Brar 	switch (blc) {
3595033f43cSJassi Brar 	case 2: return 24;
3605033f43cSJassi Brar 	case 1:	return 8;
3615033f43cSJassi Brar 	default: return 16;
3625033f43cSJassi Brar 	}
3635033f43cSJassi Brar }
3645033f43cSJassi Brar 
3655033f43cSJassi Brar /* TX Channel Control */
3665033f43cSJassi Brar static void i2s_txctrl(struct i2s_dai *i2s, int on)
3675033f43cSJassi Brar {
3685033f43cSJassi Brar 	void __iomem *addr = i2s->addr;
369a5a56871SPadmavathi Venna 	int txr_off = i2s->variant_regs->txr_off;
3705033f43cSJassi Brar 	u32 con = readl(addr + I2SCON);
371a5a56871SPadmavathi Venna 	u32 mod = readl(addr + I2SMOD) & ~(3 << txr_off);
3725033f43cSJassi Brar 
3735033f43cSJassi Brar 	if (on) {
3745033f43cSJassi Brar 		con |= CON_ACTIVE;
3755033f43cSJassi Brar 		con &= ~CON_TXCH_PAUSE;
3765033f43cSJassi Brar 
3775033f43cSJassi Brar 		if (is_secondary(i2s)) {
3785033f43cSJassi Brar 			con |= CON_TXSDMA_ACTIVE;
3795033f43cSJassi Brar 			con &= ~CON_TXSDMA_PAUSE;
3805033f43cSJassi Brar 		} else {
3815033f43cSJassi Brar 			con |= CON_TXDMA_ACTIVE;
3825033f43cSJassi Brar 			con &= ~CON_TXDMA_PAUSE;
3835033f43cSJassi Brar 		}
3845033f43cSJassi Brar 
3855033f43cSJassi Brar 		if (any_rx_active(i2s))
386a5a56871SPadmavathi Venna 			mod |= 2 << txr_off;
3875033f43cSJassi Brar 		else
388a5a56871SPadmavathi Venna 			mod |= 0 << txr_off;
3895033f43cSJassi Brar 	} else {
3905033f43cSJassi Brar 		if (is_secondary(i2s)) {
3915033f43cSJassi Brar 			con |=  CON_TXSDMA_PAUSE;
3925033f43cSJassi Brar 			con &= ~CON_TXSDMA_ACTIVE;
3935033f43cSJassi Brar 		} else {
3945033f43cSJassi Brar 			con |=  CON_TXDMA_PAUSE;
3955033f43cSJassi Brar 			con &= ~CON_TXDMA_ACTIVE;
3965033f43cSJassi Brar 		}
3975033f43cSJassi Brar 
3985033f43cSJassi Brar 		if (other_tx_active(i2s)) {
3995033f43cSJassi Brar 			writel(con, addr + I2SCON);
4005033f43cSJassi Brar 			return;
4015033f43cSJassi Brar 		}
4025033f43cSJassi Brar 
4035033f43cSJassi Brar 		con |=  CON_TXCH_PAUSE;
4045033f43cSJassi Brar 
4055033f43cSJassi Brar 		if (any_rx_active(i2s))
406a5a56871SPadmavathi Venna 			mod |= 1 << txr_off;
4075033f43cSJassi Brar 		else
4085033f43cSJassi Brar 			con &= ~CON_ACTIVE;
4095033f43cSJassi Brar 	}
4105033f43cSJassi Brar 
4115033f43cSJassi Brar 	writel(mod, addr + I2SMOD);
4125033f43cSJassi Brar 	writel(con, addr + I2SCON);
4135033f43cSJassi Brar }
4145033f43cSJassi Brar 
4155033f43cSJassi Brar /* RX Channel Control */
4165033f43cSJassi Brar static void i2s_rxctrl(struct i2s_dai *i2s, int on)
4175033f43cSJassi Brar {
4185033f43cSJassi Brar 	void __iomem *addr = i2s->addr;
419a5a56871SPadmavathi Venna 	int txr_off = i2s->variant_regs->txr_off;
4205033f43cSJassi Brar 	u32 con = readl(addr + I2SCON);
421a5a56871SPadmavathi Venna 	u32 mod = readl(addr + I2SMOD) & ~(3 << txr_off);
4225033f43cSJassi Brar 
4235033f43cSJassi Brar 	if (on) {
4245033f43cSJassi Brar 		con |= CON_RXDMA_ACTIVE | CON_ACTIVE;
4255033f43cSJassi Brar 		con &= ~(CON_RXDMA_PAUSE | CON_RXCH_PAUSE);
4265033f43cSJassi Brar 
4275033f43cSJassi Brar 		if (any_tx_active(i2s))
428a5a56871SPadmavathi Venna 			mod |= 2 << txr_off;
4295033f43cSJassi Brar 		else
430a5a56871SPadmavathi Venna 			mod |= 1 << txr_off;
4315033f43cSJassi Brar 	} else {
4325033f43cSJassi Brar 		con |=  CON_RXDMA_PAUSE | CON_RXCH_PAUSE;
4335033f43cSJassi Brar 		con &= ~CON_RXDMA_ACTIVE;
4345033f43cSJassi Brar 
4355033f43cSJassi Brar 		if (any_tx_active(i2s))
436a5a56871SPadmavathi Venna 			mod |= 0 << txr_off;
4375033f43cSJassi Brar 		else
4385033f43cSJassi Brar 			con &= ~CON_ACTIVE;
4395033f43cSJassi Brar 	}
4405033f43cSJassi Brar 
4415033f43cSJassi Brar 	writel(mod, addr + I2SMOD);
4425033f43cSJassi Brar 	writel(con, addr + I2SCON);
4435033f43cSJassi Brar }
4445033f43cSJassi Brar 
4455033f43cSJassi Brar /* Flush FIFO of an interface */
4465033f43cSJassi Brar static inline void i2s_fifo(struct i2s_dai *i2s, u32 flush)
4475033f43cSJassi Brar {
4485033f43cSJassi Brar 	void __iomem *fic;
4495033f43cSJassi Brar 	u32 val;
4505033f43cSJassi Brar 
4515033f43cSJassi Brar 	if (!i2s)
4525033f43cSJassi Brar 		return;
4535033f43cSJassi Brar 
4545033f43cSJassi Brar 	if (is_secondary(i2s))
4555033f43cSJassi Brar 		fic = i2s->addr + I2SFICS;
4565033f43cSJassi Brar 	else
4575033f43cSJassi Brar 		fic = i2s->addr + I2SFIC;
4585033f43cSJassi Brar 
4595033f43cSJassi Brar 	/* Flush the FIFO */
4605033f43cSJassi Brar 	writel(readl(fic) | flush, fic);
4615033f43cSJassi Brar 
4625033f43cSJassi Brar 	/* Be patient */
4635033f43cSJassi Brar 	val = msecs_to_loops(1) / 1000; /* 1 usec */
4645033f43cSJassi Brar 	while (--val)
4655033f43cSJassi Brar 		cpu_relax();
4665033f43cSJassi Brar 
4675033f43cSJassi Brar 	writel(readl(fic) & ~flush, fic);
4685033f43cSJassi Brar }
4695033f43cSJassi Brar 
4705033f43cSJassi Brar static int i2s_set_sysclk(struct snd_soc_dai *dai,
4715033f43cSJassi Brar 	  int clk_id, unsigned int rfs, int dir)
4725033f43cSJassi Brar {
4735033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
474dcd60fc3SSylwester Nawrocki 	struct i2s_dai *other = get_other_dai(i2s);
475a5a56871SPadmavathi Venna 	const struct samsung_i2s_variant_regs *i2s_regs = i2s->variant_regs;
476a5a56871SPadmavathi Venna 	unsigned int cdcon_mask = 1 << i2s_regs->cdclkcon_off;
477a5a56871SPadmavathi Venna 	unsigned int rsrc_mask = 1 << i2s_regs->rclksrc_off;
478ce8bcdbbSSylwester Nawrocki 	u32 mod, mask, val = 0;
479316fa9e0SCharles Keepax 	unsigned long flags;
480dc938ddbSMarek Szyprowski 	int ret = 0;
481dc938ddbSMarek Szyprowski 
482dc938ddbSMarek Szyprowski 	pm_runtime_get_sync(dai->dev);
483ce8bcdbbSSylwester Nawrocki 
484316fa9e0SCharles Keepax 	spin_lock_irqsave(i2s->lock, flags);
485ce8bcdbbSSylwester Nawrocki 	mod = readl(i2s->addr + I2SMOD);
486316fa9e0SCharles Keepax 	spin_unlock_irqrestore(i2s->lock, flags);
4875033f43cSJassi Brar 
4885033f43cSJassi Brar 	switch (clk_id) {
489c86d50f9SSylwester Nawrocki 	case SAMSUNG_I2S_OPCLK:
490ce8bcdbbSSylwester Nawrocki 		mask = MOD_OPCLK_MASK;
491ce8bcdbbSSylwester Nawrocki 		val = dir;
492c86d50f9SSylwester Nawrocki 		break;
4935033f43cSJassi Brar 	case SAMSUNG_I2S_CDCLK:
494ce8bcdbbSSylwester Nawrocki 		mask = 1 << i2s_regs->cdclkcon_off;
4955033f43cSJassi Brar 		/* Shouldn't matter in GATING(CLOCK_IN) mode */
4965033f43cSJassi Brar 		if (dir == SND_SOC_CLOCK_IN)
4975033f43cSJassi Brar 			rfs = 0;
4985033f43cSJassi Brar 
499133c2681SCharles Keepax 		if ((rfs && other && other->rfs && (other->rfs != rfs)) ||
5005033f43cSJassi Brar 				(any_active(i2s) &&
5015033f43cSJassi Brar 				(((dir == SND_SOC_CLOCK_IN)
502a5a56871SPadmavathi Venna 					&& !(mod & cdcon_mask)) ||
5035033f43cSJassi Brar 				((dir == SND_SOC_CLOCK_OUT)
504a5a56871SPadmavathi Venna 					&& (mod & cdcon_mask))))) {
5055033f43cSJassi Brar 			dev_err(&i2s->pdev->dev,
5065033f43cSJassi Brar 				"%s:%d Other DAI busy\n", __func__, __LINE__);
507dc938ddbSMarek Szyprowski 			ret = -EAGAIN;
508dc938ddbSMarek Szyprowski 			goto err;
5095033f43cSJassi Brar 		}
5105033f43cSJassi Brar 
5115033f43cSJassi Brar 		if (dir == SND_SOC_CLOCK_IN)
512ce8bcdbbSSylwester Nawrocki 			val = 1 << i2s_regs->cdclkcon_off;
5135033f43cSJassi Brar 
5145033f43cSJassi Brar 		i2s->rfs = rfs;
5155033f43cSJassi Brar 		break;
5165033f43cSJassi Brar 
5175033f43cSJassi Brar 	case SAMSUNG_I2S_RCLKSRC_0: /* clock corrsponding to IISMOD[10] := 0 */
5185033f43cSJassi Brar 	case SAMSUNG_I2S_RCLKSRC_1: /* clock corrsponding to IISMOD[10] := 1 */
519ce8bcdbbSSylwester Nawrocki 		mask = 1 << i2s_regs->rclksrc_off;
520ce8bcdbbSSylwester Nawrocki 
5215033f43cSJassi Brar 		if ((i2s->quirks & QUIRK_NO_MUXPSR)
5225033f43cSJassi Brar 				|| (clk_id == SAMSUNG_I2S_RCLKSRC_0))
5235033f43cSJassi Brar 			clk_id = 0;
5245033f43cSJassi Brar 		else
5255033f43cSJassi Brar 			clk_id = 1;
5265033f43cSJassi Brar 
5275033f43cSJassi Brar 		if (!any_active(i2s)) {
528a6aba536SSylwester Nawrocki 			if (i2s->op_clk && !IS_ERR(i2s->op_clk)) {
529a5a56871SPadmavathi Venna 				if ((clk_id && !(mod & rsrc_mask)) ||
530a5a56871SPadmavathi Venna 					(!clk_id && (mod & rsrc_mask))) {
53198614cf6SThomas Abraham 					clk_disable_unprepare(i2s->op_clk);
5325033f43cSJassi Brar 					clk_put(i2s->op_clk);
5335033f43cSJassi Brar 				} else {
5346ce534aaSJassi Brar 					i2s->rclk_srcrate =
5356ce534aaSJassi Brar 						clk_get_rate(i2s->op_clk);
536dc938ddbSMarek Szyprowski 					goto done;
5375033f43cSJassi Brar 				}
5385033f43cSJassi Brar 			}
5395033f43cSJassi Brar 
5401974a042SPadmavathi Venna 			if (clk_id)
5415033f43cSJassi Brar 				i2s->op_clk = clk_get(&i2s->pdev->dev,
5421974a042SPadmavathi Venna 						"i2s_opclk1");
5431974a042SPadmavathi Venna 			else
5441974a042SPadmavathi Venna 				i2s->op_clk = clk_get(&i2s->pdev->dev,
5451974a042SPadmavathi Venna 						"i2s_opclk0");
546a6aba536SSylwester Nawrocki 
547dc938ddbSMarek Szyprowski 			if (WARN_ON(IS_ERR(i2s->op_clk))) {
548dc938ddbSMarek Szyprowski 				ret = PTR_ERR(i2s->op_clk);
549dc938ddbSMarek Szyprowski 				goto err;
550dc938ddbSMarek Szyprowski 			}
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__);
564dc938ddbSMarek Szyprowski 			ret = -EAGAIN;
565dc938ddbSMarek Szyprowski 			goto err;
5665033f43cSJassi Brar 		} else {
5675033f43cSJassi Brar 			/* Call can't be on the active DAI */
5685033f43cSJassi Brar 			i2s->op_clk = other->op_clk;
5695033f43cSJassi Brar 			i2s->rclk_srcrate = other->rclk_srcrate;
570dc938ddbSMarek Szyprowski 			goto done;
5715033f43cSJassi Brar 		}
5725033f43cSJassi Brar 
573ce8bcdbbSSylwester Nawrocki 		if (clk_id == 1)
574ce8bcdbbSSylwester Nawrocki 			val = 1 << i2s_regs->rclksrc_off;
575b2de1d20SPadmavathi Venna 		break;
5765033f43cSJassi Brar 	default:
5775033f43cSJassi Brar 		dev_err(&i2s->pdev->dev, "We don't serve that!\n");
578dc938ddbSMarek Szyprowski 		ret = -EINVAL;
579dc938ddbSMarek Szyprowski 		goto err;
5805033f43cSJassi Brar 	}
5815033f43cSJassi Brar 
582316fa9e0SCharles Keepax 	spin_lock_irqsave(i2s->lock, flags);
583ce8bcdbbSSylwester Nawrocki 	mod = readl(i2s->addr + I2SMOD);
584ce8bcdbbSSylwester Nawrocki 	mod = (mod & ~mask) | val;
5855033f43cSJassi Brar 	writel(mod, i2s->addr + I2SMOD);
586316fa9e0SCharles Keepax 	spin_unlock_irqrestore(i2s->lock, flags);
587dc938ddbSMarek Szyprowski done:
588dc938ddbSMarek Szyprowski 	pm_runtime_put(dai->dev);
5895033f43cSJassi Brar 
5905033f43cSJassi Brar 	return 0;
591dc938ddbSMarek Szyprowski err:
592dc938ddbSMarek Szyprowski 	pm_runtime_put(dai->dev);
593dc938ddbSMarek Szyprowski 	return ret;
5945033f43cSJassi Brar }
5955033f43cSJassi Brar 
5965033f43cSJassi Brar static int i2s_set_fmt(struct snd_soc_dai *dai,
5975033f43cSJassi Brar 	unsigned int fmt)
5985033f43cSJassi Brar {
5995033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
600a5a56871SPadmavathi Venna 	int lrp_shift, sdf_shift, sdf_mask, lrp_rlow, mod_slave;
601ce8bcdbbSSylwester Nawrocki 	u32 mod, tmp = 0;
602316fa9e0SCharles Keepax 	unsigned long flags;
6035033f43cSJassi Brar 
604a5a56871SPadmavathi Venna 	lrp_shift = i2s->variant_regs->lrp_off;
605a5a56871SPadmavathi Venna 	sdf_shift = i2s->variant_regs->sdf_off;
606a5a56871SPadmavathi Venna 	mod_slave = 1 << i2s->variant_regs->mss_off;
6074ca0c0d4SPadmavathi Venna 
608b60be4aaSPadmavathi Venna 	sdf_mask = MOD_SDF_MASK << sdf_shift;
609b60be4aaSPadmavathi Venna 	lrp_rlow = MOD_LR_RLOW << lrp_shift;
610b60be4aaSPadmavathi Venna 
6115033f43cSJassi Brar 	/* Format is priority */
6125033f43cSJassi Brar 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
6135033f43cSJassi Brar 	case SND_SOC_DAIFMT_RIGHT_J:
614b60be4aaSPadmavathi Venna 		tmp |= lrp_rlow;
615b60be4aaSPadmavathi Venna 		tmp |= (MOD_SDF_MSB << sdf_shift);
6165033f43cSJassi Brar 		break;
6175033f43cSJassi Brar 	case SND_SOC_DAIFMT_LEFT_J:
618b60be4aaSPadmavathi Venna 		tmp |= lrp_rlow;
619b60be4aaSPadmavathi Venna 		tmp |= (MOD_SDF_LSB << sdf_shift);
6205033f43cSJassi Brar 		break;
6215033f43cSJassi Brar 	case SND_SOC_DAIFMT_I2S:
622b60be4aaSPadmavathi Venna 		tmp |= (MOD_SDF_IIS << sdf_shift);
6235033f43cSJassi Brar 		break;
6245033f43cSJassi Brar 	default:
6255033f43cSJassi Brar 		dev_err(&i2s->pdev->dev, "Format not supported\n");
6265033f43cSJassi Brar 		return -EINVAL;
6275033f43cSJassi Brar 	}
6285033f43cSJassi Brar 
6295033f43cSJassi Brar 	/*
6305033f43cSJassi Brar 	 * INV flag is relative to the FORMAT flag - if set it simply
6315033f43cSJassi Brar 	 * flips the polarity specified by the Standard
6325033f43cSJassi Brar 	 */
6335033f43cSJassi Brar 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
6345033f43cSJassi Brar 	case SND_SOC_DAIFMT_NB_NF:
6355033f43cSJassi Brar 		break;
6365033f43cSJassi Brar 	case SND_SOC_DAIFMT_NB_IF:
637b60be4aaSPadmavathi Venna 		if (tmp & lrp_rlow)
638b60be4aaSPadmavathi Venna 			tmp &= ~lrp_rlow;
6395033f43cSJassi Brar 		else
640b60be4aaSPadmavathi Venna 			tmp |= lrp_rlow;
6415033f43cSJassi Brar 		break;
6425033f43cSJassi Brar 	default:
6435033f43cSJassi Brar 		dev_err(&i2s->pdev->dev, "Polarity not supported\n");
6445033f43cSJassi Brar 		return -EINVAL;
6455033f43cSJassi Brar 	}
6465033f43cSJassi Brar 
6475033f43cSJassi Brar 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
6485033f43cSJassi Brar 	case SND_SOC_DAIFMT_CBM_CFM:
649a5a56871SPadmavathi Venna 		tmp |= mod_slave;
6505033f43cSJassi Brar 		break;
6515033f43cSJassi Brar 	case SND_SOC_DAIFMT_CBS_CFS:
6525033f43cSJassi Brar 		/* Set default source clock in Master mode */
6535033f43cSJassi Brar 		if (i2s->rclk_srcrate == 0)
6545033f43cSJassi Brar 			i2s_set_sysclk(dai, SAMSUNG_I2S_RCLKSRC_0,
6555033f43cSJassi Brar 							0, SND_SOC_CLOCK_IN);
6565033f43cSJassi Brar 		break;
6575033f43cSJassi Brar 	default:
6585033f43cSJassi Brar 		dev_err(&i2s->pdev->dev, "master/slave format not supported\n");
6595033f43cSJassi Brar 		return -EINVAL;
6605033f43cSJassi Brar 	}
6615033f43cSJassi Brar 
662dc938ddbSMarek Szyprowski 	pm_runtime_get_sync(dai->dev);
663316fa9e0SCharles Keepax 	spin_lock_irqsave(i2s->lock, flags);
664ce8bcdbbSSylwester Nawrocki 	mod = readl(i2s->addr + I2SMOD);
665b60be4aaSPadmavathi Venna 	/*
666b60be4aaSPadmavathi Venna 	 * Don't change the I2S mode if any controller is active on this
667b60be4aaSPadmavathi Venna 	 * channel.
668b60be4aaSPadmavathi Venna 	 */
6695033f43cSJassi Brar 	if (any_active(i2s) &&
670a5a56871SPadmavathi Venna 		((mod & (sdf_mask | lrp_rlow | mod_slave)) != tmp)) {
671316fa9e0SCharles Keepax 		spin_unlock_irqrestore(i2s->lock, flags);
672dc938ddbSMarek Szyprowski 		pm_runtime_put(dai->dev);
6735033f43cSJassi Brar 		dev_err(&i2s->pdev->dev,
6745033f43cSJassi Brar 				"%s:%d Other DAI busy\n", __func__, __LINE__);
6755033f43cSJassi Brar 		return -EAGAIN;
6765033f43cSJassi Brar 	}
6775033f43cSJassi Brar 
678a5a56871SPadmavathi Venna 	mod &= ~(sdf_mask | lrp_rlow | mod_slave);
6795033f43cSJassi Brar 	mod |= tmp;
6805033f43cSJassi Brar 	writel(mod, i2s->addr + I2SMOD);
681316fa9e0SCharles Keepax 	spin_unlock_irqrestore(i2s->lock, flags);
682dc938ddbSMarek Szyprowski 	pm_runtime_put(dai->dev);
6835033f43cSJassi Brar 
6845033f43cSJassi Brar 	return 0;
6855033f43cSJassi Brar }
6865033f43cSJassi Brar 
6875033f43cSJassi Brar static int i2s_hw_params(struct snd_pcm_substream *substream,
6885033f43cSJassi Brar 	struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
6895033f43cSJassi Brar {
6905033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
691ce8bcdbbSSylwester Nawrocki 	u32 mod, mask = 0, val = 0;
692316fa9e0SCharles Keepax 	unsigned long flags;
6935033f43cSJassi Brar 
694dc938ddbSMarek Szyprowski 	WARN_ON(!pm_runtime_active(dai->dev));
695dc938ddbSMarek Szyprowski 
6965033f43cSJassi Brar 	if (!is_secondary(i2s))
697ce8bcdbbSSylwester Nawrocki 		mask |= (MOD_DC2_EN | MOD_DC1_EN);
6985033f43cSJassi Brar 
6995033f43cSJassi Brar 	switch (params_channels(params)) {
7005033f43cSJassi Brar 	case 6:
701ce8bcdbbSSylwester Nawrocki 		val |= MOD_DC2_EN;
7025033f43cSJassi Brar 	case 4:
703ce8bcdbbSSylwester Nawrocki 		val |= MOD_DC1_EN;
7045033f43cSJassi Brar 		break;
7055033f43cSJassi Brar 	case 2:
706588fb705SSangsu Park 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
70769e7a69aSSylwester Nawrocki 			i2s->dma_playback.addr_width = 4;
708588fb705SSangsu Park 		else
70969e7a69aSSylwester Nawrocki 			i2s->dma_capture.addr_width = 4;
710588fb705SSangsu Park 		break;
711588fb705SSangsu Park 	case 1:
712588fb705SSangsu Park 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
71369e7a69aSSylwester Nawrocki 			i2s->dma_playback.addr_width = 2;
714588fb705SSangsu Park 		else
71569e7a69aSSylwester Nawrocki 			i2s->dma_capture.addr_width = 2;
716588fb705SSangsu Park 
7175033f43cSJassi Brar 		break;
7185033f43cSJassi Brar 	default:
7195033f43cSJassi Brar 		dev_err(&i2s->pdev->dev, "%d channels not supported\n",
7205033f43cSJassi Brar 				params_channels(params));
7215033f43cSJassi Brar 		return -EINVAL;
7225033f43cSJassi Brar 	}
7235033f43cSJassi Brar 
7245033f43cSJassi Brar 	if (is_secondary(i2s))
725ce8bcdbbSSylwester Nawrocki 		mask |= MOD_BLCS_MASK;
7265033f43cSJassi Brar 	else
727ce8bcdbbSSylwester Nawrocki 		mask |= MOD_BLCP_MASK;
7285033f43cSJassi Brar 
7295033f43cSJassi Brar 	if (is_manager(i2s))
730ce8bcdbbSSylwester Nawrocki 		mask |= MOD_BLC_MASK;
7315033f43cSJassi Brar 
73288ce1465STushar Behera 	switch (params_width(params)) {
73388ce1465STushar Behera 	case 8:
7345033f43cSJassi Brar 		if (is_secondary(i2s))
735ce8bcdbbSSylwester Nawrocki 			val |= MOD_BLCS_8BIT;
7365033f43cSJassi Brar 		else
737ce8bcdbbSSylwester Nawrocki 			val |= MOD_BLCP_8BIT;
7385033f43cSJassi Brar 		if (is_manager(i2s))
739ce8bcdbbSSylwester Nawrocki 			val |= MOD_BLC_8BIT;
7405033f43cSJassi Brar 		break;
74188ce1465STushar Behera 	case 16:
7425033f43cSJassi Brar 		if (is_secondary(i2s))
743ce8bcdbbSSylwester Nawrocki 			val |= MOD_BLCS_16BIT;
7445033f43cSJassi Brar 		else
745ce8bcdbbSSylwester Nawrocki 			val |= MOD_BLCP_16BIT;
7465033f43cSJassi Brar 		if (is_manager(i2s))
747ce8bcdbbSSylwester Nawrocki 			val |= MOD_BLC_16BIT;
7485033f43cSJassi Brar 		break;
74988ce1465STushar Behera 	case 24:
7505033f43cSJassi Brar 		if (is_secondary(i2s))
751ce8bcdbbSSylwester Nawrocki 			val |= MOD_BLCS_24BIT;
7525033f43cSJassi Brar 		else
753ce8bcdbbSSylwester Nawrocki 			val |= MOD_BLCP_24BIT;
7545033f43cSJassi Brar 		if (is_manager(i2s))
755ce8bcdbbSSylwester Nawrocki 			val |= MOD_BLC_24BIT;
7565033f43cSJassi Brar 		break;
7575033f43cSJassi Brar 	default:
7585033f43cSJassi Brar 		dev_err(&i2s->pdev->dev, "Format(%d) not supported\n",
7595033f43cSJassi Brar 				params_format(params));
7605033f43cSJassi Brar 		return -EINVAL;
7615033f43cSJassi Brar 	}
762ce8bcdbbSSylwester Nawrocki 
763316fa9e0SCharles Keepax 	spin_lock_irqsave(i2s->lock, flags);
764ce8bcdbbSSylwester Nawrocki 	mod = readl(i2s->addr + I2SMOD);
765ce8bcdbbSSylwester Nawrocki 	mod = (mod & ~mask) | val;
7665033f43cSJassi Brar 	writel(mod, i2s->addr + I2SMOD);
767316fa9e0SCharles Keepax 	spin_unlock_irqrestore(i2s->lock, flags);
7685033f43cSJassi Brar 
76969e7a69aSSylwester Nawrocki 	snd_soc_dai_init_dma_data(dai, &i2s->dma_playback, &i2s->dma_capture);
770d37bdf73SMark Brown 
7715033f43cSJassi Brar 	i2s->frmclk = params_rate(params);
7725033f43cSJassi Brar 
7735033f43cSJassi Brar 	return 0;
7745033f43cSJassi Brar }
7755033f43cSJassi Brar 
7765033f43cSJassi Brar /* We set constraints on the substream acc to the version of I2S */
7775033f43cSJassi Brar static int i2s_startup(struct snd_pcm_substream *substream,
7785033f43cSJassi Brar 	  struct snd_soc_dai *dai)
7795033f43cSJassi Brar {
7805033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
781dcd60fc3SSylwester Nawrocki 	struct i2s_dai *other = get_other_dai(i2s);
7825033f43cSJassi Brar 	unsigned long flags;
7835033f43cSJassi Brar 
784dc938ddbSMarek Szyprowski 	pm_runtime_get_sync(dai->dev);
785dc938ddbSMarek Szyprowski 
7865033f43cSJassi Brar 	spin_lock_irqsave(&lock, flags);
7875033f43cSJassi Brar 
7885033f43cSJassi Brar 	i2s->mode |= DAI_OPENED;
7895033f43cSJassi Brar 
7905033f43cSJassi Brar 	if (is_manager(other))
7915033f43cSJassi Brar 		i2s->mode &= ~DAI_MANAGER;
7925033f43cSJassi Brar 	else
7935033f43cSJassi Brar 		i2s->mode |= DAI_MANAGER;
7945033f43cSJassi Brar 
7952d77828dSPadmavathi Venna 	if (!any_active(i2s) && (i2s->quirks & QUIRK_NEED_RSTCLR))
7962d77828dSPadmavathi Venna 		writel(CON_RSTCLR, i2s->addr + I2SCON);
7972d77828dSPadmavathi Venna 
7985033f43cSJassi Brar 	spin_unlock_irqrestore(&lock, flags);
7995033f43cSJassi Brar 
8005033f43cSJassi Brar 	return 0;
8015033f43cSJassi Brar }
8025033f43cSJassi Brar 
8035033f43cSJassi Brar static void i2s_shutdown(struct snd_pcm_substream *substream,
8045033f43cSJassi Brar 	struct snd_soc_dai *dai)
8055033f43cSJassi Brar {
8065033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
807dcd60fc3SSylwester Nawrocki 	struct i2s_dai *other = get_other_dai(i2s);
8085033f43cSJassi Brar 	unsigned long flags;
8095033f43cSJassi Brar 
8105033f43cSJassi Brar 	spin_lock_irqsave(&lock, flags);
8115033f43cSJassi Brar 
8125033f43cSJassi Brar 	i2s->mode &= ~DAI_OPENED;
8135033f43cSJassi Brar 	i2s->mode &= ~DAI_MANAGER;
8145033f43cSJassi Brar 
815074b89bbSSylwester Nawrocki 	if (is_opened(other))
8165033f43cSJassi Brar 		other->mode |= DAI_MANAGER;
817074b89bbSSylwester Nawrocki 
8185033f43cSJassi Brar 	/* Reset any constraint on RFS and BFS */
8195033f43cSJassi Brar 	i2s->rfs = 0;
8205033f43cSJassi Brar 	i2s->bfs = 0;
8215033f43cSJassi Brar 
8225033f43cSJassi Brar 	spin_unlock_irqrestore(&lock, flags);
823dc938ddbSMarek Szyprowski 
824dc938ddbSMarek Szyprowski 	pm_runtime_put(dai->dev);
8255033f43cSJassi Brar }
8265033f43cSJassi Brar 
8275033f43cSJassi Brar static int config_setup(struct i2s_dai *i2s)
8285033f43cSJassi Brar {
829dcd60fc3SSylwester Nawrocki 	struct i2s_dai *other = get_other_dai(i2s);
8305033f43cSJassi Brar 	unsigned rfs, bfs, blc;
8315033f43cSJassi Brar 	u32 psr;
8325033f43cSJassi Brar 
8335033f43cSJassi Brar 	blc = get_blc(i2s);
8345033f43cSJassi Brar 
8355033f43cSJassi Brar 	bfs = i2s->bfs;
8365033f43cSJassi Brar 
8375033f43cSJassi Brar 	if (!bfs && other)
8385033f43cSJassi Brar 		bfs = other->bfs;
8395033f43cSJassi Brar 
8405033f43cSJassi Brar 	/* Select least possible multiple(2) if no constraint set */
8415033f43cSJassi Brar 	if (!bfs)
8425033f43cSJassi Brar 		bfs = blc * 2;
8435033f43cSJassi Brar 
8445033f43cSJassi Brar 	rfs = i2s->rfs;
8455033f43cSJassi Brar 
8465033f43cSJassi Brar 	if (!rfs && other)
8475033f43cSJassi Brar 		rfs = other->rfs;
8485033f43cSJassi Brar 
8495033f43cSJassi Brar 	if ((rfs == 256 || rfs == 512) && (blc == 24)) {
8505033f43cSJassi Brar 		dev_err(&i2s->pdev->dev,
8515033f43cSJassi Brar 			"%d-RFS not supported for 24-blc\n", rfs);
8525033f43cSJassi Brar 		return -EINVAL;
8535033f43cSJassi Brar 	}
8545033f43cSJassi Brar 
8555033f43cSJassi Brar 	if (!rfs) {
8565033f43cSJassi Brar 		if (bfs == 16 || bfs == 32)
8575033f43cSJassi Brar 			rfs = 256;
8585033f43cSJassi Brar 		else
8595033f43cSJassi Brar 			rfs = 384;
8605033f43cSJassi Brar 	}
8615033f43cSJassi Brar 
8625033f43cSJassi Brar 	/* If already setup and running */
8635033f43cSJassi Brar 	if (any_active(i2s) && (get_rfs(i2s) != rfs || get_bfs(i2s) != bfs)) {
8645033f43cSJassi Brar 		dev_err(&i2s->pdev->dev,
8655033f43cSJassi Brar 				"%s:%d Other DAI busy\n", __func__, __LINE__);
8665033f43cSJassi Brar 		return -EAGAIN;
8675033f43cSJassi Brar 	}
8685033f43cSJassi Brar 
8695033f43cSJassi Brar 	set_bfs(i2s, bfs);
8705033f43cSJassi Brar 	set_rfs(i2s, rfs);
8715033f43cSJassi Brar 
87277010010SPadmavathi Venna 	/* Don't bother with PSR in Slave mode */
87377010010SPadmavathi Venna 	if (is_slave(i2s))
87477010010SPadmavathi Venna 		return 0;
87577010010SPadmavathi Venna 
8765033f43cSJassi Brar 	if (!(i2s->quirks & QUIRK_NO_MUXPSR)) {
8775033f43cSJassi Brar 		psr = i2s->rclk_srcrate / i2s->frmclk / rfs;
8785033f43cSJassi Brar 		writel(((psr - 1) << 8) | PSR_PSREN, i2s->addr + I2SPSR);
8795033f43cSJassi Brar 		dev_dbg(&i2s->pdev->dev,
8805033f43cSJassi Brar 			"RCLK_SRC=%luHz PSR=%u, RCLK=%dfs, BCLK=%dfs\n",
8815033f43cSJassi Brar 				i2s->rclk_srcrate, psr, rfs, bfs);
8825033f43cSJassi Brar 	}
8835033f43cSJassi Brar 
8845033f43cSJassi Brar 	return 0;
8855033f43cSJassi Brar }
8865033f43cSJassi Brar 
8875033f43cSJassi Brar static int i2s_trigger(struct snd_pcm_substream *substream,
8885033f43cSJassi Brar 	int cmd, struct snd_soc_dai *dai)
8895033f43cSJassi Brar {
8905033f43cSJassi Brar 	int capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
8915033f43cSJassi Brar 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
8925033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(rtd->cpu_dai);
8935033f43cSJassi Brar 	unsigned long flags;
8945033f43cSJassi Brar 
8955033f43cSJassi Brar 	switch (cmd) {
8965033f43cSJassi Brar 	case SNDRV_PCM_TRIGGER_START:
8975033f43cSJassi Brar 	case SNDRV_PCM_TRIGGER_RESUME:
8985033f43cSJassi Brar 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
899dc938ddbSMarek Szyprowski 		pm_runtime_get_sync(dai->dev);
900f3670536SSylwester Nawrocki 		spin_lock_irqsave(i2s->lock, flags);
9015033f43cSJassi Brar 
9025033f43cSJassi Brar 		if (config_setup(i2s)) {
903f3670536SSylwester Nawrocki 			spin_unlock_irqrestore(i2s->lock, flags);
9045033f43cSJassi Brar 			return -EINVAL;
9055033f43cSJassi Brar 		}
9065033f43cSJassi Brar 
9075033f43cSJassi Brar 		if (capture)
9085033f43cSJassi Brar 			i2s_rxctrl(i2s, 1);
9095033f43cSJassi Brar 		else
9105033f43cSJassi Brar 			i2s_txctrl(i2s, 1);
9115033f43cSJassi Brar 
912f3670536SSylwester Nawrocki 		spin_unlock_irqrestore(i2s->lock, flags);
9135033f43cSJassi Brar 		break;
9145033f43cSJassi Brar 	case SNDRV_PCM_TRIGGER_STOP:
9155033f43cSJassi Brar 	case SNDRV_PCM_TRIGGER_SUSPEND:
9165033f43cSJassi Brar 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
917f3670536SSylwester Nawrocki 		spin_lock_irqsave(i2s->lock, flags);
9185033f43cSJassi Brar 
919c90887feSJassi Brar 		if (capture) {
9205033f43cSJassi Brar 			i2s_rxctrl(i2s, 0);
921775bc971SJassi Brar 			i2s_fifo(i2s, FIC_RXFLUSH);
922c90887feSJassi Brar 		} else {
923c90887feSJassi Brar 			i2s_txctrl(i2s, 0);
924775bc971SJassi Brar 			i2s_fifo(i2s, FIC_TXFLUSH);
925c90887feSJassi Brar 		}
926775bc971SJassi Brar 
927f3670536SSylwester Nawrocki 		spin_unlock_irqrestore(i2s->lock, flags);
928dc938ddbSMarek Szyprowski 		pm_runtime_put(dai->dev);
9295033f43cSJassi Brar 		break;
9305033f43cSJassi Brar 	}
9315033f43cSJassi Brar 
9325033f43cSJassi Brar 	return 0;
9335033f43cSJassi Brar }
9345033f43cSJassi Brar 
9355033f43cSJassi Brar static int i2s_set_clkdiv(struct snd_soc_dai *dai,
9365033f43cSJassi Brar 	int div_id, int div)
9375033f43cSJassi Brar {
9385033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
939dcd60fc3SSylwester Nawrocki 	struct i2s_dai *other = get_other_dai(i2s);
9405033f43cSJassi Brar 
9415033f43cSJassi Brar 	switch (div_id) {
9425033f43cSJassi Brar 	case SAMSUNG_I2S_DIV_BCLK:
943dc938ddbSMarek Szyprowski 		pm_runtime_get_sync(dai->dev);
9445033f43cSJassi Brar 		if ((any_active(i2s) && div && (get_bfs(i2s) != div))
9455033f43cSJassi Brar 			|| (other && other->bfs && (other->bfs != div))) {
946dc938ddbSMarek Szyprowski 			pm_runtime_put(dai->dev);
9475033f43cSJassi Brar 			dev_err(&i2s->pdev->dev,
9485033f43cSJassi Brar 				"%s:%d Other DAI busy\n", __func__, __LINE__);
9495033f43cSJassi Brar 			return -EAGAIN;
9505033f43cSJassi Brar 		}
9515033f43cSJassi Brar 		i2s->bfs = div;
952dc938ddbSMarek Szyprowski 		pm_runtime_put(dai->dev);
9535033f43cSJassi Brar 		break;
9545033f43cSJassi Brar 	default:
9555033f43cSJassi Brar 		dev_err(&i2s->pdev->dev,
9565033f43cSJassi Brar 			"Invalid clock divider(%d)\n", div_id);
9575033f43cSJassi Brar 		return -EINVAL;
9585033f43cSJassi Brar 	}
9595033f43cSJassi Brar 
9605033f43cSJassi Brar 	return 0;
9615033f43cSJassi Brar }
9625033f43cSJassi Brar 
9635033f43cSJassi Brar static snd_pcm_sframes_t
9645033f43cSJassi Brar i2s_delay(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
9655033f43cSJassi Brar {
9665033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
9675033f43cSJassi Brar 	u32 reg = readl(i2s->addr + I2SFIC);
9685033f43cSJassi Brar 	snd_pcm_sframes_t delay;
969a5a56871SPadmavathi Venna 	const struct samsung_i2s_variant_regs *i2s_regs = i2s->variant_regs;
9705033f43cSJassi Brar 
971dc938ddbSMarek Szyprowski 	WARN_ON(!pm_runtime_active(dai->dev));
972dc938ddbSMarek Szyprowski 
9735033f43cSJassi Brar 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
9745033f43cSJassi Brar 		delay = FIC_RXCOUNT(reg);
9755033f43cSJassi Brar 	else if (is_secondary(i2s))
9765033f43cSJassi Brar 		delay = FICS_TXCOUNT(readl(i2s->addr + I2SFICS));
9775033f43cSJassi Brar 	else
978a5a56871SPadmavathi Venna 		delay = (reg >> i2s_regs->ftx0cnt_off) & 0x7f;
9795033f43cSJassi Brar 
9805033f43cSJassi Brar 	return delay;
9815033f43cSJassi Brar }
9825033f43cSJassi Brar 
9835033f43cSJassi Brar #ifdef CONFIG_PM
9845033f43cSJassi Brar static int i2s_suspend(struct snd_soc_dai *dai)
9855033f43cSJassi Brar {
986*e7e52dfcSMarek Szyprowski 	return pm_runtime_force_suspend(dai->dev);
9875033f43cSJassi Brar }
9885033f43cSJassi Brar 
9895033f43cSJassi Brar static int i2s_resume(struct snd_soc_dai *dai)
9905033f43cSJassi Brar {
991*e7e52dfcSMarek Szyprowski 	return pm_runtime_force_resume(dai->dev);
9925033f43cSJassi Brar }
9935033f43cSJassi Brar #else
9945033f43cSJassi Brar #define i2s_suspend NULL
9955033f43cSJassi Brar #define i2s_resume  NULL
9965033f43cSJassi Brar #endif
9975033f43cSJassi Brar 
9985033f43cSJassi Brar static int samsung_i2s_dai_probe(struct snd_soc_dai *dai)
9995033f43cSJassi Brar {
10005033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
1001dcd60fc3SSylwester Nawrocki 	struct i2s_dai *other = get_other_dai(i2s);
1002ce8bcdbbSSylwester Nawrocki 	unsigned long flags;
10035033f43cSJassi Brar 
1004dc938ddbSMarek Szyprowski 	pm_runtime_get_sync(dai->dev);
1005dc938ddbSMarek Szyprowski 
10060ec2ba80SSylwester Nawrocki 	if (is_secondary(i2s)) { /* If this is probe on the secondary DAI */
100769e7a69aSSylwester Nawrocki 		snd_soc_dai_init_dma_data(dai, &other->sec_dai->dma_playback,
10083688569eSMark Brown 					   NULL);
1009872c26bdSSylwester Nawrocki 	} else {
101069e7a69aSSylwester Nawrocki 		snd_soc_dai_init_dma_data(dai, &i2s->dma_playback,
1011872c26bdSSylwester Nawrocki 					   &i2s->dma_capture);
1012511e3033SMark Brown 
10135033f43cSJassi Brar 		if (i2s->quirks & QUIRK_NEED_RSTCLR)
10145033f43cSJassi Brar 			writel(CON_RSTCLR, i2s->addr + I2SCON);
10155033f43cSJassi Brar 
1016b0759736SPadmavathi Venna 		if (i2s->quirks & QUIRK_SUPPORTS_IDMA)
10179b8f5695SMark Brown 			idma_reg_addr_init(i2s->addr,
101869e7a69aSSylwester Nawrocki 					i2s->sec_dai->idma_playback.addr);
1019872c26bdSSylwester Nawrocki 	}
102061100f40SSangbeom Kim 
10215033f43cSJassi Brar 	/* Reset any constraint on RFS and BFS */
10225033f43cSJassi Brar 	i2s->rfs = 0;
10235033f43cSJassi Brar 	i2s->bfs = 0;
1024d66eac3eSTushar Behera 	i2s->rclk_srcrate = 0;
1025ce8bcdbbSSylwester Nawrocki 
1026ce8bcdbbSSylwester Nawrocki 	spin_lock_irqsave(i2s->lock, flags);
10275033f43cSJassi Brar 	i2s_txctrl(i2s, 0);
10285033f43cSJassi Brar 	i2s_rxctrl(i2s, 0);
10295033f43cSJassi Brar 	i2s_fifo(i2s, FIC_TXFLUSH);
10305033f43cSJassi Brar 	i2s_fifo(other, FIC_TXFLUSH);
10315033f43cSJassi Brar 	i2s_fifo(i2s, FIC_RXFLUSH);
1032ce8bcdbbSSylwester Nawrocki 	spin_unlock_irqrestore(i2s->lock, flags);
10335033f43cSJassi Brar 
10345033f43cSJassi Brar 	/* Gate CDCLK by default */
10355033f43cSJassi Brar 	if (!is_opened(other))
10365033f43cSJassi Brar 		i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK,
10375033f43cSJassi Brar 				0, SND_SOC_CLOCK_IN);
1038dc938ddbSMarek Szyprowski 	pm_runtime_put(dai->dev);
10395033f43cSJassi Brar 
10405033f43cSJassi Brar 	return 0;
10415033f43cSJassi Brar }
10425033f43cSJassi Brar 
10435033f43cSJassi Brar static int samsung_i2s_dai_remove(struct snd_soc_dai *dai)
10445033f43cSJassi Brar {
10455033f43cSJassi Brar 	struct i2s_dai *i2s = snd_soc_dai_get_drvdata(dai);
10465faf071dSCharles Keepax 	unsigned long flags;
10475033f43cSJassi Brar 
1048dc938ddbSMarek Szyprowski 	pm_runtime_get_sync(dai->dev);
1049dc938ddbSMarek Szyprowski 
1050c92f1d0eSSylwester Nawrocki 	if (!is_secondary(i2s)) {
1051ce8bcdbbSSylwester Nawrocki 		if (i2s->quirks & QUIRK_NEED_RSTCLR) {
10525faf071dSCharles Keepax 			spin_lock_irqsave(i2s->lock, flags);
10535033f43cSJassi Brar 			writel(0, i2s->addr + I2SCON);
10545faf071dSCharles Keepax 			spin_unlock_irqrestore(i2s->lock, flags);
1055ce8bcdbbSSylwester Nawrocki 		}
10565033f43cSJassi Brar 	}
10575033f43cSJassi Brar 
1058dc938ddbSMarek Szyprowski 	pm_runtime_put(dai->dev);
1059dc938ddbSMarek Szyprowski 
10605033f43cSJassi Brar 	return 0;
10615033f43cSJassi Brar }
10625033f43cSJassi Brar 
106385e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops samsung_i2s_dai_ops = {
10645033f43cSJassi Brar 	.trigger = i2s_trigger,
10655033f43cSJassi Brar 	.hw_params = i2s_hw_params,
10665033f43cSJassi Brar 	.set_fmt = i2s_set_fmt,
10675033f43cSJassi Brar 	.set_clkdiv = i2s_set_clkdiv,
10685033f43cSJassi Brar 	.set_sysclk = i2s_set_sysclk,
10695033f43cSJassi Brar 	.startup = i2s_startup,
10705033f43cSJassi Brar 	.shutdown = i2s_shutdown,
10715033f43cSJassi Brar 	.delay = i2s_delay,
10725033f43cSJassi Brar };
10735033f43cSJassi Brar 
10744b828535SKuninori Morimoto static const struct snd_soc_component_driver samsung_i2s_component = {
10754b828535SKuninori Morimoto 	.name		= "samsung-i2s",
10764b828535SKuninori Morimoto };
10774b828535SKuninori Morimoto 
10785033f43cSJassi Brar #define SAMSUNG_I2S_RATES	SNDRV_PCM_RATE_8000_96000
10795033f43cSJassi Brar 
10805033f43cSJassi Brar #define SAMSUNG_I2S_FMTS	(SNDRV_PCM_FMTBIT_S8 | \
10815033f43cSJassi Brar 					SNDRV_PCM_FMTBIT_S16_LE | \
10825033f43cSJassi Brar 					SNDRV_PCM_FMTBIT_S24_LE)
10835033f43cSJassi Brar 
1084fdca21adSBill Pemberton static struct i2s_dai *i2s_alloc_dai(struct platform_device *pdev, bool sec)
10855033f43cSJassi Brar {
10865033f43cSJassi Brar 	struct i2s_dai *i2s;
10875033f43cSJassi Brar 
1088b960ce74SMark Brown 	i2s = devm_kzalloc(&pdev->dev, sizeof(struct i2s_dai), GFP_KERNEL);
10895033f43cSJassi Brar 	if (i2s == NULL)
10905033f43cSJassi Brar 		return NULL;
10915033f43cSJassi Brar 
10925033f43cSJassi Brar 	i2s->pdev = pdev;
10935033f43cSJassi Brar 	i2s->pri_dai = NULL;
10945033f43cSJassi Brar 	i2s->sec_dai = NULL;
10955033f43cSJassi Brar 	i2s->i2s_dai_drv.symmetric_rates = 1;
10965033f43cSJassi Brar 	i2s->i2s_dai_drv.probe = samsung_i2s_dai_probe;
10975033f43cSJassi Brar 	i2s->i2s_dai_drv.remove = samsung_i2s_dai_remove;
10985033f43cSJassi Brar 	i2s->i2s_dai_drv.ops = &samsung_i2s_dai_ops;
10995033f43cSJassi Brar 	i2s->i2s_dai_drv.suspend = i2s_suspend;
11005033f43cSJassi Brar 	i2s->i2s_dai_drv.resume = i2s_resume;
1101a0ff6ea2SCharles Keepax 	i2s->i2s_dai_drv.playback.channels_min = 1;
11025033f43cSJassi Brar 	i2s->i2s_dai_drv.playback.channels_max = 2;
11035033f43cSJassi Brar 	i2s->i2s_dai_drv.playback.rates = SAMSUNG_I2S_RATES;
11045033f43cSJassi Brar 	i2s->i2s_dai_drv.playback.formats = SAMSUNG_I2S_FMTS;
11055033f43cSJassi Brar 
11065033f43cSJassi Brar 	if (!sec) {
1107588fb705SSangsu Park 		i2s->i2s_dai_drv.capture.channels_min = 1;
11085033f43cSJassi Brar 		i2s->i2s_dai_drv.capture.channels_max = 2;
11095033f43cSJassi Brar 		i2s->i2s_dai_drv.capture.rates = SAMSUNG_I2S_RATES;
11105033f43cSJassi Brar 		i2s->i2s_dai_drv.capture.formats = SAMSUNG_I2S_FMTS;
1111c6f9b1ebSPrathyush K 	}
11125033f43cSJassi Brar 	return i2s;
11135033f43cSJassi Brar }
11145033f43cSJassi Brar 
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 
1120*e7e52dfcSMarek Szyprowski 	i2s->suspend_i2smod = readl(i2s->addr + I2SMOD);
1121*e7e52dfcSMarek Szyprowski 	i2s->suspend_i2scon = readl(i2s->addr + I2SCON);
1122*e7e52dfcSMarek Szyprowski 	i2s->suspend_i2spsr = readl(i2s->addr + I2SPSR);
1123*e7e52dfcSMarek Szyprowski 
11245b1d3c34SR. Chandrasekar 	clk_disable_unprepare(i2s->clk);
11255b1d3c34SR. Chandrasekar 
11265b1d3c34SR. Chandrasekar 	return 0;
11275b1d3c34SR. Chandrasekar }
11285b1d3c34SR. Chandrasekar 
11295b1d3c34SR. Chandrasekar static int i2s_runtime_resume(struct device *dev)
11305b1d3c34SR. Chandrasekar {
11315b1d3c34SR. Chandrasekar 	struct i2s_dai *i2s = dev_get_drvdata(dev);
11325b1d3c34SR. Chandrasekar 
11335b1d3c34SR. Chandrasekar 	clk_prepare_enable(i2s->clk);
11345b1d3c34SR. Chandrasekar 
1135*e7e52dfcSMarek Szyprowski 	writel(i2s->suspend_i2scon, i2s->addr + I2SCON);
1136*e7e52dfcSMarek Szyprowski 	writel(i2s->suspend_i2smod, i2s->addr + I2SMOD);
1137*e7e52dfcSMarek Szyprowski 	writel(i2s->suspend_i2spsr, i2s->addr + I2SPSR);
1138*e7e52dfcSMarek Szyprowski 
11395b1d3c34SR. Chandrasekar 	return 0;
11405b1d3c34SR. Chandrasekar }
1141641d334bSRafael J. Wysocki #endif /* CONFIG_PM */
11425b1d3c34SR. Chandrasekar 
1143074b89bbSSylwester Nawrocki static void i2s_unregister_clocks(struct i2s_dai *i2s)
1144074b89bbSSylwester Nawrocki {
1145074b89bbSSylwester Nawrocki 	int i;
1146074b89bbSSylwester Nawrocki 
1147074b89bbSSylwester Nawrocki 	for (i = 0; i < i2s->clk_data.clk_num; i++) {
1148074b89bbSSylwester Nawrocki 		if (!IS_ERR(i2s->clk_table[i]))
1149074b89bbSSylwester Nawrocki 			clk_unregister(i2s->clk_table[i]);
1150074b89bbSSylwester Nawrocki 	}
1151074b89bbSSylwester Nawrocki }
1152074b89bbSSylwester Nawrocki 
1153074b89bbSSylwester Nawrocki static void i2s_unregister_clock_provider(struct platform_device *pdev)
1154074b89bbSSylwester Nawrocki {
1155074b89bbSSylwester Nawrocki 	struct i2s_dai *i2s = dev_get_drvdata(&pdev->dev);
1156074b89bbSSylwester Nawrocki 
1157074b89bbSSylwester Nawrocki 	of_clk_del_provider(pdev->dev.of_node);
1158074b89bbSSylwester Nawrocki 	i2s_unregister_clocks(i2s);
1159074b89bbSSylwester Nawrocki }
1160074b89bbSSylwester Nawrocki 
1161074b89bbSSylwester Nawrocki static int i2s_register_clock_provider(struct platform_device *pdev)
1162074b89bbSSylwester Nawrocki {
1163074b89bbSSylwester Nawrocki 	struct device *dev = &pdev->dev;
1164074b89bbSSylwester Nawrocki 	struct i2s_dai *i2s = dev_get_drvdata(dev);
1165074b89bbSSylwester Nawrocki 	const char *clk_name[2] = { "i2s_opclk0", "i2s_opclk1" };
1166074b89bbSSylwester Nawrocki 	const char *p_names[2] = { NULL };
1167074b89bbSSylwester Nawrocki 	const struct samsung_i2s_variant_regs *reg_info = i2s->variant_regs;
1168074b89bbSSylwester Nawrocki 	struct clk *rclksrc;
1169074b89bbSSylwester Nawrocki 	int ret, i;
1170074b89bbSSylwester Nawrocki 
1171074b89bbSSylwester Nawrocki 	/* Register the clock provider only if it's expected in the DTB */
1172074b89bbSSylwester Nawrocki 	if (!of_find_property(dev->of_node, "#clock-cells", NULL))
1173074b89bbSSylwester Nawrocki 		return 0;
1174074b89bbSSylwester Nawrocki 
1175074b89bbSSylwester Nawrocki 	/* Get the RCLKSRC mux clock parent clock names */
1176074b89bbSSylwester Nawrocki 	for (i = 0; i < ARRAY_SIZE(p_names); i++) {
1177074b89bbSSylwester Nawrocki 		rclksrc = clk_get(dev, clk_name[i]);
1178074b89bbSSylwester Nawrocki 		if (IS_ERR(rclksrc))
1179074b89bbSSylwester Nawrocki 			continue;
1180074b89bbSSylwester Nawrocki 		p_names[i] = __clk_get_name(rclksrc);
1181074b89bbSSylwester Nawrocki 		clk_put(rclksrc);
1182074b89bbSSylwester Nawrocki 	}
1183074b89bbSSylwester Nawrocki 
1184074b89bbSSylwester Nawrocki 	if (!(i2s->quirks & QUIRK_NO_MUXPSR)) {
1185074b89bbSSylwester Nawrocki 		/* Activate the prescaler */
1186074b89bbSSylwester Nawrocki 		u32 val = readl(i2s->addr + I2SPSR);
1187074b89bbSSylwester Nawrocki 		writel(val | PSR_PSREN, i2s->addr + I2SPSR);
1188074b89bbSSylwester Nawrocki 
1189074b89bbSSylwester Nawrocki 		i2s->clk_table[CLK_I2S_RCLK_SRC] = clk_register_mux(NULL,
1190074b89bbSSylwester Nawrocki 				"i2s_rclksrc", p_names, ARRAY_SIZE(p_names),
1191074b89bbSSylwester Nawrocki 				CLK_SET_RATE_NO_REPARENT | CLK_SET_RATE_PARENT,
1192074b89bbSSylwester Nawrocki 				i2s->addr + I2SMOD, reg_info->rclksrc_off,
1193074b89bbSSylwester Nawrocki 				1, 0, i2s->lock);
1194074b89bbSSylwester Nawrocki 
1195074b89bbSSylwester Nawrocki 		i2s->clk_table[CLK_I2S_RCLK_PSR] = clk_register_divider(NULL,
1196074b89bbSSylwester Nawrocki 				"i2s_presc", "i2s_rclksrc",
1197074b89bbSSylwester Nawrocki 				CLK_SET_RATE_PARENT,
1198074b89bbSSylwester Nawrocki 				i2s->addr + I2SPSR, 8, 6, 0, i2s->lock);
1199074b89bbSSylwester Nawrocki 
1200074b89bbSSylwester Nawrocki 		p_names[0] = "i2s_presc";
1201074b89bbSSylwester Nawrocki 		i2s->clk_data.clk_num = 2;
1202074b89bbSSylwester Nawrocki 	}
1203074b89bbSSylwester Nawrocki 	of_property_read_string_index(dev->of_node,
1204074b89bbSSylwester Nawrocki 				"clock-output-names", 0, &clk_name[0]);
1205074b89bbSSylwester Nawrocki 
1206074b89bbSSylwester Nawrocki 	i2s->clk_table[CLK_I2S_CDCLK] = clk_register_gate(NULL, clk_name[0],
1207074b89bbSSylwester Nawrocki 				p_names[0], CLK_SET_RATE_PARENT,
1208074b89bbSSylwester Nawrocki 				i2s->addr + I2SMOD, reg_info->cdclkcon_off,
1209074b89bbSSylwester Nawrocki 				CLK_GATE_SET_TO_DISABLE, i2s->lock);
1210074b89bbSSylwester Nawrocki 
1211074b89bbSSylwester Nawrocki 	i2s->clk_data.clk_num += 1;
1212074b89bbSSylwester Nawrocki 	i2s->clk_data.clks = i2s->clk_table;
1213074b89bbSSylwester Nawrocki 
1214074b89bbSSylwester Nawrocki 	ret = of_clk_add_provider(dev->of_node, of_clk_src_onecell_get,
1215074b89bbSSylwester Nawrocki 				  &i2s->clk_data);
1216074b89bbSSylwester Nawrocki 	if (ret < 0) {
1217074b89bbSSylwester Nawrocki 		dev_err(dev, "failed to add clock provider: %d\n", ret);
1218074b89bbSSylwester Nawrocki 		i2s_unregister_clocks(i2s);
1219074b89bbSSylwester Nawrocki 	}
1220074b89bbSSylwester Nawrocki 
1221074b89bbSSylwester Nawrocki 	return ret;
1222074b89bbSSylwester Nawrocki }
1223074b89bbSSylwester Nawrocki 
1224fdca21adSBill Pemberton static int samsung_i2s_probe(struct platform_device *pdev)
12255033f43cSJassi Brar {
12265033f43cSJassi Brar 	struct i2s_dai *pri_dai, *sec_dai = NULL;
122740476f61SPadmavathi Venna 	struct s3c_audio_pdata *i2s_pdata = pdev->dev.platform_data;
12285033f43cSJassi Brar 	struct resource *res;
122940476f61SPadmavathi Venna 	u32 regs_base, quirks = 0, idma_addr = 0;
123040476f61SPadmavathi Venna 	struct device_node *np = pdev->dev.of_node;
12317da493e9SPadmavathi Venna 	const struct samsung_i2s_dai_data *i2s_dai_data;
1232c92f1d0eSSylwester Nawrocki 	int ret;
12335033f43cSJassi Brar 
12342f7b5d14SSylwester Nawrocki 	if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node)
12352f7b5d14SSylwester Nawrocki 		i2s_dai_data = of_device_get_match_data(&pdev->dev);
12362f7b5d14SSylwester Nawrocki 	else
12372f7b5d14SSylwester Nawrocki 		i2s_dai_data = (struct samsung_i2s_dai_data *)
12382f7b5d14SSylwester Nawrocki 				platform_get_device_id(pdev)->driver_data;
12397c62eebbSPadmavathi Venna 
12405033f43cSJassi Brar 
124140476f61SPadmavathi Venna 	pri_dai = i2s_alloc_dai(pdev, false);
124240476f61SPadmavathi Venna 	if (!pri_dai) {
124340476f61SPadmavathi Venna 		dev_err(&pdev->dev, "Unable to alloc I2S_pri\n");
124440476f61SPadmavathi Venna 		return -ENOMEM;
124540476f61SPadmavathi Venna 	}
124640476f61SPadmavathi Venna 
1247f3670536SSylwester Nawrocki 	spin_lock_init(&pri_dai->spinlock);
1248f3670536SSylwester Nawrocki 	pri_dai->lock = &pri_dai->spinlock;
1249f3670536SSylwester Nawrocki 
125040476f61SPadmavathi Venna 	if (!np) {
12515033f43cSJassi Brar 		if (i2s_pdata == NULL) {
12525033f43cSJassi Brar 			dev_err(&pdev->dev, "Can't work without s3c_audio_pdata\n");
12535033f43cSJassi Brar 			return -EINVAL;
12545033f43cSJassi Brar 		}
12555033f43cSJassi Brar 
125669e7a69aSSylwester Nawrocki 		pri_dai->dma_playback.filter_data = i2s_pdata->dma_playback;
125769e7a69aSSylwester Nawrocki 		pri_dai->dma_capture.filter_data = i2s_pdata->dma_capture;
12589bdca822SArnd Bergmann 		pri_dai->filter = i2s_pdata->dma_filter;
1259b9a1a743SArnd Bergmann 
1260409c69beSKrzysztof Kozlowski 		quirks = i2s_pdata->type.quirks;
1261409c69beSKrzysztof Kozlowski 		idma_addr = i2s_pdata->type.idma_addr;
126240476f61SPadmavathi Venna 	} else {
12637da493e9SPadmavathi Venna 		quirks = i2s_dai_data->quirks;
126440476f61SPadmavathi Venna 		if (of_property_read_u32(np, "samsung,idma-addr",
126540476f61SPadmavathi Venna 					 &idma_addr)) {
1266b0759736SPadmavathi Venna 			if (quirks & QUIRK_SUPPORTS_IDMA) {
1267b0759736SPadmavathi Venna 				dev_info(&pdev->dev, "idma address is not"\
126840476f61SPadmavathi Venna 						"specified");
126940476f61SPadmavathi Venna 			}
127040476f61SPadmavathi Venna 		}
127140476f61SPadmavathi Venna 	}
12725033f43cSJassi Brar 
12735033f43cSJassi Brar 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1274af1cf5cfSSylwester Nawrocki 	pri_dai->addr = devm_ioremap_resource(&pdev->dev, res);
1275af1cf5cfSSylwester Nawrocki 	if (IS_ERR(pri_dai->addr))
1276af1cf5cfSSylwester Nawrocki 		return PTR_ERR(pri_dai->addr);
12775033f43cSJassi Brar 
12785033f43cSJassi Brar 	regs_base = res->start;
12795033f43cSJassi Brar 
12800ec2ba80SSylwester Nawrocki 	pri_dai->clk = devm_clk_get(&pdev->dev, "iis");
12810ec2ba80SSylwester Nawrocki 	if (IS_ERR(pri_dai->clk)) {
12820ec2ba80SSylwester Nawrocki 		dev_err(&pdev->dev, "Failed to get iis clock\n");
12830ec2ba80SSylwester Nawrocki 		return PTR_ERR(pri_dai->clk);
12840ec2ba80SSylwester Nawrocki 	}
1285c92f1d0eSSylwester Nawrocki 
1286c92f1d0eSSylwester Nawrocki 	ret = clk_prepare_enable(pri_dai->clk);
1287c92f1d0eSSylwester Nawrocki 	if (ret != 0) {
1288c92f1d0eSSylwester Nawrocki 		dev_err(&pdev->dev, "failed to enable clock: %d\n", ret);
1289c92f1d0eSSylwester Nawrocki 		return ret;
1290c92f1d0eSSylwester Nawrocki 	}
129169e7a69aSSylwester Nawrocki 	pri_dai->dma_playback.addr = regs_base + I2STXD;
129269e7a69aSSylwester Nawrocki 	pri_dai->dma_capture.addr = regs_base + I2SRXD;
129369e7a69aSSylwester Nawrocki 	pri_dai->dma_playback.addr_width = 4;
129469e7a69aSSylwester Nawrocki 	pri_dai->dma_capture.addr_width = 4;
12955033f43cSJassi Brar 	pri_dai->quirks = quirks;
1296a5a56871SPadmavathi Venna 	pri_dai->variant_regs = i2s_dai_data->i2s_variant_regs;
12975033f43cSJassi Brar 
12985033f43cSJassi Brar 	if (quirks & QUIRK_PRI_6CHAN)
12995033f43cSJassi Brar 		pri_dai->i2s_dai_drv.playback.channels_max = 6;
13005033f43cSJassi Brar 
130173f5dfc6SMarek Szyprowski 	ret = samsung_asoc_dma_platform_register(&pdev->dev, pri_dai->filter,
130273f5dfc6SMarek Szyprowski 						 NULL, NULL);
130373f5dfc6SMarek Szyprowski 	if (ret < 0)
130473f5dfc6SMarek Szyprowski 		goto err_disable_clk;
130573f5dfc6SMarek Szyprowski 
1306be2c92ebSMarek Szyprowski 	ret = devm_snd_soc_register_component(&pdev->dev,
1307be2c92ebSMarek Szyprowski 					&samsung_i2s_component,
1308be2c92ebSMarek Szyprowski 					&pri_dai->i2s_dai_drv, 1);
1309be2c92ebSMarek Szyprowski 	if (ret < 0)
1310be2c92ebSMarek Szyprowski 		goto err_disable_clk;
1311be2c92ebSMarek Szyprowski 
13125033f43cSJassi Brar 	if (quirks & QUIRK_SEC_DAI) {
13135033f43cSJassi Brar 		sec_dai = i2s_alloc_dai(pdev, true);
13145033f43cSJassi Brar 		if (!sec_dai) {
13155033f43cSJassi Brar 			dev_err(&pdev->dev, "Unable to alloc I2S_sec\n");
1316fd61576fSWei Yongjun 			ret = -ENOMEM;
1317fd61576fSWei Yongjun 			goto err_disable_clk;
13185033f43cSJassi Brar 		}
13197e5d8706SSylwester Nawrocki 
1320f3670536SSylwester Nawrocki 		sec_dai->lock = &pri_dai->spinlock;
13217e5d8706SSylwester Nawrocki 		sec_dai->variant_regs = pri_dai->variant_regs;
132269e7a69aSSylwester Nawrocki 		sec_dai->dma_playback.addr = regs_base + I2STXDS;
132340476f61SPadmavathi Venna 
13249bdca822SArnd Bergmann 		if (!np) {
132569e7a69aSSylwester Nawrocki 			sec_dai->dma_playback.filter_data = i2s_pdata->dma_play_sec;
13269bdca822SArnd Bergmann 			sec_dai->filter = i2s_pdata->dma_filter;
13279bdca822SArnd Bergmann 		}
132840476f61SPadmavathi Venna 
132969e7a69aSSylwester Nawrocki 		sec_dai->dma_playback.addr_width = 4;
1330af1cf5cfSSylwester Nawrocki 		sec_dai->addr = pri_dai->addr;
13310ec2ba80SSylwester Nawrocki 		sec_dai->clk = pri_dai->clk;
13325033f43cSJassi Brar 		sec_dai->quirks = quirks;
133369e7a69aSSylwester Nawrocki 		sec_dai->idma_playback.addr = idma_addr;
13345033f43cSJassi Brar 		sec_dai->pri_dai = pri_dai;
13355033f43cSJassi Brar 		pri_dai->sec_dai = sec_dai;
1336be2c92ebSMarek Szyprowski 
1337be2c92ebSMarek Szyprowski 		ret = samsung_asoc_dma_platform_register(&pdev->dev,
1338be2c92ebSMarek Szyprowski 					sec_dai->filter, "tx-sec", NULL);
1339be2c92ebSMarek Szyprowski 		if (ret < 0)
1340be2c92ebSMarek Szyprowski 			goto err_disable_clk;
1341be2c92ebSMarek Szyprowski 
1342be2c92ebSMarek Szyprowski 		ret = devm_snd_soc_register_component(&pdev->dev,
1343be2c92ebSMarek Szyprowski 						&samsung_i2s_component,
1344be2c92ebSMarek Szyprowski 						&sec_dai->i2s_dai_drv, 1);
1345be2c92ebSMarek Szyprowski 		if (ret < 0)
1346be2c92ebSMarek Szyprowski 			goto err_disable_clk;
13475033f43cSJassi Brar 	}
13485033f43cSJassi Brar 
13490429ffefSMark Brown 	if (i2s_pdata && i2s_pdata->cfg_gpio && i2s_pdata->cfg_gpio(pdev)) {
135040476f61SPadmavathi Venna 		dev_err(&pdev->dev, "Unable to configure gpio\n");
1351fd61576fSWei Yongjun 		ret = -EINVAL;
1352fd61576fSWei Yongjun 		goto err_disable_clk;
135340476f61SPadmavathi Venna 	}
13545033f43cSJassi Brar 
1355be2c92ebSMarek Szyprowski 	dev_set_drvdata(&pdev->dev, pri_dai);
13562b960386SSylwester Nawrocki 
1357dc938ddbSMarek Szyprowski 	pm_runtime_set_active(&pdev->dev);
1358c5cf4dbcSMark Brown 	pm_runtime_enable(&pdev->dev);
1359c5cf4dbcSMark Brown 
13602b960386SSylwester Nawrocki 	ret = i2s_register_clock_provider(pdev);
13612b960386SSylwester Nawrocki 	if (!ret)
13622b960386SSylwester Nawrocki 		return 0;
1363a08485d8SPadmavathi Venna 
13642b960386SSylwester Nawrocki 	pm_runtime_disable(&pdev->dev);
1365fd61576fSWei Yongjun err_disable_clk:
1366fd61576fSWei Yongjun 	clk_disable_unprepare(pri_dai->clk);
13672b960386SSylwester Nawrocki 	return ret;
13685033f43cSJassi Brar }
13695033f43cSJassi Brar 
1370fdca21adSBill Pemberton static int samsung_i2s_remove(struct platform_device *pdev)
13715033f43cSJassi Brar {
1372be2c92ebSMarek Szyprowski 	struct i2s_dai *pri_dai, *sec_dai;
13735033f43cSJassi Brar 
1374be2c92ebSMarek Szyprowski 	pri_dai = dev_get_drvdata(&pdev->dev);
1375be2c92ebSMarek Szyprowski 	sec_dai = pri_dai->sec_dai;
13765033f43cSJassi Brar 
1377be2c92ebSMarek Szyprowski 	pri_dai->sec_dai = NULL;
1378be2c92ebSMarek Szyprowski 	sec_dai->pri_dai = NULL;
1379be2c92ebSMarek Szyprowski 
1380dc938ddbSMarek Szyprowski 	pm_runtime_get_sync(&pdev->dev);
1381c5cf4dbcSMark Brown 	pm_runtime_disable(&pdev->dev);
13825033f43cSJassi Brar 
1383074b89bbSSylwester Nawrocki 	i2s_unregister_clock_provider(pdev);
1384be2c92ebSMarek Szyprowski 	clk_disable_unprepare(pri_dai->clk);
1385dc938ddbSMarek Szyprowski 	pm_runtime_put_noidle(&pdev->dev);
13865033f43cSJassi Brar 
13875033f43cSJassi Brar 	return 0;
13885033f43cSJassi Brar }
13895033f43cSJassi Brar 
1390a5a56871SPadmavathi Venna static const struct samsung_i2s_variant_regs i2sv3_regs = {
1391a5a56871SPadmavathi Venna 	.bfs_off = 1,
1392a5a56871SPadmavathi Venna 	.rfs_off = 3,
1393a5a56871SPadmavathi Venna 	.sdf_off = 5,
1394a5a56871SPadmavathi Venna 	.txr_off = 8,
1395a5a56871SPadmavathi Venna 	.rclksrc_off = 10,
1396a5a56871SPadmavathi Venna 	.mss_off = 11,
1397a5a56871SPadmavathi Venna 	.cdclkcon_off = 12,
1398a5a56871SPadmavathi Venna 	.lrp_off = 7,
1399a5a56871SPadmavathi Venna 	.bfs_mask = 0x3,
1400a5a56871SPadmavathi Venna 	.rfs_mask = 0x3,
1401a5a56871SPadmavathi Venna 	.ftx0cnt_off = 8,
1402a5a56871SPadmavathi Venna };
1403a5a56871SPadmavathi Venna 
1404a5a56871SPadmavathi Venna static const struct samsung_i2s_variant_regs i2sv6_regs = {
1405a5a56871SPadmavathi Venna 	.bfs_off = 0,
1406a5a56871SPadmavathi Venna 	.rfs_off = 4,
1407a5a56871SPadmavathi Venna 	.sdf_off = 6,
1408a5a56871SPadmavathi Venna 	.txr_off = 8,
1409a5a56871SPadmavathi Venna 	.rclksrc_off = 10,
1410a5a56871SPadmavathi Venna 	.mss_off = 11,
1411a5a56871SPadmavathi Venna 	.cdclkcon_off = 12,
1412a5a56871SPadmavathi Venna 	.lrp_off = 15,
1413a5a56871SPadmavathi Venna 	.bfs_mask = 0xf,
1414a5a56871SPadmavathi Venna 	.rfs_mask = 0x3,
1415a5a56871SPadmavathi Venna 	.ftx0cnt_off = 8,
1416a5a56871SPadmavathi Venna };
1417a5a56871SPadmavathi Venna 
1418a5a56871SPadmavathi Venna static const struct samsung_i2s_variant_regs i2sv7_regs = {
1419a5a56871SPadmavathi Venna 	.bfs_off = 0,
1420a5a56871SPadmavathi Venna 	.rfs_off = 4,
1421a5a56871SPadmavathi Venna 	.sdf_off = 7,
1422a5a56871SPadmavathi Venna 	.txr_off = 9,
1423a5a56871SPadmavathi Venna 	.rclksrc_off = 11,
1424a5a56871SPadmavathi Venna 	.mss_off = 12,
1425a5a56871SPadmavathi Venna 	.cdclkcon_off = 22,
1426a5a56871SPadmavathi Venna 	.lrp_off = 15,
1427a5a56871SPadmavathi Venna 	.bfs_mask = 0xf,
1428a5a56871SPadmavathi Venna 	.rfs_mask = 0x7,
1429a5a56871SPadmavathi Venna 	.ftx0cnt_off = 0,
1430a5a56871SPadmavathi Venna };
1431a5a56871SPadmavathi Venna 
1432a5a56871SPadmavathi Venna static const struct samsung_i2s_variant_regs i2sv5_i2s1_regs = {
1433a5a56871SPadmavathi Venna 	.bfs_off = 0,
1434a5a56871SPadmavathi Venna 	.rfs_off = 3,
1435a5a56871SPadmavathi Venna 	.sdf_off = 6,
1436a5a56871SPadmavathi Venna 	.txr_off = 8,
1437a5a56871SPadmavathi Venna 	.rclksrc_off = 10,
1438a5a56871SPadmavathi Venna 	.mss_off = 11,
1439a5a56871SPadmavathi Venna 	.cdclkcon_off = 12,
1440a5a56871SPadmavathi Venna 	.lrp_off = 15,
1441a5a56871SPadmavathi Venna 	.bfs_mask = 0x7,
1442a5a56871SPadmavathi Venna 	.rfs_mask = 0x7,
1443a5a56871SPadmavathi Venna 	.ftx0cnt_off = 8,
1444a5a56871SPadmavathi Venna };
1445a5a56871SPadmavathi Venna 
14467da493e9SPadmavathi Venna static const struct samsung_i2s_dai_data i2sv3_dai_type = {
14477da493e9SPadmavathi Venna 	.quirks = QUIRK_NO_MUXPSR,
1448a5a56871SPadmavathi Venna 	.i2s_variant_regs = &i2sv3_regs,
14497da493e9SPadmavathi Venna };
14507da493e9SPadmavathi Venna 
14517da493e9SPadmavathi Venna static const struct samsung_i2s_dai_data i2sv5_dai_type = {
1452b0759736SPadmavathi Venna 	.quirks = QUIRK_PRI_6CHAN | QUIRK_SEC_DAI | QUIRK_NEED_RSTCLR |
1453b0759736SPadmavathi Venna 			QUIRK_SUPPORTS_IDMA,
1454a5a56871SPadmavathi Venna 	.i2s_variant_regs = &i2sv3_regs,
14557da493e9SPadmavathi Venna };
14567da493e9SPadmavathi Venna 
14574ca0c0d4SPadmavathi Venna static const struct samsung_i2s_dai_data i2sv6_dai_type = {
14584ca0c0d4SPadmavathi Venna 	.quirks = QUIRK_PRI_6CHAN | QUIRK_SEC_DAI | QUIRK_NEED_RSTCLR |
1459b0759736SPadmavathi Venna 			QUIRK_SUPPORTS_TDM | QUIRK_SUPPORTS_IDMA,
1460a5a56871SPadmavathi Venna 	.i2s_variant_regs = &i2sv6_regs,
1461a5a56871SPadmavathi Venna };
1462a5a56871SPadmavathi Venna 
1463a5a56871SPadmavathi Venna static const struct samsung_i2s_dai_data i2sv7_dai_type = {
1464a5a56871SPadmavathi Venna 	.quirks = QUIRK_PRI_6CHAN | QUIRK_SEC_DAI | QUIRK_NEED_RSTCLR |
1465a5a56871SPadmavathi Venna 			QUIRK_SUPPORTS_TDM,
1466a5a56871SPadmavathi Venna 	.i2s_variant_regs = &i2sv7_regs,
1467a5a56871SPadmavathi Venna };
1468a5a56871SPadmavathi Venna 
1469a5a56871SPadmavathi Venna static const struct samsung_i2s_dai_data i2sv5_dai_type_i2s1 = {
1470a5a56871SPadmavathi Venna 	.quirks = QUIRK_PRI_6CHAN | QUIRK_NEED_RSTCLR,
1471a5a56871SPadmavathi Venna 	.i2s_variant_regs = &i2sv5_i2s1_regs,
14724ca0c0d4SPadmavathi Venna };
14734ca0c0d4SPadmavathi Venna 
1474eb8ca0faSKrzysztof Kozlowski static const struct platform_device_id samsung_i2s_driver_ids[] = {
14757c62eebbSPadmavathi Venna 	{
14767c62eebbSPadmavathi Venna 		.name           = "samsung-i2s",
14773f024980SMark Brown 		.driver_data	= (kernel_ulong_t)&i2sv3_dai_type,
14787c62eebbSPadmavathi Venna 	},
14797c62eebbSPadmavathi Venna 	{},
14807c62eebbSPadmavathi Venna };
14812af19558SArnd Bergmann MODULE_DEVICE_TABLE(platform, samsung_i2s_driver_ids);
14827c62eebbSPadmavathi Venna 
148340476f61SPadmavathi Venna #ifdef CONFIG_OF
148440476f61SPadmavathi Venna static const struct of_device_id exynos_i2s_match[] = {
14857da493e9SPadmavathi Venna 	{
14867da493e9SPadmavathi Venna 		.compatible = "samsung,s3c6410-i2s",
14877da493e9SPadmavathi Venna 		.data = &i2sv3_dai_type,
14887da493e9SPadmavathi Venna 	}, {
14897da493e9SPadmavathi Venna 		.compatible = "samsung,s5pv210-i2s",
14907da493e9SPadmavathi Venna 		.data = &i2sv5_dai_type,
14914ca0c0d4SPadmavathi Venna 	}, {
14924ca0c0d4SPadmavathi Venna 		.compatible = "samsung,exynos5420-i2s",
14934ca0c0d4SPadmavathi Venna 		.data = &i2sv6_dai_type,
1494a5a56871SPadmavathi Venna 	}, {
1495a5a56871SPadmavathi Venna 		.compatible = "samsung,exynos7-i2s",
1496a5a56871SPadmavathi Venna 		.data = &i2sv7_dai_type,
1497a5a56871SPadmavathi Venna 	}, {
1498a5a56871SPadmavathi Venna 		.compatible = "samsung,exynos7-i2s1",
1499a5a56871SPadmavathi Venna 		.data = &i2sv5_dai_type_i2s1,
150040476f61SPadmavathi Venna 	},
150140476f61SPadmavathi Venna 	{},
150240476f61SPadmavathi Venna };
150340476f61SPadmavathi Venna MODULE_DEVICE_TABLE(of, exynos_i2s_match);
150440476f61SPadmavathi Venna #endif
150540476f61SPadmavathi Venna 
15065b1d3c34SR. Chandrasekar static const struct dev_pm_ops samsung_i2s_pm = {
15075b1d3c34SR. Chandrasekar 	SET_RUNTIME_PM_OPS(i2s_runtime_suspend,
15085b1d3c34SR. Chandrasekar 				i2s_runtime_resume, NULL)
1509*e7e52dfcSMarek Szyprowski 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1510*e7e52dfcSMarek Szyprowski 				     pm_runtime_force_resume)
15115b1d3c34SR. Chandrasekar };
15125b1d3c34SR. Chandrasekar 
15135033f43cSJassi Brar static struct platform_driver samsung_i2s_driver = {
15145033f43cSJassi Brar 	.probe  = samsung_i2s_probe,
1515fdca21adSBill Pemberton 	.remove = samsung_i2s_remove,
15167c62eebbSPadmavathi Venna 	.id_table = samsung_i2s_driver_ids,
15175033f43cSJassi Brar 	.driver = {
15185033f43cSJassi Brar 		.name = "samsung-i2s",
151940476f61SPadmavathi Venna 		.of_match_table = of_match_ptr(exynos_i2s_match),
15205b1d3c34SR. Chandrasekar 		.pm = &samsung_i2s_pm,
15215033f43cSJassi Brar 	},
15225033f43cSJassi Brar };
15235033f43cSJassi Brar 
1524e00c3f55SMark Brown module_platform_driver(samsung_i2s_driver);
15255033f43cSJassi Brar 
15265033f43cSJassi Brar /* Module information */
1527df8ad335SJaswinder Singh MODULE_AUTHOR("Jaswinder Singh, <jassisinghbrar@gmail.com>");
15285033f43cSJassi Brar MODULE_DESCRIPTION("Samsung I2S Interface");
15295033f43cSJassi Brar MODULE_ALIAS("platform:samsung-i2s");
15305033f43cSJassi Brar MODULE_LICENSE("GPL");
1531