1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 Google, Inc
6 * Pavel Herrmann <morpheus.ibis@gmail.com>
9 #define LOG_CATEGORY LOGC_DM
15 #include <dm/device.h>
16 #include <dm/device-internal.h>
18 #include <dm/uclass.h>
19 #include <dm/uclass-internal.h>
22 DECLARE_GLOBAL_DATA_PTR;
24 struct uclass *uclass_find(enum uclass_id key)
31 * TODO(sjg@chromium.org): Optimise this, perhaps moving the found
32 * node to the start of the list, or creating a linear array mapping
35 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
36 if (uc->uc_drv->id == key)
44 * uclass_add() - Create new uclass in list
45 * @id: Id number to create
46 * @ucp: Returns pointer to uclass, or NULL on error
47 * @return 0 on success, -ve on error
49 * The new uclass is added to the list. There must be only one uclass for
52 static int uclass_add(enum uclass_id id, struct uclass **ucp)
54 struct uclass_driver *uc_drv;
59 uc_drv = lists_uclass_lookup(id);
61 debug("Cannot find uclass for id %d: please add the UCLASS_DRIVER() declaration for this UCLASS_... id\n",
64 * Use a strange error to make this case easier to find. When
65 * a uclass is not available it can prevent driver model from
66 * starting up and this failure is otherwise hard to debug.
70 uc = calloc(1, sizeof(*uc));
73 if (uc_drv->priv_auto_alloc_size) {
74 uc->priv = calloc(1, uc_drv->priv_auto_alloc_size);
81 INIT_LIST_HEAD(&uc->sibling_node);
82 INIT_LIST_HEAD(&uc->dev_head);
83 list_add(&uc->sibling_node, &DM_UCLASS_ROOT_NON_CONST);
86 ret = uc_drv->init(uc);
95 if (uc_drv->priv_auto_alloc_size) {
99 list_del(&uc->sibling_node);
106 int uclass_destroy(struct uclass *uc)
108 struct uclass_driver *uc_drv;
113 * We cannot use list_for_each_entry_safe() here. If a device in this
114 * uclass has a child device also in this uclass, it will be also be
115 * unbound (by the recursion in the call to device_unbind() below).
116 * We can loop until the list is empty.
118 while (!list_empty(&uc->dev_head)) {
119 dev = list_first_entry(&uc->dev_head, struct udevice,
121 ret = device_remove(dev, DM_REMOVE_NORMAL);
124 ret = device_unbind(dev);
132 list_del(&uc->sibling_node);
133 if (uc_drv->priv_auto_alloc_size)
140 int uclass_get(enum uclass_id id, struct uclass **ucp)
145 uc = uclass_find(id);
147 return uclass_add(id, ucp);
153 const char *uclass_get_name(enum uclass_id id)
157 if (uclass_get(id, &uc))
159 return uc->uc_drv->name;
162 enum uclass_id uclass_get_by_name(const char *name)
166 for (i = 0; i < UCLASS_COUNT; i++) {
167 struct uclass_driver *uc_drv = lists_uclass_lookup(i);
169 if (uc_drv && !strcmp(uc_drv->name, name))
173 return UCLASS_INVALID;
176 int dev_get_uclass_index(struct udevice *dev, struct uclass **ucp)
178 struct udevice *iter;
179 struct uclass *uc = dev->uclass;
182 if (list_empty(&uc->dev_head))
185 uclass_foreach_dev(iter, uc) {
197 int uclass_find_device(enum uclass_id id, int index, struct udevice **devp)
204 ret = uclass_get(id, &uc);
207 if (list_empty(&uc->dev_head))
210 uclass_foreach_dev(dev, uc) {
220 int uclass_find_first_device(enum uclass_id id, struct udevice **devp)
226 ret = uclass_get(id, &uc);
229 if (list_empty(&uc->dev_head))
232 *devp = list_first_entry(&uc->dev_head, struct udevice, uclass_node);
237 int uclass_find_next_device(struct udevice **devp)
239 struct udevice *dev = *devp;
242 if (list_is_last(&dev->uclass_node, &dev->uclass->dev_head))
245 *devp = list_entry(dev->uclass_node.next, struct udevice, uclass_node);
250 int uclass_find_device_by_name(enum uclass_id id, const char *name,
251 struct udevice **devp)
260 ret = uclass_get(id, &uc);
264 uclass_foreach_dev(dev, uc) {
265 if (!strcmp(dev->name, name)) {
274 #if !CONFIG_IS_ENABLED(OF_CONTROL) || \
275 CONFIG_IS_ENABLED(OF_PLATDATA) || \
276 CONFIG_IS_ENABLED(OF_PRIOR_STAGE)
277 int uclass_find_next_free_req_seq(enum uclass_id id)
284 ret = uclass_get(id, &uc);
288 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
289 if ((dev->req_seq != -1) && (dev->req_seq > max))
300 int uclass_find_device_by_seq(enum uclass_id id, int seq_or_req_seq,
301 bool find_req_seq, struct udevice **devp)
308 log_debug("%d %d\n", find_req_seq, seq_or_req_seq);
309 if (seq_or_req_seq == -1)
311 ret = uclass_get(id, &uc);
315 uclass_foreach_dev(dev, uc) {
316 log_debug(" - %d %d '%s'\n",
317 dev->req_seq, dev->seq, dev->name);
318 if ((find_req_seq ? dev->req_seq : dev->seq) ==
321 log_debug(" - found\n");
325 log_debug(" - not found\n");
330 int uclass_find_device_by_of_offset(enum uclass_id id, int node,
331 struct udevice **devp)
340 ret = uclass_get(id, &uc);
344 uclass_foreach_dev(dev, uc) {
345 if (dev_of_offset(dev) == node) {
354 int uclass_find_device_by_ofnode(enum uclass_id id, ofnode node,
355 struct udevice **devp)
361 log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node));
363 if (!ofnode_valid(node))
365 ret = uclass_get(id, &uc);
369 uclass_foreach_dev(dev, uc) {
370 log(LOGC_DM, LOGL_DEBUG_CONTENT, " - checking %s\n",
372 if (ofnode_equal(dev_ofnode(dev), node)) {
380 log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n",
381 ofnode_get_name(node), *devp ? (*devp)->name : "(none)", ret);
385 #if CONFIG_IS_ENABLED(OF_CONTROL)
386 int uclass_find_device_by_phandle(enum uclass_id id, struct udevice *parent,
387 const char *name, struct udevice **devp)
395 find_phandle = dev_read_u32_default(parent, name, -1);
396 if (find_phandle <= 0)
398 ret = uclass_get(id, &uc);
402 uclass_foreach_dev(dev, uc) {
405 phandle = dev_read_phandle(dev);
407 if (phandle == find_phandle) {
417 int uclass_get_device_by_driver(enum uclass_id id,
418 const struct driver *find_drv,
419 struct udevice **devp)
425 ret = uclass_get(id, &uc);
429 uclass_foreach_dev(dev, uc) {
430 if (dev->driver == find_drv)
431 return uclass_get_device_tail(dev, 0, devp);
437 int uclass_get_device_tail(struct udevice *dev, int ret, struct udevice **devp)
443 ret = device_probe(dev);
452 int uclass_get_device(enum uclass_id id, int index, struct udevice **devp)
458 ret = uclass_find_device(id, index, &dev);
459 return uclass_get_device_tail(dev, ret, devp);
462 int uclass_get_device_by_name(enum uclass_id id, const char *name,
463 struct udevice **devp)
469 ret = uclass_find_device_by_name(id, name, &dev);
470 return uclass_get_device_tail(dev, ret, devp);
473 int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
479 ret = uclass_find_device_by_seq(id, seq, false, &dev);
480 if (ret == -ENODEV) {
482 * We didn't find it in probed devices. See if there is one
483 * that will request this seq if probed.
485 ret = uclass_find_device_by_seq(id, seq, true, &dev);
487 return uclass_get_device_tail(dev, ret, devp);
490 int uclass_get_device_by_of_offset(enum uclass_id id, int node,
491 struct udevice **devp)
497 ret = uclass_find_device_by_of_offset(id, node, &dev);
498 return uclass_get_device_tail(dev, ret, devp);
501 int uclass_get_device_by_ofnode(enum uclass_id id, ofnode node,
502 struct udevice **devp)
507 log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node));
509 ret = uclass_find_device_by_ofnode(id, node, &dev);
510 log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n",
511 ofnode_get_name(node), dev ? dev->name : "(none)", ret);
513 return uclass_get_device_tail(dev, ret, devp);
516 #if CONFIG_IS_ENABLED(OF_CONTROL)
517 int uclass_get_device_by_phandle_id(enum uclass_id id, uint phandle_id,
518 struct udevice **devp)
525 ret = uclass_get(id, &uc);
529 uclass_foreach_dev(dev, uc) {
532 phandle = dev_read_phandle(dev);
534 if (phandle == phandle_id) {
536 return uclass_get_device_tail(dev, ret, devp);
543 int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent,
544 const char *name, struct udevice **devp)
550 ret = uclass_find_device_by_phandle(id, parent, name, &dev);
551 return uclass_get_device_tail(dev, ret, devp);
555 int uclass_first_device(enum uclass_id id, struct udevice **devp)
561 ret = uclass_find_first_device(id, &dev);
564 return uclass_get_device_tail(dev, ret, devp);
567 int uclass_first_device_err(enum uclass_id id, struct udevice **devp)
571 ret = uclass_first_device(id, devp);
580 int uclass_next_device(struct udevice **devp)
582 struct udevice *dev = *devp;
586 ret = uclass_find_next_device(&dev);
589 return uclass_get_device_tail(dev, ret, devp);
592 int uclass_next_device_err(struct udevice **devp)
596 ret = uclass_next_device(devp);
605 int uclass_first_device_check(enum uclass_id id, struct udevice **devp)
610 ret = uclass_find_first_device(id, devp);
616 return device_probe(*devp);
619 int uclass_next_device_check(struct udevice **devp)
623 ret = uclass_find_next_device(devp);
629 return device_probe(*devp);
632 int uclass_bind_device(struct udevice *dev)
638 list_add_tail(&dev->uclass_node, &uc->dev_head);
641 struct uclass_driver *uc_drv = dev->parent->uclass->uc_drv;
643 if (uc_drv->child_post_bind) {
644 ret = uc_drv->child_post_bind(dev);
652 /* There is no need to undo the parent's post_bind call */
653 list_del(&dev->uclass_node);
658 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
659 int uclass_unbind_device(struct udevice *dev)
665 if (uc->uc_drv->pre_unbind) {
666 ret = uc->uc_drv->pre_unbind(dev);
671 list_del(&dev->uclass_node);
676 int uclass_resolve_seq(struct udevice *dev)
682 assert(dev->seq == -1);
683 ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, dev->req_seq,
686 dm_warn("Device '%s': seq %d is in use by '%s'\n",
687 dev->name, dev->req_seq, dup->name);
688 } else if (ret == -ENODEV) {
689 /* Our requested sequence number is available */
690 if (dev->req_seq != -1)
696 for (seq = 0; seq < DM_MAX_SEQ; seq++) {
697 ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, seq,
707 int uclass_pre_probe_device(struct udevice *dev)
709 struct uclass_driver *uc_drv;
712 uc_drv = dev->uclass->uc_drv;
713 if (uc_drv->pre_probe) {
714 ret = uc_drv->pre_probe(dev);
721 uc_drv = dev->parent->uclass->uc_drv;
722 if (uc_drv->child_pre_probe) {
723 ret = uc_drv->child_pre_probe(dev);
731 int uclass_post_probe_device(struct udevice *dev)
733 struct uclass_driver *uc_drv;
737 uc_drv = dev->parent->uclass->uc_drv;
738 if (uc_drv->child_post_probe) {
739 ret = uc_drv->child_post_probe(dev);
745 uc_drv = dev->uclass->uc_drv;
746 if (uc_drv->post_probe) {
747 ret = uc_drv->post_probe(dev);
755 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
756 int uclass_pre_remove_device(struct udevice *dev)
762 if (uc->uc_drv->pre_remove) {
763 ret = uc->uc_drv->pre_remove(dev);
772 UCLASS_DRIVER(nop) = {