blob: c3f1b73cd6b4c3b751bd81fadf977ccea5163f4a [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>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070015#include <malloc.h>
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
23DECLARE_GLOBAL_DATA_PTR;
24
25struct uclass *uclass_find(enum uclass_id key)
26{
27 struct uclass *uc;
28
Simon Glassde708672014-07-23 06:55:15 -060029 if (!gd->dm_root)
30 return NULL;
Simon Glassdd6ab882014-02-26 15:59:18 -070031 /*
32 * TODO(sjg@chromium.org): 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 */
36 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
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
48 * @return 0 on success, -ve on error
49 *
50 * The new uclass is added to the list. There must be only one uclass for
51 * each id.
52 */
53static 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) {
Masahiro Yamada1d06ced2015-07-07 18:51:32 +090062 debug("Cannot find uclass for id %d: please add the UCLASS_DRIVER() declaration for this UCLASS_... id\n",
63 id);
Simon Glass43313de2015-08-30 16:55:16 -060064 /*
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;
Simon Glassdd6ab882014-02-26 15:59:18 -070070 }
Simon Glassdd6ab882014-02-26 15:59:18 -070071 uc = calloc(1, sizeof(*uc));
72 if (!uc)
73 return -ENOMEM;
74 if (uc_drv->priv_auto_alloc_size) {
75 uc->priv = calloc(1, uc_drv->priv_auto_alloc_size);
76 if (!uc->priv) {
77 ret = -ENOMEM;
78 goto fail_mem;
79 }
80 }
81 uc->uc_drv = uc_drv;
82 INIT_LIST_HEAD(&uc->sibling_node);
83 INIT_LIST_HEAD(&uc->dev_head);
Simon Glass34a1d352014-06-11 23:29:49 -060084 list_add(&uc->sibling_node, &DM_UCLASS_ROOT_NON_CONST);
Simon Glassdd6ab882014-02-26 15:59:18 -070085
86 if (uc_drv->init) {
87 ret = uc_drv->init(uc);
88 if (ret)
89 goto fail;
90 }
91
92 *ucp = uc;
93
94 return 0;
95fail:
96 if (uc_drv->priv_auto_alloc_size) {
97 free(uc->priv);
98 uc->priv = NULL;
99 }
100 list_del(&uc->sibling_node);
101fail_mem:
102 free(uc);
103
104 return ret;
105}
106
107int uclass_destroy(struct uclass *uc)
108{
109 struct uclass_driver *uc_drv;
Simon Glassf33663d2015-04-19 07:20:58 -0600110 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700111 int ret;
112
Simon Glassf33663d2015-04-19 07:20:58 -0600113 /*
114 * We cannot use list_for_each_entry_safe() here. If a device in this
115 * uclass has a child device also in this uclass, it will be also be
116 * unbound (by the recursion in the call to device_unbind() below).
117 * We can loop until the list is empty.
118 */
119 while (!list_empty(&uc->dev_head)) {
120 dev = list_first_entry(&uc->dev_head, struct udevice,
121 uclass_node);
Simon Glass42697f52020-03-28 14:03:48 -0600122 ret = device_remove(dev, DM_REMOVE_NORMAL | DM_REMOVE_NO_PD);
Simon Glassdd6ab882014-02-26 15:59:18 -0700123 if (ret)
Simon Glass5f3c9e02020-03-28 14:03:47 -0600124 return log_msg_ret("remove", ret);
Simon Glassdd6ab882014-02-26 15:59:18 -0700125 ret = device_unbind(dev);
126 if (ret)
Simon Glass5f3c9e02020-03-28 14:03:47 -0600127 return log_msg_ret("unbind", ret);
Simon Glassdd6ab882014-02-26 15:59:18 -0700128 }
129
130 uc_drv = uc->uc_drv;
131 if (uc_drv->destroy)
132 uc_drv->destroy(uc);
133 list_del(&uc->sibling_node);
134 if (uc_drv->priv_auto_alloc_size)
135 free(uc->priv);
136 free(uc);
137
138 return 0;
139}
140
141int uclass_get(enum uclass_id id, struct uclass **ucp)
142{
143 struct uclass *uc;
144
145 *ucp = NULL;
146 uc = uclass_find(id);
147 if (!uc)
148 return uclass_add(id, ucp);
149 *ucp = uc;
150
151 return 0;
152}
153
Simon Glassd19d0732016-10-05 20:42:13 -0600154const char *uclass_get_name(enum uclass_id id)
155{
156 struct uclass *uc;
157
158 if (uclass_get(id, &uc))
159 return NULL;
160 return uc->uc_drv->name;
161}
162
Simon Glass70e35b42017-12-28 13:14:15 -0700163enum uclass_id uclass_get_by_name(const char *name)
164{
165 int i;
166
167 for (i = 0; i < UCLASS_COUNT; i++) {
168 struct uclass_driver *uc_drv = lists_uclass_lookup(i);
169
170 if (uc_drv && !strcmp(uc_drv->name, name))
171 return i;
172 }
173
174 return UCLASS_INVALID;
175}
176
Jean-Jacques Hiblotc4f02482018-08-09 16:17:42 +0200177int dev_get_uclass_index(struct udevice *dev, struct uclass **ucp)
178{
179 struct udevice *iter;
180 struct uclass *uc = dev->uclass;
181 int i = 0;
182
183 if (list_empty(&uc->dev_head))
184 return -ENODEV;
185
Liviu Dudau6f386222018-09-28 14:12:55 +0100186 uclass_foreach_dev(iter, uc) {
Jean-Jacques Hiblotc4f02482018-08-09 16:17:42 +0200187 if (iter == dev) {
188 if (ucp)
189 *ucp = uc;
190 return i;
191 }
192 i++;
193 }
194
195 return -ENODEV;
196}
197
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200198int uclass_find_device(enum uclass_id id, int index, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700199{
200 struct uclass *uc;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200201 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700202 int ret;
203
204 *devp = NULL;
205 ret = uclass_get(id, &uc);
206 if (ret)
207 return ret;
Simon Glass46766df2015-07-31 09:31:19 -0600208 if (list_empty(&uc->dev_head))
209 return -ENODEV;
Simon Glassdd6ab882014-02-26 15:59:18 -0700210
Liviu Dudau6f386222018-09-28 14:12:55 +0100211 uclass_foreach_dev(dev, uc) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700212 if (!index--) {
213 *devp = dev;
214 return 0;
215 }
216 }
217
218 return -ENODEV;
219}
220
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200221int uclass_find_first_device(enum uclass_id id, struct udevice **devp)
222{
223 struct uclass *uc;
224 int ret;
225
226 *devp = NULL;
227 ret = uclass_get(id, &uc);
228 if (ret)
229 return ret;
230 if (list_empty(&uc->dev_head))
Simon Glass0bb44272019-09-25 08:55:55 -0600231 return 0;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200232
233 *devp = list_first_entry(&uc->dev_head, struct udevice, uclass_node);
234
235 return 0;
236}
237
238int uclass_find_next_device(struct udevice **devp)
239{
240 struct udevice *dev = *devp;
241
242 *devp = NULL;
243 if (list_is_last(&dev->uclass_node, &dev->uclass->dev_head))
244 return 0;
245
246 *devp = list_entry(dev->uclass_node.next, struct udevice, uclass_node);
247
248 return 0;
249}
250
Przemyslaw Marczak2ffdf142015-04-15 13:07:22 +0200251int uclass_find_device_by_name(enum uclass_id id, const char *name,
252 struct udevice **devp)
253{
254 struct uclass *uc;
255 struct udevice *dev;
256 int ret;
257
258 *devp = NULL;
259 if (!name)
260 return -EINVAL;
261 ret = uclass_get(id, &uc);
262 if (ret)
263 return ret;
264
Liviu Dudau6f386222018-09-28 14:12:55 +0100265 uclass_foreach_dev(dev, uc) {
Peng Fan26d68b22019-05-22 07:08:14 +0000266 if (!strcmp(dev->name, name)) {
Przemyslaw Marczak2ffdf142015-04-15 13:07:22 +0200267 *devp = dev;
268 return 0;
269 }
270 }
271
272 return -ENODEV;
273}
274
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100275int uclass_find_next_free_req_seq(enum uclass_id id)
276{
277 struct uclass *uc;
278 struct udevice *dev;
279 int ret;
280 int max = -1;
281
282 ret = uclass_get(id, &uc);
283 if (ret)
284 return ret;
285
286 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
287 if ((dev->req_seq != -1) && (dev->req_seq > max))
288 max = dev->req_seq;
289 }
290
291 if (max == -1)
292 return 0;
293
294 return max + 1;
295}
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100296
Simon Glassdb6f0202014-07-23 06:55:12 -0600297int uclass_find_device_by_seq(enum uclass_id id, int seq_or_req_seq,
298 bool find_req_seq, struct udevice **devp)
299{
300 struct uclass *uc;
301 struct udevice *dev;
302 int ret;
303
304 *devp = NULL;
Kever Yang5f09a2d2019-10-22 15:39:47 +0800305 log_debug("%d %d\n", find_req_seq, seq_or_req_seq);
Simon Glassdb6f0202014-07-23 06:55:12 -0600306 if (seq_or_req_seq == -1)
307 return -ENODEV;
308 ret = uclass_get(id, &uc);
309 if (ret)
310 return ret;
311
Liviu Dudau6f386222018-09-28 14:12:55 +0100312 uclass_foreach_dev(dev, uc) {
Kever Yang5f09a2d2019-10-22 15:39:47 +0800313 log_debug(" - %d %d '%s'\n",
314 dev->req_seq, dev->seq, dev->name);
Simon Glassdb6f0202014-07-23 06:55:12 -0600315 if ((find_req_seq ? dev->req_seq : dev->seq) ==
316 seq_or_req_seq) {
317 *devp = dev;
Kever Yang5f09a2d2019-10-22 15:39:47 +0800318 log_debug(" - found\n");
Simon Glassdb6f0202014-07-23 06:55:12 -0600319 return 0;
320 }
321 }
Kever Yang5f09a2d2019-10-22 15:39:47 +0800322 log_debug(" - not found\n");
Simon Glassdb6f0202014-07-23 06:55:12 -0600323
324 return -ENODEV;
325}
326
Simon Glass96f04442016-01-21 19:43:57 -0700327int uclass_find_device_by_of_offset(enum uclass_id id, int node,
328 struct udevice **devp)
Simon Glassc1464ab2014-07-23 06:55:14 -0600329{
330 struct uclass *uc;
331 struct udevice *dev;
332 int ret;
333
334 *devp = NULL;
335 if (node < 0)
336 return -ENODEV;
337 ret = uclass_get(id, &uc);
338 if (ret)
339 return ret;
340
Liviu Dudau6f386222018-09-28 14:12:55 +0100341 uclass_foreach_dev(dev, uc) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700342 if (dev_of_offset(dev) == node) {
Simon Glassc1464ab2014-07-23 06:55:14 -0600343 *devp = dev;
344 return 0;
345 }
346 }
347
348 return -ENODEV;
349}
350
Simon Glassee145d62017-05-18 20:09:09 -0600351int uclass_find_device_by_ofnode(enum uclass_id id, ofnode node,
352 struct udevice **devp)
353{
354 struct uclass *uc;
355 struct udevice *dev;
356 int ret;
357
Simon Glass5ab533c2018-06-11 13:07:19 -0600358 log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node));
Simon Glassee145d62017-05-18 20:09:09 -0600359 *devp = NULL;
360 if (!ofnode_valid(node))
361 return -ENODEV;
362 ret = uclass_get(id, &uc);
363 if (ret)
364 return ret;
365
Liviu Dudau6f386222018-09-28 14:12:55 +0100366 uclass_foreach_dev(dev, uc) {
Simon Glass5ab533c2018-06-11 13:07:19 -0600367 log(LOGC_DM, LOGL_DEBUG_CONTENT, " - checking %s\n",
368 dev->name);
Simon Glassee145d62017-05-18 20:09:09 -0600369 if (ofnode_equal(dev_ofnode(dev), node)) {
370 *devp = dev;
Simon Glass5ab533c2018-06-11 13:07:19 -0600371 goto done;
Simon Glassee145d62017-05-18 20:09:09 -0600372 }
373 }
Simon Glass5ab533c2018-06-11 13:07:19 -0600374 ret = -ENODEV;
Simon Glassee145d62017-05-18 20:09:09 -0600375
Simon Glass5ab533c2018-06-11 13:07:19 -0600376done:
377 log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n",
378 ofnode_get_name(node), *devp ? (*devp)->name : "(none)", ret);
379 return ret;
Simon Glassee145d62017-05-18 20:09:09 -0600380}
381
Simon Glass359c4872015-12-19 19:38:55 -0700382#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glassdc3efdc2018-11-18 08:14:30 -0700383int uclass_find_device_by_phandle(enum uclass_id id, struct udevice *parent,
384 const char *name, struct udevice **devp)
Simon Glass75f00df2015-07-02 18:15:38 -0600385{
386 struct udevice *dev;
387 struct uclass *uc;
388 int find_phandle;
389 int ret;
390
391 *devp = NULL;
Simon Glassbfb6e3d2017-05-18 20:09:12 -0600392 find_phandle = dev_read_u32_default(parent, name, -1);
Simon Glass75f00df2015-07-02 18:15:38 -0600393 if (find_phandle <= 0)
394 return -ENOENT;
395 ret = uclass_get(id, &uc);
396 if (ret)
397 return ret;
398
Liviu Dudau6f386222018-09-28 14:12:55 +0100399 uclass_foreach_dev(dev, uc) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700400 uint phandle;
401
Simon Glassbfb6e3d2017-05-18 20:09:12 -0600402 phandle = dev_read_phandle(dev);
Simon Glass75f00df2015-07-02 18:15:38 -0600403
404 if (phandle == find_phandle) {
405 *devp = dev;
406 return 0;
407 }
408 }
409
410 return -ENODEV;
411}
Simon Glass359c4872015-12-19 19:38:55 -0700412#endif
Simon Glass75f00df2015-07-02 18:15:38 -0600413
Simon Glass32d8ab62016-07-17 15:23:15 -0600414int uclass_get_device_by_driver(enum uclass_id id,
415 const struct driver *find_drv,
416 struct udevice **devp)
417{
418 struct udevice *dev;
419 struct uclass *uc;
420 int ret;
421
422 ret = uclass_get(id, &uc);
423 if (ret)
424 return ret;
425
Liviu Dudau6f386222018-09-28 14:12:55 +0100426 uclass_foreach_dev(dev, uc) {
Simon Glass32d8ab62016-07-17 15:23:15 -0600427 if (dev->driver == find_drv)
428 return uclass_get_device_tail(dev, 0, devp);
429 }
430
431 return -ENODEV;
432}
433
Simon Glass98c9fb02017-04-23 20:10:43 -0600434int uclass_get_device_tail(struct udevice *dev, int ret, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700435{
Simon Glassdd6ab882014-02-26 15:59:18 -0700436 if (ret)
437 return ret;
438
Simon Glass46cc8ba92015-04-24 22:33:07 -0600439 assert(dev);
Simon Glassdd6ab882014-02-26 15:59:18 -0700440 ret = device_probe(dev);
441 if (ret)
442 return ret;
443
444 *devp = dev;
445
446 return 0;
447}
448
Simon Glass4b67cef2014-07-23 06:55:08 -0600449int uclass_get_device(enum uclass_id id, int index, struct udevice **devp)
450{
451 struct udevice *dev;
452 int ret;
453
454 *devp = NULL;
455 ret = uclass_find_device(id, index, &dev);
456 return uclass_get_device_tail(dev, ret, devp);
457}
458
Przemyslaw Marczaka12a1f52015-04-15 13:07:23 +0200459int uclass_get_device_by_name(enum uclass_id id, const char *name,
460 struct udevice **devp)
461{
462 struct udevice *dev;
463 int ret;
464
465 *devp = NULL;
466 ret = uclass_find_device_by_name(id, name, &dev);
467 return uclass_get_device_tail(dev, ret, devp);
468}
469
Simon Glassdb6f0202014-07-23 06:55:12 -0600470int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
471{
472 struct udevice *dev;
473 int ret;
474
475 *devp = NULL;
476 ret = uclass_find_device_by_seq(id, seq, false, &dev);
477 if (ret == -ENODEV) {
478 /*
479 * We didn't find it in probed devices. See if there is one
480 * that will request this seq if probed.
481 */
482 ret = uclass_find_device_by_seq(id, seq, true, &dev);
483 }
484 return uclass_get_device_tail(dev, ret, devp);
485}
486
Simon Glassc1464ab2014-07-23 06:55:14 -0600487int uclass_get_device_by_of_offset(enum uclass_id id, int node,
488 struct udevice **devp)
489{
490 struct udevice *dev;
491 int ret;
492
493 *devp = NULL;
494 ret = uclass_find_device_by_of_offset(id, node, &dev);
495 return uclass_get_device_tail(dev, ret, devp);
496}
497
Simon Glassee145d62017-05-18 20:09:09 -0600498int uclass_get_device_by_ofnode(enum uclass_id id, ofnode node,
499 struct udevice **devp)
500{
501 struct udevice *dev;
502 int ret;
503
Simon Glass5ab533c2018-06-11 13:07:19 -0600504 log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node));
Simon Glassee145d62017-05-18 20:09:09 -0600505 *devp = NULL;
506 ret = uclass_find_device_by_ofnode(id, node, &dev);
Simon Glass5ab533c2018-06-11 13:07:19 -0600507 log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n",
508 ofnode_get_name(node), dev ? dev->name : "(none)", ret);
Simon Glassee145d62017-05-18 20:09:09 -0600509
510 return uclass_get_device_tail(dev, ret, devp);
511}
512
Simon Glass359c4872015-12-19 19:38:55 -0700513#if CONFIG_IS_ENABLED(OF_CONTROL)
Kever Yang66a0b5a2018-02-09 10:56:23 +0800514int uclass_get_device_by_phandle_id(enum uclass_id id, uint phandle_id,
515 struct udevice **devp)
516{
517 struct udevice *dev;
518 struct uclass *uc;
519 int ret;
520
521 *devp = NULL;
522 ret = uclass_get(id, &uc);
523 if (ret)
524 return ret;
525
Liviu Dudau6f386222018-09-28 14:12:55 +0100526 uclass_foreach_dev(dev, uc) {
Kever Yang66a0b5a2018-02-09 10:56:23 +0800527 uint phandle;
528
529 phandle = dev_read_phandle(dev);
530
531 if (phandle == phandle_id) {
532 *devp = dev;
533 return uclass_get_device_tail(dev, ret, devp);
534 }
535 }
536
537 return -ENODEV;
538}
539
Simon Glass75f00df2015-07-02 18:15:38 -0600540int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent,
541 const char *name, struct udevice **devp)
542{
543 struct udevice *dev;
544 int ret;
545
546 *devp = NULL;
547 ret = uclass_find_device_by_phandle(id, parent, name, &dev);
548 return uclass_get_device_tail(dev, ret, devp);
549}
Simon Glass359c4872015-12-19 19:38:55 -0700550#endif
Simon Glass75f00df2015-07-02 18:15:38 -0600551
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200552int uclass_first_device(enum uclass_id id, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700553{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200554 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700555 int ret;
556
557 *devp = NULL;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200558 ret = uclass_find_first_device(id, &dev);
Simon Glass46cc8ba92015-04-24 22:33:07 -0600559 if (!dev)
560 return 0;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200561 return uclass_get_device_tail(dev, ret, devp);
Simon Glassdd6ab882014-02-26 15:59:18 -0700562}
563
Simon Glass832c3f02016-02-11 13:23:25 -0700564int uclass_first_device_err(enum uclass_id id, struct udevice **devp)
565{
566 int ret;
567
568 ret = uclass_first_device(id, devp);
569 if (ret)
570 return ret;
571 else if (!*devp)
572 return -ENODEV;
573
574 return 0;
575}
576
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200577int uclass_next_device(struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700578{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200579 struct udevice *dev = *devp;
Simon Glassdd6ab882014-02-26 15:59:18 -0700580 int ret;
581
582 *devp = NULL;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200583 ret = uclass_find_next_device(&dev);
Simon Glass46cc8ba92015-04-24 22:33:07 -0600584 if (!dev)
585 return 0;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200586 return uclass_get_device_tail(dev, ret, devp);
Simon Glassdd6ab882014-02-26 15:59:18 -0700587}
588
Patrice Chotard4dff1782018-10-24 14:10:15 +0200589int uclass_next_device_err(struct udevice **devp)
590{
591 int ret;
592
593 ret = uclass_next_device(devp);
594 if (ret)
595 return ret;
596 else if (!*devp)
597 return -ENODEV;
598
599 return 0;
600}
601
Simon Glass3ff83bc2017-04-23 20:10:45 -0600602int uclass_first_device_check(enum uclass_id id, struct udevice **devp)
603{
604 int ret;
605
606 *devp = NULL;
607 ret = uclass_find_first_device(id, devp);
608 if (ret)
609 return ret;
610 if (!*devp)
611 return 0;
612
613 return device_probe(*devp);
614}
615
616int uclass_next_device_check(struct udevice **devp)
617{
618 int ret;
619
620 ret = uclass_find_next_device(devp);
621 if (ret)
622 return ret;
623 if (!*devp)
624 return 0;
625
626 return device_probe(*devp);
627}
628
Simon Glass35cb2a42020-02-06 09:54:50 -0700629int uclass_first_device_drvdata(enum uclass_id id, ulong driver_data,
630 struct udevice **devp)
631{
632 struct udevice *dev;
633 struct uclass *uc;
634
635 uclass_id_foreach_dev(id, dev, uc) {
636 if (dev_get_driver_data(dev) == driver_data) {
637 *devp = dev;
638
639 return device_probe(dev);
640 }
641 }
642
643 return -ENODEV;
644}
645
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200646int uclass_bind_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700647{
648 struct uclass *uc;
649 int ret;
650
651 uc = dev->uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700652 list_add_tail(&dev->uclass_node, &uc->dev_head);
653
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700654 if (dev->parent) {
655 struct uclass_driver *uc_drv = dev->parent->uclass->uc_drv;
656
657 if (uc_drv->child_post_bind) {
658 ret = uc_drv->child_post_bind(dev);
659 if (ret)
660 goto err;
661 }
662 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700663
664 return 0;
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700665err:
666 /* There is no need to undo the parent's post_bind call */
667 list_del(&dev->uclass_node);
668
669 return ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700670}
671
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900672#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200673int uclass_unbind_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700674{
675 struct uclass *uc;
676 int ret;
677
678 uc = dev->uclass;
679 if (uc->uc_drv->pre_unbind) {
680 ret = uc->uc_drv->pre_unbind(dev);
681 if (ret)
682 return ret;
683 }
684
685 list_del(&dev->uclass_node);
686 return 0;
687}
Simon Glass8914c8a2015-02-27 22:06:31 -0700688#endif
Simon Glassdd6ab882014-02-26 15:59:18 -0700689
Simon Glassdb6f0202014-07-23 06:55:12 -0600690int uclass_resolve_seq(struct udevice *dev)
691{
Michael Walle7c41a222020-06-02 01:47:09 +0200692 struct uclass *uc = dev->uclass;
693 struct uclass_driver *uc_drv = uc->uc_drv;
Simon Glassdb6f0202014-07-23 06:55:12 -0600694 struct udevice *dup;
Michael Walle7c41a222020-06-02 01:47:09 +0200695 int seq = 0;
Simon Glassdb6f0202014-07-23 06:55:12 -0600696 int ret;
697
698 assert(dev->seq == -1);
Michael Walle7c41a222020-06-02 01:47:09 +0200699 ret = uclass_find_device_by_seq(uc_drv->id, dev->req_seq, false, &dup);
Simon Glassdb6f0202014-07-23 06:55:12 -0600700 if (!ret) {
701 dm_warn("Device '%s': seq %d is in use by '%s'\n",
702 dev->name, dev->req_seq, dup->name);
703 } else if (ret == -ENODEV) {
704 /* Our requested sequence number is available */
705 if (dev->req_seq != -1)
706 return dev->req_seq;
707 } else {
708 return ret;
709 }
710
Michael Walle7c41a222020-06-02 01:47:09 +0200711 if (CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
712 (uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) {
713 /*
714 * dev_read_alias_highest_id() will return -1 if there no
715 * alias. Thus we can always add one.
716 */
717 seq = dev_read_alias_highest_id(uc_drv->name) + 1;
718 }
719
720 for (; seq < DM_MAX_SEQ; seq++) {
721 ret = uclass_find_device_by_seq(uc_drv->id, seq, false, &dup);
Simon Glassdb6f0202014-07-23 06:55:12 -0600722 if (ret == -ENODEV)
723 break;
724 if (ret)
725 return ret;
726 }
727 return seq;
728}
729
Simon Glass9c1f3822015-03-05 12:25:22 -0700730int uclass_pre_probe_device(struct udevice *dev)
Simon Glass5104b982015-01-25 08:27:10 -0700731{
732 struct uclass_driver *uc_drv;
Simon Glass9c1f3822015-03-05 12:25:22 -0700733 int ret;
734
735 uc_drv = dev->uclass->uc_drv;
736 if (uc_drv->pre_probe) {
737 ret = uc_drv->pre_probe(dev);
738 if (ret)
739 return ret;
740 }
Simon Glass5104b982015-01-25 08:27:10 -0700741
742 if (!dev->parent)
743 return 0;
744 uc_drv = dev->parent->uclass->uc_drv;
Simon Glass256d4022019-09-25 08:55:47 -0600745 if (uc_drv->child_pre_probe) {
746 ret = uc_drv->child_pre_probe(dev);
747 if (ret)
748 return ret;
749 }
Simon Glass5104b982015-01-25 08:27:10 -0700750
751 return 0;
752}
753
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200754int uclass_post_probe_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700755{
Bin Meng30569cf2018-10-15 02:20:57 -0700756 struct uclass_driver *uc_drv;
757 int ret;
758
759 if (dev->parent) {
760 uc_drv = dev->parent->uclass->uc_drv;
761 if (uc_drv->child_post_probe) {
762 ret = uc_drv->child_post_probe(dev);
763 if (ret)
764 return ret;
765 }
766 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700767
Bin Meng30569cf2018-10-15 02:20:57 -0700768 uc_drv = dev->uclass->uc_drv;
Simon Glass256d4022019-09-25 08:55:47 -0600769 if (uc_drv->post_probe) {
770 ret = uc_drv->post_probe(dev);
771 if (ret)
772 return ret;
773 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700774
775 return 0;
776}
777
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900778#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200779int uclass_pre_remove_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700780{
Simon Glassdd6ab882014-02-26 15:59:18 -0700781 struct uclass *uc;
782 int ret;
783
784 uc = dev->uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700785 if (uc->uc_drv->pre_remove) {
786 ret = uc->uc_drv->pre_remove(dev);
787 if (ret)
788 return ret;
789 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700790
791 return 0;
792}
Simon Glass8914c8a2015-02-27 22:06:31 -0700793#endif
Jean-Jacques Hiblotdb97c7f2019-07-05 09:33:57 +0200794
795UCLASS_DRIVER(nop) = {
796 .id = UCLASS_NOP,
797 .name = "nop",
798};