r600_audio.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 ASIC_IS_DCE2(rdev) && !ASIC_IS_DCE6(rdev);
  59. }
  60. struct r600_audio r600_audio_status(struct radeon_device *rdev)
  61. {
  62. struct r600_audio status;
  63. uint32_t value;
  64. value = RREG32(R600_AUDIO_RATE_BPS_CHANNEL);
  65. /* number of channels */
  66. status.channels = (value & 0x7) + 1;
  67. /* bits per sample */
  68. switch ((value & 0xF0) >> 4) {
  69. case 0x0:
  70. status.bits_per_sample = 8;
  71. break;
  72. case 0x1:
  73. status.bits_per_sample = 16;
  74. break;
  75. case 0x2:
  76. status.bits_per_sample = 20;
  77. break;
  78. case 0x3:
  79. status.bits_per_sample = 24;
  80. break;
  81. case 0x4:
  82. status.bits_per_sample = 32;
  83. break;
  84. default:
  85. dev_err(rdev->dev, "Unknown bits per sample 0x%x, using 16\n",
  86. (int)value);
  87. status.bits_per_sample = 16;
  88. }
  89. /* current sampling rate in HZ */
  90. if (value & 0x4000)
  91. status.rate = 44100;
  92. else
  93. status.rate = 48000;
  94. status.rate *= ((value >> 11) & 0x7) + 1;
  95. status.rate /= ((value >> 8) & 0x7) + 1;
  96. value = RREG32(R600_AUDIO_STATUS_BITS);
  97. /* iec 60958 status bits */
  98. status.status_bits = value & 0xff;
  99. /* iec 60958 category code */
  100. status.category_code = (value >> 8) & 0xff;
  101. return status;
  102. }
  103. /*
  104. * update all hdmi interfaces with current audio parameters
  105. */
  106. void r600_audio_update_hdmi(struct work_struct *work)
  107. {
  108. struct radeon_device *rdev = container_of(work, struct radeon_device,
  109. audio_work);
  110. struct drm_device *dev = rdev->ddev;
  111. struct r600_audio audio_status = r600_audio_status(rdev);
  112. struct drm_encoder *encoder;
  113. bool changed = false;
  114. if (rdev->audio_status.channels != audio_status.channels ||
  115. rdev->audio_status.rate != audio_status.rate ||
  116. rdev->audio_status.bits_per_sample != audio_status.bits_per_sample ||
  117. rdev->audio_status.status_bits != audio_status.status_bits ||
  118. rdev->audio_status.category_code != audio_status.category_code) {
  119. rdev->audio_status = audio_status;
  120. changed = true;
  121. }
  122. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  123. if (!radeon_dig_encoder(encoder))
  124. continue;
  125. if (changed || r600_hdmi_buffer_status_changed(encoder))
  126. r600_hdmi_update_audio_settings(encoder);
  127. }
  128. }
  129. /*
  130. * turn on/off audio engine
  131. */
  132. static void r600_audio_engine_enable(struct radeon_device *rdev, bool enable)
  133. {
  134. u32 value = 0;
  135. DRM_INFO("%s audio support\n", enable ? "Enabling" : "Disabling");
  136. if (ASIC_IS_DCE4(rdev)) {
  137. if (enable) {
  138. value |= 0x81000000; /* Required to enable audio */
  139. value |= 0x0e1000f0; /* fglrx sets that too */
  140. }
  141. WREG32(EVERGREEN_AUDIO_ENABLE, value);
  142. } else {
  143. WREG32_P(R600_AUDIO_ENABLE,
  144. enable ? 0x81000000 : 0x0, ~0x81000000);
  145. }
  146. rdev->audio_enabled = enable;
  147. }
  148. /*
  149. * initialize the audio vars
  150. */
  151. int r600_audio_init(struct radeon_device *rdev)
  152. {
  153. if (!radeon_audio || !r600_audio_chipset_supported(rdev))
  154. return 0;
  155. r600_audio_engine_enable(rdev, true);
  156. rdev->audio_status.channels = -1;
  157. rdev->audio_status.rate = -1;
  158. rdev->audio_status.bits_per_sample = -1;
  159. rdev->audio_status.status_bits = 0;
  160. rdev->audio_status.category_code = 0;
  161. return 0;
  162. }
  163. /*
  164. * release the audio timer
  165. * TODO: How to do this correctly on SMP systems?
  166. */
  167. void r600_audio_fini(struct radeon_device *rdev)
  168. {
  169. if (!rdev->audio_enabled)
  170. return;
  171. r600_audio_engine_enable(rdev, false);
  172. }