blob: 7ae0884a75eccc6a0705839b9b03d829673a35e1 [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 Glassb941f562020-12-16 21:20:30 -0700307int uclass_find_next_free_seq(struct uclass *uc)
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100308{
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100309 struct udevice *dev;
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100310 int max = -1;
311
Simon Glassb941f562020-12-16 21:20:30 -0700312 /* If using aliases, start with the highest alias value */
313 if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
314 (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS))
315 max = dev_read_alias_highest_id(uc->uc_drv->name);
316
317 /* Avoid conflict with existing devices */
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100318 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
Simon Glass5e349922020-12-19 10:40:09 -0700319 if (dev->seq_ > max)
320 max = dev->seq_;
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100321 }
Simon Glassb941f562020-12-16 21:20:30 -0700322 /*
323 * At this point, max will be -1 if there are no existing aliases or
324 * devices
325 */
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100326
327 return max + 1;
328}
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100329
Simon Glass07e13382020-12-16 21:20:29 -0700330int uclass_find_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
Simon Glassdb6f0202014-07-23 06:55:12 -0600331{
332 struct uclass *uc;
333 struct udevice *dev;
334 int ret;
335
336 *devp = NULL;
Simon Glass07e13382020-12-16 21:20:29 -0700337 log_debug("%d\n", seq);
338 if (seq == -1)
Simon Glassdb6f0202014-07-23 06:55:12 -0600339 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 Glass5e349922020-12-19 10:40:09 -0700345 log_debug(" - %d '%s'\n", dev->seq_, dev->name);
346 if (dev->seq_ == seq) {
Simon Glassdb6f0202014-07-23 06:55:12 -0600347 *devp = dev;
Kever Yang5f09a2d2019-10-22 15:39:47 +0800348 log_debug(" - found\n");
Simon Glassdb6f0202014-07-23 06:55:12 -0600349 return 0;
350 }
351 }
Kever Yang5f09a2d2019-10-22 15:39:47 +0800352 log_debug(" - not found\n");
Simon Glassdb6f0202014-07-23 06:55:12 -0600353
354 return -ENODEV;
355}
356
Simon Glass96f04442016-01-21 19:43:57 -0700357int uclass_find_device_by_of_offset(enum uclass_id id, int node,
358 struct udevice **devp)
Simon Glassc1464ab2014-07-23 06:55:14 -0600359{
360 struct uclass *uc;
361 struct udevice *dev;
362 int ret;
363
364 *devp = NULL;
365 if (node < 0)
366 return -ENODEV;
367 ret = uclass_get(id, &uc);
368 if (ret)
369 return ret;
370
Liviu Dudau6f386222018-09-28 14:12:55 +0100371 uclass_foreach_dev(dev, uc) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700372 if (dev_of_offset(dev) == node) {
Simon Glassc1464ab2014-07-23 06:55:14 -0600373 *devp = dev;
374 return 0;
375 }
376 }
377
378 return -ENODEV;
379}
380
Simon Glassee145d62017-05-18 20:09:09 -0600381int uclass_find_device_by_ofnode(enum uclass_id id, ofnode node,
382 struct udevice **devp)
383{
384 struct uclass *uc;
385 struct udevice *dev;
386 int ret;
387
Simon Glass5ab533c2018-06-11 13:07:19 -0600388 log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node));
Simon Glassee145d62017-05-18 20:09:09 -0600389 *devp = NULL;
390 if (!ofnode_valid(node))
391 return -ENODEV;
392 ret = uclass_get(id, &uc);
393 if (ret)
394 return ret;
395
Liviu Dudau6f386222018-09-28 14:12:55 +0100396 uclass_foreach_dev(dev, uc) {
Simon Glass5ab533c2018-06-11 13:07:19 -0600397 log(LOGC_DM, LOGL_DEBUG_CONTENT, " - checking %s\n",
398 dev->name);
Simon Glassee145d62017-05-18 20:09:09 -0600399 if (ofnode_equal(dev_ofnode(dev), node)) {
400 *devp = dev;
Simon Glass5ab533c2018-06-11 13:07:19 -0600401 goto done;
Simon Glassee145d62017-05-18 20:09:09 -0600402 }
403 }
Simon Glass5ab533c2018-06-11 13:07:19 -0600404 ret = -ENODEV;
Simon Glassee145d62017-05-18 20:09:09 -0600405
Simon Glass5ab533c2018-06-11 13:07:19 -0600406done:
407 log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n",
408 ofnode_get_name(node), *devp ? (*devp)->name : "(none)", ret);
409 return ret;
Simon Glassee145d62017-05-18 20:09:09 -0600410}
411
Simon Glass3580f6d2021-08-07 07:24:03 -0600412#if CONFIG_IS_ENABLED(OF_REAL)
Rasmus Villemoes8bf32b02023-04-13 17:16:11 +0200413static int uclass_find_device_by_phandle_id(enum uclass_id id,
414 uint find_phandle,
415 struct udevice **devp)
Simon Glass75f00df2015-07-02 18:15:38 -0600416{
417 struct udevice *dev;
418 struct uclass *uc;
Simon Glass75f00df2015-07-02 18:15:38 -0600419 int ret;
420
Simon Glass75f00df2015-07-02 18:15:38 -0600421 ret = uclass_get(id, &uc);
422 if (ret)
423 return ret;
424
Liviu Dudau6f386222018-09-28 14:12:55 +0100425 uclass_foreach_dev(dev, uc) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700426 uint phandle;
427
Simon Glassbfb6e3d2017-05-18 20:09:12 -0600428 phandle = dev_read_phandle(dev);
Simon Glass75f00df2015-07-02 18:15:38 -0600429
430 if (phandle == find_phandle) {
431 *devp = dev;
432 return 0;
433 }
434 }
435
436 return -ENODEV;
437}
Rasmus Villemoes8bf32b02023-04-13 17:16:11 +0200438
439int uclass_find_device_by_phandle(enum uclass_id id, struct udevice *parent,
440 const char *name, struct udevice **devp)
441{
442 int find_phandle;
443
444 *devp = NULL;
445 find_phandle = dev_read_u32_default(parent, name, -1);
446 if (find_phandle <= 0)
447 return -ENOENT;
448
449 return uclass_find_device_by_phandle_id(id, find_phandle, devp);
450}
Simon Glass359c4872015-12-19 19:38:55 -0700451#endif
Simon Glass75f00df2015-07-02 18:15:38 -0600452
Simon Glass32d8ab62016-07-17 15:23:15 -0600453int uclass_get_device_by_driver(enum uclass_id id,
454 const struct driver *find_drv,
455 struct udevice **devp)
456{
457 struct udevice *dev;
458 struct uclass *uc;
459 int ret;
460
461 ret = uclass_get(id, &uc);
462 if (ret)
463 return ret;
464
Liviu Dudau6f386222018-09-28 14:12:55 +0100465 uclass_foreach_dev(dev, uc) {
Simon Glass32d8ab62016-07-17 15:23:15 -0600466 if (dev->driver == find_drv)
467 return uclass_get_device_tail(dev, 0, devp);
468 }
469
470 return -ENODEV;
471}
472
Simon Glass98c9fb02017-04-23 20:10:43 -0600473int uclass_get_device_tail(struct udevice *dev, int ret, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700474{
Simon Glassdd6ab882014-02-26 15:59:18 -0700475 if (ret)
476 return ret;
477
Simon Glass46cc8ba92015-04-24 22:33:07 -0600478 assert(dev);
Simon Glassdd6ab882014-02-26 15:59:18 -0700479 ret = device_probe(dev);
480 if (ret)
481 return ret;
482
483 *devp = dev;
484
485 return 0;
486}
487
Simon Glass4b67cef2014-07-23 06:55:08 -0600488int uclass_get_device(enum uclass_id id, int index, struct udevice **devp)
489{
490 struct udevice *dev;
491 int ret;
492
493 *devp = NULL;
494 ret = uclass_find_device(id, index, &dev);
495 return uclass_get_device_tail(dev, ret, devp);
496}
497
Przemyslaw Marczaka12a1f52015-04-15 13:07:23 +0200498int uclass_get_device_by_name(enum uclass_id id, const char *name,
499 struct udevice **devp)
500{
501 struct udevice *dev;
502 int ret;
503
504 *devp = NULL;
505 ret = uclass_find_device_by_name(id, name, &dev);
506 return uclass_get_device_tail(dev, ret, devp);
507}
508
Simon Glassdb6f0202014-07-23 06:55:12 -0600509int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
510{
511 struct udevice *dev;
512 int ret;
513
514 *devp = NULL;
Simon Glass07e13382020-12-16 21:20:29 -0700515 ret = uclass_find_device_by_seq(id, seq, &dev);
516
Simon Glassdb6f0202014-07-23 06:55:12 -0600517 return uclass_get_device_tail(dev, ret, devp);
518}
519
Simon Glassc1464ab2014-07-23 06:55:14 -0600520int uclass_get_device_by_of_offset(enum uclass_id id, int node,
521 struct udevice **devp)
522{
523 struct udevice *dev;
524 int ret;
525
526 *devp = NULL;
527 ret = uclass_find_device_by_of_offset(id, node, &dev);
528 return uclass_get_device_tail(dev, ret, devp);
529}
530
Simon Glassee145d62017-05-18 20:09:09 -0600531int uclass_get_device_by_ofnode(enum uclass_id id, ofnode node,
532 struct udevice **devp)
533{
534 struct udevice *dev;
535 int ret;
536
Simon Glass5ab533c2018-06-11 13:07:19 -0600537 log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node));
Simon Glassee145d62017-05-18 20:09:09 -0600538 *devp = NULL;
539 ret = uclass_find_device_by_ofnode(id, node, &dev);
Simon Glass5ab533c2018-06-11 13:07:19 -0600540 log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n",
541 ofnode_get_name(node), dev ? dev->name : "(none)", ret);
Simon Glassee145d62017-05-18 20:09:09 -0600542
543 return uclass_get_device_tail(dev, ret, devp);
544}
545
Rasmus Villemoes8bf32b02023-04-13 17:16:11 +0200546#if CONFIG_IS_ENABLED(OF_REAL)
Rasmus Villemoes4b876e32023-04-13 17:17:03 +0200547int uclass_get_device_by_of_path(enum uclass_id id, const char *path,
548 struct udevice **devp)
549{
550 return uclass_get_device_by_ofnode(id, ofnode_path(path), devp);
551}
552
Kever Yang66a0b5a2018-02-09 10:56:23 +0800553int uclass_get_device_by_phandle_id(enum uclass_id id, uint phandle_id,
554 struct udevice **devp)
555{
556 struct udevice *dev;
Kever Yang66a0b5a2018-02-09 10:56:23 +0800557 int ret;
558
559 *devp = NULL;
Rasmus Villemoes8bf32b02023-04-13 17:16:11 +0200560 ret = uclass_find_device_by_phandle_id(id, phandle_id, &dev);
561 return uclass_get_device_tail(dev, ret, devp);
Kever Yang66a0b5a2018-02-09 10:56:23 +0800562}
563
Simon Glass75f00df2015-07-02 18:15:38 -0600564int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent,
565 const char *name, struct udevice **devp)
566{
567 struct udevice *dev;
568 int ret;
569
570 *devp = NULL;
571 ret = uclass_find_device_by_phandle(id, parent, name, &dev);
572 return uclass_get_device_tail(dev, ret, devp);
573}
Simon Glass359c4872015-12-19 19:38:55 -0700574#endif
Simon Glass75f00df2015-07-02 18:15:38 -0600575
Michal Suchanek98e1ada2022-10-12 21:58:09 +0200576/*
577 * Starting from the given device @dev, return pointer to the first device in
578 * the uclass that probes successfully in @devp.
579 */
580static void _uclass_next_device(struct udevice *dev, struct udevice **devp)
581{
582 for (; dev; uclass_find_next_device(&dev)) {
583 if (!device_probe(dev))
584 break;
585 }
586 *devp = dev;
587}
588
589void uclass_first_device(enum uclass_id id, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700590{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200591 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700592 int ret;
593
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200594 ret = uclass_find_first_device(id, &dev);
Michal Suchanek98e1ada2022-10-12 21:58:09 +0200595 _uclass_next_device(dev, devp);
Simon Glassdd6ab882014-02-26 15:59:18 -0700596}
597
Michal Suchanek98e1ada2022-10-12 21:58:09 +0200598void uclass_next_device(struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700599{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200600 struct udevice *dev = *devp;
Simon Glassdd6ab882014-02-26 15:59:18 -0700601
Michal Suchanek98e1ada2022-10-12 21:58:09 +0200602 uclass_find_next_device(&dev);
603 _uclass_next_device(dev, devp);
Simon Glassdd6ab882014-02-26 15:59:18 -0700604}
605
Michal Suchanek93a85dc2022-10-12 21:58:06 +0200606int uclass_first_device_err(enum uclass_id id, struct udevice **devp)
607{
608 int ret;
609
610 ret = uclass_first_device_check(id, devp);
611 if (ret)
612 return ret;
613 else if (!*devp)
614 return -ENODEV;
615
616 return 0;
617}
618
Patrice Chotard4dff1782018-10-24 14:10:15 +0200619int uclass_next_device_err(struct udevice **devp)
620{
621 int ret;
622
Michal Suchanek93a85dc2022-10-12 21:58:06 +0200623 ret = uclass_next_device_check(devp);
Patrice Chotard4dff1782018-10-24 14:10:15 +0200624 if (ret)
625 return ret;
626 else if (!*devp)
627 return -ENODEV;
628
629 return 0;
630}
631
Simon Glass3ff83bc2017-04-23 20:10:45 -0600632int uclass_first_device_check(enum uclass_id id, struct udevice **devp)
633{
634 int ret;
635
636 *devp = NULL;
637 ret = uclass_find_first_device(id, devp);
638 if (ret)
639 return ret;
640 if (!*devp)
641 return 0;
642
643 return device_probe(*devp);
644}
645
646int uclass_next_device_check(struct udevice **devp)
647{
648 int ret;
649
650 ret = uclass_find_next_device(devp);
651 if (ret)
652 return ret;
653 if (!*devp)
654 return 0;
655
656 return device_probe(*devp);
657}
658
Simon Glass51608c92021-12-16 20:59:32 -0700659int uclass_get_count(void)
660{
661 const struct uclass *uc;
662 int count = 0;
663
664 if (gd->dm_root) {
665 list_for_each_entry(uc, gd->uclass_root, sibling_node)
666 count++;
667 }
668
669 return count;
670}
671
Simon Glass35cb2a42020-02-06 09:54:50 -0700672int uclass_first_device_drvdata(enum uclass_id id, ulong driver_data,
673 struct udevice **devp)
674{
675 struct udevice *dev;
676 struct uclass *uc;
677
678 uclass_id_foreach_dev(id, dev, uc) {
679 if (dev_get_driver_data(dev) == driver_data) {
680 *devp = dev;
681
682 return device_probe(dev);
683 }
684 }
685
686 return -ENODEV;
687}
688
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200689int uclass_bind_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700690{
691 struct uclass *uc;
692 int ret;
693
694 uc = dev->uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700695 list_add_tail(&dev->uclass_node, &uc->dev_head);
696
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700697 if (dev->parent) {
698 struct uclass_driver *uc_drv = dev->parent->uclass->uc_drv;
699
700 if (uc_drv->child_post_bind) {
701 ret = uc_drv->child_post_bind(dev);
702 if (ret)
703 goto err;
704 }
705 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700706
707 return 0;
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700708err:
709 /* There is no need to undo the parent's post_bind call */
710 list_del(&dev->uclass_node);
711
712 return ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700713}
714
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900715#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
Simon Glassce15bfb2021-10-23 17:26:05 -0600716int uclass_pre_unbind_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700717{
718 struct uclass *uc;
719 int ret;
720
721 uc = dev->uclass;
722 if (uc->uc_drv->pre_unbind) {
723 ret = uc->uc_drv->pre_unbind(dev);
724 if (ret)
725 return ret;
726 }
727
Simon Glassce15bfb2021-10-23 17:26:05 -0600728 return 0;
729}
730
731int uclass_unbind_device(struct udevice *dev)
732{
Simon Glassdd6ab882014-02-26 15:59:18 -0700733 list_del(&dev->uclass_node);
Simon Glassce15bfb2021-10-23 17:26:05 -0600734
Simon Glassdd6ab882014-02-26 15:59:18 -0700735 return 0;
736}
Simon Glass8914c8a2015-02-27 22:06:31 -0700737#endif
Simon Glassdd6ab882014-02-26 15:59:18 -0700738
Simon Glass9c1f3822015-03-05 12:25:22 -0700739int uclass_pre_probe_device(struct udevice *dev)
Simon Glass5104b982015-01-25 08:27:10 -0700740{
741 struct uclass_driver *uc_drv;
Simon Glass9c1f3822015-03-05 12:25:22 -0700742 int ret;
743
744 uc_drv = dev->uclass->uc_drv;
745 if (uc_drv->pre_probe) {
746 ret = uc_drv->pre_probe(dev);
747 if (ret)
748 return ret;
749 }
Simon Glass5104b982015-01-25 08:27:10 -0700750
751 if (!dev->parent)
752 return 0;
753 uc_drv = dev->parent->uclass->uc_drv;
Simon Glass256d4022019-09-25 08:55:47 -0600754 if (uc_drv->child_pre_probe) {
755 ret = uc_drv->child_pre_probe(dev);
756 if (ret)
757 return ret;
758 }
Simon Glass5104b982015-01-25 08:27:10 -0700759
760 return 0;
761}
762
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200763int uclass_post_probe_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700764{
Bin Meng30569cf2018-10-15 02:20:57 -0700765 struct uclass_driver *uc_drv;
766 int ret;
767
768 if (dev->parent) {
769 uc_drv = dev->parent->uclass->uc_drv;
770 if (uc_drv->child_post_probe) {
771 ret = uc_drv->child_post_probe(dev);
772 if (ret)
773 return ret;
774 }
775 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700776
Bin Meng30569cf2018-10-15 02:20:57 -0700777 uc_drv = dev->uclass->uc_drv;
Simon Glass256d4022019-09-25 08:55:47 -0600778 if (uc_drv->post_probe) {
779 ret = uc_drv->post_probe(dev);
780 if (ret)
781 return ret;
782 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700783
784 return 0;
785}
786
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900787#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200788int uclass_pre_remove_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700789{
Simon Glassdd6ab882014-02-26 15:59:18 -0700790 struct uclass *uc;
791 int ret;
792
793 uc = dev->uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700794 if (uc->uc_drv->pre_remove) {
795 ret = uc->uc_drv->pre_remove(dev);
796 if (ret)
797 return ret;
798 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700799
800 return 0;
801}
Simon Glass8914c8a2015-02-27 22:06:31 -0700802#endif
Jean-Jacques Hiblotdb97c7f2019-07-05 09:33:57 +0200803
Vabhav Sharmabcab47a2020-12-09 10:42:03 +0530804int uclass_probe_all(enum uclass_id id)
805{
806 struct udevice *dev;
Michal Suchanekf5a59022022-10-12 21:57:50 +0200807 int ret, err;
Vabhav Sharmabcab47a2020-12-09 10:42:03 +0530808
Michal Suchanekf5a59022022-10-12 21:57:50 +0200809 err = uclass_first_device_check(id, &dev);
Vabhav Sharmabcab47a2020-12-09 10:42:03 +0530810
811 /* Scanning uclass to probe all devices */
812 while (dev) {
Michal Suchanekf5a59022022-10-12 21:57:50 +0200813 ret = uclass_next_device_check(&dev);
Vabhav Sharmabcab47a2020-12-09 10:42:03 +0530814 if (ret)
Michal Suchanekf5a59022022-10-12 21:57:50 +0200815 err = ret;
Vabhav Sharmabcab47a2020-12-09 10:42:03 +0530816 }
817
Michal Suchanekf5a59022022-10-12 21:57:50 +0200818 return err;
Vabhav Sharmabcab47a2020-12-09 10:42:03 +0530819}
820
Simon Glassdab7e0d2021-10-23 17:26:09 -0600821int uclass_id_count(enum uclass_id id)
822{
823 struct udevice *dev;
824 struct uclass *uc;
825 int count = 0;
826
827 uclass_id_foreach_dev(id, dev, uc)
828 count++;
829
830 return count;
831}
832
Jean-Jacques Hiblotdb97c7f2019-07-05 09:33:57 +0200833UCLASS_DRIVER(nop) = {
834 .id = UCLASS_NOP,
835 .name = "nop",
836};