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;
78 /* put dev into parent's successor list */
80 list_add_tail(&dev->sibling_node, &parent->child_head);
82 ret = uclass_bind_device(dev);
84 goto fail_uclass_bind;
86 /* if we fail to bind we remove device from successors and free it */
93 dm_dbg("Bound device %s to %s\n", dev->name, parent->name);
99 if (uclass_unbind_device(dev)) {
100 dm_warn("Failed to unbind dev '%s' on error path\n",
105 list_del(&dev->sibling_node);
111 int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
112 const struct driver_info *info, struct udevice **devp)
116 drv = lists_driver_lookup_name(info->name);
119 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
122 return device_bind(parent, drv, info->name, (void *)info->platdata,
126 int device_probe_child(struct udevice *dev, void *parent_priv)
136 if (dev->flags & DM_FLAG_ACTIVATED)
142 /* Allocate private data and platdata if requested */
143 if (drv->priv_auto_alloc_size) {
144 dev->priv = calloc(1, drv->priv_auto_alloc_size);
150 /* Allocate private data if requested */
151 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
152 dev->platdata = calloc(1, drv->platdata_auto_alloc_size);
153 if (!dev->platdata) {
158 size = dev->uclass->uc_drv->per_device_auto_alloc_size;
160 dev->uclass_priv = calloc(1, size);
161 if (!dev->uclass_priv) {
167 /* Ensure all parents are probed */
169 size = dev->parent->driver->per_child_auto_alloc_size;
171 dev->parent_priv = calloc(1, size);
172 if (!dev->parent_priv) {
177 memcpy(dev->parent_priv, parent_priv, size);
180 ret = device_probe(dev->parent);
185 seq = uclass_resolve_seq(dev);
192 if (dev->parent && dev->parent->driver->child_pre_probe) {
193 ret = dev->parent->driver->child_pre_probe(dev);
198 if (drv->ofdata_to_platdata && dev->of_offset >= 0) {
199 ret = drv->ofdata_to_platdata(dev);
205 ret = drv->probe(dev);
210 dev->flags |= DM_FLAG_ACTIVATED;
212 ret = uclass_post_probe_device(dev);
214 dev->flags &= ~DM_FLAG_ACTIVATED;
220 if (device_remove(dev)) {
221 dm_warn("%s: Device '%s' failed to remove on error path\n",
222 __func__, dev->name);
231 int device_probe(struct udevice *dev)
233 return device_probe_child(dev, NULL);
236 void *dev_get_platdata(struct udevice *dev)
239 dm_warn("%s: null device\n", __func__);
243 return dev->platdata;
246 void *dev_get_priv(struct udevice *dev)
249 dm_warn("%s: null device\n", __func__);
256 void *dev_get_parentdata(struct udevice *dev)
259 dm_warn("%s: null device\n", __func__);
263 return dev->parent_priv;
266 static int device_get_device_tail(struct udevice *dev, int ret,
267 struct udevice **devp)
272 ret = device_probe(dev);
281 int device_get_child(struct udevice *parent, int index, struct udevice **devp)
285 list_for_each_entry(dev, &parent->child_head, sibling_node) {
287 return device_get_device_tail(dev, 0, devp);
293 int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
294 bool find_req_seq, struct udevice **devp)
299 if (seq_or_req_seq == -1)
302 list_for_each_entry(dev, &parent->child_head, sibling_node) {
303 if ((find_req_seq ? dev->req_seq : dev->seq) ==
313 int device_get_child_by_seq(struct udevice *parent, int seq,
314 struct udevice **devp)
320 ret = device_find_child_by_seq(parent, seq, false, &dev);
321 if (ret == -ENODEV) {
323 * We didn't find it in probed devices. See if there is one
324 * that will request this seq if probed.
326 ret = device_find_child_by_seq(parent, seq, true, &dev);
328 return device_get_device_tail(dev, ret, devp);
331 int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
332 struct udevice **devp)
338 list_for_each_entry(dev, &parent->child_head, sibling_node) {
339 if (dev->of_offset == of_offset) {
348 int device_get_child_by_of_offset(struct udevice *parent, int seq,
349 struct udevice **devp)
355 ret = device_find_child_by_of_offset(parent, seq, &dev);
356 return device_get_device_tail(dev, ret, devp);
359 int device_find_first_child(struct udevice *parent, struct udevice **devp)
361 if (list_empty(&parent->child_head)) {
364 *devp = list_first_entry(&parent->child_head, struct udevice,
371 int device_find_next_child(struct udevice **devp)
373 struct udevice *dev = *devp;
374 struct udevice *parent = dev->parent;
376 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
379 *devp = list_entry(dev->sibling_node.next, struct udevice,
386 struct udevice *dev_get_parent(struct udevice *child)
388 return child->parent;
391 ulong dev_get_of_data(struct udevice *dev)
393 return dev->of_id->data;