1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2014 Google, Inc
8 * Pavel Herrmann <morpheus.ibis@gmail.com>
14 #include <dm/device.h>
15 #include <dm/device-internal.h>
16 #include <dm/uclass.h>
17 #include <dm/uclass-internal.h>
19 #include <power-domain.h>
21 int device_chld_unbind(struct udevice *dev, struct driver *drv)
23 struct udevice *pos, *n;
24 int ret, saved_ret = 0;
28 list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
29 if (drv && (pos->driver != drv))
32 ret = device_unbind(pos);
33 if (ret && !saved_ret)
40 int device_chld_remove(struct udevice *dev, struct driver *drv,
43 struct udevice *pos, *n;
48 list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
49 if (drv && (pos->driver != drv))
52 ret = device_remove(pos, flags);
60 int device_unbind(struct udevice *dev)
62 const struct driver *drv;
68 if (dev->flags & DM_FLAG_ACTIVATED)
71 if (!(dev->flags & DM_FLAG_BOUND))
78 ret = drv->unbind(dev);
83 ret = device_chld_unbind(dev, NULL);
87 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
91 if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
92 free(dev->uclass_platdata);
93 dev->uclass_platdata = NULL;
95 if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
96 free(dev->parent_platdata);
97 dev->parent_platdata = NULL;
99 ret = uclass_unbind_device(dev);
104 list_del(&dev->sibling_node);
106 devres_release_all(dev);
108 if (dev->flags & DM_FLAG_NAME_ALLOCED)
109 free((char *)dev->name);
116 * device_free() - Free memory buffers allocated by a device
117 * @dev: Device that is to be started
119 void device_free(struct udevice *dev)
123 if (dev->driver->priv_auto_alloc_size) {
127 size = dev->uclass->uc_drv->per_device_auto_alloc_size;
129 free(dev->uclass_priv);
130 dev->uclass_priv = NULL;
133 size = dev->parent->driver->per_child_auto_alloc_size;
135 size = dev->parent->uclass->uc_drv->
136 per_child_auto_alloc_size;
139 free(dev->parent_priv);
140 dev->parent_priv = NULL;
143 dev->flags &= ~DM_FLAG_PLATDATA_VALID;
145 devres_release_probe(dev);
148 static bool flags_remove(uint flags, uint drv_flags)
150 if ((flags & DM_REMOVE_NORMAL) ||
151 (flags & (drv_flags & (DM_FLAG_ACTIVE_DMA | DM_FLAG_OS_PREPARE))))
157 int device_remove(struct udevice *dev, uint flags)
159 const struct driver *drv;
165 if (!(dev->flags & DM_FLAG_ACTIVATED))
171 ret = uclass_pre_remove_device(dev);
175 ret = device_chld_remove(dev, NULL, flags);
180 * Remove the device if called with the "normal" remove flag set,
181 * or if the remove flag matches any of the drivers remove flags
183 if (drv->remove && flags_remove(flags, drv->flags)) {
184 ret = drv->remove(dev);
189 if (dev->parent && dev->parent->driver->child_post_remove) {
190 ret = dev->parent->driver->child_post_remove(dev);
192 dm_warn("%s: Device '%s' failed child_post_remove()",
193 __func__, dev->name);
197 if (!(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF) &&
198 (dev != gd->cur_serial_dev))
199 dev_power_domain_off(dev);
201 if (flags_remove(flags, drv->flags)) {
205 dev->flags &= ~DM_FLAG_ACTIVATED;
211 /* We can't put the children back */
212 dm_warn("%s: Device '%s' failed to remove, but children are gone\n",
213 __func__, dev->name);
215 ret = uclass_post_probe_device(dev);
217 dm_warn("%s: Device '%s' failed to post_probe on error path\n",
218 __func__, dev->name);