18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Au12x0/Au1550 PSC ALSA ASoC audio support.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * (c) 2007-2008 MSC Vertriebsges.m.b.H.,
68c2ecf20Sopenharmony_ci *	Manuel Lauss <manuel.lauss@gmail.com>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Au1xxx-PSC I2S glue.
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * NOTE: so far only PSC slave mode (bit- and frameclock) is supported.
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/init.h>
148c2ecf20Sopenharmony_ci#include <linux/module.h>
158c2ecf20Sopenharmony_ci#include <linux/slab.h>
168c2ecf20Sopenharmony_ci#include <linux/suspend.h>
178c2ecf20Sopenharmony_ci#include <sound/core.h>
188c2ecf20Sopenharmony_ci#include <sound/pcm.h>
198c2ecf20Sopenharmony_ci#include <sound/initval.h>
208c2ecf20Sopenharmony_ci#include <sound/soc.h>
218c2ecf20Sopenharmony_ci#include <asm/mach-au1x00/au1000.h>
228c2ecf20Sopenharmony_ci#include <asm/mach-au1x00/au1xxx_psc.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#include "psc.h"
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci/* supported I2S DAI hardware formats */
278c2ecf20Sopenharmony_ci#define AU1XPSC_I2S_DAIFMT \
288c2ecf20Sopenharmony_ci	(SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_LEFT_J |	\
298c2ecf20Sopenharmony_ci	 SND_SOC_DAIFMT_NB_NF)
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci/* supported I2S direction */
328c2ecf20Sopenharmony_ci#define AU1XPSC_I2S_DIR \
338c2ecf20Sopenharmony_ci	(SND_SOC_DAIDIR_PLAYBACK | SND_SOC_DAIDIR_CAPTURE)
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#define AU1XPSC_I2S_RATES \
368c2ecf20Sopenharmony_ci	SNDRV_PCM_RATE_8000_192000
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#define AU1XPSC_I2S_FMTS \
398c2ecf20Sopenharmony_ci	(SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE)
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#define I2SSTAT_BUSY(stype)	\
428c2ecf20Sopenharmony_ci	((stype) == SNDRV_PCM_STREAM_PLAYBACK ? PSC_I2SSTAT_TB : PSC_I2SSTAT_RB)
438c2ecf20Sopenharmony_ci#define I2SPCR_START(stype)	\
448c2ecf20Sopenharmony_ci	((stype) == SNDRV_PCM_STREAM_PLAYBACK ? PSC_I2SPCR_TS : PSC_I2SPCR_RS)
458c2ecf20Sopenharmony_ci#define I2SPCR_STOP(stype)	\
468c2ecf20Sopenharmony_ci	((stype) == SNDRV_PCM_STREAM_PLAYBACK ? PSC_I2SPCR_TP : PSC_I2SPCR_RP)
478c2ecf20Sopenharmony_ci#define I2SPCR_CLRFIFO(stype)	\
488c2ecf20Sopenharmony_ci	((stype) == SNDRV_PCM_STREAM_PLAYBACK ? PSC_I2SPCR_TC : PSC_I2SPCR_RC)
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic int au1xpsc_i2s_set_fmt(struct snd_soc_dai *cpu_dai,
528c2ecf20Sopenharmony_ci			       unsigned int fmt)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	struct au1xpsc_audio_data *pscdata = snd_soc_dai_get_drvdata(cpu_dai);
558c2ecf20Sopenharmony_ci	unsigned long ct;
568c2ecf20Sopenharmony_ci	int ret;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	ret = -EINVAL;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	ct = pscdata->cfg;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	ct &= ~(PSC_I2SCFG_XM | PSC_I2SCFG_MLJ);	/* left-justified */
638c2ecf20Sopenharmony_ci	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
648c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_I2S:
658c2ecf20Sopenharmony_ci		ct |= PSC_I2SCFG_XM;	/* enable I2S mode */
668c2ecf20Sopenharmony_ci		break;
678c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_MSB:
688c2ecf20Sopenharmony_ci		break;
698c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_LSB:
708c2ecf20Sopenharmony_ci		ct |= PSC_I2SCFG_MLJ;	/* LSB (right-) justified */
718c2ecf20Sopenharmony_ci		break;
728c2ecf20Sopenharmony_ci	default:
738c2ecf20Sopenharmony_ci		goto out;
748c2ecf20Sopenharmony_ci	}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	ct &= ~(PSC_I2SCFG_BI | PSC_I2SCFG_WI);		/* IB-IF */
778c2ecf20Sopenharmony_ci	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
788c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_NB_NF:
798c2ecf20Sopenharmony_ci		ct |= PSC_I2SCFG_BI | PSC_I2SCFG_WI;
808c2ecf20Sopenharmony_ci		break;
818c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_NB_IF:
828c2ecf20Sopenharmony_ci		ct |= PSC_I2SCFG_BI;
838c2ecf20Sopenharmony_ci		break;
848c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_IB_NF:
858c2ecf20Sopenharmony_ci		ct |= PSC_I2SCFG_WI;
868c2ecf20Sopenharmony_ci		break;
878c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_IB_IF:
888c2ecf20Sopenharmony_ci		break;
898c2ecf20Sopenharmony_ci	default:
908c2ecf20Sopenharmony_ci		goto out;
918c2ecf20Sopenharmony_ci	}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
948c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_CBM_CFM:	/* CODEC master */
958c2ecf20Sopenharmony_ci		ct |= PSC_I2SCFG_MS;	/* PSC I2S slave mode */
968c2ecf20Sopenharmony_ci		break;
978c2ecf20Sopenharmony_ci	case SND_SOC_DAIFMT_CBS_CFS:	/* CODEC slave */
988c2ecf20Sopenharmony_ci		ct &= ~PSC_I2SCFG_MS;	/* PSC I2S Master mode */
998c2ecf20Sopenharmony_ci		break;
1008c2ecf20Sopenharmony_ci	default:
1018c2ecf20Sopenharmony_ci		goto out;
1028c2ecf20Sopenharmony_ci	}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	pscdata->cfg = ct;
1058c2ecf20Sopenharmony_ci	ret = 0;
1068c2ecf20Sopenharmony_ciout:
1078c2ecf20Sopenharmony_ci	return ret;
1088c2ecf20Sopenharmony_ci}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_cistatic int au1xpsc_i2s_hw_params(struct snd_pcm_substream *substream,
1118c2ecf20Sopenharmony_ci				 struct snd_pcm_hw_params *params,
1128c2ecf20Sopenharmony_ci				 struct snd_soc_dai *dai)
1138c2ecf20Sopenharmony_ci{
1148c2ecf20Sopenharmony_ci	struct au1xpsc_audio_data *pscdata = snd_soc_dai_get_drvdata(dai);
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	int cfgbits;
1178c2ecf20Sopenharmony_ci	unsigned long stat;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	/* check if the PSC is already streaming data */
1208c2ecf20Sopenharmony_ci	stat = __raw_readl(I2S_STAT(pscdata));
1218c2ecf20Sopenharmony_ci	if (stat & (PSC_I2SSTAT_TB | PSC_I2SSTAT_RB)) {
1228c2ecf20Sopenharmony_ci		/* reject parameters not currently set up in hardware */
1238c2ecf20Sopenharmony_ci		cfgbits = __raw_readl(I2S_CFG(pscdata));
1248c2ecf20Sopenharmony_ci		if ((PSC_I2SCFG_GET_LEN(cfgbits) != params->msbits) ||
1258c2ecf20Sopenharmony_ci		    (params_rate(params) != pscdata->rate))
1268c2ecf20Sopenharmony_ci			return -EINVAL;
1278c2ecf20Sopenharmony_ci	} else {
1288c2ecf20Sopenharmony_ci		/* set sample bitdepth */
1298c2ecf20Sopenharmony_ci		pscdata->cfg &= ~(0x1f << 4);
1308c2ecf20Sopenharmony_ci		pscdata->cfg |= PSC_I2SCFG_SET_LEN(params->msbits);
1318c2ecf20Sopenharmony_ci		/* remember current rate for other stream */
1328c2ecf20Sopenharmony_ci		pscdata->rate = params_rate(params);
1338c2ecf20Sopenharmony_ci	}
1348c2ecf20Sopenharmony_ci	return 0;
1358c2ecf20Sopenharmony_ci}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci/* Configure PSC late:  on my devel systems the codec  is I2S master and
1388c2ecf20Sopenharmony_ci * supplies the i2sbitclock __AND__ i2sMclk (!) to the PSC unit.  ASoC
1398c2ecf20Sopenharmony_ci * uses aggressive PM and  switches the codec off  when it is not in use
1408c2ecf20Sopenharmony_ci * which also means the PSC unit doesn't get any clocks and is therefore
1418c2ecf20Sopenharmony_ci * dead. That's why this chunk here gets called from the trigger callback
1428c2ecf20Sopenharmony_ci * because I can be reasonably certain the codec is driving the clocks.
1438c2ecf20Sopenharmony_ci */
1448c2ecf20Sopenharmony_cistatic int au1xpsc_i2s_configure(struct au1xpsc_audio_data *pscdata)
1458c2ecf20Sopenharmony_ci{
1468c2ecf20Sopenharmony_ci	unsigned long tmo;
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	/* bring PSC out of sleep, and configure I2S unit */
1498c2ecf20Sopenharmony_ci	__raw_writel(PSC_CTRL_ENABLE, PSC_CTRL(pscdata));
1508c2ecf20Sopenharmony_ci	wmb(); /* drain writebuffer */
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	tmo = 1000000;
1538c2ecf20Sopenharmony_ci	while (!(__raw_readl(I2S_STAT(pscdata)) & PSC_I2SSTAT_SR) && tmo)
1548c2ecf20Sopenharmony_ci		tmo--;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	if (!tmo)
1578c2ecf20Sopenharmony_ci		goto psc_err;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	__raw_writel(0, I2S_CFG(pscdata));
1608c2ecf20Sopenharmony_ci	wmb(); /* drain writebuffer */
1618c2ecf20Sopenharmony_ci	__raw_writel(pscdata->cfg | PSC_I2SCFG_DE_ENABLE, I2S_CFG(pscdata));
1628c2ecf20Sopenharmony_ci	wmb(); /* drain writebuffer */
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	/* wait for I2S controller to become ready */
1658c2ecf20Sopenharmony_ci	tmo = 1000000;
1668c2ecf20Sopenharmony_ci	while (!(__raw_readl(I2S_STAT(pscdata)) & PSC_I2SSTAT_DR) && tmo)
1678c2ecf20Sopenharmony_ci		tmo--;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	if (tmo)
1708c2ecf20Sopenharmony_ci		return 0;
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_cipsc_err:
1738c2ecf20Sopenharmony_ci	__raw_writel(0, I2S_CFG(pscdata));
1748c2ecf20Sopenharmony_ci	__raw_writel(PSC_CTRL_SUSPEND, PSC_CTRL(pscdata));
1758c2ecf20Sopenharmony_ci	wmb(); /* drain writebuffer */
1768c2ecf20Sopenharmony_ci	return -ETIMEDOUT;
1778c2ecf20Sopenharmony_ci}
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_cistatic int au1xpsc_i2s_start(struct au1xpsc_audio_data *pscdata, int stype)
1808c2ecf20Sopenharmony_ci{
1818c2ecf20Sopenharmony_ci	unsigned long tmo, stat;
1828c2ecf20Sopenharmony_ci	int ret;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	ret = 0;
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	/* if both TX and RX are idle, configure the PSC  */
1878c2ecf20Sopenharmony_ci	stat = __raw_readl(I2S_STAT(pscdata));
1888c2ecf20Sopenharmony_ci	if (!(stat & (PSC_I2SSTAT_TB | PSC_I2SSTAT_RB))) {
1898c2ecf20Sopenharmony_ci		ret = au1xpsc_i2s_configure(pscdata);
1908c2ecf20Sopenharmony_ci		if (ret)
1918c2ecf20Sopenharmony_ci			goto out;
1928c2ecf20Sopenharmony_ci	}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	__raw_writel(I2SPCR_CLRFIFO(stype), I2S_PCR(pscdata));
1958c2ecf20Sopenharmony_ci	wmb(); /* drain writebuffer */
1968c2ecf20Sopenharmony_ci	__raw_writel(I2SPCR_START(stype), I2S_PCR(pscdata));
1978c2ecf20Sopenharmony_ci	wmb(); /* drain writebuffer */
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	/* wait for start confirmation */
2008c2ecf20Sopenharmony_ci	tmo = 1000000;
2018c2ecf20Sopenharmony_ci	while (!(__raw_readl(I2S_STAT(pscdata)) & I2SSTAT_BUSY(stype)) && tmo)
2028c2ecf20Sopenharmony_ci		tmo--;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	if (!tmo) {
2058c2ecf20Sopenharmony_ci		__raw_writel(I2SPCR_STOP(stype), I2S_PCR(pscdata));
2068c2ecf20Sopenharmony_ci		wmb(); /* drain writebuffer */
2078c2ecf20Sopenharmony_ci		ret = -ETIMEDOUT;
2088c2ecf20Sopenharmony_ci	}
2098c2ecf20Sopenharmony_ciout:
2108c2ecf20Sopenharmony_ci	return ret;
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_cistatic int au1xpsc_i2s_stop(struct au1xpsc_audio_data *pscdata, int stype)
2148c2ecf20Sopenharmony_ci{
2158c2ecf20Sopenharmony_ci	unsigned long tmo, stat;
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	__raw_writel(I2SPCR_STOP(stype), I2S_PCR(pscdata));
2188c2ecf20Sopenharmony_ci	wmb(); /* drain writebuffer */
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	/* wait for stop confirmation */
2218c2ecf20Sopenharmony_ci	tmo = 1000000;
2228c2ecf20Sopenharmony_ci	while ((__raw_readl(I2S_STAT(pscdata)) & I2SSTAT_BUSY(stype)) && tmo)
2238c2ecf20Sopenharmony_ci		tmo--;
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	/* if both TX and RX are idle, disable PSC */
2268c2ecf20Sopenharmony_ci	stat = __raw_readl(I2S_STAT(pscdata));
2278c2ecf20Sopenharmony_ci	if (!(stat & (PSC_I2SSTAT_TB | PSC_I2SSTAT_RB))) {
2288c2ecf20Sopenharmony_ci		__raw_writel(0, I2S_CFG(pscdata));
2298c2ecf20Sopenharmony_ci		wmb(); /* drain writebuffer */
2308c2ecf20Sopenharmony_ci		__raw_writel(PSC_CTRL_SUSPEND, PSC_CTRL(pscdata));
2318c2ecf20Sopenharmony_ci		wmb(); /* drain writebuffer */
2328c2ecf20Sopenharmony_ci	}
2338c2ecf20Sopenharmony_ci	return 0;
2348c2ecf20Sopenharmony_ci}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_cistatic int au1xpsc_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
2378c2ecf20Sopenharmony_ci			       struct snd_soc_dai *dai)
2388c2ecf20Sopenharmony_ci{
2398c2ecf20Sopenharmony_ci	struct au1xpsc_audio_data *pscdata = snd_soc_dai_get_drvdata(dai);
2408c2ecf20Sopenharmony_ci	int ret, stype = substream->stream;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	switch (cmd) {
2438c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_START:
2448c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_RESUME:
2458c2ecf20Sopenharmony_ci		ret = au1xpsc_i2s_start(pscdata, stype);
2468c2ecf20Sopenharmony_ci		break;
2478c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_STOP:
2488c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_SUSPEND:
2498c2ecf20Sopenharmony_ci		ret = au1xpsc_i2s_stop(pscdata, stype);
2508c2ecf20Sopenharmony_ci		break;
2518c2ecf20Sopenharmony_ci	default:
2528c2ecf20Sopenharmony_ci		ret = -EINVAL;
2538c2ecf20Sopenharmony_ci	}
2548c2ecf20Sopenharmony_ci	return ret;
2558c2ecf20Sopenharmony_ci}
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_cistatic int au1xpsc_i2s_startup(struct snd_pcm_substream *substream,
2588c2ecf20Sopenharmony_ci			       struct snd_soc_dai *dai)
2598c2ecf20Sopenharmony_ci{
2608c2ecf20Sopenharmony_ci	struct au1xpsc_audio_data *pscdata = snd_soc_dai_get_drvdata(dai);
2618c2ecf20Sopenharmony_ci	snd_soc_dai_set_dma_data(dai, substream, &pscdata->dmaids[0]);
2628c2ecf20Sopenharmony_ci	return 0;
2638c2ecf20Sopenharmony_ci}
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_cistatic const struct snd_soc_dai_ops au1xpsc_i2s_dai_ops = {
2668c2ecf20Sopenharmony_ci	.startup	= au1xpsc_i2s_startup,
2678c2ecf20Sopenharmony_ci	.trigger	= au1xpsc_i2s_trigger,
2688c2ecf20Sopenharmony_ci	.hw_params	= au1xpsc_i2s_hw_params,
2698c2ecf20Sopenharmony_ci	.set_fmt	= au1xpsc_i2s_set_fmt,
2708c2ecf20Sopenharmony_ci};
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_cistatic const struct snd_soc_dai_driver au1xpsc_i2s_dai_template = {
2738c2ecf20Sopenharmony_ci	.playback = {
2748c2ecf20Sopenharmony_ci		.rates		= AU1XPSC_I2S_RATES,
2758c2ecf20Sopenharmony_ci		.formats	= AU1XPSC_I2S_FMTS,
2768c2ecf20Sopenharmony_ci		.channels_min	= 2,
2778c2ecf20Sopenharmony_ci		.channels_max	= 8,	/* 2 without external help */
2788c2ecf20Sopenharmony_ci	},
2798c2ecf20Sopenharmony_ci	.capture = {
2808c2ecf20Sopenharmony_ci		.rates		= AU1XPSC_I2S_RATES,
2818c2ecf20Sopenharmony_ci		.formats	= AU1XPSC_I2S_FMTS,
2828c2ecf20Sopenharmony_ci		.channels_min	= 2,
2838c2ecf20Sopenharmony_ci		.channels_max	= 8,	/* 2 without external help */
2848c2ecf20Sopenharmony_ci	},
2858c2ecf20Sopenharmony_ci	.ops = &au1xpsc_i2s_dai_ops,
2868c2ecf20Sopenharmony_ci};
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_cistatic const struct snd_soc_component_driver au1xpsc_i2s_component = {
2898c2ecf20Sopenharmony_ci	.name		= "au1xpsc-i2s",
2908c2ecf20Sopenharmony_ci};
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_cistatic int au1xpsc_i2s_drvprobe(struct platform_device *pdev)
2938c2ecf20Sopenharmony_ci{
2948c2ecf20Sopenharmony_ci	struct resource *dmares;
2958c2ecf20Sopenharmony_ci	unsigned long sel;
2968c2ecf20Sopenharmony_ci	struct au1xpsc_audio_data *wd;
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	wd = devm_kzalloc(&pdev->dev, sizeof(struct au1xpsc_audio_data),
2998c2ecf20Sopenharmony_ci			  GFP_KERNEL);
3008c2ecf20Sopenharmony_ci	if (!wd)
3018c2ecf20Sopenharmony_ci		return -ENOMEM;
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	wd->mmio = devm_platform_ioremap_resource(pdev, 0);
3048c2ecf20Sopenharmony_ci	if (IS_ERR(wd->mmio))
3058c2ecf20Sopenharmony_ci		return PTR_ERR(wd->mmio);
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	dmares = platform_get_resource(pdev, IORESOURCE_DMA, 0);
3088c2ecf20Sopenharmony_ci	if (!dmares)
3098c2ecf20Sopenharmony_ci		return -EBUSY;
3108c2ecf20Sopenharmony_ci	wd->dmaids[SNDRV_PCM_STREAM_PLAYBACK] = dmares->start;
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	dmares = platform_get_resource(pdev, IORESOURCE_DMA, 1);
3138c2ecf20Sopenharmony_ci	if (!dmares)
3148c2ecf20Sopenharmony_ci		return -EBUSY;
3158c2ecf20Sopenharmony_ci	wd->dmaids[SNDRV_PCM_STREAM_CAPTURE] = dmares->start;
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	/* preserve PSC clock source set up by platform (dev.platform_data
3188c2ecf20Sopenharmony_ci	 * is already occupied by soc layer)
3198c2ecf20Sopenharmony_ci	 */
3208c2ecf20Sopenharmony_ci	sel = __raw_readl(PSC_SEL(wd)) & PSC_SEL_CLK_MASK;
3218c2ecf20Sopenharmony_ci	__raw_writel(PSC_CTRL_DISABLE, PSC_CTRL(wd));
3228c2ecf20Sopenharmony_ci	wmb(); /* drain writebuffer */
3238c2ecf20Sopenharmony_ci	__raw_writel(PSC_SEL_PS_I2SMODE | sel, PSC_SEL(wd));
3248c2ecf20Sopenharmony_ci	__raw_writel(0, I2S_CFG(wd));
3258c2ecf20Sopenharmony_ci	wmb(); /* drain writebuffer */
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	/* preconfigure: set max rx/tx fifo depths */
3288c2ecf20Sopenharmony_ci	wd->cfg |= PSC_I2SCFG_RT_FIFO8 | PSC_I2SCFG_TT_FIFO8;
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	/* don't wait for I2S core to become ready now; clocks may not
3318c2ecf20Sopenharmony_ci	 * be running yet; depending on clock input for PSC a wait might
3328c2ecf20Sopenharmony_ci	 * time out.
3338c2ecf20Sopenharmony_ci	 */
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	/* name the DAI like this device instance ("au1xpsc-i2s.PSCINDEX") */
3368c2ecf20Sopenharmony_ci	memcpy(&wd->dai_drv, &au1xpsc_i2s_dai_template,
3378c2ecf20Sopenharmony_ci	       sizeof(struct snd_soc_dai_driver));
3388c2ecf20Sopenharmony_ci	wd->dai_drv.name = dev_name(&pdev->dev);
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, wd);
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci	return devm_snd_soc_register_component(&pdev->dev,
3438c2ecf20Sopenharmony_ci				&au1xpsc_i2s_component, &wd->dai_drv, 1);
3448c2ecf20Sopenharmony_ci}
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_cistatic int au1xpsc_i2s_drvremove(struct platform_device *pdev)
3478c2ecf20Sopenharmony_ci{
3488c2ecf20Sopenharmony_ci	struct au1xpsc_audio_data *wd = platform_get_drvdata(pdev);
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	__raw_writel(0, I2S_CFG(wd));
3518c2ecf20Sopenharmony_ci	wmb(); /* drain writebuffer */
3528c2ecf20Sopenharmony_ci	__raw_writel(PSC_CTRL_DISABLE, PSC_CTRL(wd));
3538c2ecf20Sopenharmony_ci	wmb(); /* drain writebuffer */
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	return 0;
3568c2ecf20Sopenharmony_ci}
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
3598c2ecf20Sopenharmony_cistatic int au1xpsc_i2s_drvsuspend(struct device *dev)
3608c2ecf20Sopenharmony_ci{
3618c2ecf20Sopenharmony_ci	struct au1xpsc_audio_data *wd = dev_get_drvdata(dev);
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	/* save interesting register and disable PSC */
3648c2ecf20Sopenharmony_ci	wd->pm[0] = __raw_readl(PSC_SEL(wd));
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	__raw_writel(0, I2S_CFG(wd));
3678c2ecf20Sopenharmony_ci	wmb(); /* drain writebuffer */
3688c2ecf20Sopenharmony_ci	__raw_writel(PSC_CTRL_DISABLE, PSC_CTRL(wd));
3698c2ecf20Sopenharmony_ci	wmb(); /* drain writebuffer */
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	return 0;
3728c2ecf20Sopenharmony_ci}
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_cistatic int au1xpsc_i2s_drvresume(struct device *dev)
3758c2ecf20Sopenharmony_ci{
3768c2ecf20Sopenharmony_ci	struct au1xpsc_audio_data *wd = dev_get_drvdata(dev);
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	/* select I2S mode and PSC clock */
3798c2ecf20Sopenharmony_ci	__raw_writel(PSC_CTRL_DISABLE, PSC_CTRL(wd));
3808c2ecf20Sopenharmony_ci	wmb(); /* drain writebuffer */
3818c2ecf20Sopenharmony_ci	__raw_writel(0, PSC_SEL(wd));
3828c2ecf20Sopenharmony_ci	wmb(); /* drain writebuffer */
3838c2ecf20Sopenharmony_ci	__raw_writel(wd->pm[0], PSC_SEL(wd));
3848c2ecf20Sopenharmony_ci	wmb(); /* drain writebuffer */
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	return 0;
3878c2ecf20Sopenharmony_ci}
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_cistatic const struct dev_pm_ops au1xpsci2s_pmops = {
3908c2ecf20Sopenharmony_ci	.suspend	= au1xpsc_i2s_drvsuspend,
3918c2ecf20Sopenharmony_ci	.resume		= au1xpsc_i2s_drvresume,
3928c2ecf20Sopenharmony_ci};
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci#define AU1XPSCI2S_PMOPS &au1xpsci2s_pmops
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci#else
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci#define AU1XPSCI2S_PMOPS NULL
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci#endif
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_cistatic struct platform_driver au1xpsc_i2s_driver = {
4038c2ecf20Sopenharmony_ci	.driver		= {
4048c2ecf20Sopenharmony_ci		.name	= "au1xpsc_i2s",
4058c2ecf20Sopenharmony_ci		.pm	= AU1XPSCI2S_PMOPS,
4068c2ecf20Sopenharmony_ci	},
4078c2ecf20Sopenharmony_ci	.probe		= au1xpsc_i2s_drvprobe,
4088c2ecf20Sopenharmony_ci	.remove		= au1xpsc_i2s_drvremove,
4098c2ecf20Sopenharmony_ci};
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_cimodule_platform_driver(au1xpsc_i2s_driver);
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
4148c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Au12x0/Au1550 PSC I2S ALSA ASoC audio driver");
4158c2ecf20Sopenharmony_ciMODULE_AUTHOR("Manuel Lauss");
416