hdmi_bridge.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. };
  22. #define to_hdmi_bridge(x) container_of(x, struct hdmi_bridge, base)
  23. void msm_hdmi_bridge_destroy(struct drm_bridge *bridge)
  24. {
  25. }
  26. static void msm_hdmi_power_on(struct drm_bridge *bridge)
  27. {
  28. struct drm_device *dev = bridge->dev;
  29. struct hdmi_bridge *hdmi_bridge = to_hdmi_bridge(bridge);
  30. struct hdmi *hdmi = hdmi_bridge->hdmi;
  31. const struct hdmi_platform_config *config = hdmi->config;
  32. int i, ret;
  33. for (i = 0; i < config->pwr_reg_cnt; i++) {
  34. ret = regulator_enable(hdmi->pwr_regs[i]);
  35. if (ret) {
  36. dev_err(dev->dev, "failed to enable pwr regulator: %s (%d)\n",
  37. config->pwr_reg_names[i], ret);
  38. }
  39. }
  40. if (config->pwr_clk_cnt > 0) {
  41. DBG("pixclock: %lu", hdmi->pixclock);
  42. ret = clk_set_rate(hdmi->pwr_clks[0], hdmi->pixclock);
  43. if (ret) {
  44. dev_err(dev->dev, "failed to set pixel clk: %s (%d)\n",
  45. config->pwr_clk_names[0], ret);
  46. }
  47. }
  48. for (i = 0; i < config->pwr_clk_cnt; i++) {
  49. ret = clk_prepare_enable(hdmi->pwr_clks[i]);
  50. if (ret) {
  51. dev_err(dev->dev, "failed to enable pwr clk: %s (%d)\n",
  52. config->pwr_clk_names[i], ret);
  53. }
  54. }
  55. }
  56. static void power_off(struct drm_bridge *bridge)
  57. {
  58. struct drm_device *dev = bridge->dev;
  59. struct hdmi_bridge *hdmi_bridge = to_hdmi_bridge(bridge);
  60. struct hdmi *hdmi = hdmi_bridge->hdmi;
  61. const struct hdmi_platform_config *config = hdmi->config;
  62. int i, ret;
  63. /* TODO do we need to wait for final vblank somewhere before
  64. * cutting the clocks?
  65. */
  66. mdelay(16 + 4);
  67. for (i = 0; i < config->pwr_clk_cnt; i++)
  68. clk_disable_unprepare(hdmi->pwr_clks[i]);
  69. for (i = 0; i < config->pwr_reg_cnt; i++) {
  70. ret = regulator_disable(hdmi->pwr_regs[i]);
  71. if (ret) {
  72. dev_err(dev->dev, "failed to disable pwr regulator: %s (%d)\n",
  73. config->pwr_reg_names[i], ret);
  74. }
  75. }
  76. }
  77. #define AVI_IFRAME_LINE_NUMBER 1
  78. static void msm_hdmi_config_avi_infoframe(struct hdmi *hdmi)
  79. {
  80. struct drm_crtc *crtc = hdmi->encoder->crtc;
  81. const struct drm_display_mode *mode = &crtc->state->adjusted_mode;
  82. union hdmi_infoframe frame;
  83. u8 buffer[HDMI_INFOFRAME_SIZE(AVI)];
  84. u32 val;
  85. int len;
  86. drm_hdmi_avi_infoframe_from_display_mode(&frame.avi, mode, false);
  87. len = hdmi_infoframe_pack(&frame, buffer, sizeof(buffer));
  88. if (len < 0) {
  89. dev_err(&hdmi->pdev->dev,
  90. "failed to configure avi infoframe\n");
  91. return;
  92. }
  93. /*
  94. * the AVI_INFOx registers don't map exactly to how the AVI infoframes
  95. * are packed according to the spec. The checksum from the header is
  96. * written to the LSB byte of AVI_INFO0 and the version is written to
  97. * the third byte from the LSB of AVI_INFO3
  98. */
  99. hdmi_write(hdmi, REG_HDMI_AVI_INFO(0),
  100. buffer[3] |
  101. buffer[4] << 8 |
  102. buffer[5] << 16 |
  103. buffer[6] << 24);
  104. hdmi_write(hdmi, REG_HDMI_AVI_INFO(1),
  105. buffer[7] |
  106. buffer[8] << 8 |
  107. buffer[9] << 16 |
  108. buffer[10] << 24);
  109. hdmi_write(hdmi, REG_HDMI_AVI_INFO(2),
  110. buffer[11] |
  111. buffer[12] << 8 |
  112. buffer[13] << 16 |
  113. buffer[14] << 24);
  114. hdmi_write(hdmi, REG_HDMI_AVI_INFO(3),
  115. buffer[15] |
  116. buffer[16] << 8 |
  117. buffer[1] << 24);
  118. hdmi_write(hdmi, REG_HDMI_INFOFRAME_CTRL0,
  119. HDMI_INFOFRAME_CTRL0_AVI_SEND |
  120. HDMI_INFOFRAME_CTRL0_AVI_CONT);
  121. val = hdmi_read(hdmi, REG_HDMI_INFOFRAME_CTRL1);
  122. val &= ~HDMI_INFOFRAME_CTRL1_AVI_INFO_LINE__MASK;
  123. val |= HDMI_INFOFRAME_CTRL1_AVI_INFO_LINE(AVI_IFRAME_LINE_NUMBER);
  124. hdmi_write(hdmi, REG_HDMI_INFOFRAME_CTRL1, val);
  125. }
  126. static void msm_hdmi_bridge_pre_enable(struct drm_bridge *bridge)
  127. {
  128. struct hdmi_bridge *hdmi_bridge = to_hdmi_bridge(bridge);
  129. struct hdmi *hdmi = hdmi_bridge->hdmi;
  130. struct hdmi_phy *phy = hdmi->phy;
  131. DBG("power up");
  132. if (!hdmi->power_on) {
  133. msm_hdmi_phy_resource_enable(phy);
  134. msm_hdmi_power_on(bridge);
  135. hdmi->power_on = true;
  136. if (hdmi->hdmi_mode) {
  137. msm_hdmi_config_avi_infoframe(hdmi);
  138. msm_hdmi_audio_update(hdmi);
  139. }
  140. }
  141. msm_hdmi_phy_powerup(phy, hdmi->pixclock);
  142. msm_hdmi_set_mode(hdmi, true);
  143. if (hdmi->hdcp_ctrl)
  144. msm_hdmi_hdcp_on(hdmi->hdcp_ctrl);
  145. }
  146. static void msm_hdmi_bridge_enable(struct drm_bridge *bridge)
  147. {
  148. }
  149. static void msm_hdmi_bridge_disable(struct drm_bridge *bridge)
  150. {
  151. }
  152. static void msm_hdmi_bridge_post_disable(struct drm_bridge *bridge)
  153. {
  154. struct hdmi_bridge *hdmi_bridge = to_hdmi_bridge(bridge);
  155. struct hdmi *hdmi = hdmi_bridge->hdmi;
  156. struct hdmi_phy *phy = hdmi->phy;
  157. if (hdmi->hdcp_ctrl)
  158. msm_hdmi_hdcp_off(hdmi->hdcp_ctrl);
  159. DBG("power down");
  160. msm_hdmi_set_mode(hdmi, false);
  161. msm_hdmi_phy_powerdown(phy);
  162. if (hdmi->power_on) {
  163. power_off(bridge);
  164. hdmi->power_on = false;
  165. if (hdmi->hdmi_mode)
  166. msm_hdmi_audio_update(hdmi);
  167. msm_hdmi_phy_resource_disable(phy);
  168. }
  169. }
  170. static void msm_hdmi_bridge_mode_set(struct drm_bridge *bridge,
  171. struct drm_display_mode *mode,
  172. struct drm_display_mode *adjusted_mode)
  173. {
  174. struct hdmi_bridge *hdmi_bridge = to_hdmi_bridge(bridge);
  175. struct hdmi *hdmi = hdmi_bridge->hdmi;
  176. int hstart, hend, vstart, vend;
  177. uint32_t frame_ctrl;
  178. mode = adjusted_mode;
  179. hdmi->pixclock = mode->clock * 1000;
  180. hstart = mode->htotal - mode->hsync_start;
  181. hend = mode->htotal - mode->hsync_start + mode->hdisplay;
  182. vstart = mode->vtotal - mode->vsync_start - 1;
  183. vend = mode->vtotal - mode->vsync_start + mode->vdisplay - 1;
  184. DBG("htotal=%d, vtotal=%d, hstart=%d, hend=%d, vstart=%d, vend=%d",
  185. mode->htotal, mode->vtotal, hstart, hend, vstart, vend);
  186. hdmi_write(hdmi, REG_HDMI_TOTAL,
  187. HDMI_TOTAL_H_TOTAL(mode->htotal - 1) |
  188. HDMI_TOTAL_V_TOTAL(mode->vtotal - 1));
  189. hdmi_write(hdmi, REG_HDMI_ACTIVE_HSYNC,
  190. HDMI_ACTIVE_HSYNC_START(hstart) |
  191. HDMI_ACTIVE_HSYNC_END(hend));
  192. hdmi_write(hdmi, REG_HDMI_ACTIVE_VSYNC,
  193. HDMI_ACTIVE_VSYNC_START(vstart) |
  194. HDMI_ACTIVE_VSYNC_END(vend));
  195. if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
  196. hdmi_write(hdmi, REG_HDMI_VSYNC_TOTAL_F2,
  197. HDMI_VSYNC_TOTAL_F2_V_TOTAL(mode->vtotal));
  198. hdmi_write(hdmi, REG_HDMI_VSYNC_ACTIVE_F2,
  199. HDMI_VSYNC_ACTIVE_F2_START(vstart + 1) |
  200. HDMI_VSYNC_ACTIVE_F2_END(vend + 1));
  201. } else {
  202. hdmi_write(hdmi, REG_HDMI_VSYNC_TOTAL_F2,
  203. HDMI_VSYNC_TOTAL_F2_V_TOTAL(0));
  204. hdmi_write(hdmi, REG_HDMI_VSYNC_ACTIVE_F2,
  205. HDMI_VSYNC_ACTIVE_F2_START(0) |
  206. HDMI_VSYNC_ACTIVE_F2_END(0));
  207. }
  208. frame_ctrl = 0;
  209. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  210. frame_ctrl |= HDMI_FRAME_CTRL_HSYNC_LOW;
  211. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  212. frame_ctrl |= HDMI_FRAME_CTRL_VSYNC_LOW;
  213. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  214. frame_ctrl |= HDMI_FRAME_CTRL_INTERLACED_EN;
  215. DBG("frame_ctrl=%08x", frame_ctrl);
  216. hdmi_write(hdmi, REG_HDMI_FRAME_CTRL, frame_ctrl);
  217. if (hdmi->hdmi_mode)
  218. msm_hdmi_audio_update(hdmi);
  219. }
  220. static const struct drm_bridge_funcs msm_hdmi_bridge_funcs = {
  221. .pre_enable = msm_hdmi_bridge_pre_enable,
  222. .enable = msm_hdmi_bridge_enable,
  223. .disable = msm_hdmi_bridge_disable,
  224. .post_disable = msm_hdmi_bridge_post_disable,
  225. .mode_set = msm_hdmi_bridge_mode_set,
  226. };
  227. /* initialize bridge */
  228. struct drm_bridge *msm_hdmi_bridge_init(struct hdmi *hdmi)
  229. {
  230. struct drm_bridge *bridge = NULL;
  231. struct hdmi_bridge *hdmi_bridge;
  232. int ret;
  233. hdmi_bridge = devm_kzalloc(hdmi->dev->dev,
  234. sizeof(*hdmi_bridge), GFP_KERNEL);
  235. if (!hdmi_bridge) {
  236. ret = -ENOMEM;
  237. goto fail;
  238. }
  239. hdmi_bridge->hdmi = hdmi;
  240. bridge = &hdmi_bridge->base;
  241. bridge->funcs = &msm_hdmi_bridge_funcs;
  242. ret = drm_bridge_attach(hdmi->encoder, bridge, NULL);
  243. if (ret)
  244. goto fail;
  245. return bridge;
  246. fail:
  247. if (bridge)
  248. msm_hdmi_bridge_destroy(bridge);
  249. return ERR_PTR(ret);
  250. }