Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

hwmon: (sht4x) Fix sht4x_read_values return value

Kernel doc for sht4x_read_values() shows 0 on success, 1 on failure but
the return value on success is actually always positive as it is set to
SHT4X_RESPONSE_LENGTH by a successful call to i2c_master_recv().

Miscellanea:

o Update the kernel doc for sht4x_read_values to 0 for success or -ERRNO
o Remove incorrectly used kernel doc /** header for other _read functions
o Typo fix succesfull->successful
o Reverse a test to unindent a block and use goto unlock
o Declare cmd[SHT4X_CMD_LEN] rather than cmd[]

At least for gcc 10.2, object size is reduced a tiny bit.

$ size drivers/hwmon/sht4x.o*
text data bss dec hex filename
1752 404 256 2412 96c drivers/hwmon/sht4x.o.new
1825 404 256 2485 9b5 drivers/hwmon/sht4x.o.old

Signed-off-by: Joe Perches <joe@perches.com>
Link: https://lore.kernel.org/r/60eedce497137eb34448c0c77e01ec9d9c972ad7.camel@perches.com
Reviewed by: Navin Sankar Velliangiri <navin@linumiz.com>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>

authored by

Joe Perches and committed by
Guenter Roeck
07c6621a 505c2549

+41 -46
+41 -46
drivers/hwmon/sht4x.c
··· 67 67 /** 68 68 * sht4x_read_values() - read and parse the raw data from the SHT4X 69 69 * @sht4x_data: the struct sht4x_data to use for the lock 70 - * Return: 0 if succesfull, 1 if not 70 + * Return: 0 if successful, -ERRNO if not 71 71 */ 72 72 static int sht4x_read_values(struct sht4x_data *data) 73 73 { ··· 75 75 u16 t_ticks, rh_ticks; 76 76 unsigned long next_update; 77 77 struct i2c_client *client = data->client; 78 - u8 crc, raw_data[SHT4X_RESPONSE_LENGTH], 79 - cmd[] = {SHT4X_CMD_MEASURE_HPM}; 78 + u8 crc; 79 + u8 cmd[SHT4X_CMD_LEN] = {SHT4X_CMD_MEASURE_HPM}; 80 + u8 raw_data[SHT4X_RESPONSE_LENGTH]; 80 81 81 82 mutex_lock(&data->lock); 82 83 next_update = data->last_updated + 83 84 msecs_to_jiffies(data->update_interval); 84 - if (!data->valid || time_after(jiffies, next_update)) { 85 - ret = i2c_master_send(client, cmd, SHT4X_CMD_LEN); 86 - if (ret < 0) 87 - goto unlock; 88 85 89 - usleep_range(SHT4X_MEAS_DELAY, 90 - SHT4X_MEAS_DELAY + SHT4X_DELAY_EXTRA); 86 + if (data->valid && time_before_eq(jiffies, next_update)) 87 + goto unlock; 91 88 92 - ret = i2c_master_recv(client, raw_data, SHT4X_RESPONSE_LENGTH); 93 - if (ret != SHT4X_RESPONSE_LENGTH) { 94 - if (ret >= 0) 95 - ret = -ENODATA; 89 + ret = i2c_master_send(client, cmd, SHT4X_CMD_LEN); 90 + if (ret < 0) 91 + goto unlock; 96 92 97 - goto unlock; 98 - } 93 + usleep_range(SHT4X_MEAS_DELAY, SHT4X_MEAS_DELAY + SHT4X_DELAY_EXTRA); 99 94 100 - t_ticks = raw_data[0] << 8 | raw_data[1]; 101 - rh_ticks = raw_data[3] << 8 | raw_data[4]; 102 - 103 - crc = crc8(sht4x_crc8_table, &raw_data[0], SHT4X_WORD_LEN, CRC8_INIT_VALUE); 104 - if (crc != raw_data[2]) { 105 - dev_err(&client->dev, "data integrity check failed\n"); 106 - ret = -EIO; 107 - goto unlock; 108 - } 109 - 110 - crc = crc8(sht4x_crc8_table, &raw_data[3], SHT4X_WORD_LEN, CRC8_INIT_VALUE); 111 - if (crc != raw_data[5]) { 112 - dev_err(&client->dev, "data integrity check failed\n"); 113 - ret = -EIO; 114 - goto unlock; 115 - } 116 - 117 - data->temperature = ((21875 * (int32_t)t_ticks) >> 13) - 45000; 118 - data->humidity = ((15625 * (int32_t)rh_ticks) >> 13) - 6000; 119 - data->last_updated = jiffies; 120 - data->valid = true; 95 + ret = i2c_master_recv(client, raw_data, SHT4X_RESPONSE_LENGTH); 96 + if (ret != SHT4X_RESPONSE_LENGTH) { 97 + if (ret >= 0) 98 + ret = -ENODATA; 99 + goto unlock; 121 100 } 101 + 102 + t_ticks = raw_data[0] << 8 | raw_data[1]; 103 + rh_ticks = raw_data[3] << 8 | raw_data[4]; 104 + 105 + crc = crc8(sht4x_crc8_table, &raw_data[0], SHT4X_WORD_LEN, CRC8_INIT_VALUE); 106 + if (crc != raw_data[2]) { 107 + dev_err(&client->dev, "data integrity check failed\n"); 108 + ret = -EIO; 109 + goto unlock; 110 + } 111 + 112 + crc = crc8(sht4x_crc8_table, &raw_data[3], SHT4X_WORD_LEN, CRC8_INIT_VALUE); 113 + if (crc != raw_data[5]) { 114 + dev_err(&client->dev, "data integrity check failed\n"); 115 + ret = -EIO; 116 + goto unlock; 117 + } 118 + 119 + data->temperature = ((21875 * (int32_t)t_ticks) >> 13) - 45000; 120 + data->humidity = ((15625 * (int32_t)rh_ticks) >> 13) - 6000; 121 + data->last_updated = jiffies; 122 + data->valid = true; 123 + ret = 0; 122 124 123 125 unlock: 124 126 mutex_unlock(&data->lock); ··· 134 132 return 0; 135 133 } 136 134 137 - /** 138 - * sht4x_interval_read() - read the minimum poll interval 139 - * in milliseconds 140 - */ 135 + /* sht4x_interval_read() - read the minimum poll interval in milliseconds */ 141 136 static size_t sht4x_interval_read(struct sht4x_data *data, long *val) 142 137 { 143 138 *val = data->update_interval; 144 139 return 0; 145 140 } 146 141 147 - /** 148 - * sht4x_temperature1_read() - read the temperature in millidegrees 149 - */ 142 + /* sht4x_temperature1_read() - read the temperature in millidegrees */ 150 143 static int sht4x_temperature1_read(struct sht4x_data *data, long *val) 151 144 { 152 145 int ret; ··· 155 158 return 0; 156 159 } 157 160 158 - /** 159 - * sht4x_humidity1_read() - read a relative humidity in millipercent 160 - */ 161 + /* sht4x_humidity1_read() - read a relative humidity in millipercent */ 161 162 static int sht4x_humidity1_read(struct sht4x_data *data, long *val) 162 163 { 163 164 int ret;