diff mbox series

[13/37] iio: humidity: hts211: Factor out everything under direct mode claim into helper functions.

Message ID 20250331121317.1694135-14-jic23@kernel.org (mailing list archive)
State New
Headers show
Series IIO: Sparse friendly claim of direct mode (the rest) | expand

Commit Message

Jonathan Cameron March 31, 2025, 12:12 p.m. UTC
From: Jonathan Cameron <Jonathan.Cameron@huawei.com>

Pulling out the functionality of read_raw() and write_raw() callbacks
so that only the mode claim is done in the initial call allows for
direct returns and simpler error handling in the new __hts211_write_raw()
/ __hts211_read_raw() functions.

Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
---
 drivers/iio/humidity/hts221_core.c | 89 +++++++++++++++---------------
 1 file changed, 45 insertions(+), 44 deletions(-)

Comments

Lorenzo Bianconi April 1, 2025, 6:33 a.m. UTC | #1
On Mar 31, Jonathan Cameron wrote:
> From: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> 
> Pulling out the functionality of read_raw() and write_raw() callbacks
> so that only the mode claim is done in the initial call allows for
> direct returns and simpler error handling in the new __hts211_write_raw()
> / __hts211_read_raw() functions.
> 
> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

Acked-by: Lorenzo Bianconi <lorenzo@kernel.org>

> ---
>  drivers/iio/humidity/hts221_core.c | 89 +++++++++++++++---------------
>  1 file changed, 45 insertions(+), 44 deletions(-)
> 
> diff --git a/drivers/iio/humidity/hts221_core.c b/drivers/iio/humidity/hts221_core.c
> index 0be11470730c..ca4746f2ecba 100644
> --- a/drivers/iio/humidity/hts221_core.c
> +++ b/drivers/iio/humidity/hts221_core.c
> @@ -418,31 +418,22 @@ static int hts221_read_oneshot(struct hts221_hw *hw, u8 addr, int *val)
>  	return IIO_VAL_INT;
>  }
>  
> -static int hts221_read_raw(struct iio_dev *iio_dev,
> -			   struct iio_chan_spec const *ch,
> -			   int *val, int *val2, long mask)
> +static int __hts221_read_raw(struct iio_dev *iio_dev,
> +			     struct iio_chan_spec const *ch,
> +			     int *val, int *val2, long mask)
>  {
>  	struct hts221_hw *hw = iio_priv(iio_dev);
> -	int ret;
> -
> -	ret = iio_device_claim_direct_mode(iio_dev);
> -	if (ret)
> -		return ret;
>  
>  	switch (mask) {
>  	case IIO_CHAN_INFO_RAW:
> -		ret = hts221_read_oneshot(hw, ch->address, val);
> -		break;
> +		return hts221_read_oneshot(hw, ch->address, val);
>  	case IIO_CHAN_INFO_SCALE:
> -		ret = hts221_get_sensor_scale(hw, ch->type, val, val2);
> -		break;
> +		return hts221_get_sensor_scale(hw, ch->type, val, val2);
>  	case IIO_CHAN_INFO_OFFSET:
> -		ret = hts221_get_sensor_offset(hw, ch->type, val, val2);
> -		break;
> +		return hts221_get_sensor_offset(hw, ch->type, val, val2);
>  	case IIO_CHAN_INFO_SAMP_FREQ:
>  		*val = hw->odr;
> -		ret = IIO_VAL_INT;
> -		break;
> +		return IIO_VAL_INT;
>  	case IIO_CHAN_INFO_OVERSAMPLING_RATIO: {
>  		u8 idx;
>  		const struct hts221_avg *avg;
> @@ -452,62 +443,72 @@ static int hts221_read_raw(struct iio_dev *iio_dev,
>  			avg = &hts221_avg_list[HTS221_SENSOR_H];
>  			idx = hw->sensors[HTS221_SENSOR_H].cur_avg_idx;
>  			*val = avg->avg_avl[idx];
> -			ret = IIO_VAL_INT;
> -			break;
> +			return IIO_VAL_INT;
>  		case IIO_TEMP:
>  			avg = &hts221_avg_list[HTS221_SENSOR_T];
>  			idx = hw->sensors[HTS221_SENSOR_T].cur_avg_idx;
>  			*val = avg->avg_avl[idx];
> -			ret = IIO_VAL_INT;
> -			break;
> +			return IIO_VAL_INT;
>  		default:
> -			ret = -EINVAL;
> -			break;
> +			return -EINVAL;
>  		}
> -		break;
>  	}
>  	default:
> -		ret = -EINVAL;
> -		break;
> +		return -EINVAL;
>  	}
> -
> -	iio_device_release_direct_mode(iio_dev);
> -
> -	return ret;
>  }
>  
> -static int hts221_write_raw(struct iio_dev *iio_dev,
> -			    struct iio_chan_spec const *chan,
> -			    int val, int val2, long mask)
> +static int hts221_read_raw(struct iio_dev *iio_dev,
> +			   struct iio_chan_spec const *ch,
> +			   int *val, int *val2, long mask)
>  {
> -	struct hts221_hw *hw = iio_priv(iio_dev);
>  	int ret;
>  
>  	ret = iio_device_claim_direct_mode(iio_dev);
>  	if (ret)
>  		return ret;
>  
> +	ret = __hts221_read_raw(iio_dev, ch, val, val2, mask);
> +
> +	iio_device_release_direct_mode(iio_dev);
> +
> +	return ret;
> +}
> +
> +static int __hts221_write_raw(struct iio_dev *iio_dev,
> +			      struct iio_chan_spec const *chan,
> +			      int val, long mask)
> +{
> +	struct hts221_hw *hw = iio_priv(iio_dev);
> +
>  	switch (mask) {
>  	case IIO_CHAN_INFO_SAMP_FREQ:
> -		ret = hts221_update_odr(hw, val);
> -		break;
> +		return hts221_update_odr(hw, val);
>  	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
>  		switch (chan->type) {
>  		case IIO_HUMIDITYRELATIVE:
> -			ret = hts221_update_avg(hw, HTS221_SENSOR_H, val);
> -			break;
> +			return hts221_update_avg(hw, HTS221_SENSOR_H, val);
>  		case IIO_TEMP:
> -			ret = hts221_update_avg(hw, HTS221_SENSOR_T, val);
> -			break;
> +			return hts221_update_avg(hw, HTS221_SENSOR_T, val);
>  		default:
> -			ret = -EINVAL;
> -			break;
> +			return -EINVAL;
>  		}
> -		break;
>  	default:
> -		ret = -EINVAL;
> -		break;
> +		return -EINVAL;
>  	}
> +}
> +
> +static int hts221_write_raw(struct iio_dev *iio_dev,
> +			    struct iio_chan_spec const *chan,
> +			    int val, int val2, long mask)
> +{
> +	int ret;
> +
> +	ret = iio_device_claim_direct_mode(iio_dev);
> +	if (ret)
> +		return ret;
> +
> +	ret = __hts221_write_raw(iio_dev, chan, val, mask);
>  
>  	iio_device_release_direct_mode(iio_dev);
>  
> -- 
> 2.48.1
>
diff mbox series

Patch

diff --git a/drivers/iio/humidity/hts221_core.c b/drivers/iio/humidity/hts221_core.c
index 0be11470730c..ca4746f2ecba 100644
--- a/drivers/iio/humidity/hts221_core.c
+++ b/drivers/iio/humidity/hts221_core.c
@@ -418,31 +418,22 @@  static int hts221_read_oneshot(struct hts221_hw *hw, u8 addr, int *val)
 	return IIO_VAL_INT;
 }
 
-static int hts221_read_raw(struct iio_dev *iio_dev,
-			   struct iio_chan_spec const *ch,
-			   int *val, int *val2, long mask)
+static int __hts221_read_raw(struct iio_dev *iio_dev,
+			     struct iio_chan_spec const *ch,
+			     int *val, int *val2, long mask)
 {
 	struct hts221_hw *hw = iio_priv(iio_dev);
-	int ret;
-
-	ret = iio_device_claim_direct_mode(iio_dev);
-	if (ret)
-		return ret;
 
 	switch (mask) {
 	case IIO_CHAN_INFO_RAW:
-		ret = hts221_read_oneshot(hw, ch->address, val);
-		break;
+		return hts221_read_oneshot(hw, ch->address, val);
 	case IIO_CHAN_INFO_SCALE:
-		ret = hts221_get_sensor_scale(hw, ch->type, val, val2);
-		break;
+		return hts221_get_sensor_scale(hw, ch->type, val, val2);
 	case IIO_CHAN_INFO_OFFSET:
-		ret = hts221_get_sensor_offset(hw, ch->type, val, val2);
-		break;
+		return hts221_get_sensor_offset(hw, ch->type, val, val2);
 	case IIO_CHAN_INFO_SAMP_FREQ:
 		*val = hw->odr;
-		ret = IIO_VAL_INT;
-		break;
+		return IIO_VAL_INT;
 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO: {
 		u8 idx;
 		const struct hts221_avg *avg;
@@ -452,62 +443,72 @@  static int hts221_read_raw(struct iio_dev *iio_dev,
 			avg = &hts221_avg_list[HTS221_SENSOR_H];
 			idx = hw->sensors[HTS221_SENSOR_H].cur_avg_idx;
 			*val = avg->avg_avl[idx];
-			ret = IIO_VAL_INT;
-			break;
+			return IIO_VAL_INT;
 		case IIO_TEMP:
 			avg = &hts221_avg_list[HTS221_SENSOR_T];
 			idx = hw->sensors[HTS221_SENSOR_T].cur_avg_idx;
 			*val = avg->avg_avl[idx];
-			ret = IIO_VAL_INT;
-			break;
+			return IIO_VAL_INT;
 		default:
-			ret = -EINVAL;
-			break;
+			return -EINVAL;
 		}
-		break;
 	}
 	default:
-		ret = -EINVAL;
-		break;
+		return -EINVAL;
 	}
-
-	iio_device_release_direct_mode(iio_dev);
-
-	return ret;
 }
 
-static int hts221_write_raw(struct iio_dev *iio_dev,
-			    struct iio_chan_spec const *chan,
-			    int val, int val2, long mask)
+static int hts221_read_raw(struct iio_dev *iio_dev,
+			   struct iio_chan_spec const *ch,
+			   int *val, int *val2, long mask)
 {
-	struct hts221_hw *hw = iio_priv(iio_dev);
 	int ret;
 
 	ret = iio_device_claim_direct_mode(iio_dev);
 	if (ret)
 		return ret;
 
+	ret = __hts221_read_raw(iio_dev, ch, val, val2, mask);
+
+	iio_device_release_direct_mode(iio_dev);
+
+	return ret;
+}
+
+static int __hts221_write_raw(struct iio_dev *iio_dev,
+			      struct iio_chan_spec const *chan,
+			      int val, long mask)
+{
+	struct hts221_hw *hw = iio_priv(iio_dev);
+
 	switch (mask) {
 	case IIO_CHAN_INFO_SAMP_FREQ:
-		ret = hts221_update_odr(hw, val);
-		break;
+		return hts221_update_odr(hw, val);
 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
 		switch (chan->type) {
 		case IIO_HUMIDITYRELATIVE:
-			ret = hts221_update_avg(hw, HTS221_SENSOR_H, val);
-			break;
+			return hts221_update_avg(hw, HTS221_SENSOR_H, val);
 		case IIO_TEMP:
-			ret = hts221_update_avg(hw, HTS221_SENSOR_T, val);
-			break;
+			return hts221_update_avg(hw, HTS221_SENSOR_T, val);
 		default:
-			ret = -EINVAL;
-			break;
+			return -EINVAL;
 		}
-		break;
 	default:
-		ret = -EINVAL;
-		break;
+		return -EINVAL;
 	}
+}
+
+static int hts221_write_raw(struct iio_dev *iio_dev,
+			    struct iio_chan_spec const *chan,
+			    int val, int val2, long mask)
+{
+	int ret;
+
+	ret = iio_device_claim_direct_mode(iio_dev);
+	if (ret)
+		return ret;
+
+	ret = __hts221_write_raw(iio_dev, chan, val, mask);
 
 	iio_device_release_direct_mode(iio_dev);