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