[PATCH v2] mfd: stmpe: Disable regulators on probe failure

Haoran Wang posted 1 patch 22 hours ago
drivers/mfd/stmpe.c       | 24 +++++++++++++++++++-----
include/linux/mfd/stmpe.h |  2 ++
2 files changed, 21 insertions(+), 5 deletions(-)
[PATCH v2] mfd: stmpe: Disable regulators on probe failure
Posted by Haoran Wang 22 hours ago
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;