Re: [PATCH 2/3] hwmon: (aht10) Refactor aht10_read_values function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, May 11, 2023 at 10:26:32PM +0200, Kirill Yatsenko wrote:
> Exit from the function immediately if the poll time hasn't yet expired.
> Therefore the code after the check can be moved one tab to the left which
> improves readability.
> 
> Signed-off-by: Kirill Yatsenko <kiriyatsenko@xxxxxxxxx>
> ---
>  drivers/hwmon/aht10.c | 67 ++++++++++++++++++++++---------------------
>  1 file changed, 35 insertions(+), 32 deletions(-)
> 
> diff --git a/drivers/hwmon/aht10.c b/drivers/hwmon/aht10.c
> index 87a433e570e1..17ceec9aab66 100644
> --- a/drivers/hwmon/aht10.c
> +++ b/drivers/hwmon/aht10.c
> @@ -135,40 +135,43 @@ static int aht10_read_values(struct aht10_data *data)
>  	struct i2c_client *client = data->client;
>  
>  	mutex_lock(&data->lock);
> -	if (aht10_polltime_expired(data)) {
> -		res = i2c_master_send(client, cmd_meas, sizeof(cmd_meas));
> -		if (res < 0) {
> -			mutex_unlock(&data->lock);
> +	if (!aht10_polltime_expired(data)) {
> +		mutex_unlock(&data->lock);
> +		return 0;
> +	}
> +
> +	res = i2c_master_send(client, cmd_meas, sizeof(cmd_meas));
> +	if (res < 0) {
> +		mutex_unlock(&data->lock);
> +		return res;
> +	}
> +
> +	usleep_range(AHT10_MEAS_DELAY, AHT10_MEAS_DELAY + AHT10_DELAY_EXTRA);
> +
> +	res = i2c_master_recv(client, raw_data, AHT10_MEAS_SIZE);
> +	if (res != AHT10_MEAS_SIZE) {
> +		mutex_unlock(&data->lock);
> +		if (res >= 0)
> +			return -ENODATA;
> +		else
>  			return res;

else after return is pointless (and static analyzers complain about it).

No need to resend (I see it is inherited from old code). I'll fix it up
when applying the patch.

Guenter

> -		}
> -
> -		usleep_range(AHT10_MEAS_DELAY,
> -			     AHT10_MEAS_DELAY + AHT10_DELAY_EXTRA);
> -
> -		res = i2c_master_recv(client, raw_data, AHT10_MEAS_SIZE);
> -		if (res != AHT10_MEAS_SIZE) {
> -			mutex_unlock(&data->lock);
> -			if (res >= 0)
> -				return -ENODATA;
> -			else
> -				return res;
> -		}
> -
> -		hum =   ((u32)raw_data[1] << 12u) |
> -			((u32)raw_data[2] << 4u) |
> -			((raw_data[3] & 0xF0u) >> 4u);
> -
> -		temp =  ((u32)(raw_data[3] & 0x0Fu) << 16u) |
> -			((u32)raw_data[4] << 8u) |
> -			raw_data[5];
> -
> -		temp = ((temp * 625) >> 15u) * 10;
> -		hum = ((hum * 625) >> 16u) * 10;
> -
> -		data->temperature = (int)temp - 50000;
> -		data->humidity = hum;
> -		data->previous_poll_time = ktime_get_boottime();
>  	}
> +
> +	hum =   ((u32)raw_data[1] << 12u) |
> +		((u32)raw_data[2] << 4u) |
> +		((raw_data[3] & 0xF0u) >> 4u);
> +
> +	temp =  ((u32)(raw_data[3] & 0x0Fu) << 16u) |
> +		((u32)raw_data[4] << 8u) |
> +		raw_data[5];
> +
> +	temp = ((temp * 625) >> 15u) * 10;
> +	hum = ((hum * 625) >> 16u) * 10;
> +
> +	data->temperature = (int)temp - 50000;
> +	data->humidity = hum;
> +	data->previous_poll_time = ktime_get_boottime();
> +
>  	mutex_unlock(&data->lock);
>  	return 0;
>  }



[Index of Archives]     [LM Sensors]     [Linux Sound]     [ALSA Users]     [ALSA Devel]     [Linux Audio Users]     [Linux Media]     [Kernel]     [Gimp]     [Yosemite News]     [Linux Media]

  Powered by Linux