4 * Copyright (c) 2013 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>
18 #include <dm/platdata.h>
19 #include <dm/uclass.h>
20 #include <dm/uclass-internal.h>
22 #include <linux/err.h>
23 #include <linux/list.h>
25 DECLARE_GLOBAL_DATA_PTR;
28 * device_chld_unbind() - Unbind all device's children from the device
30 * On error, the function continues to unbind all children, and reports the
33 * @dev: The device that is to be stripped of its children
34 * @return 0 on success, -ve on error
36 static int device_chld_unbind(struct udevice *dev)
38 struct udevice *pos, *n;
39 int ret, saved_ret = 0;
43 list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
44 ret = device_unbind(pos);
45 if (ret && !saved_ret)
53 * device_chld_remove() - Stop all device's children
54 * @dev: The device whose children are to be removed
55 * @return 0 on success, -ve on error
57 static int device_chld_remove(struct udevice *dev)
59 struct udevice *pos, *n;
64 list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
65 ret = device_remove(pos);
73 int device_bind(struct udevice *parent, struct driver *drv, const char *name,
74 void *platdata, int of_offset, struct udevice **devp)
84 ret = uclass_get(drv->id, &uc);
88 dev = calloc(1, sizeof(struct udevice));
92 INIT_LIST_HEAD(&dev->sibling_node);
93 INIT_LIST_HEAD(&dev->child_head);
94 INIT_LIST_HEAD(&dev->uclass_node);
95 dev->platdata = platdata;
97 dev->of_offset = of_offset;
103 * For some devices, such as a SPI or I2C bus, the 'reg' property
104 * is a reasonable indicator of the sequence number. But if there is
105 * an alias, we use that in preference. In any case, this is just
106 * a 'requested' sequence, and will be resolved (and ->seq updated)
107 * when the device is probed.
109 dev->req_seq = fdtdec_get_int(gd->fdt_blob, of_offset, "reg", -1);
111 if (uc->uc_drv->name && of_offset != -1) {
112 fdtdec_get_alias_seq(gd->fdt_blob, uc->uc_drv->name, of_offset,
116 if (!dev->platdata && drv->platdata_auto_alloc_size)
117 dev->flags |= DM_FLAG_ALLOC_PDATA;
119 /* put dev into parent's successor list */
121 list_add_tail(&dev->sibling_node, &parent->child_head);
123 ret = uclass_bind_device(dev);
127 /* if we fail to bind we remove device from successors and free it */
129 ret = drv->bind(dev);
131 if (uclass_unbind_device(dev)) {
132 dm_warn("Failed to unbind dev '%s' on error path\n",
139 dm_dbg("Bound device %s to %s\n", dev->name, parent->name);
145 list_del(&dev->sibling_node);
150 int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
151 const struct driver_info *info, struct udevice **devp)
155 drv = lists_driver_lookup_name(info->name);
158 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
161 return device_bind(parent, drv, info->name, (void *)info->platdata,
165 int device_unbind(struct udevice *dev)
173 if (dev->flags & DM_FLAG_ACTIVATED)
180 ret = drv->unbind(dev);
185 ret = device_chld_unbind(dev);
189 ret = uclass_unbind_device(dev);
194 list_del(&dev->sibling_node);
201 * device_free() - Free memory buffers allocated by a device
202 * @dev: Device that is to be started
204 static void device_free(struct udevice *dev)
208 if (dev->driver->priv_auto_alloc_size) {
212 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
214 dev->platdata = NULL;
216 size = dev->uclass->uc_drv->per_device_auto_alloc_size;
218 free(dev->uclass_priv);
219 dev->uclass_priv = NULL;
223 int device_probe(struct udevice *dev)
233 if (dev->flags & DM_FLAG_ACTIVATED)
239 /* Allocate private data and platdata if requested */
240 if (drv->priv_auto_alloc_size) {
241 dev->priv = calloc(1, drv->priv_auto_alloc_size);
247 /* Allocate private data if requested */
248 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
249 dev->platdata = calloc(1, drv->platdata_auto_alloc_size);
250 if (!dev->platdata) {
255 size = dev->uclass->uc_drv->per_device_auto_alloc_size;
257 dev->uclass_priv = calloc(1, size);
258 if (!dev->uclass_priv) {
264 /* Ensure all parents are probed */
266 ret = device_probe(dev->parent);
271 seq = uclass_resolve_seq(dev);
278 if (drv->ofdata_to_platdata && dev->of_offset >= 0) {
279 ret = drv->ofdata_to_platdata(dev);
285 ret = drv->probe(dev);
290 dev->flags |= DM_FLAG_ACTIVATED;
292 ret = uclass_post_probe_device(dev);
294 dev->flags &= ~DM_FLAG_ACTIVATED;
300 if (device_remove(dev)) {
301 dm_warn("%s: Device '%s' failed to remove on error path\n",
302 __func__, dev->name);
311 int device_remove(struct udevice *dev)
319 if (!(dev->flags & DM_FLAG_ACTIVATED))
325 ret = uclass_pre_remove_device(dev);
329 ret = device_chld_remove(dev);
334 ret = drv->remove(dev);
342 dev->flags &= ~DM_FLAG_ACTIVATED;
347 /* We can't put the children back */
348 dm_warn("%s: Device '%s' failed to remove, but children are gone\n",
349 __func__, dev->name);
351 ret = uclass_post_probe_device(dev);
353 dm_warn("%s: Device '%s' failed to post_probe on error path\n",
354 __func__, dev->name);
360 void *dev_get_platdata(struct udevice *dev)
363 dm_warn("%s: null device", __func__);
367 return dev->platdata;
370 void *dev_get_priv(struct udevice *dev)
373 dm_warn("%s: null device", __func__);