[PATCH 3/5] driver: implement find_device() helper

Ahmad Fatoum a.fatoum at pengutronix.de
Mon Sep 5 03:35:44 PDT 2022


For use in commands like done in the follow-up commits, it can be useful
to have a best effort find_device, that not only finds by device name,
but also by device tree path or alias. Add such a helper.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 drivers/base/driver.c | 16 ++++++++++++++++
 include/driver.h      |  5 +++++
 2 files changed, 21 insertions(+)

diff --git a/drivers/base/driver.c b/drivers/base/driver.c
index 072870bea444..cbe1c974f4fe 100644
--- a/drivers/base/driver.c
+++ b/drivers/base/driver.c
@@ -46,6 +46,22 @@ LIST_HEAD(active_device_list);
 EXPORT_SYMBOL(active_device_list);
 static LIST_HEAD(deferred);
 
+struct device_d *find_device(const char *str)
+{
+	struct device_d *dev;
+	struct device_node *np;
+
+	dev = get_device_by_name(str);
+	if (dev)
+		return dev;
+
+	np = of_find_node_by_path_or_alias(NULL, str);
+	if (np)
+		return of_find_device_by_node(np);
+
+	return NULL;
+}
+
 struct device_d *get_device_by_name(const char *name)
 {
 	struct device_d *dev;
diff --git a/include/driver.h b/include/driver.h
index 67ffbba6be80..543287a27638 100644
--- a/include/driver.h
+++ b/include/driver.h
@@ -169,6 +169,11 @@ struct device_d *get_device_by_type(ulong type, struct device_d *last);
 struct device_d *get_device_by_id(const char *id);
 struct device_d *get_device_by_name(const char *name);
 
+/* Find a device by name and if not found look up by device tree path
+ * or alias
+ */
+struct device_d *find_device(const char *str);
+
 /* Find a free device id from the given template. This is archieved by
  * appending a number to the template. Dynamically created devices should
  * use this function rather than filling the id field themselves.
-- 
2.30.2




More information about the barebox mailing list