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.
Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
---
All further patches depend on this.
---
drivers/of/base.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
include/linux/of.h | 13 +++++++++++++
2 files changed, 60 insertions(+)
diff --git a/drivers/of/base.c b/drivers/of/base.c
index 7043acd971a0..0b65039ece53 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -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)
{
diff --git a/include/linux/of.h b/include/linux/of.h
index 121a288ca92d..01bb3affcd49 100644
--- a/include/linux/of.h
+++ b/include/linux/of.h
@@ -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;
--
2.48.1
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.
On Wed, 12 Nov 2025 at 11:30, Krzysztof Kozlowski
<krzysztof.kozlowski@linaro.org> wrote:
> 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.
Shouldn't the first function be called of_match_machine(), and return
a const struct of_device_id *, cfr. of_match_device()?
>
> Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
> Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
> --- a/drivers/of/base.c
> +++ b/drivers/of/base.c
> @@ -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);
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
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org
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
Hi Krzysztof,
On 11/12/25 10:28, Krzysztof Kozlowski wrote:
> 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.
>
> Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
> Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
> ---
>
> All further patches depend on this.
> ---
> drivers/of/base.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
> include/linux/of.h | 13 +++++++++++++
> 2 files changed, 60 insertions(+)
>
> diff --git a/drivers/of/base.c b/drivers/of/base.c
> index 7043acd971a0..0b65039ece53 100644
> --- a/drivers/of/base.c
> +++ b/drivers/of/base.c
> @@ -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)
> {
> diff --git a/include/linux/of.h b/include/linux/of.h
> index 121a288ca92d..01bb3affcd49 100644
> --- a/include/linux/of.h
> +++ b/include/linux/of.h
> @@ -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;
>
Makes sense based on the clean-up in dtpm.c (since I've been there
I looked here as well).
LGTM
Reviewed-by: Lukasz Luba <lukasz.luba@arm.com>
Regards,
Lukasz
© 2016 - 2025 Red Hat, Inc.