]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/core/lists.c
dm: core: remove meaningless if conditional
[karo-tx-uboot.git] / drivers / core / lists.c
1 /*
2  * Copyright (c) 2013 Google, Inc
3  *
4  * (C) Copyright 2012
5  * Marek Vasut <marex@denx.de>
6  *
7  * SPDX-License-Identifier:     GPL-2.0+
8  */
9
10 #include <common.h>
11 #include <errno.h>
12 #include <dm/device.h>
13 #include <dm/device-internal.h>
14 #include <dm/lists.h>
15 #include <dm/platdata.h>
16 #include <dm/uclass.h>
17 #include <dm/util.h>
18 #include <fdtdec.h>
19 #include <linux/compiler.h>
20
21 struct driver *lists_driver_lookup_name(const char *name)
22 {
23         struct driver *drv =
24                 ll_entry_start(struct driver, driver);
25         const int n_ents = ll_entry_count(struct driver, driver);
26         struct driver *entry;
27
28         if (!drv || !n_ents)
29                 return NULL;
30
31         for (entry = drv; entry != drv + n_ents; entry++) {
32                 if (!strcmp(name, entry->name))
33                         return entry;
34         }
35
36         /* Not found */
37         return NULL;
38 }
39
40 struct uclass_driver *lists_uclass_lookup(enum uclass_id id)
41 {
42         struct uclass_driver *uclass =
43                 ll_entry_start(struct uclass_driver, uclass);
44         const int n_ents = ll_entry_count(struct uclass_driver, uclass);
45         struct uclass_driver *entry;
46
47         if ((id == UCLASS_INVALID) || !uclass)
48                 return NULL;
49
50         for (entry = uclass; entry != uclass + n_ents; entry++) {
51                 if (entry->id == id)
52                         return entry;
53         }
54
55         return NULL;
56 }
57
58 int lists_bind_drivers(struct udevice *parent, bool pre_reloc_only)
59 {
60         struct driver_info *info =
61                 ll_entry_start(struct driver_info, driver_info);
62         const int n_ents = ll_entry_count(struct driver_info, driver_info);
63         struct driver_info *entry;
64         struct udevice *dev;
65         int result = 0;
66         int ret;
67
68         for (entry = info; entry != info + n_ents; entry++) {
69                 ret = device_bind_by_name(parent, pre_reloc_only, entry, &dev);
70                 if (ret && ret != -EPERM) {
71                         dm_warn("No match for driver '%s'\n", entry->name);
72                         if (!result || ret != -ENOENT)
73                                 result = ret;
74                 }
75         }
76
77         return result;
78 }
79
80 int device_bind_driver(struct udevice *parent, const char *drv_name,
81                        const char *dev_name, struct udevice **devp)
82 {
83         struct driver *drv;
84         int ret;
85
86         drv = lists_driver_lookup_name(drv_name);
87         if (!drv) {
88                 printf("Cannot find driver '%s'\n", drv_name);
89                 return -ENOENT;
90         }
91         ret = device_bind(parent, drv, dev_name, NULL, -1, devp);
92         if (ret) {
93                 printf("Cannot create device named '%s' (err=%d)\n",
94                        dev_name, ret);
95                 return ret;
96         }
97
98         return 0;
99 }
100
101 #ifdef CONFIG_OF_CONTROL
102 /**
103  * driver_check_compatible() - Check if a driver is compatible with this node
104  *
105  * @param blob:         Device tree pointer
106  * @param offset:       Offset of node in device tree
107  * @param of_match:     List of compatible strings to match
108  * @param of_idp:       Returns the match that was found
109  * @return 0 if there is a match, -ENOENT if no match, -ENODEV if the node
110  * does not have a compatible string, other error <0 if there is a device
111  * tree error
112  */
113 static int driver_check_compatible(const void *blob, int offset,
114                                    const struct udevice_id *of_match,
115                                    const struct udevice_id **of_idp)
116 {
117         int ret;
118
119         *of_idp = NULL;
120         if (!of_match)
121                 return -ENOENT;
122
123         while (of_match->compatible) {
124                 ret = fdt_node_check_compatible(blob, offset,
125                                                 of_match->compatible);
126                 if (!ret) {
127                         *of_idp = of_match;
128                         return 0;
129                 } else if (ret == -FDT_ERR_NOTFOUND) {
130                         return -ENODEV;
131                 } else if (ret < 0) {
132                         return -EINVAL;
133                 }
134                 of_match++;
135         }
136
137         return -ENOENT;
138 }
139
140 int lists_bind_fdt(struct udevice *parent, const void *blob, int offset,
141                    struct udevice **devp)
142 {
143         struct driver *driver = ll_entry_start(struct driver, driver);
144         const int n_ents = ll_entry_count(struct driver, driver);
145         const struct udevice_id *id;
146         struct driver *entry;
147         struct udevice *dev;
148         bool found = false;
149         const char *name;
150         int result = 0;
151         int ret = 0;
152
153         dm_dbg("bind node %s\n", fdt_get_name(blob, offset, NULL));
154         if (devp)
155                 *devp = NULL;
156         for (entry = driver; entry != driver + n_ents; entry++) {
157                 ret = driver_check_compatible(blob, offset, entry->of_match,
158                                               &id);
159                 name = fdt_get_name(blob, offset, NULL);
160                 if (ret == -ENOENT) {
161                         continue;
162                 } else if (ret == -ENODEV) {
163                         dm_dbg("Device '%s' has no compatible string\n", name);
164                         break;
165                 } else if (ret) {
166                         dm_warn("Device tree error at offset %d\n", offset);
167                         result = ret;
168                         break;
169                 }
170
171                 dm_dbg("   - found match at '%s'\n", entry->name);
172                 ret = device_bind(parent, entry, name, NULL, offset, &dev);
173                 if (ret) {
174                         dm_warn("Error binding driver '%s'\n", entry->name);
175                         return ret;
176                 } else {
177                         dev->of_id = id;
178                         found = true;
179                         if (devp)
180                                 *devp = dev;
181                 }
182                 break;
183         }
184
185         if (!found && !result && ret != -ENODEV) {
186                 dm_dbg("No match for node '%s'\n",
187                        fdt_get_name(blob, offset, NULL));
188         }
189
190         return result;
191 }
192 #endif