4 * Copyright (c) 2014 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>
17 #include <dm/uclass.h>
18 #include <dm/uclass-internal.h>
22 * device_chld_unbind() - Unbind all device's children from the device
24 * On error, the function continues to unbind all children, and reports the
27 * @dev: The device that is to be stripped of its children
28 * @return 0 on success, -ve on error
30 static int device_chld_unbind(struct udevice *dev)
32 struct udevice *pos, *n;
33 int ret, saved_ret = 0;
37 list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
38 ret = device_unbind(pos);
39 if (ret && !saved_ret)
47 * device_chld_remove() - Stop all device's children
48 * @dev: The device whose children are to be removed
49 * @return 0 on success, -ve on error
51 static int device_chld_remove(struct udevice *dev)
53 struct udevice *pos, *n;
58 list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
59 ret = device_remove(pos);
67 int device_unbind(struct udevice *dev)
75 if (dev->flags & DM_FLAG_ACTIVATED)
82 ret = drv->unbind(dev);
87 ret = device_chld_unbind(dev);
91 ret = uclass_unbind_device(dev);
96 list_del(&dev->sibling_node);
103 * device_free() - Free memory buffers allocated by a device
104 * @dev: Device that is to be started
106 void device_free(struct udevice *dev)
110 if (dev->driver->priv_auto_alloc_size) {
114 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
116 dev->platdata = NULL;
118 size = dev->uclass->uc_drv->per_device_auto_alloc_size;
120 free(dev->uclass_priv);
121 dev->uclass_priv = NULL;
124 size = dev->parent->driver->per_child_auto_alloc_size;
126 free(dev->parent_priv);
127 dev->parent_priv = NULL;
132 int device_remove(struct udevice *dev)
140 if (!(dev->flags & DM_FLAG_ACTIVATED))
146 ret = uclass_pre_remove_device(dev);
150 ret = device_chld_remove(dev);
155 ret = drv->remove(dev);
160 if (dev->parent && dev->parent->driver->child_post_remove) {
161 ret = dev->parent->driver->child_post_remove(dev);
163 dm_warn("%s: Device '%s' failed child_post_remove()",
164 __func__, dev->name);
171 dev->flags &= ~DM_FLAG_ACTIVATED;
176 /* We can't put the children back */
177 dm_warn("%s: Device '%s' failed to remove, but children are gone\n",
178 __func__, dev->name);
180 ret = uclass_post_probe_device(dev);
182 dm_warn("%s: Device '%s' failed to post_probe on error path\n",
183 __func__, dev->name);