blob: f846a35d6b2537bab56bca158cedfb134b37f477 [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 Glassee145d62017-05-18 20:09:09 -060011#include <dm.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070012#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070014#include <malloc.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060015#include <asm/global_data.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070016#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
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010048 * Return: 0 on success, -ve on error
Simon Glassdd6ab882014-02-26 15:59:18 -070049 *
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) {
Quentin Schulz95a5a2b2024-06-11 15:04:26 +020062 dm_warn("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
Simon Glass8e9eacf2021-08-01 12:05:23 -0600148 /* Immediately fail if driver model is not set up */
149 if (!gd->uclass_root)
150 return -EDEADLK;
Simon Glassdd6ab882014-02-26 15:59:18 -0700151 *ucp = NULL;
152 uc = uclass_find(id);
Simon Glass05693e92021-03-15 17:25:18 +1300153 if (!uc) {
154 if (CONFIG_IS_ENABLED(OF_PLATDATA_INST))
155 return -ENOENT;
Simon Glassdd6ab882014-02-26 15:59:18 -0700156 return uclass_add(id, ucp);
Simon Glass05693e92021-03-15 17:25:18 +1300157 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700158 *ucp = uc;
159
160 return 0;
161}
162
Simon Glassd19d0732016-10-05 20:42:13 -0600163const char *uclass_get_name(enum uclass_id id)
164{
165 struct uclass *uc;
166
167 if (uclass_get(id, &uc))
168 return NULL;
169 return uc->uc_drv->name;
170}
171
Simon Glass96404c22020-12-22 19:30:26 -0700172void *uclass_get_priv(const struct uclass *uc)
173{
Simon Glass541f62c2020-12-22 19:30:30 -0700174 return uc->priv_;
Simon Glass96404c22020-12-22 19:30:26 -0700175}
176
177void uclass_set_priv(struct uclass *uc, void *priv)
178{
Simon Glass541f62c2020-12-22 19:30:30 -0700179 uc->priv_ = priv;
Simon Glass96404c22020-12-22 19:30:26 -0700180}
181
Simon Glassf1f519f2022-04-24 23:30:59 -0600182enum uclass_id uclass_get_by_namelen(const char *name, int len)
Simon Glass70e35b42017-12-28 13:14:15 -0700183{
184 int i;
185
186 for (i = 0; i < UCLASS_COUNT; i++) {
187 struct uclass_driver *uc_drv = lists_uclass_lookup(i);
188
Simon Glassf1f519f2022-04-24 23:30:59 -0600189 if (uc_drv && !strncmp(uc_drv->name, name, len) &&
190 strlen(uc_drv->name) == len)
Simon Glass70e35b42017-12-28 13:14:15 -0700191 return i;
192 }
193
194 return UCLASS_INVALID;
195}
196
Simon Glass4b23e452021-10-23 17:26:08 -0600197enum uclass_id uclass_get_by_name(const char *name)
198{
Simon Glassf1f519f2022-04-24 23:30:59 -0600199 return uclass_get_by_namelen(name, strlen(name));
Simon Glass4b23e452021-10-23 17:26:08 -0600200}
201
Jean-Jacques Hiblotc4f02482018-08-09 16:17:42 +0200202int dev_get_uclass_index(struct udevice *dev, struct uclass **ucp)
203{
204 struct udevice *iter;
205 struct uclass *uc = dev->uclass;
206 int i = 0;
207
208 if (list_empty(&uc->dev_head))
209 return -ENODEV;
210
Liviu Dudau6f386222018-09-28 14:12:55 +0100211 uclass_foreach_dev(iter, uc) {
Jean-Jacques Hiblotc4f02482018-08-09 16:17:42 +0200212 if (iter == dev) {
213 if (ucp)
214 *ucp = uc;
215 return i;
216 }
217 i++;
218 }
219
220 return -ENODEV;
221}
222
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200223int uclass_find_device(enum uclass_id id, int index, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700224{
225 struct uclass *uc;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200226 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700227 int ret;
228
229 *devp = NULL;
230 ret = uclass_get(id, &uc);
231 if (ret)
232 return ret;
Simon Glass46766df2015-07-31 09:31:19 -0600233 if (list_empty(&uc->dev_head))
234 return -ENODEV;
Simon Glassdd6ab882014-02-26 15:59:18 -0700235
Liviu Dudau6f386222018-09-28 14:12:55 +0100236 uclass_foreach_dev(dev, uc) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700237 if (!index--) {
238 *devp = dev;
239 return 0;
240 }
241 }
242
243 return -ENODEV;
244}
245
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200246int uclass_find_first_device(enum uclass_id id, struct udevice **devp)
247{
248 struct uclass *uc;
249 int ret;
250
251 *devp = NULL;
252 ret = uclass_get(id, &uc);
253 if (ret)
254 return ret;
255 if (list_empty(&uc->dev_head))
Simon Glass0bb44272019-09-25 08:55:55 -0600256 return 0;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200257
258 *devp = list_first_entry(&uc->dev_head, struct udevice, uclass_node);
259
260 return 0;
261}
262
263int uclass_find_next_device(struct udevice **devp)
264{
265 struct udevice *dev = *devp;
266
267 *devp = NULL;
268 if (list_is_last(&dev->uclass_node, &dev->uclass->dev_head))
269 return 0;
270
271 *devp = list_entry(dev->uclass_node.next, struct udevice, uclass_node);
272
273 return 0;
274}
275
Simon Glass9670f7d2022-04-24 23:31:00 -0600276int uclass_find_device_by_namelen(enum uclass_id id, const char *name, int len,
277 struct udevice **devp)
Przemyslaw Marczak2ffdf142015-04-15 13:07:22 +0200278{
279 struct uclass *uc;
280 struct udevice *dev;
281 int ret;
282
283 *devp = NULL;
284 if (!name)
285 return -EINVAL;
286 ret = uclass_get(id, &uc);
287 if (ret)
288 return ret;
289
Liviu Dudau6f386222018-09-28 14:12:55 +0100290 uclass_foreach_dev(dev, uc) {
Simon Glass9670f7d2022-04-24 23:31:00 -0600291 if (!strncmp(dev->name, name, len) &&
292 strlen(dev->name) == len) {
Przemyslaw Marczak2ffdf142015-04-15 13:07:22 +0200293 *devp = dev;
294 return 0;
295 }
296 }
297
298 return -ENODEV;
299}
300
Simon Glass9670f7d2022-04-24 23:31:00 -0600301int uclass_find_device_by_name(enum uclass_id id, const char *name,
302 struct udevice **devp)
303{
304 return uclass_find_device_by_namelen(id, name, strlen(name), devp);
305}
306
Simon Glassf66139f2024-10-19 09:21:49 -0600307struct udevice *uclass_try_first_device(enum uclass_id id)
308{
309 struct uclass *uc;
310
311 uc = uclass_find(id);
312 if (!uc || list_empty(&uc->dev_head))
313 return NULL;
314
315 return list_first_entry(&uc->dev_head, struct udevice, uclass_node);
316}
317
Simon Glassb941f562020-12-16 21:20:30 -0700318int uclass_find_next_free_seq(struct uclass *uc)
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100319{
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100320 struct udevice *dev;
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100321 int max = -1;
322
Simon Glassb941f562020-12-16 21:20:30 -0700323 /* If using aliases, start with the highest alias value */
324 if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
325 (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS))
326 max = dev_read_alias_highest_id(uc->uc_drv->name);
327
328 /* Avoid conflict with existing devices */
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100329 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
Simon Glass5e349922020-12-19 10:40:09 -0700330 if (dev->seq_ > max)
331 max = dev->seq_;
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100332 }
Simon Glassb941f562020-12-16 21:20:30 -0700333 /*
334 * At this point, max will be -1 if there are no existing aliases or
335 * devices
336 */
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100337
338 return max + 1;
339}
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100340
Simon Glass07e13382020-12-16 21:20:29 -0700341int uclass_find_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
Simon Glassdb6f0202014-07-23 06:55:12 -0600342{
343 struct uclass *uc;
344 struct udevice *dev;
345 int ret;
346
347 *devp = NULL;
Simon Glass07e13382020-12-16 21:20:29 -0700348 log_debug("%d\n", seq);
349 if (seq == -1)
Simon Glassdb6f0202014-07-23 06:55:12 -0600350 return -ENODEV;
351 ret = uclass_get(id, &uc);
352 if (ret)
353 return ret;
354
Liviu Dudau6f386222018-09-28 14:12:55 +0100355 uclass_foreach_dev(dev, uc) {
Simon Glass5e349922020-12-19 10:40:09 -0700356 log_debug(" - %d '%s'\n", dev->seq_, dev->name);
357 if (dev->seq_ == seq) {
Simon Glassdb6f0202014-07-23 06:55:12 -0600358 *devp = dev;
Kever Yang5f09a2d2019-10-22 15:39:47 +0800359 log_debug(" - found\n");
Simon Glassdb6f0202014-07-23 06:55:12 -0600360 return 0;
361 }
362 }
Kever Yang5f09a2d2019-10-22 15:39:47 +0800363 log_debug(" - not found\n");
Simon Glassdb6f0202014-07-23 06:55:12 -0600364
365 return -ENODEV;
366}
367
Simon Glass96f04442016-01-21 19:43:57 -0700368int uclass_find_device_by_of_offset(enum uclass_id id, int node,
369 struct udevice **devp)
Simon Glassc1464ab2014-07-23 06:55:14 -0600370{
371 struct uclass *uc;
372 struct udevice *dev;
373 int ret;
374
375 *devp = NULL;
376 if (node < 0)
377 return -ENODEV;
378 ret = uclass_get(id, &uc);
379 if (ret)
380 return ret;
381
Liviu Dudau6f386222018-09-28 14:12:55 +0100382 uclass_foreach_dev(dev, uc) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700383 if (dev_of_offset(dev) == node) {
Simon Glassc1464ab2014-07-23 06:55:14 -0600384 *devp = dev;
385 return 0;
386 }
387 }
388
389 return -ENODEV;
390}
391
Simon Glassee145d62017-05-18 20:09:09 -0600392int uclass_find_device_by_ofnode(enum uclass_id id, ofnode node,
393 struct udevice **devp)
394{
395 struct uclass *uc;
396 struct udevice *dev;
397 int ret;
398
Simon Glass5ab533c2018-06-11 13:07:19 -0600399 log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node));
Simon Glassee145d62017-05-18 20:09:09 -0600400 *devp = NULL;
401 if (!ofnode_valid(node))
402 return -ENODEV;
403 ret = uclass_get(id, &uc);
404 if (ret)
405 return ret;
406
Liviu Dudau6f386222018-09-28 14:12:55 +0100407 uclass_foreach_dev(dev, uc) {
Simon Glass5ab533c2018-06-11 13:07:19 -0600408 log(LOGC_DM, LOGL_DEBUG_CONTENT, " - checking %s\n",
409 dev->name);
Simon Glassee145d62017-05-18 20:09:09 -0600410 if (ofnode_equal(dev_ofnode(dev), node)) {
411 *devp = dev;
Simon Glass5ab533c2018-06-11 13:07:19 -0600412 goto done;
Simon Glassee145d62017-05-18 20:09:09 -0600413 }
414 }
Simon Glass5ab533c2018-06-11 13:07:19 -0600415 ret = -ENODEV;
Simon Glassee145d62017-05-18 20:09:09 -0600416
Simon Glass5ab533c2018-06-11 13:07:19 -0600417done:
418 log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n",
419 ofnode_get_name(node), *devp ? (*devp)->name : "(none)", ret);
420 return ret;
Simon Glassee145d62017-05-18 20:09:09 -0600421}
422
Simon Glass3580f6d2021-08-07 07:24:03 -0600423#if CONFIG_IS_ENABLED(OF_REAL)
Rasmus Villemoes8bf32b02023-04-13 17:16:11 +0200424static int uclass_find_device_by_phandle_id(enum uclass_id id,
425 uint find_phandle,
426 struct udevice **devp)
Simon Glass75f00df2015-07-02 18:15:38 -0600427{
428 struct udevice *dev;
429 struct uclass *uc;
Simon Glass75f00df2015-07-02 18:15:38 -0600430 int ret;
431
Simon Glass75f00df2015-07-02 18:15:38 -0600432 ret = uclass_get(id, &uc);
433 if (ret)
434 return ret;
435
Liviu Dudau6f386222018-09-28 14:12:55 +0100436 uclass_foreach_dev(dev, uc) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700437 uint phandle;
438
Simon Glassbfb6e3d2017-05-18 20:09:12 -0600439 phandle = dev_read_phandle(dev);
Simon Glass75f00df2015-07-02 18:15:38 -0600440
441 if (phandle == find_phandle) {
442 *devp = dev;
443 return 0;
444 }
445 }
446
447 return -ENODEV;
448}
Rasmus Villemoes8bf32b02023-04-13 17:16:11 +0200449
450int uclass_find_device_by_phandle(enum uclass_id id, struct udevice *parent,
451 const char *name, struct udevice **devp)
452{
453 int find_phandle;
454
455 *devp = NULL;
456 find_phandle = dev_read_u32_default(parent, name, -1);
457 if (find_phandle <= 0)
458 return -ENOENT;
459
460 return uclass_find_device_by_phandle_id(id, find_phandle, devp);
461}
Simon Glass359c4872015-12-19 19:38:55 -0700462#endif
Simon Glass75f00df2015-07-02 18:15:38 -0600463
Simon Glass32d8ab62016-07-17 15:23:15 -0600464int uclass_get_device_by_driver(enum uclass_id id,
465 const struct driver *find_drv,
466 struct udevice **devp)
467{
468 struct udevice *dev;
469 struct uclass *uc;
470 int ret;
471
472 ret = uclass_get(id, &uc);
473 if (ret)
474 return ret;
475
Liviu Dudau6f386222018-09-28 14:12:55 +0100476 uclass_foreach_dev(dev, uc) {
Simon Glass32d8ab62016-07-17 15:23:15 -0600477 if (dev->driver == find_drv)
478 return uclass_get_device_tail(dev, 0, devp);
479 }
480
481 return -ENODEV;
482}
483
Simon Glass98c9fb02017-04-23 20:10:43 -0600484int uclass_get_device_tail(struct udevice *dev, int ret, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700485{
Simon Glassdd6ab882014-02-26 15:59:18 -0700486 if (ret)
487 return ret;
488
Simon Glass46cc8ba92015-04-24 22:33:07 -0600489 assert(dev);
Simon Glassdd6ab882014-02-26 15:59:18 -0700490 ret = device_probe(dev);
491 if (ret)
492 return ret;
493
494 *devp = dev;
495
496 return 0;
497}
498
Simon Glass4b67cef2014-07-23 06:55:08 -0600499int uclass_get_device(enum uclass_id id, int index, struct udevice **devp)
500{
501 struct udevice *dev;
502 int ret;
503
504 *devp = NULL;
505 ret = uclass_find_device(id, index, &dev);
506 return uclass_get_device_tail(dev, ret, devp);
507}
508
Przemyslaw Marczaka12a1f52015-04-15 13:07:23 +0200509int uclass_get_device_by_name(enum uclass_id id, const char *name,
510 struct udevice **devp)
511{
512 struct udevice *dev;
513 int ret;
514
515 *devp = NULL;
516 ret = uclass_find_device_by_name(id, name, &dev);
517 return uclass_get_device_tail(dev, ret, devp);
518}
519
Simon Glassdb6f0202014-07-23 06:55:12 -0600520int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
521{
522 struct udevice *dev;
523 int ret;
524
525 *devp = NULL;
Simon Glass07e13382020-12-16 21:20:29 -0700526 ret = uclass_find_device_by_seq(id, seq, &dev);
527
Simon Glassdb6f0202014-07-23 06:55:12 -0600528 return uclass_get_device_tail(dev, ret, devp);
529}
530
Simon Glassc1464ab2014-07-23 06:55:14 -0600531int uclass_get_device_by_of_offset(enum uclass_id id, int node,
532 struct udevice **devp)
533{
534 struct udevice *dev;
535 int ret;
536
537 *devp = NULL;
538 ret = uclass_find_device_by_of_offset(id, node, &dev);
539 return uclass_get_device_tail(dev, ret, devp);
540}
541
Simon Glassee145d62017-05-18 20:09:09 -0600542int uclass_get_device_by_ofnode(enum uclass_id id, ofnode node,
543 struct udevice **devp)
544{
545 struct udevice *dev;
546 int ret;
547
Simon Glass5ab533c2018-06-11 13:07:19 -0600548 log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node));
Simon Glassee145d62017-05-18 20:09:09 -0600549 *devp = NULL;
550 ret = uclass_find_device_by_ofnode(id, node, &dev);
Simon Glass5ab533c2018-06-11 13:07:19 -0600551 log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n",
552 ofnode_get_name(node), dev ? dev->name : "(none)", ret);
Simon Glassee145d62017-05-18 20:09:09 -0600553
554 return uclass_get_device_tail(dev, ret, devp);
555}
556
Rasmus Villemoes8bf32b02023-04-13 17:16:11 +0200557#if CONFIG_IS_ENABLED(OF_REAL)
Rasmus Villemoes4b876e32023-04-13 17:17:03 +0200558int uclass_get_device_by_of_path(enum uclass_id id, const char *path,
559 struct udevice **devp)
560{
561 return uclass_get_device_by_ofnode(id, ofnode_path(path), devp);
562}
563
Kever Yang66a0b5a2018-02-09 10:56:23 +0800564int uclass_get_device_by_phandle_id(enum uclass_id id, uint phandle_id,
565 struct udevice **devp)
566{
567 struct udevice *dev;
Kever Yang66a0b5a2018-02-09 10:56:23 +0800568 int ret;
569
570 *devp = NULL;
Rasmus Villemoes8bf32b02023-04-13 17:16:11 +0200571 ret = uclass_find_device_by_phandle_id(id, phandle_id, &dev);
572 return uclass_get_device_tail(dev, ret, devp);
Kever Yang66a0b5a2018-02-09 10:56:23 +0800573}
574
Simon Glass75f00df2015-07-02 18:15:38 -0600575int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent,
576 const char *name, struct udevice **devp)
577{
578 struct udevice *dev;
579 int ret;
580
581 *devp = NULL;
582 ret = uclass_find_device_by_phandle(id, parent, name, &dev);
583 return uclass_get_device_tail(dev, ret, devp);
584}
Simon Glass359c4872015-12-19 19:38:55 -0700585#endif
Simon Glass75f00df2015-07-02 18:15:38 -0600586
Michal Suchanek98e1ada2022-10-12 21:58:09 +0200587/*
588 * Starting from the given device @dev, return pointer to the first device in
589 * the uclass that probes successfully in @devp.
590 */
591static void _uclass_next_device(struct udevice *dev, struct udevice **devp)
592{
593 for (; dev; uclass_find_next_device(&dev)) {
594 if (!device_probe(dev))
595 break;
596 }
597 *devp = dev;
598}
599
600void uclass_first_device(enum uclass_id id, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700601{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200602 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700603 int ret;
604
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200605 ret = uclass_find_first_device(id, &dev);
Michal Suchanek98e1ada2022-10-12 21:58:09 +0200606 _uclass_next_device(dev, devp);
Simon Glassdd6ab882014-02-26 15:59:18 -0700607}
608
Michal Suchanek98e1ada2022-10-12 21:58:09 +0200609void uclass_next_device(struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700610{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200611 struct udevice *dev = *devp;
Simon Glassdd6ab882014-02-26 15:59:18 -0700612
Michal Suchanek98e1ada2022-10-12 21:58:09 +0200613 uclass_find_next_device(&dev);
614 _uclass_next_device(dev, devp);
Simon Glassdd6ab882014-02-26 15:59:18 -0700615}
616
Michal Suchanek93a85dc2022-10-12 21:58:06 +0200617int uclass_first_device_err(enum uclass_id id, struct udevice **devp)
618{
619 int ret;
620
621 ret = uclass_first_device_check(id, devp);
622 if (ret)
623 return ret;
624 else if (!*devp)
625 return -ENODEV;
626
627 return 0;
628}
629
Patrice Chotard4dff1782018-10-24 14:10:15 +0200630int uclass_next_device_err(struct udevice **devp)
631{
632 int ret;
633
Michal Suchanek93a85dc2022-10-12 21:58:06 +0200634 ret = uclass_next_device_check(devp);
Patrice Chotard4dff1782018-10-24 14:10:15 +0200635 if (ret)
636 return ret;
637 else if (!*devp)
638 return -ENODEV;
639
640 return 0;
641}
642
Simon Glass3ff83bc2017-04-23 20:10:45 -0600643int uclass_first_device_check(enum uclass_id id, struct udevice **devp)
644{
645 int ret;
646
647 *devp = NULL;
648 ret = uclass_find_first_device(id, devp);
649 if (ret)
650 return ret;
651 if (!*devp)
652 return 0;
653
654 return device_probe(*devp);
655}
656
657int uclass_next_device_check(struct udevice **devp)
658{
659 int ret;
660
661 ret = uclass_find_next_device(devp);
662 if (ret)
663 return ret;
664 if (!*devp)
665 return 0;
666
667 return device_probe(*devp);
668}
669
Simon Glass51608c92021-12-16 20:59:32 -0700670int uclass_get_count(void)
671{
672 const struct uclass *uc;
673 int count = 0;
674
675 if (gd->dm_root) {
676 list_for_each_entry(uc, gd->uclass_root, sibling_node)
677 count++;
678 }
679
680 return count;
681}
682
Simon Glass35cb2a42020-02-06 09:54:50 -0700683int uclass_first_device_drvdata(enum uclass_id id, ulong driver_data,
684 struct udevice **devp)
685{
686 struct udevice *dev;
687 struct uclass *uc;
688
689 uclass_id_foreach_dev(id, dev, uc) {
690 if (dev_get_driver_data(dev) == driver_data) {
691 *devp = dev;
692
693 return device_probe(dev);
694 }
695 }
696
697 return -ENODEV;
698}
699
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200700int uclass_bind_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700701{
702 struct uclass *uc;
703 int ret;
704
705 uc = dev->uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700706 list_add_tail(&dev->uclass_node, &uc->dev_head);
707
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700708 if (dev->parent) {
709 struct uclass_driver *uc_drv = dev->parent->uclass->uc_drv;
710
711 if (uc_drv->child_post_bind) {
712 ret = uc_drv->child_post_bind(dev);
713 if (ret)
714 goto err;
715 }
716 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700717
718 return 0;
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700719err:
720 /* There is no need to undo the parent's post_bind call */
721 list_del(&dev->uclass_node);
722
723 return ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700724}
725
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900726#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
Simon Glassce15bfb2021-10-23 17:26:05 -0600727int uclass_pre_unbind_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700728{
729 struct uclass *uc;
730 int ret;
731
732 uc = dev->uclass;
733 if (uc->uc_drv->pre_unbind) {
734 ret = uc->uc_drv->pre_unbind(dev);
735 if (ret)
736 return ret;
737 }
738
Simon Glassce15bfb2021-10-23 17:26:05 -0600739 return 0;
740}
741
742int uclass_unbind_device(struct udevice *dev)
743{
Simon Glassdd6ab882014-02-26 15:59:18 -0700744 list_del(&dev->uclass_node);
Simon Glassce15bfb2021-10-23 17:26:05 -0600745
Simon Glassdd6ab882014-02-26 15:59:18 -0700746 return 0;
747}
Simon Glass8914c8a2015-02-27 22:06:31 -0700748#endif
Simon Glassdd6ab882014-02-26 15:59:18 -0700749
Simon Glass9c1f3822015-03-05 12:25:22 -0700750int uclass_pre_probe_device(struct udevice *dev)
Simon Glass5104b982015-01-25 08:27:10 -0700751{
752 struct uclass_driver *uc_drv;
Simon Glass9c1f3822015-03-05 12:25:22 -0700753 int ret;
754
755 uc_drv = dev->uclass->uc_drv;
756 if (uc_drv->pre_probe) {
757 ret = uc_drv->pre_probe(dev);
758 if (ret)
759 return ret;
760 }
Simon Glass5104b982015-01-25 08:27:10 -0700761
762 if (!dev->parent)
763 return 0;
764 uc_drv = dev->parent->uclass->uc_drv;
Simon Glass256d4022019-09-25 08:55:47 -0600765 if (uc_drv->child_pre_probe) {
766 ret = uc_drv->child_pre_probe(dev);
767 if (ret)
768 return ret;
769 }
Simon Glass5104b982015-01-25 08:27:10 -0700770
771 return 0;
772}
773
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200774int uclass_post_probe_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700775{
Bin Meng30569cf2018-10-15 02:20:57 -0700776 struct uclass_driver *uc_drv;
777 int ret;
778
779 if (dev->parent) {
780 uc_drv = dev->parent->uclass->uc_drv;
781 if (uc_drv->child_post_probe) {
782 ret = uc_drv->child_post_probe(dev);
783 if (ret)
784 return ret;
785 }
786 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700787
Bin Meng30569cf2018-10-15 02:20:57 -0700788 uc_drv = dev->uclass->uc_drv;
Simon Glass256d4022019-09-25 08:55:47 -0600789 if (uc_drv->post_probe) {
790 ret = uc_drv->post_probe(dev);
791 if (ret)
792 return ret;
793 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700794
795 return 0;
796}
797
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900798#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200799int uclass_pre_remove_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700800{
Simon Glassdd6ab882014-02-26 15:59:18 -0700801 struct uclass *uc;
802 int ret;
803
804 uc = dev->uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700805 if (uc->uc_drv->pre_remove) {
806 ret = uc->uc_drv->pre_remove(dev);
807 if (ret)
808 return ret;
809 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700810
811 return 0;
812}
Simon Glass8914c8a2015-02-27 22:06:31 -0700813#endif
Jean-Jacques Hiblotdb97c7f2019-07-05 09:33:57 +0200814
Vabhav Sharmabcab47a2020-12-09 10:42:03 +0530815int uclass_probe_all(enum uclass_id id)
816{
817 struct udevice *dev;
Michal Suchanekf5a59022022-10-12 21:57:50 +0200818 int ret, err;
Vabhav Sharmabcab47a2020-12-09 10:42:03 +0530819
Michal Suchanekf5a59022022-10-12 21:57:50 +0200820 err = uclass_first_device_check(id, &dev);
Vabhav Sharmabcab47a2020-12-09 10:42:03 +0530821
822 /* Scanning uclass to probe all devices */
823 while (dev) {
Michal Suchanekf5a59022022-10-12 21:57:50 +0200824 ret = uclass_next_device_check(&dev);
Vabhav Sharmabcab47a2020-12-09 10:42:03 +0530825 if (ret)
Michal Suchanekf5a59022022-10-12 21:57:50 +0200826 err = ret;
Vabhav Sharmabcab47a2020-12-09 10:42:03 +0530827 }
828
Michal Suchanekf5a59022022-10-12 21:57:50 +0200829 return err;
Vabhav Sharmabcab47a2020-12-09 10:42:03 +0530830}
831
Simon Glassdab7e0d2021-10-23 17:26:09 -0600832int uclass_id_count(enum uclass_id id)
833{
834 struct udevice *dev;
835 struct uclass *uc;
836 int count = 0;
837
838 uclass_id_foreach_dev(id, dev, uc)
839 count++;
840
841 return count;
842}
843
Jean-Jacques Hiblotdb97c7f2019-07-05 09:33:57 +0200844UCLASS_DRIVER(nop) = {
845 .id = UCLASS_NOP,
846 .name = "nop",
847};