lists.c 3.5 KB

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