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/bitfield.h>
78c2ecf20Sopenharmony_ci#include <linux/clk.h>
88c2ecf20Sopenharmony_ci#include <linux/module.h>
98c2ecf20Sopenharmony_ci#include <linux/of_platform.h>
108c2ecf20Sopenharmony_ci#include <linux/regmap.h>
118c2ecf20Sopenharmony_ci#include <sound/soc.h>
128c2ecf20Sopenharmony_ci#include <sound/soc-dai.h>
138c2ecf20Sopenharmony_ci#include <sound/pcm_params.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#define SPDIFIN_CTRL0			0x00
168c2ecf20Sopenharmony_ci#define  SPDIFIN_CTRL0_EN		BIT(31)
178c2ecf20Sopenharmony_ci#define  SPDIFIN_CTRL0_RST_OUT		BIT(29)
188c2ecf20Sopenharmony_ci#define  SPDIFIN_CTRL0_RST_IN		BIT(28)
198c2ecf20Sopenharmony_ci#define  SPDIFIN_CTRL0_WIDTH_SEL	BIT(24)
208c2ecf20Sopenharmony_ci#define  SPDIFIN_CTRL0_STATUS_CH_SHIFT	11
218c2ecf20Sopenharmony_ci#define  SPDIFIN_CTRL0_STATUS_SEL	GENMASK(10, 8)
228c2ecf20Sopenharmony_ci#define  SPDIFIN_CTRL0_SRC_SEL		GENMASK(5, 4)
238c2ecf20Sopenharmony_ci#define  SPDIFIN_CTRL0_CHK_VALID	BIT(3)
248c2ecf20Sopenharmony_ci#define SPDIFIN_CTRL1			0x04
258c2ecf20Sopenharmony_ci#define  SPDIFIN_CTRL1_BASE_TIMER	GENMASK(19, 0)
268c2ecf20Sopenharmony_ci#define  SPDIFIN_CTRL1_IRQ_MASK		GENMASK(27, 20)
278c2ecf20Sopenharmony_ci#define SPDIFIN_CTRL2			0x08
288c2ecf20Sopenharmony_ci#define  SPDIFIN_THRES_PER_REG		3
298c2ecf20Sopenharmony_ci#define  SPDIFIN_THRES_WIDTH		10
308c2ecf20Sopenharmony_ci#define SPDIFIN_CTRL3			0x0c
318c2ecf20Sopenharmony_ci#define SPDIFIN_CTRL4			0x10
328c2ecf20Sopenharmony_ci#define  SPDIFIN_TIMER_PER_REG		4
338c2ecf20Sopenharmony_ci#define  SPDIFIN_TIMER_WIDTH		8
348c2ecf20Sopenharmony_ci#define SPDIFIN_CTRL5			0x14
358c2ecf20Sopenharmony_ci#define SPDIFIN_CTRL6			0x18
368c2ecf20Sopenharmony_ci#define SPDIFIN_STAT0			0x1c
378c2ecf20Sopenharmony_ci#define  SPDIFIN_STAT0_MODE		GENMASK(30, 28)
388c2ecf20Sopenharmony_ci#define  SPDIFIN_STAT0_MAXW		GENMASK(17, 8)
398c2ecf20Sopenharmony_ci#define  SPDIFIN_STAT0_IRQ		GENMASK(7, 0)
408c2ecf20Sopenharmony_ci#define  SPDIFIN_IRQ_MODE_CHANGED	BIT(2)
418c2ecf20Sopenharmony_ci#define SPDIFIN_STAT1			0x20
428c2ecf20Sopenharmony_ci#define SPDIFIN_STAT2			0x24
438c2ecf20Sopenharmony_ci#define SPDIFIN_MUTE_VAL		0x28
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci#define SPDIFIN_MODE_NUM		7
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistruct axg_spdifin_cfg {
488c2ecf20Sopenharmony_ci	const unsigned int *mode_rates;
498c2ecf20Sopenharmony_ci	unsigned int ref_rate;
508c2ecf20Sopenharmony_ci};
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistruct axg_spdifin {
538c2ecf20Sopenharmony_ci	const struct axg_spdifin_cfg *conf;
548c2ecf20Sopenharmony_ci	struct regmap *map;
558c2ecf20Sopenharmony_ci	struct clk *refclk;
568c2ecf20Sopenharmony_ci	struct clk *pclk;
578c2ecf20Sopenharmony_ci};
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci/*
608c2ecf20Sopenharmony_ci * TODO:
618c2ecf20Sopenharmony_ci * It would have been nice to check the actual rate against the sample rate
628c2ecf20Sopenharmony_ci * requested in hw_params(). Unfortunately, I was not able to make the mode
638c2ecf20Sopenharmony_ci * detection and IRQ work reliably:
648c2ecf20Sopenharmony_ci *
658c2ecf20Sopenharmony_ci * 1. IRQs are generated on mode change only, so there is no notification
668c2ecf20Sopenharmony_ci *    on transition between no signal and mode 0 (32kHz).
678c2ecf20Sopenharmony_ci * 2. Mode detection very often has glitches, and may detects the
688c2ecf20Sopenharmony_ci *    lowest or the highest mode before zeroing in on the actual mode.
698c2ecf20Sopenharmony_ci *
708c2ecf20Sopenharmony_ci * This makes calling snd_pcm_stop() difficult to get right. Even notifying
718c2ecf20Sopenharmony_ci * the kcontrol would be very unreliable at this point.
728c2ecf20Sopenharmony_ci * Let's keep things simple until the magic spell that makes this work is
738c2ecf20Sopenharmony_ci * found.
748c2ecf20Sopenharmony_ci */
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistatic unsigned int axg_spdifin_get_rate(struct axg_spdifin *priv)
778c2ecf20Sopenharmony_ci{
788c2ecf20Sopenharmony_ci	unsigned int stat, mode, rate = 0;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	regmap_read(priv->map, SPDIFIN_STAT0, &stat);
818c2ecf20Sopenharmony_ci	mode = FIELD_GET(SPDIFIN_STAT0_MODE, stat);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	/*
848c2ecf20Sopenharmony_ci	 * If max width is zero, we are not capturing anything.
858c2ecf20Sopenharmony_ci	 * Also Sometimes, when the capture is on but there is no data,
868c2ecf20Sopenharmony_ci	 * mode is SPDIFIN_MODE_NUM, but not always ...
878c2ecf20Sopenharmony_ci	 */
888c2ecf20Sopenharmony_ci	if (FIELD_GET(SPDIFIN_STAT0_MAXW, stat) &&
898c2ecf20Sopenharmony_ci	    mode < SPDIFIN_MODE_NUM)
908c2ecf20Sopenharmony_ci		rate = priv->conf->mode_rates[mode];
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	return rate;
938c2ecf20Sopenharmony_ci}
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cistatic int axg_spdifin_prepare(struct snd_pcm_substream *substream,
968c2ecf20Sopenharmony_ci			       struct snd_soc_dai *dai)
978c2ecf20Sopenharmony_ci{
988c2ecf20Sopenharmony_ci	struct axg_spdifin *priv = snd_soc_dai_get_drvdata(dai);
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	/* Apply both reset */
1018c2ecf20Sopenharmony_ci	regmap_update_bits(priv->map, SPDIFIN_CTRL0,
1028c2ecf20Sopenharmony_ci			   SPDIFIN_CTRL0_RST_OUT |
1038c2ecf20Sopenharmony_ci			   SPDIFIN_CTRL0_RST_IN,
1048c2ecf20Sopenharmony_ci			   0);
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	/* Clear out reset before in reset */
1078c2ecf20Sopenharmony_ci	regmap_update_bits(priv->map, SPDIFIN_CTRL0,
1088c2ecf20Sopenharmony_ci			   SPDIFIN_CTRL0_RST_OUT, SPDIFIN_CTRL0_RST_OUT);
1098c2ecf20Sopenharmony_ci	regmap_update_bits(priv->map, SPDIFIN_CTRL0,
1108c2ecf20Sopenharmony_ci			   SPDIFIN_CTRL0_RST_IN,  SPDIFIN_CTRL0_RST_IN);
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	return 0;
1138c2ecf20Sopenharmony_ci}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_cistatic void axg_spdifin_write_mode_param(struct regmap *map, int mode,
1168c2ecf20Sopenharmony_ci					 unsigned int val,
1178c2ecf20Sopenharmony_ci					 unsigned int num_per_reg,
1188c2ecf20Sopenharmony_ci					 unsigned int base_reg,
1198c2ecf20Sopenharmony_ci					 unsigned int width)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	uint64_t offset = mode;
1228c2ecf20Sopenharmony_ci	unsigned int reg, shift, rem;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	rem = do_div(offset, num_per_reg);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	reg = offset * regmap_get_reg_stride(map) + base_reg;
1278c2ecf20Sopenharmony_ci	shift = width * (num_per_reg - 1 - rem);
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	regmap_update_bits(map, reg, GENMASK(width - 1, 0) << shift,
1308c2ecf20Sopenharmony_ci			   val << shift);
1318c2ecf20Sopenharmony_ci}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_cistatic void axg_spdifin_write_timer(struct regmap *map, int mode,
1348c2ecf20Sopenharmony_ci				    unsigned int val)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	axg_spdifin_write_mode_param(map, mode, val, SPDIFIN_TIMER_PER_REG,
1378c2ecf20Sopenharmony_ci				     SPDIFIN_CTRL4, SPDIFIN_TIMER_WIDTH);
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_cistatic void axg_spdifin_write_threshold(struct regmap *map, int mode,
1418c2ecf20Sopenharmony_ci					unsigned int val)
1428c2ecf20Sopenharmony_ci{
1438c2ecf20Sopenharmony_ci	axg_spdifin_write_mode_param(map, mode, val, SPDIFIN_THRES_PER_REG,
1448c2ecf20Sopenharmony_ci				     SPDIFIN_CTRL2, SPDIFIN_THRES_WIDTH);
1458c2ecf20Sopenharmony_ci}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_cistatic unsigned int axg_spdifin_mode_timer(struct axg_spdifin *priv,
1488c2ecf20Sopenharmony_ci					   int mode,
1498c2ecf20Sopenharmony_ci					   unsigned int rate)
1508c2ecf20Sopenharmony_ci{
1518c2ecf20Sopenharmony_ci	/*
1528c2ecf20Sopenharmony_ci	 * Number of period of the reference clock during a period of the
1538c2ecf20Sopenharmony_ci	 * input signal reference clock
1548c2ecf20Sopenharmony_ci	 */
1558c2ecf20Sopenharmony_ci	return rate / (128 * priv->conf->mode_rates[mode]);
1568c2ecf20Sopenharmony_ci}
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_cistatic int axg_spdifin_sample_mode_config(struct snd_soc_dai *dai,
1598c2ecf20Sopenharmony_ci					  struct axg_spdifin *priv)
1608c2ecf20Sopenharmony_ci{
1618c2ecf20Sopenharmony_ci	unsigned int rate, t_next;
1628c2ecf20Sopenharmony_ci	int ret, i = SPDIFIN_MODE_NUM - 1;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	/* Set spdif input reference clock */
1658c2ecf20Sopenharmony_ci	ret = clk_set_rate(priv->refclk, priv->conf->ref_rate);
1668c2ecf20Sopenharmony_ci	if (ret) {
1678c2ecf20Sopenharmony_ci		dev_err(dai->dev, "reference clock rate set failed\n");
1688c2ecf20Sopenharmony_ci		return ret;
1698c2ecf20Sopenharmony_ci	}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	/*
1728c2ecf20Sopenharmony_ci	 * The rate actually set might be slightly different, get
1738c2ecf20Sopenharmony_ci	 * the actual rate for the following mode calculation
1748c2ecf20Sopenharmony_ci	 */
1758c2ecf20Sopenharmony_ci	rate = clk_get_rate(priv->refclk);
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	/* HW will update mode every 1ms */
1788c2ecf20Sopenharmony_ci	regmap_update_bits(priv->map, SPDIFIN_CTRL1,
1798c2ecf20Sopenharmony_ci			   SPDIFIN_CTRL1_BASE_TIMER,
1808c2ecf20Sopenharmony_ci			   FIELD_PREP(SPDIFIN_CTRL1_BASE_TIMER, rate / 1000));
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	/* Threshold based on the minimum width between two edges */
1838c2ecf20Sopenharmony_ci	regmap_update_bits(priv->map, SPDIFIN_CTRL0,
1848c2ecf20Sopenharmony_ci			   SPDIFIN_CTRL0_WIDTH_SEL, SPDIFIN_CTRL0_WIDTH_SEL);
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	/* Calculate the last timer which has no threshold */
1878c2ecf20Sopenharmony_ci	t_next = axg_spdifin_mode_timer(priv, i, rate);
1888c2ecf20Sopenharmony_ci	axg_spdifin_write_timer(priv->map, i, t_next);
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	do {
1918c2ecf20Sopenharmony_ci		unsigned int t;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci		i -= 1;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci		/* Calculate the timer */
1968c2ecf20Sopenharmony_ci		t = axg_spdifin_mode_timer(priv, i, rate);
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci		/* Set the timer value */
1998c2ecf20Sopenharmony_ci		axg_spdifin_write_timer(priv->map, i, t);
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci		/* Set the threshold value */
2028c2ecf20Sopenharmony_ci		axg_spdifin_write_threshold(priv->map, i, t + t_next);
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci		/* Save the current timer for the next threshold calculation */
2058c2ecf20Sopenharmony_ci		t_next = t;
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	} while (i > 0);
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	return 0;
2108c2ecf20Sopenharmony_ci}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_cistatic int axg_spdifin_dai_probe(struct snd_soc_dai *dai)
2138c2ecf20Sopenharmony_ci{
2148c2ecf20Sopenharmony_ci	struct axg_spdifin *priv = snd_soc_dai_get_drvdata(dai);
2158c2ecf20Sopenharmony_ci	int ret;
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(priv->pclk);
2188c2ecf20Sopenharmony_ci	if (ret) {
2198c2ecf20Sopenharmony_ci		dev_err(dai->dev, "failed to enable pclk\n");
2208c2ecf20Sopenharmony_ci		return ret;
2218c2ecf20Sopenharmony_ci	}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	ret = axg_spdifin_sample_mode_config(dai, priv);
2248c2ecf20Sopenharmony_ci	if (ret) {
2258c2ecf20Sopenharmony_ci		dev_err(dai->dev, "mode configuration failed\n");
2268c2ecf20Sopenharmony_ci		goto pclk_err;
2278c2ecf20Sopenharmony_ci	}
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(priv->refclk);
2308c2ecf20Sopenharmony_ci	if (ret) {
2318c2ecf20Sopenharmony_ci		dev_err(dai->dev,
2328c2ecf20Sopenharmony_ci			"failed to enable spdifin reference clock\n");
2338c2ecf20Sopenharmony_ci		goto pclk_err;
2348c2ecf20Sopenharmony_ci	}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	regmap_update_bits(priv->map, SPDIFIN_CTRL0, SPDIFIN_CTRL0_EN,
2378c2ecf20Sopenharmony_ci			   SPDIFIN_CTRL0_EN);
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	return 0;
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_cipclk_err:
2428c2ecf20Sopenharmony_ci	clk_disable_unprepare(priv->pclk);
2438c2ecf20Sopenharmony_ci	return ret;
2448c2ecf20Sopenharmony_ci}
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_cistatic int axg_spdifin_dai_remove(struct snd_soc_dai *dai)
2478c2ecf20Sopenharmony_ci{
2488c2ecf20Sopenharmony_ci	struct axg_spdifin *priv = snd_soc_dai_get_drvdata(dai);
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	regmap_update_bits(priv->map, SPDIFIN_CTRL0, SPDIFIN_CTRL0_EN, 0);
2518c2ecf20Sopenharmony_ci	clk_disable_unprepare(priv->refclk);
2528c2ecf20Sopenharmony_ci	clk_disable_unprepare(priv->pclk);
2538c2ecf20Sopenharmony_ci	return 0;
2548c2ecf20Sopenharmony_ci}
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_cistatic const struct snd_soc_dai_ops axg_spdifin_ops = {
2578c2ecf20Sopenharmony_ci	.prepare	= axg_spdifin_prepare,
2588c2ecf20Sopenharmony_ci};
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_cistatic int axg_spdifin_iec958_info(struct snd_kcontrol *kcontrol,
2618c2ecf20Sopenharmony_ci				   struct snd_ctl_elem_info *uinfo)
2628c2ecf20Sopenharmony_ci{
2638c2ecf20Sopenharmony_ci	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2648c2ecf20Sopenharmony_ci	uinfo->count = 1;
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	return 0;
2678c2ecf20Sopenharmony_ci}
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_cistatic int axg_spdifin_get_status_mask(struct snd_kcontrol *kcontrol,
2708c2ecf20Sopenharmony_ci				       struct snd_ctl_elem_value *ucontrol)
2718c2ecf20Sopenharmony_ci{
2728c2ecf20Sopenharmony_ci	int i;
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	for (i = 0; i < 24; i++)
2758c2ecf20Sopenharmony_ci		ucontrol->value.iec958.status[i] = 0xff;
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	return 0;
2788c2ecf20Sopenharmony_ci}
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_cistatic int axg_spdifin_get_status(struct snd_kcontrol *kcontrol,
2818c2ecf20Sopenharmony_ci				  struct snd_ctl_elem_value *ucontrol)
2828c2ecf20Sopenharmony_ci{
2838c2ecf20Sopenharmony_ci	struct snd_soc_component *c = snd_kcontrol_chip(kcontrol);
2848c2ecf20Sopenharmony_ci	struct axg_spdifin *priv = snd_soc_component_get_drvdata(c);
2858c2ecf20Sopenharmony_ci	int i, j;
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	for (i = 0; i < 6; i++) {
2888c2ecf20Sopenharmony_ci		unsigned int val;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci		regmap_update_bits(priv->map, SPDIFIN_CTRL0,
2918c2ecf20Sopenharmony_ci				   SPDIFIN_CTRL0_STATUS_SEL,
2928c2ecf20Sopenharmony_ci				   FIELD_PREP(SPDIFIN_CTRL0_STATUS_SEL, i));
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci		regmap_read(priv->map, SPDIFIN_STAT1, &val);
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci		for (j = 0; j < 4; j++) {
2978c2ecf20Sopenharmony_ci			unsigned int offset = i * 4 + j;
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci			ucontrol->value.iec958.status[offset] =
3008c2ecf20Sopenharmony_ci				(val >> (j * 8)) & 0xff;
3018c2ecf20Sopenharmony_ci		}
3028c2ecf20Sopenharmony_ci	}
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	return 0;
3058c2ecf20Sopenharmony_ci}
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci#define AXG_SPDIFIN_IEC958_MASK						\
3088c2ecf20Sopenharmony_ci	{								\
3098c2ecf20Sopenharmony_ci		.access = SNDRV_CTL_ELEM_ACCESS_READ,			\
3108c2ecf20Sopenharmony_ci		.iface = SNDRV_CTL_ELEM_IFACE_PCM,			\
3118c2ecf20Sopenharmony_ci		.name = SNDRV_CTL_NAME_IEC958("", CAPTURE, MASK),	\
3128c2ecf20Sopenharmony_ci		.info = axg_spdifin_iec958_info,			\
3138c2ecf20Sopenharmony_ci		.get = axg_spdifin_get_status_mask,			\
3148c2ecf20Sopenharmony_ci	}
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci#define AXG_SPDIFIN_IEC958_STATUS					\
3178c2ecf20Sopenharmony_ci	{								\
3188c2ecf20Sopenharmony_ci		.access = (SNDRV_CTL_ELEM_ACCESS_READ |			\
3198c2ecf20Sopenharmony_ci			   SNDRV_CTL_ELEM_ACCESS_VOLATILE),		\
3208c2ecf20Sopenharmony_ci		.iface = SNDRV_CTL_ELEM_IFACE_PCM,			\
3218c2ecf20Sopenharmony_ci		.name =	SNDRV_CTL_NAME_IEC958("", CAPTURE, NONE),	\
3228c2ecf20Sopenharmony_ci		.info = axg_spdifin_iec958_info,			\
3238c2ecf20Sopenharmony_ci		.get = axg_spdifin_get_status,				\
3248c2ecf20Sopenharmony_ci	}
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_cistatic const char * const spdifin_chsts_src_texts[] = {
3278c2ecf20Sopenharmony_ci	"A", "B",
3288c2ecf20Sopenharmony_ci};
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_cistatic SOC_ENUM_SINGLE_DECL(axg_spdifin_chsts_src_enum, SPDIFIN_CTRL0,
3318c2ecf20Sopenharmony_ci			    SPDIFIN_CTRL0_STATUS_CH_SHIFT,
3328c2ecf20Sopenharmony_ci			    spdifin_chsts_src_texts);
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_cistatic int axg_spdifin_rate_lock_info(struct snd_kcontrol *kcontrol,
3358c2ecf20Sopenharmony_ci				      struct snd_ctl_elem_info *uinfo)
3368c2ecf20Sopenharmony_ci{
3378c2ecf20Sopenharmony_ci	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3388c2ecf20Sopenharmony_ci	uinfo->count = 1;
3398c2ecf20Sopenharmony_ci	uinfo->value.integer.min = 0;
3408c2ecf20Sopenharmony_ci	uinfo->value.integer.max = 192000;
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci	return 0;
3438c2ecf20Sopenharmony_ci}
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_cistatic int axg_spdifin_rate_lock_get(struct snd_kcontrol *kcontrol,
3468c2ecf20Sopenharmony_ci				     struct snd_ctl_elem_value *ucontrol)
3478c2ecf20Sopenharmony_ci{
3488c2ecf20Sopenharmony_ci	struct snd_soc_component *c = snd_kcontrol_chip(kcontrol);
3498c2ecf20Sopenharmony_ci	struct axg_spdifin *priv = snd_soc_component_get_drvdata(c);
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	ucontrol->value.integer.value[0] = axg_spdifin_get_rate(priv);
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	return 0;
3548c2ecf20Sopenharmony_ci}
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci#define AXG_SPDIFIN_LOCK_RATE(xname)				\
3578c2ecf20Sopenharmony_ci	{							\
3588c2ecf20Sopenharmony_ci		.iface = SNDRV_CTL_ELEM_IFACE_PCM,		\
3598c2ecf20Sopenharmony_ci		.access = (SNDRV_CTL_ELEM_ACCESS_READ |		\
3608c2ecf20Sopenharmony_ci			   SNDRV_CTL_ELEM_ACCESS_VOLATILE),	\
3618c2ecf20Sopenharmony_ci		.get = axg_spdifin_rate_lock_get,		\
3628c2ecf20Sopenharmony_ci		.info = axg_spdifin_rate_lock_info,		\
3638c2ecf20Sopenharmony_ci		.name = xname,					\
3648c2ecf20Sopenharmony_ci	}
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new axg_spdifin_controls[] = {
3678c2ecf20Sopenharmony_ci	AXG_SPDIFIN_LOCK_RATE("Capture Rate Lock"),
3688c2ecf20Sopenharmony_ci	SOC_DOUBLE("Capture Switch", SPDIFIN_CTRL0, 7, 6, 1, 1),
3698c2ecf20Sopenharmony_ci	SOC_ENUM(SNDRV_CTL_NAME_IEC958("", CAPTURE, NONE) "Src",
3708c2ecf20Sopenharmony_ci		 axg_spdifin_chsts_src_enum),
3718c2ecf20Sopenharmony_ci	AXG_SPDIFIN_IEC958_MASK,
3728c2ecf20Sopenharmony_ci	AXG_SPDIFIN_IEC958_STATUS,
3738c2ecf20Sopenharmony_ci};
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_cistatic const struct snd_soc_component_driver axg_spdifin_component_drv = {
3768c2ecf20Sopenharmony_ci	.controls		= axg_spdifin_controls,
3778c2ecf20Sopenharmony_ci	.num_controls		= ARRAY_SIZE(axg_spdifin_controls),
3788c2ecf20Sopenharmony_ci};
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_cistatic const struct regmap_config axg_spdifin_regmap_cfg = {
3818c2ecf20Sopenharmony_ci	.reg_bits	= 32,
3828c2ecf20Sopenharmony_ci	.val_bits	= 32,
3838c2ecf20Sopenharmony_ci	.reg_stride	= 4,
3848c2ecf20Sopenharmony_ci	.max_register	= SPDIFIN_MUTE_VAL,
3858c2ecf20Sopenharmony_ci};
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_cistatic const unsigned int axg_spdifin_mode_rates[SPDIFIN_MODE_NUM] = {
3888c2ecf20Sopenharmony_ci	32000, 44100, 48000, 88200, 96000, 176400, 192000,
3898c2ecf20Sopenharmony_ci};
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_cistatic const struct axg_spdifin_cfg axg_cfg = {
3928c2ecf20Sopenharmony_ci	.mode_rates = axg_spdifin_mode_rates,
3938c2ecf20Sopenharmony_ci	.ref_rate = 333333333,
3948c2ecf20Sopenharmony_ci};
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_cistatic const struct of_device_id axg_spdifin_of_match[] = {
3978c2ecf20Sopenharmony_ci	{
3988c2ecf20Sopenharmony_ci		.compatible = "amlogic,axg-spdifin",
3998c2ecf20Sopenharmony_ci		.data = &axg_cfg,
4008c2ecf20Sopenharmony_ci	}, {}
4018c2ecf20Sopenharmony_ci};
4028c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, axg_spdifin_of_match);
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_cistatic struct snd_soc_dai_driver *
4058c2ecf20Sopenharmony_ciaxg_spdifin_get_dai_drv(struct device *dev, struct axg_spdifin *priv)
4068c2ecf20Sopenharmony_ci{
4078c2ecf20Sopenharmony_ci	struct snd_soc_dai_driver *drv;
4088c2ecf20Sopenharmony_ci	int i;
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
4118c2ecf20Sopenharmony_ci	if (!drv)
4128c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	drv->name = "SPDIF Input";
4158c2ecf20Sopenharmony_ci	drv->ops = &axg_spdifin_ops;
4168c2ecf20Sopenharmony_ci	drv->probe = axg_spdifin_dai_probe;
4178c2ecf20Sopenharmony_ci	drv->remove = axg_spdifin_dai_remove;
4188c2ecf20Sopenharmony_ci	drv->capture.stream_name = "Capture";
4198c2ecf20Sopenharmony_ci	drv->capture.channels_min = 1;
4208c2ecf20Sopenharmony_ci	drv->capture.channels_max = 2;
4218c2ecf20Sopenharmony_ci	drv->capture.formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	for (i = 0; i < SPDIFIN_MODE_NUM; i++) {
4248c2ecf20Sopenharmony_ci		unsigned int rb =
4258c2ecf20Sopenharmony_ci			snd_pcm_rate_to_rate_bit(priv->conf->mode_rates[i]);
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci		if (rb == SNDRV_PCM_RATE_KNOT)
4288c2ecf20Sopenharmony_ci			return ERR_PTR(-EINVAL);
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci		drv->capture.rates |= rb;
4318c2ecf20Sopenharmony_ci	}
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	return drv;
4348c2ecf20Sopenharmony_ci}
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_cistatic int axg_spdifin_probe(struct platform_device *pdev)
4378c2ecf20Sopenharmony_ci{
4388c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
4398c2ecf20Sopenharmony_ci	struct axg_spdifin *priv;
4408c2ecf20Sopenharmony_ci	struct snd_soc_dai_driver *dai_drv;
4418c2ecf20Sopenharmony_ci	void __iomem *regs;
4428c2ecf20Sopenharmony_ci	int ret;
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
4458c2ecf20Sopenharmony_ci	if (!priv)
4468c2ecf20Sopenharmony_ci		return -ENOMEM;
4478c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, priv);
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci	priv->conf = of_device_get_match_data(dev);
4508c2ecf20Sopenharmony_ci	if (!priv->conf) {
4518c2ecf20Sopenharmony_ci		dev_err(dev, "failed to match device\n");
4528c2ecf20Sopenharmony_ci		return -ENODEV;
4538c2ecf20Sopenharmony_ci	}
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	regs = devm_platform_ioremap_resource(pdev, 0);
4568c2ecf20Sopenharmony_ci	if (IS_ERR(regs))
4578c2ecf20Sopenharmony_ci		return PTR_ERR(regs);
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	priv->map = devm_regmap_init_mmio(dev, regs, &axg_spdifin_regmap_cfg);
4608c2ecf20Sopenharmony_ci	if (IS_ERR(priv->map)) {
4618c2ecf20Sopenharmony_ci		dev_err(dev, "failed to init regmap: %ld\n",
4628c2ecf20Sopenharmony_ci			PTR_ERR(priv->map));
4638c2ecf20Sopenharmony_ci		return PTR_ERR(priv->map);
4648c2ecf20Sopenharmony_ci	}
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci	priv->pclk = devm_clk_get(dev, "pclk");
4678c2ecf20Sopenharmony_ci	if (IS_ERR(priv->pclk)) {
4688c2ecf20Sopenharmony_ci		ret = PTR_ERR(priv->pclk);
4698c2ecf20Sopenharmony_ci		if (ret != -EPROBE_DEFER)
4708c2ecf20Sopenharmony_ci			dev_err(dev, "failed to get pclk: %d\n", ret);
4718c2ecf20Sopenharmony_ci		return ret;
4728c2ecf20Sopenharmony_ci	}
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	priv->refclk = devm_clk_get(dev, "refclk");
4758c2ecf20Sopenharmony_ci	if (IS_ERR(priv->refclk)) {
4768c2ecf20Sopenharmony_ci		ret = PTR_ERR(priv->refclk);
4778c2ecf20Sopenharmony_ci		if (ret != -EPROBE_DEFER)
4788c2ecf20Sopenharmony_ci			dev_err(dev, "failed to get mclk: %d\n", ret);
4798c2ecf20Sopenharmony_ci		return ret;
4808c2ecf20Sopenharmony_ci	}
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_ci	dai_drv = axg_spdifin_get_dai_drv(dev, priv);
4838c2ecf20Sopenharmony_ci	if (IS_ERR(dai_drv)) {
4848c2ecf20Sopenharmony_ci		dev_err(dev, "failed to get dai driver: %ld\n",
4858c2ecf20Sopenharmony_ci			PTR_ERR(dai_drv));
4868c2ecf20Sopenharmony_ci		return PTR_ERR(dai_drv);
4878c2ecf20Sopenharmony_ci	}
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci	return devm_snd_soc_register_component(dev, &axg_spdifin_component_drv,
4908c2ecf20Sopenharmony_ci					       dai_drv, 1);
4918c2ecf20Sopenharmony_ci}
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_cistatic struct platform_driver axg_spdifin_pdrv = {
4948c2ecf20Sopenharmony_ci	.probe = axg_spdifin_probe,
4958c2ecf20Sopenharmony_ci	.driver = {
4968c2ecf20Sopenharmony_ci		.name = "axg-spdifin",
4978c2ecf20Sopenharmony_ci		.of_match_table = axg_spdifin_of_match,
4988c2ecf20Sopenharmony_ci	},
4998c2ecf20Sopenharmony_ci};
5008c2ecf20Sopenharmony_cimodule_platform_driver(axg_spdifin_pdrv);
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Amlogic AXG SPDIF Input driver");
5038c2ecf20Sopenharmony_ciMODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
5048c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
505