omap_encoder.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
  3. * Author: Rob Clark <rob@ti.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/list.h>
  18. #include <drm/drm_crtc.h>
  19. #include <drm/drm_crtc_helper.h>
  20. #include <drm/drm_edid.h>
  21. #include "omap_drv.h"
  22. /*
  23. * encoder funcs
  24. */
  25. #define to_omap_encoder(x) container_of(x, struct omap_encoder, base)
  26. /* The encoder and connector both map to same dssdev.. the encoder
  27. * handles the 'active' parts, ie. anything the modifies the state
  28. * of the hw, and the connector handles the 'read-only' parts, like
  29. * detecting connection and reading edid.
  30. */
  31. struct omap_encoder {
  32. struct drm_encoder base;
  33. struct omap_dss_device *output;
  34. struct omap_dss_device *display;
  35. };
  36. static void omap_encoder_destroy(struct drm_encoder *encoder)
  37. {
  38. struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
  39. drm_encoder_cleanup(encoder);
  40. kfree(omap_encoder);
  41. }
  42. static const struct drm_encoder_funcs omap_encoder_funcs = {
  43. .destroy = omap_encoder_destroy,
  44. };
  45. static void omap_encoder_hdmi_mode_set(struct drm_encoder *encoder,
  46. struct drm_display_mode *adjusted_mode)
  47. {
  48. struct drm_device *dev = encoder->dev;
  49. struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
  50. struct omap_dss_device *dssdev = omap_encoder->output;
  51. struct drm_connector *connector;
  52. bool hdmi_mode;
  53. hdmi_mode = false;
  54. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  55. if (connector->encoder == encoder) {
  56. hdmi_mode = omap_connector_get_hdmi_mode(connector);
  57. break;
  58. }
  59. }
  60. if (dssdev->ops->hdmi.set_hdmi_mode)
  61. dssdev->ops->hdmi.set_hdmi_mode(dssdev, hdmi_mode);
  62. if (hdmi_mode && dssdev->ops->hdmi.set_infoframe) {
  63. struct hdmi_avi_infoframe avi;
  64. int r;
  65. r = drm_hdmi_avi_infoframe_from_display_mode(&avi, adjusted_mode,
  66. false);
  67. if (r == 0)
  68. dssdev->ops->hdmi.set_infoframe(dssdev, &avi);
  69. }
  70. }
  71. static void omap_encoder_mode_set(struct drm_encoder *encoder,
  72. struct drm_display_mode *mode,
  73. struct drm_display_mode *adjusted_mode)
  74. {
  75. struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
  76. struct omap_dss_device *dssdev;
  77. struct videomode vm = { 0 };
  78. drm_display_mode_to_videomode(adjusted_mode, &vm);
  79. /*
  80. * HACK: This fixes the vm flags.
  81. * struct drm_display_mode does not contain the VSYNC/HSYNC/DE flags and
  82. * they get lost when converting back and forth between struct
  83. * drm_display_mode and struct videomode. The hack below goes and
  84. * fetches the missing flags.
  85. *
  86. * A better solution is to use DRM's bus-flags through the whole driver.
  87. */
  88. for (dssdev = omap_encoder->output; dssdev; dssdev = dssdev->next) {
  89. unsigned long bus_flags = dssdev->bus_flags;
  90. if (!(vm.flags & (DISPLAY_FLAGS_DE_LOW |
  91. DISPLAY_FLAGS_DE_HIGH))) {
  92. if (bus_flags & DRM_BUS_FLAG_DE_LOW)
  93. vm.flags |= DISPLAY_FLAGS_DE_LOW;
  94. else if (bus_flags & DRM_BUS_FLAG_DE_HIGH)
  95. vm.flags |= DISPLAY_FLAGS_DE_HIGH;
  96. }
  97. if (!(vm.flags & (DISPLAY_FLAGS_PIXDATA_POSEDGE |
  98. DISPLAY_FLAGS_PIXDATA_NEGEDGE))) {
  99. if (bus_flags & DRM_BUS_FLAG_PIXDATA_POSEDGE)
  100. vm.flags |= DISPLAY_FLAGS_PIXDATA_POSEDGE;
  101. else if (bus_flags & DRM_BUS_FLAG_PIXDATA_NEGEDGE)
  102. vm.flags |= DISPLAY_FLAGS_PIXDATA_NEGEDGE;
  103. }
  104. if (!(vm.flags & (DISPLAY_FLAGS_SYNC_POSEDGE |
  105. DISPLAY_FLAGS_SYNC_NEGEDGE))) {
  106. if (bus_flags & DRM_BUS_FLAG_SYNC_POSEDGE)
  107. vm.flags |= DISPLAY_FLAGS_SYNC_POSEDGE;
  108. else if (bus_flags & DRM_BUS_FLAG_SYNC_NEGEDGE)
  109. vm.flags |= DISPLAY_FLAGS_SYNC_NEGEDGE;
  110. }
  111. }
  112. /* Set timings for all devices in the display pipeline. */
  113. dss_mgr_set_timings(omap_encoder->output, &vm);
  114. for (dssdev = omap_encoder->output; dssdev; dssdev = dssdev->next) {
  115. if (dssdev->ops->set_timings)
  116. dssdev->ops->set_timings(dssdev, &vm);
  117. }
  118. /* Set the HDMI mode and HDMI infoframe if applicable. */
  119. if (omap_encoder->output->output_type == OMAP_DISPLAY_TYPE_HDMI)
  120. omap_encoder_hdmi_mode_set(encoder, adjusted_mode);
  121. }
  122. static void omap_encoder_disable(struct drm_encoder *encoder)
  123. {
  124. struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
  125. struct omap_dss_device *dssdev = omap_encoder->display;
  126. dssdev->ops->disable(dssdev);
  127. }
  128. static void omap_encoder_enable(struct drm_encoder *encoder)
  129. {
  130. struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
  131. struct omap_dss_device *dssdev = omap_encoder->display;
  132. int r;
  133. r = dssdev->ops->enable(dssdev);
  134. if (r)
  135. dev_err(encoder->dev->dev,
  136. "Failed to enable display '%s': %d\n",
  137. dssdev->name, r);
  138. }
  139. static int omap_encoder_atomic_check(struct drm_encoder *encoder,
  140. struct drm_crtc_state *crtc_state,
  141. struct drm_connector_state *conn_state)
  142. {
  143. struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
  144. enum omap_channel channel = omap_encoder->output->dispc_channel;
  145. struct drm_device *dev = encoder->dev;
  146. struct omap_drm_private *priv = dev->dev_private;
  147. struct omap_dss_device *dssdev;
  148. struct videomode vm = { 0 };
  149. int ret;
  150. drm_display_mode_to_videomode(&crtc_state->mode, &vm);
  151. ret = priv->dispc_ops->mgr_check_timings(priv->dispc, channel, &vm);
  152. if (ret)
  153. goto done;
  154. for (dssdev = omap_encoder->output; dssdev; dssdev = dssdev->next) {
  155. if (!dssdev->ops->check_timings)
  156. continue;
  157. ret = dssdev->ops->check_timings(dssdev, &vm);
  158. if (ret)
  159. goto done;
  160. }
  161. drm_display_mode_from_videomode(&vm, &crtc_state->adjusted_mode);
  162. done:
  163. if (ret)
  164. dev_err(dev->dev, "invalid timings: %d\n", ret);
  165. return ret;
  166. }
  167. static const struct drm_encoder_helper_funcs omap_encoder_helper_funcs = {
  168. .mode_set = omap_encoder_mode_set,
  169. .disable = omap_encoder_disable,
  170. .enable = omap_encoder_enable,
  171. .atomic_check = omap_encoder_atomic_check,
  172. };
  173. /* initialize encoder */
  174. struct drm_encoder *omap_encoder_init(struct drm_device *dev,
  175. struct omap_dss_device *output,
  176. struct omap_dss_device *display)
  177. {
  178. struct drm_encoder *encoder = NULL;
  179. struct omap_encoder *omap_encoder;
  180. omap_encoder = kzalloc(sizeof(*omap_encoder), GFP_KERNEL);
  181. if (!omap_encoder)
  182. goto fail;
  183. omap_encoder->output = output;
  184. omap_encoder->display = display;
  185. encoder = &omap_encoder->base;
  186. drm_encoder_init(dev, encoder, &omap_encoder_funcs,
  187. DRM_MODE_ENCODER_TMDS, NULL);
  188. drm_encoder_helper_add(encoder, &omap_encoder_helper_funcs);
  189. return encoder;
  190. fail:
  191. if (encoder)
  192. omap_encoder_destroy(encoder);
  193. return NULL;
  194. }