[PATCH 01/12] pinctrl: devicetree: Factor out dt_find_controller() helper

From: Dmitry Torokhov

Date: Sat Dec 05 2026 - 01:39:28 EST


Move the ancestor walk that locates the pin controller for a DT
configuration node out of dt_to_map_one_config() into a new
dt_find_controller() helper using __free(device_node) to manage the
device node reference across all exit paths. Return an ERR_PTR() on
lookup failure, NULL when skipping a self-referenced hog, or the matched
controller pointer, eliminating the magic return value of 1 between
dt_to_map_one_config() and pinctrl_dt_to_map().

No functional change intended.

Signed-off-by: Dmitry Torokhov <dmitry.torokhov@xxxxxxxxx>
---
drivers/pinctrl/devicetree.c | 71 ++++++++++++++++++++++----------------------
1 file changed, 36 insertions(+), 35 deletions(-)

diff --git a/drivers/pinctrl/devicetree.c b/drivers/pinctrl/devicetree.c
index 465b43092eb7..69e701cb9c1f 100644
--- a/drivers/pinctrl/devicetree.c
+++ b/drivers/pinctrl/devicetree.c
@@ -6,6 +6,7 @@
*/

#include <linux/device.h>
+#include <linux/err.h>
#include <linux/of.h>
#include <linux/pinctrl/pinctrl.h>
#include <linux/slab.h>
@@ -110,54 +111,57 @@ struct pinctrl_dev *of_pinctrl_get(struct device_node *np)
}
EXPORT_SYMBOL_GPL(of_pinctrl_get);

-static int dt_to_map_one_config(struct pinctrl *p,
- struct pinctrl_dev *hog_pctldev,
- const char *statename,
- struct device_node *np_config)
+static struct pinctrl_dev *dt_find_controller(struct pinctrl *p,
+ struct pinctrl_dev *hog_pctldev,
+ struct device_node *np_config)
{
- struct pinctrl_dev *pctldev = NULL;
- struct device_node *np_pctldev;
- const struct pinctrl_ops *ops;
- int ret;
- struct pinctrl_map *map;
- unsigned int num_maps;
+ struct device_node *np_pctldev __free(device_node) = of_node_get(np_config);
+ struct pinctrl_dev *pctldev;
bool allow_default = false;

- /* Find the pin controller containing np_config */
- np_pctldev = of_node_get(np_config);
for (;;) {
if (!allow_default)
- allow_default = of_property_read_bool(np_pctldev,
- "pinctrl-use-default");
+ allow_default = of_property_read_bool(np_pctldev, "pinctrl-use-default");

np_pctldev = of_get_next_parent(np_pctldev);
if (!np_pctldev || of_node_is_root(np_pctldev)) {
- of_node_put(np_pctldev);
- ret = -ENODEV;
/* keep deferring if modules are enabled */
- if (IS_ENABLED(CONFIG_MODULES) && !allow_default && ret < 0)
- ret = -EPROBE_DEFER;
- return ret;
+ if (IS_ENABLED(CONFIG_MODULES) && !allow_default)
+ return ERR_PTR(-EPROBE_DEFER);
+ return ERR_PTR(-ENODEV);
}
/* If we're creating a hog we can use the passed pctldev */
- if (hog_pctldev && (np_pctldev == p->dev->of_node)) {
- pctldev = hog_pctldev;
- break;
- }
+ if (hog_pctldev && np_pctldev == p->dev->of_node)
+ return hog_pctldev;
pctldev = get_pinctrl_dev_from_of_node(np_pctldev);
if (pctldev)
- break;
+ return pctldev;
/*
- * Do not defer probing of hogs (circular loop)
+ * Do not defer probing of hogs (circular loop).
*
- * Return 1 to let the caller catch the case.
+ * Return NULL to let the caller skip self-referenced configs.
*/
- if (np_pctldev == p->dev->of_node) {
- of_node_put(np_pctldev);
- return 1;
- }
+ if (np_pctldev == p->dev->of_node)
+ return NULL;
}
- of_node_put(np_pctldev);
+}
+
+static int dt_to_map_one_config(struct pinctrl *p,
+ struct pinctrl_dev *hog_pctldev,
+ const char *statename,
+ struct device_node *np_config)
+{
+ struct pinctrl_dev *pctldev;
+ const struct pinctrl_ops *ops;
+ int ret;
+ struct pinctrl_map *map;
+ unsigned int num_maps;
+
+ pctldev = dt_find_controller(p, hog_pctldev, np_config);
+ if (IS_ERR(pctldev))
+ return PTR_ERR(pctldev);
+ if (!pctldev)
+ return 0;

/*
* Call pinctrl driver to parse device tree node, and
@@ -270,11 +274,8 @@ int pinctrl_dt_to_map(struct pinctrl *p, struct pinctrl_dev *pctldev)
}

/* Parse the node */
- ret = dt_to_map_one_config(p, pctldev, statename,
- np_config);
+ ret = dt_to_map_one_config(p, pctldev, statename, np_config);
of_node_put(np_config);
- if (ret == 1)
- continue;
if (ret < 0)
goto err;
}

--
2.56.1.987.g2ec0ab4260-goog