From f4cdead24a1a0c39c29c04e107c2f98ba61c5da8 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Wed, 23 Jul 2014 06:55:14 -0600 Subject: [PATCH] dm: Allow a device to be found by its FDT offset Each device that was bound from a device tree has an node that caused it to be bound. Add functions that find and return a device based on a device tree offset. Signed-off-by: Simon Glass --- doc/driver-model/README.txt | 3 ++- drivers/core/uclass.c | 35 +++++++++++++++++++++++++++++++++++ include/dm/uclass.h | 16 ++++++++++++++++ test/dm/test-fdt.c | 30 ++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 1 deletion(-) diff --git a/doc/driver-model/README.txt b/doc/driver-model/README.txt index 84d21cfb99..346cf08429 100644 --- a/doc/driver-model/README.txt +++ b/doc/driver-model/README.txt @@ -95,12 +95,13 @@ are provided in test/dm. To run them, try: You should see something like this: <...U-Boot banner...> - Running 15 driver model tests + Running 16 driver model tests Test: dm_test_autobind Test: dm_test_autoprobe Test: dm_test_children Test: dm_test_fdt Device 'd-test': seq 3 is in use by 'b-test' + Test: dm_test_fdt_offset Test: dm_test_fdt_pre_reloc Test: dm_test_fdt_uclass_seq Device 'd-test': seq 3 is in use by 'b-test' diff --git a/drivers/core/uclass.c b/drivers/core/uclass.c index c28cf6795f..a27f3d5027 100644 --- a/drivers/core/uclass.c +++ b/drivers/core/uclass.c @@ -187,6 +187,30 @@ int uclass_find_device_by_seq(enum uclass_id id, int seq_or_req_seq, return -ENODEV; } +static int uclass_find_device_by_of_offset(enum uclass_id id, int node, + struct udevice **devp) +{ + struct uclass *uc; + struct udevice *dev; + int ret; + + *devp = NULL; + if (node < 0) + return -ENODEV; + ret = uclass_get(id, &uc); + if (ret) + return ret; + + list_for_each_entry(dev, &uc->dev_head, uclass_node) { + if (dev->of_offset == node) { + *devp = dev; + return 0; + } + } + + return -ENODEV; +} + /** * uclass_get_device_tail() - handle the end of a get_device call * @@ -239,6 +263,17 @@ int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp) return uclass_get_device_tail(dev, ret, devp); } +int uclass_get_device_by_of_offset(enum uclass_id id, int node, + struct udevice **devp) +{ + struct udevice *dev; + int ret; + + *devp = NULL; + ret = uclass_find_device_by_of_offset(id, node, &dev); + return uclass_get_device_tail(dev, ret, devp); +} + int uclass_first_device(enum uclass_id id, struct udevice **devp) { struct uclass *uc; diff --git a/include/dm/uclass.h b/include/dm/uclass.h index 48ae2420df..0b5ade6d68 100644 --- a/include/dm/uclass.h +++ b/include/dm/uclass.h @@ -121,6 +121,22 @@ int uclass_get_device(enum uclass_id id, int index, struct udevice **devp); */ int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp); +/** + * uclass_get_device_by_of_offset() - Get a uclass device by device tree node + * + * This searches the devices in the uclass for one attached to the given + * device tree node. + * + * The device is probed to activate it ready for use. + * + * @id: ID to look up + * @node: Device tree offset to search for (if -ve then -ENODEV is returned) + * @devp: Returns pointer to device (there is only one for each node) + * @return 0 if OK, -ve on error + */ +int uclass_get_device_by_of_offset(enum uclass_id id, int node, + struct udevice **devp); + /** * uclass_first_device() - Get the first device in a uclass * diff --git a/test/dm/test-fdt.c b/test/dm/test-fdt.c index d8e94d8570..7980a683b5 100644 --- a/test/dm/test-fdt.c +++ b/test/dm/test-fdt.c @@ -215,3 +215,33 @@ static int dm_test_fdt_uclass_seq(struct dm_test_state *dms) return 0; } DM_TEST(dm_test_fdt_uclass_seq, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); + +/* Test that we can find a device by device tree offset */ +static int dm_test_fdt_offset(struct dm_test_state *dms) +{ + const void *blob = gd->fdt_blob; + struct udevice *dev; + int node; + + node = fdt_path_offset(blob, "/e-test"); + ut_assert(node > 0); + ut_assertok(uclass_get_device_by_of_offset(UCLASS_TEST_FDT, node, + &dev)); + ut_asserteq_str("e-test", dev->name); + + /* This node should not be bound */ + node = fdt_path_offset(blob, "/junk"); + ut_assert(node > 0); + ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT, + node, &dev)); + + /* This is not a top level node so should not be probed */ + node = fdt_path_offset(blob, "/some-bus/c-test"); + ut_assert(node > 0); + ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT, + node, &dev)); + + return 0; +} + +DM_TEST(dm_test_fdt_offset, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); -- 2.39.5