[PATCH v2 28/45] dm: core: Add a macro to iterate through properties

Simon Glass sjg at chromium.org
Wed Sep 7 04:27:16 CEST 2022


Add a 'for_each' macro like we have for nodes.

Fix the comment for struct ofprop while we are here.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

(no changes since v1)

 include/dm/ofnode.h      | 38 ++++++++++++++++++++++++++++++++++++++
 include/dm/ofnode_decl.h |  2 +-
 test/dm/ofnode.c         | 24 ++++++++++++++++++++++++
 3 files changed, 63 insertions(+), 1 deletion(-)

diff --git a/include/dm/ofnode.h b/include/dm/ofnode.h
index ac9babbb5b5..8428e1a661e 100644
--- a/include/dm/ofnode.h
+++ b/include/dm/ofnode.h
@@ -176,6 +176,20 @@ static inline ofnode ofnode_root(void)
 	return node;
 }
 
+/**
+ * ofprop_valid() - check if an ofprop is valid
+ *
+ * @prop: Pointer to ofprop to check
+ * Return: true if the reference contains a valid ofprop, false if not
+ */
+static inline bool ofprop_valid(struct ofprop *prop)
+{
+	if (of_live_active())
+		return prop->prop;
+	else
+		return prop->offset >= 0;
+}
+
 /**
  * oftree_default() - Returns the default device tree (U-Boot's control FDT)
  *
@@ -781,6 +795,30 @@ int ofnode_first_property(ofnode node, struct ofprop *prop);
  */
 int ofnode_next_property(struct ofprop *prop);
 
+/**
+ * ofnode_for_each_prop() - iterate over all properties of a node
+ *
+ * @prop:	struct ofprop
+ * @node:	node (lvalue, ofnode)
+ *
+ * This is a wrapper around a for loop and is used like this::
+ *
+ *   ofnode node;
+ *   struct ofprop prop;
+ *
+ *   ofnode_for_each_prop(prop, node) {
+ *       ...use prop...
+ *   }
+ *
+ * Note that this is implemented as a macro and @prop is used as
+ * iterator in the loop. The parent variable can be a constant or even a
+ * literal.
+ */
+#define ofnode_for_each_prop(prop, node) \
+	for (ofnode_first_property(node, &prop); \
+	     ofprop_valid(&prop); \
+	     ofnode_next_property(&prop))
+
 /**
  * ofprop_get_property() - get a pointer to the value of a property
  *
diff --git a/include/dm/ofnode_decl.h b/include/dm/ofnode_decl.h
index 8d0d7885aa6..f666a0287ba 100644
--- a/include/dm/ofnode_decl.h
+++ b/include/dm/ofnode_decl.h
@@ -57,7 +57,7 @@ typedef union ofnode_union {
  *
  * @node: Pointer to device node
  * @offset: Pointer into flat device tree, used for flat tree.
- * @prop: Pointer to property, used for live treee.
+ * @prop: Pointer to property, used for live tree.
  */
 
 struct ofprop {
diff --git a/test/dm/ofnode.c b/test/dm/ofnode.c
index a421392a976..5ddfd0298a6 100644
--- a/test/dm/ofnode.c
+++ b/test/dm/ofnode.c
@@ -669,3 +669,27 @@ static int dm_test_ofnode_add_subnode(struct unit_test_state *uts)
 	return 0;
 }
 DM_TEST(dm_test_ofnode_add_subnode, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
+
+static int dm_test_ofnode_for_each_prop(struct unit_test_state *uts)
+{
+	ofnode node, subnode;
+	struct ofprop prop;
+	int count;
+
+	node = ofnode_path("/buttons");
+	count = 0;
+
+	/* we expect "compatible" for each node */
+	ofnode_for_each_prop(prop, node)
+		count++;
+	ut_asserteq(1, count);
+
+	/* there are two nodes, each with 2 properties */
+	ofnode_for_each_subnode(subnode, node)
+		ofnode_for_each_prop(prop, subnode)
+			count++;
+	ut_asserteq(5, count);
+
+	return 0;
+}
+DM_TEST(dm_test_ofnode_for_each_prop, UT_TESTF_SCAN_FDT);
-- 
2.37.2.789.g6183377224-goog



More information about the U-Boot mailing list