[RFC 01/10] property: add fwnode_match_node()

Clément Léger
2022-02-21T16:38:59Z

Add a function equivalent to of_match_node() which is usable for fwnode
support. Matching is based on the compatible property and it returns
the best matches for the node according to the compatible list
ordering.

Signed-off-by: Clément Léger <clement.leger@bootlin.com>
---
drivers/base/property.c | 23 +++++++++++++++++++++++
include/linux/property.h | 3 +++
2 files changed, 26 insertions(+)

diff --git a/drivers/base/property.c b/drivers/base/property.c
index e6497f6877ee..434c2713fd99 100644
--- a/drivers/base/property.c
+++ b/drivers/base/property.c
@@ -1158,6 +1158,29 @@ int fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
}
EXPORT_SYMBOL(fwnode_graph_parse_endpoint);

+const struct of_device_id *fwnode_match_node(const struct fwnode_handle *fwnode,
+ const struct of_device_id *matches)
+{
+ int index;
+ const struct of_device_id *best_match = NULL;
+ int best_index = INT_MAX;
+
+ if (!matches)
+ return NULL;
+
+ for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
+ index = fwnode_property_match_string(fwnode, "compatible",
+ matches->compatible);
+ if (index >= 0 && index < best_index) {
+ best_match = matches;
+ best_index = index;
+ }
+ }
+
+ return best_match;
+}
+EXPORT_SYMBOL(fwnode_match_node);
+
const void *device_get_match_data(struct device *dev)
{
return fwnode_call_ptr_op(dev_fwnode(dev), device_get_match_data, dev);
diff --git a/include/linux/property.h b/include/linux/property.h
index 7399a0b45f98..978ecf6be34e 100644
--- a/include/linux/property.h
+++ b/include/linux/property.h
@@ -446,6 +446,9 @@ static inline void *device_connection_find_match(struct device *dev,
return fwnode_connection_find_match(dev_fwnode(dev), con_id, data, match);
}

+const struct of_device_id *fwnode_match_node(const struct fwnode_handle *fwnode,
+ const struct of_device_id *matches);
+
/* -------------------------------------------------------------------------- */
/* Software fwnode support - when HW description is incomplete or missing */

--
2.34.1