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
16 #include <asm/global_data.h>
17 #include <dm/device.h>
18 #include <dm/device-internal.h>
20 #include <dm/uclass.h>
21 #include <dm/uclass-internal.h>
24 DECLARE_GLOBAL_DATA_PTR;
26 struct uclass *uclass_find(enum uclass_id key)
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
37 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
38 if (uc->uc_drv->id == key)
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
51 * The new uclass is added to the list. There must be only one uclass for
54 static int uclass_add(enum uclass_id id, struct uclass **ucp)
56 struct uclass_driver *uc_drv;
61 uc_drv = lists_uclass_lookup(id);
63 debug("Cannot find uclass for id %d: please add the UCLASS_DRIVER() declaration for this UCLASS_... id\n",
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.
72 uc = calloc(1, sizeof(*uc));
75 if (uc_drv->priv_auto) {
78 ptr = calloc(1, uc_drv->priv_auto);
83 uclass_set_priv(uc, ptr);
86 INIT_LIST_HEAD(&uc->sibling_node);
87 INIT_LIST_HEAD(&uc->dev_head);
88 list_add(&uc->sibling_node, DM_UCLASS_ROOT_NON_CONST);
91 ret = uc_drv->init(uc);
100 if (uc_drv->priv_auto) {
101 free(uclass_get_priv(uc));
102 uclass_set_priv(uc, NULL);
104 list_del(&uc->sibling_node);
111 int uclass_destroy(struct uclass *uc)
113 struct uclass_driver *uc_drv;
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.
123 while (!list_empty(&uc->dev_head)) {
124 dev = list_first_entry(&uc->dev_head, struct udevice,
126 ret = device_remove(dev, DM_REMOVE_NORMAL | DM_REMOVE_NO_PD);
128 return log_msg_ret("remove", ret);
129 ret = device_unbind(dev);
131 return log_msg_ret("unbind", ret);
137 list_del(&uc->sibling_node);
138 if (uc_drv->priv_auto)
139 free(uclass_get_priv(uc));
145 int uclass_get(enum uclass_id id, struct uclass **ucp)
149 /* Immediately fail if driver model is not set up */
150 if (!gd->uclass_root)
153 uc = uclass_find(id);
155 if (CONFIG_IS_ENABLED(OF_PLATDATA_INST))
157 return uclass_add(id, ucp);
164 const char *uclass_get_name(enum uclass_id id)
168 if (uclass_get(id, &uc))
170 return uc->uc_drv->name;
173 void *uclass_get_priv(const struct uclass *uc)
178 void uclass_set_priv(struct uclass *uc, void *priv)
183 enum uclass_id uclass_get_by_namelen(const char *name, int len)
187 for (i = 0; i < UCLASS_COUNT; i++) {
188 struct uclass_driver *uc_drv = lists_uclass_lookup(i);
190 if (uc_drv && !strncmp(uc_drv->name, name, len) &&
191 strlen(uc_drv->name) == len)
195 return UCLASS_INVALID;
198 enum uclass_id uclass_get_by_name(const char *name)
200 return uclass_get_by_namelen(name, strlen(name));
203 int dev_get_uclass_index(struct udevice *dev, struct uclass **ucp)
205 struct udevice *iter;
206 struct uclass *uc = dev->uclass;
209 if (list_empty(&uc->dev_head))
212 uclass_foreach_dev(iter, uc) {
224 int uclass_find_device(enum uclass_id id, int index, struct udevice **devp)
231 ret = uclass_get(id, &uc);
234 if (list_empty(&uc->dev_head))
237 uclass_foreach_dev(dev, uc) {
247 int uclass_find_first_device(enum uclass_id id, struct udevice **devp)
253 ret = uclass_get(id, &uc);
256 if (list_empty(&uc->dev_head))
259 *devp = list_first_entry(&uc->dev_head, struct udevice, uclass_node);
264 int uclass_find_next_device(struct udevice **devp)
266 struct udevice *dev = *devp;
269 if (list_is_last(&dev->uclass_node, &dev->uclass->dev_head))
272 *devp = list_entry(dev->uclass_node.next, struct udevice, uclass_node);
277 int uclass_find_device_by_name(enum uclass_id id, const char *name,
278 struct udevice **devp)
287 ret = uclass_get(id, &uc);
291 uclass_foreach_dev(dev, uc) {
292 if (!strcmp(dev->name, name)) {
301 int uclass_find_next_free_seq(struct uclass *uc)
306 /* If using aliases, start with the highest alias value */
307 if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
308 (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS))
309 max = dev_read_alias_highest_id(uc->uc_drv->name);
311 /* Avoid conflict with existing devices */
312 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
317 * At this point, max will be -1 if there are no existing aliases or
324 int uclass_find_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
331 log_debug("%d\n", seq);
334 ret = uclass_get(id, &uc);
338 uclass_foreach_dev(dev, uc) {
339 log_debug(" - %d '%s'\n", dev->seq_, dev->name);
340 if (dev->seq_ == seq) {
342 log_debug(" - found\n");
346 log_debug(" - not found\n");
351 int uclass_find_device_by_of_offset(enum uclass_id id, int node,
352 struct udevice **devp)
361 ret = uclass_get(id, &uc);
365 uclass_foreach_dev(dev, uc) {
366 if (dev_of_offset(dev) == node) {
375 int uclass_find_device_by_ofnode(enum uclass_id id, ofnode node,
376 struct udevice **devp)
382 log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node));
384 if (!ofnode_valid(node))
386 ret = uclass_get(id, &uc);
390 uclass_foreach_dev(dev, uc) {
391 log(LOGC_DM, LOGL_DEBUG_CONTENT, " - checking %s\n",
393 if (ofnode_equal(dev_ofnode(dev), node)) {
401 log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n",
402 ofnode_get_name(node), *devp ? (*devp)->name : "(none)", ret);
406 #if CONFIG_IS_ENABLED(OF_REAL)
407 int uclass_find_device_by_phandle(enum uclass_id id, struct udevice *parent,
408 const char *name, struct udevice **devp)
416 find_phandle = dev_read_u32_default(parent, name, -1);
417 if (find_phandle <= 0)
419 ret = uclass_get(id, &uc);
423 uclass_foreach_dev(dev, uc) {
426 phandle = dev_read_phandle(dev);
428 if (phandle == find_phandle) {
438 int uclass_get_device_by_driver(enum uclass_id id,
439 const struct driver *find_drv,
440 struct udevice **devp)
446 ret = uclass_get(id, &uc);
450 uclass_foreach_dev(dev, uc) {
451 if (dev->driver == find_drv)
452 return uclass_get_device_tail(dev, 0, devp);
458 int uclass_get_device_tail(struct udevice *dev, int ret, struct udevice **devp)
464 ret = device_probe(dev);
473 int uclass_get_device(enum uclass_id id, int index, struct udevice **devp)
479 ret = uclass_find_device(id, index, &dev);
480 return uclass_get_device_tail(dev, ret, devp);
483 int uclass_get_device_by_name(enum uclass_id id, const char *name,
484 struct udevice **devp)
490 ret = uclass_find_device_by_name(id, name, &dev);
491 return uclass_get_device_tail(dev, ret, devp);
494 int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
500 ret = uclass_find_device_by_seq(id, seq, &dev);
502 return uclass_get_device_tail(dev, ret, devp);
505 int uclass_get_device_by_of_offset(enum uclass_id id, int node,
506 struct udevice **devp)
512 ret = uclass_find_device_by_of_offset(id, node, &dev);
513 return uclass_get_device_tail(dev, ret, devp);
516 int uclass_get_device_by_ofnode(enum uclass_id id, ofnode node,
517 struct udevice **devp)
522 log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node));
524 ret = uclass_find_device_by_ofnode(id, node, &dev);
525 log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n",
526 ofnode_get_name(node), dev ? dev->name : "(none)", ret);
528 return uclass_get_device_tail(dev, ret, devp);
531 #if CONFIG_IS_ENABLED(OF_CONTROL)
532 int uclass_get_device_by_phandle_id(enum uclass_id id, uint phandle_id,
533 struct udevice **devp)
540 ret = uclass_get(id, &uc);
544 uclass_foreach_dev(dev, uc) {
547 phandle = dev_read_phandle(dev);
549 if (phandle == phandle_id) {
551 return uclass_get_device_tail(dev, ret, devp);
558 int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent,
559 const char *name, struct udevice **devp)
565 ret = uclass_find_device_by_phandle(id, parent, name, &dev);
566 return uclass_get_device_tail(dev, ret, devp);
570 int uclass_first_device(enum uclass_id id, struct udevice **devp)
576 ret = uclass_find_first_device(id, &dev);
579 return uclass_get_device_tail(dev, ret, devp);
582 int uclass_first_device_err(enum uclass_id id, struct udevice **devp)
586 ret = uclass_first_device(id, devp);
595 int uclass_next_device(struct udevice **devp)
597 struct udevice *dev = *devp;
601 ret = uclass_find_next_device(&dev);
604 return uclass_get_device_tail(dev, ret, devp);
607 int uclass_next_device_err(struct udevice **devp)
611 ret = uclass_next_device(devp);
620 int uclass_first_device_check(enum uclass_id id, struct udevice **devp)
625 ret = uclass_find_first_device(id, devp);
631 return device_probe(*devp);
634 int uclass_next_device_check(struct udevice **devp)
638 ret = uclass_find_next_device(devp);
644 return device_probe(*devp);
647 int uclass_get_count(void)
649 const struct uclass *uc;
653 list_for_each_entry(uc, gd->uclass_root, sibling_node)
660 int uclass_first_device_drvdata(enum uclass_id id, ulong driver_data,
661 struct udevice **devp)
666 uclass_id_foreach_dev(id, dev, uc) {
667 if (dev_get_driver_data(dev) == driver_data) {
670 return device_probe(dev);
677 int uclass_bind_device(struct udevice *dev)
683 list_add_tail(&dev->uclass_node, &uc->dev_head);
686 struct uclass_driver *uc_drv = dev->parent->uclass->uc_drv;
688 if (uc_drv->child_post_bind) {
689 ret = uc_drv->child_post_bind(dev);
697 /* There is no need to undo the parent's post_bind call */
698 list_del(&dev->uclass_node);
703 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
704 int uclass_pre_unbind_device(struct udevice *dev)
710 if (uc->uc_drv->pre_unbind) {
711 ret = uc->uc_drv->pre_unbind(dev);
719 int uclass_unbind_device(struct udevice *dev)
721 list_del(&dev->uclass_node);
727 int uclass_pre_probe_device(struct udevice *dev)
729 struct uclass_driver *uc_drv;
732 uc_drv = dev->uclass->uc_drv;
733 if (uc_drv->pre_probe) {
734 ret = uc_drv->pre_probe(dev);
741 uc_drv = dev->parent->uclass->uc_drv;
742 if (uc_drv->child_pre_probe) {
743 ret = uc_drv->child_pre_probe(dev);
751 int uclass_post_probe_device(struct udevice *dev)
753 struct uclass_driver *uc_drv;
757 uc_drv = dev->parent->uclass->uc_drv;
758 if (uc_drv->child_post_probe) {
759 ret = uc_drv->child_post_probe(dev);
765 uc_drv = dev->uclass->uc_drv;
766 if (uc_drv->post_probe) {
767 ret = uc_drv->post_probe(dev);
775 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
776 int uclass_pre_remove_device(struct udevice *dev)
782 if (uc->uc_drv->pre_remove) {
783 ret = uc->uc_drv->pre_remove(dev);
792 int uclass_probe_all(enum uclass_id id)
797 ret = uclass_first_device(id, &dev);
801 /* Scanning uclass to probe all devices */
803 ret = uclass_next_device(&dev);
811 int uclass_id_count(enum uclass_id id)
817 uclass_id_foreach_dev(id, dev, uc)
823 UCLASS_DRIVER(nop) = {