base.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 *src,
  115. struct omap_dss_device *dst)
  116. {
  117. dev_dbg(&dss->pdev->dev, "connect(%s, %s)\n",
  118. src ? dev_name(src->dev) : "NULL",
  119. dst ? dev_name(dst->dev) : "NULL");
  120. if (!dst) {
  121. /*
  122. * The destination is NULL when the source is connected to a
  123. * bridge instead of a DSS device. Stop here, we will attach
  124. * the bridge later when we will have a DRM encoder.
  125. */
  126. return src && src->bridge ? 0 : -EINVAL;
  127. }
  128. if (omapdss_device_is_connected(dst))
  129. return -EBUSY;
  130. dst->dss = dss;
  131. return 0;
  132. }
  133. void omapdss_device_disconnect(struct omap_dss_device *src,
  134. struct omap_dss_device *dst)
  135. {
  136. struct dss_device *dss = src ? src->dss : dst->dss;
  137. dev_dbg(&dss->pdev->dev, "disconnect(%s, %s)\n",
  138. src ? dev_name(src->dev) : "NULL",
  139. dst ? dev_name(dst->dev) : "NULL");
  140. if (!dst) {
  141. WARN_ON(!src->bridge);
  142. return;
  143. }
  144. if (!dst->id && !omapdss_device_is_connected(dst)) {
  145. WARN_ON(1);
  146. return;
  147. }
  148. dst->dss = NULL;
  149. }
  150. /* -----------------------------------------------------------------------------
  151. * Components Handling
  152. */
  153. static struct list_head omapdss_comp_list;
  154. struct omapdss_comp_node {
  155. struct list_head list;
  156. struct device_node *node;
  157. bool dss_core_component;
  158. const char *compat;
  159. };
  160. static bool omapdss_list_contains(const struct device_node *node)
  161. {
  162. struct omapdss_comp_node *comp;
  163. list_for_each_entry(comp, &omapdss_comp_list, list) {
  164. if (comp->node == node)
  165. return true;
  166. }
  167. return false;
  168. }
  169. static void omapdss_walk_device(struct device *dev, struct device_node *node,
  170. bool dss_core)
  171. {
  172. struct omapdss_comp_node *comp;
  173. struct device_node *n;
  174. const char *compat;
  175. int ret;
  176. ret = of_property_read_string(node, "compatible", &compat);
  177. if (ret < 0)
  178. return;
  179. comp = devm_kzalloc(dev, sizeof(*comp), GFP_KERNEL);
  180. if (comp) {
  181. comp->node = node;
  182. comp->dss_core_component = dss_core;
  183. comp->compat = compat;
  184. list_add(&comp->list, &omapdss_comp_list);
  185. }
  186. /*
  187. * of_graph_get_remote_port_parent() prints an error if there is no
  188. * port/ports node. To avoid that, check first that there's the node.
  189. */
  190. n = of_get_child_by_name(node, "ports");
  191. if (!n)
  192. n = of_get_child_by_name(node, "port");
  193. if (!n)
  194. return;
  195. of_node_put(n);
  196. n = NULL;
  197. while ((n = of_graph_get_next_endpoint(node, n)) != NULL) {
  198. struct device_node *pn = of_graph_get_remote_port_parent(n);
  199. if (!pn)
  200. continue;
  201. if (!of_device_is_available(pn) || omapdss_list_contains(pn)) {
  202. of_node_put(pn);
  203. continue;
  204. }
  205. omapdss_walk_device(dev, pn, false);
  206. }
  207. }
  208. void omapdss_gather_components(struct device *dev)
  209. {
  210. struct device_node *child;
  211. INIT_LIST_HEAD(&omapdss_comp_list);
  212. omapdss_walk_device(dev, dev->of_node, true);
  213. for_each_available_child_of_node(dev->of_node, child)
  214. omapdss_walk_device(dev, child, true);
  215. }
  216. static bool omapdss_component_is_loaded(struct omapdss_comp_node *comp)
  217. {
  218. if (comp->dss_core_component)
  219. return true;
  220. if (!strstarts(comp->compat, "omapdss,"))
  221. return true;
  222. if (omapdss_device_is_registered(comp->node))
  223. return true;
  224. return false;
  225. }
  226. bool omapdss_stack_is_ready(void)
  227. {
  228. struct omapdss_comp_node *comp;
  229. list_for_each_entry(comp, &omapdss_comp_list, list) {
  230. if (!omapdss_component_is_loaded(comp))
  231. return false;
  232. }
  233. return true;
  234. }