xref: /linux/sound/soc/samsung/i2s.c (revision c86d50f9dc525cb0264c25ed5186faf0f1d00477)
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 
135033f43cSJassi Brar #include <linux/delay.h>
145033f43cSJassi Brar #include <linux/slab.h>
155033f43cSJassi Brar #include <linux/clk.h>
165033f43cSJassi Brar #include <linux/io.h>
17da155d5bSPaul Gortmaker #include <linux/module.h>
1840476f61SPadmavathi Venna #include <linux/of.h>
1940476f61SPadmavathi Venna #include <linux/of_gpio.h>
20c5cf4dbcSMark Brown #include <linux/pm_runtime.h>
215033f43cSJassi Brar 
225033f43cSJassi Brar #include <sound/soc.h>
230378b6acSSeungwhan Youn #include <sound/pcm_params.h>
245033f43cSJassi Brar 
25436d42c6SArnd Bergmann #include <linux/platform_data/asoc-s3c.h>
265033f43cSJassi Brar 
275033f43cSJassi Brar #include "dma.h"
2861100f40SSangbeom Kim #include "idma.h"
295033f43cSJassi Brar #include "i2s.h"
30172a453dSSangbeom Kim #include "i2s-regs.h"
315033f43cSJassi Brar 
325033f43cSJassi Brar #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
335033f43cSJassi Brar 
347c62eebbSPadmavathi Venna enum samsung_dai_type {
357c62eebbSPadmavathi Venna 	TYPE_PRI,
367c62eebbSPadmavathi Venna 	TYPE_SEC,
377c62eebbSPadmavathi Venna };
387c62eebbSPadmavathi Venna 
3940476f61SPadmavathi Venna struct samsung_i2s_dai_data {
4040476f61SPadmavathi Venna 	int dai_type;
417da493e9SPadmavathi Venna 	u32 quirks;
4240476f61SPadmavathi Venna };
4340476f61SPadmavathi Venna 
445033f43cSJassi Brar struct i2s_dai {
455033f43cSJassi Brar 	/* Platform device for this DAI */
465033f43cSJassi Brar 	struct platform_device *pdev;
475033f43cSJassi Brar 	/* IOREMAP'd SFRs */
485033f43cSJassi Brar 	void __iomem	*addr;
495033f43cSJassi Brar 	/* Physical base address of SFRs */
505033f43cSJassi Brar 	u32	base;
515033f43cSJassi Brar 	/* Rate of RCLK source clock */
525033f43cSJassi Brar 	unsigned long rclk_srcrate;
535033f43cSJassi Brar 	/* Frame Clock */
545033f43cSJassi Brar 	unsigned frmclk;
555033f43cSJassi Brar 	/*
565033f43cSJassi Brar 	 * Specifically requested RCLK,BCLK by MACHINE Driver.
575033f43cSJassi Brar 	 * 0 indicates CPU driver is free to choose any value.
585033f43cSJassi Brar 	 */
595033f43cSJassi Brar 	unsigned rfs, bfs;
605033f43cSJassi Brar 	/* I2S Controller's core clock */
615033f43cSJassi Brar 	struct clk *clk;
625033f43cSJassi Brar 	/* Clock for generating I2S signals */
635033f43cSJassi Brar 	struct clk *op_clk;
645033f43cSJassi Brar 	/* Pointer to the Primary_Fifo if this is Sec_Fifo, NULL otherwise */
655033f43cSJassi Brar 	struct i2s_dai *pri_dai;
665033f43cSJassi Brar 	/* Pointer to the Secondary_Fifo if it has one, NULL otherwise */
675033f43cSJassi Brar 	struct i2s_dai *sec_dai;
685033f43cSJassi Brar #define DAI_OPENED	(1 << 0) /* Dai is opened */
695033f43cSJassi Brar #define DAI_MANAGER	(1 << 1) /* Dai is the manager */
705033f43cSJassi Brar 	unsigned mode;
715033f43cSJassi Brar 	/* Driver for this DAI */
725033f43cSJassi Brar 	struct snd_soc_dai_driver i2s_dai_drv;
735033f43cSJassi Brar 	/* DMA parameters */
745033f43cSJassi Brar 	struct s3c_dma_params dma_playback;
755033f43cSJassi Brar 	struct s3c_dma_params dma_capture;
7661100f40SSangbeom Kim 	struct s3c_dma_params idma_playback;
775033f43cSJassi Brar 	u32	quirks;
785033f43cSJassi Brar 	u32	suspend_i2smod;
795033f43cSJassi Brar 	u32	suspend_i2scon;
805033f43cSJassi Brar 	u32	suspend_i2spsr;
8140476f61SPadmavathi Venna 	unsigned long gpios[7];	/* i2s gpio line numbers */
825033f43cSJassi Brar };
835033f43cSJassi Brar 
845033f43cSJassi Brar /* Lock for cross i/f checks */
855033f43cSJassi Brar static DEFINE_SPINLOCK(lock);
865033f43cSJassi Brar 
875033f43cSJassi Brar /* If this is the 'overlay' stereo DAI */
885033f43cSJassi Brar static inline bool is_secondary(struct i2s_dai *i2s)
895033f43cSJassi Brar {
905033f43cSJassi Brar 	return i2s->pri_dai ? true : false;
915033f43cSJassi Brar }
925033f43cSJassi Brar 
935033f43cSJassi Brar /* If operating in SoC-Slave mode */
945033f43cSJassi Brar static inline bool is_slave(struct i2s_dai *i2s)
955033f43cSJassi Brar {
965033f43cSJassi Brar 	return (readl(i2s->addr + I2SMOD) & MOD_SLAVE) ? true : false;
975033f43cSJassi Brar }
985033f43cSJassi Brar 
995033f43cSJassi Brar /* If this interface of the controller is transmitting data */
1005033f43cSJassi Brar static inline bool tx_active(struct i2s_dai *i2s)
1015033f43cSJassi Brar {
1025033f43cSJassi Brar 	u32 active;
1035033f43cSJassi Brar 
1045033f43cSJassi Brar 	if (!i2s)
1055033f43cSJassi Brar 		return false;
1065033f43cSJassi Brar 
10733195500SSangbeom Kim 	active = readl(i2s->addr + I2SCON);
1085033f43cSJassi Brar 
1095033f43cSJassi Brar 	if (is_secondary(i2s))
1105033f43cSJassi Brar 		active &= CON_TXSDMA_ACTIVE;
1115033f43cSJassi Brar 	else
1125033f43cSJassi Brar 		active &= CON_TXDMA_ACTIVE;
1135033f43cSJassi Brar 
1145033f43cSJassi Brar 	return active ? true : false;
1155033f43cSJassi Brar }
1165033f43cSJassi Brar 
1175033f43cSJassi Brar /* If the other interface of the controller is transmitting data */
1185033f43cSJassi Brar static inline bool other_tx_active(struct i2s_dai *i2s)
1195033f43cSJassi Brar {
1205033f43cSJassi Brar 	struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
1215033f43cSJassi Brar 
1225033f43cSJassi Brar 	return tx_active(other);
1235033f43cSJassi Brar }
1245033f43cSJassi Brar 
1255033f43cSJassi Brar /* If any interface of the controller is transmitting data */
1265033f43cSJassi Brar static inline bool any_tx_active(struct i2s_dai *i2s)
1275033f43cSJassi Brar {
1285033f43cSJassi Brar 	return tx_active(i2s) || other_tx_active(i2s);
1295033f43cSJassi Brar }
1305033f43cSJassi Brar 
1315033f43cSJassi Brar /* If this interface of the controller is receiving data */
1325033f43cSJassi Brar static inline bool rx_active(struct i2s_dai *i2s)
1335033f43cSJassi Brar {
1345033f43cSJassi Brar 	u32 active;
1355033f43cSJassi Brar 
1365033f43cSJassi Brar 	if (!i2s)
1375033f43cSJassi Brar 		return false;
1385033f43cSJassi Brar 
13933195500SSangbeom Kim 	active = readl(i2s->addr + I2SCON) & CON_RXDMA_ACTIVE;
1405033f43cSJassi Brar 
1415033f43cSJassi Brar 	return active ? true : false;
1425033f43cSJassi Brar }
1435033f43cSJassi Brar 
1445033f43cSJassi Brar /* If the other interface of the controller is receiving data */
1455033f43cSJassi Brar static inline bool other_rx_active(struct i2s_dai *i2s)
1465033f43cSJassi Brar {
1475033f43cSJassi Brar 	struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
1485033f43cSJassi Brar 
1495033f43cSJassi Brar 	return rx_active(other);
1505033f43cSJassi Brar }
1515033f43cSJassi Brar 
1525033f43cSJassi Brar /* If any interface of the controller is receiving data */
1535033f43cSJassi Brar static inline bool any_rx_active(struct i2s_dai *i2s)
1545033f43cSJassi Brar {
1555033f43cSJassi Brar 	return rx_active(i2s) || other_rx_active(i2s);
1565033f43cSJassi Brar }
1575033f43cSJassi Brar 
1585033f43cSJassi Brar /* If the other DAI is transmitting or receiving data */
1595033f43cSJassi Brar static inline bool other_active(struct i2s_dai *i2s)
1605033f43cSJassi Brar {
1615033f43cSJassi Brar 	return other_rx_active(i2s) || other_tx_active(i2s);
1625033f43cSJassi Brar }
1635033f43cSJassi Brar 
1645033f43cSJassi Brar /* If this DAI is transmitting or receiving data */
1655033f43cSJassi Brar static inline bool this_active(struct i2s_dai *i2s)
1665033f43cSJassi Brar {
1675033f43cSJassi Brar 	return tx_active(i2s) || rx_active(i2s);
1685033f43cSJassi Brar }
1695033f43cSJassi Brar 
1705033f43cSJassi Brar /* If the controller is active anyway */
1715033f43cSJassi Brar static inline bool any_active(struct i2s_dai *i2s)
1725033f43cSJassi Brar {
1735033f43cSJassi Brar 	return this_active(i2s) || other_active(i2s);
1745033f43cSJassi Brar }
1755033f43cSJassi Brar 
1765033f43cSJassi Brar static inline struct i2s_dai *to_info(struct snd_soc_dai *dai)
1775033f43cSJassi Brar {
1785033f43cSJassi Brar 	return snd_soc_dai_get_drvdata(dai);
1795033f43cSJassi Brar }
1805033f43cSJassi Brar 
1815033f43cSJassi Brar static inline bool is_opened(struct i2s_dai *i2s)
1825033f43cSJassi Brar {
1835033f43cSJassi Brar 	if (i2s && (i2s->mode & DAI_OPENED))
1845033f43cSJassi Brar 		return true;
1855033f43cSJassi Brar 	else
1865033f43cSJassi Brar 		return false;
1875033f43cSJassi Brar }
1885033f43cSJassi Brar 
1895033f43cSJassi Brar static inline bool is_manager(struct i2s_dai *i2s)
1905033f43cSJassi Brar {
1915033f43cSJassi Brar 	if (is_opened(i2s) && (i2s->mode & DAI_MANAGER))
1925033f43cSJassi Brar 		return true;
1935033f43cSJassi Brar 	else
1945033f43cSJassi Brar 		return false;
1955033f43cSJassi Brar }
1965033f43cSJassi Brar 
1975033f43cSJassi Brar /* Read RCLK of I2S (in multiples of LRCLK) */
1985033f43cSJassi Brar static inline unsigned get_rfs(struct i2s_dai *i2s)
1995033f43cSJassi Brar {
2004ca0c0d4SPadmavathi Venna 	u32 rfs;
2014ca0c0d4SPadmavathi Venna 
2024ca0c0d4SPadmavathi Venna 	if (i2s->quirks & QUIRK_SUPPORTS_TDM)
2034ca0c0d4SPadmavathi Venna 		rfs = readl(i2s->addr + I2SMOD) >> EXYNOS5420_MOD_RCLK_SHIFT;
2044ca0c0d4SPadmavathi Venna 	else
2054ca0c0d4SPadmavathi Venna 		rfs = (readl(i2s->addr + I2SMOD) >> MOD_RCLK_SHIFT);
206b60be4aaSPadmavathi Venna 	rfs &= MOD_RCLK_MASK;
2075033f43cSJassi Brar 
2085033f43cSJassi Brar 	switch (rfs) {
2095033f43cSJassi Brar 	case 3:	return 768;
2105033f43cSJassi Brar 	case 2: return 384;
2115033f43cSJassi Brar 	case 1:	return 512;
2125033f43cSJassi Brar 	default: return 256;
2135033f43cSJassi Brar 	}
2145033f43cSJassi Brar }
2155033f43cSJassi Brar 
2165033f43cSJassi Brar /* Write RCLK of I2S (in multiples of LRCLK) */
2175033f43cSJassi Brar static inline void set_rfs(struct i2s_dai *i2s, unsigned rfs)
2185033f43cSJassi Brar {
2195033f43cSJassi Brar 	u32 mod = readl(i2s->addr + I2SMOD);
2204ca0c0d4SPadmavathi Venna 	int rfs_shift;
2215033f43cSJassi Brar 
2224ca0c0d4SPadmavathi Venna 	if (i2s->quirks & QUIRK_SUPPORTS_TDM)
2234ca0c0d4SPadmavathi Venna 		rfs_shift = EXYNOS5420_MOD_RCLK_SHIFT;
2244ca0c0d4SPadmavathi Venna 	else
2254ca0c0d4SPadmavathi Venna 		rfs_shift = MOD_RCLK_SHIFT;
226b60be4aaSPadmavathi Venna 	mod &= ~(MOD_RCLK_MASK << rfs_shift);
2275033f43cSJassi Brar 
2285033f43cSJassi Brar 	switch (rfs) {
2295033f43cSJassi Brar 	case 768:
230b60be4aaSPadmavathi Venna 		mod |= (MOD_RCLK_768FS << rfs_shift);
2315033f43cSJassi Brar 		break;
2325033f43cSJassi Brar 	case 512:
233b60be4aaSPadmavathi Venna 		mod |= (MOD_RCLK_512FS << rfs_shift);
2345033f43cSJassi Brar 		break;
2355033f43cSJassi Brar 	case 384:
236b60be4aaSPadmavathi Venna 		mod |= (MOD_RCLK_384FS << rfs_shift);
2375033f43cSJassi Brar 		break;
2385033f43cSJassi Brar 	default:
239b60be4aaSPadmavathi Venna 		mod |= (MOD_RCLK_256FS << rfs_shift);
2405033f43cSJassi Brar 		break;
2415033f43cSJassi Brar 	}
2425033f43cSJassi Brar 
2435033f43cSJassi Brar 	writel(mod, i2s->addr + I2SMOD);
2445033f43cSJassi Brar }
2455033f43cSJassi Brar 
2465033f43cSJassi Brar /* Read Bit-Clock of I2S (in multiples of LRCLK) */
2475033f43cSJassi Brar static inline unsigned get_bfs(struct i2s_dai *i2s)
2485033f43cSJassi Brar {
2494ca0c0d4SPadmavathi Venna 	u32 bfs;
2504ca0c0d4SPadmavathi Venna 
2514ca0c0d4SPadmavathi Venna 	if (i2s->quirks & QUIRK_SUPPORTS_TDM) {
2524ca0c0d4SPadmavathi Venna 		bfs = readl(i2s->addr + I2SMOD) >> EXYNOS5420_MOD_BCLK_SHIFT;
2534ca0c0d4SPadmavathi Venna 		bfs &= EXYNOS5420_MOD_BCLK_MASK;
2544ca0c0d4SPadmavathi Venna 	} else {
2554ca0c0d4SPadmavathi Venna 		bfs =  readl(i2s->addr + I2SMOD) >> MOD_BCLK_SHIFT;
256b60be4aaSPadmavathi Venna 		bfs &= MOD_BCLK_MASK;
2574ca0c0d4SPadmavathi Venna 	}
2585033f43cSJassi Brar 
2595033f43cSJassi Brar 	switch (bfs) {
2604ca0c0d4SPadmavathi Venna 	case 8: return 256;
2614ca0c0d4SPadmavathi Venna 	case 7: return 192;
2624ca0c0d4SPadmavathi Venna 	case 6: return 128;
2634ca0c0d4SPadmavathi Venna 	case 5: return 96;
2644ca0c0d4SPadmavathi Venna 	case 4: return 64;
2655033f43cSJassi Brar 	case 3: return 24;
2665033f43cSJassi Brar 	case 2: return 16;
2675033f43cSJassi Brar 	case 1:	return 48;
2685033f43cSJassi Brar 	default: return 32;
2695033f43cSJassi Brar 	}
2705033f43cSJassi Brar }
2715033f43cSJassi Brar 
2725033f43cSJassi Brar /* Write Bit-Clock of I2S (in multiples of LRCLK) */
2735033f43cSJassi Brar static inline void set_bfs(struct i2s_dai *i2s, unsigned bfs)
2745033f43cSJassi Brar {
2755033f43cSJassi Brar 	u32 mod = readl(i2s->addr + I2SMOD);
2764ca0c0d4SPadmavathi Venna 	int bfs_shift;
2774ca0c0d4SPadmavathi Venna 	int tdm = i2s->quirks & QUIRK_SUPPORTS_TDM;
2785033f43cSJassi Brar 
2794ca0c0d4SPadmavathi Venna 	if (i2s->quirks & QUIRK_SUPPORTS_TDM) {
2804ca0c0d4SPadmavathi Venna 		bfs_shift = EXYNOS5420_MOD_BCLK_SHIFT;
2814ca0c0d4SPadmavathi Venna 		mod &= ~(EXYNOS5420_MOD_BCLK_MASK << bfs_shift);
2824ca0c0d4SPadmavathi Venna 	} else {
2834ca0c0d4SPadmavathi Venna 		bfs_shift = MOD_BCLK_SHIFT;
284b60be4aaSPadmavathi Venna 		mod &= ~(MOD_BCLK_MASK << bfs_shift);
2854ca0c0d4SPadmavathi Venna 	}
2864ca0c0d4SPadmavathi Venna 
2874ca0c0d4SPadmavathi Venna 	/* Non-TDM I2S controllers do not support BCLK > 48 * FS */
2884ca0c0d4SPadmavathi Venna 	if (!tdm && bfs > 48) {
2894ca0c0d4SPadmavathi Venna 		dev_err(&i2s->pdev->dev, "Unsupported BCLK divider\n");
2904ca0c0d4SPadmavathi Venna 		return;
2914ca0c0d4SPadmavathi Venna 	}
2925033f43cSJassi Brar 
2935033f43cSJassi Brar 	switch (bfs) {
2945033f43cSJassi Brar 	case 48:
295b60be4aaSPadmavathi Venna 		mod |= (MOD_BCLK_48FS << bfs_shift);
2965033f43cSJassi Brar 		break;
2975033f43cSJassi Brar 	case 32:
298b60be4aaSPadmavathi Venna 		mod |= (MOD_BCLK_32FS << bfs_shift);
2995033f43cSJassi Brar 		break;
3005033f43cSJassi Brar 	case 24:
301b60be4aaSPadmavathi Venna 		mod |= (MOD_BCLK_24FS << bfs_shift);
3025033f43cSJassi Brar 		break;
3035033f43cSJassi Brar 	case 16:
304b60be4aaSPadmavathi Venna 		mod |= (MOD_BCLK_16FS << bfs_shift);
3055033f43cSJassi Brar 		break;
3064ca0c0d4SPadmavathi Venna 	case 64:
3074ca0c0d4SPadmavathi Venna 		mod |= (EXYNOS5420_MOD_BCLK_64FS << bfs_shift);
3084ca0c0d4SPadmavathi Venna 		break;
3094ca0c0d4SPadmavathi Venna 	case 96:
3104ca0c0d4SPadmavathi Venna 		mod |= (EXYNOS5420_MOD_BCLK_96FS << bfs_shift);
3114ca0c0d4SPadmavathi Venna 		break;
3124ca0c0d4SPadmavathi Venna 	case 128:
3134ca0c0d4SPadmavathi Venna 		mod |= (EXYNOS5420_MOD_BCLK_128FS << bfs_shift);
3144ca0c0d4SPadmavathi Venna 		break;
3154ca0c0d4SPadmavathi Venna 	case 192:
3164ca0c0d4SPadmavathi Venna 		mod |= (EXYNOS5420_MOD_BCLK_192FS << bfs_shift);
3174ca0c0d4SPadmavathi Venna 		break;
3184ca0c0d4SPadmavathi Venna 	case 256:
3194ca0c0d4SPadmavathi Venna 		mod |= (EXYNOS5420_MOD_BCLK_256FS << bfs_shift);
3205033f43cSJassi Brar 		break;
3215033f43cSJassi Brar 	default:
3225033f43cSJassi Brar 		dev_err(&i2s->pdev->dev, "Wrong BCLK Divider!\n");
3235033f43cSJassi Brar 		return;
3245033f43cSJassi Brar 	}
3255033f43cSJassi Brar 
3265033f43cSJassi Brar 	writel(mod, i2s->addr + I2SMOD);
3275033f43cSJassi Brar }
3285033f43cSJassi Brar 
3295033f43cSJassi Brar /* Sample-Size */
3305033f43cSJassi Brar static inline int get_blc(struct i2s_dai *i2s)
3315033f43cSJassi Brar {
3325033f43cSJassi Brar 	int blc = readl(i2s->addr + I2SMOD);
3335033f43cSJassi Brar 
3345033f43cSJassi Brar 	blc = (blc >> 13) & 0x3;
3355033f43cSJassi Brar 
3365033f43cSJassi Brar 	switch (blc) {
3375033f43cSJassi Brar 	case 2: return 24;
3385033f43cSJassi Brar 	case 1:	return 8;
3395033f43cSJassi Brar 	default: return 16;
3405033f43cSJassi Brar 	}
3415033f43cSJassi Brar }
3425033f43cSJassi Brar 
3435033f43cSJassi Brar /* TX Channel Control */
3445033f43cSJassi Brar static void i2s_txctrl(struct i2s_dai *i2s, int on)
3455033f43cSJassi Brar {
3465033f43cSJassi Brar 	void __iomem *addr = i2s->addr;
3475033f43cSJassi Brar 	u32 con = readl(addr + I2SCON);
3485033f43cSJassi Brar 	u32 mod = readl(addr + I2SMOD) & ~MOD_MASK;
3495033f43cSJassi Brar 
3505033f43cSJassi Brar 	if (on) {
3515033f43cSJassi Brar 		con |= CON_ACTIVE;
3525033f43cSJassi Brar 		con &= ~CON_TXCH_PAUSE;
3535033f43cSJassi Brar 
3545033f43cSJassi Brar 		if (is_secondary(i2s)) {
3555033f43cSJassi Brar 			con |= CON_TXSDMA_ACTIVE;
3565033f43cSJassi Brar 			con &= ~CON_TXSDMA_PAUSE;
3575033f43cSJassi Brar 		} else {
3585033f43cSJassi Brar 			con |= CON_TXDMA_ACTIVE;
3595033f43cSJassi Brar 			con &= ~CON_TXDMA_PAUSE;
3605033f43cSJassi Brar 		}
3615033f43cSJassi Brar 
3625033f43cSJassi Brar 		if (any_rx_active(i2s))
3635033f43cSJassi Brar 			mod |= MOD_TXRX;
3645033f43cSJassi Brar 		else
3655033f43cSJassi Brar 			mod |= MOD_TXONLY;
3665033f43cSJassi Brar 	} else {
3675033f43cSJassi Brar 		if (is_secondary(i2s)) {
3685033f43cSJassi Brar 			con |=  CON_TXSDMA_PAUSE;
3695033f43cSJassi Brar 			con &= ~CON_TXSDMA_ACTIVE;
3705033f43cSJassi Brar 		} else {
3715033f43cSJassi Brar 			con |=  CON_TXDMA_PAUSE;
3725033f43cSJassi Brar 			con &= ~CON_TXDMA_ACTIVE;
3735033f43cSJassi Brar 		}
3745033f43cSJassi Brar 
3755033f43cSJassi Brar 		if (other_tx_active(i2s)) {
3765033f43cSJassi Brar 			writel(con, addr + I2SCON);
3775033f43cSJassi Brar 			return;
3785033f43cSJassi Brar 		}
3795033f43cSJassi Brar 
3805033f43cSJassi Brar 		con |=  CON_TXCH_PAUSE;
3815033f43cSJassi Brar 
3825033f43cSJassi Brar 		if (any_rx_active(i2s))
3835033f43cSJassi Brar 			mod |= MOD_RXONLY;
3845033f43cSJassi Brar 		else
3855033f43cSJassi Brar 			con &= ~CON_ACTIVE;
3865033f43cSJassi Brar 	}
3875033f43cSJassi Brar 
3885033f43cSJassi Brar 	writel(mod, addr + I2SMOD);
3895033f43cSJassi Brar 	writel(con, addr + I2SCON);
3905033f43cSJassi Brar }
3915033f43cSJassi Brar 
3925033f43cSJassi Brar /* RX Channel Control */
3935033f43cSJassi Brar static void i2s_rxctrl(struct i2s_dai *i2s, int on)
3945033f43cSJassi Brar {
3955033f43cSJassi Brar 	void __iomem *addr = i2s->addr;
3965033f43cSJassi Brar 	u32 con = readl(addr + I2SCON);
3975033f43cSJassi Brar 	u32 mod = readl(addr + I2SMOD) & ~MOD_MASK;
3985033f43cSJassi Brar 
3995033f43cSJassi Brar 	if (on) {
4005033f43cSJassi Brar 		con |= CON_RXDMA_ACTIVE | CON_ACTIVE;
4015033f43cSJassi Brar 		con &= ~(CON_RXDMA_PAUSE | CON_RXCH_PAUSE);
4025033f43cSJassi Brar 
4035033f43cSJassi Brar 		if (any_tx_active(i2s))
4045033f43cSJassi Brar 			mod |= MOD_TXRX;
4055033f43cSJassi Brar 		else
4065033f43cSJassi Brar 			mod |= MOD_RXONLY;
4075033f43cSJassi Brar 	} else {
4085033f43cSJassi Brar 		con |=  CON_RXDMA_PAUSE | CON_RXCH_PAUSE;
4095033f43cSJassi Brar 		con &= ~CON_RXDMA_ACTIVE;
4105033f43cSJassi Brar 
4115033f43cSJassi Brar 		if (any_tx_active(i2s))
4125033f43cSJassi Brar 			mod |= MOD_TXONLY;
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 /* Flush FIFO of an interface */
4225033f43cSJassi Brar static inline void i2s_fifo(struct i2s_dai *i2s, u32 flush)
4235033f43cSJassi Brar {
4245033f43cSJassi Brar 	void __iomem *fic;
4255033f43cSJassi Brar 	u32 val;
4265033f43cSJassi Brar 
4275033f43cSJassi Brar 	if (!i2s)
4285033f43cSJassi Brar 		return;
4295033f43cSJassi Brar 
4305033f43cSJassi Brar 	if (is_secondary(i2s))
4315033f43cSJassi Brar 		fic = i2s->addr + I2SFICS;
4325033f43cSJassi Brar 	else
4335033f43cSJassi Brar 		fic = i2s->addr + I2SFIC;
4345033f43cSJassi Brar 
4355033f43cSJassi Brar 	/* Flush the FIFO */
4365033f43cSJassi Brar 	writel(readl(fic) | flush, fic);
4375033f43cSJassi Brar 
4385033f43cSJassi Brar 	/* Be patient */
4395033f43cSJassi Brar 	val = msecs_to_loops(1) / 1000; /* 1 usec */
4405033f43cSJassi Brar 	while (--val)
4415033f43cSJassi Brar 		cpu_relax();
4425033f43cSJassi Brar 
4435033f43cSJassi Brar 	writel(readl(fic) & ~flush, fic);
4445033f43cSJassi Brar }
4455033f43cSJassi Brar 
4465033f43cSJassi Brar static int i2s_set_sysclk(struct snd_soc_dai *dai,
4475033f43cSJassi Brar 	  int clk_id, unsigned int rfs, int dir)
4485033f43cSJassi Brar {
4495033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
4505033f43cSJassi Brar 	struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
4515033f43cSJassi Brar 	u32 mod = readl(i2s->addr + I2SMOD);
4525033f43cSJassi Brar 
4535033f43cSJassi Brar 	switch (clk_id) {
454*c86d50f9SSylwester Nawrocki 	case SAMSUNG_I2S_OPCLK:
455*c86d50f9SSylwester Nawrocki 		mod &= ~MOD_OPCLK_MASK;
456*c86d50f9SSylwester Nawrocki 		mod |= dir;
457*c86d50f9SSylwester Nawrocki 		break;
4585033f43cSJassi Brar 	case SAMSUNG_I2S_CDCLK:
4595033f43cSJassi Brar 		/* Shouldn't matter in GATING(CLOCK_IN) mode */
4605033f43cSJassi Brar 		if (dir == SND_SOC_CLOCK_IN)
4615033f43cSJassi Brar 			rfs = 0;
4625033f43cSJassi Brar 
4635033f43cSJassi Brar 		if ((rfs && other->rfs && (other->rfs != rfs)) ||
4645033f43cSJassi Brar 				(any_active(i2s) &&
4655033f43cSJassi Brar 				(((dir == SND_SOC_CLOCK_IN)
4665033f43cSJassi Brar 					&& !(mod & MOD_CDCLKCON)) ||
4675033f43cSJassi Brar 				((dir == SND_SOC_CLOCK_OUT)
4685033f43cSJassi Brar 					&& (mod & MOD_CDCLKCON))))) {
4695033f43cSJassi Brar 			dev_err(&i2s->pdev->dev,
4705033f43cSJassi Brar 				"%s:%d Other DAI busy\n", __func__, __LINE__);
4715033f43cSJassi Brar 			return -EAGAIN;
4725033f43cSJassi Brar 		}
4735033f43cSJassi Brar 
4745033f43cSJassi Brar 		if (dir == SND_SOC_CLOCK_IN)
4755033f43cSJassi Brar 			mod |= MOD_CDCLKCON;
4765033f43cSJassi Brar 		else
4775033f43cSJassi Brar 			mod &= ~MOD_CDCLKCON;
4785033f43cSJassi Brar 
4795033f43cSJassi Brar 		i2s->rfs = rfs;
4805033f43cSJassi Brar 		break;
4815033f43cSJassi Brar 
4825033f43cSJassi Brar 	case SAMSUNG_I2S_RCLKSRC_0: /* clock corrsponding to IISMOD[10] := 0 */
4835033f43cSJassi Brar 	case SAMSUNG_I2S_RCLKSRC_1: /* clock corrsponding to IISMOD[10] := 1 */
4845033f43cSJassi Brar 		if ((i2s->quirks & QUIRK_NO_MUXPSR)
4855033f43cSJassi Brar 				|| (clk_id == SAMSUNG_I2S_RCLKSRC_0))
4865033f43cSJassi Brar 			clk_id = 0;
4875033f43cSJassi Brar 		else
4885033f43cSJassi Brar 			clk_id = 1;
4895033f43cSJassi Brar 
4905033f43cSJassi Brar 		if (!any_active(i2s)) {
4915033f43cSJassi Brar 			if (i2s->op_clk) {
4925033f43cSJassi Brar 				if ((clk_id && !(mod & MOD_IMS_SYSMUX)) ||
4935033f43cSJassi Brar 					(!clk_id && (mod & MOD_IMS_SYSMUX))) {
49498614cf6SThomas Abraham 					clk_disable_unprepare(i2s->op_clk);
4955033f43cSJassi Brar 					clk_put(i2s->op_clk);
4965033f43cSJassi Brar 				} else {
4976ce534aaSJassi Brar 					i2s->rclk_srcrate =
4986ce534aaSJassi Brar 						clk_get_rate(i2s->op_clk);
4995033f43cSJassi Brar 					return 0;
5005033f43cSJassi Brar 				}
5015033f43cSJassi Brar 			}
5025033f43cSJassi Brar 
5031974a042SPadmavathi Venna 			if (clk_id)
5045033f43cSJassi Brar 				i2s->op_clk = clk_get(&i2s->pdev->dev,
5051974a042SPadmavathi Venna 						"i2s_opclk1");
5061974a042SPadmavathi Venna 			else
5071974a042SPadmavathi Venna 				i2s->op_clk = clk_get(&i2s->pdev->dev,
5081974a042SPadmavathi Venna 						"i2s_opclk0");
50998614cf6SThomas Abraham 			clk_prepare_enable(i2s->op_clk);
5105033f43cSJassi Brar 			i2s->rclk_srcrate = clk_get_rate(i2s->op_clk);
5115033f43cSJassi Brar 
5125033f43cSJassi Brar 			/* Over-ride the other's */
5135033f43cSJassi Brar 			if (other) {
5145033f43cSJassi Brar 				other->op_clk = i2s->op_clk;
5155033f43cSJassi Brar 				other->rclk_srcrate = i2s->rclk_srcrate;
5165033f43cSJassi Brar 			}
5175033f43cSJassi Brar 		} else if ((!clk_id && (mod & MOD_IMS_SYSMUX))
5185033f43cSJassi Brar 				|| (clk_id && !(mod & MOD_IMS_SYSMUX))) {
5195033f43cSJassi Brar 			dev_err(&i2s->pdev->dev,
5205033f43cSJassi Brar 				"%s:%d Other DAI busy\n", __func__, __LINE__);
5215033f43cSJassi Brar 			return -EAGAIN;
5225033f43cSJassi Brar 		} else {
5235033f43cSJassi Brar 			/* Call can't be on the active DAI */
5245033f43cSJassi Brar 			i2s->op_clk = other->op_clk;
5255033f43cSJassi Brar 			i2s->rclk_srcrate = other->rclk_srcrate;
5265033f43cSJassi Brar 			return 0;
5275033f43cSJassi Brar 		}
5285033f43cSJassi Brar 
5295033f43cSJassi Brar 		if (clk_id == 0)
5305033f43cSJassi Brar 			mod &= ~MOD_IMS_SYSMUX;
5315033f43cSJassi Brar 		else
5325033f43cSJassi Brar 			mod |= MOD_IMS_SYSMUX;
5335033f43cSJassi Brar 		break;
5345033f43cSJassi Brar 
5355033f43cSJassi Brar 	default:
5365033f43cSJassi Brar 		dev_err(&i2s->pdev->dev, "We don't serve that!\n");
5375033f43cSJassi Brar 		return -EINVAL;
5385033f43cSJassi Brar 	}
5395033f43cSJassi Brar 
5405033f43cSJassi Brar 	writel(mod, i2s->addr + I2SMOD);
5415033f43cSJassi Brar 
5425033f43cSJassi Brar 	return 0;
5435033f43cSJassi Brar }
5445033f43cSJassi Brar 
5455033f43cSJassi Brar static int i2s_set_fmt(struct snd_soc_dai *dai,
5465033f43cSJassi Brar 	unsigned int fmt)
5475033f43cSJassi Brar {
5485033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
5495033f43cSJassi Brar 	u32 mod = readl(i2s->addr + I2SMOD);
5504ca0c0d4SPadmavathi Venna 	int lrp_shift, sdf_shift, sdf_mask, lrp_rlow;
5515033f43cSJassi Brar 	u32 tmp = 0;
5525033f43cSJassi Brar 
5534ca0c0d4SPadmavathi Venna 	if (i2s->quirks & QUIRK_SUPPORTS_TDM) {
5544ca0c0d4SPadmavathi Venna 		lrp_shift = EXYNOS5420_MOD_LRP_SHIFT;
5554ca0c0d4SPadmavathi Venna 		sdf_shift = EXYNOS5420_MOD_SDF_SHIFT;
5564ca0c0d4SPadmavathi Venna 	} else {
5574ca0c0d4SPadmavathi Venna 		lrp_shift = MOD_LRP_SHIFT;
5584ca0c0d4SPadmavathi Venna 		sdf_shift = MOD_SDF_SHIFT;
5594ca0c0d4SPadmavathi Venna 	}
5604ca0c0d4SPadmavathi Venna 
561b60be4aaSPadmavathi Venna 	sdf_mask = MOD_SDF_MASK << sdf_shift;
562b60be4aaSPadmavathi Venna 	lrp_rlow = MOD_LR_RLOW << lrp_shift;
563b60be4aaSPadmavathi Venna 
5645033f43cSJassi Brar 	/* Format is priority */
5655033f43cSJassi Brar 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
5665033f43cSJassi Brar 	case SND_SOC_DAIFMT_RIGHT_J:
567b60be4aaSPadmavathi Venna 		tmp |= lrp_rlow;
568b60be4aaSPadmavathi Venna 		tmp |= (MOD_SDF_MSB << sdf_shift);
5695033f43cSJassi Brar 		break;
5705033f43cSJassi Brar 	case SND_SOC_DAIFMT_LEFT_J:
571b60be4aaSPadmavathi Venna 		tmp |= lrp_rlow;
572b60be4aaSPadmavathi Venna 		tmp |= (MOD_SDF_LSB << sdf_shift);
5735033f43cSJassi Brar 		break;
5745033f43cSJassi Brar 	case SND_SOC_DAIFMT_I2S:
575b60be4aaSPadmavathi Venna 		tmp |= (MOD_SDF_IIS << sdf_shift);
5765033f43cSJassi Brar 		break;
5775033f43cSJassi Brar 	default:
5785033f43cSJassi Brar 		dev_err(&i2s->pdev->dev, "Format not supported\n");
5795033f43cSJassi Brar 		return -EINVAL;
5805033f43cSJassi Brar 	}
5815033f43cSJassi Brar 
5825033f43cSJassi Brar 	/*
5835033f43cSJassi Brar 	 * INV flag is relative to the FORMAT flag - if set it simply
5845033f43cSJassi Brar 	 * flips the polarity specified by the Standard
5855033f43cSJassi Brar 	 */
5865033f43cSJassi Brar 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
5875033f43cSJassi Brar 	case SND_SOC_DAIFMT_NB_NF:
5885033f43cSJassi Brar 		break;
5895033f43cSJassi Brar 	case SND_SOC_DAIFMT_NB_IF:
590b60be4aaSPadmavathi Venna 		if (tmp & lrp_rlow)
591b60be4aaSPadmavathi Venna 			tmp &= ~lrp_rlow;
5925033f43cSJassi Brar 		else
593b60be4aaSPadmavathi Venna 			tmp |= lrp_rlow;
5945033f43cSJassi Brar 		break;
5955033f43cSJassi Brar 	default:
5965033f43cSJassi Brar 		dev_err(&i2s->pdev->dev, "Polarity not supported\n");
5975033f43cSJassi Brar 		return -EINVAL;
5985033f43cSJassi Brar 	}
5995033f43cSJassi Brar 
6005033f43cSJassi Brar 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
6015033f43cSJassi Brar 	case SND_SOC_DAIFMT_CBM_CFM:
6025033f43cSJassi Brar 		tmp |= MOD_SLAVE;
6035033f43cSJassi Brar 		break;
6045033f43cSJassi Brar 	case SND_SOC_DAIFMT_CBS_CFS:
6055033f43cSJassi Brar 		/* Set default source clock in Master mode */
6065033f43cSJassi Brar 		if (i2s->rclk_srcrate == 0)
6075033f43cSJassi Brar 			i2s_set_sysclk(dai, SAMSUNG_I2S_RCLKSRC_0,
6085033f43cSJassi Brar 							0, SND_SOC_CLOCK_IN);
6095033f43cSJassi Brar 		break;
6105033f43cSJassi Brar 	default:
6115033f43cSJassi Brar 		dev_err(&i2s->pdev->dev, "master/slave format not supported\n");
6125033f43cSJassi Brar 		return -EINVAL;
6135033f43cSJassi Brar 	}
6145033f43cSJassi Brar 
615b60be4aaSPadmavathi Venna 	/*
616b60be4aaSPadmavathi Venna 	 * Don't change the I2S mode if any controller is active on this
617b60be4aaSPadmavathi Venna 	 * channel.
618b60be4aaSPadmavathi Venna 	 */
6195033f43cSJassi Brar 	if (any_active(i2s) &&
620b60be4aaSPadmavathi Venna 		((mod & (sdf_mask | lrp_rlow | MOD_SLAVE)) != tmp)) {
6215033f43cSJassi Brar 		dev_err(&i2s->pdev->dev,
6225033f43cSJassi Brar 				"%s:%d Other DAI busy\n", __func__, __LINE__);
6235033f43cSJassi Brar 		return -EAGAIN;
6245033f43cSJassi Brar 	}
6255033f43cSJassi Brar 
626b60be4aaSPadmavathi Venna 	mod &= ~(sdf_mask | lrp_rlow | MOD_SLAVE);
6275033f43cSJassi Brar 	mod |= tmp;
6285033f43cSJassi Brar 	writel(mod, i2s->addr + I2SMOD);
6295033f43cSJassi Brar 
6305033f43cSJassi Brar 	return 0;
6315033f43cSJassi Brar }
6325033f43cSJassi Brar 
6335033f43cSJassi Brar static int i2s_hw_params(struct snd_pcm_substream *substream,
6345033f43cSJassi Brar 	struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
6355033f43cSJassi Brar {
6365033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
6375033f43cSJassi Brar 	u32 mod = readl(i2s->addr + I2SMOD);
6385033f43cSJassi Brar 
6395033f43cSJassi Brar 	if (!is_secondary(i2s))
6405033f43cSJassi Brar 		mod &= ~(MOD_DC2_EN | MOD_DC1_EN);
6415033f43cSJassi Brar 
6425033f43cSJassi Brar 	switch (params_channels(params)) {
6435033f43cSJassi Brar 	case 6:
6445033f43cSJassi Brar 		mod |= MOD_DC2_EN;
6455033f43cSJassi Brar 	case 4:
6465033f43cSJassi Brar 		mod |= MOD_DC1_EN;
6475033f43cSJassi Brar 		break;
6485033f43cSJassi Brar 	case 2:
649588fb705SSangsu Park 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
650588fb705SSangsu Park 			i2s->dma_playback.dma_size = 4;
651588fb705SSangsu Park 		else
652588fb705SSangsu Park 			i2s->dma_capture.dma_size = 4;
653588fb705SSangsu Park 		break;
654588fb705SSangsu Park 	case 1:
655588fb705SSangsu Park 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
656588fb705SSangsu Park 			i2s->dma_playback.dma_size = 2;
657588fb705SSangsu Park 		else
658588fb705SSangsu Park 			i2s->dma_capture.dma_size = 2;
659588fb705SSangsu Park 
6605033f43cSJassi Brar 		break;
6615033f43cSJassi Brar 	default:
6625033f43cSJassi Brar 		dev_err(&i2s->pdev->dev, "%d channels not supported\n",
6635033f43cSJassi Brar 				params_channels(params));
6645033f43cSJassi Brar 		return -EINVAL;
6655033f43cSJassi Brar 	}
6665033f43cSJassi Brar 
6675033f43cSJassi Brar 	if (is_secondary(i2s))
6685033f43cSJassi Brar 		mod &= ~MOD_BLCS_MASK;
6695033f43cSJassi Brar 	else
6705033f43cSJassi Brar 		mod &= ~MOD_BLCP_MASK;
6715033f43cSJassi Brar 
6725033f43cSJassi Brar 	if (is_manager(i2s))
6735033f43cSJassi Brar 		mod &= ~MOD_BLC_MASK;
6745033f43cSJassi Brar 
6755033f43cSJassi Brar 	switch (params_format(params)) {
6765033f43cSJassi Brar 	case SNDRV_PCM_FORMAT_S8:
6775033f43cSJassi Brar 		if (is_secondary(i2s))
6785033f43cSJassi Brar 			mod |= MOD_BLCS_8BIT;
6795033f43cSJassi Brar 		else
6805033f43cSJassi Brar 			mod |= MOD_BLCP_8BIT;
6815033f43cSJassi Brar 		if (is_manager(i2s))
6825033f43cSJassi Brar 			mod |= MOD_BLC_8BIT;
6835033f43cSJassi Brar 		break;
6845033f43cSJassi Brar 	case SNDRV_PCM_FORMAT_S16_LE:
6855033f43cSJassi Brar 		if (is_secondary(i2s))
6865033f43cSJassi Brar 			mod |= MOD_BLCS_16BIT;
6875033f43cSJassi Brar 		else
6885033f43cSJassi Brar 			mod |= MOD_BLCP_16BIT;
6895033f43cSJassi Brar 		if (is_manager(i2s))
6905033f43cSJassi Brar 			mod |= MOD_BLC_16BIT;
6915033f43cSJassi Brar 		break;
6925033f43cSJassi Brar 	case SNDRV_PCM_FORMAT_S24_LE:
6935033f43cSJassi Brar 		if (is_secondary(i2s))
6945033f43cSJassi Brar 			mod |= MOD_BLCS_24BIT;
6955033f43cSJassi Brar 		else
6965033f43cSJassi Brar 			mod |= MOD_BLCP_24BIT;
6975033f43cSJassi Brar 		if (is_manager(i2s))
6985033f43cSJassi Brar 			mod |= MOD_BLC_24BIT;
6995033f43cSJassi Brar 		break;
7005033f43cSJassi Brar 	default:
7015033f43cSJassi Brar 		dev_err(&i2s->pdev->dev, "Format(%d) not supported\n",
7025033f43cSJassi Brar 				params_format(params));
7035033f43cSJassi Brar 		return -EINVAL;
7045033f43cSJassi Brar 	}
7055033f43cSJassi Brar 	writel(mod, i2s->addr + I2SMOD);
7065033f43cSJassi Brar 
707d37bdf73SMark Brown 	samsung_asoc_init_dma_data(dai, &i2s->dma_playback, &i2s->dma_capture);
708d37bdf73SMark Brown 
7095033f43cSJassi Brar 	i2s->frmclk = params_rate(params);
7105033f43cSJassi Brar 
7115033f43cSJassi Brar 	return 0;
7125033f43cSJassi Brar }
7135033f43cSJassi Brar 
7145033f43cSJassi Brar /* We set constraints on the substream acc to the version of I2S */
7155033f43cSJassi Brar static int i2s_startup(struct snd_pcm_substream *substream,
7165033f43cSJassi Brar 	  struct snd_soc_dai *dai)
7175033f43cSJassi Brar {
7185033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
7195033f43cSJassi Brar 	struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
7205033f43cSJassi Brar 	unsigned long flags;
7215033f43cSJassi Brar 
7225033f43cSJassi Brar 	spin_lock_irqsave(&lock, flags);
7235033f43cSJassi Brar 
7245033f43cSJassi Brar 	i2s->mode |= DAI_OPENED;
7255033f43cSJassi Brar 
7265033f43cSJassi Brar 	if (is_manager(other))
7275033f43cSJassi Brar 		i2s->mode &= ~DAI_MANAGER;
7285033f43cSJassi Brar 	else
7295033f43cSJassi Brar 		i2s->mode |= DAI_MANAGER;
7305033f43cSJassi Brar 
7312d77828dSPadmavathi Venna 	if (!any_active(i2s) && (i2s->quirks & QUIRK_NEED_RSTCLR))
7322d77828dSPadmavathi Venna 		writel(CON_RSTCLR, i2s->addr + I2SCON);
7332d77828dSPadmavathi Venna 
7345033f43cSJassi Brar 	spin_unlock_irqrestore(&lock, flags);
7355033f43cSJassi Brar 
7365033f43cSJassi Brar 	return 0;
7375033f43cSJassi Brar }
7385033f43cSJassi Brar 
7395033f43cSJassi Brar static void i2s_shutdown(struct snd_pcm_substream *substream,
7405033f43cSJassi Brar 	struct snd_soc_dai *dai)
7415033f43cSJassi Brar {
7425033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
7435033f43cSJassi Brar 	struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
7445033f43cSJassi Brar 	unsigned long flags;
7455033f43cSJassi Brar 
7465033f43cSJassi Brar 	spin_lock_irqsave(&lock, flags);
7475033f43cSJassi Brar 
7485033f43cSJassi Brar 	i2s->mode &= ~DAI_OPENED;
7495033f43cSJassi Brar 	i2s->mode &= ~DAI_MANAGER;
7505033f43cSJassi Brar 
7515033f43cSJassi Brar 	if (is_opened(other))
7525033f43cSJassi Brar 		other->mode |= DAI_MANAGER;
7535033f43cSJassi Brar 
7545033f43cSJassi Brar 	/* Reset any constraint on RFS and BFS */
7555033f43cSJassi Brar 	i2s->rfs = 0;
7565033f43cSJassi Brar 	i2s->bfs = 0;
7575033f43cSJassi Brar 
7585033f43cSJassi Brar 	spin_unlock_irqrestore(&lock, flags);
7595033f43cSJassi Brar 
7605033f43cSJassi Brar 	/* Gate CDCLK by default */
7615033f43cSJassi Brar 	if (!is_opened(other))
7625033f43cSJassi Brar 		i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK,
7635033f43cSJassi Brar 				0, SND_SOC_CLOCK_IN);
7645033f43cSJassi Brar }
7655033f43cSJassi Brar 
7665033f43cSJassi Brar static int config_setup(struct i2s_dai *i2s)
7675033f43cSJassi Brar {
7685033f43cSJassi Brar 	struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
7695033f43cSJassi Brar 	unsigned rfs, bfs, blc;
7705033f43cSJassi Brar 	u32 psr;
7715033f43cSJassi Brar 
7725033f43cSJassi Brar 	blc = get_blc(i2s);
7735033f43cSJassi Brar 
7745033f43cSJassi Brar 	bfs = i2s->bfs;
7755033f43cSJassi Brar 
7765033f43cSJassi Brar 	if (!bfs && other)
7775033f43cSJassi Brar 		bfs = other->bfs;
7785033f43cSJassi Brar 
7795033f43cSJassi Brar 	/* Select least possible multiple(2) if no constraint set */
7805033f43cSJassi Brar 	if (!bfs)
7815033f43cSJassi Brar 		bfs = blc * 2;
7825033f43cSJassi Brar 
7835033f43cSJassi Brar 	rfs = i2s->rfs;
7845033f43cSJassi Brar 
7855033f43cSJassi Brar 	if (!rfs && other)
7865033f43cSJassi Brar 		rfs = other->rfs;
7875033f43cSJassi Brar 
7885033f43cSJassi Brar 	if ((rfs == 256 || rfs == 512) && (blc == 24)) {
7895033f43cSJassi Brar 		dev_err(&i2s->pdev->dev,
7905033f43cSJassi Brar 			"%d-RFS not supported for 24-blc\n", rfs);
7915033f43cSJassi Brar 		return -EINVAL;
7925033f43cSJassi Brar 	}
7935033f43cSJassi Brar 
7945033f43cSJassi Brar 	if (!rfs) {
7955033f43cSJassi Brar 		if (bfs == 16 || bfs == 32)
7965033f43cSJassi Brar 			rfs = 256;
7975033f43cSJassi Brar 		else
7985033f43cSJassi Brar 			rfs = 384;
7995033f43cSJassi Brar 	}
8005033f43cSJassi Brar 
8015033f43cSJassi Brar 	/* If already setup and running */
8025033f43cSJassi Brar 	if (any_active(i2s) && (get_rfs(i2s) != rfs || get_bfs(i2s) != bfs)) {
8035033f43cSJassi Brar 		dev_err(&i2s->pdev->dev,
8045033f43cSJassi Brar 				"%s:%d Other DAI busy\n", __func__, __LINE__);
8055033f43cSJassi Brar 		return -EAGAIN;
8065033f43cSJassi Brar 	}
8075033f43cSJassi Brar 
8085033f43cSJassi Brar 	set_bfs(i2s, bfs);
8095033f43cSJassi Brar 	set_rfs(i2s, rfs);
8105033f43cSJassi Brar 
81177010010SPadmavathi Venna 	/* Don't bother with PSR in Slave mode */
81277010010SPadmavathi Venna 	if (is_slave(i2s))
81377010010SPadmavathi Venna 		return 0;
81477010010SPadmavathi Venna 
8155033f43cSJassi Brar 	if (!(i2s->quirks & QUIRK_NO_MUXPSR)) {
8165033f43cSJassi Brar 		psr = i2s->rclk_srcrate / i2s->frmclk / rfs;
8175033f43cSJassi Brar 		writel(((psr - 1) << 8) | PSR_PSREN, i2s->addr + I2SPSR);
8185033f43cSJassi Brar 		dev_dbg(&i2s->pdev->dev,
8195033f43cSJassi Brar 			"RCLK_SRC=%luHz PSR=%u, RCLK=%dfs, BCLK=%dfs\n",
8205033f43cSJassi Brar 				i2s->rclk_srcrate, psr, rfs, bfs);
8215033f43cSJassi Brar 	}
8225033f43cSJassi Brar 
8235033f43cSJassi Brar 	return 0;
8245033f43cSJassi Brar }
8255033f43cSJassi Brar 
8265033f43cSJassi Brar static int i2s_trigger(struct snd_pcm_substream *substream,
8275033f43cSJassi Brar 	int cmd, struct snd_soc_dai *dai)
8285033f43cSJassi Brar {
8295033f43cSJassi Brar 	int capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
8305033f43cSJassi Brar 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
8315033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(rtd->cpu_dai);
8325033f43cSJassi Brar 	unsigned long flags;
8335033f43cSJassi Brar 
8345033f43cSJassi Brar 	switch (cmd) {
8355033f43cSJassi Brar 	case SNDRV_PCM_TRIGGER_START:
8365033f43cSJassi Brar 	case SNDRV_PCM_TRIGGER_RESUME:
8375033f43cSJassi Brar 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
8385033f43cSJassi Brar 		local_irq_save(flags);
8395033f43cSJassi Brar 
8405033f43cSJassi Brar 		if (config_setup(i2s)) {
8415033f43cSJassi Brar 			local_irq_restore(flags);
8425033f43cSJassi Brar 			return -EINVAL;
8435033f43cSJassi Brar 		}
8445033f43cSJassi Brar 
8455033f43cSJassi Brar 		if (capture)
8465033f43cSJassi Brar 			i2s_rxctrl(i2s, 1);
8475033f43cSJassi Brar 		else
8485033f43cSJassi Brar 			i2s_txctrl(i2s, 1);
8495033f43cSJassi Brar 
8505033f43cSJassi Brar 		local_irq_restore(flags);
8515033f43cSJassi Brar 		break;
8525033f43cSJassi Brar 	case SNDRV_PCM_TRIGGER_STOP:
8535033f43cSJassi Brar 	case SNDRV_PCM_TRIGGER_SUSPEND:
8545033f43cSJassi Brar 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
8555033f43cSJassi Brar 		local_irq_save(flags);
8565033f43cSJassi Brar 
857c90887feSJassi Brar 		if (capture) {
8585033f43cSJassi Brar 			i2s_rxctrl(i2s, 0);
859775bc971SJassi Brar 			i2s_fifo(i2s, FIC_RXFLUSH);
860c90887feSJassi Brar 		} else {
861c90887feSJassi Brar 			i2s_txctrl(i2s, 0);
862775bc971SJassi Brar 			i2s_fifo(i2s, FIC_TXFLUSH);
863c90887feSJassi Brar 		}
864775bc971SJassi Brar 
8655033f43cSJassi Brar 		local_irq_restore(flags);
8665033f43cSJassi Brar 		break;
8675033f43cSJassi Brar 	}
8685033f43cSJassi Brar 
8695033f43cSJassi Brar 	return 0;
8705033f43cSJassi Brar }
8715033f43cSJassi Brar 
8725033f43cSJassi Brar static int i2s_set_clkdiv(struct snd_soc_dai *dai,
8735033f43cSJassi Brar 	int div_id, int div)
8745033f43cSJassi Brar {
8755033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
8765033f43cSJassi Brar 	struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
8775033f43cSJassi Brar 
8785033f43cSJassi Brar 	switch (div_id) {
8795033f43cSJassi Brar 	case SAMSUNG_I2S_DIV_BCLK:
8805033f43cSJassi Brar 		if ((any_active(i2s) && div && (get_bfs(i2s) != div))
8815033f43cSJassi Brar 			|| (other && other->bfs && (other->bfs != div))) {
8825033f43cSJassi Brar 			dev_err(&i2s->pdev->dev,
8835033f43cSJassi Brar 				"%s:%d Other DAI busy\n", __func__, __LINE__);
8845033f43cSJassi Brar 			return -EAGAIN;
8855033f43cSJassi Brar 		}
8865033f43cSJassi Brar 		i2s->bfs = div;
8875033f43cSJassi Brar 		break;
8885033f43cSJassi Brar 	default:
8895033f43cSJassi Brar 		dev_err(&i2s->pdev->dev,
8905033f43cSJassi Brar 			"Invalid clock divider(%d)\n", div_id);
8915033f43cSJassi Brar 		return -EINVAL;
8925033f43cSJassi Brar 	}
8935033f43cSJassi Brar 
8945033f43cSJassi Brar 	return 0;
8955033f43cSJassi Brar }
8965033f43cSJassi Brar 
8975033f43cSJassi Brar static snd_pcm_sframes_t
8985033f43cSJassi Brar i2s_delay(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
8995033f43cSJassi Brar {
9005033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
9015033f43cSJassi Brar 	u32 reg = readl(i2s->addr + I2SFIC);
9025033f43cSJassi Brar 	snd_pcm_sframes_t delay;
9035033f43cSJassi Brar 
9045033f43cSJassi Brar 	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
9055033f43cSJassi Brar 		delay = FIC_RXCOUNT(reg);
9065033f43cSJassi Brar 	else if (is_secondary(i2s))
9075033f43cSJassi Brar 		delay = FICS_TXCOUNT(readl(i2s->addr + I2SFICS));
9085033f43cSJassi Brar 	else
9095033f43cSJassi Brar 		delay = FIC_TXCOUNT(reg);
9105033f43cSJassi Brar 
9115033f43cSJassi Brar 	return delay;
9125033f43cSJassi Brar }
9135033f43cSJassi Brar 
9145033f43cSJassi Brar #ifdef CONFIG_PM
9155033f43cSJassi Brar static int i2s_suspend(struct snd_soc_dai *dai)
9165033f43cSJassi Brar {
9175033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
9185033f43cSJassi Brar 
9195033f43cSJassi Brar 	if (dai->active) {
9205033f43cSJassi Brar 		i2s->suspend_i2smod = readl(i2s->addr + I2SMOD);
9215033f43cSJassi Brar 		i2s->suspend_i2scon = readl(i2s->addr + I2SCON);
9225033f43cSJassi Brar 		i2s->suspend_i2spsr = readl(i2s->addr + I2SPSR);
9235033f43cSJassi Brar 	}
9245033f43cSJassi Brar 
9255033f43cSJassi Brar 	return 0;
9265033f43cSJassi Brar }
9275033f43cSJassi Brar 
9285033f43cSJassi Brar static int i2s_resume(struct snd_soc_dai *dai)
9295033f43cSJassi Brar {
9305033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
9315033f43cSJassi Brar 
9325033f43cSJassi Brar 	if (dai->active) {
9335033f43cSJassi Brar 		writel(i2s->suspend_i2scon, i2s->addr + I2SCON);
9345033f43cSJassi Brar 		writel(i2s->suspend_i2smod, i2s->addr + I2SMOD);
9355033f43cSJassi Brar 		writel(i2s->suspend_i2spsr, i2s->addr + I2SPSR);
9365033f43cSJassi Brar 	}
9375033f43cSJassi Brar 
9385033f43cSJassi Brar 	return 0;
9395033f43cSJassi Brar }
9405033f43cSJassi Brar #else
9415033f43cSJassi Brar #define i2s_suspend NULL
9425033f43cSJassi Brar #define i2s_resume  NULL
9435033f43cSJassi Brar #endif
9445033f43cSJassi Brar 
9455033f43cSJassi Brar static int samsung_i2s_dai_probe(struct snd_soc_dai *dai)
9465033f43cSJassi Brar {
9475033f43cSJassi Brar 	struct i2s_dai *i2s = to_info(dai);
9485033f43cSJassi Brar 	struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
9495033f43cSJassi Brar 
9503688569eSMark Brown 	if (other && other->clk) { /* If this is probe on secondary */
9513688569eSMark Brown 		samsung_asoc_init_dma_data(dai, &other->sec_dai->dma_playback,
9523688569eSMark Brown 					   NULL);
9535033f43cSJassi Brar 		goto probe_exit;
9543688569eSMark Brown 	}
9555033f43cSJassi Brar 
9565033f43cSJassi Brar 	i2s->addr = ioremap(i2s->base, 0x100);
9575033f43cSJassi Brar 	if (i2s->addr == NULL) {
9585033f43cSJassi Brar 		dev_err(&i2s->pdev->dev, "cannot ioremap registers\n");
9595033f43cSJassi Brar 		return -ENXIO;
9605033f43cSJassi Brar 	}
9615033f43cSJassi Brar 
9625033f43cSJassi Brar 	i2s->clk = clk_get(&i2s->pdev->dev, "iis");
9635033f43cSJassi Brar 	if (IS_ERR(i2s->clk)) {
9645033f43cSJassi Brar 		dev_err(&i2s->pdev->dev, "failed to get i2s_clock\n");
9655033f43cSJassi Brar 		iounmap(i2s->addr);
9665033f43cSJassi Brar 		return -ENOENT;
9675033f43cSJassi Brar 	}
96898614cf6SThomas Abraham 	clk_prepare_enable(i2s->clk);
9695033f43cSJassi Brar 
9703688569eSMark Brown 	samsung_asoc_init_dma_data(dai, &i2s->dma_playback, &i2s->dma_capture);
971511e3033SMark Brown 
9725033f43cSJassi Brar 	if (other) {
9735033f43cSJassi Brar 		other->addr = i2s->addr;
9745033f43cSJassi Brar 		other->clk = i2s->clk;
9755033f43cSJassi Brar 	}
9765033f43cSJassi Brar 
9775033f43cSJassi Brar 	if (i2s->quirks & QUIRK_NEED_RSTCLR)
9785033f43cSJassi Brar 		writel(CON_RSTCLR, i2s->addr + I2SCON);
9795033f43cSJassi Brar 
98061100f40SSangbeom Kim 	if (i2s->quirks & QUIRK_SEC_DAI)
9819b8f5695SMark Brown 		idma_reg_addr_init(i2s->addr,
98261100f40SSangbeom Kim 					i2s->sec_dai->idma_playback.dma_addr);
98361100f40SSangbeom Kim 
9845033f43cSJassi Brar probe_exit:
9855033f43cSJassi Brar 	/* Reset any constraint on RFS and BFS */
9865033f43cSJassi Brar 	i2s->rfs = 0;
9875033f43cSJassi Brar 	i2s->bfs = 0;
988d66eac3eSTushar Behera 	i2s->rclk_srcrate = 0;
9895033f43cSJassi Brar 	i2s_txctrl(i2s, 0);
9905033f43cSJassi Brar 	i2s_rxctrl(i2s, 0);
9915033f43cSJassi Brar 	i2s_fifo(i2s, FIC_TXFLUSH);
9925033f43cSJassi Brar 	i2s_fifo(other, FIC_TXFLUSH);
9935033f43cSJassi Brar 	i2s_fifo(i2s, FIC_RXFLUSH);
9945033f43cSJassi Brar 
9955033f43cSJassi Brar 	/* Gate CDCLK by default */
9965033f43cSJassi Brar 	if (!is_opened(other))
9975033f43cSJassi Brar 		i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK,
9985033f43cSJassi Brar 				0, SND_SOC_CLOCK_IN);
9995033f43cSJassi Brar 
10005033f43cSJassi Brar 	return 0;
10015033f43cSJassi Brar }
10025033f43cSJassi Brar 
10035033f43cSJassi Brar static int samsung_i2s_dai_remove(struct snd_soc_dai *dai)
10045033f43cSJassi Brar {
10055033f43cSJassi Brar 	struct i2s_dai *i2s = snd_soc_dai_get_drvdata(dai);
10065033f43cSJassi Brar 	struct i2s_dai *other = i2s->pri_dai ? : i2s->sec_dai;
10075033f43cSJassi Brar 
10085033f43cSJassi Brar 	if (!other || !other->clk) {
10095033f43cSJassi Brar 
10105033f43cSJassi Brar 		if (i2s->quirks & QUIRK_NEED_RSTCLR)
10115033f43cSJassi Brar 			writel(0, i2s->addr + I2SCON);
10125033f43cSJassi Brar 
101398614cf6SThomas Abraham 		clk_disable_unprepare(i2s->clk);
10145033f43cSJassi Brar 		clk_put(i2s->clk);
10155033f43cSJassi Brar 
10165033f43cSJassi Brar 		iounmap(i2s->addr);
10175033f43cSJassi Brar 	}
10185033f43cSJassi Brar 
10195033f43cSJassi Brar 	i2s->clk = NULL;
10205033f43cSJassi Brar 
10215033f43cSJassi Brar 	return 0;
10225033f43cSJassi Brar }
10235033f43cSJassi Brar 
102485e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops samsung_i2s_dai_ops = {
10255033f43cSJassi Brar 	.trigger = i2s_trigger,
10265033f43cSJassi Brar 	.hw_params = i2s_hw_params,
10275033f43cSJassi Brar 	.set_fmt = i2s_set_fmt,
10285033f43cSJassi Brar 	.set_clkdiv = i2s_set_clkdiv,
10295033f43cSJassi Brar 	.set_sysclk = i2s_set_sysclk,
10305033f43cSJassi Brar 	.startup = i2s_startup,
10315033f43cSJassi Brar 	.shutdown = i2s_shutdown,
10325033f43cSJassi Brar 	.delay = i2s_delay,
10335033f43cSJassi Brar };
10345033f43cSJassi Brar 
10354b828535SKuninori Morimoto static const struct snd_soc_component_driver samsung_i2s_component = {
10364b828535SKuninori Morimoto 	.name		= "samsung-i2s",
10374b828535SKuninori Morimoto };
10384b828535SKuninori Morimoto 
10395033f43cSJassi Brar #define SAMSUNG_I2S_RATES	SNDRV_PCM_RATE_8000_96000
10405033f43cSJassi Brar 
10415033f43cSJassi Brar #define SAMSUNG_I2S_FMTS	(SNDRV_PCM_FMTBIT_S8 | \
10425033f43cSJassi Brar 					SNDRV_PCM_FMTBIT_S16_LE | \
10435033f43cSJassi Brar 					SNDRV_PCM_FMTBIT_S24_LE)
10445033f43cSJassi Brar 
1045fdca21adSBill Pemberton static struct i2s_dai *i2s_alloc_dai(struct platform_device *pdev, bool sec)
10465033f43cSJassi Brar {
10475033f43cSJassi Brar 	struct i2s_dai *i2s;
1048c6f9b1ebSPrathyush K 	int ret;
10495033f43cSJassi Brar 
1050b960ce74SMark Brown 	i2s = devm_kzalloc(&pdev->dev, sizeof(struct i2s_dai), GFP_KERNEL);
10515033f43cSJassi Brar 	if (i2s == NULL)
10525033f43cSJassi Brar 		return NULL;
10535033f43cSJassi Brar 
10545033f43cSJassi Brar 	i2s->pdev = pdev;
10555033f43cSJassi Brar 	i2s->pri_dai = NULL;
10565033f43cSJassi Brar 	i2s->sec_dai = NULL;
10575033f43cSJassi Brar 	i2s->i2s_dai_drv.symmetric_rates = 1;
10585033f43cSJassi Brar 	i2s->i2s_dai_drv.probe = samsung_i2s_dai_probe;
10595033f43cSJassi Brar 	i2s->i2s_dai_drv.remove = samsung_i2s_dai_remove;
10605033f43cSJassi Brar 	i2s->i2s_dai_drv.ops = &samsung_i2s_dai_ops;
10615033f43cSJassi Brar 	i2s->i2s_dai_drv.suspend = i2s_suspend;
10625033f43cSJassi Brar 	i2s->i2s_dai_drv.resume = i2s_resume;
1063a0ff6ea2SCharles Keepax 	i2s->i2s_dai_drv.playback.channels_min = 1;
10645033f43cSJassi Brar 	i2s->i2s_dai_drv.playback.channels_max = 2;
10655033f43cSJassi Brar 	i2s->i2s_dai_drv.playback.rates = SAMSUNG_I2S_RATES;
10665033f43cSJassi Brar 	i2s->i2s_dai_drv.playback.formats = SAMSUNG_I2S_FMTS;
10675033f43cSJassi Brar 
10685033f43cSJassi Brar 	if (!sec) {
1069588fb705SSangsu Park 		i2s->i2s_dai_drv.capture.channels_min = 1;
10705033f43cSJassi Brar 		i2s->i2s_dai_drv.capture.channels_max = 2;
10715033f43cSJassi Brar 		i2s->i2s_dai_drv.capture.rates = SAMSUNG_I2S_RATES;
10725033f43cSJassi Brar 		i2s->i2s_dai_drv.capture.formats = SAMSUNG_I2S_FMTS;
1073c6f9b1ebSPrathyush K 		dev_set_drvdata(&i2s->pdev->dev, i2s);
10745033f43cSJassi Brar 	} else {	/* Create a new platform_device for Secondary */
1075c6f9b1ebSPrathyush K 		i2s->pdev = platform_device_alloc("samsung-i2s-sec", -1);
107629ca9c73SWei Yongjun 		if (!i2s->pdev)
10775033f43cSJassi Brar 			return NULL;
10785033f43cSJassi Brar 
10792f6f0ffbSMark Brown 		i2s->pdev->dev.parent = &pdev->dev;
10802f6f0ffbSMark Brown 
1081c6f9b1ebSPrathyush K 		platform_set_drvdata(i2s->pdev, i2s);
1082c6f9b1ebSPrathyush K 		ret = platform_device_add(i2s->pdev);
1083c6f9b1ebSPrathyush K 		if (ret < 0)
1084c6f9b1ebSPrathyush K 			return NULL;
1085c6f9b1ebSPrathyush K 	}
10865033f43cSJassi Brar 
10875033f43cSJassi Brar 	return i2s;
10885033f43cSJassi Brar }
10895033f43cSJassi Brar 
109040476f61SPadmavathi Venna static const struct of_device_id exynos_i2s_match[];
109140476f61SPadmavathi Venna 
10927da493e9SPadmavathi Venna static inline const struct samsung_i2s_dai_data *samsung_i2s_get_driver_data(
10937da493e9SPadmavathi Venna 						struct platform_device *pdev)
10947c62eebbSPadmavathi Venna {
109540476f61SPadmavathi Venna #ifdef CONFIG_OF
109640476f61SPadmavathi Venna 	if (pdev->dev.of_node) {
109740476f61SPadmavathi Venna 		const struct of_device_id *match;
109840476f61SPadmavathi Venna 		match = of_match_node(exynos_i2s_match, pdev->dev.of_node);
10997da493e9SPadmavathi Venna 		return match->data;
110040476f61SPadmavathi Venna 	} else
110140476f61SPadmavathi Venna #endif
11027da493e9SPadmavathi Venna 		return (struct samsung_i2s_dai_data *)
11037da493e9SPadmavathi Venna 				platform_get_device_id(pdev)->driver_data;
11047c62eebbSPadmavathi Venna }
11057c62eebbSPadmavathi Venna 
11065b1d3c34SR. Chandrasekar #ifdef CONFIG_PM_RUNTIME
11075b1d3c34SR. Chandrasekar static int i2s_runtime_suspend(struct device *dev)
11085b1d3c34SR. Chandrasekar {
11095b1d3c34SR. Chandrasekar 	struct i2s_dai *i2s = dev_get_drvdata(dev);
11105b1d3c34SR. Chandrasekar 
11115b1d3c34SR. Chandrasekar 	clk_disable_unprepare(i2s->clk);
11125b1d3c34SR. Chandrasekar 
11135b1d3c34SR. Chandrasekar 	return 0;
11145b1d3c34SR. Chandrasekar }
11155b1d3c34SR. Chandrasekar 
11165b1d3c34SR. Chandrasekar static int i2s_runtime_resume(struct device *dev)
11175b1d3c34SR. Chandrasekar {
11185b1d3c34SR. Chandrasekar 	struct i2s_dai *i2s = dev_get_drvdata(dev);
11195b1d3c34SR. Chandrasekar 
11205b1d3c34SR. Chandrasekar 	clk_prepare_enable(i2s->clk);
11215b1d3c34SR. Chandrasekar 
11225b1d3c34SR. Chandrasekar 	return 0;
11235b1d3c34SR. Chandrasekar }
11245b1d3c34SR. Chandrasekar #endif /* CONFIG_PM_RUNTIME */
11255b1d3c34SR. Chandrasekar 
1126fdca21adSBill Pemberton static int samsung_i2s_probe(struct platform_device *pdev)
11275033f43cSJassi Brar {
11285033f43cSJassi Brar 	struct i2s_dai *pri_dai, *sec_dai = NULL;
112940476f61SPadmavathi Venna 	struct s3c_audio_pdata *i2s_pdata = pdev->dev.platform_data;
113040476f61SPadmavathi Venna 	struct samsung_i2s *i2s_cfg = NULL;
11315033f43cSJassi Brar 	struct resource *res;
113240476f61SPadmavathi Venna 	u32 regs_base, quirks = 0, idma_addr = 0;
113340476f61SPadmavathi Venna 	struct device_node *np = pdev->dev.of_node;
11347da493e9SPadmavathi Venna 	const struct samsung_i2s_dai_data *i2s_dai_data;
11355033f43cSJassi Brar 	int ret = 0;
11365033f43cSJassi Brar 
11375033f43cSJassi Brar 	/* Call during Seconday interface registration */
11387da493e9SPadmavathi Venna 	i2s_dai_data = samsung_i2s_get_driver_data(pdev);
11397c62eebbSPadmavathi Venna 
11407da493e9SPadmavathi Venna 	if (i2s_dai_data->dai_type == TYPE_SEC) {
11415033f43cSJassi Brar 		sec_dai = dev_get_drvdata(&pdev->dev);
1142a9b977ecSPrathyush K 		if (!sec_dai) {
1143a9b977ecSPrathyush K 			dev_err(&pdev->dev, "Unable to get drvdata\n");
1144a9b977ecSPrathyush K 			return -EFAULT;
1145a9b977ecSPrathyush K 		}
1146d644a115SMark Brown 		devm_snd_soc_register_component(&sec_dai->pdev->dev,
11474b828535SKuninori Morimoto 						&samsung_i2s_component,
11484b828535SKuninori Morimoto 						&sec_dai->i2s_dai_drv, 1);
114985ff3c29SMark Brown 		samsung_asoc_dma_platform_register(&pdev->dev);
11505033f43cSJassi Brar 		return 0;
11515033f43cSJassi Brar 	}
11525033f43cSJassi Brar 
115340476f61SPadmavathi Venna 	pri_dai = i2s_alloc_dai(pdev, false);
115440476f61SPadmavathi Venna 	if (!pri_dai) {
115540476f61SPadmavathi Venna 		dev_err(&pdev->dev, "Unable to alloc I2S_pri\n");
115640476f61SPadmavathi Venna 		return -ENOMEM;
115740476f61SPadmavathi Venna 	}
115840476f61SPadmavathi Venna 
115940476f61SPadmavathi Venna 	if (!np) {
116040476f61SPadmavathi Venna 		res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
116140476f61SPadmavathi Venna 		if (!res) {
116240476f61SPadmavathi Venna 			dev_err(&pdev->dev,
116340476f61SPadmavathi Venna 				"Unable to get I2S-TX dma resource\n");
116440476f61SPadmavathi Venna 			return -ENXIO;
116540476f61SPadmavathi Venna 		}
116640476f61SPadmavathi Venna 		pri_dai->dma_playback.channel = res->start;
116740476f61SPadmavathi Venna 
116840476f61SPadmavathi Venna 		res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
116940476f61SPadmavathi Venna 		if (!res) {
117040476f61SPadmavathi Venna 			dev_err(&pdev->dev,
117140476f61SPadmavathi Venna 				"Unable to get I2S-RX dma resource\n");
117240476f61SPadmavathi Venna 			return -ENXIO;
117340476f61SPadmavathi Venna 		}
117440476f61SPadmavathi Venna 		pri_dai->dma_capture.channel = res->start;
117540476f61SPadmavathi Venna 
11765033f43cSJassi Brar 		if (i2s_pdata == NULL) {
11775033f43cSJassi Brar 			dev_err(&pdev->dev, "Can't work without s3c_audio_pdata\n");
11785033f43cSJassi Brar 			return -EINVAL;
11795033f43cSJassi Brar 		}
11805033f43cSJassi Brar 
118140476f61SPadmavathi Venna 		if (&i2s_pdata->type)
118240476f61SPadmavathi Venna 			i2s_cfg = &i2s_pdata->type.i2s;
11835033f43cSJassi Brar 
118440476f61SPadmavathi Venna 		if (i2s_cfg) {
118540476f61SPadmavathi Venna 			quirks = i2s_cfg->quirks;
118640476f61SPadmavathi Venna 			idma_addr = i2s_cfg->idma_addr;
11875033f43cSJassi Brar 		}
118840476f61SPadmavathi Venna 	} else {
11897da493e9SPadmavathi Venna 		quirks = i2s_dai_data->quirks;
119040476f61SPadmavathi Venna 		if (of_property_read_u32(np, "samsung,idma-addr",
119140476f61SPadmavathi Venna 					 &idma_addr)) {
119240476f61SPadmavathi Venna 			if (quirks & QUIRK_SEC_DAI) {
119340476f61SPadmavathi Venna 				dev_err(&pdev->dev, "idma address is not"\
119440476f61SPadmavathi Venna 						"specified");
119540476f61SPadmavathi Venna 				return -EINVAL;
119640476f61SPadmavathi Venna 			}
119740476f61SPadmavathi Venna 		}
119840476f61SPadmavathi Venna 	}
11995033f43cSJassi Brar 
12005033f43cSJassi Brar 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
12015033f43cSJassi Brar 	if (!res) {
12025033f43cSJassi Brar 		dev_err(&pdev->dev, "Unable to get I2S SFR address\n");
12035033f43cSJassi Brar 		return -ENXIO;
12045033f43cSJassi Brar 	}
12055033f43cSJassi Brar 
12065033f43cSJassi Brar 	if (!request_mem_region(res->start, resource_size(res),
12075033f43cSJassi Brar 							"samsung-i2s")) {
12085033f43cSJassi Brar 		dev_err(&pdev->dev, "Unable to request SFR region\n");
12095033f43cSJassi Brar 		return -EBUSY;
12105033f43cSJassi Brar 	}
12115033f43cSJassi Brar 	regs_base = res->start;
12125033f43cSJassi Brar 
12135033f43cSJassi Brar 	pri_dai->dma_playback.dma_addr = regs_base + I2STXD;
12145033f43cSJassi Brar 	pri_dai->dma_capture.dma_addr = regs_base + I2SRXD;
12155033f43cSJassi Brar 	pri_dai->dma_playback.client =
121671e5222cSSachin Kamat 		(struct s3c_dma_client *)&pri_dai->dma_playback;
121740476f61SPadmavathi Venna 	pri_dai->dma_playback.ch_name = "tx";
12185033f43cSJassi Brar 	pri_dai->dma_capture.client =
121971e5222cSSachin Kamat 		(struct s3c_dma_client *)&pri_dai->dma_capture;
122040476f61SPadmavathi Venna 	pri_dai->dma_capture.ch_name = "rx";
12215033f43cSJassi Brar 	pri_dai->dma_playback.dma_size = 4;
12225033f43cSJassi Brar 	pri_dai->dma_capture.dma_size = 4;
12235033f43cSJassi Brar 	pri_dai->base = regs_base;
12245033f43cSJassi Brar 	pri_dai->quirks = quirks;
12255033f43cSJassi Brar 
12265033f43cSJassi Brar 	if (quirks & QUIRK_PRI_6CHAN)
12275033f43cSJassi Brar 		pri_dai->i2s_dai_drv.playback.channels_max = 6;
12285033f43cSJassi Brar 
12295033f43cSJassi Brar 	if (quirks & QUIRK_SEC_DAI) {
12305033f43cSJassi Brar 		sec_dai = i2s_alloc_dai(pdev, true);
12315033f43cSJassi Brar 		if (!sec_dai) {
12325033f43cSJassi Brar 			dev_err(&pdev->dev, "Unable to alloc I2S_sec\n");
12335033f43cSJassi Brar 			ret = -ENOMEM;
1234b960ce74SMark Brown 			goto err;
12355033f43cSJassi Brar 		}
12365033f43cSJassi Brar 		sec_dai->dma_playback.dma_addr = regs_base + I2STXDS;
12375033f43cSJassi Brar 		sec_dai->dma_playback.client =
123871e5222cSSachin Kamat 			(struct s3c_dma_client *)&sec_dai->dma_playback;
123940476f61SPadmavathi Venna 		sec_dai->dma_playback.ch_name = "tx-sec";
124040476f61SPadmavathi Venna 
124140476f61SPadmavathi Venna 		if (!np) {
124240476f61SPadmavathi Venna 			res = platform_get_resource(pdev, IORESOURCE_DMA, 2);
124340476f61SPadmavathi Venna 			if (res)
124440476f61SPadmavathi Venna 				sec_dai->dma_playback.channel = res->start;
124540476f61SPadmavathi Venna 		}
124640476f61SPadmavathi Venna 
12475033f43cSJassi Brar 		sec_dai->dma_playback.dma_size = 4;
12485033f43cSJassi Brar 		sec_dai->base = regs_base;
12495033f43cSJassi Brar 		sec_dai->quirks = quirks;
125040476f61SPadmavathi Venna 		sec_dai->idma_playback.dma_addr = idma_addr;
12515033f43cSJassi Brar 		sec_dai->pri_dai = pri_dai;
12525033f43cSJassi Brar 		pri_dai->sec_dai = sec_dai;
12535033f43cSJassi Brar 	}
12545033f43cSJassi Brar 
12550429ffefSMark Brown 	if (i2s_pdata && i2s_pdata->cfg_gpio && i2s_pdata->cfg_gpio(pdev)) {
125640476f61SPadmavathi Venna 		dev_err(&pdev->dev, "Unable to configure gpio\n");
125740476f61SPadmavathi Venna 		ret = -EINVAL;
125840476f61SPadmavathi Venna 		goto err;
125940476f61SPadmavathi Venna 	}
12605033f43cSJassi Brar 
1261d644a115SMark Brown 	devm_snd_soc_register_component(&pri_dai->pdev->dev,
1262d644a115SMark Brown 					&samsung_i2s_component,
12634b828535SKuninori Morimoto 					&pri_dai->i2s_dai_drv, 1);
12645033f43cSJassi Brar 
1265c5cf4dbcSMark Brown 	pm_runtime_enable(&pdev->dev);
1266c5cf4dbcSMark Brown 
126785ff3c29SMark Brown 	samsung_asoc_dma_platform_register(&pdev->dev);
1268a08485d8SPadmavathi Venna 
12695033f43cSJassi Brar 	return 0;
1270b960ce74SMark Brown err:
127157e33781SSachin Kamat 	if (res)
12725033f43cSJassi Brar 		release_mem_region(regs_base, resource_size(res));
12735033f43cSJassi Brar 
12745033f43cSJassi Brar 	return ret;
12755033f43cSJassi Brar }
12765033f43cSJassi Brar 
1277fdca21adSBill Pemberton static int samsung_i2s_remove(struct platform_device *pdev)
12785033f43cSJassi Brar {
12795033f43cSJassi Brar 	struct i2s_dai *i2s, *other;
1280c5cf4dbcSMark Brown 	struct resource *res;
12815033f43cSJassi Brar 
12825033f43cSJassi Brar 	i2s = dev_get_drvdata(&pdev->dev);
12835033f43cSJassi Brar 	other = i2s->pri_dai ? : i2s->sec_dai;
12845033f43cSJassi Brar 
12855033f43cSJassi Brar 	if (other) {
12865033f43cSJassi Brar 		other->pri_dai = NULL;
12875033f43cSJassi Brar 		other->sec_dai = NULL;
12885033f43cSJassi Brar 	} else {
1289c5cf4dbcSMark Brown 		pm_runtime_disable(&pdev->dev);
12905033f43cSJassi Brar 		res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
12915033f43cSJassi Brar 		if (res)
12925033f43cSJassi Brar 			release_mem_region(res->start, resource_size(res));
12935033f43cSJassi Brar 	}
12945033f43cSJassi Brar 
12955033f43cSJassi Brar 	i2s->pri_dai = NULL;
12965033f43cSJassi Brar 	i2s->sec_dai = NULL;
12975033f43cSJassi Brar 
129885ff3c29SMark Brown 	samsung_asoc_dma_platform_unregister(&pdev->dev);
12995033f43cSJassi Brar 
13005033f43cSJassi Brar 	return 0;
13015033f43cSJassi Brar }
13025033f43cSJassi Brar 
13037da493e9SPadmavathi Venna static const struct samsung_i2s_dai_data i2sv3_dai_type = {
13047da493e9SPadmavathi Venna 	.dai_type = TYPE_PRI,
13057da493e9SPadmavathi Venna 	.quirks = QUIRK_NO_MUXPSR,
13067da493e9SPadmavathi Venna };
13077da493e9SPadmavathi Venna 
13087da493e9SPadmavathi Venna static const struct samsung_i2s_dai_data i2sv5_dai_type = {
13097da493e9SPadmavathi Venna 	.dai_type = TYPE_PRI,
13107da493e9SPadmavathi Venna 	.quirks = QUIRK_PRI_6CHAN | QUIRK_SEC_DAI | QUIRK_NEED_RSTCLR,
13117da493e9SPadmavathi Venna };
13127da493e9SPadmavathi Venna 
13134ca0c0d4SPadmavathi Venna static const struct samsung_i2s_dai_data i2sv6_dai_type = {
13144ca0c0d4SPadmavathi Venna 	.dai_type = TYPE_PRI,
13154ca0c0d4SPadmavathi Venna 	.quirks = QUIRK_PRI_6CHAN | QUIRK_SEC_DAI | QUIRK_NEED_RSTCLR |
13164ca0c0d4SPadmavathi Venna 			QUIRK_SUPPORTS_TDM,
13174ca0c0d4SPadmavathi Venna };
13184ca0c0d4SPadmavathi Venna 
13197da493e9SPadmavathi Venna static const struct samsung_i2s_dai_data samsung_dai_type_pri = {
13207da493e9SPadmavathi Venna 	.dai_type = TYPE_PRI,
13217da493e9SPadmavathi Venna };
13227da493e9SPadmavathi Venna 
13237da493e9SPadmavathi Venna static const struct samsung_i2s_dai_data samsung_dai_type_sec = {
13247da493e9SPadmavathi Venna 	.dai_type = TYPE_SEC,
13257da493e9SPadmavathi Venna };
13267da493e9SPadmavathi Venna 
13277c62eebbSPadmavathi Venna static struct platform_device_id samsung_i2s_driver_ids[] = {
13287c62eebbSPadmavathi Venna 	{
13297c62eebbSPadmavathi Venna 		.name           = "samsung-i2s",
13307da493e9SPadmavathi Venna 		.driver_data    = (kernel_ulong_t)&samsung_dai_type_pri,
13317c62eebbSPadmavathi Venna 	}, {
13327c62eebbSPadmavathi Venna 		.name           = "samsung-i2s-sec",
13337da493e9SPadmavathi Venna 		.driver_data    = (kernel_ulong_t)&samsung_dai_type_sec,
13347c62eebbSPadmavathi Venna 	},
13357c62eebbSPadmavathi Venna 	{},
13367c62eebbSPadmavathi Venna };
13372af19558SArnd Bergmann MODULE_DEVICE_TABLE(platform, samsung_i2s_driver_ids);
13387c62eebbSPadmavathi Venna 
133940476f61SPadmavathi Venna #ifdef CONFIG_OF
134040476f61SPadmavathi Venna static const struct of_device_id exynos_i2s_match[] = {
13417da493e9SPadmavathi Venna 	{
13427da493e9SPadmavathi Venna 		.compatible = "samsung,s3c6410-i2s",
13437da493e9SPadmavathi Venna 		.data = &i2sv3_dai_type,
13447da493e9SPadmavathi Venna 	}, {
13457da493e9SPadmavathi Venna 		.compatible = "samsung,s5pv210-i2s",
13467da493e9SPadmavathi Venna 		.data = &i2sv5_dai_type,
13474ca0c0d4SPadmavathi Venna 	}, {
13484ca0c0d4SPadmavathi Venna 		.compatible = "samsung,exynos5420-i2s",
13494ca0c0d4SPadmavathi Venna 		.data = &i2sv6_dai_type,
135040476f61SPadmavathi Venna 	},
135140476f61SPadmavathi Venna 	{},
135240476f61SPadmavathi Venna };
135340476f61SPadmavathi Venna MODULE_DEVICE_TABLE(of, exynos_i2s_match);
135440476f61SPadmavathi Venna #endif
135540476f61SPadmavathi Venna 
13565b1d3c34SR. Chandrasekar static const struct dev_pm_ops samsung_i2s_pm = {
13575b1d3c34SR. Chandrasekar 	SET_RUNTIME_PM_OPS(i2s_runtime_suspend,
13585b1d3c34SR. Chandrasekar 				i2s_runtime_resume, NULL)
13595b1d3c34SR. Chandrasekar };
13605b1d3c34SR. Chandrasekar 
13615033f43cSJassi Brar static struct platform_driver samsung_i2s_driver = {
13625033f43cSJassi Brar 	.probe  = samsung_i2s_probe,
1363fdca21adSBill Pemberton 	.remove = samsung_i2s_remove,
13647c62eebbSPadmavathi Venna 	.id_table = samsung_i2s_driver_ids,
13655033f43cSJassi Brar 	.driver = {
13665033f43cSJassi Brar 		.name = "samsung-i2s",
13675033f43cSJassi Brar 		.owner = THIS_MODULE,
136840476f61SPadmavathi Venna 		.of_match_table = of_match_ptr(exynos_i2s_match),
13695b1d3c34SR. Chandrasekar 		.pm = &samsung_i2s_pm,
13705033f43cSJassi Brar 	},
13715033f43cSJassi Brar };
13725033f43cSJassi Brar 
1373e00c3f55SMark Brown module_platform_driver(samsung_i2s_driver);
13745033f43cSJassi Brar 
13755033f43cSJassi Brar /* Module information */
1376df8ad335SJaswinder Singh MODULE_AUTHOR("Jaswinder Singh, <jassisinghbrar@gmail.com>");
13775033f43cSJassi Brar MODULE_DESCRIPTION("Samsung I2S Interface");
13785033f43cSJassi Brar MODULE_ALIAS("platform:samsung-i2s");
13795033f43cSJassi Brar MODULE_LICENSE("GPL");
1380