[PATCH 18/21] dm: core: Allow obtaining a node offset in the same tree

Simon Glass sjg at chromium.org
Wed Aug 31 05:08:14 CEST 2022


In some cases we want to obtain an ofnode in the same tree as a different
ofnode, such as when looking up a subnode. At present this is trivial,
since there is only one tree. When there are multiple trees, this
implementation will change.

Also move the ofnode_to_offset() function up higher in the header file,
since we will need to provide a different implementation with multiple
trees.

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

 drivers/core/ofnode.c |  2 +-
 include/dm/ofnode.h   | 39 +++++++++++++++++++++++++++++----------
 2 files changed, 30 insertions(+), 11 deletions(-)

diff --git a/drivers/core/ofnode.c b/drivers/core/ofnode.c
index aceb809c1f2..a5c6e309615 100644
--- a/drivers/core/ofnode.c
+++ b/drivers/core/ofnode.c
@@ -1273,7 +1273,7 @@ int ofnode_add_subnode(ofnode node, const char *name, ofnode *subnodep)
 					 ofnode_to_offset(node), name);
 		if (offset < 0)
 			return offset == -FDT_ERR_EXISTS ? -EEXIST : 0;
-		subnode = offset_to_ofnode(offset);
+		subnode = noffset_to_ofnode(node, offset);
 	}
 
 	*subnodep = subnode;
diff --git a/include/dm/ofnode.h b/include/dm/ofnode.h
index ad196af2d45..a79909c12e1 100644
--- a/include/dm/ofnode.h
+++ b/include/dm/ofnode.h
@@ -54,6 +54,23 @@ static inline void *ofnode_to_fdt(ofnode node)
 	return (void *)gd->fdt_blob;
 }
 
+/**
+ * ofnode_to_offset() - convert an ofnode to a flat DT offset
+ *
+ * This cannot be called if the reference contains a node pointer.
+ *
+ * @node: Reference containing offset (possibly invalid)
+ * Return: DT offset (can be -1)
+ */
+static inline int ofnode_to_offset(ofnode node)
+{
+#ifdef OF_CHECKS
+	if (of_live_active())
+		return -1;
+#endif
+	return node.of_offset;
+}
+
 /**
  * ofnode_to_np() - convert an ofnode to a live DT node pointer
  *
@@ -90,20 +107,22 @@ static inline struct device_node *ofnode_to_npw(ofnode node)
 }
 
 /**
- * ofnode_to_offset() - convert an ofnode to a flat DT offset
+ * noffset_to_ofnode() - convert a DT offset to an ofnode
  *
- * This cannot be called if the reference contains a node pointer.
- *
- * @node: Reference containing offset (possibly invalid)
- * Return: DT offset (can be -1)
+ * @other_node: Node in the same tree to use as a reference
+ * @of_offset: DT offset (either valid, or -1)
+ * Return: reference to the associated DT offset
  */
-static inline int ofnode_to_offset(ofnode node)
+static inline ofnode noffset_to_ofnode(ofnode other_node, int of_offset)
 {
-#ifdef OF_CHECKS
+	ofnode node;
+
 	if (of_live_active())
-		return -1;
-#endif
-	return node.of_offset;
+		node.np = NULL;
+	else
+		node.of_offset = of_offset;
+
+	return node;
 }
 
 /**
-- 
2.37.2.672.g94769d06f0-goog



More information about the U-Boot mailing list