Skip to contentSkip to conversation

ASoC: tas2781: Add tas2781 driver

PATCH Superseded Shenghao Ding 2 participants 2 messages Mar 27, 2023

Subsystems (3)

v8 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

PATCH v8 1/1 ASoC: tas2781: Add tas2781 driver

Create tas2781 driver.

---
Changes in v8:
- rework all the style issue
- some kzalloc+memcpy was repalaced by kmemdup
- rewrite check_inxxxxx_yram
- Add commets on why tasdevice_dev_update_bits did not support
- Add commets on why block size is not fixed.
- avoid code duplication, add some new function
- Use shorter name
Changes to be committed:

	modified:   sound/soc/codecs/Kconfig
	modified:   sound/soc/codecs/Makefile
	new file:   sound/soc/codecs/tas2781-dsp.c
	new file:   sound/soc/codecs/tas2781-dsp.h
	new file:   sound/soc/codecs/tas2781-i2c.c
	new file:   sound/soc/codecs/tas2781.h
---
 sound/soc/codecs/Kconfig       |   13 +
 sound/soc/codecs/Makefile      |    2 +
 sound/soc/codecs/tas2781-dsp.c | 2140 ++++++++++++++++++++++++++++++++
 sound/soc/codecs/tas2781-dsp.h |  181 +++
 sound/soc/codecs/tas2781-i2c.c | 1618 ++++++++++++++++++++++++
 sound/soc/codecs/tas2781.h     |  159 +++
 6 files changed, 4113 insertions(+)
 create mode 100644 sound/soc/codecs/tas2781-dsp.c
 create mode 100644 sound/soc/codecs/tas2781-dsp.h
 create mode 100644 sound/soc/codecs/tas2781-i2c.c
 create mode 100644 sound/soc/codecs/tas2781.h
sound/soc/codecs/Kconfig +13 -0
index 07747565c3b5..c4e3eb748f09 100644
@@ -229,6 +229,7 @@ config SND_SOC_ALL_CODECS
 	imply SND_SOC_TAS2764
 	imply SND_SOC_TAS2770
 	imply SND_SOC_TAS2780
+	imply SND_SOC_TAS2781
 	imply SND_SOC_TAS5086
 	imply SND_SOC_TAS571X
 	imply SND_SOC_TAS5720
@@ -1637,6 +1638,18 @@ config SND_SOC_TAS2780
 	  Enable support for Texas Instruments TAS2780 high-efficiency
 	  digital input mono Class-D audio power amplifiers.
 
+config SND_SOC_TAS2781
+	tristate "Texas Instruments TAS2781 speaker amplifier"
+	depends on I2C
+	select REGMAP_I2C
+	select CRC8
+	help
+	  Enable support for Texas Instruments TAS2781 Smart Amplifier
+	  Digital input mono Class-D and DSP-inside audio power amplifiers.
+	  Note the TAS2781 driver implements a flexible and configurable
+	  algo coefficient setting, for one, two or even multiple TAS2781
+	  chips.
+
 config SND_SOC_TAS5086
 	tristate "Texas Instruments TAS5086 speaker amplifier"
 	depends on I2C
sound/soc/codecs/Makefile +2 -0
index f1ca18f7946c..5559b9e9cc17 100644
@@ -262,6 +262,7 @@ snd-soc-tas5805m-objs := tas5805m.o
 snd-soc-tas6424-objs := tas6424.o
 snd-soc-tda7419-objs := tda7419.o
 snd-soc-tas2770-objs := tas2770.o
+snd-soc-tas2781-objs :=	tas2781-i2c.o tas2781-dsp.o
 snd-soc-tfa9879-objs := tfa9879.o
 snd-soc-tfa989x-objs := tfa989x.o
 snd-soc-tlv320adc3xxx-objs := tlv320adc3xxx.o
@@ -619,6 +620,7 @@ obj-$(CONFIG_SND_SOC_TAS2552)	+= snd-soc-tas2552.o
 obj-$(CONFIG_SND_SOC_TAS2562)	+= snd-soc-tas2562.o
 obj-$(CONFIG_SND_SOC_TAS2764)	+= snd-soc-tas2764.o
 obj-$(CONFIG_SND_SOC_TAS2780)	+= snd-soc-tas2780.o
+obj-$(CONFIG_SND_SOC_TAS2781)	+= snd-soc-tas2781.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/tas2781-dsp.c new file +2140 -0

2,140 lines not on this page · raw message

sound/soc/codecs/tas2781-dsp.h new file +181 -0

181 lines not on this page · raw message

sound/soc/codecs/tas2781-i2c.c new file +1618 -0

640 lines not on this page · raw message

sound/soc/codecs/tas2781.h new file +159 -0

159 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.34.1

Part of the diff is not shown here. View the raw message.

#1 lore raw

The code looks much better, and now that you've replaced all the -1 error codes with -EINVAL or -ENOMEM we can see clearly cases with memory leaks in the error handling path.

Quoted 6 lines
sound/soc/codecs/tas2781-dsp.c new file +6 -0
+	/* Skip the unused prog_size */
+	offset += (4 * (TASDEVICE_MAXPROGRAM_NUM_KERNEL -
+		tas_fmw->nr_programs));
+
+	if (offset + 4 > fmw->size) {
+		dev_err(tas_dev->dev, "%s: Configurations error\n", __func__);

sound/soc/codecs/tas2781-dsp.c line 428

Quoted 10 lines
sound/soc/codecs/tas2781-dsp.c new file +10 -0
+		offset = -EINVAL;
+		goto out;
+	}
+	tas_fmw->nr_configurations = SMS_HTONL(buf[offset], buf[offset + 1],
+		buf[offset + 2], buf[offset + 3]);
+	offset += 4;
+
+	/* The max of conf for more than equal to 4 pcs of tas2781s is
+	 * different from the one less than 4 pcs of tas2781s.
+	 */

Consider rewording, this comment is just confusing. Not sure what 'pcs' means here, and "more than equal' and 'one less than' should be replaced by 'greater than' and 'lower than'.

Quoted 41 lines
sound/soc/codecs/tas2781-dsp.c new file +41 -0
+static int fw_parse_data(struct tasdevice_fw *tas_fmw,
+	struct tasdevice_data *img_data, const struct firmware *fmw,
+	int offset)
+{
+	const unsigned char *data = (unsigned char *)fmw->data;
+	struct tasdev_blk *blk;
+	unsigned int i;
+	int n;
+
+	if (offset + 64 > fmw->size) {
+		dev_err(tas_fmw->dev, "%s: Name error\n", __func__);
+		offset = -EINVAL;
+		goto out;
+	}
+	memcpy(img_data->name, &data[offset], 64);
+	offset += 64;
+
+	n = strlen((char *)&data[offset]);
+	n++;
+	if (offset + n > fmw->size) {
+		dev_err(tas_fmw->dev, "%s: Description error\n", __func__);
+		offset = -EINVAL;
+		goto out;
+	}
+	offset += n;
+
+	if (offset + 2 > fmw->size) {
+		dev_err(tas_fmw->dev, "%s: Blocks error\n", __func__);
+		offset = -EINVAL;
+		goto out;
+	}
+	img_data->nr_blk = SMS_HTONS(data[offset], data[offset + 1]);
+	offset += 2;
+
+	img_data->dev_blks = kcalloc(img_data->nr_blk,
+		sizeof(struct tasdev_blk), GFP_KERNEL);
+	if (!img_data->dev_blks) {
+		offset = -ENOMEM;
+		goto out;
+	}
+	for (i = 0; i < img_data->nr_blk; i++) {

sound/soc/codecs/tas2781-dsp.c line 853

Quoted 10 lines
sound/soc/codecs/tas2781-dsp.c new file +10 -0
+		blk = &(img_data->dev_blks[i]);
+		offset = fw_parse_block_data(tas_fmw, blk, fmw, offset);
+		if (offset < 0) {
+			offset = -EINVAL;
+			goto out;
+		}
+	}
+
+out:
+	return offset;

memory leak on img_data->dev_blks, you need to free memory in the error handling path.

Quoted 24 lines
sound/soc/codecs/tas2781-dsp.c new file +24 -0
+}
+
+static int fw_parse_calibration_data(struct tasdevice_priv *tas_dev,
+	struct tasdevice_fw *tas_fmw, const struct firmware *fmw, int offset)
+{
+	struct tasdevice_calibration *calibration;
+	unsigned char *data = (unsigned char *)fmw->data;
+	unsigned int i, n;
+
+	if (offset + 2 > fmw->size) {
+		dev_err(tas_dev->dev, "%s: Calibrations error\n", __func__);
+		offset = -EINVAL;
+		goto out;
+	}
+	tas_fmw->nr_calibrations = SMS_HTONS(data[offset], data[offset + 1]);
+	offset += 2;
+
+	if (tas_fmw->nr_calibrations != 1) {
+		dev_err(tas_dev->dev,
+			"%s: only support one calibraiton(%d)!\n",
+			__func__, tas_fmw->nr_calibrations);
+		goto out;
+	}
+

sound/soc/codecs/tas2781-dsp.c line 887

Quoted 10 lines
sound/soc/codecs/tas2781-dsp.c new file +10 -0
+	tas_fmw->calibrations = kcalloc(tas_fmw->nr_calibrations,
+		sizeof(struct tasdevice_calibration), GFP_KERNEL);
+	if (!tas_fmw->calibrations) {
+		offset = -ENOMEM;
+		goto out;
+	}
+	for (i = 0; i < tas_fmw->nr_calibrations; i++) {
+		if (offset + 64 > fmw->size) {
+			dev_err(tas_dev->dev, "Calibrations error\n");
+			offset = -EINVAL;

memory leak on tas_fmw->calibrations, you need to change the error handling path.

Quoted 67 lines
sound/soc/codecs/tas2781-dsp.c new file +67 -0
+			goto out;
+		}
+		calibration = &(tas_fmw->calibrations[i]);
+		offset += 64;
+
+		n = strlen((char *)&data[offset]);
+		n++;
+		if (offset + n > fmw->size) {
+			dev_err(tas_dev->dev, "Description err\n");
+			offset = -EINVAL;
+			goto out;
+		}
+		offset += n;
+
+		if (offset + 1 > fmw->size) {
+			dev_err(tas_dev->dev, "%s: Prog err, offset = %d\n",
+				__func__, offset);
+			offset = -EINVAL;
+			goto out;
+		}
+		offset++;
+
+		if (offset + 1 > fmw->size) {
+			dev_err(tas_dev->dev, "%s: Conf err, offset = %d\n",
+				__func__, offset);
+			offset = -EINVAL;
+			goto out;
+		}
+		offset++;
+
+		offset = fw_parse_data(tas_fmw, &(calibration->dev_data), fmw,
+			offset);
+		if (offset < 0)
+			goto out;
+	}
+
+out:
+	return offset;
+}
+
+static int fw_parse_program_data(struct tasdevice_priv *tas_dev,
+	struct tasdevice_fw *tas_fmw, const struct firmware *fmw, int offset)
+{
+	unsigned char *buf = (unsigned char *)fmw->data;
+	struct tasdevice_prog *program;
+	int i;
+
+	if (offset + 2 > fmw->size) {
+		dev_err(tas_dev->dev, "%s: File Size error\n", __func__);
+		offset = -EINVAL;
+		goto out;
+	}
+	tas_fmw->nr_programs = SMS_HTONS(buf[offset], buf[offset + 1]);
+	offset += 2;
+
+	if (tas_fmw->nr_programs == 0) {
+		/*Not error in calibration Data file, return directly*/
+		dev_info(tas_dev->dev, "%s: No Programs data, maybe calbin\n",
+			__func__);
+		goto out;
+	}
+
+	tas_fmw->programs =
+		kcalloc(tas_fmw->nr_programs, sizeof(struct tasdevice_prog),
+			GFP_KERNEL);
+	if (!tas_fmw->programs) {
+		offset = -ENOMEM;

sound/soc/codecs/tas2781-dsp.c line 964

Quoted 10 lines
sound/soc/codecs/tas2781-dsp.c new file +10 -0
+		goto out;
+	}
+	for (i = 0; i < tas_fmw->nr_programs; i++) {
+		int n = 0;
+
+		program = &(tas_fmw->programs[i]);
+		if (offset + 64 > fmw->size) {
+			dev_err(tas_dev->dev, "%s: mpName error\n", __func__);
+			offset = -EINVAL;
+			goto out;

and memory leak here as well.

Stopping the review here, please revisit the error handling. you probably need two labels when memory is allocated, and a kfree() for one of the two.