From: Jonathan Cameron <jic23@kernel.org>
To: Anson Huang <anson.huang@nxp.com>
Cc: "knaack.h@gmx.de" <knaack.h@gmx.de>,
"lars@metafoo.de" <lars@metafoo.de>,
"pmeerw@pmeerw.net" <pmeerw@pmeerw.net>,
"linux-iio@vger.kernel.org" <linux-iio@vger.kernel.org>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
"festevam@gmail.com" <festevam@gmail.com>,
"preid@electromag.com.au" <preid@electromag.com.au>,
dl-linux-imx <linux-imx@nxp.com>
Subject: Re: [PATCH V7] iio: light: isl29018: add vcc regulator operation support
Date: Sat, 22 Dec 2018 17:14:34 +0000 [thread overview]
Message-ID: <20181222171434.4eca4ea4@archlinux> (raw)
In-Reply-To: <1545016752-9865-1-git-send-email-Anson.Huang@nxp.com>
On Mon, 17 Dec 2018 03:25:17 +0000
Anson Huang <anson.huang@nxp.com> wrote:
> The light sensor's power supply could be controllable by regulator
> on some platforms, such as i.MX6Q-SABRESD board, the light sensor
> isl29023's power supply is controlled by a GPIO fixed regulator,
> need to make sure the regulator is enabled before any operation of
> sensor, this patch adds vcc regulator operation support.
>
> Signed-off-by: Anson Huang <Anson.Huang@nxp.com>
Hi Anson
See below.
> ---
> ChangeLog since V6
> - using devm_regulator_get() instead of devm_regulator_get_optional() since the regulator is
> there anyway, if dtb does NOT specify one, regulator framework will assign dummy regulator for it;
> - Setup devm action for cleaning up regulator resource for error handling.
> ---
> drivers/iio/light/isl29018.c | 58 ++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 58 insertions(+)
>
> diff --git a/drivers/iio/light/isl29018.c b/drivers/iio/light/isl29018.c
> index b45400f..63f7b9d 100644
> --- a/drivers/iio/light/isl29018.c
> +++ b/drivers/iio/light/isl29018.c
> @@ -23,6 +23,7 @@
> #include <linux/mutex.h>
> #include <linux/delay.h>
> #include <linux/regmap.h>
> +#include <linux/regulator/consumer.h>
> #include <linux/slab.h>
> #include <linux/iio/iio.h>
> #include <linux/iio/sysfs.h>
> @@ -95,6 +96,7 @@ struct isl29018_chip {
> struct isl29018_scale scale;
> int prox_scheme;
> bool suspended;
> + struct regulator *vcc_reg;
> };
>
> static int isl29018_set_integration_time(struct isl29018_chip *chip,
> @@ -708,6 +710,17 @@ static const char *isl29018_match_acpi_device(struct device *dev, int *data)
> return dev_name(dev);
> }
>
> +static void isl29018_disable_regulator_action(void *_data)
> +{
> + struct isl29018_chip *chip = _data;
> + int err;
> +
> + err = regulator_disable(chip->vcc_reg);
> + if (err)
> + dev_err(regmap_get_device(chip->regmap),
> + "failed to disable VCC regulator!\n");
> +}
> +
> static int isl29018_probe(struct i2c_client *client,
> const struct i2c_device_id *id)
> {
> @@ -742,6 +755,37 @@ static int isl29018_probe(struct i2c_client *client,
> chip->scale = isl29018_scales[chip->int_time][0];
> chip->suspended = false;
>
> + chip->vcc_reg = devm_regulator_get(&client->dev, "vcc");
> + if (IS_ERR(chip->vcc_reg)) {
> + err = PTR_ERR(chip->vcc_reg);
> + if (err != -EPROBE_DEFER)
> + dev_err(&client->dev, "failed to get VCC regulator!\n");
> + return err;
> + }
> +
> + err = regulator_enable(chip->vcc_reg);
> + if (err) {
> + dev_err(&client->dev, "failed to enable VCC regulator!\n");
> + return err;
> + }
> +
> + chip->regmap = devm_regmap_init_i2c(client,
> + isl29018_chip_info_tbl[dev_id].regmap_cfg);
> + if (IS_ERR(chip->regmap)) {
> + err = PTR_ERR(chip->regmap);
> + dev_err(&client->dev, "regmap initialization fails: %d\n", err);
> + regulator_disable(chip->vcc_reg);
> + return err;
> + }
> +
> + err = devm_add_action(&client->dev, isl29018_disable_regulator_action,
> + chip);
> + if (err) {
I'm a little confused, why not do this before devm_regmap_init_i2c.
That way you won't have to disable the regulator in that one error path.
Also, devm_add_action_or_reset will call isl29018_disable_regulator_action for
you on error.
> + isl29018_disable_regulator_action(chip);
> + dev_err(&client->dev, "failed to setup regulator cleanup action!\n");
> + return err;
> + }
> +
> chip->regmap = devm_regmap_init_i2c(client,
> isl29018_chip_info_tbl[dev_id].regmap_cfg);
> if (IS_ERR(chip->regmap)) {
> @@ -768,6 +812,7 @@ static int isl29018_probe(struct i2c_client *client,
> static int isl29018_suspend(struct device *dev)
> {
> struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
> + int ret;
>
> mutex_lock(&chip->lock);
>
> @@ -777,6 +822,12 @@ static int isl29018_suspend(struct device *dev)
> * So we do not have much to do here.
> */
> chip->suspended = true;
> + ret = regulator_disable(chip->vcc_reg);
> + if (ret) {
> + dev_err(dev, "failed to disable VCC regulator\n");
> + mutex_unlock(&chip->lock);
> + return ret;
Given you are about to unlock anyway a common pattern is to not
check ret until after the unlock, thus simplifying the code.
> + }
>
> mutex_unlock(&chip->lock);
>
> @@ -790,6 +841,13 @@ static int isl29018_resume(struct device *dev)
>
> mutex_lock(&chip->lock);
>
> + err = regulator_enable(chip->vcc_reg);
> + if (err) {
> + dev_err(dev, "failed to enable VCC regulator\n");
> + mutex_unlock(&chip->lock);
> + return err;
> + }
> +
> err = isl29018_chip_init(chip);
> if (!err)
> chip->suspended = false;
next prev parent reply other threads:[~2018-12-22 17:14 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-12-17 3:25 [PATCH V7] iio: light: isl29018: add vcc regulator operation support Anson Huang
2018-12-22 17:14 ` Jonathan Cameron [this message]
2018-12-23 6:19 ` Anson Huang
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=20181222171434.4eca4ea4@archlinux \
--to=jic23@kernel.org \
--cc=anson.huang@nxp.com \
--cc=festevam@gmail.com \
--cc=knaack.h@gmx.de \
--cc=lars@metafoo.de \
--cc=linux-iio@vger.kernel.org \
--cc=linux-imx@nxp.com \
--cc=linux-kernel@vger.kernel.org \
--cc=pmeerw@pmeerw.net \
--cc=preid@electromag.com.au \
/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.