hdmi_bridge.c 8.1 KB

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