When a unit-address is provided, use it to match against the node
name.

Since this increases code size, put it into a separate function.

Signed-off-by: Simon Glass <s...@chromium.org>
---

(no changes since v2)

Changes in v2:
- Split this into its own function

 drivers/core/ofnode.c | 23 +++++++++++++++++++++++
 include/dm/ofnode.h   | 14 ++++++++++++++
 test/dm/core.c        | 18 ++++++++++++++++++
 3 files changed, 55 insertions(+)

diff --git a/drivers/core/ofnode.c b/drivers/core/ofnode.c
index 5b8be218d3b..529bc132f7d 100644
--- a/drivers/core/ofnode.c
+++ b/drivers/core/ofnode.c
@@ -309,6 +309,29 @@ bool ofnode_name_eq(ofnode node, const char *name)
        return (strlen(name) == len) && !strncmp(node_name, name, len);
 }
 
+bool ofnode_name_eq_unit(ofnode node, const char *name)
+{
+       const char *node_name, *p;
+       int len;
+
+       assert(ofnode_valid(node));
+
+       node_name = ofnode_get_name(node);
+
+       /* check the whole name */
+       if (!strcmp(node_name, name))
+               return true;
+
+       /* if @name has no unit address, try the node name without it */
+       len = strlen(name);
+       p = strchr(node_name, '@');
+       if (p && !strchr(name, '@') && len == p - node_name &&
+           !strncmp(node_name, name, len))
+               return true;
+
+       return false;
+}
+
 int ofnode_read_u8(ofnode node, const char *propname, u8 *outp)
 {
        const u8 *cell;
diff --git a/include/dm/ofnode.h b/include/dm/ofnode.h
index 6d6a6fef8ef..3bb7d3e05e1 100644
--- a/include/dm/ofnode.h
+++ b/include/dm/ofnode.h
@@ -394,6 +394,20 @@ void oftree_dispose(oftree tree);
  */
 bool ofnode_name_eq(ofnode node, const char *name);
 
+/**
+ * ofnode_name_eq_unit() - Check a node name ignoring its unit address
+ *
+ * This is separate from ofnode_name_eq() to avoid code-size increase for
+ * boards which don't need this function
+ *
+ * @node:      valid node to compared, which may have a unit address
+ * @name:      name to compare with the node name. If this contains a unit
+ *             address, it is matched, otherwise the unit address is ignored
+ *             when searching for matches
+ * Return: true if matches, false if it doesn't match
+ */
+bool ofnode_name_eq_unit(ofnode node, const char *name);
+
 /**
  * ofnode_read_u8() - Read a 8-bit integer from a property
  *
diff --git a/test/dm/core.c b/test/dm/core.c
index d40916ef588..959b834576f 100644
--- a/test/dm/core.c
+++ b/test/dm/core.c
@@ -192,6 +192,24 @@ static int dm_test_compare_node_name(struct 
unit_test_state *uts)
 }
 DM_TEST(dm_test_compare_node_name, UTF_SCAN_PDATA);
 
+/* compare node names ignoring the unit address */
+static int dm_test_compare_node_name_unit(struct unit_test_state *uts)
+{
+       ofnode node;
+
+       node = ofnode_path("/mmio-bus@0");
+       ut_assert(ofnode_valid(node));
+       ut_assert(ofnode_name_eq_unit(node, "mmio-bus"));
+
+       ut_assert(ofnode_name_eq_unit(node, "mmio-bus@0"));
+       ut_assert(!ofnode_name_eq_unit(node, "mmio-bus@1"));
+       ut_assert(!ofnode_name_eq_unit(node, "mmio-bu"));
+       ut_assert(!ofnode_name_eq_unit(node, "mmio-buss@0"));
+
+       return 0;
+}
+DM_TEST(dm_test_compare_node_name_unit, UTF_SCAN_PDATA);
+
 /* Test that binding with uclass plat setting occurs correctly */
 static int dm_test_autobind_uclass_pdata_valid(struct unit_test_state *uts)
 {
-- 
2.34.1

Reply via email to