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