r600_audio.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 <drm/drmP.h>
  27. #include "radeon.h"
  28. #include "radeon_reg.h"
  29. #include "radeon_asic.h"
  30. #include "atom.h"
  31. /*
  32. * check if enc_priv stores radeon_encoder_atom_dig
  33. */
  34. static bool radeon_dig_encoder(struct drm_encoder *encoder)
  35. {
  36. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  37. switch (radeon_encoder->encoder_id) {
  38. case ENCODER_OBJECT_ID_INTERNAL_LVDS:
  39. case ENCODER_OBJECT_ID_INTERNAL_TMDS1:
  40. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
  41. case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
  42. case ENCODER_OBJECT_ID_INTERNAL_DVO1:
  43. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
  44. case ENCODER_OBJECT_ID_INTERNAL_DDI:
  45. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
  46. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_LVTMA:
  47. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
  48. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
  49. return true;
  50. }
  51. return false;
  52. }
  53. /*
  54. * check if the chipset is supported
  55. */
  56. static int r600_audio_chipset_supported(struct radeon_device *rdev)
  57. {
  58. return (rdev->family >= CHIP_R600 && !ASIC_IS_DCE6(rdev))
  59. || rdev->family == CHIP_RS600
  60. || rdev->family == CHIP_RS690
  61. || rdev->family == CHIP_RS740;
  62. }
  63. struct r600_audio r600_audio_status(struct radeon_device *rdev)
  64. {
  65. struct r600_audio status;
  66. uint32_t value;
  67. value = RREG32(R600_AUDIO_RATE_BPS_CHANNEL);
  68. /* number of channels */
  69. status.channels = (value & 0x7) + 1;
  70. /* bits per sample */
  71. switch ((value & 0xF0) >> 4) {
  72. case 0x0:
  73. status.bits_per_sample = 8;
  74. break;
  75. case 0x1:
  76. status.bits_per_sample = 16;
  77. break;
  78. case 0x2:
  79. status.bits_per_sample = 20;
  80. break;
  81. case 0x3:
  82. status.bits_per_sample = 24;
  83. break;
  84. case 0x4:
  85. status.bits_per_sample = 32;
  86. break;
  87. default:
  88. dev_err(rdev->dev, "Unknown bits per sample 0x%x, using 16\n",
  89. (int)value);
  90. status.bits_per_sample = 16;
  91. }
  92. /* current sampling rate in HZ */
  93. if (value & 0x4000)
  94. status.rate = 44100;
  95. else
  96. status.rate = 48000;
  97. status.rate *= ((value >> 11) & 0x7) + 1;
  98. status.rate /= ((value >> 8) & 0x7) + 1;
  99. value = RREG32(R600_AUDIO_STATUS_BITS);
  100. /* iec 60958 status bits */
  101. status.status_bits = value & 0xff;
  102. /* iec 60958 category code */
  103. status.category_code = (value >> 8) & 0xff;
  104. return status;
  105. }
  106. /*
  107. * update all hdmi interfaces with current audio parameters
  108. */
  109. void r600_audio_update_hdmi(struct work_struct *work)
  110. {
  111. struct radeon_device *rdev = container_of(work, struct radeon_device,
  112. audio_work);
  113. struct drm_device *dev = rdev->ddev;
  114. struct r600_audio audio_status = r600_audio_status(rdev);
  115. struct drm_encoder *encoder;
  116. bool changed = false;
  117. if (rdev->audio_status.channels != audio_status.channels ||
  118. rdev->audio_status.rate != audio_status.rate ||
  119. rdev->audio_status.bits_per_sample != audio_status.bits_per_sample ||
  120. rdev->audio_status.status_bits != audio_status.status_bits ||
  121. rdev->audio_status.category_code != audio_status.category_code) {
  122. rdev->audio_status = audio_status;
  123. changed = true;
  124. }
  125. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  126. if (!radeon_dig_encoder(encoder))
  127. continue;
  128. if (changed || r600_hdmi_buffer_status_changed(encoder))
  129. r600_hdmi_update_audio_settings(encoder);
  130. }
  131. }
  132. /*
  133. * turn on/off audio engine
  134. */
  135. static void r600_audio_engine_enable(struct radeon_device *rdev, bool enable)
  136. {
  137. u32 value = 0;
  138. DRM_INFO("%s audio support\n", enable ? "Enabling" : "Disabling");
  139. if (ASIC_IS_DCE4(rdev)) {
  140. if (enable) {
  141. value |= 0x81000000; /* Required to enable audio */
  142. value |= 0x0e1000f0; /* fglrx sets that too */
  143. }
  144. WREG32(EVERGREEN_AUDIO_ENABLE, value);
  145. } else {
  146. WREG32_P(R600_AUDIO_ENABLE,
  147. enable ? 0x81000000 : 0x0, ~0x81000000);
  148. }
  149. rdev->audio_enabled = enable;
  150. }
  151. /*
  152. * initialize the audio vars
  153. */
  154. int r600_audio_init(struct radeon_device *rdev)
  155. {
  156. if (!radeon_audio || !r600_audio_chipset_supported(rdev))
  157. return 0;
  158. r600_audio_engine_enable(rdev, true);
  159. rdev->audio_status.channels = -1;
  160. rdev->audio_status.rate = -1;
  161. rdev->audio_status.bits_per_sample = -1;
  162. rdev->audio_status.status_bits = 0;
  163. rdev->audio_status.category_code = 0;
  164. return 0;
  165. }
  166. /*
  167. * atach the audio codec to the clock source of the encoder
  168. */
  169. void r600_audio_set_clock(struct drm_encoder *encoder, int clock)
  170. {
  171. struct drm_device *dev = encoder->dev;
  172. struct radeon_device *rdev = dev->dev_private;
  173. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  174. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  175. struct radeon_crtc *radeon_crtc = to_radeon_crtc(encoder->crtc);
  176. int base_rate = 48000;
  177. switch (radeon_encoder->encoder_id) {
  178. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
  179. case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
  180. WREG32_P(R600_AUDIO_TIMING, 0, ~0x301);
  181. break;
  182. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
  183. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
  184. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
  185. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_LVTMA:
  186. WREG32_P(R600_AUDIO_TIMING, 0x100, ~0x301);
  187. break;
  188. default:
  189. dev_err(rdev->dev, "Unsupported encoder type 0x%02X\n",
  190. radeon_encoder->encoder_id);
  191. return;
  192. }
  193. if (ASIC_IS_DCE4(rdev)) {
  194. /* TODO: other PLLs? */
  195. WREG32(EVERGREEN_AUDIO_PLL1_MUL, base_rate * 10);
  196. WREG32(EVERGREEN_AUDIO_PLL1_DIV, clock * 10);
  197. WREG32(EVERGREEN_AUDIO_PLL1_UNK, 0x00000071);
  198. /* Select DTO source */
  199. WREG32(0x5ac, radeon_crtc->crtc_id);
  200. } else {
  201. switch (dig->dig_encoder) {
  202. case 0:
  203. WREG32(R600_AUDIO_PLL1_MUL, base_rate * 50);
  204. WREG32(R600_AUDIO_PLL1_DIV, clock * 100);
  205. WREG32(R600_AUDIO_CLK_SRCSEL, 0);
  206. break;
  207. case 1:
  208. WREG32(R600_AUDIO_PLL2_MUL, base_rate * 50);
  209. WREG32(R600_AUDIO_PLL2_DIV, clock * 100);
  210. WREG32(R600_AUDIO_CLK_SRCSEL, 1);
  211. break;
  212. default:
  213. dev_err(rdev->dev,
  214. "Unsupported DIG on encoder 0x%02X\n",
  215. radeon_encoder->encoder_id);
  216. return;
  217. }
  218. }
  219. }
  220. /*
  221. * release the audio timer
  222. * TODO: How to do this correctly on SMP systems?
  223. */
  224. void r600_audio_fini(struct radeon_device *rdev)
  225. {
  226. if (!rdev->audio_enabled)
  227. return;
  228. r600_audio_engine_enable(rdev, false);
  229. }