r600_hdmi.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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 <linux/hdmi.h>
  27. #include <linux/gcd.h>
  28. #include <drm/drmP.h>
  29. #include <drm/radeon_drm.h>
  30. #include "radeon.h"
  31. #include "radeon_asic.h"
  32. #include "r600d.h"
  33. #include "atom.h"
  34. /*
  35. * HDMI color format
  36. */
  37. enum r600_hdmi_color_format {
  38. RGB = 0,
  39. YCC_422 = 1,
  40. YCC_444 = 2
  41. };
  42. /*
  43. * IEC60958 status bits
  44. */
  45. enum r600_hdmi_iec_status_bits {
  46. AUDIO_STATUS_DIG_ENABLE = 0x01,
  47. AUDIO_STATUS_V = 0x02,
  48. AUDIO_STATUS_VCFG = 0x04,
  49. AUDIO_STATUS_EMPHASIS = 0x08,
  50. AUDIO_STATUS_COPYRIGHT = 0x10,
  51. AUDIO_STATUS_NONAUDIO = 0x20,
  52. AUDIO_STATUS_PROFESSIONAL = 0x40,
  53. AUDIO_STATUS_LEVEL = 0x80
  54. };
  55. static const struct radeon_hdmi_acr r600_hdmi_predefined_acr[] = {
  56. /* 32kHz 44.1kHz 48kHz */
  57. /* Clock N CTS N CTS N CTS */
  58. { 25175, 4096, 25175, 28224, 125875, 6144, 25175 }, /* 25,20/1.001 MHz */
  59. { 25200, 4096, 25200, 6272, 28000, 6144, 25200 }, /* 25.20 MHz */
  60. { 27000, 4096, 27000, 6272, 30000, 6144, 27000 }, /* 27.00 MHz */
  61. { 27027, 4096, 27027, 6272, 30030, 6144, 27027 }, /* 27.00*1.001 MHz */
  62. { 54000, 4096, 54000, 6272, 60000, 6144, 54000 }, /* 54.00 MHz */
  63. { 54054, 4096, 54054, 6272, 60060, 6144, 54054 }, /* 54.00*1.001 MHz */
  64. { 74176, 4096, 74176, 5733, 75335, 6144, 74176 }, /* 74.25/1.001 MHz */
  65. { 74250, 4096, 74250, 6272, 82500, 6144, 74250 }, /* 74.25 MHz */
  66. { 148352, 4096, 148352, 5733, 150670, 6144, 148352 }, /* 148.50/1.001 MHz */
  67. { 148500, 4096, 148500, 6272, 165000, 6144, 148500 }, /* 148.50 MHz */
  68. };
  69. /*
  70. * calculate CTS and N values if they are not found in the table
  71. */
  72. static void r600_hdmi_calc_cts(uint32_t clock, int *CTS, int *N, int freq)
  73. {
  74. int n, cts;
  75. unsigned long div, mul;
  76. /* Safe, but overly large values */
  77. n = 128 * freq;
  78. cts = clock * 1000;
  79. /* Smallest valid fraction */
  80. div = gcd(n, cts);
  81. n /= div;
  82. cts /= div;
  83. /*
  84. * The optimal N is 128*freq/1000. Calculate the closest larger
  85. * value that doesn't truncate any bits.
  86. */
  87. mul = ((128*freq/1000) + (n-1))/n;
  88. n *= mul;
  89. cts *= mul;
  90. /* Check that we are in spec (not always possible) */
  91. if (n < (128*freq/1500))
  92. printk(KERN_WARNING "Calculated ACR N value is too small. You may experience audio problems.\n");
  93. if (n > (128*freq/300))
  94. printk(KERN_WARNING "Calculated ACR N value is too large. You may experience audio problems.\n");
  95. *N = n;
  96. *CTS = cts;
  97. DRM_DEBUG("Calculated ACR timing N=%d CTS=%d for frequency %d\n",
  98. *N, *CTS, freq);
  99. }
  100. struct radeon_hdmi_acr r600_hdmi_acr(uint32_t clock)
  101. {
  102. struct radeon_hdmi_acr res;
  103. u8 i;
  104. /* Precalculated values for common clocks */
  105. for (i = 0; i < ARRAY_SIZE(r600_hdmi_predefined_acr); i++) {
  106. if (r600_hdmi_predefined_acr[i].clock == clock)
  107. return r600_hdmi_predefined_acr[i];
  108. }
  109. /* And odd clocks get manually calculated */
  110. r600_hdmi_calc_cts(clock, &res.cts_32khz, &res.n_32khz, 32000);
  111. r600_hdmi_calc_cts(clock, &res.cts_44_1khz, &res.n_44_1khz, 44100);
  112. r600_hdmi_calc_cts(clock, &res.cts_48khz, &res.n_48khz, 48000);
  113. return res;
  114. }
  115. /*
  116. * update the N and CTS parameters for a given pixel clock rate
  117. */
  118. void r600_hdmi_update_ACR(struct drm_encoder *encoder, uint32_t clock)
  119. {
  120. struct drm_device *dev = encoder->dev;
  121. struct radeon_device *rdev = dev->dev_private;
  122. struct radeon_hdmi_acr acr = r600_hdmi_acr(clock);
  123. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  124. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  125. uint32_t offset = dig->afmt->offset;
  126. WREG32_P(HDMI0_ACR_32_0 + offset,
  127. HDMI0_ACR_CTS_32(acr.cts_32khz),
  128. ~HDMI0_ACR_CTS_32_MASK);
  129. WREG32_P(HDMI0_ACR_32_1 + offset,
  130. HDMI0_ACR_N_32(acr.n_32khz),
  131. ~HDMI0_ACR_N_32_MASK);
  132. WREG32_P(HDMI0_ACR_44_0 + offset,
  133. HDMI0_ACR_CTS_44(acr.cts_44_1khz),
  134. ~HDMI0_ACR_CTS_44_MASK);
  135. WREG32_P(HDMI0_ACR_44_1 + offset,
  136. HDMI0_ACR_N_44(acr.n_44_1khz),
  137. ~HDMI0_ACR_N_44_MASK);
  138. WREG32_P(HDMI0_ACR_48_0 + offset,
  139. HDMI0_ACR_CTS_48(acr.cts_48khz),
  140. ~HDMI0_ACR_CTS_48_MASK);
  141. WREG32_P(HDMI0_ACR_48_1 + offset,
  142. HDMI0_ACR_N_48(acr.n_48khz),
  143. ~HDMI0_ACR_N_48_MASK);
  144. }
  145. /*
  146. * build a HDMI Video Info Frame
  147. */
  148. void r600_hdmi_update_avi_infoframe(struct drm_encoder *encoder, void *buffer,
  149. size_t size)
  150. {
  151. struct drm_device *dev = encoder->dev;
  152. struct radeon_device *rdev = dev->dev_private;
  153. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  154. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  155. uint32_t offset = dig->afmt->offset;
  156. uint8_t *frame = buffer + 3;
  157. uint8_t *header = buffer;
  158. WREG32(HDMI0_AVI_INFO0 + offset,
  159. frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
  160. WREG32(HDMI0_AVI_INFO1 + offset,
  161. frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x7] << 24));
  162. WREG32(HDMI0_AVI_INFO2 + offset,
  163. frame[0x8] | (frame[0x9] << 8) | (frame[0xA] << 16) | (frame[0xB] << 24));
  164. WREG32(HDMI0_AVI_INFO3 + offset,
  165. frame[0xC] | (frame[0xD] << 8) | (header[1] << 24));
  166. }
  167. /*
  168. * build a Audio Info Frame
  169. */
  170. static void r600_hdmi_update_audio_infoframe(struct drm_encoder *encoder,
  171. const void *buffer, size_t size)
  172. {
  173. struct drm_device *dev = encoder->dev;
  174. struct radeon_device *rdev = dev->dev_private;
  175. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  176. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  177. uint32_t offset = dig->afmt->offset;
  178. const u8 *frame = buffer + 3;
  179. WREG32(HDMI0_AUDIO_INFO0 + offset,
  180. frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
  181. WREG32(HDMI0_AUDIO_INFO1 + offset,
  182. frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x8] << 24));
  183. }
  184. /*
  185. * test if audio buffer is filled enough to start playing
  186. */
  187. static bool r600_hdmi_is_audio_buffer_filled(struct drm_encoder *encoder)
  188. {
  189. struct drm_device *dev = encoder->dev;
  190. struct radeon_device *rdev = dev->dev_private;
  191. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  192. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  193. uint32_t offset = dig->afmt->offset;
  194. return (RREG32(HDMI0_STATUS + offset) & 0x10) != 0;
  195. }
  196. /*
  197. * have buffer status changed since last call?
  198. */
  199. int r600_hdmi_buffer_status_changed(struct drm_encoder *encoder)
  200. {
  201. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  202. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  203. int status, result;
  204. if (!dig->afmt || !dig->afmt->enabled)
  205. return 0;
  206. status = r600_hdmi_is_audio_buffer_filled(encoder);
  207. result = dig->afmt->last_buffer_filled_status != status;
  208. dig->afmt->last_buffer_filled_status = status;
  209. return result;
  210. }
  211. /*
  212. * write the audio workaround status to the hardware
  213. */
  214. void r600_hdmi_audio_workaround(struct drm_encoder *encoder)
  215. {
  216. struct drm_device *dev = encoder->dev;
  217. struct radeon_device *rdev = dev->dev_private;
  218. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  219. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  220. uint32_t offset = dig->afmt->offset;
  221. bool hdmi_audio_workaround = false; /* FIXME */
  222. u32 value;
  223. if (!hdmi_audio_workaround ||
  224. r600_hdmi_is_audio_buffer_filled(encoder))
  225. value = 0; /* disable workaround */
  226. else
  227. value = HDMI0_AUDIO_TEST_EN; /* enable workaround */
  228. WREG32_P(HDMI0_AUDIO_PACKET_CONTROL + offset,
  229. value, ~HDMI0_AUDIO_TEST_EN);
  230. }
  231. void r600_audio_set_dto(struct drm_encoder *encoder, u32 clock)
  232. {
  233. struct drm_device *dev = encoder->dev;
  234. struct radeon_device *rdev = dev->dev_private;
  235. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  236. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  237. u32 base_rate = 24000;
  238. u32 max_ratio = clock / base_rate;
  239. u32 dto_phase;
  240. u32 dto_modulo = clock;
  241. u32 wallclock_ratio;
  242. u32 dto_cntl;
  243. if (!dig || !dig->afmt)
  244. return;
  245. if (max_ratio >= 8) {
  246. dto_phase = 192 * 1000;
  247. wallclock_ratio = 3;
  248. } else if (max_ratio >= 4) {
  249. dto_phase = 96 * 1000;
  250. wallclock_ratio = 2;
  251. } else if (max_ratio >= 2) {
  252. dto_phase = 48 * 1000;
  253. wallclock_ratio = 1;
  254. } else {
  255. dto_phase = 24 * 1000;
  256. wallclock_ratio = 0;
  257. }
  258. /* there are two DTOs selected by DCCG_AUDIO_DTO_SELECT.
  259. * doesn't matter which one you use. Just use the first one.
  260. */
  261. /* XXX two dtos; generally use dto0 for hdmi */
  262. /* Express [24MHz / target pixel clock] as an exact rational
  263. * number (coefficient of two integer numbers. DCCG_AUDIO_DTOx_PHASE
  264. * is the numerator, DCCG_AUDIO_DTOx_MODULE is the denominator
  265. */
  266. if (ASIC_IS_DCE32(rdev)) {
  267. if (dig->dig_encoder == 0) {
  268. dto_cntl = RREG32(DCCG_AUDIO_DTO0_CNTL) & ~DCCG_AUDIO_DTO_WALLCLOCK_RATIO_MASK;
  269. dto_cntl |= DCCG_AUDIO_DTO_WALLCLOCK_RATIO(wallclock_ratio);
  270. WREG32(DCCG_AUDIO_DTO0_CNTL, dto_cntl);
  271. WREG32(DCCG_AUDIO_DTO0_PHASE, dto_phase);
  272. WREG32(DCCG_AUDIO_DTO0_MODULE, dto_modulo);
  273. WREG32(DCCG_AUDIO_DTO_SELECT, 0); /* select DTO0 */
  274. } else {
  275. dto_cntl = RREG32(DCCG_AUDIO_DTO1_CNTL) & ~DCCG_AUDIO_DTO_WALLCLOCK_RATIO_MASK;
  276. dto_cntl |= DCCG_AUDIO_DTO_WALLCLOCK_RATIO(wallclock_ratio);
  277. WREG32(DCCG_AUDIO_DTO1_CNTL, dto_cntl);
  278. WREG32(DCCG_AUDIO_DTO1_PHASE, dto_phase);
  279. WREG32(DCCG_AUDIO_DTO1_MODULE, dto_modulo);
  280. WREG32(DCCG_AUDIO_DTO_SELECT, 1); /* select DTO1 */
  281. }
  282. } else {
  283. /* according to the reg specs, this should DCE3.2 only, but in
  284. * practice it seems to cover DCE2.0/3.0/3.1 as well.
  285. */
  286. if (dig->dig_encoder == 0) {
  287. WREG32(DCCG_AUDIO_DTO0_PHASE, base_rate * 100);
  288. WREG32(DCCG_AUDIO_DTO0_MODULE, clock * 100);
  289. WREG32(DCCG_AUDIO_DTO_SELECT, 0); /* select DTO0 */
  290. } else {
  291. WREG32(DCCG_AUDIO_DTO1_PHASE, base_rate * 100);
  292. WREG32(DCCG_AUDIO_DTO1_MODULE, clock * 100);
  293. WREG32(DCCG_AUDIO_DTO_SELECT, 1); /* select DTO1 */
  294. }
  295. }
  296. }
  297. /*
  298. * update the info frames with the data from the current display mode
  299. */
  300. void r600_hdmi_setmode(struct drm_encoder *encoder, struct drm_display_mode *mode)
  301. {
  302. struct drm_device *dev = encoder->dev;
  303. struct radeon_device *rdev = dev->dev_private;
  304. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  305. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  306. u8 buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AVI_INFOFRAME_SIZE];
  307. struct hdmi_avi_infoframe frame;
  308. uint32_t offset;
  309. uint32_t acr_ctl;
  310. ssize_t err;
  311. if (!dig || !dig->afmt)
  312. return;
  313. /* Silent, r600_hdmi_enable will raise WARN for us */
  314. if (!dig->afmt->enabled)
  315. return;
  316. offset = dig->afmt->offset;
  317. /* disable audio prior to setting up hw */
  318. dig->afmt->pin = r600_audio_get_pin(rdev);
  319. r600_audio_enable(rdev, dig->afmt->pin, false);
  320. r600_audio_set_dto(encoder, mode->clock);
  321. WREG32_P(HDMI0_AUDIO_PACKET_CONTROL + offset,
  322. HDMI0_AUDIO_SAMPLE_SEND | /* send audio packets */
  323. HDMI0_AUDIO_DELAY_EN(1) | /* default audio delay */
  324. HDMI0_AUDIO_PACKETS_PER_LINE(3) | /* should be suffient for all audio modes and small enough for all hblanks */
  325. HDMI0_60958_CS_UPDATE, /* allow 60958 channel status fields to be updated */
  326. ~(HDMI0_AUDIO_SAMPLE_SEND |
  327. HDMI0_AUDIO_DELAY_EN_MASK |
  328. HDMI0_AUDIO_PACKETS_PER_LINE_MASK |
  329. HDMI0_60958_CS_UPDATE));
  330. /* DCE 3.0 uses register that's normally for CRC_CONTROL */
  331. acr_ctl = ASIC_IS_DCE3(rdev) ? DCE3_HDMI0_ACR_PACKET_CONTROL :
  332. HDMI0_ACR_PACKET_CONTROL;
  333. WREG32_P(acr_ctl + offset,
  334. HDMI0_ACR_SOURCE | /* select SW CTS value - XXX verify that hw CTS works on all families */
  335. HDMI0_ACR_AUTO_SEND, /* allow hw to sent ACR packets when required */
  336. ~(HDMI0_ACR_SOURCE |
  337. HDMI0_ACR_AUTO_SEND));
  338. WREG32_OR(HDMI0_VBI_PACKET_CONTROL + offset,
  339. HDMI0_NULL_SEND | /* send null packets when required */
  340. HDMI0_GC_SEND | /* send general control packets */
  341. HDMI0_GC_CONT); /* send general control packets every frame */
  342. WREG32_OR(HDMI0_INFOFRAME_CONTROL0 + offset,
  343. HDMI0_AVI_INFO_SEND | /* enable AVI info frames */
  344. HDMI0_AVI_INFO_CONT | /* send AVI info frames every frame/field */
  345. HDMI0_AUDIO_INFO_SEND | /* enable audio info frames (frames won't be set until audio is enabled) */
  346. HDMI0_AUDIO_INFO_UPDATE); /* required for audio info values to be updated */
  347. WREG32_P(HDMI0_INFOFRAME_CONTROL1 + offset,
  348. HDMI0_AVI_INFO_LINE(2) | /* anything other than 0 */
  349. HDMI0_AUDIO_INFO_LINE(2), /* anything other than 0 */
  350. ~(HDMI0_AVI_INFO_LINE_MASK |
  351. HDMI0_AUDIO_INFO_LINE_MASK));
  352. WREG32_AND(HDMI0_GC + offset,
  353. ~HDMI0_GC_AVMUTE); /* unset HDMI0_GC_AVMUTE */
  354. err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode);
  355. if (err < 0) {
  356. DRM_ERROR("failed to setup AVI infoframe: %zd\n", err);
  357. return;
  358. }
  359. err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
  360. if (err < 0) {
  361. DRM_ERROR("failed to pack AVI infoframe: %zd\n", err);
  362. return;
  363. }
  364. r600_hdmi_update_avi_infoframe(encoder, buffer, sizeof(buffer));
  365. /* fglrx duplicates INFOFRAME_CONTROL0 & INFOFRAME_CONTROL1 ops here */
  366. WREG32_AND(HDMI0_GENERIC_PACKET_CONTROL + offset,
  367. ~(HDMI0_GENERIC0_SEND |
  368. HDMI0_GENERIC0_CONT |
  369. HDMI0_GENERIC0_UPDATE |
  370. HDMI0_GENERIC1_SEND |
  371. HDMI0_GENERIC1_CONT |
  372. HDMI0_GENERIC0_LINE_MASK |
  373. HDMI0_GENERIC1_LINE_MASK));
  374. r600_hdmi_update_ACR(encoder, mode->clock);
  375. WREG32_P(HDMI0_60958_0 + offset,
  376. HDMI0_60958_CS_CHANNEL_NUMBER_L(1),
  377. ~(HDMI0_60958_CS_CHANNEL_NUMBER_L_MASK |
  378. HDMI0_60958_CS_CLOCK_ACCURACY_MASK));
  379. WREG32_P(HDMI0_60958_1 + offset,
  380. HDMI0_60958_CS_CHANNEL_NUMBER_R(2),
  381. ~HDMI0_60958_CS_CHANNEL_NUMBER_R_MASK);
  382. /* it's unknown what these bits do excatly, but it's indeed quite useful for debugging */
  383. WREG32(HDMI0_RAMP_CONTROL0 + offset, 0x00FFFFFF);
  384. WREG32(HDMI0_RAMP_CONTROL1 + offset, 0x007FFFFF);
  385. WREG32(HDMI0_RAMP_CONTROL2 + offset, 0x00000001);
  386. WREG32(HDMI0_RAMP_CONTROL3 + offset, 0x00000001);
  387. /* enable audio after to setting up hw */
  388. r600_audio_enable(rdev, dig->afmt->pin, true);
  389. }
  390. /**
  391. * r600_hdmi_update_audio_settings - Update audio infoframe
  392. *
  393. * @encoder: drm encoder
  394. *
  395. * Gets info about current audio stream and updates audio infoframe.
  396. */
  397. void r600_hdmi_update_audio_settings(struct drm_encoder *encoder)
  398. {
  399. struct drm_device *dev = encoder->dev;
  400. struct radeon_device *rdev = dev->dev_private;
  401. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  402. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  403. struct r600_audio_pin audio = r600_audio_status(rdev);
  404. uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
  405. struct hdmi_audio_infoframe frame;
  406. uint32_t offset;
  407. uint32_t value;
  408. ssize_t err;
  409. if (!dig->afmt || !dig->afmt->enabled)
  410. return;
  411. offset = dig->afmt->offset;
  412. DRM_DEBUG("%s with %d channels, %d Hz sampling rate, %d bits per sample,\n",
  413. r600_hdmi_is_audio_buffer_filled(encoder) ? "playing" : "stopped",
  414. audio.channels, audio.rate, audio.bits_per_sample);
  415. DRM_DEBUG("0x%02X IEC60958 status bits and 0x%02X category code\n",
  416. (int)audio.status_bits, (int)audio.category_code);
  417. err = hdmi_audio_infoframe_init(&frame);
  418. if (err < 0) {
  419. DRM_ERROR("failed to setup audio infoframe\n");
  420. return;
  421. }
  422. frame.channels = audio.channels;
  423. err = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
  424. if (err < 0) {
  425. DRM_ERROR("failed to pack audio infoframe\n");
  426. return;
  427. }
  428. value = RREG32(HDMI0_AUDIO_PACKET_CONTROL + offset);
  429. if (value & HDMI0_AUDIO_TEST_EN)
  430. WREG32(HDMI0_AUDIO_PACKET_CONTROL + offset,
  431. value & ~HDMI0_AUDIO_TEST_EN);
  432. WREG32_OR(HDMI0_CONTROL + offset,
  433. HDMI0_ERROR_ACK);
  434. WREG32_AND(HDMI0_INFOFRAME_CONTROL0 + offset,
  435. ~HDMI0_AUDIO_INFO_SOURCE);
  436. r600_hdmi_update_audio_infoframe(encoder, buffer, sizeof(buffer));
  437. WREG32_OR(HDMI0_INFOFRAME_CONTROL0 + offset,
  438. HDMI0_AUDIO_INFO_CONT |
  439. HDMI0_AUDIO_INFO_UPDATE);
  440. }
  441. /*
  442. * enable the HDMI engine
  443. */
  444. void r600_hdmi_enable(struct drm_encoder *encoder, bool enable)
  445. {
  446. struct drm_device *dev = encoder->dev;
  447. struct radeon_device *rdev = dev->dev_private;
  448. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  449. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  450. u32 hdmi = HDMI0_ERROR_ACK;
  451. if (!dig || !dig->afmt)
  452. return;
  453. /* Silent, r600_hdmi_enable will raise WARN for us */
  454. if (enable && dig->afmt->enabled)
  455. return;
  456. if (!enable && !dig->afmt->enabled)
  457. return;
  458. /* Older chipsets require setting HDMI and routing manually */
  459. if (!ASIC_IS_DCE3(rdev)) {
  460. if (enable)
  461. hdmi |= HDMI0_ENABLE;
  462. switch (radeon_encoder->encoder_id) {
  463. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
  464. if (enable) {
  465. WREG32_OR(AVIVO_TMDSA_CNTL, AVIVO_TMDSA_CNTL_HDMI_EN);
  466. hdmi |= HDMI0_STREAM(HDMI0_STREAM_TMDSA);
  467. } else {
  468. WREG32_AND(AVIVO_TMDSA_CNTL, ~AVIVO_TMDSA_CNTL_HDMI_EN);
  469. }
  470. break;
  471. case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
  472. if (enable) {
  473. WREG32_OR(AVIVO_LVTMA_CNTL, AVIVO_LVTMA_CNTL_HDMI_EN);
  474. hdmi |= HDMI0_STREAM(HDMI0_STREAM_LVTMA);
  475. } else {
  476. WREG32_AND(AVIVO_LVTMA_CNTL, ~AVIVO_LVTMA_CNTL_HDMI_EN);
  477. }
  478. break;
  479. case ENCODER_OBJECT_ID_INTERNAL_DDI:
  480. if (enable) {
  481. WREG32_OR(DDIA_CNTL, DDIA_HDMI_EN);
  482. hdmi |= HDMI0_STREAM(HDMI0_STREAM_DDIA);
  483. } else {
  484. WREG32_AND(DDIA_CNTL, ~DDIA_HDMI_EN);
  485. }
  486. break;
  487. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
  488. if (enable)
  489. hdmi |= HDMI0_STREAM(HDMI0_STREAM_DVOA);
  490. break;
  491. default:
  492. dev_err(rdev->dev, "Invalid encoder for HDMI: 0x%X\n",
  493. radeon_encoder->encoder_id);
  494. break;
  495. }
  496. WREG32(HDMI0_CONTROL + dig->afmt->offset, hdmi);
  497. }
  498. if (rdev->irq.installed) {
  499. /* if irq is available use it */
  500. /* XXX: shouldn't need this on any asics. Double check DCE2/3 */
  501. if (enable)
  502. radeon_irq_kms_enable_afmt(rdev, dig->afmt->id);
  503. else
  504. radeon_irq_kms_disable_afmt(rdev, dig->afmt->id);
  505. }
  506. dig->afmt->enabled = enable;
  507. DRM_DEBUG("%sabling HDMI interface @ 0x%04X for encoder 0x%x\n",
  508. enable ? "En" : "Dis", dig->afmt->offset, radeon_encoder->encoder_id);
  509. }