omap_encoder.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * drivers/gpu/drm/omapdrm/omap_encoder.c
  3. *
  4. * Copyright (C) 2011 Texas Instruments
  5. * Author: Rob Clark <rob@ti.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/list.h>
  20. #include <drm/drm_crtc.h>
  21. #include <drm/drm_crtc_helper.h>
  22. #include <drm/drm_edid.h>
  23. #include "omap_drv.h"
  24. /*
  25. * encoder funcs
  26. */
  27. #define to_omap_encoder(x) container_of(x, struct omap_encoder, base)
  28. /* The encoder and connector both map to same dssdev.. the encoder
  29. * handles the 'active' parts, ie. anything the modifies the state
  30. * of the hw, and the connector handles the 'read-only' parts, like
  31. * detecting connection and reading edid.
  32. */
  33. struct omap_encoder {
  34. struct drm_encoder base;
  35. struct omap_dss_device *dssdev;
  36. };
  37. struct omap_dss_device *omap_encoder_get_dssdev(struct drm_encoder *encoder)
  38. {
  39. struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
  40. return omap_encoder->dssdev;
  41. }
  42. static void omap_encoder_destroy(struct drm_encoder *encoder)
  43. {
  44. struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
  45. drm_encoder_cleanup(encoder);
  46. kfree(omap_encoder);
  47. }
  48. static const struct drm_encoder_funcs omap_encoder_funcs = {
  49. .destroy = omap_encoder_destroy,
  50. };
  51. static void omap_encoder_mode_set(struct drm_encoder *encoder,
  52. struct drm_display_mode *mode,
  53. struct drm_display_mode *adjusted_mode)
  54. {
  55. struct drm_device *dev = encoder->dev;
  56. struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
  57. struct omap_dss_device *dssdev = omap_encoder->dssdev;
  58. struct drm_connector *connector;
  59. bool hdmi_mode;
  60. int r;
  61. hdmi_mode = false;
  62. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  63. if (connector->encoder == encoder) {
  64. hdmi_mode = omap_connector_get_hdmi_mode(connector);
  65. break;
  66. }
  67. }
  68. if (dssdev->driver->set_hdmi_mode)
  69. dssdev->driver->set_hdmi_mode(dssdev, hdmi_mode);
  70. if (hdmi_mode && dssdev->driver->set_hdmi_infoframe) {
  71. struct hdmi_avi_infoframe avi;
  72. r = drm_hdmi_avi_infoframe_from_display_mode(&avi, adjusted_mode);
  73. if (r == 0)
  74. dssdev->driver->set_hdmi_infoframe(dssdev, &avi);
  75. }
  76. }
  77. static void omap_encoder_disable(struct drm_encoder *encoder)
  78. {
  79. struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
  80. struct omap_dss_device *dssdev = omap_encoder->dssdev;
  81. struct omap_dss_driver *dssdrv = dssdev->driver;
  82. dssdrv->disable(dssdev);
  83. }
  84. static int omap_encoder_update(struct drm_encoder *encoder,
  85. enum omap_channel channel,
  86. struct omap_video_timings *timings)
  87. {
  88. struct drm_device *dev = encoder->dev;
  89. struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
  90. struct omap_dss_device *dssdev = omap_encoder->dssdev;
  91. struct omap_dss_driver *dssdrv = dssdev->driver;
  92. int ret;
  93. dssdev->src->manager = omap_dss_get_overlay_manager(channel);
  94. if (dssdrv->check_timings) {
  95. ret = dssdrv->check_timings(dssdev, timings);
  96. } else {
  97. struct omap_video_timings t = {0};
  98. dssdrv->get_timings(dssdev, &t);
  99. if (memcmp(timings, &t, sizeof(struct omap_video_timings)))
  100. ret = -EINVAL;
  101. else
  102. ret = 0;
  103. }
  104. if (ret) {
  105. dev_err(dev->dev, "could not set timings: %d\n", ret);
  106. return ret;
  107. }
  108. if (dssdrv->set_timings)
  109. dssdrv->set_timings(dssdev, timings);
  110. return 0;
  111. }
  112. static void omap_encoder_enable(struct drm_encoder *encoder)
  113. {
  114. struct omap_encoder *omap_encoder = to_omap_encoder(encoder);
  115. struct omap_dss_device *dssdev = omap_encoder->dssdev;
  116. struct omap_dss_driver *dssdrv = dssdev->driver;
  117. omap_encoder_update(encoder, omap_crtc_channel(encoder->crtc),
  118. omap_crtc_timings(encoder->crtc));
  119. dssdrv->enable(dssdev);
  120. }
  121. static int omap_encoder_atomic_check(struct drm_encoder *encoder,
  122. struct drm_crtc_state *crtc_state,
  123. struct drm_connector_state *conn_state)
  124. {
  125. return 0;
  126. }
  127. static const struct drm_encoder_helper_funcs omap_encoder_helper_funcs = {
  128. .mode_set = omap_encoder_mode_set,
  129. .disable = omap_encoder_disable,
  130. .enable = omap_encoder_enable,
  131. .atomic_check = omap_encoder_atomic_check,
  132. };
  133. /* initialize encoder */
  134. struct drm_encoder *omap_encoder_init(struct drm_device *dev,
  135. struct omap_dss_device *dssdev)
  136. {
  137. struct drm_encoder *encoder = NULL;
  138. struct omap_encoder *omap_encoder;
  139. omap_encoder = kzalloc(sizeof(*omap_encoder), GFP_KERNEL);
  140. if (!omap_encoder)
  141. goto fail;
  142. omap_encoder->dssdev = dssdev;
  143. encoder = &omap_encoder->base;
  144. drm_encoder_init(dev, encoder, &omap_encoder_funcs,
  145. DRM_MODE_ENCODER_TMDS);
  146. drm_encoder_helper_add(encoder, &omap_encoder_helper_funcs);
  147. return encoder;
  148. fail:
  149. if (encoder)
  150. omap_encoder_destroy(encoder);
  151. return NULL;
  152. }