blob: 2fede896bfb5f19b16772d4602b6e20601301ac4 [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>
Simon Glass3ba929a2020-10-30 21:38:53 -060016#include <asm/global_data.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070017#include <dm/device.h>
18#include <dm/device-internal.h>
19#include <dm/lists.h>
20#include <dm/uclass.h>
21#include <dm/uclass-internal.h>
22#include <dm/util.h>
23
24DECLARE_GLOBAL_DATA_PTR;
25
26struct uclass *uclass_find(enum uclass_id key)
27{
28 struct uclass *uc;
29
Simon Glassde708672014-07-23 06:55:15 -060030 if (!gd->dm_root)
31 return NULL;
Simon Glassdd6ab882014-02-26 15:59:18 -070032 /*
33 * TODO(sjg@chromium.org): Optimise this, perhaps moving the found
34 * node to the start of the list, or creating a linear array mapping
35 * id to node.
36 */
Simon Glass784cd9e2020-12-19 10:40:17 -070037 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
Simon Glassdd6ab882014-02-26 15:59:18 -070038 if (uc->uc_drv->id == key)
39 return uc;
40 }
41
42 return NULL;
43}
44
45/**
46 * uclass_add() - Create new uclass in list
47 * @id: Id number to create
48 * @ucp: Returns pointer to uclass, or NULL on error
49 * @return 0 on success, -ve on error
50 *
51 * The new uclass is added to the list. There must be only one uclass for
52 * each id.
53 */
54static int uclass_add(enum uclass_id id, struct uclass **ucp)
55{
56 struct uclass_driver *uc_drv;
57 struct uclass *uc;
58 int ret;
59
60 *ucp = NULL;
61 uc_drv = lists_uclass_lookup(id);
62 if (!uc_drv) {
Masahiro Yamada1d06ced2015-07-07 18:51:32 +090063 debug("Cannot find uclass for id %d: please add the UCLASS_DRIVER() declaration for this UCLASS_... id\n",
64 id);
Simon Glass43313de2015-08-30 16:55:16 -060065 /*
66 * Use a strange error to make this case easier to find. When
67 * a uclass is not available it can prevent driver model from
68 * starting up and this failure is otherwise hard to debug.
69 */
70 return -EPFNOSUPPORT;
Simon Glassdd6ab882014-02-26 15:59:18 -070071 }
Simon Glassdd6ab882014-02-26 15:59:18 -070072 uc = calloc(1, sizeof(*uc));
73 if (!uc)
74 return -ENOMEM;
Simon Glass8a2b47f2020-12-03 16:55:17 -070075 if (uc_drv->priv_auto) {
Simon Glass2543b162020-12-22 19:30:29 -070076 void *ptr;
77
78 ptr = calloc(1, uc_drv->priv_auto);
79 if (!ptr) {
Simon Glassdd6ab882014-02-26 15:59:18 -070080 ret = -ENOMEM;
81 goto fail_mem;
82 }
Simon Glass2543b162020-12-22 19:30:29 -070083 uclass_set_priv(uc, ptr);
Simon Glassdd6ab882014-02-26 15:59:18 -070084 }
85 uc->uc_drv = uc_drv;
86 INIT_LIST_HEAD(&uc->sibling_node);
87 INIT_LIST_HEAD(&uc->dev_head);
Simon Glass784cd9e2020-12-19 10:40:17 -070088 list_add(&uc->sibling_node, DM_UCLASS_ROOT_NON_CONST);
Simon Glassdd6ab882014-02-26 15:59:18 -070089
90 if (uc_drv->init) {
91 ret = uc_drv->init(uc);
92 if (ret)
93 goto fail;
94 }
95
96 *ucp = uc;
97
98 return 0;
99fail:
Simon Glass8a2b47f2020-12-03 16:55:17 -0700100 if (uc_drv->priv_auto) {
Simon Glass2543b162020-12-22 19:30:29 -0700101 free(uclass_get_priv(uc));
102 uclass_set_priv(uc, NULL);
Simon Glassdd6ab882014-02-26 15:59:18 -0700103 }
104 list_del(&uc->sibling_node);
105fail_mem:
106 free(uc);
107
108 return ret;
109}
110
111int uclass_destroy(struct uclass *uc)
112{
113 struct uclass_driver *uc_drv;
Simon Glassf33663d2015-04-19 07:20:58 -0600114 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700115 int ret;
116
Simon Glassf33663d2015-04-19 07:20:58 -0600117 /*
118 * We cannot use list_for_each_entry_safe() here. If a device in this
119 * uclass has a child device also in this uclass, it will be also be
120 * unbound (by the recursion in the call to device_unbind() below).
121 * We can loop until the list is empty.
122 */
123 while (!list_empty(&uc->dev_head)) {
124 dev = list_first_entry(&uc->dev_head, struct udevice,
125 uclass_node);
Simon Glass42697f52020-03-28 14:03:48 -0600126 ret = device_remove(dev, DM_REMOVE_NORMAL | DM_REMOVE_NO_PD);
Simon Glassdd6ab882014-02-26 15:59:18 -0700127 if (ret)
Simon Glass5f3c9e02020-03-28 14:03:47 -0600128 return log_msg_ret("remove", ret);
Simon Glassdd6ab882014-02-26 15:59:18 -0700129 ret = device_unbind(dev);
130 if (ret)
Simon Glass5f3c9e02020-03-28 14:03:47 -0600131 return log_msg_ret("unbind", ret);
Simon Glassdd6ab882014-02-26 15:59:18 -0700132 }
133
134 uc_drv = uc->uc_drv;
135 if (uc_drv->destroy)
136 uc_drv->destroy(uc);
137 list_del(&uc->sibling_node);
Simon Glass8a2b47f2020-12-03 16:55:17 -0700138 if (uc_drv->priv_auto)
Simon Glass2543b162020-12-22 19:30:29 -0700139 free(uclass_get_priv(uc));
Simon Glassdd6ab882014-02-26 15:59:18 -0700140 free(uc);
141
142 return 0;
143}
144
145int uclass_get(enum uclass_id id, struct uclass **ucp)
146{
147 struct uclass *uc;
148
Simon Glass8e9eacf2021-08-01 12:05:23 -0600149 /* Immediately fail if driver model is not set up */
150 if (!gd->uclass_root)
151 return -EDEADLK;
Simon Glassdd6ab882014-02-26 15:59:18 -0700152 *ucp = NULL;
153 uc = uclass_find(id);
Simon Glass05693e92021-03-15 17:25:18 +1300154 if (!uc) {
155 if (CONFIG_IS_ENABLED(OF_PLATDATA_INST))
156 return -ENOENT;
Simon Glassdd6ab882014-02-26 15:59:18 -0700157 return uclass_add(id, ucp);
Simon Glass05693e92021-03-15 17:25:18 +1300158 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700159 *ucp = uc;
160
161 return 0;
162}
163
Simon Glassd19d0732016-10-05 20:42:13 -0600164const char *uclass_get_name(enum uclass_id id)
165{
166 struct uclass *uc;
167
168 if (uclass_get(id, &uc))
169 return NULL;
170 return uc->uc_drv->name;
171}
172
Simon Glass96404c22020-12-22 19:30:26 -0700173void *uclass_get_priv(const struct uclass *uc)
174{
Simon Glass541f62c2020-12-22 19:30:30 -0700175 return uc->priv_;
Simon Glass96404c22020-12-22 19:30:26 -0700176}
177
178void uclass_set_priv(struct uclass *uc, void *priv)
179{
Simon Glass541f62c2020-12-22 19:30:30 -0700180 uc->priv_ = priv;
Simon Glass96404c22020-12-22 19:30:26 -0700181}
182
Simon Glass70e35b42017-12-28 13:14:15 -0700183enum uclass_id uclass_get_by_name(const char *name)
184{
185 int i;
186
187 for (i = 0; i < UCLASS_COUNT; i++) {
188 struct uclass_driver *uc_drv = lists_uclass_lookup(i);
189
190 if (uc_drv && !strcmp(uc_drv->name, name))
191 return i;
192 }
193
194 return UCLASS_INVALID;
195}
196
Jean-Jacques Hiblotc4f02482018-08-09 16:17:42 +0200197int dev_get_uclass_index(struct udevice *dev, struct uclass **ucp)
198{
199 struct udevice *iter;
200 struct uclass *uc = dev->uclass;
201 int i = 0;
202
203 if (list_empty(&uc->dev_head))
204 return -ENODEV;
205
Liviu Dudau6f386222018-09-28 14:12:55 +0100206 uclass_foreach_dev(iter, uc) {
Jean-Jacques Hiblotc4f02482018-08-09 16:17:42 +0200207 if (iter == dev) {
208 if (ucp)
209 *ucp = uc;
210 return i;
211 }
212 i++;
213 }
214
215 return -ENODEV;
216}
217
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200218int uclass_find_device(enum uclass_id id, int index, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700219{
220 struct uclass *uc;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200221 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700222 int ret;
223
224 *devp = NULL;
225 ret = uclass_get(id, &uc);
226 if (ret)
227 return ret;
Simon Glass46766df2015-07-31 09:31:19 -0600228 if (list_empty(&uc->dev_head))
229 return -ENODEV;
Simon Glassdd6ab882014-02-26 15:59:18 -0700230
Liviu Dudau6f386222018-09-28 14:12:55 +0100231 uclass_foreach_dev(dev, uc) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700232 if (!index--) {
233 *devp = dev;
234 return 0;
235 }
236 }
237
238 return -ENODEV;
239}
240
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200241int uclass_find_first_device(enum uclass_id id, struct udevice **devp)
242{
243 struct uclass *uc;
244 int ret;
245
246 *devp = NULL;
247 ret = uclass_get(id, &uc);
248 if (ret)
249 return ret;
250 if (list_empty(&uc->dev_head))
Simon Glass0bb44272019-09-25 08:55:55 -0600251 return 0;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200252
253 *devp = list_first_entry(&uc->dev_head, struct udevice, uclass_node);
254
255 return 0;
256}
257
258int uclass_find_next_device(struct udevice **devp)
259{
260 struct udevice *dev = *devp;
261
262 *devp = NULL;
263 if (list_is_last(&dev->uclass_node, &dev->uclass->dev_head))
264 return 0;
265
266 *devp = list_entry(dev->uclass_node.next, struct udevice, uclass_node);
267
268 return 0;
269}
270
Przemyslaw Marczak2ffdf142015-04-15 13:07:22 +0200271int uclass_find_device_by_name(enum uclass_id id, const char *name,
272 struct udevice **devp)
273{
274 struct uclass *uc;
275 struct udevice *dev;
276 int ret;
277
278 *devp = NULL;
279 if (!name)
280 return -EINVAL;
281 ret = uclass_get(id, &uc);
282 if (ret)
283 return ret;
284
Liviu Dudau6f386222018-09-28 14:12:55 +0100285 uclass_foreach_dev(dev, uc) {
Peng Fan26d68b22019-05-22 07:08:14 +0000286 if (!strcmp(dev->name, name)) {
Przemyslaw Marczak2ffdf142015-04-15 13:07:22 +0200287 *devp = dev;
288 return 0;
289 }
290 }
291
292 return -ENODEV;
293}
294
Simon Glassb941f562020-12-16 21:20:30 -0700295int uclass_find_next_free_seq(struct uclass *uc)
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100296{
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100297 struct udevice *dev;
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100298 int max = -1;
299
Simon Glassb941f562020-12-16 21:20:30 -0700300 /* If using aliases, start with the highest alias value */
301 if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
302 (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS))
303 max = dev_read_alias_highest_id(uc->uc_drv->name);
304
305 /* Avoid conflict with existing devices */
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100306 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
Simon Glass5e349922020-12-19 10:40:09 -0700307 if (dev->seq_ > max)
308 max = dev->seq_;
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100309 }
Simon Glassb941f562020-12-16 21:20:30 -0700310 /*
311 * At this point, max will be -1 if there are no existing aliases or
312 * devices
313 */
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100314
315 return max + 1;
316}
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100317
Simon Glass07e13382020-12-16 21:20:29 -0700318int uclass_find_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
Simon Glassdb6f0202014-07-23 06:55:12 -0600319{
320 struct uclass *uc;
321 struct udevice *dev;
322 int ret;
323
324 *devp = NULL;
Simon Glass07e13382020-12-16 21:20:29 -0700325 log_debug("%d\n", seq);
326 if (seq == -1)
Simon Glassdb6f0202014-07-23 06:55:12 -0600327 return -ENODEV;
328 ret = uclass_get(id, &uc);
329 if (ret)
330 return ret;
331
Liviu Dudau6f386222018-09-28 14:12:55 +0100332 uclass_foreach_dev(dev, uc) {
Simon Glass5e349922020-12-19 10:40:09 -0700333 log_debug(" - %d '%s'\n", dev->seq_, dev->name);
334 if (dev->seq_ == seq) {
Simon Glassdb6f0202014-07-23 06:55:12 -0600335 *devp = dev;
Kever Yang5f09a2d2019-10-22 15:39:47 +0800336 log_debug(" - found\n");
Simon Glassdb6f0202014-07-23 06:55:12 -0600337 return 0;
338 }
339 }
Kever Yang5f09a2d2019-10-22 15:39:47 +0800340 log_debug(" - not found\n");
Simon Glassdb6f0202014-07-23 06:55:12 -0600341
342 return -ENODEV;
343}
344
Simon Glass96f04442016-01-21 19:43:57 -0700345int uclass_find_device_by_of_offset(enum uclass_id id, int node,
346 struct udevice **devp)
Simon Glassc1464ab2014-07-23 06:55:14 -0600347{
348 struct uclass *uc;
349 struct udevice *dev;
350 int ret;
351
352 *devp = NULL;
353 if (node < 0)
354 return -ENODEV;
355 ret = uclass_get(id, &uc);
356 if (ret)
357 return ret;
358
Liviu Dudau6f386222018-09-28 14:12:55 +0100359 uclass_foreach_dev(dev, uc) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700360 if (dev_of_offset(dev) == node) {
Simon Glassc1464ab2014-07-23 06:55:14 -0600361 *devp = dev;
362 return 0;
363 }
364 }
365
366 return -ENODEV;
367}
368
Simon Glassee145d62017-05-18 20:09:09 -0600369int uclass_find_device_by_ofnode(enum uclass_id id, ofnode node,
370 struct udevice **devp)
371{
372 struct uclass *uc;
373 struct udevice *dev;
374 int ret;
375
Simon Glass5ab533c2018-06-11 13:07:19 -0600376 log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node));
Simon Glassee145d62017-05-18 20:09:09 -0600377 *devp = NULL;
378 if (!ofnode_valid(node))
379 return -ENODEV;
380 ret = uclass_get(id, &uc);
381 if (ret)
382 return ret;
383
Liviu Dudau6f386222018-09-28 14:12:55 +0100384 uclass_foreach_dev(dev, uc) {
Simon Glass5ab533c2018-06-11 13:07:19 -0600385 log(LOGC_DM, LOGL_DEBUG_CONTENT, " - checking %s\n",
386 dev->name);
Simon Glassee145d62017-05-18 20:09:09 -0600387 if (ofnode_equal(dev_ofnode(dev), node)) {
388 *devp = dev;
Simon Glass5ab533c2018-06-11 13:07:19 -0600389 goto done;
Simon Glassee145d62017-05-18 20:09:09 -0600390 }
391 }
Simon Glass5ab533c2018-06-11 13:07:19 -0600392 ret = -ENODEV;
Simon Glassee145d62017-05-18 20:09:09 -0600393
Simon Glass5ab533c2018-06-11 13:07:19 -0600394done:
395 log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n",
396 ofnode_get_name(node), *devp ? (*devp)->name : "(none)", ret);
397 return ret;
Simon Glassee145d62017-05-18 20:09:09 -0600398}
399
Simon Glass3580f6d2021-08-07 07:24:03 -0600400#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glassdc3efdc2018-11-18 08:14:30 -0700401int uclass_find_device_by_phandle(enum uclass_id id, struct udevice *parent,
402 const char *name, struct udevice **devp)
Simon Glass75f00df2015-07-02 18:15:38 -0600403{
404 struct udevice *dev;
405 struct uclass *uc;
406 int find_phandle;
407 int ret;
408
409 *devp = NULL;
Simon Glassbfb6e3d2017-05-18 20:09:12 -0600410 find_phandle = dev_read_u32_default(parent, name, -1);
Simon Glass75f00df2015-07-02 18:15:38 -0600411 if (find_phandle <= 0)
412 return -ENOENT;
413 ret = uclass_get(id, &uc);
414 if (ret)
415 return ret;
416
Liviu Dudau6f386222018-09-28 14:12:55 +0100417 uclass_foreach_dev(dev, uc) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700418 uint phandle;
419
Simon Glassbfb6e3d2017-05-18 20:09:12 -0600420 phandle = dev_read_phandle(dev);
Simon Glass75f00df2015-07-02 18:15:38 -0600421
422 if (phandle == find_phandle) {
423 *devp = dev;
424 return 0;
425 }
426 }
427
428 return -ENODEV;
429}
Simon Glass359c4872015-12-19 19:38:55 -0700430#endif
Simon Glass75f00df2015-07-02 18:15:38 -0600431
Simon Glass32d8ab62016-07-17 15:23:15 -0600432int uclass_get_device_by_driver(enum uclass_id id,
433 const struct driver *find_drv,
434 struct udevice **devp)
435{
436 struct udevice *dev;
437 struct uclass *uc;
438 int ret;
439
440 ret = uclass_get(id, &uc);
441 if (ret)
442 return ret;
443
Liviu Dudau6f386222018-09-28 14:12:55 +0100444 uclass_foreach_dev(dev, uc) {
Simon Glass32d8ab62016-07-17 15:23:15 -0600445 if (dev->driver == find_drv)
446 return uclass_get_device_tail(dev, 0, devp);
447 }
448
449 return -ENODEV;
450}
451
Simon Glass98c9fb02017-04-23 20:10:43 -0600452int uclass_get_device_tail(struct udevice *dev, int ret, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700453{
Simon Glassdd6ab882014-02-26 15:59:18 -0700454 if (ret)
455 return ret;
456
Simon Glass46cc8ba92015-04-24 22:33:07 -0600457 assert(dev);
Simon Glassdd6ab882014-02-26 15:59:18 -0700458 ret = device_probe(dev);
459 if (ret)
460 return ret;
461
462 *devp = dev;
463
464 return 0;
465}
466
Simon Glass4b67cef2014-07-23 06:55:08 -0600467int uclass_get_device(enum uclass_id id, int index, struct udevice **devp)
468{
469 struct udevice *dev;
470 int ret;
471
472 *devp = NULL;
473 ret = uclass_find_device(id, index, &dev);
474 return uclass_get_device_tail(dev, ret, devp);
475}
476
Przemyslaw Marczaka12a1f52015-04-15 13:07:23 +0200477int uclass_get_device_by_name(enum uclass_id id, const char *name,
478 struct udevice **devp)
479{
480 struct udevice *dev;
481 int ret;
482
483 *devp = NULL;
484 ret = uclass_find_device_by_name(id, name, &dev);
485 return uclass_get_device_tail(dev, ret, devp);
486}
487
Simon Glassdb6f0202014-07-23 06:55:12 -0600488int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
489{
490 struct udevice *dev;
491 int ret;
492
493 *devp = NULL;
Simon Glass07e13382020-12-16 21:20:29 -0700494 ret = uclass_find_device_by_seq(id, seq, &dev);
495
Simon Glassdb6f0202014-07-23 06:55:12 -0600496 return uclass_get_device_tail(dev, ret, devp);
497}
498
Simon Glassc1464ab2014-07-23 06:55:14 -0600499int uclass_get_device_by_of_offset(enum uclass_id id, int node,
500 struct udevice **devp)
501{
502 struct udevice *dev;
503 int ret;
504
505 *devp = NULL;
506 ret = uclass_find_device_by_of_offset(id, node, &dev);
507 return uclass_get_device_tail(dev, ret, devp);
508}
509
Simon Glassee145d62017-05-18 20:09:09 -0600510int uclass_get_device_by_ofnode(enum uclass_id id, ofnode node,
511 struct udevice **devp)
512{
513 struct udevice *dev;
514 int ret;
515
Simon Glass5ab533c2018-06-11 13:07:19 -0600516 log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node));
Simon Glassee145d62017-05-18 20:09:09 -0600517 *devp = NULL;
518 ret = uclass_find_device_by_ofnode(id, node, &dev);
Simon Glass5ab533c2018-06-11 13:07:19 -0600519 log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n",
520 ofnode_get_name(node), dev ? dev->name : "(none)", ret);
Simon Glassee145d62017-05-18 20:09:09 -0600521
522 return uclass_get_device_tail(dev, ret, devp);
523}
524
Simon Glass359c4872015-12-19 19:38:55 -0700525#if CONFIG_IS_ENABLED(OF_CONTROL)
Kever Yang66a0b5a2018-02-09 10:56:23 +0800526int uclass_get_device_by_phandle_id(enum uclass_id id, uint phandle_id,
527 struct udevice **devp)
528{
529 struct udevice *dev;
530 struct uclass *uc;
531 int ret;
532
533 *devp = NULL;
534 ret = uclass_get(id, &uc);
535 if (ret)
536 return ret;
537
Liviu Dudau6f386222018-09-28 14:12:55 +0100538 uclass_foreach_dev(dev, uc) {
Kever Yang66a0b5a2018-02-09 10:56:23 +0800539 uint phandle;
540
541 phandle = dev_read_phandle(dev);
542
543 if (phandle == phandle_id) {
544 *devp = dev;
545 return uclass_get_device_tail(dev, ret, devp);
546 }
547 }
548
549 return -ENODEV;
550}
551
Simon Glass75f00df2015-07-02 18:15:38 -0600552int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent,
553 const char *name, struct udevice **devp)
554{
555 struct udevice *dev;
556 int ret;
557
558 *devp = NULL;
559 ret = uclass_find_device_by_phandle(id, parent, name, &dev);
560 return uclass_get_device_tail(dev, ret, devp);
561}
Simon Glass359c4872015-12-19 19:38:55 -0700562#endif
Simon Glass75f00df2015-07-02 18:15:38 -0600563
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200564int uclass_first_device(enum uclass_id id, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700565{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200566 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700567 int ret;
568
569 *devp = NULL;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200570 ret = uclass_find_first_device(id, &dev);
Simon Glass46cc8ba92015-04-24 22:33:07 -0600571 if (!dev)
572 return 0;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200573 return uclass_get_device_tail(dev, ret, devp);
Simon Glassdd6ab882014-02-26 15:59:18 -0700574}
575
Simon Glass832c3f02016-02-11 13:23:25 -0700576int uclass_first_device_err(enum uclass_id id, struct udevice **devp)
577{
578 int ret;
579
580 ret = uclass_first_device(id, devp);
581 if (ret)
582 return ret;
583 else if (!*devp)
584 return -ENODEV;
585
586 return 0;
587}
588
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200589int uclass_next_device(struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700590{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200591 struct udevice *dev = *devp;
Simon Glassdd6ab882014-02-26 15:59:18 -0700592 int ret;
593
594 *devp = NULL;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200595 ret = uclass_find_next_device(&dev);
Simon Glass46cc8ba92015-04-24 22:33:07 -0600596 if (!dev)
597 return 0;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200598 return uclass_get_device_tail(dev, ret, devp);
Simon Glassdd6ab882014-02-26 15:59:18 -0700599}
600
Patrice Chotard4dff1782018-10-24 14:10:15 +0200601int uclass_next_device_err(struct udevice **devp)
602{
603 int ret;
604
605 ret = uclass_next_device(devp);
606 if (ret)
607 return ret;
608 else if (!*devp)
609 return -ENODEV;
610
611 return 0;
612}
613
Simon Glass3ff83bc2017-04-23 20:10:45 -0600614int uclass_first_device_check(enum uclass_id id, struct udevice **devp)
615{
616 int ret;
617
618 *devp = NULL;
619 ret = uclass_find_first_device(id, devp);
620 if (ret)
621 return ret;
622 if (!*devp)
623 return 0;
624
625 return device_probe(*devp);
626}
627
628int uclass_next_device_check(struct udevice **devp)
629{
630 int ret;
631
632 ret = uclass_find_next_device(devp);
633 if (ret)
634 return ret;
635 if (!*devp)
636 return 0;
637
638 return device_probe(*devp);
639}
640
Simon Glass35cb2a42020-02-06 09:54:50 -0700641int uclass_first_device_drvdata(enum uclass_id id, ulong driver_data,
642 struct udevice **devp)
643{
644 struct udevice *dev;
645 struct uclass *uc;
646
647 uclass_id_foreach_dev(id, dev, uc) {
648 if (dev_get_driver_data(dev) == driver_data) {
649 *devp = dev;
650
651 return device_probe(dev);
652 }
653 }
654
655 return -ENODEV;
656}
657
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200658int uclass_bind_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700659{
660 struct uclass *uc;
661 int ret;
662
663 uc = dev->uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700664 list_add_tail(&dev->uclass_node, &uc->dev_head);
665
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700666 if (dev->parent) {
667 struct uclass_driver *uc_drv = dev->parent->uclass->uc_drv;
668
669 if (uc_drv->child_post_bind) {
670 ret = uc_drv->child_post_bind(dev);
671 if (ret)
672 goto err;
673 }
674 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700675
676 return 0;
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700677err:
678 /* There is no need to undo the parent's post_bind call */
679 list_del(&dev->uclass_node);
680
681 return ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700682}
683
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900684#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
Simon Glassce15bfb2021-10-23 17:26:05 -0600685int uclass_pre_unbind_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700686{
687 struct uclass *uc;
688 int ret;
689
690 uc = dev->uclass;
691 if (uc->uc_drv->pre_unbind) {
692 ret = uc->uc_drv->pre_unbind(dev);
693 if (ret)
694 return ret;
695 }
696
Simon Glassce15bfb2021-10-23 17:26:05 -0600697 return 0;
698}
699
700int uclass_unbind_device(struct udevice *dev)
701{
Simon Glassdd6ab882014-02-26 15:59:18 -0700702 list_del(&dev->uclass_node);
Simon Glassce15bfb2021-10-23 17:26:05 -0600703
Simon Glassdd6ab882014-02-26 15:59:18 -0700704 return 0;
705}
Simon Glass8914c8a2015-02-27 22:06:31 -0700706#endif
Simon Glassdd6ab882014-02-26 15:59:18 -0700707
Simon Glass9c1f3822015-03-05 12:25:22 -0700708int uclass_pre_probe_device(struct udevice *dev)
Simon Glass5104b982015-01-25 08:27:10 -0700709{
710 struct uclass_driver *uc_drv;
Simon Glass9c1f3822015-03-05 12:25:22 -0700711 int ret;
712
713 uc_drv = dev->uclass->uc_drv;
714 if (uc_drv->pre_probe) {
715 ret = uc_drv->pre_probe(dev);
716 if (ret)
717 return ret;
718 }
Simon Glass5104b982015-01-25 08:27:10 -0700719
720 if (!dev->parent)
721 return 0;
722 uc_drv = dev->parent->uclass->uc_drv;
Simon Glass256d4022019-09-25 08:55:47 -0600723 if (uc_drv->child_pre_probe) {
724 ret = uc_drv->child_pre_probe(dev);
725 if (ret)
726 return ret;
727 }
Simon Glass5104b982015-01-25 08:27:10 -0700728
729 return 0;
730}
731
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200732int uclass_post_probe_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700733{
Bin Meng30569cf2018-10-15 02:20:57 -0700734 struct uclass_driver *uc_drv;
735 int ret;
736
737 if (dev->parent) {
738 uc_drv = dev->parent->uclass->uc_drv;
739 if (uc_drv->child_post_probe) {
740 ret = uc_drv->child_post_probe(dev);
741 if (ret)
742 return ret;
743 }
744 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700745
Bin Meng30569cf2018-10-15 02:20:57 -0700746 uc_drv = dev->uclass->uc_drv;
Simon Glass256d4022019-09-25 08:55:47 -0600747 if (uc_drv->post_probe) {
748 ret = uc_drv->post_probe(dev);
749 if (ret)
750 return ret;
751 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700752
753 return 0;
754}
755
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900756#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200757int uclass_pre_remove_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700758{
Simon Glassdd6ab882014-02-26 15:59:18 -0700759 struct uclass *uc;
760 int ret;
761
762 uc = dev->uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700763 if (uc->uc_drv->pre_remove) {
764 ret = uc->uc_drv->pre_remove(dev);
765 if (ret)
766 return ret;
767 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700768
769 return 0;
770}
Simon Glass8914c8a2015-02-27 22:06:31 -0700771#endif
Jean-Jacques Hiblotdb97c7f2019-07-05 09:33:57 +0200772
Vabhav Sharmabcab47a2020-12-09 10:42:03 +0530773int uclass_probe_all(enum uclass_id id)
774{
775 struct udevice *dev;
776 int ret;
777
778 ret = uclass_first_device(id, &dev);
779 if (ret || !dev)
780 return ret;
781
782 /* Scanning uclass to probe all devices */
783 while (dev) {
784 ret = uclass_next_device(&dev);
785 if (ret)
786 return ret;
787 }
788
789 return 0;
790}
791
Jean-Jacques Hiblotdb97c7f2019-07-05 09:33:57 +0200792UCLASS_DRIVER(nop) = {
793 .id = UCLASS_NOP,
794 .name = "nop",
795};