4 * Copyright (c) 2013 Google, Inc
7 * Pavel Herrmann <morpheus.ibis@gmail.com>
9 * SPDX-License-Identifier: GPL-2.0+
14 #include <dm/device.h>
15 #include <dm/device-internal.h>
17 #include <dm/platdata.h>
18 #include <dm/uclass.h>
19 #include <dm/uclass-internal.h>
21 #include <linux/err.h>
22 #include <linux/list.h>
25 * device_chld_unbind() - Unbind all device's children from the device
27 * On error, the function continues to unbind all children, and reports the
30 * @dev: The device that is to be stripped of its children
31 * @return 0 on success, -ve on error
33 static int device_chld_unbind(struct udevice *dev)
35 struct udevice *pos, *n;
36 int ret, saved_ret = 0;
40 list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
41 ret = device_unbind(pos);
42 if (ret && !saved_ret)
50 * device_chld_remove() - Stop all device's children
51 * @dev: The device whose children are to be removed
52 * @return 0 on success, -ve on error
54 static int device_chld_remove(struct udevice *dev)
56 struct udevice *pos, *n;
61 list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
62 ret = device_remove(pos);
70 int device_bind(struct udevice *parent, struct driver *drv, const char *name,
71 void *platdata, int of_offset, struct udevice **devp)
81 ret = uclass_get(drv->id, &uc);
85 dev = calloc(1, sizeof(struct udevice));
89 INIT_LIST_HEAD(&dev->sibling_node);
90 INIT_LIST_HEAD(&dev->child_head);
91 INIT_LIST_HEAD(&dev->uclass_node);
92 dev->platdata = platdata;
94 dev->of_offset = of_offset;
98 if (!dev->platdata && drv->platdata_auto_alloc_size)
99 dev->flags |= DM_FLAG_ALLOC_PDATA;
101 /* put dev into parent's successor list */
103 list_add_tail(&dev->sibling_node, &parent->child_head);
105 ret = uclass_bind_device(dev);
109 /* if we fail to bind we remove device from successors and free it */
111 ret = drv->bind(dev);
113 if (uclass_unbind_device(dev)) {
114 dm_warn("Failed to unbind dev '%s' on error path\n",
121 dm_dbg("Bound device %s to %s\n", dev->name, parent->name);
127 list_del(&dev->sibling_node);
132 int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
133 const struct driver_info *info, struct udevice **devp)
137 drv = lists_driver_lookup_name(info->name);
140 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
143 return device_bind(parent, drv, info->name, (void *)info->platdata,
147 int device_unbind(struct udevice *dev)
155 if (dev->flags & DM_FLAG_ACTIVATED)
162 ret = drv->unbind(dev);
167 ret = device_chld_unbind(dev);
171 ret = uclass_unbind_device(dev);
176 list_del(&dev->sibling_node);
183 * device_free() - Free memory buffers allocated by a device
184 * @dev: Device that is to be started
186 static void device_free(struct udevice *dev)
190 if (dev->driver->priv_auto_alloc_size) {
194 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
196 dev->platdata = NULL;
198 size = dev->uclass->uc_drv->per_device_auto_alloc_size;
200 free(dev->uclass_priv);
201 dev->uclass_priv = NULL;
205 int device_probe(struct udevice *dev)
214 if (dev->flags & DM_FLAG_ACTIVATED)
220 /* Allocate private data and platdata if requested */
221 if (drv->priv_auto_alloc_size) {
222 dev->priv = calloc(1, drv->priv_auto_alloc_size);
228 /* Allocate private data if requested */
229 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
230 dev->platdata = calloc(1, drv->platdata_auto_alloc_size);
231 if (!dev->platdata) {
236 size = dev->uclass->uc_drv->per_device_auto_alloc_size;
238 dev->uclass_priv = calloc(1, size);
239 if (!dev->uclass_priv) {
245 /* Ensure all parents are probed */
247 ret = device_probe(dev->parent);
252 if (drv->ofdata_to_platdata && dev->of_offset >= 0) {
253 ret = drv->ofdata_to_platdata(dev);
259 ret = drv->probe(dev);
264 dev->flags |= DM_FLAG_ACTIVATED;
266 ret = uclass_post_probe_device(dev);
268 dev->flags &= ~DM_FLAG_ACTIVATED;
274 if (device_remove(dev)) {
275 dm_warn("%s: Device '%s' failed to remove on error path\n",
276 __func__, dev->name);
284 int device_remove(struct udevice *dev)
292 if (!(dev->flags & DM_FLAG_ACTIVATED))
298 ret = uclass_pre_remove_device(dev);
302 ret = device_chld_remove(dev);
307 ret = drv->remove(dev);
314 dev->flags &= ~DM_FLAG_ACTIVATED;
319 /* We can't put the children back */
320 dm_warn("%s: Device '%s' failed to remove, but children are gone\n",
321 __func__, dev->name);
323 ret = uclass_post_probe_device(dev);
325 dm_warn("%s: Device '%s' failed to post_probe on error path\n",
326 __func__, dev->name);
332 void *dev_get_platdata(struct udevice *dev)
335 dm_warn("%s: null device", __func__);
339 return dev->platdata;
342 void *dev_get_priv(struct udevice *dev)
345 dm_warn("%s: null device", __func__);