1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 Google, Inc
6 * Marek Vasut <marex@denx.de>
9 #define LOG_CATEGORY LOGC_DM
14 #include <dm/device.h>
15 #include <dm/device-internal.h>
17 #include <dm/platdata.h>
18 #include <dm/uclass.h>
21 #include <linux/compiler.h>
23 struct driver *lists_driver_lookup_name(const char *name)
26 ll_entry_start(struct driver, driver);
27 const int n_ents = ll_entry_count(struct driver, driver);
30 for (entry = drv; entry != drv + n_ents; entry++) {
31 if (!strcmp(name, entry->name))
39 struct uclass_driver *lists_uclass_lookup(enum uclass_id id)
41 struct uclass_driver *uclass =
42 ll_entry_start(struct uclass_driver, uclass);
43 const int n_ents = ll_entry_count(struct uclass_driver, uclass);
44 struct uclass_driver *entry;
46 for (entry = uclass; entry != uclass + n_ents; entry++) {
54 int lists_bind_drivers(struct udevice *parent, bool pre_reloc_only)
56 struct driver_info *info =
57 ll_entry_start(struct driver_info, driver_info);
58 const int n_ents = ll_entry_count(struct driver_info, driver_info);
59 struct driver_info *entry;
64 for (entry = info; entry != info + n_ents; entry++) {
65 ret = device_bind_by_name(parent, pre_reloc_only, entry, &dev);
66 if (ret && ret != -EPERM) {
67 dm_warn("No match for driver '%s'\n", entry->name);
68 if (!result || ret != -ENOENT)
76 int device_bind_driver(struct udevice *parent, const char *drv_name,
77 const char *dev_name, struct udevice **devp)
79 return device_bind_driver_to_node(parent, drv_name, dev_name,
83 int device_bind_driver_to_node(struct udevice *parent, const char *drv_name,
84 const char *dev_name, ofnode node,
85 struct udevice **devp)
90 drv = lists_driver_lookup_name(drv_name);
92 debug("Cannot find driver '%s'\n", drv_name);
95 ret = device_bind_with_driver_data(parent, drv, dev_name, 0 /* data */,
101 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
103 * driver_check_compatible() - Check if a driver matches a compatible string
105 * @param of_match: List of compatible strings to match
106 * @param of_idp: Returns the match that was found
107 * @param compat: The compatible string to search for
108 * @return 0 if there is a match, -ENOENT if no match
110 static int driver_check_compatible(const struct udevice_id *of_match,
111 const struct udevice_id **of_idp,
117 while (of_match->compatible) {
118 if (!strcmp(of_match->compatible, compat)) {
128 int lists_bind_fdt(struct udevice *parent, ofnode node, struct udevice **devp,
131 struct driver *driver = ll_entry_start(struct driver, driver);
132 const int n_ents = ll_entry_count(struct driver, driver);
133 const struct udevice_id *id;
134 struct driver *entry;
137 const char *name, *compat_list, *compat;
138 int compat_length, i;
144 name = ofnode_get_name(node);
145 log_debug("bind node %s\n", name);
147 compat_list = ofnode_get_property(node, "compatible", &compat_length);
149 if (compat_length == -FDT_ERR_NOTFOUND) {
150 log_debug("Device '%s' has no compatible string\n",
155 dm_warn("Device tree error at node '%s'\n", name);
156 return compat_length;
160 * Walk through the compatible string list, attempting to match each
161 * compatible string in order such that we match in order of priority
162 * from the first string to the last.
164 for (i = 0; i < compat_length; i += strlen(compat) + 1) {
165 compat = compat_list + i;
166 log_debug(" - attempt to match compatible string '%s'\n",
169 for (entry = driver; entry != driver + n_ents; entry++) {
170 ret = driver_check_compatible(entry->of_match, &id,
175 if (entry == driver + n_ents)
178 if (pre_reloc_only) {
179 if (!ofnode_pre_reloc(node) &&
180 !(entry->flags & DM_FLAG_PRE_RELOC)) {
181 log_debug("Skipping device pre-relocation\n");
186 log_debug(" - found match at '%s': '%s' matches '%s'\n",
187 entry->name, entry->of_match->compatible,
189 ret = device_bind_with_driver_data(parent, entry, name,
190 id->data, node, &dev);
191 if (ret == -ENODEV) {
192 log_debug("Driver '%s' refuses to bind\n", entry->name);
196 dm_warn("Error binding driver '%s': %d\n", entry->name,
207 if (!found && !result && ret != -ENODEV)
208 log_debug("No match for node '%s'\n", name);