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;
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 size = parent->uclass->uc_drv->
88 per_child_platdata_auto_alloc_size;
91 dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA;
92 dev->parent_platdata = calloc(1, size);
93 if (!dev->parent_platdata) {
100 /* put dev into parent's successor list */
102 list_add_tail(&dev->sibling_node, &parent->child_head);
104 ret = uclass_bind_device(dev);
106 goto fail_uclass_bind;
108 /* if we fail to bind we remove device from successors and free it */
110 ret = drv->bind(dev);
115 dm_dbg("Bound device %s to %s\n", dev->name, parent->name);
121 if (uclass_unbind_device(dev)) {
122 dm_warn("Failed to unbind dev '%s' on error path\n",
126 list_del(&dev->sibling_node);
127 if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
128 free(dev->parent_platdata);
129 dev->parent_platdata = NULL;
132 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
134 dev->platdata = NULL;
142 int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
143 const struct driver_info *info, struct udevice **devp)
147 drv = lists_driver_lookup_name(info->name);
150 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
153 return device_bind(parent, drv, info->name, (void *)info->platdata,
157 int device_probe_child(struct udevice *dev, void *parent_priv)
167 if (dev->flags & DM_FLAG_ACTIVATED)
173 /* Allocate private data if requested */
174 if (drv->priv_auto_alloc_size) {
175 dev->priv = calloc(1, drv->priv_auto_alloc_size);
181 /* Allocate private data if requested */
182 size = dev->uclass->uc_drv->per_device_auto_alloc_size;
184 dev->uclass_priv = calloc(1, size);
185 if (!dev->uclass_priv) {
191 /* Ensure all parents are probed */
193 size = dev->parent->driver->per_child_auto_alloc_size;
195 dev->parent_priv = calloc(1, size);
196 if (!dev->parent_priv) {
201 memcpy(dev->parent_priv, parent_priv, size);
204 ret = device_probe(dev->parent);
209 seq = uclass_resolve_seq(dev);
216 if (dev->parent && dev->parent->driver->child_pre_probe) {
217 ret = dev->parent->driver->child_pre_probe(dev);
222 if (drv->ofdata_to_platdata && dev->of_offset >= 0) {
223 ret = drv->ofdata_to_platdata(dev);
229 ret = drv->probe(dev);
234 dev->flags |= DM_FLAG_ACTIVATED;
236 ret = uclass_post_probe_device(dev);
238 dev->flags &= ~DM_FLAG_ACTIVATED;
244 if (device_remove(dev)) {
245 dm_warn("%s: Device '%s' failed to remove on error path\n",
246 __func__, dev->name);
255 int device_probe(struct udevice *dev)
257 return device_probe_child(dev, NULL);
260 void *dev_get_platdata(struct udevice *dev)
263 dm_warn("%s: null device\n", __func__);
267 return dev->platdata;
270 void *dev_get_parent_platdata(struct udevice *dev)
273 dm_warn("%s: null device", __func__);
277 return dev->parent_platdata;
280 void *dev_get_priv(struct udevice *dev)
283 dm_warn("%s: null device\n", __func__);
290 void *dev_get_parentdata(struct udevice *dev)
293 dm_warn("%s: null device\n", __func__);
297 return dev->parent_priv;
300 static int device_get_device_tail(struct udevice *dev, int ret,
301 struct udevice **devp)
306 ret = device_probe(dev);
315 int device_get_child(struct udevice *parent, int index, struct udevice **devp)
319 list_for_each_entry(dev, &parent->child_head, sibling_node) {
321 return device_get_device_tail(dev, 0, devp);
327 int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
328 bool find_req_seq, struct udevice **devp)
333 if (seq_or_req_seq == -1)
336 list_for_each_entry(dev, &parent->child_head, sibling_node) {
337 if ((find_req_seq ? dev->req_seq : dev->seq) ==
347 int device_get_child_by_seq(struct udevice *parent, int seq,
348 struct udevice **devp)
354 ret = device_find_child_by_seq(parent, seq, false, &dev);
355 if (ret == -ENODEV) {
357 * We didn't find it in probed devices. See if there is one
358 * that will request this seq if probed.
360 ret = device_find_child_by_seq(parent, seq, true, &dev);
362 return device_get_device_tail(dev, ret, devp);
365 int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
366 struct udevice **devp)
372 list_for_each_entry(dev, &parent->child_head, sibling_node) {
373 if (dev->of_offset == of_offset) {
382 int device_get_child_by_of_offset(struct udevice *parent, int seq,
383 struct udevice **devp)
389 ret = device_find_child_by_of_offset(parent, seq, &dev);
390 return device_get_device_tail(dev, ret, devp);
393 int device_find_first_child(struct udevice *parent, struct udevice **devp)
395 if (list_empty(&parent->child_head)) {
398 *devp = list_first_entry(&parent->child_head, struct udevice,
405 int device_find_next_child(struct udevice **devp)
407 struct udevice *dev = *devp;
408 struct udevice *parent = dev->parent;
410 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
413 *devp = list_entry(dev->sibling_node.next, struct udevice,
420 struct udevice *dev_get_parent(struct udevice *child)
422 return child->parent;
425 ulong dev_get_of_data(struct udevice *dev)
427 return dev->of_id->data;