r600_audio.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 && rdev->family < CHIP_CEDAR)
  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. 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. 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. 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. uint8_t r600_audio_category_code(struct radeon_device *rdev)
  90. {
  91. return (RREG32(R600_AUDIO_STATUS_BITS) >> 8) & 0xff;
  92. }
  93. /*
  94. * schedule next audio update event
  95. */
  96. void r600_audio_schedule_polling(struct radeon_device *rdev)
  97. {
  98. mod_timer(&rdev->audio_timer,
  99. jiffies + msecs_to_jiffies(AUDIO_TIMER_INTERVALL));
  100. }
  101. /*
  102. * update all hdmi interfaces with current audio parameters
  103. */
  104. static void r600_audio_update_hdmi(unsigned long param)
  105. {
  106. struct radeon_device *rdev = (struct radeon_device *)param;
  107. struct drm_device *dev = rdev->ddev;
  108. int channels = r600_audio_channels(rdev);
  109. int rate = r600_audio_rate(rdev);
  110. int bps = r600_audio_bits_per_sample(rdev);
  111. uint8_t status_bits = r600_audio_status_bits(rdev);
  112. uint8_t category_code = r600_audio_category_code(rdev);
  113. struct drm_encoder *encoder;
  114. int changes = 0, still_going = 0;
  115. changes |= channels != rdev->audio_channels;
  116. changes |= rate != rdev->audio_rate;
  117. changes |= bps != rdev->audio_bits_per_sample;
  118. changes |= status_bits != rdev->audio_status_bits;
  119. changes |= category_code != rdev->audio_category_code;
  120. if (changes) {
  121. rdev->audio_channels = channels;
  122. rdev->audio_rate = rate;
  123. rdev->audio_bits_per_sample = bps;
  124. rdev->audio_status_bits = status_bits;
  125. rdev->audio_category_code = category_code;
  126. }
  127. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  128. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  129. still_going |= radeon_encoder->audio_polling_active;
  130. if (changes || r600_hdmi_buffer_status_changed(encoder))
  131. r600_hdmi_update_audio_settings(encoder);
  132. }
  133. if(still_going) r600_audio_schedule_polling(rdev);
  134. }
  135. /*
  136. * turn on/off audio engine
  137. */
  138. static void r600_audio_engine_enable(struct radeon_device *rdev, bool enable)
  139. {
  140. DRM_INFO("%s audio support", enable ? "Enabling" : "Disabling");
  141. WREG32_P(R600_AUDIO_ENABLE, enable ? 0x81000000 : 0x0, ~0x81000000);
  142. }
  143. /*
  144. * initialize the audio vars and register the update timer
  145. */
  146. int r600_audio_init(struct radeon_device *rdev)
  147. {
  148. if (!radeon_audio || !r600_audio_chipset_supported(rdev))
  149. return 0;
  150. r600_audio_engine_enable(rdev, true);
  151. rdev->audio_channels = -1;
  152. rdev->audio_rate = -1;
  153. rdev->audio_bits_per_sample = -1;
  154. rdev->audio_status_bits = 0;
  155. rdev->audio_category_code = 0;
  156. setup_timer(
  157. &rdev->audio_timer,
  158. r600_audio_update_hdmi,
  159. (unsigned long)rdev);
  160. return 0;
  161. }
  162. /*
  163. * enable the polling timer, to check for status changes
  164. */
  165. void r600_audio_enable_polling(struct drm_encoder *encoder)
  166. {
  167. struct drm_device *dev = encoder->dev;
  168. struct radeon_device *rdev = dev->dev_private;
  169. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  170. DRM_DEBUG("r600_audio_enable_polling: %d", radeon_encoder->audio_polling_active);
  171. if (radeon_encoder->audio_polling_active)
  172. return;
  173. radeon_encoder->audio_polling_active = 1;
  174. mod_timer(&rdev->audio_timer, jiffies + 1);
  175. }
  176. /*
  177. * disable the polling timer, so we get no more status updates
  178. */
  179. void r600_audio_disable_polling(struct drm_encoder *encoder)
  180. {
  181. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  182. DRM_DEBUG("r600_audio_disable_polling: %d", radeon_encoder->audio_polling_active);
  183. radeon_encoder->audio_polling_active = 0;
  184. }
  185. /*
  186. * atach the audio codec to the clock source of the encoder
  187. */
  188. void r600_audio_set_clock(struct drm_encoder *encoder, int clock)
  189. {
  190. struct drm_device *dev = encoder->dev;
  191. struct radeon_device *rdev = dev->dev_private;
  192. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  193. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  194. int base_rate = 48000;
  195. switch (radeon_encoder->encoder_id) {
  196. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
  197. case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
  198. WREG32_P(R600_AUDIO_TIMING, 0, ~0x301);
  199. break;
  200. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:
  201. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:
  202. case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:
  203. case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_LVTMA:
  204. WREG32_P(R600_AUDIO_TIMING, 0x100, ~0x301);
  205. break;
  206. default:
  207. DRM_ERROR("Unsupported encoder type 0x%02X\n",
  208. radeon_encoder->encoder_id);
  209. return;
  210. }
  211. switch (dig->dig_encoder) {
  212. case 0:
  213. WREG32(R600_AUDIO_PLL1_MUL, base_rate * 50);
  214. WREG32(R600_AUDIO_PLL1_DIV, clock * 100);
  215. WREG32(R600_AUDIO_CLK_SRCSEL, 0);
  216. break;
  217. case 1:
  218. WREG32(R600_AUDIO_PLL2_MUL, base_rate * 50);
  219. WREG32(R600_AUDIO_PLL2_DIV, clock * 100);
  220. WREG32(R600_AUDIO_CLK_SRCSEL, 1);
  221. break;
  222. default:
  223. dev_err(rdev->dev, "Unsupported DIG on encoder 0x%02X\n",
  224. radeon_encoder->encoder_id);
  225. return;
  226. }
  227. }
  228. /*
  229. * release the audio timer
  230. * TODO: How to do this correctly on SMP systems?
  231. */
  232. void r600_audio_fini(struct radeon_device *rdev)
  233. {
  234. if (!radeon_audio || !r600_audio_chipset_supported(rdev))
  235. return;
  236. del_timer(&rdev->audio_timer);
  237. r600_audio_engine_enable(rdev, false);
  238. }