Merge https://gitlab.denx.de/u-boot/custodians/u-boot-net
[oweals/u-boot.git] / drivers / core / device.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Device manager
4  *
5  * Copyright (c) 2013 Google, Inc
6  *
7  * (C) Copyright 2012
8  * Pavel Herrmann <morpheus.ibis@gmail.com>
9  */
10
11 #include <common.h>
12 #include <asm/io.h>
13 #include <clk.h>
14 #include <fdtdec.h>
15 #include <fdt_support.h>
16 #include <malloc.h>
17 #include <dm/device.h>
18 #include <dm/device-internal.h>
19 #include <dm/lists.h>
20 #include <dm/of_access.h>
21 #include <dm/pinctrl.h>
22 #include <dm/platdata.h>
23 #include <dm/read.h>
24 #include <dm/uclass.h>
25 #include <dm/uclass-internal.h>
26 #include <dm/util.h>
27 #include <linux/err.h>
28 #include <linux/list.h>
29 #include <power-domain.h>
30
31 DECLARE_GLOBAL_DATA_PTR;
32
33 static int device_bind_common(struct udevice *parent, const struct driver *drv,
34                               const char *name, void *platdata,
35                               ulong driver_data, ofnode node,
36                               uint of_platdata_size, struct udevice **devp)
37 {
38         struct udevice *dev;
39         struct uclass *uc;
40         int size, ret = 0;
41
42         if (devp)
43                 *devp = NULL;
44         if (!name)
45                 return -EINVAL;
46
47         ret = uclass_get(drv->id, &uc);
48         if (ret) {
49                 debug("Missing uclass for driver %s\n", drv->name);
50                 return ret;
51         }
52
53         dev = calloc(1, sizeof(struct udevice));
54         if (!dev)
55                 return -ENOMEM;
56
57         INIT_LIST_HEAD(&dev->sibling_node);
58         INIT_LIST_HEAD(&dev->child_head);
59         INIT_LIST_HEAD(&dev->uclass_node);
60 #ifdef CONFIG_DEVRES
61         INIT_LIST_HEAD(&dev->devres_head);
62 #endif
63         dev->platdata = platdata;
64         dev->driver_data = driver_data;
65         dev->name = name;
66         dev->node = node;
67         dev->parent = parent;
68         dev->driver = drv;
69         dev->uclass = uc;
70
71         dev->seq = -1;
72         dev->req_seq = -1;
73         if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
74             (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) {
75                 /*
76                  * Some devices, such as a SPI bus, I2C bus and serial ports
77                  * are numbered using aliases.
78                  *
79                  * This is just a 'requested' sequence, and will be
80                  * resolved (and ->seq updated) when the device is probed.
81                  */
82                 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
83                         if (uc->uc_drv->name && ofnode_valid(node))
84                                 dev_read_alias_seq(dev, &dev->req_seq);
85                 } else {
86                         dev->req_seq = uclass_find_next_free_req_seq(drv->id);
87                 }
88         }
89
90         if (drv->platdata_auto_alloc_size) {
91                 bool alloc = !platdata;
92
93                 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
94                         if (of_platdata_size) {
95                                 dev->flags |= DM_FLAG_OF_PLATDATA;
96                                 if (of_platdata_size <
97                                                 drv->platdata_auto_alloc_size)
98                                         alloc = true;
99                         }
100                 }
101                 if (alloc) {
102                         dev->flags |= DM_FLAG_ALLOC_PDATA;
103                         dev->platdata = calloc(1,
104                                                drv->platdata_auto_alloc_size);
105                         if (!dev->platdata) {
106                                 ret = -ENOMEM;
107                                 goto fail_alloc1;
108                         }
109                         if (CONFIG_IS_ENABLED(OF_PLATDATA) && platdata) {
110                                 memcpy(dev->platdata, platdata,
111                                        of_platdata_size);
112                         }
113                 }
114         }
115
116         size = uc->uc_drv->per_device_platdata_auto_alloc_size;
117         if (size) {
118                 dev->flags |= DM_FLAG_ALLOC_UCLASS_PDATA;
119                 dev->uclass_platdata = calloc(1, size);
120                 if (!dev->uclass_platdata) {
121                         ret = -ENOMEM;
122                         goto fail_alloc2;
123                 }
124         }
125
126         if (parent) {
127                 size = parent->driver->per_child_platdata_auto_alloc_size;
128                 if (!size) {
129                         size = parent->uclass->uc_drv->
130                                         per_child_platdata_auto_alloc_size;
131                 }
132                 if (size) {
133                         dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA;
134                         dev->parent_platdata = calloc(1, size);
135                         if (!dev->parent_platdata) {
136                                 ret = -ENOMEM;
137                                 goto fail_alloc3;
138                         }
139                 }
140         }
141
142         /* put dev into parent's successor list */
143         if (parent)
144                 list_add_tail(&dev->sibling_node, &parent->child_head);
145
146         ret = uclass_bind_device(dev);
147         if (ret)
148                 goto fail_uclass_bind;
149
150         /* if we fail to bind we remove device from successors and free it */
151         if (drv->bind) {
152                 ret = drv->bind(dev);
153                 if (ret)
154                         goto fail_bind;
155         }
156         if (parent && parent->driver->child_post_bind) {
157                 ret = parent->driver->child_post_bind(dev);
158                 if (ret)
159                         goto fail_child_post_bind;
160         }
161         if (uc->uc_drv->post_bind) {
162                 ret = uc->uc_drv->post_bind(dev);
163                 if (ret)
164                         goto fail_uclass_post_bind;
165         }
166
167         if (parent)
168                 pr_debug("Bound device %s to %s\n", dev->name, parent->name);
169         if (devp)
170                 *devp = dev;
171
172         dev->flags |= DM_FLAG_BOUND;
173
174         return 0;
175
176 fail_uclass_post_bind:
177         /* There is no child unbind() method, so no clean-up required */
178 fail_child_post_bind:
179         if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
180                 if (drv->unbind && drv->unbind(dev)) {
181                         dm_warn("unbind() method failed on dev '%s' on error path\n",
182                                 dev->name);
183                 }
184         }
185
186 fail_bind:
187         if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
188                 if (uclass_unbind_device(dev)) {
189                         dm_warn("Failed to unbind dev '%s' on error path\n",
190                                 dev->name);
191                 }
192         }
193 fail_uclass_bind:
194         if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
195                 list_del(&dev->sibling_node);
196                 if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
197                         free(dev->parent_platdata);
198                         dev->parent_platdata = NULL;
199                 }
200         }
201 fail_alloc3:
202         if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
203                 free(dev->uclass_platdata);
204                 dev->uclass_platdata = NULL;
205         }
206 fail_alloc2:
207         if (dev->flags & DM_FLAG_ALLOC_PDATA) {
208                 free(dev->platdata);
209                 dev->platdata = NULL;
210         }
211 fail_alloc1:
212         devres_release_all(dev);
213
214         free(dev);
215
216         return ret;
217 }
218
219 int device_bind_with_driver_data(struct udevice *parent,
220                                  const struct driver *drv, const char *name,
221                                  ulong driver_data, ofnode node,
222                                  struct udevice **devp)
223 {
224         return device_bind_common(parent, drv, name, NULL, driver_data, node,
225                                   0, devp);
226 }
227
228 int device_bind(struct udevice *parent, const struct driver *drv,
229                 const char *name, void *platdata, int of_offset,
230                 struct udevice **devp)
231 {
232         return device_bind_common(parent, drv, name, platdata, 0,
233                                   offset_to_ofnode(of_offset), 0, devp);
234 }
235
236 int device_bind_ofnode(struct udevice *parent, const struct driver *drv,
237                        const char *name, void *platdata, ofnode node,
238                        struct udevice **devp)
239 {
240         return device_bind_common(parent, drv, name, platdata, 0, node, 0,
241                                   devp);
242 }
243
244 int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
245                         const struct driver_info *info, struct udevice **devp)
246 {
247         struct driver *drv;
248         uint platdata_size = 0;
249
250         drv = lists_driver_lookup_name(info->name);
251         if (!drv)
252                 return -ENOENT;
253         if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
254                 return -EPERM;
255
256 #if CONFIG_IS_ENABLED(OF_PLATDATA)
257         platdata_size = info->platdata_size;
258 #endif
259         return device_bind_common(parent, drv, info->name,
260                         (void *)info->platdata, 0, ofnode_null(), platdata_size,
261                         devp);
262 }
263
264 static void *alloc_priv(int size, uint flags)
265 {
266         void *priv;
267
268         if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
269                 size = ROUND(size, ARCH_DMA_MINALIGN);
270                 priv = memalign(ARCH_DMA_MINALIGN, size);
271                 if (priv) {
272                         memset(priv, '\0', size);
273
274                         /*
275                          * Ensure that the zero bytes are flushed to memory.
276                          * This prevents problems if the driver uses this as
277                          * both an input and an output buffer:
278                          *
279                          * 1. Zeroes written to buffer (here) and sit in the
280                          *      cache
281                          * 2. Driver issues a read command to DMA
282                          * 3. CPU runs out of cache space and evicts some cache
283                          *      data in the buffer, writing zeroes to RAM from
284                          *      the memset() above
285                          * 4. DMA completes
286                          * 5. Buffer now has some DMA data and some zeroes
287                          * 6. Data being read is now incorrect
288                          *
289                          * To prevent this, ensure that the cache is clean
290                          * within this range at the start. The driver can then
291                          * use normal flush-after-write, invalidate-before-read
292                          * procedures.
293                          *
294                          * TODO(sjg@chromium.org): Drop this microblaze
295                          * exception.
296                          */
297 #ifndef CONFIG_MICROBLAZE
298                         flush_dcache_range((ulong)priv, (ulong)priv + size);
299 #endif
300                 }
301         } else {
302                 priv = calloc(1, size);
303         }
304
305         return priv;
306 }
307
308 int device_probe(struct udevice *dev)
309 {
310         struct power_domain pd;
311         const struct driver *drv;
312         int size = 0;
313         int ret;
314         int seq;
315
316         if (!dev)
317                 return -EINVAL;
318
319         if (dev->flags & DM_FLAG_ACTIVATED)
320                 return 0;
321
322         drv = dev->driver;
323         assert(drv);
324
325         /* Allocate private data if requested and not reentered */
326         if (drv->priv_auto_alloc_size && !dev->priv) {
327                 dev->priv = alloc_priv(drv->priv_auto_alloc_size, drv->flags);
328                 if (!dev->priv) {
329                         ret = -ENOMEM;
330                         goto fail;
331                 }
332         }
333         /* Allocate private data if requested and not reentered */
334         size = dev->uclass->uc_drv->per_device_auto_alloc_size;
335         if (size && !dev->uclass_priv) {
336                 dev->uclass_priv = alloc_priv(size,
337                                               dev->uclass->uc_drv->flags);
338                 if (!dev->uclass_priv) {
339                         ret = -ENOMEM;
340                         goto fail;
341                 }
342         }
343
344         /* Ensure all parents are probed */
345         if (dev->parent) {
346                 size = dev->parent->driver->per_child_auto_alloc_size;
347                 if (!size) {
348                         size = dev->parent->uclass->uc_drv->
349                                         per_child_auto_alloc_size;
350                 }
351                 if (size && !dev->parent_priv) {
352                         dev->parent_priv = alloc_priv(size, drv->flags);
353                         if (!dev->parent_priv) {
354                                 ret = -ENOMEM;
355                                 goto fail;
356                         }
357                 }
358
359                 ret = device_probe(dev->parent);
360                 if (ret)
361                         goto fail;
362
363                 /*
364                  * The device might have already been probed during
365                  * the call to device_probe() on its parent device
366                  * (e.g. PCI bridge devices). Test the flags again
367                  * so that we don't mess up the device.
368                  */
369                 if (dev->flags & DM_FLAG_ACTIVATED)
370                         return 0;
371         }
372
373         seq = uclass_resolve_seq(dev);
374         if (seq < 0) {
375                 ret = seq;
376                 goto fail;
377         }
378         dev->seq = seq;
379
380         dev->flags |= DM_FLAG_ACTIVATED;
381
382         /*
383          * Process pinctrl for everything except the root device, and
384          * continue regardless of the result of pinctrl. Don't process pinctrl
385          * settings for pinctrl devices since the device may not yet be
386          * probed.
387          */
388         if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
389                 pinctrl_select_state(dev, "default");
390
391         if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
392             device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) {
393                 if (!power_domain_get(dev, &pd))
394                         power_domain_on(&pd);
395         }
396
397         ret = uclass_pre_probe_device(dev);
398         if (ret)
399                 goto fail;
400
401         if (dev->parent && dev->parent->driver->child_pre_probe) {
402                 ret = dev->parent->driver->child_pre_probe(dev);
403                 if (ret)
404                         goto fail;
405         }
406
407         if (drv->ofdata_to_platdata && dev_has_of_node(dev)) {
408                 ret = drv->ofdata_to_platdata(dev);
409                 if (ret)
410                         goto fail;
411         }
412
413         /* Only handle devices that have a valid ofnode */
414         if (dev_of_valid(dev)) {
415                 /*
416                  * Process 'assigned-{clocks/clock-parents/clock-rates}'
417                  * properties
418                  */
419                 ret = clk_set_defaults(dev);
420                 if (ret)
421                         goto fail;
422         }
423
424         if (drv->probe) {
425                 ret = drv->probe(dev);
426                 if (ret) {
427                         dev->flags &= ~DM_FLAG_ACTIVATED;
428                         goto fail;
429                 }
430         }
431
432         ret = uclass_post_probe_device(dev);
433         if (ret)
434                 goto fail_uclass;
435
436         if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
437                 pinctrl_select_state(dev, "default");
438
439         return 0;
440 fail_uclass:
441         if (device_remove(dev, DM_REMOVE_NORMAL)) {
442                 dm_warn("%s: Device '%s' failed to remove on error path\n",
443                         __func__, dev->name);
444         }
445 fail:
446         dev->flags &= ~DM_FLAG_ACTIVATED;
447
448         dev->seq = -1;
449         device_free(dev);
450
451         return ret;
452 }
453
454 void *dev_get_platdata(const struct udevice *dev)
455 {
456         if (!dev) {
457                 dm_warn("%s: null device\n", __func__);
458                 return NULL;
459         }
460
461         return dev->platdata;
462 }
463
464 void *dev_get_parent_platdata(const struct udevice *dev)
465 {
466         if (!dev) {
467                 dm_warn("%s: null device\n", __func__);
468                 return NULL;
469         }
470
471         return dev->parent_platdata;
472 }
473
474 void *dev_get_uclass_platdata(const struct udevice *dev)
475 {
476         if (!dev) {
477                 dm_warn("%s: null device\n", __func__);
478                 return NULL;
479         }
480
481         return dev->uclass_platdata;
482 }
483
484 void *dev_get_priv(const struct udevice *dev)
485 {
486         if (!dev) {
487                 dm_warn("%s: null device\n", __func__);
488                 return NULL;
489         }
490
491         return dev->priv;
492 }
493
494 void *dev_get_uclass_priv(const struct udevice *dev)
495 {
496         if (!dev) {
497                 dm_warn("%s: null device\n", __func__);
498                 return NULL;
499         }
500
501         return dev->uclass_priv;
502 }
503
504 void *dev_get_parent_priv(const struct udevice *dev)
505 {
506         if (!dev) {
507                 dm_warn("%s: null device\n", __func__);
508                 return NULL;
509         }
510
511         return dev->parent_priv;
512 }
513
514 static int device_get_device_tail(struct udevice *dev, int ret,
515                                   struct udevice **devp)
516 {
517         if (ret)
518                 return ret;
519
520         ret = device_probe(dev);
521         if (ret)
522                 return ret;
523
524         *devp = dev;
525
526         return 0;
527 }
528
529 /**
530  * device_find_by_ofnode() - Return device associated with given ofnode
531  *
532  * The returned device is *not* activated.
533  *
534  * @node: The ofnode for which a associated device should be looked up
535  * @devp: Pointer to structure to hold the found device
536  * Return: 0 if OK, -ve on error
537  */
538 static int device_find_by_ofnode(ofnode node, struct udevice **devp)
539 {
540         struct uclass *uc;
541         struct udevice *dev;
542         int ret;
543
544         list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
545                 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
546                                                    &dev);
547                 if (!ret || dev) {
548                         *devp = dev;
549                         return 0;
550                 }
551         }
552
553         return -ENODEV;
554 }
555
556 int device_get_child(struct udevice *parent, int index, struct udevice **devp)
557 {
558         struct udevice *dev;
559
560         list_for_each_entry(dev, &parent->child_head, sibling_node) {
561                 if (!index--)
562                         return device_get_device_tail(dev, 0, devp);
563         }
564
565         return -ENODEV;
566 }
567
568 int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
569                              bool find_req_seq, struct udevice **devp)
570 {
571         struct udevice *dev;
572
573         *devp = NULL;
574         if (seq_or_req_seq == -1)
575                 return -ENODEV;
576
577         list_for_each_entry(dev, &parent->child_head, sibling_node) {
578                 if ((find_req_seq ? dev->req_seq : dev->seq) ==
579                                 seq_or_req_seq) {
580                         *devp = dev;
581                         return 0;
582                 }
583         }
584
585         return -ENODEV;
586 }
587
588 int device_get_child_by_seq(struct udevice *parent, int seq,
589                             struct udevice **devp)
590 {
591         struct udevice *dev;
592         int ret;
593
594         *devp = NULL;
595         ret = device_find_child_by_seq(parent, seq, false, &dev);
596         if (ret == -ENODEV) {
597                 /*
598                  * We didn't find it in probed devices. See if there is one
599                  * that will request this seq if probed.
600                  */
601                 ret = device_find_child_by_seq(parent, seq, true, &dev);
602         }
603         return device_get_device_tail(dev, ret, devp);
604 }
605
606 int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
607                                    struct udevice **devp)
608 {
609         struct udevice *dev;
610
611         *devp = NULL;
612
613         list_for_each_entry(dev, &parent->child_head, sibling_node) {
614                 if (dev_of_offset(dev) == of_offset) {
615                         *devp = dev;
616                         return 0;
617                 }
618         }
619
620         return -ENODEV;
621 }
622
623 int device_get_child_by_of_offset(struct udevice *parent, int node,
624                                   struct udevice **devp)
625 {
626         struct udevice *dev;
627         int ret;
628
629         *devp = NULL;
630         ret = device_find_child_by_of_offset(parent, node, &dev);
631         return device_get_device_tail(dev, ret, devp);
632 }
633
634 static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
635                                                      ofnode ofnode)
636 {
637         struct udevice *dev, *found;
638
639         if (ofnode_equal(dev_ofnode(parent), ofnode))
640                 return parent;
641
642         list_for_each_entry(dev, &parent->child_head, sibling_node) {
643                 found = _device_find_global_by_ofnode(dev, ofnode);
644                 if (found)
645                         return found;
646         }
647
648         return NULL;
649 }
650
651 int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
652 {
653         *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
654
655         return *devp ? 0 : -ENOENT;
656 }
657
658 int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
659 {
660         struct udevice *dev;
661
662         dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
663         return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
664 }
665
666 int device_find_first_child(struct udevice *parent, struct udevice **devp)
667 {
668         if (list_empty(&parent->child_head)) {
669                 *devp = NULL;
670         } else {
671                 *devp = list_first_entry(&parent->child_head, struct udevice,
672                                          sibling_node);
673         }
674
675         return 0;
676 }
677
678 int device_find_next_child(struct udevice **devp)
679 {
680         struct udevice *dev = *devp;
681         struct udevice *parent = dev->parent;
682
683         if (list_is_last(&dev->sibling_node, &parent->child_head)) {
684                 *devp = NULL;
685         } else {
686                 *devp = list_entry(dev->sibling_node.next, struct udevice,
687                                    sibling_node);
688         }
689
690         return 0;
691 }
692
693 int device_find_first_inactive_child(struct udevice *parent,
694                                      enum uclass_id uclass_id,
695                                      struct udevice **devp)
696 {
697         struct udevice *dev;
698
699         *devp = NULL;
700         list_for_each_entry(dev, &parent->child_head, sibling_node) {
701                 if (!device_active(dev) &&
702                     device_get_uclass_id(dev) == uclass_id) {
703                         *devp = dev;
704                         return 0;
705                 }
706         }
707
708         return -ENODEV;
709 }
710
711 int device_find_first_child_by_uclass(struct udevice *parent,
712                                       enum uclass_id uclass_id,
713                                       struct udevice **devp)
714 {
715         struct udevice *dev;
716
717         *devp = NULL;
718         list_for_each_entry(dev, &parent->child_head, sibling_node) {
719                 if (device_get_uclass_id(dev) == uclass_id) {
720                         *devp = dev;
721                         return 0;
722                 }
723         }
724
725         return -ENODEV;
726 }
727
728 int device_find_child_by_name(struct udevice *parent, const char *name,
729                               struct udevice **devp)
730 {
731         struct udevice *dev;
732
733         *devp = NULL;
734
735         list_for_each_entry(dev, &parent->child_head, sibling_node) {
736                 if (!strcmp(dev->name, name)) {
737                         *devp = dev;
738                         return 0;
739                 }
740         }
741
742         return -ENODEV;
743 }
744
745 struct udevice *dev_get_parent(const struct udevice *child)
746 {
747         return child->parent;
748 }
749
750 ulong dev_get_driver_data(const struct udevice *dev)
751 {
752         return dev->driver_data;
753 }
754
755 const void *dev_get_driver_ops(const struct udevice *dev)
756 {
757         if (!dev || !dev->driver->ops)
758                 return NULL;
759
760         return dev->driver->ops;
761 }
762
763 enum uclass_id device_get_uclass_id(const struct udevice *dev)
764 {
765         return dev->uclass->uc_drv->id;
766 }
767
768 const char *dev_get_uclass_name(const struct udevice *dev)
769 {
770         if (!dev)
771                 return NULL;
772
773         return dev->uclass->uc_drv->name;
774 }
775
776 bool device_has_children(const struct udevice *dev)
777 {
778         return !list_empty(&dev->child_head);
779 }
780
781 bool device_has_active_children(struct udevice *dev)
782 {
783         struct udevice *child;
784
785         for (device_find_first_child(dev, &child);
786              child;
787              device_find_next_child(&child)) {
788                 if (device_active(child))
789                         return true;
790         }
791
792         return false;
793 }
794
795 bool device_is_last_sibling(struct udevice *dev)
796 {
797         struct udevice *parent = dev->parent;
798
799         if (!parent)
800                 return false;
801         return list_is_last(&dev->sibling_node, &parent->child_head);
802 }
803
804 void device_set_name_alloced(struct udevice *dev)
805 {
806         dev->flags |= DM_FLAG_NAME_ALLOCED;
807 }
808
809 int device_set_name(struct udevice *dev, const char *name)
810 {
811         name = strdup(name);
812         if (!name)
813                 return -ENOMEM;
814         dev->name = name;
815         device_set_name_alloced(dev);
816
817         return 0;
818 }
819
820 bool device_is_compatible(struct udevice *dev, const char *compat)
821 {
822         return ofnode_device_is_compatible(dev_ofnode(dev), compat);
823 }
824
825 bool of_machine_is_compatible(const char *compat)
826 {
827         const void *fdt = gd->fdt_blob;
828
829         return !fdt_node_check_compatible(fdt, 0, compat);
830 }
831
832 int dev_disable_by_path(const char *path)
833 {
834         struct uclass *uc;
835         ofnode node = ofnode_path(path);
836         struct udevice *dev;
837         int ret = 1;
838
839         if (!of_live_active())
840                 return -ENOSYS;
841
842         list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
843                 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
844                 if (!ret)
845                         break;
846         }
847
848         if (ret)
849                 return ret;
850
851         ret = device_remove(dev, DM_REMOVE_NORMAL);
852         if (ret)
853                 return ret;
854
855         ret = device_unbind(dev);
856         if (ret)
857                 return ret;
858
859         return ofnode_set_enabled(node, false);
860 }
861
862 int dev_enable_by_path(const char *path)
863 {
864         ofnode node = ofnode_path(path);
865         ofnode pnode = ofnode_get_parent(node);
866         struct udevice *parent;
867         int ret = 1;
868
869         if (!of_live_active())
870                 return -ENOSYS;
871
872         ret = device_find_by_ofnode(pnode, &parent);
873         if (ret)
874                 return ret;
875
876         ret = ofnode_set_enabled(node, true);
877         if (ret)
878                 return ret;
879
880         return lists_bind_fdt(parent, node, NULL, false);
881 }