r600_audio.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 "atom.h"
  30. #define AUDIO_TIMER_INTERVALL 100 /* 1/10 sekund should be enough */
  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
  37. || rdev->family == CHIP_RS600
  38. || rdev->family == CHIP_RS690
  39. || rdev->family == CHIP_RS740;
  40. }
  41. /*
  42. * current number of channels
  43. */
  44. static 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. static 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. DRM_ERROR("Unknown bits per sample 0x%x using 16 instead.\n", (int)value);
  62. return 16;
  63. }
  64. /*
  65. * current sampling rate in HZ
  66. */
  67. static int r600_audio_rate(struct radeon_device *rdev)
  68. {
  69. uint32_t value = RREG32(R600_AUDIO_RATE_BPS_CHANNEL);
  70. uint32_t result;
  71. if (value & 0x4000)
  72. result = 44100;
  73. else
  74. result = 48000;
  75. result *= ((value >> 11) & 0x7) + 1;
  76. result /= ((value >> 8) & 0x7) + 1;
  77. return result;
  78. }
  79. /*
  80. * iec 60958 status bits
  81. */
  82. static uint8_t r600_audio_status_bits(struct radeon_device *rdev)
  83. {
  84. return RREG32(R600_AUDIO_STATUS_BITS) & 0xff;
  85. }
  86. /*
  87. * iec 60958 category code
  88. */
  89. static uint8_t r600_audio_category_code(struct radeon_device *rdev)
  90. {
  91. return (RREG32(R600_AUDIO_STATUS_BITS) >> 8) & 0xff;
  92. }
  93. /*
  94. * update all hdmi interfaces with current audio parameters
  95. */
  96. static void r600_audio_update_hdmi(unsigned long param)
  97. {
  98. struct radeon_device *rdev = (struct radeon_device *)param;
  99. struct drm_device *dev = rdev->ddev;
  100. int channels = r600_audio_channels(rdev);
  101. int rate = r600_audio_rate(rdev);
  102. int bps = r600_audio_bits_per_sample(rdev);
  103. uint8_t status_bits = r600_audio_status_bits(rdev);
  104. uint8_t category_code = r600_audio_category_code(rdev);
  105. struct drm_encoder *encoder;
  106. int changes = 0;
  107. changes |= channels != rdev->audio_channels;
  108. changes |= rate != rdev->audio_rate;
  109. changes |= bps != rdev->audio_bits_per_sample;
  110. changes |= status_bits != rdev->audio_status_bits;
  111. changes |= category_code != rdev->audio_category_code;
  112. if (changes) {
  113. rdev->audio_channels = channels;
  114. rdev->audio_rate = rate;
  115. rdev->audio_bits_per_sample = bps;
  116. rdev->audio_status_bits = status_bits;
  117. rdev->audio_category_code = category_code;
  118. }
  119. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  120. if (changes || r600_hdmi_buffer_status_changed(encoder))
  121. r600_hdmi_update_audio_settings(
  122. encoder, channels,
  123. rate, bps, status_bits,
  124. category_code);
  125. }
  126. mod_timer(&rdev->audio_timer,
  127. jiffies + msecs_to_jiffies(AUDIO_TIMER_INTERVALL));
  128. }
  129. /*
  130. * initialize the audio vars and register the update timer
  131. */
  132. int r600_audio_init(struct radeon_device *rdev)
  133. {
  134. if (!r600_audio_chipset_supported(rdev))
  135. return 0;
  136. DRM_INFO("%s audio support", radeon_audio ? "Enabling" : "Disabling");
  137. WREG32_P(R600_AUDIO_ENABLE, radeon_audio ? 0x81000000 : 0x0, ~0x81000000);
  138. rdev->audio_channels = -1;
  139. rdev->audio_rate = -1;
  140. rdev->audio_bits_per_sample = -1;
  141. rdev->audio_status_bits = 0;
  142. rdev->audio_category_code = 0;
  143. setup_timer(
  144. &rdev->audio_timer,
  145. r600_audio_update_hdmi,
  146. (unsigned long)rdev);
  147. mod_timer(&rdev->audio_timer, jiffies + 1);
  148. return 0;
  149. }
  150. /*
  151. * determin how the encoders and audio interface is wired together
  152. */
  153. int r600_audio_tmds_index(struct drm_encoder *encoder)
  154. {
  155. struct drm_device *dev = encoder->dev;
  156. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  157. struct drm_encoder *other;
  158. switch (radeon_encoder->encoder_id) {
  159. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
  160. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
  161. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
  162. return 0;
  163. case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
  164. /* special case check if an TMDS1 is present */
  165. list_for_each_entry(other, &dev->mode_config.encoder_list, head) {
  166. if (to_radeon_encoder(other)->encoder_id ==
  167. ENCODER_OBJECT_ID_INTERNAL_TMDS1)
  168. return 1;
  169. }
  170. return 0;
  171. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
  172. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_LVTMA:
  173. return 1;
  174. default:
  175. DRM_ERROR("Unsupported encoder type 0x%02X\n",
  176. radeon_encoder->encoder_id);
  177. return -1;
  178. }
  179. }
  180. /*
  181. * atach the audio codec to the clock source of the encoder
  182. */
  183. void r600_audio_set_clock(struct drm_encoder *encoder, int clock)
  184. {
  185. struct drm_device *dev = encoder->dev;
  186. struct radeon_device *rdev = dev->dev_private;
  187. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  188. int base_rate = 48000;
  189. switch (radeon_encoder->encoder_id) {
  190. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
  191. case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
  192. WREG32_P(R600_AUDIO_TIMING, 0, ~0x301);
  193. break;
  194. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
  195. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
  196. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
  197. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_LVTMA:
  198. WREG32_P(R600_AUDIO_TIMING, 0x100, ~0x301);
  199. break;
  200. default:
  201. DRM_ERROR("Unsupported encoder type 0x%02X\n",
  202. radeon_encoder->encoder_id);
  203. return;
  204. }
  205. switch (r600_audio_tmds_index(encoder)) {
  206. case 0:
  207. WREG32(R600_AUDIO_PLL1_MUL, base_rate*50);
  208. WREG32(R600_AUDIO_PLL1_DIV, clock*100);
  209. WREG32(R600_AUDIO_CLK_SRCSEL, 0);
  210. break;
  211. case 1:
  212. WREG32(R600_AUDIO_PLL2_MUL, base_rate*50);
  213. WREG32(R600_AUDIO_PLL2_DIV, clock*100);
  214. WREG32(R600_AUDIO_CLK_SRCSEL, 1);
  215. break;
  216. }
  217. }
  218. /*
  219. * release the audio timer
  220. * TODO: How to do this correctly on SMP systems?
  221. */
  222. void r600_audio_fini(struct radeon_device *rdev)
  223. {
  224. if (!r600_audio_chipset_supported(rdev))
  225. return;
  226. WREG32_P(R600_AUDIO_ENABLE, 0x0, ~0x81000000);
  227. del_timer(&rdev->audio_timer);
  228. }