diff mbox series

[v2,1/4] ALSA: hda/cirrus: Add error handling into CS8409 I2C functions

Message ID 20210315165706.3629-2-vitalyr@opensource.cirrus.com (mailing list archive)
State Superseded
Headers show
Series ALSA: hda/cirrus: Make CS8409 driver more generic by using fixups | expand

Commit Message

Vitaly Rodionov March 15, 2021, 4:57 p.m. UTC
From: Stefan Binding <sbinding@opensource.cirrus.com>

Also removing 2 redundant cs8409_i2c_read() calls, as we already did read
them in a code above.

Tested on DELL Inspiron-3505, DELL Inspiron-3501, DELL Inspiron-3500

Signed-off-by: Stefan Binding <sbinding@opensource.cirrus.com>
Signed-off-by: Vitaly Rodionov <vitalyr@opensource.cirrus.com>

Changes in v1:
- No changes

Changes in v2:
- Chanaged commit message to describe removal of 2 cs8409_i2c_read()s.
- Added comments for i2c_read/write functions
- Removed redundant parentheses

---
 sound/pci/hda/patch_cirrus.c | 136 +++++++++++++++++++++++------------
 1 file changed, 91 insertions(+), 45 deletions(-)

Comments

kernel test robot March 15, 2021, 6:29 p.m. UTC | #1
Hi Vitaly,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on sound/for-next]
[also build test WARNING on next-20210315]
[cannot apply to v5.12-rc3]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Vitaly-Rodionov/ALSA-hda-cirrus-Make-CS8409-driver-more-generic-by-using-fixups/20210316-010109
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound.git for-next
config: i386-randconfig-s002-20210315 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0
reproduce:
        # apt-get install sparse
        # sparse version: v0.6.3-277-gc089cd2d-dirty
        # https://github.com/0day-ci/linux/commit/649ba4016100c8793c314c9dd5213ea910a70ffd
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Vitaly-Rodionov/ALSA-hda-cirrus-Make-CS8409-driver-more-generic-by-using-fixups/20210316-010109
        git checkout 649ba4016100c8793c314c9dd5213ea910a70ffd
        # save the attached .config to linux build tree
        make W=1 C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' ARCH=i386 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

>> sound/pci/hda/patch_cirrus.c:1600: warning: expecting prototype for cs8409_i2c_read(). Prototype was for cs8409_i2c_write() instead


vim +1600 sound/pci/hda/patch_cirrus.c

6cc7e93f46a5ce Vitaly Rodionov 2021-03-06  1584  
649ba4016100c8 Stefan Binding  2021-03-15  1585  /**
649ba4016100c8 Stefan Binding  2021-03-15  1586   * cs8409_i2c_read - CS8409 I2C Write.
649ba4016100c8 Stefan Binding  2021-03-15  1587   * @codec: the codec instance
649ba4016100c8 Stefan Binding  2021-03-15  1588   * @i2c_address: I2C Address
649ba4016100c8 Stefan Binding  2021-03-15  1589   * @i2c_reg: Register to write to
649ba4016100c8 Stefan Binding  2021-03-15  1590   * @i2c_data: Data to write
649ba4016100c8 Stefan Binding  2021-03-15  1591   * @paged: Is a paged transaction
649ba4016100c8 Stefan Binding  2021-03-15  1592   *
649ba4016100c8 Stefan Binding  2021-03-15  1593   * CS8409 I2C Write.
649ba4016100c8 Stefan Binding  2021-03-15  1594   * Returns negative on error, otherwise returns 0.
649ba4016100c8 Stefan Binding  2021-03-15  1595   */
649ba4016100c8 Stefan Binding  2021-03-15  1596  static int cs8409_i2c_write(struct hda_codec *codec,
6cc7e93f46a5ce Vitaly Rodionov 2021-03-06  1597  		unsigned int i2c_address, unsigned int i2c_reg,
6cc7e93f46a5ce Vitaly Rodionov 2021-03-06  1598  		unsigned int i2c_data,
6cc7e93f46a5ce Vitaly Rodionov 2021-03-06  1599  		unsigned int paged)
6cc7e93f46a5ce Vitaly Rodionov 2021-03-06 @1600  {
649ba4016100c8 Stefan Binding  2021-03-15  1601  	unsigned int i2c_reg_data;
6cc7e93f46a5ce Vitaly Rodionov 2021-03-06  1602  
6cc7e93f46a5ce Vitaly Rodionov 2021-03-06  1603  	cs8409_enable_i2c_clock(codec, 1);
6cc7e93f46a5ce Vitaly Rodionov 2021-03-06  1604  	cs_vendor_coef_set(codec, CIR_I2C_ADDR, i2c_address);
6cc7e93f46a5ce Vitaly Rodionov 2021-03-06  1605  
6cc7e93f46a5ce Vitaly Rodionov 2021-03-06  1606  	if (paged) {
6cc7e93f46a5ce Vitaly Rodionov 2021-03-06  1607  		cs_vendor_coef_set(codec, CIR_I2C_QWRITE, i2c_reg >> 8);
649ba4016100c8 Stefan Binding  2021-03-15  1608  		if (cs8409_i2c_wait_complete(codec) < 0) {
6cc7e93f46a5ce Vitaly Rodionov 2021-03-06  1609  			codec_err(codec,
649ba4016100c8 Stefan Binding  2021-03-15  1610  				"%s() Paged Transaction Failed 0x%02x : 0x%04x\n",
649ba4016100c8 Stefan Binding  2021-03-15  1611  				__func__, i2c_address, i2c_reg);
649ba4016100c8 Stefan Binding  2021-03-15  1612  			return -EIO;
6cc7e93f46a5ce Vitaly Rodionov 2021-03-06  1613  		}
6cc7e93f46a5ce Vitaly Rodionov 2021-03-06  1614  	}
6cc7e93f46a5ce Vitaly Rodionov 2021-03-06  1615  
6cc7e93f46a5ce Vitaly Rodionov 2021-03-06  1616  	i2c_reg_data = ((i2c_reg << 8) & 0x0ff00) | (i2c_data & 0x0ff);
6cc7e93f46a5ce Vitaly Rodionov 2021-03-06  1617  	cs_vendor_coef_set(codec, CIR_I2C_QWRITE, i2c_reg_data);
6cc7e93f46a5ce Vitaly Rodionov 2021-03-06  1618  
649ba4016100c8 Stefan Binding  2021-03-15  1619  	if (cs8409_i2c_wait_complete(codec) < 0) {
649ba4016100c8 Stefan Binding  2021-03-15  1620  		codec_err(codec, "%s() Transaction Failed 0x%02x : 0x%04x\n",
649ba4016100c8 Stefan Binding  2021-03-15  1621  			__func__, i2c_address, i2c_reg);
649ba4016100c8 Stefan Binding  2021-03-15  1622  		return -EIO;
6cc7e93f46a5ce Vitaly Rodionov 2021-03-06  1623  	}
6cc7e93f46a5ce Vitaly Rodionov 2021-03-06  1624  
6cc7e93f46a5ce Vitaly Rodionov 2021-03-06  1625  	cs8409_enable_i2c_clock(codec, 0);
6cc7e93f46a5ce Vitaly Rodionov 2021-03-06  1626  
649ba4016100c8 Stefan Binding  2021-03-15  1627  	return 0;
6cc7e93f46a5ce Vitaly Rodionov 2021-03-06  1628  }
6cc7e93f46a5ce Vitaly Rodionov 2021-03-06  1629  

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
diff mbox series

Patch

diff --git a/sound/pci/hda/patch_cirrus.c b/sound/pci/hda/patch_cirrus.c
index 6a9e5c803977..6c062c7ab76a 100644
--- a/sound/pci/hda/patch_cirrus.c
+++ b/sound/pci/hda/patch_cirrus.c
@@ -1493,22 +1493,34 @@  static const struct cs8409_cir_param cs8409_cs42l42_hw_cfg[] = {
 	{} /* Terminator */
 };
 
-/* Enable I2C clocks */
-static void cs8409_enable_i2c_clock(struct hda_codec *codec, unsigned int flag)
+/**
+ * cs8409_enable_i2c_clock - Enable I2C clocks
+ * @codec: the codec instance
+ * @enable: Enable or disable I2C clocks
+ *
+ * Enable or Disable I2C clocks.
+ */
+static void cs8409_enable_i2c_clock(struct hda_codec *codec, unsigned int enable)
 {
 	unsigned int retval = 0;
 	unsigned int newval = 0;
 
 	retval = cs_vendor_coef_get(codec, 0x0);
-	newval = (flag) ? (retval | 0x8) : (retval & 0xfffffff7);
+	newval = (enable) ? (retval | 0x8) : (retval & 0xfffffff7);
 	cs_vendor_coef_set(codec, 0x0, newval);
 }
 
-/* Wait I2C transaction  */
+/**
+ * cs8409_i2c_wait_complete - Wait for I2C transaction
+ * @codec: the codec instance
+ *
+ * Wait for I2C transaction to complete.
+ * Return -1 if transaction wait times out.
+ */
 static int cs8409_i2c_wait_complete(struct hda_codec *codec)
 {
 	int repeat = 5;
-	unsigned int retval = 0;
+	unsigned int retval;
 
 	do {
 		retval = cs_vendor_coef_get(codec, CIR_I2C_STATUS);
@@ -1516,82 +1528,103 @@  static int cs8409_i2c_wait_complete(struct hda_codec *codec)
 			usleep_range(2000, 4000);
 			--repeat;
 		} else
-			break;
+			return 0;
 
 	} while (repeat);
 
-	return repeat > 0 ? 0 : -1;
+	return -1;
 }
 
-/* CS8409 slave i2cRead */
-static unsigned int cs8409_i2c_read(struct hda_codec *codec,
+/**
+ * cs8409_i2c_read - CS8409 I2C Read.
+ * @codec: the codec instance
+ * @i2c_address: I2C Address
+ * @i2c_reg: Register to read
+ * @paged: Is a paged transaction
+ *
+ * CS8409 I2C Read.
+ * Returns negative on error, otherwise returns read value in bits 0-7.
+ */
+static int cs8409_i2c_read(struct hda_codec *codec,
 		unsigned int i2c_address,
 		unsigned int i2c_reg,
 		unsigned int paged)
 {
 	unsigned int i2c_reg_data;
-	unsigned int retval = 0;
+	unsigned int read_data;
 
 	cs8409_enable_i2c_clock(codec, 1);
 	cs_vendor_coef_set(codec, CIR_I2C_ADDR, i2c_address);
 
 	if (paged) {
 		cs_vendor_coef_set(codec, CIR_I2C_QWRITE, i2c_reg >> 8);
-		if (cs8409_i2c_wait_complete(codec) == -1) {
+		if (cs8409_i2c_wait_complete(codec) < 0) {
 			codec_err(codec,
-				"%s() Paged Transaction Failed 0x%02x : 0x%04x = 0x%02x\n",
-				__func__, i2c_address, i2c_reg, retval);
+				"%s() Paged Transaction Failed 0x%02x : 0x%04x\n",
+				__func__, i2c_address, i2c_reg);
+			return -EIO;
 		}
 	}
 
 	i2c_reg_data = (i2c_reg << 8) & 0x0ffff;
 	cs_vendor_coef_set(codec, CIR_I2C_QREAD, i2c_reg_data);
-	if (cs8409_i2c_wait_complete(codec) == -1) {
-		codec_err(codec, "%s() Transaction Failed 0x%02x : 0x%04x = 0x%02x\n",
-			__func__, i2c_address, i2c_reg, retval);
+	if (cs8409_i2c_wait_complete(codec) < 0) {
+		codec_err(codec, "%s() Transaction Failed 0x%02x : 0x%04x\n",
+			__func__, i2c_address, i2c_reg);
+		return -EIO;
 	}
 
 	/* Register in bits 15-8 and the data in 7-0 */
-	retval = cs_vendor_coef_get(codec, CIR_I2C_QREAD);
-	retval &= 0x0ff;
+	read_data = cs_vendor_coef_get(codec, CIR_I2C_QREAD);
 
 	cs8409_enable_i2c_clock(codec, 0);
 
-	return retval;
+	return read_data & 0x0ff;
 }
 
-/* CS8409 slave i2cWrite */
-static unsigned int cs8409_i2c_write(struct hda_codec *codec,
+/**
+ * cs8409_i2c_read - CS8409 I2C Write.
+ * @codec: the codec instance
+ * @i2c_address: I2C Address
+ * @i2c_reg: Register to write to
+ * @i2c_data: Data to write
+ * @paged: Is a paged transaction
+ *
+ * CS8409 I2C Write.
+ * Returns negative on error, otherwise returns 0.
+ */
+static int cs8409_i2c_write(struct hda_codec *codec,
 		unsigned int i2c_address, unsigned int i2c_reg,
 		unsigned int i2c_data,
 		unsigned int paged)
 {
-	unsigned int retval = 0;
-	unsigned int i2c_reg_data = 0;
+	unsigned int i2c_reg_data;
 
 	cs8409_enable_i2c_clock(codec, 1);
 	cs_vendor_coef_set(codec, CIR_I2C_ADDR, i2c_address);
 
 	if (paged) {
 		cs_vendor_coef_set(codec, CIR_I2C_QWRITE, i2c_reg >> 8);
-		if (cs8409_i2c_wait_complete(codec) == -1) {
+		if (cs8409_i2c_wait_complete(codec) < 0) {
 			codec_err(codec,
-				"%s() Paged Transaction Failed 0x%02x : 0x%04x = 0x%02x\n",
-				__func__, i2c_address, i2c_reg, retval);
+				"%s() Paged Transaction Failed 0x%02x : 0x%04x\n",
+				__func__, i2c_address, i2c_reg);
+			return -EIO;
 		}
 	}
 
 	i2c_reg_data = ((i2c_reg << 8) & 0x0ff00) | (i2c_data & 0x0ff);
 	cs_vendor_coef_set(codec, CIR_I2C_QWRITE, i2c_reg_data);
 
-	if (cs8409_i2c_wait_complete(codec) == -1) {
-		codec_err(codec, "%s() Transaction Failed 0x%02x : 0x%04x = 0x%02x\n",
-			__func__, i2c_address, i2c_reg, retval);
+	if (cs8409_i2c_wait_complete(codec) < 0) {
+		codec_err(codec, "%s() Transaction Failed 0x%02x : 0x%04x\n",
+			__func__, i2c_address, i2c_reg);
+		return -EIO;
 	}
 
 	cs8409_enable_i2c_clock(codec, 0);
 
-	return retval;
+	return 0;
 }
 
 static int cs8409_cs42l42_volume_info(struct snd_kcontrol *kcontrol,
@@ -1624,14 +1657,27 @@  static int cs8409_cs42l42_volume_info(struct snd_kcontrol *kcontrol,
 static void cs8409_cs42l42_update_volume(struct hda_codec *codec)
 {
 	struct cs_spec *spec = codec->spec;
+	int data;
 
 	mutex_lock(&spec->cs8409_i2c_mux);
-	spec->cs42l42_hp_volume[0] = -(cs8409_i2c_read(codec, CS42L42_I2C_ADDR,
-				CS8409_CS42L42_REG_HS_VOLUME_CHA, 1));
-	spec->cs42l42_hp_volume[1] = -(cs8409_i2c_read(codec, CS42L42_I2C_ADDR,
-				CS8409_CS42L42_REG_HS_VOLUME_CHB, 1));
-	spec->cs42l42_hs_mic_volume[0] = -(cs8409_i2c_read(codec, CS42L42_I2C_ADDR,
-				CS8409_CS42L42_REG_AMIC_VOLUME, 1));
+	data = cs8409_i2c_read(codec, CS42L42_I2C_ADDR,
+				CS8409_CS42L42_REG_HS_VOLUME_CHA, 1);
+	if (data >= 0)
+		spec->cs42l42_hp_volume[0] = -data;
+	else
+		spec->cs42l42_hp_volume[0] = CS8409_CS42L42_HP_VOL_REAL_MIN;
+	data = cs8409_i2c_read(codec, CS42L42_I2C_ADDR,
+				CS8409_CS42L42_REG_HS_VOLUME_CHB, 1);
+	if (data >= 0)
+		spec->cs42l42_hp_volume[1] = -data;
+	else
+		spec->cs42l42_hp_volume[1] = CS8409_CS42L42_HP_VOL_REAL_MIN;
+	data = cs8409_i2c_read(codec, CS42L42_I2C_ADDR,
+				CS8409_CS42L42_REG_AMIC_VOLUME, 1);
+	if (data >= 0)
+		spec->cs42l42_hs_mic_volume[0] = -data;
+	else
+		spec->cs42l42_hs_mic_volume[0] = CS8409_CS42L42_AMIC_VOL_REAL_MIN;
 	mutex_unlock(&spec->cs8409_i2c_mux);
 	spec->cs42l42_volume_init = 1;
 }
@@ -1782,7 +1828,7 @@  static void cs8409_cs42l42_reset(struct hda_codec *codec)
 }
 
 /* Configure CS42L42 slave codec for jack autodetect */
-static int cs8409_cs42l42_enable_jack_detect(struct hda_codec *codec)
+static void cs8409_cs42l42_enable_jack_detect(struct hda_codec *codec)
 {
 	struct cs_spec *spec = codec->spec;
 
@@ -1804,8 +1850,6 @@  static int cs8409_cs42l42_enable_jack_detect(struct hda_codec *codec)
 	cs8409_i2c_write(codec, CS42L42_I2C_ADDR, 0x1b79, 0x00, 1);
 
 	mutex_unlock(&spec->cs8409_i2c_mux);
-
-	return 0;
 }
 
 /* Enable and run CS42L42 slave codec jack auto detect */
@@ -1860,9 +1904,9 @@  static void cs8409_jack_unsol_event(struct hda_codec *codec, unsigned int res)
 {
 	struct cs_spec *spec = codec->spec;
 	int status_changed = 0;
-	unsigned int reg_cdc_status = 0;
-	unsigned int reg_hs_status = 0;
-	unsigned int reg_ts_status = 0;
+	int reg_cdc_status;
+	int reg_hs_status;
+	int reg_ts_status;
 	int type = 0;
 	struct hda_jack_tbl *jk;
 
@@ -1881,13 +1925,15 @@  static void cs8409_jack_unsol_event(struct hda_codec *codec, unsigned int res)
 	reg_hs_status = cs8409_i2c_read(codec, CS42L42_I2C_ADDR, 0x1124, 1);
 	reg_ts_status = cs8409_i2c_read(codec, CS42L42_I2C_ADDR, 0x130f, 1);
 
-	/* Clear interrupts */
+	/* Clear interrupts, by reading interrupt status registers */
 	cs8409_i2c_read(codec, CS42L42_I2C_ADDR, 0x1b7b, 1);
-	cs8409_i2c_read(codec, CS42L42_I2C_ADDR, 0x1308, 1);
-	cs8409_i2c_read(codec, CS42L42_I2C_ADDR, 0x130f, 1);
 
 	mutex_unlock(&spec->cs8409_i2c_mux);
 
+	/* If status values are < 0, read error has occurred. */
+	if (reg_cdc_status < 0 || reg_hs_status < 0 || reg_ts_status < 0)
+		return;
+
 	/* HSDET_AUTO_DONE */
 	if (reg_cdc_status & CS42L42_HSDET_AUTO_DONE) {