r600_audio.c 6.5 KB

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