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 device *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 device **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 device **devp)
167 ret = uclass_find_device(id, index, &dev);
171 ret = device_probe(dev);
180 int uclass_first_device(enum uclass_id id, struct device **devp)
187 ret = uclass_get(id, &uc);
190 if (list_empty(&uc->dev_head))
193 dev = list_first_entry(&uc->dev_head, struct device, uclass_node);
194 ret = device_probe(dev);
202 int uclass_next_device(struct device **devp)
204 struct device *dev = *devp;
208 if (list_is_last(&dev->uclass_node, &dev->uclass->dev_head))
211 dev = list_entry(dev->uclass_node.next, struct device, uclass_node);
212 ret = device_probe(dev);
220 int uclass_bind_device(struct device *dev)
227 list_add_tail(&dev->uclass_node, &uc->dev_head);
229 if (uc->uc_drv->post_bind) {
230 ret = uc->uc_drv->post_bind(dev);
232 list_del(&dev->uclass_node);
240 int uclass_unbind_device(struct device *dev)
246 if (uc->uc_drv->pre_unbind) {
247 ret = uc->uc_drv->pre_unbind(dev);
252 list_del(&dev->uclass_node);
256 int uclass_post_probe_device(struct device *dev)
258 struct uclass_driver *uc_drv = dev->uclass->uc_drv;
260 if (uc_drv->post_probe)
261 return uc_drv->post_probe(dev);
266 int uclass_pre_remove_device(struct device *dev)
268 struct uclass_driver *uc_drv;
274 if (uc->uc_drv->pre_remove) {
275 ret = uc->uc_drv->pre_remove(dev);
279 if (uc_drv->per_device_auto_alloc_size) {
280 free(dev->uclass_priv);
281 dev->uclass_priv = NULL;