r600_hdmi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. */
  26. #include "drmP.h"
  27. #include "radeon_drm.h"
  28. #include "radeon.h"
  29. #include "atom.h"
  30. /*
  31. * HDMI color format
  32. */
  33. enum r600_hdmi_color_format {
  34. RGB = 0,
  35. YCC_422 = 1,
  36. YCC_444 = 2
  37. };
  38. /*
  39. * IEC60958 status bits
  40. */
  41. enum r600_hdmi_iec_status_bits {
  42. AUDIO_STATUS_DIG_ENABLE = 0x01,
  43. AUDIO_STATUS_V = 0x02,
  44. AUDIO_STATUS_VCFG = 0x04,
  45. AUDIO_STATUS_EMPHASIS = 0x08,
  46. AUDIO_STATUS_COPYRIGHT = 0x10,
  47. AUDIO_STATUS_NONAUDIO = 0x20,
  48. AUDIO_STATUS_PROFESSIONAL = 0x40,
  49. AUDIO_STATUS_LEVEL = 0x80
  50. };
  51. struct {
  52. uint32_t Clock;
  53. int N_32kHz;
  54. int CTS_32kHz;
  55. int N_44_1kHz;
  56. int CTS_44_1kHz;
  57. int N_48kHz;
  58. int CTS_48kHz;
  59. } r600_hdmi_ACR[] = {
  60. /* 32kHz 44.1kHz 48kHz */
  61. /* Clock N CTS N CTS N CTS */
  62. { 25174, 4576, 28125, 7007, 31250, 6864, 28125 }, /* 25,20/1.001 MHz */
  63. { 25200, 4096, 25200, 6272, 28000, 6144, 25200 }, /* 25.20 MHz */
  64. { 27000, 4096, 27000, 6272, 30000, 6144, 27000 }, /* 27.00 MHz */
  65. { 27027, 4096, 27027, 6272, 30030, 6144, 27027 }, /* 27.00*1.001 MHz */
  66. { 54000, 4096, 54000, 6272, 60000, 6144, 54000 }, /* 54.00 MHz */
  67. { 54054, 4096, 54054, 6272, 60060, 6144, 54054 }, /* 54.00*1.001 MHz */
  68. { 74175, 11648, 210937, 17836, 234375, 11648, 140625 }, /* 74.25/1.001 MHz */
  69. { 74250, 4096, 74250, 6272, 82500, 6144, 74250 }, /* 74.25 MHz */
  70. { 148351, 11648, 421875, 8918, 234375, 5824, 140625 }, /* 148.50/1.001 MHz */
  71. { 148500, 4096, 148500, 6272, 165000, 6144, 148500 }, /* 148.50 MHz */
  72. { 0, 4096, 0, 6272, 0, 6144, 0 } /* Other */
  73. };
  74. /*
  75. * calculate CTS value if it's not found in the table
  76. */
  77. static void r600_hdmi_calc_CTS(uint32_t clock, int *CTS, int N, int freq)
  78. {
  79. if (*CTS == 0)
  80. *CTS = clock*N/(128*freq)*1000;
  81. DRM_DEBUG("Using ACR timing N=%d CTS=%d for frequency %d\n",
  82. N, *CTS, freq);
  83. }
  84. /*
  85. * update the N and CTS parameters for a given pixel clock rate
  86. */
  87. static void r600_hdmi_update_ACR(struct drm_encoder *encoder, uint32_t clock)
  88. {
  89. struct drm_device *dev = encoder->dev;
  90. struct radeon_device *rdev = dev->dev_private;
  91. uint32_t offset = to_radeon_encoder(encoder)->hdmi_offset;
  92. int CTS;
  93. int N;
  94. int i;
  95. for (i = 0; r600_hdmi_ACR[i].Clock != clock && r600_hdmi_ACR[i].Clock != 0; i++);
  96. CTS = r600_hdmi_ACR[i].CTS_32kHz;
  97. N = r600_hdmi_ACR[i].N_32kHz;
  98. r600_hdmi_calc_CTS(clock, &CTS, N, 32000);
  99. WREG32(offset+R600_HDMI_32kHz_CTS, CTS << 12);
  100. WREG32(offset+R600_HDMI_32kHz_N, N);
  101. CTS = r600_hdmi_ACR[i].CTS_44_1kHz;
  102. N = r600_hdmi_ACR[i].N_44_1kHz;
  103. r600_hdmi_calc_CTS(clock, &CTS, N, 44100);
  104. WREG32(offset+R600_HDMI_44_1kHz_CTS, CTS << 12);
  105. WREG32(offset+R600_HDMI_44_1kHz_N, N);
  106. CTS = r600_hdmi_ACR[i].CTS_48kHz;
  107. N = r600_hdmi_ACR[i].N_48kHz;
  108. r600_hdmi_calc_CTS(clock, &CTS, N, 48000);
  109. WREG32(offset+R600_HDMI_48kHz_CTS, CTS << 12);
  110. WREG32(offset+R600_HDMI_48kHz_N, N);
  111. }
  112. /*
  113. * calculate the crc for a given info frame
  114. */
  115. static void r600_hdmi_infoframe_checksum(uint8_t packetType,
  116. uint8_t versionNumber,
  117. uint8_t length,
  118. uint8_t *frame)
  119. {
  120. int i;
  121. frame[0] = packetType + versionNumber + length;
  122. for (i = 1; i <= length; i++)
  123. frame[0] += frame[i];
  124. frame[0] = 0x100 - frame[0];
  125. }
  126. /*
  127. * build a HDMI Video Info Frame
  128. */
  129. static void r600_hdmi_videoinfoframe(
  130. struct drm_encoder *encoder,
  131. enum r600_hdmi_color_format color_format,
  132. int active_information_present,
  133. uint8_t active_format_aspect_ratio,
  134. uint8_t scan_information,
  135. uint8_t colorimetry,
  136. uint8_t ex_colorimetry,
  137. uint8_t quantization,
  138. int ITC,
  139. uint8_t picture_aspect_ratio,
  140. uint8_t video_format_identification,
  141. uint8_t pixel_repetition,
  142. uint8_t non_uniform_picture_scaling,
  143. uint8_t bar_info_data_valid,
  144. uint16_t top_bar,
  145. uint16_t bottom_bar,
  146. uint16_t left_bar,
  147. uint16_t right_bar
  148. )
  149. {
  150. struct drm_device *dev = encoder->dev;
  151. struct radeon_device *rdev = dev->dev_private;
  152. uint32_t offset = to_radeon_encoder(encoder)->hdmi_offset;
  153. uint8_t frame[14];
  154. frame[0x0] = 0;
  155. frame[0x1] =
  156. (scan_information & 0x3) |
  157. ((bar_info_data_valid & 0x3) << 2) |
  158. ((active_information_present & 0x1) << 4) |
  159. ((color_format & 0x3) << 5);
  160. frame[0x2] =
  161. (active_format_aspect_ratio & 0xF) |
  162. ((picture_aspect_ratio & 0x3) << 4) |
  163. ((colorimetry & 0x3) << 6);
  164. frame[0x3] =
  165. (non_uniform_picture_scaling & 0x3) |
  166. ((quantization & 0x3) << 2) |
  167. ((ex_colorimetry & 0x7) << 4) |
  168. ((ITC & 0x1) << 7);
  169. frame[0x4] = (video_format_identification & 0x7F);
  170. frame[0x5] = (pixel_repetition & 0xF);
  171. frame[0x6] = (top_bar & 0xFF);
  172. frame[0x7] = (top_bar >> 8);
  173. frame[0x8] = (bottom_bar & 0xFF);
  174. frame[0x9] = (bottom_bar >> 8);
  175. frame[0xA] = (left_bar & 0xFF);
  176. frame[0xB] = (left_bar >> 8);
  177. frame[0xC] = (right_bar & 0xFF);
  178. frame[0xD] = (right_bar >> 8);
  179. r600_hdmi_infoframe_checksum(0x82, 0x02, 0x0D, frame);
  180. WREG32(offset+R600_HDMI_VIDEOINFOFRAME_0,
  181. frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
  182. WREG32(offset+R600_HDMI_VIDEOINFOFRAME_1,
  183. frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x7] << 24));
  184. WREG32(offset+R600_HDMI_VIDEOINFOFRAME_2,
  185. frame[0x8] | (frame[0x9] << 8) | (frame[0xA] << 16) | (frame[0xB] << 24));
  186. WREG32(offset+R600_HDMI_VIDEOINFOFRAME_3,
  187. frame[0xC] | (frame[0xD] << 8));
  188. }
  189. /*
  190. * build a Audio Info Frame
  191. */
  192. static void r600_hdmi_audioinfoframe(
  193. struct drm_encoder *encoder,
  194. uint8_t channel_count,
  195. uint8_t coding_type,
  196. uint8_t sample_size,
  197. uint8_t sample_frequency,
  198. uint8_t format,
  199. uint8_t channel_allocation,
  200. uint8_t level_shift,
  201. int downmix_inhibit
  202. )
  203. {
  204. struct drm_device *dev = encoder->dev;
  205. struct radeon_device *rdev = dev->dev_private;
  206. uint32_t offset = to_radeon_encoder(encoder)->hdmi_offset;
  207. uint8_t frame[11];
  208. frame[0x0] = 0;
  209. frame[0x1] = (channel_count & 0x7) | ((coding_type & 0xF) << 4);
  210. frame[0x2] = (sample_size & 0x3) | ((sample_frequency & 0x7) << 2);
  211. frame[0x3] = format;
  212. frame[0x4] = channel_allocation;
  213. frame[0x5] = ((level_shift & 0xF) << 3) | ((downmix_inhibit & 0x1) << 7);
  214. frame[0x6] = 0;
  215. frame[0x7] = 0;
  216. frame[0x8] = 0;
  217. frame[0x9] = 0;
  218. frame[0xA] = 0;
  219. r600_hdmi_infoframe_checksum(0x84, 0x01, 0x0A, frame);
  220. WREG32(offset+R600_HDMI_AUDIOINFOFRAME_0,
  221. frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
  222. WREG32(offset+R600_HDMI_AUDIOINFOFRAME_1,
  223. frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x8] << 24));
  224. }
  225. /*
  226. * test if audio buffer is filled enough to start playing
  227. */
  228. static int r600_hdmi_is_audio_buffer_filled(struct drm_encoder *encoder)
  229. {
  230. struct drm_device *dev = encoder->dev;
  231. struct radeon_device *rdev = dev->dev_private;
  232. uint32_t offset = to_radeon_encoder(encoder)->hdmi_offset;
  233. return (RREG32(offset+R600_HDMI_STATUS) & 0x10) != 0;
  234. }
  235. /*
  236. * have buffer status changed since last call?
  237. */
  238. int r600_hdmi_buffer_status_changed(struct drm_encoder *encoder)
  239. {
  240. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  241. int status, result;
  242. if (!radeon_encoder->hdmi_offset)
  243. return 0;
  244. status = r600_hdmi_is_audio_buffer_filled(encoder);
  245. result = radeon_encoder->hdmi_buffer_status != status;
  246. radeon_encoder->hdmi_buffer_status = status;
  247. return result;
  248. }
  249. /*
  250. * write the audio workaround status to the hardware
  251. */
  252. void r600_hdmi_audio_workaround(struct drm_encoder *encoder)
  253. {
  254. struct drm_device *dev = encoder->dev;
  255. struct radeon_device *rdev = dev->dev_private;
  256. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  257. uint32_t offset = radeon_encoder->hdmi_offset;
  258. if (!offset)
  259. return;
  260. if (r600_hdmi_is_audio_buffer_filled(encoder)) {
  261. /* disable audio workaround and start delivering of audio frames */
  262. WREG32_P(offset+R600_HDMI_CNTL, 0x00000001, ~0x00001001);
  263. } else if (radeon_encoder->hdmi_audio_workaround) {
  264. /* enable audio workaround and start delivering of audio frames */
  265. WREG32_P(offset+R600_HDMI_CNTL, 0x00001001, ~0x00001001);
  266. } else {
  267. /* disable audio workaround and stop delivering of audio frames */
  268. WREG32_P(offset+R600_HDMI_CNTL, 0x00000000, ~0x00001001);
  269. }
  270. }
  271. /*
  272. * update the info frames with the data from the current display mode
  273. */
  274. void r600_hdmi_setmode(struct drm_encoder *encoder, struct drm_display_mode *mode)
  275. {
  276. struct drm_device *dev = encoder->dev;
  277. struct radeon_device *rdev = dev->dev_private;
  278. uint32_t offset = to_radeon_encoder(encoder)->hdmi_offset;
  279. if (!offset)
  280. return;
  281. r600_audio_set_clock(encoder, mode->clock);
  282. WREG32(offset+R600_HDMI_UNKNOWN_0, 0x1000);
  283. WREG32(offset+R600_HDMI_UNKNOWN_1, 0x0);
  284. WREG32(offset+R600_HDMI_UNKNOWN_2, 0x1000);
  285. r600_hdmi_update_ACR(encoder, mode->clock);
  286. WREG32(offset+R600_HDMI_VIDEOCNTL, 0x13);
  287. WREG32(offset+R600_HDMI_VERSION, 0x202);
  288. r600_hdmi_videoinfoframe(encoder, RGB, 0, 0, 0, 0,
  289. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  290. /* it's unknown what these bits do excatly, but it's indeed quite usefull for debugging */
  291. WREG32(offset+R600_HDMI_AUDIO_DEBUG_0, 0x00FFFFFF);
  292. WREG32(offset+R600_HDMI_AUDIO_DEBUG_1, 0x007FFFFF);
  293. WREG32(offset+R600_HDMI_AUDIO_DEBUG_2, 0x00000001);
  294. WREG32(offset+R600_HDMI_AUDIO_DEBUG_3, 0x00000001);
  295. r600_hdmi_audio_workaround(encoder);
  296. /* audio packets per line, does anyone know how to calc this ? */
  297. WREG32_P(offset+R600_HDMI_CNTL, 0x00040000, ~0x001F0000);
  298. /* update? reset? don't realy know */
  299. WREG32_P(offset+R600_HDMI_CNTL, 0x14000000, ~0x14000000);
  300. }
  301. /*
  302. * update settings with current parameters from audio engine
  303. */
  304. void r600_hdmi_update_audio_settings(struct drm_encoder *encoder,
  305. int channels,
  306. int rate,
  307. int bps,
  308. uint8_t status_bits,
  309. uint8_t category_code)
  310. {
  311. struct drm_device *dev = encoder->dev;
  312. struct radeon_device *rdev = dev->dev_private;
  313. uint32_t offset = to_radeon_encoder(encoder)->hdmi_offset;
  314. uint32_t iec;
  315. if (!offset)
  316. return;
  317. DRM_DEBUG("%s with %d channels, %d Hz sampling rate, %d bits per sample,\n",
  318. r600_hdmi_is_audio_buffer_filled(encoder) ? "playing" : "stopped",
  319. channels, rate, bps);
  320. DRM_DEBUG("0x%02X IEC60958 status bits and 0x%02X category code\n",
  321. (int)status_bits, (int)category_code);
  322. iec = 0;
  323. if (status_bits & AUDIO_STATUS_PROFESSIONAL)
  324. iec |= 1 << 0;
  325. if (status_bits & AUDIO_STATUS_NONAUDIO)
  326. iec |= 1 << 1;
  327. if (status_bits & AUDIO_STATUS_COPYRIGHT)
  328. iec |= 1 << 2;
  329. if (status_bits & AUDIO_STATUS_EMPHASIS)
  330. iec |= 1 << 3;
  331. iec |= category_code << 8;
  332. switch (rate) {
  333. case 32000: iec |= 0x3 << 24; break;
  334. case 44100: iec |= 0x0 << 24; break;
  335. case 88200: iec |= 0x8 << 24; break;
  336. case 176400: iec |= 0xc << 24; break;
  337. case 48000: iec |= 0x2 << 24; break;
  338. case 96000: iec |= 0xa << 24; break;
  339. case 192000: iec |= 0xe << 24; break;
  340. }
  341. WREG32(offset+R600_HDMI_IEC60958_1, iec);
  342. iec = 0;
  343. switch (bps) {
  344. case 16: iec |= 0x2; break;
  345. case 20: iec |= 0x3; break;
  346. case 24: iec |= 0xb; break;
  347. }
  348. if (status_bits & AUDIO_STATUS_V)
  349. iec |= 0x5 << 16;
  350. WREG32_P(offset+R600_HDMI_IEC60958_2, iec, ~0x5000f);
  351. /* 0x021 or 0x031 sets the audio frame length */
  352. WREG32(offset+R600_HDMI_AUDIOCNTL, 0x31);
  353. r600_hdmi_audioinfoframe(encoder, channels-1, 0, 0, 0, 0, 0, 0, 0);
  354. r600_hdmi_audio_workaround(encoder);
  355. /* update? reset? don't realy know */
  356. WREG32_P(offset+R600_HDMI_CNTL, 0x04000000, ~0x04000000);
  357. }
  358. /*
  359. * enable/disable the HDMI engine
  360. */
  361. void r600_hdmi_enable(struct drm_encoder *encoder, int enable)
  362. {
  363. struct drm_device *dev = encoder->dev;
  364. struct radeon_device *rdev = dev->dev_private;
  365. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  366. uint32_t offset = to_radeon_encoder(encoder)->hdmi_offset;
  367. if (!offset)
  368. return;
  369. DRM_DEBUG("%s HDMI interface @ 0x%04X\n", enable ? "Enabling" : "Disabling", offset);
  370. /* some version of atombios ignore the enable HDMI flag
  371. * so enabling/disabling HDMI was moved here for TMDS1+2 */
  372. switch (radeon_encoder->encoder_id) {
  373. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
  374. WREG32_P(AVIVO_TMDSA_CNTL, enable ? 0x4 : 0x0, ~0x4);
  375. WREG32(offset+R600_HDMI_ENABLE, enable ? 0x101 : 0x0);
  376. break;
  377. case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
  378. WREG32_P(AVIVO_LVTMA_CNTL, enable ? 0x4 : 0x0, ~0x4);
  379. WREG32(offset+R600_HDMI_ENABLE, enable ? 0x105 : 0x0);
  380. break;
  381. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
  382. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
  383. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
  384. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_LVTMA:
  385. /* This part is doubtfull in my opinion */
  386. WREG32(offset+R600_HDMI_ENABLE, enable ? 0x110 : 0x0);
  387. break;
  388. default:
  389. DRM_ERROR("unknown HDMI output type\n");
  390. break;
  391. }
  392. }
  393. /*
  394. * determin at which register offset the HDMI encoder is
  395. */
  396. void r600_hdmi_init(struct drm_encoder *encoder)
  397. {
  398. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  399. switch (radeon_encoder->encoder_id) {
  400. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
  401. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
  402. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
  403. radeon_encoder->hdmi_offset = R600_HDMI_TMDS1;
  404. break;
  405. case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
  406. switch (r600_audio_tmds_index(encoder)) {
  407. case 0:
  408. radeon_encoder->hdmi_offset = R600_HDMI_TMDS1;
  409. break;
  410. case 1:
  411. radeon_encoder->hdmi_offset = R600_HDMI_TMDS2;
  412. break;
  413. default:
  414. radeon_encoder->hdmi_offset = 0;
  415. break;
  416. }
  417. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
  418. radeon_encoder->hdmi_offset = R600_HDMI_TMDS2;
  419. break;
  420. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_LVTMA:
  421. radeon_encoder->hdmi_offset = R600_HDMI_DIG;
  422. break;
  423. default:
  424. radeon_encoder->hdmi_offset = 0;
  425. break;
  426. }
  427. DRM_DEBUG("using HDMI engine at offset 0x%04X for encoder 0x%x\n",
  428. radeon_encoder->hdmi_offset, radeon_encoder->encoder_id);
  429. /* TODO: make this configureable */
  430. radeon_encoder->hdmi_audio_workaround = 0;
  431. }