1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2014 Google, Inc
8 * Pavel Herrmann <morpheus.ibis@gmail.com>
15 #include <dm/device.h>
16 #include <dm/device-internal.h>
17 #include <dm/uclass.h>
18 #include <dm/uclass-internal.h>
20 #include <power-domain.h>
22 int device_chld_unbind(struct udevice *dev, struct driver *drv)
24 struct udevice *pos, *n;
25 int ret, saved_ret = 0;
29 list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
30 if (drv && (pos->driver != drv))
33 ret = device_unbind(pos);
34 if (ret && !saved_ret) {
35 log_warning("device '%s' failed to unbind\n",
41 return log_ret(saved_ret);
44 int device_chld_remove(struct udevice *dev, struct driver *drv,
47 struct udevice *pos, *n;
52 list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
53 if (drv && (pos->driver != drv))
56 ret = device_remove(pos, flags);
64 int device_unbind(struct udevice *dev)
66 const struct driver *drv;
70 return log_msg_ret("dev", -EINVAL);
72 if (dev->flags & DM_FLAG_ACTIVATED)
73 return log_msg_ret("active", -EINVAL);
75 if (!(dev->flags & DM_FLAG_BOUND))
76 return log_msg_ret("not-bound", -EINVAL);
82 ret = drv->unbind(dev);
84 return log_msg_ret("unbind", ret);
87 ret = device_chld_unbind(dev, NULL);
89 return log_msg_ret("child unbind", ret);
91 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
95 if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
96 free(dev->uclass_platdata);
97 dev->uclass_platdata = NULL;
99 if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
100 free(dev->parent_platdata);
101 dev->parent_platdata = NULL;
103 ret = uclass_unbind_device(dev);
105 return log_msg_ret("uc", ret);
108 list_del(&dev->sibling_node);
110 devres_release_all(dev);
112 if (dev->flags & DM_FLAG_NAME_ALLOCED)
113 free((char *)dev->name);
120 * device_free() - Free memory buffers allocated by a device
121 * @dev: Device that is to be started
123 void device_free(struct udevice *dev)
127 if (dev->driver->priv_auto_alloc_size) {
131 size = dev->uclass->uc_drv->per_device_auto_alloc_size;
133 free(dev->uclass_priv);
134 dev->uclass_priv = NULL;
137 size = dev->parent->driver->per_child_auto_alloc_size;
139 size = dev->parent->uclass->uc_drv->
140 per_child_auto_alloc_size;
143 free(dev->parent_priv);
144 dev->parent_priv = NULL;
147 dev->flags &= ~DM_FLAG_PLATDATA_VALID;
149 devres_release_probe(dev);
152 static bool flags_remove(uint flags, uint drv_flags)
154 if ((flags & DM_REMOVE_NORMAL) ||
155 (flags && (drv_flags & (DM_FLAG_ACTIVE_DMA | DM_FLAG_OS_PREPARE))))
161 int device_remove(struct udevice *dev, uint flags)
163 const struct driver *drv;
169 if (!(dev->flags & DM_FLAG_ACTIVATED))
175 ret = uclass_pre_remove_device(dev);
179 ret = device_chld_remove(dev, NULL, flags);
184 * Remove the device if called with the "normal" remove flag set,
185 * or if the remove flag matches any of the drivers remove flags
187 if (drv->remove && flags_remove(flags, drv->flags)) {
188 ret = drv->remove(dev);
193 if (dev->parent && dev->parent->driver->child_post_remove) {
194 ret = dev->parent->driver->child_post_remove(dev);
196 dm_warn("%s: Device '%s' failed child_post_remove()",
197 __func__, dev->name);
201 if (!(flags & DM_REMOVE_NO_PD) &&
203 (DM_FLAG_DEFAULT_PD_CTRL_OFF | DM_FLAG_REMOVE_WITH_PD_ON)) &&
204 dev != gd->cur_serial_dev)
205 dev_power_domain_off(dev);
207 if (flags_remove(flags, drv->flags)) {
211 dev->flags &= ~DM_FLAG_ACTIVATED;
217 /* We can't put the children back */
218 dm_warn("%s: Device '%s' failed to remove, but children are gone\n",
219 __func__, dev->name);
221 ret = uclass_post_probe_device(dev);
223 dm_warn("%s: Device '%s' failed to post_probe on error path\n",
224 __func__, dev->name);