evergreen_hdmi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * Copyright 2008 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. * Copyright 2009 Christian König.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors: Christian König
  25. * Rafał Miłecki
  26. */
  27. #include <linux/hdmi.h>
  28. #include <drm/drmP.h>
  29. #include <drm/radeon_drm.h>
  30. #include "radeon.h"
  31. #include "radeon_asic.h"
  32. #include "evergreend.h"
  33. #include "atom.h"
  34. /*
  35. * update the N and CTS parameters for a given pixel clock rate
  36. */
  37. static void evergreen_hdmi_update_ACR(struct drm_encoder *encoder, uint32_t clock)
  38. {
  39. struct drm_device *dev = encoder->dev;
  40. struct radeon_device *rdev = dev->dev_private;
  41. struct radeon_hdmi_acr acr = r600_hdmi_acr(clock);
  42. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  43. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  44. uint32_t offset = dig->afmt->offset;
  45. WREG32(HDMI_ACR_32_0 + offset, HDMI_ACR_CTS_32(acr.cts_32khz));
  46. WREG32(HDMI_ACR_32_1 + offset, acr.n_32khz);
  47. WREG32(HDMI_ACR_44_0 + offset, HDMI_ACR_CTS_44(acr.cts_44_1khz));
  48. WREG32(HDMI_ACR_44_1 + offset, acr.n_44_1khz);
  49. WREG32(HDMI_ACR_48_0 + offset, HDMI_ACR_CTS_48(acr.cts_48khz));
  50. WREG32(HDMI_ACR_48_1 + offset, acr.n_48khz);
  51. }
  52. static void evergreen_hdmi_write_sad_regs(struct drm_encoder *encoder)
  53. {
  54. struct radeon_device *rdev = encoder->dev->dev_private;
  55. struct drm_connector *connector;
  56. struct radeon_connector *radeon_connector = NULL;
  57. struct cea_sad *sads;
  58. int i, sad_count;
  59. static const u16 eld_reg_to_type[][2] = {
  60. { AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR0, HDMI_AUDIO_CODING_TYPE_PCM },
  61. { AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR1, HDMI_AUDIO_CODING_TYPE_AC3 },
  62. { AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR2, HDMI_AUDIO_CODING_TYPE_MPEG1 },
  63. { AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR3, HDMI_AUDIO_CODING_TYPE_MP3 },
  64. { AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR4, HDMI_AUDIO_CODING_TYPE_MPEG2 },
  65. { AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR5, HDMI_AUDIO_CODING_TYPE_AAC_LC },
  66. { AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR6, HDMI_AUDIO_CODING_TYPE_DTS },
  67. { AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR7, HDMI_AUDIO_CODING_TYPE_ATRAC },
  68. { AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR9, HDMI_AUDIO_CODING_TYPE_EAC3 },
  69. { AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR10, HDMI_AUDIO_CODING_TYPE_DTS_HD },
  70. { AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR11, HDMI_AUDIO_CODING_TYPE_MLP },
  71. { AZ_F0_CODEC_PIN0_CONTROL_AUDIO_DESCRIPTOR13, HDMI_AUDIO_CODING_TYPE_WMA_PRO },
  72. };
  73. list_for_each_entry(connector, &encoder->dev->mode_config.connector_list, head) {
  74. if (connector->encoder == encoder)
  75. radeon_connector = to_radeon_connector(connector);
  76. }
  77. if (!radeon_connector) {
  78. DRM_ERROR("Couldn't find encoder's connector\n");
  79. return;
  80. }
  81. sad_count = drm_edid_to_sad(radeon_connector->edid, &sads);
  82. if (sad_count < 0) {
  83. DRM_ERROR("Couldn't read SADs: %d\n", sad_count);
  84. return;
  85. }
  86. BUG_ON(!sads);
  87. for (i = 0; i < ARRAY_SIZE(eld_reg_to_type); i++) {
  88. u32 value = 0;
  89. int j;
  90. for (j = 0; j < sad_count; j++) {
  91. struct cea_sad *sad = &sads[j];
  92. if (sad->format == eld_reg_to_type[i][1]) {
  93. value = MAX_CHANNELS(sad->channels) |
  94. DESCRIPTOR_BYTE_2(sad->byte2) |
  95. SUPPORTED_FREQUENCIES(sad->freq);
  96. if (sad->format == HDMI_AUDIO_CODING_TYPE_PCM)
  97. value |= SUPPORTED_FREQUENCIES_STEREO(sad->freq);
  98. break;
  99. }
  100. }
  101. WREG32(eld_reg_to_type[i][0], value);
  102. }
  103. kfree(sads);
  104. }
  105. /*
  106. * build a HDMI Video Info Frame
  107. */
  108. static void evergreen_hdmi_update_avi_infoframe(struct drm_encoder *encoder,
  109. void *buffer, size_t size)
  110. {
  111. struct drm_device *dev = encoder->dev;
  112. struct radeon_device *rdev = dev->dev_private;
  113. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  114. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  115. uint32_t offset = dig->afmt->offset;
  116. uint8_t *frame = buffer + 3;
  117. uint8_t *header = buffer;
  118. WREG32(AFMT_AVI_INFO0 + offset,
  119. frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
  120. WREG32(AFMT_AVI_INFO1 + offset,
  121. frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x7] << 24));
  122. WREG32(AFMT_AVI_INFO2 + offset,
  123. frame[0x8] | (frame[0x9] << 8) | (frame[0xA] << 16) | (frame[0xB] << 24));
  124. WREG32(AFMT_AVI_INFO3 + offset,
  125. frame[0xC] | (frame[0xD] << 8) | (header[1] << 24));
  126. }
  127. static void evergreen_audio_set_dto(struct drm_encoder *encoder, u32 clock)
  128. {
  129. struct drm_device *dev = encoder->dev;
  130. struct radeon_device *rdev = dev->dev_private;
  131. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  132. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  133. struct radeon_crtc *radeon_crtc = to_radeon_crtc(encoder->crtc);
  134. u32 base_rate = 24000;
  135. u32 max_ratio = clock / base_rate;
  136. u32 dto_phase;
  137. u32 dto_modulo = clock;
  138. u32 wallclock_ratio;
  139. u32 dto_cntl;
  140. if (!dig || !dig->afmt)
  141. return;
  142. if (max_ratio >= 8) {
  143. dto_phase = 192 * 1000;
  144. wallclock_ratio = 3;
  145. } else if (max_ratio >= 4) {
  146. dto_phase = 96 * 1000;
  147. wallclock_ratio = 2;
  148. } else if (max_ratio >= 2) {
  149. dto_phase = 48 * 1000;
  150. wallclock_ratio = 1;
  151. } else {
  152. dto_phase = 24 * 1000;
  153. wallclock_ratio = 0;
  154. }
  155. dto_cntl = RREG32(DCCG_AUDIO_DTO0_CNTL) & ~DCCG_AUDIO_DTO_WALLCLOCK_RATIO_MASK;
  156. dto_cntl |= DCCG_AUDIO_DTO_WALLCLOCK_RATIO(wallclock_ratio);
  157. WREG32(DCCG_AUDIO_DTO0_CNTL, dto_cntl);
  158. /* XXX two dtos; generally use dto0 for hdmi */
  159. /* Express [24MHz / target pixel clock] as an exact rational
  160. * number (coefficient of two integer numbers. DCCG_AUDIO_DTOx_PHASE
  161. * is the numerator, DCCG_AUDIO_DTOx_MODULE is the denominator
  162. */
  163. WREG32(DCCG_AUDIO_DTO_SOURCE, DCCG_AUDIO_DTO0_SOURCE_SEL(radeon_crtc->crtc_id));
  164. WREG32(DCCG_AUDIO_DTO0_PHASE, dto_phase);
  165. WREG32(DCCG_AUDIO_DTO0_MODULE, dto_modulo);
  166. }
  167. /*
  168. * update the info frames with the data from the current display mode
  169. */
  170. void evergreen_hdmi_setmode(struct drm_encoder *encoder, struct drm_display_mode *mode)
  171. {
  172. struct drm_device *dev = encoder->dev;
  173. struct radeon_device *rdev = dev->dev_private;
  174. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  175. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  176. u8 buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AVI_INFOFRAME_SIZE];
  177. struct hdmi_avi_infoframe frame;
  178. uint32_t offset;
  179. ssize_t err;
  180. if (!dig || !dig->afmt)
  181. return;
  182. /* Silent, r600_hdmi_enable will raise WARN for us */
  183. if (!dig->afmt->enabled)
  184. return;
  185. offset = dig->afmt->offset;
  186. evergreen_audio_set_dto(encoder, mode->clock);
  187. WREG32(HDMI_VBI_PACKET_CONTROL + offset,
  188. HDMI_NULL_SEND); /* send null packets when required */
  189. WREG32(AFMT_AUDIO_CRC_CONTROL + offset, 0x1000);
  190. WREG32(HDMI_VBI_PACKET_CONTROL + offset,
  191. HDMI_NULL_SEND | /* send null packets when required */
  192. HDMI_GC_SEND | /* send general control packets */
  193. HDMI_GC_CONT); /* send general control packets every frame */
  194. WREG32(HDMI_INFOFRAME_CONTROL0 + offset,
  195. HDMI_AUDIO_INFO_SEND | /* enable audio info frames (frames won't be set until audio is enabled) */
  196. HDMI_AUDIO_INFO_CONT); /* required for audio info values to be updated */
  197. WREG32(AFMT_INFOFRAME_CONTROL0 + offset,
  198. AFMT_AUDIO_INFO_UPDATE); /* required for audio info values to be updated */
  199. WREG32(HDMI_INFOFRAME_CONTROL1 + offset,
  200. HDMI_AUDIO_INFO_LINE(2)); /* anything other than 0 */
  201. WREG32(HDMI_GC + offset, 0); /* unset HDMI_GC_AVMUTE */
  202. WREG32(HDMI_AUDIO_PACKET_CONTROL + offset,
  203. HDMI_AUDIO_DELAY_EN(1) | /* set the default audio delay */
  204. HDMI_AUDIO_PACKETS_PER_LINE(3)); /* should be suffient for all audio modes and small enough for all hblanks */
  205. WREG32(AFMT_AUDIO_PACKET_CONTROL + offset,
  206. AFMT_60958_CS_UPDATE); /* allow 60958 channel status fields to be updated */
  207. /* fglrx clears sth in AFMT_AUDIO_PACKET_CONTROL2 here */
  208. WREG32(HDMI_ACR_PACKET_CONTROL + offset,
  209. HDMI_ACR_AUTO_SEND | /* allow hw to sent ACR packets when required */
  210. HDMI_ACR_SOURCE); /* select SW CTS value */
  211. evergreen_hdmi_update_ACR(encoder, mode->clock);
  212. WREG32(AFMT_60958_0 + offset,
  213. AFMT_60958_CS_CHANNEL_NUMBER_L(1));
  214. WREG32(AFMT_60958_1 + offset,
  215. AFMT_60958_CS_CHANNEL_NUMBER_R(2));
  216. WREG32(AFMT_60958_2 + offset,
  217. AFMT_60958_CS_CHANNEL_NUMBER_2(3) |
  218. AFMT_60958_CS_CHANNEL_NUMBER_3(4) |
  219. AFMT_60958_CS_CHANNEL_NUMBER_4(5) |
  220. AFMT_60958_CS_CHANNEL_NUMBER_5(6) |
  221. AFMT_60958_CS_CHANNEL_NUMBER_6(7) |
  222. AFMT_60958_CS_CHANNEL_NUMBER_7(8));
  223. /* fglrx sets 0x0001005f | (x & 0x00fc0000) in 0x5f78 here */
  224. WREG32(AFMT_AUDIO_PACKET_CONTROL2 + offset,
  225. AFMT_AUDIO_CHANNEL_ENABLE(0xff));
  226. /* fglrx sets 0x40 in 0x5f80 here */
  227. evergreen_hdmi_write_sad_regs(encoder);
  228. err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode);
  229. if (err < 0) {
  230. DRM_ERROR("failed to setup AVI infoframe: %zd\n", err);
  231. return;
  232. }
  233. err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
  234. if (err < 0) {
  235. DRM_ERROR("failed to pack AVI infoframe: %zd\n", err);
  236. return;
  237. }
  238. evergreen_hdmi_update_avi_infoframe(encoder, buffer, sizeof(buffer));
  239. WREG32_OR(HDMI_INFOFRAME_CONTROL0 + offset,
  240. HDMI_AVI_INFO_SEND | /* enable AVI info frames */
  241. HDMI_AVI_INFO_CONT); /* required for audio info values to be updated */
  242. WREG32_P(HDMI_INFOFRAME_CONTROL1 + offset,
  243. HDMI_AVI_INFO_LINE(2), /* anything other than 0 */
  244. ~HDMI_AVI_INFO_LINE_MASK);
  245. WREG32_OR(AFMT_AUDIO_PACKET_CONTROL + offset,
  246. AFMT_AUDIO_SAMPLE_SEND); /* send audio packets */
  247. /* it's unknown what these bits do excatly, but it's indeed quite useful for debugging */
  248. WREG32(AFMT_RAMP_CONTROL0 + offset, 0x00FFFFFF);
  249. WREG32(AFMT_RAMP_CONTROL1 + offset, 0x007FFFFF);
  250. WREG32(AFMT_RAMP_CONTROL2 + offset, 0x00000001);
  251. WREG32(AFMT_RAMP_CONTROL3 + offset, 0x00000001);
  252. }
  253. void evergreen_hdmi_enable(struct drm_encoder *encoder, bool enable)
  254. {
  255. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  256. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  257. if (!dig || !dig->afmt)
  258. return;
  259. /* Silent, r600_hdmi_enable will raise WARN for us */
  260. if (enable && dig->afmt->enabled)
  261. return;
  262. if (!enable && !dig->afmt->enabled)
  263. return;
  264. dig->afmt->enabled = enable;
  265. DRM_DEBUG("%sabling HDMI interface @ 0x%04X for encoder 0x%x\n",
  266. enable ? "En" : "Dis", dig->afmt->offset, radeon_encoder->encoder_id);
  267. }