Commit f4cdead2 authored by Simon Glass's avatar Simon Glass
Browse files

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: default avatarSimon Glass <sjg@chromium.org>
parent b7d66570
......@@ -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'
......
......@@ -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;
......
......@@ -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
*
......
......@@ -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);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment