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 /* put dev into parent's successor list */
86 list_add_tail(&dev->sibling_node, &parent->child_head);
88 ret = uclass_bind_device(dev);
90 goto fail_uclass_bind;
92 /* if we fail to bind we remove device from successors and free it */
99 dm_dbg("Bound device %s to %s\n", dev->name, parent->name);
105 if (uclass_unbind_device(dev)) {
106 dm_warn("Failed to unbind dev '%s' on error path\n",
111 list_del(&dev->sibling_node);
112 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
114 dev->platdata = NULL;
122 int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
123 const struct driver_info *info, struct udevice **devp)
127 drv = lists_driver_lookup_name(info->name);
130 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
133 return device_bind(parent, drv, info->name, (void *)info->platdata,
137 int device_probe_child(struct udevice *dev, void *parent_priv)
147 if (dev->flags & DM_FLAG_ACTIVATED)
153 /* Allocate private data if requested */
154 if (drv->priv_auto_alloc_size) {
155 dev->priv = calloc(1, drv->priv_auto_alloc_size);
161 /* Allocate private data if requested */
162 size = dev->uclass->uc_drv->per_device_auto_alloc_size;
164 dev->uclass_priv = calloc(1, size);
165 if (!dev->uclass_priv) {
171 /* Ensure all parents are probed */
173 size = dev->parent->driver->per_child_auto_alloc_size;
175 dev->parent_priv = calloc(1, size);
176 if (!dev->parent_priv) {
181 memcpy(dev->parent_priv, parent_priv, size);
184 ret = device_probe(dev->parent);
189 seq = uclass_resolve_seq(dev);
196 if (dev->parent && dev->parent->driver->child_pre_probe) {
197 ret = dev->parent->driver->child_pre_probe(dev);
202 if (drv->ofdata_to_platdata && dev->of_offset >= 0) {
203 ret = drv->ofdata_to_platdata(dev);
209 ret = drv->probe(dev);
214 dev->flags |= DM_FLAG_ACTIVATED;
216 ret = uclass_post_probe_device(dev);
218 dev->flags &= ~DM_FLAG_ACTIVATED;
224 if (device_remove(dev)) {
225 dm_warn("%s: Device '%s' failed to remove on error path\n",
226 __func__, dev->name);
235 int device_probe(struct udevice *dev)
237 return device_probe_child(dev, NULL);
240 void *dev_get_platdata(struct udevice *dev)
243 dm_warn("%s: null device\n", __func__);
247 return dev->platdata;
250 void *dev_get_priv(struct udevice *dev)
253 dm_warn("%s: null device\n", __func__);
260 void *dev_get_parentdata(struct udevice *dev)
263 dm_warn("%s: null device\n", __func__);
267 return dev->parent_priv;
270 static int device_get_device_tail(struct udevice *dev, int ret,
271 struct udevice **devp)
276 ret = device_probe(dev);
285 int device_get_child(struct udevice *parent, int index, struct udevice **devp)
289 list_for_each_entry(dev, &parent->child_head, sibling_node) {
291 return device_get_device_tail(dev, 0, devp);
297 int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
298 bool find_req_seq, struct udevice **devp)
303 if (seq_or_req_seq == -1)
306 list_for_each_entry(dev, &parent->child_head, sibling_node) {
307 if ((find_req_seq ? dev->req_seq : dev->seq) ==
317 int device_get_child_by_seq(struct udevice *parent, int seq,
318 struct udevice **devp)
324 ret = device_find_child_by_seq(parent, seq, false, &dev);
325 if (ret == -ENODEV) {
327 * We didn't find it in probed devices. See if there is one
328 * that will request this seq if probed.
330 ret = device_find_child_by_seq(parent, seq, true, &dev);
332 return device_get_device_tail(dev, ret, devp);
335 int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
336 struct udevice **devp)
342 list_for_each_entry(dev, &parent->child_head, sibling_node) {
343 if (dev->of_offset == of_offset) {
352 int device_get_child_by_of_offset(struct udevice *parent, int seq,
353 struct udevice **devp)
359 ret = device_find_child_by_of_offset(parent, seq, &dev);
360 return device_get_device_tail(dev, ret, devp);
363 int device_find_first_child(struct udevice *parent, struct udevice **devp)
365 if (list_empty(&parent->child_head)) {
368 *devp = list_first_entry(&parent->child_head, struct udevice,
375 int device_find_next_child(struct udevice **devp)
377 struct udevice *dev = *devp;
378 struct udevice *parent = dev->parent;
380 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
383 *devp = list_entry(dev->sibling_node.next, struct udevice,
390 struct udevice *dev_get_parent(struct udevice *child)
392 return child->parent;
395 ulong dev_get_of_data(struct udevice *dev)
397 return dev->of_id->data;