drm_panel.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (C) 2013, NVIDIA Corporation. All rights reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sub license,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the
  12. * next paragraph) shall be included in all copies or substantial portions
  13. * of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <linux/err.h>
  24. #include <linux/module.h>
  25. #include <drm/drm_crtc.h>
  26. #include <drm/drm_panel.h>
  27. static DEFINE_MUTEX(panel_lock);
  28. static LIST_HEAD(panel_list);
  29. /**
  30. * DOC: drm panel
  31. *
  32. * The DRM panel helpers allow drivers to register panel objects with a
  33. * central registry and provide functions to retrieve those panels in display
  34. * drivers.
  35. */
  36. /**
  37. * drm_panel_init - initialize a panel
  38. * @panel: DRM panel
  39. *
  40. * Sets up internal fields of the panel so that it can subsequently be added
  41. * to the registry.
  42. */
  43. void drm_panel_init(struct drm_panel *panel)
  44. {
  45. INIT_LIST_HEAD(&panel->list);
  46. }
  47. EXPORT_SYMBOL(drm_panel_init);
  48. /**
  49. * drm_panel_add - add a panel to the global registry
  50. * @panel: panel to add
  51. *
  52. * Add a panel to the global registry so that it can be looked up by display
  53. * drivers.
  54. *
  55. * Return: 0 on success or a negative error code on failure.
  56. */
  57. int drm_panel_add(struct drm_panel *panel)
  58. {
  59. mutex_lock(&panel_lock);
  60. list_add_tail(&panel->list, &panel_list);
  61. mutex_unlock(&panel_lock);
  62. return 0;
  63. }
  64. EXPORT_SYMBOL(drm_panel_add);
  65. /**
  66. * drm_panel_remove - remove a panel from the global registry
  67. * @panel: DRM panel
  68. *
  69. * Removes a panel from the global registry.
  70. */
  71. void drm_panel_remove(struct drm_panel *panel)
  72. {
  73. mutex_lock(&panel_lock);
  74. list_del_init(&panel->list);
  75. mutex_unlock(&panel_lock);
  76. }
  77. EXPORT_SYMBOL(drm_panel_remove);
  78. /**
  79. * drm_panel_attach - attach a panel to a connector
  80. * @panel: DRM panel
  81. * @connector: DRM connector
  82. *
  83. * After obtaining a pointer to a DRM panel a display driver calls this
  84. * function to attach a panel to a connector.
  85. *
  86. * An error is returned if the panel is already attached to another connector.
  87. *
  88. * When unloading, the driver should detach from the panel by calling
  89. * drm_panel_detach().
  90. *
  91. * Return: 0 on success or a negative error code on failure.
  92. */
  93. int drm_panel_attach(struct drm_panel *panel, struct drm_connector *connector)
  94. {
  95. if (panel->connector)
  96. return -EBUSY;
  97. panel->connector = connector;
  98. panel->drm = connector->dev;
  99. return 0;
  100. }
  101. EXPORT_SYMBOL(drm_panel_attach);
  102. /**
  103. * drm_panel_detach - detach a panel from a connector
  104. * @panel: DRM panel
  105. *
  106. * Detaches a panel from the connector it is attached to. If a panel is not
  107. * attached to any connector this is effectively a no-op.
  108. *
  109. * This function should not be called by the panel device itself. It
  110. * is only for the drm device that called drm_panel_attach().
  111. *
  112. * Return: 0 on success or a negative error code on failure.
  113. */
  114. int drm_panel_detach(struct drm_panel *panel)
  115. {
  116. panel->connector = NULL;
  117. panel->drm = NULL;
  118. return 0;
  119. }
  120. EXPORT_SYMBOL(drm_panel_detach);
  121. #ifdef CONFIG_OF
  122. /**
  123. * of_drm_find_panel - look up a panel using a device tree node
  124. * @np: device tree node of the panel
  125. *
  126. * Searches the set of registered panels for one that matches the given device
  127. * tree node. If a matching panel is found, return a pointer to it.
  128. *
  129. * Return: A pointer to the panel registered for the specified device tree
  130. * node or an ERR_PTR() if no panel matching the device tree node can be found.
  131. *
  132. * Possible error codes returned by this function:
  133. *
  134. * - EPROBE_DEFER: the panel device has not been probed yet, and the caller
  135. * should retry later
  136. * - ENODEV: the device is not available (status != "okay" or "ok")
  137. */
  138. struct drm_panel *of_drm_find_panel(const struct device_node *np)
  139. {
  140. struct drm_panel *panel;
  141. if (!of_device_is_available(np))
  142. return ERR_PTR(-ENODEV);
  143. mutex_lock(&panel_lock);
  144. list_for_each_entry(panel, &panel_list, list) {
  145. if (panel->dev->of_node == np) {
  146. mutex_unlock(&panel_lock);
  147. return panel;
  148. }
  149. }
  150. mutex_unlock(&panel_lock);
  151. return ERR_PTR(-EPROBE_DEFER);
  152. }
  153. EXPORT_SYMBOL(of_drm_find_panel);
  154. #endif
  155. MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
  156. MODULE_DESCRIPTION("DRM panel infrastructure");
  157. MODULE_LICENSE("GPL and additional rights");