lists.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) 2013 Google, Inc
  3. *
  4. * (C) Copyright 2012
  5. * Marek Vasut <marex@denx.de>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <errno.h>
  11. #include <dm/device.h>
  12. #include <dm/device-internal.h>
  13. #include <dm/platdata.h>
  14. #include <dm/uclass.h>
  15. #include <dm/util.h>
  16. #include <fdtdec.h>
  17. #include <linux/compiler.h>
  18. struct driver *lists_driver_lookup_name(const char *name)
  19. {
  20. struct driver *drv =
  21. ll_entry_start(struct driver, driver);
  22. const int n_ents = ll_entry_count(struct driver, driver);
  23. struct driver *entry;
  24. int len;
  25. if (!drv || !n_ents)
  26. return NULL;
  27. len = strlen(name);
  28. for (entry = drv; entry != drv + n_ents; entry++) {
  29. if (strncmp(name, entry->name, len))
  30. continue;
  31. /* Full match */
  32. if (len == strlen(entry->name))
  33. return entry;
  34. }
  35. /* Not found */
  36. return NULL;
  37. }
  38. struct uclass_driver *lists_uclass_lookup(enum uclass_id id)
  39. {
  40. struct uclass_driver *uclass =
  41. ll_entry_start(struct uclass_driver, uclass);
  42. const int n_ents = ll_entry_count(struct uclass_driver, uclass);
  43. struct uclass_driver *entry;
  44. if ((id == UCLASS_INVALID) || !uclass)
  45. return NULL;
  46. for (entry = uclass; entry != uclass + n_ents; entry++) {
  47. if (entry->id == id)
  48. return entry;
  49. }
  50. return NULL;
  51. }
  52. int lists_bind_drivers(struct udevice *parent)
  53. {
  54. struct driver_info *info =
  55. ll_entry_start(struct driver_info, driver_info);
  56. const int n_ents = ll_entry_count(struct driver_info, driver_info);
  57. struct driver_info *entry;
  58. struct udevice *dev;
  59. int result = 0;
  60. int ret;
  61. for (entry = info; entry != info + n_ents; entry++) {
  62. ret = device_bind_by_name(parent, entry, &dev);
  63. if (ret) {
  64. dm_warn("No match for driver '%s'\n", entry->name);
  65. if (!result || ret != -ENOENT)
  66. result = ret;
  67. }
  68. }
  69. return result;
  70. }
  71. #ifdef CONFIG_OF_CONTROL
  72. /**
  73. * driver_check_compatible() - Check if a driver is compatible with this node
  74. *
  75. * @param blob: Device tree pointer
  76. * @param offset: Offset of node in device tree
  77. * @param of_matchL List of compatible strings to match
  78. * @return 0 if there is a match, -ENOENT if no match, -ENODEV if the node
  79. * does not have a compatible string, other error <0 if there is a device
  80. * tree error
  81. */
  82. static int driver_check_compatible(const void *blob, int offset,
  83. const struct udevice_id *of_match)
  84. {
  85. int ret;
  86. if (!of_match)
  87. return -ENOENT;
  88. while (of_match->compatible) {
  89. ret = fdt_node_check_compatible(blob, offset,
  90. of_match->compatible);
  91. if (!ret)
  92. return 0;
  93. else if (ret == -FDT_ERR_NOTFOUND)
  94. return -ENODEV;
  95. else if (ret < 0)
  96. return -EINVAL;
  97. of_match++;
  98. }
  99. return -ENOENT;
  100. }
  101. int lists_bind_fdt(struct udevice *parent, const void *blob, int offset)
  102. {
  103. struct driver *driver = ll_entry_start(struct driver, driver);
  104. const int n_ents = ll_entry_count(struct driver, driver);
  105. struct driver *entry;
  106. struct udevice *dev;
  107. const char *name;
  108. int result = 0;
  109. int ret;
  110. dm_dbg("bind node %s\n", fdt_get_name(blob, offset, NULL));
  111. for (entry = driver; entry != driver + n_ents; entry++) {
  112. ret = driver_check_compatible(blob, offset, entry->of_match);
  113. if (ret == -ENOENT) {
  114. continue;
  115. } else if (ret == -ENODEV) {
  116. break;
  117. } else if (ret) {
  118. dm_warn("Device tree error at offset %d\n", offset);
  119. if (!result || ret != -ENOENT)
  120. result = ret;
  121. break;
  122. }
  123. name = fdt_get_name(blob, offset, NULL);
  124. dm_dbg(" - found match at '%s'\n", entry->name);
  125. ret = device_bind(parent, entry, name, NULL, offset, &dev);
  126. if (ret) {
  127. dm_warn("No match for driver '%s'\n", entry->name);
  128. if (!result || ret != -ENOENT)
  129. result = ret;
  130. }
  131. }
  132. return result;
  133. }
  134. #endif