Commit 40bb637dae10abff32ca2d952fd443e0e09fd524
1 parent
19c8205e68
Exists in
smarc_8mq_lf_v2020.04
and in
17 other branches
dm: core: Add a way to find a device by ofnode
Add a function which looks up a device by its node (either in live tree or flat tree). Signed-off-by: Simon Glass <sjg@chromium.org>
Showing 3 changed files with 72 additions and 0 deletions Side-by-side Diff
drivers/core/uclass.c
... | ... | @@ -8,6 +8,7 @@ |
8 | 8 | */ |
9 | 9 | |
10 | 10 | #include <common.h> |
11 | +#include <dm.h> | |
11 | 12 | #include <errno.h> |
12 | 13 | #include <malloc.h> |
13 | 14 | #include <dm/device.h> |
... | ... | @@ -287,6 +288,30 @@ |
287 | 288 | return -ENODEV; |
288 | 289 | } |
289 | 290 | |
291 | +int uclass_find_device_by_ofnode(enum uclass_id id, ofnode node, | |
292 | + struct udevice **devp) | |
293 | +{ | |
294 | + struct uclass *uc; | |
295 | + struct udevice *dev; | |
296 | + int ret; | |
297 | + | |
298 | + *devp = NULL; | |
299 | + if (!ofnode_valid(node)) | |
300 | + return -ENODEV; | |
301 | + ret = uclass_get(id, &uc); | |
302 | + if (ret) | |
303 | + return ret; | |
304 | + | |
305 | + list_for_each_entry(dev, &uc->dev_head, uclass_node) { | |
306 | + if (ofnode_equal(dev_ofnode(dev), node)) { | |
307 | + *devp = dev; | |
308 | + return 0; | |
309 | + } | |
310 | + } | |
311 | + | |
312 | + return -ENODEV; | |
313 | +} | |
314 | + | |
290 | 315 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
291 | 316 | static int uclass_find_device_by_phandle(enum uclass_id id, |
292 | 317 | struct udevice *parent, |
... | ... | @@ -404,6 +429,18 @@ |
404 | 429 | |
405 | 430 | *devp = NULL; |
406 | 431 | ret = uclass_find_device_by_of_offset(id, node, &dev); |
432 | + return uclass_get_device_tail(dev, ret, devp); | |
433 | +} | |
434 | + | |
435 | +int uclass_get_device_by_ofnode(enum uclass_id id, ofnode node, | |
436 | + struct udevice **devp) | |
437 | +{ | |
438 | + struct udevice *dev; | |
439 | + int ret; | |
440 | + | |
441 | + *devp = NULL; | |
442 | + ret = uclass_find_device_by_ofnode(id, node, &dev); | |
443 | + | |
407 | 444 | return uclass_get_device_tail(dev, ret, devp); |
408 | 445 | } |
409 | 446 |
include/dm/uclass-internal.h
... | ... | @@ -10,6 +10,8 @@ |
10 | 10 | #ifndef _DM_UCLASS_INTERNAL_H |
11 | 11 | #define _DM_UCLASS_INTERNAL_H |
12 | 12 | |
13 | +#include <dm/ofnode.h> | |
14 | + | |
13 | 15 | /** |
14 | 16 | * uclass_get_device_tail() - handle the end of a get_device call |
15 | 17 | * |
... | ... | @@ -113,6 +115,22 @@ |
113 | 115 | */ |
114 | 116 | int uclass_find_device_by_of_offset(enum uclass_id id, int node, |
115 | 117 | struct udevice **devp); |
118 | + | |
119 | +/** | |
120 | + * uclass_find_device_by_of_node() - Find a uclass device by device tree node | |
121 | + * | |
122 | + * This searches the devices in the uclass for one attached to the given | |
123 | + * device tree node. | |
124 | + * | |
125 | + * The device is NOT probed, it is merely returned. | |
126 | + * | |
127 | + * @id: ID to look up | |
128 | + * @node: Device tree offset to search for (if NULL then -ENODEV is returned) | |
129 | + * @devp: Returns pointer to device (there is only one for each node) | |
130 | + * @return 0 if OK, -ve on error | |
131 | + */ | |
132 | +int uclass_find_device_by_ofnode(enum uclass_id id, ofnode node, | |
133 | + struct udevice **devp); | |
116 | 134 | |
117 | 135 | /** |
118 | 136 | * uclass_bind_device() - Associate device with a uclass |
include/dm/uclass.h
... | ... | @@ -10,6 +10,7 @@ |
10 | 10 | #ifndef _DM_UCLASS_H |
11 | 11 | #define _DM_UCLASS_H |
12 | 12 | |
13 | +#include <dm/ofnode.h> | |
13 | 14 | #include <dm/uclass-id.h> |
14 | 15 | #include <linker_lists.h> |
15 | 16 | #include <linux/list.h> |
... | ... | @@ -184,6 +185,22 @@ |
184 | 185 | */ |
185 | 186 | int uclass_get_device_by_of_offset(enum uclass_id id, int node, |
186 | 187 | struct udevice **devp); |
188 | + | |
189 | +/** | |
190 | + * uclass_get_device_by_ofnode() - Get a uclass device by device tree node | |
191 | + * | |
192 | + * This searches the devices in the uclass for one attached to the given | |
193 | + * device tree node. | |
194 | + * | |
195 | + * The device is probed to activate it ready for use. | |
196 | + * | |
197 | + * @id: ID to look up | |
198 | + * @np: Device tree node to search for (if NULL then -ENODEV is returned) | |
199 | + * @devp: Returns pointer to device (there is only one for each node) | |
200 | + * @return 0 if OK, -ve on error | |
201 | + */ | |
202 | +int uclass_get_device_by_ofnode(enum uclass_id id, ofnode node, | |
203 | + struct udevice **devp); | |
187 | 204 | |
188 | 205 | /** |
189 | 206 | * uclass_get_device_by_phandle() - Get a uclass device by phandle |