1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 Google, Inc
9 #define LOG_CATEGORY LOGC_DM
16 #include <dm/device.h>
17 #include <dm/device-internal.h>
19 #include <dm/uclass.h>
20 #include <dm/uclass-internal.h>
23 DECLARE_GLOBAL_DATA_PTR;
25 struct uclass *uclass_find(enum uclass_id key)
33 * node to the start of the list, or creating a linear array mapping
36 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
37 if (uc->uc_drv->id == key)
45 * uclass_add() - Create new uclass in list
46 * @id: Id number to create
47 * @ucp: Returns pointer to uclass, or NULL on error
48 * @return 0 on success, -ve on error
50 * The new uclass is added to the list. There must be only one uclass for
53 static int uclass_add(enum uclass_id id, struct uclass **ucp)
55 struct uclass_driver *uc_drv;
60 uc_drv = lists_uclass_lookup(id);
62 debug("Cannot find uclass for id %d: please add the UCLASS_DRIVER() declaration for this UCLASS_... id\n",
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.
71 uc = calloc(1, sizeof(*uc));
74 if (uc_drv->priv_auto_alloc_size) {
75 uc->priv = calloc(1, uc_drv->priv_auto_alloc_size);
82 INIT_LIST_HEAD(&uc->sibling_node);
83 INIT_LIST_HEAD(&uc->dev_head);
84 list_add(&uc->sibling_node, &DM_UCLASS_ROOT_NON_CONST);
87 ret = uc_drv->init(uc);
96 if (uc_drv->priv_auto_alloc_size) {
100 list_del(&uc->sibling_node);
107 int uclass_destroy(struct uclass *uc)
109 struct uclass_driver *uc_drv;
114 * We cannot use list_for_each_entry_safe() here. If a device in this
115 * uclass has a child device also in this uclass, it will be also be
116 * unbound (by the recursion in the call to device_unbind() below).
117 * We can loop until the list is empty.
119 while (!list_empty(&uc->dev_head)) {
120 dev = list_first_entry(&uc->dev_head, struct udevice,
122 ret = device_remove(dev, DM_REMOVE_NORMAL | DM_REMOVE_NO_PD);
124 return log_msg_ret("remove", ret);
125 ret = device_unbind(dev);
127 return log_msg_ret("unbind", ret);
133 list_del(&uc->sibling_node);
134 if (uc_drv->priv_auto_alloc_size)
141 int uclass_get(enum uclass_id id, struct uclass **ucp)
146 uc = uclass_find(id);
148 return uclass_add(id, ucp);
154 const char *uclass_get_name(enum uclass_id id)
158 if (uclass_get(id, &uc))
160 return uc->uc_drv->name;
163 enum uclass_id uclass_get_by_name(const char *name)
167 for (i = 0; i < UCLASS_COUNT; i++) {
168 struct uclass_driver *uc_drv = lists_uclass_lookup(i);
170 if (uc_drv && !strcmp(uc_drv->name, name))
174 return UCLASS_INVALID;
177 int dev_get_uclass_index(struct udevice *dev, struct uclass **ucp)
179 struct udevice *iter;
180 struct uclass *uc = dev->uclass;
183 if (list_empty(&uc->dev_head))
186 uclass_foreach_dev(iter, uc) {
198 int uclass_find_device(enum uclass_id id, int index, struct udevice **devp)
205 ret = uclass_get(id, &uc);
208 if (list_empty(&uc->dev_head))
211 uclass_foreach_dev(dev, uc) {
221 int uclass_find_first_device(enum uclass_id id, struct udevice **devp)
227 ret = uclass_get(id, &uc);
230 if (list_empty(&uc->dev_head))
233 *devp = list_first_entry(&uc->dev_head, struct udevice, uclass_node);
238 int uclass_find_next_device(struct udevice **devp)
240 struct udevice *dev = *devp;
243 if (list_is_last(&dev->uclass_node, &dev->uclass->dev_head))
246 *devp = list_entry(dev->uclass_node.next, struct udevice, uclass_node);
251 int uclass_find_device_by_name(enum uclass_id id, const char *name,
252 struct udevice **devp)
261 ret = uclass_get(id, &uc);
265 uclass_foreach_dev(dev, uc) {
266 if (!strcmp(dev->name, name)) {
275 int uclass_find_next_free_req_seq(enum uclass_id id)
282 ret = uclass_get(id, &uc);
286 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
287 if ((dev->req_seq != -1) && (dev->req_seq > max))
297 int uclass_find_device_by_seq(enum uclass_id id, int seq_or_req_seq,
298 bool find_req_seq, struct udevice **devp)
305 log_debug("%d %d\n", find_req_seq, seq_or_req_seq);
306 if (seq_or_req_seq == -1)
308 ret = uclass_get(id, &uc);
312 uclass_foreach_dev(dev, uc) {
313 log_debug(" - %d %d '%s'\n",
314 dev->req_seq, dev->seq, dev->name);
315 if ((find_req_seq ? dev->req_seq : dev->seq) ==
318 log_debug(" - found\n");
322 log_debug(" - not found\n");
327 int uclass_find_device_by_of_offset(enum uclass_id id, int node,
328 struct udevice **devp)
337 ret = uclass_get(id, &uc);
341 uclass_foreach_dev(dev, uc) {
342 if (dev_of_offset(dev) == node) {
351 int uclass_find_device_by_ofnode(enum uclass_id id, ofnode node,
352 struct udevice **devp)
358 log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node));
360 if (!ofnode_valid(node))
362 ret = uclass_get(id, &uc);
366 uclass_foreach_dev(dev, uc) {
367 log(LOGC_DM, LOGL_DEBUG_CONTENT, " - checking %s\n",
369 if (ofnode_equal(dev_ofnode(dev), node)) {
377 log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n",
378 ofnode_get_name(node), *devp ? (*devp)->name : "(none)", ret);
382 #if CONFIG_IS_ENABLED(OF_CONTROL)
383 int uclass_find_device_by_phandle(enum uclass_id id, struct udevice *parent,
384 const char *name, struct udevice **devp)
392 find_phandle = dev_read_u32_default(parent, name, -1);
393 if (find_phandle <= 0)
395 ret = uclass_get(id, &uc);
399 uclass_foreach_dev(dev, uc) {
402 phandle = dev_read_phandle(dev);
404 if (phandle == find_phandle) {
414 int uclass_get_device_by_driver(enum uclass_id id,
415 const struct driver *find_drv,
416 struct udevice **devp)
422 ret = uclass_get(id, &uc);
426 uclass_foreach_dev(dev, uc) {
427 if (dev->driver == find_drv)
428 return uclass_get_device_tail(dev, 0, devp);
434 int uclass_get_device_tail(struct udevice *dev, int ret, struct udevice **devp)
440 ret = device_probe(dev);
449 int uclass_get_device(enum uclass_id id, int index, struct udevice **devp)
455 ret = uclass_find_device(id, index, &dev);
456 return uclass_get_device_tail(dev, ret, devp);
459 int uclass_get_device_by_name(enum uclass_id id, const char *name,
460 struct udevice **devp)
466 ret = uclass_find_device_by_name(id, name, &dev);
467 return uclass_get_device_tail(dev, ret, devp);
470 int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
476 ret = uclass_find_device_by_seq(id, seq, false, &dev);
477 if (ret == -ENODEV) {
479 * We didn't find it in probed devices. See if there is one
480 * that will request this seq if probed.
482 ret = uclass_find_device_by_seq(id, seq, true, &dev);
484 return uclass_get_device_tail(dev, ret, devp);
487 int uclass_get_device_by_of_offset(enum uclass_id id, int node,
488 struct udevice **devp)
494 ret = uclass_find_device_by_of_offset(id, node, &dev);
495 return uclass_get_device_tail(dev, ret, devp);
498 int uclass_get_device_by_ofnode(enum uclass_id id, ofnode node,
499 struct udevice **devp)
504 log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node));
506 ret = uclass_find_device_by_ofnode(id, node, &dev);
507 log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n",
508 ofnode_get_name(node), dev ? dev->name : "(none)", ret);
510 return uclass_get_device_tail(dev, ret, devp);
513 #if CONFIG_IS_ENABLED(OF_CONTROL)
514 int uclass_get_device_by_phandle_id(enum uclass_id id, uint phandle_id,
515 struct udevice **devp)
522 ret = uclass_get(id, &uc);
526 uclass_foreach_dev(dev, uc) {
529 phandle = dev_read_phandle(dev);
531 if (phandle == phandle_id) {
533 return uclass_get_device_tail(dev, ret, devp);
540 int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent,
541 const char *name, struct udevice **devp)
547 ret = uclass_find_device_by_phandle(id, parent, name, &dev);
548 return uclass_get_device_tail(dev, ret, devp);
552 int uclass_first_device(enum uclass_id id, struct udevice **devp)
558 ret = uclass_find_first_device(id, &dev);
561 return uclass_get_device_tail(dev, ret, devp);
564 int uclass_first_device_err(enum uclass_id id, struct udevice **devp)
568 ret = uclass_first_device(id, devp);
577 int uclass_next_device(struct udevice **devp)
579 struct udevice *dev = *devp;
583 ret = uclass_find_next_device(&dev);
586 return uclass_get_device_tail(dev, ret, devp);
589 int uclass_next_device_err(struct udevice **devp)
593 ret = uclass_next_device(devp);
602 int uclass_first_device_check(enum uclass_id id, struct udevice **devp)
607 ret = uclass_find_first_device(id, devp);
613 return device_probe(*devp);
616 int uclass_next_device_check(struct udevice **devp)
620 ret = uclass_find_next_device(devp);
626 return device_probe(*devp);
629 int uclass_first_device_drvdata(enum uclass_id id, ulong driver_data,
630 struct udevice **devp)
635 uclass_id_foreach_dev(id, dev, uc) {
636 if (dev_get_driver_data(dev) == driver_data) {
639 return device_probe(dev);
646 int uclass_bind_device(struct udevice *dev)
652 list_add_tail(&dev->uclass_node, &uc->dev_head);
655 struct uclass_driver *uc_drv = dev->parent->uclass->uc_drv;
657 if (uc_drv->child_post_bind) {
658 ret = uc_drv->child_post_bind(dev);
666 /* There is no need to undo the parent's post_bind call */
667 list_del(&dev->uclass_node);
672 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
673 int uclass_unbind_device(struct udevice *dev)
679 if (uc->uc_drv->pre_unbind) {
680 ret = uc->uc_drv->pre_unbind(dev);
685 list_del(&dev->uclass_node);
690 int uclass_resolve_seq(struct udevice *dev)
696 assert(dev->seq == -1);
697 ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, dev->req_seq,
700 dm_warn("Device '%s': seq %d is in use by '%s'\n",
701 dev->name, dev->req_seq, dup->name);
702 } else if (ret == -ENODEV) {
703 /* Our requested sequence number is available */
704 if (dev->req_seq != -1)
710 for (seq = 0; seq < DM_MAX_SEQ; seq++) {
711 ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, seq,
721 int uclass_pre_probe_device(struct udevice *dev)
723 struct uclass_driver *uc_drv;
726 uc_drv = dev->uclass->uc_drv;
727 if (uc_drv->pre_probe) {
728 ret = uc_drv->pre_probe(dev);
735 uc_drv = dev->parent->uclass->uc_drv;
736 if (uc_drv->child_pre_probe) {
737 ret = uc_drv->child_pre_probe(dev);
745 int uclass_post_probe_device(struct udevice *dev)
747 struct uclass_driver *uc_drv;
751 uc_drv = dev->parent->uclass->uc_drv;
752 if (uc_drv->child_post_probe) {
753 ret = uc_drv->child_post_probe(dev);
759 uc_drv = dev->uclass->uc_drv;
760 if (uc_drv->post_probe) {
761 ret = uc_drv->post_probe(dev);
769 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
770 int uclass_pre_remove_device(struct udevice *dev)
776 if (uc->uc_drv->pre_remove) {
777 ret = uc->uc_drv->pre_remove(dev);
786 UCLASS_DRIVER(nop) = {