blob: c520ef113a2a3fbc8f155e19a763d579e601dbd5 [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);
Stefan Roese80b5bc92017-03-20 12:51:48 +0100121 ret = device_remove(dev, DM_REMOVE_NORMAL);
Simon Glassdd6ab882014-02-26 15:59:18 -0700122 if (ret)
123 return ret;
124 ret = device_unbind(dev);
125 if (ret)
126 return ret;
127 }
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
Thomas Fitzsimmonsf327ecb2019-09-06 07:51:18 -0400274#if !CONFIG_IS_ENABLED(OF_CONTROL) || \
275 CONFIG_IS_ENABLED(OF_PLATDATA) || \
276 CONFIG_IS_ENABLED(OF_PRIOR_STAGE)
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100277int uclass_find_next_free_req_seq(enum uclass_id id)
278{
279 struct uclass *uc;
280 struct udevice *dev;
281 int ret;
282 int max = -1;
283
284 ret = uclass_get(id, &uc);
285 if (ret)
286 return ret;
287
288 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
289 if ((dev->req_seq != -1) && (dev->req_seq > max))
290 max = dev->req_seq;
291 }
292
293 if (max == -1)
294 return 0;
295
296 return max + 1;
297}
298#endif
299
Simon Glassdb6f0202014-07-23 06:55:12 -0600300int uclass_find_device_by_seq(enum uclass_id id, int seq_or_req_seq,
301 bool find_req_seq, struct udevice **devp)
302{
303 struct uclass *uc;
304 struct udevice *dev;
305 int ret;
306
307 *devp = NULL;
Kever Yang5f09a2d2019-10-22 15:39:47 +0800308 log_debug("%d %d\n", find_req_seq, seq_or_req_seq);
Simon Glassdb6f0202014-07-23 06:55:12 -0600309 if (seq_or_req_seq == -1)
310 return -ENODEV;
311 ret = uclass_get(id, &uc);
312 if (ret)
313 return ret;
314
Liviu Dudau6f386222018-09-28 14:12:55 +0100315 uclass_foreach_dev(dev, uc) {
Kever Yang5f09a2d2019-10-22 15:39:47 +0800316 log_debug(" - %d %d '%s'\n",
317 dev->req_seq, dev->seq, dev->name);
Simon Glassdb6f0202014-07-23 06:55:12 -0600318 if ((find_req_seq ? dev->req_seq : dev->seq) ==
319 seq_or_req_seq) {
320 *devp = dev;
Kever Yang5f09a2d2019-10-22 15:39:47 +0800321 log_debug(" - found\n");
Simon Glassdb6f0202014-07-23 06:55:12 -0600322 return 0;
323 }
324 }
Kever Yang5f09a2d2019-10-22 15:39:47 +0800325 log_debug(" - not found\n");
Simon Glassdb6f0202014-07-23 06:55:12 -0600326
327 return -ENODEV;
328}
329
Simon Glass96f04442016-01-21 19:43:57 -0700330int uclass_find_device_by_of_offset(enum uclass_id id, int node,
331 struct udevice **devp)
Simon Glassc1464ab2014-07-23 06:55:14 -0600332{
333 struct uclass *uc;
334 struct udevice *dev;
335 int ret;
336
337 *devp = NULL;
338 if (node < 0)
339 return -ENODEV;
340 ret = uclass_get(id, &uc);
341 if (ret)
342 return ret;
343
Liviu Dudau6f386222018-09-28 14:12:55 +0100344 uclass_foreach_dev(dev, uc) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700345 if (dev_of_offset(dev) == node) {
Simon Glassc1464ab2014-07-23 06:55:14 -0600346 *devp = dev;
347 return 0;
348 }
349 }
350
351 return -ENODEV;
352}
353
Simon Glassee145d62017-05-18 20:09:09 -0600354int uclass_find_device_by_ofnode(enum uclass_id id, ofnode node,
355 struct udevice **devp)
356{
357 struct uclass *uc;
358 struct udevice *dev;
359 int ret;
360
Simon Glass5ab533c2018-06-11 13:07:19 -0600361 log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node));
Simon Glassee145d62017-05-18 20:09:09 -0600362 *devp = NULL;
363 if (!ofnode_valid(node))
364 return -ENODEV;
365 ret = uclass_get(id, &uc);
366 if (ret)
367 return ret;
368
Liviu Dudau6f386222018-09-28 14:12:55 +0100369 uclass_foreach_dev(dev, uc) {
Simon Glass5ab533c2018-06-11 13:07:19 -0600370 log(LOGC_DM, LOGL_DEBUG_CONTENT, " - checking %s\n",
371 dev->name);
Simon Glassee145d62017-05-18 20:09:09 -0600372 if (ofnode_equal(dev_ofnode(dev), node)) {
373 *devp = dev;
Simon Glass5ab533c2018-06-11 13:07:19 -0600374 goto done;
Simon Glassee145d62017-05-18 20:09:09 -0600375 }
376 }
Simon Glass5ab533c2018-06-11 13:07:19 -0600377 ret = -ENODEV;
Simon Glassee145d62017-05-18 20:09:09 -0600378
Simon Glass5ab533c2018-06-11 13:07:19 -0600379done:
380 log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n",
381 ofnode_get_name(node), *devp ? (*devp)->name : "(none)", ret);
382 return ret;
Simon Glassee145d62017-05-18 20:09:09 -0600383}
384
Simon Glass359c4872015-12-19 19:38:55 -0700385#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glassdc3efdc2018-11-18 08:14:30 -0700386int uclass_find_device_by_phandle(enum uclass_id id, struct udevice *parent,
387 const char *name, struct udevice **devp)
Simon Glass75f00df2015-07-02 18:15:38 -0600388{
389 struct udevice *dev;
390 struct uclass *uc;
391 int find_phandle;
392 int ret;
393
394 *devp = NULL;
Simon Glassbfb6e3d2017-05-18 20:09:12 -0600395 find_phandle = dev_read_u32_default(parent, name, -1);
Simon Glass75f00df2015-07-02 18:15:38 -0600396 if (find_phandle <= 0)
397 return -ENOENT;
398 ret = uclass_get(id, &uc);
399 if (ret)
400 return ret;
401
Liviu Dudau6f386222018-09-28 14:12:55 +0100402 uclass_foreach_dev(dev, uc) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700403 uint phandle;
404
Simon Glassbfb6e3d2017-05-18 20:09:12 -0600405 phandle = dev_read_phandle(dev);
Simon Glass75f00df2015-07-02 18:15:38 -0600406
407 if (phandle == find_phandle) {
408 *devp = dev;
409 return 0;
410 }
411 }
412
413 return -ENODEV;
414}
Simon Glass359c4872015-12-19 19:38:55 -0700415#endif
Simon Glass75f00df2015-07-02 18:15:38 -0600416
Simon Glass32d8ab62016-07-17 15:23:15 -0600417int uclass_get_device_by_driver(enum uclass_id id,
418 const struct driver *find_drv,
419 struct udevice **devp)
420{
421 struct udevice *dev;
422 struct uclass *uc;
423 int ret;
424
425 ret = uclass_get(id, &uc);
426 if (ret)
427 return ret;
428
Liviu Dudau6f386222018-09-28 14:12:55 +0100429 uclass_foreach_dev(dev, uc) {
Simon Glass32d8ab62016-07-17 15:23:15 -0600430 if (dev->driver == find_drv)
431 return uclass_get_device_tail(dev, 0, devp);
432 }
433
434 return -ENODEV;
435}
436
Simon Glass98c9fb02017-04-23 20:10:43 -0600437int uclass_get_device_tail(struct udevice *dev, int ret, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700438{
Simon Glassdd6ab882014-02-26 15:59:18 -0700439 if (ret)
440 return ret;
441
Simon Glass46cc8ba92015-04-24 22:33:07 -0600442 assert(dev);
Simon Glassdd6ab882014-02-26 15:59:18 -0700443 ret = device_probe(dev);
444 if (ret)
445 return ret;
446
447 *devp = dev;
448
449 return 0;
450}
451
Simon Glass4b67cef2014-07-23 06:55:08 -0600452int uclass_get_device(enum uclass_id id, int index, struct udevice **devp)
453{
454 struct udevice *dev;
455 int ret;
456
457 *devp = NULL;
458 ret = uclass_find_device(id, index, &dev);
459 return uclass_get_device_tail(dev, ret, devp);
460}
461
Przemyslaw Marczaka12a1f52015-04-15 13:07:23 +0200462int uclass_get_device_by_name(enum uclass_id id, const char *name,
463 struct udevice **devp)
464{
465 struct udevice *dev;
466 int ret;
467
468 *devp = NULL;
469 ret = uclass_find_device_by_name(id, name, &dev);
470 return uclass_get_device_tail(dev, ret, devp);
471}
472
Simon Glassdb6f0202014-07-23 06:55:12 -0600473int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
474{
475 struct udevice *dev;
476 int ret;
477
478 *devp = NULL;
479 ret = uclass_find_device_by_seq(id, seq, false, &dev);
480 if (ret == -ENODEV) {
481 /*
482 * We didn't find it in probed devices. See if there is one
483 * that will request this seq if probed.
484 */
485 ret = uclass_find_device_by_seq(id, seq, true, &dev);
486 }
487 return uclass_get_device_tail(dev, ret, devp);
488}
489
Simon Glassc1464ab2014-07-23 06:55:14 -0600490int uclass_get_device_by_of_offset(enum uclass_id id, int node,
491 struct udevice **devp)
492{
493 struct udevice *dev;
494 int ret;
495
496 *devp = NULL;
497 ret = uclass_find_device_by_of_offset(id, node, &dev);
498 return uclass_get_device_tail(dev, ret, devp);
499}
500
Simon Glassee145d62017-05-18 20:09:09 -0600501int uclass_get_device_by_ofnode(enum uclass_id id, ofnode node,
502 struct udevice **devp)
503{
504 struct udevice *dev;
505 int ret;
506
Simon Glass5ab533c2018-06-11 13:07:19 -0600507 log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node));
Simon Glassee145d62017-05-18 20:09:09 -0600508 *devp = NULL;
509 ret = uclass_find_device_by_ofnode(id, node, &dev);
Simon Glass5ab533c2018-06-11 13:07:19 -0600510 log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n",
511 ofnode_get_name(node), dev ? dev->name : "(none)", ret);
Simon Glassee145d62017-05-18 20:09:09 -0600512
513 return uclass_get_device_tail(dev, ret, devp);
514}
515
Simon Glass359c4872015-12-19 19:38:55 -0700516#if CONFIG_IS_ENABLED(OF_CONTROL)
Kever Yang66a0b5a2018-02-09 10:56:23 +0800517int uclass_get_device_by_phandle_id(enum uclass_id id, uint phandle_id,
518 struct udevice **devp)
519{
520 struct udevice *dev;
521 struct uclass *uc;
522 int ret;
523
524 *devp = NULL;
525 ret = uclass_get(id, &uc);
526 if (ret)
527 return ret;
528
Liviu Dudau6f386222018-09-28 14:12:55 +0100529 uclass_foreach_dev(dev, uc) {
Kever Yang66a0b5a2018-02-09 10:56:23 +0800530 uint phandle;
531
532 phandle = dev_read_phandle(dev);
533
534 if (phandle == phandle_id) {
535 *devp = dev;
536 return uclass_get_device_tail(dev, ret, devp);
537 }
538 }
539
540 return -ENODEV;
541}
542
Simon Glass75f00df2015-07-02 18:15:38 -0600543int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent,
544 const char *name, struct udevice **devp)
545{
546 struct udevice *dev;
547 int ret;
548
549 *devp = NULL;
550 ret = uclass_find_device_by_phandle(id, parent, name, &dev);
551 return uclass_get_device_tail(dev, ret, devp);
552}
Simon Glass359c4872015-12-19 19:38:55 -0700553#endif
Simon Glass75f00df2015-07-02 18:15:38 -0600554
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200555int uclass_first_device(enum uclass_id id, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700556{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200557 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700558 int ret;
559
560 *devp = NULL;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200561 ret = uclass_find_first_device(id, &dev);
Simon Glass46cc8ba92015-04-24 22:33:07 -0600562 if (!dev)
563 return 0;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200564 return uclass_get_device_tail(dev, ret, devp);
Simon Glassdd6ab882014-02-26 15:59:18 -0700565}
566
Simon Glass832c3f02016-02-11 13:23:25 -0700567int uclass_first_device_err(enum uclass_id id, struct udevice **devp)
568{
569 int ret;
570
571 ret = uclass_first_device(id, devp);
572 if (ret)
573 return ret;
574 else if (!*devp)
575 return -ENODEV;
576
577 return 0;
578}
579
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200580int uclass_next_device(struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700581{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200582 struct udevice *dev = *devp;
Simon Glassdd6ab882014-02-26 15:59:18 -0700583 int ret;
584
585 *devp = NULL;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200586 ret = uclass_find_next_device(&dev);
Simon Glass46cc8ba92015-04-24 22:33:07 -0600587 if (!dev)
588 return 0;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200589 return uclass_get_device_tail(dev, ret, devp);
Simon Glassdd6ab882014-02-26 15:59:18 -0700590}
591
Patrice Chotard4dff1782018-10-24 14:10:15 +0200592int uclass_next_device_err(struct udevice **devp)
593{
594 int ret;
595
596 ret = uclass_next_device(devp);
597 if (ret)
598 return ret;
599 else if (!*devp)
600 return -ENODEV;
601
602 return 0;
603}
604
Simon Glass3ff83bc2017-04-23 20:10:45 -0600605int uclass_first_device_check(enum uclass_id id, struct udevice **devp)
606{
607 int ret;
608
609 *devp = NULL;
610 ret = uclass_find_first_device(id, devp);
611 if (ret)
612 return ret;
613 if (!*devp)
614 return 0;
615
616 return device_probe(*devp);
617}
618
619int uclass_next_device_check(struct udevice **devp)
620{
621 int ret;
622
623 ret = uclass_find_next_device(devp);
624 if (ret)
625 return ret;
626 if (!*devp)
627 return 0;
628
629 return device_probe(*devp);
630}
631
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200632int uclass_bind_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700633{
634 struct uclass *uc;
635 int ret;
636
637 uc = dev->uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700638 list_add_tail(&dev->uclass_node, &uc->dev_head);
639
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700640 if (dev->parent) {
641 struct uclass_driver *uc_drv = dev->parent->uclass->uc_drv;
642
643 if (uc_drv->child_post_bind) {
644 ret = uc_drv->child_post_bind(dev);
645 if (ret)
646 goto err;
647 }
648 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700649
650 return 0;
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700651err:
652 /* There is no need to undo the parent's post_bind call */
653 list_del(&dev->uclass_node);
654
655 return ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700656}
657
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900658#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200659int uclass_unbind_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700660{
661 struct uclass *uc;
662 int ret;
663
664 uc = dev->uclass;
665 if (uc->uc_drv->pre_unbind) {
666 ret = uc->uc_drv->pre_unbind(dev);
667 if (ret)
668 return ret;
669 }
670
671 list_del(&dev->uclass_node);
672 return 0;
673}
Simon Glass8914c8a2015-02-27 22:06:31 -0700674#endif
Simon Glassdd6ab882014-02-26 15:59:18 -0700675
Simon Glassdb6f0202014-07-23 06:55:12 -0600676int uclass_resolve_seq(struct udevice *dev)
677{
678 struct udevice *dup;
679 int seq;
680 int ret;
681
682 assert(dev->seq == -1);
683 ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, dev->req_seq,
684 false, &dup);
685 if (!ret) {
686 dm_warn("Device '%s': seq %d is in use by '%s'\n",
687 dev->name, dev->req_seq, dup->name);
688 } else if (ret == -ENODEV) {
689 /* Our requested sequence number is available */
690 if (dev->req_seq != -1)
691 return dev->req_seq;
692 } else {
693 return ret;
694 }
695
696 for (seq = 0; seq < DM_MAX_SEQ; seq++) {
697 ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, seq,
698 false, &dup);
699 if (ret == -ENODEV)
700 break;
701 if (ret)
702 return ret;
703 }
704 return seq;
705}
706
Simon Glass9c1f3822015-03-05 12:25:22 -0700707int uclass_pre_probe_device(struct udevice *dev)
Simon Glass5104b982015-01-25 08:27:10 -0700708{
709 struct uclass_driver *uc_drv;
Simon Glass9c1f3822015-03-05 12:25:22 -0700710 int ret;
711
712 uc_drv = dev->uclass->uc_drv;
713 if (uc_drv->pre_probe) {
714 ret = uc_drv->pre_probe(dev);
715 if (ret)
716 return ret;
717 }
Simon Glass5104b982015-01-25 08:27:10 -0700718
719 if (!dev->parent)
720 return 0;
721 uc_drv = dev->parent->uclass->uc_drv;
Simon Glass256d4022019-09-25 08:55:47 -0600722 if (uc_drv->child_pre_probe) {
723 ret = uc_drv->child_pre_probe(dev);
724 if (ret)
725 return ret;
726 }
Simon Glass5104b982015-01-25 08:27:10 -0700727
728 return 0;
729}
730
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200731int uclass_post_probe_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700732{
Bin Meng30569cf2018-10-15 02:20:57 -0700733 struct uclass_driver *uc_drv;
734 int ret;
735
736 if (dev->parent) {
737 uc_drv = dev->parent->uclass->uc_drv;
738 if (uc_drv->child_post_probe) {
739 ret = uc_drv->child_post_probe(dev);
740 if (ret)
741 return ret;
742 }
743 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700744
Bin Meng30569cf2018-10-15 02:20:57 -0700745 uc_drv = dev->uclass->uc_drv;
Simon Glass256d4022019-09-25 08:55:47 -0600746 if (uc_drv->post_probe) {
747 ret = uc_drv->post_probe(dev);
748 if (ret)
749 return ret;
750 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700751
752 return 0;
753}
754
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900755#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200756int uclass_pre_remove_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700757{
Simon Glassdd6ab882014-02-26 15:59:18 -0700758 struct uclass *uc;
759 int ret;
760
761 uc = dev->uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700762 if (uc->uc_drv->pre_remove) {
763 ret = uc->uc_drv->pre_remove(dev);
764 if (ret)
765 return ret;
766 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700767
768 return 0;
769}
Simon Glass8914c8a2015-02-27 22:06:31 -0700770#endif
Jean-Jacques Hiblotdb97c7f2019-07-05 09:33:57 +0200771
772UCLASS_DRIVER(nop) = {
773 .id = UCLASS_NOP,
774 .name = "nop",
775};