dce6_afmt.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Copyright 2013 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. */
  23. #include <linux/hdmi.h>
  24. #include <drm/drmP.h>
  25. #include "radeon.h"
  26. #include "radeon_audio.h"
  27. #include "sid.h"
  28. #define DCE8_DCCG_AUDIO_DTO1_PHASE 0x05b8
  29. #define DCE8_DCCG_AUDIO_DTO1_MODULE 0x05bc
  30. u32 dce6_endpoint_rreg(struct radeon_device *rdev,
  31. u32 block_offset, u32 reg)
  32. {
  33. unsigned long flags;
  34. u32 r;
  35. spin_lock_irqsave(&rdev->end_idx_lock, flags);
  36. WREG32(AZ_F0_CODEC_ENDPOINT_INDEX + block_offset, reg);
  37. r = RREG32(AZ_F0_CODEC_ENDPOINT_DATA + block_offset);
  38. spin_unlock_irqrestore(&rdev->end_idx_lock, flags);
  39. return r;
  40. }
  41. void dce6_endpoint_wreg(struct radeon_device *rdev,
  42. u32 block_offset, u32 reg, u32 v)
  43. {
  44. unsigned long flags;
  45. spin_lock_irqsave(&rdev->end_idx_lock, flags);
  46. if (ASIC_IS_DCE8(rdev))
  47. WREG32(AZ_F0_CODEC_ENDPOINT_INDEX + block_offset, reg);
  48. else
  49. WREG32(AZ_F0_CODEC_ENDPOINT_INDEX + block_offset,
  50. AZ_ENDPOINT_REG_WRITE_EN | AZ_ENDPOINT_REG_INDEX(reg));
  51. WREG32(AZ_F0_CODEC_ENDPOINT_DATA + block_offset, v);
  52. spin_unlock_irqrestore(&rdev->end_idx_lock, flags);
  53. }
  54. static void dce6_afmt_get_connected_pins(struct radeon_device *rdev)
  55. {
  56. int i;
  57. u32 offset, tmp;
  58. for (i = 0; i < rdev->audio.num_pins; i++) {
  59. offset = rdev->audio.pin[i].offset;
  60. tmp = RREG32_ENDPOINT(offset,
  61. AZ_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT);
  62. if (((tmp & PORT_CONNECTIVITY_MASK) >> PORT_CONNECTIVITY_SHIFT) == 1)
  63. rdev->audio.pin[i].connected = false;
  64. else
  65. rdev->audio.pin[i].connected = true;
  66. }
  67. }
  68. struct r600_audio_pin *dce6_audio_get_pin(struct radeon_device *rdev)
  69. {
  70. struct drm_encoder *encoder;
  71. struct radeon_encoder *radeon_encoder;
  72. struct radeon_encoder_atom_dig *dig;
  73. struct r600_audio_pin *pin = NULL;
  74. int i, pin_count;
  75. dce6_afmt_get_connected_pins(rdev);
  76. for (i = 0; i < rdev->audio.num_pins; i++) {
  77. if (rdev->audio.pin[i].connected) {
  78. pin = &rdev->audio.pin[i];
  79. pin_count = 0;
  80. list_for_each_entry(encoder, &rdev->ddev->mode_config.encoder_list, head) {
  81. if (radeon_encoder_is_digital(encoder)) {
  82. radeon_encoder = to_radeon_encoder(encoder);
  83. dig = radeon_encoder->enc_priv;
  84. if (dig->pin == pin)
  85. pin_count++;
  86. }
  87. }
  88. if (pin_count == 0)
  89. return pin;
  90. }
  91. }
  92. if (!pin)
  93. DRM_ERROR("No connected audio pins found!\n");
  94. return pin;
  95. }
  96. void dce6_afmt_select_pin(struct drm_encoder *encoder)
  97. {
  98. struct radeon_device *rdev = encoder->dev->dev_private;
  99. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  100. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  101. if (!dig || !dig->afmt || !dig->pin)
  102. return;
  103. WREG32(AFMT_AUDIO_SRC_CONTROL + dig->afmt->offset,
  104. AFMT_AUDIO_SRC_SELECT(dig->pin->id));
  105. }
  106. void dce6_afmt_write_latency_fields(struct drm_encoder *encoder,
  107. struct drm_connector *connector,
  108. struct drm_display_mode *mode)
  109. {
  110. struct radeon_device *rdev = encoder->dev->dev_private;
  111. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  112. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  113. u32 tmp = 0;
  114. if (!dig || !dig->afmt || !dig->pin)
  115. return;
  116. if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
  117. if (connector->latency_present[1])
  118. tmp = VIDEO_LIPSYNC(connector->video_latency[1]) |
  119. AUDIO_LIPSYNC(connector->audio_latency[1]);
  120. else
  121. tmp = VIDEO_LIPSYNC(0) | AUDIO_LIPSYNC(0);
  122. } else {
  123. if (connector->latency_present[0])
  124. tmp = VIDEO_LIPSYNC(connector->video_latency[0]) |
  125. AUDIO_LIPSYNC(connector->audio_latency[0]);
  126. else
  127. tmp = VIDEO_LIPSYNC(0) | AUDIO_LIPSYNC(0);
  128. }
  129. WREG32_ENDPOINT(dig->pin->offset,
  130. AZ_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC, tmp);
  131. }
  132. void dce6_afmt_hdmi_write_speaker_allocation(struct drm_encoder *encoder,
  133. u8 *sadb, int sad_count)
  134. {
  135. struct radeon_device *rdev = encoder->dev->dev_private;
  136. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  137. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  138. u32 tmp;
  139. if (!dig || !dig->afmt || !dig->pin)
  140. return;
  141. /* program the speaker allocation */
  142. tmp = RREG32_ENDPOINT(dig->pin->offset,
  143. AZ_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER);
  144. tmp &= ~(DP_CONNECTION | SPEAKER_ALLOCATION_MASK);
  145. /* set HDMI mode */
  146. tmp |= HDMI_CONNECTION;
  147. if (sad_count)
  148. tmp |= SPEAKER_ALLOCATION(sadb[0]);
  149. else
  150. tmp |= SPEAKER_ALLOCATION(5); /* stereo */
  151. WREG32_ENDPOINT(dig->pin->offset,
  152. AZ_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER, tmp);
  153. }
  154. void dce6_afmt_dp_write_speaker_allocation(struct drm_encoder *encoder,
  155. u8 *sadb, int sad_count)
  156. {
  157. struct radeon_device *rdev = encoder->dev->dev_private;
  158. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  159. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  160. u32 tmp;
  161. if (!dig || !dig->afmt || !dig->pin)
  162. return;
  163. /* program the speaker allocation */
  164. tmp = RREG32_ENDPOINT(dig->pin->offset,
  165. AZ_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER);
  166. tmp &= ~(HDMI_CONNECTION | SPEAKER_ALLOCATION_MASK);
  167. /* set DP mode */
  168. tmp |= DP_CONNECTION;
  169. if (sad_count)
  170. tmp |= SPEAKER_ALLOCATION(sadb[0]);
  171. else
  172. tmp |= SPEAKER_ALLOCATION(5); /* stereo */
  173. WREG32_ENDPOINT(dig->pin->offset,
  174. AZ_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER, tmp);
  175. }
  176. void dce6_afmt_write_sad_regs(struct drm_encoder *encoder,
  177. struct cea_sad *sads, int sad_count)
  178. {
  179. int i;
  180. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  181. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  182. struct radeon_device *rdev = encoder->dev->dev_private;
  183. static const u16 eld_reg_to_type[][2] = {
  184. { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0, HDMI_AUDIO_CODING_TYPE_PCM },
  185. { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR1, HDMI_AUDIO_CODING_TYPE_AC3 },
  186. { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR2, HDMI_AUDIO_CODING_TYPE_MPEG1 },
  187. { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR3, HDMI_AUDIO_CODING_TYPE_MP3 },
  188. { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR4, HDMI_AUDIO_CODING_TYPE_MPEG2 },
  189. { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR5, HDMI_AUDIO_CODING_TYPE_AAC_LC },
  190. { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR6, HDMI_AUDIO_CODING_TYPE_DTS },
  191. { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR7, HDMI_AUDIO_CODING_TYPE_ATRAC },
  192. { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR9, HDMI_AUDIO_CODING_TYPE_EAC3 },
  193. { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR10, HDMI_AUDIO_CODING_TYPE_DTS_HD },
  194. { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR11, HDMI_AUDIO_CODING_TYPE_MLP },
  195. { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR13, HDMI_AUDIO_CODING_TYPE_WMA_PRO },
  196. };
  197. if (!dig || !dig->afmt || !dig->pin)
  198. return;
  199. for (i = 0; i < ARRAY_SIZE(eld_reg_to_type); i++) {
  200. u32 value = 0;
  201. u8 stereo_freqs = 0;
  202. int max_channels = -1;
  203. int j;
  204. for (j = 0; j < sad_count; j++) {
  205. struct cea_sad *sad = &sads[j];
  206. if (sad->format == eld_reg_to_type[i][1]) {
  207. if (sad->channels > max_channels) {
  208. value = MAX_CHANNELS(sad->channels) |
  209. DESCRIPTOR_BYTE_2(sad->byte2) |
  210. SUPPORTED_FREQUENCIES(sad->freq);
  211. max_channels = sad->channels;
  212. }
  213. if (sad->format == HDMI_AUDIO_CODING_TYPE_PCM)
  214. stereo_freqs |= sad->freq;
  215. else
  216. break;
  217. }
  218. }
  219. value |= SUPPORTED_FREQUENCIES_STEREO(stereo_freqs);
  220. WREG32_ENDPOINT(dig->pin->offset, eld_reg_to_type[i][0], value);
  221. }
  222. }
  223. void dce6_audio_enable(struct radeon_device *rdev,
  224. struct r600_audio_pin *pin,
  225. u8 enable_mask)
  226. {
  227. if (!pin)
  228. return;
  229. WREG32_ENDPOINT(pin->offset, AZ_F0_CODEC_PIN_CONTROL_HOT_PLUG_CONTROL,
  230. enable_mask ? AUDIO_ENABLED : 0);
  231. }
  232. void dce6_hdmi_audio_set_dto(struct radeon_device *rdev,
  233. struct radeon_crtc *crtc, unsigned int clock)
  234. {
  235. /* Two dtos; generally use dto0 for HDMI */
  236. u32 value = 0;
  237. if (crtc)
  238. value |= DCCG_AUDIO_DTO0_SOURCE_SEL(crtc->crtc_id);
  239. WREG32(DCCG_AUDIO_DTO_SOURCE, value);
  240. /* Express [24MHz / target pixel clock] as an exact rational
  241. * number (coefficient of two integer numbers. DCCG_AUDIO_DTOx_PHASE
  242. * is the numerator, DCCG_AUDIO_DTOx_MODULE is the denominator
  243. */
  244. WREG32(DCCG_AUDIO_DTO0_PHASE, 24000);
  245. WREG32(DCCG_AUDIO_DTO0_MODULE, clock);
  246. }
  247. void dce6_dp_audio_set_dto(struct radeon_device *rdev,
  248. struct radeon_crtc *crtc, unsigned int clock)
  249. {
  250. /* Two dtos; generally use dto1 for DP */
  251. u32 value = 0;
  252. value |= DCCG_AUDIO_DTO_SEL;
  253. if (crtc)
  254. value |= DCCG_AUDIO_DTO0_SOURCE_SEL(crtc->crtc_id);
  255. WREG32(DCCG_AUDIO_DTO_SOURCE, value);
  256. /* Express [24MHz / target pixel clock] as an exact rational
  257. * number (coefficient of two integer numbers. DCCG_AUDIO_DTOx_PHASE
  258. * is the numerator, DCCG_AUDIO_DTOx_MODULE is the denominator
  259. */
  260. if (ASIC_IS_DCE8(rdev)) {
  261. unsigned int div = (RREG32(DENTIST_DISPCLK_CNTL) &
  262. DENTIST_DPREFCLK_WDIVIDER_MASK) >>
  263. DENTIST_DPREFCLK_WDIVIDER_SHIFT;
  264. div = radeon_audio_decode_dfs_div(div);
  265. if (div)
  266. clock = clock * 100 / div;
  267. WREG32(DCE8_DCCG_AUDIO_DTO1_PHASE, 24000);
  268. WREG32(DCE8_DCCG_AUDIO_DTO1_MODULE, clock);
  269. } else {
  270. WREG32(DCCG_AUDIO_DTO1_PHASE, 24000);
  271. WREG32(DCCG_AUDIO_DTO1_MODULE, clock);
  272. }
  273. }