[PATCH 03/12] pinctrl: Add optional fwnode to struct pinctrl_desc

From: Dmitry Torokhov

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


Pin controller drivers for MFD or auxiliary sub-devices often need to
associate their controller with a parent or child firmware node for DT
lookups and hog parsing. Attaching that node directly to the live
device during probe mutates dev->fwnode and dev->of_node concurrently
with readers such as uevent_show() and leads to reference count
underflows, leaks, or rebind conflicts on release.

Add an optional fwnode field to struct pinctrl_desc so drivers can
supply a controller firmware node directly, mirroring struct gpio_chip.
Keep the pinctrl core DT-agnostic by holding firmware node references
in struct pinctrl_dev and struct pinctrl, replacing
get_pinctrl_dev_from_of_node() with get_pinctrl_dev_from_fwnode(), and
updating devicetree.c to convert between struct device_node and struct
fwnode_handle. Release both references before freeing the structs on
teardown.

Signed-off-by: Dmitry Torokhov <dmitry.torokhov@xxxxxxxxx>
---
drivers/pinctrl/core.c | 18 ++++++++++++++----
drivers/pinctrl/core.h | 10 ++++++++--
drivers/pinctrl/devicetree.c | 19 +++++++------------
include/linux/pinctrl/pinctrl.h | 5 +++++
4 files changed, 34 insertions(+), 18 deletions(-)

diff --git a/drivers/pinctrl/core.c b/drivers/pinctrl/core.c
index 4968747a9650..df328ec5db25 100644
--- a/drivers/pinctrl/core.c
+++ b/drivers/pinctrl/core.c
@@ -21,6 +21,7 @@
#include <linux/init.h>
#include <linux/kref.h>
#include <linux/list.h>
+#include <linux/property.h>
#include <linux/seq_file.h>
#include <linux/slab.h>

@@ -121,14 +122,17 @@ struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *devname)
return NULL;
}

-struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np)
+struct pinctrl_dev *get_pinctrl_dev_from_fwnode(const struct fwnode_handle *fwnode)
{
struct pinctrl_dev *pctldev;

+ if (!fwnode)
+ return NULL;
+
mutex_lock(&pinctrldev_list_mutex);

list_for_each_entry(pctldev, &pinctrldev_list, node)
- if (device_match_of_node(pctldev->dev, np)) {
+ if (pctldev->fwnode == fwnode) {
mutex_unlock(&pinctrldev_list_mutex);
return pctldev;
}
@@ -1111,11 +1115,13 @@ static struct pinctrl *create_pinctrl(struct device *dev,
if (!p)
return ERR_PTR(-ENOMEM);
p->dev = dev;
+ p->fwnode = fwnode_handle_get(pctldev ? pctldev->fwnode : dev_fwnode(dev));
INIT_LIST_HEAD(&p->states);
INIT_LIST_HEAD(&p->dt_maps);

ret = pinctrl_dt_to_map(p, pctldev);
if (ret < 0) {
+ fwnode_handle_put(p->fwnode);
kfree(p);
return ERR_PTR(ret);
}
@@ -1242,6 +1248,7 @@ static void pinctrl_free(struct pinctrl *p, bool inlist)

if (inlist)
list_del(&p->node);
+ fwnode_handle_put(p->fwnode);
kfree(p);
mutex_unlock(&pinctrl_list_mutex);
}
@@ -2157,6 +2164,7 @@ pinctrl_init_controller(const struct pinctrl_desc *pctldesc, struct device *dev,
INIT_LIST_HEAD(&pctldev->gpio_ranges);
INIT_LIST_HEAD(&pctldev->node);
pctldev->dev = dev;
+ pctldev->fwnode = fwnode_handle_get(pctldesc->fwnode ?: dev_fwnode(dev));
mutex_init(&pctldev->mutex);

/* check core ops for sanity */
@@ -2194,6 +2202,7 @@ pinctrl_init_controller(const struct pinctrl_desc *pctldesc, struct device *dev,

out_err:
mutex_destroy(&pctldev->mutex);
+ fwnode_handle_put(pctldev->fwnode);
kfree(pctldev);
return ERR_PTR(ret);
}
@@ -2201,9 +2210,9 @@ pinctrl_init_controller(const struct pinctrl_desc *pctldesc, struct device *dev,
static void pinctrl_uninit_controller(struct pinctrl_dev *pctldev,
const struct pinctrl_desc *pctldesc)
{
- pinctrl_free_pindescs(pctldev, pctldesc->pins,
- pctldesc->npins);
+ pinctrl_free_pindescs(pctldev, pctldesc->pins, pctldesc->npins);
mutex_destroy(&pctldev->mutex);
+ fwnode_handle_put(pctldev->fwnode);
kfree(pctldev);
}

@@ -2361,6 +2370,7 @@ void pinctrl_unregister(struct pinctrl_dev *pctldev)

mutex_unlock(&pctldev->mutex);
mutex_destroy(&pctldev->mutex);
+ fwnode_handle_put(pctldev->fwnode);
kfree(pctldev);
mutex_unlock(&pinctrldev_list_mutex);
}
diff --git a/drivers/pinctrl/core.h b/drivers/pinctrl/core.h
index fc513a9cdd4f..2ac402788463 100644
--- a/drivers/pinctrl/core.h
+++ b/drivers/pinctrl/core.h
@@ -18,7 +18,7 @@

struct dentry;
struct device;
-struct device_node;
+struct fwnode_handle;
struct module;

struct pinctrl;
@@ -40,6 +40,8 @@ struct pinctrl_state;
* @gpio_ranges: a list of GPIO ranges that is handled by this pin controller,
* ranges are added to this list at runtime
* @dev: the device entry for this pin controller
+ * @fwnode: firmware node for this pin controller; reference held until
+ * unregistration
* @owner: module providing the pin controller, used for refcounting
* @driver_data: driver data for drivers registering to the pin controller
* subsystem
@@ -63,6 +65,7 @@ struct pinctrl_dev {
#endif
struct list_head gpio_ranges;
struct device *dev;
+ struct fwnode_handle *fwnode;
struct module *owner;
void *driver_data;
struct pinctrl *p;
@@ -78,6 +81,8 @@ struct pinctrl_dev {
* struct pinctrl - per-device pin control state holder
* @node: global list node
* @dev: the device using this pin control handle
+ * @fwnode: firmware node for this handle; reference held for the handle's
+ * lifetime
* @states: a list of states for this device
* @state: the current state
* @dt_maps: the mapping table chunks dynamically parsed from device tree for
@@ -87,6 +92,7 @@ struct pinctrl_dev {
struct pinctrl {
struct list_head node;
struct device *dev;
+ struct fwnode_handle *fwnode;
struct list_head states;
struct pinctrl_state *state;
struct list_head dt_maps;
@@ -236,7 +242,7 @@ int pinctrl_generic_remove_group(struct pinctrl_dev *pctldev,
#endif /* CONFIG_GENERIC_PINCTRL_GROUPS */

struct pinctrl_dev *get_pinctrl_dev_from_devname(const char *dev_name);
-struct pinctrl_dev *get_pinctrl_dev_from_of_node(struct device_node *np);
+struct pinctrl_dev *get_pinctrl_dev_from_fwnode(const struct fwnode_handle *fwnode);
int pin_get_from_name(struct pinctrl_dev *pctldev, const char *name);
const char *pin_get_name(struct pinctrl_dev *pctldev, const unsigned int pin);
int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
diff --git a/drivers/pinctrl/devicetree.c b/drivers/pinctrl/devicetree.c
index f61a8c75e9e2..ce7176636b68 100644
--- a/drivers/pinctrl/devicetree.c
+++ b/drivers/pinctrl/devicetree.c
@@ -59,8 +59,6 @@ void pinctrl_dt_free_maps(struct pinctrl *p)
dt_map->num_maps);
kfree(dt_map);
}
-
- of_node_put(p->dev->of_node);
}

static int dt_remember_or_free_map(struct pinctrl *p, const char *statename,
@@ -107,12 +105,12 @@ static int dt_remember_or_free_map(struct pinctrl *p, const char *statename,

struct pinctrl_dev *of_pinctrl_get(struct device_node *np)
{
- return get_pinctrl_dev_from_of_node(np);
+ return get_pinctrl_dev_from_fwnode(of_fwnode_handle(np));
}
EXPORT_SYMBOL_GPL(of_pinctrl_get);

static struct pinctrl_dev *dt_find_controller(struct device_node *np_config,
- const struct device_node *stop_node)
+ const struct fwnode_handle *stop_fwnode)
{
struct device_node *np_pctldev __free(device_node) = of_node_get(np_config);
struct pinctrl_dev *pctldev;
@@ -129,9 +127,9 @@ static struct pinctrl_dev *dt_find_controller(struct device_node *np_config,
return ERR_PTR(-EPROBE_DEFER);
return ERR_PTR(-ENODEV);
}
- if (np_pctldev == stop_node)
+ if (of_fwnode_handle(np_pctldev) == stop_fwnode)
return NULL;
- pctldev = get_pinctrl_dev_from_of_node(np_pctldev);
+ pctldev = get_pinctrl_dev_from_fwnode(of_fwnode_handle(np_pctldev));
if (pctldev)
return pctldev;
}
@@ -148,7 +146,7 @@ static int dt_to_map_one_config(struct pinctrl *p,
struct pinctrl_map *map;
unsigned int num_maps;

- pctldev = dt_find_controller(np_config, p->dev->of_node);
+ pctldev = dt_find_controller(np_config, p->fwnode);
if (IS_ERR(pctldev))
return PTR_ERR(pctldev);
if (!pctldev) {
@@ -210,7 +208,7 @@ static int dt_remember_dummy_state(struct pinctrl *p, const char *statename)

int pinctrl_dt_to_map(struct pinctrl *p, struct pinctrl_dev *pctldev)
{
- struct device_node *np = p->dev->of_node;
+ struct device_node *np = to_of_node(p->fwnode);
int state, ret;
char *propname;
struct property *prop;
@@ -220,7 +218,7 @@ int pinctrl_dt_to_map(struct pinctrl *p, struct pinctrl_dev *pctldev)
phandle phandle;
struct device_node *np_config;

- /* CONFIG_OF enabled, p->dev not instantiated from DT */
+ /* CONFIG_OF enabled, no DT node on device or pctldev->fwnode */
if (!np) {
if (of_have_populated_dt())
dev_dbg(p->dev,
@@ -228,9 +226,6 @@ int pinctrl_dt_to_map(struct pinctrl *p, struct pinctrl_dev *pctldev)
return 0;
}

- /* We may store pointers to property names within the node */
- of_node_get(np);
-
/* For each defined state ID */
for (state = 0; ; state++) {
/* Retrieve the pinctrl-* property */
diff --git a/include/linux/pinctrl/pinctrl.h b/include/linux/pinctrl/pinctrl.h
index c329cc693139..82689052c4c1 100644
--- a/include/linux/pinctrl/pinctrl.h
+++ b/include/linux/pinctrl/pinctrl.h
@@ -16,6 +16,7 @@

struct device;
struct device_node;
+struct fwnode_handle;
struct gpio_chip;
struct module;
struct seq_file;
@@ -143,6 +144,9 @@ struct pinctrl_ops {
* the hardware description
* @custom_conf_items: Information how to print @params in debugfs, must be
* the same size as the @custom_params, i.e. @num_custom_params
+ * @fwnode: Optional firmware node for the pin controller. If unset,
+ * the core falls back to the firmware node of the parent device.
+ * The core acquires its own reference to the node.
* @link_consumers: If true create a device link between pinctrl and its
* consumers (i.e. the devices requesting pin control states). This is
* sometimes necessary to ascertain the right suspend/resume order for
@@ -161,6 +165,7 @@ struct pinctrl_desc {
const struct pinconf_generic_params *custom_params;
const struct pin_config_item *custom_conf_items;
#endif
+ struct fwnode_handle *fwnode;
bool link_consumers;
};


--
2.56.1.987.g2ec0ab4260-goog