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, const struct driver_info *info,
133 struct udevice **devp)
137 drv = lists_driver_lookup_name(info->name);
141 return device_bind(parent, drv, info->name, (void *)info->platdata,
145 int device_unbind(struct udevice *dev)
153 if (dev->flags & DM_FLAG_ACTIVATED)
160 ret = drv->unbind(dev);
165 ret = device_chld_unbind(dev);
169 ret = uclass_unbind_device(dev);
174 list_del(&dev->sibling_node);
181 * device_free() - Free memory buffers allocated by a device
182 * @dev: Device that is to be started
184 static void device_free(struct udevice *dev)
188 if (dev->driver->priv_auto_alloc_size) {
192 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
194 dev->platdata = NULL;
196 size = dev->uclass->uc_drv->per_device_auto_alloc_size;
198 free(dev->uclass_priv);
199 dev->uclass_priv = NULL;
203 int device_probe(struct udevice *dev)
212 if (dev->flags & DM_FLAG_ACTIVATED)
218 /* Allocate private data and platdata if requested */
219 if (drv->priv_auto_alloc_size) {
220 dev->priv = calloc(1, drv->priv_auto_alloc_size);
226 /* Allocate private data if requested */
227 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
228 dev->platdata = calloc(1, drv->platdata_auto_alloc_size);
229 if (!dev->platdata) {
234 size = dev->uclass->uc_drv->per_device_auto_alloc_size;
236 dev->uclass_priv = calloc(1, size);
237 if (!dev->uclass_priv) {
243 /* Ensure all parents are probed */
245 ret = device_probe(dev->parent);
250 if (drv->ofdata_to_platdata && dev->of_offset >= 0) {
251 ret = drv->ofdata_to_platdata(dev);
257 ret = drv->probe(dev);
262 dev->flags |= DM_FLAG_ACTIVATED;
264 ret = uclass_post_probe_device(dev);
266 dev->flags &= ~DM_FLAG_ACTIVATED;
272 if (device_remove(dev)) {
273 dm_warn("%s: Device '%s' failed to remove on error path\n",
274 __func__, dev->name);
282 int device_remove(struct udevice *dev)
290 if (!(dev->flags & DM_FLAG_ACTIVATED))
296 ret = uclass_pre_remove_device(dev);
300 ret = device_chld_remove(dev);
305 ret = drv->remove(dev);
312 dev->flags &= ~DM_FLAG_ACTIVATED;
317 /* We can't put the children back */
318 dm_warn("%s: Device '%s' failed to remove, but children are gone\n",
319 __func__, dev->name);
321 ret = uclass_post_probe_device(dev);
323 dm_warn("%s: Device '%s' failed to post_probe on error path\n",
324 __func__, dev->name);
330 void *dev_get_platdata(struct udevice *dev)
333 dm_warn("%s: null device", __func__);
337 return dev->platdata;
340 void *dev_get_priv(struct udevice *dev)
343 dm_warn("%s: null device", __func__);