Skip to contentSkip to conversation

ASoC: tas2781: Add tas2781 driver

PATCH In review Kevin Lu 4 participants 4 messages Feb 27, 2023 – Mar 8, 2023

Subsystems (3)
lore
Share & tools
lore

Kevin Lu

author
#0 lore raw

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

Create tas2781 driver.

---
Changes in v3:
- fixed issues reported-by: kernel test robot <[email protected]>
- correct all the inconsistent indenting warnings
- correct uninitialized symbol 'pwr_state'
- correct unsigned 'val' is never less than zero
- correct 'tas_dev' could be null

---
 sound/soc/codecs/Kconfig       |   12 +
 sound/soc/codecs/Makefile      |    2 +
 sound/soc/codecs/tas2781-dsp.c | 2261 ++++++++++++++++++++++++++++++++
 sound/soc/codecs/tas2781-dsp.h |  178 +++
 sound/soc/codecs/tas2781-i2c.c | 1913 +++++++++++++++++++++++++++
 sound/soc/codecs/tas2781.h     |  176 +++
 6 files changed, 4542 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 +12 -0
index 0f9d714..9016c48 100644
@@ -223,6 +223,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
@@ -1579,6 +1580,17 @@ 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 coff setting, for one, two, even multiple TAS2781 chips.
+
 config SND_SOC_TAS5086
 	tristate "Texas Instruments TAS5086 speaker amplifier"
 	depends on I2C
sound/soc/codecs/Makefile +2 -0
index 71d3ce5..49f912a 100644
@@ -355,6 +355,7 @@ snd-soc-tas2552-objs := tas2552.o
 snd-soc-tas2562-objs := tas2562.o
 snd-soc-tas2764-objs := tas2764.o
 snd-soc-tas2780-objs := tas2780.o
+snd-soc-tas2781-objs :=	tas2781-i2c.o tas2781-dsp.o
 # Mux
 snd-soc-simple-mux-objs := simple-mux.o
 
@@ -606,6 +607,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 +2261 -0

2,261 lines not on this page · raw message

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

178 lines not on this page · raw message

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

523 lines not on this page · raw message

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

176 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.

maintainer
#1 lore raw
Quoted 1 line · Kevin Lu

Create tas2781 driver.

Quite a few comments below, a lot of them are fairly simple stylistic ones rather than substantial things but there are a few bigger things. A lot of the bigger comments are essentially that the driver is doing more work than it needs to be and could either stop doing things that seem redundant or use generic functionality.

The bulk of the code looks good though.

Quoted 10 lines

---
Changes in v3:
- fixed issues reported-by: kernel test robot <[email protected]>
- correct all the inconsistent indenting warnings
- correct uninitialized symbol 'pwr_state'
- correct unsigned 'val' is never less than zero
- correct 'tas_dev' could be null

Your signoff needs to go before the ---, tooling will remove anything after the --- but your signoff needs to stay.

Quoted 5 lines

snd-soc-tas2562-objs := tas2562.o
snd-soc-tas2764-objs := tas2764.o
snd-soc-tas2780-objs := tas2780.o
+snd-soc-tas2781-objs := tas2781-i2c.o tas2781-dsp.o
# Mux

Please keep the blank line in the file.

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

Quoted 7 lines
sound/soc/codecs/tas2781-dsp.c new file +4 -0
@@ -0,0 +1,2261 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * ALSA SoC Texas Instruments TAS2781 Audio Smart Amplifier
+ *

Please make the entire comment block a C++ one so things look more intentional.

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

Quoted 3 lines
sound/soc/codecs/tas2781-dsp.c new file +3 -0
+static const unsigned char crc8_lookup_table[CRC8_TABLE_SIZE] = {
+	0x00, 0x4D, 0x9A, 0xD7, 0x79, 0x34, 0xE3, 0xAE,
+	0xF2, 0xBF, 0x68, 0x25, 0x8B, 0xC6, 0x11, 0x5C,

Should the driver be using one of the crc8_populate_() functions here?

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

Quoted 7 lines
sound/soc/codecs/tas2781-dsp.c new file +7 -0
+	ret = check_yram(&crc_data, book, page, reg, len);
+	dev_info(tasdevice->dev,
+		"isYRAM: Bk 0x%02x, Pg 0x%02x, Rg 0x%02x\n",
+		book, page, reg);
+	dev_info(tasdevice->dev,
+		"isYRAM: crc_data.mnLen 0x%x, len 0x%x, nResult %d\n",
+		crc_data.len, len, ret);

There's a lot of logging in this firmware load code which should at least be reduced to dbg level if not vdbg, it's pretty loud and it looks like at least some of it can be triggered after initial boot.

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

Quoted 2 lines
sound/soc/codecs/tas2781-dsp.c new file +2 -0
+/* max. length of the alsa mixer control name */
+#define MAX_CONTROL_NAME		(48)

This should be coming from the ALSA headers.

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

Quoted 3 lines
sound/soc/codecs/tas2781-dsp.c new file +3 -0
+{
+	struct tasdevice_priv *tas_dev =
+		(struct tasdevice_priv *)pContext;

The kernel doesn't gennerally use hungarian notation (I see a bunch of it in the driver, n_result and p_tasdevice for example), and if there's a need to cast away from void that usually means something's gone wrong
- it looks unneeded here so you can just remove the cast.

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

Quoted 3 lines
sound/soc/codecs/tas2781-dsp.c new file +3 -0
+	if (config_offset + 4 > (int)config_size) {
+		dev_err(tas_dev->dev,
+			"add config: Out of memory\n");

This isn't OOM though?

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

Quoted 6 lines
sound/soc/codecs/tas2781-dsp.c new file +6 -0
+	cfg_info->blk_data = kcalloc(
+		cfg_info->nblocks, sizeof(struct tasdev_blk_data *),
+		GFP_KERNEL);
+	if (!cfg_info->blk_data) {
+		dev_err(tas_dev->dev,
+			"add config: blk_data alloc failed!\n");

No need to log failures to allocate, the kernel is already pretty loud when it runs out of memory.

sound/soc/codecs/tas2781-dsp.c

Quoted 10 lines
sound/soc/codecs/tas2781-dsp.c new file +9 -0
+static int tas2781_digital_getvol(struct snd_kcontrol *kcontrol,
+       struct snd_ctl_elem_value *ucontrol)
 
+	/* Read the primary device as the whole */
+	ret = tasdevice_dev_read(tas_dev, 0, mc->reg, &val);
+	if (ret) {
+		dev_err(tas_dev->dev,
+		"%s, get digital vol error\n",
+		__func__);
+		goto out;

Your regmap looked like it already had paging support so I'm not clear why this is a custom control - can't you just use the standard ASoC helpers, the regmap paging support will work for paged register maps?

sound/soc/codecs/tas2781-dsp.c

Quoted 3 lines
sound/soc/codecs/tas2781-dsp.c new file +3 -0
+	}
+	val = (val > mc->max) ? mc->max : val;
+	val = mc->invert ? mc->max - val : val;

Please write normal conditional statements, they're easier to read than the ternery opreator.

sound/soc/codecs/tas2781-dsp.c

Quoted 11 lines
sound/soc/codecs/tas2781-dsp.c new file +11 -0
+static int tas2781_digital_putvol(struct snd_kcontrol *kcontrol,
+	struct snd_ctl_elem_value *ucontrol)
+{
+	struct snd_soc_component *codec
+		= snd_soc_kcontrol_component(kcontrol);
+	struct tasdevice_priv *tas_dev =
+		snd_soc_component_get_drvdata(codec);
+	struct soc_mixer_control *mc =
+		(struct soc_mixer_control *)kcontrol->private_value;
+	int val;
+	int i, ret = 0;

Please run the mixer-test selftest on a card with this device, it'll tell you that this function doesn't return 1 on value changes so doesn't generate events for changes. Similar issues with a lot of other custom controls.

sound/soc/codecs/tas2781-dsp.c

Quoted 7 lines
sound/soc/codecs/tas2781-dsp.c new file +7 -0
+static const struct snd_kcontrol_new tas2781_snd_controls[] = {
+	SOC_SINGLE_RANGE_EXT_TLV("Amp Gain Volume", TAS2781_AMP_LEVEL,
+		1, 0, 20, 0, tas2781_amp_getvol,
+		tas2781_amp_putvol, amp_vol_tlv),
+	SOC_SINGLE_RANGE_EXT_TLV("Digital Volume Control", TAS2781_DVC_LVL,
+		0, 0, 200, 1, tas2781_digital_getvol,
+		tas2781_digital_putvol, dvc_tlv),

Control names for volumes should end with Volume, see control-names.rst.

Quoted 7 lines
sound/soc/codecs/tas2781-dsp.c new file +7 -0
+static int tasdevice_info_programs(struct snd_kcontrol *kcontrol,
+			struct snd_ctl_elem_info *uinfo)
+{
+	struct snd_soc_component *codec
+		= snd_soc_kcontrol_component(kcontrol);
+	struct tasdevice_priv *p_tasdevice =
+		snd_soc_component_get_drvdata(codec);

sound/soc/codecs/tas2781-dsp.c

Quoted 10 lines
sound/soc/codecs/tas2781-dsp.c new file +10 -0
+	struct tasdevice_fw *tas_fw = p_tasdevice->fmw;
+
+	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
+	/* Codec Lock Hold*/
+	mutex_lock(&p_tasdevice->codec_lock);
+	uinfo->count = 1;
+	uinfo->value.integer.min = 0;
+	uinfo->value.integer.max = (int)tas_fw->n_programs;
+	/* Codec Lock Release*/
+	mutex_unlock(&p_tasdevice->codec_lock);

Why does this need a lock - what might be changing? The casting is suspicious too.

sound/soc/codecs/tas2781-dsp.c

Quoted 8 lines
sound/soc/codecs/tas2781-dsp.c new file +7 -0
+static void tasdevice_rca_ready(const struct firmware *fmw,
+	void *context)
+{
 
+	mutex_lock(&tas_dev->codec_lock);
+	rca = &(tas_dev->rcabin);
+	fw_hdr = &(rca->fw_hdr);
+	if (unlikely(!fmw) || unlikely(!fmw->data)) {

If you're not in a *very* hot path it's seldom useful to annotate with likely() or unlikely().

sound/soc/codecs/tas2781-dsp.c

Quoted 9 lines
sound/soc/codecs/tas2781-dsp.c new file +9 -0
+static void tasdevice_enable_irq(
+	struct tasdevice_priv *tas_dev, bool enable)
+{
+	if (enable) {
+		if (tas_dev->irq_info.is_irq_enable)
+			return;
+		if (gpio_is_valid(tas_dev->irq_info.irq_gpio))
+			enable_irq(tas_dev->irq_info.irq);
+		tas_dev->irq_info.is_irq_enable = true;

The driver shouldn't need to care if the interrupt is implemented with something that can also be a GPIO, and indeed I can't see anything that actually looks at the GPIO other than these checks. The check for double enalbes also seems suspicious, I don't think it's needed but...

sound/soc/codecs/tas2781-dsp.c

Quoted 3 lines
sound/soc/codecs/tas2781-dsp.c new file +3 -0
+	} else {
+		if (gpio_is_valid(tas_dev->irq_info.irq_gpio))
+			disable_irq_nosync(tas_dev->irq_info.irq);

...it's certainly not matched in the disable path. In general it looks like this function is just redundant and could be replaced by enable_irq()/disable_irq() calls in the callers, though I'm not sure it's needed at all.

Quoted 8 lines
sound/soc/codecs/tas2781-dsp.c new file +8 -0
+static void tasdevice_set_power_state(
+	struct tasdevice_priv *tas_dev, int state)
+{
+	switch (state) {
+	case 0:
+		schedule_delayed_work(&tas_dev->tuning_switch_wrk,
+			msecs_to_jiffies(20));
+		break;

sound/soc/codecs/tas2781-dsp.c

Quoted 10 lines
sound/soc/codecs/tas2781-dsp.c new file +10 -0
+	default:
+		if (!(tas_dev->pstream || tas_dev->cstream)) {
+			if (tas_dev->irq_work_func)
+				tasdevice_enable_irq(tas_dev, false);
+			tasdevice_select_cfg_blk(tas_dev,
+				tas_dev->rcabin.profile_cfg_id,
+				TASDEVICE_BIN_BLK_PRE_SHUTDOWN);
+		}
+		break;
+	}

It's really not clear what thisis supposed to do - what are the two power states here?

Quoted 5 lines
sound/soc/codecs/tas2781-dsp.c new file +5 -0
+static int tasdevice_dapm_event(struct snd_soc_dapm_widget *w,
+			struct snd_kcontrol *kcontrol, int event)
+{
+	struct snd_soc_component *codec = snd_soc_dapm_to_component(w->dapm);
+	struct tasdevice_priv *tas_dev = snd_soc_component_get_drvdata(codec);

sound/soc/codecs/tas2781-dsp.c

Quoted 10 lines
sound/soc/codecs/tas2781-dsp.c new file +10 -0
+	int stream;
+
+	/* Codec Lock Hold */
+	mutex_lock(&tas_dev->codec_lock);
+	stream = strcmp(w->name, "SPK") ? SNDRV_PCM_STREAM_PLAYBACK :
+		SNDRV_PCM_STREAM_CAPTURE;
+
+	switch (event) {
+	case SND_SOC_DAPM_POST_PMU:
+		dev_info(tas_dev->dev, "SND_SOC_DAPM_POST_PMU\n");

dev_info() on DAPM events is going to be way too noisy, there's a lot of logging in the core so you can probably just remove this.

sound/soc/codecs/tas2781-dsp.c

Quoted 11 lines
sound/soc/codecs/tas2781-dsp.c new file +11 -0
+	case SND_SOC_DAPM_PRE_PMD:
+		dev_info(tas_dev->dev, "SND_SOC_DAPM_PRE_PMD\n");
+		/* stop DSP only when both playback and capture streams
+		 * are deactivated
+		 */
+		if (stream == SNDRV_PCM_STREAM_PLAYBACK)
+			tas_dev->pstream = 0;
+		else
+			tas_dev->cstream = 0;
+		if (tas_dev->pstream != 0 || tas_dev->cstream != 0)
+			goto out;

It would probably be easier to have a supply widget that feeds both the ASI and speaker widgets, that way DAPM will reference count for you and the driver can just handle the event without these checks.

sound/soc/codecs/tas2781-dsp.c

Quoted 12 lines
sound/soc/codecs/tas2781-dsp.c new file +12 -0
+static int tasdevice_hw_params(struct snd_pcm_substream *substream,
+	struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
+{
+	struct tasdevice_priv *tas_dev = snd_soc_dai_get_drvdata(dai);
+	unsigned int fsrate;
+	unsigned int slot_width;
+	int bclk_rate;
+	int rc = 0;
+
+	dev_info(tas_dev->dev, "%s: %s\n",
+		__func__, substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
+		"Playback":"Capture");

Again, too much logging. The driver should be silent during normal operation - some logging on boot or error is fine but userspace shouldn't be able to spam the logs and hide other information.

sound/soc/codecs/tas2781-dsp.c

Quoted 12 lines
sound/soc/codecs/tas2781-dsp.c new file +12 -0
+static int tasdevice_set_dai_sysclk(struct snd_soc_dai *codec_dai,
+	int clk_id, unsigned int freq, int dir)
+{
+	struct tasdevice_priv *tas_dev = snd_soc_dai_get_drvdata(codec_dai);
+
+	dev_info(tas_dev->dev,
+		"%s: clk_id = %d, freq = %u, CLK direction %s\n",
+		__func__, clk_id, freq,
+		dir == TASDEVICE_CLK_DIR_OUT ? "OUT":"IN");
+
+	return 0;
+}

You can just remove this, it doesn't do anything except log.

sound/soc/codecs/tas2781-dsp.c

Quoted 3 lines
sound/soc/codecs/tas2781-dsp.c new file +3 -0
+static void tasdevice_codec_remove(
+	struct snd_soc_component *codec)
+{

Weird indentation here.

Quoted 10 lines
sound/soc/codecs/tas2781-dsp.c new file +10 -0
+static void irq_work_routine(struct work_struct *pWork)
+{
+	struct tasdevice_priv *tas_dev =
+		container_of(pWork, struct tasdevice_priv,
+		irq_info.irq_work.work);
+
+	mutex_lock(&tas_dev->codec_lock);
+	if (tas_dev->runtime_suspend) {
+		dev_info(tas_dev->dev, "%s, Runtime Suspended\n", __func__);
+		goto end;

sound/soc/codecs/tas2781-dsp.c

Quoted 10 lines
sound/soc/codecs/tas2781-dsp.c new file +10 -0
+	}
+	/*Logical Layer IRQ function, return is ignored*/
+	if (tas_dev->irq_work_func)
+		tas_dev->irq_work_func(tas_dev);
+	else
+		dev_info(tas_dev->dev,
+			"%s, irq_work_func is NULL\n", __func__);
+end:
+	mutex_unlock(&tas_dev->codec_lock);
+}

There is only one function that's ever assigned for the irq_work_func, just inline it and don't bother with the pointer in the driver structure - that should be a lot simpler.

sound/soc/codecs/tas2781-dsp.c

Quoted 13 lines
sound/soc/codecs/tas2781-dsp.c new file +13 -0
+static void tas2781_irq_work_func(struct tasdevice_priv *tas_dev)
+{
+	int rc = 0;
+	unsigned int reg_val = 0, array_size = 0, i = 0, ndev = 0;
+	unsigned int int_reg_array[] = {
+		TAS2781_REG_INT_LTCH0,
+		TAS2781_REG_INT_LTCH1,
+		TAS2781_REG_INT_LTCH1_0,
+		TAS2781_REG_INT_LTCH2,
+		TAS2781_REG_INT_LTCH3,
+		TAS2781_REG_INT_LTCH4};
+
+	tasdevice_enable_irq(tas_dev, false);

It needs to be a lot clearer what will reenable the interrupt - some comments would help, I see there's some interlock with tuning_switch_routine() but I'm not clear what that does...

Quoted 7 lines
sound/soc/codecs/tas2781-dsp.c new file +7 -0
+	array_size = ARRAY_SIZE(int_reg_array);
+
+	for (ndev = 0; ndev < tas_dev->ndev; ndev++) {
+		for (i = 0; i < array_size; i++) {
+			rc = tasdevice_dev_read(tas_dev,
+				ndev, int_reg_array[i], &reg_val);
+			if (!rc)

sound/soc/codecs/tas2781-dsp.c

Quoted 10 lines
sound/soc/codecs/tas2781-dsp.c new file +10 -0
+				dev_info(tas_dev->dev,
+					"INT STATUS REG 0x%04x=0x%02x\n",
+					int_reg_array[i], reg_val);
+			else
+				dev_err(tas_dev->dev,
+					"Read Reg 0x%04x error(rc=%d)\n",
+					int_reg_array[i], rc);
+		}
+	}
+}

...and this seems to just read the interrupt status registers and log the results so do we even need to enable the interrupt?

sound/soc/codecs/tas2781-dsp.c

Quoted 6 lines
sound/soc/codecs/tas2781-dsp.c new file +6 -0
+static int tasdevice_parse_dt(struct tasdevice_priv *tas_dev)
+{
+	struct device_node *np = tas_dev->dev->of_node;
+	int rc = 0, i = 0, ndev = 0;
+	unsigned int dev_addrs[max_chn];
+	struct i2c_client *client = (struct i2c_client *)tas_dev->client;

If you're adding DT bindings you need to send a DT bindings document.

sound/soc/codecs/tas2781-dsp.c

Quoted 3 lines
sound/soc/codecs/tas2781-dsp.c new file +3 -0
+static int tasdevice_change_chn_book(struct tasdevice_priv *tas_dev,
+	enum channel chn, int book)
+{

As I said earlier the regmap uses ranges so I'm not clear why there's open coding of page changing?

sound/soc/codecs/tas2781-dsp.c

Quoted 4 lines
sound/soc/codecs/tas2781-dsp.c new file +4 -0
+static void tas2781_set_global_mode(struct tasdevice_priv *tas_dev)
+{
+	int i = 0;
+	int ret = 0;

What's all this global mode stuff about? It's not clear from the code.

sound/soc/codecs/tas2781-dsp.c

Quoted 10 lines
sound/soc/codecs/tas2781-dsp.c new file +10 -0
+	mutex_init(&tas_dev->codec_lock);
+	ret = devm_snd_soc_register_component(tas_dev->dev,
+		&soc_codec_driver_tasdevice,
+		tasdevice_dai_driver, ARRAY_SIZE(tasdevice_dai_driver));
+	if (ret)
+		dev_err(tas_dev->dev, "%s: codec register error:0x%08x\n",
+			__func__, ret);
+
+	INIT_DELAYED_WORK(&tas_dev->irq_info.irq_work, irq_work_routine);
+	tas_dev->irq_info.is_irq_enable = false;

Shouldn't this happen before we register with the core?

sound/soc/codecs/tas2781-dsp.c

Quoted 13 lines
sound/soc/codecs/tas2781-dsp.c new file +13 -0
+static int tasdevice_pm_suspend(struct device *dev)
+{
+	struct tasdevice_priv *tas_dev = dev_get_drvdata(dev);
+
+	mutex_lock(&tas_dev->codec_lock);
+	tas_dev->runtime_suspend = true;
+
+	if (tas_dev->irq_work_func) {
+		if (delayed_work_pending(&tas_dev->irq_info.irq_work)) {
+			dev_dbg(tas_dev->dev, "cancel IRQ work\n");
+			cancel_delayed_work_sync(&tas_dev->irq_info.irq_work);
+		}
+	}

Just cancelling the work unconditionally should do the right thing, though given that there's no configuration done for the device it's unclear why we're implementing runtime suspend at all?

sound/soc/codecs/tas2781-dsp.c

Quoted 12 lines
sound/soc/codecs/tas2781-dsp.c new file +12 -0
+static int tasdevice_i2c_probe(struct i2c_client *i2c,
+	const struct i2c_device_id *id)
+{
+	struct tasdevice_priv *tas_dev = NULL;
+	int ret = 0;
+
+	if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_I2C)) {
+		dev_err(&i2c->dev,
+			"%s: I2C check failed\n", __func__);
+		ret = -ENODEV;
+		goto out;
+	}

Why the functionality check?

maintainer
#2 lore raw

Hi Brown
Thanks for your comments. You will rework and upstream immediately.
DT document has been already prepared, once the code has been upstream successfully, DT document will be done soon.

As to the comment on global mode, the mode is used for downloading the dsp firmware in multiple devices cases, all the devices can share the same generic i2c address in order to download the firmware simultaneously to save downloading firmware time consumption. I will add the comment for this part in the code.

As to the comment on page changing, this code only handles book changing, no extra code for page changing, all the page changings are handled automatically by the kernel system.

-----Original Message-----
From: Mark Brown <[email protected]>
Sent: Wednesday, March 8, 2023 4:48 AM
To: Kevin Lu <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; Lu, Kevin <[email protected]>; Ding, Shenghao <[email protected]>; [email protected]; [email protected]
Subject: [EXTERNAL] Re: [PATCH v3] ASoC: tas2781: Add tas2781 driver

Quoted 1 line · Kevin Lu

Create tas2781 driver.

Quite a few comments below, a lot of them are fairly simple stylistic ones rather than substantial things but there are a few bigger things.
A lot of the bigger comments are essentially that the driver is doing more work than it needs to be and could either stop doing things that seem redundant or use generic functionality.

The bulk of the code looks good though.

Quoted 10 lines

---
Changes in v3:
- fixed issues reported-by: kernel test robot <[email protected]>
- correct all the inconsistent indenting warnings
- correct uninitialized symbol 'pwr_state'
- correct unsigned 'val' is never less than zero
- correct 'tas_dev' could be null

Your signoff needs to go before the ---, tooling will remove anything after the --- but your signoff needs to stay.

Quoted 5 lines

snd-soc-tas2562-objs := tas2562.o
snd-soc-tas2764-objs := tas2764.o
snd-soc-tas2780-objs := tas2780.o
+snd-soc-tas2781-objs := tas2781-i2c.o tas2781-dsp.o
# Mux

Please keep the blank line in the file.

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

Quoted 7 lines
sound/soc/codecs/tas2781-dsp.c new file +4 -0
@@ -0,0 +1,2261 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * ALSA SoC Texas Instruments TAS2781 Audio Smart Amplifier
+ *

Please make the entire comment block a C++ one so things look more intentional.

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

Quoted 3 lines
sound/soc/codecs/tas2781-dsp.c new file +3 -0
+static const unsigned char crc8_lookup_table[CRC8_TABLE_SIZE] = {
+	0x00, 0x4D, 0x9A, 0xD7, 0x79, 0x34, 0xE3, 0xAE,
+	0xF2, 0xBF, 0x68, 0x25, 0x8B, 0xC6, 0x11, 0x5C,

Should the driver be using one of the crc8_populate_() functions here?

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

Quoted 7 lines
sound/soc/codecs/tas2781-dsp.c new file +7 -0
+	ret = check_yram(&crc_data, book, page, reg, len);
+	dev_info(tasdevice->dev,
+		"isYRAM: Bk 0x%02x, Pg 0x%02x, Rg 0x%02x\n",
+		book, page, reg);
+	dev_info(tasdevice->dev,
+		"isYRAM: crc_data.mnLen 0x%x, len 0x%x, nResult %d\n",
+		crc_data.len, len, ret);

There's a lot of logging in this firmware load code which should at least be reduced to dbg level if not vdbg, it's pretty loud and it looks like at least some of it can be triggered after initial boot.

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

Quoted 2 lines
sound/soc/codecs/tas2781-dsp.c new file +2 -0
+/* max. length of the alsa mixer control name */
+#define MAX_CONTROL_NAME		(48)

This should be coming from the ALSA headers.

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

Quoted 3 lines
sound/soc/codecs/tas2781-dsp.c new file +3 -0
+{
+	struct tasdevice_priv *tas_dev =
+		(struct tasdevice_priv *)pContext;

The kernel doesn't gennerally use hungarian notation (I see a bunch of it in the driver, n_result and p_tasdevice for example), and if there's a need to cast away from void that usually means something's gone wrong
- it looks unneeded here so you can just remove the cast.

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

Quoted 3 lines
sound/soc/codecs/tas2781-dsp.c new file +3 -0
+	if (config_offset + 4 > (int)config_size) {
+		dev_err(tas_dev->dev,
+			"add config: Out of memory\n");

This isn't OOM though?

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

Quoted 6 lines
sound/soc/codecs/tas2781-dsp.c new file +6 -0
+	cfg_info->blk_data = kcalloc(
+		cfg_info->nblocks, sizeof(struct tasdev_blk_data *),
+		GFP_KERNEL);
+	if (!cfg_info->blk_data) {
+		dev_err(tas_dev->dev,
+			"add config: blk_data alloc failed!\n");

No need to log failures to allocate, the kernel is already pretty loud when it runs out of memory.

sound/soc/codecs/tas2781-dsp.c

Quoted 10 lines
sound/soc/codecs/tas2781-dsp.c new file +9 -0
+static int tas2781_digital_getvol(struct snd_kcontrol *kcontrol,
+       struct snd_ctl_elem_value *ucontrol)
 
+	/* Read the primary device as the whole */
+	ret = tasdevice_dev_read(tas_dev, 0, mc->reg, &val);
+	if (ret) {
+		dev_err(tas_dev->dev,
+		"%s, get digital vol error\n",
+		__func__);
+		goto out;

Your regmap looked like it already had paging support so I'm not clear why this is a custom control - can't you just use the standard ASoC helpers, the regmap paging support will work for paged register maps?

sound/soc/codecs/tas2781-dsp.c

Quoted 3 lines
sound/soc/codecs/tas2781-dsp.c new file +3 -0
+	}
+	val = (val > mc->max) ? mc->max : val;
+	val = mc->invert ? mc->max - val : val;

Please write normal conditional statements, they're easier to read than the ternery opreator.

sound/soc/codecs/tas2781-dsp.c

Quoted 10 lines
sound/soc/codecs/tas2781-dsp.c new file +10 -0
+static int tas2781_digital_putvol(struct snd_kcontrol *kcontrol,
+	struct snd_ctl_elem_value *ucontrol) {
+	struct snd_soc_component *codec
+		= snd_soc_kcontrol_component(kcontrol);
+	struct tasdevice_priv *tas_dev =
+		snd_soc_component_get_drvdata(codec);
+	struct soc_mixer_control *mc =
+		(struct soc_mixer_control *)kcontrol->private_value;
+	int val;
+	int i, ret = 0;

Please run the mixer-test selftest on a card with this device, it'll tell you that this function doesn't return 1 on value changes so doesn't generate events for changes. Similar issues with a lot of other custom controls.

sound/soc/codecs/tas2781-dsp.c

Quoted 7 lines
sound/soc/codecs/tas2781-dsp.c new file +7 -0
+static const struct snd_kcontrol_new tas2781_snd_controls[] = {
+	SOC_SINGLE_RANGE_EXT_TLV("Amp Gain Volume", TAS2781_AMP_LEVEL,
+		1, 0, 20, 0, tas2781_amp_getvol,
+		tas2781_amp_putvol, amp_vol_tlv),
+	SOC_SINGLE_RANGE_EXT_TLV("Digital Volume Control", TAS2781_DVC_LVL,
+		0, 0, 200, 1, tas2781_digital_getvol,
+		tas2781_digital_putvol, dvc_tlv),

Control names for volumes should end with Volume, see control-names.rst.

Quoted 7 lines
sound/soc/codecs/tas2781-dsp.c new file +7 -0
+static int tasdevice_info_programs(struct snd_kcontrol *kcontrol,
+			struct snd_ctl_elem_info *uinfo)
+{
+	struct snd_soc_component *codec
+		= snd_soc_kcontrol_component(kcontrol);
+	struct tasdevice_priv *p_tasdevice =
+		snd_soc_component_get_drvdata(codec);

sound/soc/codecs/tas2781-dsp.c

Quoted 10 lines
sound/soc/codecs/tas2781-dsp.c new file +10 -0
+	struct tasdevice_fw *tas_fw = p_tasdevice->fmw;
+
+	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
+	/* Codec Lock Hold*/
+	mutex_lock(&p_tasdevice->codec_lock);
+	uinfo->count = 1;
+	uinfo->value.integer.min = 0;
+	uinfo->value.integer.max = (int)tas_fw->n_programs;
+	/* Codec Lock Release*/
+	mutex_unlock(&p_tasdevice->codec_lock);

Why does this need a lock - what might be changing? The casting is suspicious too.

sound/soc/codecs/tas2781-dsp.c

Quoted 8 lines
sound/soc/codecs/tas2781-dsp.c new file +7 -0
+static void tasdevice_rca_ready(const struct firmware *fmw,
+	void *context)
+{
 
+	mutex_lock(&tas_dev->codec_lock);
+	rca = &(tas_dev->rcabin);
+	fw_hdr = &(rca->fw_hdr);
+	if (unlikely(!fmw) || unlikely(!fmw->data)) {

If you're not in a *very* hot path it's seldom useful to annotate with likely() or unlikely().

sound/soc/codecs/tas2781-dsp.c

Quoted 8 lines
sound/soc/codecs/tas2781-dsp.c new file +8 -0
+static void tasdevice_enable_irq(
+	struct tasdevice_priv *tas_dev, bool enable) {
+	if (enable) {
+		if (tas_dev->irq_info.is_irq_enable)
+			return;
+		if (gpio_is_valid(tas_dev->irq_info.irq_gpio))
+			enable_irq(tas_dev->irq_info.irq);
+		tas_dev->irq_info.is_irq_enable = true;

The driver shouldn't need to care if the interrupt is implemented with something that can also be a GPIO, and indeed I can't see anything that actually looks at the GPIO other than these checks. The check for double enalbes also seems suspicious, I don't think it's needed but...

sound/soc/codecs/tas2781-dsp.c

Quoted 3 lines
sound/soc/codecs/tas2781-dsp.c new file +3 -0
+	} else {
+		if (gpio_is_valid(tas_dev->irq_info.irq_gpio))
+			disable_irq_nosync(tas_dev->irq_info.irq);

...it's certainly not matched in the disable path. In general it looks like this function is just redundant and could be replaced by enable_irq()/disable_irq() calls in the callers, though I'm not sure it's needed at all.

Quoted 7 lines
sound/soc/codecs/tas2781-dsp.c new file +7 -0
+static void tasdevice_set_power_state(
+	struct tasdevice_priv *tas_dev, int state) {
+	switch (state) {
+	case 0:
+		schedule_delayed_work(&tas_dev->tuning_switch_wrk,
+			msecs_to_jiffies(20));
+		break;

sound/soc/codecs/tas2781-dsp.c

Quoted 10 lines
sound/soc/codecs/tas2781-dsp.c new file +10 -0
+	default:
+		if (!(tas_dev->pstream || tas_dev->cstream)) {
+			if (tas_dev->irq_work_func)
+				tasdevice_enable_irq(tas_dev, false);
+			tasdevice_select_cfg_blk(tas_dev,
+				tas_dev->rcabin.profile_cfg_id,
+				TASDEVICE_BIN_BLK_PRE_SHUTDOWN);
+		}
+		break;
+	}

It's really not clear what thisis supposed to do - what are the two power states here?

Quoted 4 lines
sound/soc/codecs/tas2781-dsp.c new file +4 -0
+static int tasdevice_dapm_event(struct snd_soc_dapm_widget *w,
+			struct snd_kcontrol *kcontrol, int event) {
+	struct snd_soc_component *codec = snd_soc_dapm_to_component(w->dapm);
+	struct tasdevice_priv *tas_dev = snd_soc_component_get_drvdata(codec);

sound/soc/codecs/tas2781-dsp.c

Quoted 10 lines
sound/soc/codecs/tas2781-dsp.c new file +10 -0
+	int stream;
+
+	/* Codec Lock Hold */
+	mutex_lock(&tas_dev->codec_lock);
+	stream = strcmp(w->name, "SPK") ? SNDRV_PCM_STREAM_PLAYBACK :
+		SNDRV_PCM_STREAM_CAPTURE;
+
+	switch (event) {
+	case SND_SOC_DAPM_POST_PMU:
+		dev_info(tas_dev->dev, "SND_SOC_DAPM_POST_PMU\n");

dev_info() on DAPM events is going to be way too noisy, there's a lot of logging in the core so you can probably just remove this.

sound/soc/codecs/tas2781-dsp.c

Quoted 11 lines
sound/soc/codecs/tas2781-dsp.c new file +11 -0
+	case SND_SOC_DAPM_PRE_PMD:
+		dev_info(tas_dev->dev, "SND_SOC_DAPM_PRE_PMD\n");
+		/* stop DSP only when both playback and capture streams
+		 * are deactivated
+		 */
+		if (stream == SNDRV_PCM_STREAM_PLAYBACK)
+			tas_dev->pstream = 0;
+		else
+			tas_dev->cstream = 0;
+		if (tas_dev->pstream != 0 || tas_dev->cstream != 0)
+			goto out;

It would probably be easier to have a supply widget that feeds both the ASI and speaker widgets, that way DAPM will reference count for you and the driver can just handle the event without these checks.

sound/soc/codecs/tas2781-dsp.c

Quoted 11 lines
sound/soc/codecs/tas2781-dsp.c new file +11 -0
+static int tasdevice_hw_params(struct snd_pcm_substream *substream,
+	struct snd_pcm_hw_params *params, struct snd_soc_dai *dai) {
+	struct tasdevice_priv *tas_dev = snd_soc_dai_get_drvdata(dai);
+	unsigned int fsrate;
+	unsigned int slot_width;
+	int bclk_rate;
+	int rc = 0;
+
+	dev_info(tas_dev->dev, "%s: %s\n",
+		__func__, substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
+		"Playback":"Capture");

Again, too much logging. The driver should be silent during normal operation - some logging on boot or error is fine but userspace shouldn't be able to spam the logs and hide other information.

sound/soc/codecs/tas2781-dsp.c

Quoted 11 lines
sound/soc/codecs/tas2781-dsp.c new file +11 -0
+static int tasdevice_set_dai_sysclk(struct snd_soc_dai *codec_dai,
+	int clk_id, unsigned int freq, int dir) {
+	struct tasdevice_priv *tas_dev = snd_soc_dai_get_drvdata(codec_dai);
+
+	dev_info(tas_dev->dev,
+		"%s: clk_id = %d, freq = %u, CLK direction %s\n",
+		__func__, clk_id, freq,
+		dir == TASDEVICE_CLK_DIR_OUT ? "OUT":"IN");
+
+	return 0;
+}

You can just remove this, it doesn't do anything except log.

sound/soc/codecs/tas2781-dsp.c

Quoted 3 lines
sound/soc/codecs/tas2781-dsp.c new file +3 -0
+static void tasdevice_codec_remove(
+	struct snd_soc_component *codec)
+{

Weird indentation here.

Quoted 9 lines
sound/soc/codecs/tas2781-dsp.c new file +9 -0
+static void irq_work_routine(struct work_struct *pWork) {
+	struct tasdevice_priv *tas_dev =
+		container_of(pWork, struct tasdevice_priv,
+		irq_info.irq_work.work);
+
+	mutex_lock(&tas_dev->codec_lock);
+	if (tas_dev->runtime_suspend) {
+		dev_info(tas_dev->dev, "%s, Runtime Suspended\n", __func__);
+		goto end;

sound/soc/codecs/tas2781-dsp.c

Quoted 10 lines
sound/soc/codecs/tas2781-dsp.c new file +10 -0
+	}
+	/*Logical Layer IRQ function, return is ignored*/
+	if (tas_dev->irq_work_func)
+		tas_dev->irq_work_func(tas_dev);
+	else
+		dev_info(tas_dev->dev,
+			"%s, irq_work_func is NULL\n", __func__);
+end:
+	mutex_unlock(&tas_dev->codec_lock);
+}

There is only one function that's ever assigned for the irq_work_func, just inline it and don't bother with the pointer in the driver structure - that should be a lot simpler.

sound/soc/codecs/tas2781-dsp.c

Quoted 12 lines
sound/soc/codecs/tas2781-dsp.c new file +12 -0
+static void tas2781_irq_work_func(struct tasdevice_priv *tas_dev) {
+	int rc = 0;
+	unsigned int reg_val = 0, array_size = 0, i = 0, ndev = 0;
+	unsigned int int_reg_array[] = {
+		TAS2781_REG_INT_LTCH0,
+		TAS2781_REG_INT_LTCH1,
+		TAS2781_REG_INT_LTCH1_0,
+		TAS2781_REG_INT_LTCH2,
+		TAS2781_REG_INT_LTCH3,
+		TAS2781_REG_INT_LTCH4};
+
+	tasdevice_enable_irq(tas_dev, false);

It needs to be a lot clearer what will reenable the interrupt - some comments would help, I see there's some interlock with tuning_switch_routine() but I'm not clear what that does...

Quoted 7 lines
sound/soc/codecs/tas2781-dsp.c new file +7 -0
+	array_size = ARRAY_SIZE(int_reg_array);
+
+	for (ndev = 0; ndev < tas_dev->ndev; ndev++) {
+		for (i = 0; i < array_size; i++) {
+			rc = tasdevice_dev_read(tas_dev,
+				ndev, int_reg_array[i], &reg_val);
+			if (!rc)

sound/soc/codecs/tas2781-dsp.c

Quoted 10 lines
sound/soc/codecs/tas2781-dsp.c new file +10 -0
+				dev_info(tas_dev->dev,
+					"INT STATUS REG 0x%04x=0x%02x\n",
+					int_reg_array[i], reg_val);
+			else
+				dev_err(tas_dev->dev,
+					"Read Reg 0x%04x error(rc=%d)\n",
+					int_reg_array[i], rc);
+		}
+	}
+}

...and this seems to just read the interrupt status registers and log the results so do we even need to enable the interrupt?

sound/soc/codecs/tas2781-dsp.c

Quoted 5 lines
sound/soc/codecs/tas2781-dsp.c new file +5 -0
+static int tasdevice_parse_dt(struct tasdevice_priv *tas_dev) {
+	struct device_node *np = tas_dev->dev->of_node;
+	int rc = 0, i = 0, ndev = 0;
+	unsigned int dev_addrs[max_chn];
+	struct i2c_client *client = (struct i2c_client *)tas_dev->client;

If you're adding DT bindings you need to send a DT bindings document.

sound/soc/codecs/tas2781-dsp.c

Quoted 3 lines
sound/soc/codecs/tas2781-dsp.c new file +3 -0
+static int tasdevice_change_chn_book(struct tasdevice_priv *tas_dev,
+	enum channel chn, int book)
+{

As I said earlier the regmap uses ranges so I'm not clear why there's open coding of page changing?

sound/soc/codecs/tas2781-dsp.c

Quoted 3 lines
sound/soc/codecs/tas2781-dsp.c new file +3 -0
+static void tas2781_set_global_mode(struct tasdevice_priv *tas_dev) {
+	int i = 0;
+	int ret = 0;

What's all this global mode stuff about? It's not clear from the code.

sound/soc/codecs/tas2781-dsp.c

Quoted 10 lines
sound/soc/codecs/tas2781-dsp.c new file +10 -0
+	mutex_init(&tas_dev->codec_lock);
+	ret = devm_snd_soc_register_component(tas_dev->dev,
+		&soc_codec_driver_tasdevice,
+		tasdevice_dai_driver, ARRAY_SIZE(tasdevice_dai_driver));
+	if (ret)
+		dev_err(tas_dev->dev, "%s: codec register error:0x%08x\n",
+			__func__, ret);
+
+	INIT_DELAYED_WORK(&tas_dev->irq_info.irq_work, irq_work_routine);
+	tas_dev->irq_info.is_irq_enable = false;

Shouldn't this happen before we register with the core?

sound/soc/codecs/tas2781-dsp.c

Quoted 12 lines
sound/soc/codecs/tas2781-dsp.c new file +12 -0
+static int tasdevice_pm_suspend(struct device *dev) {
+	struct tasdevice_priv *tas_dev = dev_get_drvdata(dev);
+
+	mutex_lock(&tas_dev->codec_lock);
+	tas_dev->runtime_suspend = true;
+
+	if (tas_dev->irq_work_func) {
+		if (delayed_work_pending(&tas_dev->irq_info.irq_work)) {
+			dev_dbg(tas_dev->dev, "cancel IRQ work\n");
+			cancel_delayed_work_sync(&tas_dev->irq_info.irq_work);
+		}
+	}

Just cancelling the work unconditionally should do the right thing, though given that there's no configuration done for the device it's unclear why we're implementing runtime suspend at all?

sound/soc/codecs/tas2781-dsp.c

Quoted 12 lines
sound/soc/codecs/tas2781-dsp.c new file +12 -0
+static int tasdevice_i2c_probe(struct i2c_client *i2c,
+	const struct i2c_device_id *id)
+{
+	struct tasdevice_priv *tas_dev = NULL;
+	int ret = 0;
+
+	if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_I2C)) {
+		dev_err(&i2c->dev,
+			"%s: I2C check failed\n", __func__);
+		ret = -ENODEV;
+		goto out;
+	}

Why the functionality check?

#3 lore raw
In reply to Mark Brown

Hi Brown
Thanks for your comments. You will rework and upstream immediately.
DT document has been already prepared, once the code has been upstream successfully, DT document will be done soon.

As to the comment on global mode, the mode is used for downloading the dsp firmware in multiple devices cases, all the devices can share the same generic i2c address in order to download the firmware simultaneously to save downloading firmware time consumption. I will add the comment for this part in the code.

As to the comment on page changing, this code only handles book changing, no extra code for page changing, all the page changings are handled automatically by the kernel system.

-----Original Message-----
From: Mark Brown <[email protected]>
Sent: Wednesday, March 8, 2023 4:48 AM
To: Kevin Lu <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; Lu, Kevin <[email protected]>; Ding, Shenghao <[email protected]>; [email protected]; [email protected]
Subject: [EXTERNAL] Re: [PATCH v3] ASoC: tas2781: Add tas2781 driver

Quoted 1 line · Kevin Lu

Create tas2781 driver.

Quite a few comments below, a lot of them are fairly simple stylistic ones rather than substantial things but there are a few bigger things.
A lot of the bigger comments are essentially that the driver is doing more work than it needs to be and could either stop doing things that seem redundant or use generic functionality.

The bulk of the code looks good though.

Quoted 10 lines

---
Changes in v3:
- fixed issues reported-by: kernel test robot <[email protected]>
- correct all the inconsistent indenting warnings
- correct uninitialized symbol 'pwr_state'
- correct unsigned 'val' is never less than zero
- correct 'tas_dev' could be null

Your signoff needs to go before the ---, tooling will remove anything after the --- but your signoff needs to stay.

Quoted 5 lines

snd-soc-tas2562-objs := tas2562.o
snd-soc-tas2764-objs := tas2764.o
snd-soc-tas2780-objs := tas2780.o
+snd-soc-tas2781-objs := tas2781-i2c.o tas2781-dsp.o
# Mux

Please keep the blank line in the file.

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

Quoted 7 lines
sound/soc/codecs/tas2781-dsp.c new file +4 -0
@@ -0,0 +1,2261 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * ALSA SoC Texas Instruments TAS2781 Audio Smart Amplifier
+ *

Please make the entire comment block a C++ one so things look more intentional.

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

Quoted 3 lines
sound/soc/codecs/tas2781-dsp.c new file +3 -0
+static const unsigned char crc8_lookup_table[CRC8_TABLE_SIZE] = {
+	0x00, 0x4D, 0x9A, 0xD7, 0x79, 0x34, 0xE3, 0xAE,
+	0xF2, 0xBF, 0x68, 0x25, 0x8B, 0xC6, 0x11, 0x5C,

Should the driver be using one of the crc8_populate_() functions here?

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

Quoted 7 lines
sound/soc/codecs/tas2781-dsp.c new file +7 -0
+	ret = check_yram(&crc_data, book, page, reg, len);
+	dev_info(tasdevice->dev,
+		"isYRAM: Bk 0x%02x, Pg 0x%02x, Rg 0x%02x\n",
+		book, page, reg);
+	dev_info(tasdevice->dev,
+		"isYRAM: crc_data.mnLen 0x%x, len 0x%x, nResult %d\n",
+		crc_data.len, len, ret);

There's a lot of logging in this firmware load code which should at least be reduced to dbg level if not vdbg, it's pretty loud and it looks like at least some of it can be triggered after initial boot.

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

Quoted 2 lines
sound/soc/codecs/tas2781-dsp.c new file +2 -0
+/* max. length of the alsa mixer control name */
+#define MAX_CONTROL_NAME		(48)

This should be coming from the ALSA headers.

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

Quoted 3 lines
sound/soc/codecs/tas2781-dsp.c new file +3 -0
+{
+	struct tasdevice_priv *tas_dev =
+		(struct tasdevice_priv *)pContext;

The kernel doesn't gennerally use hungarian notation (I see a bunch of it in the driver, n_result and p_tasdevice for example), and if there's a need to cast away from void that usually means something's gone wrong
- it looks unneeded here so you can just remove the cast.

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

Quoted 3 lines
sound/soc/codecs/tas2781-dsp.c new file +3 -0
+	if (config_offset + 4 > (int)config_size) {
+		dev_err(tas_dev->dev,
+			"add config: Out of memory\n");

This isn't OOM though?

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

Quoted 6 lines
sound/soc/codecs/tas2781-dsp.c new file +6 -0
+	cfg_info->blk_data = kcalloc(
+		cfg_info->nblocks, sizeof(struct tasdev_blk_data *),
+		GFP_KERNEL);
+	if (!cfg_info->blk_data) {
+		dev_err(tas_dev->dev,
+			"add config: blk_data alloc failed!\n");

No need to log failures to allocate, the kernel is already pretty loud when it runs out of memory.

sound/soc/codecs/tas2781-dsp.c

Quoted 10 lines
sound/soc/codecs/tas2781-dsp.c new file +9 -0
+static int tas2781_digital_getvol(struct snd_kcontrol *kcontrol,
+       struct snd_ctl_elem_value *ucontrol)
 
+	/* Read the primary device as the whole */
+	ret = tasdevice_dev_read(tas_dev, 0, mc->reg, &val);
+	if (ret) {
+		dev_err(tas_dev->dev,
+		"%s, get digital vol error\n",
+		__func__);
+		goto out;

Your regmap looked like it already had paging support so I'm not clear why this is a custom control - can't you just use the standard ASoC helpers, the regmap paging support will work for paged register maps?

sound/soc/codecs/tas2781-dsp.c

Quoted 3 lines
sound/soc/codecs/tas2781-dsp.c new file +3 -0
+	}
+	val = (val > mc->max) ? mc->max : val;
+	val = mc->invert ? mc->max - val : val;

Please write normal conditional statements, they're easier to read than the ternery opreator.

sound/soc/codecs/tas2781-dsp.c

Quoted 10 lines
sound/soc/codecs/tas2781-dsp.c new file +10 -0
+static int tas2781_digital_putvol(struct snd_kcontrol *kcontrol,
+	struct snd_ctl_elem_value *ucontrol) {
+	struct snd_soc_component *codec
+		= snd_soc_kcontrol_component(kcontrol);
+	struct tasdevice_priv *tas_dev =
+		snd_soc_component_get_drvdata(codec);
+	struct soc_mixer_control *mc =
+		(struct soc_mixer_control *)kcontrol->private_value;
+	int val;
+	int i, ret = 0;

Please run the mixer-test selftest on a card with this device, it'll tell you that this function doesn't return 1 on value changes so doesn't generate events for changes. Similar issues with a lot of other custom controls.

sound/soc/codecs/tas2781-dsp.c

Quoted 7 lines
sound/soc/codecs/tas2781-dsp.c new file +7 -0
+static const struct snd_kcontrol_new tas2781_snd_controls[] = {
+	SOC_SINGLE_RANGE_EXT_TLV("Amp Gain Volume", TAS2781_AMP_LEVEL,
+		1, 0, 20, 0, tas2781_amp_getvol,
+		tas2781_amp_putvol, amp_vol_tlv),
+	SOC_SINGLE_RANGE_EXT_TLV("Digital Volume Control", TAS2781_DVC_LVL,
+		0, 0, 200, 1, tas2781_digital_getvol,
+		tas2781_digital_putvol, dvc_tlv),

Control names for volumes should end with Volume, see control-names.rst.

Quoted 7 lines
sound/soc/codecs/tas2781-dsp.c new file +7 -0
+static int tasdevice_info_programs(struct snd_kcontrol *kcontrol,
+			struct snd_ctl_elem_info *uinfo)
+{
+	struct snd_soc_component *codec
+		= snd_soc_kcontrol_component(kcontrol);
+	struct tasdevice_priv *p_tasdevice =
+		snd_soc_component_get_drvdata(codec);

sound/soc/codecs/tas2781-dsp.c

Quoted 10 lines
sound/soc/codecs/tas2781-dsp.c new file +10 -0
+	struct tasdevice_fw *tas_fw = p_tasdevice->fmw;
+
+	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
+	/* Codec Lock Hold*/
+	mutex_lock(&p_tasdevice->codec_lock);
+	uinfo->count = 1;
+	uinfo->value.integer.min = 0;
+	uinfo->value.integer.max = (int)tas_fw->n_programs;
+	/* Codec Lock Release*/
+	mutex_unlock(&p_tasdevice->codec_lock);

Why does this need a lock - what might be changing? The casting is suspicious too.

sound/soc/codecs/tas2781-dsp.c

Quoted 8 lines
sound/soc/codecs/tas2781-dsp.c new file +7 -0
+static void tasdevice_rca_ready(const struct firmware *fmw,
+	void *context)
+{
 
+	mutex_lock(&tas_dev->codec_lock);
+	rca = &(tas_dev->rcabin);
+	fw_hdr = &(rca->fw_hdr);
+	if (unlikely(!fmw) || unlikely(!fmw->data)) {

If you're not in a *very* hot path it's seldom useful to annotate with likely() or unlikely().

sound/soc/codecs/tas2781-dsp.c

Quoted 8 lines
sound/soc/codecs/tas2781-dsp.c new file +8 -0
+static void tasdevice_enable_irq(
+	struct tasdevice_priv *tas_dev, bool enable) {
+	if (enable) {
+		if (tas_dev->irq_info.is_irq_enable)
+			return;
+		if (gpio_is_valid(tas_dev->irq_info.irq_gpio))
+			enable_irq(tas_dev->irq_info.irq);
+		tas_dev->irq_info.is_irq_enable = true;

The driver shouldn't need to care if the interrupt is implemented with something that can also be a GPIO, and indeed I can't see anything that actually looks at the GPIO other than these checks. The check for double enalbes also seems suspicious, I don't think it's needed but...

sound/soc/codecs/tas2781-dsp.c

Quoted 3 lines
sound/soc/codecs/tas2781-dsp.c new file +3 -0
+	} else {
+		if (gpio_is_valid(tas_dev->irq_info.irq_gpio))
+			disable_irq_nosync(tas_dev->irq_info.irq);

...it's certainly not matched in the disable path. In general it looks like this function is just redundant and could be replaced by enable_irq()/disable_irq() calls in the callers, though I'm not sure it's needed at all.

Quoted 7 lines
sound/soc/codecs/tas2781-dsp.c new file +7 -0
+static void tasdevice_set_power_state(
+	struct tasdevice_priv *tas_dev, int state) {
+	switch (state) {
+	case 0:
+		schedule_delayed_work(&tas_dev->tuning_switch_wrk,
+			msecs_to_jiffies(20));
+		break;

sound/soc/codecs/tas2781-dsp.c

Quoted 10 lines
sound/soc/codecs/tas2781-dsp.c new file +10 -0
+	default:
+		if (!(tas_dev->pstream || tas_dev->cstream)) {
+			if (tas_dev->irq_work_func)
+				tasdevice_enable_irq(tas_dev, false);
+			tasdevice_select_cfg_blk(tas_dev,
+				tas_dev->rcabin.profile_cfg_id,
+				TASDEVICE_BIN_BLK_PRE_SHUTDOWN);
+		}
+		break;
+	}

It's really not clear what thisis supposed to do - what are the two power states here?

Quoted 4 lines
sound/soc/codecs/tas2781-dsp.c new file +4 -0
+static int tasdevice_dapm_event(struct snd_soc_dapm_widget *w,
+			struct snd_kcontrol *kcontrol, int event) {
+	struct snd_soc_component *codec = snd_soc_dapm_to_component(w->dapm);
+	struct tasdevice_priv *tas_dev = snd_soc_component_get_drvdata(codec);

sound/soc/codecs/tas2781-dsp.c

Quoted 10 lines
sound/soc/codecs/tas2781-dsp.c new file +10 -0
+	int stream;
+
+	/* Codec Lock Hold */
+	mutex_lock(&tas_dev->codec_lock);
+	stream = strcmp(w->name, "SPK") ? SNDRV_PCM_STREAM_PLAYBACK :
+		SNDRV_PCM_STREAM_CAPTURE;
+
+	switch (event) {
+	case SND_SOC_DAPM_POST_PMU:
+		dev_info(tas_dev->dev, "SND_SOC_DAPM_POST_PMU\n");

dev_info() on DAPM events is going to be way too noisy, there's a lot of logging in the core so you can probably just remove this.

sound/soc/codecs/tas2781-dsp.c

Quoted 11 lines
sound/soc/codecs/tas2781-dsp.c new file +11 -0
+	case SND_SOC_DAPM_PRE_PMD:
+		dev_info(tas_dev->dev, "SND_SOC_DAPM_PRE_PMD\n");
+		/* stop DSP only when both playback and capture streams
+		 * are deactivated
+		 */
+		if (stream == SNDRV_PCM_STREAM_PLAYBACK)
+			tas_dev->pstream = 0;
+		else
+			tas_dev->cstream = 0;
+		if (tas_dev->pstream != 0 || tas_dev->cstream != 0)
+			goto out;

It would probably be easier to have a supply widget that feeds both the ASI and speaker widgets, that way DAPM will reference count for you and the driver can just handle the event without these checks.

sound/soc/codecs/tas2781-dsp.c

Quoted 11 lines
sound/soc/codecs/tas2781-dsp.c new file +11 -0
+static int tasdevice_hw_params(struct snd_pcm_substream *substream,
+	struct snd_pcm_hw_params *params, struct snd_soc_dai *dai) {
+	struct tasdevice_priv *tas_dev = snd_soc_dai_get_drvdata(dai);
+	unsigned int fsrate;
+	unsigned int slot_width;
+	int bclk_rate;
+	int rc = 0;
+
+	dev_info(tas_dev->dev, "%s: %s\n",
+		__func__, substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
+		"Playback":"Capture");

Again, too much logging. The driver should be silent during normal operation - some logging on boot or error is fine but userspace shouldn't be able to spam the logs and hide other information.

sound/soc/codecs/tas2781-dsp.c

Quoted 11 lines
sound/soc/codecs/tas2781-dsp.c new file +11 -0
+static int tasdevice_set_dai_sysclk(struct snd_soc_dai *codec_dai,
+	int clk_id, unsigned int freq, int dir) {
+	struct tasdevice_priv *tas_dev = snd_soc_dai_get_drvdata(codec_dai);
+
+	dev_info(tas_dev->dev,
+		"%s: clk_id = %d, freq = %u, CLK direction %s\n",
+		__func__, clk_id, freq,
+		dir == TASDEVICE_CLK_DIR_OUT ? "OUT":"IN");
+
+	return 0;
+}

You can just remove this, it doesn't do anything except log.

sound/soc/codecs/tas2781-dsp.c

Quoted 3 lines
sound/soc/codecs/tas2781-dsp.c new file +3 -0
+static void tasdevice_codec_remove(
+	struct snd_soc_component *codec)
+{

Weird indentation here.

Quoted 9 lines
sound/soc/codecs/tas2781-dsp.c new file +9 -0
+static void irq_work_routine(struct work_struct *pWork) {
+	struct tasdevice_priv *tas_dev =
+		container_of(pWork, struct tasdevice_priv,
+		irq_info.irq_work.work);
+
+	mutex_lock(&tas_dev->codec_lock);
+	if (tas_dev->runtime_suspend) {
+		dev_info(tas_dev->dev, "%s, Runtime Suspended\n", __func__);
+		goto end;

sound/soc/codecs/tas2781-dsp.c

Quoted 10 lines
sound/soc/codecs/tas2781-dsp.c new file +10 -0
+	}
+	/*Logical Layer IRQ function, return is ignored*/
+	if (tas_dev->irq_work_func)
+		tas_dev->irq_work_func(tas_dev);
+	else
+		dev_info(tas_dev->dev,
+			"%s, irq_work_func is NULL\n", __func__);
+end:
+	mutex_unlock(&tas_dev->codec_lock);
+}

There is only one function that's ever assigned for the irq_work_func, just inline it and don't bother with the pointer in the driver structure - that should be a lot simpler.

sound/soc/codecs/tas2781-dsp.c

Quoted 12 lines
sound/soc/codecs/tas2781-dsp.c new file +12 -0
+static void tas2781_irq_work_func(struct tasdevice_priv *tas_dev) {
+	int rc = 0;
+	unsigned int reg_val = 0, array_size = 0, i = 0, ndev = 0;
+	unsigned int int_reg_array[] = {
+		TAS2781_REG_INT_LTCH0,
+		TAS2781_REG_INT_LTCH1,
+		TAS2781_REG_INT_LTCH1_0,
+		TAS2781_REG_INT_LTCH2,
+		TAS2781_REG_INT_LTCH3,
+		TAS2781_REG_INT_LTCH4};
+
+	tasdevice_enable_irq(tas_dev, false);

It needs to be a lot clearer what will reenable the interrupt - some comments would help, I see there's some interlock with tuning_switch_routine() but I'm not clear what that does...

Quoted 7 lines
sound/soc/codecs/tas2781-dsp.c new file +7 -0
+	array_size = ARRAY_SIZE(int_reg_array);
+
+	for (ndev = 0; ndev < tas_dev->ndev; ndev++) {
+		for (i = 0; i < array_size; i++) {
+			rc = tasdevice_dev_read(tas_dev,
+				ndev, int_reg_array[i], &reg_val);
+			if (!rc)

sound/soc/codecs/tas2781-dsp.c

Quoted 10 lines
sound/soc/codecs/tas2781-dsp.c new file +10 -0
+				dev_info(tas_dev->dev,
+					"INT STATUS REG 0x%04x=0x%02x\n",
+					int_reg_array[i], reg_val);
+			else
+				dev_err(tas_dev->dev,
+					"Read Reg 0x%04x error(rc=%d)\n",
+					int_reg_array[i], rc);
+		}
+	}
+}

...and this seems to just read the interrupt status registers and log the results so do we even need to enable the interrupt?

sound/soc/codecs/tas2781-dsp.c

Quoted 5 lines
sound/soc/codecs/tas2781-dsp.c new file +5 -0
+static int tasdevice_parse_dt(struct tasdevice_priv *tas_dev) {
+	struct device_node *np = tas_dev->dev->of_node;
+	int rc = 0, i = 0, ndev = 0;
+	unsigned int dev_addrs[max_chn];
+	struct i2c_client *client = (struct i2c_client *)tas_dev->client;

If you're adding DT bindings you need to send a DT bindings document.

sound/soc/codecs/tas2781-dsp.c

Quoted 3 lines
sound/soc/codecs/tas2781-dsp.c new file +3 -0
+static int tasdevice_change_chn_book(struct tasdevice_priv *tas_dev,
+	enum channel chn, int book)
+{

As I said earlier the regmap uses ranges so I'm not clear why there's open coding of page changing?

sound/soc/codecs/tas2781-dsp.c

Quoted 3 lines
sound/soc/codecs/tas2781-dsp.c new file +3 -0
+static void tas2781_set_global_mode(struct tasdevice_priv *tas_dev) {
+	int i = 0;
+	int ret = 0;

What's all this global mode stuff about? It's not clear from the code.

sound/soc/codecs/tas2781-dsp.c

Quoted 10 lines
sound/soc/codecs/tas2781-dsp.c new file +10 -0
+	mutex_init(&tas_dev->codec_lock);
+	ret = devm_snd_soc_register_component(tas_dev->dev,
+		&soc_codec_driver_tasdevice,
+		tasdevice_dai_driver, ARRAY_SIZE(tasdevice_dai_driver));
+	if (ret)
+		dev_err(tas_dev->dev, "%s: codec register error:0x%08x\n",
+			__func__, ret);
+
+	INIT_DELAYED_WORK(&tas_dev->irq_info.irq_work, irq_work_routine);
+	tas_dev->irq_info.is_irq_enable = false;

Shouldn't this happen before we register with the core?

sound/soc/codecs/tas2781-dsp.c

Quoted 12 lines
sound/soc/codecs/tas2781-dsp.c new file +12 -0
+static int tasdevice_pm_suspend(struct device *dev) {
+	struct tasdevice_priv *tas_dev = dev_get_drvdata(dev);
+
+	mutex_lock(&tas_dev->codec_lock);
+	tas_dev->runtime_suspend = true;
+
+	if (tas_dev->irq_work_func) {
+		if (delayed_work_pending(&tas_dev->irq_info.irq_work)) {
+			dev_dbg(tas_dev->dev, "cancel IRQ work\n");
+			cancel_delayed_work_sync(&tas_dev->irq_info.irq_work);
+		}
+	}

Just cancelling the work unconditionally should do the right thing, though given that there's no configuration done for the device it's unclear why we're implementing runtime suspend at all?

sound/soc/codecs/tas2781-dsp.c

Quoted 12 lines
sound/soc/codecs/tas2781-dsp.c new file +12 -0
+static int tasdevice_i2c_probe(struct i2c_client *i2c,
+	const struct i2c_device_id *id)
+{
+	struct tasdevice_priv *tas_dev = NULL;
+	int ret = 0;
+
+	if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_I2C)) {
+		dev_err(&i2c->dev,
+			"%s: I2C check failed\n", __func__);
+		ret = -ENODEV;
+		goto out;
+	}

Why the functionality check?