ofnode: add {ofnode, dev}_read_resource_byname()
Linux supports platform_get_resource_byname() to look up a resource
by name.
We want a similar helper. It is useful when a device node has named
register regions.
Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
diff --git a/drivers/core/ofnode.c b/drivers/core/ofnode.c
index c1a2e9f..0685b68 100644
--- a/drivers/core/ofnode.c
+++ b/drivers/core/ofnode.c
@@ -627,3 +627,15 @@
return 0;
}
}
+
+int ofnode_read_resource_byname(ofnode node, const char *name,
+ struct resource *res)
+{
+ int index;
+
+ index = ofnode_stringlist_search(node, "reg-names", name);
+ if (index < 0)
+ return index;
+
+ return ofnode_read_resource(node, index, res);
+}
diff --git a/drivers/core/read.c b/drivers/core/read.c
index fe40bed..6acb333 100644
--- a/drivers/core/read.c
+++ b/drivers/core/read.c
@@ -164,3 +164,9 @@
{
return ofnode_read_resource(dev_ofnode(dev), index, res);
}
+
+int dev_read_resource_byname(struct udevice *dev, const char *name,
+ struct resource *res)
+{
+ return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
+}
diff --git a/include/dm/ofnode.h b/include/dm/ofnode.h
index 210ddb2..de2769e 100644
--- a/include/dm/ofnode.h
+++ b/include/dm/ofnode.h
@@ -625,5 +625,7 @@
bool ofnode_pre_reloc(ofnode node);
int ofnode_read_resource(ofnode node, uint index, struct resource *res);
+int ofnode_read_resource_byname(ofnode node, const char *name,
+ struct resource *res);
#endif
diff --git a/include/dm/read.h b/include/dm/read.h
index c3a4a56..49d69c9 100644
--- a/include/dm/read.h
+++ b/include/dm/read.h
@@ -359,13 +359,24 @@
/**
* dev_read_resource() - obtain an indexed resource from a device.
*
- * @dev: devuce to examine
+ * @dev: device to examine
* @index index of the resource to retrieve (0 = first)
* @res returns the resource
* @return 0 if ok, negative on error
*/
int dev_read_resource(struct udevice *dev, uint index, struct resource *res);
+/**
+ * dev_read_resource_byname() - obtain a named resource from a device.
+ *
+ * @dev: device to examine
+ * @name: name of the resource to retrieve
+ * @res: returns the resource
+ * @return 0 if ok, negative on error
+ */
+int dev_read_resource_byname(struct udevice *dev, const char *name,
+ struct resource *res);
+
#else /* CONFIG_DM_DEV_READ_INLINE is enabled */
static inline int dev_read_u32_default(struct udevice *dev,
@@ -513,6 +524,13 @@
return ofnode_read_resource(dev_ofnode(dev), index, res);
}
+static inline int dev_read_resource_byname(struct udevice *dev,
+ const char *name,
+ struct resource *res)
+{
+ return ofnode_read_resource_byname(dev_ofnode(dev), name, res);
+}
+
#endif /* CONFIG_DM_DEV_READ_INLINE */
/**