blob: 684930293646c0de297114fac52e4d4acf902047 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassdd6ab882014-02-26 15:59:18 -07002/*
3 * Copyright (c) 2013 Google, Inc
4 *
5 * (C) Copyright 2012
6 * Pavel Herrmann <morpheus.ibis@gmail.com>
Simon Glassdd6ab882014-02-26 15:59:18 -07007 */
8
Kever Yang5f09a2d2019-10-22 15:39:47 +08009#define LOG_CATEGORY LOGC_DM
10
Simon Glassdd6ab882014-02-26 15:59:18 -070011#include <common.h>
Simon Glassee145d62017-05-18 20:09:09 -060012#include <dm.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070013#include <errno.h>
14#include <malloc.h>
15#include <dm/device.h>
16#include <dm/device-internal.h>
17#include <dm/lists.h>
18#include <dm/uclass.h>
19#include <dm/uclass-internal.h>
20#include <dm/util.h>
21
22DECLARE_GLOBAL_DATA_PTR;
23
24struct uclass *uclass_find(enum uclass_id key)
25{
26 struct uclass *uc;
27
Simon Glassde708672014-07-23 06:55:15 -060028 if (!gd->dm_root)
29 return NULL;
Simon Glassdd6ab882014-02-26 15:59:18 -070030 /*
31 * TODO(sjg@chromium.org): Optimise this, perhaps moving the found
32 * node to the start of the list, or creating a linear array mapping
33 * id to node.
34 */
35 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
36 if (uc->uc_drv->id == key)
37 return uc;
38 }
39
40 return NULL;
41}
42
43/**
44 * uclass_add() - Create new uclass in list
45 * @id: Id number to create
46 * @ucp: Returns pointer to uclass, or NULL on error
47 * @return 0 on success, -ve on error
48 *
49 * The new uclass is added to the list. There must be only one uclass for
50 * each id.
51 */
52static int uclass_add(enum uclass_id id, struct uclass **ucp)
53{
54 struct uclass_driver *uc_drv;
55 struct uclass *uc;
56 int ret;
57
58 *ucp = NULL;
59 uc_drv = lists_uclass_lookup(id);
60 if (!uc_drv) {
Masahiro Yamada1d06ced2015-07-07 18:51:32 +090061 debug("Cannot find uclass for id %d: please add the UCLASS_DRIVER() declaration for this UCLASS_... id\n",
62 id);
Simon Glass43313de2015-08-30 16:55:16 -060063 /*
64 * Use a strange error to make this case easier to find. When
65 * a uclass is not available it can prevent driver model from
66 * starting up and this failure is otherwise hard to debug.
67 */
68 return -EPFNOSUPPORT;
Simon Glassdd6ab882014-02-26 15:59:18 -070069 }
Simon Glassdd6ab882014-02-26 15:59:18 -070070 uc = calloc(1, sizeof(*uc));
71 if (!uc)
72 return -ENOMEM;
73 if (uc_drv->priv_auto_alloc_size) {
74 uc->priv = calloc(1, uc_drv->priv_auto_alloc_size);
75 if (!uc->priv) {
76 ret = -ENOMEM;
77 goto fail_mem;
78 }
79 }
80 uc->uc_drv = uc_drv;
81 INIT_LIST_HEAD(&uc->sibling_node);
82 INIT_LIST_HEAD(&uc->dev_head);
Simon Glass34a1d352014-06-11 23:29:49 -060083 list_add(&uc->sibling_node, &DM_UCLASS_ROOT_NON_CONST);
Simon Glassdd6ab882014-02-26 15:59:18 -070084
85 if (uc_drv->init) {
86 ret = uc_drv->init(uc);
87 if (ret)
88 goto fail;
89 }
90
91 *ucp = uc;
92
93 return 0;
94fail:
95 if (uc_drv->priv_auto_alloc_size) {
96 free(uc->priv);
97 uc->priv = NULL;
98 }
99 list_del(&uc->sibling_node);
100fail_mem:
101 free(uc);
102
103 return ret;
104}
105
106int uclass_destroy(struct uclass *uc)
107{
108 struct uclass_driver *uc_drv;
Simon Glassf33663d2015-04-19 07:20:58 -0600109 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700110 int ret;
111
Simon Glassf33663d2015-04-19 07:20:58 -0600112 /*
113 * We cannot use list_for_each_entry_safe() here. If a device in this
114 * uclass has a child device also in this uclass, it will be also be
115 * unbound (by the recursion in the call to device_unbind() below).
116 * We can loop until the list is empty.
117 */
118 while (!list_empty(&uc->dev_head)) {
119 dev = list_first_entry(&uc->dev_head, struct udevice,
120 uclass_node);
Simon Glass42697f52020-03-28 14:03:48 -0600121 ret = device_remove(dev, DM_REMOVE_NORMAL | DM_REMOVE_NO_PD);
Simon Glassdd6ab882014-02-26 15:59:18 -0700122 if (ret)
Simon Glass5f3c9e02020-03-28 14:03:47 -0600123 return log_msg_ret("remove", ret);
Simon Glassdd6ab882014-02-26 15:59:18 -0700124 ret = device_unbind(dev);
125 if (ret)
Simon Glass5f3c9e02020-03-28 14:03:47 -0600126 return log_msg_ret("unbind", ret);
Simon Glassdd6ab882014-02-26 15:59:18 -0700127 }
128
129 uc_drv = uc->uc_drv;
130 if (uc_drv->destroy)
131 uc_drv->destroy(uc);
132 list_del(&uc->sibling_node);
133 if (uc_drv->priv_auto_alloc_size)
134 free(uc->priv);
135 free(uc);
136
137 return 0;
138}
139
140int uclass_get(enum uclass_id id, struct uclass **ucp)
141{
142 struct uclass *uc;
143
144 *ucp = NULL;
145 uc = uclass_find(id);
146 if (!uc)
147 return uclass_add(id, ucp);
148 *ucp = uc;
149
150 return 0;
151}
152
Simon Glassd19d0732016-10-05 20:42:13 -0600153const char *uclass_get_name(enum uclass_id id)
154{
155 struct uclass *uc;
156
157 if (uclass_get(id, &uc))
158 return NULL;
159 return uc->uc_drv->name;
160}
161
Simon Glass70e35b42017-12-28 13:14:15 -0700162enum uclass_id uclass_get_by_name(const char *name)
163{
164 int i;
165
166 for (i = 0; i < UCLASS_COUNT; i++) {
167 struct uclass_driver *uc_drv = lists_uclass_lookup(i);
168
169 if (uc_drv && !strcmp(uc_drv->name, name))
170 return i;
171 }
172
173 return UCLASS_INVALID;
174}
175
Jean-Jacques Hiblotc4f02482018-08-09 16:17:42 +0200176int dev_get_uclass_index(struct udevice *dev, struct uclass **ucp)
177{
178 struct udevice *iter;
179 struct uclass *uc = dev->uclass;
180 int i = 0;
181
182 if (list_empty(&uc->dev_head))
183 return -ENODEV;
184
Liviu Dudau6f386222018-09-28 14:12:55 +0100185 uclass_foreach_dev(iter, uc) {
Jean-Jacques Hiblotc4f02482018-08-09 16:17:42 +0200186 if (iter == dev) {
187 if (ucp)
188 *ucp = uc;
189 return i;
190 }
191 i++;
192 }
193
194 return -ENODEV;
195}
196
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200197int uclass_find_device(enum uclass_id id, int index, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700198{
199 struct uclass *uc;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200200 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700201 int ret;
202
203 *devp = NULL;
204 ret = uclass_get(id, &uc);
205 if (ret)
206 return ret;
Simon Glass46766df2015-07-31 09:31:19 -0600207 if (list_empty(&uc->dev_head))
208 return -ENODEV;
Simon Glassdd6ab882014-02-26 15:59:18 -0700209
Liviu Dudau6f386222018-09-28 14:12:55 +0100210 uclass_foreach_dev(dev, uc) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700211 if (!index--) {
212 *devp = dev;
213 return 0;
214 }
215 }
216
217 return -ENODEV;
218}
219
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200220int uclass_find_first_device(enum uclass_id id, struct udevice **devp)
221{
222 struct uclass *uc;
223 int ret;
224
225 *devp = NULL;
226 ret = uclass_get(id, &uc);
227 if (ret)
228 return ret;
229 if (list_empty(&uc->dev_head))
Simon Glass0bb44272019-09-25 08:55:55 -0600230 return 0;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200231
232 *devp = list_first_entry(&uc->dev_head, struct udevice, uclass_node);
233
234 return 0;
235}
236
237int uclass_find_next_device(struct udevice **devp)
238{
239 struct udevice *dev = *devp;
240
241 *devp = NULL;
242 if (list_is_last(&dev->uclass_node, &dev->uclass->dev_head))
243 return 0;
244
245 *devp = list_entry(dev->uclass_node.next, struct udevice, uclass_node);
246
247 return 0;
248}
249
Przemyslaw Marczak2ffdf142015-04-15 13:07:22 +0200250int uclass_find_device_by_name(enum uclass_id id, const char *name,
251 struct udevice **devp)
252{
253 struct uclass *uc;
254 struct udevice *dev;
255 int ret;
256
257 *devp = NULL;
258 if (!name)
259 return -EINVAL;
260 ret = uclass_get(id, &uc);
261 if (ret)
262 return ret;
263
Liviu Dudau6f386222018-09-28 14:12:55 +0100264 uclass_foreach_dev(dev, uc) {
Peng Fan26d68b22019-05-22 07:08:14 +0000265 if (!strcmp(dev->name, name)) {
Przemyslaw Marczak2ffdf142015-04-15 13:07:22 +0200266 *devp = dev;
267 return 0;
268 }
269 }
270
271 return -ENODEV;
272}
273
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100274int uclass_find_next_free_req_seq(enum uclass_id id)
275{
276 struct uclass *uc;
277 struct udevice *dev;
278 int ret;
279 int max = -1;
280
281 ret = uclass_get(id, &uc);
282 if (ret)
283 return ret;
284
285 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
286 if ((dev->req_seq != -1) && (dev->req_seq > max))
287 max = dev->req_seq;
288 }
289
290 if (max == -1)
291 return 0;
292
293 return max + 1;
294}
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100295
Simon Glassdb6f0202014-07-23 06:55:12 -0600296int uclass_find_device_by_seq(enum uclass_id id, int seq_or_req_seq,
297 bool find_req_seq, struct udevice **devp)
298{
299 struct uclass *uc;
300 struct udevice *dev;
301 int ret;
302
303 *devp = NULL;
Kever Yang5f09a2d2019-10-22 15:39:47 +0800304 log_debug("%d %d\n", find_req_seq, seq_or_req_seq);
Simon Glassdb6f0202014-07-23 06:55:12 -0600305 if (seq_or_req_seq == -1)
306 return -ENODEV;
307 ret = uclass_get(id, &uc);
308 if (ret)
309 return ret;
310
Liviu Dudau6f386222018-09-28 14:12:55 +0100311 uclass_foreach_dev(dev, uc) {
Kever Yang5f09a2d2019-10-22 15:39:47 +0800312 log_debug(" - %d %d '%s'\n",
313 dev->req_seq, dev->seq, dev->name);
Simon Glassdb6f0202014-07-23 06:55:12 -0600314 if ((find_req_seq ? dev->req_seq : dev->seq) ==
315 seq_or_req_seq) {
316 *devp = dev;
Kever Yang5f09a2d2019-10-22 15:39:47 +0800317 log_debug(" - found\n");
Simon Glassdb6f0202014-07-23 06:55:12 -0600318 return 0;
319 }
320 }
Kever Yang5f09a2d2019-10-22 15:39:47 +0800321 log_debug(" - not found\n");
Simon Glassdb6f0202014-07-23 06:55:12 -0600322
323 return -ENODEV;
324}
325
Simon Glass96f04442016-01-21 19:43:57 -0700326int uclass_find_device_by_of_offset(enum uclass_id id, int node,
327 struct udevice **devp)
Simon Glassc1464ab2014-07-23 06:55:14 -0600328{
329 struct uclass *uc;
330 struct udevice *dev;
331 int ret;
332
333 *devp = NULL;
334 if (node < 0)
335 return -ENODEV;
336 ret = uclass_get(id, &uc);
337 if (ret)
338 return ret;
339
Liviu Dudau6f386222018-09-28 14:12:55 +0100340 uclass_foreach_dev(dev, uc) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700341 if (dev_of_offset(dev) == node) {
Simon Glassc1464ab2014-07-23 06:55:14 -0600342 *devp = dev;
343 return 0;
344 }
345 }
346
347 return -ENODEV;
348}
349
Simon Glassee145d62017-05-18 20:09:09 -0600350int uclass_find_device_by_ofnode(enum uclass_id id, ofnode node,
351 struct udevice **devp)
352{
353 struct uclass *uc;
354 struct udevice *dev;
355 int ret;
356
Simon Glass5ab533c2018-06-11 13:07:19 -0600357 log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node));
Simon Glassee145d62017-05-18 20:09:09 -0600358 *devp = NULL;
359 if (!ofnode_valid(node))
360 return -ENODEV;
361 ret = uclass_get(id, &uc);
362 if (ret)
363 return ret;
364
Liviu Dudau6f386222018-09-28 14:12:55 +0100365 uclass_foreach_dev(dev, uc) {
Simon Glass5ab533c2018-06-11 13:07:19 -0600366 log(LOGC_DM, LOGL_DEBUG_CONTENT, " - checking %s\n",
367 dev->name);
Simon Glassee145d62017-05-18 20:09:09 -0600368 if (ofnode_equal(dev_ofnode(dev), node)) {
369 *devp = dev;
Simon Glass5ab533c2018-06-11 13:07:19 -0600370 goto done;
Simon Glassee145d62017-05-18 20:09:09 -0600371 }
372 }
Simon Glass5ab533c2018-06-11 13:07:19 -0600373 ret = -ENODEV;
Simon Glassee145d62017-05-18 20:09:09 -0600374
Simon Glass5ab533c2018-06-11 13:07:19 -0600375done:
376 log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n",
377 ofnode_get_name(node), *devp ? (*devp)->name : "(none)", ret);
378 return ret;
Simon Glassee145d62017-05-18 20:09:09 -0600379}
380
Simon Glass359c4872015-12-19 19:38:55 -0700381#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glassdc3efdc2018-11-18 08:14:30 -0700382int uclass_find_device_by_phandle(enum uclass_id id, struct udevice *parent,
383 const char *name, struct udevice **devp)
Simon Glass75f00df2015-07-02 18:15:38 -0600384{
385 struct udevice *dev;
386 struct uclass *uc;
387 int find_phandle;
388 int ret;
389
390 *devp = NULL;
Simon Glassbfb6e3d2017-05-18 20:09:12 -0600391 find_phandle = dev_read_u32_default(parent, name, -1);
Simon Glass75f00df2015-07-02 18:15:38 -0600392 if (find_phandle <= 0)
393 return -ENOENT;
394 ret = uclass_get(id, &uc);
395 if (ret)
396 return ret;
397
Liviu Dudau6f386222018-09-28 14:12:55 +0100398 uclass_foreach_dev(dev, uc) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700399 uint phandle;
400
Simon Glassbfb6e3d2017-05-18 20:09:12 -0600401 phandle = dev_read_phandle(dev);
Simon Glass75f00df2015-07-02 18:15:38 -0600402
403 if (phandle == find_phandle) {
404 *devp = dev;
405 return 0;
406 }
407 }
408
409 return -ENODEV;
410}
Simon Glass359c4872015-12-19 19:38:55 -0700411#endif
Simon Glass75f00df2015-07-02 18:15:38 -0600412
Simon Glass32d8ab62016-07-17 15:23:15 -0600413int uclass_get_device_by_driver(enum uclass_id id,
414 const struct driver *find_drv,
415 struct udevice **devp)
416{
417 struct udevice *dev;
418 struct uclass *uc;
419 int ret;
420
421 ret = uclass_get(id, &uc);
422 if (ret)
423 return ret;
424
Liviu Dudau6f386222018-09-28 14:12:55 +0100425 uclass_foreach_dev(dev, uc) {
Simon Glass32d8ab62016-07-17 15:23:15 -0600426 if (dev->driver == find_drv)
427 return uclass_get_device_tail(dev, 0, devp);
428 }
429
430 return -ENODEV;
431}
432
Simon Glass98c9fb02017-04-23 20:10:43 -0600433int uclass_get_device_tail(struct udevice *dev, int ret, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700434{
Simon Glassdd6ab882014-02-26 15:59:18 -0700435 if (ret)
436 return ret;
437
Simon Glass46cc8ba92015-04-24 22:33:07 -0600438 assert(dev);
Simon Glassdd6ab882014-02-26 15:59:18 -0700439 ret = device_probe(dev);
440 if (ret)
441 return ret;
442
443 *devp = dev;
444
445 return 0;
446}
447
Simon Glass4b67cef2014-07-23 06:55:08 -0600448int uclass_get_device(enum uclass_id id, int index, struct udevice **devp)
449{
450 struct udevice *dev;
451 int ret;
452
453 *devp = NULL;
454 ret = uclass_find_device(id, index, &dev);
455 return uclass_get_device_tail(dev, ret, devp);
456}
457
Przemyslaw Marczaka12a1f52015-04-15 13:07:23 +0200458int uclass_get_device_by_name(enum uclass_id id, const char *name,
459 struct udevice **devp)
460{
461 struct udevice *dev;
462 int ret;
463
464 *devp = NULL;
465 ret = uclass_find_device_by_name(id, name, &dev);
466 return uclass_get_device_tail(dev, ret, devp);
467}
468
Simon Glassdb6f0202014-07-23 06:55:12 -0600469int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
470{
471 struct udevice *dev;
472 int ret;
473
474 *devp = NULL;
475 ret = uclass_find_device_by_seq(id, seq, false, &dev);
476 if (ret == -ENODEV) {
477 /*
478 * We didn't find it in probed devices. See if there is one
479 * that will request this seq if probed.
480 */
481 ret = uclass_find_device_by_seq(id, seq, true, &dev);
482 }
483 return uclass_get_device_tail(dev, ret, devp);
484}
485
Simon Glassc1464ab2014-07-23 06:55:14 -0600486int uclass_get_device_by_of_offset(enum uclass_id id, int node,
487 struct udevice **devp)
488{
489 struct udevice *dev;
490 int ret;
491
492 *devp = NULL;
493 ret = uclass_find_device_by_of_offset(id, node, &dev);
494 return uclass_get_device_tail(dev, ret, devp);
495}
496
Simon Glassee145d62017-05-18 20:09:09 -0600497int uclass_get_device_by_ofnode(enum uclass_id id, ofnode node,
498 struct udevice **devp)
499{
500 struct udevice *dev;
501 int ret;
502
Simon Glass5ab533c2018-06-11 13:07:19 -0600503 log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node));
Simon Glassee145d62017-05-18 20:09:09 -0600504 *devp = NULL;
505 ret = uclass_find_device_by_ofnode(id, node, &dev);
Simon Glass5ab533c2018-06-11 13:07:19 -0600506 log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n",
507 ofnode_get_name(node), dev ? dev->name : "(none)", ret);
Simon Glassee145d62017-05-18 20:09:09 -0600508
509 return uclass_get_device_tail(dev, ret, devp);
510}
511
Simon Glass359c4872015-12-19 19:38:55 -0700512#if CONFIG_IS_ENABLED(OF_CONTROL)
Kever Yang66a0b5a2018-02-09 10:56:23 +0800513int uclass_get_device_by_phandle_id(enum uclass_id id, uint phandle_id,
514 struct udevice **devp)
515{
516 struct udevice *dev;
517 struct uclass *uc;
518 int ret;
519
520 *devp = NULL;
521 ret = uclass_get(id, &uc);
522 if (ret)
523 return ret;
524
Liviu Dudau6f386222018-09-28 14:12:55 +0100525 uclass_foreach_dev(dev, uc) {
Kever Yang66a0b5a2018-02-09 10:56:23 +0800526 uint phandle;
527
528 phandle = dev_read_phandle(dev);
529
530 if (phandle == phandle_id) {
531 *devp = dev;
532 return uclass_get_device_tail(dev, ret, devp);
533 }
534 }
535
536 return -ENODEV;
537}
538
Simon Glass75f00df2015-07-02 18:15:38 -0600539int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent,
540 const char *name, struct udevice **devp)
541{
542 struct udevice *dev;
543 int ret;
544
545 *devp = NULL;
546 ret = uclass_find_device_by_phandle(id, parent, name, &dev);
547 return uclass_get_device_tail(dev, ret, devp);
548}
Simon Glass359c4872015-12-19 19:38:55 -0700549#endif
Simon Glass75f00df2015-07-02 18:15:38 -0600550
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200551int uclass_first_device(enum uclass_id id, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700552{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200553 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700554 int ret;
555
556 *devp = NULL;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200557 ret = uclass_find_first_device(id, &dev);
Simon Glass46cc8ba92015-04-24 22:33:07 -0600558 if (!dev)
559 return 0;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200560 return uclass_get_device_tail(dev, ret, devp);
Simon Glassdd6ab882014-02-26 15:59:18 -0700561}
562
Simon Glass832c3f02016-02-11 13:23:25 -0700563int uclass_first_device_err(enum uclass_id id, struct udevice **devp)
564{
565 int ret;
566
567 ret = uclass_first_device(id, devp);
568 if (ret)
569 return ret;
570 else if (!*devp)
571 return -ENODEV;
572
573 return 0;
574}
575
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200576int uclass_next_device(struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700577{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200578 struct udevice *dev = *devp;
Simon Glassdd6ab882014-02-26 15:59:18 -0700579 int ret;
580
581 *devp = NULL;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200582 ret = uclass_find_next_device(&dev);
Simon Glass46cc8ba92015-04-24 22:33:07 -0600583 if (!dev)
584 return 0;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200585 return uclass_get_device_tail(dev, ret, devp);
Simon Glassdd6ab882014-02-26 15:59:18 -0700586}
587
Patrice Chotard4dff1782018-10-24 14:10:15 +0200588int uclass_next_device_err(struct udevice **devp)
589{
590 int ret;
591
592 ret = uclass_next_device(devp);
593 if (ret)
594 return ret;
595 else if (!*devp)
596 return -ENODEV;
597
598 return 0;
599}
600
Simon Glass3ff83bc2017-04-23 20:10:45 -0600601int uclass_first_device_check(enum uclass_id id, struct udevice **devp)
602{
603 int ret;
604
605 *devp = NULL;
606 ret = uclass_find_first_device(id, devp);
607 if (ret)
608 return ret;
609 if (!*devp)
610 return 0;
611
612 return device_probe(*devp);
613}
614
615int uclass_next_device_check(struct udevice **devp)
616{
617 int ret;
618
619 ret = uclass_find_next_device(devp);
620 if (ret)
621 return ret;
622 if (!*devp)
623 return 0;
624
625 return device_probe(*devp);
626}
627
Simon Glass35cb2a42020-02-06 09:54:50 -0700628int uclass_first_device_drvdata(enum uclass_id id, ulong driver_data,
629 struct udevice **devp)
630{
631 struct udevice *dev;
632 struct uclass *uc;
633
634 uclass_id_foreach_dev(id, dev, uc) {
635 if (dev_get_driver_data(dev) == driver_data) {
636 *devp = dev;
637
638 return device_probe(dev);
639 }
640 }
641
642 return -ENODEV;
643}
644
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200645int uclass_bind_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700646{
647 struct uclass *uc;
648 int ret;
649
650 uc = dev->uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700651 list_add_tail(&dev->uclass_node, &uc->dev_head);
652
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700653 if (dev->parent) {
654 struct uclass_driver *uc_drv = dev->parent->uclass->uc_drv;
655
656 if (uc_drv->child_post_bind) {
657 ret = uc_drv->child_post_bind(dev);
658 if (ret)
659 goto err;
660 }
661 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700662
663 return 0;
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700664err:
665 /* There is no need to undo the parent's post_bind call */
666 list_del(&dev->uclass_node);
667
668 return ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700669}
670
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900671#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200672int uclass_unbind_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700673{
674 struct uclass *uc;
675 int ret;
676
677 uc = dev->uclass;
678 if (uc->uc_drv->pre_unbind) {
679 ret = uc->uc_drv->pre_unbind(dev);
680 if (ret)
681 return ret;
682 }
683
684 list_del(&dev->uclass_node);
685 return 0;
686}
Simon Glass8914c8a2015-02-27 22:06:31 -0700687#endif
Simon Glassdd6ab882014-02-26 15:59:18 -0700688
Simon Glassdb6f0202014-07-23 06:55:12 -0600689int uclass_resolve_seq(struct udevice *dev)
690{
691 struct udevice *dup;
692 int seq;
693 int ret;
694
695 assert(dev->seq == -1);
696 ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, dev->req_seq,
697 false, &dup);
698 if (!ret) {
699 dm_warn("Device '%s': seq %d is in use by '%s'\n",
700 dev->name, dev->req_seq, dup->name);
701 } else if (ret == -ENODEV) {
702 /* Our requested sequence number is available */
703 if (dev->req_seq != -1)
704 return dev->req_seq;
705 } else {
706 return ret;
707 }
708
709 for (seq = 0; seq < DM_MAX_SEQ; seq++) {
710 ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, seq,
711 false, &dup);
712 if (ret == -ENODEV)
713 break;
714 if (ret)
715 return ret;
716 }
717 return seq;
718}
719
Simon Glass9c1f3822015-03-05 12:25:22 -0700720int uclass_pre_probe_device(struct udevice *dev)
Simon Glass5104b982015-01-25 08:27:10 -0700721{
722 struct uclass_driver *uc_drv;
Simon Glass9c1f3822015-03-05 12:25:22 -0700723 int ret;
724
725 uc_drv = dev->uclass->uc_drv;
726 if (uc_drv->pre_probe) {
727 ret = uc_drv->pre_probe(dev);
728 if (ret)
729 return ret;
730 }
Simon Glass5104b982015-01-25 08:27:10 -0700731
732 if (!dev->parent)
733 return 0;
734 uc_drv = dev->parent->uclass->uc_drv;
Simon Glass256d4022019-09-25 08:55:47 -0600735 if (uc_drv->child_pre_probe) {
736 ret = uc_drv->child_pre_probe(dev);
737 if (ret)
738 return ret;
739 }
Simon Glass5104b982015-01-25 08:27:10 -0700740
741 return 0;
742}
743
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200744int uclass_post_probe_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700745{
Bin Meng30569cf2018-10-15 02:20:57 -0700746 struct uclass_driver *uc_drv;
747 int ret;
748
749 if (dev->parent) {
750 uc_drv = dev->parent->uclass->uc_drv;
751 if (uc_drv->child_post_probe) {
752 ret = uc_drv->child_post_probe(dev);
753 if (ret)
754 return ret;
755 }
756 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700757
Bin Meng30569cf2018-10-15 02:20:57 -0700758 uc_drv = dev->uclass->uc_drv;
Simon Glass256d4022019-09-25 08:55:47 -0600759 if (uc_drv->post_probe) {
760 ret = uc_drv->post_probe(dev);
761 if (ret)
762 return ret;
763 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700764
765 return 0;
766}
767
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900768#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200769int uclass_pre_remove_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700770{
Simon Glassdd6ab882014-02-26 15:59:18 -0700771 struct uclass *uc;
772 int ret;
773
774 uc = dev->uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700775 if (uc->uc_drv->pre_remove) {
776 ret = uc->uc_drv->pre_remove(dev);
777 if (ret)
778 return ret;
779 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700780
781 return 0;
782}
Simon Glass8914c8a2015-02-27 22:06:31 -0700783#endif
Jean-Jacques Hiblotdb97c7f2019-07-05 09:33:57 +0200784
785UCLASS_DRIVER(nop) = {
786 .id = UCLASS_NOP,
787 .name = "nop",
788};