blob: 6bfc46369db1b4f8daa02fba453a382e863a3585 [file] [log] [blame]
Alex Deucherb5306022013-07-31 16:51:33 -04001/*
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"
Slava Grigorev1a626b62014-12-01 13:49:39 -050026#include "radeon_audio.h"
Alex Deucherb5306022013-07-31 16:51:33 -040027#include "sid.h"
28
Slava Grigorev2afa3262015-03-02 12:05:29 -050029#define DCE8_DCCG_AUDIO_DTO1_PHASE 0x05b8
30#define DCE8_DCCG_AUDIO_DTO1_MODULE 0x05bc
31
Slava Grigorev1a626b62014-12-01 13:49:39 -050032u32 dce6_endpoint_rreg(struct radeon_device *rdev,
Alex Deucherb5306022013-07-31 16:51:33 -040033 u32 block_offset, u32 reg)
34{
Alex Deucher0a5b7b02013-09-03 19:00:09 -040035 unsigned long flags;
Alex Deucherb5306022013-07-31 16:51:33 -040036 u32 r;
37
Alex Deucher0a5b7b02013-09-03 19:00:09 -040038 spin_lock_irqsave(&rdev->end_idx_lock, flags);
Alex Deucherb5306022013-07-31 16:51:33 -040039 WREG32(AZ_F0_CODEC_ENDPOINT_INDEX + block_offset, reg);
40 r = RREG32(AZ_F0_CODEC_ENDPOINT_DATA + block_offset);
Alex Deucher0a5b7b02013-09-03 19:00:09 -040041 spin_unlock_irqrestore(&rdev->end_idx_lock, flags);
42
Alex Deucherb5306022013-07-31 16:51:33 -040043 return r;
44}
45
Slava Grigorev1a626b62014-12-01 13:49:39 -050046void dce6_endpoint_wreg(struct radeon_device *rdev,
Alex Deucherb5306022013-07-31 16:51:33 -040047 u32 block_offset, u32 reg, u32 v)
48{
Alex Deucher0a5b7b02013-09-03 19:00:09 -040049 unsigned long flags;
50
51 spin_lock_irqsave(&rdev->end_idx_lock, flags);
Alex Deucherb5306022013-07-31 16:51:33 -040052 if (ASIC_IS_DCE8(rdev))
53 WREG32(AZ_F0_CODEC_ENDPOINT_INDEX + block_offset, reg);
54 else
55 WREG32(AZ_F0_CODEC_ENDPOINT_INDEX + block_offset,
56 AZ_ENDPOINT_REG_WRITE_EN | AZ_ENDPOINT_REG_INDEX(reg));
57 WREG32(AZ_F0_CODEC_ENDPOINT_DATA + block_offset, v);
Alex Deucher0a5b7b02013-09-03 19:00:09 -040058 spin_unlock_irqrestore(&rdev->end_idx_lock, flags);
Alex Deucherb5306022013-07-31 16:51:33 -040059}
60
Alex Deucherb5306022013-07-31 16:51:33 -040061static void dce6_afmt_get_connected_pins(struct radeon_device *rdev)
62{
63 int i;
64 u32 offset, tmp;
65
66 for (i = 0; i < rdev->audio.num_pins; i++) {
67 offset = rdev->audio.pin[i].offset;
68 tmp = RREG32_ENDPOINT(offset,
69 AZ_F0_CODEC_PIN_CONTROL_RESPONSE_CONFIGURATION_DEFAULT);
70 if (((tmp & PORT_CONNECTIVITY_MASK) >> PORT_CONNECTIVITY_SHIFT) == 1)
71 rdev->audio.pin[i].connected = false;
72 else
73 rdev->audio.pin[i].connected = true;
74 }
75}
76
77struct r600_audio_pin *dce6_audio_get_pin(struct radeon_device *rdev)
78{
Alex Deucherd0465202015-07-23 10:53:51 -040079 struct drm_encoder *encoder;
80 struct radeon_encoder *radeon_encoder;
81 struct radeon_encoder_atom_dig *dig;
82 struct r600_audio_pin *pin = NULL;
83 int i, pin_count;
Alex Deucherb5306022013-07-31 16:51:33 -040084
85 dce6_afmt_get_connected_pins(rdev);
86
87 for (i = 0; i < rdev->audio.num_pins; i++) {
Alex Deucherd0465202015-07-23 10:53:51 -040088 if (rdev->audio.pin[i].connected) {
89 pin = &rdev->audio.pin[i];
90 pin_count = 0;
91
92 list_for_each_entry(encoder, &rdev->ddev->mode_config.encoder_list, head) {
93 if (radeon_encoder_is_digital(encoder)) {
94 radeon_encoder = to_radeon_encoder(encoder);
95 dig = radeon_encoder->enc_priv;
96 if (dig->pin == pin)
97 pin_count++;
98 }
99 }
100
101 if (pin_count == 0)
102 return pin;
103 }
Alex Deucherb5306022013-07-31 16:51:33 -0400104 }
Alex Deucherd0465202015-07-23 10:53:51 -0400105 if (!pin)
106 DRM_ERROR("No connected audio pins found!\n");
107 return pin;
Alex Deucherb5306022013-07-31 16:51:33 -0400108}
109
110void dce6_afmt_select_pin(struct drm_encoder *encoder)
111{
112 struct radeon_device *rdev = encoder->dev->dev_private;
113 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
114 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Alex Deucherb5306022013-07-31 16:51:33 -0400115
Alex Deucherd0ea3972015-07-23 10:01:09 -0400116 if (!dig || !dig->afmt || !dig->pin)
Alex Deucherb5306022013-07-31 16:51:33 -0400117 return;
118
Alex Deucherd0ea3972015-07-23 10:01:09 -0400119 WREG32(AFMT_AUDIO_SRC_CONTROL + dig->afmt->offset,
120 AFMT_AUDIO_SRC_SELECT(dig->pin->id));
Alex Deucherb5306022013-07-31 16:51:33 -0400121}
122
Alex Deucherb1880252013-10-10 18:03:06 -0400123void dce6_afmt_write_latency_fields(struct drm_encoder *encoder,
Alex Deucherd0ea3972015-07-23 10:01:09 -0400124 struct drm_connector *connector,
125 struct drm_display_mode *mode)
Alex Deucherb1880252013-10-10 18:03:06 -0400126{
127 struct radeon_device *rdev = encoder->dev->dev_private;
128 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
129 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Alex Deucherd0ea3972015-07-23 10:01:09 -0400130 u32 tmp = 0;
Alex Deucherb1880252013-10-10 18:03:06 -0400131
Alex Deucherd0ea3972015-07-23 10:01:09 -0400132 if (!dig || !dig->afmt || !dig->pin)
Alex Deucherb1880252013-10-10 18:03:06 -0400133 return;
134
Alex Deucherb1880252013-10-10 18:03:06 -0400135 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
136 if (connector->latency_present[1])
137 tmp = VIDEO_LIPSYNC(connector->video_latency[1]) |
138 AUDIO_LIPSYNC(connector->audio_latency[1]);
139 else
Stefan Brünsc7489902014-07-13 01:47:14 +0200140 tmp = VIDEO_LIPSYNC(0) | AUDIO_LIPSYNC(0);
Alex Deucherb1880252013-10-10 18:03:06 -0400141 } else {
142 if (connector->latency_present[0])
143 tmp = VIDEO_LIPSYNC(connector->video_latency[0]) |
144 AUDIO_LIPSYNC(connector->audio_latency[0]);
145 else
Stefan Brünsc7489902014-07-13 01:47:14 +0200146 tmp = VIDEO_LIPSYNC(0) | AUDIO_LIPSYNC(0);
Alex Deucherb1880252013-10-10 18:03:06 -0400147 }
Alex Deucherd0ea3972015-07-23 10:01:09 -0400148 WREG32_ENDPOINT(dig->pin->offset,
149 AZ_F0_CODEC_PIN_CONTROL_RESPONSE_LIPSYNC, tmp);
Alex Deucherb1880252013-10-10 18:03:06 -0400150}
151
Slava Grigorev00a9d4b2014-12-01 18:02:57 -0500152void dce6_afmt_hdmi_write_speaker_allocation(struct drm_encoder *encoder,
Alex Deucherd0ea3972015-07-23 10:01:09 -0400153 u8 *sadb, int sad_count)
Rafał Miłecki6159b652013-08-15 11:16:30 +0200154{
155 struct radeon_device *rdev = encoder->dev->dev_private;
156 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
157 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Alex Deucherd0ea3972015-07-23 10:01:09 -0400158 u32 tmp;
Rafał Miłecki6159b652013-08-15 11:16:30 +0200159
Alex Deucherd0ea3972015-07-23 10:01:09 -0400160 if (!dig || !dig->afmt || !dig->pin)
Rafał Miłecki6159b652013-08-15 11:16:30 +0200161 return;
162
Rafał Miłecki6159b652013-08-15 11:16:30 +0200163 /* program the speaker allocation */
Alex Deucherd0ea3972015-07-23 10:01:09 -0400164 tmp = RREG32_ENDPOINT(dig->pin->offset,
165 AZ_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER);
Rafał Miłecki6159b652013-08-15 11:16:30 +0200166 tmp &= ~(DP_CONNECTION | SPEAKER_ALLOCATION_MASK);
167 /* set HDMI mode */
168 tmp |= HDMI_CONNECTION;
169 if (sad_count)
170 tmp |= SPEAKER_ALLOCATION(sadb[0]);
171 else
172 tmp |= SPEAKER_ALLOCATION(5); /* stereo */
Alex Deucherd0ea3972015-07-23 10:01:09 -0400173 WREG32_ENDPOINT(dig->pin->offset,
174 AZ_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER, tmp);
Slava Grigorev00a9d4b2014-12-01 18:02:57 -0500175}
Rafał Miłecki6159b652013-08-15 11:16:30 +0200176
Slava Grigorev00a9d4b2014-12-01 18:02:57 -0500177void dce6_afmt_dp_write_speaker_allocation(struct drm_encoder *encoder,
Alex Deucherd0ea3972015-07-23 10:01:09 -0400178 u8 *sadb, int sad_count)
Slava Grigorev00a9d4b2014-12-01 18:02:57 -0500179{
180 struct radeon_device *rdev = encoder->dev->dev_private;
181 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
182 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Alex Deucherd0ea3972015-07-23 10:01:09 -0400183 u32 tmp;
Slava Grigorev00a9d4b2014-12-01 18:02:57 -0500184
Alex Deucherd0ea3972015-07-23 10:01:09 -0400185 if (!dig || !dig->afmt || !dig->pin)
Slava Grigorev00a9d4b2014-12-01 18:02:57 -0500186 return;
187
Slava Grigorev00a9d4b2014-12-01 18:02:57 -0500188 /* program the speaker allocation */
Alex Deucherd0ea3972015-07-23 10:01:09 -0400189 tmp = RREG32_ENDPOINT(dig->pin->offset,
190 AZ_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER);
Slava Grigorev00a9d4b2014-12-01 18:02:57 -0500191 tmp &= ~(HDMI_CONNECTION | SPEAKER_ALLOCATION_MASK);
192 /* set DP mode */
193 tmp |= DP_CONNECTION;
194 if (sad_count)
195 tmp |= SPEAKER_ALLOCATION(sadb[0]);
196 else
197 tmp |= SPEAKER_ALLOCATION(5); /* stereo */
Alex Deucherd0ea3972015-07-23 10:01:09 -0400198 WREG32_ENDPOINT(dig->pin->offset,
199 AZ_F0_CODEC_PIN_CONTROL_CHANNEL_SPEAKER, tmp);
Rafał Miłecki6159b652013-08-15 11:16:30 +0200200}
201
Alex Deucher070a2e62015-01-22 10:41:55 -0500202void dce6_afmt_write_sad_regs(struct drm_encoder *encoder,
Alex Deucherd0ea3972015-07-23 10:01:09 -0400203 struct cea_sad *sads, int sad_count)
Alex Deucherb5306022013-07-31 16:51:33 -0400204{
Alex Deucher070a2e62015-01-22 10:41:55 -0500205 int i;
Alex Deucherb5306022013-07-31 16:51:33 -0400206 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
207 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Alex Deucher070a2e62015-01-22 10:41:55 -0500208 struct radeon_device *rdev = encoder->dev->dev_private;
Alex Deucherb5306022013-07-31 16:51:33 -0400209 static const u16 eld_reg_to_type[][2] = {
210 { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR0, HDMI_AUDIO_CODING_TYPE_PCM },
211 { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR1, HDMI_AUDIO_CODING_TYPE_AC3 },
212 { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR2, HDMI_AUDIO_CODING_TYPE_MPEG1 },
213 { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR3, HDMI_AUDIO_CODING_TYPE_MP3 },
214 { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR4, HDMI_AUDIO_CODING_TYPE_MPEG2 },
215 { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR5, HDMI_AUDIO_CODING_TYPE_AAC_LC },
216 { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR6, HDMI_AUDIO_CODING_TYPE_DTS },
217 { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR7, HDMI_AUDIO_CODING_TYPE_ATRAC },
218 { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR9, HDMI_AUDIO_CODING_TYPE_EAC3 },
219 { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR10, HDMI_AUDIO_CODING_TYPE_DTS_HD },
220 { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR11, HDMI_AUDIO_CODING_TYPE_MLP },
221 { AZ_F0_CODEC_PIN_CONTROL_AUDIO_DESCRIPTOR13, HDMI_AUDIO_CODING_TYPE_WMA_PRO },
222 };
223
Alex Deucherd0ea3972015-07-23 10:01:09 -0400224 if (!dig || !dig->afmt || !dig->pin)
Alex Deucherb5306022013-07-31 16:51:33 -0400225 return;
226
Alex Deucherb5306022013-07-31 16:51:33 -0400227 for (i = 0; i < ARRAY_SIZE(eld_reg_to_type); i++) {
228 u32 value = 0;
Anssi Hannula0f57bca2013-10-29 01:19:16 +0200229 u8 stereo_freqs = 0;
230 int max_channels = -1;
Alex Deucherb5306022013-07-31 16:51:33 -0400231 int j;
232
233 for (j = 0; j < sad_count; j++) {
234 struct cea_sad *sad = &sads[j];
235
236 if (sad->format == eld_reg_to_type[i][1]) {
Anssi Hannula0f57bca2013-10-29 01:19:16 +0200237 if (sad->channels > max_channels) {
238 value = MAX_CHANNELS(sad->channels) |
239 DESCRIPTOR_BYTE_2(sad->byte2) |
240 SUPPORTED_FREQUENCIES(sad->freq);
241 max_channels = sad->channels;
242 }
243
Alex Deucherb5306022013-07-31 16:51:33 -0400244 if (sad->format == HDMI_AUDIO_CODING_TYPE_PCM)
Anssi Hannula0f57bca2013-10-29 01:19:16 +0200245 stereo_freqs |= sad->freq;
246 else
247 break;
Alex Deucherb5306022013-07-31 16:51:33 -0400248 }
249 }
Anssi Hannula0f57bca2013-10-29 01:19:16 +0200250
251 value |= SUPPORTED_FREQUENCIES_STEREO(stereo_freqs);
252
Alex Deucherd0ea3972015-07-23 10:01:09 -0400253 WREG32_ENDPOINT(dig->pin->offset, eld_reg_to_type[i][0], value);
Alex Deucherb5306022013-07-31 16:51:33 -0400254 }
Alex Deucherb5306022013-07-31 16:51:33 -0400255}
256
Alex Deucher832eafa2014-02-18 11:07:55 -0500257void dce6_audio_enable(struct radeon_device *rdev,
258 struct r600_audio_pin *pin,
Alex Deucherd3d8c142014-09-18 17:26:39 -0400259 u8 enable_mask)
Alex Deucherb5306022013-07-31 16:51:33 -0400260{
Alex Deucher832eafa2014-02-18 11:07:55 -0500261 if (!pin)
262 return;
263
Alex Deucherf68fdbe2014-09-18 16:19:04 -0400264 WREG32_ENDPOINT(pin->offset, AZ_F0_CODEC_PIN_CONTROL_HOT_PLUG_CONTROL,
Alex Deucherd3d8c142014-09-18 17:26:39 -0400265 enable_mask ? AUDIO_ENABLED : 0);
Alex Deucherb5306022013-07-31 16:51:33 -0400266}
Slava Grigoreva85d6822014-12-05 13:38:31 -0500267
268void dce6_hdmi_audio_set_dto(struct radeon_device *rdev,
Alex Deucherd0ea3972015-07-23 10:01:09 -0400269 struct radeon_crtc *crtc, unsigned int clock)
Slava Grigoreva85d6822014-12-05 13:38:31 -0500270{
Slava Grigorevb983a8f2015-03-02 11:31:07 -0500271 /* Two dtos; generally use dto0 for HDMI */
Slava Grigoreva85d6822014-12-05 13:38:31 -0500272 u32 value = 0;
273
Slava Grigorevb983a8f2015-03-02 11:31:07 -0500274 if (crtc)
Slava Grigoreva85d6822014-12-05 13:38:31 -0500275 value |= DCCG_AUDIO_DTO0_SOURCE_SEL(crtc->crtc_id);
276
277 WREG32(DCCG_AUDIO_DTO_SOURCE, value);
278
Slava Grigorevb983a8f2015-03-02 11:31:07 -0500279 /* Express [24MHz / target pixel clock] as an exact rational
280 * number (coefficient of two integer numbers. DCCG_AUDIO_DTOx_PHASE
281 * is the numerator, DCCG_AUDIO_DTOx_MODULE is the denominator
282 */
283 WREG32(DCCG_AUDIO_DTO0_PHASE, 24000);
284 WREG32(DCCG_AUDIO_DTO0_MODULE, clock);
Slava Grigoreva85d6822014-12-05 13:38:31 -0500285}
286
287void dce6_dp_audio_set_dto(struct radeon_device *rdev,
Alex Deucherd0ea3972015-07-23 10:01:09 -0400288 struct radeon_crtc *crtc, unsigned int clock)
Slava Grigoreva85d6822014-12-05 13:38:31 -0500289{
Slava Grigorevb983a8f2015-03-02 11:31:07 -0500290 /* Two dtos; generally use dto1 for DP */
Slava Grigoreva85d6822014-12-05 13:38:31 -0500291 u32 value = 0;
292 value |= DCCG_AUDIO_DTO_SEL;
293
Slava Grigorevb983a8f2015-03-02 11:31:07 -0500294 if (crtc)
Slava Grigoreva85d6822014-12-05 13:38:31 -0500295 value |= DCCG_AUDIO_DTO0_SOURCE_SEL(crtc->crtc_id);
296
297 WREG32(DCCG_AUDIO_DTO_SOURCE, value);
298
Slava Grigorevb983a8f2015-03-02 11:31:07 -0500299 /* Express [24MHz / target pixel clock] as an exact rational
300 * number (coefficient of two integer numbers. DCCG_AUDIO_DTOx_PHASE
301 * is the numerator, DCCG_AUDIO_DTOx_MODULE is the denominator
302 */
Slava Grigorev2afa3262015-03-02 12:05:29 -0500303 if (ASIC_IS_DCE8(rdev)) {
Slava Grigorevac4a9352015-12-17 11:09:58 -0500304 unsigned int div = (RREG32(DENTIST_DISPCLK_CNTL) &
305 DENTIST_DPREFCLK_WDIVIDER_MASK) >>
306 DENTIST_DPREFCLK_WDIVIDER_SHIFT;
307
308 if (div < 128 && div >= 96)
309 div -= 64;
310 else if (div >= 64)
311 div = div / 2 - 16;
312 else if (div >= 8)
313 div /= 4;
314 else
315 div = 0;
316
317 if (div)
318 clock = rdev->clock.gpupll_outputfreq * 10 / div;
319
Slava Grigorev2afa3262015-03-02 12:05:29 -0500320 WREG32(DCE8_DCCG_AUDIO_DTO1_PHASE, 24000);
321 WREG32(DCE8_DCCG_AUDIO_DTO1_MODULE, clock);
322 } else {
323 WREG32(DCCG_AUDIO_DTO1_PHASE, 24000);
324 WREG32(DCCG_AUDIO_DTO1_MODULE, clock);
325 }
Slava Grigoreva85d6822014-12-05 13:38:31 -0500326}