Linux IIO development
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Adriana Reus <adriana.reus@intel.com>
Cc: linux-iio@vger.kernel.org
Subject: Re: [PATCH v2 3/3] iio: light: us5182d: Refactor read_raw function
Date: Tue, 22 Dec 2015 18:07:26 +0000	[thread overview]
Message-ID: <5679915E.8050803@kernel.org> (raw)
In-Reply-To: <1450095887-13122-4-git-send-email-adriana.reus@intel.com>

On 14/12/15 12:24, Adriana Reus wrote:
> A bit of refactoring for better readability.
> Moved and slightly reorganized all the activity necessary for reading als
> and proximity into a different function. This way the switch in read raw
> becomes clearer and more compact.
> 
> Signed-off-by: Adriana Reus <adriana.reus@intel.com>
Applied.

Thanks,

Jonathan
> ---
>  No change since initial patch
> 
>  drivers/iio/light/us5182d.c | 155 ++++++++++++++++++++++----------------------
>  1 file changed, 78 insertions(+), 77 deletions(-)
> 
> diff --git a/drivers/iio/light/us5182d.c b/drivers/iio/light/us5182d.c
> index 213f778..45bc2f7 100644
> --- a/drivers/iio/light/us5182d.c
> +++ b/drivers/iio/light/us5182d.c
> @@ -203,23 +203,6 @@ static const struct iio_chan_spec us5182d_channels[] = {
>  	}
>  };
>  
> -static int us5182d_get_als(struct us5182d_data *data)
> -{
> -	int ret;
> -	unsigned long result;
> -
> -	ret = i2c_smbus_read_word_data(data->client,
> -				       US5182D_REG_ADL);
> -	if (ret < 0)
> -		return ret;
> -
> -	result = ret * data->ga / US5182D_GA_RESOLUTION;
> -	if (result > 0xffff)
> -		result = 0xffff;
> -
> -	return result;
> -}
> -
>  static int us5182d_oneshot_en(struct us5182d_data *data)
>  {
>  	int ret;
> @@ -324,6 +307,39 @@ static int us5182d_px_enable(struct us5182d_data *data)
>  	return 0;
>  }
>  
> +static int us5182d_get_als(struct us5182d_data *data)
> +{
> +	int ret;
> +	unsigned long result;
> +
> +	ret = us5182d_als_enable(data);
> +	if (ret < 0)
> +		return ret;
> +
> +	ret = i2c_smbus_read_word_data(data->client,
> +				       US5182D_REG_ADL);
> +	if (ret < 0)
> +		return ret;
> +
> +	result = ret * data->ga / US5182D_GA_RESOLUTION;
> +	if (result > 0xffff)
> +		result = 0xffff;
> +
> +	return result;
> +}
> +
> +static int us5182d_get_px(struct us5182d_data *data)
> +{
> +	int ret;
> +
> +	ret = us5182d_px_enable(data);
> +	if (ret < 0)
> +		return ret;
> +
> +	return i2c_smbus_read_word_data(data->client,
> +					US5182D_REG_PDL);
> +}
> +
>  static int us5182d_shutdown_en(struct us5182d_data *data, u8 state)
>  {
>  	int ret;
> @@ -370,6 +386,46 @@ static int us5182d_set_power_state(struct us5182d_data *data, bool on)
>  	return ret;
>  }
>  
> +static int us5182d_read_value(struct us5182d_data *data,
> +			      struct iio_chan_spec const *chan)
> +{
> +	int ret, value;
> +
> +	mutex_lock(&data->lock);
> +
> +	if (data->power_mode == US5182D_ONESHOT) {
> +		ret = us5182d_oneshot_en(data);
> +		if (ret < 0)
> +			goto out_err;
> +	}
> +
> +	ret = us5182d_set_power_state(data, true);
> +	if (ret < 0)
> +		goto out_err;
> +
> +	if (chan->type == IIO_LIGHT)
> +		ret = us5182d_get_als(data);
> +	else
> +		ret = us5182d_get_px(data);
> +	if (ret < 0)
> +		goto out_poweroff;
> +
> +	value = ret;
> +
> +	ret = us5182d_set_power_state(data, false);
> +	if (ret < 0)
> +		goto out_err;
> +
> +	mutex_unlock(&data->lock);
> +	return value;
> +
> +out_poweroff:
> +	us5182d_set_power_state(data, false);
> +out_err:
> +	mutex_unlock(&data->lock);
> +	return ret;
> +}
> +
>  static int us5182d_read_raw(struct iio_dev *indio_dev,
>  			    struct iio_chan_spec const *chan, int *val,
>  			    int *val2, long mask)
> @@ -379,76 +435,21 @@ static int us5182d_read_raw(struct iio_dev *indio_dev,
>  
>  	switch (mask) {
>  	case IIO_CHAN_INFO_RAW:
> -		switch (chan->type) {
> -		case IIO_LIGHT:
> -			mutex_lock(&data->lock);
> -			if (data->power_mode == US5182D_ONESHOT) {
> -				ret = us5182d_oneshot_en(data);
> -				if (ret < 0)
> -					goto out_err;
> -			}
> -			ret = us5182d_set_power_state(data, true);
> -			if (ret < 0)
> -				goto out_err;
> -			ret = us5182d_als_enable(data);
> -			if (ret < 0)
> -				goto out_poweroff;
> -			ret = us5182d_get_als(data);
> -			if (ret < 0)
> -				goto out_poweroff;
> -			*val = ret;
> -			ret = us5182d_set_power_state(data, false);
> -			if (ret < 0)
> -				goto out_err;
> -			mutex_unlock(&data->lock);
> -			return IIO_VAL_INT;
> -		case IIO_PROXIMITY:
> -			mutex_lock(&data->lock);
> -			if (data->power_mode == US5182D_ONESHOT) {
> -				ret = us5182d_oneshot_en(data);
> -				if (ret < 0)
> -					goto out_err;
> -			}
> -			ret = us5182d_set_power_state(data, true);
> -			if (ret < 0)
> -				goto out_err;
> -			ret = us5182d_px_enable(data);
> -			if (ret < 0)
> -				goto out_poweroff;
> -			ret = i2c_smbus_read_word_data(data->client,
> -						       US5182D_REG_PDL);
> -			if (ret < 0)
> -				goto out_poweroff;
> -			*val = ret;
> -			ret = us5182d_set_power_state(data, false);
> -			if (ret < 0)
> -				goto out_err;
> -			mutex_unlock(&data->lock);
> -			return IIO_VAL_INT;
> -		default:
> -			return -EINVAL;
> -		}
> -
> +		ret = us5182d_read_value(data, chan);
> +		if (ret < 0)
> +			return ret;
> +		*val = ret;
> +		return IIO_VAL_INT;
>  	case IIO_CHAN_INFO_SCALE:
>  		ret = i2c_smbus_read_byte_data(data->client, US5182D_REG_CFG1);
>  		if (ret < 0)
>  			return ret;
> -
>  		*val = 0;
>  		*val2 = us5182d_scales[ret & US5182D_AGAIN_MASK];
> -
>  		return IIO_VAL_INT_PLUS_MICRO;
>  	default:
>  		return -EINVAL;
>  	}
> -
> -	return -EINVAL;
> -
> -out_poweroff:
> -	us5182d_set_power_state(data, false);
> -out_err:
> -	mutex_unlock(&data->lock);
> -	return ret;
>  }
>  
>  /**
> 


      reply	other threads:[~2015-12-22 18:07 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-12-14 12:24 [PATCH v2 0/3] iio: light: us5218d: Add interrupt support and some tidying up Adriana Reus
2015-12-14 12:24 ` [PATCH v2 1/3] iio: light: us5182d: Fix enable status inconcistency Adriana Reus
2015-12-22 18:01   ` Jonathan Cameron
2015-12-14 12:24 ` [PATCH v2 2/3] iio: light: us5182d: Add interrupt support and events Adriana Reus
2015-12-22 18:06   ` Jonathan Cameron
2015-12-14 12:24 ` [PATCH v2 3/3] iio: light: us5182d: Refactor read_raw function Adriana Reus
2015-12-22 18:07   ` Jonathan Cameron [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5679915E.8050803@kernel.org \
    --to=jic23@kernel.org \
    --cc=adriana.reus@intel.com \
    --cc=linux-iio@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox