4 * Copyright (c) 2013 Google, Inc
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;
27 int device_bind(struct udevice *parent, struct driver *drv, const char *name,
28 void *platdata, int of_offset, struct udevice **devp)
38 ret = uclass_get(drv->id, &uc);
42 dev = calloc(1, sizeof(struct udevice));
46 INIT_LIST_HEAD(&dev->sibling_node);
47 INIT_LIST_HEAD(&dev->child_head);
48 INIT_LIST_HEAD(&dev->uclass_node);
49 dev->platdata = platdata;
51 dev->of_offset = of_offset;
57 * For some devices, such as a SPI or I2C bus, the 'reg' property
58 * is a reasonable indicator of the sequence number. But if there is
59 * an alias, we use that in preference. In any case, this is just
60 * a 'requested' sequence, and will be resolved (and ->seq updated)
61 * when the device is probed.
64 #ifdef CONFIG_OF_CONTROL
65 dev->req_seq = fdtdec_get_int(gd->fdt_blob, of_offset, "reg", -1);
66 if (!IS_ERR_VALUE(dev->req_seq))
67 dev->req_seq &= INT_MAX;
68 if (uc->uc_drv->name && of_offset != -1) {
69 fdtdec_get_alias_seq(gd->fdt_blob, uc->uc_drv->name, of_offset,
75 if (!dev->platdata && drv->platdata_auto_alloc_size) {
76 dev->flags |= DM_FLAG_ALLOC_PDATA;
77 dev->platdata = calloc(1, drv->platdata_auto_alloc_size);
84 int size = parent->driver->per_child_platdata_auto_alloc_size;
87 dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA;
88 dev->parent_platdata = calloc(1, size);
89 if (!dev->parent_platdata) {
96 /* put dev into parent's successor list */
98 list_add_tail(&dev->sibling_node, &parent->child_head);
100 ret = uclass_bind_device(dev);
102 goto fail_uclass_bind;
104 /* if we fail to bind we remove device from successors and free it */
106 ret = drv->bind(dev);
111 dm_dbg("Bound device %s to %s\n", dev->name, parent->name);
117 if (uclass_unbind_device(dev)) {
118 dm_warn("Failed to unbind dev '%s' on error path\n",
122 list_del(&dev->sibling_node);
123 if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
124 free(dev->parent_platdata);
125 dev->parent_platdata = NULL;
128 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
130 dev->platdata = NULL;
138 int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
139 const struct driver_info *info, struct udevice **devp)
143 drv = lists_driver_lookup_name(info->name);
146 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
149 return device_bind(parent, drv, info->name, (void *)info->platdata,
153 int device_probe_child(struct udevice *dev, void *parent_priv)
163 if (dev->flags & DM_FLAG_ACTIVATED)
169 /* Allocate private data if requested */
170 if (drv->priv_auto_alloc_size) {
171 dev->priv = calloc(1, drv->priv_auto_alloc_size);
177 /* Allocate private data if requested */
178 size = dev->uclass->uc_drv->per_device_auto_alloc_size;
180 dev->uclass_priv = calloc(1, size);
181 if (!dev->uclass_priv) {
187 /* Ensure all parents are probed */
189 size = dev->parent->driver->per_child_auto_alloc_size;
191 dev->parent_priv = calloc(1, size);
192 if (!dev->parent_priv) {
197 memcpy(dev->parent_priv, parent_priv, size);
200 ret = device_probe(dev->parent);
205 seq = uclass_resolve_seq(dev);
212 if (dev->parent && dev->parent->driver->child_pre_probe) {
213 ret = dev->parent->driver->child_pre_probe(dev);
218 if (drv->ofdata_to_platdata && dev->of_offset >= 0) {
219 ret = drv->ofdata_to_platdata(dev);
225 ret = drv->probe(dev);
230 dev->flags |= DM_FLAG_ACTIVATED;
232 ret = uclass_post_probe_device(dev);
234 dev->flags &= ~DM_FLAG_ACTIVATED;
240 if (device_remove(dev)) {
241 dm_warn("%s: Device '%s' failed to remove on error path\n",
242 __func__, dev->name);
251 int device_probe(struct udevice *dev)
253 return device_probe_child(dev, NULL);
256 void *dev_get_platdata(struct udevice *dev)
259 dm_warn("%s: null device\n", __func__);
263 return dev->platdata;
266 void *dev_get_parent_platdata(struct udevice *dev)
269 dm_warn("%s: null device", __func__);
273 return dev->parent_platdata;
276 void *dev_get_priv(struct udevice *dev)
279 dm_warn("%s: null device\n", __func__);
286 void *dev_get_parentdata(struct udevice *dev)
289 dm_warn("%s: null device\n", __func__);
293 return dev->parent_priv;
296 static int device_get_device_tail(struct udevice *dev, int ret,
297 struct udevice **devp)
302 ret = device_probe(dev);
311 int device_get_child(struct udevice *parent, int index, struct udevice **devp)
315 list_for_each_entry(dev, &parent->child_head, sibling_node) {
317 return device_get_device_tail(dev, 0, devp);
323 int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
324 bool find_req_seq, struct udevice **devp)
329 if (seq_or_req_seq == -1)
332 list_for_each_entry(dev, &parent->child_head, sibling_node) {
333 if ((find_req_seq ? dev->req_seq : dev->seq) ==
343 int device_get_child_by_seq(struct udevice *parent, int seq,
344 struct udevice **devp)
350 ret = device_find_child_by_seq(parent, seq, false, &dev);
351 if (ret == -ENODEV) {
353 * We didn't find it in probed devices. See if there is one
354 * that will request this seq if probed.
356 ret = device_find_child_by_seq(parent, seq, true, &dev);
358 return device_get_device_tail(dev, ret, devp);
361 int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
362 struct udevice **devp)
368 list_for_each_entry(dev, &parent->child_head, sibling_node) {
369 if (dev->of_offset == of_offset) {
378 int device_get_child_by_of_offset(struct udevice *parent, int seq,
379 struct udevice **devp)
385 ret = device_find_child_by_of_offset(parent, seq, &dev);
386 return device_get_device_tail(dev, ret, devp);
389 int device_find_first_child(struct udevice *parent, struct udevice **devp)
391 if (list_empty(&parent->child_head)) {
394 *devp = list_first_entry(&parent->child_head, struct udevice,
401 int device_find_next_child(struct udevice **devp)
403 struct udevice *dev = *devp;
404 struct udevice *parent = dev->parent;
406 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
409 *devp = list_entry(dev->sibling_node.next, struct udevice,
416 struct udevice *dev_get_parent(struct udevice *child)
418 return child->parent;
421 ulong dev_get_of_data(struct udevice *dev)
423 return dev->of_id->data;