2 * Copyright (c) 2013 Google, Inc
5 * Pavel Herrmann <morpheus.ibis@gmail.com>
7 * SPDX-License-Identifier: GPL-2.0+
13 #include <dm/device.h>
14 #include <dm/device-internal.h>
16 #include <dm/uclass.h>
17 #include <dm/uclass-internal.h>
20 DECLARE_GLOBAL_DATA_PTR;
22 struct uclass *uclass_find(enum uclass_id key)
27 * TODO(sjg@chromium.org): Optimise this, perhaps moving the found
28 * node to the start of the list, or creating a linear array mapping
31 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
32 if (uc->uc_drv->id == key)
40 * uclass_add() - Create new uclass in list
41 * @id: Id number to create
42 * @ucp: Returns pointer to uclass, or NULL on error
43 * @return 0 on success, -ve on error
45 * The new uclass is added to the list. There must be only one uclass for
48 static int uclass_add(enum uclass_id id, struct uclass **ucp)
50 struct uclass_driver *uc_drv;
55 uc_drv = lists_uclass_lookup(id);
57 dm_warn("Cannot find uclass for id %d: please add the UCLASS_DRIVER() declaration for this UCLASS_... id\n",
62 dm_warn("No ops for uclass id %d\n", id);
65 uc = calloc(1, sizeof(*uc));
68 if (uc_drv->priv_auto_alloc_size) {
69 uc->priv = calloc(1, uc_drv->priv_auto_alloc_size);
76 INIT_LIST_HEAD(&uc->sibling_node);
77 INIT_LIST_HEAD(&uc->dev_head);
78 list_add(&uc->sibling_node, &gd->uclass_root);
81 ret = uc_drv->init(uc);
90 if (uc_drv->priv_auto_alloc_size) {
94 list_del(&uc->sibling_node);
101 int uclass_destroy(struct uclass *uc)
103 struct uclass_driver *uc_drv;
104 struct udevice *dev, *tmp;
107 list_for_each_entry_safe(dev, tmp, &uc->dev_head, uclass_node) {
108 ret = device_remove(dev);
111 ret = device_unbind(dev);
119 list_del(&uc->sibling_node);
120 if (uc_drv->priv_auto_alloc_size)
127 int uclass_get(enum uclass_id id, struct uclass **ucp)
132 uc = uclass_find(id);
134 return uclass_add(id, ucp);
140 int uclass_find_device(enum uclass_id id, int index, struct udevice **devp)
147 ret = uclass_get(id, &uc);
151 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
161 int uclass_get_device(enum uclass_id id, int index, struct udevice **devp)
167 ret = uclass_find_device(id, index, &dev);
171 ret = device_probe(dev);
180 int uclass_first_device(enum uclass_id id, struct udevice **devp)
187 ret = uclass_get(id, &uc);
190 if (list_empty(&uc->dev_head))
193 dev = list_first_entry(&uc->dev_head, struct udevice, uclass_node);
194 ret = device_probe(dev);
202 int uclass_next_device(struct udevice **devp)
204 struct udevice *dev = *devp;
208 if (list_is_last(&dev->uclass_node, &dev->uclass->dev_head))
211 dev = list_entry(dev->uclass_node.next, struct udevice,
213 ret = device_probe(dev);
221 int uclass_bind_device(struct udevice *dev)
228 list_add_tail(&dev->uclass_node, &uc->dev_head);
230 if (uc->uc_drv->post_bind) {
231 ret = uc->uc_drv->post_bind(dev);
233 list_del(&dev->uclass_node);
241 int uclass_unbind_device(struct udevice *dev)
247 if (uc->uc_drv->pre_unbind) {
248 ret = uc->uc_drv->pre_unbind(dev);
253 list_del(&dev->uclass_node);
257 int uclass_post_probe_device(struct udevice *dev)
259 struct uclass_driver *uc_drv = dev->uclass->uc_drv;
261 if (uc_drv->post_probe)
262 return uc_drv->post_probe(dev);
267 int uclass_pre_remove_device(struct udevice *dev)
269 struct uclass_driver *uc_drv;
275 if (uc->uc_drv->pre_remove) {
276 ret = uc->uc_drv->pre_remove(dev);
280 if (uc_drv->per_device_auto_alloc_size) {
281 free(dev->uclass_priv);
282 dev->uclass_priv = NULL;