Skip to contentSkip to conversation

ASoC: rt1015: support TDM slot configuration

PATCH Superseded [email protected] 2 participants 3 messages Nov 2, 2020 – Nov 3, 2020

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ASoC: rt1015: support TDM slot configuration

From: Shuming Fan <[email protected]>

Add TDM slot callback function to support TDM configuration

---
 sound/soc/codecs/rt1015.c | 101 ++++++++++++++++++++++++++++++++++++++
 sound/soc/codecs/rt1015.h |  42 ++++++++++++++++
 2 files changed, 143 insertions(+)
sound/soc/codecs/rt1015.c +101 -0
index 25fe2ddedd54..5d3e269d17f5 100644
@@ -944,6 +944,106 @@ static int rt1015_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
 	return 0;
 }
 
+static int rt1015_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;
+	unsigned int val = 0, rx_slotnum, tx_slotnum;
+	int ret = 0, first_bit;
+
+	switch (slots) {
+	case 4:
+		val |= RT1015_I2S_TX_4CH;
+		break;
+	case 6:
+		val |= RT1015_I2S_TX_6CH;
+		break;
+	case 8:
+		val |= RT1015_I2S_TX_8CH;
+		break;
+	case 2:
+		break;
+	default:
+		ret = -EINVAL;
+		goto _set_tdm_err_;
+	}
+
+	switch (slot_width) {
+	case 20:
+		val |= RT1015_I2S_CH_TX_LEN_20B;
+		break;
+	case 24:
+		val |= RT1015_I2S_CH_TX_LEN_24B;
+		break;
+	case 32:
+		val |= RT1015_I2S_CH_TX_LEN_32B;
+		break;
+	case 16:
+		break;
+	default:
+		ret = -EINVAL;
+		goto _set_tdm_err_;
+	}
+
+	/* Rx slot configuration */
+	rx_slotnum = hweight_long(rx_mask);
+	if (rx_slotnum > 1 || !rx_slotnum) {
+		ret = -EINVAL;
+		dev_err(component->dev, "too many rx slots or zero slot\n");
+		goto _set_tdm_err_;
+	}
+
+	first_bit = __ffs(rx_mask);
+	switch (first_bit) {
+	case 0:
+	case 2:
+	case 4:
+	case 6:
+		snd_soc_component_update_bits(component,
+			RT1015_PAD_DRV2, RT1015_MONO_LR_SEL_MASK,
+			RT1015_MONO_L_CHANNEL);
+		snd_soc_component_update_bits(component,
+			RT1015_TDM1_4,
+			RT1015_TDM_I2S_TX_L_DAC1_1_MASK |
+			RT1015_TDM_I2S_TX_R_DAC1_1_MASK,
+			(first_bit << RT1015_TDM_I2S_TX_L_DAC1_1_SFT) |
+			((first_bit+1) << RT1015_TDM_I2S_TX_R_DAC1_1_SFT));
+		break;
+	case 1:
+	case 3:
+	case 5:
+	case 7:
+		snd_soc_component_update_bits(component,
+			RT1015_PAD_DRV2, RT1015_MONO_LR_SEL_MASK,
+			RT1015_MONO_R_CHANNEL);
+		snd_soc_component_update_bits(component,
+			RT1015_TDM1_4,
+			RT1015_TDM_I2S_TX_L_DAC1_1_MASK |
+			RT1015_TDM_I2S_TX_R_DAC1_1_MASK,
+			((first_bit-1) << RT1015_TDM_I2S_TX_L_DAC1_1_SFT) |
+			(first_bit << RT1015_TDM_I2S_TX_R_DAC1_1_SFT));
+		break;
+	default:
+		ret = -EINVAL;
+		goto _set_tdm_err_;
+	}
+
+	/* Tx slot configuration */
+	tx_slotnum = hweight_long(tx_mask);
+	if (tx_slotnum) {
+		ret = -EINVAL;
+		dev_err(component->dev, "doesn't need to support tx slots\n");
+		goto _set_tdm_err_;
+	}
+
+	snd_soc_component_update_bits(component, RT1015_TDM1_1,
+		RT1015_I2S_CH_TX_MASK | RT1015_I2S_CH_RX_MASK |
+		RT1015_I2S_CH_TX_LEN_MASK | RT1015_I2S_CH_RX_LEN_MASK, val);
+
+_set_tdm_err_:
+	return ret;
+}
+
 static int rt1015_probe(struct snd_soc_component *component)
 {
 	struct rt1015_priv *rt1015 =
@@ -975,6 +1075,7 @@ static struct snd_soc_dai_ops rt1015_aif_dai_ops = {
 	.hw_params = rt1015_hw_params,
 	.set_fmt = rt1015_set_dai_fmt,
 	.set_bclk_ratio = rt1015_set_bclk_ratio,
+	.set_tdm_slot = rt1015_set_tdm_slot,
 };
 
 static struct snd_soc_dai_driver rt1015_dai[] = {
sound/soc/codecs/rt1015.h +42 -0

54 lines not on this page · raw message

Signature · 1 line
2.28.0
#1 lore raw
Quoted 12 lines
sound/soc/codecs/rt1015.c +9 -0
+static int rt1015_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;
+	unsigned int val = 0, rx_slotnum, tx_slotnum;
+	int ret = 0, first_bit;
+
+	switch (slots) {
+	case 4:

sound/soc/codecs/rt1015.c static int rt1015_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio) · line 965

Quoted 10 lines
sound/soc/codecs/rt1015.c +10 -0
+		val |= RT1015_I2S_TX_4CH;
+		break;
+	case 6:
+		val |= RT1015_I2S_TX_6CH;
+		break;
+	case 8:
+		val |= RT1015_I2S_TX_8CH;
+		break;
+	case 2:
+		break;

nit-pick: I would put the case 2 first to keep the order. I thought for a second this was an error case due to the discontinuity.

Quoted 7 lines
sound/soc/codecs/rt1015.c +7 -0
+	default:
+		ret = -EINVAL;
+		goto _set_tdm_err_;
+	}
+
+	switch (slot_width) {
+	case 20:

sound/soc/codecs/rt1015.c static int rt1015_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio) · line 982

Quoted 10 lines
sound/soc/codecs/rt1015.c +10 -0
+		val |= RT1015_I2S_CH_TX_LEN_20B;
+		break;
+	case 24:
+		val |= RT1015_I2S_CH_TX_LEN_24B;
+		break;
+	case 32:
+		val |= RT1015_I2S_CH_TX_LEN_32B;
+		break;
+	case 16:
+		break;

nit-pick: same here, I would put 16 first.

sound/soc/codecs/rt1015.c static int rt1015_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio) · line 990

Quoted 8 lines
sound/soc/codecs/rt1015.c +8 -0
+	default:
+		ret = -EINVAL;
+		goto _set_tdm_err_;
+	}
+
+	/* Rx slot configuration */
+	rx_slotnum = hweight_long(rx_mask);
+	if (rx_slotnum > 1 || !rx_slotnum) {

I am confused here, is this saying you can only have a single channel on RX?

If yes should this be simplified as if (rx_slotnum != 1) ?

Quoted 10 lines
sound/soc/codecs/rt1015.c +10 -0
+		ret = -EINVAL;
+		dev_err(component->dev, "too many rx slots or zero slot\n");
+		goto _set_tdm_err_;
+	}
+
+	first_bit = __ffs(rx_mask);
+	switch (first_bit) {
+	case 0:
+	case 2:
+	case 4:

sound/soc/codecs/rt1015.c static int rt1015_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio) · line 1010

Quoted 10 lines
sound/soc/codecs/rt1015.c +10 -0
+	case 6:
+		snd_soc_component_update_bits(component,
+			RT1015_PAD_DRV2, RT1015_MONO_LR_SEL_MASK,
+			RT1015_MONO_L_CHANNEL);
+		snd_soc_component_update_bits(component,
+			RT1015_TDM1_4,
+			RT1015_TDM_I2S_TX_L_DAC1_1_MASK |
+			RT1015_TDM_I2S_TX_R_DAC1_1_MASK,
+			(first_bit << RT1015_TDM_I2S_TX_L_DAC1_1_SFT) |
+			((first_bit+1) << RT1015_TDM_I2S_TX_R_DAC1_1_SFT));

looks like there's an assumption that the rx mask has contiguous bits set? Maybe add a comment to explain how the RX path works?

Quoted 36 lines
sound/soc/codecs/rt1015.c +36 -0
+		break;
+	case 1:
+	case 3:
+	case 5:
+	case 7:
+		snd_soc_component_update_bits(component,
+			RT1015_PAD_DRV2, RT1015_MONO_LR_SEL_MASK,
+			RT1015_MONO_R_CHANNEL);
+		snd_soc_component_update_bits(component,
+			RT1015_TDM1_4,
+			RT1015_TDM_I2S_TX_L_DAC1_1_MASK |
+			RT1015_TDM_I2S_TX_R_DAC1_1_MASK,
+			((first_bit-1) << RT1015_TDM_I2S_TX_L_DAC1_1_SFT) |
+			(first_bit << RT1015_TDM_I2S_TX_R_DAC1_1_SFT));
+		break;
+	default:
+		ret = -EINVAL;
+		goto _set_tdm_err_;
+	}
+
+	/* Tx slot configuration */
+	tx_slotnum = hweight_long(tx_mask);
+	if (tx_slotnum) {
+		ret = -EINVAL;
+		dev_err(component->dev, "doesn't need to support tx slots\n");
+		goto _set_tdm_err_;
+	}
+
+	snd_soc_component_update_bits(component, RT1015_TDM1_1,
+		RT1015_I2S_CH_TX_MASK | RT1015_I2S_CH_RX_MASK |
+		RT1015_I2S_CH_TX_LEN_MASK | RT1015_I2S_CH_RX_LEN_MASK, val);
+
+_set_tdm_err_:
+	return ret;
+}
+
#2 lore raw
Quoted 14 lines
Quoted 14 lines
sound/soc/codecs/rt1015.c +14 -0
+static int rt1015_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;
+	unsigned int val = 0, rx_slotnum, tx_slotnum;
+	int ret = 0, first_bit;
+
+	switch (slots) {
+	case 4:
+		val |= RT1015_I2S_TX_4CH;
+		break;
+	case 6:
+		val |= RT1015_I2S_TX_6CH;
+		break;
Quoted 8 lines

sound/soc/codecs/rt1015.c static int rt1015_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio) · line 965

Quoted 5 lines
sound/soc/codecs/rt1015.c +5 -0
+	case 8:
+		val |= RT1015_I2S_TX_8CH;
+		break;
+	case 2:
+		break;

nit-pick: I would put the case 2 first to keep the order. I thought for a second this was an error case due to the discontinuity.

OK, will fix that.

Quoted 31 lines
Quoted 7 lines
sound/soc/codecs/rt1015.c +7 -0
+	default:
+		ret = -EINVAL;
+		goto _set_tdm_err_;
+	}
+
+	switch (slot_width) {
+	case 20:

sound/soc/codecs/rt1015.c static int rt1015_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio) · line 982

Quoted 10 lines
sound/soc/codecs/rt1015.c +10 -0
+		val |= RT1015_I2S_CH_TX_LEN_20B;
+		break;
+	case 24:
+		val |= RT1015_I2S_CH_TX_LEN_24B;
+		break;
+	case 32:
+		val |= RT1015_I2S_CH_TX_LEN_32B;
+		break;
+	case 16:
+		break;

nit-pick: same here, I would put 16 first.

sound/soc/codecs/rt1015.c static int rt1015_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio) · line 990

Quoted 8 lines
sound/soc/codecs/rt1015.c +8 -0
+	default:
+		ret = -EINVAL;
+		goto _set_tdm_err_;
+	}
+
+	/* Rx slot configuration */
+	rx_slotnum = hweight_long(rx_mask);
+	if (rx_slotnum > 1 || !rx_slotnum) {

I am confused here, is this saying you can only have a single channel on RX?

Quoted 1 line

If yes should this be simplified as if (rx_slotnum != 1) ?

Yes, RT1015 is a mono amplifier, so only a single channel will be configured. I will fix the check condition as your suggestion. Thanks.

Quoted 15 lines
Quoted 15 lines
sound/soc/codecs/rt1015.c +15 -0
+		ret = -EINVAL;
+		dev_err(component->dev, "too many rx slots or zero slot\n");
+		goto _set_tdm_err_;
+	}
+
+	first_bit = __ffs(rx_mask);
+	switch (first_bit) {
+	case 0:
+	case 2:
+	case 4:
+	case 6:
+		snd_soc_component_update_bits(component,
+			RT1015_PAD_DRV2, RT1015_MONO_LR_SEL_MASK,
+			RT1015_MONO_L_CHANNEL);
+		snd_soc_component_update_bits(component,
Quoted 8 lines

sound/soc/codecs/rt1015.c static int rt1015_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio) · line 1010

Quoted 5 lines
sound/soc/codecs/rt1015.c +5 -0
+			RT1015_TDM1_4,
+			RT1015_TDM_I2S_TX_L_DAC1_1_MASK |
+			RT1015_TDM_I2S_TX_R_DAC1_1_MASK,
+			(first_bit << RT1015_TDM_I2S_TX_L_DAC1_1_SFT) |
+			((first_bit+1) << RT1015_TDM_I2S_TX_R_DAC1_1_SFT));

looks like there's an assumption that the rx mask has contiguous bits set? Maybe add a comment to explain how the RX path works?

Yes. In the normal case, the system will send the stereo audio to the codec. I assume that the stereo audio is placed in slot 0/2/4/6.
I will add a comment above this switch case.

Quoted 15 lines
Quoted 15 lines
sound/soc/codecs/rt1015.c +15 -0
+		break;
+	case 1:
+	case 3:
+	case 5:
+	case 7:
+		snd_soc_component_update_bits(component,
+			RT1015_PAD_DRV2, RT1015_MONO_LR_SEL_MASK,
+			RT1015_MONO_R_CHANNEL);
+		snd_soc_component_update_bits(component,
+			RT1015_TDM1_4,
+			RT1015_TDM_I2S_TX_L_DAC1_1_MASK |
+			RT1015_TDM_I2S_TX_R_DAC1_1_MASK,
+			((first_bit-1) << RT1015_TDM_I2S_TX_L_DAC1_1_SFT) |
+			(first_bit << RT1015_TDM_I2S_TX_R_DAC1_1_SFT));
+		break;