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);
86 /* if we fail to bind we remove device from successors and free it */
90 if (uclass_unbind_device(dev)) {
91 dm_warn("Failed to unbind dev '%s' on error path\n",
98 dm_dbg("Bound device %s to %s\n", dev->name, parent->name);
104 list_del(&dev->sibling_node);
109 int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
110 const struct driver_info *info, struct udevice **devp)
114 drv = lists_driver_lookup_name(info->name);
117 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
120 return device_bind(parent, drv, info->name, (void *)info->platdata,
124 int device_probe_child(struct udevice *dev, void *parent_priv)
134 if (dev->flags & DM_FLAG_ACTIVATED)
140 /* Allocate private data and platdata if requested */
141 if (drv->priv_auto_alloc_size) {
142 dev->priv = calloc(1, drv->priv_auto_alloc_size);
148 /* Allocate private data if requested */
149 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
150 dev->platdata = calloc(1, drv->platdata_auto_alloc_size);
151 if (!dev->platdata) {
156 size = dev->uclass->uc_drv->per_device_auto_alloc_size;
158 dev->uclass_priv = calloc(1, size);
159 if (!dev->uclass_priv) {
165 /* Ensure all parents are probed */
167 size = dev->parent->driver->per_child_auto_alloc_size;
169 dev->parent_priv = calloc(1, size);
170 if (!dev->parent_priv) {
175 memcpy(dev->parent_priv, parent_priv, size);
178 ret = device_probe(dev->parent);
183 seq = uclass_resolve_seq(dev);
190 if (dev->parent && dev->parent->driver->child_pre_probe) {
191 ret = dev->parent->driver->child_pre_probe(dev);
196 if (drv->ofdata_to_platdata && dev->of_offset >= 0) {
197 ret = drv->ofdata_to_platdata(dev);
203 ret = drv->probe(dev);
208 dev->flags |= DM_FLAG_ACTIVATED;
210 ret = uclass_post_probe_device(dev);
212 dev->flags &= ~DM_FLAG_ACTIVATED;
218 if (device_remove(dev)) {
219 dm_warn("%s: Device '%s' failed to remove on error path\n",
220 __func__, dev->name);
229 int device_probe(struct udevice *dev)
231 return device_probe_child(dev, NULL);
234 void *dev_get_platdata(struct udevice *dev)
237 dm_warn("%s: null device\n", __func__);
241 return dev->platdata;
244 void *dev_get_priv(struct udevice *dev)
247 dm_warn("%s: null device\n", __func__);
254 void *dev_get_parentdata(struct udevice *dev)
257 dm_warn("%s: null device\n", __func__);
261 return dev->parent_priv;
264 static int device_get_device_tail(struct udevice *dev, int ret,
265 struct udevice **devp)
270 ret = device_probe(dev);
279 int device_get_child(struct udevice *parent, int index, struct udevice **devp)
283 list_for_each_entry(dev, &parent->child_head, sibling_node) {
285 return device_get_device_tail(dev, 0, devp);
291 int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
292 bool find_req_seq, struct udevice **devp)
297 if (seq_or_req_seq == -1)
300 list_for_each_entry(dev, &parent->child_head, sibling_node) {
301 if ((find_req_seq ? dev->req_seq : dev->seq) ==
311 int device_get_child_by_seq(struct udevice *parent, int seq,
312 struct udevice **devp)
318 ret = device_find_child_by_seq(parent, seq, false, &dev);
319 if (ret == -ENODEV) {
321 * We didn't find it in probed devices. See if there is one
322 * that will request this seq if probed.
324 ret = device_find_child_by_seq(parent, seq, true, &dev);
326 return device_get_device_tail(dev, ret, devp);
329 int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
330 struct udevice **devp)
336 list_for_each_entry(dev, &parent->child_head, sibling_node) {
337 if (dev->of_offset == of_offset) {
346 int device_get_child_by_of_offset(struct udevice *parent, int seq,
347 struct udevice **devp)
353 ret = device_find_child_by_of_offset(parent, seq, &dev);
354 return device_get_device_tail(dev, ret, devp);
357 int device_find_first_child(struct udevice *parent, struct udevice **devp)
359 if (list_empty(&parent->child_head)) {
362 *devp = list_first_entry(&parent->child_head, struct udevice,
369 int device_find_next_child(struct udevice **devp)
371 struct udevice *dev = *devp;
372 struct udevice *parent = dev->parent;
374 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
377 *devp = list_entry(dev->sibling_node.next, struct udevice,
384 struct udevice *dev_get_parent(struct udevice *child)
386 return child->parent;
389 ulong dev_get_of_data(struct udevice *dev)
391 return dev->of_id->data;