ASoC: tas2781: read TAS2573 status registers by entry length
Subsystems (3)
Shenghao Ding
maintainerPATCH 1/1 ASoC: tas2781: read TAS2573 status registers by entry length
The TAS2573 status kcontrols read registers through a shared descriptor that assumes the first register is one byte and all others are four bytes. The assumption is position dependent, so a one-byte register can only be described by moving it to the front of the table.
Make the width explicit by replacing the address array with entries that carry their own access width.
---
v1:
- Convert the register descriptor used by the TAS2573 calibration and real-time speaker protection kcontrols from a plain array of register addresses into an explicit table of entries that carry their own access width.
- Two helper macros, TASDEV_REG_ENTRY() and TASDEV_REG_ENTRY_B1(), keep the tables readable for 4-byte and 1-byte entries respectively.
- Now that each register entry knows its access width, drop the implicit "the first register is one byte, all the rest are four bytes" rule in tas2573_status_get_common() and select the access method per entry from val_len.
--- include/sound/tas2573-reg.h | 78 +++++++++++++++++++--------------- sound/soc/codecs/tas2781-i2c.c | 66 +++++++++++++++------------- 2 files changed, 80 insertions(+), 64 deletions(-)
include/sound/tas2573-reg.h +44 -34
@@ -14,16 +14,25 @@#ifndef __TAS2573_REG_H__#define __TAS2573_REG_H__ +/* Descriptor of a single register field: address plus value length. */ +struct tasdev_reg_entry { +const unsigned int reg; +const unsigned char val_len; +}; +/* -* Register map descriptor: binds the register table, its length, and the log -* tag together into a single argument. +* A named group of register fields accessed together. Each entry occupies +* a fixed 4-byte slot in the userspace report, regardless of val_len. */ -struct tasdev_reg_map { -const unsigned int *regs; -unsigned int nregs; +struct tasdev_reg_table { +const struct tasdev_reg_entry *entries; +unsigned int nentries;const char *tag; }; +#define TASDEV_REG_ENTRY (regid) { .reg = (regid), .val_len = 4 } +#define TASDEV_REG_ENTRY_B1 (regid) { .reg = (regid), .val_len = 1 } +#define TAS2573_TDM_DET_REG TASDEVICE_REG(0x00, 0x00, 0x14)#define TAS2573_FCT_INT_LATCH TASDEVICE_REG(0x00, 0x00, 0x67)/* YM57 */
@@ -59,41 +68,42 @@ struct tasdev_reg_map {/* YM955 */ #define TAS2573_SHORTCKT TASDEVICE_REG(0x00, 0x83, 0x6C) -/* Registers for the speaker calibration */ -static const __maybe_unused unsigned int fct_status_regs[] = { -TAS2573_FCT_INT_LATCH , -TAS2573_FCT_STATUS_CTRL , -TAS2573_FCT_STATUS_BINNING , -TAS2573_SILENCE_DETECTED , -TAS2573_OPEN_CIRCUIT , -TAS2573_SHORTCKT , -TAS2573_FCT_OUTPUT_R0 , -TAS2573_FCT_OUTPUT_R0_LOW , -TAS2573_FCT_OUTPUT_INV_R0 , -TAS2573_FCT_OUTPUT_POWERTOT , -TAS2573_FCT_OUTPUT_F0 , -TAS2573_RE_OUT , +/* Register Array for the speaker calibration */ +static const __maybe_unused struct tasdev_reg_entry fct_status_regs[] = { +TASDEV_REG_ENTRY (TAS2573_FCT_INT_LATCH ), +TASDEV_REG_ENTRY (TAS2573_FCT_STATUS_CTRL ), +TASDEV_REG_ENTRY (TAS2573_FCT_STATUS_BINNING ), +TASDEV_REG_ENTRY (TAS2573_SILENCE_DETECTED ), +TASDEV_REG_ENTRY (TAS2573_OPEN_CIRCUIT ), +TASDEV_REG_ENTRY (TAS2573_SHORTCKT ), +TASDEV_REG_ENTRY (TAS2573_FCT_OUTPUT_R0 ), +TASDEV_REG_ENTRY (TAS2573_FCT_OUTPUT_R0_LOW ), +TASDEV_REG_ENTRY (TAS2573_FCT_OUTPUT_INV_R0 ), +TASDEV_REG_ENTRY (TAS2573_FCT_OUTPUT_POWERTOT ), +TASDEV_REG_ENTRY (TAS2573_FCT_OUTPUT_F0 ), +TASDEV_REG_ENTRY (TAS2573_RE_OUT ), }; -static const __maybe_unused struct tasdev_reg_map calib_status_map = { - .regs = fct_status_regs, - .nregs =ARRAY_SIZE (fct_status_regs), +static const __maybe_unused struct tasdev_reg_table calib_status_map = { + .entries = fct_status_regs, + .nentries =ARRAY_SIZE (fct_status_regs), .tag ="fct_status_regs" , }; -/* Registers for the realtime (RT) speaker protection data */ -static const __maybe_unused unsigned int tas2573_rt_data_regs[] = { -TAS2573_TDM_DET_REG , -TAS2573_SPK_COIL_TEMPERATURE , -TAS2573_RE_OUT , -TAS2573_A1_OUT , -TAS2573_A2_OUT , -TAS2573_EXCURSION_GAIN , -TAS2573_THERMAL_GAIN , +/* Register Array for the realtime (RT) speaker protection data */ +static const __maybe_unused struct tasdev_reg_entry tas2573_rt_data_regs[] = { +TASDEV_REG_ENTRY_B1 (TAS2573_TDM_DET_REG ), +TASDEV_REG_ENTRY (TAS2573_SPK_COIL_TEMPERATURE ), +TASDEV_REG_ENTRY (TAS2573_RE_OUT ), +TASDEV_REG_ENTRY (TAS2573_A1_OUT ), +TASDEV_REG_ENTRY (TAS2573_A2_OUT ), +TASDEV_REG_ENTRY (TAS2573_EXCURSION_GAIN ), +TASDEV_REG_ENTRY (TAS2573_THERMAL_GAIN ), }; -static const __maybe_unused struct tasdev_reg_map tas2573_rt_data_map = { - .regs = tas2573_rt_data_regs, - .nregs =ARRAY_SIZE (tas2573_rt_data_regs), + +static const __maybe_unused struct tasdev_reg_table tas2573_rt_data_map = { + .entries = tas2573_rt_data_regs, + .nentries =ARRAY_SIZE (tas2573_rt_data_regs), .tag ="tas2573_rt_data_regs" , };#endif
sound/soc/codecs/tas2781-i2c.c +36 -30
99 lines not on this page · raw message
Signature · 1 line
2.53.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] ASoC: tas2781: TAS2573_FCT_INT_LATCH uses 4-byte entry in fct_status_regs - [Medium] ASoC…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] ASoC: tas2781: TAS2573_FCT_INT_LATCH uses 4-byte entry in fct_status_regs
- [Medium] ASoC: tas2781: wrong 1-byte value offset in tas2573_status_get_common()
--
commit 01ec8dd477ee424fc19c367d085a8a14cf238238
Author: Shenghao Ding <[email protected]>
ASoC: tas2781: read TAS2573 status registers by entry length
This commit replaces the TAS2573 status register address arrays with struct tasdev_reg_entry descriptors that carry an explicit val_len field, and updates tas2573_status_get_common() to choose single-byte or 4-byte bulk reads per entry.
Quoted 46 lines
include/sound/tas2573-reg.h +14
-12@@ -14,16 +14,25 @@#ifndef __TAS2573_REG_H__#define __TAS2573_REG_H__ +/* Descriptor of a single register field: address plus value length. */ +struct tasdev_reg_entry { +const unsigned int reg; +const unsigned char val_len; +}; +/* -* Register map descriptor: binds the register table, its length, and the log -* tag together into a single argument. +* A named group of register fields accessed together. Each entry occupies +* a fixed 4-byte slot in the userspace report, regardless of val_len. */ -struct tasdev_reg_map { -const unsigned int *regs; -unsigned int nregs; +struct tasdev_reg_table { +const struct tasdev_reg_entry *entries; +unsigned int nentries;const char *tag; }; +#define TASDEV_REG_ENTRY (regid) { .reg = (regid), .val_len = 4 } +#define TASDEV_REG_ENTRY_B1 (regid) { .reg = (regid), .val_len = 1 } +#define TAS2573_TDM_DET_REG TASDEVICE_REG(0x00, 0x00, 0x14)#define TAS2573_FCT_INT_LATCH TASDEVICE_REG(0x00, 0x00, 0x67)/* YM57 */ @@ -59,41 +68,42 @@ struct tasdev_reg_map {/* YM955 */ #define TAS2573_SHORTCKT TASDEVICE_REG(0x00, 0x83, 0x6C) -/* Registers for the speaker calibration */ -static const __maybe_unused unsigned int fct_status_regs[] = { -TAS2573_FCT_INT_LATCH , -TAS2573_FCT_STATUS_CTRL , -TAS2573_FCT_STATUS_BINNING , -TAS2573_SILENCE_DETECTED , -TAS2573_OPEN_CIRCUIT ,
include/sound/tas2573-reg.h struct tasdev_reg_map { · line 73
Quoted 10 lines
include/sound/tas2573-reg.h +3
-7-TAS2573_SHORTCKT , -TAS2573_FCT_OUTPUT_R0 , -TAS2573_FCT_OUTPUT_R0_LOW , -TAS2573_FCT_OUTPUT_INV_R0 , -TAS2573_FCT_OUTPUT_POWERTOT , -TAS2573_FCT_OUTPUT_F0 , - TAS2573_RE_OUT, +/* Register Array for the speaker calibration */ +static const __maybe_unused struct tasdev_reg_entry fct_status_regs[] = { +TASDEV_REG_ENTRY (TAS2573_FCT_INT_LATCH ),
[Severity: Medium]
Should TAS2573_FCT_INT_LATCH use TASDEV_REG_ENTRY_B1() here?
Before this patch, tas2573_status_get_common() read fct_status_regs[0] as a 1-byte register via tasdevice_dev_read(). With TASDEV_REG_ENTRY(), val_len is 4, so reading the "Calibration Status" control will perform a 4-byte bulk read starting at Book 0, Page 0, Register 0x67 and include three adjacent Page 0 registers in the slot.
[ ... ]
Quoted 5 lines
sound/soc/codecs/tas2781-i2c.c +0
-0@@ -1565,40 +1566,45 @@ static int tas2573_status_get_common(struct snd_kcontrol *kcontrol,
[ ... ]
Quoted 29 lines
sound/soc/codecs/tas2781-i2c.c +8
-19for (j =0 ; j < priv->ndev; j++) { dst[i++] = j; - dst[i++] =0 ; - dst[i++] =0 ; - dst[i++] =0 ; - - rc =tasdevice_dev_read (priv, j, map->regs[0 ], &val); -if (rc <0 ) -dev_err (priv->dev, -"chn %d %s[0] rd err = %d\n" , - j, map->tag, rc); -else - dst[i++] = val; - -for (k =1 ; k < map->nregs; k++, i +=4 ) { - rc =tasdevice_dev_bulk_read (priv, j, map->regs[k], - &dst[i],4 ); -if (rc <0 ) -dev_err (priv->dev, -"chn %d %s[%u] bulk_rd err = %d\n" , - j, map->tag, k, rc); +for (k =0 ; k < map->nentries; k++, i +=4 ) { + dst[i] =0 ; + dst[i +1 ] =0 ; + dst[i +2 ] =0 ; + dst[i +3 ] =0 ; + +if (map->entries[k].val_len ==4 ) { + rc = tasdevice_dev_bulk_read(priv, j,
sound/soc/codecs/tas2781-i2c.c static int tas2573_status_get_common(struct snd_kcontrol *kcontrol, · line 1602
Quoted 10 lines
sound/soc/codecs/tas2781-i2c.c +10
-0+ map->entries[k].reg, &dst[i], 4); +if (rc <0 ) +dev_err (priv->dev, +"chn %d %s[%u] bulk_read err = %d\n" , + j, map->tag, k, rc); + }else { + rc =tasdevice_dev_read (priv, j, + map->entries[k].reg, &val); +if (!rc) + dst[i] = val;
[Severity: Medium]
Does writing dst[i] = val place 1-byte register values at the wrong offset in the 4-byte slot?
Previously, i was incremented three times while zeroing the leading bytes before writing dst[i++] = val, placing the byte at offset +3 ({0, 0, 0, val}). Since i is now advanced by 4 in the loop header, dst[i] = val stores the value at offset +0 ({val, 0, 0, 0}) instead of dst[i + 3], changing the report layout seen by userspace.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Mark Brown
maintainerinclude/sound/tas2573-reg.h struct tasdev_reg_map { · line 73
Quoted 8 lines
The TAS2573 status kcontrols read registers through a shared descriptor that assumes the first register is one byte and all others are four bytes. The assumption is position dependent, so a one-byte register can only be described by moving it to the front of the table.
include/sound/tas2573-reg.h +3
-0+/* Register Array for the speaker calibration */ +static const __maybe_unused struct tasdev_reg_entry fct_status_regs[] = { +TASDEV_REG_ENTRY (TAS2573_FCT_INT_LATCH ),
Sashiko suggest sthis should be TASDEV_REG_ENTRY_B1()? There was also another thing that might be worth checking out.
Signature · 1 line
Mark Brown <[email protected]>