Skip to contentSkip to conversation

ASoc: tas2783: Add tas2783 codec driver

PATCH Superseded Shenghao Ding 3 participants 7 messages Mar 5, 2024 – Mar 18, 2024

Subsystems (3)

v11 of 12 Superseded Latest is v12 →

lore
Share & tools
lore

Shenghao Ding

author maintainer
#0 lore raw

PATCH v11 1/1 ASoc: tas2783: Add tas2783 codec driver

The tas2783 is a smart audio amplifier with integrated MIPI SoundWire interface (Version 1.2.1 compliant), I2C, and I2S/TDM interfaces designed for portable applications. An on-chip DSP supports Texas Instruments SmartAmp speaker protection algorithm. The integrated speaker voltage and current sense provides for real-time monitoring of loudspeakers.

The ASoC component provides the majority of the functionality of the device, all the audio functions.

---
Change in v11:
- simplify tasdevice_set_sdw_stream
- fixed some Linux coding style
- fixed the spelling mistakes
- Select left/right channel based on unique id
- a longer description has been added
- remove unused crc8 in KCONFIG
- Invert of RE here means reciprocal of RE
- correct indentation
- correct the wrong pow calibrated register
- correct comment on RESET register
- fixed usually lines don't end with an open parenthesis
- fixed tasdevice_sdw_shutdown
- use defines for function, entity, etc. for readable
- remove unnecessary codec_lock in component_probe()
- remove struct sdw_stream_data
- remove unused macro definitions
- use BIT(2) and BIT(1) instead of 0x2 and 0x1
- implement pm_runtime
- reg_start is u8, reg_start + i only adds i byte, it should be reg_start + i * 4, because the calibration values
- fixed total_sz in tas2783_calibration
- add a define for TASDEVICE_REG(0, 0, 0x0a)
- rework macro style in header file
- Add select CRC32
- rewrite the mute and the return value handling
- Add comments on bin file
- Add link_id into the fw name to distinguish the same sdw id on different sdw id.
- remove unnecessary mutex_lock
- use set_active() in tasdevice_io_init and move all other parts in the probe.
- move pm_runtime_get_noresume to the probe
- fix the return value of putvol function
- add put_noidle in tasdevice_sdw_remove
- remove unused header file(of.h)
- correct the style of the multi-line comment
- Set the readable registers in tas2783_readable_register for debugfs
- Use new PM macros
- Use default put and get inline SOC_SINGLE_RANGE_TLV and SOC_SINGLE_SX_TLV
- Use respective %pt to log the calibrated time
- Remove unnecessary struct tm
- Add trailing comma tasdevice_dapm_widgets and tasdevice_audio_map
- Rewrite the return value handling in tasdevice_sdw_probe
- use dev_num instead of id.unique_id
- rework the log
- Add code to check the size of calibrated data in UEFI
- Add description of Layout of calibrated Data in UEFI
- switch back link_id and unique_id to identify the devcie
- unique_id offset as the device index of the calibrated data
- update calibrated data layout stored in UEFI, add link_id and unique_id into the calibrated data in case of tas2783 sitting in different SDQW buses.
- correct the wrong unique_id
- set the unique_id as hex style in dsp bin file name

---
 sound/soc/codecs/Kconfig       |  16 +
 sound/soc/codecs/Makefile      |   2 +
 sound/soc/codecs/tas2783-sdw.c | 932 +++++++++++++++++++++++++++++++++
 sound/soc/codecs/tas2783.h     | 106 ++++
 4 files changed, 1056 insertions(+)
 create mode 100644 sound/soc/codecs/tas2783-sdw.c
 create mode 100644 sound/soc/codecs/tas2783.h
sound/soc/codecs/Kconfig +16 -0
index 59f9742e9ff4..6ba3fd0c0811 100644
@@ -247,6 +247,7 @@ config SND_SOC_ALL_CODECS
 	imply SND_SOC_TAS2781_COMLIB
 	imply SND_SOC_TAS2781_FMWLIB
 	imply SND_SOC_TAS2781_I2C
+	imply SND_SOC_TAS2783
 	imply SND_SOC_TAS5086
 	imply SND_SOC_TAS571X
 	imply SND_SOC_TAS5720
@@ -1847,6 +1848,21 @@ config SND_SOC_TAS2781_I2C
 	  algo coefficient setting, for one, two or even multiple TAS2781
 	  chips.
 
+config SND_SOC_TAS2783
+	tristate "Texas Instruments TAS2783 speaker amplifier (sdw)"
+	depends on SOUNDWIRE
+	depends on EFI
+	select REGMAP
+	select REGMAP_SOUNDWIRE
+	select CRC32
+	select CRC32_SARWATE
+	help
+	  Enable support for Texas Instruments TAS2783 Smart Amplifier
+	  Digital input mono Class-D and DSP-inside audio power amplifiers.
+	  TAS2783 supports the MIPI SoundWire v1.2 compatible interface
+	  for audio and control data. At the end of calibrated data appends
+	  the crc data with crc32_sarwate for error detection.
+
 config SND_SOC_TAS5086
 	tristate "Texas Instruments TAS5086 speaker amplifier"
 	depends on I2C
sound/soc/codecs/Makefile +2 -0
index f53baa2b9565..6afceb126ae3 100644
@@ -284,6 +284,7 @@ snd-soc-tas2770-objs := tas2770.o
 snd-soc-tas2781-comlib-objs := tas2781-comlib.o
 snd-soc-tas2781-fmwlib-objs := tas2781-fmwlib.o
 snd-soc-tas2781-i2c-objs := tas2781-i2c.o
+snd-soc-tas2783-objs := tas2783-sdw.o
 snd-soc-tfa9879-objs := tfa9879.o
 snd-soc-tfa989x-objs := tfa989x.o
 snd-soc-tlv320adc3xxx-objs := tlv320adc3xxx.o
@@ -664,6 +665,7 @@ obj-$(CONFIG_SND_SOC_TAS2780)	+= snd-soc-tas2780.o
 obj-$(CONFIG_SND_SOC_TAS2781_COMLIB)	+= snd-soc-tas2781-comlib.o
 obj-$(CONFIG_SND_SOC_TAS2781_FMWLIB)	+= snd-soc-tas2781-fmwlib.o
 obj-$(CONFIG_SND_SOC_TAS2781_I2C)	+= snd-soc-tas2781-i2c.o
+obj-$(CONFIG_SND_SOC_TAS2783)	+= snd-soc-tas2783.o
 obj-$(CONFIG_SND_SOC_TAS5086)	+= snd-soc-tas5086.o
 obj-$(CONFIG_SND_SOC_TAS571X)	+= snd-soc-tas571x.o
 obj-$(CONFIG_SND_SOC_TAS5720)	+= snd-soc-tas5720.o
sound/soc/codecs/tas2783-sdw.c new file +932 -0

932 lines not on this page · raw message

sound/soc/codecs/tas2783.h new file +106 -0

106 lines not on this page · raw message

Signature · 1 line
2.34.1
#1 lore raw
Quoted 12 lines · Shenghao Ding

The tas2783 is a smart audio amplifier with integrated MIPI SoundWire interface (Version 1.2.1 compliant), I2C, and I2S/TDM interfaces designed for portable applications. An on-chip DSP supports Texas Instruments SmartAmp speaker protection algorithm. The integrated speaker voltage and current sense provides for real-time monitoring of loudspeakers.

The ASoC component provides the majority of the functionality of the device, all the audio functions.

---

...

Quoted 34 lines
sound/soc/codecs/tas2783-sdw.c new file +34 -0
+
+static void tas2783_apply_calibv2(struct tasdevice_priv *tas_dev,
+	unsigned int *cali_data)
+{
+	const unsigned int arr_size = ARRAY_SIZE(tas2783_cali_reg);
+	struct regmap *map = tas_dev->regmap;
+	unsigned int dev_sum = cali_data[1], i, j, k;
+	u8 *cali_start;
+	u16 dev_info;
+	int ret;
+
+	if (!tas_dev->sdw_peripheral) {
+		dev_err(tas_dev->dev, "%s: peripheral doesn't exist.\n",
+			__func__);
+		return;
+	}
+
+	dev_info = tas_dev->sdw_peripheral->bus->link_id |
+		tas_dev->sdw_peripheral->id.unique_id << 16;
+
+	/*
+	 * The area saving tas2783 calibrated data is specified by its
+	 * unique_id offset. cali_start is the first address of current
+	 * tas2783's calibrated data.
+	 */
+	cali_start = (u8 *)&cali_data[3];
+	for (i = 0; i < dev_sum; i++) {
+		k = i * (arr_size + 1) + 3;
+		if (dev_info != cali_data[k]) {
+			for (j = 0; j < arr_size; j++) {
+				k = 4 * (k + 1 + j);
+				ret = regmap_bulk_write(map,
+					tas2783_cali_reg[j],
+					&cali_start[k], 4);

sound/soc/codecs/tas2783-sdw.c line 212

Quoted 10 lines
sound/soc/codecs/tas2783-sdw.c new file +10 -0
+				if (ret) {
+					dev_err(tas_dev->dev,
+						"Cali failed %x:%d\n",
+						tas2783_cali_reg[j], ret);
+					break;
+				}
+			}
+			break;
+		}
+	}

This seems a bit hard to read, any chance to do some reordering to make it more readable?

Quoted 2 lines
(quoted diff) +2 -0
+}
+

...

Quoted 88 lines
sound/soc/codecs/tas2783-sdw.c new file +88 -0
+
+static int tasdevice_mute(struct snd_soc_dai *dai, int mute,
+	int direction)
+{
+	struct snd_soc_component *component = dai->component;
+	struct tasdevice_priv *tas_dev =
+		snd_soc_component_get_drvdata(component);
+	struct regmap *map = tas_dev->regmap;
+	int ret;
+
+	dev_dbg(tas_dev->dev, "%s: %d.\n", __func__, mute);
+
+	if (mute) {
+		if (direction == SNDRV_PCM_STREAM_CAPTURE) {
+			ret = regmap_update_bits(map, TAS2873_REG_PWR_CTRL,
+				TAS2783_REG_AEF_MASK,
+				TAS2783_REG_AEF_INACTIVE);
+			if (ret)
+				dev_err(tas_dev->dev,
+					"%s: Disable AEF failed.\n", __func__);
+		} else {
+			/* FU23 mute (0x40400108) */
+			ret = regmap_write(map,
+				SDW_SDCA_CTL(TAS2783_FUNC_TYPE_SMART_AMP,
+				TAS2783_SDCA_ENT_FU23,
+				TAS2783_SDCA_CTL_FU_MUTE, 0), 1);
+			if (ret) {
+				dev_err(tas_dev->dev,
+					"%s: FU23 mute failed.\n", __func__);
+				goto out;
+			}
+			/*
+			 * Both playback and echo data will be shutdown in
+			 * playback stream.
+			 */
+			ret = regmap_update_bits(map, TAS2873_REG_PWR_CTRL,
+				TAS2783_REG_PWR_MODE_MASK |
+				TAS2783_REG_AEF_MASK,
+				TAS2783_REG_PWR_MODE_ACTIVE |
+				TAS2783_REG_PWR_MODE_SW_PWD);
+			if (ret) {
+				dev_err(tas_dev->dev,
+					"%s: PWR&AEF shutdown failed.\n",
+					__func__);
+				goto out;
+			}
+			tas_dev->pstream = false;
+		}
+	} else {
+		/* FU23 Unmute, 0x40400108. */
+		if (direction == SNDRV_PCM_STREAM_PLAYBACK) {
+			ret = regmap_write(map,
+				SDW_SDCA_CTL(TAS2783_FUNC_TYPE_SMART_AMP,
+				TAS2783_SDCA_ENT_FU23,
+				TAS2783_SDCA_CTL_FU_MUTE, 0), 0);
+			if (ret) {
+				dev_err(tas_dev->dev,
+					"%s: FU23 Unmute failed.\n", __func__);
+				goto out;
+			}
+			ret = regmap_update_bits(map, TAS2873_REG_PWR_CTRL,
+				TAS2783_REG_PWR_MODE_MASK,
+				TAS2783_REG_PWR_MODE_ACTIVE);
+			if (ret) {
+				dev_err(tas_dev->dev,
+					"%s: PWR Unmute failed.\n", __func__);
+				goto out;
+			}
+			tas_dev->pstream = true;
+		} else {
+			/* Capture stream is the echo ref data for voice.
+			 * Without playback, it can't be active.
+			 */
+			if (tas_dev->pstream == true) {
+				ret = regmap_update_bits(map,
+					TAS2873_REG_PWR_CTRL,
+					TAS2783_REG_AEF_MASK,
+					TAS2783_REG_AEF_ACTIVE);
+				if (ret) {
+					dev_err(tas_dev->dev,
+						"%s: AEF enable failed.\n",
+						__func__);
+					goto out;
+				}
+			} else {
+				dev_err(tas_dev->dev,
+					"%s: No playback, no AEF!", __func__);
+				ret = -EINVAL;

sound/soc/codecs/tas2783-sdw.c line 545

Quoted 10 lines
sound/soc/codecs/tas2783-sdw.c new file +10 -0
+			}
+		}
+	}
+out:
+	if (ret)
+		dev_err(tas_dev->dev, "Mute or unmute %d failed %d.\n",
+			mute, ret);
+
+	return ret;
+}

Above function seem to be bit long, which also causes a lot of indentation, perhaps split it into mute and unmute helpers?

...

Quoted 33 lines
sound/soc/codecs/tas2783-sdw.c new file +33 -0
+
+static int tasdevice_read_prop(struct sdw_slave *slave)
+{
+	struct sdw_slave_prop *prop = &slave->prop;
+	struct sdw_dpn_prop *dpn;
+	unsigned long addr;
+	int nval, i, j;
+	u32 bit;
+
+	prop->scp_int1_mask = SDW_SCP_INT1_BUS_CLASH | SDW_SCP_INT1_PARITY;
+	prop->quirks = SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY;
+
+	prop->paging_support = true;
+
+	/* first we need to allocate memory for set bits in port lists */
+	prop->source_ports = BIT(2); /* BITMAP: 00000100 */
+	prop->sink_ports = BIT(1); /* BITMAP:  00000010 */
+
+	nval = hweight32(prop->source_ports);
+	prop->src_dpn_prop = devm_kcalloc(&slave->dev, nval,
+		sizeof(*prop->src_dpn_prop), GFP_KERNEL);
+	if (!prop->src_dpn_prop)
+		return -ENOMEM;
+
+	i = 0;
+	dpn = prop->src_dpn_prop;
+	addr = prop->source_ports;
+	for_each_set_bit(bit, &addr, 32) {
+		dpn[i].num = bit;
+		dpn[i].type = SDW_DPN_FULL;
+		dpn[i].simple_ch_prep_sm = true;
+		dpn[i].ch_prep_timeout = 10;
+		i++;

sound/soc/codecs/tas2783-sdw.c line 735

Quoted 10 lines
sound/soc/codecs/tas2783-sdw.c new file +10 -0
+	}
+
+	/* do this again for sink now */
+	nval = hweight32(prop->sink_ports);
+	prop->sink_dpn_prop = devm_kcalloc(&slave->dev, nval,
+		sizeof(*prop->sink_dpn_prop), GFP_KERNEL);
+	if (!prop->sink_dpn_prop)
+		return -ENOMEM;
+
+	j = 0;

No need for separate j variable, you can reuse i here.

Quoted 16 lines
sound/soc/codecs/tas2783-sdw.c new file +16 -0
+	dpn = prop->sink_dpn_prop;
+	addr = prop->sink_ports;
+	for_each_set_bit(bit, &addr, 32) {
+		dpn[j].num = bit;
+		dpn[j].type = SDW_DPN_FULL;
+		dpn[j].simple_ch_prep_sm = true;
+		dpn[j].ch_prep_timeout = 10;
+		j++;
+	}
+
+	/* set the timeout values */
+	prop->clk_stop_timeout = 20;
+
+	return 0;
+}
+

...

sound/soc/codecs/tas2783.h line 92

Quoted 3 lines
sound/soc/codecs/tas2783.h new file +3 -0
+
+struct tasdevice_priv {
+	struct snd_soc_component *component;

Apart from being assigned this field seems to be unused.

sound/soc/codecs/tas2783.h line 94

Quoted 2 lines
sound/soc/codecs/tas2783.h new file +2 -0
+	struct sdw_slave *sdw_peripheral;
+	enum sdw_slave_status status;

This one seems to be only used in tasdevice_update_status()? Does it really need to be kept in struct?

sound/soc/codecs/tas2783.h line 95

Quoted 1 line
sound/soc/codecs/tas2783.h new file +1 -0
+	struct sdw_bus_params params;

Unused?

sound/soc/codecs/tas2783.h line 98

Quoted 3 lines
sound/soc/codecs/tas2783.h new file +3 -0
+	struct regmap *regmap;
+	struct device *dev;
+	unsigned char dspfw_binaryname[TAS2783_DSPFW_FILENAME_LEN];

This one also seems weird, it is mainly needed when loading FW and could be local to tasdevice_comp_probe(), although there is one dev_warn which uses it outside of it, but pretty sure it could be dropped.

sound/soc/codecs/tas2783.h line 99

Quoted 1 line
sound/soc/codecs/tas2783.h new file +1 -0
+	unsigned char dev_name[32];

Another unused field.

sound/soc/codecs/tas2783.h line 100

Quoted 1 line
sound/soc/codecs/tas2783.h new file +1 -0
+	unsigned int chip_id;

Another one that only seems to be assigned.

Quoted 6 lines
sound/soc/codecs/tas2783.h new file +6 -0
+	bool pstream;
+	bool hw_init;
+	bool first_hw_init;
+};
+
+#endif /*__TAS2783_H__ */
author maintainer
#2 lore raw

Thanks for your comments. Kindly see the feedback

Quoted 30 lines

-----Original Message-----
From: Amadeusz Sławiński <[email protected]>
Sent: Wednesday, March 6, 2024 12:05 AM
To: Ding, Shenghao <[email protected]>; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Lu, Kevin <[email protected]>; [email protected]; [email protected]; [email protected]; Navada Kanyana, Mukund <[email protected]>
Subject: [EXTERNAL] Re: [PATCH v11] ASoc: tas2783: Add tas2783 codec driver

Quoted 5 lines · Shenghao Ding

The tas2783 is a smart audio amplifier with integrated MIPI SoundWire interface (Version 1.2.1 compliant), I2C, and I2S/TDM interfaces designed for portable applications. An on-chip DSP supports Texas Instruments SmartAmp speaker protection algorithm. The integrated speaker voltage and current sense provides for real-time monitoring of

loudspeakers.

Quoted 7 lines

The ASoC component provides the majority of the functionality of the device, all the audio functions.

---

...

...

Quoted 4 lines

sound/soc/codecs/tas2783.h line 92

Quoted 2 lines
sound/soc/codecs/tas2783.h new file +2 -0
+struct tasdevice_priv {
+	struct snd_soc_component *component;

Apart from being assigned this field seems to be unused.

This field can help to get the point to "struct tasdevice_priv" in tasdevice_sdw_pcm_hw_free, tasdevice_sdw_hw_params, tasdevice_mute

Quoted 33 lines

sound/soc/codecs/tas2783.h line 94

Quoted 3 lines
sound/soc/codecs/tas2783.h new file +2 -0
+	struct sdw_slave *sdw_peripheral;
+	enum sdw_slave_status status;

This one seems to be only used in tasdevice_update_status()? Does it really need to be kept in struct?

sound/soc/codecs/tas2783.h line 95

Quoted 1 line
sound/soc/codecs/tas2783.h new file +1 -0
+	struct sdw_bus_params params;

Unused?

sound/soc/codecs/tas2783.h line 98

Quoted 3 lines
sound/soc/codecs/tas2783.h new file +3 -0
+	struct regmap *regmap;
+	struct device *dev;
+	unsigned char dspfw_binaryname[TAS2783_DSPFW_FILENAME_LEN];

This one also seems weird, it is mainly needed when loading FW and could be local to tasdevice_comp_probe(), although there is one dev_warn which uses it outside of it, but pretty sure it could be dropped.

sound/soc/codecs/tas2783.h line 99

Quoted 1 line
sound/soc/codecs/tas2783.h new file +1 -0
+	unsigned char dev_name[32];

Another unused field.

sound/soc/codecs/tas2783.h line 100

Quoted 1 line
sound/soc/codecs/tas2783.h new file +1 -0
+	unsigned int chip_id;

Another one that only seems to be assigned.

Quoted 6 lines
sound/soc/codecs/tas2783.h new file +6 -0
+	bool pstream;
+	bool hw_init;
+	bool first_hw_init;
+};
+
+#endif /*__TAS2783_H__ */
#3 lore raw
Quoted 31 lines · Shenghao

Thanks for your comments. Kindly see the feedback

Quoted 29 lines

-----Original Message-----
From: Amadeusz Sławiński <[email protected]>
Sent: Wednesday, March 6, 2024 12:05 AM
To: Ding, Shenghao <[email protected]>; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Lu, Kevin <[email protected]>; [email protected]; [email protected]; [email protected]; Navada Kanyana, Mukund <[email protected]>
Subject: [EXTERNAL] Re: [PATCH v11] ASoc: tas2783: Add tas2783 codec driver

Quoted 5 lines · Shenghao Ding

The tas2783 is a smart audio amplifier with integrated MIPI SoundWire interface (Version 1.2.1 compliant), I2C, and I2S/TDM interfaces designed for portable applications. An on-chip DSP supports Texas Instruments SmartAmp speaker protection algorithm. The integrated speaker voltage and current sense provides for real-time monitoring of

loudspeakers.

Quoted 7 lines

The ASoC component provides the majority of the functionality of the device, all the audio functions.

---

Quoted 9 lines
Quoted 1 line

...

...

Quoted 4 lines

sound/soc/codecs/tas2783.h line 92

Quoted 2 lines
sound/soc/codecs/tas2783.h new file +2 -0
+struct tasdevice_priv {
+	struct snd_soc_component *component;

Apart from being assigned this field seems to be unused.

This field can help to get the point to "struct tasdevice_priv" in tasdevice_sdw_pcm_hw_free, tasdevice_sdw_hw_params, tasdevice_mute

This field is unused in those functions, in all of them you get component from dai, via dai->component, and tas_priv from this component via snd_soc_component_get_drvdata, which is wrapper around dev_get_drvdata, which just returns dev->driver_data.

#4 lore raw
In reply to Shenghao Ding
Quoted 36 lines
sound/soc/codecs/tas2783-sdw.c new file +35 -0
+static void tas2783_apply_calibv1(struct tasdevice_priv *tas_dev,
+	unsigned int *cali_data)
+{
+	struct regmap *map = tas_dev->regmap;
+	u8 *cali_start;
+	u16 offset;
+	int ret;
+
+	if (!tas_dev->sdw_peripheral) {
+		dev_err(tas_dev->dev, "%s: peripheral doesn't exist.\n",
+			__func__);
+		return;
+	}
+
+	offset = tas_dev->sdw_peripheral->id.unique_id -
+		TAS2783_UNIQUE_ID_BASE;
+	if (offset >= TAS2783_MAX_DEV_NUM) {
+		dev_err(tas_dev->dev, "%s: offset(%u) is out of range.\n",
+			 __func__, offset);
+		return;
+	}
+
+	/*
+	 * The area saving tas2783 calibrated data is specified by its
+	 * unique_id offset. cali_start is the first address of current
+	 * tas2783's calibrated data.
+	 */
+	cali_start = (u8 *)(cali_data + offset * sizeof(tas2783_cali_reg));
+	for (int i = 0; i < ARRAY_SIZE(tas2783_cali_reg); i++) {
+		ret = regmap_bulk_write(map, tas2783_cali_reg[i],
+			&cali_start[4 * i], 4);
+		if (ret) {
+			dev_err(tas_dev->dev, "Cali failed %x:%d\n",
+				tas2783_cali_reg[i], ret);
+			break;

sound/soc/codecs/tas2783-sdw.c line 175

Quoted 10 lines
sound/soc/codecs/tas2783-sdw.c new file +10 -0
+		}
+	}
+}
+
+static void tas2783_apply_calibv2(struct tasdevice_priv *tas_dev,
+	unsigned int *cali_data)
+{
+	const unsigned int arr_size = ARRAY_SIZE(tas2783_cali_reg);
+	struct regmap *map = tas_dev->regmap;
+	unsigned int dev_sum = cali_data[1], i, j, k;

this is horrible to read, at the very least move initializations to a different line. I personally stick to one variable per line.

Quoted 28 lines
sound/soc/codecs/tas2783-sdw.c new file +28 -0
+	u8 *cali_start;
+	u16 dev_info;
+	int ret;
+
+	if (!tas_dev->sdw_peripheral) {
+		dev_err(tas_dev->dev, "%s: peripheral doesn't exist.\n",
+			__func__);
+		return;
+	}
+
+	dev_info = tas_dev->sdw_peripheral->bus->link_id |
+		tas_dev->sdw_peripheral->id.unique_id << 16;
+
+	/*
+	 * The area saving tas2783 calibrated data is specified by its
+	 * unique_id offset. cali_start is the first address of current
+	 * tas2783's calibrated data.
+	 */
+	cali_start = (u8 *)&cali_data[3];
+	for (i = 0; i < dev_sum; i++) {
+		k = i * (arr_size + 1) + 3;
+		if (dev_info != cali_data[k]) {
+			for (j = 0; j < arr_size; j++) {
+				k = 4 * (k + 1 + j);
+				ret = regmap_bulk_write(map,
+					tas2783_cali_reg[j],
+					&cali_start[k], 4);
+				if (ret) {

sound/soc/codecs/tas2783-sdw.c line 213

Quoted 10 lines
sound/soc/codecs/tas2783-sdw.c new file +10 -0
+					dev_err(tas_dev->dev,
+						"Cali failed %x:%d\n",
+						tas2783_cali_reg[j], ret);
+					break;
+				}
+			}
+			break;
+		}
+	}
+}

You may want to explain what v1 and v2 versions of the calibration do, and why you need two versions in the first place.
The code has this weird indentation that makes it hard to read. Also please use __func__ in the dev_err message, nothing is more annoying that having two identical logs in different functions.

Quoted 25 lines
sound/soc/codecs/tas2783-sdw.c new file +25 -0
+
+/*
+ * Load the calibration data, including speaker impedance, f0, etc.
+ * Calibration is done by the manufacturer in the factory. The calibration
+ * data are used by the algorithm for calculating the speaker temperature,
+ * speaker membrane excursion and f0 in real time during playback.
+ * The DSP will work with default data values if calibrated data are
+ * missing or are invalid.
+ * Layout of calibrated Data in UEFI(total 256 bytes):
+ * V2:
+ *	ChipID (2783, 4 bytes)
+ *	Device-Sum (4 bytes)
+ *	TimeStamp of Calibration (4 bytes)
+ *	for (i = 0; i < Device-Sum; i++) {
+ *		Device #i info () {
+ *			SDW link id (2bytes)
+ *			SDW unique_id (2bytes)
+ *		}
+ *		Calibrated Data of Device #i (20 bytes)
+ *	}
+ *	CRC (4 bytes)
+ *	Reserved (the rest)
+ * Or
+ * V1:
+ *	Calibrated Data of Dev 0(unique_id offset 0) (20 bytes)

sound/soc/codecs/tas2783-sdw.c line 248

Quoted 10 lines
sound/soc/codecs/tas2783-sdw.c new file +10 -0
+ *	Calibrated Data of Dev 1(unique_id offset 1) (20 bytes)
+ *	Calibrated Data of Dev 2(unique_id offset 2) (20 bytes)
+ *	Calibrated Data of Dev 3(unique_id offset 3) (20 bytes)
+ *	Calibrated Data of Dev 4(unique_id offset 4) (20 bytes)
+ *	Calibrated Data of Dev 5(unique_id offset 5) (20 bytes)
+ *	Calibrated Data of Dev 6(unique_id offset 6) (20 bytes)
+ *	Calibrated Data of Dev 7(unique_id offset 7) (20 bytes)
+ *	TimeStamp of Calibration (4 bytes)
+ *	CRC (4 bytes)
+ *	Reserved (88 bytes)

This information should be moved earlier to help reviewers who read a patch sequentially.

If you took the time to define two separate UEFI layouts, it's probably because some initial devices only support the first version? In that case, you should still detect and make sure multi-link solutions do not rely on the v1?

Quoted 22 lines

+ */

sound/soc/codecs/tas2783-sdw.c new file +21 -0
+static void tas2783_calibration(struct tasdevice_priv *tas_dev)
+{
+	efi_guid_t efi_guid = EFI_GUID(0x1f52d2a1, 0xbb3a, 0x457d, 0xbc,
+			0x09, 0x43, 0xa3, 0xf4, 0x31, 0x0a, 0x92);
+	static efi_char16_t efi_name[] = L"CALI_DATA";
+	struct calibration_data cali_data;
+	unsigned int *tmp_val;
+	unsigned int crc;
+	efi_status_t status;
+
+	cali_data.total_sz = 0;
+
+	status = efi.get_variable(efi_name, &efi_guid, NULL,
+		&cali_data.total_sz, NULL);
+	if (status == EFI_BUFFER_TOO_SMALL
+		&& cali_data.total_sz < TAS2783_MAX_CALIDATA_SIZE) {
+		status = efi.get_variable(efi_name, &efi_guid, NULL,
+			&cali_data.total_sz,
+			cali_data.data);
+		dev_dbg(tas_dev->dev, "%s: cali get %lx bytes result:%ld\n",
+			__func__, cali_data.total_sz, status);

sound/soc/codecs/tas2783-sdw.c line 280

Quoted 10 lines
sound/soc/codecs/tas2783-sdw.c new file +10 -0
+	}
+	if (status != 0) {
+		/* Failed got calibration data from EFI. */
+		dev_dbg(tas_dev->dev, "No calibration data in UEFI.");
+		return;
+	}
+
+	tmp_val = (unsigned int *)cali_data.data;
+
+	if (tmp_val[0] == 2783) {

Goodness, what guaranteers that the '2783' value is NOT a calibrated value?

V2:
* ChipID (2783, 4 bytes)

V1:
* Calibrated Data of Dev 0(unique_id offset 0) (20 bytes)

Comparing binary values is very risky. Usually you need some sort of CRC to avoid conflicts...

sound/soc/codecs/tas2783-sdw.c line 284

Quoted 4 lines
sound/soc/codecs/tas2783-sdw.c new file +4 -0
+		/* Calibrated Data V2 */
+		unsigned int dev_sum = tmp_val[1];
+
+		if (dev_sum > TAS2783_MAX_DEV_NUM ||

I thought dev_sum meant some sort of 'sum' in the CRC sense, but it's really number_of_devices, isn't it?

Quoted 16 lines
sound/soc/codecs/tas2783-sdw.c new file +16 -0
+			dev_sum == 0) {
+			dev_dbg(tas_dev->dev, "No dev in calibrated data V2.");
+			return;
+		}
+		crc = crc32(~0, cali_data.data, 12 + dev_sum * 24) ^ ~0;
+		if (crc == tmp_val[3 + dev_sum * 6]) {
+			tas2783_apply_calibv2(tas_dev, tmp_val);
+			dev_dbg(tas_dev->dev, "V2: %ptTs", &tmp_val[40]);
+		} else {
+			dev_dbg(tas_dev->dev,
+				"V2: CRC 0x%08x not match 0x%08x\n",
+				crc, tmp_val[41]);
+		}
+	} else {
+		/* Calibrated Data V1 */
+		/* 8 devs * 20 bytes calibrated data/dev + 4 bytes Timestamp */

sound/soc/codecs/tas2783-sdw.c line 310

Quoted 10 lines
sound/soc/codecs/tas2783-sdw.c new file +10 -0
+		crc = crc32(~0, cali_data.data, 164) ^ ~0;
+		if (crc == tmp_val[41]) {
+			/* Date and time of when calibration was done. */
+			tas2783_apply_calibv1(tas_dev, tmp_val);
+			dev_dbg(tas_dev->dev, "V1: %ptTs", &tmp_val[40]);
+		} else {
+			dev_dbg(tas_dev->dev,
+				"V1: CRC 0x%08x not match 0x%08x\n",
+				crc, tmp_val[41]);
+		}

The CRC check should be used to determine if the v1 or v2 should be used. This is really broken IMHO, you could detect the wrong layout then flag that the CRC is bad without even trying the other layout...

Quoted 37 lines
sound/soc/codecs/tas2783-sdw.c new file +37 -0
+	}
+}
+
+static void tasdevice_dspfw_ready(const struct firmware *fmw,
+	void *context)
+{
+	struct tasdevice_priv *tas_dev =
+		(struct tasdevice_priv *) context;
+	struct tas2783_firmware_node *p;
+	struct regmap *map = tas_dev->regmap;
+	unsigned char *buf = NULL;
+	int offset = 0, img_sz;
+	int ret;
+
+	if (!fmw || !fmw->data) {
+		dev_warn(tas_dev->dev,
+			"%s: failed to read %s: work in bypass-dsp mode.\n",
+			__func__, tas_dev->dspfw_binaryname);
+		return;
+	}
+	buf = (unsigned char *)fmw->data;
+
+	img_sz = get_unaligned_le32(&buf[offset]);
+	offset  += sizeof(img_sz);
+	if (img_sz != fmw->size) {
+		dev_warn(tas_dev->dev, "%s: size not matching, %d %u.",
+			__func__, (int)fmw->size, img_sz);
+		return;
+	}
+
+	while (offset < img_sz) {
+		p = (struct tas2783_firmware_node *)(buf + offset);
+		if (p->length > 1) {
+			ret = regmap_bulk_write(map, p->download_addr,
+			buf + offset + sizeof(unsigned int) * 5, p->length);
+		} else {
+			ret = regmap_write(map, p->download_addr,

sound/soc/codecs/tas2783-sdw.c line 357

Quoted 10 lines
sound/soc/codecs/tas2783-sdw.c new file +10 -0
+				*(buf + offset + sizeof(unsigned int) * 5));
+		}
+
+		if (ret != 0) {
+			dev_dbg(tas_dev->dev,
+				"%s: load FW fail: %d, work in bypass.\n",
+				__func__, ret);
+			return;
+		}
+		offset += sizeof(unsigned int) * 5 + p->length;

what is '5'? Using a define is best to avoid such magic numbers.

Quoted 29 lines
sound/soc/codecs/tas2783-sdw.c new file +28 -0
+	}
+
+	tas2783_calibration(tas_dev);
+}
+
 
+static int tasdevice_mute(struct snd_soc_dai *dai, int mute,
+	int direction)
+{
+	struct snd_soc_component *component = dai->component;
+	struct tasdevice_priv *tas_dev =
+		snd_soc_component_get_drvdata(component);
+	struct regmap *map = tas_dev->regmap;
+	int ret;
+
+	dev_dbg(tas_dev->dev, "%s: %d.\n", __func__, mute);
+
+	if (mute) {
+		if (direction == SNDRV_PCM_STREAM_CAPTURE) {
+			ret = regmap_update_bits(map, TAS2873_REG_PWR_CTRL,
+				TAS2783_REG_AEF_MASK,
+				TAS2783_REG_AEF_INACTIVE);
+			if (ret)
+				dev_err(tas_dev->dev,
+					"%s: Disable AEF failed.\n", __func__);
+		} else {
+			/* FU23 mute (0x40400108) */
+			ret = regmap_write(map,
+				SDW_SDCA_CTL(TAS2783_FUNC_TYPE_SMART_AMP,

sound/soc/codecs/tas2783-sdw.c line 481

Quoted 10 lines
sound/soc/codecs/tas2783-sdw.c new file +10 -0
+				TAS2783_SDCA_ENT_FU23,
+				TAS2783_SDCA_CTL_FU_MUTE, 0), 1);
+			if (ret) {
+				dev_err(tas_dev->dev,
+					"%s: FU23 mute failed.\n", __func__);
+				goto out;
+			}
+			/*
+			 * Both playback and echo data will be shutdown in
+			 * playback stream.

[1] echo reference data is usually not part of the playback stream but provided over a capture stream. Please clarify this comment.

Quoted 28 lines
sound/soc/codecs/tas2783-sdw.c new file +28 -0
+			 */
+			ret = regmap_update_bits(map, TAS2873_REG_PWR_CTRL,
+				TAS2783_REG_PWR_MODE_MASK |
+				TAS2783_REG_AEF_MASK,
+				TAS2783_REG_PWR_MODE_ACTIVE |
+				TAS2783_REG_PWR_MODE_SW_PWD);
+			if (ret) {
+				dev_err(tas_dev->dev,
+					"%s: PWR&AEF shutdown failed.\n",
+					__func__);
+				goto out;
+			}
+			tas_dev->pstream = false;
+		}
+	} else {
+		/* FU23 Unmute, 0x40400108. */
+		if (direction == SNDRV_PCM_STREAM_PLAYBACK) {
+			ret = regmap_write(map,
+				SDW_SDCA_CTL(TAS2783_FUNC_TYPE_SMART_AMP,
+				TAS2783_SDCA_ENT_FU23,
+				TAS2783_SDCA_CTL_FU_MUTE, 0), 0);
+			if (ret) {
+				dev_err(tas_dev->dev,
+					"%s: FU23 Unmute failed.\n", __func__);
+				goto out;
+			}
+			ret = regmap_update_bits(map, TAS2873_REG_PWR_CTRL,
+				TAS2783_REG_PWR_MODE_MASK,

sound/soc/codecs/tas2783-sdw.c line 519

Quoted 10 lines
sound/soc/codecs/tas2783-sdw.c new file +10 -0
+				TAS2783_REG_PWR_MODE_ACTIVE);
+			if (ret) {
+				dev_err(tas_dev->dev,
+					"%s: PWR Unmute failed.\n", __func__);
+				goto out;
+			}
+			tas_dev->pstream = true;
+		} else {
+			/* Capture stream is the echo ref data for voice.
+			 * Without playback, it can't be active.

That makes case for [1] above.

I also don't get the concept of 'active'. Even when the playback is muted, you do want to provide a reference stream, don't you?

Also don't we have a potential race condition, you set the 'pstream' status for the playback but use it for capture. What tells you that this cannot be executed concurrently?

Quoted 26 lines
sound/soc/codecs/tas2783-sdw.c new file +26 -0
+			 */
+			if (tas_dev->pstream == true) {
+				ret = regmap_update_bits(map,
+					TAS2873_REG_PWR_CTRL,
+					TAS2783_REG_AEF_MASK,
+					TAS2783_REG_AEF_ACTIVE);
+				if (ret) {
+					dev_err(tas_dev->dev,
+						"%s: AEF enable failed.\n",
+						__func__);
+					goto out;
+				}
+			} else {
+				dev_err(tas_dev->dev,
+					"%s: No playback, no AEF!", __func__);
+				ret = -EINVAL;
+			}
+		}
+	}
+out:
+	if (ret)
+		dev_err(tas_dev->dev, "Mute or unmute %d failed %d.\n",
+			mute, ret);
+
+	return ret;
+}
author maintainer
#5 lore raw

Hi Pierre-Louis
Thanks for your commets.

Quoted 14 lines

-----Original Message-----
From: Pierre-Louis Bossart <[email protected]>
Sent: Wednesday, March 6, 2024 12:03 AM
To: Ding, Shenghao <[email protected]>; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; yung- [email protected]; Lu, Kevin <[email protected]>; [email protected]; [email protected]; [email protected]; Navada Kanyana, Mukund <[email protected]>
Subject: [EXTERNAL] Re: [PATCH v11] ASoc: tas2783: Add tas2783 codec driver

...............................

Quoted 68 lines
Quoted 20 lines
sound/soc/codecs/tas2783-sdw.c new file +20 -0
+static void tas2783_calibration(struct tasdevice_priv *tas_dev) {
+	efi_guid_t efi_guid = EFI_GUID(0x1f52d2a1, 0xbb3a, 0x457d, 0xbc,
+			0x09, 0x43, 0xa3, 0xf4, 0x31, 0x0a, 0x92);
+	static efi_char16_t efi_name[] = L"CALI_DATA";
+	struct calibration_data cali_data;
+	unsigned int *tmp_val;
+	unsigned int crc;
+	efi_status_t status;
+
+	cali_data.total_sz = 0;
+
+	status = efi.get_variable(efi_name, &efi_guid, NULL,
+		&cali_data.total_sz, NULL);
+	if (status == EFI_BUFFER_TOO_SMALL
+		&& cali_data.total_sz < TAS2783_MAX_CALIDATA_SIZE) {
+		status = efi.get_variable(efi_name, &efi_guid, NULL,
+			&cali_data.total_sz,
+			cali_data.data);
+		dev_dbg(tas_dev->dev, "%s: cali get %lx bytes result:%ld\n",
+			__func__, cali_data.total_sz, status);

sound/soc/codecs/tas2783-sdw.c line 280

Quoted 10 lines
sound/soc/codecs/tas2783-sdw.c new file +10 -0
+	}
+	if (status != 0) {
+		/* Failed got calibration data from EFI. */
+		dev_dbg(tas_dev->dev, "No calibration data in UEFI.");
+		return;
+	}
+
+	tmp_val = (unsigned int *)cali_data.data;
+
+	if (tmp_val[0] == 2783) {

Goodness, what guaranteers that the '2783' value is NOT a calibrated value?

V2:
* ChipID (2783, 4 bytes)

V1:
* Calibrated Data of Dev 0(unique_id offset 0) (20 bytes)

Comparing binary values is very risky. Usually you need some sort of CRC to avoid conflicts...

sound/soc/codecs/tas2783-sdw.c line 284

Quoted 4 lines
sound/soc/codecs/tas2783-sdw.c new file +4 -0
+		/* Calibrated Data V2 */
+		unsigned int dev_sum = tmp_val[1];
+
+		if (dev_sum > TAS2783_MAX_DEV_NUM ||

I thought dev_sum meant some sort of 'sum' in the CRC sense, but it's really number_of_devices, isn't it?

Review

Quoted 2 lines
(quoted diff) +2 -0
+			dev_sum == 0) {
+			dev_dbg(tas_dev->dev, "No dev in calibrated data

V2.");

Quoted 4 lines
sound/soc/codecs/tas2783-sdw.c new file +4 -0
+			return;
+		}
+		crc = crc32(~0, cali_data.data, 12 + dev_sum * 24) ^ ~0;
+		if (crc == tmp_val[3 + dev_sum * 6]) {

sound/soc/codecs/tas2783-sdw.c

Quoted 10 lines
sound/soc/codecs/tas2783-sdw.c new file +10 -0
+			tas2783_apply_calibv2(tas_dev, tmp_val);
+			dev_dbg(tas_dev->dev, "V2: %ptTs", &tmp_val[40]);
+		} else {
+			dev_dbg(tas_dev->dev,
+				"V2: CRC 0x%08x not match 0x%08x\n",
+				crc, tmp_val[41]);
+		}
+	} else {
+		/* Calibrated Data V1 */
+		/* 8 devs * 20 bytes calibrated data/dev + 4 bytes Timestamp

*/

Quoted 14 lines

sound/soc/codecs/tas2783-sdw.c line 310

Quoted 10 lines
sound/soc/codecs/tas2783-sdw.c new file +10 -0
+		crc = crc32(~0, cali_data.data, 164) ^ ~0;
+		if (crc == tmp_val[41]) {
+			/* Date and time of when calibration was done. */
+			tas2783_apply_calibv1(tas_dev, tmp_val);
+			dev_dbg(tas_dev->dev, "V1: %ptTs", &tmp_val[40]);
+		} else {
+			dev_dbg(tas_dev->dev,
+				"V1: CRC 0x%08x not match 0x%08x\n",
+				crc, tmp_val[41]);
+		}

The CRC check should be used to determine if the v1 or v2 should be used. This is really broken IMHO, you could detect the wrong layout then flag that the CRC is bad without even trying the other layout...

It seemed not easy to add an extra CRC in V1, especially for the old device in the end users. As you know, the V1 is stored in raw data. In order to take care of the
old devices have been already in the end users, the V1 part has to keep here.

Quoted 143 lines
Quoted 38 lines
sound/soc/codecs/tas2783-sdw.c new file +37 -0
+	}
+}
+
+static void tasdevice_dspfw_ready(const struct firmware *fmw,
+	void *context)
+{
+	struct tasdevice_priv *tas_dev =
+		(struct tasdevice_priv *) context;
+	struct tas2783_firmware_node *p;
+	struct regmap *map = tas_dev->regmap;
+	unsigned char *buf = NULL;
+	int offset = 0, img_sz;
+	int ret;
+
+	if (!fmw || !fmw->data) {
+		dev_warn(tas_dev->dev,
+			"%s: failed to read %s: work in bypass-dsp mode.\n",
+			__func__, tas_dev->dspfw_binaryname);
+		return;
+	}
+	buf = (unsigned char *)fmw->data;
+
+	img_sz = get_unaligned_le32(&buf[offset]);
+	offset  += sizeof(img_sz);
+	if (img_sz != fmw->size) {
+		dev_warn(tas_dev->dev, "%s: size not matching, %d %u.",
+			__func__, (int)fmw->size, img_sz);
+		return;
+	}
+
+	while (offset < img_sz) {
+		p = (struct tas2783_firmware_node *)(buf + offset);
+		if (p->length > 1) {
+			ret = regmap_bulk_write(map, p->download_addr,
+			buf + offset + sizeof(unsigned int) * 5, p->length);
+		} else {
+			ret = regmap_write(map, p->download_addr,

sound/soc/codecs/tas2783-sdw.c line 357

Quoted 10 lines
sound/soc/codecs/tas2783-sdw.c new file +10 -0
+				*(buf + offset + sizeof(unsigned int) * 5));
+		}
+
+		if (ret != 0) {
+			dev_dbg(tas_dev->dev,
+				"%s: load FW fail: %d, work in bypass.\n",
+				__func__, ret);
+			return;
+		}
+		offset += sizeof(unsigned int) * 5 + p->length;

what is '5'? Using a define is best to avoid such magic numbers.

Quoted 10 lines
sound/soc/codecs/tas2783-sdw.c new file +9 -0
+	}
+
+	tas2783_calibration(tas_dev);
+}
+
 
+static int tasdevice_mute(struct snd_soc_dai *dai, int mute,
+	int direction)
+{
+	struct snd_soc_component *component = dai->component;

sound/soc/codecs/tas2783-sdw.c line 522

Quoted 10 lines
sound/soc/codecs/tas2783-sdw.c new file +10 -0
+	struct tasdevice_priv *tas_dev =
+		snd_soc_component_get_drvdata(component);
+	struct regmap *map = tas_dev->regmap;
+	int ret;
+
+	dev_dbg(tas_dev->dev, "%s: %d.\n", __func__, mute);
+
+	if (mute) {
+		if (direction == SNDRV_PCM_STREAM_CAPTURE) {
+			ret = regmap_update_bits(map,

TAS2873_REG_PWR_CTRL,

sound/soc/codecs/tas2783-sdw.c

Quoted 5 lines
sound/soc/codecs/tas2783-sdw.c new file +5 -0
+				TAS2783_REG_AEF_MASK,
+				TAS2783_REG_AEF_INACTIVE);
+			if (ret)
+				dev_err(tas_dev->dev,
+					"%s: Disable AEF failed.\n",

__func__);

sound/soc/codecs/tas2783-sdw.c line 543

Quoted 4 lines
sound/soc/codecs/tas2783-sdw.c new file +4 -0
+		} else {
+			/* FU23 mute (0x40400108) */
+			ret = regmap_write(map,
+
	SDW_SDCA_CTL(TAS2783_FUNC_TYPE_SMART_AMP,

sound/soc/codecs/tas2783-sdw.c line 481

Quoted 10 lines
sound/soc/codecs/tas2783-sdw.c new file +10 -0
+				TAS2783_SDCA_ENT_FU23,
+				TAS2783_SDCA_CTL_FU_MUTE, 0), 1);
+			if (ret) {
+				dev_err(tas_dev->dev,
+					"%s: FU23 mute failed.\n", __func__);
+				goto out;
+			}
+			/*
+			 * Both playback and echo data will be shutdown in
+			 * playback stream.

[1] echo reference data is usually not part of the playback stream but provided over a capture stream. Please clarify this comment.

Review

Quoted 2 lines
(quoted diff) +2 -0
+			 */
+			ret = regmap_update_bits(map,

TAS2873_REG_PWR_CTRL,

Quoted 7 lines
sound/soc/codecs/tas2783-sdw.c new file +7 -0
+				TAS2783_REG_PWR_MODE_MASK |
+				TAS2783_REG_AEF_MASK,
+				TAS2783_REG_PWR_MODE_ACTIVE |
+				TAS2783_REG_PWR_MODE_SW_PWD);
+			if (ret) {
+				dev_err(tas_dev->dev,
+					"%s: PWR&AEF shutdown failed.\n",

sound/soc/codecs/tas2783-sdw.c line 543

Quoted 10 lines
sound/soc/codecs/tas2783-sdw.c new file +10 -0
+					__func__);
+				goto out;
+			}
+			tas_dev->pstream = false;
+		}
+	} else {
+		/* FU23 Unmute, 0x40400108. */
+		if (direction == SNDRV_PCM_STREAM_PLAYBACK) {
+			ret = regmap_write(map,
+
	SDW_SDCA_CTL(TAS2783_FUNC_TYPE_SMART_AMP,

sound/soc/codecs/tas2783-sdw.c

Quoted 5 lines
sound/soc/codecs/tas2783-sdw.c new file +5 -0
+				TAS2783_SDCA_ENT_FU23,
+				TAS2783_SDCA_CTL_FU_MUTE, 0), 0);
+			if (ret) {
+				dev_err(tas_dev->dev,
+					"%s: FU23 Unmute failed.\n",

__func__);

sound/soc/codecs/tas2783-sdw.c line 522

Quoted 3 lines
sound/soc/codecs/tas2783-sdw.c new file +3 -0
+				goto out;
+			}
+			ret = regmap_update_bits(map,

TAS2873_REG_PWR_CTRL,

sound/soc/codecs/tas2783-sdw.c

Quoted 5 lines
sound/soc/codecs/tas2783-sdw.c new file +5 -0
+				TAS2783_REG_PWR_MODE_MASK,
+				TAS2783_REG_PWR_MODE_ACTIVE);
+			if (ret) {
+				dev_err(tas_dev->dev,
+					"%s: PWR Unmute failed.\n",

__func__);

sound/soc/codecs/tas2783-sdw.c line 519

Quoted 6 lines
sound/soc/codecs/tas2783-sdw.c new file +6 -0
+				goto out;
+			}
+			tas_dev->pstream = true;
+		} else {
+			/* Capture stream is the echo ref data for voice.
+			 * Without playback, it can't be active.

That makes case for [1] above.

Quoted 2 lines

I also don't get the concept of 'active'. Even when the playback is muted, you do want to provide a reference stream, don't you?

When playback is muted, it will turn off echo ref.

Quoted 4 lines

Also don't we have a potential race condition, you set the 'pstream' status for the playback but use it for capture. What tells you that this cannot be executed concurrently?

Capture in tas2783 can be unmute during playback is unmute. No playback, no capture.

Quoted 28 lines
Quoted 6 lines
sound/soc/codecs/tas2783-sdw.c new file +5 -0
+			 */
+			if (tas_dev->pstream == true) {
+				ret = regmap_update_bits(map,
+					TAS2873_REG_PWR_CTRL,
+					TAS2783_REG_AEF_MASK,

sound/soc/codecs/tas2783-sdw.c

Quoted 10 lines
sound/soc/codecs/tas2783-sdw.c new file +10 -0
+					TAS2783_REG_AEF_ACTIVE);
+				if (ret) {
+					dev_err(tas_dev->dev,
+						"%s: AEF enable failed.\n",
+						__func__);
+					goto out;
+				}
+			} else {
+				dev_err(tas_dev->dev,
+					"%s: No playback, no AEF!",

__func__);

Quoted 11 lines
sound/soc/codecs/tas2783-sdw.c new file +11 -0
+				ret = -EINVAL;
+			}
+		}
+	}
+out:
+	if (ret)
+		dev_err(tas_dev->dev, "Mute or unmute %d failed %d.\n",
+			mute, ret);
+
+	return ret;
+}
#6 lore raw
Quoted 36 lines
Quoted 21 lines

Review

Quoted 5 lines
(quoted diff) +2 -0
+			dev_sum == 0) {
+			dev_dbg(tas_dev->dev, "No dev in calibrated data

V2.");

Quoted 4 lines
sound/soc/codecs/tas2783-sdw.c new file +4 -0
+			return;
+		}
+		crc = crc32(~0, cali_data.data, 12 + dev_sum * 24) ^ ~0;
+		if (crc == tmp_val[3 + dev_sum * 6]) {

sound/soc/codecs/tas2783-sdw.c

Quoted 10 lines
sound/soc/codecs/tas2783-sdw.c new file +10 -0
+			tas2783_apply_calibv2(tas_dev, tmp_val);
+			dev_dbg(tas_dev->dev, "V2: %ptTs", &tmp_val[40]);
+		} else {
+			dev_dbg(tas_dev->dev,
+				"V2: CRC 0x%08x not match 0x%08x\n",
+				crc, tmp_val[41]);
+		}
+	} else {
+		/* Calibrated Data V1 */
+		/* 8 devs * 20 bytes calibrated data/dev + 4 bytes Timestamp

*/

Quoted 14 lines

sound/soc/codecs/tas2783-sdw.c line 310

Quoted 10 lines
sound/soc/codecs/tas2783-sdw.c new file +10 -0
+		crc = crc32(~0, cali_data.data, 164) ^ ~0;
+		if (crc == tmp_val[41]) {
+			/* Date and time of when calibration was done. */
+			tas2783_apply_calibv1(tas_dev, tmp_val);
+			dev_dbg(tas_dev->dev, "V1: %ptTs", &tmp_val[40]);
+		} else {
+			dev_dbg(tas_dev->dev,
+				"V1: CRC 0x%08x not match 0x%08x\n",
+				crc, tmp_val[41]);
+		}

The CRC check should be used to determine if the v1 or v2 should be used. This is really broken IMHO, you could detect the wrong layout then flag that the CRC is bad without even trying the other layout...

Quoted 3 lines

It seemed not easy to add an extra CRC in V1, especially for the old device in the end users. As you know, the V1 is stored in raw data. In order to take care of the
old devices have been already in the end users, the V1 part has to keep here.

I was referreing to the existing CRC at the end...

* V1:
* Calibrated Data of Dev 0(unique_id offset 0) (20 bytes)
* Calibrated Data of Dev 1(unique_id offset 1) (20 bytes)
* Calibrated Data of Dev 2(unique_id offset 2) (20 bytes)
* Calibrated Data of Dev 3(unique_id offset 3) (20 bytes)
* Calibrated Data of Dev 4(unique_id offset 4) (20 bytes)
* Calibrated Data of Dev 5(unique_id offset 5) (20 bytes)
* Calibrated Data of Dev 6(unique_id offset 6) (20 bytes)
* Calibrated Data of Dev 7(unique_id offset 7) (20 bytes)
* TimeStamp of Calibration (4 bytes)
* CRC (4 bytes) <<<< HERE
* Reserved (88 bytes)

Presumably the CRC covers all the data above, so you could first check if the format is V1 or V2 by checking the CRC values, then check the binary value as confirmation. You're doing the opposite comparison.

Quoted 54 lines
Quoted 54 lines
Quoted 41 lines
sound/soc/codecs/tas2783-sdw.c new file +41 -0
+	}
+}
+
+static void tasdevice_dspfw_ready(const struct firmware *fmw,
+	void *context)
+{
+	struct tasdevice_priv *tas_dev =
+		(struct tasdevice_priv *) context;
+	struct tas2783_firmware_node *p;
+	struct regmap *map = tas_dev->regmap;
+	unsigned char *buf = NULL;
+	int offset = 0, img_sz;
+	int ret;
+
+	if (!fmw || !fmw->data) {
+		dev_warn(tas_dev->dev,
+			"%s: failed to read %s: work in bypass-dsp mode.\n",
+			__func__, tas_dev->dspfw_binaryname);
+		return;
+	}
+	buf = (unsigned char *)fmw->data;
+
+	img_sz = get_unaligned_le32(&buf[offset]);
+	offset  += sizeof(img_sz);
+	if (img_sz != fmw->size) {
+		dev_warn(tas_dev->dev, "%s: size not matching, %d %u.",
+			__func__, (int)fmw->size, img_sz);
+		return;
+	}
+
+	while (offset < img_sz) {
+		p = (struct tas2783_firmware_node *)(buf + offset);
+		if (p->length > 1) {
+			ret = regmap_bulk_write(map, p->download_addr,
+			buf + offset + sizeof(unsigned int) * 5, p->length);
+		} else {
+			ret = regmap_write(map, p->download_addr,
+				*(buf + offset + sizeof(unsigned int) * 5));
+		}
+
+		if (ret != 0) {

sound/soc/codecs/tas2783-sdw.c

Quoted 10 lines
sound/soc/codecs/tas2783-sdw.c new file +9 -0
+			dev_dbg(tas_dev->dev,
+				"%s: load FW fail: %d, work in bypass.\n",
+				__func__, ret);
+			return;
+		}
+		offset += sizeof(unsigned int) * 5 + p->length;
 
+		} else {
+			/* Capture stream is the echo ref data for voice.
+			 * Without playback, it can't be active.

That makes case for [1] above.

Quoted 3 lines
Quoted 2 lines

I also don't get the concept of 'active'. Even when the playback is muted, you do want to provide a reference stream, don't you?

When playback is muted, it will turn off echo ref.

What does 'turn off' mean?

This could be
a) the echo reference is made of zero-value samples but is transmitted on the link
b) the echo reference is no longer transmitted and the stream is disabled with a bank switch.

Quoted 6 lines
Quoted 4 lines

Also don't we have a potential race condition, you set the 'pstream' status for the playback but use it for capture. What tells you that this cannot be executed concurrently?

Capture in tas2783 can be unmute during playback is unmute. No playback, no capture.

I can't follow your answer.

What I was asking is whether we can have a case where tas_dev->pstream can be tested while it's being set by another thread.

Quoted 28 lines
Quoted 27 lines
Quoted 5 lines
sound/soc/codecs/tas2783-sdw.c new file +5 -0
+			 */
+			if (tas_dev->pstream == true) {
+				ret = regmap_update_bits(map,
+					TAS2873_REG_PWR_CTRL,
+					TAS2783_REG_AEF_MASK,

sound/soc/codecs/tas2783-sdw.c

Quoted 10 lines
sound/soc/codecs/tas2783-sdw.c new file +10 -0
+					TAS2783_REG_AEF_ACTIVE);
+				if (ret) {
+					dev_err(tas_dev->dev,
+						"%s: AEF enable failed.\n",
+						__func__);
+					goto out;
+				}
+			} else {
+				dev_err(tas_dev->dev,
+					"%s: No playback, no AEF!",

__func__);

Quoted 11 lines
sound/soc/codecs/tas2783-sdw.c new file +11 -0
+				ret = -EINVAL;
+			}
+		}
+	}
+out:
+	if (ret)
+		dev_err(tas_dev->dev, "Mute or unmute %d failed %d.\n",
+			mute, ret);
+
+	return ret;
+}