r600_hdmi.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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. * check if the chipset is supported
  71. */
  72. static int r600_audio_chipset_supported(struct radeon_device *rdev)
  73. {
  74. return ASIC_IS_DCE2(rdev) && !ASIC_IS_NODCE(rdev);
  75. }
  76. static struct r600_audio_pin r600_audio_status(struct radeon_device *rdev)
  77. {
  78. struct r600_audio_pin status;
  79. uint32_t value;
  80. value = RREG32(R600_AUDIO_RATE_BPS_CHANNEL);
  81. /* number of channels */
  82. status.channels = (value & 0x7) + 1;
  83. /* bits per sample */
  84. switch ((value & 0xF0) >> 4) {
  85. case 0x0:
  86. status.bits_per_sample = 8;
  87. break;
  88. case 0x1:
  89. status.bits_per_sample = 16;
  90. break;
  91. case 0x2:
  92. status.bits_per_sample = 20;
  93. break;
  94. case 0x3:
  95. status.bits_per_sample = 24;
  96. break;
  97. case 0x4:
  98. status.bits_per_sample = 32;
  99. break;
  100. default:
  101. dev_err(rdev->dev, "Unknown bits per sample 0x%x, using 16\n",
  102. (int)value);
  103. status.bits_per_sample = 16;
  104. }
  105. /* current sampling rate in HZ */
  106. if (value & 0x4000)
  107. status.rate = 44100;
  108. else
  109. status.rate = 48000;
  110. status.rate *= ((value >> 11) & 0x7) + 1;
  111. status.rate /= ((value >> 8) & 0x7) + 1;
  112. value = RREG32(R600_AUDIO_STATUS_BITS);
  113. /* iec 60958 status bits */
  114. status.status_bits = value & 0xff;
  115. /* iec 60958 category code */
  116. status.category_code = (value >> 8) & 0xff;
  117. return status;
  118. }
  119. /*
  120. * update all hdmi interfaces with current audio parameters
  121. */
  122. void r600_audio_update_hdmi(struct work_struct *work)
  123. {
  124. struct radeon_device *rdev = container_of(work, struct radeon_device,
  125. audio_work);
  126. struct drm_device *dev = rdev->ddev;
  127. struct r600_audio_pin audio_status = r600_audio_status(rdev);
  128. struct drm_encoder *encoder;
  129. bool changed = false;
  130. if (rdev->audio.pin[0].channels != audio_status.channels ||
  131. rdev->audio.pin[0].rate != audio_status.rate ||
  132. rdev->audio.pin[0].bits_per_sample != audio_status.bits_per_sample ||
  133. rdev->audio.pin[0].status_bits != audio_status.status_bits ||
  134. rdev->audio.pin[0].category_code != audio_status.category_code) {
  135. rdev->audio.pin[0] = audio_status;
  136. changed = true;
  137. }
  138. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  139. if (!radeon_encoder_is_digital(encoder))
  140. continue;
  141. if (changed || r600_hdmi_buffer_status_changed(encoder))
  142. r600_hdmi_update_audio_settings(encoder);
  143. }
  144. }
  145. /* enable the audio stream */
  146. void r600_audio_enable(struct radeon_device *rdev,
  147. struct r600_audio_pin *pin,
  148. u8 enable_mask)
  149. {
  150. u32 tmp = RREG32(AZ_HOT_PLUG_CONTROL);
  151. if (!pin)
  152. return;
  153. if (enable_mask) {
  154. tmp |= AUDIO_ENABLED;
  155. if (enable_mask & 1)
  156. tmp |= PIN0_AUDIO_ENABLED;
  157. if (enable_mask & 2)
  158. tmp |= PIN1_AUDIO_ENABLED;
  159. if (enable_mask & 4)
  160. tmp |= PIN2_AUDIO_ENABLED;
  161. if (enable_mask & 8)
  162. tmp |= PIN3_AUDIO_ENABLED;
  163. } else {
  164. tmp &= ~(AUDIO_ENABLED |
  165. PIN0_AUDIO_ENABLED |
  166. PIN1_AUDIO_ENABLED |
  167. PIN2_AUDIO_ENABLED |
  168. PIN3_AUDIO_ENABLED);
  169. }
  170. WREG32(AZ_HOT_PLUG_CONTROL, tmp);
  171. }
  172. /*
  173. * initialize the audio vars
  174. */
  175. int r600_audio_init(struct radeon_device *rdev)
  176. {
  177. if (!radeon_audio || !r600_audio_chipset_supported(rdev))
  178. return 0;
  179. rdev->audio.enabled = true;
  180. rdev->audio.num_pins = 1;
  181. rdev->audio.pin[0].channels = -1;
  182. rdev->audio.pin[0].rate = -1;
  183. rdev->audio.pin[0].bits_per_sample = -1;
  184. rdev->audio.pin[0].status_bits = 0;
  185. rdev->audio.pin[0].category_code = 0;
  186. rdev->audio.pin[0].id = 0;
  187. /* disable audio. it will be set up later */
  188. r600_audio_enable(rdev, &rdev->audio.pin[0], 0);
  189. return 0;
  190. }
  191. /*
  192. * release the audio timer
  193. * TODO: How to do this correctly on SMP systems?
  194. */
  195. void r600_audio_fini(struct radeon_device *rdev)
  196. {
  197. if (!rdev->audio.enabled)
  198. return;
  199. r600_audio_enable(rdev, &rdev->audio.pin[0], 0);
  200. rdev->audio.enabled = false;
  201. }
  202. struct r600_audio_pin *r600_audio_get_pin(struct radeon_device *rdev)
  203. {
  204. /* only one pin on 6xx-NI */
  205. return &rdev->audio.pin[0];
  206. }
  207. /*
  208. * calculate CTS and N values if they are not found in the table
  209. */
  210. static void r600_hdmi_calc_cts(uint32_t clock, int *CTS, int *N, int freq)
  211. {
  212. int n, cts;
  213. unsigned long div, mul;
  214. /* Safe, but overly large values */
  215. n = 128 * freq;
  216. cts = clock * 1000;
  217. /* Smallest valid fraction */
  218. div = gcd(n, cts);
  219. n /= div;
  220. cts /= div;
  221. /*
  222. * The optimal N is 128*freq/1000. Calculate the closest larger
  223. * value that doesn't truncate any bits.
  224. */
  225. mul = ((128*freq/1000) + (n-1))/n;
  226. n *= mul;
  227. cts *= mul;
  228. /* Check that we are in spec (not always possible) */
  229. if (n < (128*freq/1500))
  230. printk(KERN_WARNING "Calculated ACR N value is too small. You may experience audio problems.\n");
  231. if (n > (128*freq/300))
  232. printk(KERN_WARNING "Calculated ACR N value is too large. You may experience audio problems.\n");
  233. *N = n;
  234. *CTS = cts;
  235. DRM_DEBUG("Calculated ACR timing N=%d CTS=%d for frequency %d\n",
  236. *N, *CTS, freq);
  237. }
  238. struct radeon_hdmi_acr r600_hdmi_acr(uint32_t clock)
  239. {
  240. struct radeon_hdmi_acr res;
  241. u8 i;
  242. /* Precalculated values for common clocks */
  243. for (i = 0; i < ARRAY_SIZE(r600_hdmi_predefined_acr); i++) {
  244. if (r600_hdmi_predefined_acr[i].clock == clock)
  245. return r600_hdmi_predefined_acr[i];
  246. }
  247. /* And odd clocks get manually calculated */
  248. r600_hdmi_calc_cts(clock, &res.cts_32khz, &res.n_32khz, 32000);
  249. r600_hdmi_calc_cts(clock, &res.cts_44_1khz, &res.n_44_1khz, 44100);
  250. r600_hdmi_calc_cts(clock, &res.cts_48khz, &res.n_48khz, 48000);
  251. return res;
  252. }
  253. /*
  254. * update the N and CTS parameters for a given pixel clock rate
  255. */
  256. void r600_hdmi_update_ACR(struct drm_encoder *encoder, uint32_t clock)
  257. {
  258. struct drm_device *dev = encoder->dev;
  259. struct radeon_device *rdev = dev->dev_private;
  260. struct radeon_hdmi_acr acr = r600_hdmi_acr(clock);
  261. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  262. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  263. uint32_t offset = dig->afmt->offset;
  264. WREG32_P(HDMI0_ACR_32_0 + offset,
  265. HDMI0_ACR_CTS_32(acr.cts_32khz),
  266. ~HDMI0_ACR_CTS_32_MASK);
  267. WREG32_P(HDMI0_ACR_32_1 + offset,
  268. HDMI0_ACR_N_32(acr.n_32khz),
  269. ~HDMI0_ACR_N_32_MASK);
  270. WREG32_P(HDMI0_ACR_44_0 + offset,
  271. HDMI0_ACR_CTS_44(acr.cts_44_1khz),
  272. ~HDMI0_ACR_CTS_44_MASK);
  273. WREG32_P(HDMI0_ACR_44_1 + offset,
  274. HDMI0_ACR_N_44(acr.n_44_1khz),
  275. ~HDMI0_ACR_N_44_MASK);
  276. WREG32_P(HDMI0_ACR_48_0 + offset,
  277. HDMI0_ACR_CTS_48(acr.cts_48khz),
  278. ~HDMI0_ACR_CTS_48_MASK);
  279. WREG32_P(HDMI0_ACR_48_1 + offset,
  280. HDMI0_ACR_N_48(acr.n_48khz),
  281. ~HDMI0_ACR_N_48_MASK);
  282. }
  283. /*
  284. * build a HDMI Video Info Frame
  285. */
  286. void r600_hdmi_update_avi_infoframe(struct drm_encoder *encoder, void *buffer,
  287. size_t size)
  288. {
  289. struct drm_device *dev = encoder->dev;
  290. struct radeon_device *rdev = dev->dev_private;
  291. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  292. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  293. uint32_t offset = dig->afmt->offset;
  294. uint8_t *frame = buffer + 3;
  295. uint8_t *header = buffer;
  296. WREG32(HDMI0_AVI_INFO0 + offset,
  297. frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
  298. WREG32(HDMI0_AVI_INFO1 + offset,
  299. frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x7] << 24));
  300. WREG32(HDMI0_AVI_INFO2 + offset,
  301. frame[0x8] | (frame[0x9] << 8) | (frame[0xA] << 16) | (frame[0xB] << 24));
  302. WREG32(HDMI0_AVI_INFO3 + offset,
  303. frame[0xC] | (frame[0xD] << 8) | (header[1] << 24));
  304. }
  305. /*
  306. * build a Audio Info Frame
  307. */
  308. static void r600_hdmi_update_audio_infoframe(struct drm_encoder *encoder,
  309. const void *buffer, size_t size)
  310. {
  311. struct drm_device *dev = encoder->dev;
  312. struct radeon_device *rdev = dev->dev_private;
  313. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  314. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  315. uint32_t offset = dig->afmt->offset;
  316. const u8 *frame = buffer + 3;
  317. WREG32(HDMI0_AUDIO_INFO0 + offset,
  318. frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
  319. WREG32(HDMI0_AUDIO_INFO1 + offset,
  320. frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x8] << 24));
  321. }
  322. /*
  323. * test if audio buffer is filled enough to start playing
  324. */
  325. static bool r600_hdmi_is_audio_buffer_filled(struct drm_encoder *encoder)
  326. {
  327. struct drm_device *dev = encoder->dev;
  328. struct radeon_device *rdev = dev->dev_private;
  329. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  330. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  331. uint32_t offset = dig->afmt->offset;
  332. return (RREG32(HDMI0_STATUS + offset) & 0x10) != 0;
  333. }
  334. /*
  335. * have buffer status changed since last call?
  336. */
  337. int r600_hdmi_buffer_status_changed(struct drm_encoder *encoder)
  338. {
  339. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  340. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  341. int status, result;
  342. if (!dig->afmt || !dig->afmt->enabled)
  343. return 0;
  344. status = r600_hdmi_is_audio_buffer_filled(encoder);
  345. result = dig->afmt->last_buffer_filled_status != status;
  346. dig->afmt->last_buffer_filled_status = status;
  347. return result;
  348. }
  349. /*
  350. * write the audio workaround status to the hardware
  351. */
  352. void r600_hdmi_audio_workaround(struct drm_encoder *encoder)
  353. {
  354. struct drm_device *dev = encoder->dev;
  355. struct radeon_device *rdev = dev->dev_private;
  356. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  357. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  358. uint32_t offset = dig->afmt->offset;
  359. bool hdmi_audio_workaround = false; /* FIXME */
  360. u32 value;
  361. if (!hdmi_audio_workaround ||
  362. r600_hdmi_is_audio_buffer_filled(encoder))
  363. value = 0; /* disable workaround */
  364. else
  365. value = HDMI0_AUDIO_TEST_EN; /* enable workaround */
  366. WREG32_P(HDMI0_AUDIO_PACKET_CONTROL + offset,
  367. value, ~HDMI0_AUDIO_TEST_EN);
  368. }
  369. void r600_audio_set_dto(struct drm_encoder *encoder, u32 clock)
  370. {
  371. struct drm_device *dev = encoder->dev;
  372. struct radeon_device *rdev = dev->dev_private;
  373. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  374. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  375. u32 base_rate = 24000;
  376. u32 max_ratio = clock / base_rate;
  377. u32 dto_phase;
  378. u32 dto_modulo = clock;
  379. u32 wallclock_ratio;
  380. u32 dto_cntl;
  381. if (!dig || !dig->afmt)
  382. return;
  383. if (max_ratio >= 8) {
  384. dto_phase = 192 * 1000;
  385. wallclock_ratio = 3;
  386. } else if (max_ratio >= 4) {
  387. dto_phase = 96 * 1000;
  388. wallclock_ratio = 2;
  389. } else if (max_ratio >= 2) {
  390. dto_phase = 48 * 1000;
  391. wallclock_ratio = 1;
  392. } else {
  393. dto_phase = 24 * 1000;
  394. wallclock_ratio = 0;
  395. }
  396. /* there are two DTOs selected by DCCG_AUDIO_DTO_SELECT.
  397. * doesn't matter which one you use. Just use the first one.
  398. */
  399. /* XXX two dtos; generally use dto0 for hdmi */
  400. /* Express [24MHz / target pixel clock] as an exact rational
  401. * number (coefficient of two integer numbers. DCCG_AUDIO_DTOx_PHASE
  402. * is the numerator, DCCG_AUDIO_DTOx_MODULE is the denominator
  403. */
  404. if (ASIC_IS_DCE32(rdev)) {
  405. if (dig->dig_encoder == 0) {
  406. dto_cntl = RREG32(DCCG_AUDIO_DTO0_CNTL) & ~DCCG_AUDIO_DTO_WALLCLOCK_RATIO_MASK;
  407. dto_cntl |= DCCG_AUDIO_DTO_WALLCLOCK_RATIO(wallclock_ratio);
  408. WREG32(DCCG_AUDIO_DTO0_CNTL, dto_cntl);
  409. WREG32(DCCG_AUDIO_DTO0_PHASE, dto_phase);
  410. WREG32(DCCG_AUDIO_DTO0_MODULE, dto_modulo);
  411. WREG32(DCCG_AUDIO_DTO_SELECT, 0); /* select DTO0 */
  412. } else {
  413. dto_cntl = RREG32(DCCG_AUDIO_DTO1_CNTL) & ~DCCG_AUDIO_DTO_WALLCLOCK_RATIO_MASK;
  414. dto_cntl |= DCCG_AUDIO_DTO_WALLCLOCK_RATIO(wallclock_ratio);
  415. WREG32(DCCG_AUDIO_DTO1_CNTL, dto_cntl);
  416. WREG32(DCCG_AUDIO_DTO1_PHASE, dto_phase);
  417. WREG32(DCCG_AUDIO_DTO1_MODULE, dto_modulo);
  418. WREG32(DCCG_AUDIO_DTO_SELECT, 1); /* select DTO1 */
  419. }
  420. } else {
  421. /* according to the reg specs, this should DCE3.2 only, but in
  422. * practice it seems to cover DCE2.0/3.0/3.1 as well.
  423. */
  424. if (dig->dig_encoder == 0) {
  425. WREG32(DCCG_AUDIO_DTO0_PHASE, base_rate * 100);
  426. WREG32(DCCG_AUDIO_DTO0_MODULE, clock * 100);
  427. WREG32(DCCG_AUDIO_DTO_SELECT, 0); /* select DTO0 */
  428. } else {
  429. WREG32(DCCG_AUDIO_DTO1_PHASE, base_rate * 100);
  430. WREG32(DCCG_AUDIO_DTO1_MODULE, clock * 100);
  431. WREG32(DCCG_AUDIO_DTO_SELECT, 1); /* select DTO1 */
  432. }
  433. }
  434. }
  435. /*
  436. * update the info frames with the data from the current display mode
  437. */
  438. void r600_hdmi_setmode(struct drm_encoder *encoder, struct drm_display_mode *mode)
  439. {
  440. struct drm_device *dev = encoder->dev;
  441. struct radeon_device *rdev = dev->dev_private;
  442. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  443. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  444. u8 buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AVI_INFOFRAME_SIZE];
  445. struct hdmi_avi_infoframe frame;
  446. uint32_t offset;
  447. uint32_t acr_ctl;
  448. ssize_t err;
  449. if (!dig || !dig->afmt)
  450. return;
  451. /* Silent, r600_hdmi_enable will raise WARN for us */
  452. if (!dig->afmt->enabled)
  453. return;
  454. offset = dig->afmt->offset;
  455. /* disable audio prior to setting up hw */
  456. dig->afmt->pin = r600_audio_get_pin(rdev);
  457. r600_audio_enable(rdev, dig->afmt->pin, 0xf);
  458. r600_audio_set_dto(encoder, mode->clock);
  459. WREG32_P(HDMI0_AUDIO_PACKET_CONTROL + offset,
  460. HDMI0_AUDIO_SAMPLE_SEND | /* send audio packets */
  461. HDMI0_AUDIO_DELAY_EN(1) | /* default audio delay */
  462. HDMI0_AUDIO_PACKETS_PER_LINE(3) | /* should be suffient for all audio modes and small enough for all hblanks */
  463. HDMI0_60958_CS_UPDATE, /* allow 60958 channel status fields to be updated */
  464. ~(HDMI0_AUDIO_SAMPLE_SEND |
  465. HDMI0_AUDIO_DELAY_EN_MASK |
  466. HDMI0_AUDIO_PACKETS_PER_LINE_MASK |
  467. HDMI0_60958_CS_UPDATE));
  468. /* DCE 3.0 uses register that's normally for CRC_CONTROL */
  469. acr_ctl = ASIC_IS_DCE3(rdev) ? DCE3_HDMI0_ACR_PACKET_CONTROL :
  470. HDMI0_ACR_PACKET_CONTROL;
  471. WREG32_P(acr_ctl + offset,
  472. HDMI0_ACR_SOURCE | /* select SW CTS value - XXX verify that hw CTS works on all families */
  473. HDMI0_ACR_AUTO_SEND, /* allow hw to sent ACR packets when required */
  474. ~(HDMI0_ACR_SOURCE |
  475. HDMI0_ACR_AUTO_SEND));
  476. WREG32_OR(HDMI0_VBI_PACKET_CONTROL + offset,
  477. HDMI0_NULL_SEND | /* send null packets when required */
  478. HDMI0_GC_SEND | /* send general control packets */
  479. HDMI0_GC_CONT); /* send general control packets every frame */
  480. WREG32_OR(HDMI0_INFOFRAME_CONTROL0 + offset,
  481. HDMI0_AVI_INFO_SEND | /* enable AVI info frames */
  482. HDMI0_AVI_INFO_CONT | /* send AVI info frames every frame/field */
  483. HDMI0_AUDIO_INFO_SEND | /* enable audio info frames (frames won't be set until audio is enabled) */
  484. HDMI0_AUDIO_INFO_UPDATE); /* required for audio info values to be updated */
  485. WREG32_P(HDMI0_INFOFRAME_CONTROL1 + offset,
  486. HDMI0_AVI_INFO_LINE(2) | /* anything other than 0 */
  487. HDMI0_AUDIO_INFO_LINE(2), /* anything other than 0 */
  488. ~(HDMI0_AVI_INFO_LINE_MASK |
  489. HDMI0_AUDIO_INFO_LINE_MASK));
  490. WREG32_AND(HDMI0_GC + offset,
  491. ~HDMI0_GC_AVMUTE); /* unset HDMI0_GC_AVMUTE */
  492. err = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode);
  493. if (err < 0) {
  494. DRM_ERROR("failed to setup AVI infoframe: %zd\n", err);
  495. return;
  496. }
  497. err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
  498. if (err < 0) {
  499. DRM_ERROR("failed to pack AVI infoframe: %zd\n", err);
  500. return;
  501. }
  502. r600_hdmi_update_avi_infoframe(encoder, buffer, sizeof(buffer));
  503. /* fglrx duplicates INFOFRAME_CONTROL0 & INFOFRAME_CONTROL1 ops here */
  504. WREG32_AND(HDMI0_GENERIC_PACKET_CONTROL + offset,
  505. ~(HDMI0_GENERIC0_SEND |
  506. HDMI0_GENERIC0_CONT |
  507. HDMI0_GENERIC0_UPDATE |
  508. HDMI0_GENERIC1_SEND |
  509. HDMI0_GENERIC1_CONT |
  510. HDMI0_GENERIC0_LINE_MASK |
  511. HDMI0_GENERIC1_LINE_MASK));
  512. r600_hdmi_update_ACR(encoder, mode->clock);
  513. WREG32_P(HDMI0_60958_0 + offset,
  514. HDMI0_60958_CS_CHANNEL_NUMBER_L(1),
  515. ~(HDMI0_60958_CS_CHANNEL_NUMBER_L_MASK |
  516. HDMI0_60958_CS_CLOCK_ACCURACY_MASK));
  517. WREG32_P(HDMI0_60958_1 + offset,
  518. HDMI0_60958_CS_CHANNEL_NUMBER_R(2),
  519. ~HDMI0_60958_CS_CHANNEL_NUMBER_R_MASK);
  520. /* it's unknown what these bits do excatly, but it's indeed quite useful for debugging */
  521. WREG32(HDMI0_RAMP_CONTROL0 + offset, 0x00FFFFFF);
  522. WREG32(HDMI0_RAMP_CONTROL1 + offset, 0x007FFFFF);
  523. WREG32(HDMI0_RAMP_CONTROL2 + offset, 0x00000001);
  524. WREG32(HDMI0_RAMP_CONTROL3 + offset, 0x00000001);
  525. /* enable audio after to setting up hw */
  526. r600_audio_enable(rdev, dig->afmt->pin, 0xf);
  527. }
  528. /**
  529. * r600_hdmi_update_audio_settings - Update audio infoframe
  530. *
  531. * @encoder: drm encoder
  532. *
  533. * Gets info about current audio stream and updates audio infoframe.
  534. */
  535. void r600_hdmi_update_audio_settings(struct drm_encoder *encoder)
  536. {
  537. struct drm_device *dev = encoder->dev;
  538. struct radeon_device *rdev = dev->dev_private;
  539. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  540. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  541. struct r600_audio_pin audio = r600_audio_status(rdev);
  542. uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
  543. struct hdmi_audio_infoframe frame;
  544. uint32_t offset;
  545. uint32_t value;
  546. ssize_t err;
  547. if (!dig->afmt || !dig->afmt->enabled)
  548. return;
  549. offset = dig->afmt->offset;
  550. DRM_DEBUG("%s with %d channels, %d Hz sampling rate, %d bits per sample,\n",
  551. r600_hdmi_is_audio_buffer_filled(encoder) ? "playing" : "stopped",
  552. audio.channels, audio.rate, audio.bits_per_sample);
  553. DRM_DEBUG("0x%02X IEC60958 status bits and 0x%02X category code\n",
  554. (int)audio.status_bits, (int)audio.category_code);
  555. err = hdmi_audio_infoframe_init(&frame);
  556. if (err < 0) {
  557. DRM_ERROR("failed to setup audio infoframe\n");
  558. return;
  559. }
  560. frame.channels = audio.channels;
  561. err = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
  562. if (err < 0) {
  563. DRM_ERROR("failed to pack audio infoframe\n");
  564. return;
  565. }
  566. value = RREG32(HDMI0_AUDIO_PACKET_CONTROL + offset);
  567. if (value & HDMI0_AUDIO_TEST_EN)
  568. WREG32(HDMI0_AUDIO_PACKET_CONTROL + offset,
  569. value & ~HDMI0_AUDIO_TEST_EN);
  570. WREG32_OR(HDMI0_CONTROL + offset,
  571. HDMI0_ERROR_ACK);
  572. WREG32_AND(HDMI0_INFOFRAME_CONTROL0 + offset,
  573. ~HDMI0_AUDIO_INFO_SOURCE);
  574. r600_hdmi_update_audio_infoframe(encoder, buffer, sizeof(buffer));
  575. WREG32_OR(HDMI0_INFOFRAME_CONTROL0 + offset,
  576. HDMI0_AUDIO_INFO_CONT |
  577. HDMI0_AUDIO_INFO_UPDATE);
  578. }
  579. /*
  580. * enable the HDMI engine
  581. */
  582. void r600_hdmi_enable(struct drm_encoder *encoder, bool enable)
  583. {
  584. struct drm_device *dev = encoder->dev;
  585. struct radeon_device *rdev = dev->dev_private;
  586. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  587. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  588. u32 hdmi = HDMI0_ERROR_ACK;
  589. if (!dig || !dig->afmt)
  590. return;
  591. /* Silent, r600_hdmi_enable will raise WARN for us */
  592. if (enable && dig->afmt->enabled)
  593. return;
  594. if (!enable && !dig->afmt->enabled)
  595. return;
  596. if (!enable && dig->afmt->pin) {
  597. r600_audio_enable(rdev, dig->afmt->pin, 0);
  598. dig->afmt->pin = NULL;
  599. }
  600. /* Older chipsets require setting HDMI and routing manually */
  601. if (!ASIC_IS_DCE3(rdev)) {
  602. if (enable)
  603. hdmi |= HDMI0_ENABLE;
  604. switch (radeon_encoder->encoder_id) {
  605. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
  606. if (enable) {
  607. WREG32_OR(AVIVO_TMDSA_CNTL, AVIVO_TMDSA_CNTL_HDMI_EN);
  608. hdmi |= HDMI0_STREAM(HDMI0_STREAM_TMDSA);
  609. } else {
  610. WREG32_AND(AVIVO_TMDSA_CNTL, ~AVIVO_TMDSA_CNTL_HDMI_EN);
  611. }
  612. break;
  613. case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
  614. if (enable) {
  615. WREG32_OR(AVIVO_LVTMA_CNTL, AVIVO_LVTMA_CNTL_HDMI_EN);
  616. hdmi |= HDMI0_STREAM(HDMI0_STREAM_LVTMA);
  617. } else {
  618. WREG32_AND(AVIVO_LVTMA_CNTL, ~AVIVO_LVTMA_CNTL_HDMI_EN);
  619. }
  620. break;
  621. case ENCODER_OBJECT_ID_INTERNAL_DDI:
  622. if (enable) {
  623. WREG32_OR(DDIA_CNTL, DDIA_HDMI_EN);
  624. hdmi |= HDMI0_STREAM(HDMI0_STREAM_DDIA);
  625. } else {
  626. WREG32_AND(DDIA_CNTL, ~DDIA_HDMI_EN);
  627. }
  628. break;
  629. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
  630. if (enable)
  631. hdmi |= HDMI0_STREAM(HDMI0_STREAM_DVOA);
  632. break;
  633. default:
  634. dev_err(rdev->dev, "Invalid encoder for HDMI: 0x%X\n",
  635. radeon_encoder->encoder_id);
  636. break;
  637. }
  638. WREG32(HDMI0_CONTROL + dig->afmt->offset, hdmi);
  639. }
  640. if (rdev->irq.installed) {
  641. /* if irq is available use it */
  642. /* XXX: shouldn't need this on any asics. Double check DCE2/3 */
  643. if (enable)
  644. radeon_irq_kms_enable_afmt(rdev, dig->afmt->id);
  645. else
  646. radeon_irq_kms_disable_afmt(rdev, dig->afmt->id);
  647. }
  648. dig->afmt->enabled = enable;
  649. DRM_DEBUG("%sabling HDMI interface @ 0x%04X for encoder 0x%x\n",
  650. enable ? "En" : "Dis", dig->afmt->offset, radeon_encoder->encoder_id);
  651. }