Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] mfd: stmpe: Disable regulators on probe failure
@ 2026-11-22 11:35 Haoran Wang
  2026-11-27 15:07 ` Lee Jones
  0 siblings, 1 reply; 2+ messages in thread
From: Haoran Wang @ 2026-11-22 11:35 UTC (permalink / raw)
  To: Lee Jones
  Cc: Maxime Coquelin, Alexandre Torgue, mfd, linux-stm32,
	linux-arm-kernel, linux-kernel

The VCC and VIO supplies are enabled in stmpe_probe() but were never
disabled if probing failed later, leaking the enabled regulators.

Register a devm action to disable the supplies, and track whether each
supply was enabled in dedicated flags instead of consulting
regulator_is_enabled(), which reports the hardware state rather than
whether this consumer enabled it.  Drop the manual regulator_disable()
calls from stmpe_remove() so the supplies are disabled exactly once via
the devres action.

Fixes: 9c9e321455fb ("mfd: stmpe: add optional regulators")
Signed-off-by: Haoran Wang <wanghr2026@mail.zgclab.edu.cn>
---
 drivers/mfd/stmpe.c       | 24 +++++++++++++++++++-----
 include/linux/mfd/stmpe.h |  2 ++
 2 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/drivers/mfd/stmpe.c b/drivers/mfd/stmpe.c
index fd51ba74275c..fb30b8a4fbac 100644
--- a/drivers/mfd/stmpe.c
+++ b/drivers/mfd/stmpe.c
@@ -1362,6 +1362,16 @@ static void stmpe_of_probe(struct stmpe_platform_data *pdata,
 	}
 }
 
+static void stmpe_disable_regulators(void *data)
+{
+	struct stmpe *stmpe = data;
+
+	if (stmpe->vio_enabled)
+		regulator_disable(stmpe->vio);
+	if (stmpe->vcc_enabled)
+		regulator_disable(stmpe->vcc);
+}
+
 /* Called from client specific probe routines */
 int stmpe_probe(struct stmpe_client_info *ci, enum stmpe_partnum partnum)
 {
@@ -1410,13 +1420,22 @@ int stmpe_probe(struct stmpe_client_info *ci, enum stmpe_partnum partnum)
 		ret = regulator_enable(stmpe->vcc);
 		if (ret)
 			dev_warn(ci->dev, "failed to enable VCC supply\n");
+		else
+			stmpe->vcc_enabled = true;
 	}
 	stmpe->vio = devm_regulator_get_optional(ci->dev, "vio");
 	if (!IS_ERR(stmpe->vio)) {
 		ret = regulator_enable(stmpe->vio);
 		if (ret)
 			dev_warn(ci->dev, "failed to enable VIO supply\n");
+		else
+			stmpe->vio_enabled = true;
 	}
+
+	ret = devm_add_action_or_reset(ci->dev, stmpe_disable_regulators, stmpe);
+	if (ret)
+		return ret;
+
 	dev_set_drvdata(stmpe->dev, stmpe);
 
 	if (ci->init)
@@ -1489,11 +1508,6 @@ void stmpe_remove(struct stmpe *stmpe)
 	if (stmpe->domain)
 		irq_domain_remove(stmpe->domain);
 
-	if (!IS_ERR(stmpe->vio) && regulator_is_enabled(stmpe->vio))
-		regulator_disable(stmpe->vio);
-	if (!IS_ERR(stmpe->vcc) && regulator_is_enabled(stmpe->vcc))
-		regulator_disable(stmpe->vcc);
-
 	__stmpe_disable(stmpe, STMPE_BLOCK_ADC);
 
 	mfd_remove_devices(stmpe->dev);
diff --git a/include/linux/mfd/stmpe.h b/include/linux/mfd/stmpe.h
index 87e29d561e22..eec5fc795014 100644
--- a/include/linux/mfd/stmpe.h
+++ b/include/linux/mfd/stmpe.h
@@ -122,6 +122,8 @@ struct stmpe_platform_data;
 struct stmpe {
 	struct regulator *vcc;
 	struct regulator *vio;
+	bool vcc_enabled;
+	bool vio_enabled;
 	struct mutex lock;
 	struct mutex irq_lock;
 	struct device *dev;



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] mfd: stmpe: Disable regulators on probe failure
  2026-11-22 11:35 [PATCH v2] mfd: stmpe: Disable regulators on probe failure Haoran Wang
@ 2026-11-27 15:07 ` Lee Jones
  0 siblings, 0 replies; 2+ messages in thread
From: Lee Jones @ 2026-11-27 15:07 UTC (permalink / raw)
  To: Haoran Wang
  Cc: Maxime Coquelin, Alexandre Torgue, mfd, linux-stm32,
	linux-arm-kernel, linux-kernel

On Sun, 22 Nov 2026, Haoran Wang wrote:

> The VCC and VIO supplies are enabled in stmpe_probe() but were never
> disabled if probing failed later, leaking the enabled regulators.
>
> Register a devm action to disable the supplies, and track whether each
> supply was enabled in dedicated flags instead of consulting
> regulator_is_enabled(), which reports the hardware state rather than
> whether this consumer enabled it.  Drop the manual regulator_disable()
> calls from stmpe_remove() so the supplies are disabled exactly once via
> the devres action.
>
> Fixes: 9c9e321455fb ("mfd: stmpe: add optional regulators")
> Signed-off-by: Haoran Wang <wanghr2026@mail.zgclab.edu.cn>
> ---

Changelog?

>  drivers/mfd/stmpe.c       | 24 +++++++++++++++++++-----
>  include/linux/mfd/stmpe.h |  2 ++
>  2 files changed, 21 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/mfd/stmpe.c b/drivers/mfd/stmpe.c
> index fd51ba74275c..fb30b8a4fbac 100644
> --- a/drivers/mfd/stmpe.c
> +++ b/drivers/mfd/stmpe.c
> @@ -1362,6 +1362,16 @@ static void stmpe_of_probe(struct stmpe_platform_data *pdata,
>  	}
>  }
>
> +static void stmpe_disable_regulators(void *data)
> +{
> +	struct stmpe *stmpe = data;
> +
> +	if (stmpe->vio_enabled)
> +		regulator_disable(stmpe->vio);
> +	if (stmpe->vcc_enabled)
> +		regulator_disable(stmpe->vcc);
> +}

Yeah, this is worse!

Instead of manually tracking these, why not use devm_add_action_or_reset() for each?

    static void stmpe_disable_regulator(void *regulator)
    {
            regulator_disable(regulator);
    }

Then, when regulator_enable() succeeds:

    ret = devm_add_action_or_reset(ci->dev, stmpe_disable_regulator, stmpe->vcc);
    if (ret)
            return ret;

    ret = devm_add_action_or_reset(ci->dev, stmpe_disable_regulator, stmpe->vio);
    if (ret)
            return ret;

>  /* Called from client specific probe routines */
>  int stmpe_probe(struct stmpe_client_info *ci, enum stmpe_partnum partnum)
>  {
> @@ -1410,13 +1420,22 @@ int stmpe_probe(struct stmpe_client_info *ci, enum stmpe_partnum partnum)
>  		ret = regulator_enable(stmpe->vcc);
>  		if (ret)
>  			dev_warn(ci->dev, "failed to enable VCC supply\n");
> +		else
> +			stmpe->vcc_enabled = true;
>  	}
>  	stmpe->vio = devm_regulator_get_optional(ci->dev, "vio");
>  	if (!IS_ERR(stmpe->vio)) {
>  		ret = regulator_enable(stmpe->vio);
>  		if (ret)
>  			dev_warn(ci->dev, "failed to enable VIO supply\n");
> +		else
> +			stmpe->vio_enabled = true;
>  	}
> +
> +	ret = devm_add_action_or_reset(ci->dev, stmpe_disable_regulators, stmpe);
> +	if (ret)
> +		return ret;
> +
>  	dev_set_drvdata(stmpe->dev, stmpe);
>
>  	if (ci->init)
> @@ -1489,11 +1508,6 @@ void stmpe_remove(struct stmpe *stmpe)
>  	if (stmpe->domain)
>  		irq_domain_remove(stmpe->domain);
>
> -	if (!IS_ERR(stmpe->vio) && regulator_is_enabled(stmpe->vio))
> -		regulator_disable(stmpe->vio);
> -	if (!IS_ERR(stmpe->vcc) && regulator_is_enabled(stmpe->vcc))
> -		regulator_disable(stmpe->vcc);
> -
>  	__stmpe_disable(stmpe, STMPE_BLOCK_ADC);
>
>  	mfd_remove_devices(stmpe->dev);
> diff --git a/include/linux/mfd/stmpe.h b/include/linux/mfd/stmpe.h
> index 87e29d561e22..eec5fc795014 100644
> --- a/include/linux/mfd/stmpe.h
> +++ b/include/linux/mfd/stmpe.h
> @@ -122,6 +122,8 @@ struct stmpe_platform_data;
>  struct stmpe {
>  	struct regulator *vcc;
>  	struct regulator *vio;
> +	bool vcc_enabled;
> +	bool vio_enabled;
>  	struct mutex lock;
>  	struct mutex irq_lock;
>  	struct device *dev;
>

-- 
Lee Jones


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-27 15:07 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-22 11:35 [PATCH v2] mfd: stmpe: Disable regulators on probe failure Haoran Wang
2026-11-27 15:07 ` Lee Jones

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox