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)
29 * TODO(sjg@chromium.org): Optimise this, perhaps moving the found
30 * node to the start of the list, or creating a linear array mapping
33 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
34 if (uc->uc_drv->id == key)
42 * uclass_add() - Create new uclass in list
43 * @id: Id number to create
44 * @ucp: Returns pointer to uclass, or NULL on error
45 * @return 0 on success, -ve on error
47 * The new uclass is added to the list. There must be only one uclass for
50 static int uclass_add(enum uclass_id id, struct uclass **ucp)
52 struct uclass_driver *uc_drv;
57 uc_drv = lists_uclass_lookup(id);
59 dm_warn("Cannot find uclass for id %d: please add the UCLASS_DRIVER() declaration for this UCLASS_... id\n",
63 uc = calloc(1, sizeof(*uc));
66 if (uc_drv->priv_auto_alloc_size) {
67 uc->priv = calloc(1, uc_drv->priv_auto_alloc_size);
74 INIT_LIST_HEAD(&uc->sibling_node);
75 INIT_LIST_HEAD(&uc->dev_head);
76 list_add(&uc->sibling_node, &DM_UCLASS_ROOT_NON_CONST);
79 ret = uc_drv->init(uc);
88 if (uc_drv->priv_auto_alloc_size) {
92 list_del(&uc->sibling_node);
99 int uclass_destroy(struct uclass *uc)
101 struct uclass_driver *uc_drv;
102 struct udevice *dev, *tmp;
105 list_for_each_entry_safe(dev, tmp, &uc->dev_head, uclass_node) {
106 ret = device_remove(dev);
109 ret = device_unbind(dev);
117 list_del(&uc->sibling_node);
118 if (uc_drv->priv_auto_alloc_size)
125 int uclass_get(enum uclass_id id, struct uclass **ucp)
130 uc = uclass_find(id);
132 return uclass_add(id, ucp);
138 int uclass_find_device(enum uclass_id id, int index, struct udevice **devp)
145 ret = uclass_get(id, &uc);
149 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
159 int uclass_find_device_by_seq(enum uclass_id id, int seq_or_req_seq,
160 bool find_req_seq, struct udevice **devp)
167 debug("%s: %d %d\n", __func__, find_req_seq, seq_or_req_seq);
168 if (seq_or_req_seq == -1)
170 ret = uclass_get(id, &uc);
174 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
175 debug(" - %d %d\n", dev->req_seq, dev->seq);
176 if ((find_req_seq ? dev->req_seq : dev->seq) ==
183 debug(" - not found\n");
188 static int uclass_find_device_by_of_offset(enum uclass_id id, int node,
189 struct udevice **devp)
198 ret = uclass_get(id, &uc);
202 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
203 if (dev->of_offset == node) {
213 * uclass_get_device_tail() - handle the end of a get_device call
215 * This handles returning an error or probing a device as needed.
217 * @dev: Device that needs to be probed
218 * @ret: Error to return. If non-zero then the device is not probed
219 * @devp: Returns the value of 'dev' if there is no error
220 * @return ret, if non-zero, else the result of the device_probe() call
222 static int uclass_get_device_tail(struct udevice *dev, int ret,
223 struct udevice **devp)
228 ret = device_probe(dev);
237 int uclass_get_device(enum uclass_id id, int index, struct udevice **devp)
243 ret = uclass_find_device(id, index, &dev);
244 return uclass_get_device_tail(dev, ret, devp);
247 int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
253 ret = uclass_find_device_by_seq(id, seq, false, &dev);
254 if (ret == -ENODEV) {
256 * We didn't find it in probed devices. See if there is one
257 * that will request this seq if probed.
259 ret = uclass_find_device_by_seq(id, seq, true, &dev);
261 return uclass_get_device_tail(dev, ret, devp);
264 int uclass_get_device_by_of_offset(enum uclass_id id, int node,
265 struct udevice **devp)
271 ret = uclass_find_device_by_of_offset(id, node, &dev);
272 return uclass_get_device_tail(dev, ret, devp);
275 int uclass_first_device(enum uclass_id id, struct udevice **devp)
282 ret = uclass_get(id, &uc);
285 if (list_empty(&uc->dev_head))
288 dev = list_first_entry(&uc->dev_head, struct udevice, uclass_node);
289 ret = device_probe(dev);
297 int uclass_next_device(struct udevice **devp)
299 struct udevice *dev = *devp;
303 if (list_is_last(&dev->uclass_node, &dev->uclass->dev_head))
306 dev = list_entry(dev->uclass_node.next, struct udevice,
308 ret = device_probe(dev);
316 int uclass_bind_device(struct udevice *dev)
323 list_add_tail(&dev->uclass_node, &uc->dev_head);
325 if (uc->uc_drv->post_bind) {
326 ret = uc->uc_drv->post_bind(dev);
328 list_del(&dev->uclass_node);
336 int uclass_unbind_device(struct udevice *dev)
342 if (uc->uc_drv->pre_unbind) {
343 ret = uc->uc_drv->pre_unbind(dev);
348 list_del(&dev->uclass_node);
352 int uclass_resolve_seq(struct udevice *dev)
358 assert(dev->seq == -1);
359 ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, dev->req_seq,
362 dm_warn("Device '%s': seq %d is in use by '%s'\n",
363 dev->name, dev->req_seq, dup->name);
364 } else if (ret == -ENODEV) {
365 /* Our requested sequence number is available */
366 if (dev->req_seq != -1)
372 for (seq = 0; seq < DM_MAX_SEQ; seq++) {
373 ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, seq,
383 int uclass_post_probe_device(struct udevice *dev)
385 struct uclass_driver *uc_drv = dev->uclass->uc_drv;
387 if (uc_drv->post_probe)
388 return uc_drv->post_probe(dev);
393 int uclass_pre_remove_device(struct udevice *dev)
395 struct uclass_driver *uc_drv;
401 if (uc->uc_drv->pre_remove) {
402 ret = uc->uc_drv->pre_remove(dev);
406 if (uc_drv->per_device_auto_alloc_size) {
407 free(dev->uclass_priv);
408 dev->uclass_priv = NULL;