Unverified Commit b7560e8a authored by Mark Brown's avatar Mark Brown
Browse files

ASoC: ak4613: add TDM256 test support

Merge series from Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>:

Renesas is the only user of ak4613 driver on upstream for now.
It has STEREO/TDM512/TDM256/TDM128 mode, but STEREO only is used,
because of Renesas board connection.

I noticed that I can test first 2ch out of TDM256 mode 8ch
Playback even in such a situation.

base-commit: 31231092
parents 97da0ed7 f28dbaa9
Loading
Loading
Loading
Loading
+300 −67
Original line number Diff line number Diff line
@@ -10,11 +10,97 @@
// Based on ak4535.c by Richard Purdie
// Based on wm8753.c by Liam Girdwood

/*
 *		+-------+
 *		|AK4613	|
 *	SDTO1 <-|	|
 *		|	|
 *	SDTI1 ->|	|
 *	SDTI2 ->|	|
 *	SDTI3 ->|	|
 *		+-------+
 *
 *	  +---+
 * clk	  |   |___________________________________________...
 *
 * [TDM512]
 * SDTO1  [L1][R1][L2][R2]
 * SDTI1  [L1][R1][L2][R2][L3][R3][L4][R4][L5][R5][L6][R6]
 *
 * [TDM256]
 * SDTO1  [L1][R1][L2][R2]
 * SDTI1  [L1][R1][L2][R2][L3][R3][L4][R4]
 * SDTI2  [L5][R5][L6][R6]
 *
 * [TDM128]
 * SDTO1  [L1][R1][L2][R2]
 * SDTI1  [L1][R1][L2][R2]
 * SDTI2  [L3][R3][L4][R4]
 * SDTI3  [L5][R5][L6][R6]
 *
 * [STEREO]
 *	Playback  2ch : SDTI1
 *	Capture   2ch : SDTO1
 *
 * [TDM512]
 *	Playback 12ch : SDTI1
 *	Capture   4ch : SDTO1
 *
 * [TDM256]
 *	Playback 12ch : SDTI1 + SDTI2
 *	Playback  8ch : SDTI1
 *	Capture   4ch : SDTO1
 *
 * [TDM128]
 *	Playback 12ch : SDTI1 + SDTI2 + SDTI3
 *	Playback  8ch : SDTI1 + SDTI2
 *	Playback  4ch : SDTI1
 *	Capture   4ch : SDTO1
 *
 *
 * !!! NOTE !!!
 *
 * Renesas is the only user of ak4613 on upstream so far,
 * but the chip connection is like below.
 * Thus, Renesas can't test all connection case.
 * Tested TDM is very limited.
 *
 * +-----+	+-----------+
 * | SoC |	|  AK4613   |
 * |     |<-----|SDTO1	 IN1|<-- Mic
 * |     |	|	 IN2|
 * |     |	|	    |
 * |     |----->|SDTI1	OUT1|--> Headphone
 * +-----+	|SDTI2	OUT2|
 *		|SDTI3	OUT3|
 *		|	OUT4|
 *		|	OUT5|
 *		|	OUT6|
 *		+-----------+
 *
 * Renesas SoC can handle [2,  6,8]    channels.
 * Ak4613      can handle [2,4,  8,12] channels.
 *
 * Because of above HW connection and available channels number,
 * Renesas could test are ...
 *
 *	[STEREO] Playback  2ch : SDTI1
 *		 Capture   2ch : SDTO1
 *	[TDM256] Playback  8ch : SDTI1 (*)
 *
 * (*) it used 8ch data between SoC <-> AK4613 on TDM256 mode,
 *     but could confirm is only first 2ch because only 1
 *     Headphone is connected.
 *
 * see
 *	AK4613_ENABLE_TDM_TEST
 */
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/i2c.h>
#include <linux/slab.h>
#include <linux/of_device.h>
#include <linux/of_graph.h>
#include <linux/module.h>
#include <linux/regmap.h>
#include <sound/soc.h>
@@ -78,29 +164,74 @@
/* OCTRL */
#define OCTRL_MASK	(0x3F)

struct ak4613_formats {
	unsigned int width;
	unsigned int fmt;
};
/*
 * configs
 *
 * 0x000000BA
 *
 * B : AK4613_CONFIG_SDTI_x
 * A : AK4613_CONFIG_MODE_x
 */
#define AK4613_CONFIG_SET(priv, x)	 priv->configs |= AK4613_CONFIG_##x
#define AK4613_CONFIG_GET(priv, x)	(priv->configs &  AK4613_CONFIG_##x##_MASK)

/*
 * AK4613_CONFIG_SDTI_x
 *
 * It indicates how many SDTIx is connected.
 */
#define AK4613_CONFIG_SDTI_MASK		(0xF << 4)
#define AK4613_CONFIG_SDTI(x)		(((x) & 0xF) << 4)
#define AK4613_CONFIG_SDTI_set(priv, x)	  AK4613_CONFIG_SET(priv, SDTI(x))
#define AK4613_CONFIG_SDTI_get(priv)	((AK4613_CONFIG_GET(priv, SDTI) >> 4) & 0xF)

/*
 * AK4613_CONFIG_MODE_x
 *
 * Same as Ctrl1 :: TDM1/TDM0
 * No shift is requested
 * see
 *	AK4613_CTRL1_TO_MODE()
 *	Table 11/12/13/14
 */
#define AK4613_CONFIG_MODE_MASK		(0xF)
#define AK4613_CONFIG_MODE_STEREO	(0x0)
#define AK4613_CONFIG_MODE_TDM512	(0x1)
#define AK4613_CONFIG_MODE_TDM256	(0x2)
#define AK4613_CONFIG_MODE_TDM128	(0x3)

/*
 * !!!! FIXME !!!!
 *
 * Because of testable HW limitation, TDM256 8ch TDM was only tested.
 * This driver uses AK4613_ENABLE_TDM_TEST instead of new DT property so far.
 * Don't hesitate to update driver, you don't need to care compatible
 * with Renesas.
 *
 * #define AK4613_ENABLE_TDM_TEST
 */

struct ak4613_interface {
	struct ak4613_formats capture;
	struct ak4613_formats playback;
	unsigned int width;
	unsigned int fmt;
	u8 dif;
};

struct ak4613_priv {
	struct mutex lock;
	const struct ak4613_interface *iface;
	struct snd_pcm_hw_constraint_list constraint;
	struct snd_pcm_hw_constraint_list constraint_rates;
	struct snd_pcm_hw_constraint_list constraint_channels;
	struct work_struct dummy_write_work;
	struct snd_soc_component *component;
	unsigned int rate;
	unsigned int sysclk;

	unsigned int fmt;
	unsigned int configs;
	int cnt;
	u8 ctrl1;
	u8 oc;
	u8 ic;
	int cnt;
};

/*
@@ -137,14 +268,24 @@ static const struct reg_default ak4613_reg[] = {
	{ 0x14, 0x00 }, { 0x15, 0x00 }, { 0x16, 0x00 },
};

#define AUDIO_IFACE_TO_VAL(fmts) ((fmts - ak4613_iface) << 3)
#define AUDIO_IFACE(b, fmt) { b, SND_SOC_DAIFMT_##fmt }
/*
 * CTRL1 register
 * see
 *	Table 11/12/13/14
 */
#define AUDIO_IFACE(_dif, _width, _fmt)		\
	{					\
		.dif	= _dif,			\
		.width	= _width,		\
		.fmt	= SND_SOC_DAIFMT_##_fmt,\
	}
static const struct ak4613_interface ak4613_iface[] = {
	/* capture */				/* playback */
	/* [0] - [2] are not supported */
	[3] = {	AUDIO_IFACE(24, LEFT_J),	AUDIO_IFACE(24, LEFT_J) },
	[4] = {	AUDIO_IFACE(24, I2S),		AUDIO_IFACE(24, I2S) },
	/* It doesn't support asymmetric format */

	AUDIO_IFACE(0x03, 24, LEFT_J),
	AUDIO_IFACE(0x04, 24, I2S),
};
#define AK4613_CTRL1_TO_MODE(priv)	((priv)->ctrl1 >> 6) /* AK4613_CONFIG_MODE_x */

static const struct regmap_config ak4613_regmap_cfg = {
	.reg_bits		= 8,
@@ -250,13 +391,14 @@ static void ak4613_dai_shutdown(struct snd_pcm_substream *substream,
		priv->cnt = 0;
	}
	if (!priv->cnt)
		priv->iface = NULL;
		priv->ctrl1 = 0;
	mutex_unlock(&priv->lock);
}

static void ak4613_hw_constraints(struct ak4613_priv *priv,
				  struct snd_pcm_runtime *runtime)
				  struct snd_pcm_substream *substream)
{
	struct snd_pcm_runtime *runtime = substream->runtime;
	static const unsigned int ak4613_rates[] = {
		 32000,
		 44100,
@@ -267,10 +409,36 @@ static void ak4613_hw_constraints(struct ak4613_priv *priv,
		176400,
		192000,
	};
	struct snd_pcm_hw_constraint_list *constraint = &priv->constraint;
#define AK4613_CHANNEL_2	 0
#define AK4613_CHANNEL_4	 1
#define AK4613_CHANNEL_8	 2
#define AK4613_CHANNEL_12	 3
#define AK4613_CHANNEL_NONE	-1
	static const unsigned int ak4613_channels[] = {
		[AK4613_CHANNEL_2]  =  2,
		[AK4613_CHANNEL_4]  =  4,
		[AK4613_CHANNEL_8]  =  8,
		[AK4613_CHANNEL_12] = 12,
	};
#define MODE_MAX 4
#define SDTx_MAX 4
#define MASK(x) (1 << AK4613_CHANNEL_##x)
	static const int mask_list[MODE_MAX][SDTx_MAX] = {
		/* 				SDTO	 SDTIx1    SDTIx2		SDTIx3 */
		[AK4613_CONFIG_MODE_STEREO] = { MASK(2), MASK(2),  MASK(2),		MASK(2)},
		[AK4613_CONFIG_MODE_TDM512] = { MASK(4), MASK(12), MASK(12),		MASK(12)},
		[AK4613_CONFIG_MODE_TDM256] = { MASK(4), MASK(8),  MASK(8)|MASK(12),	MASK(8)|MASK(12)},
		[AK4613_CONFIG_MODE_TDM128] = { MASK(4), MASK(4),  MASK(4)|MASK(8),	MASK(4)|MASK(8)|MASK(12)},
	};
	struct snd_pcm_hw_constraint_list *constraint;
	unsigned int mask;
	unsigned int mode;
	unsigned int fs;
	int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
	int sdti_num;
	int i;

	constraint		= &priv->constraint_rates;
	constraint->list	= ak4613_rates;
	constraint->mask	= 0;
	constraint->count	= 0;
@@ -296,6 +464,41 @@ static void ak4613_hw_constraints(struct ak4613_priv *priv,

	snd_pcm_hw_constraint_list(runtime, 0,
				SNDRV_PCM_HW_PARAM_RATE, constraint);


	sdti_num = AK4613_CONFIG_SDTI_get(priv);
	if (WARN_ON(sdti_num >= SDTx_MAX))
		return;

	if (priv->cnt) {
		/*
		 * If it was already working,
		 * the constraint is same as working mode.
		 */
		mode = AK4613_CTRL1_TO_MODE(priv);
		mask = 0; /* no default */
	} else {
		/*
		 * It is not yet working,
		 * the constraint is based on board configs.
		 * STEREO mask is default
		 */
		mode = AK4613_CONFIG_GET(priv, MODE);
		mask = mask_list[AK4613_CONFIG_MODE_STEREO][is_play * sdti_num];
	}

	if (WARN_ON(mode >= MODE_MAX))
		return;

	/* add each mode mask */
	mask |= mask_list[mode][is_play * sdti_num];

	constraint		= &priv->constraint_channels;
	constraint->list	= ak4613_channels;
	constraint->mask	= mask;
	constraint->count	= sizeof(ak4613_channels);
	snd_pcm_hw_constraint_list(runtime, 0,
				   SNDRV_PCM_HW_PARAM_CHANNELS, constraint);
}

static int ak4613_dai_startup(struct snd_pcm_substream *substream,
@@ -304,9 +507,10 @@ static int ak4613_dai_startup(struct snd_pcm_substream *substream,
	struct snd_soc_component *component = dai->component;
	struct ak4613_priv *priv = snd_soc_component_get_drvdata(component);

	mutex_lock(&priv->lock);
	ak4613_hw_constraints(priv, substream);
	priv->cnt++;

	ak4613_hw_constraints(priv, substream->runtime);
	mutex_unlock(&priv->lock);

	return 0;
}
@@ -322,13 +526,13 @@ static int ak4613_dai_set_sysclk(struct snd_soc_dai *codec_dai,
	return 0;
}

static int ak4613_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
static int ak4613_dai_set_fmt(struct snd_soc_dai *dai, unsigned int format)
{
	struct snd_soc_component *component = dai->component;
	struct ak4613_priv *priv = snd_soc_component_get_drvdata(component);
	unsigned int fmt;

	fmt &= SND_SOC_DAIFMT_FORMAT_MASK;

	fmt = format & SND_SOC_DAIFMT_FORMAT_MASK;
	switch (fmt) {
	case SND_SOC_DAIFMT_LEFT_J:
	case SND_SOC_DAIFMT_I2S:
@@ -338,24 +542,20 @@ static int ak4613_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
		return -EINVAL;
	}

	return 0;
	fmt = format & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK;
	switch (fmt) {
	case SND_SOC_DAIFMT_CBC_CFC:
		break;
	default:
		/*
		 * SUPPORTME
		 *
		 * "clock provider" is not yet supperted
		 */
		return -EINVAL;
	}

static bool ak4613_dai_fmt_matching(const struct ak4613_interface *iface,
				    int is_play,
				    unsigned int fmt, unsigned int width)
{
	const struct ak4613_formats *fmts;

	fmts = (is_play) ? &iface->playback : &iface->capture;

	if (fmts->fmt != fmt)
		return false;

	if (fmts->width != width)
		return false;

	return true;
	return 0;
}

static int ak4613_dai_hw_params(struct snd_pcm_substream *substream,
@@ -364,14 +564,12 @@ static int ak4613_dai_hw_params(struct snd_pcm_substream *substream,
{
	struct snd_soc_component *component = dai->component;
	struct ak4613_priv *priv = snd_soc_component_get_drvdata(component);
	const struct ak4613_interface *iface;
	struct device *dev = component->dev;
	unsigned int width = params_width(params);
	unsigned int fmt = priv->fmt;
	unsigned int rate;
	int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
	int i, ret;
	u8 fmt_ctrl, ctrl2;
	u8 ctrl2;

	rate = params_rate(params);
	switch (rate) {
@@ -397,40 +595,51 @@ static int ak4613_dai_hw_params(struct snd_pcm_substream *substream,
	/*
	 * FIXME
	 *
	 * It doesn't support TDM at this point
	 * It doesn't have full TDM suppert yet
	 */
	fmt_ctrl = NO_FMT;
	ret = -EINVAL;
	iface = NULL;

	mutex_lock(&priv->lock);
	if (priv->iface) {
		if (ak4613_dai_fmt_matching(priv->iface, is_play, fmt, width))
			iface = priv->iface;
	if (priv->cnt > 1) {
		/*
		 * If it was already working, use current priv->ctrl1
		 */
		ret = 0;
	} else {
		/*
		 * It is not yet working,
		 */
		unsigned int channel = params_channels(params);
		u8 tdm;

		/* STEREO or TDM */
		if (channel == 2)
			tdm = AK4613_CONFIG_MODE_STEREO;
		else
			tdm = AK4613_CONFIG_GET(priv, MODE);

		for (i = ARRAY_SIZE(ak4613_iface) - 1; i >= 0; i--) {
			if (!ak4613_dai_fmt_matching(ak4613_iface + i,
						     is_play,
						     fmt, width))
				continue;
			iface = ak4613_iface + i;
			const struct ak4613_interface *iface = ak4613_iface + i;

			if ((iface->fmt == fmt) && (iface->width == width)) {
				/*
				 * Ctrl1
				 * | D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0  |
				 * |TDM1|TDM0|DIF2|DIF1|DIF0|ATS1|ATS0|SMUTE|
				 *  <  tdm  > < iface->dif >
				 */
				priv->ctrl1 = (tdm << 6) | (iface->dif << 3);
				ret = 0;
				break;
			}
		}

	if ((priv->iface == NULL) ||
	    (priv->iface == iface)) {
		priv->iface = iface;
		ret = 0;
	}
	mutex_unlock(&priv->lock);

	if (ret < 0)
		goto hw_params_end;

	fmt_ctrl = AUDIO_IFACE_TO_VAL(iface);

	snd_soc_component_update_bits(component, CTRL1, FMT_MASK, fmt_ctrl);
	snd_soc_component_update_bits(component, CTRL1, FMT_MASK, priv->ctrl1);
	snd_soc_component_update_bits(component, CTRL2, DFS_MASK, ctrl2);

	snd_soc_component_update_bits(component, ICTRL, ICTRL_MASK, priv->ic);
@@ -574,14 +783,14 @@ static struct snd_soc_dai_driver ak4613_dai = {
	.playback = {
		.stream_name	= "Playback",
		.channels_min	= 2,
		.channels_max	= 2,
		.channels_max	= 12,
		.rates		= AK4613_PCM_RATE,
		.formats	= AK4613_PCM_FMTBIT,
	},
	.capture = {
		.stream_name	= "Capture",
		.channels_min	= 2,
		.channels_max	= 2,
		.channels_max	= 4,
		.rates		= AK4613_PCM_RATE,
		.formats	= AK4613_PCM_FMTBIT,
	},
@@ -626,6 +835,7 @@ static void ak4613_parse_of(struct ak4613_priv *priv,
{
	struct device_node *np = dev->of_node;
	char prop[32];
	int sdti_num;
	int i;

	/* Input 1 - 2 */
@@ -641,6 +851,29 @@ static void ak4613_parse_of(struct ak4613_priv *priv,
		if (!of_get_property(np, prop, NULL))
			priv->oc |= 1 << i;
	}

	/*
	 * enable TDM256 test
	 *
	 * !!! FIXME !!!
	 *
	 * It should be configured by DT or other way
	 * if it was full supported.
	 * But it is using ifdef style for now for test
	 * purpose.
	 */
#if defined(AK4613_ENABLE_TDM_TEST)
	AK4613_CONFIG_SET(priv, MODE_TDM256);
#endif

	/*
	 * connected STDI
	 */
	sdti_num = of_graph_get_endpoint_count(np);
	if (WARN_ON((sdti_num > 3) || (sdti_num < 1)))
		return;

	AK4613_CONFIG_SDTI_set(priv, sdti_num);
}

static int ak4613_i2c_probe(struct i2c_client *i2c,
@@ -667,7 +900,7 @@ static int ak4613_i2c_probe(struct i2c_client *i2c,

	ak4613_parse_of(priv, dev);

	priv->iface		= NULL;
	priv->ctrl1		= 0;
	priv->cnt		= 0;
	priv->sysclk		= 0;
	INIT_WORK(&priv->dummy_write_work, ak4613_dummy_write);