Skip to contentSkip to conversation

of: Add wrappers to match root node with OF device ID tables

PATCH Applied Krzysztof Kozlowski 7 participants 26 messages Nov 12, 2025 – Dec 2, 2025

Subsystems (17)
lore
Share & tools
lore
#0 lore raw

v2 cover of: Add wrappers to match root node with OF device ID tables

Changes in v2:
- Drop cpufreq/ti change: not correct.
- Drop soc/qcom/qcom_pd_mapper.c - objections from Dmitry and I think better to drop the patch in such case.
- I did not implement feedback for first patch to make the of_machine_compatible_match() matching machines in arbitrary nodes, because there is no such use case possible and no arguments were provided. I also did not use cleanup.h in first patch because existing code of_device_get_match_data() does not use it and I prefer uniformity.

- Add Ack/Rb tags.
- Link to v1: https://patch.msgid.link/[email protected]

Dependency/merging
==================
All patches depend on the first patch, thus everything could go via Rob's tree with people's acks.

Description
===========
Several drivers duplicate same code for getting reference to the root node, matching it against 'struct of_device_id' table and getting out the match data from the table entry.

There is a of_machine_compatible_match() wrapper but it takes array of strings, which is not suitable for many drivers since they want the driver data associated with each compatible.

Add two wrappers, similar to existing of_device_get_match_data():
1. of_machine_device_match() doing only matching against 'struct of_device_id' and returning bool.
2. of_machine_get_match_data() doing the matching and returning associated driver data for found compatible.

Best regards,
Krzysztof

---
Krzysztof Kozlowski (11):
of: Add wrappers to match root node with OF device ID tables
cpufreq: dt-platdev: Simplify with of_machine_get_match_data()
cpufreq: mediatek: Simplify with of_machine_get_match_data()
cpufreq: sun50i: Simplify with of_machine_device_match()
cpuidle: big_little: Simplify with of_machine_device_match()
firmware: qcom: scm: Simplify with of_machine_device_match() irqchip/atmel-aic: Simplify with of_machine_get_match_data()
platform: surface: Simplify with of_machine_get_match_data()
powercap: dtpm: Simplify with of_machine_get_match_data()
soc: qcom: ubwc: Simplify with of_machine_get_match_data()
soc: tegra: Simplify with of_machine_device_match()

 drivers/cpufreq/cpufreq-dt-platdev.c               | 15 ++-----
 drivers/cpufreq/mediatek-cpufreq.c                 | 12 +-----
 drivers/cpufreq/sun50i-cpufreq-nvmem.c             | 11 +----
 drivers/cpuidle/cpuidle-big_little.c               | 11 +----
 drivers/firmware/qcom/qcom_scm.c                   | 17 +-------
 drivers/irqchip/irq-atmel-aic-common.c             | 15 ++-----
 drivers/of/base.c                                  | 47 ++++++++++++++++++++++
 .../platform/surface/surface_aggregator_registry.c | 13 +-----
 drivers/powercap/dtpm.c                            | 16 +-------
 drivers/soc/qcom/ubwc_config.c                     | 14 ++-----
 drivers/soc/tegra/common.c                         | 12 +-----
 include/linux/of.h                                 | 13 ++++++
 12 files changed, 79 insertions(+), 117 deletions(-)

---
base-commit: a4ebba34e722123f1c09ce3282e26f052fc8b27f
change-id: 20251106-b4-of-match-matchine-data-4a64bf046814

Best regards,

Signature · 1 line
Krzysztof Kozlowski <[email protected]>
#1 lore raw

PATCH v2 1/11 of: Add wrappers to match root node with OF device ID tables

Several drivers duplicate same code for getting reference to the root node, matching it against 'struct of_device_id' table and getting out the match data from the table entry.

There is a of_machine_compatible_match() wrapper but it takes array of strings, which is not suitable for many drivers since they want the driver data associated with each compatible.

Add two wrappers, similar to existing of_device_get_match_data():
1. of_machine_device_match() doing only matching against 'struct of_device_id' and returning bool.
2. of_machine_get_match_data() doing the matching and returning associated driver data for found compatible.

---

All further patches depend on this.

---
 drivers/of/base.c  | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/of.h | 13 +++++++++++++
 2 files changed, 60 insertions(+)
drivers/of/base.c +47 -0
index 7043acd971a0..0b65039ece53 100644
@@ -434,6 +434,53 @@ bool of_machine_compatible_match(const char *const *compats)
 }
 EXPORT_SYMBOL(of_machine_compatible_match);
 
+/**
+ * of_machine_device_match - Test root of device tree against a of_device_id array
+ * @matches:	NULL terminated array of of_device_id match structures to search in
+ *
+ * Returns true if the root node has any of the given compatible values in its
+ * compatible property.
+ */
+bool of_machine_device_match(const struct of_device_id *matches)
+{
+	struct device_node *root;
+	const struct of_device_id *match = NULL;
+
+	root = of_find_node_by_path("/");
+	if (root) {
+		match = of_match_node(matches, root);
+		of_node_put(root);
+	}
+
+	return match != NULL;
+}
+EXPORT_SYMBOL(of_machine_device_match);
+
+/**
+ * of_machine_get_match_data - Tell if root of device tree has a matching of_match structure
+ * @matches:	NULL terminated array of of_device_id match structures to search in
+ *
+ * Returns data associated with matched entry or NULL
+ */
+const void *of_machine_get_match_data(const struct of_device_id *matches)
+{
+	const struct of_device_id *match;
+	struct device_node *root;
+
+	root = of_find_node_by_path("/");
+	if (!root)
+		return NULL;
+
+	match = of_match_node(matches, root);
+	of_node_put(root);
+
+	if (!match)
+		return NULL;
+
+	return match->data;
+}
+EXPORT_SYMBOL(of_machine_get_match_data);
+
 static bool __of_device_is_status(const struct device_node *device,
 				  const char * const*strings)
 {
include/linux/of.h +13 -0
index 121a288ca92d..01bb3affcd49 100644
@@ -407,6 +407,8 @@ extern int of_alias_get_id(const struct device_node *np, const char *stem);
 extern int of_alias_get_highest_id(const char *stem);
 
 bool of_machine_compatible_match(const char *const *compats);
+bool of_machine_device_match(const struct of_device_id *matches);
+const void *of_machine_get_match_data(const struct of_device_id *matches);
 
 /**
  * of_machine_is_compatible - Test root of device tree for a given compatible value
@@ -855,6 +857,17 @@ static inline bool of_machine_compatible_match(const char *const *compats)
 	return false;
 }
 
+static inline bool of_machine_device_match(const struct of_device_id *matches)
+{
+	return false;
+}
+
+static inline const void *
+of_machine_get_match_data(const struct of_device_id *matches)
+{
+	return NULL;
+}
+
 static inline bool of_console_check(const struct device_node *dn, const char *name, int index)
 {
 	return false;
Signature · 1 line
2.48.1
Reviewed-by on v2 1/11
#2 lore raw

Hi Krzysztof,

Quoted 104 lines · Krzysztof Kozlowski

Several drivers duplicate same code for getting reference to the root node, matching it against 'struct of_device_id' table and getting out the match data from the table entry.

There is a of_machine_compatible_match() wrapper but it takes array of strings, which is not suitable for many drivers since they want the driver data associated with each compatible.

Add two wrappers, similar to existing of_device_get_match_data():
1. of_machine_device_match() doing only matching against 'struct of_device_id' and returning bool.
2. of_machine_get_match_data() doing the matching and returning associated driver data for found compatible.

---

All further patches depend on this.

---
  drivers/of/base.c  | 47 +++++++++++++++++++++++++++++++++++++++++++++++
  include/linux/of.h | 13 +++++++++++++
  2 files changed, 60 insertions(+)
drivers/of/base.c +47 -0
index 7043acd971a0..0b65039ece53 100644
@@ -434,6 +434,53 @@ bool of_machine_compatible_match(const char *const *compats)
  }
  EXPORT_SYMBOL(of_machine_compatible_match);
 
+/**
+ * of_machine_device_match - Test root of device tree against a of_device_id array
+ * @matches:	NULL terminated array of of_device_id match structures to search in
+ *
+ * Returns true if the root node has any of the given compatible values in its
+ * compatible property.
+ */
+bool of_machine_device_match(const struct of_device_id *matches)
+{
+	struct device_node *root;
+	const struct of_device_id *match = NULL;
+
+	root = of_find_node_by_path("/");
+	if (root) {
+		match = of_match_node(matches, root);
+		of_node_put(root);
+	}
+
+	return match != NULL;
+}
+EXPORT_SYMBOL(of_machine_device_match);
+
+/**
+ * of_machine_get_match_data - Tell if root of device tree has a matching of_match structure
+ * @matches:	NULL terminated array of of_device_id match structures to search in
+ *
+ * Returns data associated with matched entry or NULL
+ */
+const void *of_machine_get_match_data(const struct of_device_id *matches)
+{
+	const struct of_device_id *match;
+	struct device_node *root;
+
+	root = of_find_node_by_path("/");
+	if (!root)
+		return NULL;
+
+	match = of_match_node(matches, root);
+	of_node_put(root);
+
+	if (!match)
+		return NULL;
+
+	return match->data;
+}
+EXPORT_SYMBOL(of_machine_get_match_data);
+
  static bool __of_device_is_status(const struct device_node *device,
  				  const char * const*strings)
  {
include/linux/of.h +6 -0
index 121a288ca92d..01bb3affcd49 100644
@@ -407,6 +407,8 @@ extern int of_alias_get_id(const struct device_node *np, const char *stem);
  extern int of_alias_get_highest_id(const char *stem);
 
  bool of_machine_compatible_match(const char *const *compats);
+bool of_machine_device_match(const struct of_device_id *matches);
+const void *of_machine_get_match_data(const struct of_device_id *matches);
 
  /**
   * of_machine_is_compatible - Test root of device tree for a given compatible value
@@ -855,6 +857,17 @@ static inline bool of_machine_compatible_match(const char *const *compats)
  	return false;
  }
 
+static inline bool of_machine_device_match(const struct of_device_id *matches)
+{
+	return false;
+}

include/linux/of.h static inline bool of_machine_compatible_match(const char *const *compats) · line 873

Quoted 10 lines
include/linux/of.h +7 -0
+
+static inline const void *
+of_machine_get_match_data(const struct of_device_id *matches)
+{
+	return NULL;
+}
+
  static inline bool of_console_check(const struct device_node *dn, const char *name, int index)
  {
  	return false;

Makes sense based on the clean-up in dtpm.c (since I've been there I looked here as well).

LGTM

Regards,
Lukasz

#3 lore raw
In reply to Krzysztof Kozlowski

Hi Krzysztof,

Thanks for your patch, which is now commit 4a93adcbd201aad5 ("of: Add wrappers to match root node with OF device ID tables") in dt-rh/for-next.

Quoted 8 lines · Krzysztof Kozlowski

Several drivers duplicate same code for getting reference to the root node, matching it against 'struct of_device_id' table and getting out the match data from the table entry.

There is a of_machine_compatible_match() wrapper but it takes array of strings, which is not suitable for many drivers since they want the driver data associated with each compatible.

Quoted 5 lines

Add two wrappers, similar to existing of_device_get_match_data():
1. of_machine_device_match() doing only matching against 'struct of_device_id' and returning bool.
2. of_machine_get_match_data() doing the matching and returning associated driver data for found compatible.

Shouldn't the first function be called of_match_machine(), and return a const struct of_device_id *, cfr. of_match_device()?

Quoted 46 lines
drivers/of/base.c +36 -0
@@ -434,6 +434,53 @@ bool of_machine_compatible_match(const char *const *compats)
 }
 EXPORT_SYMBOL(of_machine_compatible_match);
 
+/**
+ * of_machine_device_match - Test root of device tree against a of_device_id array
+ * @matches:   NULL terminated array of of_device_id match structures to search in
+ *
+ * Returns true if the root node has any of the given compatible values in its
+ * compatible property.
+ */
+bool of_machine_device_match(const struct of_device_id *matches)
+{
+       struct device_node *root;
+       const struct of_device_id *match = NULL;
+
+       root = of_find_node_by_path("/");
+       if (root) {
+               match = of_match_node(matches, root);
+               of_node_put(root);
+       }
+
+       return match != NULL;
+}
+EXPORT_SYMBOL(of_machine_device_match);
+
+/**
+ * of_machine_get_match_data - Tell if root of device tree has a matching of_match structure
+ * @matches:   NULL terminated array of of_device_id match structures to search in
+ *
+ * Returns data associated with matched entry or NULL
+ */
+const void *of_machine_get_match_data(const struct of_device_id *matches)
+{
+       const struct of_device_id *match;
+       struct device_node *root;
+
+       root = of_find_node_by_path("/");
+       if (!root)
+               return NULL;

drivers/of/base.c bool of_machine_compatible_match(const char *const *compats) · line 482

Quoted 10 lines
drivers/of/base.c +10 -0
+
+       match = of_match_node(matches, root);
+       of_node_put(root);
+
+       if (!match)
+               return NULL;
+
+       return match->data;
+}
+EXPORT_SYMBOL(of_machine_get_match_data);

These two functions are very similar, but look different. If the former would return a pointer instead of a bool, the latter could be built on top.

Even if you still prefer returning a bool, they could share a common private helper returning a pointer.

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#4 lore raw

PATCH v2 2/11 cpufreq: dt-platdev: Simplify with of_machine_get_match_data()

In reply to Krzysztof Kozlowski

Replace open-coded getting root OF node, matching against it and getting the match data with two new helpers: of_machine_get_match_data() and of_machine_device_match().

---

Depends on the first OF patch.

---
 drivers/cpufreq/cpufreq-dt-platdev.c | 15 ++++-----------
 1 file changed, 4 insertions(+), 11 deletions(-)
drivers/cpufreq/cpufreq-dt-platdev.c +4 -11
index dc11b62399ad..a1d11ecd1ac8 100644
@@ -219,20 +219,13 @@ static bool __init cpu0_node_has_opp_v2_prop(void)
 
 static int __init cpufreq_dt_platdev_init(void)
 {
-	struct device_node *np __free(device_node) = of_find_node_by_path("/");
-	const struct of_device_id *match;
-	const void *data = NULL;
+	const void *data;
 
-	if (!np)
-		return -ENODEV;
-
-	match = of_match_node(allowlist, np);
-	if (match) {
-		data = match->data;
+	data = of_machine_get_match_data(allowlist);
+	if (data)
 		goto create_pdev;
-	}
 
-	if (cpu0_node_has_opp_v2_prop() && !of_match_node(blocklist, np))
+	if (cpu0_node_has_opp_v2_prop() && !of_machine_device_match(blocklist))
 		goto create_pdev;
 
 	return -ENODEV;
Signature · 1 line
2.48.1
maintainer Reviewed-by on v2 2/11
#5 lore raw

Re: [PATCH v2 02/11] cpufreq: dt-platdev: Simplify with of_machine_get_match_data()

Il 12/11/25 11:28, Krzysztof Kozlowski ha scritto:

Quoted 6 lines

Replace open-coded getting root OF node, matching against it and getting the match data with two new helpers: of_machine_get_match_data() and of_machine_device_match().

#6 lore raw

Re: [PATCH v2 02/11] cpufreq: dt-platdev: Simplify with of_machine_get_match_data()

In reply to Krzysztof Kozlowski

Hi Krzystof,

Quoted 6 lines · Krzysztof Kozlowski

Replace open-coded getting root OF node, matching against it and getting the match data with two new helpers: of_machine_get_match_data() and of_machine_device_match().

Thanks for your patch, which is now commit 6ea891a6dd370ab2 ("cpufreq: dt-platdev: Simplify with of_machine_get_match_data()") in dt-rh/for-next.

Quoted 11 lines
drivers/cpufreq/cpufreq-dt-platdev.c +1 -3
@@ -219,20 +219,13 @@ static bool __init cpu0_node_has_opp_v2_prop(void)
 
 static int __init cpufreq_dt_platdev_init(void)
 {
-       struct device_node *np __free(device_node) = of_find_node_by_path("/");
-       const struct of_device_id *match;
-       const void *data = NULL;
+       const void *data;
 

drivers/cpufreq/cpufreq-dt-platdev.c static bool __init cpu0_node_has_opp_v2_prop(void) · line 233

Quoted 10 lines
drivers/cpufreq/cpufreq-dt-platdev.c +2 -7
-       if (!np)
-               return -ENODEV;
-
-       match = of_match_node(allowlist, np);
-       if (match) {
-               data = match->data;
+       data = of_machine_get_match_data(allowlist);
+       if (data)
                goto create_pdev;
-       }

I think this is a change in behavior:
Before, the pdev was created immediately if the node's compatible value is found in allowlist, regardless of the value of data (which is always NULL, except on RK3399),
After, the pdev is created immediately if the node's compatible value is found in allowlist, AND data is non-NULL.

drivers/cpufreq/cpufreq-dt-platdev.c static bool __init cpu0_node_has_opp_v2_prop(void) · line 231

Quoted 6 lines
drivers/cpufreq/cpufreq-dt-platdev.c +1 -1
-       if (cpu0_node_has_opp_v2_prop() && !of_match_node(blocklist, np))
+       if (cpu0_node_has_opp_v2_prop() && !of_machine_device_match(blocklist))
                goto create_pdev;
 
        return -ENODEV;

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#7 lore raw

PATCH v2 3/11 cpufreq: mediatek: Simplify with of_machine_get_match_data()

In reply to Krzysztof Kozlowski

Replace open-coded getting root OF node, matching against it and getting the match data with new of_machine_get_match_data() helper.

---

Depends on the first OF patch.

---
 drivers/cpufreq/mediatek-cpufreq.c | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)
drivers/cpufreq/mediatek-cpufreq.c +2 -10
index 5d50a231f944..052ca7cd2f4f 100644
@@ -764,22 +764,14 @@ MODULE_DEVICE_TABLE(of, mtk_cpufreq_machines);
 
 static int __init mtk_cpufreq_driver_init(void)
 {
-	struct device_node *np;
-	const struct of_device_id *match;
 	const struct mtk_cpufreq_platform_data *data;
 	int err;
 
-	np = of_find_node_by_path("/");
-	if (!np)
-		return -ENODEV;
-
-	match = of_match_node(mtk_cpufreq_machines, np);
-	of_node_put(np);
-	if (!match) {
+	data = of_machine_get_match_data(mtk_cpufreq_machines);
+	if (!data) {
 		pr_debug("Machine is not compatible with mtk-cpufreq\n");
 		return -ENODEV;
 	}
-	data = match->data;
 
 	err = platform_driver_register(&mtk_cpufreq_platdrv);
 	if (err)
Signature · 1 line
2.48.1
maintainer Reviewed-by on v2 3/11
#8 lore raw

Re: [PATCH v2 03/11] cpufreq: mediatek: Simplify with of_machine_get_match_data()

Il 12/11/25 11:28, Krzysztof Kozlowski ha scritto:

Quoted 5 lines

Replace open-coded getting root OF node, matching against it and getting the match data with new of_machine_get_match_data() helper.

#9 lore raw

PATCH v2 4/11 cpufreq: sun50i: Simplify with of_machine_device_match()

In reply to Krzysztof Kozlowski

Replace open-coded getting root OF node and matching against it with new of_machine_device_match() helper.

---

Depends on the first OF patch.

---
 drivers/cpufreq/sun50i-cpufreq-nvmem.c | 11 +----------
 1 file changed, 1 insertion(+), 10 deletions(-)
drivers/cpufreq/sun50i-cpufreq-nvmem.c +1 -10
index 744312a44279..4fffc8e83692 100644
@@ -332,13 +332,6 @@ static const struct of_device_id sun50i_cpufreq_match_list[] = {
 };
 MODULE_DEVICE_TABLE(of, sun50i_cpufreq_match_list);
 
-static const struct of_device_id *sun50i_cpufreq_match_node(void)
-{
-	struct device_node *np __free(device_node) = of_find_node_by_path("/");
-
-	return of_match_node(sun50i_cpufreq_match_list, np);
-}
-
 /*
  * Since the driver depends on nvmem drivers, which may return EPROBE_DEFER,
  * all the real activity is done in the probe, which may be defered as well.
@@ -346,11 +339,9 @@ static const struct of_device_id *sun50i_cpufreq_match_node(void)
  */
 static int __init sun50i_cpufreq_init(void)
 {
-	const struct of_device_id *match;
 	int ret;
 
-	match = sun50i_cpufreq_match_node();
-	if (!match)
+	if (!of_machine_device_match(sun50i_cpufreq_match_list))
 		return -ENODEV;
 
 	ret = platform_driver_register(&sun50i_cpufreq_driver);
Signature · 1 line
2.48.1
#10 lore raw

PATCH v2 5/11 cpuidle: big_little: Simplify with of_machine_device_match()

In reply to Krzysztof Kozlowski

Replace open-coded getting root OF node and matching against it with new of_machine_device_match() helper.

---

Depends on the first OF patch.

---
 drivers/cpuidle/cpuidle-big_little.c | 11 +----------
 1 file changed, 1 insertion(+), 10 deletions(-)
drivers/cpuidle/cpuidle-big_little.c +1 -10
index 4abba42fcc31..08f6bf2f6409 100644
@@ -166,20 +166,11 @@ static const struct of_device_id compatible_machine_match[] = {
 static int __init bl_idle_init(void)
 {
 	int ret;
-	struct device_node *root = of_find_node_by_path("/");
-	const struct of_device_id *match_id;
-
-	if (!root)
-		return -ENODEV;
 
 	/*
 	 * Initialize the driver just for a compliant set of machines
 	 */
-	match_id = of_match_node(compatible_machine_match, root);
-
-	of_node_put(root);
-
-	if (!match_id)
+	if (!of_machine_device_match(compatible_machine_match))
 		return -ENODEV;
 
 	if (!mcpm_is_available())
Signature · 1 line
2.48.1
#12 lore raw

PATCH v2 6/11 firmware: qcom: scm: Simplify with of_machine_device_match()

In reply to Krzysztof Kozlowski

Replace open-coded getting root OF node and matching against it with new of_machine_device_match() helper.

---

Depends on the first OF patch.

---
 drivers/firmware/qcom/qcom_scm.c | 17 +----------------
 1 file changed, 1 insertion(+), 16 deletions(-)
drivers/firmware/qcom/qcom_scm.c +1 -16
index e777b7cb9b12..1a6f85e463e0 100644
@@ -2018,21 +2018,6 @@ static const struct of_device_id qcom_scm_qseecom_allowlist[] __maybe_unused = {
 	{ }
 };
 
-static bool qcom_scm_qseecom_machine_is_allowed(void)
-{
-	struct device_node *np;
-	bool match;
-
-	np = of_find_node_by_path("/");
-	if (!np)
-		return false;
-
-	match = of_match_node(qcom_scm_qseecom_allowlist, np);
-	of_node_put(np);
-
-	return match;
-}
-
 static void qcom_scm_qseecom_free(void *data)
 {
 	struct platform_device *qseecom_dev = data;
@@ -2064,7 +2049,7 @@ static int qcom_scm_qseecom_init(struct qcom_scm *scm)
 
 	dev_info(scm->dev, "qseecom: found qseecom with version 0x%x\n", version);
 
-	if (!qcom_scm_qseecom_machine_is_allowed()) {
+	if (!of_machine_device_match(qcom_scm_qseecom_allowlist)) {
 		dev_info(scm->dev, "qseecom: untested machine, skipping\n");
 		return 0;
 	}
Signature · 1 line
2.48.1
maintainer Reviewed-by on v2 6/11
#13 lore raw

Re: [PATCH v2 06/11] firmware: qcom: scm: Simplify with of_machine_device_match()

Il 12/11/25 11:28, Krzysztof Kozlowski ha scritto:

Quoted 5 lines

Replace open-coded getting root OF node and matching against it with new of_machine_device_match() helper.

#14 lore raw

PATCH v2 7/11 irqchip/atmel-aic: Simplify with of_machine_get_match_data()

In reply to Krzysztof Kozlowski

Replace open-coded getting root OF node, matching against it and getting the match data with new of_machine_get_match_data() helper.

---

Depends on the first OF patch.

---
 drivers/irqchip/irq-atmel-aic-common.c | 15 +++------------
 1 file changed, 3 insertions(+), 12 deletions(-)
drivers/irqchip/irq-atmel-aic-common.c +3 -12
index 3cad30a40c19..e68853815c7a 100644
@@ -187,20 +187,11 @@ void __init aic_common_rtt_irq_fixup(void)
 
 static void __init aic_common_irq_fixup(const struct of_device_id *matches)
 {
-	struct device_node *root = of_find_node_by_path("/");
-	const struct of_device_id *match;
+	void (*fixup)(void);
 
-	if (!root)
-		return;
-
-	match = of_match_node(matches, root);
-
-	if (match) {
-		void (*fixup)(void) = match->data;
+	fixup = of_machine_get_match_data(matches);
+	if (fixup)
 		fixup();
-	}
-
-	of_node_put(root);
 }
 
 struct irq_domain *__init aic_common_of_init(struct device_node *node,
Signature · 1 line
2.48.1
maintainer Reviewed-by on v2 7/11
#15 lore raw

Re: [PATCH v2 07/11] irqchip/atmel-aic: Simplify with of_machine_get_match_data()

Quoted 5 lines · Krzysztof Kozlowski

Replace open-coded getting root OF node, matching against it and getting the match data with new of_machine_get_match_data() helper.

Quoted 40 lines

---

Depends on the first OF patch.

---
 drivers/irqchip/irq-atmel-aic-common.c | 15 +++------------
 1 file changed, 3 insertions(+), 12 deletions(-)
drivers/irqchip/irq-atmel-aic-common.c +3 -12
index 3cad30a40c19..e68853815c7a 100644
@@ -187,20 +187,11 @@ void __init aic_common_rtt_irq_fixup(void)
 
 static void __init aic_common_irq_fixup(const struct of_device_id *matches)
 {
-	struct device_node *root = of_find_node_by_path("/");
-	const struct of_device_id *match;
+	void (*fixup)(void);
 
-	if (!root)
-		return;
-
-	match = of_match_node(matches, root);
-
-	if (match) {
-		void (*fixup)(void) = match->data;
+	fixup = of_machine_get_match_data(matches);
+	if (fixup)
 		fixup();
-	}
-
-	of_node_put(root);
 }
 
 struct irq_domain *__init aic_common_of_init(struct device_node *node,
Signature · 2 lines
-- 
2.48.1
Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#16 lore raw

PATCH v2 8/11 platform: surface: Simplify with of_machine_get_match_data()

In reply to Krzysztof Kozlowski

Replace open-coded getting root OF node, matching against it and getting the match data with new of_machine_get_match_data() helper.

---

Depends on the first OF patch.

---
 drivers/platform/surface/surface_aggregator_registry.c | 13 +------------
 1 file changed, 1 insertion(+), 12 deletions(-)
drivers/platform/surface/surface_aggregator_registry.c +1 -12
index a594d5fcfcfd..78ac3a8fbb73 100644
@@ -491,24 +491,13 @@ static const struct of_device_id ssam_platform_hub_of_match[] __maybe_unused = {
 static int ssam_platform_hub_probe(struct platform_device *pdev)
 {
 	const struct software_node **nodes;
-	const struct of_device_id *match;
-	struct device_node *fdt_root;
 	struct ssam_controller *ctrl;
 	struct fwnode_handle *root;
 	int status;
 
 	nodes = (const struct software_node **)acpi_device_get_match_data(&pdev->dev);
 	if (!nodes) {
-		fdt_root = of_find_node_by_path("/");
-		if (!fdt_root)
-			return -ENODEV;
-
-		match = of_match_node(ssam_platform_hub_of_match, fdt_root);
-		of_node_put(fdt_root);
-		if (!match)
-			return -ENODEV;
-
-		nodes = (const struct software_node **)match->data;
+		nodes = (const struct software_node **)of_machine_get_match_data(ssam_platform_hub_of_match);
 		if (!nodes)
 			return -ENODEV;
 	}
Signature · 1 line
2.48.1
#17 lore raw

PATCH v2 9/11 powercap: dtpm: Simplify with of_machine_get_match_data()

In reply to Krzysztof Kozlowski

Replace open-coded getting root OF node, matching against it and getting the match data with new of_machine_get_match_data() helper.

---

Depends on the first OF patch.

---
 drivers/powercap/dtpm.c | 16 +---------------
 1 file changed, 1 insertion(+), 15 deletions(-)
drivers/powercap/dtpm.c +1 -15
index f390665743c4..129d55bc705c 100644
@@ -548,9 +548,7 @@ static int dtpm_for_each_child(const struct dtpm_node *hierarchy,
  */
 int dtpm_create_hierarchy(struct of_device_id *dtpm_match_table)
 {
-	const struct of_device_id *match;
 	const struct dtpm_node *hierarchy;
-	struct device_node *np;
 	int i, ret;
 
 	mutex_lock(&dtpm_lock);
@@ -567,19 +565,7 @@ int dtpm_create_hierarchy(struct of_device_id *dtpm_match_table)
 		goto out_pct;
 	}
 
-	ret = -ENODEV;
-	np = of_find_node_by_path("/");
-	if (!np)
-		goto out_err;
-
-	match = of_match_node(dtpm_match_table, np);
-
-	of_node_put(np);
-
-	if (!match)
-		goto out_err;
-
-	hierarchy = match->data;
+	hierarchy = of_machine_get_match_data(dtpm_match_table);
 	if (!hierarchy) {
 		ret = -EFAULT;
 		goto out_err;
Signature · 1 line
2.48.1
Reviewed-by on v2 9/11
#18 lore raw

Re: [PATCH v2 09/11] powercap: dtpm: Simplify with of_machine_get_match_data()

Quoted 38 lines · Krzysztof Kozlowski

Replace open-coded getting root OF node, matching against it and getting the match data with new of_machine_get_match_data() helper.

---

Depends on the first OF patch.

---
  drivers/powercap/dtpm.c | 16 +---------------
  1 file changed, 1 insertion(+), 15 deletions(-)
drivers/powercap/dtpm.c +0 -9
index f390665743c4..129d55bc705c 100644
@@ -548,9 +548,7 @@ static int dtpm_for_each_child(const struct dtpm_node *hierarchy,
   */
  int dtpm_create_hierarchy(struct of_device_id *dtpm_match_table)
  {
-	const struct of_device_id *match;
  	const struct dtpm_node *hierarchy;
-	struct device_node *np;
  	int i, ret;
 
  	mutex_lock(&dtpm_lock);
@@ -567,19 +565,7 @@ int dtpm_create_hierarchy(struct of_device_id *dtpm_match_table)
  		goto out_pct;
  	}
 
-	ret = -ENODEV;
-	np = of_find_node_by_path("/");
-	if (!np)
-		goto out_err;
-
-	match = of_match_node(dtpm_match_table, np);
-

drivers/powercap/dtpm.c int dtpm_create_hierarchy(struct of_device_id *dtpm_match_table) · line 571

Quoted 10 lines
drivers/powercap/dtpm.c +1 -6
-	of_node_put(np);
-
-	if (!match)
-		goto out_err;
-
-	hierarchy = match->data;
+	hierarchy = of_machine_get_match_data(dtpm_match_table);
  	if (!hierarchy) {
  		ret = -EFAULT;
  		goto out_err;

I don't know if Daniel had a chance to look at it, but I can help him.

The patch looks OK.

The extra return error value which is removed doesn't harm the client of this function in other subsystem.

#19 lore raw

PATCH v2 10/11 soc: qcom: ubwc: Simplify with of_machine_get_match_data()

In reply to Krzysztof Kozlowski

Replace open-coded getting root OF node, matching against it and getting the match data with new of_machine_get_match_data() helper.

---

Depends on the first OF patch.

---
 drivers/soc/qcom/ubwc_config.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)
drivers/soc/qcom/ubwc_config.c +4 -10
index 1c09796163b0..1c25aaf55e52 100644
@@ -301,21 +301,15 @@ static const struct of_device_id qcom_ubwc_configs[] __maybe_unused = {
 
 const struct qcom_ubwc_cfg_data *qcom_ubwc_config_get_data(void)
 {
-	const struct of_device_id *match;
-	struct device_node *root;
+	const struct qcom_ubwc_cfg_data *data;
 
-	root = of_find_node_by_path("/");
-	if (!root)
-		return ERR_PTR(-ENODEV);
-
-	match = of_match_node(qcom_ubwc_configs, root);
-	of_node_put(root);
-	if (!match) {
+	data = of_machine_get_match_data(qcom_ubwc_configs);
+	if (!data) {
 		pr_err("Couldn't find UBWC config data for this platform!\n");
 		return ERR_PTR(-EINVAL);
 	}
 
-	return match->data;
+	return data;
 }
 EXPORT_SYMBOL_GPL(qcom_ubwc_config_get_data);
 
Signature · 1 line
2.48.1
#20 lore raw

PATCH v2 11/11 soc: tegra: Simplify with of_machine_device_match()

In reply to Krzysztof Kozlowski

Replace open-coded getting root OF node and matching against it with new of_machine_device_match() helper.

---

Depends on the first OF patch.

---
 drivers/soc/tegra/common.c | 12 +-----------
 1 file changed, 1 insertion(+), 11 deletions(-)
drivers/soc/tegra/common.c +1 -11
index dff6d5ef4e46..d82b7670abb7 100644
@@ -27,17 +27,7 @@ static const struct of_device_id tegra_machine_match[] = {
 
 bool soc_is_tegra(void)
 {
-	const struct of_device_id *match;
-	struct device_node *root;
-
-	root = of_find_node_by_path("/");
-	if (!root)
-		return false;
-
-	match = of_match_node(tegra_machine_match, root);
-	of_node_put(root);
-
-	return match != NULL;
+	return of_machine_device_match(tegra_machine_match);
 }
 
 static int tegra_core_dev_init_opp_state(struct device *dev)
Signature · 1 line
2.48.1
Acked-by on v2 11/11
#22 lore raw

Re: [PATCH v2 11/11] soc: tegra: Simplify with of_machine_device_match()

In reply to Krzysztof Kozlowski
Quoted 11 lines · Krzysztof Kozlowski

Replace open-coded getting root OF node and matching against it with new of_machine_device_match() helper.

---

Depends on the first OF patch.

---
 drivers/soc/tegra/common.c | 12 +-----------
 1 file changed, 1 insertion(+), 11 deletions(-)

I assume that you'd want to take this through the DT (or some other) tree, so:

maintainer Tested-by on the v2 series
#23 lore raw
In reply to Krzysztof Kozlowski

Il 12/11/25 11:28, Krzysztof Kozlowski ha scritto:

Quoted 1 line

Changes in v2:

Note:

Looks ok based on code and based on testing on the following platforms:
- tegra: Jetson Xavier NX Development Kit
- qcom: sdm630 Sony Xperia XA2 (Nile), sc7180 Trogdor Lazor Chromebook
- mediatek: MT6795 Xperia M5 (midstream kernel), MT8173 Elm Chromebook

             MT8365 Genio 350 (mt8365-evk)

Cheers,
Angelo

Quoted 68 lines

- Drop cpufreq/ti change: not correct.
- Drop soc/qcom/qcom_pd_mapper.c - objections from Dmitry and I think better to drop the patch in such case.
- I did not implement feedback for first patch to make the of_machine_compatible_match() matching machines in arbitrary nodes, because there is no such use case possible and no arguments were provided. I also did not use cleanup.h in first patch because existing code of_device_get_match_data() does not use it and I prefer uniformity.

- Add Ack/Rb tags.
- Link to v1: https://patch.msgid.link/[email protected]

Dependency/merging
==================
All patches depend on the first patch, thus everything could go via Rob's tree with people's acks.

Description
===========
Several drivers duplicate same code for getting reference to the root node, matching it against 'struct of_device_id' table and getting out the match data from the table entry.

There is a of_machine_compatible_match() wrapper but it takes array of strings, which is not suitable for many drivers since they want the driver data associated with each compatible.

Add two wrappers, similar to existing of_device_get_match_data():
1. of_machine_device_match() doing only matching against 'struct of_device_id' and returning bool.
2. of_machine_get_match_data() doing the matching and returning associated driver data for found compatible.

Best regards,
Krzysztof

---
Krzysztof Kozlowski (11):
of: Add wrappers to match root node with OF device ID tables
cpufreq: dt-platdev: Simplify with of_machine_get_match_data()
cpufreq: mediatek: Simplify with of_machine_get_match_data()
cpufreq: sun50i: Simplify with of_machine_device_match()
cpuidle: big_little: Simplify with of_machine_device_match()
firmware: qcom: scm: Simplify with of_machine_device_match() irqchip/atmel-aic: Simplify with of_machine_get_match_data()
platform: surface: Simplify with of_machine_get_match_data()
powercap: dtpm: Simplify with of_machine_get_match_data()
soc: qcom: ubwc: Simplify with of_machine_get_match_data()
soc: tegra: Simplify with of_machine_device_match()

  drivers/cpufreq/cpufreq-dt-platdev.c               | 15 ++-----
  drivers/cpufreq/mediatek-cpufreq.c                 | 12 +-----
  drivers/cpufreq/sun50i-cpufreq-nvmem.c             | 11 +----
  drivers/cpuidle/cpuidle-big_little.c               | 11 +----
  drivers/firmware/qcom/qcom_scm.c                   | 17 +-------
  drivers/irqchip/irq-atmel-aic-common.c             | 15 ++-----
  drivers/of/base.c                                  | 47 ++++++++++++++++++++++
  .../platform/surface/surface_aggregator_registry.c | 13 +-----
  drivers/powercap/dtpm.c                            | 16 +-------
  drivers/soc/qcom/ubwc_config.c                     | 14 ++-----
  drivers/soc/tegra/common.c                         | 12 +-----
  include/linux/of.h                                 | 13 ++++++
  12 files changed, 79 insertions(+), 117 deletions(-)

---
base-commit: a4ebba34e722123f1c09ce3282e26f052fc8b27f
change-id: 20251106-b4-of-match-matchine-data-4a64bf046814

Best regards,

#24 lore raw
Quoted 7 lines · AngeloGioacchino Del Regno

Il 12/11/25 11:28, Krzysztof Kozlowski ha scritto:

Quoted 1 line

Changes in v2:

Note:

Looks ok based on code and based on testing on the following platforms:
- tegra: Jetson Xavier NX Development Kit

Thanks for testing, but Xavier NX doesn't run any of the code changed by this patch. soc_is_tegra() is a legacy function that we need for DT backwards-compatibility and should only run on 32-bit ARM devices.

Technically there's one case in drivers/soc/tegra/flowctrl.c that runs this on Tegra210, but it should probably undergo the same treatment as the PMC and FUSE drivers. The code that needs this is only used for CPU power management on 32-bit ARM devices.

Thierry

maintainer Applied
#25 lore raw
In reply to Krzysztof Kozlowski
Quoted 51 lines · Krzysztof Kozlowski

Changes in v2:
- Drop cpufreq/ti change: not correct.
- Drop soc/qcom/qcom_pd_mapper.c - objections from Dmitry and I think better to drop the patch in such case.
- I did not implement feedback for first patch to make the of_machine_compatible_match() matching machines in arbitrary nodes, because there is no such use case possible and no arguments were provided. I also did not use cleanup.h in first patch because existing code of_device_get_match_data() does not use it and I prefer uniformity.

- Add Ack/Rb tags.
- Link to v1: https://patch.msgid.link/[email protected]

Dependency/merging
==================
All patches depend on the first patch, thus everything could go via Rob's tree with people's acks.

Description
===========
Several drivers duplicate same code for getting reference to the root node, matching it against 'struct of_device_id' table and getting out the match data from the table entry.

There is a of_machine_compatible_match() wrapper but it takes array of strings, which is not suitable for many drivers since they want the driver data associated with each compatible.

Add two wrappers, similar to existing of_device_get_match_data():
1. of_machine_device_match() doing only matching against 'struct of_device_id' and returning bool.
2. of_machine_get_match_data() doing the matching and returning associated driver data for found compatible.

Best regards,
Krzysztof

---
Krzysztof Kozlowski (11):
of: Add wrappers to match root node with OF device ID tables
cpufreq: dt-platdev: Simplify with of_machine_get_match_data()
cpufreq: mediatek: Simplify with of_machine_get_match_data()
cpufreq: sun50i: Simplify with of_machine_device_match()
cpuidle: big_little: Simplify with of_machine_device_match()
firmware: qcom: scm: Simplify with of_machine_device_match() irqchip/atmel-aic: Simplify with of_machine_get_match_data()
platform: surface: Simplify with of_machine_get_match_data()
powercap: dtpm: Simplify with of_machine_get_match_data()
soc: qcom: ubwc: Simplify with of_machine_get_match_data()
soc: tegra: Simplify with of_machine_device_match()

Quoted 13 lines
 drivers/cpufreq/cpufreq-dt-platdev.c               | 15 ++-----
 drivers/cpufreq/mediatek-cpufreq.c                 | 12 +-----
 drivers/cpufreq/sun50i-cpufreq-nvmem.c             | 11 +----
 drivers/cpuidle/cpuidle-big_little.c               | 11 +----
 drivers/firmware/qcom/qcom_scm.c                   | 17 +-------
 drivers/irqchip/irq-atmel-aic-common.c             | 15 ++-----
 drivers/of/base.c                                  | 47 ++++++++++++++++++++++
 .../platform/surface/surface_aggregator_registry.c | 13 +-----
 drivers/powercap/dtpm.c                            | 16 +-------
 drivers/soc/qcom/ubwc_config.c                     | 14 ++-----
 drivers/soc/tegra/common.c                         | 12 +-----
 include/linux/of.h                                 | 13 ++++++
 12 files changed, 79 insertions(+), 117 deletions(-)

Applied, thanks.

Rob