mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Dmitry Torokhov <dmitry.torokhov@gmail.com>
To: Support Opensource <support.opensource@diasemi.com>,
	 Linus Walleij <linusw@kernel.org>,
	 Charles Keepax <ckeepax@opensource.cirrus.com>,
	 Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	 Laxman Dewangan <ldewangan@nvidia.com>,
	 Richard Fitzgerald <rf@opensource.cirrus.com>,
	Lee Jones <lee@kernel.org>,  Joseph Chen <chenjh@rock-chips.com>,
	Heiko Stuebner <heiko@sntech.de>,
	 Herve Codina <herve.codina@bootlin.com>,
	 Christophe Leroy <chleroy@kernel.org>,
	 David Rhodes <david.rhodes@cirrus.com>,
	 Mathieu Dubois-Briand <mathieu.dubois-briand@bootlin.com>,
	 Nikita Shubin <nikita.shubin@maquefel.me>
Cc: Bartosz Golaszewski <brgl@kernel.org>,
	 Haotian Zhang <vulab@iscas.ac.cn>,
	linux-gpio@vger.kernel.org,  linux-sound@vger.kernel.org,
	patches@opensource.cirrus.com,  linux-kernel@vger.kernel.org
Subject: [PATCH 03/12] pinctrl: Add optional fwnode to struct pinctrl_desc
Date: Fri, 04 Dec 2026 22:38:31 -0800	[thread overview]
Message-ID: <20261204-pinctrl-fwnode-fix-v1-3-8589cbd35fb2@gmail.com> (raw)
In-Reply-To: <20261204-pinctrl-fwnode-fix-v1-0-8589cbd35fb2@gmail.com>

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@gmail.com>
---
 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


  parent reply	other threads:[~2026-12-05  6:39 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05  6:38 [PATCH 00/12] pinctrl: Fix firmware node reference count underflows Dmitry Torokhov
2026-12-05  6:38 ` [PATCH 01/12] pinctrl: devicetree: Factor out dt_find_controller() helper Dmitry Torokhov
2026-12-05 12:01   ` Andy Shevchenko
2026-12-05  6:38 ` [PATCH 02/12] pinctrl: devicetree: Do not re-claim hogs for sibling devices Dmitry Torokhov
2026-12-05  6:38 ` Dmitry Torokhov [this message]
2026-12-05 12:07   ` [PATCH 03/12] pinctrl: Add optional fwnode to struct pinctrl_desc Andy Shevchenko
2026-12-05  6:38 ` [PATCH 04/12] pinctrl: da9062: Fix firmware node reference count underflow Dmitry Torokhov
2026-12-05  6:38 ` [PATCH 05/12] pinctrl: as3722: " Dmitry Torokhov
2026-12-05  6:38 ` [PATCH 06/12] pinctrl: max77620: " Dmitry Torokhov
2026-12-05  6:38 ` [PATCH 07/12] pinctrl: madera: " Dmitry Torokhov
2026-12-05  6:38 ` [PATCH 08/12] pinctrl: rk805: " Dmitry Torokhov
2026-12-05  6:38 ` [PATCH 09/12] pinctrl: pef2256: " Dmitry Torokhov
2026-12-05  6:38 ` [PATCH 10/12] pinctrl: cirrus: cs42l43: " Dmitry Torokhov
2026-12-05  6:38 ` [PATCH 11/12] pinctrl: max7360: Pass parent fwnode via pinctrl_desc Dmitry Torokhov
2026-12-05  6:38 ` [PATCH 12/12] pinctrl: ep93xx: " Dmitry Torokhov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204-pinctrl-fwnode-fix-v1-3-8589cbd35fb2@gmail.com \
    --to=dmitry.torokhov@gmail.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=brgl@kernel.org \
    --cc=chenjh@rock-chips.com \
    --cc=chleroy@kernel.org \
    --cc=ckeepax@opensource.cirrus.com \
    --cc=david.rhodes@cirrus.com \
    --cc=heiko@sntech.de \
    --cc=herve.codina@bootlin.com \
    --cc=ldewangan@nvidia.com \
    --cc=lee@kernel.org \
    --cc=linusw@kernel.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=mathieu.dubois-briand@bootlin.com \
    --cc=nikita.shubin@maquefel.me \
    --cc=patches@opensource.cirrus.com \
    --cc=rf@opensource.cirrus.com \
    --cc=support.opensource@diasemi.com \
    --cc=vulab@iscas.ac.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®