All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Adriana Reus <adriana.reus@intel.com>
Cc: linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org,
	robh+dt@kernel.org, ijc+devicetree@hellion.org.uk,
	galak@codeaurora.org, pawel.moll@arm.com, pmeerw@pmeerw.net,
	lars@metafoo.de
Subject: Re: [PATCH 4/5] iio: light: us8152d: Add power management support
Date: Sun, 29 Nov 2015 14:59:56 +0000	[thread overview]
Message-ID: <565B12EB.4070208@kernel.org> (raw)
In-Reply-To: <1448362792-5181-5-git-send-email-adriana.reus@intel.com>

On 24/11/15 10:59, Adriana Reus wrote:
> Add power management for sleep as well as runtime pm.
> 
> Signed-off-by: Adriana Reus <adriana.reus@intel.com>
Mostly fine, but a comment on a possible future tidy up inline.

Applied to the togreg branch of iio.git - initially pushed out as
testing for the autobuilders to play with it.

> ---
>  drivers/iio/light/us5182d.c | 95 +++++++++++++++++++++++++++++++++++++++++----
>  1 file changed, 88 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/iio/light/us5182d.c b/drivers/iio/light/us5182d.c
> index 3d959f3..60cab4a 100644
> --- a/drivers/iio/light/us5182d.c
> +++ b/drivers/iio/light/us5182d.c
> @@ -23,6 +23,8 @@
>  #include <linux/iio/iio.h>
>  #include <linux/iio/sysfs.h>
>  #include <linux/mutex.h>
> +#include <linux/pm.h>
> +#include <linux/pm_runtime.h>
>  
>  #define US5182D_REG_CFG0				0x00
>  #define US5182D_CFG0_ONESHOT_EN				BIT(6)
> @@ -81,6 +83,7 @@
>  #define US5182D_READ_BYTE			1
>  #define US5182D_READ_WORD			2
>  #define US5182D_OPSTORE_SLEEP_TIME		20 /* ms */
> +#define US5182D_SLEEP_MS			3000 /* ms */
>  
>  /* Available ranges: [12354, 7065, 3998, 2202, 1285, 498, 256, 138] lux */
>  static const int us5182d_scales[] = {188500, 107800, 61000, 33600, 19600, 7600,
> @@ -298,6 +301,26 @@ static int us5182d_shutdown_en (struct us5182d_data *data, u8 state)
>  	return ret;
>  }
>  
> +
> +static int us5182d_set_power_state(struct us5182d_data *data, bool on)
> +{
> +	int ret;
> +
> +	if (data->power_mode == US5182D_ONESHOT)
> +		return 0;
> +
> +	if (on) {
> +		ret = pm_runtime_get_sync(&data->client->dev);
> +		if (ret < 0)
> +			pm_runtime_put_noidle(&data->client->dev);
> +	} else {
> +		pm_runtime_mark_last_busy(&data->client->dev);
> +		ret = pm_runtime_put_autosuspend(&data->client->dev);
> +	}
> +
> +	return ret;
> +}
> +
>  static int us5182d_read_raw(struct iio_dev *indio_dev,
>  			    struct iio_chan_spec const *chan, int *val,
>  			    int *val2, long mask)
> @@ -315,15 +338,20 @@ static int us5182d_read_raw(struct iio_dev *indio_dev,
>  				if (ret < 0)
>  					goto out_err;
>  			}
> -			ret = us5182d_als_enable(data);
> +			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);
> -			*val = ret;
>  			return IIO_VAL_INT;
>  		case IIO_PROXIMITY:
I'd argue the complexity of this has reached the point where ideally you'd
break it out to a function.  The jumps out of the switch statement are 
particularly nasty from a readability point of view.

Perhaps a job for a followup patch?
>  			mutex_lock(&data->lock);
> @@ -332,17 +360,22 @@ static int us5182d_read_raw(struct iio_dev *indio_dev,
>  				if (ret < 0)
>  					goto out_err;
>  			}
> -			ret = us5182d_px_enable(data);
> +			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);
> -			*val = ret;
> -			return  IIO_VAL_INT;
> +			return IIO_VAL_INT;
This is a little bit of noise that should have been dealt with separately...

>  		default:
>  			return -EINVAL;
>  		}
> @@ -361,6 +394,9 @@ static int us5182d_read_raw(struct iio_dev *indio_dev,
>  	}
>  
>  	return -EINVAL;
> +
> +out_poweroff:
> +	us5182d_set_power_state(data, false);
>  out_err:
>  	mutex_unlock(&data->lock);
>  	return ret;
> @@ -577,6 +613,17 @@ static int us5182d_probe(struct i2c_client *client,
>  	if (ret < 0)
>  		goto out_err;
>  
> +	if (data->default_continuous) {
> +		pm_runtime_set_active(&client->dev);
> +		if (ret < 0)
> +			goto out_err;
> +	}
> +
> +	pm_runtime_enable(&client->dev);
> +	pm_runtime_set_autosuspend_delay(&client->dev,
> +					 US5182D_SLEEP_MS);
> +	pm_runtime_use_autosuspend(&client->dev);
> +
>  	ret = iio_device_register(indio_dev);
>  	if (ret < 0)
>  		goto out_err;
> @@ -595,9 +642,42 @@ static int us5182d_remove(struct i2c_client *client)
>  
>  	iio_device_unregister(i2c_get_clientdata(client));
>  
> +	pm_runtime_disable(&client->dev);
> +	pm_runtime_set_suspended(&client->dev);
> +
>  	return us5182d_shutdown_en(data, US5182D_CFG0_SHUTDOWN_EN);
>  }
>  
> +#if defined(CONFIG_PM_SLEEP) || defined(CONFIG_PM)
> +static int us5182d_suspend(struct device *dev)
> +{
> +	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
> +	struct us5182d_data *data = iio_priv(indio_dev);
> +
> +	if (data->power_mode == US5182D_CONTINUOUS)
> +		return us5182d_shutdown_en(data, US5182D_CFG0_SHUTDOWN_EN);
> +
> +	return 0;
> +}
> +
> +static int us5182d_resume(struct device *dev)
> +{
> +	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
> +	struct us5182d_data *data = iio_priv(indio_dev);
> +
> +	if (data->power_mode == US5182D_CONTINUOUS)
> +		return us5182d_shutdown_en(data,
> +					   ~US5182D_CFG0_SHUTDOWN_EN & 0xff);
> +
> +	return 0;
> +}
> +#endif
> +
> +static const struct dev_pm_ops us5182d_pm_ops = {
> +	SET_SYSTEM_SLEEP_PM_OPS(us5182d_suspend, us5182d_resume)
> +	SET_RUNTIME_PM_OPS(us5182d_suspend, us5182d_resume, NULL)
> +};
> +
>  static const struct acpi_device_id us5182d_acpi_match[] = {
>  	{ "USD5182", 0},
>  	{}
> @@ -615,6 +695,7 @@ MODULE_DEVICE_TABLE(i2c, us5182d_id);
>  static struct i2c_driver us5182d_driver = {
>  	.driver = {
>  		.name = US5182D_DRV_NAME,
> +		.pm = &us5182d_pm_ops,
>  		.acpi_match_table = ACPI_PTR(us5182d_acpi_match),
>  	},
>  	.probe = us5182d_probe,
> 


  reply	other threads:[~2015-11-29 14:59 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-24 10:59 [PATCH 0/5] iio: light: us5281d: Add power managmenet and interrupt support Adriana Reus
2015-11-24 10:59 ` Adriana Reus
2015-11-24 10:59 ` [PATCH 1/5] iio: light: us5182d: Add property for choosing default power mode Adriana Reus
2015-11-29 14:35   ` Jonathan Cameron
2015-11-24 10:59 ` [PATCH 2/5] Documentation: devicetree: Add property for controlling power saving mode for the us5182 als sensor Adriana Reus
2015-11-25  0:01   ` Rob Herring
2015-11-25  0:01     ` Rob Herring
2015-11-25  9:50     ` Adriana Reus
2015-11-25 23:55       ` Rob Herring
2015-11-25 23:55         ` Rob Herring
2015-11-29 14:36         ` Jonathan Cameron
2015-11-24 10:59 ` [PATCH 3/5] iio: light: us5182d: Add functions for selectively enabling als and proximity Adriana Reus
2015-11-24 10:59   ` Adriana Reus
2015-11-29 16:13   ` Jonathan Cameron
2015-11-24 10:59 ` [PATCH 4/5] iio: light: us8152d: Add power management support Adriana Reus
2015-11-29 14:59   ` Jonathan Cameron [this message]
2015-11-24 10:59 ` [PATCH 5/5] iio: light: us5182d: Add interrupt support and events Adriana Reus
2015-11-29 14:59   ` Jonathan Cameron

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=565B12EB.4070208@kernel.org \
    --to=jic23@kernel.org \
    --cc=adriana.reus@intel.com \
    --cc=devicetree@vger.kernel.org \
    --cc=galak@codeaurora.org \
    --cc=ijc+devicetree@hellion.org.uk \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=pawel.moll@arm.com \
    --cc=pmeerw@pmeerw.net \
    --cc=robh+dt@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.