Skip to contentSkip to conversation

mfd: twl-core: Reset twl_priv using devm_add_action_or_reset()

PATCH In review Lee Jones 2 participants 2 messages Nov 5 – Nov 7

Subsystems (2)
lore
Share & tools
lore

Lee Jones

author maintainer
#0 lore raw

PATCH 1/1 mfd: twl-core: Reset twl_priv using devm_add_action_or_reset()

Register a devres cleanup action immediately after allocating twl_priv with devm_kzalloc() so that twl_priv is automatically reset to NULL just before its underlying memory is released on probe failure or driver unbind. This removes the need to manually clear twl_priv in both twl_remove() and the probe error unwind path.

---
 drivers/mfd/twl-core.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
drivers/mfd/twl-core.c +10 -4
index 21fc0acb827b..6077a87282f5 100644
@@ -711,7 +711,6 @@ static void twl_remove(struct i2c_client *client)
 		twl->client = NULL;
 	}
 	twl_priv->ready = false;
-	twl_priv = NULL;
 }
 
 
@@ -728,6 +727,11 @@ static const struct mfd_cell twl6032_cells[] = {
 	{ .name = "twl6032-clk" },
 };
 
+static void twl_reset_priv(void *data)
+{
+	twl_priv = NULL;
+}
+
 /* NOTE: This driver only handles a single twl4030/tps659x0 chip */
 static int
 twl_probe(struct i2c_client *client)
@@ -776,6 +780,10 @@ twl_probe(struct i2c_client *client)
 		goto free;
 	}
 
+	status = devm_add_action_or_reset(&client->dev, twl_reset_priv, NULL);
+	if (status)
+		goto free;
+
 	if ((id->driver_data) & TWL6030_CLASS) {
 		twl_priv->twl_id = TWL6030_CLASS_ID;
 		twl_priv->twl_map = &twl6030_map[0];
@@ -906,10 +914,8 @@ twl_probe(struct i2c_client *client)
 	if (status < 0)
 		twl_remove(client);
 free:
-	if (status < 0) {
+	if (status < 0)
 		platform_device_unregister(pdev);
-		twl_priv = NULL;
-	}
 
 	return status;
 }
Signature · 1 line
2.56.0.398.g882cbad275-goog
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · On Thu, 5 Nov 2026 11:03:23 +0000 Lee Jones <[email protected]> wrote:

Register a devres cleanup action immediately after allocating twl_priv with devm_kzalloc() so that twl_priv is automatically reset to NULL just before its underlying memory is released on probe failure or driver unbind. This removes the need to manually clear twl_priv in both twl_remove() and the probe error unwind path.

Quoted 51 lines
---
 drivers/mfd/twl-core.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
drivers/mfd/twl-core.c +10 -4
index 21fc0acb827b..6077a87282f5 100644
@@ -711,7 +711,6 @@ static void twl_remove(struct i2c_client *client)
 		twl->client = NULL;
 	}
 	twl_priv->ready = false;
-	twl_priv = NULL;
 }
 
 
@@ -728,6 +727,11 @@ static const struct mfd_cell twl6032_cells[] = {
 	{ .name = "twl6032-clk" },
 };
 
+static void twl_reset_priv(void *data)
+{
+	twl_priv = NULL;
+}
+
 /* NOTE: This driver only handles a single twl4030/tps659x0 chip */
 static int
 twl_probe(struct i2c_client *client)
@@ -776,6 +780,10 @@ twl_probe(struct i2c_client *client)
 		goto free;
 	}
 
+	status = devm_add_action_or_reset(&client->dev, twl_reset_priv, NULL);
+	if (status)
+		goto free;
+
 	if ((id->driver_data) & TWL6030_CLASS) {
 		twl_priv->twl_id = TWL6030_CLASS_ID;
 		twl_priv->twl_map = &twl6030_map[0];
@@ -906,10 +914,8 @@ twl_probe(struct i2c_client *client)
 	if (status < 0)
 		twl_remove(client);
 free:
-	if (status < 0) {
+	if (status < 0)
 		platform_device_unregister(pdev);
-		twl_priv = NULL;
-	}
 
 	return status;
 }