]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
6494d708 SG |
2 | /* |
3 | * Copyright (c) 2013 Google, Inc | |
4 | * | |
5 | * (C) Copyright 2012 | |
6 | * Pavel Herrmann <[email protected]> | |
6494d708 SG |
7 | */ |
8 | ||
97b5f9d1 KY |
9 | #define LOG_CATEGORY LOGC_DM |
10 | ||
40bb637d | 11 | #include <dm.h> |
6494d708 | 12 | #include <errno.h> |
f7ae49fc | 13 | #include <log.h> |
6494d708 | 14 | #include <malloc.h> |
401d1c4f | 15 | #include <asm/global_data.h> |
6494d708 SG |
16 | #include <dm/device.h> |
17 | #include <dm/device-internal.h> | |
18 | #include <dm/lists.h> | |
19 | #include <dm/uclass.h> | |
20 | #include <dm/uclass-internal.h> | |
21 | #include <dm/util.h> | |
22 | ||
23 | DECLARE_GLOBAL_DATA_PTR; | |
24 | ||
25 | struct uclass *uclass_find(enum uclass_id key) | |
26 | { | |
27 | struct uclass *uc; | |
28 | ||
c910e2e2 SG |
29 | if (!gd->dm_root) |
30 | return NULL; | |
6494d708 SG |
31 | /* |
32 | * TODO([email protected]): Optimise this, perhaps moving the found | |
33 | * node to the start of the list, or creating a linear array mapping | |
34 | * id to node. | |
35 | */ | |
8a715530 | 36 | list_for_each_entry(uc, gd->uclass_root, sibling_node) { |
6494d708 SG |
37 | if (uc->uc_drv->id == key) |
38 | return uc; | |
39 | } | |
40 | ||
41 | return NULL; | |
42 | } | |
43 | ||
44 | /** | |
45 | * uclass_add() - Create new uclass in list | |
46 | * @id: Id number to create | |
47 | * @ucp: Returns pointer to uclass, or NULL on error | |
185f812c | 48 | * Return: 0 on success, -ve on error |
6494d708 SG |
49 | * |
50 | * The new uclass is added to the list. There must be only one uclass for | |
51 | * each id. | |
52 | */ | |
53 | static int uclass_add(enum uclass_id id, struct uclass **ucp) | |
54 | { | |
55 | struct uclass_driver *uc_drv; | |
56 | struct uclass *uc; | |
57 | int ret; | |
58 | ||
59 | *ucp = NULL; | |
60 | uc_drv = lists_uclass_lookup(id); | |
61 | if (!uc_drv) { | |
643e6902 MY |
62 | debug("Cannot find uclass for id %d: please add the UCLASS_DRIVER() declaration for this UCLASS_... id\n", |
63 | id); | |
3346c876 SG |
64 | /* |
65 | * Use a strange error to make this case easier to find. When | |
66 | * a uclass is not available it can prevent driver model from | |
67 | * starting up and this failure is otherwise hard to debug. | |
68 | */ | |
69 | return -EPFNOSUPPORT; | |
6494d708 | 70 | } |
6494d708 SG |
71 | uc = calloc(1, sizeof(*uc)); |
72 | if (!uc) | |
73 | return -ENOMEM; | |
41575d8e | 74 | if (uc_drv->priv_auto) { |
89ba6d55 SG |
75 | void *ptr; |
76 | ||
77 | ptr = calloc(1, uc_drv->priv_auto); | |
78 | if (!ptr) { | |
6494d708 SG |
79 | ret = -ENOMEM; |
80 | goto fail_mem; | |
81 | } | |
89ba6d55 | 82 | uclass_set_priv(uc, ptr); |
6494d708 SG |
83 | } |
84 | uc->uc_drv = uc_drv; | |
85 | INIT_LIST_HEAD(&uc->sibling_node); | |
86 | INIT_LIST_HEAD(&uc->dev_head); | |
8a715530 | 87 | list_add(&uc->sibling_node, DM_UCLASS_ROOT_NON_CONST); |
6494d708 SG |
88 | |
89 | if (uc_drv->init) { | |
90 | ret = uc_drv->init(uc); | |
91 | if (ret) | |
92 | goto fail; | |
93 | } | |
94 | ||
95 | *ucp = uc; | |
96 | ||
97 | return 0; | |
98 | fail: | |
41575d8e | 99 | if (uc_drv->priv_auto) { |
89ba6d55 SG |
100 | free(uclass_get_priv(uc)); |
101 | uclass_set_priv(uc, NULL); | |
6494d708 SG |
102 | } |
103 | list_del(&uc->sibling_node); | |
104 | fail_mem: | |
105 | free(uc); | |
106 | ||
107 | return ret; | |
108 | } | |
109 | ||
110 | int uclass_destroy(struct uclass *uc) | |
111 | { | |
112 | struct uclass_driver *uc_drv; | |
07d260e0 | 113 | struct udevice *dev; |
6494d708 SG |
114 | int ret; |
115 | ||
07d260e0 SG |
116 | /* |
117 | * We cannot use list_for_each_entry_safe() here. If a device in this | |
118 | * uclass has a child device also in this uclass, it will be also be | |
119 | * unbound (by the recursion in the call to device_unbind() below). | |
120 | * We can loop until the list is empty. | |
121 | */ | |
122 | while (!list_empty(&uc->dev_head)) { | |
123 | dev = list_first_entry(&uc->dev_head, struct udevice, | |
124 | uclass_node); | |
ced10804 | 125 | ret = device_remove(dev, DM_REMOVE_NORMAL | DM_REMOVE_NO_PD); |
6494d708 | 126 | if (ret) |
8474da94 | 127 | return log_msg_ret("remove", ret); |
6494d708 SG |
128 | ret = device_unbind(dev); |
129 | if (ret) | |
8474da94 | 130 | return log_msg_ret("unbind", ret); |
6494d708 SG |
131 | } |
132 | ||
133 | uc_drv = uc->uc_drv; | |
134 | if (uc_drv->destroy) | |
135 | uc_drv->destroy(uc); | |
136 | list_del(&uc->sibling_node); | |
41575d8e | 137 | if (uc_drv->priv_auto) |
89ba6d55 | 138 | free(uclass_get_priv(uc)); |
6494d708 SG |
139 | free(uc); |
140 | ||
141 | return 0; | |
142 | } | |
143 | ||
144 | int uclass_get(enum uclass_id id, struct uclass **ucp) | |
145 | { | |
146 | struct uclass *uc; | |
147 | ||
1e9ced28 SG |
148 | /* Immediately fail if driver model is not set up */ |
149 | if (!gd->uclass_root) | |
150 | return -EDEADLK; | |
6494d708 SG |
151 | *ucp = NULL; |
152 | uc = uclass_find(id); | |
91bcfdf0 SG |
153 | if (!uc) { |
154 | if (CONFIG_IS_ENABLED(OF_PLATDATA_INST)) | |
155 | return -ENOENT; | |
6494d708 | 156 | return uclass_add(id, ucp); |
91bcfdf0 | 157 | } |
6494d708 SG |
158 | *ucp = uc; |
159 | ||
160 | return 0; | |
161 | } | |
162 | ||
0a5f6f86 SG |
163 | const char *uclass_get_name(enum uclass_id id) |
164 | { | |
165 | struct uclass *uc; | |
166 | ||
167 | if (uclass_get(id, &uc)) | |
168 | return NULL; | |
169 | return uc->uc_drv->name; | |
170 | } | |
171 | ||
80647393 SG |
172 | void *uclass_get_priv(const struct uclass *uc) |
173 | { | |
fb8c9fb3 | 174 | return uc->priv_; |
80647393 SG |
175 | } |
176 | ||
177 | void uclass_set_priv(struct uclass *uc, void *priv) | |
178 | { | |
fb8c9fb3 | 179 | uc->priv_ = priv; |
80647393 SG |
180 | } |
181 | ||
6aa4fe39 | 182 | enum uclass_id uclass_get_by_namelen(const char *name, int len) |
6e43d1b1 SG |
183 | { |
184 | int i; | |
185 | ||
186 | for (i = 0; i < UCLASS_COUNT; i++) { | |
187 | struct uclass_driver *uc_drv = lists_uclass_lookup(i); | |
188 | ||
6aa4fe39 SG |
189 | if (uc_drv && !strncmp(uc_drv->name, name, len) && |
190 | strlen(uc_drv->name) == len) | |
6e43d1b1 SG |
191 | return i; |
192 | } | |
193 | ||
194 | return UCLASS_INVALID; | |
195 | } | |
196 | ||
4b030177 SG |
197 | enum uclass_id uclass_get_by_name(const char *name) |
198 | { | |
6aa4fe39 | 199 | return uclass_get_by_namelen(name, strlen(name)); |
4b030177 SG |
200 | } |
201 | ||
e7c86562 JJH |
202 | int dev_get_uclass_index(struct udevice *dev, struct uclass **ucp) |
203 | { | |
204 | struct udevice *iter; | |
205 | struct uclass *uc = dev->uclass; | |
206 | int i = 0; | |
207 | ||
208 | if (list_empty(&uc->dev_head)) | |
209 | return -ENODEV; | |
210 | ||
81f351d6 | 211 | uclass_foreach_dev(iter, uc) { |
e7c86562 JJH |
212 | if (iter == dev) { |
213 | if (ucp) | |
214 | *ucp = uc; | |
215 | return i; | |
216 | } | |
217 | i++; | |
218 | } | |
219 | ||
220 | return -ENODEV; | |
221 | } | |
222 | ||
54c5d08a | 223 | int uclass_find_device(enum uclass_id id, int index, struct udevice **devp) |
6494d708 SG |
224 | { |
225 | struct uclass *uc; | |
54c5d08a | 226 | struct udevice *dev; |
6494d708 SG |
227 | int ret; |
228 | ||
229 | *devp = NULL; | |
230 | ret = uclass_get(id, &uc); | |
231 | if (ret) | |
232 | return ret; | |
2fda14ae SG |
233 | if (list_empty(&uc->dev_head)) |
234 | return -ENODEV; | |
6494d708 | 235 | |
81f351d6 | 236 | uclass_foreach_dev(dev, uc) { |
6494d708 SG |
237 | if (!index--) { |
238 | *devp = dev; | |
239 | return 0; | |
240 | } | |
241 | } | |
242 | ||
243 | return -ENODEV; | |
244 | } | |
245 | ||
c1d6f919 PM |
246 | int uclass_find_first_device(enum uclass_id id, struct udevice **devp) |
247 | { | |
248 | struct uclass *uc; | |
249 | int ret; | |
250 | ||
251 | *devp = NULL; | |
252 | ret = uclass_get(id, &uc); | |
253 | if (ret) | |
254 | return ret; | |
255 | if (list_empty(&uc->dev_head)) | |
4805a7af | 256 | return 0; |
c1d6f919 PM |
257 | |
258 | *devp = list_first_entry(&uc->dev_head, struct udevice, uclass_node); | |
259 | ||
260 | return 0; | |
261 | } | |
262 | ||
263 | int uclass_find_next_device(struct udevice **devp) | |
264 | { | |
265 | struct udevice *dev = *devp; | |
266 | ||
267 | *devp = NULL; | |
268 | if (list_is_last(&dev->uclass_node, &dev->uclass->dev_head)) | |
269 | return 0; | |
270 | ||
271 | *devp = list_entry(dev->uclass_node.next, struct udevice, uclass_node); | |
272 | ||
273 | return 0; | |
274 | } | |
275 | ||
4e0710a2 SG |
276 | int uclass_find_device_by_namelen(enum uclass_id id, const char *name, int len, |
277 | struct udevice **devp) | |
e0735a4c PM |
278 | { |
279 | struct uclass *uc; | |
280 | struct udevice *dev; | |
281 | int ret; | |
282 | ||
283 | *devp = NULL; | |
284 | if (!name) | |
285 | return -EINVAL; | |
286 | ret = uclass_get(id, &uc); | |
287 | if (ret) | |
288 | return ret; | |
289 | ||
81f351d6 | 290 | uclass_foreach_dev(dev, uc) { |
4e0710a2 SG |
291 | if (!strncmp(dev->name, name, len) && |
292 | strlen(dev->name) == len) { | |
e0735a4c PM |
293 | *devp = dev; |
294 | return 0; | |
295 | } | |
296 | } | |
297 | ||
298 | return -ENODEV; | |
299 | } | |
300 | ||
4e0710a2 SG |
301 | int uclass_find_device_by_name(enum uclass_id id, const char *name, |
302 | struct udevice **devp) | |
303 | { | |
304 | return uclass_find_device_by_namelen(id, name, strlen(name), devp); | |
305 | } | |
306 | ||
a133e217 | 307 | int uclass_find_next_free_seq(struct uclass *uc) |
3542ff29 | 308 | { |
3542ff29 | 309 | struct udevice *dev; |
3542ff29 JJH |
310 | int max = -1; |
311 | ||
a133e217 SG |
312 | /* If using aliases, start with the highest alias value */ |
313 | if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) && | |
314 | (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) | |
315 | max = dev_read_alias_highest_id(uc->uc_drv->name); | |
316 | ||
317 | /* Avoid conflict with existing devices */ | |
3542ff29 | 318 | list_for_each_entry(dev, &uc->dev_head, uclass_node) { |
2462139f SG |
319 | if (dev->seq_ > max) |
320 | max = dev->seq_; | |
3542ff29 | 321 | } |
a133e217 SG |
322 | /* |
323 | * At this point, max will be -1 if there are no existing aliases or | |
324 | * devices | |
325 | */ | |
3542ff29 JJH |
326 | |
327 | return max + 1; | |
328 | } | |
3542ff29 | 329 | |
99175919 | 330 | int uclass_find_device_by_seq(enum uclass_id id, int seq, struct udevice **devp) |
5a66a8ff SG |
331 | { |
332 | struct uclass *uc; | |
333 | struct udevice *dev; | |
334 | int ret; | |
335 | ||
336 | *devp = NULL; | |
99175919 SG |
337 | log_debug("%d\n", seq); |
338 | if (seq == -1) | |
5a66a8ff SG |
339 | return -ENODEV; |
340 | ret = uclass_get(id, &uc); | |
341 | if (ret) | |
342 | return ret; | |
343 | ||
81f351d6 | 344 | uclass_foreach_dev(dev, uc) { |
2462139f SG |
345 | log_debug(" - %d '%s'\n", dev->seq_, dev->name); |
346 | if (dev->seq_ == seq) { | |
5a66a8ff | 347 | *devp = dev; |
97b5f9d1 | 348 | log_debug(" - found\n"); |
5a66a8ff SG |
349 | return 0; |
350 | } | |
351 | } | |
97b5f9d1 | 352 | log_debug(" - not found\n"); |
5a66a8ff SG |
353 | |
354 | return -ENODEV; | |
355 | } | |
356 | ||
1b30d61d SG |
357 | int uclass_find_device_by_of_offset(enum uclass_id id, int node, |
358 | struct udevice **devp) | |
f4cdead2 SG |
359 | { |
360 | struct uclass *uc; | |
361 | struct udevice *dev; | |
362 | int ret; | |
363 | ||
364 | *devp = NULL; | |
365 | if (node < 0) | |
366 | return -ENODEV; | |
367 | ret = uclass_get(id, &uc); | |
368 | if (ret) | |
369 | return ret; | |
370 | ||
81f351d6 | 371 | uclass_foreach_dev(dev, uc) { |
e160f7d4 | 372 | if (dev_of_offset(dev) == node) { |
f4cdead2 SG |
373 | *devp = dev; |
374 | return 0; | |
375 | } | |
376 | } | |
377 | ||
378 | return -ENODEV; | |
379 | } | |
380 | ||
40bb637d SG |
381 | int uclass_find_device_by_ofnode(enum uclass_id id, ofnode node, |
382 | struct udevice **devp) | |
383 | { | |
384 | struct uclass *uc; | |
385 | struct udevice *dev; | |
386 | int ret; | |
387 | ||
31e60ffa | 388 | log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node)); |
40bb637d SG |
389 | *devp = NULL; |
390 | if (!ofnode_valid(node)) | |
391 | return -ENODEV; | |
392 | ret = uclass_get(id, &uc); | |
393 | if (ret) | |
394 | return ret; | |
395 | ||
81f351d6 | 396 | uclass_foreach_dev(dev, uc) { |
31e60ffa SG |
397 | log(LOGC_DM, LOGL_DEBUG_CONTENT, " - checking %s\n", |
398 | dev->name); | |
40bb637d SG |
399 | if (ofnode_equal(dev_ofnode(dev), node)) { |
400 | *devp = dev; | |
31e60ffa | 401 | goto done; |
40bb637d SG |
402 | } |
403 | } | |
31e60ffa | 404 | ret = -ENODEV; |
40bb637d | 405 | |
31e60ffa SG |
406 | done: |
407 | log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n", | |
408 | ofnode_get_name(node), *devp ? (*devp)->name : "(none)", ret); | |
409 | return ret; | |
40bb637d SG |
410 | } |
411 | ||
414cc151 | 412 | #if CONFIG_IS_ENABLED(OF_REAL) |
6ebb357a RV |
413 | static int uclass_find_device_by_phandle_id(enum uclass_id id, |
414 | uint find_phandle, | |
415 | struct udevice **devp) | |
d82ba4c0 SG |
416 | { |
417 | struct udevice *dev; | |
418 | struct uclass *uc; | |
d82ba4c0 SG |
419 | int ret; |
420 | ||
d82ba4c0 SG |
421 | ret = uclass_get(id, &uc); |
422 | if (ret) | |
423 | return ret; | |
424 | ||
81f351d6 | 425 | uclass_foreach_dev(dev, uc) { |
e160f7d4 SG |
426 | uint phandle; |
427 | ||
a40cc8e1 | 428 | phandle = dev_read_phandle(dev); |
d82ba4c0 SG |
429 | |
430 | if (phandle == find_phandle) { | |
431 | *devp = dev; | |
432 | return 0; | |
433 | } | |
434 | } | |
435 | ||
436 | return -ENODEV; | |
437 | } | |
6ebb357a RV |
438 | |
439 | int uclass_find_device_by_phandle(enum uclass_id id, struct udevice *parent, | |
440 | const char *name, struct udevice **devp) | |
441 | { | |
442 | int find_phandle; | |
443 | ||
444 | *devp = NULL; | |
445 | find_phandle = dev_read_u32_default(parent, name, -1); | |
446 | if (find_phandle <= 0) | |
447 | return -ENOENT; | |
448 | ||
449 | return uclass_find_device_by_phandle_id(id, find_phandle, devp); | |
450 | } | |
c275dfef | 451 | #endif |
d82ba4c0 | 452 | |
c57f806b SG |
453 | int uclass_get_device_by_driver(enum uclass_id id, |
454 | const struct driver *find_drv, | |
455 | struct udevice **devp) | |
456 | { | |
457 | struct udevice *dev; | |
458 | struct uclass *uc; | |
459 | int ret; | |
460 | ||
461 | ret = uclass_get(id, &uc); | |
462 | if (ret) | |
463 | return ret; | |
464 | ||
81f351d6 | 465 | uclass_foreach_dev(dev, uc) { |
c57f806b SG |
466 | if (dev->driver == find_drv) |
467 | return uclass_get_device_tail(dev, 0, devp); | |
468 | } | |
469 | ||
470 | return -ENODEV; | |
471 | } | |
472 | ||
30a570a9 | 473 | int uclass_get_device_tail(struct udevice *dev, int ret, struct udevice **devp) |
6494d708 | 474 | { |
6494d708 SG |
475 | if (ret) |
476 | return ret; | |
477 | ||
f66529f9 | 478 | assert(dev); |
6494d708 SG |
479 | ret = device_probe(dev); |
480 | if (ret) | |
481 | return ret; | |
482 | ||
483 | *devp = dev; | |
484 | ||
485 | return 0; | |
486 | } | |
487 | ||
9ca296a1 SG |
488 | int uclass_get_device(enum uclass_id id, int index, struct udevice **devp) |
489 | { | |
490 | struct udevice *dev; | |
491 | int ret; | |
492 | ||
493 | *devp = NULL; | |
494 | ret = uclass_find_device(id, index, &dev); | |
495 | return uclass_get_device_tail(dev, ret, devp); | |
496 | } | |
497 | ||
b7af1a2d PM |
498 | int uclass_get_device_by_name(enum uclass_id id, const char *name, |
499 | struct udevice **devp) | |
500 | { | |
501 | struct udevice *dev; | |
502 | int ret; | |
503 | ||
504 | *devp = NULL; | |
505 | ret = uclass_find_device_by_name(id, name, &dev); | |
506 | return uclass_get_device_tail(dev, ret, devp); | |
507 | } | |
508 | ||
5a66a8ff SG |
509 | int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp) |
510 | { | |
511 | struct udevice *dev; | |
512 | int ret; | |
513 | ||
514 | *devp = NULL; | |
99175919 SG |
515 | ret = uclass_find_device_by_seq(id, seq, &dev); |
516 | ||
5a66a8ff SG |
517 | return uclass_get_device_tail(dev, ret, devp); |
518 | } | |
519 | ||
f4cdead2 SG |
520 | int uclass_get_device_by_of_offset(enum uclass_id id, int node, |
521 | struct udevice **devp) | |
522 | { | |
523 | struct udevice *dev; | |
524 | int ret; | |
525 | ||
526 | *devp = NULL; | |
527 | ret = uclass_find_device_by_of_offset(id, node, &dev); | |
528 | return uclass_get_device_tail(dev, ret, devp); | |
529 | } | |
530 | ||
40bb637d SG |
531 | int uclass_get_device_by_ofnode(enum uclass_id id, ofnode node, |
532 | struct udevice **devp) | |
533 | { | |
534 | struct udevice *dev; | |
535 | int ret; | |
536 | ||
31e60ffa | 537 | log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node)); |
40bb637d SG |
538 | *devp = NULL; |
539 | ret = uclass_find_device_by_ofnode(id, node, &dev); | |
31e60ffa SG |
540 | log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n", |
541 | ofnode_get_name(node), dev ? dev->name : "(none)", ret); | |
40bb637d SG |
542 | |
543 | return uclass_get_device_tail(dev, ret, devp); | |
544 | } | |
545 | ||
6ebb357a | 546 | #if CONFIG_IS_ENABLED(OF_REAL) |
ca031c08 RV |
547 | int uclass_get_device_by_of_path(enum uclass_id id, const char *path, |
548 | struct udevice **devp) | |
549 | { | |
550 | return uclass_get_device_by_ofnode(id, ofnode_path(path), devp); | |
551 | } | |
552 | ||
d255fade KY |
553 | int uclass_get_device_by_phandle_id(enum uclass_id id, uint phandle_id, |
554 | struct udevice **devp) | |
555 | { | |
556 | struct udevice *dev; | |
d255fade KY |
557 | int ret; |
558 | ||
559 | *devp = NULL; | |
6ebb357a RV |
560 | ret = uclass_find_device_by_phandle_id(id, phandle_id, &dev); |
561 | return uclass_get_device_tail(dev, ret, devp); | |
d255fade KY |
562 | } |
563 | ||
d82ba4c0 SG |
564 | int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent, |
565 | const char *name, struct udevice **devp) | |
566 | { | |
567 | struct udevice *dev; | |
568 | int ret; | |
569 | ||
570 | *devp = NULL; | |
571 | ret = uclass_find_device_by_phandle(id, parent, name, &dev); | |
572 | return uclass_get_device_tail(dev, ret, devp); | |
573 | } | |
c275dfef | 574 | #endif |
d82ba4c0 | 575 | |
f2195475 MS |
576 | /* |
577 | * Starting from the given device @dev, return pointer to the first device in | |
578 | * the uclass that probes successfully in @devp. | |
579 | */ | |
580 | static void _uclass_next_device(struct udevice *dev, struct udevice **devp) | |
581 | { | |
582 | for (; dev; uclass_find_next_device(&dev)) { | |
583 | if (!device_probe(dev)) | |
584 | break; | |
585 | } | |
586 | *devp = dev; | |
587 | } | |
588 | ||
589 | void uclass_first_device(enum uclass_id id, struct udevice **devp) | |
6494d708 | 590 | { |
54c5d08a | 591 | struct udevice *dev; |
6494d708 SG |
592 | int ret; |
593 | ||
c1d6f919 | 594 | ret = uclass_find_first_device(id, &dev); |
f2195475 | 595 | _uclass_next_device(dev, devp); |
6494d708 SG |
596 | } |
597 | ||
f2195475 | 598 | void uclass_next_device(struct udevice **devp) |
6494d708 | 599 | { |
54c5d08a | 600 | struct udevice *dev = *devp; |
6494d708 | 601 | |
f2195475 MS |
602 | uclass_find_next_device(&dev); |
603 | _uclass_next_device(dev, devp); | |
6494d708 SG |
604 | } |
605 | ||
e44d7e73 MS |
606 | int uclass_first_device_err(enum uclass_id id, struct udevice **devp) |
607 | { | |
608 | int ret; | |
609 | ||
610 | ret = uclass_first_device_check(id, devp); | |
611 | if (ret) | |
612 | return ret; | |
613 | else if (!*devp) | |
614 | return -ENODEV; | |
615 | ||
616 | return 0; | |
617 | } | |
618 | ||
f6abd538 PC |
619 | int uclass_next_device_err(struct udevice **devp) |
620 | { | |
621 | int ret; | |
622 | ||
e44d7e73 | 623 | ret = uclass_next_device_check(devp); |
f6abd538 PC |
624 | if (ret) |
625 | return ret; | |
626 | else if (!*devp) | |
627 | return -ENODEV; | |
628 | ||
629 | return 0; | |
630 | } | |
631 | ||
95ce385a SG |
632 | int uclass_first_device_check(enum uclass_id id, struct udevice **devp) |
633 | { | |
634 | int ret; | |
635 | ||
636 | *devp = NULL; | |
637 | ret = uclass_find_first_device(id, devp); | |
638 | if (ret) | |
639 | return ret; | |
640 | if (!*devp) | |
641 | return 0; | |
642 | ||
643 | return device_probe(*devp); | |
644 | } | |
645 | ||
646 | int uclass_next_device_check(struct udevice **devp) | |
647 | { | |
648 | int ret; | |
649 | ||
650 | ret = uclass_find_next_device(devp); | |
651 | if (ret) | |
652 | return ret; | |
653 | if (!*devp) | |
654 | return 0; | |
655 | ||
656 | return device_probe(*devp); | |
657 | } | |
658 | ||
6476c4d9 SG |
659 | int uclass_get_count(void) |
660 | { | |
661 | const struct uclass *uc; | |
662 | int count = 0; | |
663 | ||
664 | if (gd->dm_root) { | |
665 | list_for_each_entry(uc, gd->uclass_root, sibling_node) | |
666 | count++; | |
667 | } | |
668 | ||
669 | return count; | |
670 | } | |
671 | ||
50162348 SG |
672 | int uclass_first_device_drvdata(enum uclass_id id, ulong driver_data, |
673 | struct udevice **devp) | |
674 | { | |
675 | struct udevice *dev; | |
676 | struct uclass *uc; | |
677 | ||
678 | uclass_id_foreach_dev(id, dev, uc) { | |
679 | if (dev_get_driver_data(dev) == driver_data) { | |
680 | *devp = dev; | |
681 | ||
682 | return device_probe(dev); | |
683 | } | |
684 | } | |
685 | ||
686 | return -ENODEV; | |
687 | } | |
688 | ||
54c5d08a | 689 | int uclass_bind_device(struct udevice *dev) |
6494d708 SG |
690 | { |
691 | struct uclass *uc; | |
692 | int ret; | |
693 | ||
694 | uc = dev->uclass; | |
6494d708 SG |
695 | list_add_tail(&dev->uclass_node, &uc->dev_head); |
696 | ||
081f2fcb SG |
697 | if (dev->parent) { |
698 | struct uclass_driver *uc_drv = dev->parent->uclass->uc_drv; | |
699 | ||
700 | if (uc_drv->child_post_bind) { | |
701 | ret = uc_drv->child_post_bind(dev); | |
702 | if (ret) | |
703 | goto err; | |
704 | } | |
705 | } | |
6494d708 SG |
706 | |
707 | return 0; | |
081f2fcb SG |
708 | err: |
709 | /* There is no need to undo the parent's post_bind call */ | |
710 | list_del(&dev->uclass_node); | |
711 | ||
712 | return ret; | |
6494d708 SG |
713 | } |
714 | ||
0a5804b5 | 715 | #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE) |
80443183 | 716 | int uclass_pre_unbind_device(struct udevice *dev) |
6494d708 SG |
717 | { |
718 | struct uclass *uc; | |
719 | int ret; | |
720 | ||
721 | uc = dev->uclass; | |
722 | if (uc->uc_drv->pre_unbind) { | |
723 | ret = uc->uc_drv->pre_unbind(dev); | |
724 | if (ret) | |
725 | return ret; | |
726 | } | |
727 | ||
80443183 SG |
728 | return 0; |
729 | } | |
730 | ||
731 | int uclass_unbind_device(struct udevice *dev) | |
732 | { | |
6494d708 | 733 | list_del(&dev->uclass_node); |
80443183 | 734 | |
6494d708 SG |
735 | return 0; |
736 | } | |
7f9875e7 | 737 | #endif |
6494d708 | 738 | |
02c07b37 | 739 | int uclass_pre_probe_device(struct udevice *dev) |
83c7e434 SG |
740 | { |
741 | struct uclass_driver *uc_drv; | |
02c07b37 SG |
742 | int ret; |
743 | ||
744 | uc_drv = dev->uclass->uc_drv; | |
745 | if (uc_drv->pre_probe) { | |
746 | ret = uc_drv->pre_probe(dev); | |
747 | if (ret) | |
748 | return ret; | |
749 | } | |
83c7e434 SG |
750 | |
751 | if (!dev->parent) | |
752 | return 0; | |
753 | uc_drv = dev->parent->uclass->uc_drv; | |
c8b31cce SG |
754 | if (uc_drv->child_pre_probe) { |
755 | ret = uc_drv->child_pre_probe(dev); | |
756 | if (ret) | |
757 | return ret; | |
758 | } | |
83c7e434 SG |
759 | |
760 | return 0; | |
761 | } | |
762 | ||
54c5d08a | 763 | int uclass_post_probe_device(struct udevice *dev) |
6494d708 | 764 | { |
651d0c01 BM |
765 | struct uclass_driver *uc_drv; |
766 | int ret; | |
767 | ||
768 | if (dev->parent) { | |
769 | uc_drv = dev->parent->uclass->uc_drv; | |
770 | if (uc_drv->child_post_probe) { | |
771 | ret = uc_drv->child_post_probe(dev); | |
772 | if (ret) | |
773 | return ret; | |
774 | } | |
775 | } | |
6494d708 | 776 | |
651d0c01 | 777 | uc_drv = dev->uclass->uc_drv; |
c8b31cce SG |
778 | if (uc_drv->post_probe) { |
779 | ret = uc_drv->post_probe(dev); | |
780 | if (ret) | |
781 | return ret; | |
782 | } | |
6494d708 SG |
783 | |
784 | return 0; | |
785 | } | |
786 | ||
0a5804b5 | 787 | #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE) |
54c5d08a | 788 | int uclass_pre_remove_device(struct udevice *dev) |
6494d708 | 789 | { |
6494d708 SG |
790 | struct uclass *uc; |
791 | int ret; | |
792 | ||
793 | uc = dev->uclass; | |
6494d708 SG |
794 | if (uc->uc_drv->pre_remove) { |
795 | ret = uc->uc_drv->pre_remove(dev); | |
796 | if (ret) | |
797 | return ret; | |
798 | } | |
6494d708 SG |
799 | |
800 | return 0; | |
801 | } | |
7f9875e7 | 802 | #endif |
07e33711 | 803 | |
a59153df VS |
804 | int uclass_probe_all(enum uclass_id id) |
805 | { | |
806 | struct udevice *dev; | |
dfecd631 | 807 | int ret, err; |
a59153df | 808 | |
dfecd631 | 809 | err = uclass_first_device_check(id, &dev); |
a59153df VS |
810 | |
811 | /* Scanning uclass to probe all devices */ | |
812 | while (dev) { | |
dfecd631 | 813 | ret = uclass_next_device_check(&dev); |
a59153df | 814 | if (ret) |
dfecd631 | 815 | err = ret; |
a59153df VS |
816 | } |
817 | ||
dfecd631 | 818 | return err; |
a59153df VS |
819 | } |
820 | ||
29fe555d SG |
821 | int uclass_id_count(enum uclass_id id) |
822 | { | |
823 | struct udevice *dev; | |
824 | struct uclass *uc; | |
825 | int count = 0; | |
826 | ||
827 | uclass_id_foreach_dev(id, dev, uc) | |
828 | count++; | |
829 | ||
830 | return count; | |
831 | } | |
832 | ||
07e33711 JJH |
833 | UCLASS_DRIVER(nop) = { |
834 | .id = UCLASS_NOP, | |
835 | .name = "nop", | |
836 | }; |