mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Aaron Kling via B4 Relay <devnull+webgeek1234.gmail.com@kernel.org>
To: Liam Girdwood <lgirdwood@gmail.com>,
	Mark Brown <broonie@kernel.org>,
	 Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.com>,
	 Weidong Wang <wangweidong.a@awinic.com>
Cc: linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org,
	 Val Packett <val@packett.cool>,
	Aaron Kling <webgeek1234@gmail.com>
Subject: [PATCH 2/6] ASoC: codecs: aw88166: add TDM support
Date: Fri, 25 Sep 2026 02:47:30 -0500	[thread overview]
Message-ID: <20260925-aw88166-cleanup-v1-2-11f74cb5fe28@gmail.com> (raw)
In-Reply-To: <20260925-aw88166-cleanup-v1-0-11f74cb5fe28@gmail.com>

From: Aaron Kling <webgeek1234@gmail.com>

This amp supports TDM mode, so implement the set_tdm_slot operation to
let the SoC driver configure the TDM slot number, width, and masks.

Signed-off-by: Aaron Kling <webgeek1234@gmail.com>
---
 sound/soc/codecs/aw88166.c | 123 ++++++++++++++++++++++++++++++++++++++++++++-
 sound/soc/codecs/aw88166.h |  50 ++++++++++++++++++
 2 files changed, 171 insertions(+), 2 deletions(-)

diff --git a/sound/soc/codecs/aw88166.c b/sound/soc/codecs/aw88166.c
index 89608118cac6c..cd8773e316b90 100644
--- a/sound/soc/codecs/aw88166.c
+++ b/sound/soc/codecs/aw88166.c
@@ -7,6 +7,7 @@
 // Author: Weidong Wang <wangweidong.a@awinic.com>
 //
 
+#include <linux/bitops.h>
 #include <linux/cleanup.h>
 #include <linux/crc32.h>
 #include <linux/firmware.h>
@@ -38,8 +39,14 @@ struct aw88166 {
 	unsigned int fs_value;
 	unsigned int bck_value;
 	unsigned int bck_inv_value;
+	unsigned int tdm_bck_value;
 	unsigned int md_value;
 
+	unsigned int slot_num_value;
+	unsigned int tx_slotvld_mask;
+	unsigned int rxl_slotvld_mask;
+	unsigned int rxr_slotvld_mask;
+
 	bool phase_sync;
 };
 
@@ -125,11 +132,35 @@ static int aw88166_dev_check_pll(struct aw_device *aw_dev)
 	return -EPERM;
 }
 
-static int aw88166_dev_configure_syspll(struct aw88166 *aw88261)
+static int aw88166_dev_configure_syspll(struct aw88166 *aw88166)
 {
 	struct aw_device *aw_dev = aw88166->aw_pa;
 	int ret;
 
+	/* Configure TDM slots (I2S is represented as no slots) */
+	ret = regmap_update_bits(aw_dev->regmap, AW88166_I2SCTRL2_REG,
+			~AW88166_SLOT_NUM_MASK, aw88166->slot_num_value);
+	if (ret)
+		return ret;
+
+	ret = regmap_update_bits(aw_dev->regmap, AW88166_I2SCTRL2_REG,
+			~AW88166_I2S_TX_SLOTVLD_MASK,
+			aw88166->tx_slotvld_mask);
+	if (ret)
+		return ret;
+
+	ret = regmap_update_bits(aw_dev->regmap, AW88166_I2SCTRL2_REG,
+			~AW88166_I2S_RXL_SLOTVLD_MASK,
+			aw88166->rxl_slotvld_mask);
+	if (ret)
+		return ret;
+
+	ret = regmap_update_bits(aw_dev->regmap, AW88166_I2SCTRL2_REG,
+			~AW88166_I2S_RXR_SLOTVLD_MASK,
+			aw88166->rxr_slotvld_mask);
+	if (ret)
+		return ret;
+
 	/* PLL divider must be used for 8/16/32 kHz modes */
 	ret = regmap_update_bits(aw_dev->regmap, AW88166_PLLCTRL1_REG,
 			~AW88166_CCO_MUX_MASK, aw88166->cco_mux_value);
@@ -144,7 +175,9 @@ static int aw88166_dev_configure_syspll(struct aw88166 *aw88261)
 
 	/* The bit clock (BCK) defines the length of a frame */
 	ret = regmap_update_bits(aw_dev->regmap, AW88166_I2SCTRL1_REG,
-			~AW88166_I2SBCK_MASK, aw88166->bck_value);
+			~AW88166_I2SBCK_MASK,
+			(aw88166->tdm_bck_value != AW88166_TDM_BCK_UNSET)
+			? aw88166->tdm_bck_value : aw88166->bck_value);
 	if (ret)
 		return ret;
 
@@ -1260,9 +1293,11 @@ static int aw88166_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
 
 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
 	case SND_SOC_DAIFMT_I2S:
+	case SND_SOC_DAIFMT_DSP_A:
 		aw88166->md_value = AW88166_I2SMD_PHILIPS_STANDARD_VALUE;
 		break;
 	case SND_SOC_DAIFMT_MSB:
+	case SND_SOC_DAIFMT_DSP_B:
 		aw88166->md_value = AW88166_I2SMD_MSB_JUSTIFIED_VALUE;
 		break;
 	case SND_SOC_DAIFMT_LSB:
@@ -1369,9 +1404,90 @@ static int aw88166_hw_params(struct snd_pcm_substream *substream,
 	return 0;
 }
 
+static int aw88166_set_tdm_slot(struct snd_soc_dai *dai,
+	unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
+{
+	struct snd_soc_component *component = dai->component;
+	struct aw88166 *aw88166 = snd_soc_component_get_drvdata(component);
+	int chan;
+
+	switch (slots) {
+	case 0:
+		/* Just reset everything TDM related to I2S values */
+		aw88166->slot_num_value = AW88166_SLOT_NUM_I2S_MODE_VALUE;
+		aw88166->tdm_bck_value = AW88166_TDM_BCK_UNSET;
+		aw88166->tx_slotvld_mask = 0 << AW88166_I2S_TX_SLOTVLD_START_BIT;
+		aw88166->rxl_slotvld_mask = 0 << AW88166_I2S_RXL_SLOTVLD_START_BIT;
+		aw88166->rxr_slotvld_mask = 1 << AW88166_I2S_RXR_SLOTVLD_START_BIT;
+		return 0;
+	case 1:
+		aw88166->slot_num_value = AW88166_SLOT_NUM_TDM1S_VALUE;
+		break;
+	case 2:
+		aw88166->slot_num_value = AW88166_SLOT_NUM_TDM2S_VALUE;
+		break;
+	case 4:
+		aw88166->slot_num_value = AW88166_SLOT_NUM_TDM4S_VALUE;
+		break;
+	case 6:
+		aw88166->slot_num_value = AW88166_SLOT_NUM_TDM6S_VALUE;
+		break;
+	case 8:
+		aw88166->slot_num_value = AW88166_SLOT_NUM_TDM8S_VALUE;
+		break;
+	case 16:
+		aw88166->slot_num_value = AW88166_SLOT_NUM_TDM16S_VALUE;
+		break;
+	default:
+		dev_err(aw88166->aw_pa->dev, "unsupported slot count %d\n", slots);
+		return -EINVAL;
+	}
+
+	switch (slot_width) {
+	case 16:
+		aw88166->tdm_bck_value = AW88166_I2SBCK_32FS_VALUE;
+		break;
+	case 20:
+	case 24:
+		aw88166->tdm_bck_value = AW88166_I2SBCK_48FS_VALUE;
+		break;
+	case 32:
+		aw88166->tdm_bck_value = AW88166_I2SBCK_64FS_VALUE;
+		break;
+	default:
+		dev_err(aw88166->aw_pa->dev, "unsupported slot width %d\n",
+			slot_width);
+		return -EINVAL;
+	}
+
+	if (tx_mask != 0) {
+		if ((chan = __ffs(tx_mask)) > 16)
+			return -EINVAL;
+
+		aw88166->tx_slotvld_mask = chan << AW88166_I2S_TX_SLOTVLD_START_BIT;
+	}
+
+	if (rx_mask != 0) {
+		if ((chan = __ffs(rx_mask)) > 16)
+			return -EINVAL;
+
+		aw88166->rxl_slotvld_mask = chan << AW88166_I2S_RXL_SLOTVLD_START_BIT;
+	}
+
+	if ((rx_mask & ~BIT(chan)) != 0) {
+		if ((chan = __ffs(rx_mask & ~BIT(chan))) > 16)
+			return -EINVAL;
+
+		aw88166->rxr_slotvld_mask = chan << AW88166_I2S_RXR_SLOTVLD_START_BIT;
+	}
+
+	return 0;
+}
+
 static const struct snd_soc_dai_ops aw88166_dai_ops = {
 	.set_fmt = aw88166_set_fmt,
 	.hw_params = aw88166_hw_params,
+	.set_tdm_slot = aw88166_set_tdm_slot,
 };
 
 static struct snd_soc_dai_driver aw88166_dai[] = {
@@ -1891,12 +2007,15 @@ static int aw88166_i2c_probe(struct i2c_client *i2c)
 		return -ENOMEM;
 
 	/* set defaults */
+	aw88166->slot_num_value = AW88166_SLOT_NUM_I2S_MODE_VALUE;
 	aw88166->sr_value = AW88166_I2SSR_48KHZ_VALUE;
 	aw88166->cco_mux_value = AW88166_CCO_MUX_BYPASS_VALUE;
 	aw88166->fs_value = AW88166_I2SFS_24_BITS_VALUE;
 	aw88166->bck_value = AW88166_I2SBCK_64FS_VALUE;
 	aw88166->bck_inv_value = AW88166_BCKINV_NOT_INVERT_VALUE;
+	aw88166->tdm_bck_value = AW88166_TDM_BCK_UNSET;
 	aw88166->md_value = AW88166_I2SMD_PHILIPS_STANDARD_VALUE;
+	aw88166->rxr_slotvld_mask = 1 << AW88166_I2S_RXR_SLOTVLD_START_BIT;
 
 	mutex_init(&aw88166->lock);
 
diff --git a/sound/soc/codecs/aw88166.h b/sound/soc/codecs/aw88166.h
index 0aa92b3b6c3b8..377a733447e70 100644
--- a/sound/soc/codecs/aw88166.h
+++ b/sound/soc/codecs/aw88166.h
@@ -437,6 +437,8 @@
 #define AW88166_I2SBCK_64FS_VALUE	\
 	(AW88166_I2SBCK_64FS << AW88166_I2SBCK_START_BIT)
 
+#define AW88166_TDM_BCK_UNSET			UINT_MAX
+
 #define AW88166_I2SSR_START_BIT			(0)
 #define AW88166_I2SSR_BITS_LEN			(4)
 #define AW88166_I2SSR_MASK				\
@@ -476,6 +478,54 @@
 #define AW88166_I2SSR_192KHZ_VALUE		\
 	(AW88166_I2SSR_192KHZ << AW88166_I2SSR_START_BIT)
 
+#define AW88166_SLOT_NUM_START_BIT	(12)
+#define AW88166_SLOT_NUM_BITS_LEN	(3)
+#define AW88166_SLOT_NUM_MASK	\
+	(~(((1<<AW88166_SLOT_NUM_BITS_LEN)-1) << AW88166_SLOT_NUM_START_BIT))
+
+#define AW88166_SLOT_NUM_I2S_MODE	(0)
+#define AW88166_SLOT_NUM_I2S_MODE_VALUE	\
+	(AW88166_SLOT_NUM_I2S_MODE << AW88166_SLOT_NUM_START_BIT)
+
+#define AW88166_SLOT_NUM_TDM1S	(1)
+#define AW88166_SLOT_NUM_TDM1S_VALUE	\
+	(AW88166_SLOT_NUM_TDM1S << AW88166_SLOT_NUM_START_BIT)
+
+#define AW88166_SLOT_NUM_TDM2S	(2)
+#define AW88166_SLOT_NUM_TDM2S_VALUE	\
+	(AW88166_SLOT_NUM_TDM2S << AW88166_SLOT_NUM_START_BIT)
+
+#define AW88166_SLOT_NUM_TDM4S	(3)
+#define AW88166_SLOT_NUM_TDM4S_VALUE	\
+	(AW88166_SLOT_NUM_TDM4S << AW88166_SLOT_NUM_START_BIT)
+
+#define AW88166_SLOT_NUM_TDM6S	(4)
+#define AW88166_SLOT_NUM_TDM6S_VALUE	\
+	(AW88166_SLOT_NUM_TDM6S << AW88166_SLOT_NUM_START_BIT)
+
+#define AW88166_SLOT_NUM_TDM8S	(5)
+#define AW88166_SLOT_NUM_TDM8S_VALUE	\
+	(AW88166_SLOT_NUM_TDM8S << AW88166_SLOT_NUM_START_BIT)
+
+#define AW88166_SLOT_NUM_TDM16S	(6)
+#define AW88166_SLOT_NUM_TDM16S_VALUE	\
+	(AW88166_SLOT_NUM_TDM16S << AW88166_SLOT_NUM_START_BIT)
+
+#define AW88166_I2S_TX_SLOTVLD_START_BIT	(8)
+#define AW88166_I2S_TX_SLOTVLD_BITS_LEN	(4)
+#define AW88166_I2S_TX_SLOTVLD_MASK	\
+	(~(((1<<AW88166_I2S_TX_SLOTVLD_BITS_LEN)-1) << AW88166_I2S_TX_SLOTVLD_START_BIT))
+
+#define AW88166_I2S_RXR_SLOTVLD_START_BIT	(4)
+#define AW88166_I2S_RXR_SLOTVLD_BITS_LEN	(4)
+#define AW88166_I2S_RXR_SLOTVLD_MASK	\
+	(~(((1<<AW88166_I2S_RXR_SLOTVLD_BITS_LEN)-1) << AW88166_I2S_RXR_SLOTVLD_START_BIT))
+
+#define AW88166_I2S_RXL_SLOTVLD_START_BIT	(0)
+#define AW88166_I2S_RXL_SLOTVLD_BITS_LEN	(4)
+#define AW88166_I2S_RXL_SLOTVLD_MASK	\
+	(~(((1<<AW88166_I2S_RXL_SLOTVLD_BITS_LEN)-1) << AW88166_I2S_RXL_SLOTVLD_START_BIT))
+
 #define AW88166_CCO_MUX_START_BIT	(8)
 #define AW88166_CCO_MUX_BITS_LEN	(1)
 #define AW88166_CCO_MUX_MASK		\

-- 
2.54.0



  parent reply	other threads:[~2026-09-25  7:47 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-25  7:47 [PATCH 0/6] ASoC: codecs: aw88166: fixes and cleanup Aaron Kling via B4 Relay
2026-09-25  7:47 ` [PATCH 1/6] ASoC: codecs: aw88166: support changing sample rate and bit width Aaron Kling via B4 Relay
2026-09-25 14:00   ` Mark Brown
2026-09-25 15:56     ` Aaron Kling
2026-09-25  7:47 ` Aaron Kling via B4 Relay [this message]
2026-09-25  7:47 ` [PATCH 3/6] ASoC: codecs: aw88166: reduce log spam Aaron Kling via B4 Relay
2026-09-25  7:47 ` [PATCH 4/6] ASoC: codecs: aw88166: remove fade in/out on start/stop Aaron Kling via B4 Relay
2026-09-25 14:11   ` Mark Brown
2026-09-25 15:59     ` Aaron Kling
2026-09-25 16:27       ` Mark Brown
2026-09-25  7:47 ` [PATCH 5/6] ASoC: codecs: aw88166: remove async start Aaron Kling via B4 Relay
2026-09-25 14:12   ` Mark Brown
2026-09-25 15:44     ` Aaron Kling
2026-09-25 16:06       ` Mark Brown
2026-09-25 16:16         ` Aaron Kling
2026-09-25 16:27           ` Mark Brown
2026-09-25 14:35   ` Cezary Rojewski
2026-09-25 15:42     ` Aaron Kling
2026-09-25  7:47 ` [PATCH 6/6] ASoC: codecs: aw88166: make volume control usable Aaron Kling via B4 Relay
2026-09-25 14:18   ` Mark Brown
2026-09-25 16:05     ` Aaron Kling
2026-09-25 16:18       ` Mark Brown
2026-09-25 16:25         ` Aaron Kling
2026-09-25 16:33           ` Mark Brown

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260925-aw88166-cleanup-v1-2-11f74cb5fe28@gmail.com \
    --to=devnull+webgeek1234.gmail.com@kernel.org \
    --cc=broonie@kernel.org \
    --cc=lgirdwood@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=perex@perex.cz \
    --cc=tiwai@suse.com \
    --cc=val@packett.cool \
    --cc=wangweidong.a@awinic.com \
    --cc=webgeek1234@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®