regulator: sgm3804: handle gpiod_get_value_cansleep() error returns
PATCH 1/1 regulator: sgm3804: handle gpiod_get_value_cansleep() error returns
From: Surendra Singh Chouhan <[email protected]>
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().
--- drivers/regulator/sgm3804-regulator.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)
drivers/regulator/sgm3804-regulator.c +14 -3
@@ -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))
Signature · 1 line
2.55.0
Quoted 50 lines · On 7/24/26 04:46, [email protected] wrote:
From: Surendra Singh Chouhan <[email protected]>
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.-EIOor-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, replacemutex_init()withdevm_mutex_init()inprobe().Fixes 0c47e1a8cf5d ("regulator: add SGM3804 Dual Output driver")Signed-off-by Surendra Singh Chouhan <[email protected]>--- drivers/regulator/sgm3804-regulator.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)drivers/regulator/sgm3804-regulator.c +11
-2@@ -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 {
drivers/regulator/sgm3804-regulator.c static int sgm3804_probe(struct i2c_client *i2c) · line 252
Quoted 10 lines
drivers/regulator/sgm3804-regulator.c +3
-1@@ -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))
Thanks,
Neil
Mark Brown
maintainerQuoted 1 line · On Fri, 24 Jul 2026 08:16:25 +0530, [email protected] wrote:
regulator: sgm3804: handle
gpiod_get_value_cansleep()error returns
Applied to
https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regulator.git for-7.5
Thanks!
[1/1] regulator: sgm3804: handle gpiod_get_value_cansleep() error returns
https://git.kernel.org/broonie/regulator/c/cdfbeace852d
All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.
You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.
If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.
Please add any relevant lists and maintainers to the CCs when replying to this mail.
Thanks,
Mark