linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Guenter Roeck <linux@roeck-us.net>
To: Nicolin Chen <nicoleotsuka@gmail.com>
Cc: jdelvare@suse.com, afd@ti.com, linux-hwmon@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH 2/2] hwmon: ina3221: Add suspend and resume functions
Date: Sat, 29 Sep 2018 08:33:46 -0700	[thread overview]
Message-ID: <20180929153346.GA26651@roeck-us.net> (raw)
In-Reply-To: <20180928214921.11528-3-nicoleotsuka@gmail.com>

On Fri, Sep 28, 2018 at 02:49:21PM -0700, Nicolin Chen wrote:
> Depending on the hardware design, an INA3221 chip might lose
> its power during system suspend/resume. So this patch adds
> a pair of suspend and resume functions to cache the register
> values including config register value and limit settings.
> 
> Signed-off-by: Nicolin Chen <nicoleotsuka@gmail.com>
> ---
>  drivers/hwmon/ina3221.c | 54 +++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 54 insertions(+)
> 
> diff --git a/drivers/hwmon/ina3221.c b/drivers/hwmon/ina3221.c
> index cfe65ff01051..26f66f728b27 100644
> --- a/drivers/hwmon/ina3221.c
> +++ b/drivers/hwmon/ina3221.c
> @@ -91,11 +91,13 @@ static const unsigned int register_channel[] = {
>   * @regmap: Register map of the device
>   * @fields: Register fields of the device
>   * @shunt_resistors: Array of resistor values per channel
> + * @reg_config: Register value of INA3221_CONFIG
>   */
>  struct ina3221_data {
>  	struct regmap *regmap;
>  	struct regmap_field *fields[F_MAX_FIELDS];
>  	int shunt_resistors[INA3221_NUM_CHANNELS];
> +	u32 reg_config;
>  };
>  
>  static int ina3221_read_value(struct ina3221_data *ina, unsigned int reg,
> @@ -415,8 +417,59 @@ static int ina3221_probe(struct i2c_client *client,
>  		return PTR_ERR(hwmon_dev);
>  	}
>  
> +	dev_set_drvdata(dev, ina);
> +
> +	return 0;
> +}
> +
> +#ifdef CONFIG_PM
> +static int ina3221_suspend(struct device *dev)
> +{
> +	struct ina3221_data *ina = dev_get_drvdata(dev);
> +	int ret;
> +
> +	/* Save config register value and enable cache-only */
> +	ret = regmap_read(ina->regmap, INA3221_CONFIG, &ina->reg_config);
> +	if (ret)
> +		return ret;
> +

Would it make sense to explicitly disable all channels here ?

Thanks,
Guenter

> +	regcache_cache_only(ina->regmap, true);
> +	regcache_mark_dirty(ina->regmap);
> +
> +	return 0;
> +}
> +
> +static int ina3221_resume(struct device *dev)
> +{
> +	struct ina3221_data *ina = dev_get_drvdata(dev);
> +	int ret;
> +
> +	regcache_cache_only(ina->regmap, false);
> +
> +	/* Software reset the chip */
> +	ret = regmap_field_write(ina->fields[F_RST], true);
> +	if (ret) {
> +		dev_err(dev, "Unable to reset device\n");
> +		return ret;
> +	}
> +
> +	/* Restore cached register values to hardware */
> +	ret = regcache_sync(ina->regmap);
> +	if (ret)
> +		return ret;
> +
> +	/* Restore config register value to hardware */
> +	ret = regmap_write(ina->regmap, INA3221_CONFIG, ina->reg_config);
> +	if (ret)
> +		return ret;
> +
>  	return 0;
>  }
> +#endif
> +
> +static const struct dev_pm_ops ina3221_pm = {
> +	SET_SYSTEM_SLEEP_PM_OPS(ina3221_suspend, ina3221_resume)
> +};
>  
>  static const struct of_device_id ina3221_of_match_table[] = {
>  	{ .compatible = "ti,ina3221", },
> @@ -435,6 +488,7 @@ static struct i2c_driver ina3221_i2c_driver = {
>  	.driver = {
>  		.name = INA3221_DRIVER_NAME,
>  		.of_match_table = ina3221_of_match_table,
> +		.pm = &ina3221_pm,
>  	},
>  	.id_table = ina3221_ids,
>  };
> -- 
> 2.17.1
> 

  reply	other threads:[~2018-09-29 15:33 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-28 21:49 [PATCH 0/2] hwmon: ina3221: Add suspend and resume functions Nicolin Chen
2018-09-28 21:49 ` [PATCH 1/2] hwmon: ina3221: Add INA3221_CONFIG to volatile_table Nicolin Chen
2018-09-28 21:49 ` [PATCH 2/2] hwmon: ina3221: Add suspend and resume functions Nicolin Chen
2018-09-29 15:33   ` Guenter Roeck [this message]
2018-09-29 19:12     ` Nicolin Chen

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=20180929153346.GA26651@roeck-us.net \
    --to=linux@roeck-us.net \
    --cc=afd@ti.com \
    --cc=jdelvare@suse.com \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nicoleotsuka@gmail.com \
    /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;
as well as URLs for NNTP newsgroup(s).