base.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * OMAP Display Subsystem Base
  4. *
  5. * Copyright (C) 2015-2017 Texas Instruments Incorporated - https://www.ti.com/
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/list.h>
  9. #include <linux/module.h>
  10. #include <linux/mutex.h>
  11. #include <linux/of.h>
  12. #include <linux/of_graph.h>
  13. #include <linux/platform_device.h>
  14. #include "dss.h"
  15. #include "omapdss.h"
  16. struct dispc_device *dispc_get_dispc(struct dss_device *dss)
  17. {
  18. return dss->dispc;
  19. }
  20. /* -----------------------------------------------------------------------------
  21. * OMAP DSS Devices Handling
  22. */
  23. static LIST_HEAD(omapdss_devices_list);
  24. static DEFINE_MUTEX(omapdss_devices_lock);
  25. void omapdss_device_register(struct omap_dss_device *dssdev)
  26. {
  27. mutex_lock(&omapdss_devices_lock);
  28. list_add_tail(&dssdev->list, &omapdss_devices_list);
  29. mutex_unlock(&omapdss_devices_lock);
  30. }
  31. void omapdss_device_unregister(struct omap_dss_device *dssdev)
  32. {
  33. mutex_lock(&omapdss_devices_lock);
  34. list_del(&dssdev->list);
  35. mutex_unlock(&omapdss_devices_lock);
  36. }
  37. static bool omapdss_device_is_registered(struct device_node *node)
  38. {
  39. struct omap_dss_device *dssdev;
  40. bool found = false;
  41. mutex_lock(&omapdss_devices_lock);
  42. list_for_each_entry(dssdev, &omapdss_devices_list, list) {
  43. if (dssdev->dev->of_node == node) {
  44. found = true;
  45. break;
  46. }
  47. }
  48. mutex_unlock(&omapdss_devices_lock);
  49. return found;
  50. }
  51. struct omap_dss_device *omapdss_device_get(struct omap_dss_device *dssdev)
  52. {
  53. if (get_device(dssdev->dev) == NULL)
  54. return NULL;
  55. return dssdev;
  56. }
  57. void omapdss_device_put(struct omap_dss_device *dssdev)
  58. {
  59. put_device(dssdev->dev);
  60. }
  61. struct omap_dss_device *omapdss_find_device_by_node(struct device_node *node)
  62. {
  63. struct omap_dss_device *dssdev;
  64. list_for_each_entry(dssdev, &omapdss_devices_list, list) {
  65. if (dssdev->dev->of_node == node)
  66. return omapdss_device_get(dssdev);
  67. }
  68. return NULL;
  69. }
  70. /*
  71. * Search for the next output device starting at @from. Release the reference to
  72. * the @from device, and acquire a reference to the returned device if found.
  73. */
  74. struct omap_dss_device *omapdss_device_next_output(struct omap_dss_device *from)
  75. {
  76. struct omap_dss_device *dssdev;
  77. struct list_head *list;
  78. mutex_lock(&omapdss_devices_lock);
  79. if (list_empty(&omapdss_devices_list)) {
  80. dssdev = NULL;
  81. goto done;
  82. }
  83. /*
  84. * Start from the from entry if given or from omapdss_devices_list
  85. * otherwise.
  86. */
  87. list = from ? &from->list : &omapdss_devices_list;
  88. list_for_each_entry(dssdev, list, list) {
  89. /*
  90. * Stop if we reach the omapdss_devices_list, that's the end of
  91. * the list.
  92. */
  93. if (&dssdev->list == &omapdss_devices_list) {
  94. dssdev = NULL;
  95. goto done;
  96. }
  97. if (dssdev->id && dssdev->bridge)
  98. goto done;
  99. }
  100. dssdev = NULL;
  101. done:
  102. if (from)
  103. omapdss_device_put(from);
  104. if (dssdev)
  105. omapdss_device_get(dssdev);
  106. mutex_unlock(&omapdss_devices_lock);
  107. return dssdev;
  108. }
  109. static bool omapdss_device_is_connected(struct omap_dss_device *dssdev)
  110. {
  111. return dssdev->dss;
  112. }
  113. int omapdss_device_connect(struct dss_device *dss,
  114. struct omap_dss_device *dst)
  115. {
  116. dev_dbg(&dss->pdev->dev, "connect(%s)\n",
  117. dst ? dev_name(dst->dev) : "NULL");
  118. if (!dst)
  119. return -EINVAL;
  120. if (omapdss_device_is_connected(dst))
  121. return -EBUSY;
  122. dst->dss = dss;
  123. return 0;
  124. }
  125. void omapdss_device_disconnect(struct dss_device *dss,
  126. struct omap_dss_device *dst)
  127. {
  128. dev_dbg(&dss->pdev->dev, "disconnect(%s)\n",
  129. dst ? dev_name(dst->dev) : "NULL");
  130. if (WARN_ON(!dst))
  131. return;
  132. if (!dst->id && !omapdss_device_is_connected(dst)) {
  133. WARN_ON(1);
  134. return;
  135. }
  136. dst->dss = NULL;
  137. }
  138. /* -----------------------------------------------------------------------------
  139. * Components Handling
  140. */
  141. static struct list_head omapdss_comp_list;
  142. struct omapdss_comp_node {
  143. struct list_head list;
  144. struct device_node *node;
  145. bool dss_core_component;
  146. const char *compat;
  147. };
  148. static bool omapdss_list_contains(const struct device_node *node)
  149. {
  150. struct omapdss_comp_node *comp;
  151. list_for_each_entry(comp, &omapdss_comp_list, list) {
  152. if (comp->node == node)
  153. return true;
  154. }
  155. return false;
  156. }
  157. static void omapdss_walk_device(struct device *dev, struct device_node *node,
  158. bool dss_core)
  159. {
  160. struct omapdss_comp_node *comp;
  161. struct device_node *n;
  162. const char *compat;
  163. int ret;
  164. ret = of_property_read_string(node, "compatible", &compat);
  165. if (ret < 0)
  166. return;
  167. comp = devm_kzalloc(dev, sizeof(*comp), GFP_KERNEL);
  168. if (comp) {
  169. comp->node = node;
  170. comp->dss_core_component = dss_core;
  171. comp->compat = compat;
  172. list_add(&comp->list, &omapdss_comp_list);
  173. }
  174. /*
  175. * of_graph_get_remote_port_parent() prints an error if there is no
  176. * port/ports node. To avoid that, check first that there's the node.
  177. */
  178. n = of_get_child_by_name(node, "ports");
  179. if (!n)
  180. n = of_get_child_by_name(node, "port");
  181. if (!n)
  182. return;
  183. of_node_put(n);
  184. for_each_endpoint_of_node(node, n) {
  185. struct device_node *pn = of_graph_get_remote_port_parent(n);
  186. if (!pn)
  187. continue;
  188. if (!of_device_is_available(pn) || omapdss_list_contains(pn)) {
  189. of_node_put(pn);
  190. continue;
  191. }
  192. omapdss_walk_device(dev, pn, false);
  193. }
  194. }
  195. void omapdss_gather_components(struct device *dev)
  196. {
  197. struct device_node *child;
  198. INIT_LIST_HEAD(&omapdss_comp_list);
  199. omapdss_walk_device(dev, dev->of_node, true);
  200. for_each_available_child_of_node(dev->of_node, child)
  201. omapdss_walk_device(dev, child, true);
  202. }
  203. static bool omapdss_component_is_loaded(struct omapdss_comp_node *comp)
  204. {
  205. if (comp->dss_core_component)
  206. return true;
  207. if (!strstarts(comp->compat, "omapdss,"))
  208. return true;
  209. if (omapdss_device_is_registered(comp->node))
  210. return true;
  211. return false;
  212. }
  213. bool omapdss_stack_is_ready(void)
  214. {
  215. struct omapdss_comp_node *comp;
  216. list_for_each_entry(comp, &omapdss_comp_list, list) {
  217. if (!omapdss_component_is_loaded(comp))
  218. return false;
  219. }
  220. return true;
  221. }