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