From: neil.armstrong@linaro.org
To: kr494167@gmail.com, broonie@kernel.org, lgirdwood@gmail.com
Cc: kancy2333@outlook.com, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] regulator: sgm3804: handle gpiod_get_value_cansleep() error returns
Date: Fri, 24 Jul 2026 10:29:51 +0200 [thread overview]
Message-ID: <646038d3-c4b0-4892-9e8a-29cd498dd317@linaro.org> (raw)
In-Reply-To: <20260724024625.11057-1-kr494167@gmail.com>
On 7/24/26 04:46, kr494167@gmail.com wrote:
> From: Surendra Singh Chouhan <kr494167@gmail.com>
>
> sgm3804_sync_regcache_state() evaluated !gpiod_get_value_cansleep(...) to
> check if the enable GPIOs were down.
>
> gpiod_get_value_cansleep() returns 1 if active, 0 if inactive, and a
> negative error code (e.g. -EIO or -EINVAL) on failure. Evaluating
> !gpiod_get_value_cansleep(...) treats a negative error code as boolean
> false, swallowing GPIO read failures and misinterpreting a failed read as
> an active GPIO. This improperly attempts to sync regcache registers
> when the IC may be powered down.
>
> Fix this by capturing the return values of gpiod_get_value_cansleep() for
> both positive and negative rails and propagating any error immediately.
> In addition, replace mutex_init() with devm_mutex_init() in probe().
>
> Fixes: 0c47e1a8cf5d ("regulator: add SGM3804 Dual Output driver")
> Signed-off-by: Surendra Singh Chouhan <kr494167@gmail.com>
> ---
> drivers/regulator/sgm3804-regulator.c | 17 ++++++++++++++---
> 1 file changed, 14 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/regulator/sgm3804-regulator.c b/drivers/regulator/sgm3804-regulator.c
> index c3406cfb73d0..a430c89b4d5d 100644
> --- a/drivers/regulator/sgm3804-regulator.c
> +++ b/drivers/regulator/sgm3804-regulator.c
> @@ -97,11 +97,20 @@ static const struct regmap_config sgm3804_regmap_config = {
>
> static int sgm3804_sync_regcache_state(struct sgm3804_data *ctx)
> {
> + int pos, neg;
> +
> guard(mutex)(&ctx->lock);
>
> + pos = gpiod_get_value_cansleep(ctx->gpios[SGM3804_POS_RAIL]);
> + if (pos < 0)
> + return pos;
> +
> + neg = gpiod_get_value_cansleep(ctx->gpios[SGM3804_NEG_RAIL]);
> + if (neg < 0)
> + return neg;
> +
> /* If both GPIOs are down, IC is powered down and I2C writes will fail */
> - if (!gpiod_get_value_cansleep(ctx->gpios[SGM3804_POS_RAIL]) &&
> - !gpiod_get_value_cansleep(ctx->gpios[SGM3804_NEG_RAIL])) {
> + if (!pos && !neg) {
> regcache_cache_only(ctx->regmap, true);
> regcache_mark_dirty(ctx->regmap);
> } else {
> @@ -235,7 +244,9 @@ static int sgm3804_probe(struct i2c_client *i2c)
> if (!ctx)
> return -ENOMEM;
>
> - mutex_init(&ctx->lock);
> + ret = devm_mutex_init(dev, &ctx->lock);
> + if (ret)
> + return ret;
>
> ctx->regmap = devm_regmap_init_i2c(i2c, &sgm3804_regmap_config);
> if (IS_ERR(ctx->regmap))
Reviewed-by: Neil Armstrong <neil.armstrong@linaro.org>
Thanks,
Neil
prev parent reply other threads:[~2026-07-24 8:29 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-24 2:46 kr494167
2026-07-24 8:29 ` neil.armstrong [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=646038d3-c4b0-4892-9e8a-29cd498dd317@linaro.org \
--to=neil.armstrong@linaro.org \
--cc=broonie@kernel.org \
--cc=kancy2333@outlook.com \
--cc=kr494167@gmail.com \
--cc=lgirdwood@gmail.com \
--cc=linux-kernel@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
all inboxes | Powered by JetHome®