Skip to contentSkip to conversation

ASoC: rt766: add RT766/RT767 SDCA driver

PATCH Superseded [email protected] 3 participants 9 messages Jul 20 – Jul 23

Subsystems (3)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ASoC: rt766: add RT766/RT767 SDCA driver

From: Shuming Fan <[email protected]>

This patch adds the initial SDCA multi-function codec driver for the RT766 and RT767.

---
 sound/soc/codecs/Kconfig          |    8 +
 sound/soc/codecs/Makefile         |    2 +
 sound/soc/codecs/rt766-sdca-sdw.c |  350 +++++++
 sound/soc/codecs/rt766-sdca-sdw.h |   71 ++
 sound/soc/codecs/rt766-sdca.c     | 1521 +++++++++++++++++++++++++++++
 sound/soc/codecs/rt766-sdca.h     |  132 +++
 6 files changed, 2084 insertions(+)
 create mode 100644 sound/soc/codecs/rt766-sdca-sdw.c
 create mode 100644 sound/soc/codecs/rt766-sdca-sdw.h
 create mode 100644 sound/soc/codecs/rt766-sdca.c
 create mode 100644 sound/soc/codecs/rt766-sdca.h
sound/soc/codecs/Kconfig +8 -0
index 76e90144ea91..8b3690fe2b5a 100644
@@ -238,6 +238,7 @@ config SND_SOC_ALL_CODECS
 	imply SND_SOC_RT715_SDCA_SDW
 	imply SND_SOC_RT721_SDCA_SDW
 	imply SND_SOC_RT722_SDCA_SDW
+	imply SND_SOC_RT766_SDCA_SDW
 	imply SND_SOC_RT1308_SDW
 	imply SND_SOC_RT1316_SDW
 	imply SND_SOC_RT1318
@@ -1978,6 +1979,13 @@ config SND_SOC_RT715_SDCA_SDW
 	select REGMAP_SOUNDWIRE
 	select REGMAP_SOUNDWIRE_MBQ
 
+config SND_SOC_RT766_SDCA_SDW
+	tristate "Realtek RT766 SDCA Codec - SDW"
+	depends on SOUNDWIRE
+	depends on SND_SOC_SDCA
+	select REGMAP_SOUNDWIRE
+	select REGMAP_SOUNDWIRE_MBQ
+
 config SND_SOC_RT9120
 	tristate "Richtek RT9120 Stereo Class-D Amplifier"
 	depends on I2C
sound/soc/codecs/Makefile +2 -0
index aa0396e5b575..bcc753bd0ca9 100644
@@ -284,6 +284,7 @@ snd-soc-rt715-y := rt715.o rt715-sdw.o
 snd-soc-rt715-sdca-y := rt715-sdca.o rt715-sdca-sdw.o
 snd-soc-rt721-sdca-y := rt721-sdca.o rt721-sdca-sdw.o
 snd-soc-rt722-sdca-y := rt722-sdca.o rt722-sdca-sdw.o
+snd-soc-rt766-sdca-y := rt766-sdca.o rt766-sdca-sdw.o
 snd-soc-rt9120-y := rt9120.o
 snd-soc-rt9123-y := rt9123.o
 snd-soc-rt9123p-y := rt9123p.o
@@ -724,6 +725,7 @@ obj-$(CONFIG_SND_SOC_RT715)     += snd-soc-rt715.o
 obj-$(CONFIG_SND_SOC_RT715_SDCA_SDW)     += snd-soc-rt715-sdca.o
 obj-$(CONFIG_SND_SOC_RT721_SDCA_SDW)     += snd-soc-rt721-sdca.o
 obj-$(CONFIG_SND_SOC_RT722_SDCA_SDW)     += snd-soc-rt722-sdca.o
+obj-$(CONFIG_SND_SOC_RT766_SDCA_SDW)     += snd-soc-rt766-sdca.o
 obj-$(CONFIG_SND_SOC_RT9120)	+= snd-soc-rt9120.o
 obj-$(CONFIG_SND_SOC_RT9123)	+= snd-soc-rt9123.o
 obj-$(CONFIG_SND_SOC_RT9123P)	+= snd-soc-rt9123p.o
sound/soc/codecs/rt766-sdca-sdw.c new file +350 -0

350 lines not on this page · raw message

sound/soc/codecs/rt766-sdca-sdw.h new file +71 -0

71 lines not on this page · raw message

sound/soc/codecs/rt766-sdca.c new file +1521 -0

1,521 lines not on this page · raw message

sound/soc/codecs/rt766-sdca.h new file +132 -0

132 lines not on this page · raw message

Signature · 1 line
2.54.0
#1 lore raw
Quoted 5 lines · On 7/20/26 11:06, [email protected] wrote:

From: Shuming Fan <[email protected]>

This patch adds the initial SDCA multi-function codec driver for the RT766 and RT767.

Looks mostly good, I only have minor comments, see below.

Quoted 21 lines
sound/soc/codecs/rt766-sdca-sdw.c new file +21 -0
+static bool rt766_sdca_readable_register(struct device *dev, unsigned int reg)
+{
+	switch (reg) {
+	case SDW_SCP_SDCA_INT1 ... SDW_SCP_SDCA_INTMASK4:
+	case RT766_VERSION_ID ... RT766_BOND_LATCH_ID:
+	case 0xc344 ... 0xc345:
+	case 0xc900:
+	case 0xc920:
+	case 0xd540 ... 0xd542:
+	case 0xf01e:
+	case RT766_HP_POWER_STATE ... RT766_HP_FSM_CTL2_1:
+	case 0x310100:
+	case RT766_MCU_PATCH_ADDR1_START ... RT766_MCU_PATCH_ADDR1_END:
+	case RT766_MCU_PATCH_ADDR2_START ... RT766_MCU_PATCH_ADDR2_END:
+	case RT766_MUTE_REG(UAJ, USER_FU41, 1):
+	case RT766_MUTE_REG(UAJ, USER_FU41, 2):
+	case RT766_VOLUME_REG(UAJ, USER_FU41, 1):
+	case RT766_VOLUME_REG(UAJ, USER_FU41, 2):
+	case RT766_MUTE_REG(UAJ, USER_FU36, 1):
+	case RT766_MUTE_REG(UAJ, USER_FU36, 2):
+	case RT766_VOLUME_REG(UAJ, USER_FU36, 1):

sound/soc/codecs/rt766-sdca-sdw.c line 51

Quoted 10 lines
sound/soc/codecs/rt766-sdca-sdw.c new file +10 -0
+	case RT766_VOLUME_REG(UAJ, USER_FU36, 2):
+	case RT766_PDE_REQ_REG(UAJ, PDE47):
+	case RT766_PDE_REQ_REG(UAJ, PDE34):
+	case RT766_SDCA_CTL(UAJ, CS41, SDCA_CTL_CS_SAMPLERATEINDEX):
+	case RT766_SDCA_CTL(UAJ, CS36, SDCA_CTL_CS_SAMPLERATEINDEX):
+	/* 0x40480000 */
+	case RT766_FUNC_STATUS_REG(UAJ):
+	/* 0x40481400 */
+	case RT766_PDE_ACTUAL_REG(UAJ, PDE47):
+	/* 0x40481480 */

these single line registers listed in comments confuse me, not clear for example if this one applies to the following line...

Review

Quoted 1 line
(quoted diff) +1 -0
+	case RT766_PDE_ACTUAL_REG(UAJ, PDE34):

but then why not adde the values for the next 3?

Quoted 6 lines
sound/soc/codecs/rt766-sdca-sdw.c new file +6 -0
+	case RT766_GAIN_REG(UAJ, PLATFORM_FU33, 1):
+	case RT766_GAIN_REG(UAJ, PLATFORM_FU33, 2):
+	case RT766_SDCA_CTL(UAJ, GE49, SDCA_CTL_GE_SELECTED_MODE):
+	/* 0x40600490 */
+	case RT766_SDCA_CTL(UAJ, GE49, SDCA_CTL_GE_DETECTED_MODE):
+	case RT766_PDE_REQ_REG(MIC, PDE11):

sound/soc/codecs/rt766-sdca-sdw.c line 68

Quoted 10 lines
sound/soc/codecs/rt766-sdca-sdw.c new file +10 -0
+	case RT766_MUTE_REG(MIC, USER_FU113, 1):
+	case RT766_MUTE_REG(MIC, USER_FU113, 2):
+	case RT766_MUTE_REG(MIC, USER_FU113, 3):
+	case RT766_MUTE_REG(MIC, USER_FU113, 4):
+	case RT766_VOLUME_REG(MIC, USER_FU113, 1):
+	case RT766_VOLUME_REG(MIC, USER_FU113, 2):
+	case RT766_VOLUME_REG(MIC, USER_FU113, 3):
+	case RT766_VOLUME_REG(MIC, USER_FU113, 4):
+	/* 0x40880000 */
+	case RT766_FUNC_STATUS_REG(MIC):

same here, are 0x40880000 and RT766_FUNC_STATUS_REG equivalent? If yes maybe move the comment to the right side?

Quoted 20 lines
sound/soc/codecs/rt766-sdca-sdw.c new file +20 -0
+	case RT766_SDCA_CTL(MIC, CS113, SDCA_CTL_CS_SAMPLERATEINDEX):
+	/* 0x40881500 */
+	case RT766_PDE_ACTUAL_REG(MIC, PDE11):
+	/* 0x40c80000 */
+	case RT766_FUNC_STATUS_REG(HID):
+	/* 0x40c80080 - 0x40c80098 */
+	case RT766_SDCA_CTL(HID, HID101, SDCA_CTL_HIDE_HIDTX_CURRENTOWNER) ...
+		RT766_SDCA_CTL(HID, HID101, SDCA_CTL_HIDE_HIDTX_MESSAGELENGTH):
+	case RT766_MUTE_REG(AMP, USER_FU21, 1):
+	case RT766_MUTE_REG(AMP, USER_FU21, 2):
+	case RT766_VOLUME_REG(AMP, USER_FU21, 1):
+	case RT766_VOLUME_REG(AMP, USER_FU21, 2):
+	case RT766_PDE_REQ_REG(AMP, PDE23):
+	/* 0x41080000 */
+	case RT766_FUNC_STATUS_REG(AMP):
+	case RT766_SDCA_CTL(AMP, PPU21, SDCA_CTL_PPU_POSTURENUMBER):
+	case RT766_SDCA_CTL(AMP, CS21, SDCA_CTL_CS_SAMPLERATEINDEX):
+	/* 0x41081980 */
+	case RT766_PDE_ACTUAL_REG(AMP, PDE23):
+	case RT766_BUF_ADDR_HID1 ... RT766_BUF_ADDR_HID2:

sound/soc/codecs/rt766-sdca-sdw.c

Quoted 10 lines
sound/soc/codecs/rt766-sdca-sdw.c new file +9 -0
+		return true;
+	default:
+		return false;
+	}
+}
 
+static const struct reg_default rt766_sdca_defaults[] = {
+	/* 0x40400289 - 0x4040028a */
+	{ RT766_MUTE_REG(UAJ, USER_FU41, 1), 0x01 },
+	{ RT766_MUTE_REG(UAJ, USER_FU41, 2), 0x01 },

things are much clearer/organized here!

Quoted 17 lines
sound/soc/codecs/rt766-sdca-sdw.h new file +17 -0
+	/* 0x40400291 - 0x40400292 */
+	{ RT766_VOLUME_REG(UAJ, USER_FU41, 1), 0x0000 },
+	{ RT766_VOLUME_REG(UAJ, USER_FU41, 2), 0x0000 },
+	/* 0x40400789 - 0x4040078a */
+	{ RT766_MUTE_REG(UAJ, USER_FU36, 1), 0x01 },
+	{ RT766_MUTE_REG(UAJ, USER_FU36, 2), 0x01 },
+	/* 0x40400791 - 0x40400792 */
+	{ RT766_VOLUME_REG(UAJ, USER_FU36, 1), 0x0000 },
+	{ RT766_VOLUME_REG(UAJ, USER_FU36, 2), 0x0000 },
+	/* 0x40401408 */
+	{ RT766_PDE_REQ_REG(UAJ, PDE47), 0x03 },
+	/* 0x40401488 */
+	{ RT766_PDE_REQ_REG(UAJ, PDE34), 0x03 },
+	/* 0x40480080 */
+	{ RT766_SDCA_CTL(UAJ, CS41, SDCA_CTL_CS_SAMPLERATEINDEX), 0x09 },
+	/* 0x40480880 */
+	{ RT766_SDCA_CTL(UAJ, CS36, SDCA_CTL_CS_SAMPLERATEINDEX), 0x09 },

sound/soc/codecs/rt766-sdca-sdw.h

Quoted 10 lines
sound/soc/codecs/rt766-sdca-sdw.h new file +9 -0
+	/* 0x40600259 - 0x4060025a */
+	{ RT766_GAIN_REG(UAJ, PLATFORM_FU33, 1), 0xfe00 },
+	{ RT766_GAIN_REG(UAJ, PLATFORM_FU33, 2), 0xfe00 },
+	/* 0x40600488 */
+	{ RT766_SDCA_CTL(UAJ, GE49, SDCA_CTL_GE_SELECTED_MODE), 0x00 },
 
+static struct sdca_entity *rt766_find_entity_by_label(struct sdca_function_data *func,
+	const char *label)
+{
+	struct sdca_entity *entity = NULL;

useless initialization?

Quoted 45 lines
(quoted diff) +44 -0
+	int idx;
+
+	for (idx = 0; idx < func->num_entities; idx++) {
+		entity = &func->entities[idx];
+
+		if (!strcmp(entity->label, label))
+			return entity;
+	}
+
+	return NULL;
+}
 
+static int rt766_sdca_irq_ctl(struct rt766_sdca_priv *rt766,
+							  struct sdca_function_data *function,
+							  struct snd_soc_component *component,
+							  struct sdca_interrupt_info *info,
+							  bool enabled)
+{
+	struct device *dev = &rt766->slave->dev;
+	struct sdca_interrupt *interrupt;
+	struct sdca_control *control;
+	struct sdca_entity *entity;
+	irq_handler_t handler;
+	int i, j, irq, ret;
+
+	for (i = 0; i < function->num_entities; i++) {
+		entity = &function->entities[i];
+
+		for (j = 0; j < entity->num_controls; j++) {
+			control = &entity->controls[j];
+			irq = control->interrupt_position;
+
+			switch (SDCA_CTL_TYPE(entity->type, control->sel)) {
+			case SDCA_CTL_TYPE_S(GE, DETECTED_MODE):
+				handler = rt766_sdca_irq_jd_handler;
+				break;
+			case SDCA_CTL_TYPE_S(HIDE, HIDTX_CURRENTOWNER):
+				handler = rt766_sdca_irq_btn_handler;
+				break;
+			default:
+				continue;
+			}
+
+			interrupt = &info->irqs[irq];
+

Review

Quoted 10 lines
(quoted diff) +10 -0
+			if (enabled) {
+				ret = sdca_irq_data_populate(dev, rt766->regmap, component,
+								function, entity, control,
+								interrupt);
+				if (ret)
+					return ret;
+
+				interrupt->priv = rt766;
+				ret = sdca_irq_request(dev, info, irq, interrupt->name,
+								handler, interrupt);

There was a proposal from Charles to do an irq_populate_early, is there a conflict between the two patchsets? see "ASoC: SDCA: Populate IRQ data earlier"

Quoted 10 lines
sound/soc/codecs/rt766-sdca.c new file +10 -0
+				if (ret) {
+					dev_err(dev, "failed to request irq %s: %d\n",
+						interrupt->name, ret);
+					return ret;
+				}
+				dev_dbg(dev, "Requesting IRQ %d InterruptName=%s\n", irq, interrupt->name);
+			} else {
+				sdca_irq_free(dev, info, irq, interrupt->name, interrupt);
+				dev_dbg(dev, "Freeing IRQ %d\n", irq);
+			}

sound/soc/codecs/rt766-sdca.c line 315

Quoted 10 lines
sound/soc/codecs/rt766-sdca.c new file +9 -0
+		}
+	}
+
+	return 0;
+}
 
+static int rt766_sdca_set_fu41_playback_ctl(struct rt766_sdca_priv *rt766)
+{
+	int err;
+	unsigned int ch_01, ch_02;

nit-pick: likely copy/paste from other drivers, but flipping the two lines would be nicer. there are a couple of similar patterns in this driver.

Quoted 11 lines
sound/soc/codecs/rt766-sdca.c new file +11 -0
+
+	ch_01 = (rt766->fu41_dapm_mute || rt766->fu41_mixer_l_mute) ? 0x01 : 0x00;
+	ch_02 = (rt766->fu41_dapm_mute || rt766->fu41_mixer_r_mute) ? 0x01 : 0x00;
+
+	err = regmap_write(rt766->regmap, RT766_MUTE_REG(UAJ, USER_FU41, 1), ch_01);
+	if (err < 0)
+		return err;
+
+	err = regmap_write(rt766->regmap, RT766_MUTE_REG(UAJ, USER_FU41, 2), ch_02);
+	if (err < 0)
+		return err;

sound/soc/codecs/rt766-sdca.c

Quoted 10 lines
sound/soc/codecs/rt766-sdca.c new file +9 -0
+
+	return 0;
+}
 
+static int rt766_sdca_pde47_event(struct snd_soc_dapm_widget *w,
+	struct snd_kcontrol *kcontrol, int event)
+{
+	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
+	struct rt766_sdca_priv *rt766 = snd_soc_component_get_drvdata(component);
+	const struct sdca_entity *entity = NULL;

useless init?

Quoted 27 lines
sound/soc/codecs/rt766-sdca.c new file +27 -0
+	unsigned char ps0 = 0x0, ps3 = 0x3;
+	int from_ps, to_ps;
+	int ret;
+
+	switch (event) {
+	case SND_SOC_DAPM_POST_PMU:
+		regmap_write(rt766->regmap, RT766_PDE_REQ_REG(UAJ, PDE47), ps0);
+		from_ps = ps3;
+		to_ps = ps0;
+		break;
+	case SND_SOC_DAPM_PRE_PMD:
+		regmap_write(rt766->regmap, RT766_PDE_REQ_REG(UAJ, PDE47), ps3);
+		from_ps = ps0;
+		to_ps = ps3;
+		break;
+	}
+
+	entity = rt766_find_entity_by_label(rt766->uaj_func_data, "PDE 47");
+	ret = sdca_asoc_pde_poll_actual_ps(component->dev, rt766->regmap,
+				   RT766_FUNC_NUM_UAJ,
+				   RT766_SDCA_ENT_PDE47,
+				   from_ps, to_ps,
+				   entity ? entity->pde.max_delay : NULL,
+				   entity ? entity->pde.num_max_delay : 0);
+	if (ret)
+		dev_err(component->dev, "%s: PDE transition %x -> %x failed, err=%d\n",
+			__func__, from_ps, to_ps, ret);

sound/soc/codecs/rt766-sdca.c line 674

Quoted 10 lines
sound/soc/codecs/rt766-sdca.c new file +9 -0
+
+	return ret;
+}
 
+static int rt766_sdca_pde34_event(struct snd_soc_dapm_widget *w,
+	struct snd_kcontrol *kcontrol, int event)
+{
+	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
+	struct rt766_sdca_priv *rt766 = snd_soc_component_get_drvdata(component);
+	const struct sdca_entity *entity = NULL;

useless init? Same comment for all PDE events.

Quoted 43 lines
sound/soc/codecs/rt766-sdca.c new file +41 -0
+	unsigned char ps0 = 0x0, ps3 = 0x3;
+	int from_ps, to_ps;
+	int ret;
+
+	switch (event) {
+	case SND_SOC_DAPM_POST_PMU:
+		regmap_write(rt766->regmap, RT766_PDE_REQ_REG(UAJ, PDE34), ps0);
+		from_ps = ps3;
+		to_ps = ps0;
+		break;
+	case SND_SOC_DAPM_PRE_PMD:
+		regmap_write(rt766->regmap, RT766_PDE_REQ_REG(UAJ, PDE34), ps3);
+		from_ps = ps0;
+		to_ps = ps3;
+		break;
+	}
+
+	entity = rt766_find_entity_by_label(rt766->uaj_func_data, "PDE 34");
+	ret = sdca_asoc_pde_poll_actual_ps(component->dev, rt766->regmap,
+				   RT766_FUNC_NUM_UAJ,
+				   RT766_SDCA_ENT_PDE34,
+				   from_ps, to_ps,
+				   entity ? entity->pde.max_delay : NULL,
+				   entity ? entity->pde.num_max_delay : 0);
+	if (ret)
+		dev_err(component->dev, "%s: PDE transition %x -> %x failed, err=%d\n",
+			__func__, from_ps, to_ps, ret);
+
+	return ret;
+}
 
 
+static int rt766_sdca_probe(struct snd_soc_component *component)
+{
+	struct rt766_sdca_priv *rt766 = snd_soc_component_get_drvdata(component);
+	struct device *dev = &rt766->slave->dev;
+	int ret;
+
+	rt766->component = component;
+
+	ret = pm_runtime_resume(component->dev);
+	if (ret < 0 && ret != -EACCES)
+		return ret;

sound/soc/codecs/rt766-sdca.c

Quoted 10 lines
sound/soc/codecs/rt766-sdca.c new file +10 -0
+
+	if (rt766->uaj_func_data) {
+		dev_dbg(dev, "%s : irq %d\n", __func__, rt766->slave->irq);
+
+		rt766->irq_info = sdca_irq_allocate(dev, rt766->regmap, rt766->slave->irq);
+		if (IS_ERR(rt766->irq_info))
+			return PTR_ERR(rt766->irq_info);
+
+		ret = rt766_sdca_irq_ctl(rt766, rt766->uaj_func_data,
+			component, rt766->irq_info, true);

same question on IRQ allocate/request for Charles.

Quoted 12 lines
sound/soc/codecs/rt766-sdca.c new file +12 -0
+		if (ret < 0) {
+			dev_err(dev, "Failed to request UAJ SDCA IRQ: %d\n", ret);
+			return ret;
+		}
+
+		if (rt766->hid_func_data) {
+			ret = rt766_sdca_irq_ctl(rt766, rt766->hid_func_data,
+				component, rt766->irq_info, true);
+			if (ret < 0) {
+				dev_err(dev, "Failed to request HID SDCA IRQ: %d\n", ret);
+				return ret;
+			}

sound/soc/codecs/rt766-sdca.c

Quoted 10 lines
sound/soc/codecs/rt766-sdca.c new file +9 -0
+		}
+	}
+
+	return 0;
+}
 
+static unsigned int rt766_find_dt_rates(struct device *dev, struct sdca_function_data *function,
+							const char *label)
+{
+	struct sdca_entity *entity = NULL;

useless init?

Quoted 96 lines
sound/soc/codecs/rt766-sdca.c new file +96 -0
+	unsigned int rates;
+	int i, ret;
+
+	for (i = 0; i < function->num_entities; i++) {
+		entity = &function->entities[i];
+
+		if (strcmp(entity->label, label))
+			continue;
+
+		/* Can't check earlier as only terminals have an iot member. */
+		if (!entity->iot.is_dataport)
+			continue;
+
+		ret = rt766_parse_rates(dev, function, entity, &rates);
+		if (ret < 0) {
+			dev_dbg(dev, "%s: failed to parse rates for entity %s\n",
+				__func__, entity->label);
+			return 0;
+		}
+
+		dev_dbg(dev, "%s: %s supports rates 0x%08x\n", __func__, entity->label, rates);
+	}
+
+	return rates;
+}
+
+int rt766_sdca_init(struct device *dev, struct regmap *regmap, struct sdw_slave *slave)
+{
+	struct sdca_function_data *func_data_ptr;
+	struct rt766_sdca_priv *rt766;
+	unsigned int rates;
+	int ret;
+	int i;
+
+	rt766 = devm_kzalloc(dev, sizeof(*rt766), GFP_KERNEL);
+	if (!rt766)
+		return -ENOMEM;
+
+	dev_set_drvdata(dev, rt766);
+	rt766->slave = slave;
+	rt766->regmap = regmap;
+
+	regcache_cache_only(rt766->regmap, true);
+
+	mutex_init(&rt766->disable_irq_lock);
+
+	/*
+	 * Mark hw_init to false
+	 * HW init will be performed when device reports present
+	 */
+	rt766->hw_init = false;
+	rt766->first_hw_init = false;
+	rt766->fu41_dapm_mute = true;
+	rt766->fu41_mixer_l_mute = rt766->fu41_mixer_r_mute = false;
+	rt766->fu36_dapm_mute = true;
+	rt766->fu36_mixer_l_mute = rt766->fu36_mixer_r_mute = true;
+	rt766->fu21_dapm_mute = true;
+	rt766->fu21_mixer_l_mute = rt766->fu21_mixer_r_mute = false;
+	rt766->fu113_dapm_mute = true;
+	rt766->fu113_mixer_mute[0] = rt766->fu113_mixer_mute[1] =
+		rt766->fu113_mixer_mute[2] = rt766->fu113_mixer_mute[3] = true;
+
+	/* get SDCA function data */
+	dev_dbg(dev, "SDCA functions found: %d", slave->sdca_data.num_functions);
+	for (i = 0; i < slave->sdca_data.num_functions; i++) {
+		func_data_ptr = devm_kzalloc(dev, sizeof(*func_data_ptr), GFP_KERNEL);
+		if (!func_data_ptr)
+			return dev_err_probe(dev, -ENOMEM, "failed to allocate function data");
+
+		func_data_ptr->desc = &slave->sdca_data.function[i];
+		ret = sdca_parse_function(dev, slave, func_data_ptr);
+		if (ret) {
+			devm_kfree(dev, func_data_ptr);
+			return ret;
+		}
+		dev_dbg(dev, "Function type=%d, num_entities=%d",
+			slave->sdca_data.function[i].type, func_data_ptr->num_entities);
+
+		switch (slave->sdca_data.function[i].type) {
+		case SDCA_FUNCTION_TYPE_UAJ:
+			rt766->uaj_func_data = func_data_ptr;
+			/*
+			 * Some machines may only support a subset of the sample rates supported by the codec.
+			 * Therefore, we need to parse the supported sample rates from the DisCo table and
+			 * configure them in the DAI. If the DisCo table does not provide sample rate information,
+			 * we will fall back to the default supported rates defined in the codec driver.
+			 */
+			rates = rt766_find_dt_rates(dev, func_data_ptr, "IT 41");
+			if (rates)
+				rt766_sdca_dai[0].playback.rates = rates;
+
+			rates = rt766_find_dt_rates(dev, func_data_ptr, "OT 36");
+			if (rates)
+				rt766_sdca_dai[0].capture.rates = rates;
+			break;
+		case SDCA_FUNCTION_TYPE_SMART_AMP:

sound/soc/codecs/rt766-sdca.c line 1387

Quoted 10 lines
sound/soc/codecs/rt766-sdca.c new file +10 -0
+			rt766->sa_func_data = func_data_ptr;
+			rates = rt766_find_dt_rates(dev, func_data_ptr, "IT 21");
+			if (rates)
+				rt766_sdca_dai[1].playback.rates = rates;
+			break;
+		case SDCA_FUNCTION_TYPE_SMART_MIC:
+			rt766->sm_func_data = func_data_ptr;
+			rates = rt766_find_dt_rates(dev, func_data_ptr, "OT 113");
+			if (rates)
+				rt766_sdca_dai[2].capture.rates = rates;

are the sdca_dai[index] related to the function? Wondering if we can use e.g sdca_dai[MIC] instead?

Quoted 28 lines
sound/soc/codecs/rt766-sdca.c new file +28 -0
+			break;
+		case SDCA_FUNCTION_TYPE_HID:
+			rt766->hid_func_data = func_data_ptr;
+			break;
+		default:
+			dev_dbg(dev, "Unexpected SDCA function type found: %d",
+				slave->sdca_data.function[i].type);
+		}
+	}
+
+	ret =  devm_snd_soc_register_component(dev,
+			&soc_sdca_dev_rt766, rt766_sdca_dai, ARRAY_SIZE(rt766_sdca_dai));
+	if (ret < 0)
+		return ret;
+
+	/* set autosuspend parameters */
+	pm_runtime_set_autosuspend_delay(dev, 3000);
+	pm_runtime_use_autosuspend(dev);
+
+	/* make sure the device does not suspend immediately */
+	pm_runtime_mark_last_busy(dev);
+
+	pm_runtime_enable(dev);
+
+	dev_dbg(dev, "%s\n", __func__);
+
+	return 0;
+}
#2 lore raw
Quoted 6 lines
Quoted 1 line

This patch adds the initial SDCA multi-function codec driver for the RT766

and RT767.

Quoted 2 lines

Looks mostly good, I only have minor comments, see below.

Thanks for the review.

Quoted 30 lines

sound/soc/codecs/rt766-sdca-sdw.c

Quoted 13 lines
sound/soc/codecs/rt766-sdca-sdw.c new file +13 -0
+static bool rt766_sdca_readable_register(struct device *dev, unsigned
+int reg) {
+     switch (reg) {
+     case SDW_SCP_SDCA_INT1 ... SDW_SCP_SDCA_INTMASK4:
+     case RT766_VERSION_ID ... RT766_BOND_LATCH_ID:
+     case 0xc344 ... 0xc345:
+     case 0xc900:
+     case 0xc920:
+     case 0xd540 ... 0xd542:
+     case 0xf01e:
+     case RT766_HP_POWER_STATE ... RT766_HP_FSM_CTL2_1:
+     case 0x310100:
+     case RT766_MCU_PATCH_ADDR1_START ...

RT766_MCU_PATCH_ADDR1_END:

Quoted 1 line

+ case RT766_MCU_PATCH_ADDR2_START ...

RT766_MCU_PATCH_ADDR2_END:

sound/soc/codecs/rt766-sdca-sdw.c

Quoted 11 lines
sound/soc/codecs/rt766-sdca-sdw.c new file +11 -0
+     case RT766_MUTE_REG(UAJ, USER_FU41, 1):
+     case RT766_MUTE_REG(UAJ, USER_FU41, 2):
+     case RT766_VOLUME_REG(UAJ, USER_FU41, 1):
+     case RT766_VOLUME_REG(UAJ, USER_FU41, 2):
+     case RT766_MUTE_REG(UAJ, USER_FU36, 1):
+     case RT766_MUTE_REG(UAJ, USER_FU36, 2):
+     case RT766_VOLUME_REG(UAJ, USER_FU36, 1):
+     case RT766_VOLUME_REG(UAJ, USER_FU36, 2):
+     case RT766_PDE_REQ_REG(UAJ, PDE47):
+     case RT766_PDE_REQ_REG(UAJ, PDE34):
+     case RT766_SDCA_CTL(UAJ, CS41,

SDCA_CTL_CS_SAMPLERATEINDEX):

Quoted 1 line

+ case RT766_SDCA_CTL(UAJ, CS36,

SDCA_CTL_CS_SAMPLERATEINDEX):

Quoted 8 lines

sound/soc/codecs/rt766-sdca-sdw.c line 51

Quoted 5 lines
sound/soc/codecs/rt766-sdca-sdw.c new file +5 -0
+     /* 0x40480000 */
+     case RT766_FUNC_STATUS_REG(UAJ):
+     /* 0x40481400 */
+     case RT766_PDE_ACTUAL_REG(UAJ, PDE47):
+     /* 0x40481480 */

these single line registers listed in comments confuse me, not clear for example if this one applies to the following line...

Sorry these address comments caused any confusion. These address comments help me put the code in address order. I will apply single address comment to the following line.

Quoted 3 lines
Quoted 1 line

+ case RT766_PDE_ACTUAL_REG(UAJ, PDE34):

but then why not adde the values for the next 3?

Those registers have the comment in the rt766_sdca_defaults.

Quoted 86 lines

sound/soc/codecs/rt766-sdca-sdw.c

Quoted 3 lines
sound/soc/codecs/rt766-sdca-sdw.c new file +3 -0
+     case RT766_GAIN_REG(UAJ, PLATFORM_FU33, 1):
+     case RT766_GAIN_REG(UAJ, PLATFORM_FU33, 2):
+     case RT766_SDCA_CTL(UAJ, GE49,

SDCA_CTL_GE_SELECTED_MODE):

Review

Quoted 2 lines
(quoted diff) +2 -0
+     /* 0x40600490 */
+     case RT766_SDCA_CTL(UAJ, GE49,

SDCA_CTL_GE_DETECTED_MODE):

sound/soc/codecs/rt766-sdca-sdw.c line 68

Quoted 11 lines
sound/soc/codecs/rt766-sdca-sdw.c new file +11 -0
+     case RT766_PDE_REQ_REG(MIC, PDE11):
+     case RT766_MUTE_REG(MIC, USER_FU113, 1):
+     case RT766_MUTE_REG(MIC, USER_FU113, 2):
+     case RT766_MUTE_REG(MIC, USER_FU113, 3):
+     case RT766_MUTE_REG(MIC, USER_FU113, 4):
+     case RT766_VOLUME_REG(MIC, USER_FU113, 1):
+     case RT766_VOLUME_REG(MIC, USER_FU113, 2):
+     case RT766_VOLUME_REG(MIC, USER_FU113, 3):
+     case RT766_VOLUME_REG(MIC, USER_FU113, 4):
+     /* 0x40880000 */
+     case RT766_FUNC_STATUS_REG(MIC):

same here, are 0x40880000 and RT766_FUNC_STATUS_REG equivalent? If yes maybe move the comment to the right side?

Quoted 1 line

+ case RT766_SDCA_CTL(MIC, CS113,

SDCA_CTL_CS_SAMPLERATEINDEX):

sound/soc/codecs/rt766-sdca-sdw.c

Quoted 6 lines
sound/soc/codecs/rt766-sdca-sdw.c new file +6 -0
+     /* 0x40881500 */
+     case RT766_PDE_ACTUAL_REG(MIC, PDE11):
+     /* 0x40c80000 */
+     case RT766_FUNC_STATUS_REG(HID):
+     /* 0x40c80080 - 0x40c80098 */
+     case RT766_SDCA_CTL(HID, HID101,

SDCA_CTL_HIDE_HIDTX_CURRENTOWNER) ...

Quoted 1 line

+ RT766_SDCA_CTL(HID, HID101,

SDCA_CTL_HIDE_HIDTX_MESSAGELENGTH):

sound/soc/codecs/rt766-sdca-sdw.c

Quoted 8 lines
sound/soc/codecs/rt766-sdca-sdw.c new file +8 -0
+     case RT766_MUTE_REG(AMP, USER_FU21, 1):
+     case RT766_MUTE_REG(AMP, USER_FU21, 2):
+     case RT766_VOLUME_REG(AMP, USER_FU21, 1):
+     case RT766_VOLUME_REG(AMP, USER_FU21, 2):
+     case RT766_PDE_REQ_REG(AMP, PDE23):
+     /* 0x41080000 */
+     case RT766_FUNC_STATUS_REG(AMP):
+     case RT766_SDCA_CTL(AMP, PPU21,

SDCA_CTL_PPU_POSTURENUMBER):

Quoted 1 line

+ case RT766_SDCA_CTL(AMP, CS21,

SDCA_CTL_CS_SAMPLERATEINDEX):

sound/soc/codecs/rt766-sdca-sdw.c

Quoted 13 lines
sound/soc/codecs/rt766-sdca-sdw.c new file +12 -0
+     /* 0x41081980 */
+     case RT766_PDE_ACTUAL_REG(AMP, PDE23):
+     case RT766_BUF_ADDR_HID1 ... RT766_BUF_ADDR_HID2:
+             return true;
+     default:
+             return false;
+     }
+}
 
+static const struct reg_default rt766_sdca_defaults[] = {
+     /* 0x40400289 - 0x4040028a */
+     { RT766_MUTE_REG(UAJ, USER_FU41, 1), 0x01 },
+     { RT766_MUTE_REG(UAJ, USER_FU41, 2), 0x01 },

things are much clearer/organized here!

Quoted 5 lines
sound/soc/codecs/rt766-sdca-sdw.h new file +5 -0
+     /* 0x40400291 - 0x40400292 */
+     { RT766_VOLUME_REG(UAJ, USER_FU41, 1), 0x0000 },
+     { RT766_VOLUME_REG(UAJ, USER_FU41, 2), 0x0000 },
+     /* 0x40400789 - 0x4040078a */
+     { RT766_MUTE_REG(UAJ, USER_FU36, 1), 0x01 },

sound/soc/codecs/rt766-sdca-sdw.h

Quoted 10 lines
sound/soc/codecs/rt766-sdca-sdw.h new file +10 -0
+     { RT766_MUTE_REG(UAJ, USER_FU36, 2), 0x01 },
+     /* 0x40400791 - 0x40400792 */
+     { RT766_VOLUME_REG(UAJ, USER_FU36, 1), 0x0000 },
+     { RT766_VOLUME_REG(UAJ, USER_FU36, 2), 0x0000 },
+     /* 0x40401408 */
+     { RT766_PDE_REQ_REG(UAJ, PDE47), 0x03 },
+     /* 0x40401488 */
+     { RT766_PDE_REQ_REG(UAJ, PDE34), 0x03 },
+     /* 0x40480080 */
+     { RT766_SDCA_CTL(UAJ, CS41, SDCA_CTL_CS_SAMPLERATEINDEX),

0x09 },

Review

Quoted 2 lines
(quoted diff) +2 -0
+     /* 0x40480880 */
+     { RT766_SDCA_CTL(UAJ, CS36, SDCA_CTL_CS_SAMPLERATEINDEX),

0x09 },

sound/soc/codecs/rt766-sdca-sdw.h

Quoted 5 lines
sound/soc/codecs/rt766-sdca-sdw.h new file +5 -0
+     /* 0x40600259 - 0x4060025a */
+     { RT766_GAIN_REG(UAJ, PLATFORM_FU33, 1), 0xfe00 },
+     { RT766_GAIN_REG(UAJ, PLATFORM_FU33, 2), 0xfe00 },
+     /* 0x40600488 */
+     { RT766_SDCA_CTL(UAJ, GE49, SDCA_CTL_GE_SELECTED_MODE),

0x00 },

Quoted 7 lines
Quoted 1 line

+static struct sdca_entity *rt766_find_entity_by_label(struct

sdca_function_data *func,

sound/soc/codecs/rt766-sdca.c line 35

Quoted 3 lines
sound/soc/codecs/rt766-sdca.c new file +3 -0
+     const char *label)
+{
+     struct sdca_entity *entity = NULL;

useless initialization?

Will export find_sdca_entity_by_label() instead.

Quoted 63 lines
Quoted 4 lines
sound/soc/codecs/rt766-sdca.c new file +4 -0
+     int idx;
+
+     for (idx = 0; idx < func->num_entities; idx++) {
+             entity = &func->entities[idx];

sound/soc/codecs/rt766-sdca.c

Quoted 10 lines
sound/soc/codecs/rt766-sdca.c new file +9 -0
+
+             if (!strcmp(entity->label, label))
+                     return entity;
+     }
+
+     return NULL;
+}
 
+static int rt766_sdca_irq_ctl(struct rt766_sdca_priv *rt766,
+                                                       struct

sdca_function_data *function,

Quoted 1 line

+ struct

snd_soc_component *component,

Quoted 1 line

+ struct

sdca_interrupt_info *info,

Quoted 1 line

+ bool

enabled)

Quoted 6 lines
sound/soc/codecs/rt766-sdca.c new file +6 -0
+{
+     struct device *dev = &rt766->slave->dev;
+     struct sdca_interrupt *interrupt;
+     struct sdca_control *control;
+     struct sdca_entity *entity;
+     irq_handler_t handler;

sound/soc/codecs/rt766-sdca.c

Quoted 10 lines
sound/soc/codecs/rt766-sdca.c new file +10 -0
+     int i, j, irq, ret;
+
+     for (i = 0; i < function->num_entities; i++) {
+             entity = &function->entities[i];
+
+             for (j = 0; j < entity->num_controls; j++) {
+                     control = &entity->controls[j];
+                     irq = control->interrupt_position;
+
+                     switch (SDCA_CTL_TYPE(entity->type,

control->sel)) {

sound/soc/codecs/rt766-sdca.c

Quoted 4 lines
sound/soc/codecs/rt766-sdca.c new file +4 -0
+                     case SDCA_CTL_TYPE_S(GE, DETECTED_MODE):
+                             handler = rt766_sdca_irq_jd_handler;
+                             break;
+                     case SDCA_CTL_TYPE_S(HIDE,

HIDTX_CURRENTOWNER):

sound/soc/codecs/rt766-sdca.c

Quoted 10 lines
sound/soc/codecs/rt766-sdca.c new file +10 -0
+                             handler = rt766_sdca_irq_btn_handler;
+                             break;
+                     default:
+                             continue;
+                     }
+
+                     interrupt = &info->irqs[irq];
+
+                     if (enabled) {
+                             ret = sdca_irq_data_populate(dev,

rt766->regmap, component,

Quoted 1 line

+

function, entity, control,

Quoted 1 line

+

interrupt);

sound/soc/codecs/rt766-sdca.c

Quoted 5 lines
sound/soc/codecs/rt766-sdca.c new file +5 -0
+                             if (ret)
+                                     return ret;
+
+                             interrupt->priv = rt766;
+                             ret = sdca_irq_request(dev, info, irq,
Quoted 8 lines

interrupt->name,

Quoted 1 line

+

handler,

Quoted 1 line

+ interrupt);

There was a proposal from Charles to do an irq_populate_early, is there a conflict between the two patchsets? see "ASoC: SDCA: Populate IRQ data earlier"

Will check with Charles.

Quoted 20 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+                             if (ret) {
+                                     dev_err(dev, "failed to

request irq %s: %d\n",

Quoted 1 line

+ interrupt->name,

ret);

sound/soc/codecs/rt766-sdca-sdw.c

Quoted 3 lines
sound/soc/codecs/rt766-sdca-sdw.c new file +3 -0
+                                     return ret;
+                             }
+                             dev_dbg(dev, "Requesting IRQ %d

InterruptName=%s\n", irq, interrupt->name);

Review

Quoted 2 lines
(quoted diff) +2 -0
+                     } else {
+                             sdca_irq_free(dev, info, irq,

interrupt->name, interrupt);

Quoted 7 lines
sound/soc/codecs/rt766-sdca.c new file +7 -0
+                             dev_dbg(dev, "Freeing IRQ %d\n", irq);
+                     }
+             }
+     }
+
+     return 0;
+}
Quoted 7 lines

sound/soc/codecs/rt766-sdca.c line 315

Quoted 4 lines
sound/soc/codecs/rt766-sdca.c new file +4 -0
+static int rt766_sdca_set_fu41_playback_ctl(struct rt766_sdca_priv
+*rt766) {
+     int err;
+     unsigned int ch_01, ch_02;

nit-pick: likely copy/paste from other drivers, but flipping the two lines would be nicer. there are a couple of similar patterns in this driver.

Sure, will fix.

Quoted 22 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+     ch_01 = (rt766->fu41_dapm_mute || rt766->fu41_mixer_l_mute) ?

0x01 : 0x00;

Review

Quoted 4 lines
(quoted diff) +4 -0
+     ch_02 = (rt766->fu41_dapm_mute || rt766->fu41_mixer_r_mute) ?
+ 0x01 : 0x00;
+
+     err = regmap_write(rt766->regmap, RT766_MUTE_REG(UAJ,

USER_FU41, 1), ch_01);

sound/soc/codecs/rt766-sdca.c

Quoted 4 lines
sound/soc/codecs/rt766-sdca.c new file +4 -0
+     if (err < 0)
+             return err;
+
+     err = regmap_write(rt766->regmap, RT766_MUTE_REG(UAJ,

USER_FU41, 2), ch_02);

sound/soc/codecs/rt766-sdca.c line 652

Quoted 9 lines
sound/soc/codecs/rt766-sdca.c new file +8 -0
+     if (err < 0)
+             return err;
+
+     return 0;
+}
 
+static int rt766_sdca_pde47_event(struct snd_soc_dapm_widget *w,
+     struct snd_kcontrol *kcontrol, int event) {
+     struct snd_soc_component *component =
Quoted 6 lines

snd_soc_dapm_to_component(w->dapm);

Quoted 1 line

+ struct rt766_sdca_priv *rt766 =

snd_soc_component_get_drvdata(component);

Quoted 1 line

+ const struct sdca_entity *entity = NULL;

useless init?

Will fix.

Quoted 263 lines

sound/soc/codecs/rt766-sdca.c

Quoted 7 lines
sound/soc/codecs/rt766-sdca.c new file +7 -0
+     unsigned char ps0 = 0x0, ps3 = 0x3;
+     int from_ps, to_ps;
+     int ret;
+
+     switch (event) {
+     case SND_SOC_DAPM_POST_PMU:
+             regmap_write(rt766->regmap, RT766_PDE_REQ_REG(UAJ,

PDE47), ps0);

sound/soc/codecs/rt766-sdca.c

Quoted 5 lines
sound/soc/codecs/rt766-sdca.c new file +5 -0
+             from_ps = ps3;
+             to_ps = ps0;
+             break;
+     case SND_SOC_DAPM_PRE_PMD:
+             regmap_write(rt766->regmap, RT766_PDE_REQ_REG(UAJ,

PDE47), ps3);

sound/soc/codecs/rt766-sdca.c

Quoted 7 lines
sound/soc/codecs/rt766-sdca.c new file +7 -0
+             from_ps = ps0;
+             to_ps = ps3;
+             break;
+     }
+
+     entity = rt766_find_entity_by_label(rt766->uaj_func_data, "PDE 47");
+     ret = sdca_asoc_pde_poll_actual_ps(component->dev,

rt766->regmap,

sound/soc/codecs/rt766-sdca.c

Quoted 4 lines
sound/soc/codecs/rt766-sdca.c new file +4 -0
+                                RT766_FUNC_NUM_UAJ,
+                                RT766_SDCA_ENT_PDE47,
+                                from_ps, to_ps,
+                                entity ? entity->pde.max_delay :

NULL,

Quoted 1 line

+ entity ?

entity->pde.num_max_delay : 0);

Review

Quoted 2 lines
(quoted diff) +2 -0
+     if (ret)
+             dev_err(component->dev, "%s: PDE transition %x -> %x

failed, err=%d\n",

sound/soc/codecs/rt766-sdca.c line 709

Quoted 8 lines
sound/soc/codecs/rt766-sdca.c new file +7 -0
+                     __func__, from_ps, to_ps, ret);
+
+     return ret;
+}
 
+static int rt766_sdca_pde34_event(struct snd_soc_dapm_widget *w,
+     struct snd_kcontrol *kcontrol, int event) {
+     struct snd_soc_component *component =

snd_soc_dapm_to_component(w->dapm);

Quoted 1 line

+ struct rt766_sdca_priv *rt766 =

snd_soc_component_get_drvdata(component);

Quoted 1 line

+ const struct sdca_entity *entity = NULL;

useless init? Same comment for all PDE events.

sound/soc/codecs/rt766-sdca.c

Quoted 7 lines
sound/soc/codecs/rt766-sdca.c new file +7 -0
+     unsigned char ps0 = 0x0, ps3 = 0x3;
+     int from_ps, to_ps;
+     int ret;
+
+     switch (event) {
+     case SND_SOC_DAPM_POST_PMU:
+             regmap_write(rt766->regmap, RT766_PDE_REQ_REG(UAJ,

PDE34), ps0);

sound/soc/codecs/rt766-sdca.c

Quoted 5 lines
sound/soc/codecs/rt766-sdca.c new file +5 -0
+             from_ps = ps3;
+             to_ps = ps0;
+             break;
+     case SND_SOC_DAPM_PRE_PMD:
+             regmap_write(rt766->regmap, RT766_PDE_REQ_REG(UAJ,

PDE34), ps3);

sound/soc/codecs/rt766-sdca.c

Quoted 7 lines
sound/soc/codecs/rt766-sdca.c new file +7 -0
+             from_ps = ps0;
+             to_ps = ps3;
+             break;
+     }
+
+     entity = rt766_find_entity_by_label(rt766->uaj_func_data, "PDE 34");
+     ret = sdca_asoc_pde_poll_actual_ps(component->dev,

rt766->regmap,

sound/soc/codecs/rt766-sdca.c

Quoted 4 lines
sound/soc/codecs/rt766-sdca.c new file +4 -0
+                                RT766_FUNC_NUM_UAJ,
+                                RT766_SDCA_ENT_PDE34,
+                                from_ps, to_ps,
+                                entity ? entity->pde.max_delay :

NULL,

Quoted 1 line

+ entity ?

entity->pde.num_max_delay : 0);

Review

Quoted 2 lines
(quoted diff) +2 -0
+     if (ret)
+             dev_err(component->dev, "%s: PDE transition %x -> %x

failed, err=%d\n",

sound/soc/codecs/rt766-sdca.c

Quoted 8 lines
sound/soc/codecs/rt766-sdca.c new file +6 -0
+                     __func__, from_ps, to_ps, ret);
+
+     return ret;
+}
 
 
+static int rt766_sdca_probe(struct snd_soc_component *component) {
+     struct rt766_sdca_priv *rt766 =

snd_soc_component_get_drvdata(component);

Quoted 4 lines
sound/soc/codecs/rt766-sdca.c new file +4 -0
+     struct device *dev = &rt766->slave->dev;
+     int ret;
+
+     rt766->component = component;

sound/soc/codecs/rt766-sdca.c

Quoted 10 lines
sound/soc/codecs/rt766-sdca.c new file +10 -0
+
+     ret = pm_runtime_resume(component->dev);
+     if (ret < 0 && ret != -EACCES)
+             return ret;
+
+     if (rt766->uaj_func_data) {
+             dev_dbg(dev, "%s : irq %d\n", __func__,
+ rt766->slave->irq);
+
+             rt766->irq_info = sdca_irq_allocate(dev, rt766->regmap,

rt766->slave->irq);

sound/soc/codecs/rt766-sdca.c line 963

Quoted 5 lines
sound/soc/codecs/rt766-sdca.c new file +5 -0
+             if (IS_ERR(rt766->irq_info))
+                     return PTR_ERR(rt766->irq_info);
+
+             ret = rt766_sdca_irq_ctl(rt766, rt766->uaj_func_data,
+                     component, rt766->irq_info, true);

same question on IRQ allocate/request for Charles.

Review

Quoted 2 lines
(quoted diff) +2 -0
+             if (ret < 0) {
+                     dev_err(dev, "Failed to request UAJ SDCA IRQ:

%d\n", ret);

sound/soc/codecs/rt766-sdca.c

Quoted 5 lines
sound/soc/codecs/rt766-sdca.c new file +5 -0
+                     return ret;
+             }
+
+             if (rt766->hid_func_data) {
+                     ret = rt766_sdca_irq_ctl(rt766,

rt766->hid_func_data,

sound/soc/codecs/rt766-sdca.c

Quoted 3 lines
sound/soc/codecs/rt766-sdca.c new file +3 -0
+                             component, rt766->irq_info, true);
+                     if (ret < 0) {
+                             dev_err(dev, "Failed to request HID

SDCA IRQ: %d\n", ret);

sound/soc/codecs/rt766-sdca-sdw.c

Quoted 9 lines
sound/soc/codecs/rt766-sdca-sdw.c new file +8 -0
+                             return ret;
+                     }
+             }
+     }
+
+     return 0;
+}
 
+static unsigned int rt766_find_dt_rates(struct device *dev, struct

sdca_function_data *function,

Review

Quoted 3 lines
(quoted diff) +3 -0
+                                                     const char
+*label) {
+     struct sdca_entity *entity = NULL;

useless init?

sound/soc/codecs/rt766-sdca.c

Quoted 10 lines
sound/soc/codecs/rt766-sdca.c new file +10 -0
+     unsigned int rates;
+     int i, ret;
+
+     for (i = 0; i < function->num_entities; i++) {
+             entity = &function->entities[i];
+
+             if (strcmp(entity->label, label))
+                     continue;
+
+             /* Can't check earlier as only terminals have an iot

member. */

sound/soc/codecs/rt766-sdca.c

Quoted 6 lines
sound/soc/codecs/rt766-sdca.c new file +6 -0
+             if (!entity->iot.is_dataport)
+                     continue;
+
+             ret = rt766_parse_rates(dev, function, entity, &rates);
+             if (ret < 0) {
+                     dev_dbg(dev, "%s: failed to parse rates for entity

%s\n",

sound/soc/codecs/rt766-sdca.c

Quoted 5 lines
sound/soc/codecs/rt766-sdca.c new file +5 -0
+                             __func__, entity->label);
+                     return 0;
+             }
+
+             dev_dbg(dev, "%s: %s supports rates 0x%08x\n", __func__,

entity->label, rates);

Quoted 30 lines
sound/soc/codecs/rt766-sdca.c new file +30 -0
+     }
+
+     return rates;
+}
+
+int rt766_sdca_init(struct device *dev, struct regmap *regmap, struct
+sdw_slave *slave) {
+     struct sdca_function_data *func_data_ptr;
+     struct rt766_sdca_priv *rt766;
+     unsigned int rates;
+     int ret;
+     int i;
+
+     rt766 = devm_kzalloc(dev, sizeof(*rt766), GFP_KERNEL);
+     if (!rt766)
+             return -ENOMEM;
+
+     dev_set_drvdata(dev, rt766);
+     rt766->slave = slave;
+     rt766->regmap = regmap;
+
+     regcache_cache_only(rt766->regmap, true);
+
+     mutex_init(&rt766->disable_irq_lock);
+
+     /*
+      * Mark hw_init to false
+      * HW init will be performed when device reports present
+      */
+     rt766->hw_init = false;

sound/soc/codecs/rt766-sdca.c

Quoted 10 lines
sound/soc/codecs/rt766-sdca.c new file +10 -0
+     rt766->first_hw_init = false;
+     rt766->fu41_dapm_mute = true;
+     rt766->fu41_mixer_l_mute = rt766->fu41_mixer_r_mute = false;
+     rt766->fu36_dapm_mute = true;
+     rt766->fu36_mixer_l_mute = rt766->fu36_mixer_r_mute = true;
+     rt766->fu21_dapm_mute = true;
+     rt766->fu21_mixer_l_mute = rt766->fu21_mixer_r_mute = false;
+     rt766->fu113_dapm_mute = true;
+     rt766->fu113_mixer_mute[0] = rt766->fu113_mixer_mute[1] =
+             rt766->fu113_mixer_mute[2] =

rt766->fu113_mixer_mute[3]

sound/soc/codecs/rt766-sdca.c

Quoted 4 lines

+ = true;

sound/soc/codecs/rt766-sdca.c new file +3 -0
+
+     /* get SDCA function data */
+     dev_dbg(dev, "SDCA functions found: %d",

slave->sdca_data.num_functions);

Review

Quoted 2 lines
(quoted diff) +2 -0
+     for (i = 0; i < slave->sdca_data.num_functions; i++) {
+             func_data_ptr = devm_kzalloc(dev, sizeof(*func_data_ptr),

GFP_KERNEL);

Quoted 9 lines
sound/soc/codecs/rt766-sdca.c new file +9 -0
+             if (!func_data_ptr)
+                     return dev_err_probe(dev, -ENOMEM, "failed to
+ allocate function data");
+
+             func_data_ptr->desc = &slave->sdca_data.function[i];
+             ret = sdca_parse_function(dev, slave, func_data_ptr);
+             if (ret) {
+                     devm_kfree(dev, func_data_ptr);
+                     return ret;

sound/soc/codecs/rt766-sdca.c

Quoted 10 lines
sound/soc/codecs/rt766-sdca.c new file +10 -0
+             }
+             dev_dbg(dev, "Function type=%d, num_entities=%d",
+                     slave->sdca_data.function[i].type,
+ func_data_ptr->num_entities);
+
+             switch (slave->sdca_data.function[i].type) {
+             case SDCA_FUNCTION_TYPE_UAJ:
+                     rt766->uaj_func_data = func_data_ptr;
+                     /*
+                      * Some machines may only support a subset of

the sample rates supported by the codec.

Quoted 1 line

+ * Therefore, we need to parse the supported

sample rates from the DisCo table and

Quoted 1 line

+ * configure them in the DAI. If the DisCo table

does not provide sample rate information,

Quoted 1 line

+ * we will fall back to the default supported rates

defined in the codec driver.

Review

Quoted 2 lines
(quoted diff) +2 -0
+                      */
+                     rates = rt766_find_dt_rates(dev, func_data_ptr,

"IT 41");

Review

Quoted 5 lines
(quoted diff) +5 -0
+                     if (rates)
+                             rt766_sdca_dai[0].playback.rates =
+ rates;
+
+                     rates = rt766_find_dt_rates(dev, func_data_ptr,

"OT 36");

Review

Quoted 2 lines
(quoted diff) +2 -0
+                     if (rates)
+                             rt766_sdca_dai[0].capture.rates =

rates;

sound/soc/codecs/rt766-sdca.c

Quoted 4 lines
sound/soc/codecs/rt766-sdca.c new file +4 -0
+                     break;
+             case SDCA_FUNCTION_TYPE_SMART_AMP:
+                     rt766->sa_func_data = func_data_ptr;
+                     rates = rt766_find_dt_rates(dev, func_data_ptr,

"IT 21");

Review

Quoted 2 lines
(quoted diff) +2 -0
+                     if (rates)
+                             rt766_sdca_dai[1].playback.rates =

rates;

sound/soc/codecs/rt766-sdca.c

Quoted 4 lines
sound/soc/codecs/rt766-sdca.c new file +4 -0
+                     break;
+             case SDCA_FUNCTION_TYPE_SMART_MIC:
+                     rt766->sm_func_data = func_data_ptr;
+                     rates = rt766_find_dt_rates(dev, func_data_ptr,
Quoted 7 lines

"OT 113");

Review

Quoted 2 lines
(quoted diff) +2 -0
+                     if (rates)
+                             rt766_sdca_dai[2].capture.rates =

rates;

are the sdca_dai[index] related to the function? Wondering if we can use e.g sdca_dai[MIC] instead?

Sure, will use the enumeration value.

Quoted 31 lines

sound/soc/codecs/rt766-sdca.c

Quoted 6 lines
sound/soc/codecs/rt766-sdca.c new file +6 -0
+                     break;
+             case SDCA_FUNCTION_TYPE_HID:
+                     rt766->hid_func_data = func_data_ptr;
+                     break;
+             default:
+                     dev_dbg(dev, "Unexpected SDCA function type

found: %d",

sound/soc/codecs/rt766-sdca.c

Quoted 6 lines
sound/soc/codecs/rt766-sdca.c new file +6 -0
+                             slave->sdca_data.function[i].type);
+             }
+     }
+
+     ret =  devm_snd_soc_register_component(dev,
+                     &soc_sdca_dev_rt766, rt766_sdca_dai,

ARRAY_SIZE(rt766_sdca_dai));

Quoted 16 lines
sound/soc/codecs/rt766-sdca.c new file +16 -0
+     if (ret < 0)
+             return ret;
+
+     /* set autosuspend parameters */
+     pm_runtime_set_autosuspend_delay(dev, 3000);
+     pm_runtime_use_autosuspend(dev);
+
+     /* make sure the device does not suspend immediately */
+     pm_runtime_mark_last_busy(dev);
+
+     pm_runtime_enable(dev);
+
+     dev_dbg(dev, "%s\n", __func__);
+
+     return 0;
+}
#3 lore raw
In reply to [email protected]
Quoted 11 lines · On Mon, Jul 20, 2026 at 05:06:13PM +0800, [email protected] wrote:

From: Shuming Fan <[email protected]>

This patch adds the initial SDCA multi-function codec driver for the RT766 and RT767.

---

sound/soc/codecs/rt766-sdca.c new file +5 -0
+static struct sdca_entity *rt766_find_entity_by_label(struct sdca_function_data *func,
+	const char *label)
+{
+	struct sdca_entity *entity = NULL;
+	int idx;

sound/soc/codecs/rt766-sdca.c line 46

Quoted 10 lines
sound/soc/codecs/rt766-sdca.c new file +10 -0
+
+	for (idx = 0; idx < func->num_entities; idx++) {
+		entity = &func->entities[idx];
+
+		if (!strcmp(entity->label, label))
+			return entity;
+	}
+
+	return NULL;
+}

Probably better to export find_sdca_entity_by_label() if you need this from the driver.

Quoted 48 lines
sound/soc/codecs/rt766-sdca.c new file +48 -0
+static int rt766_sdca_irq_ctl(struct rt766_sdca_priv *rt766,
+							  struct sdca_function_data *function,
+							  struct snd_soc_component *component,
+							  struct sdca_interrupt_info *info,
+							  bool enabled)
+{
+	struct device *dev = &rt766->slave->dev;
+	struct sdca_interrupt *interrupt;
+	struct sdca_control *control;
+	struct sdca_entity *entity;
+	irq_handler_t handler;
+	int i, j, irq, ret;
+
+	for (i = 0; i < function->num_entities; i++) {
+		entity = &function->entities[i];
+
+		for (j = 0; j < entity->num_controls; j++) {
+			control = &entity->controls[j];
+			irq = control->interrupt_position;
+
+			switch (SDCA_CTL_TYPE(entity->type, control->sel)) {
+			case SDCA_CTL_TYPE_S(GE, DETECTED_MODE):
+				handler = rt766_sdca_irq_jd_handler;
+				break;
+			case SDCA_CTL_TYPE_S(HIDE, HIDTX_CURRENTOWNER):
+				handler = rt766_sdca_irq_btn_handler;
+				break;
+			default:
+				continue;
+			}
+
+			interrupt = &info->irqs[irq];
+
+			if (enabled) {
+				ret = sdca_irq_data_populate(dev, rt766->regmap, component,
+								function, entity, control,
+								interrupt);
+				if (ret)
+					return ret;
+
+				interrupt->priv = rt766;
+				ret = sdca_irq_request(dev, info, irq, interrupt->name,
+								handler, interrupt);
+				if (ret) {
+					dev_err(dev, "failed to request irq %s: %d\n",
+						interrupt->name, ret);
+					return ret;
+				}

sound/soc/codecs/rt766-sdca.c line 266

Quoted 10 lines
sound/soc/codecs/rt766-sdca.c new file +10 -0
+				dev_dbg(dev, "Requesting IRQ %d InterruptName=%s\n", irq, interrupt->name);
+			} else {
+				sdca_irq_free(dev, info, irq, interrupt->name, interrupt);
+				dev_dbg(dev, "Freeing IRQ %d\n", irq);
+			}
+		}
+	}
+
+	return 0;
+}

Hmm... yeah this is moving in a slightly different direction from where I was going with the core code. Let me think about that and update tomorrow.

Quoted 23 lines
sound/soc/codecs/rt766-sdca.c new file +23 -0
+static int rt766_sdca_pde47_event(struct snd_soc_dapm_widget *w,
+	struct snd_kcontrol *kcontrol, int event)
+{
+	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
+	struct rt766_sdca_priv *rt766 = snd_soc_component_get_drvdata(component);
+	const struct sdca_entity *entity = NULL;
+	unsigned char ps0 = 0x0, ps3 = 0x3;
+	int from_ps, to_ps;
+	int ret;
+
+	switch (event) {
+	case SND_SOC_DAPM_POST_PMU:
+		regmap_write(rt766->regmap, RT766_PDE_REQ_REG(UAJ, PDE47), ps0);
+		from_ps = ps3;
+		to_ps = ps0;
+		break;
+	case SND_SOC_DAPM_PRE_PMD:
+		regmap_write(rt766->regmap, RT766_PDE_REQ_REG(UAJ, PDE47), ps3);
+		from_ps = ps0;
+		to_ps = ps3;
+		break;
+	}
+

sound/soc/codecs/rt766-sdca.c line 644

Quoted 10 lines
sound/soc/codecs/rt766-sdca.c new file +10 -0
+	entity = rt766_find_entity_by_label(rt766->uaj_func_data, "PDE 47");
+	ret = sdca_asoc_pde_poll_actual_ps(component->dev, rt766->regmap,
+				   RT766_FUNC_NUM_UAJ,
+				   RT766_SDCA_ENT_PDE47,
+				   from_ps, to_ps,
+				   entity ? entity->pde.max_delay : NULL,
+				   entity ? entity->pde.num_max_delay : 0);
+	if (ret)
+		dev_err(component->dev, "%s: PDE transition %x -> %x failed, err=%d\n",
+			__func__, from_ps, to_ps, ret);

Does this actually run in cases where the entity doesn't exist? I would be inclined to error out if you didn't find the entity.

Quoted 26 lines
sound/soc/codecs/rt766-sdca.c new file +26 -0
+static int rt766_sdca_pde11_event(struct snd_soc_dapm_widget *w,
+	struct snd_kcontrol *kcontrol, int event)
+{
+	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
+	struct rt766_sdca_priv *rt766 = snd_soc_component_get_drvdata(component);
+	const struct sdca_entity *entity = NULL;
+	unsigned char ps0 = 0x0, ps3 = 0x3;
+	int from_ps, to_ps;
+	int ret;
+
+	switch (event) {
+	case SND_SOC_DAPM_POST_PMU:
+		regmap_write(rt766->regmap, RT766_PDE_REQ_REG(MIC, PDE11), ps0);
+		from_ps = ps3;
+		to_ps = ps0;
+		break;
+	case SND_SOC_DAPM_PRE_PMD:
+		regmap_write(rt766->regmap, RT766_PDE_REQ_REG(MIC, PDE11), ps3);
+		from_ps = ps0;
+		to_ps = ps3;
+		break;
+	}
+
+	entity = rt766_find_entity_by_label(rt766->sm_func_data, "PDE 11");
+	ret = sdca_asoc_pde_poll_actual_ps(component->dev, rt766->regmap,
+				   RT766_FUNC_NUM_MIC,

sound/soc/codecs/rt766-sdca.c line 798

Quoted 10 lines
sound/soc/codecs/rt766-sdca.c new file +10 -0
+				   RT766_SDCA_ENT_PDE11,
+				   from_ps, to_ps,
+				   entity ? entity->pde.max_delay : NULL,
+				   entity ? entity->pde.num_max_delay : 0);
+	if (ret)
+		dev_err(component->dev, "%s: PDE transition %x -> %x failed, err=%d\n",
+			__func__, from_ps, to_ps, ret);
+
+	return ret;
+}

I feel like lot of these put/get and event helpers would benefit from a parameterised helper. You have loads of functions doing the same thing, would it be nicer to have a helper that takes the required registers etc.

Quoted 39 lines
sound/soc/codecs/rt766-sdca.c new file +39 -0
+static int rt766_sdca_pcm_hw_params(struct snd_pcm_substream *substream,
+				struct snd_pcm_hw_params *params,
+				struct snd_soc_dai *dai)
+{
+	struct snd_soc_component *component = dai->component;
+	struct rt766_sdca_priv *rt766 = snd_soc_component_get_drvdata(component);
+	struct sdw_stream_config stream_config;
+	struct sdw_port_config port_config;
+	enum sdw_data_direction direction;
+	struct sdw_stream_runtime *sdw_stream;
+	int retval, port, num_channels;
+	unsigned int sampling_rate;
+
+	dev_dbg(dai->dev, "%s %s id %d", __func__, dai->name, dai->id);
+	sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
+
+	if (!sdw_stream)
+		return -EINVAL;
+
+	if (!rt766->slave)
+		return -EINVAL;
+
+	/* SoundWire specific configuration */
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
+		direction = SDW_DATA_DIR_RX;
+		if (dai->id == RT766_AIF1)
+			port = 3;
+		else if (dai->id == RT766_AIF2)
+			port = 1;
+		else
+			return -EINVAL;
+	} else {
+		direction = SDW_DATA_DIR_TX;
+		if (dai->id == RT766_AIF1)
+			port = 12;
+		else if (dai->id == RT766_AIF3)
+			port = 8;
+		else
+			return -EINVAL;

sound/soc/codecs/rt766-sdca.c line 1065

Quoted 10 lines
sound/soc/codecs/rt766-sdca.c new file +10 -0
+	}
+
+	stream_config.frame_rate = params_rate(params);
+	stream_config.ch_count = params_channels(params);
+	stream_config.bps = snd_pcm_format_width(params_format(params));
+	stream_config.direction = direction;
+
+	num_channels = params_channels(params);
+	port_config.ch_mask = GENMASK(num_channels - 1, 0);
+	port_config.num = port;

Can you use snd_sdw_params_to_config here?

Quoted 48 lines
sound/soc/codecs/rt766-sdca.c new file +48 -0
+static int rt766_parse_rates(struct device *dev,
+				struct sdca_function_data *function,
+				struct sdca_entity *entity,
+				unsigned int *out_rates)
+{
+	struct sdca_control_range *range;
+	unsigned int sample_rate;
+	unsigned int clock_rates = 0;
+	unsigned int rates = 0;
+	int sel, i;
+
+	switch (entity->type) {
+	case SDCA_ENTITY_TYPE_IT:
+		sel = SDCA_CTL_IT_USAGE;
+		break;
+	case SDCA_ENTITY_TYPE_OT:
+		sel = SDCA_CTL_OT_USAGE;
+		break;
+	default:
+		dev_err(dev, "%s: entity type has no usage control\n",
+			entity->label);
+		return -EINVAL;
+	}
+
+	if (entity->iot.clock) {
+		range = sdca_selector_find_range(dev, entity->iot.clock,
+						 SDCA_CTL_CS_SAMPLERATEINDEX,
+						 SDCA_SAMPLERATEINDEX_NCOLS, 0);
+		if (!range)
+			return -EINVAL;
+
+		for (i = 0; i < range->rows; i++) {
+			sample_rate = sdca_range(range, SDCA_SAMPLERATEINDEX_RATE, i);
+			clock_rates |= rate_find_mask(dev, sample_rate);
+		}
+	} else {
+		clock_rates = UINT_MAX;
+	}
+
+	range = sdca_selector_find_range(dev, entity, sel, SDCA_USAGE_NCOLS, 0);
+	if (!range)
+		return -EINVAL;
+
+	for (i = 0; i < range->rows; i++) {
+		sample_rate = sdca_range(range, SDCA_USAGE_SAMPLE_RATE, i);
+		sample_rate = rate_find_mask(dev, sample_rate);
+
+		if (sample_rate & clock_rates)

sound/soc/codecs/rt766-sdca.c line 1276

Quoted 10 lines
sound/soc/codecs/rt766-sdca.c new file +10 -0
+			rates |= sample_rate;
+	}
+
+	*out_rates = rates;
+
+	dev_dbg(dev, "%s: entity %s supports rates 0x%08x, clock_rates=0x%08x\n",
+		__func__, entity->label, rates, clock_rates);
+
+	return 0;
+}

This is mostly just populate_rate_format(), the values end up in the snd_soc_pcm_stream, can we export that and use it here?

Thanks,
Charles

#4 lore raw
Quoted 11 lines
Quoted 1 line

+static struct sdca_entity *rt766_find_entity_by_label(struct

sdca_function_data *func,

Quoted 9 lines
sound/soc/codecs/rt766-sdca.c new file +9 -0
+     const char *label)
+{
+     struct sdca_entity *entity = NULL;
+     int idx;
+
+     for (idx = 0; idx < func->num_entities; idx++) {
+             entity = &func->entities[idx];
+
+             if (!strcmp(entity->label, label))
Quoted 8 lines

sound/soc/codecs/rt766-sdca.c line 46

Quoted 5 lines
sound/soc/codecs/rt766-sdca.c new file +5 -0
+                     return entity;
+     }
+
+     return NULL;
+}

Probably better to export find_sdca_entity_by_label() if you need this from the driver.

Thanks for the review.
I will try to export find_sdca_entity_by_label() and populate_rate_format().

Quoted 66 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+static int rt766_sdca_irq_ctl(struct rt766_sdca_priv *rt766,
+                                                       struct

sdca_function_data *function,

Quoted 1 line

+ struct

snd_soc_component *component,

Quoted 1 line

+ struct

sdca_interrupt_info *info,

Quoted 1 line

+ bool

enabled)

Quoted 6 lines
sound/soc/codecs/rt766-sdca.c new file +6 -0
+{
+     struct device *dev = &rt766->slave->dev;
+     struct sdca_interrupt *interrupt;
+     struct sdca_control *control;
+     struct sdca_entity *entity;
+     irq_handler_t handler;

sound/soc/codecs/rt766-sdca.c

Quoted 10 lines
sound/soc/codecs/rt766-sdca.c new file +10 -0
+     int i, j, irq, ret;
+
+     for (i = 0; i < function->num_entities; i++) {
+             entity = &function->entities[i];
+
+             for (j = 0; j < entity->num_controls; j++) {
+                     control = &entity->controls[j];
+                     irq = control->interrupt_position;
+
+                     switch (SDCA_CTL_TYPE(entity->type,

control->sel)) {

sound/soc/codecs/rt766-sdca.c

Quoted 4 lines
sound/soc/codecs/rt766-sdca.c new file +4 -0
+                     case SDCA_CTL_TYPE_S(GE, DETECTED_MODE):
+                             handler = rt766_sdca_irq_jd_handler;
+                             break;
+                     case SDCA_CTL_TYPE_S(HIDE,

HIDTX_CURRENTOWNER):

sound/soc/codecs/rt766-sdca.c

Quoted 10 lines
sound/soc/codecs/rt766-sdca.c new file +10 -0
+                             handler = rt766_sdca_irq_btn_handler;
+                             break;
+                     default:
+                             continue;
+                     }
+
+                     interrupt = &info->irqs[irq];
+
+                     if (enabled) {
+                             ret = sdca_irq_data_populate(dev,

rt766->regmap, component,

Quoted 1 line

+

function, entity, control,

Quoted 1 line

+

interrupt);

sound/soc/codecs/rt766-sdca.c

Quoted 5 lines
sound/soc/codecs/rt766-sdca.c new file +5 -0
+                             if (ret)
+                                     return ret;
+
+                             interrupt->priv = rt766;
+                             ret = sdca_irq_request(dev, info, irq,

interrupt->name,

Quoted 1 line

+

handler, interrupt);

Review

Quoted 2 lines
(quoted diff) +2 -0
+                             if (ret) {
+                                     dev_err(dev, "failed to

request irq %s: %d\n",

Quoted 1 line

+ interrupt->name,

ret);

sound/soc/codecs/rt766-sdca-sdw.c

Quoted 3 lines
sound/soc/codecs/rt766-sdca-sdw.c new file +3 -0
+                                     return ret;
+                             }
+                             dev_dbg(dev, "Requesting IRQ %d

InterruptName=%s\n", irq, interrupt->name);

Review

Quoted 2 lines
(quoted diff) +2 -0
+                     } else {
+                             sdca_irq_free(dev, info, irq,

interrupt->name, interrupt);

Quoted 10 lines

sound/soc/codecs/rt766-sdca.c line 266

Quoted 7 lines
sound/soc/codecs/rt766-sdca.c new file +7 -0
+                             dev_dbg(dev, "Freeing IRQ %d\n", irq);
+                     }
+             }
+     }
+
+     return 0;
+}

Hmm... yeah this is moving in a slightly different direction from where I was going with the core code. Let me think about that and update tomorrow.

Sorry, I don't quite follow the point.
Do you mean that it will not work with this patch (ASoC: SDCA: Populate IRQ data earlier)?

Quoted 35 lines

sound/soc/codecs/rt766-sdca.c line 652

Quoted 3 lines
sound/soc/codecs/rt766-sdca.c new file +3 -0
+static int rt766_sdca_pde47_event(struct snd_soc_dapm_widget *w,
+     struct snd_kcontrol *kcontrol, int event) {
+     struct snd_soc_component *component =

snd_soc_dapm_to_component(w->dapm);

Quoted 1 line

+ struct rt766_sdca_priv *rt766 =

snd_soc_component_get_drvdata(component);

sound/soc/codecs/rt766-sdca.c

Quoted 8 lines
sound/soc/codecs/rt766-sdca.c new file +8 -0
+     const struct sdca_entity *entity = NULL;
+     unsigned char ps0 = 0x0, ps3 = 0x3;
+     int from_ps, to_ps;
+     int ret;
+
+     switch (event) {
+     case SND_SOC_DAPM_POST_PMU:
+             regmap_write(rt766->regmap, RT766_PDE_REQ_REG(UAJ,

PDE47), ps0);

sound/soc/codecs/rt766-sdca.c

Quoted 5 lines
sound/soc/codecs/rt766-sdca.c new file +5 -0
+             from_ps = ps3;
+             to_ps = ps0;
+             break;
+     case SND_SOC_DAPM_PRE_PMD:
+             regmap_write(rt766->regmap, RT766_PDE_REQ_REG(UAJ,

PDE47), ps3);

sound/soc/codecs/rt766-sdca.c

Quoted 7 lines
sound/soc/codecs/rt766-sdca.c new file +7 -0
+             from_ps = ps0;
+             to_ps = ps3;
+             break;
+     }
+
+     entity = rt766_find_entity_by_label(rt766->uaj_func_data, "PDE 47");
+     ret = sdca_asoc_pde_poll_actual_ps(component->dev,

rt766->regmap,

sound/soc/codecs/rt766-sdca.c

Quoted 4 lines
sound/soc/codecs/rt766-sdca.c new file +4 -0
+                                RT766_FUNC_NUM_UAJ,
+                                RT766_SDCA_ENT_PDE47,
+                                from_ps, to_ps,
+                                entity ? entity->pde.max_delay :

NULL,

Quoted 1 line

+ entity ?

Quoted 8 lines

entity->pde.num_max_delay : 0);

Review

Quoted 2 lines
(quoted diff) +2 -0
+     if (ret)
+             dev_err(component->dev, "%s: PDE transition %x -> %x

failed, err=%d\n",

Quoted 1 line

+ __func__, from_ps, to_ps, ret);

Does this actually run in cases where the entity doesn't exist? I would be inclined to error out if you didn't find the entity.

Ok, will fix

Quoted 39 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+static int rt766_sdca_pde11_event(struct snd_soc_dapm_widget *w,
+     struct snd_kcontrol *kcontrol, int event) {
+     struct snd_soc_component *component =

snd_soc_dapm_to_component(w->dapm);

Quoted 1 line

+ struct rt766_sdca_priv *rt766 =

snd_soc_component_get_drvdata(component);

sound/soc/codecs/rt766-sdca.c

Quoted 8 lines
sound/soc/codecs/rt766-sdca.c new file +8 -0
+     const struct sdca_entity *entity = NULL;
+     unsigned char ps0 = 0x0, ps3 = 0x3;
+     int from_ps, to_ps;
+     int ret;
+
+     switch (event) {
+     case SND_SOC_DAPM_POST_PMU:
+             regmap_write(rt766->regmap, RT766_PDE_REQ_REG(MIC,

PDE11), ps0);

sound/soc/codecs/rt766-sdca.c

Quoted 5 lines
sound/soc/codecs/rt766-sdca.c new file +5 -0
+             from_ps = ps3;
+             to_ps = ps0;
+             break;
+     case SND_SOC_DAPM_PRE_PMD:
+             regmap_write(rt766->regmap, RT766_PDE_REQ_REG(MIC,

PDE11), ps3);

sound/soc/codecs/rt766-sdca.c

Quoted 7 lines
sound/soc/codecs/rt766-sdca.c new file +7 -0
+             from_ps = ps0;
+             to_ps = ps3;
+             break;
+     }
+
+     entity = rt766_find_entity_by_label(rt766->sm_func_data, "PDE 11");
+     ret = sdca_asoc_pde_poll_actual_ps(component->dev,

rt766->regmap,

sound/soc/codecs/rt766-sdca.c

Quoted 4 lines
sound/soc/codecs/rt766-sdca.c new file +4 -0
+                                RT766_FUNC_NUM_MIC,
+                                RT766_SDCA_ENT_PDE11,
+                                from_ps, to_ps,
+                                entity ? entity->pde.max_delay :

NULL,

Quoted 1 line

+ entity ?

entity->pde.num_max_delay : 0);

Review

Quoted 2 lines
(quoted diff) +2 -0
+     if (ret)
+             dev_err(component->dev, "%s: PDE transition %x -> %x

failed, err=%d\n",

Quoted 8 lines

sound/soc/codecs/rt766-sdca.c line 647

Quoted 4 lines
sound/soc/codecs/rt766-sdca.c new file +4 -0
+                     __func__, from_ps, to_ps, ret);
+
+     return ret;
+}

I feel like lot of these put/get and event helpers would benefit from a parameterised helper. You have loads of functions doing the same thing, would it be nicer to have a helper that takes the required registers etc.

Sure, will create helper functions for put/get and event.

Quoted 46 lines
Quoted 1 line

+static int rt766_sdca_pcm_hw_params(struct snd_pcm_substream

*substream,

sound/soc/codecs/rt766-sdca.c

Quoted 4 lines
sound/soc/codecs/rt766-sdca.c new file +4 -0
+                             struct snd_pcm_hw_params *params,
+                             struct snd_soc_dai *dai) {
+     struct snd_soc_component *component = dai->component;
+     struct rt766_sdca_priv *rt766 =

snd_soc_component_get_drvdata(component);

Quoted 28 lines
sound/soc/codecs/rt766-sdca.c new file +28 -0
+     struct sdw_stream_config stream_config;
+     struct sdw_port_config port_config;
+     enum sdw_data_direction direction;
+     struct sdw_stream_runtime *sdw_stream;
+     int retval, port, num_channels;
+     unsigned int sampling_rate;
+
+     dev_dbg(dai->dev, "%s %s id %d", __func__, dai->name, dai->id);
+     sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
+
+     if (!sdw_stream)
+             return -EINVAL;
+
+     if (!rt766->slave)
+             return -EINVAL;
+
+     /* SoundWire specific configuration */
+     if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
+             direction = SDW_DATA_DIR_RX;
+             if (dai->id == RT766_AIF1)
+                     port = 3;
+             else if (dai->id == RT766_AIF2)
+                     port = 1;
+             else
+                     return -EINVAL;
+     } else {
+             direction = SDW_DATA_DIR_TX;
+             if (dai->id == RT766_AIF1)

sound/soc/codecs/rt766-sdca.c

Quoted 10 lines
sound/soc/codecs/rt766-sdca.c new file +10 -0
+                     port = 12;
+             else if (dai->id == RT766_AIF3)
+                     port = 8;
+             else
+                     return -EINVAL;
+     }
+
+     stream_config.frame_rate = params_rate(params);
+     stream_config.ch_count = params_channels(params);
+     stream_config.bps =

snd_pcm_format_width(params_format(params));

Quoted 7 lines

sound/soc/codecs/rt766-sdca.c line 1065

Quoted 5 lines
sound/soc/codecs/rt766-sdca.c new file +5 -0
+     stream_config.direction = direction;
+
+     num_channels = params_channels(params);
+     port_config.ch_mask = GENMASK(num_channels - 1, 0);
+     port_config.num = port;

Can you use snd_sdw_params_to_config here?

OK, will fix

Quoted 60 lines
Quoted 16 lines
sound/soc/codecs/rt766-sdca.c new file +16 -0
+static int rt766_parse_rates(struct device *dev,
+                             struct sdca_function_data *function,
+                             struct sdca_entity *entity,
+                             unsigned int *out_rates) {
+     struct sdca_control_range *range;
+     unsigned int sample_rate;
+     unsigned int clock_rates = 0;
+     unsigned int rates = 0;
+     int sel, i;
+
+     switch (entity->type) {
+     case SDCA_ENTITY_TYPE_IT:
+             sel = SDCA_CTL_IT_USAGE;
+             break;
+     case SDCA_ENTITY_TYPE_OT:
+             sel = SDCA_CTL_OT_USAGE;

sound/soc/codecs/rt766-sdca.c line 1249

Quoted 10 lines
sound/soc/codecs/rt766-sdca.c new file +10 -0
+             break;
+     default:
+             dev_err(dev, "%s: entity type has no usage control\n",
+                     entity->label);
+             return -EINVAL;
+     }
+
+     if (entity->iot.clock) {
+             range = sdca_selector_find_range(dev, entity->iot.clock,
+

SDCA_CTL_CS_SAMPLERATEINDEX,

Quoted 1 line

+

SDCA_SAMPLERATEINDEX_NCOLS, 0);

sound/soc/codecs/rt766-sdca.c

Quoted 5 lines
sound/soc/codecs/rt766-sdca.c new file +5 -0
+             if (!range)
+                     return -EINVAL;
+
+             for (i = 0; i < range->rows; i++) {
+                     sample_rate = sdca_range(range,

SDCA_SAMPLERATEINDEX_RATE, i);

Quoted 1 line

+ clock_rates |= rate_find_mask(dev,

sample_rate);

sound/soc/codecs/rt766-sdca.c

Quoted 6 lines
sound/soc/codecs/rt766-sdca.c new file +6 -0
+             }
+     } else {
+             clock_rates = UINT_MAX;
+     }
+
+     range = sdca_selector_find_range(dev, entity, sel,

SDCA_USAGE_NCOLS, 0);

sound/soc/codecs/rt766-sdca.c

Quoted 5 lines
sound/soc/codecs/rt766-sdca.c new file +5 -0
+     if (!range)
+             return -EINVAL;
+
+     for (i = 0; i < range->rows; i++) {
+             sample_rate = sdca_range(range,

SDCA_USAGE_SAMPLE_RATE, i);

sound/soc/codecs/rt766-sdca.c

Quoted 9 lines
sound/soc/codecs/rt766-sdca.c new file +9 -0
+             sample_rate = rate_find_mask(dev, sample_rate);
+
+             if (sample_rate & clock_rates)
+                     rates |= sample_rate;
+     }
+
+     *out_rates = rates;
+
+     dev_dbg(dev, "%s: entity %s supports rates 0x%08x,

clock_rates=0x%08x\n",

Quoted 7 lines

sound/soc/codecs/rt766-sdca.c line 1276

Quoted 4 lines
sound/soc/codecs/rt766-sdca.c new file +4 -0
+             __func__, entity->label, rates, clock_rates);
+
+     return 0;
+}

This is mostly just populate_rate_format(), the values end up in the snd_soc_pcm_stream, can we export that and use it here?

Sure, will try to export populate_rate_format() and use it here.

#5 lore raw
Quoted 31 lines · Shuming [范書銘]
Quoted 20 lines

sound/soc/codecs/rt766-sdca.c

Quoted 5 lines
sound/soc/codecs/rt766-sdca.c new file +5 -0
+                             if (ret)
+                                     return ret;
+
+                             interrupt->priv = rt766;
+                             ret = sdca_irq_request(dev, info, irq,

interrupt->name,

Quoted 1 line

+

handler, interrupt);

Review

Quoted 2 lines
(quoted diff) +2 -0
+                             if (ret) {
+                                     dev_err(dev, "failed to

request irq %s: %d\n",

Quoted 1 line

+ interrupt->name,

ret);

sound/soc/codecs/rt766-sdca-sdw.c

Quoted 3 lines
sound/soc/codecs/rt766-sdca-sdw.c new file +3 -0
+                                     return ret;
+                             }
+                             dev_dbg(dev, "Requesting IRQ %d

InterruptName=%s\n", irq, interrupt->name);

Review

Quoted 2 lines
(quoted diff) +2 -0
+                     } else {
+                             sdca_irq_free(dev, info, irq,

interrupt->name, interrupt);

Quoted 10 lines

sound/soc/codecs/rt766-sdca.c line 266

Quoted 7 lines
sound/soc/codecs/rt766-sdca.c new file +7 -0
+                             dev_dbg(dev, "Freeing IRQ %d\n", irq);
+                     }
+             }
+     }
+
+     return 0;
+}

Hmm... yeah this is moving in a slightly different direction from where I was going with the core code. Let me think about that and update tomorrow.

Quoted 2 lines

Sorry, I don't quite follow the point.
Do you mean that it will not work with this patch (ASoC: SDCA: Populate IRQ data earlier)?

Mostly, the issue here is you are calling things at different times to the class driver, so the devm allocations will probably leak but I think I will update my patch chain to drop the devm allocations to keep things flexible. There are some minor updates required to this patch for my core changes but that is the only one that doesn't work I think.

I will send out a new spin later today and CC you on it.

Thanks,
Charles

#6 lore raw
In reply to Shuming [范書銘]
Quoted 13 lines · Shuming [范書銘]
Quoted 5 lines

sound/soc/codecs/rt766-sdca.c

Quoted 4 lines
sound/soc/codecs/rt766-sdca.c new file +4 -0
+
+     *out_rates = rates;
+
+     dev_dbg(dev, "%s: entity %s supports rates 0x%08x,

clock_rates=0x%08x\n",

Quoted 7 lines

sound/soc/codecs/rt766-sdca.c line 1276

Quoted 4 lines
sound/soc/codecs/rt766-sdca.c new file +4 -0
+             __func__, entity->label, rates, clock_rates);
+
+     return 0;
+}

This is mostly just populate_rate_format(), the values end up in the snd_soc_pcm_stream, can we export that and use it here?

Quoted 1 line

Sure, will try to export populate_rate_format() and use it here.

Looking a bit more is there really anything that would stop you just using sdca_asoc_populate_dais(), which is already exported? You would probably want to fixup the channels_max at the end and the naming of the DAIs would end up being slightly more SDCA, but those don't seem like huge problems.

Thanks,
Charles

#7 lore raw
Quoted 14 lines · Shuming [范書銘]
Quoted 13 lines
Quoted 12 lines

sound/soc/codecs/rt766-sdca.c

Quoted 4 lines
sound/soc/codecs/rt766-sdca.c new file +4 -0
+
+     *out_rates = rates;
+
+     dev_dbg(dev, "%s: entity %s supports rates 0x%08x,

clock_rates=0x%08x\n",

sound/soc/codecs/rt766-sdca.c line 1276

Quoted 4 lines
sound/soc/codecs/rt766-sdca.c new file +4 -0
+             __func__, entity->label, rates, clock_rates);
+
+     return 0;
+}

This is mostly just populate_rate_format(), the values end up in the snd_soc_pcm_stream, can we export that and use it here?

Quoted 7 lines
Quoted 1 line

Sure, will try to export populate_rate_format() and use it here.

Looking a bit more is there really anything that would stop you just using sdca_asoc_populate_dais(), which is already exported?
You would probably want to fixup the channels_max at the end and the naming of the DAIs would end up being slightly more SDCA, but those don't seem like huge problems.

Thanks for the suggestion.
That's fine, but I'd still like to export populate_rate_format() for this use case.

By the way, I'll wait until your "ASoC: SDCA: Rename sdca_irq_allocate() to include devm" patch is merged before sending v2 of the RT766 codec driver.

Quoted 2 lines

Thanks,
Charles

#8 lore raw
Quoted 9 lines · Shuming [范書銘]
Quoted 6 lines · Shuming [范書銘]

Looking a bit more is there really anything that would stop you just using sdca_asoc_populate_dais(), which is already exported?
You would probably want to fixup the channels_max at the end and the naming of the DAIs would end up being slightly more SDCA, but those don't seem like huge problems.

Thanks for the suggestion.
That's fine, but I'd still like to export populate_rate_format() for this use case.

Cool cool, up to you guys.

Quoted 3 lines

By the way, I'll wait until your "ASoC: SDCA: Rename sdca_irq_allocate() to include devm" patch is merged before sending v2 of the RT766 codec driver.

Thanks that will simplify things.

Thanks,
Charles