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
13 #include <dm/device.h>
14 #include <dm/device-internal.h>
16 #include <dm/platdata.h>
17 #include <dm/uclass.h>
20 #include <linux/compiler.h>
22 struct driver *lists_driver_lookup_name(const char *name)
25 ll_entry_start(struct driver, driver);
26 const int n_ents = ll_entry_count(struct driver, driver);
29 for (entry = drv; entry != drv + n_ents; entry++) {
30 if (!strcmp(name, entry->name))
38 struct uclass_driver *lists_uclass_lookup(enum uclass_id id)
40 struct uclass_driver *uclass =
41 ll_entry_start(struct uclass_driver, uclass);
42 const int n_ents = ll_entry_count(struct uclass_driver, uclass);
43 struct uclass_driver *entry;
45 for (entry = uclass; entry != uclass + n_ents; entry++) {
53 int lists_bind_drivers(struct udevice *parent, bool pre_reloc_only)
55 struct driver_info *info =
56 ll_entry_start(struct driver_info, driver_info);
57 const int n_ents = ll_entry_count(struct driver_info, driver_info);
58 struct driver_info *entry;
63 for (entry = info; entry != info + n_ents; entry++) {
64 ret = device_bind_by_name(parent, pre_reloc_only, entry, &dev);
65 if (ret && ret != -EPERM) {
66 dm_warn("No match for driver '%s'\n", entry->name);
67 if (!result || ret != -ENOENT)
75 int device_bind_driver(struct udevice *parent, const char *drv_name,
76 const char *dev_name, struct udevice **devp)
78 return device_bind_driver_to_node(parent, drv_name, dev_name,
82 int device_bind_driver_to_node(struct udevice *parent, const char *drv_name,
83 const char *dev_name, ofnode node,
84 struct udevice **devp)
89 drv = lists_driver_lookup_name(drv_name);
91 debug("Cannot find driver '%s'\n", drv_name);
94 ret = device_bind_with_driver_data(parent, drv, dev_name, 0 /* data */,
100 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
102 * driver_check_compatible() - Check if a driver matches a compatible string
104 * @param of_match: List of compatible strings to match
105 * @param of_idp: Returns the match that was found
106 * @param compat: The compatible string to search for
107 * @return 0 if there is a match, -ENOENT if no match
109 static int driver_check_compatible(const struct udevice_id *of_match,
110 const struct udevice_id **of_idp,
116 while (of_match->compatible) {
117 if (!strcmp(of_match->compatible, compat)) {
127 int lists_bind_fdt(struct udevice *parent, ofnode node, struct udevice **devp,
130 struct driver *driver = ll_entry_start(struct driver, driver);
131 const int n_ents = ll_entry_count(struct driver, driver);
132 const struct udevice_id *id;
133 struct driver *entry;
136 const char *name, *compat_list, *compat;
137 int compat_length, i;
143 name = ofnode_get_name(node);
144 log_debug("bind node %s\n", name);
146 compat_list = ofnode_get_property(node, "compatible", &compat_length);
148 if (compat_length == -FDT_ERR_NOTFOUND) {
149 log_debug("Device '%s' has no compatible string\n",
154 dm_warn("Device tree error at node '%s'\n", name);
155 return compat_length;
159 * Walk through the compatible string list, attempting to match each
160 * compatible string in order such that we match in order of priority
161 * from the first string to the last.
163 for (i = 0; i < compat_length; i += strlen(compat) + 1) {
164 compat = compat_list + i;
165 log_debug(" - attempt to match compatible string '%s'\n",
168 for (entry = driver; entry != driver + n_ents; entry++) {
169 ret = driver_check_compatible(entry->of_match, &id,
174 if (entry == driver + n_ents)
177 if (pre_reloc_only) {
178 if (!dm_ofnode_pre_reloc(node) &&
179 !(entry->flags & DM_FLAG_PRE_RELOC))
183 log_debug(" - found match at '%s': '%s' matches '%s'\n",
184 entry->name, entry->of_match->compatible,
186 ret = device_bind_with_driver_data(parent, entry, name,
187 id->data, node, &dev);
188 if (ret == -ENODEV) {
189 log_debug("Driver '%s' refuses to bind\n", entry->name);
193 dm_warn("Error binding driver '%s': %d\n", entry->name,
204 if (!found && !result && ret != -ENODEV)
205 log_debug("No match for node '%s'\n", name);