hdmi_bridge.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (C) 2013 Red Hat
  3. * Author: Rob Clark <robdclark@gmail.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 "hdmi.h"
  18. struct hdmi_bridge {
  19. struct drm_bridge base;
  20. struct hdmi *hdmi;
  21. unsigned long int pixclock;
  22. };
  23. #define to_hdmi_bridge(x) container_of(x, struct hdmi_bridge, base)
  24. static void hdmi_bridge_destroy(struct drm_bridge *bridge)
  25. {
  26. struct hdmi_bridge *hdmi_bridge = to_hdmi_bridge(bridge);
  27. hdmi_unreference(hdmi_bridge->hdmi);
  28. drm_bridge_cleanup(bridge);
  29. kfree(hdmi_bridge);
  30. }
  31. static void hdmi_bridge_pre_enable(struct drm_bridge *bridge)
  32. {
  33. struct hdmi_bridge *hdmi_bridge = to_hdmi_bridge(bridge);
  34. struct hdmi *hdmi = hdmi_bridge->hdmi;
  35. struct hdmi_phy *phy = hdmi->phy;
  36. DBG("power up");
  37. phy->funcs->powerup(phy, hdmi_bridge->pixclock);
  38. hdmi_set_mode(hdmi, true);
  39. }
  40. static void hdmi_bridge_enable(struct drm_bridge *bridge)
  41. {
  42. }
  43. static void hdmi_bridge_disable(struct drm_bridge *bridge)
  44. {
  45. }
  46. static void hdmi_bridge_post_disable(struct drm_bridge *bridge)
  47. {
  48. struct hdmi_bridge *hdmi_bridge = to_hdmi_bridge(bridge);
  49. struct hdmi *hdmi = hdmi_bridge->hdmi;
  50. struct hdmi_phy *phy = hdmi->phy;
  51. DBG("power down");
  52. hdmi_set_mode(hdmi, false);
  53. phy->funcs->powerdown(phy);
  54. }
  55. static void hdmi_bridge_mode_set(struct drm_bridge *bridge,
  56. struct drm_display_mode *mode,
  57. struct drm_display_mode *adjusted_mode)
  58. {
  59. struct hdmi_bridge *hdmi_bridge = to_hdmi_bridge(bridge);
  60. struct hdmi *hdmi = hdmi_bridge->hdmi;
  61. int hstart, hend, vstart, vend;
  62. uint32_t frame_ctrl;
  63. mode = adjusted_mode;
  64. hdmi_bridge->pixclock = mode->clock * 1000;
  65. hdmi->hdmi_mode = drm_match_cea_mode(mode) > 1;
  66. hstart = mode->htotal - mode->hsync_start;
  67. hend = mode->htotal - mode->hsync_start + mode->hdisplay;
  68. vstart = mode->vtotal - mode->vsync_start - 1;
  69. vend = mode->vtotal - mode->vsync_start + mode->vdisplay - 1;
  70. DBG("htotal=%d, vtotal=%d, hstart=%d, hend=%d, vstart=%d, vend=%d",
  71. mode->htotal, mode->vtotal, hstart, hend, vstart, vend);
  72. hdmi_write(hdmi, REG_HDMI_TOTAL,
  73. HDMI_TOTAL_H_TOTAL(mode->htotal - 1) |
  74. HDMI_TOTAL_V_TOTAL(mode->vtotal - 1));
  75. hdmi_write(hdmi, REG_HDMI_ACTIVE_HSYNC,
  76. HDMI_ACTIVE_HSYNC_START(hstart) |
  77. HDMI_ACTIVE_HSYNC_END(hend));
  78. hdmi_write(hdmi, REG_HDMI_ACTIVE_VSYNC,
  79. HDMI_ACTIVE_VSYNC_START(vstart) |
  80. HDMI_ACTIVE_VSYNC_END(vend));
  81. if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
  82. hdmi_write(hdmi, REG_HDMI_VSYNC_TOTAL_F2,
  83. HDMI_VSYNC_TOTAL_F2_V_TOTAL(mode->vtotal));
  84. hdmi_write(hdmi, REG_HDMI_VSYNC_ACTIVE_F2,
  85. HDMI_VSYNC_ACTIVE_F2_START(vstart + 1) |
  86. HDMI_VSYNC_ACTIVE_F2_END(vend + 1));
  87. } else {
  88. hdmi_write(hdmi, REG_HDMI_VSYNC_TOTAL_F2,
  89. HDMI_VSYNC_TOTAL_F2_V_TOTAL(0));
  90. hdmi_write(hdmi, REG_HDMI_VSYNC_ACTIVE_F2,
  91. HDMI_VSYNC_ACTIVE_F2_START(0) |
  92. HDMI_VSYNC_ACTIVE_F2_END(0));
  93. }
  94. frame_ctrl = 0;
  95. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  96. frame_ctrl |= HDMI_FRAME_CTRL_HSYNC_LOW;
  97. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  98. frame_ctrl |= HDMI_FRAME_CTRL_VSYNC_LOW;
  99. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  100. frame_ctrl |= HDMI_FRAME_CTRL_INTERLACED_EN;
  101. DBG("frame_ctrl=%08x", frame_ctrl);
  102. hdmi_write(hdmi, REG_HDMI_FRAME_CTRL, frame_ctrl);
  103. // TODO until we have audio, this might be safest:
  104. if (hdmi->hdmi_mode)
  105. hdmi_write(hdmi, REG_HDMI_GC, HDMI_GC_MUTE);
  106. }
  107. static const struct drm_bridge_funcs hdmi_bridge_funcs = {
  108. .pre_enable = hdmi_bridge_pre_enable,
  109. .enable = hdmi_bridge_enable,
  110. .disable = hdmi_bridge_disable,
  111. .post_disable = hdmi_bridge_post_disable,
  112. .mode_set = hdmi_bridge_mode_set,
  113. .destroy = hdmi_bridge_destroy,
  114. };
  115. /* initialize bridge */
  116. struct drm_bridge *hdmi_bridge_init(struct hdmi *hdmi)
  117. {
  118. struct drm_bridge *bridge = NULL;
  119. struct hdmi_bridge *hdmi_bridge;
  120. int ret;
  121. hdmi_bridge = kzalloc(sizeof(*hdmi_bridge), GFP_KERNEL);
  122. if (!hdmi_bridge) {
  123. ret = -ENOMEM;
  124. goto fail;
  125. }
  126. hdmi_bridge->hdmi = hdmi_reference(hdmi);
  127. bridge = &hdmi_bridge->base;
  128. drm_bridge_init(hdmi->dev, bridge, &hdmi_bridge_funcs);
  129. return bridge;
  130. fail:
  131. if (bridge)
  132. hdmi_bridge_destroy(bridge);
  133. return ERR_PTR(ret);
  134. }