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