nouveau_hdmi.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Copyright 2011 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs
  23. */
  24. #include "drmP.h"
  25. #include "nouveau_drv.h"
  26. #include "nouveau_connector.h"
  27. #include "nouveau_encoder.h"
  28. #include "nouveau_crtc.h"
  29. static bool
  30. hdmi_sor(struct drm_encoder *encoder)
  31. {
  32. struct drm_nouveau_private *dev_priv = encoder->dev->dev_private;
  33. if (dev_priv->chipset < 0xa3)
  34. return false;
  35. return true;
  36. }
  37. static inline u32
  38. hdmi_base(struct drm_encoder *encoder)
  39. {
  40. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  41. struct nouveau_crtc *nv_crtc = nouveau_crtc(nv_encoder->crtc);
  42. if (!hdmi_sor(encoder))
  43. return 0x616500 + (nv_crtc->index * 0x800);
  44. return 0x61c500 + (nv_encoder->or * 0x800);
  45. }
  46. static void
  47. hdmi_wr32(struct drm_encoder *encoder, u32 reg, u32 val)
  48. {
  49. nv_wr32(encoder->dev, hdmi_base(encoder) + reg, val);
  50. }
  51. static u32
  52. hdmi_rd32(struct drm_encoder *encoder, u32 reg)
  53. {
  54. return nv_rd32(encoder->dev, hdmi_base(encoder) + reg);
  55. }
  56. static u32
  57. hdmi_mask(struct drm_encoder *encoder, u32 reg, u32 mask, u32 val)
  58. {
  59. u32 tmp = hdmi_rd32(encoder, reg);
  60. hdmi_wr32(encoder, reg, (tmp & ~mask) | val);
  61. return tmp;
  62. }
  63. static void
  64. nouveau_audio_disconnect(struct drm_encoder *encoder)
  65. {
  66. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  67. struct drm_device *dev = encoder->dev;
  68. u32 or = nv_encoder->or * 0x800;
  69. if (hdmi_sor(encoder)) {
  70. nv_mask(dev, 0x61c448 + or, 0x00000003, 0x00000000);
  71. }
  72. }
  73. static void
  74. nouveau_audio_mode_set(struct drm_encoder *encoder,
  75. struct drm_display_mode *mode)
  76. {
  77. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  78. struct nouveau_connector *nv_connector;
  79. struct drm_device *dev = encoder->dev;
  80. u32 or = nv_encoder->or * 0x800;
  81. int i;
  82. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  83. if (!drm_detect_monitor_audio(nv_connector->edid)) {
  84. nouveau_audio_disconnect(encoder);
  85. return;
  86. }
  87. if (hdmi_sor(encoder)) {
  88. nv_mask(dev, 0x61c448 + or, 0x00000001, 0x00000001);
  89. drm_edid_to_eld(&nv_connector->base, nv_connector->edid);
  90. if (nv_connector->base.eld[0]) {
  91. u8 *eld = nv_connector->base.eld;
  92. for (i = 0; i < eld[2] * 4; i++)
  93. nv_wr32(dev, 0x61c440 + or, (i << 8) | eld[i]);
  94. for (i = eld[2] * 4; i < 0x60; i++)
  95. nv_wr32(dev, 0x61c440 + or, (i << 8) | 0x00);
  96. nv_mask(dev, 0x61c448 + or, 0x00000002, 0x00000002);
  97. }
  98. }
  99. }
  100. static void
  101. nouveau_hdmi_infoframe(struct drm_encoder *encoder, u32 ctrl, u8 *frame)
  102. {
  103. /* calculate checksum for the infoframe */
  104. u8 sum = 0, i;
  105. for (i = 0; i < frame[2]; i++)
  106. sum += frame[i];
  107. frame[3] = 256 - sum;
  108. /* disable infoframe, and write header */
  109. hdmi_mask(encoder, ctrl + 0x00, 0x00000001, 0x00000000);
  110. hdmi_wr32(encoder, ctrl + 0x08, *(u32 *)frame & 0xffffff);
  111. /* register scans tell me the audio infoframe has only one set of
  112. * subpack regs, according to tegra (gee nvidia, it'd be nice if we
  113. * could get those docs too!), the hdmi block pads out the rest of
  114. * the packet on its own.
  115. */
  116. if (ctrl == 0x020)
  117. frame[2] = 6;
  118. /* write out checksum and data, weird weird 7 byte register pairs */
  119. for (i = 0; i < frame[2] + 1; i += 7) {
  120. u32 rsubpack = ctrl + 0x0c + ((i / 7) * 8);
  121. u32 *subpack = (u32 *)&frame[3 + i];
  122. hdmi_wr32(encoder, rsubpack + 0, subpack[0]);
  123. hdmi_wr32(encoder, rsubpack + 4, subpack[1] & 0xffffff);
  124. }
  125. /* enable the infoframe */
  126. hdmi_mask(encoder, ctrl, 0x00000001, 0x00000001);
  127. }
  128. static void
  129. nouveau_hdmi_video_infoframe(struct drm_encoder *encoder,
  130. struct drm_display_mode *mode)
  131. {
  132. const u8 Y = 0, A = 0, B = 0, S = 0, C = 0, M = 0, R = 0;
  133. const u8 ITC = 0, EC = 0, Q = 0, SC = 0, VIC = 0, PR = 0;
  134. const u8 bar_top = 0, bar_bottom = 0, bar_left = 0, bar_right = 0;
  135. u8 frame[20];
  136. frame[0x00] = 0x82; /* AVI infoframe */
  137. frame[0x01] = 0x02; /* version */
  138. frame[0x02] = 0x0d; /* length */
  139. frame[0x03] = 0x00;
  140. frame[0x04] = (Y << 5) | (A << 4) | (B << 2) | S;
  141. frame[0x05] = (C << 6) | (M << 4) | R;
  142. frame[0x06] = (ITC << 7) | (EC << 4) | (Q << 2) | SC;
  143. frame[0x07] = VIC;
  144. frame[0x08] = PR;
  145. frame[0x09] = bar_top & 0xff;
  146. frame[0x0a] = bar_top >> 8;
  147. frame[0x0b] = bar_bottom & 0xff;
  148. frame[0x0c] = bar_bottom >> 8;
  149. frame[0x0d] = bar_left & 0xff;
  150. frame[0x0e] = bar_left >> 8;
  151. frame[0x0f] = bar_right & 0xff;
  152. frame[0x10] = bar_right >> 8;
  153. frame[0x11] = 0x00;
  154. frame[0x12] = 0x00;
  155. frame[0x13] = 0x00;
  156. nouveau_hdmi_infoframe(encoder, 0x020, frame);
  157. }
  158. static void
  159. nouveau_hdmi_audio_infoframe(struct drm_encoder *encoder,
  160. struct drm_display_mode *mode)
  161. {
  162. const u8 CT = 0x00, CC = 0x01, ceaSS = 0x00, SF = 0x00, FMT = 0x00;
  163. const u8 CA = 0x00, DM_INH = 0, LSV = 0x00;
  164. u8 frame[12];
  165. frame[0x00] = 0x84; /* Audio infoframe */
  166. frame[0x01] = 0x01; /* version */
  167. frame[0x02] = 0x0a; /* length */
  168. frame[0x03] = 0x00;
  169. frame[0x04] = (CT << 4) | CC;
  170. frame[0x05] = (SF << 2) | ceaSS;
  171. frame[0x06] = FMT;
  172. frame[0x07] = CA;
  173. frame[0x08] = (DM_INH << 7) | (LSV << 3);
  174. frame[0x09] = 0x00;
  175. frame[0x0a] = 0x00;
  176. frame[0x0b] = 0x00;
  177. nouveau_hdmi_infoframe(encoder, 0x000, frame);
  178. }
  179. static void
  180. nouveau_hdmi_disconnect(struct drm_encoder *encoder)
  181. {
  182. nouveau_audio_disconnect(encoder);
  183. /* disable audio and avi infoframes */
  184. hdmi_mask(encoder, 0x000, 0x00000001, 0x00000000);
  185. hdmi_mask(encoder, 0x020, 0x00000001, 0x00000000);
  186. /* disable hdmi */
  187. hdmi_mask(encoder, 0x0a4, 0x40000000, 0x00000000);
  188. }
  189. void
  190. nouveau_hdmi_mode_set(struct drm_encoder *encoder,
  191. struct drm_display_mode *mode)
  192. {
  193. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  194. struct nouveau_connector *nv_connector;
  195. struct drm_device *dev = encoder->dev;
  196. u32 max_ac_packet, rekey;
  197. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  198. if (!mode || !nv_connector || !nv_connector->edid ||
  199. !drm_detect_hdmi_monitor(nv_connector->edid)) {
  200. nouveau_hdmi_disconnect(encoder);
  201. return;
  202. }
  203. nouveau_hdmi_video_infoframe(encoder, mode);
  204. nouveau_hdmi_audio_infoframe(encoder, mode);
  205. hdmi_mask(encoder, 0x0d0, 0x00070001, 0x00010001); /* SPARE, HW_CTS */
  206. hdmi_mask(encoder, 0x068, 0x00010101, 0x00000000); /* ACR_CTRL, ?? */
  207. hdmi_mask(encoder, 0x078, 0x80000000, 0x80000000); /* ACR_0441_ENABLE */
  208. nv_mask(dev, 0x61733c, 0x00100000, 0x00100000); /* RESETF */
  209. nv_mask(dev, 0x61733c, 0x10000000, 0x10000000); /* LOOKUP_EN */
  210. nv_mask(dev, 0x61733c, 0x00100000, 0x00000000); /* !RESETF */
  211. /* value matches nvidia binary driver, and tegra constant */
  212. rekey = 56;
  213. max_ac_packet = mode->htotal - mode->hdisplay;
  214. max_ac_packet -= rekey;
  215. max_ac_packet -= 18; /* constant from tegra */
  216. max_ac_packet /= 32;
  217. /* enable hdmi */
  218. hdmi_mask(encoder, 0x0a4, 0x5f1f003f, 0x40000000 | /* enable */
  219. 0x1f000000 | /* unknown */
  220. max_ac_packet << 16 |
  221. rekey);
  222. nouveau_audio_mode_set(encoder, mode);
  223. }