18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: (GPL-2.0 OR MIT)
28c2ecf20Sopenharmony_ci//
38c2ecf20Sopenharmony_ci// Copyright (c) 2018 BayLibre, SAS.
48c2ecf20Sopenharmony_ci// Author: Jerome Brunet <jbrunet@baylibre.com>
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/clk.h>
78c2ecf20Sopenharmony_ci#include <linux/module.h>
88c2ecf20Sopenharmony_ci#include <linux/of_platform.h>
98c2ecf20Sopenharmony_ci#include <linux/regmap.h>
108c2ecf20Sopenharmony_ci#include <sound/soc.h>
118c2ecf20Sopenharmony_ci#include <sound/soc-dai.h>
128c2ecf20Sopenharmony_ci#include <sound/pcm_params.h>
138c2ecf20Sopenharmony_ci#include <sound/pcm_iec958.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci/*
168c2ecf20Sopenharmony_ci * NOTE:
178c2ecf20Sopenharmony_ci * The meaning of bits SPDIFOUT_CTRL0_XXX_SEL is actually the opposite
188c2ecf20Sopenharmony_ci * of what the documentation says. Manual control on V, U and C bits is
198c2ecf20Sopenharmony_ci * applied when the related sel bits are cleared
208c2ecf20Sopenharmony_ci */
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define SPDIFOUT_STAT			0x00
238c2ecf20Sopenharmony_ci#define SPDIFOUT_GAIN0			0x04
248c2ecf20Sopenharmony_ci#define SPDIFOUT_GAIN1			0x08
258c2ecf20Sopenharmony_ci#define SPDIFOUT_CTRL0			0x0c
268c2ecf20Sopenharmony_ci#define  SPDIFOUT_CTRL0_EN		BIT(31)
278c2ecf20Sopenharmony_ci#define  SPDIFOUT_CTRL0_RST_OUT		BIT(29)
288c2ecf20Sopenharmony_ci#define  SPDIFOUT_CTRL0_RST_IN		BIT(28)
298c2ecf20Sopenharmony_ci#define  SPDIFOUT_CTRL0_USEL		BIT(26)
308c2ecf20Sopenharmony_ci#define  SPDIFOUT_CTRL0_USET		BIT(25)
318c2ecf20Sopenharmony_ci#define  SPDIFOUT_CTRL0_CHSTS_SEL	BIT(24)
328c2ecf20Sopenharmony_ci#define  SPDIFOUT_CTRL0_DATA_SEL	BIT(20)
338c2ecf20Sopenharmony_ci#define  SPDIFOUT_CTRL0_MSB_FIRST	BIT(19)
348c2ecf20Sopenharmony_ci#define  SPDIFOUT_CTRL0_VSEL		BIT(18)
358c2ecf20Sopenharmony_ci#define  SPDIFOUT_CTRL0_VSET		BIT(17)
368c2ecf20Sopenharmony_ci#define  SPDIFOUT_CTRL0_MASK_MASK	GENMASK(11, 4)
378c2ecf20Sopenharmony_ci#define  SPDIFOUT_CTRL0_MASK(x)		((x) << 4)
388c2ecf20Sopenharmony_ci#define SPDIFOUT_CTRL1			0x10
398c2ecf20Sopenharmony_ci#define  SPDIFOUT_CTRL1_MSB_POS_MASK	GENMASK(12, 8)
408c2ecf20Sopenharmony_ci#define  SPDIFOUT_CTRL1_MSB_POS(x)	((x) << 8)
418c2ecf20Sopenharmony_ci#define  SPDIFOUT_CTRL1_TYPE_MASK	GENMASK(6, 4)
428c2ecf20Sopenharmony_ci#define  SPDIFOUT_CTRL1_TYPE(x)		((x) << 4)
438c2ecf20Sopenharmony_ci#define SPDIFOUT_PREAMB			0x14
448c2ecf20Sopenharmony_ci#define SPDIFOUT_SWAP			0x18
458c2ecf20Sopenharmony_ci#define SPDIFOUT_CHSTS0			0x1c
468c2ecf20Sopenharmony_ci#define SPDIFOUT_CHSTS1			0x20
478c2ecf20Sopenharmony_ci#define SPDIFOUT_CHSTS2			0x24
488c2ecf20Sopenharmony_ci#define SPDIFOUT_CHSTS3			0x28
498c2ecf20Sopenharmony_ci#define SPDIFOUT_CHSTS4			0x2c
508c2ecf20Sopenharmony_ci#define SPDIFOUT_CHSTS5			0x30
518c2ecf20Sopenharmony_ci#define SPDIFOUT_CHSTS6			0x34
528c2ecf20Sopenharmony_ci#define SPDIFOUT_CHSTS7			0x38
538c2ecf20Sopenharmony_ci#define SPDIFOUT_CHSTS8			0x3c
548c2ecf20Sopenharmony_ci#define SPDIFOUT_CHSTS9			0x40
558c2ecf20Sopenharmony_ci#define SPDIFOUT_CHSTSA			0x44
568c2ecf20Sopenharmony_ci#define SPDIFOUT_CHSTSB			0x48
578c2ecf20Sopenharmony_ci#define SPDIFOUT_MUTE_VAL		0x4c
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cistruct axg_spdifout {
608c2ecf20Sopenharmony_ci	struct regmap *map;
618c2ecf20Sopenharmony_ci	struct clk *mclk;
628c2ecf20Sopenharmony_ci	struct clk *pclk;
638c2ecf20Sopenharmony_ci};
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistatic void axg_spdifout_enable(struct regmap *map)
668c2ecf20Sopenharmony_ci{
678c2ecf20Sopenharmony_ci	/* Apply both reset */
688c2ecf20Sopenharmony_ci	regmap_update_bits(map, SPDIFOUT_CTRL0,
698c2ecf20Sopenharmony_ci			   SPDIFOUT_CTRL0_RST_OUT | SPDIFOUT_CTRL0_RST_IN,
708c2ecf20Sopenharmony_ci			   0);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	/* Clear out reset before in reset */
738c2ecf20Sopenharmony_ci	regmap_update_bits(map, SPDIFOUT_CTRL0,
748c2ecf20Sopenharmony_ci			   SPDIFOUT_CTRL0_RST_OUT, SPDIFOUT_CTRL0_RST_OUT);
758c2ecf20Sopenharmony_ci	regmap_update_bits(map, SPDIFOUT_CTRL0,
768c2ecf20Sopenharmony_ci			   SPDIFOUT_CTRL0_RST_IN,  SPDIFOUT_CTRL0_RST_IN);
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	/* Enable spdifout */
798c2ecf20Sopenharmony_ci	regmap_update_bits(map, SPDIFOUT_CTRL0, SPDIFOUT_CTRL0_EN,
808c2ecf20Sopenharmony_ci			   SPDIFOUT_CTRL0_EN);
818c2ecf20Sopenharmony_ci}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cistatic void axg_spdifout_disable(struct regmap *map)
848c2ecf20Sopenharmony_ci{
858c2ecf20Sopenharmony_ci	regmap_update_bits(map, SPDIFOUT_CTRL0, SPDIFOUT_CTRL0_EN, 0);
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic int axg_spdifout_trigger(struct snd_pcm_substream *substream, int cmd,
898c2ecf20Sopenharmony_ci				struct snd_soc_dai *dai)
908c2ecf20Sopenharmony_ci{
918c2ecf20Sopenharmony_ci	struct axg_spdifout *priv = snd_soc_dai_get_drvdata(dai);
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	switch (cmd) {
948c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_START:
958c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_RESUME:
968c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
978c2ecf20Sopenharmony_ci		axg_spdifout_enable(priv->map);
988c2ecf20Sopenharmony_ci		return 0;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_STOP:
1018c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_SUSPEND:
1028c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1038c2ecf20Sopenharmony_ci		axg_spdifout_disable(priv->map);
1048c2ecf20Sopenharmony_ci		return 0;
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	default:
1078c2ecf20Sopenharmony_ci		return -EINVAL;
1088c2ecf20Sopenharmony_ci	}
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistatic int axg_spdifout_mute(struct snd_soc_dai *dai, int mute, int direction)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	struct axg_spdifout *priv = snd_soc_dai_get_drvdata(dai);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	/* Use spdif valid bit to perform digital mute */
1168c2ecf20Sopenharmony_ci	regmap_update_bits(priv->map, SPDIFOUT_CTRL0, SPDIFOUT_CTRL0_VSET,
1178c2ecf20Sopenharmony_ci			   mute ? SPDIFOUT_CTRL0_VSET : 0);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	return 0;
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistatic int axg_spdifout_sample_fmt(struct snd_pcm_hw_params *params,
1238c2ecf20Sopenharmony_ci				   struct snd_soc_dai *dai)
1248c2ecf20Sopenharmony_ci{
1258c2ecf20Sopenharmony_ci	struct axg_spdifout *priv = snd_soc_dai_get_drvdata(dai);
1268c2ecf20Sopenharmony_ci	unsigned int val;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	/* Set the samples spdifout will pull from the FIFO */
1298c2ecf20Sopenharmony_ci	switch (params_channels(params)) {
1308c2ecf20Sopenharmony_ci	case 1:
1318c2ecf20Sopenharmony_ci		val = SPDIFOUT_CTRL0_MASK(0x1);
1328c2ecf20Sopenharmony_ci		break;
1338c2ecf20Sopenharmony_ci	case 2:
1348c2ecf20Sopenharmony_ci		val = SPDIFOUT_CTRL0_MASK(0x3);
1358c2ecf20Sopenharmony_ci		break;
1368c2ecf20Sopenharmony_ci	default:
1378c2ecf20Sopenharmony_ci		dev_err(dai->dev, "too many channels for spdif dai: %u\n",
1388c2ecf20Sopenharmony_ci			params_channels(params));
1398c2ecf20Sopenharmony_ci		return -EINVAL;
1408c2ecf20Sopenharmony_ci	}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	regmap_update_bits(priv->map, SPDIFOUT_CTRL0,
1438c2ecf20Sopenharmony_ci			   SPDIFOUT_CTRL0_MASK_MASK, val);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	/* FIFO data are arranged in chunks of 64bits */
1468c2ecf20Sopenharmony_ci	switch (params_physical_width(params)) {
1478c2ecf20Sopenharmony_ci	case 8:
1488c2ecf20Sopenharmony_ci		/* 8 samples of 8 bits */
1498c2ecf20Sopenharmony_ci		val = SPDIFOUT_CTRL1_TYPE(0);
1508c2ecf20Sopenharmony_ci		break;
1518c2ecf20Sopenharmony_ci	case 16:
1528c2ecf20Sopenharmony_ci		/* 4 samples of 16 bits - right justified */
1538c2ecf20Sopenharmony_ci		val = SPDIFOUT_CTRL1_TYPE(2);
1548c2ecf20Sopenharmony_ci		break;
1558c2ecf20Sopenharmony_ci	case 32:
1568c2ecf20Sopenharmony_ci		/* 2 samples of 32 bits - right justified */
1578c2ecf20Sopenharmony_ci		val = SPDIFOUT_CTRL1_TYPE(4);
1588c2ecf20Sopenharmony_ci		break;
1598c2ecf20Sopenharmony_ci	default:
1608c2ecf20Sopenharmony_ci		dev_err(dai->dev, "Unsupported physical width: %u\n",
1618c2ecf20Sopenharmony_ci			params_physical_width(params));
1628c2ecf20Sopenharmony_ci		return -EINVAL;
1638c2ecf20Sopenharmony_ci	}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	/* Position of the MSB in FIFO samples */
1668c2ecf20Sopenharmony_ci	val |= SPDIFOUT_CTRL1_MSB_POS(params_width(params) - 1);
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	regmap_update_bits(priv->map, SPDIFOUT_CTRL1,
1698c2ecf20Sopenharmony_ci			   SPDIFOUT_CTRL1_MSB_POS_MASK |
1708c2ecf20Sopenharmony_ci			   SPDIFOUT_CTRL1_TYPE_MASK, val);
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	regmap_update_bits(priv->map, SPDIFOUT_CTRL0,
1738c2ecf20Sopenharmony_ci			   SPDIFOUT_CTRL0_MSB_FIRST | SPDIFOUT_CTRL0_DATA_SEL,
1748c2ecf20Sopenharmony_ci			   0);
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	return 0;
1778c2ecf20Sopenharmony_ci}
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_cistatic int axg_spdifout_set_chsts(struct snd_pcm_hw_params *params,
1808c2ecf20Sopenharmony_ci				  struct snd_soc_dai *dai)
1818c2ecf20Sopenharmony_ci{
1828c2ecf20Sopenharmony_ci	struct axg_spdifout *priv = snd_soc_dai_get_drvdata(dai);
1838c2ecf20Sopenharmony_ci	unsigned int offset;
1848c2ecf20Sopenharmony_ci	int ret;
1858c2ecf20Sopenharmony_ci	u8 cs[4];
1868c2ecf20Sopenharmony_ci	u32 val;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	ret = snd_pcm_create_iec958_consumer_hw_params(params, cs, 4);
1898c2ecf20Sopenharmony_ci	if (ret < 0) {
1908c2ecf20Sopenharmony_ci		dev_err(dai->dev, "Creating IEC958 channel status failed %d\n",
1918c2ecf20Sopenharmony_ci			ret);
1928c2ecf20Sopenharmony_ci		return ret;
1938c2ecf20Sopenharmony_ci	}
1948c2ecf20Sopenharmony_ci	val = cs[0] | cs[1] << 8 | cs[2] << 16 | cs[3] << 24;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	/* Setup channel status A bits [31 - 0]*/
1978c2ecf20Sopenharmony_ci	regmap_write(priv->map, SPDIFOUT_CHSTS0, val);
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	/* Clear channel status A bits [191 - 32] */
2008c2ecf20Sopenharmony_ci	for (offset = SPDIFOUT_CHSTS1; offset <= SPDIFOUT_CHSTS5;
2018c2ecf20Sopenharmony_ci	     offset += regmap_get_reg_stride(priv->map))
2028c2ecf20Sopenharmony_ci		regmap_write(priv->map, offset, 0);
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	/* Setup channel status B bits [31 - 0]*/
2058c2ecf20Sopenharmony_ci	regmap_write(priv->map, SPDIFOUT_CHSTS6, val);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	/* Clear channel status B bits [191 - 32] */
2088c2ecf20Sopenharmony_ci	for (offset = SPDIFOUT_CHSTS7; offset <= SPDIFOUT_CHSTSB;
2098c2ecf20Sopenharmony_ci	     offset += regmap_get_reg_stride(priv->map))
2108c2ecf20Sopenharmony_ci		regmap_write(priv->map, offset, 0);
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	return 0;
2138c2ecf20Sopenharmony_ci}
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_cistatic int axg_spdifout_hw_params(struct snd_pcm_substream *substream,
2168c2ecf20Sopenharmony_ci				  struct snd_pcm_hw_params *params,
2178c2ecf20Sopenharmony_ci				  struct snd_soc_dai *dai)
2188c2ecf20Sopenharmony_ci{
2198c2ecf20Sopenharmony_ci	struct axg_spdifout *priv = snd_soc_dai_get_drvdata(dai);
2208c2ecf20Sopenharmony_ci	unsigned int rate = params_rate(params);
2218c2ecf20Sopenharmony_ci	int ret;
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	/* 2 * 32bits per subframe * 2 channels = 128 */
2248c2ecf20Sopenharmony_ci	ret = clk_set_rate(priv->mclk, rate * 128);
2258c2ecf20Sopenharmony_ci	if (ret) {
2268c2ecf20Sopenharmony_ci		dev_err(dai->dev, "failed to set spdif clock\n");
2278c2ecf20Sopenharmony_ci		return ret;
2288c2ecf20Sopenharmony_ci	}
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	ret = axg_spdifout_sample_fmt(params, dai);
2318c2ecf20Sopenharmony_ci	if (ret) {
2328c2ecf20Sopenharmony_ci		dev_err(dai->dev, "failed to setup sample format\n");
2338c2ecf20Sopenharmony_ci		return ret;
2348c2ecf20Sopenharmony_ci	}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	ret = axg_spdifout_set_chsts(params, dai);
2378c2ecf20Sopenharmony_ci	if (ret) {
2388c2ecf20Sopenharmony_ci		dev_err(dai->dev, "failed to setup channel status words\n");
2398c2ecf20Sopenharmony_ci		return ret;
2408c2ecf20Sopenharmony_ci	}
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	return 0;
2438c2ecf20Sopenharmony_ci}
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_cistatic int axg_spdifout_startup(struct snd_pcm_substream *substream,
2468c2ecf20Sopenharmony_ci				struct snd_soc_dai *dai)
2478c2ecf20Sopenharmony_ci{
2488c2ecf20Sopenharmony_ci	struct axg_spdifout *priv = snd_soc_dai_get_drvdata(dai);
2498c2ecf20Sopenharmony_ci	int ret;
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	/* Clock the spdif output block */
2528c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(priv->pclk);
2538c2ecf20Sopenharmony_ci	if (ret) {
2548c2ecf20Sopenharmony_ci		dev_err(dai->dev, "failed to enable pclk\n");
2558c2ecf20Sopenharmony_ci		return ret;
2568c2ecf20Sopenharmony_ci	}
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	/* Make sure the block is initially stopped */
2598c2ecf20Sopenharmony_ci	axg_spdifout_disable(priv->map);
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	/* Insert data from bit 27 lsb first */
2628c2ecf20Sopenharmony_ci	regmap_update_bits(priv->map, SPDIFOUT_CTRL0,
2638c2ecf20Sopenharmony_ci			   SPDIFOUT_CTRL0_MSB_FIRST | SPDIFOUT_CTRL0_DATA_SEL,
2648c2ecf20Sopenharmony_ci			   0);
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	/* Manual control of V, C and U, U = 0 */
2678c2ecf20Sopenharmony_ci	regmap_update_bits(priv->map, SPDIFOUT_CTRL0,
2688c2ecf20Sopenharmony_ci			   SPDIFOUT_CTRL0_CHSTS_SEL | SPDIFOUT_CTRL0_VSEL |
2698c2ecf20Sopenharmony_ci			   SPDIFOUT_CTRL0_USEL | SPDIFOUT_CTRL0_USET,
2708c2ecf20Sopenharmony_ci			   0);
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	/* Static SWAP configuration ATM */
2738c2ecf20Sopenharmony_ci	regmap_write(priv->map, SPDIFOUT_SWAP, 0x10);
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	return 0;
2768c2ecf20Sopenharmony_ci}
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_cistatic void axg_spdifout_shutdown(struct snd_pcm_substream *substream,
2798c2ecf20Sopenharmony_ci				  struct snd_soc_dai *dai)
2808c2ecf20Sopenharmony_ci{
2818c2ecf20Sopenharmony_ci	struct axg_spdifout *priv = snd_soc_dai_get_drvdata(dai);
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	clk_disable_unprepare(priv->pclk);
2848c2ecf20Sopenharmony_ci}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_cistatic const struct snd_soc_dai_ops axg_spdifout_ops = {
2878c2ecf20Sopenharmony_ci	.trigger	= axg_spdifout_trigger,
2888c2ecf20Sopenharmony_ci	.mute_stream	= axg_spdifout_mute,
2898c2ecf20Sopenharmony_ci	.hw_params	= axg_spdifout_hw_params,
2908c2ecf20Sopenharmony_ci	.startup	= axg_spdifout_startup,
2918c2ecf20Sopenharmony_ci	.shutdown	= axg_spdifout_shutdown,
2928c2ecf20Sopenharmony_ci	.no_capture_mute = 1,
2938c2ecf20Sopenharmony_ci};
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_cistatic struct snd_soc_dai_driver axg_spdifout_dai_drv[] = {
2968c2ecf20Sopenharmony_ci	{
2978c2ecf20Sopenharmony_ci		.name = "SPDIF Output",
2988c2ecf20Sopenharmony_ci		.playback = {
2998c2ecf20Sopenharmony_ci			.stream_name	= "Playback",
3008c2ecf20Sopenharmony_ci			.channels_min	= 1,
3018c2ecf20Sopenharmony_ci			.channels_max	= 2,
3028c2ecf20Sopenharmony_ci			.rates		= (SNDRV_PCM_RATE_32000  |
3038c2ecf20Sopenharmony_ci					   SNDRV_PCM_RATE_44100  |
3048c2ecf20Sopenharmony_ci					   SNDRV_PCM_RATE_48000  |
3058c2ecf20Sopenharmony_ci					   SNDRV_PCM_RATE_88200  |
3068c2ecf20Sopenharmony_ci					   SNDRV_PCM_RATE_96000  |
3078c2ecf20Sopenharmony_ci					   SNDRV_PCM_RATE_176400 |
3088c2ecf20Sopenharmony_ci					   SNDRV_PCM_RATE_192000),
3098c2ecf20Sopenharmony_ci			.formats	= (SNDRV_PCM_FMTBIT_S8     |
3108c2ecf20Sopenharmony_ci					   SNDRV_PCM_FMTBIT_S16_LE |
3118c2ecf20Sopenharmony_ci					   SNDRV_PCM_FMTBIT_S20_LE |
3128c2ecf20Sopenharmony_ci					   SNDRV_PCM_FMTBIT_S24_LE),
3138c2ecf20Sopenharmony_ci		},
3148c2ecf20Sopenharmony_ci		.ops = &axg_spdifout_ops,
3158c2ecf20Sopenharmony_ci	},
3168c2ecf20Sopenharmony_ci};
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_cistatic const char * const spdifout_sel_texts[] = {
3198c2ecf20Sopenharmony_ci	"IN 0", "IN 1", "IN 2",
3208c2ecf20Sopenharmony_ci};
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(axg_spdifout_sel_enum, SPDIFOUT_CTRL1, 24,
3238c2ecf20Sopenharmony_ci			    spdifout_sel_texts);
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new axg_spdifout_in_mux =
3268c2ecf20Sopenharmony_ci	SOC_DAPM_ENUM("Input Source", axg_spdifout_sel_enum);
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_cistatic const struct snd_soc_dapm_widget axg_spdifout_dapm_widgets[] = {
3298c2ecf20Sopenharmony_ci	SND_SOC_DAPM_AIF_IN("IN 0", NULL, 0, SND_SOC_NOPM, 0, 0),
3308c2ecf20Sopenharmony_ci	SND_SOC_DAPM_AIF_IN("IN 1", NULL, 0, SND_SOC_NOPM, 0, 0),
3318c2ecf20Sopenharmony_ci	SND_SOC_DAPM_AIF_IN("IN 2", NULL, 0, SND_SOC_NOPM, 0, 0),
3328c2ecf20Sopenharmony_ci	SND_SOC_DAPM_MUX("SRC SEL", SND_SOC_NOPM, 0, 0, &axg_spdifout_in_mux),
3338c2ecf20Sopenharmony_ci};
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_cistatic const struct snd_soc_dapm_route axg_spdifout_dapm_routes[] = {
3368c2ecf20Sopenharmony_ci	{ "SRC SEL", "IN 0", "IN 0" },
3378c2ecf20Sopenharmony_ci	{ "SRC SEL", "IN 1", "IN 1" },
3388c2ecf20Sopenharmony_ci	{ "SRC SEL", "IN 2", "IN 2" },
3398c2ecf20Sopenharmony_ci	{ "Playback", NULL, "SRC SEL" },
3408c2ecf20Sopenharmony_ci};
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new axg_spdifout_controls[] = {
3438c2ecf20Sopenharmony_ci	SOC_DOUBLE("Playback Volume", SPDIFOUT_GAIN0,  0,  8, 255, 0),
3448c2ecf20Sopenharmony_ci	SOC_DOUBLE("Playback Switch", SPDIFOUT_CTRL0, 22, 21, 1, 1),
3458c2ecf20Sopenharmony_ci	SOC_SINGLE("Playback Gain Enable Switch",
3468c2ecf20Sopenharmony_ci		   SPDIFOUT_CTRL1, 26, 1, 0),
3478c2ecf20Sopenharmony_ci	SOC_SINGLE("Playback Channels Mix Switch",
3488c2ecf20Sopenharmony_ci		   SPDIFOUT_CTRL0, 23, 1, 0),
3498c2ecf20Sopenharmony_ci};
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_cistatic int axg_spdifout_set_bias_level(struct snd_soc_component *component,
3528c2ecf20Sopenharmony_ci				       enum snd_soc_bias_level level)
3538c2ecf20Sopenharmony_ci{
3548c2ecf20Sopenharmony_ci	struct axg_spdifout *priv = snd_soc_component_get_drvdata(component);
3558c2ecf20Sopenharmony_ci	enum snd_soc_bias_level now =
3568c2ecf20Sopenharmony_ci		snd_soc_component_get_bias_level(component);
3578c2ecf20Sopenharmony_ci	int ret = 0;
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	switch (level) {
3608c2ecf20Sopenharmony_ci	case SND_SOC_BIAS_PREPARE:
3618c2ecf20Sopenharmony_ci		if (now == SND_SOC_BIAS_STANDBY)
3628c2ecf20Sopenharmony_ci			ret = clk_prepare_enable(priv->mclk);
3638c2ecf20Sopenharmony_ci		break;
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	case SND_SOC_BIAS_STANDBY:
3668c2ecf20Sopenharmony_ci		if (now == SND_SOC_BIAS_PREPARE)
3678c2ecf20Sopenharmony_ci			clk_disable_unprepare(priv->mclk);
3688c2ecf20Sopenharmony_ci		break;
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	case SND_SOC_BIAS_OFF:
3718c2ecf20Sopenharmony_ci	case SND_SOC_BIAS_ON:
3728c2ecf20Sopenharmony_ci		break;
3738c2ecf20Sopenharmony_ci	}
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	return ret;
3768c2ecf20Sopenharmony_ci}
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_cistatic const struct snd_soc_component_driver axg_spdifout_component_drv = {
3798c2ecf20Sopenharmony_ci	.controls		= axg_spdifout_controls,
3808c2ecf20Sopenharmony_ci	.num_controls		= ARRAY_SIZE(axg_spdifout_controls),
3818c2ecf20Sopenharmony_ci	.dapm_widgets		= axg_spdifout_dapm_widgets,
3828c2ecf20Sopenharmony_ci	.num_dapm_widgets	= ARRAY_SIZE(axg_spdifout_dapm_widgets),
3838c2ecf20Sopenharmony_ci	.dapm_routes		= axg_spdifout_dapm_routes,
3848c2ecf20Sopenharmony_ci	.num_dapm_routes	= ARRAY_SIZE(axg_spdifout_dapm_routes),
3858c2ecf20Sopenharmony_ci	.set_bias_level		= axg_spdifout_set_bias_level,
3868c2ecf20Sopenharmony_ci};
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_cistatic const struct regmap_config axg_spdifout_regmap_cfg = {
3898c2ecf20Sopenharmony_ci	.reg_bits	= 32,
3908c2ecf20Sopenharmony_ci	.val_bits	= 32,
3918c2ecf20Sopenharmony_ci	.reg_stride	= 4,
3928c2ecf20Sopenharmony_ci	.max_register	= SPDIFOUT_MUTE_VAL,
3938c2ecf20Sopenharmony_ci};
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_cistatic const struct of_device_id axg_spdifout_of_match[] = {
3968c2ecf20Sopenharmony_ci	{ .compatible = "amlogic,axg-spdifout", },
3978c2ecf20Sopenharmony_ci	{}
3988c2ecf20Sopenharmony_ci};
3998c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, axg_spdifout_of_match);
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_cistatic int axg_spdifout_probe(struct platform_device *pdev)
4028c2ecf20Sopenharmony_ci{
4038c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
4048c2ecf20Sopenharmony_ci	struct axg_spdifout *priv;
4058c2ecf20Sopenharmony_ci	void __iomem *regs;
4068c2ecf20Sopenharmony_ci	int ret;
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
4098c2ecf20Sopenharmony_ci	if (!priv)
4108c2ecf20Sopenharmony_ci		return -ENOMEM;
4118c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, priv);
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	regs = devm_platform_ioremap_resource(pdev, 0);
4148c2ecf20Sopenharmony_ci	if (IS_ERR(regs))
4158c2ecf20Sopenharmony_ci		return PTR_ERR(regs);
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	priv->map = devm_regmap_init_mmio(dev, regs, &axg_spdifout_regmap_cfg);
4188c2ecf20Sopenharmony_ci	if (IS_ERR(priv->map)) {
4198c2ecf20Sopenharmony_ci		dev_err(dev, "failed to init regmap: %ld\n",
4208c2ecf20Sopenharmony_ci			PTR_ERR(priv->map));
4218c2ecf20Sopenharmony_ci		return PTR_ERR(priv->map);
4228c2ecf20Sopenharmony_ci	}
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	priv->pclk = devm_clk_get(dev, "pclk");
4258c2ecf20Sopenharmony_ci	if (IS_ERR(priv->pclk)) {
4268c2ecf20Sopenharmony_ci		ret = PTR_ERR(priv->pclk);
4278c2ecf20Sopenharmony_ci		if (ret != -EPROBE_DEFER)
4288c2ecf20Sopenharmony_ci			dev_err(dev, "failed to get pclk: %d\n", ret);
4298c2ecf20Sopenharmony_ci		return ret;
4308c2ecf20Sopenharmony_ci	}
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci	priv->mclk = devm_clk_get(dev, "mclk");
4338c2ecf20Sopenharmony_ci	if (IS_ERR(priv->mclk)) {
4348c2ecf20Sopenharmony_ci		ret = PTR_ERR(priv->mclk);
4358c2ecf20Sopenharmony_ci		if (ret != -EPROBE_DEFER)
4368c2ecf20Sopenharmony_ci			dev_err(dev, "failed to get mclk: %d\n", ret);
4378c2ecf20Sopenharmony_ci		return ret;
4388c2ecf20Sopenharmony_ci	}
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	return devm_snd_soc_register_component(dev, &axg_spdifout_component_drv,
4418c2ecf20Sopenharmony_ci			axg_spdifout_dai_drv, ARRAY_SIZE(axg_spdifout_dai_drv));
4428c2ecf20Sopenharmony_ci}
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_cistatic struct platform_driver axg_spdifout_pdrv = {
4458c2ecf20Sopenharmony_ci	.probe = axg_spdifout_probe,
4468c2ecf20Sopenharmony_ci	.driver = {
4478c2ecf20Sopenharmony_ci		.name = "axg-spdifout",
4488c2ecf20Sopenharmony_ci		.of_match_table = axg_spdifout_of_match,
4498c2ecf20Sopenharmony_ci	},
4508c2ecf20Sopenharmony_ci};
4518c2ecf20Sopenharmony_cimodule_platform_driver(axg_spdifout_pdrv);
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Amlogic AXG SPDIF Output driver");
4548c2ecf20Sopenharmony_ciMODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
4558c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
456