summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Glass <sjg@chromium.org>2018-11-18 08:14:31 -0700
committerSimon Glass <sjg@chromium.org>2018-11-29 09:30:05 -0700
commit3abe1115355900172a8681f5e6fa01ca71a9b916 (patch)
tree9c2aeebe14de6b7c227845d8b2df0372bc81cd8f
parentd0b4f68d199c075f5d734cf184f7eaf1a642083d (diff)
dm: core: Add a few more specific child-finding functions
Add two functions which can find a child device by uclass or by name. The first is useful with Multi-Function-Devices (MFDs) to find one of a particular type. The second is useful when only the name is known. Signed-off-by: Simon Glass <sjg@chromium.org>
-rw-r--r--drivers/core/device.c34
-rw-r--r--include/dm/device.h25
-rw-r--r--test/dm/test-fdt.c27
3 files changed, 86 insertions, 0 deletions
diff --git a/drivers/core/device.c b/drivers/core/device.c
index 47a697f3e5c..836bcadced5 100644
--- a/drivers/core/device.c
+++ b/drivers/core/device.c
@@ -699,6 +699,40 @@ int device_find_first_inactive_child(struct udevice *parent,
return -ENODEV;
}
+int device_find_first_child_by_uclass(struct udevice *parent,
+ enum uclass_id uclass_id,
+ struct udevice **devp)
+{
+ struct udevice *dev;
+
+ *devp = NULL;
+ list_for_each_entry(dev, &parent->child_head, sibling_node) {
+ if (device_get_uclass_id(dev) == uclass_id) {
+ *devp = dev;
+ return 0;
+ }
+ }
+
+ return -ENODEV;
+}
+
+int device_find_child_by_name(struct udevice *parent, const char *name,
+ struct udevice **devp)
+{
+ struct udevice *dev;
+
+ *devp = NULL;
+
+ list_for_each_entry(dev, &parent->child_head, sibling_node) {
+ if (!strcmp(dev->name, name)) {
+ *devp = dev;
+ return 0;
+ }
+ }
+
+ return -ENODEV;
+}
+
struct udevice *dev_get_parent(const struct udevice *child)
{
return child->parent;
diff --git a/include/dm/device.h b/include/dm/device.h
index 847934425bb..27a6d7b9fdb 100644
--- a/include/dm/device.h
+++ b/include/dm/device.h
@@ -525,6 +525,8 @@ int device_find_next_child(struct udevice **devp);
* This is used to locate an existing child of a device which is of a given
* uclass.
*
+ * The device is NOT probed
+ *
* @parent: Parent device to search
* @uclass_id: Uclass to look for
* @devp: Returns device found, if any
@@ -535,6 +537,29 @@ int device_find_first_inactive_child(struct udevice *parent,
struct udevice **devp);
/**
+ * device_find_first_child_by_uclass() - Find the first child of a device in uc
+ *
+ * @parent: Parent device to search
+ * @uclass_id: Uclass to look for
+ * @devp: Returns first child device in that uclass, if any
+ * @return 0 if found, else -ENODEV
+ */
+int device_find_first_child_by_uclass(struct udevice *parent,
+ enum uclass_id uclass_id,
+ struct udevice **devp);
+
+/**
+ * device_find_child_by_name() - Find a child by device name
+ *
+ * @parent: Parent device to search
+ * @name: Name to look for
+ * @devp: Returns device found, if any
+ * @return 0 if found, else -ENODEV
+ */
+int device_find_child_by_name(struct udevice *parent, const char *name,
+ struct udevice **devp);
+
+/**
* device_has_children() - check if a device has any children
*
* @dev: Device to check
diff --git a/test/dm/test-fdt.c b/test/dm/test-fdt.c
index b70e3fa18ac..0fbd9be765a 100644
--- a/test/dm/test-fdt.c
+++ b/test/dm/test-fdt.c
@@ -631,3 +631,30 @@ static int dm_test_fdt_phandle(struct unit_test_state *uts)
return 0;
}
DM_TEST(dm_test_fdt_phandle, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
+/* Test device_find_first_child_by_uclass() */
+static int dm_test_first_child(struct unit_test_state *uts)
+{
+ struct udevice *i2c, *dev, *dev2;
+
+ ut_assertok(uclass_first_device_err(UCLASS_I2C, &i2c));
+ ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_RTC, &dev));
+ ut_asserteq_str("rtc@43", dev->name);
+ ut_assertok(device_find_child_by_name(i2c, "rtc@43", &dev2));
+ ut_asserteq_ptr(dev, dev2);
+ ut_assertok(device_find_child_by_name(i2c, "rtc@61", &dev2));
+ ut_asserteq_str("rtc@61", dev2->name);
+
+ ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_I2C_EEPROM,
+ &dev));
+ ut_asserteq_str("eeprom@2c", dev->name);
+ ut_assertok(device_find_child_by_name(i2c, "eeprom@2c", &dev2));
+ ut_asserteq_ptr(dev, dev2);
+
+ ut_asserteq(-ENODEV, device_find_first_child_by_uclass(i2c,
+ UCLASS_VIDEO, &dev));
+ ut_asserteq(-ENODEV, device_find_child_by_name(i2c, "missing", &dev));
+
+ return 0;
+}
+DM_TEST(dm_test_first_child, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);