18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci//
38c2ecf20Sopenharmony_ci// s3c24xx-i2s.c  --  ALSA Soc Audio Layer
48c2ecf20Sopenharmony_ci//
58c2ecf20Sopenharmony_ci// (c) 2006 Wolfson Microelectronics PLC.
68c2ecf20Sopenharmony_ci// Graeme Gregory graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com
78c2ecf20Sopenharmony_ci//
88c2ecf20Sopenharmony_ci// Copyright 2004-2005 Simtec Electronics
98c2ecf20Sopenharmony_ci//	http://armlinux.simtec.co.uk/
108c2ecf20Sopenharmony_ci//	Ben Dooks <ben@simtec.co.uk>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/delay.h>
138c2ecf20Sopenharmony_ci#include <linux/clk.h>
148c2ecf20Sopenharmony_ci#include <linux/io.h>
158c2ecf20Sopenharmony_ci#include <linux/gpio.h>
168c2ecf20Sopenharmony_ci#include <linux/module.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include <sound/soc.h>
198c2ecf20Sopenharmony_ci#include <sound/pcm_params.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#include "regs-iis.h"
228c2ecf20Sopenharmony_ci#include "dma.h"
238c2ecf20Sopenharmony_ci#include "s3c24xx-i2s.h"
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistatic struct snd_dmaengine_dai_dma_data s3c24xx_i2s_pcm_stereo_out = {
268c2ecf20Sopenharmony_ci	.chan_name	= "tx",
278c2ecf20Sopenharmony_ci	.addr_width	= 2,
288c2ecf20Sopenharmony_ci};
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistatic struct snd_dmaengine_dai_dma_data s3c24xx_i2s_pcm_stereo_in = {
318c2ecf20Sopenharmony_ci	.chan_name	= "rx",
328c2ecf20Sopenharmony_ci	.addr_width	= 2,
338c2ecf20Sopenharmony_ci};
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistruct s3c24xx_i2s_info {
368c2ecf20Sopenharmony_ci	void __iomem	*regs;
378c2ecf20Sopenharmony_ci	struct clk	*iis_clk;
388c2ecf20Sopenharmony_ci	u32		iiscon;
398c2ecf20Sopenharmony_ci	u32		iismod;
408c2ecf20Sopenharmony_ci	u32		iisfcon;
418c2ecf20Sopenharmony_ci	u32		iispsr;
428c2ecf20Sopenharmony_ci};
438c2ecf20Sopenharmony_cistatic struct s3c24xx_i2s_info s3c24xx_i2s;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic void s3c24xx_snd_txctrl(int on)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	u32 iisfcon;
488c2ecf20Sopenharmony_ci	u32 iiscon;
498c2ecf20Sopenharmony_ci	u32 iismod;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	iisfcon = readl(s3c24xx_i2s.regs + S3C2410_IISFCON);
528c2ecf20Sopenharmony_ci	iiscon  = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
538c2ecf20Sopenharmony_ci	iismod  = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	pr_debug("r: IISCON: %x IISMOD: %x IISFCON: %x\n", iiscon, iismod, iisfcon);
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	if (on) {
588c2ecf20Sopenharmony_ci		iisfcon |= S3C2410_IISFCON_TXDMA | S3C2410_IISFCON_TXENABLE;
598c2ecf20Sopenharmony_ci		iiscon  |= S3C2410_IISCON_TXDMAEN | S3C2410_IISCON_IISEN;
608c2ecf20Sopenharmony_ci		iiscon  &= ~S3C2410_IISCON_TXIDLE;
618c2ecf20Sopenharmony_ci		iismod  |= S3C2410_IISMOD_TXMODE;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci		writel(iismod,  s3c24xx_i2s.regs + S3C2410_IISMOD);
648c2ecf20Sopenharmony_ci		writel(iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
658c2ecf20Sopenharmony_ci		writel(iiscon,  s3c24xx_i2s.regs + S3C2410_IISCON);
668c2ecf20Sopenharmony_ci	} else {
678c2ecf20Sopenharmony_ci		/* note, we have to disable the FIFOs otherwise bad things
688c2ecf20Sopenharmony_ci		 * seem to happen when the DMA stops. According to the
698c2ecf20Sopenharmony_ci		 * Samsung supplied kernel, this should allow the DMA
708c2ecf20Sopenharmony_ci		 * engine and FIFOs to reset. If this isn't allowed, the
718c2ecf20Sopenharmony_ci		 * DMA engine will simply freeze randomly.
728c2ecf20Sopenharmony_ci		 */
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci		iisfcon &= ~S3C2410_IISFCON_TXENABLE;
758c2ecf20Sopenharmony_ci		iisfcon &= ~S3C2410_IISFCON_TXDMA;
768c2ecf20Sopenharmony_ci		iiscon  |=  S3C2410_IISCON_TXIDLE;
778c2ecf20Sopenharmony_ci		iiscon  &= ~S3C2410_IISCON_TXDMAEN;
788c2ecf20Sopenharmony_ci		iismod  &= ~S3C2410_IISMOD_TXMODE;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci		writel(iiscon,  s3c24xx_i2s.regs + S3C2410_IISCON);
818c2ecf20Sopenharmony_ci		writel(iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
828c2ecf20Sopenharmony_ci		writel(iismod,  s3c24xx_i2s.regs + S3C2410_IISMOD);
838c2ecf20Sopenharmony_ci	}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	pr_debug("w: IISCON: %x IISMOD: %x IISFCON: %x\n", iiscon, iismod, iisfcon);
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic void s3c24xx_snd_rxctrl(int on)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	u32 iisfcon;
918c2ecf20Sopenharmony_ci	u32 iiscon;
928c2ecf20Sopenharmony_ci	u32 iismod;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	iisfcon = readl(s3c24xx_i2s.regs + S3C2410_IISFCON);
958c2ecf20Sopenharmony_ci	iiscon  = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
968c2ecf20Sopenharmony_ci	iismod  = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	pr_debug("r: IISCON: %x IISMOD: %x IISFCON: %x\n", iiscon, iismod, iisfcon);
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	if (on) {
1018c2ecf20Sopenharmony_ci		iisfcon |= S3C2410_IISFCON_RXDMA | S3C2410_IISFCON_RXENABLE;
1028c2ecf20Sopenharmony_ci		iiscon  |= S3C2410_IISCON_RXDMAEN | S3C2410_IISCON_IISEN;
1038c2ecf20Sopenharmony_ci		iiscon  &= ~S3C2410_IISCON_RXIDLE;
1048c2ecf20Sopenharmony_ci		iismod  |= S3C2410_IISMOD_RXMODE;
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci		writel(iismod,  s3c24xx_i2s.regs + S3C2410_IISMOD);
1078c2ecf20Sopenharmony_ci		writel(iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
1088c2ecf20Sopenharmony_ci		writel(iiscon,  s3c24xx_i2s.regs + S3C2410_IISCON);
1098c2ecf20Sopenharmony_ci	} else {
1108c2ecf20Sopenharmony_ci		/* note, we have to disable the FIFOs otherwise bad things
1118c2ecf20Sopenharmony_ci		 * seem to happen when the DMA stops. According to the
1128c2ecf20Sopenharmony_ci		 * Samsung supplied kernel, this should allow the DMA
1138c2ecf20Sopenharmony_ci		 * engine and FIFOs to reset. If this isn't allowed, the
1148c2ecf20Sopenharmony_ci		 * DMA engine will simply freeze randomly.
1158c2ecf20Sopenharmony_ci		 */
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci		iisfcon &= ~S3C2410_IISFCON_RXENABLE;
1188c2ecf20Sopenharmony_ci		iisfcon &= ~S3C2410_IISFCON_RXDMA;
1198c2ecf20Sopenharmony_ci		iiscon  |= S3C2410_IISCON_RXIDLE;
1208c2ecf20Sopenharmony_ci		iiscon  &= ~S3C2410_IISCON_RXDMAEN;
1218c2ecf20Sopenharmony_ci		iismod  &= ~S3C2410_IISMOD_RXMODE;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci		writel(iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
1248c2ecf20Sopenharmony_ci		writel(iiscon,  s3c24xx_i2s.regs + S3C2410_IISCON);
1258c2ecf20Sopenharmony_ci		writel(iismod,  s3c24xx_i2s.regs + S3C2410_IISMOD);
1268c2ecf20Sopenharmony_ci	}
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	pr_debug("w: IISCON: %x IISMOD: %x IISFCON: %x\n", iiscon, iismod, iisfcon);
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci/*
1328c2ecf20Sopenharmony_ci * Wait for the LR signal to allow synchronisation to the L/R clock
1338c2ecf20Sopenharmony_ci * from the codec. May only be needed for slave mode.
1348c2ecf20Sopenharmony_ci */
1358c2ecf20Sopenharmony_cistatic int s3c24xx_snd_lrsync(void)
1368c2ecf20Sopenharmony_ci{
1378c2ecf20Sopenharmony_ci	u32 iiscon;
1388c2ecf20Sopenharmony_ci	int timeout = 50; /* 5ms */
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	while (1) {
1418c2ecf20Sopenharmony_ci		iiscon = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
1428c2ecf20Sopenharmony_ci		if (iiscon & S3C2410_IISCON_LRINDEX)
1438c2ecf20Sopenharmony_ci			break;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci		if (!timeout--)
1468c2ecf20Sopenharmony_ci			return -ETIMEDOUT;
1478c2ecf20Sopenharmony_ci		udelay(100);
1488c2ecf20Sopenharmony_ci	}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	return 0;
1518c2ecf20Sopenharmony_ci}
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci/*
1548c2ecf20Sopenharmony_ci * Check whether CPU is the master or slave
1558c2ecf20Sopenharmony_ci */
1568c2ecf20Sopenharmony_cistatic inline int s3c24xx_snd_is_clkmaster(void)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	return (readl(s3c24xx_i2s.regs + S3C2410_IISMOD) & S3C2410_IISMOD_SLAVE) ? 0:1;
1598c2ecf20Sopenharmony_ci}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci/*
1628c2ecf20Sopenharmony_ci * Set S3C24xx I2S DAI format
1638c2ecf20Sopenharmony_ci */
1648c2ecf20Sopenharmony_cistatic int s3c24xx_i2s_set_fmt(struct snd_soc_dai *cpu_dai,
1658c2ecf20Sopenharmony_ci		unsigned int fmt)
1668c2ecf20Sopenharmony_ci{
1678c2ecf20Sopenharmony_ci	u32 iismod;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
1708c2ecf20Sopenharmony_ci	pr_debug("hw_params r: IISMOD: %x \n", iismod);
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1738c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_CBM_CFM:
1748c2ecf20Sopenharmony_ci		iismod |= S3C2410_IISMOD_SLAVE;
1758c2ecf20Sopenharmony_ci		break;
1768c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_CBS_CFS:
1778c2ecf20Sopenharmony_ci		iismod &= ~S3C2410_IISMOD_SLAVE;
1788c2ecf20Sopenharmony_ci		break;
1798c2ecf20Sopenharmony_ci	default:
1808c2ecf20Sopenharmony_ci		return -EINVAL;
1818c2ecf20Sopenharmony_ci	}
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1848c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_LEFT_J:
1858c2ecf20Sopenharmony_ci		iismod |= S3C2410_IISMOD_MSB;
1868c2ecf20Sopenharmony_ci		break;
1878c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_I2S:
1888c2ecf20Sopenharmony_ci		iismod &= ~S3C2410_IISMOD_MSB;
1898c2ecf20Sopenharmony_ci		break;
1908c2ecf20Sopenharmony_ci	default:
1918c2ecf20Sopenharmony_ci		return -EINVAL;
1928c2ecf20Sopenharmony_ci	}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
1958c2ecf20Sopenharmony_ci	pr_debug("hw_params w: IISMOD: %x \n", iismod);
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	return 0;
1988c2ecf20Sopenharmony_ci}
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_cistatic int s3c24xx_i2s_hw_params(struct snd_pcm_substream *substream,
2018c2ecf20Sopenharmony_ci				 struct snd_pcm_hw_params *params,
2028c2ecf20Sopenharmony_ci				 struct snd_soc_dai *dai)
2038c2ecf20Sopenharmony_ci{
2048c2ecf20Sopenharmony_ci	struct snd_dmaengine_dai_dma_data *dma_data;
2058c2ecf20Sopenharmony_ci	u32 iismod;
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	dma_data = snd_soc_dai_get_dma_data(dai, substream);
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	/* Working copies of register */
2108c2ecf20Sopenharmony_ci	iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
2118c2ecf20Sopenharmony_ci	pr_debug("hw_params r: IISMOD: %x\n", iismod);
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	switch (params_width(params)) {
2148c2ecf20Sopenharmony_ci	case 8:
2158c2ecf20Sopenharmony_ci		iismod &= ~S3C2410_IISMOD_16BIT;
2168c2ecf20Sopenharmony_ci		dma_data->addr_width = 1;
2178c2ecf20Sopenharmony_ci		break;
2188c2ecf20Sopenharmony_ci	case 16:
2198c2ecf20Sopenharmony_ci		iismod |= S3C2410_IISMOD_16BIT;
2208c2ecf20Sopenharmony_ci		dma_data->addr_width = 2;
2218c2ecf20Sopenharmony_ci		break;
2228c2ecf20Sopenharmony_ci	default:
2238c2ecf20Sopenharmony_ci		return -EINVAL;
2248c2ecf20Sopenharmony_ci	}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
2278c2ecf20Sopenharmony_ci	pr_debug("hw_params w: IISMOD: %x\n", iismod);
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	return 0;
2308c2ecf20Sopenharmony_ci}
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_cistatic int s3c24xx_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
2338c2ecf20Sopenharmony_ci			       struct snd_soc_dai *dai)
2348c2ecf20Sopenharmony_ci{
2358c2ecf20Sopenharmony_ci	int ret = 0;
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	switch (cmd) {
2388c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_START:
2398c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_RESUME:
2408c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2418c2ecf20Sopenharmony_ci		if (!s3c24xx_snd_is_clkmaster()) {
2428c2ecf20Sopenharmony_ci			ret = s3c24xx_snd_lrsync();
2438c2ecf20Sopenharmony_ci			if (ret)
2448c2ecf20Sopenharmony_ci				goto exit_err;
2458c2ecf20Sopenharmony_ci		}
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2488c2ecf20Sopenharmony_ci			s3c24xx_snd_rxctrl(1);
2498c2ecf20Sopenharmony_ci		else
2508c2ecf20Sopenharmony_ci			s3c24xx_snd_txctrl(1);
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci		break;
2538c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_STOP:
2548c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_SUSPEND:
2558c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2568c2ecf20Sopenharmony_ci		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2578c2ecf20Sopenharmony_ci			s3c24xx_snd_rxctrl(0);
2588c2ecf20Sopenharmony_ci		else
2598c2ecf20Sopenharmony_ci			s3c24xx_snd_txctrl(0);
2608c2ecf20Sopenharmony_ci		break;
2618c2ecf20Sopenharmony_ci	default:
2628c2ecf20Sopenharmony_ci		ret = -EINVAL;
2638c2ecf20Sopenharmony_ci		break;
2648c2ecf20Sopenharmony_ci	}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ciexit_err:
2678c2ecf20Sopenharmony_ci	return ret;
2688c2ecf20Sopenharmony_ci}
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci/*
2718c2ecf20Sopenharmony_ci * Set S3C24xx Clock source
2728c2ecf20Sopenharmony_ci */
2738c2ecf20Sopenharmony_cistatic int s3c24xx_i2s_set_sysclk(struct snd_soc_dai *cpu_dai,
2748c2ecf20Sopenharmony_ci	int clk_id, unsigned int freq, int dir)
2758c2ecf20Sopenharmony_ci{
2768c2ecf20Sopenharmony_ci	u32 iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	iismod &= ~S3C2440_IISMOD_MPLL;
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	switch (clk_id) {
2818c2ecf20Sopenharmony_ci	case S3C24XX_CLKSRC_PCLK:
2828c2ecf20Sopenharmony_ci		break;
2838c2ecf20Sopenharmony_ci	case S3C24XX_CLKSRC_MPLL:
2848c2ecf20Sopenharmony_ci		iismod |= S3C2440_IISMOD_MPLL;
2858c2ecf20Sopenharmony_ci		break;
2868c2ecf20Sopenharmony_ci	default:
2878c2ecf20Sopenharmony_ci		return -EINVAL;
2888c2ecf20Sopenharmony_ci	}
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	writel(iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
2918c2ecf20Sopenharmony_ci	return 0;
2928c2ecf20Sopenharmony_ci}
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci/*
2958c2ecf20Sopenharmony_ci * Set S3C24xx Clock dividers
2968c2ecf20Sopenharmony_ci */
2978c2ecf20Sopenharmony_cistatic int s3c24xx_i2s_set_clkdiv(struct snd_soc_dai *cpu_dai,
2988c2ecf20Sopenharmony_ci	int div_id, int div)
2998c2ecf20Sopenharmony_ci{
3008c2ecf20Sopenharmony_ci	u32 reg;
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	switch (div_id) {
3038c2ecf20Sopenharmony_ci	case S3C24XX_DIV_BCLK:
3048c2ecf20Sopenharmony_ci		reg = readl(s3c24xx_i2s.regs + S3C2410_IISMOD) & ~S3C2410_IISMOD_FS_MASK;
3058c2ecf20Sopenharmony_ci		writel(reg | div, s3c24xx_i2s.regs + S3C2410_IISMOD);
3068c2ecf20Sopenharmony_ci		break;
3078c2ecf20Sopenharmony_ci	case S3C24XX_DIV_MCLK:
3088c2ecf20Sopenharmony_ci		reg = readl(s3c24xx_i2s.regs + S3C2410_IISMOD) & ~(S3C2410_IISMOD_384FS);
3098c2ecf20Sopenharmony_ci		writel(reg | div, s3c24xx_i2s.regs + S3C2410_IISMOD);
3108c2ecf20Sopenharmony_ci		break;
3118c2ecf20Sopenharmony_ci	case S3C24XX_DIV_PRESCALER:
3128c2ecf20Sopenharmony_ci		writel(div, s3c24xx_i2s.regs + S3C2410_IISPSR);
3138c2ecf20Sopenharmony_ci		reg = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
3148c2ecf20Sopenharmony_ci		writel(reg | S3C2410_IISCON_PSCEN, s3c24xx_i2s.regs + S3C2410_IISCON);
3158c2ecf20Sopenharmony_ci		break;
3168c2ecf20Sopenharmony_ci	default:
3178c2ecf20Sopenharmony_ci		return -EINVAL;
3188c2ecf20Sopenharmony_ci	}
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	return 0;
3218c2ecf20Sopenharmony_ci}
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci/*
3248c2ecf20Sopenharmony_ci * To avoid duplicating clock code, allow machine driver to
3258c2ecf20Sopenharmony_ci * get the clockrate from here.
3268c2ecf20Sopenharmony_ci */
3278c2ecf20Sopenharmony_ciu32 s3c24xx_i2s_get_clockrate(void)
3288c2ecf20Sopenharmony_ci{
3298c2ecf20Sopenharmony_ci	return clk_get_rate(s3c24xx_i2s.iis_clk);
3308c2ecf20Sopenharmony_ci}
3318c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(s3c24xx_i2s_get_clockrate);
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_cistatic int s3c24xx_i2s_probe(struct snd_soc_dai *dai)
3348c2ecf20Sopenharmony_ci{
3358c2ecf20Sopenharmony_ci	int ret;
3368c2ecf20Sopenharmony_ci	snd_soc_dai_init_dma_data(dai, &s3c24xx_i2s_pcm_stereo_out,
3378c2ecf20Sopenharmony_ci					&s3c24xx_i2s_pcm_stereo_in);
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	s3c24xx_i2s.iis_clk = devm_clk_get(dai->dev, "iis");
3408c2ecf20Sopenharmony_ci	if (IS_ERR(s3c24xx_i2s.iis_clk)) {
3418c2ecf20Sopenharmony_ci		pr_err("failed to get iis_clock\n");
3428c2ecf20Sopenharmony_ci		return PTR_ERR(s3c24xx_i2s.iis_clk);
3438c2ecf20Sopenharmony_ci	}
3448c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(s3c24xx_i2s.iis_clk);
3458c2ecf20Sopenharmony_ci	if (ret)
3468c2ecf20Sopenharmony_ci		return ret;
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	writel(S3C2410_IISCON_IISEN, s3c24xx_i2s.regs + S3C2410_IISCON);
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	s3c24xx_snd_txctrl(0);
3518c2ecf20Sopenharmony_ci	s3c24xx_snd_rxctrl(0);
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	return 0;
3548c2ecf20Sopenharmony_ci}
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
3578c2ecf20Sopenharmony_cistatic int s3c24xx_i2s_suspend(struct snd_soc_component *component)
3588c2ecf20Sopenharmony_ci{
3598c2ecf20Sopenharmony_ci	s3c24xx_i2s.iiscon = readl(s3c24xx_i2s.regs + S3C2410_IISCON);
3608c2ecf20Sopenharmony_ci	s3c24xx_i2s.iismod = readl(s3c24xx_i2s.regs + S3C2410_IISMOD);
3618c2ecf20Sopenharmony_ci	s3c24xx_i2s.iisfcon = readl(s3c24xx_i2s.regs + S3C2410_IISFCON);
3628c2ecf20Sopenharmony_ci	s3c24xx_i2s.iispsr = readl(s3c24xx_i2s.regs + S3C2410_IISPSR);
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	clk_disable_unprepare(s3c24xx_i2s.iis_clk);
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	return 0;
3678c2ecf20Sopenharmony_ci}
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_cistatic int s3c24xx_i2s_resume(struct snd_soc_component *component)
3708c2ecf20Sopenharmony_ci{
3718c2ecf20Sopenharmony_ci	int ret;
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(s3c24xx_i2s.iis_clk);
3748c2ecf20Sopenharmony_ci	if (ret)
3758c2ecf20Sopenharmony_ci		return ret;
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	writel(s3c24xx_i2s.iiscon, s3c24xx_i2s.regs + S3C2410_IISCON);
3788c2ecf20Sopenharmony_ci	writel(s3c24xx_i2s.iismod, s3c24xx_i2s.regs + S3C2410_IISMOD);
3798c2ecf20Sopenharmony_ci	writel(s3c24xx_i2s.iisfcon, s3c24xx_i2s.regs + S3C2410_IISFCON);
3808c2ecf20Sopenharmony_ci	writel(s3c24xx_i2s.iispsr, s3c24xx_i2s.regs + S3C2410_IISPSR);
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci	return 0;
3838c2ecf20Sopenharmony_ci}
3848c2ecf20Sopenharmony_ci#else
3858c2ecf20Sopenharmony_ci#define s3c24xx_i2s_suspend NULL
3868c2ecf20Sopenharmony_ci#define s3c24xx_i2s_resume NULL
3878c2ecf20Sopenharmony_ci#endif
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci#define S3C24XX_I2S_RATES \
3908c2ecf20Sopenharmony_ci	(SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 | SNDRV_PCM_RATE_16000 | \
3918c2ecf20Sopenharmony_ci	SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
3928c2ecf20Sopenharmony_ci	SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_cistatic const struct snd_soc_dai_ops s3c24xx_i2s_dai_ops = {
3958c2ecf20Sopenharmony_ci	.trigger	= s3c24xx_i2s_trigger,
3968c2ecf20Sopenharmony_ci	.hw_params	= s3c24xx_i2s_hw_params,
3978c2ecf20Sopenharmony_ci	.set_fmt	= s3c24xx_i2s_set_fmt,
3988c2ecf20Sopenharmony_ci	.set_clkdiv	= s3c24xx_i2s_set_clkdiv,
3998c2ecf20Sopenharmony_ci	.set_sysclk	= s3c24xx_i2s_set_sysclk,
4008c2ecf20Sopenharmony_ci};
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_cistatic struct snd_soc_dai_driver s3c24xx_i2s_dai = {
4038c2ecf20Sopenharmony_ci	.probe = s3c24xx_i2s_probe,
4048c2ecf20Sopenharmony_ci	.playback = {
4058c2ecf20Sopenharmony_ci		.channels_min = 2,
4068c2ecf20Sopenharmony_ci		.channels_max = 2,
4078c2ecf20Sopenharmony_ci		.rates = S3C24XX_I2S_RATES,
4088c2ecf20Sopenharmony_ci		.formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE,},
4098c2ecf20Sopenharmony_ci	.capture = {
4108c2ecf20Sopenharmony_ci		.channels_min = 2,
4118c2ecf20Sopenharmony_ci		.channels_max = 2,
4128c2ecf20Sopenharmony_ci		.rates = S3C24XX_I2S_RATES,
4138c2ecf20Sopenharmony_ci		.formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE,},
4148c2ecf20Sopenharmony_ci	.ops = &s3c24xx_i2s_dai_ops,
4158c2ecf20Sopenharmony_ci};
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_cistatic const struct snd_soc_component_driver s3c24xx_i2s_component = {
4188c2ecf20Sopenharmony_ci	.name		= "s3c24xx-i2s",
4198c2ecf20Sopenharmony_ci	.suspend	= s3c24xx_i2s_suspend,
4208c2ecf20Sopenharmony_ci	.resume		= s3c24xx_i2s_resume,
4218c2ecf20Sopenharmony_ci};
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_cistatic int s3c24xx_iis_dev_probe(struct platform_device *pdev)
4248c2ecf20Sopenharmony_ci{
4258c2ecf20Sopenharmony_ci	struct resource *res;
4268c2ecf20Sopenharmony_ci	int ret;
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4298c2ecf20Sopenharmony_ci	s3c24xx_i2s.regs = devm_ioremap_resource(&pdev->dev, res);
4308c2ecf20Sopenharmony_ci	if (IS_ERR(s3c24xx_i2s.regs))
4318c2ecf20Sopenharmony_ci		return PTR_ERR(s3c24xx_i2s.regs);
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	s3c24xx_i2s_pcm_stereo_out.addr = res->start + S3C2410_IISFIFO;
4348c2ecf20Sopenharmony_ci	s3c24xx_i2s_pcm_stereo_in.addr = res->start + S3C2410_IISFIFO;
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci	ret = samsung_asoc_dma_platform_register(&pdev->dev, NULL,
4378c2ecf20Sopenharmony_ci						 "tx", "rx", NULL);
4388c2ecf20Sopenharmony_ci	if (ret) {
4398c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Failed to register the DMA: %d\n", ret);
4408c2ecf20Sopenharmony_ci		return ret;
4418c2ecf20Sopenharmony_ci	}
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	ret = devm_snd_soc_register_component(&pdev->dev,
4448c2ecf20Sopenharmony_ci			&s3c24xx_i2s_component, &s3c24xx_i2s_dai, 1);
4458c2ecf20Sopenharmony_ci	if (ret)
4468c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Failed to register the DAI\n");
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	return ret;
4498c2ecf20Sopenharmony_ci}
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_cistatic struct platform_driver s3c24xx_iis_driver = {
4528c2ecf20Sopenharmony_ci	.probe  = s3c24xx_iis_dev_probe,
4538c2ecf20Sopenharmony_ci	.driver = {
4548c2ecf20Sopenharmony_ci		.name = "s3c24xx-iis",
4558c2ecf20Sopenharmony_ci	},
4568c2ecf20Sopenharmony_ci};
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_cimodule_platform_driver(s3c24xx_iis_driver);
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci/* Module information */
4618c2ecf20Sopenharmony_ciMODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
4628c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("s3c24xx I2S SoC Interface");
4638c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
4648c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:s3c24xx-iis");
465