blob: f38122d54b5684220da814a61504ff70cfe4d2e7 [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 */
Simon Glass784cd9e2020-12-19 10:40:17 -070036 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
Simon Glassdd6ab882014-02-26 15:59:18 -070037 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;
Simon Glass8a2b47f2020-12-03 16:55:17 -070074 if (uc_drv->priv_auto) {
Simon Glass2543b162020-12-22 19:30:29 -070075 void *ptr;
76
77 ptr = calloc(1, uc_drv->priv_auto);
78 if (!ptr) {
Simon Glassdd6ab882014-02-26 15:59:18 -070079 ret = -ENOMEM;
80 goto fail_mem;
81 }
Simon Glass2543b162020-12-22 19:30:29 -070082 uclass_set_priv(uc, ptr);
Simon Glassdd6ab882014-02-26 15:59:18 -070083 }
84 uc->uc_drv = uc_drv;
85 INIT_LIST_HEAD(&uc->sibling_node);
86 INIT_LIST_HEAD(&uc->dev_head);
Simon Glass784cd9e2020-12-19 10:40:17 -070087 list_add(&uc->sibling_node, DM_UCLASS_ROOT_NON_CONST);
Simon Glassdd6ab882014-02-26 15:59:18 -070088
89 if (uc_drv->init) {
90 ret = uc_drv->init(uc);
91 if (ret)
92 goto fail;
93 }
94
95 *ucp = uc;
96
97 return 0;
98fail:
Simon Glass8a2b47f2020-12-03 16:55:17 -070099 if (uc_drv->priv_auto) {
Simon Glass2543b162020-12-22 19:30:29 -0700100 free(uclass_get_priv(uc));
101 uclass_set_priv(uc, NULL);
Simon Glassdd6ab882014-02-26 15:59:18 -0700102 }
103 list_del(&uc->sibling_node);
104fail_mem:
105 free(uc);
106
107 return ret;
108}
109
110int uclass_destroy(struct uclass *uc)
111{
112 struct uclass_driver *uc_drv;
Simon Glassf33663d2015-04-19 07:20:58 -0600113 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700114 int ret;
115
Simon Glassf33663d2015-04-19 07:20:58 -0600116 /*
117 * We cannot use list_for_each_entry_safe() here. If a device in this
118 * uclass has a child device also in this uclass, it will be also be
119 * unbound (by the recursion in the call to device_unbind() below).
120 * We can loop until the list is empty.
121 */
122 while (!list_empty(&uc->dev_head)) {
123 dev = list_first_entry(&uc->dev_head, struct udevice,
124 uclass_node);
Simon Glass42697f52020-03-28 14:03:48 -0600125 ret = device_remove(dev, DM_REMOVE_NORMAL | DM_REMOVE_NO_PD);
Simon Glassdd6ab882014-02-26 15:59:18 -0700126 if (ret)
Simon Glass5f3c9e02020-03-28 14:03:47 -0600127 return log_msg_ret("remove", ret);
Simon Glassdd6ab882014-02-26 15:59:18 -0700128 ret = device_unbind(dev);
129 if (ret)
Simon Glass5f3c9e02020-03-28 14:03:47 -0600130 return log_msg_ret("unbind", ret);
Simon Glassdd6ab882014-02-26 15:59:18 -0700131 }
132
133 uc_drv = uc->uc_drv;
134 if (uc_drv->destroy)
135 uc_drv->destroy(uc);
136 list_del(&uc->sibling_node);
Simon Glass8a2b47f2020-12-03 16:55:17 -0700137 if (uc_drv->priv_auto)
Simon Glass2543b162020-12-22 19:30:29 -0700138 free(uclass_get_priv(uc));
Simon Glassdd6ab882014-02-26 15:59:18 -0700139 free(uc);
140
141 return 0;
142}
143
144int uclass_get(enum uclass_id id, struct uclass **ucp)
145{
146 struct uclass *uc;
147
148 *ucp = NULL;
149 uc = uclass_find(id);
150 if (!uc)
151 return uclass_add(id, ucp);
152 *ucp = uc;
153
154 return 0;
155}
156
Simon Glassd19d0732016-10-05 20:42:13 -0600157const char *uclass_get_name(enum uclass_id id)
158{
159 struct uclass *uc;
160
161 if (uclass_get(id, &uc))
162 return NULL;
163 return uc->uc_drv->name;
164}
165
Simon Glass96404c22020-12-22 19:30:26 -0700166void *uclass_get_priv(const struct uclass *uc)
167{
Simon Glass541f62c2020-12-22 19:30:30 -0700168 return uc->priv_;
Simon Glass96404c22020-12-22 19:30:26 -0700169}
170
171void uclass_set_priv(struct uclass *uc, void *priv)
172{
Simon Glass541f62c2020-12-22 19:30:30 -0700173 uc->priv_ = priv;
Simon Glass96404c22020-12-22 19:30:26 -0700174}
175
Simon Glass70e35b42017-12-28 13:14:15 -0700176enum uclass_id uclass_get_by_name(const char *name)
177{
178 int i;
179
180 for (i = 0; i < UCLASS_COUNT; i++) {
181 struct uclass_driver *uc_drv = lists_uclass_lookup(i);
182
183 if (uc_drv && !strcmp(uc_drv->name, name))
184 return i;
185 }
186
187 return UCLASS_INVALID;
188}
189
Jean-Jacques Hiblotc4f02482018-08-09 16:17:42 +0200190int dev_get_uclass_index(struct udevice *dev, struct uclass **ucp)
191{
192 struct udevice *iter;
193 struct uclass *uc = dev->uclass;
194 int i = 0;
195
196 if (list_empty(&uc->dev_head))
197 return -ENODEV;
198
Liviu Dudau6f386222018-09-28 14:12:55 +0100199 uclass_foreach_dev(iter, uc) {
Jean-Jacques Hiblotc4f02482018-08-09 16:17:42 +0200200 if (iter == dev) {
201 if (ucp)
202 *ucp = uc;
203 return i;
204 }
205 i++;
206 }
207
208 return -ENODEV;
209}
210
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200211int uclass_find_device(enum uclass_id id, int index, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700212{
213 struct uclass *uc;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200214 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700215 int ret;
216
217 *devp = NULL;
218 ret = uclass_get(id, &uc);
219 if (ret)
220 return ret;
Simon Glass46766df2015-07-31 09:31:19 -0600221 if (list_empty(&uc->dev_head))
222 return -ENODEV;
Simon Glassdd6ab882014-02-26 15:59:18 -0700223
Liviu Dudau6f386222018-09-28 14:12:55 +0100224 uclass_foreach_dev(dev, uc) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700225 if (!index--) {
226 *devp = dev;
227 return 0;
228 }
229 }
230
231 return -ENODEV;
232}
233
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200234int uclass_find_first_device(enum uclass_id id, struct udevice **devp)
235{
236 struct uclass *uc;
237 int ret;
238
239 *devp = NULL;
240 ret = uclass_get(id, &uc);
241 if (ret)
242 return ret;
243 if (list_empty(&uc->dev_head))
Simon Glass0bb44272019-09-25 08:55:55 -0600244 return 0;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200245
246 *devp = list_first_entry(&uc->dev_head, struct udevice, uclass_node);
247
248 return 0;
249}
250
251int uclass_find_next_device(struct udevice **devp)
252{
253 struct udevice *dev = *devp;
254
255 *devp = NULL;
256 if (list_is_last(&dev->uclass_node, &dev->uclass->dev_head))
257 return 0;
258
259 *devp = list_entry(dev->uclass_node.next, struct udevice, uclass_node);
260
261 return 0;
262}
263
Przemyslaw Marczak2ffdf142015-04-15 13:07:22 +0200264int uclass_find_device_by_name(enum uclass_id id, const char *name,
265 struct udevice **devp)
266{
267 struct uclass *uc;
268 struct udevice *dev;
269 int ret;
270
271 *devp = NULL;
272 if (!name)
273 return -EINVAL;
274 ret = uclass_get(id, &uc);
275 if (ret)
276 return ret;
277
Liviu Dudau6f386222018-09-28 14:12:55 +0100278 uclass_foreach_dev(dev, uc) {
Peng Fan26d68b22019-05-22 07:08:14 +0000279 if (!strcmp(dev->name, name)) {
Przemyslaw Marczak2ffdf142015-04-15 13:07:22 +0200280 *devp = dev;
281 return 0;
282 }
283 }
284
285 return -ENODEV;
286}
287
Simon Glassb941f562020-12-16 21:20:30 -0700288int uclass_find_next_free_seq(struct uclass *uc)
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100289{
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100290 struct udevice *dev;
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100291 int max = -1;
292
Simon Glassb941f562020-12-16 21:20:30 -0700293 /* If using aliases, start with the highest alias value */
294 if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
295 (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS))
296 max = dev_read_alias_highest_id(uc->uc_drv->name);
297
298 /* Avoid conflict with existing devices */
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100299 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
Simon Glass5e349922020-12-19 10:40:09 -0700300 if (dev->seq_ > max)
301 max = dev->seq_;
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100302 }
Simon Glassb941f562020-12-16 21:20:30 -0700303 /*
304 * At this point, max will be -1 if there are no existing aliases or
305 * devices
306 */
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100307
308 return max + 1;
309}
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100310
Simon Glass07e13382020-12-16 21:20:29 -0700311int uclass_find_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
Simon Glassdb6f0202014-07-23 06:55:12 -0600312{
313 struct uclass *uc;
314 struct udevice *dev;
315 int ret;
316
317 *devp = NULL;
Simon Glass07e13382020-12-16 21:20:29 -0700318 log_debug("%d\n", seq);
319 if (seq == -1)
Simon Glassdb6f0202014-07-23 06:55:12 -0600320 return -ENODEV;
321 ret = uclass_get(id, &uc);
322 if (ret)
323 return ret;
324
Liviu Dudau6f386222018-09-28 14:12:55 +0100325 uclass_foreach_dev(dev, uc) {
Simon Glass5e349922020-12-19 10:40:09 -0700326 log_debug(" - %d '%s'\n", dev->seq_, dev->name);
327 if (dev->seq_ == seq) {
Simon Glassdb6f0202014-07-23 06:55:12 -0600328 *devp = dev;
Kever Yang5f09a2d2019-10-22 15:39:47 +0800329 log_debug(" - found\n");
Simon Glassdb6f0202014-07-23 06:55:12 -0600330 return 0;
331 }
332 }
Kever Yang5f09a2d2019-10-22 15:39:47 +0800333 log_debug(" - not found\n");
Simon Glassdb6f0202014-07-23 06:55:12 -0600334
335 return -ENODEV;
336}
337
Simon Glass96f04442016-01-21 19:43:57 -0700338int uclass_find_device_by_of_offset(enum uclass_id id, int node,
339 struct udevice **devp)
Simon Glassc1464ab2014-07-23 06:55:14 -0600340{
341 struct uclass *uc;
342 struct udevice *dev;
343 int ret;
344
345 *devp = NULL;
346 if (node < 0)
347 return -ENODEV;
348 ret = uclass_get(id, &uc);
349 if (ret)
350 return ret;
351
Liviu Dudau6f386222018-09-28 14:12:55 +0100352 uclass_foreach_dev(dev, uc) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700353 if (dev_of_offset(dev) == node) {
Simon Glassc1464ab2014-07-23 06:55:14 -0600354 *devp = dev;
355 return 0;
356 }
357 }
358
359 return -ENODEV;
360}
361
Simon Glassee145d62017-05-18 20:09:09 -0600362int uclass_find_device_by_ofnode(enum uclass_id id, ofnode node,
363 struct udevice **devp)
364{
365 struct uclass *uc;
366 struct udevice *dev;
367 int ret;
368
Simon Glass5ab533c2018-06-11 13:07:19 -0600369 log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node));
Simon Glassee145d62017-05-18 20:09:09 -0600370 *devp = NULL;
371 if (!ofnode_valid(node))
372 return -ENODEV;
373 ret = uclass_get(id, &uc);
374 if (ret)
375 return ret;
376
Liviu Dudau6f386222018-09-28 14:12:55 +0100377 uclass_foreach_dev(dev, uc) {
Simon Glass5ab533c2018-06-11 13:07:19 -0600378 log(LOGC_DM, LOGL_DEBUG_CONTENT, " - checking %s\n",
379 dev->name);
Simon Glassee145d62017-05-18 20:09:09 -0600380 if (ofnode_equal(dev_ofnode(dev), node)) {
381 *devp = dev;
Simon Glass5ab533c2018-06-11 13:07:19 -0600382 goto done;
Simon Glassee145d62017-05-18 20:09:09 -0600383 }
384 }
Simon Glass5ab533c2018-06-11 13:07:19 -0600385 ret = -ENODEV;
Simon Glassee145d62017-05-18 20:09:09 -0600386
Simon Glass5ab533c2018-06-11 13:07:19 -0600387done:
388 log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n",
389 ofnode_get_name(node), *devp ? (*devp)->name : "(none)", ret);
390 return ret;
Simon Glassee145d62017-05-18 20:09:09 -0600391}
392
Simon Glass359c4872015-12-19 19:38:55 -0700393#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glassdc3efdc2018-11-18 08:14:30 -0700394int uclass_find_device_by_phandle(enum uclass_id id, struct udevice *parent,
395 const char *name, struct udevice **devp)
Simon Glass75f00df2015-07-02 18:15:38 -0600396{
397 struct udevice *dev;
398 struct uclass *uc;
399 int find_phandle;
400 int ret;
401
402 *devp = NULL;
Simon Glassbfb6e3d2017-05-18 20:09:12 -0600403 find_phandle = dev_read_u32_default(parent, name, -1);
Simon Glass75f00df2015-07-02 18:15:38 -0600404 if (find_phandle <= 0)
405 return -ENOENT;
406 ret = uclass_get(id, &uc);
407 if (ret)
408 return ret;
409
Liviu Dudau6f386222018-09-28 14:12:55 +0100410 uclass_foreach_dev(dev, uc) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700411 uint phandle;
412
Simon Glassbfb6e3d2017-05-18 20:09:12 -0600413 phandle = dev_read_phandle(dev);
Simon Glass75f00df2015-07-02 18:15:38 -0600414
415 if (phandle == find_phandle) {
416 *devp = dev;
417 return 0;
418 }
419 }
420
421 return -ENODEV;
422}
Simon Glass359c4872015-12-19 19:38:55 -0700423#endif
Simon Glass75f00df2015-07-02 18:15:38 -0600424
Simon Glass32d8ab62016-07-17 15:23:15 -0600425int uclass_get_device_by_driver(enum uclass_id id,
426 const struct driver *find_drv,
427 struct udevice **devp)
428{
429 struct udevice *dev;
430 struct uclass *uc;
431 int ret;
432
433 ret = uclass_get(id, &uc);
434 if (ret)
435 return ret;
436
Liviu Dudau6f386222018-09-28 14:12:55 +0100437 uclass_foreach_dev(dev, uc) {
Simon Glass32d8ab62016-07-17 15:23:15 -0600438 if (dev->driver == find_drv)
439 return uclass_get_device_tail(dev, 0, devp);
440 }
441
442 return -ENODEV;
443}
444
Simon Glass98c9fb02017-04-23 20:10:43 -0600445int uclass_get_device_tail(struct udevice *dev, int ret, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700446{
Simon Glassdd6ab882014-02-26 15:59:18 -0700447 if (ret)
448 return ret;
449
Simon Glass46cc8ba92015-04-24 22:33:07 -0600450 assert(dev);
Simon Glassdd6ab882014-02-26 15:59:18 -0700451 ret = device_probe(dev);
452 if (ret)
453 return ret;
454
455 *devp = dev;
456
457 return 0;
458}
459
Simon Glass4b67cef2014-07-23 06:55:08 -0600460int uclass_get_device(enum uclass_id id, int index, struct udevice **devp)
461{
462 struct udevice *dev;
463 int ret;
464
465 *devp = NULL;
466 ret = uclass_find_device(id, index, &dev);
467 return uclass_get_device_tail(dev, ret, devp);
468}
469
Przemyslaw Marczaka12a1f52015-04-15 13:07:23 +0200470int uclass_get_device_by_name(enum uclass_id id, const char *name,
471 struct udevice **devp)
472{
473 struct udevice *dev;
474 int ret;
475
476 *devp = NULL;
477 ret = uclass_find_device_by_name(id, name, &dev);
478 return uclass_get_device_tail(dev, ret, devp);
479}
480
Simon Glassdb6f0202014-07-23 06:55:12 -0600481int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
482{
483 struct udevice *dev;
484 int ret;
485
486 *devp = NULL;
Simon Glass07e13382020-12-16 21:20:29 -0700487 ret = uclass_find_device_by_seq(id, seq, &dev);
488
Simon Glassdb6f0202014-07-23 06:55:12 -0600489 return uclass_get_device_tail(dev, ret, devp);
490}
491
Simon Glassc1464ab2014-07-23 06:55:14 -0600492int uclass_get_device_by_of_offset(enum uclass_id id, int node,
493 struct udevice **devp)
494{
495 struct udevice *dev;
496 int ret;
497
498 *devp = NULL;
499 ret = uclass_find_device_by_of_offset(id, node, &dev);
500 return uclass_get_device_tail(dev, ret, devp);
501}
502
Simon Glassee145d62017-05-18 20:09:09 -0600503int uclass_get_device_by_ofnode(enum uclass_id id, ofnode node,
504 struct udevice **devp)
505{
506 struct udevice *dev;
507 int ret;
508
Simon Glass5ab533c2018-06-11 13:07:19 -0600509 log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node));
Simon Glassee145d62017-05-18 20:09:09 -0600510 *devp = NULL;
511 ret = uclass_find_device_by_ofnode(id, node, &dev);
Simon Glass5ab533c2018-06-11 13:07:19 -0600512 log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n",
513 ofnode_get_name(node), dev ? dev->name : "(none)", ret);
Simon Glassee145d62017-05-18 20:09:09 -0600514
515 return uclass_get_device_tail(dev, ret, devp);
516}
517
Simon Glass359c4872015-12-19 19:38:55 -0700518#if CONFIG_IS_ENABLED(OF_CONTROL)
Kever Yang66a0b5a2018-02-09 10:56:23 +0800519int uclass_get_device_by_phandle_id(enum uclass_id id, uint phandle_id,
520 struct udevice **devp)
521{
522 struct udevice *dev;
523 struct uclass *uc;
524 int ret;
525
526 *devp = NULL;
527 ret = uclass_get(id, &uc);
528 if (ret)
529 return ret;
530
Liviu Dudau6f386222018-09-28 14:12:55 +0100531 uclass_foreach_dev(dev, uc) {
Kever Yang66a0b5a2018-02-09 10:56:23 +0800532 uint phandle;
533
534 phandle = dev_read_phandle(dev);
535
536 if (phandle == phandle_id) {
537 *devp = dev;
538 return uclass_get_device_tail(dev, ret, devp);
539 }
540 }
541
542 return -ENODEV;
543}
544
Simon Glass75f00df2015-07-02 18:15:38 -0600545int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent,
546 const char *name, struct udevice **devp)
547{
548 struct udevice *dev;
549 int ret;
550
551 *devp = NULL;
552 ret = uclass_find_device_by_phandle(id, parent, name, &dev);
553 return uclass_get_device_tail(dev, ret, devp);
554}
Simon Glass359c4872015-12-19 19:38:55 -0700555#endif
Simon Glass75f00df2015-07-02 18:15:38 -0600556
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200557int uclass_first_device(enum uclass_id id, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700558{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200559 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700560 int ret;
561
562 *devp = NULL;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200563 ret = uclass_find_first_device(id, &dev);
Simon Glass46cc8ba92015-04-24 22:33:07 -0600564 if (!dev)
565 return 0;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200566 return uclass_get_device_tail(dev, ret, devp);
Simon Glassdd6ab882014-02-26 15:59:18 -0700567}
568
Simon Glass832c3f02016-02-11 13:23:25 -0700569int uclass_first_device_err(enum uclass_id id, struct udevice **devp)
570{
571 int ret;
572
573 ret = uclass_first_device(id, devp);
574 if (ret)
575 return ret;
576 else if (!*devp)
577 return -ENODEV;
578
579 return 0;
580}
581
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200582int uclass_next_device(struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700583{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200584 struct udevice *dev = *devp;
Simon Glassdd6ab882014-02-26 15:59:18 -0700585 int ret;
586
587 *devp = NULL;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200588 ret = uclass_find_next_device(&dev);
Simon Glass46cc8ba92015-04-24 22:33:07 -0600589 if (!dev)
590 return 0;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200591 return uclass_get_device_tail(dev, ret, devp);
Simon Glassdd6ab882014-02-26 15:59:18 -0700592}
593
Patrice Chotard4dff1782018-10-24 14:10:15 +0200594int uclass_next_device_err(struct udevice **devp)
595{
596 int ret;
597
598 ret = uclass_next_device(devp);
599 if (ret)
600 return ret;
601 else if (!*devp)
602 return -ENODEV;
603
604 return 0;
605}
606
Simon Glass3ff83bc2017-04-23 20:10:45 -0600607int uclass_first_device_check(enum uclass_id id, struct udevice **devp)
608{
609 int ret;
610
611 *devp = NULL;
612 ret = uclass_find_first_device(id, devp);
613 if (ret)
614 return ret;
615 if (!*devp)
616 return 0;
617
618 return device_probe(*devp);
619}
620
621int uclass_next_device_check(struct udevice **devp)
622{
623 int ret;
624
625 ret = uclass_find_next_device(devp);
626 if (ret)
627 return ret;
628 if (!*devp)
629 return 0;
630
631 return device_probe(*devp);
632}
633
Simon Glass35cb2a42020-02-06 09:54:50 -0700634int uclass_first_device_drvdata(enum uclass_id id, ulong driver_data,
635 struct udevice **devp)
636{
637 struct udevice *dev;
638 struct uclass *uc;
639
640 uclass_id_foreach_dev(id, dev, uc) {
641 if (dev_get_driver_data(dev) == driver_data) {
642 *devp = dev;
643
644 return device_probe(dev);
645 }
646 }
647
648 return -ENODEV;
649}
650
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200651int uclass_bind_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700652{
653 struct uclass *uc;
654 int ret;
655
656 uc = dev->uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700657 list_add_tail(&dev->uclass_node, &uc->dev_head);
658
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700659 if (dev->parent) {
660 struct uclass_driver *uc_drv = dev->parent->uclass->uc_drv;
661
662 if (uc_drv->child_post_bind) {
663 ret = uc_drv->child_post_bind(dev);
664 if (ret)
665 goto err;
666 }
667 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700668
669 return 0;
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700670err:
671 /* There is no need to undo the parent's post_bind call */
672 list_del(&dev->uclass_node);
673
674 return ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700675}
676
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900677#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200678int uclass_unbind_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700679{
680 struct uclass *uc;
681 int ret;
682
683 uc = dev->uclass;
684 if (uc->uc_drv->pre_unbind) {
685 ret = uc->uc_drv->pre_unbind(dev);
686 if (ret)
687 return ret;
688 }
689
690 list_del(&dev->uclass_node);
691 return 0;
692}
Simon Glass8914c8a2015-02-27 22:06:31 -0700693#endif
Simon Glassdd6ab882014-02-26 15:59:18 -0700694
Simon Glass9c1f3822015-03-05 12:25:22 -0700695int uclass_pre_probe_device(struct udevice *dev)
Simon Glass5104b982015-01-25 08:27:10 -0700696{
697 struct uclass_driver *uc_drv;
Simon Glass9c1f3822015-03-05 12:25:22 -0700698 int ret;
699
700 uc_drv = dev->uclass->uc_drv;
701 if (uc_drv->pre_probe) {
702 ret = uc_drv->pre_probe(dev);
703 if (ret)
704 return ret;
705 }
Simon Glass5104b982015-01-25 08:27:10 -0700706
707 if (!dev->parent)
708 return 0;
709 uc_drv = dev->parent->uclass->uc_drv;
Simon Glass256d4022019-09-25 08:55:47 -0600710 if (uc_drv->child_pre_probe) {
711 ret = uc_drv->child_pre_probe(dev);
712 if (ret)
713 return ret;
714 }
Simon Glass5104b982015-01-25 08:27:10 -0700715
716 return 0;
717}
718
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200719int uclass_post_probe_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700720{
Bin Meng30569cf2018-10-15 02:20:57 -0700721 struct uclass_driver *uc_drv;
722 int ret;
723
724 if (dev->parent) {
725 uc_drv = dev->parent->uclass->uc_drv;
726 if (uc_drv->child_post_probe) {
727 ret = uc_drv->child_post_probe(dev);
728 if (ret)
729 return ret;
730 }
731 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700732
Bin Meng30569cf2018-10-15 02:20:57 -0700733 uc_drv = dev->uclass->uc_drv;
Simon Glass256d4022019-09-25 08:55:47 -0600734 if (uc_drv->post_probe) {
735 ret = uc_drv->post_probe(dev);
736 if (ret)
737 return ret;
738 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700739
740 return 0;
741}
742
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900743#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200744int uclass_pre_remove_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700745{
Simon Glassdd6ab882014-02-26 15:59:18 -0700746 struct uclass *uc;
747 int ret;
748
749 uc = dev->uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700750 if (uc->uc_drv->pre_remove) {
751 ret = uc->uc_drv->pre_remove(dev);
752 if (ret)
753 return ret;
754 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700755
756 return 0;
757}
Simon Glass8914c8a2015-02-27 22:06:31 -0700758#endif
Jean-Jacques Hiblotdb97c7f2019-07-05 09:33:57 +0200759
Vabhav Sharmabcab47a2020-12-09 10:42:03 +0530760int uclass_probe_all(enum uclass_id id)
761{
762 struct udevice *dev;
763 int ret;
764
765 ret = uclass_first_device(id, &dev);
766 if (ret || !dev)
767 return ret;
768
769 /* Scanning uclass to probe all devices */
770 while (dev) {
771 ret = uclass_next_device(&dev);
772 if (ret)
773 return ret;
774 }
775
776 return 0;
777}
778
Jean-Jacques Hiblotdb97c7f2019-07-05 09:33:57 +0200779UCLASS_DRIVER(nop) = {
780 .id = UCLASS_NOP,
781 .name = "nop",
782};