4 * Copyright (c) 2013 Google, Inc
7 * Pavel Herrmann <morpheus.ibis@gmail.com>
9 * SPDX-License-Identifier: GPL-2.0+
15 #include <dm/device.h>
16 #include <dm/device-internal.h>
18 #include <dm/platdata.h>
19 #include <dm/uclass.h>
20 #include <dm/uclass-internal.h>
22 #include <linux/err.h>
23 #include <linux/list.h>
25 DECLARE_GLOBAL_DATA_PTR;
28 * device_chld_unbind() - Unbind all device's children from the device
30 * On error, the function continues to unbind all children, and reports the
33 * @dev: The device that is to be stripped of its children
34 * @return 0 on success, -ve on error
36 static int device_chld_unbind(struct udevice *dev)
38 struct udevice *pos, *n;
39 int ret, saved_ret = 0;
43 list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
44 ret = device_unbind(pos);
45 if (ret && !saved_ret)
53 * device_chld_remove() - Stop all device's children
54 * @dev: The device whose children are to be removed
55 * @return 0 on success, -ve on error
57 static int device_chld_remove(struct udevice *dev)
59 struct udevice *pos, *n;
64 list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
65 ret = device_remove(pos);
73 int device_bind(struct udevice *parent, struct driver *drv, const char *name,
74 void *platdata, int of_offset, struct udevice **devp)
84 ret = uclass_get(drv->id, &uc);
88 dev = calloc(1, sizeof(struct udevice));
92 INIT_LIST_HEAD(&dev->sibling_node);
93 INIT_LIST_HEAD(&dev->child_head);
94 INIT_LIST_HEAD(&dev->uclass_node);
95 dev->platdata = platdata;
97 dev->of_offset = of_offset;
103 * For some devices, such as a SPI or I2C bus, the 'reg' property
104 * is a reasonable indicator of the sequence number. But if there is
105 * an alias, we use that in preference. In any case, this is just
106 * a 'requested' sequence, and will be resolved (and ->seq updated)
107 * when the device is probed.
110 #ifdef CONFIG_OF_CONTROL
111 dev->req_seq = fdtdec_get_int(gd->fdt_blob, of_offset, "reg", -1);
112 if (uc->uc_drv->name && of_offset != -1) {
113 fdtdec_get_alias_seq(gd->fdt_blob, uc->uc_drv->name, of_offset,
119 if (!dev->platdata && drv->platdata_auto_alloc_size)
120 dev->flags |= DM_FLAG_ALLOC_PDATA;
122 /* put dev into parent's successor list */
124 list_add_tail(&dev->sibling_node, &parent->child_head);
126 ret = uclass_bind_device(dev);
130 /* if we fail to bind we remove device from successors and free it */
132 ret = drv->bind(dev);
134 if (uclass_unbind_device(dev)) {
135 dm_warn("Failed to unbind dev '%s' on error path\n",
142 dm_dbg("Bound device %s to %s\n", dev->name, parent->name);
148 list_del(&dev->sibling_node);
153 int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
154 const struct driver_info *info, struct udevice **devp)
158 drv = lists_driver_lookup_name(info->name);
161 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
164 return device_bind(parent, drv, info->name, (void *)info->platdata,
168 int device_unbind(struct udevice *dev)
176 if (dev->flags & DM_FLAG_ACTIVATED)
183 ret = drv->unbind(dev);
188 ret = device_chld_unbind(dev);
192 ret = uclass_unbind_device(dev);
197 list_del(&dev->sibling_node);
204 * device_free() - Free memory buffers allocated by a device
205 * @dev: Device that is to be started
207 static void device_free(struct udevice *dev)
211 if (dev->driver->priv_auto_alloc_size) {
215 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
217 dev->platdata = NULL;
219 size = dev->uclass->uc_drv->per_device_auto_alloc_size;
221 free(dev->uclass_priv);
222 dev->uclass_priv = NULL;
225 size = dev->parent->driver->per_child_auto_alloc_size;
227 free(dev->parent_priv);
228 dev->parent_priv = NULL;
233 int device_probe(struct udevice *dev)
243 if (dev->flags & DM_FLAG_ACTIVATED)
249 /* Allocate private data and platdata if requested */
250 if (drv->priv_auto_alloc_size) {
251 dev->priv = calloc(1, drv->priv_auto_alloc_size);
257 /* Allocate private data if requested */
258 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
259 dev->platdata = calloc(1, drv->platdata_auto_alloc_size);
260 if (!dev->platdata) {
265 size = dev->uclass->uc_drv->per_device_auto_alloc_size;
267 dev->uclass_priv = calloc(1, size);
268 if (!dev->uclass_priv) {
274 /* Ensure all parents are probed */
276 size = dev->parent->driver->per_child_auto_alloc_size;
278 dev->parent_priv = calloc(1, size);
279 if (!dev->parent_priv) {
285 ret = device_probe(dev->parent);
290 seq = uclass_resolve_seq(dev);
297 if (dev->parent && dev->parent->driver->child_pre_probe) {
298 ret = dev->parent->driver->child_pre_probe(dev);
303 if (drv->ofdata_to_platdata && dev->of_offset >= 0) {
304 ret = drv->ofdata_to_platdata(dev);
310 ret = drv->probe(dev);
315 dev->flags |= DM_FLAG_ACTIVATED;
317 ret = uclass_post_probe_device(dev);
319 dev->flags &= ~DM_FLAG_ACTIVATED;
325 if (device_remove(dev)) {
326 dm_warn("%s: Device '%s' failed to remove on error path\n",
327 __func__, dev->name);
336 int device_remove(struct udevice *dev)
344 if (!(dev->flags & DM_FLAG_ACTIVATED))
350 ret = uclass_pre_remove_device(dev);
354 ret = device_chld_remove(dev);
359 ret = drv->remove(dev);
364 if (dev->parent && dev->parent->driver->child_post_remove) {
365 ret = dev->parent->driver->child_post_remove(dev);
367 dm_warn("%s: Device '%s' failed child_post_remove()",
368 __func__, dev->name);
375 dev->flags &= ~DM_FLAG_ACTIVATED;
380 /* We can't put the children back */
381 dm_warn("%s: Device '%s' failed to remove, but children are gone\n",
382 __func__, dev->name);
384 ret = uclass_post_probe_device(dev);
386 dm_warn("%s: Device '%s' failed to post_probe on error path\n",
387 __func__, dev->name);
393 void *dev_get_platdata(struct udevice *dev)
396 dm_warn("%s: null device", __func__);
400 return dev->platdata;
403 void *dev_get_priv(struct udevice *dev)
406 dm_warn("%s: null device", __func__);
413 void *dev_get_parentdata(struct udevice *dev)
416 dm_warn("%s: null device", __func__);
420 return dev->parent_priv;
423 static int device_get_device_tail(struct udevice *dev, int ret,
424 struct udevice **devp)
429 ret = device_probe(dev);
438 int device_get_child(struct udevice *parent, int index, struct udevice **devp)
442 list_for_each_entry(dev, &parent->child_head, sibling_node) {
444 return device_get_device_tail(dev, 0, devp);
450 int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
451 bool find_req_seq, struct udevice **devp)
456 if (seq_or_req_seq == -1)
459 list_for_each_entry(dev, &parent->child_head, sibling_node) {
460 if ((find_req_seq ? dev->req_seq : dev->seq) ==
470 int device_get_child_by_seq(struct udevice *parent, int seq,
471 struct udevice **devp)
477 ret = device_find_child_by_seq(parent, seq, false, &dev);
478 if (ret == -ENODEV) {
480 * We didn't find it in probed devices. See if there is one
481 * that will request this seq if probed.
483 ret = device_find_child_by_seq(parent, seq, true, &dev);
485 return device_get_device_tail(dev, ret, devp);
488 int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
489 struct udevice **devp)
495 list_for_each_entry(dev, &parent->child_head, sibling_node) {
496 if (dev->of_offset == of_offset) {
505 int device_get_child_by_of_offset(struct udevice *parent, int seq,
506 struct udevice **devp)
512 ret = device_find_child_by_of_offset(parent, seq, &dev);
513 return device_get_device_tail(dev, ret, devp);