/src/barebox/drivers/of/device.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-only |
2 | | #include <common.h> |
3 | | #include <of.h> |
4 | | #include <of_device.h> |
5 | | #include <of_address.h> |
6 | | |
7 | | /** |
8 | | * of_match_device - Tell if a struct device matches an of_device_id list |
9 | | * @ids: array of of device match structures to search in |
10 | | * @dev: the of device structure to match against |
11 | | * |
12 | | * Used by a driver to check whether an platform_device present in the |
13 | | * system is in its list of supported devices. |
14 | | */ |
15 | | const struct of_device_id *of_match_device(const struct of_device_id *matches, |
16 | | const struct device *dev) |
17 | 0 | { |
18 | 0 | if ((!matches) || (!dev->of_node)) |
19 | 0 | return NULL; |
20 | | |
21 | 0 | return of_match_node(matches, dev->of_node); |
22 | 0 | } |
23 | | EXPORT_SYMBOL(of_match_device); |
24 | | |
25 | | const void *of_device_get_match_data(const struct device *dev) |
26 | 0 | { |
27 | 0 | const struct of_device_id *match; |
28 | |
|
29 | 0 | match = of_match_device(dev->driver->of_compatible, dev); |
30 | 0 | if (!match) |
31 | 0 | return NULL; |
32 | | |
33 | 0 | return match->data; |
34 | 0 | } |
35 | | EXPORT_SYMBOL(of_device_get_match_data); |
36 | | |
37 | | const char *of_device_get_match_compatible(const struct device *dev) |
38 | 0 | { |
39 | 0 | const struct of_device_id *match; |
40 | |
|
41 | 0 | match = of_match_device(dev->driver->of_compatible, dev); |
42 | 0 | if (!match) |
43 | 0 | return NULL; |
44 | | |
45 | 0 | return match->compatible; |
46 | 0 | } |
47 | | EXPORT_SYMBOL(of_device_get_match_compatible); |
48 | | |
49 | | /** |
50 | | * of_device_make_bus_id - Use the device node data to assign a unique name |
51 | | * @dev: pointer to device structure that is linked to a device tree node |
52 | | * |
53 | | * This routine will first try using the translated bus address to |
54 | | * derive a unique name. If it cannot, then it will prepend names from |
55 | | * parent nodes until a unique name can be derived. |
56 | | */ |
57 | | void of_device_make_bus_id(struct device *dev) |
58 | 286 | { |
59 | 286 | struct device_node *node = dev->of_node; |
60 | 286 | const __be32 *reg; |
61 | 286 | u64 addr; |
62 | | |
63 | | /* Construct the name, using parent nodes if necessary to ensure uniqueness */ |
64 | 520 | while (node->parent) { |
65 | | /* |
66 | | * If the address can be translated, then that is as much |
67 | | * uniqueness as we need. Make it the first component and return |
68 | | */ |
69 | 338 | reg = of_get_property(node, "reg", NULL); |
70 | 338 | if (reg && (addr = of_translate_address(node, reg)) != OF_BAD_ADDR) { |
71 | 104 | dev_set_name(dev, dev->name ? "%llx.%s:%s" : "%llx.%s.of", |
72 | 104 | (unsigned long long)addr, node->name, |
73 | 104 | dev->name); |
74 | 104 | return; |
75 | 104 | } |
76 | | |
77 | | /* format arguments only used if dev_name() resolves to NULL */ |
78 | 234 | dev_set_name(dev, dev->name ? "%s:%s" : "%s.of", |
79 | 234 | kbasename(node->full_name), dev->name); |
80 | 234 | node = node->parent; |
81 | 234 | } |
82 | 286 | } |
83 | | EXPORT_SYMBOL_GPL(of_device_make_bus_id); |