blob: 7a02d4385081ec30b3454a2162c447b04c0875e1 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassdd6ab882014-02-26 15:59:18 -07002/*
3 * Device manager
4 *
5 * Copyright (c) 2013 Google, Inc
6 *
7 * (C) Copyright 2012
8 * Pavel Herrmann <morpheus.ibis@gmail.com>
Simon Glassdd6ab882014-02-26 15:59:18 -07009 */
10
11#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -070012#include <cpu_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Vignesh R0dff3702016-07-06 09:58:55 +053014#include <asm/io.h>
Philipp Tomsich9cf03b02018-01-08 13:59:18 +010015#include <clk.h>
Simon Glassdb6f0202014-07-23 06:55:12 -060016#include <fdtdec.h>
Stefan Roeseadc09052015-09-02 07:41:12 +020017#include <fdt_support.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070018#include <malloc.h>
Simon Glass274e0b02020-05-10 11:39:56 -060019#include <asm/cache.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070020#include <dm/device.h>
21#include <dm/device-internal.h>
22#include <dm/lists.h>
Mario Six1f1c1ec2018-01-15 11:07:20 +010023#include <dm/of_access.h>
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090024#include <dm/pinctrl.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070025#include <dm/platdata.h>
Simon Glass322b2902017-05-18 20:09:05 -060026#include <dm/read.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070027#include <dm/uclass.h>
28#include <dm/uclass-internal.h>
29#include <dm/util.h>
30#include <linux/err.h>
31#include <linux/list.h>
Peng Fan6fba32d2018-07-27 10:20:38 +080032#include <power-domain.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070033
Simon Glassdb6f0202014-07-23 06:55:12 -060034DECLARE_GLOBAL_DATA_PTR;
35
Stephen Warren8c93df12016-05-11 15:26:24 -060036static int device_bind_common(struct udevice *parent, const struct driver *drv,
Simon Glass71fa5b42020-12-03 16:55:18 -070037 const char *name, void *plat,
Simon Glassb0cf4b32017-05-17 17:18:11 -060038 ulong driver_data, ofnode node,
Simon Glassafbf9b82016-07-04 11:58:18 -060039 uint of_platdata_size, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -070040{
Heiko Schocherb74fcb42014-05-22 12:43:05 +020041 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -070042 struct uclass *uc;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +020043 int size, ret = 0;
Simon Glassdd6ab882014-02-26 15:59:18 -070044
Masahiro Yamadae1cc1f02015-08-27 12:44:28 +090045 if (devp)
46 *devp = NULL;
Simon Glassdd6ab882014-02-26 15:59:18 -070047 if (!name)
48 return -EINVAL;
49
50 ret = uclass_get(drv->id, &uc);
Simon Glass43313de2015-08-30 16:55:16 -060051 if (ret) {
52 debug("Missing uclass for driver %s\n", drv->name);
Simon Glassdd6ab882014-02-26 15:59:18 -070053 return ret;
Simon Glass43313de2015-08-30 16:55:16 -060054 }
Simon Glassdd6ab882014-02-26 15:59:18 -070055
Heiko Schocherb74fcb42014-05-22 12:43:05 +020056 dev = calloc(1, sizeof(struct udevice));
Simon Glassdd6ab882014-02-26 15:59:18 -070057 if (!dev)
58 return -ENOMEM;
59
60 INIT_LIST_HEAD(&dev->sibling_node);
61 INIT_LIST_HEAD(&dev->child_head);
62 INIT_LIST_HEAD(&dev->uclass_node);
Masahiro Yamada029bfca2015-07-25 21:52:37 +090063#ifdef CONFIG_DEVRES
Masahiro Yamada8b15b162015-07-25 21:52:35 +090064 INIT_LIST_HEAD(&dev->devres_head);
Masahiro Yamada029bfca2015-07-25 21:52:37 +090065#endif
Simon Glass71fa5b42020-12-03 16:55:18 -070066 dev->plat = plat;
Stephen Warren8c93df12016-05-11 15:26:24 -060067 dev->driver_data = driver_data;
Simon Glassdd6ab882014-02-26 15:59:18 -070068 dev->name = name;
Simon Glassb0cf4b32017-05-17 17:18:11 -060069 dev->node = node;
Simon Glassdd6ab882014-02-26 15:59:18 -070070 dev->parent = parent;
71 dev->driver = drv;
72 dev->uclass = uc;
Simon Glassdb6f0202014-07-23 06:55:12 -060073
Simon Glassdb6f0202014-07-23 06:55:12 -060074 dev->seq = -1;
Simon Glass0ccb0972015-01-25 08:27:05 -070075 dev->req_seq = -1;
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +010076 if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
77 (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) {
Simon Glass1bd3deb2015-02-27 22:06:30 -070078 /*
Stefan Roese9d4ce6b2016-03-16 09:58:01 +010079 * Some devices, such as a SPI bus, I2C bus and serial ports
80 * are numbered using aliases.
81 *
82 * This is just a 'requested' sequence, and will be
83 * resolved (and ->seq updated) when the device is probed.
84 */
Simon Glasse422f8c2020-07-09 18:39:24 -060085 if (CONFIG_IS_ENABLED(OF_CONTROL) &&
86 !CONFIG_IS_ENABLED(OF_PLATDATA)) {
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +010087 if (uc->uc_drv->name && ofnode_valid(node))
Simon Glass322b2902017-05-18 20:09:05 -060088 dev_read_alias_seq(dev, &dev->req_seq);
Thomas Fitzsimmonsf327ecb2019-09-06 07:51:18 -040089#if CONFIG_IS_ENABLED(OF_PRIOR_STAGE)
90 if (dev->req_seq == -1)
91 dev->req_seq =
92 uclass_find_next_free_req_seq(drv->id);
93#endif
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +010094 } else {
95 dev->req_seq = uclass_find_next_free_req_seq(drv->id);
Simon Glass0ccb0972015-01-25 08:27:05 -070096 }
Simon Glassdb6f0202014-07-23 06:55:12 -060097 }
Simon Glass1bd3deb2015-02-27 22:06:30 -070098
Simon Glass71fa5b42020-12-03 16:55:18 -070099 if (drv->plat_auto) {
100 bool alloc = !plat;
Simon Glassafbf9b82016-07-04 11:58:18 -0600101
102 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
103 if (of_platdata_size) {
104 dev->flags |= DM_FLAG_OF_PLATDATA;
Simon Glass71fa5b42020-12-03 16:55:18 -0700105 if (of_platdata_size < drv->plat_auto)
Simon Glassafbf9b82016-07-04 11:58:18 -0600106 alloc = true;
107 }
108 }
109 if (alloc) {
110 dev->flags |= DM_FLAG_ALLOC_PDATA;
Simon Glass71fa5b42020-12-03 16:55:18 -0700111 dev->plat = calloc(1, drv->plat_auto);
112 if (!dev->plat) {
Simon Glassafbf9b82016-07-04 11:58:18 -0600113 ret = -ENOMEM;
114 goto fail_alloc1;
115 }
Simon Glass71fa5b42020-12-03 16:55:18 -0700116 if (CONFIG_IS_ENABLED(OF_PLATDATA) && plat) {
117 memcpy(dev->plat, plat,
Simon Glassafbf9b82016-07-04 11:58:18 -0600118 of_platdata_size);
119 }
Simon Glassba750492015-01-25 08:27:00 -0700120 }
121 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200122
Simon Glass71fa5b42020-12-03 16:55:18 -0700123 size = uc->uc_drv->per_device_plat_auto;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200124 if (size) {
125 dev->flags |= DM_FLAG_ALLOC_UCLASS_PDATA;
Simon Glass71fa5b42020-12-03 16:55:18 -0700126 dev->uclass_plat = calloc(1, size);
127 if (!dev->uclass_plat) {
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200128 ret = -ENOMEM;
129 goto fail_alloc2;
130 }
131 }
Simon Glass11b61732015-01-25 08:27:01 -0700132
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200133 if (parent) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700134 size = parent->driver->per_child_plat_auto;
Simon Glass57f95402015-01-25 08:27:02 -0700135 if (!size) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700136 size = parent->uclass->uc_drv->per_child_plat_auto;
Simon Glass57f95402015-01-25 08:27:02 -0700137 }
Simon Glass11b61732015-01-25 08:27:01 -0700138 if (size) {
139 dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA;
Simon Glass71fa5b42020-12-03 16:55:18 -0700140 dev->parent_plat = calloc(1, size);
141 if (!dev->parent_plat) {
Simon Glass11b61732015-01-25 08:27:01 -0700142 ret = -ENOMEM;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200143 goto fail_alloc3;
Simon Glass11b61732015-01-25 08:27:01 -0700144 }
145 }
Heinrich Schuchardt5246b372020-02-15 21:38:48 +0100146 /* put dev into parent's successor list */
Simon Glassdd6ab882014-02-26 15:59:18 -0700147 list_add_tail(&dev->sibling_node, &parent->child_head);
Heinrich Schuchardt5246b372020-02-15 21:38:48 +0100148 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700149
150 ret = uclass_bind_device(dev);
151 if (ret)
Simon Glass4ebe01b2015-01-25 08:26:59 -0700152 goto fail_uclass_bind;
Simon Glassdd6ab882014-02-26 15:59:18 -0700153
154 /* if we fail to bind we remove device from successors and free it */
155 if (drv->bind) {
156 ret = drv->bind(dev);
Simon Glass4ebe01b2015-01-25 08:26:59 -0700157 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700158 goto fail_bind;
Simon Glassdd6ab882014-02-26 15:59:18 -0700159 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700160 if (parent && parent->driver->child_post_bind) {
161 ret = parent->driver->child_post_bind(dev);
162 if (ret)
163 goto fail_child_post_bind;
164 }
Simon Glass286863d2016-01-05 09:30:59 -0700165 if (uc->uc_drv->post_bind) {
166 ret = uc->uc_drv->post_bind(dev);
167 if (ret)
168 goto fail_uclass_post_bind;
169 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700170
Simon Glassdd6ab882014-02-26 15:59:18 -0700171 if (parent)
Masahiro Yamadaf70f39f2017-09-29 12:31:20 +0900172 pr_debug("Bound device %s to %s\n", dev->name, parent->name);
Masahiro Yamadae1cc1f02015-08-27 12:44:28 +0900173 if (devp)
174 *devp = dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700175
Masahiro Yamadabdbb5dd2015-07-25 21:52:34 +0900176 dev->flags |= DM_FLAG_BOUND;
177
Simon Glassdd6ab882014-02-26 15:59:18 -0700178 return 0;
179
Simon Glass286863d2016-01-05 09:30:59 -0700180fail_uclass_post_bind:
181 /* There is no child unbind() method, so no clean-up required */
Simon Glassa4a51a02015-01-25 08:27:03 -0700182fail_child_post_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900183 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700184 if (drv->unbind && drv->unbind(dev)) {
185 dm_warn("unbind() method failed on dev '%s' on error path\n",
186 dev->name);
187 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700188 }
189
Simon Glassdd6ab882014-02-26 15:59:18 -0700190fail_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900191 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700192 if (uclass_unbind_device(dev)) {
193 dm_warn("Failed to unbind dev '%s' on error path\n",
194 dev->name);
195 }
Simon Glass4ebe01b2015-01-25 08:26:59 -0700196 }
197fail_uclass_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900198 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700199 list_del(&dev->sibling_node);
200 if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700201 free(dev->parent_plat);
202 dev->parent_plat = NULL;
Simon Glasse70a4f42015-02-27 22:06:33 -0700203 }
Simon Glass11b61732015-01-25 08:27:01 -0700204 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200205fail_alloc3:
206 if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700207 free(dev->uclass_plat);
208 dev->uclass_plat = NULL;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200209 }
Simon Glass11b61732015-01-25 08:27:01 -0700210fail_alloc2:
Simon Glassba750492015-01-25 08:27:00 -0700211 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700212 free(dev->plat);
213 dev->plat = NULL;
Simon Glassba750492015-01-25 08:27:00 -0700214 }
215fail_alloc1:
Masahiro Yamada8b15b162015-07-25 21:52:35 +0900216 devres_release_all(dev);
217
Simon Glassdd6ab882014-02-26 15:59:18 -0700218 free(dev);
Simon Glass4ebe01b2015-01-25 08:26:59 -0700219
Simon Glassdd6ab882014-02-26 15:59:18 -0700220 return ret;
221}
222
Stephen Warren8c93df12016-05-11 15:26:24 -0600223int device_bind_with_driver_data(struct udevice *parent,
224 const struct driver *drv, const char *name,
Simon Glass322b2902017-05-18 20:09:05 -0600225 ulong driver_data, ofnode node,
Stephen Warren8c93df12016-05-11 15:26:24 -0600226 struct udevice **devp)
227{
Simon Glass322b2902017-05-18 20:09:05 -0600228 return device_bind_common(parent, drv, name, NULL, driver_data, node,
229 0, devp);
Stephen Warren8c93df12016-05-11 15:26:24 -0600230}
231
Simon Glass884870f2020-11-28 17:50:01 -0700232int device_bind(struct udevice *parent, const struct driver *drv,
Simon Glass71fa5b42020-12-03 16:55:18 -0700233 const char *name, void *plat, ofnode node,
Simon Glass884870f2020-11-28 17:50:01 -0700234 struct udevice **devp)
Simon Glass9eb151e2018-06-11 13:07:15 -0600235{
Simon Glass71fa5b42020-12-03 16:55:18 -0700236 return device_bind_common(parent, drv, name, plat, 0, node, 0,
Simon Glass9eb151e2018-06-11 13:07:15 -0600237 devp);
238}
239
Simon Glassfef72b72014-07-23 06:55:03 -0600240int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
Simon Glasscfd6a002020-10-03 11:31:33 -0600241 const struct driver_info *info, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700242{
243 struct driver *drv;
Simon Glassafbf9b82016-07-04 11:58:18 -0600244 uint platdata_size = 0;
Walter Lozanoffc41b02020-06-25 01:10:11 -0300245 int ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700246
247 drv = lists_driver_lookup_name(info->name);
248 if (!drv)
249 return -ENOENT;
Simon Glassfef72b72014-07-23 06:55:03 -0600250 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
251 return -EPERM;
Simon Glassdd6ab882014-02-26 15:59:18 -0700252
Simon Glassafbf9b82016-07-04 11:58:18 -0600253#if CONFIG_IS_ENABLED(OF_PLATDATA)
254 platdata_size = info->platdata_size;
255#endif
Simon Glass71fa5b42020-12-03 16:55:18 -0700256 ret = device_bind_common(parent, drv, info->name, (void *)info->plat, 0,
257 ofnode_null(), platdata_size, devp);
Walter Lozanoffc41b02020-06-25 01:10:11 -0300258 if (ret)
259 return ret;
Walter Lozanoffc41b02020-06-25 01:10:11 -0300260
261 return ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700262}
263
Claudiu Bezneabf5b8232020-09-07 17:46:33 +0300264int device_reparent(struct udevice *dev, struct udevice *new_parent)
265{
266 struct udevice *pos, *n;
267
268 assert(dev);
269 assert(new_parent);
270
271 list_for_each_entry_safe(pos, n, &dev->parent->child_head,
272 sibling_node) {
273 if (pos->driver != dev->driver)
274 continue;
275
276 list_del(&dev->sibling_node);
277 list_add_tail(&dev->sibling_node, &new_parent->child_head);
278 dev->parent = new_parent;
279
280 break;
281 }
282
283 return 0;
284}
285
Simon Glass825d3f92015-03-25 12:21:53 -0600286static void *alloc_priv(int size, uint flags)
287{
288 void *priv;
289
290 if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
Faiz Abbas03020a92017-09-19 16:53:50 +0530291 size = ROUND(size, ARCH_DMA_MINALIGN);
Simon Glass825d3f92015-03-25 12:21:53 -0600292 priv = memalign(ARCH_DMA_MINALIGN, size);
Simon Glass27bc9b12017-04-04 13:00:19 -0600293 if (priv) {
Simon Glass825d3f92015-03-25 12:21:53 -0600294 memset(priv, '\0', size);
Simon Glass27bc9b12017-04-04 13:00:19 -0600295
296 /*
297 * Ensure that the zero bytes are flushed to memory.
298 * This prevents problems if the driver uses this as
299 * both an input and an output buffer:
300 *
301 * 1. Zeroes written to buffer (here) and sit in the
302 * cache
303 * 2. Driver issues a read command to DMA
304 * 3. CPU runs out of cache space and evicts some cache
305 * data in the buffer, writing zeroes to RAM from
306 * the memset() above
307 * 4. DMA completes
308 * 5. Buffer now has some DMA data and some zeroes
309 * 6. Data being read is now incorrect
310 *
311 * To prevent this, ensure that the cache is clean
312 * within this range at the start. The driver can then
313 * use normal flush-after-write, invalidate-before-read
314 * procedures.
315 *
316 * TODO(sjg@chromium.org): Drop this microblaze
317 * exception.
318 */
319#ifndef CONFIG_MICROBLAZE
320 flush_dcache_range((ulong)priv, (ulong)priv + size);
321#endif
322 }
Simon Glass825d3f92015-03-25 12:21:53 -0600323 } else {
324 priv = calloc(1, size);
325 }
326
327 return priv;
328}
329
Simon Glass4a263852019-12-29 21:19:20 -0700330int device_ofdata_to_platdata(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700331{
Simon Glassa626dff2015-03-25 12:21:54 -0600332 const struct driver *drv;
Simon Glassdd6ab882014-02-26 15:59:18 -0700333 int size = 0;
334 int ret;
335
336 if (!dev)
337 return -EINVAL;
338
Simon Glassd3c9d302019-12-29 21:19:21 -0700339 if (dev->flags & DM_FLAG_PLATDATA_VALID)
Simon Glassdd6ab882014-02-26 15:59:18 -0700340 return 0;
341
Simon Glass44c47892020-04-05 15:38:19 -0600342 /* Ensure all parents have ofdata */
343 if (dev->parent) {
344 ret = device_ofdata_to_platdata(dev->parent);
345 if (ret)
346 goto fail;
347
348 /*
349 * The device might have already been probed during
350 * the call to device_probe() on its parent device
351 * (e.g. PCI bridge devices). Test the flags again
352 * so that we don't mess up the device.
353 */
354 if (dev->flags & DM_FLAG_PLATDATA_VALID)
355 return 0;
356 }
357
Simon Glassdd6ab882014-02-26 15:59:18 -0700358 drv = dev->driver;
359 assert(drv);
360
Bin Meng82cdd782015-08-24 01:14:02 -0700361 /* Allocate private data if requested and not reentered */
Simon Glass8a2b47f2020-12-03 16:55:17 -0700362 if (drv->priv_auto && !dev->priv) {
363 dev->priv = alloc_priv(drv->priv_auto, drv->flags);
Simon Glassdd6ab882014-02-26 15:59:18 -0700364 if (!dev->priv) {
365 ret = -ENOMEM;
366 goto fail;
367 }
368 }
Bin Meng82cdd782015-08-24 01:14:02 -0700369 /* Allocate private data if requested and not reentered */
Simon Glass8a2b47f2020-12-03 16:55:17 -0700370 size = dev->uclass->uc_drv->per_device_auto;
Bin Meng82cdd782015-08-24 01:14:02 -0700371 if (size && !dev->uclass_priv) {
Simon Glass7ebd13d2018-10-01 12:22:05 -0600372 dev->uclass_priv = alloc_priv(size,
373 dev->uclass->uc_drv->flags);
Simon Glassdd6ab882014-02-26 15:59:18 -0700374 if (!dev->uclass_priv) {
375 ret = -ENOMEM;
376 goto fail;
377 }
378 }
379
Simon Glassa8d8f162019-12-29 21:19:18 -0700380 /* Allocate parent data for this child */
Simon Glassdd6ab882014-02-26 15:59:18 -0700381 if (dev->parent) {
Simon Glass8a2b47f2020-12-03 16:55:17 -0700382 size = dev->parent->driver->per_child_auto;
Simon Glassc23b4282015-01-25 08:27:06 -0700383 if (!size) {
Simon Glass8a2b47f2020-12-03 16:55:17 -0700384 size = dev->parent->uclass->uc_drv->per_child_auto;
Simon Glassc23b4282015-01-25 08:27:06 -0700385 }
Bin Meng82cdd782015-08-24 01:14:02 -0700386 if (size && !dev->parent_priv) {
Simon Glass825d3f92015-03-25 12:21:53 -0600387 dev->parent_priv = alloc_priv(size, drv->flags);
Simon Glass60d971b2014-07-23 06:55:20 -0600388 if (!dev->parent_priv) {
389 ret = -ENOMEM;
390 goto fail;
391 }
392 }
Simon Glassa8d8f162019-12-29 21:19:18 -0700393 }
394
395 if (drv->ofdata_to_platdata &&
396 (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_of_node(dev))) {
397 ret = drv->ofdata_to_platdata(dev);
398 if (ret)
399 goto fail;
400 }
Simon Glass60d971b2014-07-23 06:55:20 -0600401
Simon Glassd3c9d302019-12-29 21:19:21 -0700402 dev->flags |= DM_FLAG_PLATDATA_VALID;
403
Simon Glass4a263852019-12-29 21:19:20 -0700404 return 0;
405fail:
406 device_free(dev);
407
408 return ret;
409}
410
411int device_probe(struct udevice *dev)
412{
413 const struct driver *drv;
414 int ret;
415 int seq;
416
417 if (!dev)
418 return -EINVAL;
419
420 if (dev->flags & DM_FLAG_ACTIVATED)
421 return 0;
422
423 drv = dev->driver;
424 assert(drv);
425
426 ret = device_ofdata_to_platdata(dev);
427 if (ret)
428 goto fail;
429
Simon Glassa8d8f162019-12-29 21:19:18 -0700430 /* Ensure all parents are probed */
431 if (dev->parent) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700432 ret = device_probe(dev->parent);
433 if (ret)
434 goto fail;
Bin Meng82cdd782015-08-24 01:14:02 -0700435
436 /*
437 * The device might have already been probed during
438 * the call to device_probe() on its parent device
439 * (e.g. PCI bridge devices). Test the flags again
440 * so that we don't mess up the device.
441 */
442 if (dev->flags & DM_FLAG_ACTIVATED)
443 return 0;
Simon Glassdd6ab882014-02-26 15:59:18 -0700444 }
Simon Glassdb6f0202014-07-23 06:55:12 -0600445
446 seq = uclass_resolve_seq(dev);
447 if (seq < 0) {
448 ret = seq;
449 goto fail;
450 }
451 dev->seq = seq;
Simon Glassdd6ab882014-02-26 15:59:18 -0700452
Simon Glass8b04e732015-03-25 12:21:56 -0600453 dev->flags |= DM_FLAG_ACTIVATED;
454
Simon Glassbb53bda2015-09-12 08:45:19 -0600455 /*
456 * Process pinctrl for everything except the root device, and
Simon Glass27ce9622016-01-21 19:43:25 -0700457 * continue regardless of the result of pinctrl. Don't process pinctrl
458 * settings for pinctrl devices since the device may not yet be
459 * probed.
Simon Glassbb53bda2015-09-12 08:45:19 -0600460 */
Simon Glass27ce9622016-01-21 19:43:25 -0700461 if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
Simon Glassbb53bda2015-09-12 08:45:19 -0600462 pinctrl_select_state(dev, "default");
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900463
Anatolij Gustschin63a34672019-07-14 21:11:01 +0200464 if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
Lokesh Vutlac10e52b2019-09-27 13:48:12 +0530465 (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) &&
466 !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) {
Peng Fand2201112019-09-17 09:29:22 +0000467 ret = dev_power_domain_on(dev);
468 if (ret)
469 goto fail;
Peng Fan6fba32d2018-07-27 10:20:38 +0800470 }
471
Simon Glass9c1f3822015-03-05 12:25:22 -0700472 ret = uclass_pre_probe_device(dev);
Simon Glass5104b982015-01-25 08:27:10 -0700473 if (ret)
474 goto fail;
475
Simon Glassd45560d2014-07-23 06:55:21 -0600476 if (dev->parent && dev->parent->driver->child_pre_probe) {
477 ret = dev->parent->driver->child_pre_probe(dev);
478 if (ret)
479 goto fail;
480 }
481
Bin Meng513cf402019-07-05 09:23:16 -0700482 /* Only handle devices that have a valid ofnode */
483 if (dev_of_valid(dev)) {
484 /*
485 * Process 'assigned-{clocks/clock-parents/clock-rates}'
486 * properties
487 */
Jean-Jacques Hiblot9601f322019-10-22 14:00:06 +0200488 ret = clk_set_defaults(dev, 0);
Bin Meng513cf402019-07-05 09:23:16 -0700489 if (ret)
490 goto fail;
491 }
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100492
Simon Glassdd6ab882014-02-26 15:59:18 -0700493 if (drv->probe) {
494 ret = drv->probe(dev);
Simon Glass9046dec2019-12-29 21:19:16 -0700495 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700496 goto fail;
497 }
498
Simon Glassdd6ab882014-02-26 15:59:18 -0700499 ret = uclass_post_probe_device(dev);
Simon Glass8b04e732015-03-25 12:21:56 -0600500 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700501 goto fail_uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700502
Peng Fan7cf58dd2016-03-12 13:17:38 +0800503 if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
504 pinctrl_select_state(dev, "default");
505
Simon Glassdd6ab882014-02-26 15:59:18 -0700506 return 0;
507fail_uclass:
Stefan Roese80b5bc92017-03-20 12:51:48 +0100508 if (device_remove(dev, DM_REMOVE_NORMAL)) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700509 dm_warn("%s: Device '%s' failed to remove on error path\n",
510 __func__, dev->name);
511 }
512fail:
Simon Glass8b04e732015-03-25 12:21:56 -0600513 dev->flags &= ~DM_FLAG_ACTIVATED;
514
Simon Glassdb6f0202014-07-23 06:55:12 -0600515 dev->seq = -1;
Simon Glassdd6ab882014-02-26 15:59:18 -0700516 device_free(dev);
517
518 return ret;
519}
520
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600521void *dev_get_platdata(const struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700522{
523 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700524 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700525 return NULL;
526 }
527
Simon Glass71fa5b42020-12-03 16:55:18 -0700528 return dev->plat;
Simon Glassdd6ab882014-02-26 15:59:18 -0700529}
530
Simon Glass71fa5b42020-12-03 16:55:18 -0700531void *dev_get_parent_plat(const struct udevice *dev)
Simon Glass11b61732015-01-25 08:27:01 -0700532{
533 if (!dev) {
Simon Glasscdfc5052015-07-06 16:47:40 -0600534 dm_warn("%s: null device\n", __func__);
Simon Glass11b61732015-01-25 08:27:01 -0700535 return NULL;
536 }
537
Simon Glass71fa5b42020-12-03 16:55:18 -0700538 return dev->parent_plat;
Simon Glass11b61732015-01-25 08:27:01 -0700539}
540
Simon Glass71fa5b42020-12-03 16:55:18 -0700541void *dev_get_uclass_plat(const struct udevice *dev)
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200542{
543 if (!dev) {
Simon Glasscdfc5052015-07-06 16:47:40 -0600544 dm_warn("%s: null device\n", __func__);
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200545 return NULL;
546 }
547
Simon Glass71fa5b42020-12-03 16:55:18 -0700548 return dev->uclass_plat;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200549}
550
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600551void *dev_get_priv(const struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700552{
553 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700554 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700555 return NULL;
556 }
557
558 return dev->priv;
559}
Simon Glass48d4e292014-07-23 06:55:19 -0600560
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600561void *dev_get_uclass_priv(const struct udevice *dev)
Simon Glassde0977b2015-03-05 12:25:20 -0700562{
563 if (!dev) {
564 dm_warn("%s: null device\n", __func__);
565 return NULL;
566 }
567
568 return dev->uclass_priv;
569}
570
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600571void *dev_get_parent_priv(const struct udevice *dev)
Simon Glass60d971b2014-07-23 06:55:20 -0600572{
573 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700574 dm_warn("%s: null device\n", __func__);
Simon Glass60d971b2014-07-23 06:55:20 -0600575 return NULL;
576 }
577
578 return dev->parent_priv;
579}
580
Simon Glass48d4e292014-07-23 06:55:19 -0600581static int device_get_device_tail(struct udevice *dev, int ret,
582 struct udevice **devp)
583{
584 if (ret)
585 return ret;
586
587 ret = device_probe(dev);
588 if (ret)
589 return ret;
590
591 *devp = dev;
592
593 return 0;
594}
595
Marek Vasut7f18c342019-08-31 18:03:28 +0200596#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Mario Six3bedfbe2018-06-26 08:46:50 +0200597/**
598 * device_find_by_ofnode() - Return device associated with given ofnode
599 *
600 * The returned device is *not* activated.
601 *
602 * @node: The ofnode for which a associated device should be looked up
603 * @devp: Pointer to structure to hold the found device
604 * Return: 0 if OK, -ve on error
605 */
606static int device_find_by_ofnode(ofnode node, struct udevice **devp)
607{
608 struct uclass *uc;
609 struct udevice *dev;
610 int ret;
611
612 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
613 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
614 &dev);
615 if (!ret || dev) {
616 *devp = dev;
617 return 0;
618 }
619 }
620
621 return -ENODEV;
622}
Marek Vasut7f18c342019-08-31 18:03:28 +0200623#endif
Mario Six3bedfbe2018-06-26 08:46:50 +0200624
Simon Glass2a580882020-01-27 08:49:36 -0700625int device_get_child(const struct udevice *parent, int index,
626 struct udevice **devp)
Simon Glass48d4e292014-07-23 06:55:19 -0600627{
628 struct udevice *dev;
629
630 list_for_each_entry(dev, &parent->child_head, sibling_node) {
631 if (!index--)
632 return device_get_device_tail(dev, 0, devp);
633 }
634
635 return -ENODEV;
636}
637
Simon Glass2a580882020-01-27 08:49:36 -0700638int device_get_child_count(const struct udevice *parent)
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530639{
640 struct udevice *dev;
641 int count = 0;
642
643 list_for_each_entry(dev, &parent->child_head, sibling_node)
644 count++;
645
646 return count;
647}
648
Simon Glass2a580882020-01-27 08:49:36 -0700649int device_find_child_by_seq(const struct udevice *parent, int seq_or_req_seq,
Simon Glass48d4e292014-07-23 06:55:19 -0600650 bool find_req_seq, struct udevice **devp)
651{
652 struct udevice *dev;
653
654 *devp = NULL;
655 if (seq_or_req_seq == -1)
656 return -ENODEV;
657
658 list_for_each_entry(dev, &parent->child_head, sibling_node) {
659 if ((find_req_seq ? dev->req_seq : dev->seq) ==
660 seq_or_req_seq) {
661 *devp = dev;
662 return 0;
663 }
664 }
665
666 return -ENODEV;
667}
668
Simon Glass2a580882020-01-27 08:49:36 -0700669int device_get_child_by_seq(const struct udevice *parent, int seq,
Simon Glass48d4e292014-07-23 06:55:19 -0600670 struct udevice **devp)
671{
672 struct udevice *dev;
673 int ret;
674
675 *devp = NULL;
676 ret = device_find_child_by_seq(parent, seq, false, &dev);
677 if (ret == -ENODEV) {
678 /*
679 * We didn't find it in probed devices. See if there is one
680 * that will request this seq if probed.
681 */
682 ret = device_find_child_by_seq(parent, seq, true, &dev);
683 }
684 return device_get_device_tail(dev, ret, devp);
685}
686
Simon Glass2a580882020-01-27 08:49:36 -0700687int device_find_child_by_of_offset(const struct udevice *parent, int of_offset,
Simon Glass48d4e292014-07-23 06:55:19 -0600688 struct udevice **devp)
689{
690 struct udevice *dev;
691
692 *devp = NULL;
693
694 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700695 if (dev_of_offset(dev) == of_offset) {
Simon Glass48d4e292014-07-23 06:55:19 -0600696 *devp = dev;
697 return 0;
698 }
699 }
700
701 return -ENODEV;
702}
703
Simon Glass2a580882020-01-27 08:49:36 -0700704int device_get_child_by_of_offset(const struct udevice *parent, int node,
Simon Glass48d4e292014-07-23 06:55:19 -0600705 struct udevice **devp)
706{
707 struct udevice *dev;
708 int ret;
709
710 *devp = NULL;
Simon Glass861bc9f2015-06-23 15:38:38 -0600711 ret = device_find_child_by_of_offset(parent, node, &dev);
Simon Glass48d4e292014-07-23 06:55:19 -0600712 return device_get_device_tail(dev, ret, devp);
713}
Simon Glass44da7352014-10-13 23:41:49 -0600714
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200715static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
716 ofnode ofnode)
Simon Glassae2efac2015-06-23 15:38:37 -0600717{
718 struct udevice *dev, *found;
719
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200720 if (ofnode_equal(dev_ofnode(parent), ofnode))
Simon Glassae2efac2015-06-23 15:38:37 -0600721 return parent;
722
723 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200724 found = _device_find_global_by_ofnode(dev, ofnode);
Simon Glassae2efac2015-06-23 15:38:37 -0600725 if (found)
726 return found;
727 }
728
729 return NULL;
730}
731
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200732int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
733{
734 *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
735
736 return *devp ? 0 : -ENOENT;
737}
738
739int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
Simon Glassae2efac2015-06-23 15:38:37 -0600740{
741 struct udevice *dev;
742
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200743 dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
Simon Glassae2efac2015-06-23 15:38:37 -0600744 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
745}
Walter Lozanoffc41b02020-06-25 01:10:11 -0300746
747#if CONFIG_IS_ENABLED(OF_PLATDATA)
748int device_get_by_driver_info(const struct driver_info *info,
749 struct udevice **devp)
750{
Simon Glasscfd6a002020-10-03 11:31:33 -0600751 struct driver_info *info_base =
752 ll_entry_start(struct driver_info, driver_info);
753 int idx = info - info_base;
754 struct driver_rt *drt = gd_dm_driver_rt() + idx;
Walter Lozanoffc41b02020-06-25 01:10:11 -0300755 struct udevice *dev;
756
Simon Glasscfd6a002020-10-03 11:31:33 -0600757 dev = drt->dev;
Simon Glassd825ea72020-10-03 11:31:31 -0600758 *devp = NULL;
Walter Lozanoffc41b02020-06-25 01:10:11 -0300759
760 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
761}
Simon Glass5792f4b2020-10-03 11:31:40 -0600762
763int device_get_by_driver_info_idx(uint idx, struct udevice **devp)
764{
765 struct driver_rt *drt = gd_dm_driver_rt() + idx;
766 struct udevice *dev;
767
768 dev = drt->dev;
769 *devp = NULL;
770
771 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
772}
Walter Lozanoffc41b02020-06-25 01:10:11 -0300773#endif
Simon Glassae2efac2015-06-23 15:38:37 -0600774
Simon Glass2a580882020-01-27 08:49:36 -0700775int device_find_first_child(const struct udevice *parent, struct udevice **devp)
Simon Glass44da7352014-10-13 23:41:49 -0600776{
777 if (list_empty(&parent->child_head)) {
778 *devp = NULL;
779 } else {
780 *devp = list_first_entry(&parent->child_head, struct udevice,
781 sibling_node);
782 }
783
784 return 0;
785}
786
787int device_find_next_child(struct udevice **devp)
788{
789 struct udevice *dev = *devp;
790 struct udevice *parent = dev->parent;
791
792 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
793 *devp = NULL;
794 } else {
795 *devp = list_entry(dev->sibling_node.next, struct udevice,
796 sibling_node);
797 }
798
799 return 0;
800}
Simon Glass70c3a0e2014-11-11 10:46:18 -0700801
Simon Glass2a580882020-01-27 08:49:36 -0700802int device_find_first_inactive_child(const struct udevice *parent,
Simon Glassb775e872018-10-01 12:22:07 -0600803 enum uclass_id uclass_id,
804 struct udevice **devp)
805{
806 struct udevice *dev;
807
808 *devp = NULL;
809 list_for_each_entry(dev, &parent->child_head, sibling_node) {
810 if (!device_active(dev) &&
811 device_get_uclass_id(dev) == uclass_id) {
812 *devp = dev;
813 return 0;
814 }
815 }
816
817 return -ENODEV;
818}
819
Simon Glass2a580882020-01-27 08:49:36 -0700820int device_find_first_child_by_uclass(const struct udevice *parent,
Simon Glassa11341a2018-11-18 08:14:31 -0700821 enum uclass_id uclass_id,
822 struct udevice **devp)
823{
824 struct udevice *dev;
825
826 *devp = NULL;
827 list_for_each_entry(dev, &parent->child_head, sibling_node) {
828 if (device_get_uclass_id(dev) == uclass_id) {
829 *devp = dev;
830 return 0;
831 }
832 }
833
834 return -ENODEV;
835}
836
Simon Glass2a580882020-01-27 08:49:36 -0700837int device_find_child_by_name(const struct udevice *parent, const char *name,
Simon Glassa11341a2018-11-18 08:14:31 -0700838 struct udevice **devp)
839{
840 struct udevice *dev;
841
842 *devp = NULL;
843
844 list_for_each_entry(dev, &parent->child_head, sibling_node) {
845 if (!strcmp(dev->name, name)) {
846 *devp = dev;
847 return 0;
848 }
849 }
850
851 return -ENODEV;
852}
853
Simon Glass76db9b72020-01-27 08:49:48 -0700854int device_first_child_err(struct udevice *parent, struct udevice **devp)
855{
856 struct udevice *dev;
857
858 device_find_first_child(parent, &dev);
859 if (!dev)
860 return -ENODEV;
861
862 return device_get_device_tail(dev, 0, devp);
863}
864
865int device_next_child_err(struct udevice **devp)
866{
867 struct udevice *dev = *devp;
868
869 device_find_next_child(&dev);
870 if (!dev)
871 return -ENODEV;
872
873 return device_get_device_tail(dev, 0, devp);
874}
875
Simon Glass000676b2020-01-27 08:49:47 -0700876int device_first_child_ofdata_err(struct udevice *parent, struct udevice **devp)
877{
878 struct udevice *dev;
879 int ret;
880
881 device_find_first_child(parent, &dev);
882 if (!dev)
883 return -ENODEV;
884
885 ret = device_ofdata_to_platdata(dev);
886 if (ret)
887 return ret;
888
889 *devp = dev;
890
891 return 0;
892}
893
894int device_next_child_ofdata_err(struct udevice **devp)
895{
896 struct udevice *dev = *devp;
897 int ret;
898
899 device_find_next_child(&dev);
900 if (!dev)
901 return -ENODEV;
902
903 ret = device_ofdata_to_platdata(dev);
904 if (ret)
905 return ret;
906
907 *devp = dev;
908
909 return 0;
910}
911
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600912struct udevice *dev_get_parent(const struct udevice *child)
Simon Glass43f488a2014-11-11 10:46:19 -0700913{
914 return child->parent;
915}
916
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600917ulong dev_get_driver_data(const struct udevice *dev)
Simon Glass70c3a0e2014-11-11 10:46:18 -0700918{
Simon Glass46227bd2015-03-25 12:21:55 -0600919 return dev->driver_data;
Simon Glass70c3a0e2014-11-11 10:46:18 -0700920}
Simon Glass98fd5d12015-01-25 08:27:04 -0700921
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600922const void *dev_get_driver_ops(const struct udevice *dev)
Przemyslaw Marczakd3ef0d72015-04-15 13:07:24 +0200923{
924 if (!dev || !dev->driver->ops)
925 return NULL;
926
927 return dev->driver->ops;
928}
929
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600930enum uclass_id device_get_uclass_id(const struct udevice *dev)
Simon Glass98fd5d12015-01-25 08:27:04 -0700931{
932 return dev->uclass->uc_drv->id;
933}
Peng Fan99b72352015-02-10 14:46:32 +0800934
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600935const char *dev_get_uclass_name(const struct udevice *dev)
Przemyslaw Marczak5ed2e422015-04-15 13:07:25 +0200936{
937 if (!dev)
938 return NULL;
939
940 return dev->uclass->uc_drv->name;
941}
942
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600943bool device_has_children(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -0600944{
945 return !list_empty(&dev->child_head);
946}
947
Simon Glass2a580882020-01-27 08:49:36 -0700948bool device_has_active_children(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -0600949{
950 struct udevice *child;
951
952 for (device_find_first_child(dev, &child);
953 child;
954 device_find_next_child(&child)) {
955 if (device_active(child))
956 return true;
957 }
958
959 return false;
960}
961
Simon Glass2a580882020-01-27 08:49:36 -0700962bool device_is_last_sibling(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -0600963{
964 struct udevice *parent = dev->parent;
965
966 if (!parent)
967 return false;
968 return list_is_last(&dev->sibling_node, &parent->child_head);
969}
Simon Glasse3b23e22015-07-30 13:40:39 -0600970
Simon Glass7760ba22016-05-01 13:52:23 -0600971void device_set_name_alloced(struct udevice *dev)
972{
Simon Glass2d4c7dc2016-07-04 11:58:15 -0600973 dev->flags |= DM_FLAG_NAME_ALLOCED;
Simon Glass7760ba22016-05-01 13:52:23 -0600974}
975
Simon Glasse3b23e22015-07-30 13:40:39 -0600976int device_set_name(struct udevice *dev, const char *name)
977{
978 name = strdup(name);
979 if (!name)
980 return -ENOMEM;
981 dev->name = name;
Simon Glass7760ba22016-05-01 13:52:23 -0600982 device_set_name_alloced(dev);
Simon Glasse3b23e22015-07-30 13:40:39 -0600983
984 return 0;
985}
Mugunthan V N4666bd92016-04-28 15:36:02 +0530986
Marek Vasut7f18c342019-08-31 18:03:28 +0200987#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass2a580882020-01-27 08:49:36 -0700988bool device_is_compatible(const struct udevice *dev, const char *compat)
Mugunthan V N4666bd92016-04-28 15:36:02 +0530989{
Masahiro Yamada9349bcc2018-04-19 12:14:02 +0900990 return ofnode_device_is_compatible(dev_ofnode(dev), compat);
Mugunthan V N4666bd92016-04-28 15:36:02 +0530991}
992
993bool of_machine_is_compatible(const char *compat)
994{
995 const void *fdt = gd->fdt_blob;
996
997 return !fdt_node_check_compatible(fdt, 0, compat);
998}
Mario Six3bedfbe2018-06-26 08:46:50 +0200999
1000int dev_disable_by_path(const char *path)
1001{
1002 struct uclass *uc;
1003 ofnode node = ofnode_path(path);
1004 struct udevice *dev;
1005 int ret = 1;
1006
1007 if (!of_live_active())
1008 return -ENOSYS;
1009
1010 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
1011 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
1012 if (!ret)
1013 break;
1014 }
1015
1016 if (ret)
1017 return ret;
1018
1019 ret = device_remove(dev, DM_REMOVE_NORMAL);
1020 if (ret)
1021 return ret;
1022
1023 ret = device_unbind(dev);
1024 if (ret)
1025 return ret;
1026
1027 return ofnode_set_enabled(node, false);
1028}
1029
1030int dev_enable_by_path(const char *path)
1031{
1032 ofnode node = ofnode_path(path);
1033 ofnode pnode = ofnode_get_parent(node);
1034 struct udevice *parent;
1035 int ret = 1;
1036
1037 if (!of_live_active())
1038 return -ENOSYS;
1039
1040 ret = device_find_by_ofnode(pnode, &parent);
1041 if (ret)
1042 return ret;
1043
1044 ret = ofnode_set_enabled(node, true);
1045 if (ret)
1046 return ret;
1047
Bin Meng9a9b0742018-10-10 22:06:58 -07001048 return lists_bind_fdt(parent, node, NULL, false);
Mario Six3bedfbe2018-06-26 08:46:50 +02001049}
Marek Vasut7f18c342019-08-31 18:03:28 +02001050#endif