diff mbox series

[v2,1/5] of: Add of_machine_compatible_match()

Message ID 20231214103152.12269-1-mpe@ellerman.id.au (mailing list archive)
State Accepted
Commit c029b22f8a98e14988f800d5c0176a9eaec3c8db
Headers show
Series [v2,1/5] of: Add of_machine_compatible_match() | expand

Commit Message

Michael Ellerman Dec. 14, 2023, 10:31 a.m. UTC
We have of_machine_is_compatible() to check if a machine is compatible
with a single compatible string. However some code is able to support
multiple compatible boards, and so wants to check for one of many
compatible strings.

So add of_machine_compatible_match() which takes a NULL terminated
array of compatible strings to check against the root node's
compatible property.

Compared to an open coded match this is slightly more self
documenting, and also avoids the caller needing to juggle the root
node either directly or via of_find_node_by_path().

Signed-off-by: Christophe Leroy <christophe.leroy@csgroup.eu>
Reviewed-by: Rob Herring <robh@kernel.org>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
---
 drivers/of/base.c  | 21 +++++++++++++++++++++
 include/linux/of.h |  6 ++++++
 2 files changed, 27 insertions(+)

v2: Unchanged.

Comments

Michael Ellerman March 13, 2024, 1:19 p.m. UTC | #1
On Thu, 14 Dec 2023 21:31:48 +1100, Michael Ellerman wrote:
> We have of_machine_is_compatible() to check if a machine is compatible
> with a single compatible string. However some code is able to support
> multiple compatible boards, and so wants to check for one of many
> compatible strings.
> 
> So add of_machine_compatible_match() which takes a NULL terminated
> array of compatible strings to check against the root node's
> compatible property.
> 
> [...]

Applied to powerpc/next.

[1/5] of: Add of_machine_compatible_match()
      https://git.kernel.org/powerpc/c/c029b22f8a98e14988f800d5c0176a9eaec3c8db
[2/5] of: Change of_machine_is_compatible() to return bool
      https://git.kernel.org/powerpc/c/cefdb366dcbe97908b6055595a15bf7689556bf8
[3/5] of: Reimplement of_machine_is_compatible() using of_machine_compatible_match()
      https://git.kernel.org/powerpc/c/1ac8205f907517a306b661212496fedce79d7cc5
[4/5] powerpc/machdep: Define 'compatibles' property in ppc_md and use it
      https://git.kernel.org/powerpc/c/28da734d58c8d0113d0ac4f59880d94c9f249564
[5/5] powerpc: Stop using of_root
      https://git.kernel.org/powerpc/c/2a066ae11861257223500d7515e1541199cb7832

cheers
diff mbox series

Patch

diff --git a/drivers/of/base.c b/drivers/of/base.c
index 8d93cb6ea9cd..9020be2eb4d5 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -394,6 +394,27 @@  int of_device_compatible_match(const struct device_node *device,
 }
 EXPORT_SYMBOL_GPL(of_device_compatible_match);
 
+/**
+ * of_machine_compatible_match - Test root of device tree against a compatible array
+ * @compats: NULL terminated array of compatible strings to look for in root node's compatible property.
+ *
+ * Returns true if the root node has any of the given compatible values in its
+ * compatible property.
+ */
+bool of_machine_compatible_match(const char *const *compats)
+{
+	struct device_node *root;
+	int rc = 0;
+
+	root = of_find_node_by_path("/");
+	if (root) {
+		rc = of_device_compatible_match(root, compats);
+		of_node_put(root);
+	}
+
+	return rc != 0;
+}
+
 /**
  * of_machine_is_compatible - Test root of device tree for a given compatible value
  * @compat: compatible string to look for in root node's compatible property.
diff --git a/include/linux/of.h b/include/linux/of.h
index 6a9ddf20e79a..e3418babc203 100644
--- a/include/linux/of.h
+++ b/include/linux/of.h
@@ -403,6 +403,7 @@  extern int of_alias_get_id(struct device_node *np, const char *stem);
 extern int of_alias_get_highest_id(const char *stem);
 
 extern int of_machine_is_compatible(const char *compat);
+bool of_machine_compatible_match(const char *const *compats);
 
 extern int of_add_property(struct device_node *np, struct property *prop);
 extern int of_remove_property(struct device_node *np, struct property *prop);
@@ -808,6 +809,11 @@  static inline int of_remove_property(struct device_node *np, struct property *pr
 	return 0;
 }
 
+static inline bool of_machine_compatible_match(const char *const *compats)
+{
+	return false;
+}
+
 static inline bool of_console_check(const struct device_node *dn, const char *name, int index)
 {
 	return false;