dm: core: Add a way to read platdata for all child devices
[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 <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_PLATDATA_VALID)
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         dev->flags |= DM_FLAG_PLATDATA_VALID;
372
373         return 0;
374 fail:
375         device_free(dev);
376
377         return ret;
378 }
379
380 int device_probe(struct udevice *dev)
381 {
382         const struct driver *drv;
383         int ret;
384         int seq;
385
386         if (!dev)
387                 return -EINVAL;
388
389         if (dev->flags & DM_FLAG_ACTIVATED)
390                 return 0;
391
392         drv = dev->driver;
393         assert(drv);
394
395         ret = device_ofdata_to_platdata(dev);
396         if (ret)
397                 goto fail;
398
399         /* Ensure all parents are probed */
400         if (dev->parent) {
401                 ret = device_probe(dev->parent);
402                 if (ret)
403                         goto fail;
404
405                 /*
406                  * The device might have already been probed during
407                  * the call to device_probe() on its parent device
408                  * (e.g. PCI bridge devices). Test the flags again
409                  * so that we don't mess up the device.
410                  */
411                 if (dev->flags & DM_FLAG_ACTIVATED)
412                         return 0;
413         }
414
415         seq = uclass_resolve_seq(dev);
416         if (seq < 0) {
417                 ret = seq;
418                 goto fail;
419         }
420         dev->seq = seq;
421
422         dev->flags |= DM_FLAG_ACTIVATED;
423
424         /*
425          * Process pinctrl for everything except the root device, and
426          * continue regardless of the result of pinctrl. Don't process pinctrl
427          * settings for pinctrl devices since the device may not yet be
428          * probed.
429          */
430         if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
431                 pinctrl_select_state(dev, "default");
432
433         if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
434             (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) &&
435             !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) {
436                 ret = dev_power_domain_on(dev);
437                 if (ret)
438                         goto fail;
439         }
440
441         ret = uclass_pre_probe_device(dev);
442         if (ret)
443                 goto fail;
444
445         if (dev->parent && dev->parent->driver->child_pre_probe) {
446                 ret = dev->parent->driver->child_pre_probe(dev);
447                 if (ret)
448                         goto fail;
449         }
450
451         /* Only handle devices that have a valid ofnode */
452         if (dev_of_valid(dev)) {
453                 /*
454                  * Process 'assigned-{clocks/clock-parents/clock-rates}'
455                  * properties
456                  */
457                 ret = clk_set_defaults(dev, 0);
458                 if (ret)
459                         goto fail;
460         }
461
462         if (drv->probe) {
463                 ret = drv->probe(dev);
464                 if (ret)
465                         goto fail;
466         }
467
468         ret = uclass_post_probe_device(dev);
469         if (ret)
470                 goto fail_uclass;
471
472         if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
473                 pinctrl_select_state(dev, "default");
474
475         return 0;
476 fail_uclass:
477         if (device_remove(dev, DM_REMOVE_NORMAL)) {
478                 dm_warn("%s: Device '%s' failed to remove on error path\n",
479                         __func__, dev->name);
480         }
481 fail:
482         dev->flags &= ~DM_FLAG_ACTIVATED;
483
484         dev->seq = -1;
485         device_free(dev);
486
487         return ret;
488 }
489
490 void *dev_get_platdata(const struct udevice *dev)
491 {
492         if (!dev) {
493                 dm_warn("%s: null device\n", __func__);
494                 return NULL;
495         }
496
497         return dev->platdata;
498 }
499
500 void *dev_get_parent_platdata(const struct udevice *dev)
501 {
502         if (!dev) {
503                 dm_warn("%s: null device\n", __func__);
504                 return NULL;
505         }
506
507         return dev->parent_platdata;
508 }
509
510 void *dev_get_uclass_platdata(const struct udevice *dev)
511 {
512         if (!dev) {
513                 dm_warn("%s: null device\n", __func__);
514                 return NULL;
515         }
516
517         return dev->uclass_platdata;
518 }
519
520 void *dev_get_priv(const struct udevice *dev)
521 {
522         if (!dev) {
523                 dm_warn("%s: null device\n", __func__);
524                 return NULL;
525         }
526
527         return dev->priv;
528 }
529
530 void *dev_get_uclass_priv(const struct udevice *dev)
531 {
532         if (!dev) {
533                 dm_warn("%s: null device\n", __func__);
534                 return NULL;
535         }
536
537         return dev->uclass_priv;
538 }
539
540 void *dev_get_parent_priv(const struct udevice *dev)
541 {
542         if (!dev) {
543                 dm_warn("%s: null device\n", __func__);
544                 return NULL;
545         }
546
547         return dev->parent_priv;
548 }
549
550 static int device_get_device_tail(struct udevice *dev, int ret,
551                                   struct udevice **devp)
552 {
553         if (ret)
554                 return ret;
555
556         ret = device_probe(dev);
557         if (ret)
558                 return ret;
559
560         *devp = dev;
561
562         return 0;
563 }
564
565 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
566 /**
567  * device_find_by_ofnode() - Return device associated with given ofnode
568  *
569  * The returned device is *not* activated.
570  *
571  * @node: The ofnode for which a associated device should be looked up
572  * @devp: Pointer to structure to hold the found device
573  * Return: 0 if OK, -ve on error
574  */
575 static int device_find_by_ofnode(ofnode node, struct udevice **devp)
576 {
577         struct uclass *uc;
578         struct udevice *dev;
579         int ret;
580
581         list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
582                 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
583                                                    &dev);
584                 if (!ret || dev) {
585                         *devp = dev;
586                         return 0;
587                 }
588         }
589
590         return -ENODEV;
591 }
592 #endif
593
594 int device_get_child(const struct udevice *parent, int index,
595                      struct udevice **devp)
596 {
597         struct udevice *dev;
598
599         list_for_each_entry(dev, &parent->child_head, sibling_node) {
600                 if (!index--)
601                         return device_get_device_tail(dev, 0, devp);
602         }
603
604         return -ENODEV;
605 }
606
607 int device_get_child_count(const struct udevice *parent)
608 {
609         struct udevice *dev;
610         int count = 0;
611
612         list_for_each_entry(dev, &parent->child_head, sibling_node)
613                 count++;
614
615         return count;
616 }
617
618 int device_find_child_by_seq(const struct udevice *parent, int seq_or_req_seq,
619                              bool find_req_seq, struct udevice **devp)
620 {
621         struct udevice *dev;
622
623         *devp = NULL;
624         if (seq_or_req_seq == -1)
625                 return -ENODEV;
626
627         list_for_each_entry(dev, &parent->child_head, sibling_node) {
628                 if ((find_req_seq ? dev->req_seq : dev->seq) ==
629                                 seq_or_req_seq) {
630                         *devp = dev;
631                         return 0;
632                 }
633         }
634
635         return -ENODEV;
636 }
637
638 int device_get_child_by_seq(const struct udevice *parent, int seq,
639                             struct udevice **devp)
640 {
641         struct udevice *dev;
642         int ret;
643
644         *devp = NULL;
645         ret = device_find_child_by_seq(parent, seq, false, &dev);
646         if (ret == -ENODEV) {
647                 /*
648                  * We didn't find it in probed devices. See if there is one
649                  * that will request this seq if probed.
650                  */
651                 ret = device_find_child_by_seq(parent, seq, true, &dev);
652         }
653         return device_get_device_tail(dev, ret, devp);
654 }
655
656 int device_find_child_by_of_offset(const struct udevice *parent, int of_offset,
657                                    struct udevice **devp)
658 {
659         struct udevice *dev;
660
661         *devp = NULL;
662
663         list_for_each_entry(dev, &parent->child_head, sibling_node) {
664                 if (dev_of_offset(dev) == of_offset) {
665                         *devp = dev;
666                         return 0;
667                 }
668         }
669
670         return -ENODEV;
671 }
672
673 int device_get_child_by_of_offset(const struct udevice *parent, int node,
674                                   struct udevice **devp)
675 {
676         struct udevice *dev;
677         int ret;
678
679         *devp = NULL;
680         ret = device_find_child_by_of_offset(parent, node, &dev);
681         return device_get_device_tail(dev, ret, devp);
682 }
683
684 static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
685                                                      ofnode ofnode)
686 {
687         struct udevice *dev, *found;
688
689         if (ofnode_equal(dev_ofnode(parent), ofnode))
690                 return parent;
691
692         list_for_each_entry(dev, &parent->child_head, sibling_node) {
693                 found = _device_find_global_by_ofnode(dev, ofnode);
694                 if (found)
695                         return found;
696         }
697
698         return NULL;
699 }
700
701 int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
702 {
703         *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
704
705         return *devp ? 0 : -ENOENT;
706 }
707
708 int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
709 {
710         struct udevice *dev;
711
712         dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
713         return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
714 }
715
716 int device_find_first_child(const struct udevice *parent, struct udevice **devp)
717 {
718         if (list_empty(&parent->child_head)) {
719                 *devp = NULL;
720         } else {
721                 *devp = list_first_entry(&parent->child_head, struct udevice,
722                                          sibling_node);
723         }
724
725         return 0;
726 }
727
728 int device_find_next_child(struct udevice **devp)
729 {
730         struct udevice *dev = *devp;
731         struct udevice *parent = dev->parent;
732
733         if (list_is_last(&dev->sibling_node, &parent->child_head)) {
734                 *devp = NULL;
735         } else {
736                 *devp = list_entry(dev->sibling_node.next, struct udevice,
737                                    sibling_node);
738         }
739
740         return 0;
741 }
742
743 int device_find_first_inactive_child(const struct udevice *parent,
744                                      enum uclass_id uclass_id,
745                                      struct udevice **devp)
746 {
747         struct udevice *dev;
748
749         *devp = NULL;
750         list_for_each_entry(dev, &parent->child_head, sibling_node) {
751                 if (!device_active(dev) &&
752                     device_get_uclass_id(dev) == uclass_id) {
753                         *devp = dev;
754                         return 0;
755                 }
756         }
757
758         return -ENODEV;
759 }
760
761 int device_find_first_child_by_uclass(const struct udevice *parent,
762                                       enum uclass_id uclass_id,
763                                       struct udevice **devp)
764 {
765         struct udevice *dev;
766
767         *devp = NULL;
768         list_for_each_entry(dev, &parent->child_head, sibling_node) {
769                 if (device_get_uclass_id(dev) == uclass_id) {
770                         *devp = dev;
771                         return 0;
772                 }
773         }
774
775         return -ENODEV;
776 }
777
778 int device_find_child_by_name(const struct udevice *parent, const char *name,
779                               struct udevice **devp)
780 {
781         struct udevice *dev;
782
783         *devp = NULL;
784
785         list_for_each_entry(dev, &parent->child_head, sibling_node) {
786                 if (!strcmp(dev->name, name)) {
787                         *devp = dev;
788                         return 0;
789                 }
790         }
791
792         return -ENODEV;
793 }
794
795 int device_first_child_ofdata_err(struct udevice *parent, struct udevice **devp)
796 {
797         struct udevice *dev;
798         int ret;
799
800         device_find_first_child(parent, &dev);
801         if (!dev)
802                 return -ENODEV;
803
804         ret = device_ofdata_to_platdata(dev);
805         if (ret)
806                 return ret;
807
808         *devp = dev;
809
810         return 0;
811 }
812
813 int device_next_child_ofdata_err(struct udevice **devp)
814 {
815         struct udevice *dev = *devp;
816         int ret;
817
818         device_find_next_child(&dev);
819         if (!dev)
820                 return -ENODEV;
821
822         ret = device_ofdata_to_platdata(dev);
823         if (ret)
824                 return ret;
825
826         *devp = dev;
827
828         return 0;
829 }
830
831 struct udevice *dev_get_parent(const struct udevice *child)
832 {
833         return child->parent;
834 }
835
836 ulong dev_get_driver_data(const struct udevice *dev)
837 {
838         return dev->driver_data;
839 }
840
841 const void *dev_get_driver_ops(const struct udevice *dev)
842 {
843         if (!dev || !dev->driver->ops)
844                 return NULL;
845
846         return dev->driver->ops;
847 }
848
849 enum uclass_id device_get_uclass_id(const struct udevice *dev)
850 {
851         return dev->uclass->uc_drv->id;
852 }
853
854 const char *dev_get_uclass_name(const struct udevice *dev)
855 {
856         if (!dev)
857                 return NULL;
858
859         return dev->uclass->uc_drv->name;
860 }
861
862 bool device_has_children(const struct udevice *dev)
863 {
864         return !list_empty(&dev->child_head);
865 }
866
867 bool device_has_active_children(const struct udevice *dev)
868 {
869         struct udevice *child;
870
871         for (device_find_first_child(dev, &child);
872              child;
873              device_find_next_child(&child)) {
874                 if (device_active(child))
875                         return true;
876         }
877
878         return false;
879 }
880
881 bool device_is_last_sibling(const struct udevice *dev)
882 {
883         struct udevice *parent = dev->parent;
884
885         if (!parent)
886                 return false;
887         return list_is_last(&dev->sibling_node, &parent->child_head);
888 }
889
890 void device_set_name_alloced(struct udevice *dev)
891 {
892         dev->flags |= DM_FLAG_NAME_ALLOCED;
893 }
894
895 int device_set_name(struct udevice *dev, const char *name)
896 {
897         name = strdup(name);
898         if (!name)
899                 return -ENOMEM;
900         dev->name = name;
901         device_set_name_alloced(dev);
902
903         return 0;
904 }
905
906 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
907 bool device_is_compatible(const struct udevice *dev, const char *compat)
908 {
909         return ofnode_device_is_compatible(dev_ofnode(dev), compat);
910 }
911
912 bool of_machine_is_compatible(const char *compat)
913 {
914         const void *fdt = gd->fdt_blob;
915
916         return !fdt_node_check_compatible(fdt, 0, compat);
917 }
918
919 int dev_disable_by_path(const char *path)
920 {
921         struct uclass *uc;
922         ofnode node = ofnode_path(path);
923         struct udevice *dev;
924         int ret = 1;
925
926         if (!of_live_active())
927                 return -ENOSYS;
928
929         list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
930                 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
931                 if (!ret)
932                         break;
933         }
934
935         if (ret)
936                 return ret;
937
938         ret = device_remove(dev, DM_REMOVE_NORMAL);
939         if (ret)
940                 return ret;
941
942         ret = device_unbind(dev);
943         if (ret)
944                 return ret;
945
946         return ofnode_set_enabled(node, false);
947 }
948
949 int dev_enable_by_path(const char *path)
950 {
951         ofnode node = ofnode_path(path);
952         ofnode pnode = ofnode_get_parent(node);
953         struct udevice *parent;
954         int ret = 1;
955
956         if (!of_live_active())
957                 return -ENOSYS;
958
959         ret = device_find_by_ofnode(pnode, &parent);
960         if (ret)
961                 return ret;
962
963         ret = ofnode_set_enabled(node, true);
964         if (ret)
965                 return ret;
966
967         return lists_bind_fdt(parent, node, NULL, false);
968 }
969 #endif