blob: 08d9ed82de2d4c45ce3614f24296d9e4607399ff [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
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010049 * Return: 0 on success, -ve on error
Simon Glassdd6ab882014-02-26 15:59:18 -070050 *
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 Glassf1f519f2022-04-24 23:30:59 -0600183enum uclass_id uclass_get_by_namelen(const char *name, int len)
Simon Glass70e35b42017-12-28 13:14:15 -0700184{
185 int i;
186
187 for (i = 0; i < UCLASS_COUNT; i++) {
188 struct uclass_driver *uc_drv = lists_uclass_lookup(i);
189
Simon Glassf1f519f2022-04-24 23:30:59 -0600190 if (uc_drv && !strncmp(uc_drv->name, name, len) &&
191 strlen(uc_drv->name) == len)
Simon Glass70e35b42017-12-28 13:14:15 -0700192 return i;
193 }
194
195 return UCLASS_INVALID;
196}
197
Simon Glass4b23e452021-10-23 17:26:08 -0600198enum uclass_id uclass_get_by_name(const char *name)
199{
Simon Glassf1f519f2022-04-24 23:30:59 -0600200 return uclass_get_by_namelen(name, strlen(name));
Simon Glass4b23e452021-10-23 17:26:08 -0600201}
202
Jean-Jacques Hiblotc4f02482018-08-09 16:17:42 +0200203int dev_get_uclass_index(struct udevice *dev, struct uclass **ucp)
204{
205 struct udevice *iter;
206 struct uclass *uc = dev->uclass;
207 int i = 0;
208
209 if (list_empty(&uc->dev_head))
210 return -ENODEV;
211
Liviu Dudau6f386222018-09-28 14:12:55 +0100212 uclass_foreach_dev(iter, uc) {
Jean-Jacques Hiblotc4f02482018-08-09 16:17:42 +0200213 if (iter == dev) {
214 if (ucp)
215 *ucp = uc;
216 return i;
217 }
218 i++;
219 }
220
221 return -ENODEV;
222}
223
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200224int uclass_find_device(enum uclass_id id, int index, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700225{
226 struct uclass *uc;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200227 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700228 int ret;
229
230 *devp = NULL;
231 ret = uclass_get(id, &uc);
232 if (ret)
233 return ret;
Simon Glass46766df2015-07-31 09:31:19 -0600234 if (list_empty(&uc->dev_head))
235 return -ENODEV;
Simon Glassdd6ab882014-02-26 15:59:18 -0700236
Liviu Dudau6f386222018-09-28 14:12:55 +0100237 uclass_foreach_dev(dev, uc) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700238 if (!index--) {
239 *devp = dev;
240 return 0;
241 }
242 }
243
244 return -ENODEV;
245}
246
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200247int uclass_find_first_device(enum uclass_id id, struct udevice **devp)
248{
249 struct uclass *uc;
250 int ret;
251
252 *devp = NULL;
253 ret = uclass_get(id, &uc);
254 if (ret)
255 return ret;
256 if (list_empty(&uc->dev_head))
Simon Glass0bb44272019-09-25 08:55:55 -0600257 return 0;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200258
259 *devp = list_first_entry(&uc->dev_head, struct udevice, uclass_node);
260
261 return 0;
262}
263
264int uclass_find_next_device(struct udevice **devp)
265{
266 struct udevice *dev = *devp;
267
268 *devp = NULL;
269 if (list_is_last(&dev->uclass_node, &dev->uclass->dev_head))
270 return 0;
271
272 *devp = list_entry(dev->uclass_node.next, struct udevice, uclass_node);
273
274 return 0;
275}
276
Simon Glass9670f7d2022-04-24 23:31:00 -0600277int uclass_find_device_by_namelen(enum uclass_id id, const char *name, int len,
278 struct udevice **devp)
Przemyslaw Marczak2ffdf142015-04-15 13:07:22 +0200279{
280 struct uclass *uc;
281 struct udevice *dev;
282 int ret;
283
284 *devp = NULL;
285 if (!name)
286 return -EINVAL;
287 ret = uclass_get(id, &uc);
288 if (ret)
289 return ret;
290
Liviu Dudau6f386222018-09-28 14:12:55 +0100291 uclass_foreach_dev(dev, uc) {
Simon Glass9670f7d2022-04-24 23:31:00 -0600292 if (!strncmp(dev->name, name, len) &&
293 strlen(dev->name) == len) {
Przemyslaw Marczak2ffdf142015-04-15 13:07:22 +0200294 *devp = dev;
295 return 0;
296 }
297 }
298
299 return -ENODEV;
300}
301
Simon Glass9670f7d2022-04-24 23:31:00 -0600302int uclass_find_device_by_name(enum uclass_id id, const char *name,
303 struct udevice **devp)
304{
305 return uclass_find_device_by_namelen(id, name, strlen(name), devp);
306}
307
Simon Glassb941f562020-12-16 21:20:30 -0700308int uclass_find_next_free_seq(struct uclass *uc)
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100309{
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100310 struct udevice *dev;
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100311 int max = -1;
312
Simon Glassb941f562020-12-16 21:20:30 -0700313 /* If using aliases, start with the highest alias value */
314 if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
315 (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS))
316 max = dev_read_alias_highest_id(uc->uc_drv->name);
317
318 /* Avoid conflict with existing devices */
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100319 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
Simon Glass5e349922020-12-19 10:40:09 -0700320 if (dev->seq_ > max)
321 max = dev->seq_;
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100322 }
Simon Glassb941f562020-12-16 21:20:30 -0700323 /*
324 * At this point, max will be -1 if there are no existing aliases or
325 * devices
326 */
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100327
328 return max + 1;
329}
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +0100330
Simon Glass07e13382020-12-16 21:20:29 -0700331int uclass_find_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
Simon Glassdb6f0202014-07-23 06:55:12 -0600332{
333 struct uclass *uc;
334 struct udevice *dev;
335 int ret;
336
337 *devp = NULL;
Simon Glass07e13382020-12-16 21:20:29 -0700338 log_debug("%d\n", seq);
339 if (seq == -1)
Simon Glassdb6f0202014-07-23 06:55:12 -0600340 return -ENODEV;
341 ret = uclass_get(id, &uc);
342 if (ret)
343 return ret;
344
Liviu Dudau6f386222018-09-28 14:12:55 +0100345 uclass_foreach_dev(dev, uc) {
Simon Glass5e349922020-12-19 10:40:09 -0700346 log_debug(" - %d '%s'\n", dev->seq_, dev->name);
347 if (dev->seq_ == seq) {
Simon Glassdb6f0202014-07-23 06:55:12 -0600348 *devp = dev;
Kever Yang5f09a2d2019-10-22 15:39:47 +0800349 log_debug(" - found\n");
Simon Glassdb6f0202014-07-23 06:55:12 -0600350 return 0;
351 }
352 }
Kever Yang5f09a2d2019-10-22 15:39:47 +0800353 log_debug(" - not found\n");
Simon Glassdb6f0202014-07-23 06:55:12 -0600354
355 return -ENODEV;
356}
357
Simon Glass96f04442016-01-21 19:43:57 -0700358int uclass_find_device_by_of_offset(enum uclass_id id, int node,
359 struct udevice **devp)
Simon Glassc1464ab2014-07-23 06:55:14 -0600360{
361 struct uclass *uc;
362 struct udevice *dev;
363 int ret;
364
365 *devp = NULL;
366 if (node < 0)
367 return -ENODEV;
368 ret = uclass_get(id, &uc);
369 if (ret)
370 return ret;
371
Liviu Dudau6f386222018-09-28 14:12:55 +0100372 uclass_foreach_dev(dev, uc) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700373 if (dev_of_offset(dev) == node) {
Simon Glassc1464ab2014-07-23 06:55:14 -0600374 *devp = dev;
375 return 0;
376 }
377 }
378
379 return -ENODEV;
380}
381
Simon Glassee145d62017-05-18 20:09:09 -0600382int uclass_find_device_by_ofnode(enum uclass_id id, ofnode node,
383 struct udevice **devp)
384{
385 struct uclass *uc;
386 struct udevice *dev;
387 int ret;
388
Simon Glass5ab533c2018-06-11 13:07:19 -0600389 log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node));
Simon Glassee145d62017-05-18 20:09:09 -0600390 *devp = NULL;
391 if (!ofnode_valid(node))
392 return -ENODEV;
393 ret = uclass_get(id, &uc);
394 if (ret)
395 return ret;
396
Liviu Dudau6f386222018-09-28 14:12:55 +0100397 uclass_foreach_dev(dev, uc) {
Simon Glass5ab533c2018-06-11 13:07:19 -0600398 log(LOGC_DM, LOGL_DEBUG_CONTENT, " - checking %s\n",
399 dev->name);
Simon Glassee145d62017-05-18 20:09:09 -0600400 if (ofnode_equal(dev_ofnode(dev), node)) {
401 *devp = dev;
Simon Glass5ab533c2018-06-11 13:07:19 -0600402 goto done;
Simon Glassee145d62017-05-18 20:09:09 -0600403 }
404 }
Simon Glass5ab533c2018-06-11 13:07:19 -0600405 ret = -ENODEV;
Simon Glassee145d62017-05-18 20:09:09 -0600406
Simon Glass5ab533c2018-06-11 13:07:19 -0600407done:
408 log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n",
409 ofnode_get_name(node), *devp ? (*devp)->name : "(none)", ret);
410 return ret;
Simon Glassee145d62017-05-18 20:09:09 -0600411}
412
Simon Glass3580f6d2021-08-07 07:24:03 -0600413#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glassdc3efdc2018-11-18 08:14:30 -0700414int uclass_find_device_by_phandle(enum uclass_id id, struct udevice *parent,
415 const char *name, struct udevice **devp)
Simon Glass75f00df2015-07-02 18:15:38 -0600416{
417 struct udevice *dev;
418 struct uclass *uc;
419 int find_phandle;
420 int ret;
421
422 *devp = NULL;
Simon Glassbfb6e3d2017-05-18 20:09:12 -0600423 find_phandle = dev_read_u32_default(parent, name, -1);
Simon Glass75f00df2015-07-02 18:15:38 -0600424 if (find_phandle <= 0)
425 return -ENOENT;
426 ret = uclass_get(id, &uc);
427 if (ret)
428 return ret;
429
Liviu Dudau6f386222018-09-28 14:12:55 +0100430 uclass_foreach_dev(dev, uc) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700431 uint phandle;
432
Simon Glassbfb6e3d2017-05-18 20:09:12 -0600433 phandle = dev_read_phandle(dev);
Simon Glass75f00df2015-07-02 18:15:38 -0600434
435 if (phandle == find_phandle) {
436 *devp = dev;
437 return 0;
438 }
439 }
440
441 return -ENODEV;
442}
Simon Glass359c4872015-12-19 19:38:55 -0700443#endif
Simon Glass75f00df2015-07-02 18:15:38 -0600444
Simon Glass32d8ab62016-07-17 15:23:15 -0600445int uclass_get_device_by_driver(enum uclass_id id,
446 const struct driver *find_drv,
447 struct udevice **devp)
448{
449 struct udevice *dev;
450 struct uclass *uc;
451 int ret;
452
453 ret = uclass_get(id, &uc);
454 if (ret)
455 return ret;
456
Liviu Dudau6f386222018-09-28 14:12:55 +0100457 uclass_foreach_dev(dev, uc) {
Simon Glass32d8ab62016-07-17 15:23:15 -0600458 if (dev->driver == find_drv)
459 return uclass_get_device_tail(dev, 0, devp);
460 }
461
462 return -ENODEV;
463}
464
Simon Glass98c9fb02017-04-23 20:10:43 -0600465int uclass_get_device_tail(struct udevice *dev, int ret, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700466{
Simon Glassdd6ab882014-02-26 15:59:18 -0700467 if (ret)
468 return ret;
469
Simon Glass46cc8ba92015-04-24 22:33:07 -0600470 assert(dev);
Simon Glassdd6ab882014-02-26 15:59:18 -0700471 ret = device_probe(dev);
472 if (ret)
473 return ret;
474
475 *devp = dev;
476
477 return 0;
478}
479
Simon Glass4b67cef2014-07-23 06:55:08 -0600480int uclass_get_device(enum uclass_id id, int index, struct udevice **devp)
481{
482 struct udevice *dev;
483 int ret;
484
485 *devp = NULL;
486 ret = uclass_find_device(id, index, &dev);
487 return uclass_get_device_tail(dev, ret, devp);
488}
489
Przemyslaw Marczaka12a1f52015-04-15 13:07:23 +0200490int uclass_get_device_by_name(enum uclass_id id, const char *name,
491 struct udevice **devp)
492{
493 struct udevice *dev;
494 int ret;
495
496 *devp = NULL;
497 ret = uclass_find_device_by_name(id, name, &dev);
498 return uclass_get_device_tail(dev, ret, devp);
499}
500
Simon Glassdb6f0202014-07-23 06:55:12 -0600501int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
502{
503 struct udevice *dev;
504 int ret;
505
506 *devp = NULL;
Simon Glass07e13382020-12-16 21:20:29 -0700507 ret = uclass_find_device_by_seq(id, seq, &dev);
508
Simon Glassdb6f0202014-07-23 06:55:12 -0600509 return uclass_get_device_tail(dev, ret, devp);
510}
511
Simon Glassc1464ab2014-07-23 06:55:14 -0600512int uclass_get_device_by_of_offset(enum uclass_id id, int node,
513 struct udevice **devp)
514{
515 struct udevice *dev;
516 int ret;
517
518 *devp = NULL;
519 ret = uclass_find_device_by_of_offset(id, node, &dev);
520 return uclass_get_device_tail(dev, ret, devp);
521}
522
Simon Glassee145d62017-05-18 20:09:09 -0600523int uclass_get_device_by_ofnode(enum uclass_id id, ofnode node,
524 struct udevice **devp)
525{
526 struct udevice *dev;
527 int ret;
528
Simon Glass5ab533c2018-06-11 13:07:19 -0600529 log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node));
Simon Glassee145d62017-05-18 20:09:09 -0600530 *devp = NULL;
531 ret = uclass_find_device_by_ofnode(id, node, &dev);
Simon Glass5ab533c2018-06-11 13:07:19 -0600532 log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n",
533 ofnode_get_name(node), dev ? dev->name : "(none)", ret);
Simon Glassee145d62017-05-18 20:09:09 -0600534
535 return uclass_get_device_tail(dev, ret, devp);
536}
537
Simon Glass359c4872015-12-19 19:38:55 -0700538#if CONFIG_IS_ENABLED(OF_CONTROL)
Kever Yang66a0b5a2018-02-09 10:56:23 +0800539int uclass_get_device_by_phandle_id(enum uclass_id id, uint phandle_id,
540 struct udevice **devp)
541{
542 struct udevice *dev;
543 struct uclass *uc;
544 int ret;
545
546 *devp = NULL;
547 ret = uclass_get(id, &uc);
548 if (ret)
549 return ret;
550
Liviu Dudau6f386222018-09-28 14:12:55 +0100551 uclass_foreach_dev(dev, uc) {
Kever Yang66a0b5a2018-02-09 10:56:23 +0800552 uint phandle;
553
554 phandle = dev_read_phandle(dev);
555
556 if (phandle == phandle_id) {
557 *devp = dev;
558 return uclass_get_device_tail(dev, ret, devp);
559 }
560 }
561
562 return -ENODEV;
563}
564
Simon Glass75f00df2015-07-02 18:15:38 -0600565int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent,
566 const char *name, struct udevice **devp)
567{
568 struct udevice *dev;
569 int ret;
570
571 *devp = NULL;
572 ret = uclass_find_device_by_phandle(id, parent, name, &dev);
573 return uclass_get_device_tail(dev, ret, devp);
574}
Simon Glass359c4872015-12-19 19:38:55 -0700575#endif
Simon Glass75f00df2015-07-02 18:15:38 -0600576
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200577int uclass_first_device(enum uclass_id id, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700578{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200579 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700580 int ret;
581
582 *devp = NULL;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200583 ret = uclass_find_first_device(id, &dev);
Simon Glass46cc8ba92015-04-24 22:33:07 -0600584 if (!dev)
585 return 0;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200586 return uclass_get_device_tail(dev, ret, devp);
Simon Glassdd6ab882014-02-26 15:59:18 -0700587}
588
Simon Glass832c3f02016-02-11 13:23:25 -0700589int uclass_first_device_err(enum uclass_id id, struct udevice **devp)
590{
591 int ret;
592
593 ret = uclass_first_device(id, devp);
594 if (ret)
595 return ret;
596 else if (!*devp)
597 return -ENODEV;
598
599 return 0;
600}
601
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200602int uclass_next_device(struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700603{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200604 struct udevice *dev = *devp;
Simon Glassdd6ab882014-02-26 15:59:18 -0700605 int ret;
606
607 *devp = NULL;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200608 ret = uclass_find_next_device(&dev);
Simon Glass46cc8ba92015-04-24 22:33:07 -0600609 if (!dev)
610 return 0;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200611 return uclass_get_device_tail(dev, ret, devp);
Simon Glassdd6ab882014-02-26 15:59:18 -0700612}
613
Patrice Chotard4dff1782018-10-24 14:10:15 +0200614int uclass_next_device_err(struct udevice **devp)
615{
616 int ret;
617
618 ret = uclass_next_device(devp);
619 if (ret)
620 return ret;
621 else if (!*devp)
622 return -ENODEV;
623
624 return 0;
625}
626
Simon Glass3ff83bc2017-04-23 20:10:45 -0600627int uclass_first_device_check(enum uclass_id id, struct udevice **devp)
628{
629 int ret;
630
631 *devp = NULL;
632 ret = uclass_find_first_device(id, devp);
633 if (ret)
634 return ret;
635 if (!*devp)
636 return 0;
637
638 return device_probe(*devp);
639}
640
641int uclass_next_device_check(struct udevice **devp)
642{
643 int ret;
644
645 ret = uclass_find_next_device(devp);
646 if (ret)
647 return ret;
648 if (!*devp)
649 return 0;
650
651 return device_probe(*devp);
652}
653
Simon Glass51608c92021-12-16 20:59:32 -0700654int uclass_get_count(void)
655{
656 const struct uclass *uc;
657 int count = 0;
658
659 if (gd->dm_root) {
660 list_for_each_entry(uc, gd->uclass_root, sibling_node)
661 count++;
662 }
663
664 return count;
665}
666
Simon Glass35cb2a42020-02-06 09:54:50 -0700667int uclass_first_device_drvdata(enum uclass_id id, ulong driver_data,
668 struct udevice **devp)
669{
670 struct udevice *dev;
671 struct uclass *uc;
672
673 uclass_id_foreach_dev(id, dev, uc) {
674 if (dev_get_driver_data(dev) == driver_data) {
675 *devp = dev;
676
677 return device_probe(dev);
678 }
679 }
680
681 return -ENODEV;
682}
683
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200684int uclass_bind_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700685{
686 struct uclass *uc;
687 int ret;
688
689 uc = dev->uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700690 list_add_tail(&dev->uclass_node, &uc->dev_head);
691
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700692 if (dev->parent) {
693 struct uclass_driver *uc_drv = dev->parent->uclass->uc_drv;
694
695 if (uc_drv->child_post_bind) {
696 ret = uc_drv->child_post_bind(dev);
697 if (ret)
698 goto err;
699 }
700 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700701
702 return 0;
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700703err:
704 /* There is no need to undo the parent's post_bind call */
705 list_del(&dev->uclass_node);
706
707 return ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700708}
709
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900710#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
Simon Glassce15bfb2021-10-23 17:26:05 -0600711int uclass_pre_unbind_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700712{
713 struct uclass *uc;
714 int ret;
715
716 uc = dev->uclass;
717 if (uc->uc_drv->pre_unbind) {
718 ret = uc->uc_drv->pre_unbind(dev);
719 if (ret)
720 return ret;
721 }
722
Simon Glassce15bfb2021-10-23 17:26:05 -0600723 return 0;
724}
725
726int uclass_unbind_device(struct udevice *dev)
727{
Simon Glassdd6ab882014-02-26 15:59:18 -0700728 list_del(&dev->uclass_node);
Simon Glassce15bfb2021-10-23 17:26:05 -0600729
Simon Glassdd6ab882014-02-26 15:59:18 -0700730 return 0;
731}
Simon Glass8914c8a2015-02-27 22:06:31 -0700732#endif
Simon Glassdd6ab882014-02-26 15:59:18 -0700733
Simon Glass9c1f3822015-03-05 12:25:22 -0700734int uclass_pre_probe_device(struct udevice *dev)
Simon Glass5104b982015-01-25 08:27:10 -0700735{
736 struct uclass_driver *uc_drv;
Simon Glass9c1f3822015-03-05 12:25:22 -0700737 int ret;
738
739 uc_drv = dev->uclass->uc_drv;
740 if (uc_drv->pre_probe) {
741 ret = uc_drv->pre_probe(dev);
742 if (ret)
743 return ret;
744 }
Simon Glass5104b982015-01-25 08:27:10 -0700745
746 if (!dev->parent)
747 return 0;
748 uc_drv = dev->parent->uclass->uc_drv;
Simon Glass256d4022019-09-25 08:55:47 -0600749 if (uc_drv->child_pre_probe) {
750 ret = uc_drv->child_pre_probe(dev);
751 if (ret)
752 return ret;
753 }
Simon Glass5104b982015-01-25 08:27:10 -0700754
755 return 0;
756}
757
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200758int uclass_post_probe_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700759{
Bin Meng30569cf2018-10-15 02:20:57 -0700760 struct uclass_driver *uc_drv;
761 int ret;
762
763 if (dev->parent) {
764 uc_drv = dev->parent->uclass->uc_drv;
765 if (uc_drv->child_post_probe) {
766 ret = uc_drv->child_post_probe(dev);
767 if (ret)
768 return ret;
769 }
770 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700771
Bin Meng30569cf2018-10-15 02:20:57 -0700772 uc_drv = dev->uclass->uc_drv;
Simon Glass256d4022019-09-25 08:55:47 -0600773 if (uc_drv->post_probe) {
774 ret = uc_drv->post_probe(dev);
775 if (ret)
776 return ret;
777 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700778
779 return 0;
780}
781
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900782#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200783int uclass_pre_remove_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700784{
Simon Glassdd6ab882014-02-26 15:59:18 -0700785 struct uclass *uc;
786 int ret;
787
788 uc = dev->uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700789 if (uc->uc_drv->pre_remove) {
790 ret = uc->uc_drv->pre_remove(dev);
791 if (ret)
792 return ret;
793 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700794
795 return 0;
796}
Simon Glass8914c8a2015-02-27 22:06:31 -0700797#endif
Jean-Jacques Hiblotdb97c7f2019-07-05 09:33:57 +0200798
Vabhav Sharmabcab47a2020-12-09 10:42:03 +0530799int uclass_probe_all(enum uclass_id id)
800{
801 struct udevice *dev;
802 int ret;
803
804 ret = uclass_first_device(id, &dev);
805 if (ret || !dev)
806 return ret;
807
808 /* Scanning uclass to probe all devices */
809 while (dev) {
810 ret = uclass_next_device(&dev);
811 if (ret)
812 return ret;
813 }
814
815 return 0;
816}
817
Simon Glassdab7e0d2021-10-23 17:26:09 -0600818int uclass_id_count(enum uclass_id id)
819{
820 struct udevice *dev;
821 struct uclass *uc;
822 int count = 0;
823
824 uclass_id_foreach_dev(id, dev, uc)
825 count++;
826
827 return count;
828}
829
Jean-Jacques Hiblotdb97c7f2019-07-05 09:33:57 +0200830UCLASS_DRIVER(nop) = {
831 .id = UCLASS_NOP,
832 .name = "nop",
833};